:root {
  --bg: #faf9f5; --surface: #ffffff; --raised: #f0eee6; --text: #1f1e1d; --muted: #73726c;
  --border: #e3e0d6; --accent: #c6613f; --accent-text: #ffffff; --user: #efece2; --error: #b3261e;
  --code-bg: #f3f1ea; --shadow: 0 8px 30px rgba(0, 0, 0, .12);
  color-scheme: light dark;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #1f1e1d; --surface: #2a2927; --raised: #33322f; --text: #ecebe6; --muted: #a3a199;
    --border: #3d3c38; --accent: #d97757; --accent-text: #1f1e1d; --user: #33322f; --error: #f2857d;
    --code-bg: #171716; --shadow: 0 8px 30px rgba(0, 0, 0, .5);
  }
}
* { box-sizing: border-box; }
html, body { height: 100%; margin: 0; }
body {
  background: var(--bg); color: var(--text);
  font: 16px/1.6 -apple-system, BlinkMacSystemFont, "Segoe UI", "PingFang SC", "Microsoft YaHei", "Noto Sans SC", Roboto, sans-serif;
  -webkit-text-size-adjust: 100%;
}
button { font: inherit; color: inherit; background: none; border: 0; cursor: pointer; }
[hidden] { display: none !important; }

#app { display: flex; flex-direction: column; height: 100dvh; max-width: 860px; margin: 0 auto; }
header {
  display: flex; align-items: center; gap: 6px; padding: 8px 12px;
  padding-top: max(8px, env(safe-area-inset-top)); border-bottom: 1px solid var(--border);
}
header h1 { flex: 1; min-width: 0; margin: 0; font-size: 16px; font-weight: 600; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.icon { display: grid; place-items: center; width: 40px; height: 40px; border-radius: 10px; color: var(--muted); flex: none; }
.icon:hover, .text:hover { background: var(--raised); }
.text { padding: 8px 12px; border-radius: 10px; font-size: 14px; white-space: nowrap; }
.text.primary { background: var(--raised); font-weight: 500; }

#banner { padding: 8px 16px; background: var(--raised); color: var(--muted); font-size: 14px; text-align: center; }

main { flex: 1; overflow-y: auto; overscroll-behavior: contain; padding: 16px 16px 8px; }
#empty { margin-top: 18vh; text-align: center; color: var(--muted); }
#empty h2 { color: var(--text); font-weight: 600; margin: 0 0 6px; }
#empty p { margin: 0 auto; max-width: 30em; }

.msg { margin: 0 0 14px; overflow-wrap: anywhere; }
.msg.user { display: flex; flex-direction: column; align-items: flex-end; gap: 6px; }
.msg.user .bubble { background: var(--user); padding: 10px 14px; border-radius: 18px 18px 4px 18px; max-width: 88%; white-space: pre-wrap; }
.msg.tool { margin: -6px 0 8px; font-size: 13px; color: var(--muted); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.msg.tool .tool-name { font-weight: 600; }
.msg.note { font-size: 13px; color: var(--muted); text-align: center; }
.msg.error { border: 1px solid var(--error); color: var(--error); border-radius: 12px; padding: 10px 14px; font-size: 14px; white-space: pre-wrap; }

.md > :first-child { margin-top: 0; }
.md > :last-child { margin-bottom: 0; }
.md p, .md ul, .md ol, .md blockquote, .md pre, .md .table-wrap { margin: 0 0 12px; }
.md h1, .md h2, .md h3, .md h4 { margin: 20px 0 8px; line-height: 1.3; }
.md h1 { font-size: 1.35em; } .md h2 { font-size: 1.2em; } .md h3 { font-size: 1.08em; } .md h4 { font-size: 1em; }
.md ul, .md ol { padding-left: 1.4em; }
.md li { margin: 3px 0; }
.md a { color: var(--accent); }
.md blockquote { border-left: 3px solid var(--border); padding-left: 12px; color: var(--muted); }
.md code { font: 0.88em/1.5 ui-monospace, SFMono-Regular, Menlo, Consolas, monospace; background: var(--code-bg); padding: 2px 5px; border-radius: 5px; }
.md pre { position: relative; background: var(--code-bg); border-radius: 10px; padding: 12px 14px; overflow-x: auto; }
.md pre code { background: none; padding: 0; white-space: pre; }
.md .copy { position: absolute; top: 6px; right: 6px; font-size: 12px; padding: 3px 8px; border-radius: 6px; background: var(--surface); border: 1px solid var(--border); color: var(--muted); }
.md .table-wrap { overflow-x: auto; }
.md table { border-collapse: collapse; font-size: 14px; }
.md th, .md td { border: 1px solid var(--border); padding: 6px 10px; text-align: left; vertical-align: top; }
.md th { background: var(--raised); }
.md img { max-width: 100%; }
.md hr { border: 0; border-top: 1px solid var(--border); margin: 16px 0; }

.activity { display: flex; align-items: center; gap: 8px; color: var(--muted); font-size: 14px; margin-bottom: 14px; }
.dots { width: 10px; height: 10px; border-radius: 50%; background: var(--accent); animation: pulse 1.2s ease-in-out infinite; }
@keyframes pulse { 0%, 100% { opacity: .25; transform: scale(.8); } 50% { opacity: 1; transform: scale(1); } }
@media (prefers-reduced-motion: reduce) { .dots { animation: none; } }

.chips, #attachments { display: flex; flex-wrap: wrap; gap: 6px; }
#attachments:not(:empty) { padding: 0 4px 8px; }
.chip { display: inline-flex; align-items: center; gap: 4px; max-width: 100%; padding: 4px 10px; border: 1px solid var(--border); border-radius: 999px; background: var(--surface); font-size: 13px; color: var(--text); text-decoration: none; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.chip.uploading { color: var(--muted); border-style: dashed; }
.chip-x { color: var(--muted); padding: 0 2px; font-size: 16px; line-height: 1; }

footer { padding: 8px 12px; padding-bottom: max(10px, env(safe-area-inset-bottom)); }
#composer { display: flex; align-items: flex-end; gap: 4px; background: var(--surface); border: 1px solid var(--border); border-radius: 22px; padding: 5px; }
#composer:focus-within { border-color: var(--muted); }
textarea { flex: 1; min-width: 0; resize: none; border: 0; outline: 0; background: none; color: inherit; font: inherit; padding: 8px 4px; max-height: 220px; }
.send { display: grid; place-items: center; width: 40px; height: 40px; border-radius: 50%; background: var(--accent); color: var(--accent-text); flex: none; }
.send:disabled { opacity: .35; cursor: default; }
.send.stop { background: var(--text); color: var(--bg); }

.panel { position: fixed; top: 0; bottom: 0; left: 0; width: min(340px, 88vw); background: var(--surface); box-shadow: var(--shadow); z-index: 20; display: flex; flex-direction: column; padding-top: env(safe-area-inset-top); }
.panel.right { left: auto; right: 0; width: min(420px, 92vw); }
.panel-head { display: flex; align-items: center; padding: 8px 8px 8px 16px; border-bottom: 1px solid var(--border); }
.panel-head h2 { flex: 1; margin: 0; font-size: 16px; }
.close { font-size: 24px; }
.panel-body { flex: 1; overflow-y: auto; padding: 8px; }
.panel-foot { padding: 8px; border-top: 1px solid var(--border); padding-bottom: max(8px, env(safe-area-inset-bottom)); }
.row { display: flex; flex-direction: column; width: 100%; text-align: left; padding: 10px 12px; border-radius: 10px; color: inherit; text-decoration: none; }
.row:hover, .row.active { background: var(--raised); }
.row-title { overflow-wrap: anywhere; }
.row-sub { font-size: 12px; color: var(--muted); }
.muted { color: var(--muted); padding: 8px 12px; margin: 0; font-size: 14px; }
#scrim { position: fixed; inset: 0; background: rgba(0, 0, 0, .35); z-index: 10; }

/* login */
.login { display: grid; place-items: center; min-height: 100dvh; padding: 24px; }
.login form { width: 100%; max-width: 340px; display: flex; flex-direction: column; gap: 14px; }
.login h1 { margin: 0 0 4px; font-size: 22px; }
.login label { display: flex; flex-direction: column; gap: 6px; font-size: 14px; color: var(--muted); }
.login input { font: inherit; font-size: 17px; color: var(--text); background: var(--surface); border: 1px solid var(--border); border-radius: 12px; padding: 11px 14px; outline: 0; }
.login input:focus { border-color: var(--accent); }
.login button { background: var(--accent); color: var(--accent-text); border-radius: 12px; padding: 12px; font-weight: 600; }
.login button:disabled { opacity: .5; }
.login .err { color: var(--error); font-size: 14px; min-height: 1.4em; margin: 0; }
