:root {
  --bg: #282a36;
  --surface: #2f3142;
  --line: #44475a;
  --fg: #f8f8f2;
  --dim: #bcc0d8;
  --comment: #6272a4;
  --cyan: #8be9fd;
  --green: #50fa7b;
  --orange: #ffb86c;
  --pink: #ff79c6;
  --purple: #bd93f9;
  --red: #ff5555;
  --yellow: #f1fa8c;
  --mono: ui-monospace, "SF Mono", SFMono-Regular, Menlo, monospace;
  --ease: cubic-bezier(.2, .8, .2, 1);
  color-scheme: dark;
}

* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
html, body { margin: 0; height: 100%; background: var(--bg); color: var(--fg); }
body {
  font: 16px/1.45 -apple-system, BlinkMacSystemFont, "SF Pro Text", system-ui, "Segoe UI", Roboto, sans-serif;
  -webkit-font-smoothing: antialiased;
  overflow: hidden;
  overscroll-behavior: none;
}
button, input, textarea { font: inherit; color: inherit; }
button { cursor: pointer; }
code { font-family: var(--mono); font-size: .86em; }
svg { width: 22px; height: 22px; fill: none; stroke: currentColor; stroke-width: 2; stroke-linecap: round; stroke-linejoin: round; }
.sr { position: absolute; width: 1px; height: 1px; overflow: hidden; clip: rect(0 0 0 0); white-space: nowrap; }
:focus-visible { outline: 2px solid var(--purple); outline-offset: 2px; border-radius: 10px; }
[hidden] { display: none !important; }

/* ---------- layout ---------- */
#app { display: flex; flex-direction: column; height: 100dvh; }
#main {
  flex: 1;
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
  overscroll-behavior: contain;
  padding: 12px 16px 24px;
  outline: none;
}

.top {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: calc(env(safe-area-inset-top) + 6px) 8px 8px 12px;
  background: rgba(40, 42, 54, .92);
  backdrop-filter: blur(14px);
  -webkit-backdrop-filter: blur(14px);
  border-bottom: 1px solid rgba(68, 71, 90, .6);
  z-index: 2;
}
.title { display: flex; align-items: center; gap: 10px; flex: 1; min-width: 0; }
.title img { border-radius: 7px; }
.t2 { display: flex; flex-direction: column; min-width: 0; line-height: 1.2; }
.t2 b { font-size: 17px; letter-spacing: -.01em; }
.t2 small { color: var(--comment); font-size: 12px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.top-meta { display: flex; gap: 6px; }
.pill { font: 600 13px var(--mono); padding: 4px 9px; border-radius: 99px; background: var(--line); color: var(--green); }
.pill.dim { color: var(--dim); background: transparent; border: 1px solid var(--line); }
.icon-btn {
  display: grid; place-items: center;
  width: 44px; height: 44px; border: 0; border-radius: 12px;
  background: transparent; color: var(--dim);
}
.icon-btn:active { background: var(--line); }
#back { margin-left: -6px; color: var(--purple); }
.icon-btn svg { width: 24px; height: 24px; }
#menu svg { fill: currentColor; stroke: none; }

.banner {
  display: flex; align-items: center; justify-content: space-between; gap: 12px;
  padding: 10px 16px; font-size: 14px; font-weight: 600;
  animation: drop .22s var(--ease);
}
.banner.off { background: rgba(255, 85, 85, .16); color: var(--red); }
.banner.paused { background: rgba(255, 184, 108, .14); color: var(--orange); }

.tabs {
  display: flex;
  padding: 4px 8px calc(env(safe-area-inset-bottom) + 4px);
  background: rgba(40, 42, 54, .94);
  backdrop-filter: blur(14px);
  -webkit-backdrop-filter: blur(14px);
  border-top: 1px solid rgba(68, 71, 90, .6);
}
.tabs button {
  position: relative; flex: 1;
  display: flex; flex-direction: column; align-items: center; gap: 2px;
  min-height: 52px; padding: 6px 0;
  border: 0; background: none; color: var(--comment);
  font-size: 11px; font-weight: 600; letter-spacing: .02em;
  transition: color .15s;
}
.tabs button[aria-current="page"] { color: var(--purple); }
.tabs button:active svg { transform: scale(.88); }
.tabs svg { transition: transform .12s var(--ease); }
.badge {
  position: absolute; top: 2px; left: calc(50% + 6px);
  min-width: 18px; height: 18px; padding: 0 5px;
  border-radius: 9px; background: var(--pink); color: var(--bg);
  font: 700 11px/18px system-ui; text-align: center;
  animation: pop .25s var(--ease);
}

/* ---------- common ---------- */
h1.section { font-size: 13px; font-weight: 700; text-transform: uppercase; letter-spacing: .08em; color: var(--comment); margin: 6px 2px 12px; }
.dim { color: var(--dim); }
.none { color: var(--comment); font-size: 14px; margin: 8px 2px; }
.err { color: var(--red); min-height: 1.4em; font-size: 14px; }
.tag { font: 600 12px var(--mono); padding: 2px 8px; border-radius: 6px; background: var(--line); color: var(--dim); }
.tag.mgr { background: rgba(189, 147, 249, .18); color: var(--purple); margin-left: 8px; font-family: inherit; vertical-align: 2px; }

.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 6px;
  min-height: 44px; padding: 0 18px;
  border: 1px solid var(--line); border-radius: 12px;
  background: var(--line); color: var(--fg); font-weight: 600;
  transition: transform .1s var(--ease), filter .15s;
}
.btn:active { transform: scale(.97); filter: brightness(1.15); }
.btn.small { min-height: 34px; padding: 0 12px; font-size: 14px; border-radius: 10px; }
.btn.wide { display: flex; width: 100%; margin-top: 10px; }
.btn.primary { background: var(--purple); border-color: var(--purple); color: var(--bg); }
.btn.ok { background: var(--green); border-color: var(--green); color: var(--bg); }
.btn.deny { background: rgba(255, 85, 85, .14); border-color: rgba(255, 85, 85, .4); color: var(--red); }
.btn.ghost { background: transparent; }
.btn.deny.ghost { border-color: transparent; }
.banner .btn { color: var(--bg); background: var(--orange); border: 0; }

.chips { display: flex; flex-wrap: wrap; gap: 8px; }
.chip {
  min-height: 40px; padding: 0 14px;
  border: 1px solid var(--line); border-radius: 99px;
  background: var(--surface); color: var(--fg); font-size: 15px; font-weight: 500;
  transition: background .15s, border-color .15s, transform .1s var(--ease);
}
.chip:active { transform: scale(.96); }
.chip.on { background: rgba(189, 147, 249, .2); border-color: var(--purple); color: var(--purple); }
.chip small { color: var(--comment); font-size: 12px; margin-left: 4px; }

.field {
  display: block; width: 100%; margin-top: 12px; padding: 12px 14px;
  border: 1px solid var(--line); border-radius: 12px;
  background: var(--bg); resize: none; line-height: 1.4;
  transition: border-color .15s;
}
.field:focus { outline: none; border-color: var(--purple); }
.field.big { min-height: 150px; }

.card {
  background: var(--surface);
  border: 1px solid rgba(68, 71, 90, .7);
  border-radius: 18px;
  padding: 14px;
  margin-bottom: 14px;
  animation: rise .24s var(--ease);
}
.card > header { display: flex; align-items: center; justify-content: space-between; gap: 8px; margin-bottom: 6px; }
.card time { color: var(--comment); font-size: 13px; }
.card.q { border-left: 3px solid var(--cyan); }
.card.rv { border-left: 3px solid var(--pink); }
.card.rv.lead { border-left-color: var(--comment); }
.card .chips { margin-top: 10px; }
.actions { display: flex; gap: 10px; margin-top: 12px; }
.actions .btn { flex: 1; }
.note { color: var(--comment); font-size: 14px; margin: 8px 0 0; }
.who {
  display: inline-flex; align-items: center; gap: 7px;
  min-height: 36px; margin-left: -4px; padding: 0 8px 0 4px;
  border: 0; border-radius: 10px; background: none;
  font-weight: 650; font-size: 15px;
}
.who span { color: var(--comment); font-weight: 500; font-size: 13px; }
.who:active { background: var(--line); }
.tool { display: flex; align-items: center; gap: 8px; margin: 4px 0; min-width: 0; }
.tool-name { background: rgba(255, 121, 198, .16); color: var(--pink); flex: none; }
.summary { font: 14px var(--mono); color: var(--fg); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }

details.evidence { margin-top: 10px; border-radius: 12px; background: var(--bg); border: 1px solid var(--line); overflow: hidden; }
details.evidence summary { min-height: 44px; display: flex; align-items: center; padding: 0 14px; color: var(--cyan); font-size: 14px; font-weight: 600; list-style: none; }
details.evidence summary::before { content: "▸"; margin-right: 8px; transition: transform .15s; }
details.evidence[open] summary::before { transform: rotate(90deg); }
summary::-webkit-details-marker { display: none; }
pre.diff { margin: 0; padding: 8px 0; max-height: 50vh; overflow: auto; font: 12px/1.5 var(--mono); border-top: 1px solid var(--line); }
.dl { display: block; padding: 0 12px; white-space: pre; min-width: max-content; }
.dl.a { background: rgba(80, 250, 123, .1); color: var(--green); }
.dl.d { background: rgba(255, 85, 85, .1); color: var(--red); }
.dl.h { color: var(--cyan); }
.dl.f { color: var(--comment); font-weight: 600; }

.empty { text-align: center; padding: 18vh 24px 0; color: var(--comment); animation: rise .3s var(--ease); }
.empty svg { width: 54px; height: 54px; padding: 12px; border-radius: 50%; background: rgba(80, 250, 123, .12); color: var(--green); }
.empty h2 { color: var(--fg); font-size: 20px; margin: 16px 0 6px; }
.empty p { margin: 0; }

.skel { height: 120px; border-radius: 18px; margin-bottom: 14px; background: linear-gradient(90deg, var(--surface), var(--line), var(--surface)); background-size: 200% 100%; animation: shimmer 1.2s linear infinite; }
.skel.short { height: 64px; }

/* ---------- markdown ---------- */
.md { overflow-wrap: anywhere; }
.md p { margin: 0 0 8px; }
.md p:last-child { margin-bottom: 0; }
.md p.h { font-weight: 700; color: var(--purple); }
.md ul, .md ol { margin: 0 0 8px; padding-left: 22px; }
.md li { margin: 2px 0; }
.md li::marker { color: var(--comment); }
.md code { background: var(--bg); padding: 1px 5px; border-radius: 5px; color: var(--orange); }
.md strong { color: var(--fg); }
.md a { color: var(--cyan); }
pre.code { margin: 6px 0 10px; padding: 10px 12px; background: var(--bg); border: 1px solid var(--line); border-radius: 10px; overflow-x: auto; font: 13px/1.5 var(--mono); }
pre.code code { background: none; padding: 0; color: var(--fg); font-size: inherit; }

/* ---------- fleets ---------- */
.stats { display: grid; grid-template-columns: repeat(4, 1fr); gap: 8px; margin-bottom: 18px; }
.stats div { background: var(--surface); border-radius: 14px; padding: 10px 6px; text-align: center; }
.stats b { display: block; font-size: 18px; font-variant-numeric: tabular-nums; }
.stats span { color: var(--comment); font-size: 12px; }
.fleet { margin-bottom: 22px; }
.fleet > header { display: flex; align-items: center; justify-content: space-between; margin: 0 2px 8px; }
.fleet h2 { font-size: 18px; margin: 0; letter-spacing: -.01em; }
.agents { list-style: none; margin: 0; padding: 0; background: var(--surface); border-radius: 16px; overflow: hidden; }
.agents li + li { border-top: 1px solid rgba(68, 71, 90, .6); }
.agent {
  display: flex; align-items: center; gap: 12px; width: 100%;
  min-height: 60px; padding: 8px 14px;
  border: 0; background: none; text-align: left;
  transition: background .12s;
}
.agent:active { background: var(--line); }
.agent.gone { opacity: .5; }
.who2 { flex: 1; display: flex; flex-direction: column; min-width: 0; line-height: 1.25; }
.who2 small, .right small { color: var(--comment); font-size: 13px; }
.right { display: flex; flex-direction: column; align-items: flex-end; line-height: 1.25; max-width: 50%; }
.right code { color: var(--dim); font-size: 12px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; max-width: 100%; }

.dot { flex: none; width: 10px; height: 10px; border-radius: 50%; background: var(--comment); }
.st-running { background: var(--green); box-shadow: 0 0 0 0 rgba(80, 250, 123, .6); animation: pulse 1.6s infinite; }
.st-starting { background: var(--cyan); }
.st-waiting { background: var(--orange); }
.st-idle { background: var(--purple); }
.st-failed { background: var(--red); }
.st-stopped { background: var(--line); }

/* ---------- ask ---------- */
.group { margin-bottom: 16px; }
.label { display: block; color: var(--comment); font-size: 13px; font-weight: 600; margin: 0 2px 8px; }

/* ---------- agent ---------- */
.transcript { display: flex; flex-direction: column; gap: 10px; padding-bottom: 8px; }
.ev { animation: rise .18s var(--ease); }
.k-user { align-self: flex-end; max-width: 86%; }
.bubble { background: var(--purple); color: var(--bg); padding: 9px 14px; border-radius: 18px 18px 4px 18px; white-space: pre-wrap; overflow-wrap: anywhere; font-weight: 500; }
.k-text { padding: 2px 2px; }
details.k-tool, details.k-thinking { border-radius: 10px; background: var(--surface); }
details.k-tool summary, details.k-thinking summary {
  display: flex; align-items: center; gap: 8px;
  min-height: 36px; padding: 0 12px;
  font: 12.5px var(--mono); color: var(--dim);
  white-space: nowrap; overflow: hidden; text-overflow: ellipsis; list-style: none;
}
details.k-tool summary { display: block; line-height: 36px; }
.gear { color: var(--cyan); margin-right: 8px; display: inline-block; transition: transform .15s; }
details[open] .gear { transform: rotate(90deg); }
details.k-tool pre { margin: 0; padding: 4px 12px 10px; max-height: 40vh; overflow: auto; font: 12px/1.5 var(--mono); white-space: pre-wrap; overflow-wrap: anywhere; color: var(--fg); }
.res { color: var(--comment); }
details.k-thinking { display: none; border-left: 2px solid var(--comment); }
.show-thinking details.k-thinking { display: block; }
details.k-thinking summary { font-style: italic; color: var(--comment); }
details.k-thinking .md { padding: 0 12px 10px; color: var(--dim); font-size: 14px; }
.sys { border: 1px dashed var(--line); border-radius: 12px; padding: 8px 12px; font-size: 14px; color: var(--dim); }
.sys > span { display: block; font-size: 11px; font-weight: 700; text-transform: uppercase; letter-spacing: .08em; color: var(--orange); margin-bottom: 2px; }
.meta { margin: 0; text-align: center; color: var(--comment); font-size: 12.5px; }
.k-state .meta { color: var(--red); }

.composer {
  display: flex; align-items: flex-end; gap: 8px;
  padding: 8px 10px calc(env(safe-area-inset-bottom) + 8px);
  background: rgba(40, 42, 54, .96);
  border-top: 1px solid rgba(68, 71, 90, .6);
}
.composer textarea {
  flex: 1; min-height: 44px; max-height: 160px; padding: 11px 14px;
  border: 1px solid var(--line); border-radius: 22px;
  background: var(--surface); resize: none; line-height: 1.35;
}
.composer textarea:focus { outline: none; border-color: var(--purple); }
.now, .send { flex: none; display: grid; place-items: center; width: 44px; height: 44px; border-radius: 50%; border: 0; }
.now { background: transparent; color: var(--comment); }
.now[aria-pressed="true"] { background: rgba(255, 184, 108, .18); color: var(--orange); }
.now[aria-pressed="true"] svg { fill: currentColor; }
.send { background: var(--purple); color: var(--bg); transition: transform .1s var(--ease); }
.send:active { transform: scale(.9); }
.send svg { stroke-width: 2.6; }

/* ---------- sheet ---------- */
.sheet-root { position: fixed; inset: 0; z-index: 10; }
.backdrop { position: absolute; inset: 0; background: rgba(15, 16, 22, .6); opacity: 0; transition: opacity .22s; }
.sheet {
  position: absolute; left: 0; right: 0; bottom: 0;
  max-height: 88dvh; overflow-y: auto;
  padding: 6px 18px calc(env(safe-area-inset-bottom) + 18px);
  background: var(--surface); border-radius: 22px 22px 0 0;
  box-shadow: 0 -10px 40px rgba(0, 0, 0, .4);
  transform: translateY(100%); transition: transform .22s var(--ease);
}
.sheet-root.open .backdrop { opacity: 1; }
.sheet-root.open .sheet { transform: none; }
.grabber { width: 38px; height: 5px; border-radius: 3px; background: var(--line); margin: 4px auto 12px; }
.sheet h2 { font-size: 20px; margin: 0 0 6px; overflow-wrap: anywhere; }
.sheet p { margin: 0 0 6px; }
.sheet .field { background: var(--bg); }
.kv { display: grid; grid-template-columns: auto 1fr; gap: 6px 14px; margin: 10px 0 14px; font-size: 14px; }
.kv dt { color: var(--comment); }
.kv dd { margin: 0; overflow-wrap: anywhere; }
.row {
  display: flex; align-items: center; justify-content: space-between; gap: 12px; width: 100%;
  min-height: 56px; padding: 8px 14px; margin: 6px 0;
  border: 0; border-radius: 14px; background: var(--bg); text-align: left;
}
.row span:first-child { display: flex; flex-direction: column; }
.row small { color: var(--comment); font-size: 13px; }
.row:disabled { opacity: .6; cursor: default; }
.switch { flex: none; position: relative; width: 50px; height: 30px; border-radius: 15px; background: var(--line); transition: background .18s; }
.switch::after { content: ""; position: absolute; top: 3px; left: 3px; width: 24px; height: 24px; border-radius: 50%; background: var(--fg); transition: transform .18s var(--ease); }
.row[aria-checked="true"] .switch { background: var(--green); }
.row[aria-checked="true"] .switch::after { transform: translateX(20px); }

.toast {
  position: fixed; left: 50%; bottom: calc(env(safe-area-inset-bottom) + 84px); z-index: 20;
  max-width: calc(100% - 32px); padding: 10px 16px; border-radius: 12px;
  background: var(--fg); color: var(--bg); font-weight: 600; font-size: 14px;
  transform: translate(-50%, 20px); opacity: 0; pointer-events: none;
  transition: transform .2s var(--ease), opacity .2s;
}
.toast.show { transform: translate(-50%, 0); opacity: 1; }
.toast.bad { background: var(--red); color: var(--fg); }

/* ---------- pair ---------- */
.pair { display: flex; flex-direction: column; align-items: center; text-align: center; padding: calc(env(safe-area-inset-top) + 10vh) 12px 0; }
.pair-logo { border-radius: 24px; box-shadow: 0 12px 40px rgba(189, 147, 249, .25); }
.pair h1 { font-size: 26px; margin: 22px 0 8px; letter-spacing: -.02em; }
.pair p { margin: 0 0 20px; max-width: 320px; }
.pair-form { width: 100%; max-width: 340px; }
.cells { display: flex; gap: 8px; justify-content: center; margin-bottom: 16px; }
.cell {
  width: 48px; height: 60px; padding: 0; text-align: center; caret-color: var(--purple);
  font: 600 26px var(--mono); text-transform: uppercase;
  border: 1px solid var(--line); border-radius: 14px; background: var(--surface);
}
.cell:focus { outline: none; border-color: var(--purple); box-shadow: 0 0 0 3px rgba(189, 147, 249, .25); }

/* ---------- wide screens: phone only ---------- */
.wide { display: none; }
@media (min-width: 761px) {
  #app, .sheet-root, .toast { display: none !important; }
  body { overflow: auto; }
  .wide { display: grid; place-items: center; min-height: 100vh; }
  .wide-card { text-align: center; padding: 48px; border-radius: 24px; background: var(--surface); max-width: 420px; }
  .wide-card img { border-radius: 18px; }
  .wide-card h1 { font-size: 24px; margin: 18px 0 8px; }
  .wide-card p { color: var(--dim); }
  .wide-card code { display: inline-block; margin-top: 10px; padding: 8px 12px; border-radius: 10px; background: var(--bg); color: var(--cyan); font-size: 15px; }
}

@keyframes rise { from { opacity: 0; transform: translateY(6px); } }
@keyframes drop { from { opacity: 0; transform: translateY(-6px); } }
@keyframes pop { from { transform: scale(.4); } }
@keyframes shimmer { to { background-position: -200% 0; } }
@keyframes pulse { 70% { box-shadow: 0 0 0 7px rgba(80, 250, 123, 0); } 100% { box-shadow: 0 0 0 0 rgba(80, 250, 123, 0); } }

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { animation: none !important; transition: none !important; }
}
