:root {
  --bg: #f2f2f7; --card: #ffffff; --card2: #f2f2f7; --fill: rgba(118,118,128,.12);
  --text: #1c1c1e; --muted: #6e6e73; --faint: #aeaeb2; --line: rgba(60,60,67,.14);
  --accent: #0a84ff; --accent-text: #fff; --orange: #ff9500; --green: #30b158; --red: #ff3b30; --purple: #af52de;
  --bubble: #0a84ff; --bubble-text: #fff;
  --code-bg: #f6f6f8; --diff-add: rgba(48,177,88,.14); --diff-del: rgba(255,59,48,.12);
  --term-bg: #1c1c1e;
  --mono: ui-monospace, "SF Mono", Menlo, monospace;
  --sat: env(safe-area-inset-top); --sab: env(safe-area-inset-bottom);
  --sal: env(safe-area-inset-left); --sar: env(safe-area-inset-right);
  --vh: 100dvh; --vtop: 0px;
  color-scheme: light dark;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #000000; --card: #1c1c1e; --card2: #2c2c2e; --fill: rgba(118,118,128,.24);
    --text: #f2f2f7; --muted: #98989f; --faint: #636366; --line: rgba(84,84,88,.6);
    --accent: #0a84ff; --code-bg: #141416; --diff-add: rgba(48,209,88,.16); --diff-del: rgba(255,69,58,.16);
    --orange: #ff9f0a; --green: #30d158; --red: #ff453a;
  }
}
* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
html, body { margin: 0; height: 100%; overflow: hidden; background: var(--bg); color: var(--text);
  font: 16px/1.4 -apple-system, BlinkMacSystemFont, "SF Pro Text", system-ui, sans-serif;
  -webkit-text-size-adjust: 100%; overscroll-behavior: none; }
#app { position: fixed; left: 0; right: 0; top: var(--vtop); height: var(--vh); display: flex; flex-direction: column; }
button { font: inherit; color: inherit; background: none; border: 0; padding: 0; cursor: pointer; }
input, textarea, select { font: inherit; color: inherit; }
a { color: var(--accent); }
.scroll { flex: 1; overflow-y: auto; -webkit-overflow-scrolling: touch; overscroll-behavior: contain; }
.muted { color: var(--muted); }
.mono { font-family: var(--mono); }
.row { display: flex; align-items: center; gap: 8px; }
.grow { flex: 1; min-width: 0; }
.ellipsis { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
svg.i { width: 22px; height: 22px; stroke: currentColor; fill: none; stroke-width: 2; stroke-linecap: round; stroke-linejoin: round; flex: none; }

/* ---------- top bars ---------- */
.bar { flex: none; padding: calc(var(--sat) + 6px) calc(12px + var(--sar)) 8px calc(12px + var(--sal));
  background: color-mix(in srgb, var(--bg) 82%, transparent); backdrop-filter: saturate(180%) blur(20px);
  -webkit-backdrop-filter: saturate(180%) blur(20px); border-bottom: .5px solid var(--line); z-index: 5; }
.bar.big { border-bottom: 0; padding-bottom: 4px; }
.bar h1 { font-size: 30px; font-weight: 700; letter-spacing: .3px; margin: 2px 4px 4px; }
.bar .title { font-weight: 600; font-size: 16px; }
.bar .sub { font-size: 12.5px; color: var(--muted); }
.iconbtn { width: 38px; height: 38px; border-radius: 19px; display: grid; place-items: center; color: var(--accent); }
.iconbtn:active { background: var(--fill); }
.back { color: var(--accent); display: flex; align-items: center; margin-left: -6px; height: 38px; }

/* ---------- tab bar ---------- */
.tabs { flex: none; display: flex; border-top: .5px solid var(--line);
  background: color-mix(in srgb, var(--bg) 85%, transparent); backdrop-filter: blur(20px); -webkit-backdrop-filter: blur(20px);
  padding: 4px calc(8px + var(--sar)) calc(var(--sab) + 2px) calc(8px + var(--sal)); }
.tabs button { flex: 1; display: flex; flex-direction: column; align-items: center; gap: 1px; font-size: 10.5px; color: var(--faint); padding: 4px 0; position: relative; }
.tabs button.on { color: var(--accent); }
.tabs .dot { position: absolute; top: 0; left: calc(50% + 8px); min-width: 17px; height: 17px; border-radius: 9px; background: var(--orange); color: #fff; font-size: 11px; font-weight: 600; display: grid; place-items: center; padding: 0 4px; }

/* ---------- segmented / chips ---------- */
.seg { display: flex; background: var(--fill); border-radius: 9px; padding: 2px; margin: 4px 4px 2px; }
.seg button { flex: 1; padding: 6px 4px; border-radius: 7px; font-size: 13.5px; font-weight: 500; }
.seg button.on { background: var(--card); box-shadow: 0 1px 3px rgba(0,0,0,.12); }
.chip { display: inline-flex; align-items: center; gap: 4px; font-size: 11.5px; font-weight: 600; padding: 1px 7px; border-radius: 6px; background: var(--fill); color: var(--muted); white-space: nowrap; }
.chip.pult { color: var(--accent); background: color-mix(in srgb, var(--accent) 14%, transparent); }
.chip.flow { color: var(--purple); background: color-mix(in srgb, var(--purple) 14%, transparent); }
.chip.desktop { color: var(--green); background: color-mix(in srgb, var(--green) 14%, transparent); }

/* ---------- session list ---------- */
.section { margin: 18px 16px 6px; font-size: 13px; font-weight: 600; text-transform: uppercase; letter-spacing: .4px; color: var(--muted); display: flex; justify-content: space-between; }
.group { margin: 0 12px; background: var(--card); border-radius: 14px; overflow: hidden; }
.srow { display: flex; gap: 10px; padding: 11px 14px; position: relative; width: 100%; text-align: left; }
.srow + .srow::before { content: ""; position: absolute; top: 0; left: 34px; right: 0; border-top: .5px solid var(--line); }
.srow:active { background: var(--fill); }
.srow .st { width: 10px; flex: none; padding-top: 6px; }
.srow .t { font-weight: 600; font-size: 15.5px; }
.srow .m { font-size: 12.5px; color: var(--muted); margin-top: 1px; display: flex; gap: 6px; align-items: center; flex-wrap: wrap; }
.srow .s { font-size: 13.5px; color: var(--muted); margin-top: 3px; display: -webkit-box; -webkit-line-clamp: 2; -webkit-box-orient: vertical; overflow: hidden; }
.srow .w { font-size: 13.5px; color: var(--orange); margin-top: 3px; }
.sdot { width: 9px; height: 9px; border-radius: 5px; background: var(--faint); display: block; }
.sdot.busy, .sdot.starting { background: var(--accent); animation: pulse 1.4s ease-in-out infinite; }
.sdot.waiting { background: var(--orange); box-shadow: 0 0 0 3px color-mix(in srgb, var(--orange) 25%, transparent); }
.sdot.idle { background: var(--green); }
.sdot.done, .sdot.closed { background: transparent; border: 1.5px solid var(--faint); }
@keyframes pulse { 50% { opacity: .35; transform: scale(.8); } }
.more { display: block; margin: 8px auto 0; color: var(--accent); font-size: 15px; padding: 8px 16px; }
.empty { text-align: center; color: var(--muted); padding: 60px 30px; }

/* ---------- feed ---------- */
.feed { padding: 8px calc(12px + var(--sar)) 16px calc(12px + var(--sal)); display: flex; flex-direction: column; gap: 8px; }
.u { align-self: flex-end; max-width: 86%; background: var(--bubble); color: var(--bubble-text); padding: 8px 13px; border-radius: 18px 18px 5px 18px; white-space: pre-wrap; word-wrap: break-word; font-size: 15.5px; }
.u.cmd { font-family: var(--mono); font-size: 14px; background: color-mix(in srgb, var(--bubble) 80%, #000); }
.md { font-size: 15.5px; line-height: 1.5; word-wrap: break-word; padding: 0 2px; }
.md p { margin: 6px 0; } .md > :first-child { margin-top: 0; } .md > :last-child { margin-bottom: 0; }
.md ul, .md ol { padding-left: 22px; margin: 6px 0; } .md li { margin: 2px 0; }
.md h1, .md h2, .md h3, .md h4 { font-size: 16.5px; margin: 12px 0 4px; }
.md code { font-family: var(--mono); font-size: 13.5px; background: var(--fill); padding: 1px 5px; border-radius: 5px; }
.md pre { background: var(--code-bg); border: .5px solid var(--line); border-radius: 10px; padding: 10px 12px; overflow-x: auto; font-size: 12.5px; line-height: 1.45; }
.md pre code { background: none; padding: 0; font-size: inherit; }
.md table { border-collapse: collapse; display: block; overflow-x: auto; font-size: 13.5px; }
.md th, .md td { border: .5px solid var(--line); padding: 4px 8px; text-align: left; }
.md blockquote { margin: 6px 0; padding-left: 10px; border-left: 3px solid var(--line); color: var(--muted); }
.md a { word-break: break-all; }
.partial::after { content: "▍"; animation: blink 1s steps(2) infinite; color: var(--accent); }
@keyframes blink { 50% { opacity: 0; } }

.tools { background: var(--card); border-radius: 12px; overflow: hidden; }
.tgsum { width: 100%; text-align: left; padding: 8px 12px; font-size: 13px; color: var(--muted); display: flex; gap: 6px; align-items: center; }
.tgsum + .tool, .tool + .tool { border-top: .5px solid var(--line); }
.tool > button { width: 100%; text-align: left; padding: 8px 12px; display: flex; gap: 9px; align-items: flex-start; }
.tool > button:active { background: var(--fill); }
.tool .ic { width: 18px; text-align: center; flex: none; font-size: 14px; line-height: 20px; }
.tool .tt { font-size: 13.5px; font-weight: 600; }
.tool .td { font-family: var(--mono); font-size: 12.5px; color: var(--muted); white-space: pre-wrap; word-break: break-all; display: -webkit-box; -webkit-line-clamp: 3; -webkit-box-orient: vertical; overflow: hidden; }
.tool .rs { flex: none; font-size: 12px; line-height: 20px; color: var(--muted); }
.tool .rs.err { color: var(--red); } .tool .rs.ok { color: var(--green); }
.tool .body { padding: 0 12px 10px 39px; }
.out { font-family: var(--mono); font-size: 12px; line-height: 1.45; background: var(--code-bg); border-radius: 8px; padding: 8px 10px; white-space: pre; overflow-x: auto; max-height: 60vh; overflow-y: auto; margin: 0; }
.out.wrap { white-space: pre-wrap; word-break: break-all; }
.diff { font-family: var(--mono); font-size: 11.5px; line-height: 1.5; border-radius: 8px; overflow-x: auto; background: var(--code-bg); max-height: 60vh; overflow-y: auto; }
.diff div { white-space: pre; padding: 0 8px; min-width: max-content; }
.diff .a { background: var(--diff-add); } .diff .d { background: var(--diff-del); } .diff .hh { color: var(--muted); background: var(--fill); }
.plus { color: var(--green); } .minus { color: var(--red); }
.todos { padding: 2px 12px 10px 39px; font-size: 14px; }
.todos div { display: flex; gap: 7px; padding: 2px 0; }
.todos .done { color: var(--muted); text-decoration: line-through; }
.todos .in_progress { font-weight: 600; }
.sys { align-self: center; font-size: 12.5px; color: var(--muted); text-align: center; max-width: 92%; white-space: pre-wrap; }
.sys.error { color: var(--red); }
.recap { background: color-mix(in srgb, var(--accent) 10%, var(--card)); border-radius: 12px; padding: 10px 12px; font-size: 14.5px; }
.recap b { display: block; font-size: 12px; text-transform: uppercase; color: var(--accent); letter-spacing: .4px; margin-bottom: 2px; }
.turn { align-self: center; font-size: 11.5px; color: var(--faint); display: flex; gap: 8px; align-items: center; width: 100%; }
.turn::before, .turn::after { content: ""; flex: 1; border-top: .5px solid var(--line); }
.shell { background: var(--term-bg); color: #e5e5ea; border-radius: 10px; padding: 8px 10px; font-family: var(--mono); font-size: 12px; white-space: pre-wrap; word-break: break-all; }
.working { display: flex; align-items: center; gap: 8px; font-size: 13.5px; color: var(--muted); padding: 4px 2px; }
.spin { width: 14px; height: 14px; border-radius: 50%; border: 2px solid var(--fill); border-top-color: var(--accent); animation: spin .8s linear infinite; flex: none; }
@keyframes spin { to { transform: rotate(360deg); } }
.loadmore { align-self: center; color: var(--accent); font-size: 14px; padding: 6px 12px; }

/* ---------- bottom: pending requests + composer ---------- */
.dock { flex: none; background: color-mix(in srgb, var(--bg) 88%, transparent); backdrop-filter: blur(20px); -webkit-backdrop-filter: blur(20px);
  border-top: .5px solid var(--line); padding: 8px calc(10px + var(--sar)) calc(8px + var(--sab)) calc(10px + var(--sal)); }
body.kb .dock { padding-bottom: 8px; }
.req { background: var(--card); border-radius: 14px; padding: 12px; margin-bottom: 8px; box-shadow: 0 0 0 1.5px color-mix(in srgb, var(--orange) 60%, transparent); max-height: 55vh; overflow-y: auto; }
.req .h { font-size: 12.5px; font-weight: 700; color: var(--orange); text-transform: uppercase; letter-spacing: .4px; }
.req .q { font-weight: 600; font-size: 16px; margin: 4px 0 8px; }
.req .det { font-family: var(--mono); font-size: 12.5px; background: var(--code-bg); border-radius: 8px; padding: 8px; white-space: pre-wrap; word-break: break-all; max-height: 30vh; overflow-y: auto; margin: 6px 0 10px; }
.btns { display: flex; gap: 8px; flex-wrap: wrap; }
.btn { flex: 1; min-height: 44px; border-radius: 12px; padding: 10px 14px; font-weight: 600; font-size: 15.5px; background: var(--fill); color: var(--accent); }
.btn.pri { background: var(--accent); color: #fff; }
.btn.danger { color: var(--red); }
.btn.ghost { background: none; }
.btn:active { opacity: .7; }
.btn[disabled] { opacity: .45; }
.opt { display: block; width: 100%; text-align: left; background: var(--fill); border-radius: 12px; padding: 10px 12px; margin-bottom: 6px; }
.opt.on { box-shadow: 0 0 0 2px var(--accent); background: color-mix(in srgb, var(--accent) 12%, transparent); }
.opt b { display: block; font-size: 15.5px; } .opt span { font-size: 13px; color: var(--muted); }
.composer { display: flex; align-items: flex-end; gap: 8px; }
.composer textarea { flex: 1; min-width: 0; resize: none; border: .5px solid var(--line); background: var(--card); border-radius: 20px; padding: 9px 14px; font-size: 16px; line-height: 1.35; max-height: 160px; outline: none; }
.round { width: 40px; height: 40px; border-radius: 20px; display: grid; place-items: center; flex: none; }
.round.send { background: var(--accent); color: #fff; }
.round.send[disabled] { background: var(--fill); color: var(--faint); }
.round.mic { background: var(--fill); color: var(--text); }
.round.mic.rec { background: var(--red); color: #fff; animation: recpulse 1.2s ease-in-out infinite; }
.round.stop { background: var(--text); color: var(--bg); }
@keyframes recpulse { 50% { box-shadow: 0 0 0 7px color-mix(in srgb, var(--red) 25%, transparent); } }
.dockrow { display: flex; align-items: center; gap: 8px; margin-bottom: 6px; font-size: 13px; }
.modebtn { font-size: 12.5px; font-weight: 600; padding: 3px 9px; border-radius: 8px; background: var(--fill); color: var(--muted); }
.modebtn.bypassPermissions { color: var(--red); } .modebtn.plan { color: var(--purple); } .modebtn.acceptEdits { color: var(--orange); }
.voicebar { display: flex; align-items: center; gap: 10px; font-size: 14px; color: var(--muted); padding: 2px 4px 8px; }
.level { flex: 1; height: 4px; border-radius: 2px; background: var(--fill); overflow: hidden; }
.level i { display: block; height: 100%; background: var(--red); transition: width .08s; }
.handoff { background: var(--card); border-radius: 14px; padding: 12px; }
.handoff p { margin: 0 0 10px; font-size: 14.5px; color: var(--muted); }

/* ---------- sheets ---------- */
.sheet-bg { position: fixed; inset: 0; background: rgba(0,0,0,.35); z-index: 20; animation: fade .2s; }
.sheet { position: fixed; left: 0; right: 0; bottom: 0; top: calc(var(--vtop) + var(--sat) + 30px); max-height: calc(var(--vh) - var(--sat) - 30px);
  background: var(--bg); border-radius: 14px 14px 0 0; z-index: 21; display: flex; flex-direction: column; animation: up .28s cubic-bezier(.2,.9,.3,1); }
.sheet .sh { display: flex; align-items: center; padding: 12px 14px 8px; }
.sheet .sh .t { flex: 1; text-align: center; font-weight: 600; }
.sheet .sh button { color: var(--accent); font-size: 16px; min-width: 64px; }
@keyframes up { from { transform: translateY(100%); } }
@keyframes fade { from { opacity: 0; } }
.field { margin: 0 12px 12px; background: var(--card); border-radius: 12px; }
.field input, .field textarea, .field select { width: 100%; border: 0; background: none; padding: 12px 14px; font-size: 16px; outline: none; resize: none; }
.list-pick { margin: 0 12px; background: var(--card); border-radius: 12px; overflow: hidden; }
.list-pick button { display: flex; width: 100%; text-align: left; padding: 11px 14px; gap: 10px; align-items: center; }
.list-pick button + button { border-top: .5px solid var(--line); }
.list-pick button.on { color: var(--accent); font-weight: 600; }
.list-pick .p { font-size: 12px; color: var(--muted); font-family: var(--mono); }
.menu { margin: 0 12px 12px; background: var(--card); border-radius: 12px; overflow: hidden; }
.menu button { display: flex; width: 100%; padding: 13px 14px; gap: 10px; align-items: center; text-align: left; font-size: 16px; }
.menu button + button { border-top: .5px solid var(--line); }
.menu button.on::after { content: "✓"; margin-left: auto; color: var(--accent); font-weight: 700; }
.menu .d { font-size: 12.5px; color: var(--muted); display: block; }

/* ---------- terminal ---------- */
.termwrap { flex: 1; min-height: 0; background: var(--term-bg); padding: 4px 2px 0 6px; position: relative; }
.termwrap .xterm { height: 100%; }
.termtabs { display: flex; gap: 6px; overflow-x: auto; padding-top: 4px; }
.termtabs button { padding: 5px 11px; border-radius: 8px; background: var(--fill); font-size: 13.5px; font-weight: 500; white-space: nowrap; }
.termtabs button.on { background: var(--accent); color: #fff; }
.keys { flex: none; display: flex; gap: 5px; overflow-x: auto; padding: 6px calc(6px + var(--sar)) calc(6px + var(--sab)) calc(6px + var(--sal)); background: var(--card2); border-top: .5px solid var(--line); }
body.kb .keys { padding-bottom: 6px; }
.keys button { min-width: 42px; height: 36px; border-radius: 8px; background: var(--card); font-size: 14px; font-family: var(--mono); padding: 0 9px; box-shadow: 0 1px 0 rgba(0,0,0,.18); flex: none; }
.keys button.on { background: var(--accent); color: #fff; }
.keys button:active { opacity: .6; }

/* ---------- login / settings ---------- */
.login { flex: 1; display: flex; flex-direction: column; justify-content: center; padding: 30px; gap: 14px; max-width: 460px; margin: 0 auto; width: 100%; }
.login h1 { font-size: 34px; margin: 0; }
.login input { width: 100%; padding: 14px; border-radius: 12px; border: .5px solid var(--line); background: var(--card); font-size: 16px; font-family: var(--mono); }
.err { color: var(--red); font-size: 14px; }
.toast { position: fixed; left: 50%; top: calc(var(--vtop) + var(--sat) + 10px); transform: translateX(-50%); z-index: 50; background: var(--text); color: var(--bg);
  padding: 9px 16px; border-radius: 20px; font-size: 14px; max-width: 90%; box-shadow: 0 4px 20px rgba(0,0,0,.25); animation: fade .2s; }
.conn { font-size: 12px; color: var(--orange); text-align: center; padding: 2px 0 0; }
.set { margin: 0 12px 6px; background: var(--card); border-radius: 12px; overflow: hidden; }
.set > * + * { border-top: .5px solid var(--line); }
.set .it { display: flex; align-items: center; gap: 10px; padding: 12px 14px; width: 100%; text-align: left; font-size: 16px; min-height: 46px; }
.set .it .v { margin-left: auto; color: var(--muted); font-size: 15px; }
.note { margin: 6px 16px 16px; font-size: 13px; color: var(--muted); }
body.kb .tabs { display: none; }
