:root {
  --bg: #f6f4ef; --panel: #ebe8e0; --card: #ffffff; --line: #d9d4c8;
  --text: #22201b; --muted: #746f63; --accent: #b4552d; --accent-text: #ffffff;
  --user: #e6e1d5; --err: #b3261e; --code: #1c1b19;
  --font: system-ui, -apple-system, "Segoe UI", "PingFang SC", "Microsoft YaHei", sans-serif;
  --mono: ui-monospace, "Cascadia Code", Consolas, Menlo, monospace;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #1b1a18; --panel: #141412; --card: #252421; --line: #38362f;
    --text: #ece8df; --muted: #9a9486; --accent: #d9774a; --accent-text: #1b1a18;
    --user: #33312b; --err: #f2867e; --code: #0f0f0e;
  }
}
* { box-sizing: border-box; }
html, body { height: 100%; margin: 0; }
body { background: var(--bg); color: var(--text); font: 15px/1.65 var(--font); -webkit-text-size-adjust: 100%; }
[hidden] { display: none !important; }
button, input, select, textarea { font: inherit; color: inherit; }
button { cursor: pointer; border: 1px solid var(--line); background: var(--card); border-radius: 8px; padding: 7px 14px; }
button:hover { border-color: var(--muted); }
button:disabled { opacity: .5; cursor: default; }
button.primary { background: var(--accent); border-color: var(--accent); color: var(--accent-text); font-weight: 600; }
button.ghost { background: none; border-color: transparent; color: var(--muted); padding: 4px 8px; font-size: 13px; }
button.ghost:hover { color: var(--text); }
button.icon { background: none; border: none; font-size: 18px; padding: 4px 8px; }
button.danger { color: var(--err); }
button.wide { width: 100%; }
:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }
input, select, textarea { background: var(--card); border: 1px solid var(--line); border-radius: 8px; padding: 8px 10px; }
.muted { color: var(--muted); }
.err { color: var(--err); min-height: 1.3em; margin: 0; font-size: 13px; }

/* 登录 */
.auth { min-height: 100%; display: grid; place-items: center; padding: 16px; }
.auth-card { width: min(380px, 100%); background: var(--card); border: 1px solid var(--line); border-radius: 14px; padding: 28px; display: grid; gap: 14px; }
.auth-card h1 { margin: 0; font-size: 24px; letter-spacing: -.01em; }
.auth-card p { margin: 0; }
.auth-card label { display: grid; gap: 5px; font-size: 13px; color: var(--muted); }
.auth-card input { color: var(--text); font-size: 15px; }
.tabs { display: grid; grid-template-columns: 1fr 1fr; background: var(--panel); border-radius: 9px; padding: 3px; }
.tabs button { border: none; background: none; padding: 6px; }
.tabs button.on { background: var(--card); font-weight: 600; }

/* 布局 */
.app { height: 100dvh; display: grid; grid-template-columns: 270px 1fr; }
.sidebar { background: var(--panel); border-right: 1px solid var(--line); display: flex; flex-direction: column; min-height: 0; }
.side-top { padding: 12px; }
.conv-list { flex: 1; overflow-y: auto; padding: 0 8px; }
.conv { display: flex; align-items: center; border-radius: 8px; }
.conv:hover, .conv.on { background: var(--user); }
.conv a { flex: 1; min-width: 0; padding: 7px 10px; color: inherit; text-decoration: none; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; font-size: 14px; }
.conv button { visibility: hidden; border: none; background: none; padding: 4px 8px; color: var(--muted); }
.conv:hover button, .conv.on button, .conv button:focus-visible { visibility: visible; }
.conv-day { padding: 12px 10px 4px; font-size: 12px; color: var(--muted); }
.side-bottom { border-top: 1px solid var(--line); padding: 10px 12px; display: flex; flex-wrap: wrap; align-items: center; gap: 2px; }
.who { flex: 1 1 100%; font-weight: 600; font-size: 14px; overflow: hidden; text-overflow: ellipsis; }
.scrim { display: none; }

.main { display: flex; flex-direction: column; min-width: 0; min-height: 0; }
.topbar { display: flex; align-items: center; gap: 10px; padding: 8px 14px; border-bottom: 1px solid var(--line); }
.topbar select { max-width: 50vw; min-width: 0; padding: 5px 8px; }
.conv-title { color: var(--muted); font-size: 13px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.conv-title { flex: 1; min-width: 0; }
.admin-top { flex: none; padding: 4px 12px; font-size: 13px; }
#menuBtn { display: none; }
.scroll { flex: 1; overflow-y: auto; }
.msgs { max-width: 800px; margin: 0 auto; padding: 22px 16px 30px; display: flex; flex-direction: column; gap: 18px; }
.empty { margin: 18vh auto 0; text-align: center; color: var(--muted); }
.empty b { display: block; font-size: 22px; color: var(--text); margin-bottom: 6px; }

.msg.user { align-self: flex-end; max-width: 85%; background: var(--user); border-radius: 14px; padding: 9px 14px; white-space: pre-wrap; overflow-wrap: anywhere; }
.msg.assistant { min-width: 0; }
.msg .meta { font-size: 12px; color: var(--muted); margin-top: 4px; display: flex; gap: 6px; align-items: center; }
.msg .meta button { font-size: 12px; padding: 1px 6px; }
.think { border-left: 2px solid var(--line); margin: 0 0 8px; padding-left: 10px; color: var(--muted); font-size: 13px; }
.think summary { cursor: pointer; }
.think div { white-space: pre-wrap; margin-top: 4px; }
.notice { color: var(--muted); font-size: 13px; border: 1px dashed var(--line); border-radius: 8px; padding: 6px 10px; margin-top: 8px; }
.notice.error { color: var(--err); border-color: var(--err); }
.cursor::after { content: "▍"; color: var(--accent); animation: blink 1s steps(2) infinite; }
@keyframes blink { 50% { opacity: 0; } }
@media (prefers-reduced-motion: reduce) { .cursor::after { animation: none; } }

/* Markdown */
.md { overflow-wrap: anywhere; }
.md > :first-child { margin-top: 0; } .md > :last-child { margin-bottom: 0; }
.md p, .md ul, .md ol, .md pre, .md table, .md blockquote { margin: .65em 0; }
.md h1, .md h2, .md h3, .md h4 { margin: 1.1em 0 .4em; line-height: 1.3; }
.md h1 { font-size: 1.35em; } .md h2 { font-size: 1.2em; } .md h3 { font-size: 1.08em; }
.md a { color: var(--accent); }
.md code { font-family: var(--mono); font-size: .88em; background: var(--user); border-radius: 4px; padding: .1em .35em; }
.md pre { background: var(--code); color: #e6e3db; border-radius: 10px; overflow: hidden; }
.md pre code { display: block; background: none; padding: 12px 14px; overflow-x: auto; font-size: 13px; line-height: 1.5; }
.pre-bar { display: flex; justify-content: space-between; align-items: center; padding: 4px 6px 4px 14px; font: 12px var(--mono); color: #a39e92; background: #ffffff10; }
.pre-bar button { background: none; border: none; color: inherit; font: inherit; padding: 2px 8px; }
.pre-bar button:hover { color: #fff; }
.md blockquote { border-left: 3px solid var(--line); padding-left: 12px; color: var(--muted); margin-left: 0; }
.tbl { overflow-x: auto; }
.md table { border-collapse: collapse; font-size: 14px; }
.md th, .md td { border: 1px solid var(--line); padding: 5px 10px; text-align: left; }
.md th { background: var(--panel); }
.md img { max-width: 100%; }
.md .katex-display { overflow-x: auto; overflow-y: hidden; padding: 2px 0; }

/* 输入区 */
.composer { display: flex; gap: 8px; align-items: flex-end; max-width: 800px; width: 100%; margin: 0 auto; padding: 10px 16px calc(14px + env(safe-area-inset-bottom)); }
.composer textarea { flex: 1; resize: none; max-height: 40vh; border-radius: 12px; padding: 10px 12px; line-height: 1.5; }
.composer button { height: 44px; }

/* 弹窗 */
.dlg { border: 1px solid var(--line); border-radius: 14px; background: var(--bg); color: var(--text); padding: 0; width: min(900px, calc(100vw - 24px)); max-height: calc(100dvh - 24px); }
.dlg::backdrop { background: #0008; }
.dlg-head { display: flex; justify-content: space-between; align-items: center; padding: 12px 16px; border-bottom: 1px solid var(--line); position: sticky; top: 0; background: var(--bg); }
.dlg-head h2 { margin: 0; font-size: 17px; }
.dlg-body { padding: 16px; display: grid; gap: 18px; }
.dlg-body h3 { margin: 0 0 8px; font-size: 14px; color: var(--muted); font-weight: 600; }
.dlg-body table { width: 100%; border-collapse: collapse; font-size: 13px; }
.dlg-body th, .dlg-body td { border-bottom: 1px solid var(--line); padding: 6px 8px; text-align: left; white-space: nowrap; }
.dlg-body th { color: var(--muted); font-weight: 500; }
.dlg-body td.num, .dlg-body th.num { text-align: right; font-variant-numeric: tabular-nums; }
.dlg-body td button { font-size: 12px; padding: 2px 8px; }
.row { display: flex; flex-wrap: wrap; gap: 8px; align-items: flex-end; }
.row label { display: grid; gap: 3px; font-size: 12px; color: var(--muted); }
.row input { width: 130px; color: var(--text); }
.stack { display: grid; gap: 10px; max-width: 320px; }
.stack label { display: grid; gap: 4px; font-size: 13px; color: var(--muted); }
.budgets { display: grid; grid-template-columns: repeat(auto-fit, minmax(220px, 1fr)); gap: 10px; }
.budget { background: var(--card); border: 1px solid var(--line); border-radius: 10px; padding: 10px 12px; }
.budget b { font-variant-numeric: tabular-nums; }
.bar { height: 6px; background: var(--panel); border-radius: 3px; margin-top: 6px; overflow: hidden; }
.bar i { display: block; height: 100%; background: var(--accent); }
.code { font-family: var(--mono); user-select: all; }
.off { opacity: .5; }

@media (max-width: 760px) {
  .app { grid-template-columns: 1fr; }
  .sidebar { position: fixed; inset: 0 auto 0 0; width: min(300px, 84vw); z-index: 20; transform: translateX(-100%); transition: transform .2s; }
  .app.menu .sidebar { transform: none; }
  .app.menu .scrim { display: block; position: fixed; inset: 0; background: #0006; z-index: 10; }
  #menuBtn { display: block; }
  .conv button { visibility: visible; }
  .msg.user { max-width: 92%; }
}
@media (prefers-reduced-motion: reduce) { .sidebar { transition: none; } }

/* 弹窗内容不撑破弹窗：宽表格在 .tbl 里横向滚动 */
.dlg { overflow-x: hidden; }
.dlg-body > *, .dlg-body section > * { min-width: 0; }
.dlg-body section { display: grid; gap: 8px; }
.dlg-body h3 { margin: 0; }
