:root {
  --bg: #262624;
  --side: #1F1E1D;
  --text: #ECE9E2;
  --accent: #D97757;
  --muted: rgba(236, 233, 226, .55);
  --faint: rgba(236, 233, 226, .1);
  --surface: #30302E;
  --app-h: 100dvh;
  --sat: env(safe-area-inset-top, 0px);
  --sab: env(safe-area-inset-bottom, 0px);
  --sal: env(safe-area-inset-left, 0px);
  --sar: env(safe-area-inset-right, 0px);
}
* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; background: var(--bg); color: var(--text); overflow: hidden; }
body {
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", system-ui, "PingFang SC", "Hiragino Sans GB",
    "Microsoft YaHei", "Noto Sans CJK SC", sans-serif;
  font-size: 16px; line-height: 1.65; -webkit-font-smoothing: antialiased;
  -webkit-text-size-adjust: 100%; -webkit-tap-highlight-color: transparent;
}
button, input, textarea { font: inherit; color: inherit; }
button { cursor: pointer; background: none; border: 0; padding: 0; }
[hidden] { display: none !important; }
::selection { background: rgba(217, 119, 87, .35); }

/* ---------- 登录 ---------- */
.login { position: fixed; inset: 0; display: flex; align-items: center; justify-content: center; padding: 24px; }
.login-box { width: 100%; max-width: 320px; display: flex; flex-direction: column; gap: 12px; }
.login-mark { color: var(--accent); font-size: 40px; text-align: center; line-height: 1; margin-bottom: 12px; }
.login-box input { background: var(--surface); border: 1px solid var(--faint); border-radius: 10px; padding: 11px 14px; outline: none; font-size: 16px; }
.login-box input:focus { border-color: rgba(236, 233, 226, .3); }
.login-box button { background: var(--accent); color: #fff; border-radius: 10px; padding: 10px; font-weight: 500; }
.login-err { color: var(--accent); font-size: 14px; min-height: 1.4em; text-align: center; }

/* ---------- 布局 ---------- */
.app { position: fixed; left: 0; right: 0; top: 0; height: var(--app-h); display: flex; }
.sidebar {
  width: 260px; flex: none; background: var(--side); display: flex; flex-direction: column;
  padding: calc(var(--sat) + 10px) 8px calc(var(--sab) + 8px) calc(var(--sal) + 8px);
}
.side-top { padding: 4px 4px 10px; }
.new-chat { width: 100%; text-align: left; padding: 8px 10px; border-radius: 8px; display: flex; align-items: center; gap: 8px; font-size: 15px; }
.new-chat .plus { color: var(--accent); font-size: 17px; }
.new-chat:hover { background: var(--faint); }
.conv-list { flex: 1; overflow-y: auto; -webkit-overflow-scrolling: touch; padding: 0 2px; }
.group-label { font-size: 12px; color: var(--muted); padding: 14px 10px 4px; }
.conv { position: relative; display: flex; align-items: center; border-radius: 8px; }
.conv a { flex: 1; min-width: 0; padding: 7px 10px; font-size: 14px; color: var(--text); text-decoration: none;
  white-space: nowrap; overflow: hidden; text-overflow: ellipsis; opacity: .85; }
.conv:hover, .conv.active { background: var(--faint); }
.conv.active a { opacity: 1; }
.conv .more { width: 28px; height: 28px; flex: none; color: var(--muted); border-radius: 6px; visibility: hidden; }
.conv:hover .more, .conv.active .more { visibility: visible; }
@media (hover: none) { .conv .more { visibility: visible; } }
.menu { position: absolute; right: 4px; top: 32px; z-index: 30; background: var(--surface); border: 1px solid var(--faint);
  border-radius: 8px; padding: 4px; min-width: 110px; box-shadow: 0 6px 20px rgba(0, 0, 0, .35); }
.menu button { display: block; width: 100%; text-align: left; padding: 6px 10px; font-size: 14px; border-radius: 6px; }
.menu button:hover { background: var(--faint); }
.menu .danger { color: var(--accent); }
.side-bottom { padding: 8px 6px 0; border-top: 1px solid var(--faint); }
.link-btn { font-size: 13px; color: var(--muted); padding: 4px; }

.main { flex: 1; min-width: 0; display: flex; flex-direction: column; position: relative; }
.topbar { flex: none; height: calc(48px + var(--sat)); padding: var(--sat) calc(var(--sar) + 8px) 0 8px;
  display: flex; align-items: center; gap: 6px; }
.conv-title { flex: 1; min-width: 0; text-align: center; font-size: 14px; color: var(--muted);
  white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.icon-btn { width: 36px; height: 36px; display: inline-flex; align-items: center; justify-content: center;
  border-radius: 8px; color: var(--muted); flex: none; cursor: pointer; }
.icon-btn:hover { background: var(--faint); color: var(--text); }
#menu-btn, #new-chat-top { visibility: hidden; }
.scrim { display: none; }

@media (max-width: 800px) {
  .sidebar { position: fixed; z-index: 20; top: 0; bottom: 0; left: 0; transform: translateX(-100%);
    width: 280px; }
  .app.side-open .sidebar { transform: none; }
  .app.side-open .scrim { display: block; position: fixed; inset: 0; z-index: 15; background: rgba(0, 0, 0, .45); }
  #menu-btn, #new-chat-top { visibility: visible; }
}

/* ---------- 消息 ---------- */
.scroller { flex: 1; overflow-y: auto; -webkit-overflow-scrolling: touch; overscroll-behavior: contain; }
.messages { max-width: 720px; margin: 0 auto; padding: 8px calc(var(--sar) + 20px) 24px calc(var(--sal) + 20px); }
.empty { position: absolute; left: 0; right: 0; top: 34%; text-align: center; font-size: 26px; color: var(--text);
  opacity: .9; pointer-events: none; font-family: ui-serif, Georgia, "Songti SC", serif; }
.msg { margin: 20px 0; animation: fade .15s ease-out; }
@keyframes fade { from { opacity: 0; } to { opacity: 1; } }
.msg.user { display: flex; flex-direction: column; align-items: flex-end; }
.bubble { background: var(--surface); border-radius: 18px; padding: 9px 15px; max-width: 85%;
  white-space: pre-wrap; word-wrap: break-word; overflow-wrap: anywhere; }
.msg-imgs { display: flex; flex-wrap: wrap; justify-content: flex-end; gap: 6px; margin-bottom: 6px; max-width: 85%; }
.msg-imgs img { width: 120px; height: 120px; object-fit: cover; border-radius: 12px; cursor: zoom-in; background: var(--surface); }
.msg-imgs.single img { width: auto; height: auto; max-width: min(320px, 100%); max-height: 320px; }

.md { overflow-wrap: anywhere; }
.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 .9em; }
.md ul, .md ol { padding-left: 1.5em; }
.md li { margin: .2em 0; }
.md h1, .md h2, .md h3, .md h4 { line-height: 1.35; margin: 1.3em 0 .5em; font-weight: 600; }
.md h1 { font-size: 1.4em; } .md h2 { font-size: 1.25em; } .md h3 { font-size: 1.1em; } .md h4 { font-size: 1em; }
.md a { color: var(--accent); text-decoration: underline; text-underline-offset: 2px; }
.md blockquote { border-left: 3px solid var(--faint); padding-left: 1em; color: var(--muted); }
.md hr { border: 0; border-top: 1px solid var(--faint); margin: 1.5em 0; }
.md code { font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace; font-size: .875em; }
.md :not(pre) > code { background: rgba(236, 233, 226, .08); padding: .12em .38em; border-radius: 5px; }
.md img { max-width: 100%; }
.codeblock { background: #1A1918; border: 1px solid var(--faint); border-radius: 10px; margin: 0 0 .9em; overflow: hidden; }
.codeblock .code-head { display: flex; justify-content: space-between; align-items: center; padding: 5px 8px 5px 14px;
  font-size: 12px; color: var(--muted); border-bottom: 1px solid var(--faint); }
.codeblock .copy { font-size: 12px; color: var(--muted); padding: 2px 8px; border-radius: 5px; }
.codeblock .copy:hover { background: var(--faint); color: var(--text); }
.codeblock pre { margin: 0; padding: 12px 14px; overflow-x: auto; line-height: 1.5; -webkit-overflow-scrolling: touch; }
.table-wrap { overflow-x: auto; -webkit-overflow-scrolling: touch; }
.md table { border-collapse: collapse; font-size: .94em; min-width: 100%; }
.md th, .md td { border: 1px solid var(--faint); padding: 6px 10px; text-align: left; vertical-align: top; }
.md th { font-weight: 600; background: rgba(236, 233, 226, .04); }
.md .katex-display { overflow-x: auto; overflow-y: hidden; padding: 2px 0; }
.cursor { display: inline-block; width: .5em; height: 1.05em; vertical-align: -.17em; margin-left: 2px;
  background: var(--accent); border-radius: 1px; animation: blink 1s steps(1) infinite; }
@keyframes blink { 50% { opacity: 0; } }
.msg-foot { display: flex; align-items: center; gap: 10px; margin-top: 6px; font-size: 12px; color: var(--muted); min-height: 24px; }
.msg-foot button { color: var(--muted); padding: 2px 6px; border-radius: 5px; font-size: 12px; }
.msg-foot button:hover { background: var(--faint); color: var(--text); }
.msg-err { color: var(--accent); font-size: 14px; margin-top: 6px; }
.status-line { color: var(--muted); font-size: 14px; }
.summary-note { text-align: center; font-size: 12px; color: var(--muted); margin: 18px 0; }

/* ---------- 输入区 ---------- */
.composer-wrap { flex: none; padding: 0 calc(var(--sar) + 12px) calc(var(--sab) + 10px) calc(var(--sal) + 12px); }
.composer-inner { max-width: 720px; margin: 0 auto; }
.model-switch { display: inline-flex; background: var(--side); border: 1px solid var(--faint); border-radius: 9px; padding: 2px; margin: 0 0 8px 4px; }
.model-switch button { padding: 3px 14px; border-radius: 7px; font-size: 13px; color: var(--muted); }
.model-switch button.on { background: var(--surface); color: var(--text); box-shadow: inset 0 0 0 1px rgba(217, 119, 87, .55); }
.composer { background: var(--surface); border: 1px solid var(--faint); border-radius: 18px; padding: 10px 10px 8px 14px; }
.composer:focus-within { border-color: rgba(236, 233, 226, .22); }
.composer textarea { width: 100%; resize: none; border: 0; outline: none; background: transparent; padding: 2px 4px 0 0;
  max-height: 40vh; min-height: 26px; line-height: 1.55; font-size: 16px; display: block; }
.composer textarea::placeholder { color: var(--muted); }
.composer-row { display: flex; align-items: center; gap: 6px; margin-top: 6px; margin-left: -6px; }
.composer .attach { width: 34px; height: 34px; border: 1px solid var(--faint); border-radius: 9px; }
.hint { flex: 1; font-size: 12px; color: var(--muted); }
.send { width: 34px; height: 34px; border-radius: 9px; background: var(--accent); color: #fff; display: inline-flex;
  align-items: center; justify-content: center; flex: none; }
.send:disabled { opacity: .35; cursor: default; }
.send.stop { background: var(--text); color: var(--bg); }
.thumbs { display: flex; gap: 8px; flex-wrap: wrap; }
.thumbs:not(:empty) { margin: 2px 0 10px; }
.thumb { position: relative; width: 64px; height: 64px; border-radius: 10px; overflow: hidden; background: var(--side); flex: none; }
.thumb img { width: 100%; height: 100%; object-fit: cover; display: block; }
.thumb.uploading img { opacity: .4; }
.thumb.error { box-shadow: inset 0 0 0 2px var(--accent); }
.thumb .x { position: absolute; top: 3px; right: 3px; width: 20px; height: 20px; border-radius: 50%;
  background: rgba(20, 20, 19, .8); color: var(--text); font-size: 13px; line-height: 20px; text-align: center; }
.thumb .state { position: absolute; left: 0; right: 0; bottom: 0; font-size: 11px; line-height: 18px; text-align: center;
  background: rgba(20, 20, 19, .75); color: var(--text); }
.drop { position: absolute; inset: 8px; border: 2px dashed rgba(217, 119, 87, .7); border-radius: 16px; background: rgba(38, 38, 36, .85);
  display: flex; align-items: center; justify-content: center; z-index: 25; font-size: 15px; pointer-events: none; }
.lightbox { position: fixed; inset: 0; z-index: 50; background: rgba(0, 0, 0, .88); display: flex; align-items: center;
  justify-content: center; padding: calc(var(--sat) + 16px) 16px calc(var(--sab) + 16px); cursor: zoom-out; }
.lightbox img { max-width: 100%; max-height: 100%; border-radius: 6px; }
.toast { position: fixed; left: 50%; top: calc(var(--sat) + 14px); transform: translateX(-50%); z-index: 60; background: var(--surface);
  border: 1px solid var(--faint); border-radius: 10px; padding: 8px 14px; font-size: 14px; max-width: 90vw; }
