:root {
  --bg: #0b1120; --panel: #111827; --panel2: #1f2937; --line: #263042; --text: #e5e7eb; --muted: #94a3b8;
  --accent: #60a5fa; --accent2: #2563eb; --ok: #34d399; --warn: #fbbf24; --err: #f87171; --peer: #a78bfa;
  --radius: 12px; --app-h: 100dvh;
  color-scheme: dark;
  font-family: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
}
* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; background: var(--bg); color: var(--text); overflow: hidden; overscroll-behavior: none; -webkit-text-size-adjust: 100%; }
a { color: var(--accent); text-decoration: none; }
button, .button { font: inherit; color: var(--text); background: var(--panel2); border: 1px solid var(--line); border-radius: 9px; padding: 8px 12px; min-height: 40px; cursor: pointer; display: inline-flex; align-items: center; justify-content: center; gap: 6px; touch-action: manipulation; }
button:hover, .button:hover { border-color: #3b4a63; }
button.primary, .button.primary { background: var(--accent2); border-color: var(--accent2); color: white; }
button.danger { color: var(--err); }
button.small { min-height: 30px; padding: 4px 8px; font-size: 0.85em; }
button.link { background: none; border: none; color: var(--accent); padding: 0; min-height: 0; }
button:disabled { opacity: 0.5; }
input, textarea, select { font: inherit; color: var(--text); background: #0f172a; border: 1px solid var(--line); border-radius: 9px; padding: 9px 11px; min-height: 40px; width: 100%; }
textarea { resize: vertical; }
input:focus, textarea:focus, select:focus { outline: 2px solid #1d4ed8; outline-offset: 0; }
code, pre, .mono { font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace; }
.boot { display: grid; place-items: center; height: 100%; color: var(--muted); }

#app { display: flex; height: var(--app-h); width: 100%; position: fixed; inset: 0 0 auto 0; }

/* drawer / hierarchy */
#drawer { width: 300px; flex: none; background: var(--panel); border-right: 1px solid var(--line); display: flex; flex-direction: column; overflow: hidden; padding-top: env(safe-area-inset-top); }
.brand { display: flex; align-items: center; gap: 10px; padding: 14px 16px; font-weight: 650; }
.brand img { width: 28px; height: 28px; border-radius: 7px; }
.live { margin-left: auto; font-size: 0.75em; font-weight: 500; color: var(--warn); }
#tree { flex: 1; overflow-y: auto; padding: 4px 8px 8px; }
.tree-root { color: var(--muted); font-size: 0.8em; padding: 6px 10px; text-transform: uppercase; letter-spacing: 0.06em; }
.node { display: block; color: var(--text); padding: 9px 10px 9px calc(10px + (var(--depth) - 1) * 16px); border-radius: 10px; margin: 2px 0; border-left: 2px solid transparent; }
.node:hover { background: var(--panel2); }
.node.selected { background: #172554; border-left-color: var(--accent); }
.node-head { display: flex; align-items: center; gap: 8px; }
.node-name { font-weight: 600; }
.node-state { margin-left: auto; font-size: 0.75em; color: var(--muted); }
.node-role { font-size: 0.8em; color: var(--muted); margin-top: 3px; display: -webkit-box; -webkit-line-clamp: 2; -webkit-box-orient: vertical; overflow: hidden; }
.node-meta { font-size: 0.72em; color: #64748b; margin-top: 3px; }
.navlinks { border-top: 1px solid var(--line); padding: 8px; display: flex; flex-direction: column; padding-bottom: calc(8px + env(safe-area-inset-bottom)); }
.navlinks a { padding: 10px; border-radius: 9px; color: var(--text); display: flex; align-items: center; }
.navlinks a:hover { background: var(--panel2); }
.badge { background: var(--err); color: white; border-radius: 999px; font-size: 0.7em; padding: 1px 7px; margin-left: 8px; }
.badge:empty { display: none; }
.dot { width: 9px; height: 9px; border-radius: 50%; flex: none; display: inline-block; background: #475569; }
.dot.idle { background: var(--ok); } .dot.busy { background: var(--accent); animation: pulse 1.2s infinite; } .dot.ask { background: var(--warn); }
.dot.warn { background: var(--warn); } .dot.err { background: var(--err); } .dot.off { background: #475569; }
@keyframes pulse { 50% { opacity: 0.35; } }

/* main */
#main { flex: 1; min-width: 0; display: flex; flex-direction: column; }
#top { display: flex; align-items: center; gap: 10px; padding: 8px 12px; padding-top: calc(8px + env(safe-area-inset-top)); border-bottom: 1px solid var(--line); background: var(--panel); min-height: 52px; }
#top .icon { background: none; border: none; font-size: 1.2em; min-height: 36px; padding: 4px 8px; color: var(--text); position: relative; }
#menu { display: none; }
#title { display: flex; align-items: center; gap: 8px; min-width: 0; }
.t-name { font-weight: 650; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.t-sub { color: var(--muted); font-size: 0.8em; white-space: nowrap; }
#tabs { display: flex; gap: 4px; margin-left: auto; }
#tabs a { padding: 7px 11px; border-radius: 8px; color: var(--muted); }
#tabs a.active { background: var(--panel2); color: var(--text); }
#bell .badge { position: absolute; top: 0; right: -2px; margin: 0; }
#view { flex: 1; min-height: 0; position: relative; }
#scrim { display: none; }

/* chat */
.chat { display: flex; flex-direction: column; height: 100%; position: relative; }
.chat-scroll { flex: 1; overflow-y: auto; padding: 14px 14px 6px; -webkit-overflow-scrolling: touch; overscroll-behavior: contain; }
.messages { display: flex; flex-direction: column; gap: 10px; max-width: 860px; margin: 0 auto; }
.older { display: block; margin: 0 auto 10px; font-size: 0.85em; }
.empty { color: var(--muted); text-align: center; margin: 40px 0; }
.msg { max-width: 100%; overflow-wrap: anywhere; }
.msg .who { font-size: 0.75em; color: var(--muted); margin-bottom: 3px; display: flex; gap: 6px; align-items: baseline; }
.msg .who time { margin-left: auto; }
.msg.user { align-self: flex-end; background: #1e3a8a; border-radius: var(--radius) var(--radius) 4px var(--radius); padding: 9px 12px; max-width: min(88%, 680px); }
.msg.user .who { color: #bfdbfe; }
.msg.peer { align-self: flex-start; background: #2e1065; border: 1px solid #4c1d95; border-radius: var(--radius); padding: 9px 12px; max-width: min(92%, 720px); }
.msg.peer .who a { color: #c4b5fd; font-weight: 600; }
.msg.assistant { padding: 2px 2px; }
.msg.assistant .who { color: #93c5fd; font-weight: 600; }
.msg.progress { color: var(--muted); font-size: 0.9em; border-left: 2px solid var(--line); padding-left: 10px; }
.msg.thinking, .msg.system { color: var(--muted); font-size: 0.88em; border: 1px dashed var(--line); border-radius: 10px; padding: 6px 10px; }
.msg.thinking summary, .msg.system summary { cursor: pointer; display: flex; gap: 8px; }
.msg.thinking summary time, .msg.system summary time { margin-left: auto; font-size: 0.85em; }
.msg.outbox { opacity: 0.85; }
.status.failed, .status.uncertain { color: var(--warn); }
.status-detail { font-size: 0.78em; color: #fde68a; margin-top: 4px; }
.status-detail.pending { color: var(--muted); }
.turn-end { height: 0; }
.md p { margin: 0.35em 0; } .md p:first-child { margin-top: 0; } .md p:last-child { margin-bottom: 0; }
.md pre { background: #020617; border: 1px solid var(--line); border-radius: 8px; padding: 10px; overflow-x: auto; font-size: 0.85em; }
.md code { background: #0f172a; border-radius: 5px; padding: 1px 5px; font-size: 0.9em; }
.md pre code { background: none; padding: 0; }
.md table { border-collapse: collapse; display: block; overflow-x: auto; } .md td, .md th { border: 1px solid var(--line); padding: 4px 8px; }
.md img { max-width: 100%; }
.md ul, .md ol { padding-left: 1.3em; }
.md blockquote { border-left: 3px solid var(--line); margin: 0.4em 0; padding-left: 10px; color: var(--muted); }
.newpill { position: absolute; left: 50%; transform: translateX(-50%); bottom: 130px; display: none; background: var(--accent2); color: white; border: none; box-shadow: 0 4px 16px #0008; }
.newpill.show { display: inline-flex; }
.chat-bottom { border-top: 1px solid var(--line); background: var(--panel); padding: 8px 10px calc(8px + env(safe-area-inset-bottom)); }
.activity { display: flex; align-items: center; gap: 6px; font-size: 0.8em; color: var(--muted); max-width: 860px; margin: 0 auto 6px; white-space: nowrap; overflow: hidden; }
.activity .detail { overflow: hidden; text-overflow: ellipsis; }
.activity .term-link { margin-left: auto; }
.composer { display: flex; gap: 8px; align-items: flex-end; max-width: 860px; margin: 0 auto; }
.composer textarea { min-height: 44px; max-height: 200px; resize: none; line-height: 1.35; font-size: 16px; }
.composer-actions { display: flex; gap: 6px; }
.composer .mode { font-size: 0.8em; white-space: nowrap; }
.question-slot { max-width: 860px; margin: 0 auto; }
.qcard { background: #3b2a06; border: 1px solid #a16207; border-radius: var(--radius); padding: 10px 12px; margin-bottom: 8px; }
.qcard.warn { background: #3f1d1d; border-color: #b91c1c; }
.qtitle { font-weight: 650; margin-bottom: 4px; }
.qopts { display: flex; flex-wrap: wrap; gap: 6px; margin: 8px 0; }
.qopts button.selected { border-color: var(--warn); }
.qfree { display: flex; gap: 6px; }

/* terminal */
.terminal { display: flex; flex-direction: column; height: 100%; background: var(--bg); }
.term-wrap { flex: 1; min-height: 0; position: relative; }
.term-host { position: absolute; inset: 4px 4px 0 6px; touch-action: none; }
.term-host .xterm { height: 100%; }
.term-overlay { position: absolute; top: 10px; left: 50%; transform: translateX(-50%); background: #1e293b; border: 1px solid var(--line); border-radius: 999px; padding: 6px 14px; font-size: 0.85em; color: var(--warn); display: none; z-index: 5; }
.term-overlay.show { display: block; }
.keybar { display: flex; gap: 6px; overflow-x: auto; padding: 6px 8px; background: var(--panel); border-top: 1px solid var(--line); scrollbar-width: none; -webkit-overflow-scrolling: touch; }
.keybar::-webkit-scrollbar { display: none; }
.keybar button { flex: none; min-width: 44px; min-height: 40px; padding: 4px 10px; font-family: ui-monospace, Menlo, monospace; font-size: 0.9em; }
.keybar button.on { background: var(--accent2); border-color: var(--accent2); }
.term-compose { display: flex; gap: 6px; padding: 6px 8px calc(6px + env(safe-area-inset-bottom)); background: var(--panel); }
.term-compose textarea { min-height: 42px; max-height: 120px; resize: none; font-size: 16px; font-family: ui-monospace, Menlo, monospace; }
.sel { position: fixed; inset: 0; z-index: 50; background: var(--bg); display: flex; flex-direction: column; height: var(--app-h); }
.sel-bar { display: flex; align-items: center; gap: 8px; padding: 10px; padding-top: calc(10px + env(safe-area-inset-top)); border-bottom: 1px solid var(--line); background: var(--panel); }
.sel-bar span { margin-right: auto; font-weight: 600; }
.sel-text { flex: 1; margin: 0; padding: 12px; overflow: auto; white-space: pre; font-size: 12px; user-select: text; -webkit-user-select: text; -webkit-touch-callout: default; }

/* pages */
.page { height: 100%; overflow-y: auto; padding: 16px 14px calc(30px + env(safe-area-inset-bottom)); -webkit-overflow-scrolling: touch; }
.page > * { max-width: 860px; margin-left: auto; margin-right: auto; }
.card { background: var(--panel); border: 1px solid var(--line); border-radius: var(--radius); padding: 14px 16px; margin-bottom: 14px; }
.card h2 { font-size: 1.02em; margin: 0 0 10px; }
.hero h1 { font-size: 1.3em; margin: 0; }
.hero-top { display: flex; align-items: center; gap: 10px; }
.hero code { color: var(--muted); }
.role { color: var(--muted); }
.detail { color: var(--warn); font-size: 0.9em; }
.chips { display: flex; flex-wrap: wrap; gap: 6px; margin: 6px 0; }
.chip { background: var(--panel2); border: 1px solid var(--line); border-radius: 999px; padding: 3px 10px; font-size: 0.8em; display: inline-flex; align-items: center; gap: 4px; color: var(--text); }
.chip.ok { color: var(--ok); } .chip.warn { color: var(--warn); }
.chip .x { min-height: 0; padding: 0 4px; border: none; background: none; color: var(--muted); }
.row { display: flex; gap: 8px; flex-wrap: wrap; align-items: center; margin: 8px 0; }
.row input, .row select { flex: 1; min-width: 8em; width: auto; }
.grid2 { display: grid; grid-template-columns: 1fr 1fr; gap: 12px; }
.field { display: block; margin: 10px 0; }
.field .label, .label { display: block; font-size: 0.8em; color: var(--muted); margin-bottom: 4px; }
.hint { color: var(--muted); font-size: 0.82em; }
.muted { color: var(--muted); }
.err { color: var(--err); }
.list { list-style: none; padding: 0; margin: 0; }
.list li { padding: 10px 0; border-bottom: 1px solid var(--line); display: flex; flex-direction: column; gap: 4px; }
.list li:last-child { border-bottom: none; }
.list li.unread strong::before { content: "● "; color: var(--accent); }
.files { list-style: none; padding: 0; }
.memview { border-top: 1px solid var(--line); margin-top: 10px; padding-top: 10px; }
.memhead { display: flex; justify-content: space-between; align-items: center; margin-bottom: 6px; }
.jentry { border-bottom: 1px solid var(--line); padding: 8px 0; }
.switch { display: flex; align-items: center; gap: 8px; }
.switch input { width: auto; min-height: 0; }
.harness { border-top: 1px solid var(--line); padding: 10px 0; }
.login-step { display: flex; flex-wrap: wrap; gap: 8px; align-items: center; margin: 10px 0; }
.bigcode { font-size: 1.6em; letter-spacing: 0.12em; background: #0f172a; border: 1px solid var(--line); border-radius: 10px; padding: 6px 14px; user-select: all; }
.instructions { background: #0f172a; border-radius: 10px; padding: 10px; margin: 10px 0; }
.links { display: flex; flex-wrap: wrap; gap: 8px; margin: 10px 0; }
pre.small { font-size: 0.78em; white-space: pre-wrap; color: var(--muted); }
details summary { cursor: pointer; padding: 4px 0; }

.login { max-width: 360px; margin: 12vh auto 0; display: flex; flex-direction: column; gap: 12px; padding: 24px; text-align: center; }
.login img { width: 64px; height: 64px; margin: 0 auto; border-radius: 14px; }
.login h1 { margin: 0 0 8px; }
.login .msg { color: var(--err); min-height: 1.2em; }
#app:has(.login) { position: static; display: block; overflow-y: auto; }

#toast { position: fixed; left: 50%; bottom: calc(24px + env(safe-area-inset-bottom)); transform: translateX(-50%) translateY(30px); background: #1e293b; border: 1px solid var(--line); color: var(--text); padding: 10px 16px; border-radius: 10px; opacity: 0; pointer-events: none; transition: all 0.2s; z-index: 100; max-width: 92vw; }
#toast.show { opacity: 1; transform: translateX(-50%) translateY(0); }
#toast.error { border-color: var(--err); color: #fecaca; }

/* phones */
@media (max-width: 860px) {
  #menu { display: inline-flex; }
  #drawer { position: fixed; z-index: 30; top: 0; bottom: 0; left: 0; width: min(86vw, 320px); transform: translateX(-102%); transition: transform 0.2s ease; height: var(--app-h); }
  body.drawer-open #drawer { transform: none; }
  body.drawer-open #scrim { display: block; position: fixed; inset: 0; background: #0008; z-index: 20; }
  #top { gap: 6px; padding-left: 6px; padding-right: 6px; }
  .t-sub { display: none; }
  #tabs a { padding: 7px 8px; font-size: 0.92em; }
  .grid2 { grid-template-columns: 1fr; }
  .chat-scroll { padding: 10px 10px 4px; }
  .msg.user, .msg.peer { max-width: 94%; }
  .page { padding: 12px 10px calc(30px + env(safe-area-inset-bottom)); }
}
@media (max-width: 420px) {
  #title .t-name { max-width: 34vw; }
  .composer .mode { padding: 6px 8px; }
}
.msg.note { align-self: center; background: none; border: none; color: var(--muted); font-size: 0.8em; padding: 2px 8px; display: flex; gap: 8px; align-items: baseline; }
