:root {
  --bg: #f7f6f3;
  --surface: #ffffff;
  --surface-2: #efede8;
  --text: #1c1b19;
  --muted: #77736b;
  --line: #e2dfd8;
  --accent: #4b45d6;
  --accent-text: #ffffff;
  --user-bubble: #e9e7ff;
  --danger: #c0392b;
  --ok: #2e8b57;
  --radius: 14px;
  --sat: env(safe-area-inset-top, 0px);
  --sab: env(safe-area-inset-bottom, 0px);
  color-scheme: light dark;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #16161a;
    --surface: #1f1f25;
    --surface-2: #2a2a32;
    --text: #ecebe8;
    --muted: #9a978f;
    --line: #32323b;
    --accent: #8f8aff;
    --accent-text: #14141a;
    --user-bubble: #2f2d5c;
    --danger: #ff7b6b;
    --ok: #5fcf96;
  }
}
* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
html, body { margin: 0; height: 100%; background: var(--bg); color: var(--text); overscroll-behavior: none; }
body {
  font: 16px/1.6 -apple-system, BlinkMacSystemFont, "PingFang SC", "Helvetica Neue", "Microsoft YaHei", sans-serif;
  -webkit-text-size-adjust: 100%;
}
[hidden] { display: none !important; }
button, input, select, textarea { font: inherit; color: inherit; }
h1, h2, h3, p { margin: 0; }
.muted { color: var(--muted); font-size: 14px; }
.error { color: var(--danger); font-size: 14px; min-height: 1.4em; }

#app { height: var(--vh, 100dvh); display: flex; flex-direction: column; overflow: hidden; }
.screen { flex: 1; min-height: 0; display: flex; }

/* buttons */
.btn {
  border: 1px solid var(--line); background: var(--surface); border-radius: 10px;
  padding: 9px 16px; cursor: pointer; white-space: nowrap;
}
.btn:active { opacity: .7; }
.btn.primary { background: var(--accent); border-color: var(--accent); color: var(--accent-text); font-weight: 600; }
.btn.danger { color: var(--danger); }
.btn.block { width: 100%; }
.btn.small { padding: 5px 11px; font-size: 14px; }
.icon-btn { border: 0; background: none; width: 44px; height: 44px; font-size: 20px; cursor: pointer; border-radius: 10px; flex: none; }
.icon-btn:active { background: var(--surface-2); }
.row { display: flex; gap: 10px; flex-wrap: wrap; align-items: flex-end; }
.row.between { justify-content: space-between; }
.row.end { justify-content: flex-end; }

/* login */
#login { align-items: center; justify-content: center; padding: 24px; }
.login-card { width: 100%; max-width: 320px; display: flex; flex-direction: column; gap: 14px; align-items: center; text-align: center; }
.login-card img { border-radius: 16px; }
.login-card input { width: 100%; padding: 12px 14px; border-radius: 10px; border: 1px solid var(--line); background: var(--surface); text-align: center; }
.login-card .btn { width: 100%; padding: 12px; }

/* layout */
#main { position: relative; }
#drawer {
  width: 284px; flex: none; background: var(--surface); border-right: 1px solid var(--line);
  display: flex; flex-direction: column; gap: 12px;
  padding: calc(var(--sat) + 12px) 12px calc(var(--sab) + 12px);
}
#content { flex: 1; min-width: 0; display: flex; flex-direction: column; }
#bar {
  display: flex; align-items: center; gap: 4px; flex: none;
  padding: calc(var(--sat) + 4px) 6px 4px; border-bottom: 1px solid var(--line); background: var(--bg);
}
#title { flex: 1; font-size: 16px; font-weight: 600; text-align: center; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.view { flex: 1; min-height: 0; display: flex; flex-direction: column; }
#scrim { position: absolute; inset: 0; background: rgba(0,0,0,.4); z-index: 9; }

@media (max-width: 760px) {
  #drawer { position: absolute; z-index: 10; top: 0; bottom: 0; left: 0; width: min(84vw, 320px); transform: translateX(-100%); transition: transform .22s ease; }
  #main.drawer-open #drawer { transform: none; box-shadow: 0 0 40px rgba(0,0,0,.25); }
}
@media (min-width: 761px) {
  #menu-btn, #scrim { display: none !important; }
}
@media (prefers-reduced-motion: reduce) { #drawer { transition: none; } }

/* sessions */
.session-list { flex: 1; overflow-y: auto; display: flex; flex-direction: column; gap: 2px; margin: 0 -4px; -webkit-overflow-scrolling: touch; }
.session { display: flex; align-items: center; border-radius: 10px; }
.session a { flex: 1; min-width: 0; padding: 10px 10px; color: inherit; text-decoration: none; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; font-size: 15px; }
.session.active { background: var(--surface-2); }
.session .del { border: 0; background: none; color: var(--muted); width: 36px; height: 36px; cursor: pointer; flex: none; font-size: 16px; }
.session-empty { padding: 10px; }
.drawer-foot { display: flex; flex-direction: column; gap: 2px; border-top: 1px solid var(--line); padding-top: 8px; }
.nav-link { padding: 10px; border-radius: 10px; color: inherit; text-decoration: none; }
.nav-link.active, .nav-link:active { background: var(--surface-2); }

/* messages */
.messages { flex: 1; overflow-y: auto; padding: 16px 16px 8px; -webkit-overflow-scrolling: touch; }
.messages > * { max-width: 760px; margin-left: auto; margin-right: auto; }
.empty { height: 100%; display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 8px; color: var(--muted); text-align: center; }
.empty img { border-radius: 14px; opacity: .9; }
.msg { margin-bottom: 14px; overflow-wrap: anywhere; }
.msg.user { display: flex; justify-content: flex-end; }
.msg.user .bubble { background: var(--user-bubble); padding: 9px 14px; border-radius: 18px 18px 4px 18px; max-width: 86%; white-space: pre-wrap; }
.msg.assistant { margin-bottom: 6px; }
.md > :first-child { margin-top: 0; }
.md > :last-child { margin-bottom: 0; }
.md p, .md ul, .md ol, .md pre, .md table, .md blockquote { margin: 0 0 .7em; }
.md h1, .md h2, .md h3, .md h4 { margin: 1em 0 .4em; line-height: 1.35; }
.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 { padding-left: 1.4em; }
.md li { margin: .2em 0; }
.md a { color: var(--accent); }
.md code { font: 14px/1.5 ui-monospace, SFMono-Regular, Menlo, monospace; background: var(--surface-2); padding: 1px 5px; border-radius: 5px; }
.md pre { background: var(--surface-2); padding: 12px; border-radius: 10px; overflow-x: auto; }
.md pre code { background: none; padding: 0; white-space: pre; }
.md blockquote { border-left: 3px solid var(--line); padding-left: 12px; color: var(--muted); }
.md .table-wrap { overflow-x: auto; margin-bottom: .7em; }
.md table { border-collapse: collapse; font-size: 14px; margin: 0; }
.md th, .md td { border: 1px solid var(--line); padding: 5px 10px; text-align: left; }
.md img { max-width: 100%; border-radius: 8px; }
.md hr { border: 0; border-top: 1px solid var(--line); margin: 1em 0; }
.tool { margin: 0 0 8px; font-size: 13px; color: var(--muted); border: 1px solid var(--line); border-radius: 10px; background: var(--surface); }
.tool summary { padding: 6px 10px; cursor: pointer; list-style: none; display: flex; gap: 6px; align-items: center; }
.tool summary::-webkit-details-marker { display: none; }
.tool .name { font-weight: 600; flex: none; }
.tool .what { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; font-family: ui-monospace, Menlo, monospace; font-size: 12px; }
.tool .dot { width: 7px; height: 7px; border-radius: 50%; background: var(--ok); flex: none; }
.tool.running .dot { background: var(--accent); animation: pulse 1s infinite; }
.tool.error .dot { background: var(--danger); }
.tool pre { margin: 0; padding: 8px 10px; border-top: 1px solid var(--line); overflow-x: auto; max-height: 240px; font: 12px/1.5 ui-monospace, Menlo, monospace; white-space: pre-wrap; }
.thinking { color: var(--muted); font-size: 14px; margin-bottom: 8px; }
.thinking::after { content: "…"; animation: pulse 1.2s infinite; }
@keyframes pulse { 50% { opacity: .3; } }
@media (prefers-reduced-motion: reduce) { .thinking::after, .tool.running .dot { animation: none; } }

/* composer */
.composer { flex: none; display: flex; gap: 8px; align-items: flex-end; padding: 8px 12px calc(var(--sab) + 10px); max-width: 792px; width: 100%; margin: 0 auto; }
.composer textarea {
  flex: 1; resize: none; max-height: 160px; padding: 10px 14px; border-radius: 20px;
  border: 1px solid var(--line); background: var(--surface); outline: none; line-height: 1.45;
}
.composer textarea:focus { border-color: var(--accent); }
.send { flex: none; width: 42px; height: 42px; border-radius: 50%; border: 0; background: var(--accent); color: var(--accent-text); font-size: 20px; font-weight: 700; cursor: pointer; }
.send:disabled { opacity: .4; }
.send.stop { background: var(--text); color: var(--bg); font-size: 14px; }

/* pages */
.page { overflow-y: auto; -webkit-overflow-scrolling: touch; }
.page-inner { max-width: 720px; width: 100%; margin: 0 auto; padding: 16px 16px calc(var(--sab) + 24px); display: flex; flex-direction: column; gap: 14px; align-items: flex-start; }
.cards { display: flex; flex-direction: column; gap: 10px; width: 100%; }
.card { width: 100%; background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius); padding: 14px; display: flex; flex-direction: column; gap: 8px; }
.card h3 { font-size: 16px; }
.card .prompt { font-size: 14px; color: var(--muted); display: -webkit-box; -webkit-line-clamp: 2; -webkit-box-orient: vertical; overflow: hidden; }
.card .meta { font-size: 13px; color: var(--muted); }
.card .head { display: flex; align-items: center; gap: 10px; }
.card .head h3 { flex: 1; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.card.off { opacity: .6; }
.switch { appearance: none; width: 44px; height: 26px; border-radius: 13px; background: var(--surface-2); position: relative; cursor: pointer; flex: none; margin: 0; border: 1px solid var(--line); }
.switch::after { content: ""; position: absolute; top: 2px; left: 2px; width: 20px; height: 20px; border-radius: 50%; background: #fff; box-shadow: 0 1px 3px rgba(0,0,0,.3); transition: transform .15s; }
.switch:checked { background: var(--ok); border-color: var(--ok); }
.switch:checked::after { transform: translateX(18px); }
.section-title { font-size: 14px; color: var(--muted); font-weight: 600; margin-top: 8px; }
.runs { width: 100%; display: flex; flex-direction: column; }
.run { display: block; padding: 10px 2px; border-bottom: 1px solid var(--line); color: inherit; text-decoration: none; font-size: 14px; }
.run .top { display: flex; gap: 8px; justify-content: space-between; }
.run .sum { color: var(--muted); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.run .fail { color: var(--danger); }

/* dialog */
dialog { border: 0; border-radius: 16px; padding: 0; width: min(92vw, 480px); background: var(--surface); color: var(--text); max-height: 90dvh; }
dialog::backdrop { background: rgba(0,0,0,.45); }
dialog form { padding: 18px; display: flex; flex-direction: column; gap: 12px; }
dialog label { display: flex; flex-direction: column; gap: 4px; font-size: 13px; color: var(--muted); flex: 1; min-width: 120px; }
dialog input:not([type=checkbox]), dialog select, dialog textarea { padding: 9px 11px; border-radius: 9px; border: 1px solid var(--line); background: var(--bg); color: var(--text); font-size: 16px; width: 100%; }
dialog label.check { flex-direction: row; align-items: center; gap: 8px; font-size: 15px; color: var(--text); }

#toast { position: fixed; left: 50%; bottom: calc(var(--sab) + 84px); transform: translateX(-50%); background: var(--text); color: var(--bg); padding: 9px 16px; border-radius: 20px; font-size: 14px; z-index: 50; max-width: 86vw; text-align: center; }
.composer textarea { scrollbar-width: none; }
.composer textarea::-webkit-scrollbar { display: none; }

/* voice input */
.composer { align-items: center; }
.round { flex: none; width: 44px; height: 44px; border-radius: 50%; border: 1px solid var(--line); background: var(--surface); color: var(--text); display: grid; place-items: center; cursor: pointer; }
.round:active { background: var(--surface-2); }
.composer .i-mic, .composer.text .i-kbd { display: none; }
.composer.text .i-mic { display: block; }
.hold { flex: 1; height: 46px; border-radius: 23px; border: 1px solid var(--line); background: var(--surface); color: var(--text); font-weight: 600; letter-spacing: .08em; cursor: pointer;
  touch-action: none; user-select: none; -webkit-user-select: none; -webkit-touch-callout: none; transition: transform .12s, background .12s; }
.hold.on { transform: scale(.97); background: var(--surface-2); }
.hold:disabled { opacity: .45; }

.voice { position: fixed; inset: 0; z-index: 40; pointer-events: none; color: #fff;
  background: rgba(8, 8, 18, .88); animation: voice-in .18s ease-out; }
@keyframes voice-in { from { opacity: 0; } }
#voice-fx { position: absolute; left: 0; bottom: 0; width: 100%; height: 56%; transform: translateZ(0); }
.voice.nogl #voice-fx { background: radial-gradient(120% 70% at 50% 100%, rgba(120,100,255,.85), rgba(190,90,255,.4) 45%, transparent 75%); }
.voice.nogl.cancel #voice-fx { background: radial-gradient(120% 70% at 50% 100%, rgba(255,70,80,.85), transparent 70%); }
.voice-body { position: absolute; left: 0; right: 0; top: 0; bottom: 0; padding: 0 28px calc(var(--sab) + 34px); display: flex; flex-direction: column; align-items: center; justify-content: flex-end; }
.voice-text { margin-bottom: 44vh; }
.voice-text { font-size: 22px; line-height: 1.5; font-weight: 600; text-align: center; overflow-wrap: anywhere; max-height: 9em; overflow: hidden; display: flex; flex-direction: column; justify-content: flex-end; text-shadow: 0 1px 12px rgba(0,0,0,.35); }
.voice-text.idle { font-weight: 400; opacity: .7; }
.voice-hint { font-size: 14px; letter-spacing: .06em; font-weight: 600; text-shadow: 0 1px 10px rgba(0,0,30,.6); }
.voice.cancel .voice-text { opacity: .45; }
@media (prefers-reduced-motion: reduce) { .voice { animation: none; } .hold { transition: none; } }
.select { padding: 9px 11px; border-radius: 9px; border: 1px solid var(--line); background: var(--bg); color: var(--text); font-size: 16px; width: 100%; }
