:root {
  --bg: #f7f7f5;
  --panel: #ffffff;
  --side: #efefec;
  --text: #1d1d1f;
  --muted: #6e6e73;
  --line: #deded9;
  --accent: #2f6fed;
  --accent-text: #ffffff;
  --user-bg: #e6edfb;
  --code-bg: #f0f0ed;
  --danger: #c62828;
  --radius: 14px;
  --side-w: 270px;
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #17181a;
    --panel: #202124;
    --side: #1c1d20;
    --text: #ececec;
    --muted: #9a9aa0;
    --line: #34353a;
    --accent: #5b8cff;
    --user-bg: #2a3350;
    --code-bg: #2a2b2f;
    --danger: #ef5350;
    color-scheme: dark;
  }
}
:root[data-theme="dark"] {
  --bg: #17181a; --panel: #202124; --side: #1c1d20; --text: #ececec; --muted: #9a9aa0;
  --line: #34353a; --accent: #5b8cff; --user-bg: #2a3350; --code-bg: #2a2b2f; --danger: #ef5350;
  color-scheme: dark;
}

* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; }
body {
  background: var(--bg);
  color: var(--text);
  font: 16px/1.6 -apple-system, BlinkMacSystemFont, "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", system-ui, sans-serif;
  -webkit-text-size-adjust: 100%;
}
button { font: inherit; color: inherit; cursor: pointer; }
input, textarea { font: inherit; color: inherit; }

#app { display: flex; height: 100dvh; overflow: hidden; }

/* ---------- 侧栏 ---------- */
#sidebar {
  width: var(--side-w);
  flex-shrink: 0;
  background: var(--side);
  border-right: 1px solid var(--line);
  display: flex; flex-direction: column;
  padding-top: env(safe-area-inset-top);
  padding-left: env(safe-area-inset-left);
}
.side-head, .side-foot { padding: 12px; }
.side-foot { padding-bottom: calc(12px + env(safe-area-inset-bottom)); border-top: 1px solid var(--line); }
#conv-list { flex: 1; overflow-y: auto; padding: 0 8px; }
.conv {
  display: flex; align-items: center; gap: 4px;
  padding: 8px 10px; margin: 2px 0;
  border-radius: 10px; cursor: pointer;
  color: var(--text);
}
.conv:hover { background: color-mix(in srgb, var(--text) 6%, transparent); }
.conv.active { background: color-mix(in srgb, var(--accent) 16%, transparent); }
.conv .t { flex: 1; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; font-size: 14px; }
.conv .x { opacity: 0; border: 0; background: none; padding: 2px 6px; border-radius: 6px; color: var(--muted); }
.conv:hover .x, .conv.active .x { opacity: 1; }
.conv .x:hover { color: var(--danger); }
.conv-group { font-size: 12px; color: var(--muted); padding: 12px 10px 4px; }
#overlay { position: fixed; inset: 0; background: rgb(0 0 0 / .35); z-index: 9; }

/* ---------- 主区域 ---------- */
#main { flex: 1; display: flex; flex-direction: column; min-width: 0; }
#topbar {
  display: flex; align-items: center; gap: 8px;
  padding: calc(8px + env(safe-area-inset-top)) 16px 8px;
  border-bottom: 1px solid var(--line);
  background: var(--bg);
}
.top-title { flex: 1; min-width: 0; text-align: center; }
#chat-title { font-weight: 600; font-size: 15px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.chip { border: 0; background: none; color: var(--muted); font-size: 12px; padding: 0 6px; }
.chip:hover { color: var(--accent); }
.icon-btn {
  width: 36px; height: 36px; border-radius: 10px; border: 0;
  background: none; font-size: 20px; line-height: 1;
}
.icon-btn:hover { background: color-mix(in srgb, var(--text) 8%, transparent); }
#toggle-side { visibility: hidden; }

#messages { flex: 1; overflow-y: auto; padding: 20px 16px 8px; overscroll-behavior: contain; }
.msg { max-width: 760px; margin: 0 auto 18px; display: flex; flex-direction: column; }
.msg.user { align-items: flex-end; }
.msg .bubble { max-width: 100%; overflow-wrap: anywhere; }
.msg.user .bubble {
  background: var(--user-bg); padding: 10px 14px; border-radius: var(--radius);
  border-bottom-right-radius: 4px; white-space: pre-wrap; max-width: 85%;
}
.msg.assistant .bubble { width: 100%; }
.msg .actions { display: flex; gap: 2px; margin-top: 4px; opacity: .55; flex-wrap: wrap; align-items: center; }
.msg:hover .actions, .msg:last-child .actions { opacity: 1; }
.msg .actions button { border: 0; background: none; color: var(--muted); font-size: 13px; padding: 2px 8px; border-radius: 6px; }
.msg .actions button:hover { background: color-mix(in srgb, var(--text) 8%, transparent); color: var(--text); }
.msg .meta { font-size: 12px; color: var(--muted); margin-left: 6px; }
.msg .error { color: var(--danger); background: color-mix(in srgb, var(--danger) 10%, transparent); padding: 8px 12px; border-radius: 10px; font-size: 14px; white-space: pre-wrap; }

details.reasoning { margin-bottom: 8px; border-left: 3px solid var(--line); padding-left: 10px; color: var(--muted); font-size: 14px; }
details.reasoning summary { cursor: pointer; user-select: none; }
details.reasoning .r-body { white-space: pre-wrap; margin-top: 4px; }

.md > :first-child { margin-top: 0; }
.md > :last-child { margin-bottom: 0; }
.md p { margin: 0 0 .8em; }
.md pre { background: var(--code-bg); padding: 12px; border-radius: 10px; overflow-x: auto; font-size: 13.5px; line-height: 1.5; position: relative; }
.md code { font-family: ui-monospace, "SF Mono", Menlo, monospace; font-size: .9em; }
.md :not(pre) > code { background: var(--code-bg); padding: 1px 5px; border-radius: 5px; }
.md table { border-collapse: collapse; display: block; overflow-x: auto; }
.md th, .md td { border: 1px solid var(--line); padding: 4px 10px; }
.md blockquote { margin: 0 0 .8em; padding-left: 12px; border-left: 3px solid var(--line); color: var(--muted); }
.md a { color: var(--accent); }
.md img { max-width: 100%; }
.copy-code { position: absolute; top: 6px; right: 6px; font-size: 12px; border: 1px solid var(--line); background: var(--panel); border-radius: 6px; padding: 1px 8px; opacity: .8; }

.cursor::after { content: "▍"; animation: blink 1s steps(2) infinite; color: var(--accent); }
@keyframes blink { 50% { opacity: 0; } }
.thinking { color: var(--muted); font-size: 14px; }

.empty { max-width: 560px; margin: 12vh auto 0; text-align: center; color: var(--muted); }
.empty h1 { color: var(--text); font-size: 26px; margin: 0 0 8px; }
.empty .setup { margin-top: 18px; }

/* ---------- 输入框 ---------- */
#composer { padding: 8px 16px calc(10px + env(safe-area-inset-bottom)); }
.composer-box {
  max-width: 760px; margin: 0 auto;
  display: flex; align-items: flex-end; gap: 8px;
  background: var(--panel); border: 1px solid var(--line); border-radius: 18px;
  padding: 6px 6px 6px 14px;
}
.composer-box:focus-within { border-color: var(--accent); }
#input {
  flex: 1; border: 0; outline: 0; background: none; resize: none;
  max-height: 40vh; padding: 7px 0; line-height: 1.5;
}
.send-btn {
  width: 38px; height: 38px; flex-shrink: 0; border-radius: 50%; border: 0;
  background: var(--accent); color: var(--accent-text); font-size: 20px; font-weight: 700;
}
.send-btn:disabled { opacity: .35; cursor: default; }
.send-btn.stop { background: var(--text); color: var(--bg); font-size: 14px; }
.composer-hint { max-width: 760px; margin: 4px auto 0; font-size: 12px; color: var(--muted); text-align: center; min-height: 1em; }

/* ---------- 按钮 ---------- */
.btn-primary, .btn-ghost, .btn-danger { border-radius: 10px; padding: 8px 14px; border: 1px solid transparent; }
.btn-primary { background: var(--accent); color: var(--accent-text); }
.btn-ghost { background: none; border-color: var(--line); }
.btn-ghost:hover { background: color-mix(in srgb, var(--text) 6%, transparent); }
.btn-danger { background: none; border-color: var(--danger); color: var(--danger); }
.wide { width: 100%; }

/* ---------- 设置 ---------- */
dialog {
  border: 1px solid var(--line); border-radius: 16px; padding: 0;
  background: var(--panel); color: var(--text);
  width: min(520px, calc(100vw - 32px)); max-height: calc(100dvh - 32px);
}
dialog::backdrop { background: rgb(0 0 0 / .4); }
#settings-form { padding: 20px; display: flex; flex-direction: column; gap: 12px; }
#settings-form h2 { margin: 0; font-size: 20px; }
#settings-form label { display: flex; flex-direction: column; gap: 4px; font-size: 14px; font-weight: 500; }
#settings-form input:not([type=checkbox]):not([type=range]), #settings-form textarea {
  border: 1px solid var(--line); border-radius: 10px; padding: 8px 10px;
  background: var(--bg); font-weight: 400; width: 100%;
}
#settings-form textarea { resize: vertical; }
#settings-form label.check { flex-direction: row; align-items: center; gap: 8px; font-weight: 400; }
.row { display: flex; gap: 8px; flex-wrap: wrap; }
.row > input { flex: 1; min-width: 0; }
.grid2 { display: grid; grid-template-columns: 1fr 1fr; gap: 12px; }
.note { margin: -6px 0 0; font-size: 12px; color: var(--muted); }
.note a { color: var(--accent); }
.adv summary { cursor: pointer; color: var(--muted); font-size: 14px; }
.adv[open] { display: flex; flex-direction: column; gap: 12px; }
.dialog-actions { display: flex; justify-content: flex-end; gap: 8px; margin-top: 4px; }

/* ---------- 手机 ---------- */
@media (max-width: 760px) {
  #sidebar {
    position: fixed; z-index: 10; top: 0; bottom: 0; left: 0;
    transform: translateX(-100%); transition: transform .22s ease;
    box-shadow: 0 0 24px rgb(0 0 0 / .2);
  }
  #app.side-open #sidebar { transform: none; }
  #toggle-side { visibility: visible; }
  #messages { padding: 14px 12px 6px; }
  #composer { padding-left: 10px; padding-right: 10px; }
  .msg.user .bubble { max-width: 90%; }
  .grid2 { grid-template-columns: 1fr; }
  .conv .x { opacity: 1; }
  #input { font-size: 16px; } /* 防止 iOS 聚焦时自动放大 */
}
@media (min-width: 761px) {
  #new-chat-top { visibility: hidden; }
}
