/* 移动端优先。主要用手机看，所以先写窄屏，宽屏只是加点留白。 */

:root {
  --bg: #0b0d12;
  --bg-soft: #12151d;
  --bg-share: #16221c;      /* 主动分享：偏绿的底色，一眼看出"这条是它自己想说的" */
  --line: #232838;
  --fg: #e8ebf2;
  --fg-dim: #8b93a7;
  --ai: #1b2130;
  --user: #2a5bd7;
  --accent: #4b7bff;
  --danger: #ff6b6b;
}

@media (prefers-color-scheme: light) {
  :root {
    --bg: #f5f6f9;
    --bg-soft: #ffffff;
    --bg-share: #eefaf1;
    --line: #e2e5ee;
    --fg: #1b1f2a;
    --fg-dim: #6b7280;
    --ai: #ffffff;
    --user: #2a5bd7;
    --accent: #2a5bd7;
  }
}

* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }

/* ⚠️ 必须有这条！
   HTML 的 hidden 属性靠浏览器默认样式表的 `[hidden] { display: none }` 生效，
   但它的优先级**低于**任何作者写的类选择器。所以只要 CSS 里给元素写了
   `display: flex`（设置面板就是），hidden 属性就完全失效 ——
   面板会在每次打开页面时露在屏幕底部，看起来像个莫名其妙的灰条 + 按钮。
   用 !important 把 hidden 拉回"一定不显示"。 */
[hidden] { display: none !important; }

html, body {
  margin: 0;
  padding: 0;
  background: var(--bg);
  color: var(--fg);
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", "PingFang SC",
               "Hiragino Sans GB", "Microsoft YaHei", sans-serif;
  font-size: 16px;              /* ⚠️ 必须 >=16px，否则 iOS 会自动放大页面 */
  line-height: 1.6;
  overscroll-behavior-y: none;
}

/* ⚠️ 用 dvh 而不是 vh，避免手机浏览器地址栏遮挡输入框 */
.chat-container {
  height: 100dvh;
  display: flex;
  flex-direction: column;
}

/* ── 顶栏 ── */
.topbar {
  flex: 0 0 auto;
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 10px 14px;
  padding-top: calc(10px + env(safe-area-inset-top));
  background: var(--bg-soft);
  border-bottom: 1px solid var(--line);
}

.who { display: flex; align-items: center; gap: 10px; min-width: 0; }

.avatar {
  flex: 0 0 auto;
  width: 34px; height: 34px;
  border-radius: 50%;
  background: linear-gradient(135deg, var(--accent), #8b5cf6);
  color: #fff;
  font-size: 13px; font-weight: 700;
  display: flex; align-items: center; justify-content: center;
}

.who-text { min-width: 0; }
.who-name { font-weight: 600; font-size: 15px; }
.who-status { font-size: 12px; color: var(--fg-dim); }
.who-status.live::before { content: "● "; color: #2ecc71; }
.who-status.dead::before { content: "● "; color: var(--danger); }
.who-status.wait::before { content: "● "; color: #f1c40f; }

.ghost-btn {
  background: transparent;
  border: 1px solid var(--line);
  color: var(--fg-dim);
  border-radius: 10px;
  width: 36px; height: 32px;
  font-size: 15px;
  cursor: pointer;
}
.ghost-btn:active { background: var(--line); }
.ghost-btn.wide { width: auto; padding: 0 16px; height: 40px; font-size: 15px; }

/* ⚠️ 顶栏按钮必须带文字。
 *    之前只有一个 ⚙ / ↓ 光秃秃的图标 —— 用户反馈"点了没反应"，
 *    其实一半原因是压根看不出来它能点、也不知道是干嘛的。
 *    图标 + 短文字，点击目标也更大。 */
.ghost-btn:has(.ghost-txt) {
  width: auto;
  padding: 0 10px;
  display: inline-flex;
  align-items: center;
  gap: 4px;
}
.ghost-ico { font-size: 15px; line-height: 1; }
.ghost-txt { font-size: 12px; line-height: 1; white-space: nowrap; }

.topbar-actions { display: flex; gap: 6px; align-items: center; }

/* ── 提示条 ── */
.notice {
  flex: 0 0 auto;
  margin: 8px 12px 0;
  padding: 9px 12px;
  border-radius: 10px;
  background: #3a2a12;
  border: 1px solid #6b4a15;
  color: #ffd88a;
  font-size: 13px;
  line-height: 1.5;
}
@media (prefers-color-scheme: light) {
  .notice { background: #fff6e0; border-color: #f0d69a; color: #7a5a12; }
}

/* ── 消息区 ── */
.messages {
  flex: 1 1 auto;
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
  padding: 14px 12px 18px;
  display: flex;
  flex-direction: column;
  gap: 10px;
}

.empty {
  margin: auto;
  text-align: center;
  color: var(--fg-dim);
  padding: 20px;
}
.empty-emoji { font-size: 34px; margin-bottom: 6px; }
.empty p { margin: 4px 0; font-size: 14px; }
.empty .dim { font-size: 13px; opacity: .75; }

.row { display: flex; }
.row.user { justify-content: flex-end; }
.row.ai   { justify-content: flex-start; }

.bubble {
  max-width: 82%;
  padding: 9px 13px;
  border-radius: 16px;
  background: var(--ai);
  border: 1px solid var(--line);
  white-space: pre-wrap;
  word-break: break-word;
  overflow-wrap: anywhere;
  font-size: 15px;
  animation: pop .16s ease-out;
}

@keyframes pop {
  from { opacity: 0; transform: translateY(4px); }
  to   { opacity: 1; transform: none; }
}

.row.user .bubble {
  background: var(--user);
  border-color: transparent;
  color: #fff;
  border-bottom-right-radius: 5px;
}

.row.ai .bubble { border-bottom-left-radius: 5px; }

/* ⭐ 主动分享：不同底色 + 左边框，一眼看出"这条是它自己想说的" */
.row.ai.share .bubble {
  background: var(--bg-share);
  border-left: 3px solid #2ecc71;
}
.row.ai.share .bubble::before {
  content: "主动分享";
  display: block;
  font-size: 11px;
  color: #2ecc71;
  margin-bottom: 3px;
  letter-spacing: .5px;
}

.bubble a { color: var(--accent); }
.row.user .bubble a { color: #dfe8ff; }

.bubble.error {
  border-color: var(--danger);
  color: var(--danger);
}

.bubble .caret {
  display: inline-block;
  width: 7px;
  height: 15px;
  margin-left: 2px;
  vertical-align: -2px;
  background: var(--fg-dim);
  animation: blink 1s steps(1) infinite;
}
@keyframes blink { 50% { opacity: 0; } }

.time {
  font-size: 11px;
  color: var(--fg-dim);
  align-self: flex-end;
  margin: 0 4px 2px;
}

/* ─────────────────────────────────────────────
 * 初次上手卡片（没配 Key 时直接摆在消息区）
 * ───────────────────────────────────────────── */
.setup-card {
  flex: 0 0 auto;
  margin: 4px 0 10px;
  padding: 16px;
  border: 1px solid var(--accent);
  border-radius: 16px;
  background: var(--bg-soft);
}
@media (prefers-color-scheme: dark) {
  .setup-card { background: #141a2a; }
}

.setup-title { margin: 0 0 8px; font-size: 16px; font-weight: 600; }

.setup-desc {
  margin: 0 0 14px;
  font-size: 13px;
  line-height: 1.7;
  color: var(--fg-dim);
}
.setup-desc a { color: var(--accent); }

.setup-row { display: flex; gap: 8px; }

.setup-row input {
  flex: 1 1 auto;
  min-width: 0;
  border: 1px solid var(--line);
  background: var(--bg);
  color: var(--fg);
  border-radius: 12px;
  padding: 11px 14px;
  font-size: 16px;          /* ⚠️ >=16px，否则 iOS 会自动放大页面 */
  font-family: ui-monospace, Consolas, monospace;
  outline: none;
}
.setup-row input:focus { border-color: var(--accent); }
.setup-row .send-btn { border-radius: 12px; padding: 10px 20px; }

.setup-actions { display: flex; gap: 8px; margin-top: 10px; }
.setup-actions .ghost-btn { flex: 1 1 0; height: 38px; font-size: 14px; }

.setup-msg {
  margin: 10px 0 0;
  padding: 9px 12px;
  border-radius: 10px;
  font-size: 13px;
  line-height: 1.5;
  word-break: break-word;
}
.setup-msg.ok { background: #16301f; border: 1px solid #2ecc71; color: #8ef0b4; }
.setup-msg.err { background: #331a1a; border: 1px solid var(--danger); color: #ffb0b0; }
@media (prefers-color-scheme: light) {
  .setup-msg.ok { background: #eafaf0; color: #1a7f45; }
  .setup-msg.err { background: #fdeceb; color: #b3261e; }
}

.setup-hint {
  margin: 12px 0 0;
  font-size: 12px;
  color: var(--fg-dim);
  line-height: 1.6;
}
.setup-hint code {
  background: var(--bg);
  border: 1px solid var(--line);
  border-radius: 4px;
  padding: 0 4px;
  font-size: 11px;
}

/* ── 输入区 ── */
.composer {
  flex: 0 0 auto;
  display: flex;
  gap: 8px;
  align-items: flex-end;
  padding: 10px 12px;
  padding-bottom: calc(10px + env(safe-area-inset-bottom));
  background: var(--bg-soft);
  border-top: 1px solid var(--line);
}

.composer textarea {
  flex: 1 1 auto;
  resize: none;
  border: 1px solid var(--line);
  background: var(--bg);
  color: var(--fg);
  border-radius: 18px;
  padding: 9px 14px;
  font-size: 16px;            /* ⚠️ 同上，防止 iOS 缩放 */
  font-family: inherit;
  line-height: 1.5;
  max-height: 132px;
  outline: none;
}
.composer textarea:focus { border-color: var(--accent); }

.send-btn {
  flex: 0 0 auto;
  border: none;
  background: var(--accent);
  color: #fff;
  font-size: 15px;
  font-weight: 600;
  padding: 10px 18px;
  border-radius: 18px;
  cursor: pointer;
  font-family: inherit;
}
.send-btn:disabled { opacity: .45; cursor: default; }

/* ─────────────────────────────────────────────
 * 设置面板
 * ───────────────────────────────────────────── */
.sheet-backdrop {
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, .5);
  z-index: 50;
  animation: fade .15s ease-out;
}
@keyframes fade { from { opacity: 0; } to { opacity: 1; } }

.sheet {
  position: fixed;
  z-index: 51;
  left: 50%;
  bottom: 0;
  transform: translateX(-50%);
  width: 100%;
  max-width: 560px;
  max-height: 92dvh;
  display: flex;
  flex-direction: column;
  background: var(--bg-soft);
  border: 1px solid var(--line);
  border-radius: 18px 18px 0 0;
  box-shadow: 0 -8px 40px rgba(0, 0, 0, .35);
  animation: rise .2s ease-out;
}
@keyframes rise { from { transform: translate(-50%, 24px); opacity: 0; } }

/* 宽屏上做成居中的卡片，别贴着屏幕底边 */
@media (min-width: 640px) {
  .sheet {
    bottom: auto;
    top: 50%;
    transform: translate(-50%, -50%);
    border-radius: 18px;
    max-height: 86dvh;
    animation: pop-in .18s ease-out;
  }
  @keyframes pop-in { from { transform: translate(-50%, -46%); opacity: 0; } }
}

.sheet-head {
  flex: 0 0 auto;
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 14px 16px 10px;
  border-bottom: 1px solid var(--line);
}
.sheet-head h2 { margin: 0; font-size: 16px; font-weight: 600; }

/* 分页标签：账号 / 偏好 / 全局 */
.sheet-tabs {
  flex: 0 0 auto;
  display: flex;
  gap: 4px;
  padding: 8px 12px 0;
  border-bottom: 1px solid var(--line);
}
.sheet-tabs .tab {
  flex: 1 1 0;
  background: transparent;
  border: none;
  border-bottom: 2px solid transparent;
  color: var(--fg-dim);
  font-family: inherit;
  font-size: 14px;
  padding: 8px 4px 10px;
  cursor: pointer;
}
.sheet-tabs .tab.active {
  color: var(--accent);
  border-bottom-color: var(--accent);
  font-weight: 600;
}

.sheet-section { margin: 14px 0 4px; }
.sheet-section h3 { margin: 0 0 4px; font-size: 14px; font-weight: 600; }
.sheet-section p {
  margin: 0;
  font-size: 12px;
  color: var(--fg-dim);
  line-height: 1.6;
}
.sheet-section code {
  background: var(--bg);
  border: 1px solid var(--line);
  border-radius: 4px;
  padding: 0 4px;
  font-size: 11px;
}

.sheet-body {
  flex: 1 1 auto;
  overflow-y: auto;
  padding: 6px 16px 14px;
  -webkit-overflow-scrolling: touch;
}

.sheet-loading { color: var(--fg-dim); font-size: 14px; text-align: center; padding: 24px 0; }

.field {
  padding: 12px 0;
  border-bottom: 1px solid var(--line);
}
.field:last-child { border-bottom: none; }

.field-head {
  display: flex;
  align-items: baseline;
  justify-content: space-between;
  gap: 8px;
  margin-bottom: 6px;
}
.field-label { font-size: 14px; font-weight: 500; }

.field-tags { display: flex; gap: 6px; flex: 0 0 auto; }
.tag {
  font-size: 11px;
  padding: 1px 7px;
  border-radius: 20px;
  border: 1px solid var(--line);
  color: var(--fg-dim);
  white-space: nowrap;
}
.tag.override { border-color: var(--accent); color: var(--accent); }
.tag.restart { border-color: #d9a441; color: #d9a441; }

.field input[type="text"],
.field input[type="password"],
.field input[type="number"],
.field textarea,
.field select {
  width: 100%;
  border: 1px solid var(--line);
  background: var(--bg);
  color: var(--fg);
  border-radius: 10px;
  padding: 9px 12px;
  font-size: 16px;          /* 同上，防 iOS 缩放 */
  font-family: inherit;
  outline: none;
}
.field textarea {
  resize: vertical;
  min-height: 108px;
  line-height: 1.6;
  font-size: 14px;          /* 中文长段落，14px 比 16px 好读 */
}
.field input:focus, .field select:focus, .field textarea:focus { border-color: var(--accent); }

/* 提示词的"看默认内容"折叠块 */
.default-box {
  margin-top: 6px;
  font-size: 12px;
}
.default-box summary {
  cursor: pointer;
  color: var(--fg-dim);
  padding: 3px 0;
}
.default-box pre {
  margin: 6px 0 0;
  padding: 9px 11px;
  max-height: 240px;
  overflow: auto;
  background: var(--bg);
  border: 1px solid var(--line);
  border-radius: 10px;
  font-family: inherit;
  font-size: 12px;
  line-height: 1.6;
  white-space: pre-wrap;
  word-break: break-word;
  color: var(--fg-dim);
}
.field input.masked { letter-spacing: 1px; font-family: ui-monospace, Consolas, monospace; }

.field-row { display: flex; gap: 8px; align-items: center; }
.field-row .reset-btn {
  flex: 0 0 auto;
  background: transparent;
  border: 1px solid var(--line);
  color: var(--fg-dim);
  border-radius: 10px;
  padding: 9px 12px;
  font-size: 13px;
  cursor: pointer;
  font-family: inherit;
}
.field-row .reset-btn:disabled { opacity: .35; cursor: default; }

.field-check { display: flex; align-items: center; gap: 10px; }
.field-check input { width: 20px; height: 20px; accent-color: var(--accent); }

.field-hint { font-size: 12px; color: var(--fg-dim); margin-top: 5px; line-height: 1.5; }

.sheet-foot {
  flex: 0 0 auto;
  display: flex;
  gap: 8px;
  padding: 12px 16px;
  padding-bottom: calc(12px + env(safe-area-inset-bottom));
  border-top: 1px solid var(--line);
}
.sheet-foot .send-btn { flex: 1 1 auto; border-radius: 12px; }

.sheet-msg {
  flex: 0 0 auto;
  margin: 0 16px;
  padding: 9px 12px;
  border-radius: 10px;
  font-size: 13px;
  line-height: 1.5;
  word-break: break-word;
}
.sheet-msg.ok { background: #16301f; border: 1px solid #2ecc71; color: #8ef0b4; }
.sheet-msg.err { background: #331a1a; border: 1px solid var(--danger); color: #ffb0b0; }
@media (prefers-color-scheme: light) {
  .sheet-msg.ok { background: #eafaf0; color: #1a7f45; }
  .sheet-msg.err { background: #fdeceb; color: #b3261e; }
}

.sheet-note {
  flex: 0 0 auto;
  margin: 0;
  padding: 0 16px calc(14px + env(safe-area-inset-bottom));
  font-size: 12px;
  color: var(--fg-dim);
  line-height: 1.6;
}
.sheet-note code {
  background: var(--bg);
  border: 1px solid var(--line);
  border-radius: 4px;
  padding: 0 4px;
  font-size: 11px;
}

/* ─────────────────────────────────────────────
 * 消息删除按钮
 *
 * 桌面：悬停（或键盘 Tab 聚焦）才出现，平时不干扰阅读
 * 手机：长按气泡出现 —— 手机上压根没有 hover
 * ───────────────────────────────────────────── */
.row { position: relative; align-items: center; gap: 4px; }

.del-btn {
  flex: 0 0 auto;
  width: 26px;
  height: 26px;
  padding: 0;
  border: 1px solid var(--line);
  border-radius: 50%;
  background: var(--bg-soft);
  color: var(--fg-dim);
  font-size: 13px;
  line-height: 1;
  cursor: pointer;
  opacity: 0;
  pointer-events: none;
  transition: opacity .12s;
}
.row:hover .del-btn,
.del-btn:focus-visible { opacity: 1; pointer-events: auto; }

/* 只读的老消息和正在流式输出的那条不给删：
   流式那条还没有真实 id，删了也对不上 */
.row[data-pending="1"] .del-btn,
.row.no-delete .del-btn { display: none; }

@media (hover: none) {
  /* 触屏：靠长按显形（见 app.js），所以不能只靠 :hover */
  .del-btn { opacity: 0; }
  .row.show-actions .del-btn { opacity: 1; pointer-events: auto; }
}

.del-btn:active { background: var(--line); }

.danger-btn {
  background: transparent;
  border: 1px solid var(--danger);
  color: var(--danger);
  border-radius: 10px;
  padding: 0 16px;
  height: 40px;
  font-family: inherit;
  font-size: 15px;
  cursor: pointer;
}
.danger-btn:active { background: rgba(231, 76, 60, .15); }
.danger-btn:disabled { opacity: .45; cursor: default; }

/* ─────────────────────────────────────────────
 * 确认弹窗
 *
 * ⚠️ 用 flex + [hidden] 而不是 display:none 直接写死：
 *    全局那条 `[hidden] { display: none !important }` 会赢，
 *    所以只要不写 display:flex 到 .confirm 上就不会有坑。
 *    （之前设置面板就是栽在这个"hidden 被 display:flex 覆盖"上。）
 * ───────────────────────────────────────────── */
/* ⚠️ 必须定义在全局：`pop-in` 那个在 @media (min-width: 640px) 里面，
 *    小屏手机上根本取不到，动画会静默失效。 */
@keyframes confirm-in {
  from { opacity: 0; transform: translate(-50%, -50%) scale(.96); }
  to   { opacity: 1; transform: translate(-50%, -50%) scale(1); }
}

.confirm-backdrop {
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, .55);
  z-index: 60;
  animation: fade .15s ease-out;
}

.confirm {
  position: fixed;
  z-index: 61;
  left: 50%;
  top: 50%;
  transform: translate(-50%, -50%);
  width: calc(100% - 40px);
  max-width: 380px;
  background: var(--bg-soft);
  border: 1px solid var(--line);
  border-radius: 16px;
  padding: 18px 18px 14px;
  box-shadow: 0 12px 48px rgba(0, 0, 0, .5);
  animation: confirm-in .16s ease-out;
}
.confirm h3 {
  margin: 0 0 8px;
  font-size: 16px;
  font-weight: 600;
}
.confirm p {
  margin: 0 0 10px;
  font-size: 14px;
  line-height: 1.6;
  color: var(--fg-dim);
}
.confirm-preview {
  margin: 0 0 12px;
  padding: 8px 10px;
  max-height: 120px;
  overflow: auto;
  background: var(--bg);
  border: 1px solid var(--line);
  border-radius: 10px;
  font-family: inherit;
  font-size: 13px;
  line-height: 1.5;
  white-space: pre-wrap;
  word-break: break-word;
}
.confirm-foot {
  display: flex;
  gap: 8px;
  justify-content: flex-end;
  align-items: center;
}
.confirm-foot .ghost-btn.wide { flex: 0 0 auto; }

/* ─────────────────────────────────────────────
 * 登录页
 * ───────────────────────────────────────────── */
.login-body {
  display: flex;
  align-items: center;
  justify-content: center;
  min-height: 100dvh;
  padding: 24px;
  padding-bottom: calc(24px + env(safe-area-inset-bottom));
}

.login-card {
  width: 100%;
  max-width: 360px;
  background: var(--bg-soft);
  border: 1px solid var(--line);
  border-radius: 18px;
  padding: 28px 22px;
  text-align: center;
}

.login-avatar {
  width: 52px; height: 52px;
  margin: 0 auto 14px;
  border-radius: 50%;
  background: linear-gradient(135deg, var(--accent), #8b5cf6);
  color: #fff;
  font-weight: 700;
  font-size: 18px;
  display: flex; align-items: center; justify-content: center;
}

.login-title { margin: 0 0 4px; font-size: 19px; font-weight: 600; }
.login-sub { margin: 0 0 20px; font-size: 14px; color: var(--fg-dim); }

.login-card form { display: flex; flex-direction: column; gap: 10px; }

.login-card input[type="password"],
.login-card input[type="text"] {
  width: 100%;
  border: 1px solid var(--line);
  background: var(--bg);
  color: var(--fg);
  border-radius: 12px;
  padding: 12px 14px;
  font-size: 16px;          /* 同上，防 iOS 缩放 */
  font-family: inherit;
  outline: none;
  text-align: center;
}
.login-card input[type="password"]:focus,
.login-card input[type="text"]:focus { border-color: var(--accent); }

.login-card button[type="submit"] {
  border: none;
  background: var(--accent);
  color: #fff;
  font-size: 16px;
  font-weight: 600;
  padding: 12px;
  border-radius: 12px;
  cursor: pointer;
  font-family: inherit;
}
.login-card button[type="submit"]:disabled { opacity: .5; cursor: default; }

.login-error {
  margin: 14px 0 0;
  padding: 9px 12px;
  border-radius: 10px;
  background: #331a1a;
  border: 1px solid var(--danger);
  color: #ffb0b0;
  font-size: 13px;
}
@media (prefers-color-scheme: light) {
  .login-error { background: #fdeceb; color: #b3261e; }
}

.login-hint {
  margin: 20px 0 0;
  font-size: 12px;
  color: var(--fg-dim);
  line-height: 1.7;
  text-align: left;
}
.login-hint code {
  background: var(--bg);
  border: 1px solid var(--line);
  border-radius: 4px;
  padding: 0 4px;
  font-size: 11px;
}

/* 登录卡底部的小字声明：说明这是个人学习项目，不是商业产品。
   目的就一个 —— 别让访客误以为这是个对外服务。 */
.login-note {
  margin: 16px 0 0;
  font-size: 11px;
  color: var(--fg-dim);
  opacity: .75;
  line-height: 1.6;
  text-align: center;
}

