:root {
  --bg: #f6f5f2; --panel: #ffffff; --line: #e3e0da; --text: #1d1f23; --muted: #6b6f78;
  --accent: #b86a1f; --accent-ink: #fff; --ok: #2f7d4f; --warn: #a86b00; --err: #b3392f;
  --human: #e9f0fb; --server: #f3efe7; --peer: #eef6ef; --code: #f0eee9;
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #14161a; --panel: #1c1f24; --line: #2c3037; --text: #e7e8ea; --muted: #9298a2;
    --accent: #e8a35c; --accent-ink: #1a1308; --ok: #5fbf85; --warn: #e0b04a; --err: #ef7a6e;
    --human: #1d2a3d; --server: #2a2620; --peer: #1f2b22; --code: #16181c;
    color-scheme: dark;
  }
}
:root[data-theme="dark"] {
  --bg: #14161a; --panel: #1c1f24; --line: #2c3037; --text: #e7e8ea; --muted: #9298a2;
  --accent: #e8a35c; --accent-ink: #1a1308; --ok: #5fbf85; --warn: #e0b04a; --err: #ef7a6e;
  --human: #1d2a3d; --server: #2a2620; --peer: #1f2b22; --code: #16181c;
  color-scheme: dark;
}
* { box-sizing: border-box; }
html, body { margin: 0; background: var(--bg); color: var(--text); }
body { font: 15px/1.45 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif; -webkit-tap-highlight-color: transparent; }
header#top {
  position: sticky; top: 0; z-index: 5; display: flex; align-items: center; gap: 8px;
  padding: calc(10px + env(safe-area-inset-top)) 16px 10px; background: var(--bg); border-bottom: 1px solid var(--line);
}
header h1 { flex: 1; margin: 0; font-size: 17px; font-weight: 650; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
main { max-width: 760px; margin: 0 auto; padding: 12px 16px calc(96px + env(safe-area-inset-bottom)); }
button, input, textarea, select { font: inherit; color: inherit; }
button {
  border: 1px solid var(--line); background: var(--panel); border-radius: 10px; padding: 9px 14px; cursor: pointer;
  min-height: 40px;
}
button.primary { background: var(--accent); color: var(--accent-ink); border-color: var(--accent); font-weight: 600; }
button.danger { color: var(--err); border-color: color-mix(in srgb, var(--err) 45%, var(--line)); }
button.icon { border: 0; background: none; font-size: 22px; padding: 4px 8px; min-height: 36px; line-height: 1; }
button:disabled { opacity: .5; }
input, textarea, select {
  width: 100%; border: 1px solid var(--line); background: var(--panel); border-radius: 10px; padding: 10px 12px;
}
textarea { min-height: 96px; resize: vertical; }
label { display: block; font-size: 13px; color: var(--muted); margin: 12px 0 4px; }
.muted { color: var(--muted); }
.center { text-align: center; }
.small { font-size: 13px; }
.row { display: flex; gap: 8px; align-items: center; flex-wrap: wrap; }
.row.end { justify-content: flex-end; }
.grow { flex: 1; }
.card { background: var(--panel); border: 1px solid var(--line); border-radius: 14px; padding: 12px 14px; margin: 10px 0; }
.section-title { font-size: 12px; text-transform: uppercase; letter-spacing: .06em; color: var(--muted); margin: 20px 2px 6px; }
.dot { width: 10px; height: 10px; border-radius: 50%; background: var(--muted); flex: none; }
.dot.on { background: var(--ok); } .dot.off { background: var(--err); }

.banner { border-radius: 14px; padding: 12px 14px; margin: 6px 0 10px; display: flex; gap: 10px; align-items: center; }
.banner.off { background: color-mix(in srgb, var(--err) 14%, var(--panel)); border: 1px solid color-mix(in srgb, var(--err) 40%, var(--line)); }
.banner.on { background: var(--panel); border: 1px solid var(--line); }
.banner strong { display: block; }

.thread { display: block; text-decoration: none; color: inherit; padding: 12px 14px; border-bottom: 1px solid var(--line); }
.thread:last-child { border-bottom: 0; }
.list { background: var(--panel); border: 1px solid var(--line); border-radius: 14px; overflow: hidden; }
.thread .subj { font-weight: 600; display: flex; gap: 6px; align-items: center; }
.thread .subj span:first-child { flex: 1; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.thread .snip { color: var(--muted); font-size: 13px; overflow: hidden; text-overflow: ellipsis; display: -webkit-box; -webkit-line-clamp: 2; -webkit-box-orient: vertical; }
.thread.unread .subj span:first-child::before { content: ""; display: inline-block; width: 8px; height: 8px; border-radius: 50%; background: var(--accent); margin-right: 6px; vertical-align: middle; }
.chip { font-size: 11px; font-weight: 600; padding: 2px 8px; border-radius: 999px; border: 1px solid var(--line); color: var(--muted); white-space: nowrap; }
.chip.running { color: var(--accent); border-color: var(--accent); }
.chip.paused, .chip.capped, .chip.stopped, .chip.timeout { color: var(--warn); border-color: var(--warn); }
.chip.error { color: var(--err); border-color: var(--err); }
.chip.ok { color: var(--ok); border-color: color-mix(in srgb, var(--ok) 50%, var(--line)); }

.msg { border-radius: 14px; padding: 10px 12px; margin: 8px 0; border: 1px solid var(--line); }
.msg.from-human { background: var(--human); margin-left: 12%; }
.msg.from-server { background: var(--server); margin-right: 12%; }
.msg.from-peer { background: var(--peer); margin-right: 12%; }
.msg .meta { font-size: 12px; color: var(--muted); display: flex; gap: 8px; margin-bottom: 4px; }
.msg .meta b { color: var(--text); font-weight: 600; }
.msg .body { white-space: pre-wrap; overflow-wrap: anywhere; }
.runline { display: flex; gap: 8px; align-items: center; padding: 8px 0; border-top: 1px solid var(--line); text-decoration: none; color: inherit; }
.runline:first-child { border-top: 0; }
.runline .res { flex: 1; font-size: 13px; color: var(--muted); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }

.composer { position: fixed; left: 0; right: 0; bottom: 0; background: var(--bg); border-top: 1px solid var(--line); padding: 10px 16px calc(10px + env(safe-area-inset-bottom)); z-index: 4; }
.composer .inner { max-width: 760px; margin: 0 auto; display: flex; gap: 8px; align-items: flex-end; }
.composer textarea { min-height: 44px; max-height: 40vh; height: 44px; }
.fab { position: fixed; right: 18px; bottom: calc(22px + env(safe-area-inset-bottom)); border-radius: 999px; padding: 14px 20px; box-shadow: 0 4px 16px rgba(0,0,0,.25); z-index: 4; }

.log .step { padding: 8px 10px; border-left: 3px solid var(--line); margin: 6px 0; overflow-wrap: anywhere; }
.log .step.text { border-color: var(--accent); white-space: pre-wrap; }
.log .step.tool { font-family: ui-monospace, SFMono-Regular, Menlo, monospace; font-size: 12.5px; background: var(--code); border-radius: 0 8px 8px 0; white-space: pre-wrap; }
.log .step.tool b { color: var(--accent); }
.log .step.result { font-family: ui-monospace, SFMono-Regular, Menlo, monospace; font-size: 12px; color: var(--muted); white-space: pre-wrap; max-height: 220px; overflow: auto; }
.log .step.result.err, .log .step.stderr { color: var(--err); border-color: var(--err); }
.log .step.sys, .log .step.done { font-size: 12px; color: var(--muted); }
pre.prompt { white-space: pre-wrap; overflow-wrap: anywhere; font-size: 12.5px; background: var(--code); padding: 10px; border-radius: 10px; max-height: 260px; overflow: auto; }
.event { font-size: 13px; padding: 6px 0; border-top: 1px solid var(--line); display: flex; gap: 8px; }
.event:first-child { border-top: 0; }
.event time { color: var(--muted); flex: none; font-variant-numeric: tabular-nums; }
.event.warn span { color: var(--warn); } .event.error span { color: var(--err); }
.kv { display: grid; grid-template-columns: 1fr auto; gap: 10px 12px; align-items: center; }
.kv input, .kv select { width: 110px; }
.switch { display: flex; align-items: center; justify-content: space-between; gap: 12px; }

#toast { position: fixed; left: 50%; transform: translateX(-50%); bottom: calc(90px + env(safe-area-inset-bottom)); background: var(--text); color: var(--bg); padding: 10px 16px; border-radius: 10px; z-index: 10; max-width: calc(100% - 32px); }
.login { max-width: 380px; margin: 10vh auto 0; }
.login h2 { margin: 0 0 4px; }
.err-text { color: var(--err); }
.full { width: 100%; }
.auto-w { width: auto; }
.result-text { white-space: pre-wrap; margin-top: 10px; }
.mt { margin-top: 10px; }
