:root {
  --paper: #F6F8F4; --ink: #1B2925; --muted: #5B6B65; --line: #D6DFDA;
  --board: #24493F; --board-deep: #1A3731; --chalk: #EEF3EC; --chalk-dim: #A9BDB4;
  --hl: #F4D35E; --hl-soft: #FBF0C2; --mint: #E3EEE8; --warn: #B4532F; --warn-soft: #F7E3DA;
  --bubble-me: #24493F; --bubble-me-ink: #EEF3EC; --bubble-ai: #FFFFFF;
  --kai: "KaiTi", "STKaiti", "楷体", serif;
  --sans: -apple-system, "PingFang SC", "Microsoft YaHei UI", "Microsoft YaHei", system-ui, sans-serif;
  --mono: ui-monospace, Menlo, Consolas, monospace;
  --top: env(safe-area-inset-top, 0px); --bottom: env(safe-area-inset-bottom, 0px);
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --paper: #121A18; --ink: #E4ECE8; --muted: #93A59E; --line: #2A3834;
    --board: #1D3B33; --board-deep: #142A25; --mint: #1C2A26; --hl-soft: #3A3420; --warn-soft: #3A2620;
    --bubble-me: #2F5B4F; --bubble-me-ink: #EEF3EC; --bubble-ai: #1A2421;
    color-scheme: dark;
  }
}
:root[data-theme="dark"] {
  --paper: #121A18; --ink: #E4ECE8; --muted: #93A59E; --line: #2A3834;
  --board: #1D3B33; --board-deep: #142A25; --mint: #1C2A26; --hl-soft: #3A3420; --warn-soft: #3A2620;
  --bubble-me: #2F5B4F; --bubble-me-ink: #EEF3EC; --bubble-ai: #1A2421;
  color-scheme: dark;
}

* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; }
body { background: var(--paper); color: var(--ink); font: 16px/1.6 var(--sans); -webkit-text-size-adjust: 100%; overflow: hidden; }
[hidden] { display: none !important; }
button, input, select, textarea { font: inherit; color: inherit; }
a { color: var(--board); }
@media (prefers-color-scheme: dark) { :root:not([data-theme="light"]) a { color: var(--hl); } }
svg { width: 22px; height: 22px; fill: none; stroke: currentColor; stroke-width: 2; stroke-linecap: round; stroke-linejoin: round; }

.screen { height: 100%; display: flex; flex-direction: column; }
.screen.center { align-items: center; justify-content: center; text-align: center; padding: 24px 24px calc(24px + var(--bottom)); gap: 8px; }
.big { font-family: var(--kai); font-size: 22px; margin: 8px 0 0; }
.note { color: var(--muted); font-size: 14px; margin: 8px 0; }
.note.err { color: var(--warn); }
.note.center { text-align: center; }

/* Welcome */
#welcome { overflow-y: auto; }
.board { background: var(--board); color: var(--chalk); text-align: center; padding: calc(28px + var(--top)) 16px 24px; }
.board h1 { font-family: var(--kai); font-weight: normal; font-size: 28px; margin: 8px 0 0; }
.board .tag { margin: 4px 0 0; color: var(--chalk-dim); }
.board .tag::after { content: ""; display: block; width: 84px; height: 6px; margin: 10px auto 0; background: var(--hl); border-radius: 3px; opacity: .85; }
.mascot { width: 180px; height: 180px; object-fit: contain; }
.mascot.small { width: 120px; height: 120px; }
.paper { padding: 20px 16px calc(24px + var(--bottom)); max-width: 560px; width: 100%; margin: 0 auto; }
.paper h2 { font-family: var(--kai); font-weight: normal; font-size: 22px; margin: 4px 0 8px; }
.steps { padding-left: 1.4em; margin: 0; }
.steps li { margin: 8px 0; }
.steps b { background: linear-gradient(transparent 55%, var(--hl-soft) 55%); font-weight: 600; }
.wechat-tip { background: var(--hl-soft); border-radius: 10px; padding: 10px 12px; font-size: 14px; margin-bottom: 14px; }
.wechat-tip.bar { border-radius: 0; margin: 0; padding: 8px 16px; }

/* App shell */
#app { position: relative; }
.top { display: flex; align-items: center; gap: 4px; background: var(--board); color: var(--chalk); padding: calc(6px + var(--top)) 6px 6px; flex: none; }
.top .title { flex: 1; text-align: center; font-family: var(--kai); font-size: 19px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.icon { background: none; border: 0; width: 44px; height: 44px; display: grid; place-items: center; border-radius: 12px; cursor: pointer; color: inherit; flex: none; }
.icon:active { background: rgba(255,255,255,.12); }
.view { flex: 1; min-height: 0; display: flex; flex-direction: column; }
.page { overflow-y: auto; padding: 16px 16px calc(24px + var(--bottom)); }
.page h2 { font-family: var(--kai); font-weight: normal; font-size: 24px; margin: 0 0 4px; }

/* Messages */
.messages { flex: 1; overflow-y: auto; padding: 14px 12px 8px; display: flex; flex-direction: column; gap: 12px; overscroll-behavior: contain; }
.empty { margin: auto; text-align: center; color: var(--muted); padding: 24px; }
.empty img { width: 132px; height: 132px; object-fit: contain; }
.empty .hello { font-family: var(--kai); font-size: 21px; color: var(--ink); margin: 6px 0; }
.msg { max-width: 88%; }
.msg.me { align-self: flex-end; }
.msg.ai { align-self: flex-start; width: 100%; max-width: 100%; }
.bubble { border-radius: 16px; padding: 9px 13px; word-wrap: break-word; overflow-wrap: anywhere; }
.me .bubble { background: var(--bubble-me); color: var(--bubble-me-ink); border-bottom-right-radius: 5px; white-space: pre-wrap; }
.ai .bubble { background: var(--bubble-ai); border: 1px solid var(--line); border-bottom-left-radius: 5px; }
.me .pics { display: flex; gap: 6px; justify-content: flex-end; flex-wrap: wrap; margin-bottom: 6px; }
.me .pics img { max-width: min(62vw, 240px); max-height: 200px; object-fit: contain; border-radius: 10px; background: #fff; border: 1px solid var(--line); }
.bubble.error { background: var(--warn-soft); border-color: transparent; color: var(--ink); }
.bubble :first-child { margin-top: 0; }
.bubble :last-child { margin-bottom: 0; }
.bubble p { margin: .5em 0; }
.bubble ul, .bubble ol { padding-left: 1.3em; margin: .4em 0; }
.bubble h1, .bubble h2, .bubble h3, .bubble h4 { font-size: 1.05em; margin: .8em 0 .3em; }
.bubble pre { background: var(--mint); border-radius: 8px; padding: 10px; overflow-x: auto; font: 13px/1.5 var(--mono); }
.bubble code { font: .9em var(--mono); background: var(--mint); border-radius: 4px; padding: 0 3px; }
.bubble pre code { background: none; padding: 0; }
.bubble blockquote { margin: .5em 0; padding-left: 10px; border-left: 3px solid var(--line); color: var(--muted); }
.bubble .table-wrap { overflow-x: auto; margin: .5em 0; }
.bubble table { border-collapse: collapse; font-size: 14px; }
.bubble th, .bubble td { border: 1px solid var(--line); padding: 4px 8px; text-align: left; }
.bubble img { max-width: 100%; }
.thinking { margin: 0 0 6px; font-size: 14px; color: var(--muted); }
.thinking summary { cursor: pointer; list-style: none; display: inline-flex; align-items: center; gap: 4px; padding: 2px 10px; border-radius: 999px; background: var(--mint); }
.thinking summary::-webkit-details-marker { display: none; }
.thinking .body { white-space: pre-wrap; border-left: 2px solid var(--line); padding-left: 10px; margin: 6px 0 0; max-height: 40vh; overflow-y: auto; }
.status { font-size: 13px; color: var(--muted); margin: 0 0 6px; }
.dots::after { content: "…"; animation: dots 1.2s steps(4) infinite; display: inline-block; width: 1.2em; overflow: hidden; vertical-align: bottom; }
@keyframes dots { 0% { width: 0 } 100% { width: 1.2em } }
.sources { margin: 6px 0 0; font-size: 13px; color: var(--muted); }
.sources summary { cursor: pointer; list-style: none; display: inline-flex; padding: 2px 10px; border-radius: 999px; background: var(--mint); }
.sources summary::-webkit-details-marker { display: none; }
.sources .list { display: flex; flex-direction: column; gap: 6px; margin-top: 6px; }
.sources a { font-size: 12px; text-decoration: none; background: var(--mint); color: var(--ink); border-radius: 999px; padding: 2px 10px; max-width: 100%; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.meta { font-size: 12px; color: var(--muted); margin-top: 4px; display: flex; gap: 12px; }
.meta button { background: none; border: 0; padding: 0; color: inherit; cursor: pointer; font-size: 12px; }

/* Composer */
.composer { flex: none; border-top: 1px solid var(--line); background: var(--paper); padding: 8px 10px calc(8px + var(--bottom)); }
.composer .row { display: flex; align-items: flex-end; gap: 6px; }
.composer textarea { flex: 1; resize: none; overflow-y: hidden; border: 1px solid var(--line); background: var(--bubble-ai); border-radius: 20px; padding: 9px 14px; max-height: 40vh; line-height: 1.45; outline: none; font-size: 16px; }
.composer textarea:focus { border-color: var(--board); }
.attach { color: var(--muted); }
.send { width: 44px; height: 44px; border-radius: 50%; border: 0; background: var(--board); color: var(--chalk); display: grid; place-items: center; cursor: pointer; flex: none; }
.send:disabled { opacity: .45; }
.send.stop { background: var(--warn); }
.chips { display: flex; gap: 8px; margin-top: 8px; overflow-x: auto; }
.chip { border: 1px solid var(--line); background: transparent; border-radius: 999px; padding: 4px 12px; font-size: 13px; color: var(--muted); cursor: pointer; white-space: nowrap; flex: none; }
.chip[aria-pressed="true"] { background: var(--hl-soft); border-color: var(--hl); color: var(--ink); }
.chip.select { appearance: none; -webkit-appearance: none; padding-right: 12px; }
.thumbs { display: flex; gap: 8px; flex-wrap: wrap; }
.thumbs:not(:empty) { margin-bottom: 8px; }
.thumb { position: relative; }
.thumb img { width: 64px; height: 64px; object-fit: cover; border-radius: 10px; display: block; }
.thumb button { position: absolute; top: -6px; right: -6px; width: 22px; height: 22px; border-radius: 50%; border: 0; background: var(--ink); color: var(--paper); font-size: 13px; line-height: 22px; padding: 0; cursor: pointer; }

/* Memory + settings */
.group { margin: 18px 0 0; }
.group h3 { font-size: 15px; color: var(--muted); font-weight: 600; margin: 0 0 8px; }
.mem { background: var(--bubble-ai); border: 1px solid var(--line); border-radius: 12px; padding: 10px 12px; margin: 0 0 8px; display: flex; gap: 10px; align-items: flex-start; }
.mem .text { flex: 1; min-width: 0; }
.mem .stars { color: #C9A227; font-size: 12px; letter-spacing: 1px; }
.mem .sub { color: var(--muted); font-size: 12px; }
.mem.done { opacity: .6; }
.mem .del { background: none; border: 0; color: var(--muted); width: 32px; height: 32px; border-radius: 8px; cursor: pointer; flex: none; }
.card { background: var(--bubble-ai); border: 1px solid var(--line); border-radius: 14px; padding: 14px; margin: 14px 0 0; }
.card .label { font-size: 13px; color: var(--muted); }
.card .value { font-size: 16px; margin: 2px 0 0; word-break: break-all; }
.actions { display: flex; gap: 8px; flex-wrap: wrap; margin-top: 10px; }
.btn { border: 0; background: var(--board); color: var(--chalk); border-radius: 10px; padding: 8px 14px; cursor: pointer; font-size: 15px; }
.btn.ghost { background: var(--mint); color: var(--ink); }
.btn.danger { background: var(--warn-soft); color: var(--warn); }

/* Drawer */
.scrim { position: absolute; inset: 0; background: rgba(0,0,0,.35); z-index: 10; }
.drawer { position: absolute; top: 0; bottom: 0; left: 0; width: min(82vw, 320px); background: var(--paper); z-index: 11; transform: translateX(-102%); transition: transform .22s ease; display: flex; flex-direction: column; padding: calc(12px + var(--top)) 10px calc(12px + var(--bottom)); visibility: hidden; }
.drawer.open { transform: none; visibility: visible; box-shadow: 4px 0 24px rgba(0,0,0,.18); }
.drawer-head { display: flex; align-items: center; gap: 10px; padding: 4px 8px 12px; font-family: var(--kai); font-size: 20px; }
.drawer-head img { border-radius: 10px; }
.nav-item { text-align: left; background: none; border: 0; padding: 11px 12px; border-radius: 10px; cursor: pointer; font-size: 16px; }
.nav-item:active, .chat-item:active { background: var(--mint); }
.drawer-label { font-size: 12px; color: var(--muted); padding: 14px 12px 4px; }
.chat-list { overflow-y: auto; flex: 1; }
.chat-item { display: flex; align-items: center; gap: 6px; border-radius: 10px; }
.chat-item.active { background: var(--mint); }
.chat-item .open { flex: 1; min-width: 0; text-align: left; background: none; border: 0; padding: 9px 12px; cursor: pointer; }
.chat-item .open span { display: block; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.chat-item .open small { color: var(--muted); font-size: 12px; }
.chat-item .rm { background: none; border: 0; color: var(--muted); width: 36px; height: 36px; cursor: pointer; border-radius: 8px; flex: none; }

.toast { position: absolute; left: 50%; bottom: calc(120px + var(--bottom)); transform: translateX(-50%); background: var(--ink); color: var(--paper); padding: 8px 14px; border-radius: 999px; font-size: 14px; z-index: 20; max-width: calc(100% - 32px); text-align: center; }

@media (min-width: 720px) {
  .messages, .composer > * { max-width: 760px; margin-left: auto; margin-right: auto; width: 100%; }
  .page { max-width: 640px; margin: 0 auto; width: 100%; }
}
