:root {
  --bg: #faf8f4; --surface: #ffffff; --ink: #1c2330; --muted: #6b7280; --line: #e5e1d8;
  --accent: #2f5d8a; --accent-ink: #ffffff; --user: #e6eef7; --warn: #b45309; --bad: #b91c1c; --good: #15803d;
  --bar: #1f2937; --bar-ink: #f5f1e8;
}
@media (prefers-color-scheme: dark) {
  :root { --bg: #11151b; --surface: #1a2029; --ink: #e8e6e1; --muted: #98a2b3; --line: #2b3441;
          --accent: #7aa7d6; --accent-ink: #0b1118; --user: #22344a; --warn: #f59e0b; --bad: #f87171; --good: #4ade80; }
}
* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
html, body { margin: 0; height: 100%; background: var(--bg); color: var(--ink);
  font: 16px/1.45 -apple-system, system-ui, "Segoe UI", Roboto, sans-serif; overscroll-behavior: none; }
#app { height: 100dvh; display: flex; flex-direction: column; }
button { font: inherit; color: inherit; }
.hidden { display: none !important; }

/* top bar */
.top { flex: none; display: flex; align-items: center; gap: 8px; padding: calc(8px + env(safe-area-inset-top)) 12px 8px;
  background: var(--bar); color: var(--bar-ink); min-height: 52px; }
.top h1 { flex: 1; margin: 0; font-size: 1.05rem; font-weight: 600; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.top .icon-btn { color: var(--bar-ink); }
.chip { border: 1px solid rgba(255,255,255,.35); background: rgba(255,255,255,.1); color: var(--bar-ink); border-radius: 999px;
  padding: 6px 12px; min-height: 36px; font-size: .85rem; max-width: 60%; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.icon-btn { background: none; border: 0; min-width: 44px; min-height: 44px; font-size: 1.3rem; border-radius: 8px; cursor: pointer; }

/* main + tabs */
main { flex: 1; min-height: 0; overflow-y: auto; -webkit-overflow-scrolling: touch; }
.tabs { flex: none; display: grid; grid-template-columns: repeat(4, 1fr); background: var(--surface); border-top: 1px solid var(--line);
  padding-bottom: env(safe-area-inset-bottom); }
.tab { background: none; border: 0; min-height: 56px; display: flex; flex-direction: column; align-items: center; justify-content: center;
  gap: 2px; font-size: .72rem; color: var(--muted); cursor: pointer; }
.tab svg { width: 24px; height: 24px; stroke: currentColor; fill: none; stroke-width: 1.8; stroke-linecap: round; stroke-linejoin: round; }
.tab[aria-current="page"] { color: var(--accent); font-weight: 600; }

/* lists */
.pad { padding: 16px; }
.list { list-style: none; margin: 0; padding: 0; }
.row { display: flex; align-items: center; gap: 12px; width: 100%; text-align: left; background: var(--surface); border: 0;
  border-bottom: 1px solid var(--line); padding: 14px 16px; min-height: 64px; cursor: pointer; }
.row .grow { flex: 1; min-width: 0; }
.row .title { font-weight: 600; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.row .sub { color: var(--muted); font-size: .85rem; }
.muted { color: var(--muted); }
.empty { text-align: center; color: var(--muted); padding: 48px 24px; }
.btn { border: 1px solid var(--line); background: var(--surface); border-radius: 12px; min-height: 48px; padding: 0 18px; cursor: pointer; font-weight: 600; }
.btn.primary { background: var(--accent); color: var(--accent-ink); border-color: var(--accent); }
.btn.danger { color: var(--bad); }
.btn.block { width: 100%; }
input[type=text], input[type=password], textarea, select { width: 100%; font: inherit; color: var(--ink); background: var(--surface);
  border: 1px solid var(--line); border-radius: 12px; padding: 12px; }
label.field { display: block; margin: 12px 0; font-size: .9rem; color: var(--muted); }
label.field > input, label.field > textarea { margin-top: 4px; }

/* chat */
.chat { display: flex; flex-direction: column; height: 100%; min-height: 0; }
.stream { flex: 1; overflow-y: auto; padding: 12px; display: flex; flex-direction: column; gap: 10px; -webkit-overflow-scrolling: touch; }
.msg { max-width: 92%; padding: 10px 14px; border-radius: 16px; overflow-wrap: anywhere; }
.msg p { margin: 0 0 .6em; } .msg p:last-child { margin-bottom: 0; }
.msg.user { align-self: flex-end; background: var(--user); border-bottom-right-radius: 4px; }
.msg.assistant { align-self: flex-start; background: var(--surface); border: 1px solid var(--line); border-bottom-left-radius: 4px; }
.msg pre { white-space: pre-wrap; background: var(--bg); padding: 8px; border-radius: 8px; overflow-x: auto; }
.msg .meta { font-size: .75rem; color: var(--muted); margin-top: 6px; }
.msg .flag { font-size: .8rem; color: var(--warn); margin-top: 6px; }
.status-line { align-self: flex-start; color: var(--muted); font-size: .85rem; display: flex; align-items: center; gap: 8px; padding: 2px 6px; }
.dot { width: 8px; height: 8px; border-radius: 50%; background: var(--accent); animation: pulse 1.1s infinite ease-in-out; }
@keyframes pulse { 0%,100% { opacity: .25; } 50% { opacity: 1; } }
.banner { margin: 8px 12px 0; padding: 10px 12px; border-radius: 12px; background: var(--surface); border: 1px solid var(--warn); color: var(--ink); font-size: .9rem; }
.composer { flex: none; display: flex; align-items: flex-end; gap: 8px; padding: 8px 12px; background: var(--surface); border-top: 1px solid var(--line); }
.composer textarea { resize: none; max-height: 140px; min-height: 48px; line-height: 1.35; }
.send { flex: none; width: 48px; height: 48px; border-radius: 50%; border: 0; background: var(--accent); color: var(--accent-ink); font-size: 1.3rem; cursor: pointer; }
.send.stop { background: var(--bad); color: #fff; }
.send:disabled { opacity: .4; }

/* sheets */
.scrim { position: fixed; inset: 0; background: rgba(0,0,0,.45); display: flex; align-items: flex-end; z-index: 20; animation: fade .15s; }
.sheet { width: 100%; max-height: 88dvh; overflow-y: auto; background: var(--surface); border-radius: 20px 20px 0 0;
  padding: 8px 16px calc(16px + env(safe-area-inset-bottom)); animation: rise .2s ease-out; }
.sheet .grab { width: 40px; height: 5px; border-radius: 3px; background: var(--line); margin: 4px auto 12px; }
.sheet h2 { margin: 0 0 4px; font-size: 1.15rem; }
.sheet .ctx { color: var(--muted); margin: 0 0 12px; }
.sheet .body { white-space: pre-wrap; margin-bottom: 12px; }
.sheet .actions { display: grid; gap: 10px; margin-top: 14px; }
.sheet .actions .btn { min-height: 54px; font-size: 1.05rem; }
.opt { display: flex; align-items: center; gap: 12px; min-height: 52px; padding: 8px 12px; border: 1px solid var(--line); border-radius: 12px; margin-bottom: 8px; cursor: pointer; }
.opt input { width: 22px; height: 22px; }
@keyframes rise { from { transform: translateY(40px); opacity: .6; } }
@keyframes fade { from { opacity: 0; } }

.phase { display: flex; align-items: center; gap: 12px; padding: 12px 8px; border-bottom: 1px solid var(--line); border-radius: 8px; }
.phase .mark { width: 28px; height: 28px; border-radius: 50%; border: 2px solid var(--line); display: grid; place-items: center; font-size: .8rem; flex: none; }
.phase.complete .mark { background: var(--good); border-color: var(--good); color: #fff; }
.phase.current { background: var(--user); font-weight: 600; }
.phase.current .mark { border-color: var(--accent); color: var(--accent); }

#toast { position: fixed; left: 16px; right: 16px; bottom: calc(80px + env(safe-area-inset-bottom)); background: var(--bar); color: var(--bar-ink);
  padding: 12px 16px; border-radius: 12px; text-align: center; opacity: 0; pointer-events: none; transition: opacity .2s; z-index: 40; }
#toast.show { opacity: 1; }
.login { max-width: 420px; margin: 0 auto; padding: 48px 20px; }
