/* ====== Variables ====== */
:root {
  --bg: #f3f4f6;
  --bg-elevated: #ffffff;
  --bg-input: #f3f4f6;
  --bg-bubble-self: #3b82f6;
  --bg-bubble-other: #e5e7eb;
  --text: #111827;
  --text-secondary: #6b7280;
  --text-on-bubble: #ffffff;
  --text-on-bubble-other: #111827;
  --border: #e5e7eb;
  --primary: #3b82f6;
  --primary-hover: #2563eb;
  --primary-disabled: #93c5fd;
  --danger: #ef4444;
  --warning-bg: rgba(245, 158, 11, 0.15);
  --warning-text: #b45309;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #111827;
    --bg-elevated: #1f2937;
    --bg-input: #374151;
    --bg-bubble-self: #2563eb;
    --bg-bubble-other: #374151;
    --text: #f9fafb;
    --text-secondary: #9ca3af;
    --text-on-bubble: #ffffff;
    --text-on-bubble-other: #f9fafb;
    --border: #374151;
    --primary: #3b82f6;
    --primary-hover: #60a5fa;
    --primary-disabled: #1e40af;
    --danger: #f87171;
    --warning-bg: rgba(245, 158, 11, 0.2);
    --warning-text: #fbbf24;
  }
}

/* ====== Reset ====== */
*, *::before, *::after { box-sizing: border-box; }
* { margin: 0; padding: 0; -webkit-tap-highlight-color: transparent; }

html, body {
  height: 100%;
  font-family: system-ui, -apple-system, BlinkMacSystemFont, "Helvetica Neue", sans-serif;
  font-size: 14px;
  color: var(--text);
  background: var(--bg);
  overscroll-behavior: none;
  -webkit-text-size-adjust: 100%;
}

body { display: flex; flex-direction: column; }

#app {
  flex: 1;
  display: flex;
  flex-direction: column;
  min-height: 0;
}

button { font-family: inherit; }

/* ====== Login ====== */
.login {
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 16px;
  flex: 1;
}
.login-form {
  width: 100%;
  max-width: 280px;
  background: var(--bg-elevated);
  padding: 20px;
  border-radius: 8px;
  box-shadow: 0 1px 3px rgba(0,0,0,0.1);
}
.login-form h1 {
  font-size: 18px;
  text-align: center;
  margin-bottom: 12px;
  color: var(--text);
}
.login-input {
  width: 100%;
  padding: 8px 12px;
  border: 1px solid var(--border);
  background: var(--bg);
  color: var(--text);
  border-radius: 4px;
  font-size: 14px;
  margin-bottom: 8px;
  outline: none;
}
.login-input:focus { border-color: var(--primary); }
.login-btn {
  width: 100%;
  padding: 9px;
  background: var(--primary);
  color: white;
  border: none;
  border-radius: 4px;
  font-size: 14px;
  cursor: pointer;
}
.login-btn:hover:not(:disabled) { background: var(--primary-hover); }
.login-btn:disabled { background: var(--primary-disabled); cursor: not-allowed; }
.login-error {
  color: var(--danger);
  font-size: 12px;
  text-align: center;
  margin-top: 8px;
  min-height: 16px;
}

/* ====== Chat ====== */
.chat { display: flex; flex-direction: column; flex: 1; min-height: 0; }

.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  height: 28px;
  padding: 0 12px;
  background: var(--bg-elevated);
  border-bottom: 1px solid var(--border);
  font-size: 12px;
  flex-shrink: 0;
  gap: 8px;
}
.topbar-info { color: var(--text-secondary); flex: 1; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.topbar-info b { color: var(--text); font-weight: 600; }
.topbar-actions { display: flex; align-items: center; gap: 8px; }
.topbar-btn {
  background: none;
  border: none;
  color: var(--primary);
  cursor: pointer;
  font-size: 12px;
  padding: 0;
  white-space: nowrap;
}
.topbar-btn:hover { text-decoration: underline; }
.topbar-btn.danger { color: var(--danger); }

.conn-warning {
  background: var(--warning-bg);
  color: var(--warning-text);
  text-align: center;
  padding: 2px;
  font-size: 11px;
  flex-shrink: 0;
}

.messages {
  flex: 1;
  overflow-y: auto;
  padding: 4px 8px;
  -webkit-overflow-scrolling: touch;
}
.messages::-webkit-scrollbar { display: none; }
.messages { scrollbar-width: none; }

.loading-skel { padding: 8px; }
.skel-bubble {
  background: var(--bg-elevated);
  border-radius: 6px;
  height: 28px;
  margin-bottom: 8px;
  animation: pulse 1.5s infinite;
}
.skel-bubble.ml-auto { width: 50%; margin-left: auto; }
.skel-bubble.mr-auto { width: 33%; }
@keyframes pulse { 0%, 100% { opacity: 0.6; } 50% { opacity: 0.3; } }

.history-loading {
  text-align: center;
  font-size: 10px;
  color: var(--text-secondary);
  padding: 4px;
}

.message {
  display: flex;
  flex-direction: column;
  margin-bottom: 8px;
}
.message.self { align-items: flex-end; }
.message.other { align-items: flex-start; }

/* 选择模式：所有消息强制左对齐，message 变成 row（checkbox + 内容） */
.message.selecting {
  align-items: flex-start;
  flex-direction: row;
  gap: 8px;
}
/* 选择模式 + 自己消息：row-reverse 让 checkbox 在视觉上仍在左（"消息前面"） */
.message.selecting.self { flex-direction: row-reverse; }

/* 圆形 checkbox label，position relative 让 checkbox 居中 */
.msg-check {
  display: flex;
  align-items: flex-start;
  padding-top: 14px; /* 对齐 meta 行 */
  cursor: pointer;
  flex-shrink: 0;
  user-select: none;
  -webkit-user-select: none;
}
.msg-check input {
  position: absolute;
  opacity: 0;
  pointer-events: none;
  width: 0; height: 0;
}
.msg-check-box {
  display: inline-block;
  width: 18px; height: 18px;
  border: 2px solid var(--border);
  border-radius: 50%;
  background: var(--bg-elevated);
  position: relative;
  transition: background 0.15s, border-color 0.15s;
}
.msg-check input:checked + .msg-check-box {
  background: var(--primary);
  border-color: var(--primary);
}
.msg-check input:checked + .msg-check-box::after {
  content: '';
  position: absolute;
  left: 4px; top: 0px;
  width: 5px; height: 9px;
  border: solid white;
  border-width: 0 2px 2px 0;
  transform: rotate(45deg);
}
.msg-check:hover .msg-check-box { border-color: var(--primary); }

.message-meta {
  font-size: 11px;
  color: var(--text-secondary);
  padding: 0 4px;
}
.message-meta .pending { color: var(--text-secondary); margin-left: 4px; }
.message-meta .failed { color: var(--danger); margin-left: 4px; }

.message-text {
  margin-top: 2px;
  padding: 6px 12px;
  border-radius: 8px;
  max-width: 78vw;
  word-break: break-word;
  white-space: pre-wrap;
  font-size: 14px;
  line-height: 1.4;
}
.message.self .message-text {
  background: var(--bg-bubble-self);
  color: var(--text-on-bubble);
}
.message.other .message-text {
  background: var(--bg-bubble-other);
  color: var(--text-on-bubble-other);
}

.message-images {
  margin-top: 2px;
  display: grid;
  gap: 2px;
}
.message-images.cols-1 { grid-template-columns: 1fr; max-width: 240px; }
.message-images.cols-2 { grid-template-columns: 1fr 1fr; max-width: 240px; }
.message-images.cols-3 { grid-template-columns: 1fr 1fr 1fr; max-width: 240px; }

.image-cell {
  background: var(--bg-elevated);
  overflow: hidden;
  border-radius: 4px;
  cursor: pointer;
  width: 100%;
  min-width: 0;
}
/* 单图：按真实宽高比，最大宽 240px；比例由 inline style 设到 .image-cell */
.image-cell.cols-2, .image-cell.cols-3 { height: 120px; }
.image-cell.cols-3.image-count-5 { height: 80px; }
.image-cell img {
  width: 100%;
  height: 100%;
  object-fit: cover;
  display: block;
}

/* ====== Action bar ====== */
.actionbar {
  display: flex;
  align-items: flex-end;
  gap: 6px;
  padding: 4px 8px;
  padding-bottom: max(4px, env(safe-area-inset-bottom));
  background: var(--bg-elevated);
  border-top: 1px solid var(--border);
  flex-shrink: 0;
}

.btn-image {
  width: 40px;
  height: 40px;
  font-size: 20px;
  background: none;
  border: none;
  border-radius: 4px;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  flex-shrink: 0;
}
.btn-image:hover:not(:disabled) { background: var(--bg); }
.btn-image:disabled { opacity: 0.5; }
.btn-image.spinning { animation: spin 1s linear infinite; }
@keyframes spin { from { transform: rotate(0); } to { transform: rotate(360deg); } }

.actionbar textarea {
  flex: 1;
  min-height: 36px;
  max-height: 96px;
  padding: 9px 10px;
  background: var(--bg-input);
  color: var(--text);
  border: none;
  border-radius: 4px;
  font-size: 14px;
  font-family: inherit;
  resize: none;
  outline: none;
  overflow-y: auto;
}
.actionbar textarea::-webkit-scrollbar { display: none; }
.actionbar textarea { scrollbar-width: none; }

.btn-send {
  height: 40px;
  padding: 0 12px;
  background: var(--primary);
  color: white;
  font-size: 14px;
  font-weight: 500;
  border: none;
  border-radius: 4px;
  cursor: pointer;
  flex-shrink: 0;
}
.btn-send:hover:not(:disabled) { background: var(--primary-hover); }
.btn-send:disabled {
  background: var(--bg-bubble-other);
  color: var(--text-secondary);
  cursor: not-allowed;
}

/* ====== Image preview overlay ====== */
.preview {
  position: fixed;
  inset: 0;
  background: black;
  z-index: 9999;
  overflow: hidden;
  touch-action: none;
}
/* <picture> 不需要绝对定位，里面 <img> 自己定位 */
.preview picture {
  display: block;
  width: 100%;
  height: 100%;
}
.preview-img {
  position: absolute;
  top: 50%;
  left: 50%;
  max-width: none;
  user-select: none;
  -webkit-user-drag: none;
  -webkit-user-select: none;
  transform: translate(-50%, -50%) scale(1);
  transform-origin: center center;
  cursor: zoom-in;
  will-change: transform;
}
.preview-counter {
  position: absolute;
  top: 8px;
  right: 12px;
  background: rgba(0,0,0,0.5);
  color: white;
  padding: 2px 8px;
  border-radius: 4px;
  font-size: 13px;
  pointer-events: none;
  z-index: 2;
}
.preview-nav {
  position: absolute;
  top: 50%;
  transform: translateY(-50%);
  font-size: 36px;
  color: rgba(255,255,255,0.7);
  background: rgba(0,0,0,0.3);
  width: 48px;
  height: 64px;
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  border-radius: 4px;
  user-select: none;
  z-index: 2;
}
.preview-nav:hover { background: rgba(0,0,0,0.5); color: white; }
.preview-prev { left: 8px; }
.preview-next { right: 8px; }