:root {
  --bg: #ffffff; --bg2: #f5f6f8; --bg3: #eceef2; --fg: #1d1f24; --fg2: #6b7080;
  --border: #e2e4ea; --accent: #4f6bed; --accent-fg: #fff; --danger: #d64545;
  --user-bubble: #4f6bed; --user-fg: #fff; --code-bg: #f6f8fa;
  --radius: 14px; --sidebar-w: 280px;
  --safe-top: env(safe-area-inset-top, 0px); --safe-bottom: env(safe-area-inset-bottom, 0px);
  color-scheme: light;
}
:root[data-theme="dark"] {
  --bg: #16171b; --bg2: #1e2026; --bg3: #2a2d35; --fg: #e8e9ed; --fg2: #9a9fae;
  --border: #30333c; --accent: #7088ff; --user-bubble: #3d55c9; --code-bg: #0f1013;
  color-scheme: dark;
}
* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; background: var(--bg); color: var(--fg);
  font: 16px/1.6 -apple-system, BlinkMacSystemFont, "PingFang SC", "Microsoft YaHei", "Segoe UI", sans-serif;
  -webkit-text-size-adjust: 100%; overscroll-behavior: none; }
button, input, select, textarea { font: inherit; color: inherit; }
#app { display: flex; height: 100dvh; overflow: hidden; }

/* ---------- 侧边栏 ---------- */
#sidebar { width: var(--sidebar-w); flex-shrink: 0; background: var(--bg2); border-right: 1px solid var(--border);
  display: flex; flex-direction: column; padding-top: var(--safe-top); }
.sidebar-head { padding: 12px; display: grid; gap: 8px; }
.sidebar-head input { width: 100%; padding: 9px 12px; border-radius: 10px; border: 1px solid var(--border); background: var(--bg); }
#sessionList { list-style: none; margin: 0; padding: 0 8px; flex: 1; overflow-y: auto; }
#sessionList li { display: flex; align-items: center; gap: 4px; padding: 10px 12px; border-radius: 10px; cursor: pointer;
  white-space: nowrap; overflow: hidden; min-height: 44px; }
#sessionList li span { flex: 1; overflow: hidden; text-overflow: ellipsis; }
#sessionList li:hover { background: var(--bg3); }
#sessionList li.active { background: var(--bg3); font-weight: 600; }
#sessionList li .icon-btn { width: 32px; height: 32px; font-size: 14px; opacity: .6; }
.sidebar-foot { padding: 12px; padding-bottom: calc(12px + var(--safe-bottom)); border-top: 1px solid var(--border); }

/* 手机/竖屏平板：侧边栏变抽屉 */
@media (max-width: 899px) {
  #sidebar { position: fixed; inset: 0 auto 0 0; z-index: 20; transform: translateX(-100%); transition: transform .25s ease; width: min(82vw, var(--sidebar-w)); }
  #app.drawer-open #sidebar { transform: none; box-shadow: 4px 0 24px rgba(0,0,0,.2); }
  #backdrop { position: fixed; inset: 0; background: rgba(0,0,0,.35); z-index: 15; }
}
@media (min-width: 900px) { #menuBtn { display: none; } #backdrop { display: none !important; } }

/* ---------- 主区域 ---------- */
#main { flex: 1; display: flex; flex-direction: column; min-width: 0; position: relative; }
#topbar { display: flex; align-items: center; gap: 6px; padding: 8px 10px; padding-top: calc(8px + var(--safe-top));
  border-bottom: 1px solid var(--border); background: var(--bg); }
.title-wrap { flex: 1; min-width: 0; display: flex; flex-direction: column; align-items: center; }
#chatTitle { margin: 0; font-size: 16px; max-width: 100%; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
#modelSelect { border: none; background: transparent; color: var(--fg2); font-size: 13px; max-width: 100%; }

#messages { flex: 1; overflow-y: auto; padding: 16px 12px 24px; -webkit-overflow-scrolling: touch; }
.msg { max-width: 820px; margin: 0 auto 18px; display: flex; flex-direction: column; }
.msg.user { align-items: flex-end; }
.bubble { padding: 10px 14px; border-radius: var(--radius); max-width: 100%; overflow-wrap: anywhere; }
.msg.user .bubble { background: var(--user-bubble); color: var(--user-fg); border-bottom-right-radius: 4px; max-width: 85%; white-space: pre-wrap; }
.msg.assistant .bubble { background: transparent; padding-left: 2px; padding-right: 2px; }
.msg.error .bubble { color: var(--danger); background: color-mix(in srgb, var(--danger) 10%, transparent); }
.msg-images { display: flex; flex-wrap: wrap; gap: 6px; margin-bottom: 6px; justify-content: flex-end; }
.msg-images img { width: 120px; height: 120px; object-fit: cover; border-radius: 10px; cursor: zoom-in; }
.msg-files { display: flex; flex-wrap: wrap; gap: 6px; margin-bottom: 6px; justify-content: flex-end; }
.file-chip { font-size: 13px; padding: 4px 10px; border-radius: 8px; background: var(--bg3); color: var(--fg); }
.msg-actions { display: flex; gap: 2px; margin-top: 4px; opacity: .55; }
.msg-actions:hover, .msg:focus-within .msg-actions { opacity: 1; }
.msg-actions button { border: none; background: none; cursor: pointer; padding: 6px 8px; border-radius: 8px; font-size: 13px; color: var(--fg2); min-height: 32px; }
.msg-actions button:hover { background: var(--bg3); }
.typing::after { content: "▍"; animation: blink 1s steps(2) infinite; }
@keyframes blink { 50% { opacity: 0; } }

/* Markdown */
.bubble p { margin: .4em 0; }
.bubble pre { position: relative; background: var(--code-bg); border: 1px solid var(--border); border-radius: 10px; padding: 12px; overflow-x: auto; font-size: 13.5px; }
.bubble pre code.hljs { background: transparent; padding: 0; }
.bubble code { font-family: ui-monospace, "SF Mono", Menlo, Consolas, monospace; }
.bubble :not(pre) > code { background: var(--bg3); padding: 1px 5px; border-radius: 5px; font-size: .9em; }
.bubble table { border-collapse: collapse; display: block; overflow-x: auto; }
.bubble th, .bubble td { border: 1px solid var(--border); padding: 6px 10px; }
.bubble blockquote { margin: .5em 0; padding-left: 12px; border-left: 3px solid var(--border); color: var(--fg2); }
.bubble img { max-width: 100%; }
.copy-code { position: absolute; top: 6px; right: 6px; font-size: 12px; padding: 3px 8px; border-radius: 6px;
  border: 1px solid var(--border); background: var(--bg); cursor: pointer; opacity: .8; }

/* 欢迎页 */
.welcome { position: absolute; inset: 30% 16px auto; text-align: center; pointer-events: none; }
.welcome h2 { font-weight: 600; margin-bottom: 20px; }
.chips { display: flex; flex-wrap: wrap; gap: 8px; justify-content: center; pointer-events: auto; max-width: 640px; margin: 0 auto; }
.chips button { border: 1px solid var(--border); background: var(--bg2); padding: 8px 14px; border-radius: 20px; cursor: pointer; font-size: 14px; }

/* ---------- 输入区 ---------- */
#composer { padding: 8px 12px; padding-bottom: calc(10px + var(--safe-bottom)); background: var(--bg); }
.composer-box { max-width: 820px; margin: 0 auto; display: flex; align-items: flex-end; gap: 4px; padding: 6px;
  border: 1px solid var(--border); border-radius: 22px; background: var(--bg2); transition: border-color .15s; }
.composer-box.dragover { border-color: var(--accent); border-style: dashed; }
#input { flex: 1; border: none; outline: none; resize: none; background: transparent; padding: 8px 4px; max-height: 40vh; line-height: 1.5; font-size: 16px; }
.attachments { max-width: 820px; margin: 0 auto 6px; display: flex; flex-wrap: wrap; gap: 6px; }
.attachments:empty { display: none; }
.att { position: relative; display: flex; align-items: center; gap: 6px; background: var(--bg3); border-radius: 10px; padding: 4px 28px 4px 4px; font-size: 13px; max-width: 220px; }
.att img { width: 44px; height: 44px; object-fit: cover; border-radius: 8px; }
.att .name { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; padding-left: 4px; }
.att .rm { position: absolute; right: 4px; top: 50%; transform: translateY(-50%); border: none; background: none; cursor: pointer; font-size: 16px; color: var(--fg2); }

/* ---------- 通用 ---------- */
.icon-btn { width: 40px; height: 40px; flex-shrink: 0; border: none; background: none; border-radius: 50%; cursor: pointer; font-size: 18px;
  display: inline-flex; align-items: center; justify-content: center; color: var(--fg2); }
.icon-btn:hover { background: var(--bg3); }
.icon-btn.send { background: var(--accent); color: var(--accent-fg); }
.icon-btn.send.stop { background: var(--danger); }
.icon-btn.recording { background: var(--danger); color: #fff; animation: pulse 1.2s infinite; }
@keyframes pulse { 50% { opacity: .6; } }
.btn { padding: 10px 16px; border-radius: 10px; border: 1px solid var(--border); background: var(--bg); cursor: pointer; min-height: 44px; }
.btn.primary { background: var(--accent); color: var(--accent-fg); border-color: var(--accent); }
.btn.danger { color: var(--danger); }
.btn.ghost { background: transparent; }
.btn.block { width: 100%; }
:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }

dialog { border: none; border-radius: 16px; padding: 0; width: min(520px, calc(100vw - 24px)); max-height: 90dvh; background: var(--bg); color: var(--fg); }
dialog::backdrop { background: rgba(0,0,0,.4); }
#settingsForm { padding: 20px; display: grid; gap: 12px; overflow-y: auto; }
#settingsForm h2 { margin: 0 0 4px; }
#settingsForm label { display: grid; gap: 4px; font-size: 14px; color: var(--fg2); }
#settingsForm input:not([type=checkbox]), #settingsForm textarea { padding: 10px 12px; border-radius: 10px; border: 1px solid var(--border); background: var(--bg2); color: var(--fg); font-size: 16px; }
#settingsForm label.check { display: flex; align-items: center; gap: 8px; color: var(--fg); }
#settingsForm .row { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; }
.actions { display: flex; justify-content: flex-end; gap: 8px; }
.hint { font-size: 12px; color: var(--fg2); margin: 0; }

#toast { position: fixed; left: 50%; bottom: calc(90px + var(--safe-bottom)); transform: translateX(-50%); background: var(--fg); color: var(--bg);
  padding: 8px 16px; border-radius: 20px; font-size: 14px; z-index: 50; max-width: 90vw; }
.lightbox { position: fixed; inset: 0; background: rgba(0,0,0,.85); z-index: 60; display: flex; align-items: center; justify-content: center; }
.lightbox img { max-width: 95vw; max-height: 90dvh; }
