:root {
  --bg: #faf9f5;
  --panel: #f3f1ea;
  --card: #ffffff;
  --line: #e4e0d5;
  --text: #2b2a27;
  --muted: #8a867c;
  --accent: #d97757;
  --accent-ink: #ffffff;
  --accent-soft: #f6e3da;
  --user-bubble: #efe7dc;
  --think: #8a867c;
  --danger: #c2453d;
  --ok: #3f8f5a;
  --code-bg: #f1eee6;
  --shadow: 0 1px 2px rgba(0,0,0,.05), 0 6px 24px rgba(0,0,0,.06);
  --radius: 14px;
  --font: -apple-system, BlinkMacSystemFont, "Segoe UI", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", "Noto Sans CJK SC", sans-serif;
  --mono: ui-monospace, "Cascadia Code", Consolas, "JetBrains Mono", monospace;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #262624;
    --panel: #1f1f1d;
    --card: #30302e;
    --line: #3d3d3a;
    --text: #ece9e1;
    --muted: #9b978c;
    --accent: #e08a6b;
    --accent-ink: #1f1f1d;
    --accent-soft: #4a3329;
    --user-bubble: #3a3631;
    --think: #a39f94;
    --danger: #e0716a;
    --ok: #74c08e;
    --code-bg: #1d1d1b;
    --shadow: 0 1px 2px rgba(0,0,0,.3), 0 6px 24px rgba(0,0,0,.3);
  }
}

* { box-sizing: border-box; }
html, body { height: 100%; margin: 0; }
body { background: var(--bg); color: var(--text); font: 15px/1.7 var(--font); -webkit-font-smoothing: antialiased; }
button, select, input, textarea { font: inherit; color: inherit; }
button { cursor: pointer; }
code, pre { font-family: var(--mono); }

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

/* ───────── 侧栏 ───────── */
#sidebar { width: 268px; flex: none; background: var(--panel); border-right: 1px solid var(--line); display: flex; flex-direction: column; padding: 14px 10px 10px; gap: 10px; }
.brand { display: flex; align-items: center; gap: 8px; padding: 0 6px; }
.brand-icon { width: 30px; height: 30px; }
.brand-name { font-weight: 700; font-size: 18px; letter-spacing: .5px; flex: 1; }
.new-chat { border: 1px dashed var(--accent); background: transparent; color: var(--accent); border-radius: 10px; padding: 8px; font-weight: 600; }
.new-chat:hover { background: var(--accent-soft); }
.chat-list { flex: 1; overflow-y: auto; display: flex; flex-direction: column; gap: 2px; margin: 0 -4px; padding: 0 4px; }
.chat-item { position: relative; display: block; width: 100%; text-align: left; border: 0; background: transparent; border-radius: 10px; padding: 8px 34px 8px 10px; }
.chat-item:hover { background: rgba(127,127,127,.08); }
.chat-item.active { background: var(--card); box-shadow: var(--shadow); }
.chat-item .t { display: flex; align-items: center; gap: 6px; font-size: 14px; font-weight: 600; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.chat-item .t span.title { overflow: hidden; text-overflow: ellipsis; }
.chat-item .p { font-size: 12px; color: var(--muted); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.chat-item .del { position: absolute; right: 6px; top: 50%; transform: translateY(-50%); border: 0; background: transparent; color: var(--muted); opacity: 0; padding: 4px 6px; border-radius: 6px; }
.chat-item:hover .del, .chat-item.active .del { opacity: .8; }
.chat-item .del:hover { color: var(--danger); background: rgba(127,127,127,.12); }
.badge { flex: none; font-size: 10px; font-weight: 700; line-height: 1; padding: 3px 5px; border-radius: 5px; background: var(--line); color: var(--muted); }
.badge.api { background: var(--accent-soft); color: var(--accent); }
.dot-live { width: 7px; height: 7px; border-radius: 50%; background: var(--accent); animation: pulse 1s infinite; flex: none; }
.side-foot { display: flex; flex-direction: column; gap: 6px; }
.usage-mini { border: 0; background: transparent; text-align: left; padding: 6px 8px; border-radius: 8px; font-size: 12px; color: var(--muted); }
.usage-mini:hover { background: rgba(127,127,127,.08); }
.usage-mini .bar { height: 4px; border-radius: 2px; background: var(--line); margin-top: 4px; overflow: hidden; }
.usage-mini .bar i { display: block; height: 100%; background: var(--accent); border-radius: 2px; }
.settings-btn { border: 0; background: transparent; text-align: left; padding: 8px; border-radius: 8px; color: var(--muted); }
.settings-btn:hover { background: rgba(127,127,127,.08); color: var(--text); }

/* ───────── 主区 ───────── */
#main { flex: 1; min-width: 0; display: flex; flex-direction: column; }
#topbar { display: flex; align-items: center; gap: 10px; padding: 10px 16px; border-bottom: 1px solid var(--line); background: var(--bg); }
#chatTitle { flex: 1; min-width: 0; margin: 0; font-size: 16px; font-weight: 650; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; cursor: text; }
.controls { display: flex; align-items: center; gap: 8px; flex-wrap: wrap; justify-content: flex-end; }
.seg { display: inline-flex; border: 1px solid var(--line); border-radius: 9px; padding: 2px; background: var(--panel); }
.seg button { border: 0; background: transparent; padding: 3px 11px; border-radius: 7px; font-size: 13px; color: var(--muted); }
.seg button.on { background: var(--card); color: var(--text); box-shadow: var(--shadow); font-weight: 600; }
.seg button.on[data-line="api"] { color: var(--accent); }
select { border: 1px solid var(--line); background: var(--panel); border-radius: 9px; padding: 4px 8px; font-size: 13px; }
.icon-btn { border: 0; background: transparent; width: 34px; height: 34px; border-radius: 9px; font-size: 17px; display: inline-grid; place-items: center; flex: none; }
.icon-btn:hover { background: rgba(127,127,127,.12); }
.only-mobile { display: none; }

#messages { flex: 1; overflow-y: auto; padding: 24px 16px 12px; scroll-behavior: auto; }
.msg { max-width: 780px; margin: 0 auto 22px; }
.msg.user { display: flex; flex-direction: column; align-items: flex-end; }
.msg.user .bubble { background: var(--user-bubble); border-radius: 18px 18px 4px 18px; padding: 9px 14px; max-width: 85%; white-space: pre-wrap; word-break: break-word; }
.msg.user .imgs { display: flex; gap: 6px; flex-wrap: wrap; justify-content: flex-end; margin-bottom: 6px; max-width: 85%; }
.msg.user .imgs img { width: 120px; height: 120px; object-fit: cover; border-radius: 12px; cursor: zoom-in; border: 1px solid var(--line); }
.msg.user .time { font-size: 11px; color: var(--muted); margin-top: 3px; }
.msg.bot { display: grid; grid-template-columns: 30px 1fr; gap: 10px; }
.msg.bot .avatar { width: 28px; height: 28px; margin-top: 2px; }
.msg.bot .body { min-width: 0; }

.think { margin: 2px 0 8px; color: var(--think); font-size: 13.5px; }
.think > summary { cursor: pointer; list-style: none; display: inline-flex; align-items: center; gap: 6px; padding: 2px 8px 2px 6px; border-radius: 8px; user-select: none; }
.think > summary::-webkit-details-marker { display: none; }
.think > summary:hover { background: rgba(127,127,127,.1); }
.think > summary .chev { transition: transform .15s; display: inline-block; }
.think[open] > summary .chev { transform: rotate(90deg); }
.think .think-text { border-left: 2px solid var(--line); margin: 6px 0 4px 10px; padding: 2px 0 2px 12px; white-space: pre-wrap; word-break: break-word; }
.think.live > summary .lbl::after { content: "…"; animation: blink 1s steps(2) infinite; }

.tool { margin: 4px 0 8px; font-size: 13px; }
.tool > summary { cursor: pointer; list-style: none; display: inline-flex; align-items: center; gap: 6px; max-width: 100%; padding: 4px 10px; border-radius: 999px; background: var(--panel); border: 1px solid var(--line); color: var(--muted); user-select: none; }
.tool > summary::-webkit-details-marker { display: none; }
.tool > summary .lbl { white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.tool > summary .st { flex: none; }
.tool.err > summary { color: var(--danger); border-color: var(--danger); }
.tool pre { margin: 6px 0 0; padding: 10px 12px; max-height: 320px; overflow: auto; background: var(--code-bg); border-radius: 10px; font-size: 12px; white-space: pre-wrap; word-break: break-word; }
.spin { width: 11px; height: 11px; border: 2px solid var(--line); border-top-color: var(--accent); border-radius: 50%; animation: spin .8s linear infinite; display: inline-block; }

.note { margin: 6px 0; font-size: 12.5px; color: var(--muted); font-style: italic; }
.status { margin: 4px 0; font-size: 13px; color: var(--muted); display: flex; align-items: center; gap: 8px; }
.typing { display: inline-flex; gap: 4px; padding: 8px 0; }
.typing i { width: 6px; height: 6px; border-radius: 50%; background: var(--muted); opacity: .4; animation: bounce 1.2s infinite; }
.typing i:nth-child(2) { animation-delay: .15s; }
.typing i:nth-child(3) { animation-delay: .3s; }
.meta { margin-top: 6px; font-size: 11.5px; color: var(--muted); display: flex; flex-wrap: wrap; gap: 4px 10px; opacity: .85; }
.meta .stopped { color: var(--accent); }
.err-box { margin: 6px 0; padding: 8px 12px; border-radius: 10px; border: 1px solid var(--danger); color: var(--danger); font-size: 13px; white-space: pre-wrap; word-break: break-word; background: rgba(194,69,61,.06); }

/* 正文 markdown */
.md { word-break: break-word; }
.md > :first-child { margin-top: 0; }
.md > :last-child { margin-bottom: 0; }
.md p { margin: 0 0 .75em; }
.md h1, .md h2, .md h3, .md h4 { margin: 1em 0 .5em; line-height: 1.4; }
.md h1 { font-size: 1.3em; } .md h2 { font-size: 1.18em; } .md h3 { font-size: 1.06em; } .md h4 { font-size: 1em; }
.md ul, .md ol { margin: 0 0 .75em; padding-left: 1.5em; }
.md li { margin: .15em 0; }
.md blockquote { margin: 0 0 .75em; padding: .1em 0 .1em 1em; border-left: 3px solid var(--accent-soft); color: var(--muted); }
.md code { background: var(--code-bg); padding: .1em .35em; border-radius: 5px; font-size: .9em; }
.md pre { background: var(--code-bg); padding: 12px 14px; border-radius: 10px; overflow-x: auto; margin: 0 0 .75em; }
.md pre code { background: none; padding: 0; font-size: 13px; line-height: 1.55; }
.md a { color: var(--accent); text-decoration: none; border-bottom: 1px solid var(--accent-soft); }
.md a:hover { border-bottom-color: var(--accent); }
.md hr { border: 0; border-top: 1px solid var(--line); margin: 1em 0; }
.md table { border-collapse: collapse; margin: 0 0 .75em; font-size: 14px; display: block; overflow-x: auto; }
.md th, .md td { border: 1px solid var(--line); padding: 5px 10px; text-align: left; }
.md th { background: var(--panel); }
.cursor::after { content: "▍"; color: var(--accent); animation: blink 1s steps(2) infinite; margin-left: 1px; }

.empty { max-width: 560px; margin: 12vh auto 0; text-align: center; color: var(--muted); }
.empty img { width: 64px; height: 64px; }
.empty h2 { color: var(--text); font-weight: 650; margin: 10px 0 6px; font-size: 20px; }
.empty .tips { margin-top: 18px; font-size: 13px; line-height: 1.9; }
.setup-card { max-width: 560px; margin: 20px auto 0; text-align: left; background: var(--card); border: 1px solid var(--line); border-radius: var(--radius); padding: 14px 18px; box-shadow: var(--shadow); color: var(--text); font-size: 14px; }
.setup-card ol { margin: 6px 0 0; padding-left: 1.3em; }
.setup-card button { margin-top: 10px; }

/* ───────── 输入区 ───────── */
#composer { padding: 8px 16px calc(10px + env(safe-area-inset-bottom)); background: var(--bg); }
.input-row { max-width: 780px; margin: 0 auto; display: flex; align-items: flex-end; gap: 6px; background: var(--card); border: 1px solid var(--line); border-radius: 18px; padding: 6px 6px 6px 8px; box-shadow: var(--shadow); }
.input-row:focus-within { border-color: var(--accent); }
#input { flex: 1; border: 0; outline: none; background: transparent; resize: none; max-height: 40vh; padding: 6px 4px; line-height: 1.6; }
.send-btn { flex: none; width: 36px; height: 36px; border-radius: 50%; border: 0; background: var(--accent); color: var(--accent-ink); font-size: 18px; font-weight: 700; display: grid; place-items: center; }
.send-btn:disabled { opacity: .4; cursor: default; }
.send-btn.stop { background: var(--text); color: var(--bg); font-size: 13px; }
.attach-row { max-width: 780px; margin: 0 auto 6px; display: flex; gap: 8px; flex-wrap: wrap; }
.attach-row:empty { display: none; }
.thumb { position: relative; width: 64px; height: 64px; }
.thumb img { width: 100%; height: 100%; object-fit: cover; border-radius: 10px; border: 1px solid var(--line); }
.thumb button { position: absolute; top: -6px; right: -6px; width: 20px; height: 20px; border-radius: 50%; border: 0; background: var(--text); color: var(--bg); font-size: 11px; line-height: 20px; padding: 0; }
.hint { max-width: 780px; margin: 4px auto 0; font-size: 11.5px; color: var(--muted); text-align: center; min-height: 1em; }
.drag-over #composer .input-row { border-color: var(--accent); border-style: dashed; }

/* ───────── 设置 ───────── */
dialog#settings { width: min(680px, calc(100vw - 24px)); max-height: calc(100dvh - 40px); border: 1px solid var(--line); border-radius: 18px; padding: 0; background: var(--bg); color: var(--text); box-shadow: 0 20px 60px rgba(0,0,0,.25); }
dialog::backdrop { background: rgba(20,20,18,.45); }
.dlg-head { display: flex; align-items: center; justify-content: space-between; padding: 10px 12px 0 16px; border-bottom: 1px solid var(--line); position: sticky; top: 0; background: var(--bg); z-index: 1; }
.tabs { display: flex; gap: 2px; }
.tabs button { border: 0; background: transparent; padding: 10px 12px; border-bottom: 2px solid transparent; color: var(--muted); font-weight: 600; }
.tabs button.active { color: var(--text); border-bottom-color: var(--accent); }
.tab-body { padding: 16px 18px 20px; overflow-y: auto; }
.field { margin-bottom: 20px; }
.field > label { font-weight: 650; display: flex; gap: 8px; align-items: baseline; }
.field .cur { font-weight: 400; font-size: 12px; color: var(--muted); }
.help { margin: 4px 0 8px; font-size: 13px; color: var(--muted); }
.help code { background: var(--code-bg); padding: 0 4px; border-radius: 4px; font-size: 12px; }
.row { display: flex; gap: 8px; align-items: center; }
.row.wrap { flex-wrap: wrap; }
.row input { flex: 1; min-width: 0; border: 1px solid var(--line); background: var(--card); border-radius: 9px; padding: 7px 10px; }
.row button, .setup-card button { border: 1px solid var(--line); background: var(--card); border-radius: 9px; padding: 6px 12px; white-space: nowrap; }
.row button:hover, .setup-card button:hover { border-color: var(--accent); color: var(--accent); }
.result { margin-top: 6px; font-size: 13px; white-space: pre-wrap; word-break: break-word; }
.result.inline { margin: 0 0 0 8px; }
.result.ok { color: var(--ok); }
.result.bad { color: var(--danger); }
.persona-text { width: 100%; min-height: 46vh; border: 1px solid var(--line); background: var(--card); border-radius: 10px; padding: 10px 12px; font: 13px/1.65 var(--mono); resize: vertical; margin-bottom: 8px; }
.front-notes { margin-top: 14px; font-size: 13px; color: var(--muted); }
.front-notes pre { white-space: pre-wrap; background: var(--code-bg); padding: 10px; border-radius: 8px; }
.usage-big { font-size: 28px; font-weight: 700; }
.usage-big small { font-size: 14px; color: var(--muted); font-weight: 500; }
.usage-bar { height: 10px; border-radius: 5px; background: var(--line); overflow: hidden; margin: 8px 0 4px; }
.usage-bar i { display: block; height: 100%; background: var(--accent); }
table.u { width: 100%; border-collapse: collapse; font-size: 13px; margin: 8px 0 16px; }
table.u th, table.u td { border-bottom: 1px solid var(--line); padding: 6px 4px; text-align: left; }
table.u td.n, table.u th.n { text-align: right; font-variant-numeric: tabular-nums; }

#lightbox { position: fixed; inset: 0; background: rgba(0,0,0,.85); display: grid; place-items: center; z-index: 50; cursor: zoom-out; }
#lightbox[hidden] { display: none; }
#lightbox img { max-width: 94vw; max-height: 92vh; border-radius: 8px; }
#toast { position: fixed; left: 50%; bottom: 96px; transform: translateX(-50%); background: var(--text); color: var(--bg); padding: 8px 16px; border-radius: 10px; font-size: 14px; z-index: 60; max-width: 90vw; box-shadow: var(--shadow); }
#toast[hidden] { display: none; }
.scrim { display: none; }

/* ───────── 登录页（手机上用）───────── */
.login-page { display: grid; place-items: center; min-height: 100dvh; padding: 16px; }
.login-card { width: min(340px, 100%); background: var(--card); border: 1px solid var(--line); border-radius: 20px; padding: 28px 24px 22px; box-shadow: var(--shadow); text-align: center; display: flex; flex-direction: column; gap: 12px; }
.login-card img { margin: 0 auto; }
.login-card h1 { margin: 0; font-size: 22px; }
.login-card p { margin: -4px 0 4px; color: var(--muted); font-size: 14px; }
.login-card input { border: 1px solid var(--line); background: var(--bg); border-radius: 12px; padding: 11px 14px; font-size: 16px; outline: none; }
.login-card input:focus { border-color: var(--accent); }
.login-card button { border: 0; background: var(--accent); color: var(--accent-ink); border-radius: 12px; padding: 11px; font-size: 16px; font-weight: 650; }
.login-card button:disabled { opacity: .5; }
.login-err { color: var(--danger); font-size: 14px; min-height: 1.4em; }
.remote-urls { margin-top: 10px; display: flex; flex-direction: column; gap: 6px; }
.remote-url { font: 600 17px var(--mono); background: var(--card); border: 1px dashed var(--accent); border-radius: 10px; padding: 8px 12px; user-select: all; word-break: break-all; }
.remote-url small { font: 12px var(--font); color: var(--muted); margin-left: 6px; }
.switch-on { color: var(--ok); font-weight: 600; }

@keyframes spin { to { transform: rotate(360deg); } }
@keyframes blink { 50% { opacity: 0; } }
@keyframes pulse { 50% { opacity: .3; } }
@keyframes bounce { 0%, 60%, 100% { transform: translateY(0); opacity: .4; } 30% { transform: translateY(-4px); opacity: 1; } }

/* ───────── 手机 ───────── */
@media (max-width: 760px) {
  .only-mobile { display: inline-grid; }
  #sidebar { position: fixed; z-index: 30; inset: 0 auto 0 0; width: 82vw; max-width: 320px; transform: translateX(-102%); transition: transform .2s ease; box-shadow: var(--shadow); }
  #app.side-open #sidebar { transform: none; }
  #app.side-open .scrim { display: block; position: fixed; inset: 0; background: rgba(0,0,0,.35); z-index: 25; }
  #topbar { flex-wrap: wrap; padding: 8px 10px; gap: 6px; }
  #chatTitle { font-size: 15px; }
  .controls { width: 100%; justify-content: flex-start; gap: 6px; }
  .controls select { flex: 1; min-width: 0; }
  #messages { padding: 16px 12px 8px; }
  .msg.user .bubble { max-width: 90%; }
  .msg.bot { grid-template-columns: 24px 1fr; gap: 8px; }
  .msg.bot .avatar { width: 22px; height: 22px; }
  #composer { padding: 6px 10px calc(8px + env(safe-area-inset-bottom)); }
  .chat-item .del { opacity: .6; }
}
