:root {
  --bg: #111110;
  --panel: #1a1918;
  --panel-2: #211f1d;
  --line: #2d2a27;
  --line-2: #3a3632;
  --text: #e9e5dd;
  --dim: #8d877d;
  --accent: #e8843c;
  --accent-soft: rgba(232, 132, 60, .14);
  --ok: #72cf76;
  --bad: #e7644f;
  --warn: #e2b44c;
  --mono: ui-monospace, "SF Mono", SFMono-Regular, Menlo, Consolas, "Liberation Mono", monospace;
  --vvh: 100dvh;
  --vvt: 0px;
  --sat: env(safe-area-inset-top, 0px);
  --sab: env(safe-area-inset-bottom, 0px);
  --sal: env(safe-area-inset-left, 0px);
  --sar: env(safe-area-inset-right, 0px);
}

* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
[hidden] { display: none !important; }

html, body {
  margin: 0; height: 100%; background: var(--bg); color: var(--text);
  font: 15px/1.45 var(--mono); -webkit-text-size-adjust: 100%;
  overscroll-behavior: none; overflow: hidden;
}
button, input, textarea { font: inherit; color: inherit; }
button { cursor: pointer; background: none; border: 0; padding: 0; }
svg { width: 20px; height: 20px; fill: none; stroke: currentColor; stroke-width: 1.7; stroke-linecap: round; stroke-linejoin: round; flex: none; }
h1, h2, p { margin: 0; }
.dim { color: var(--dim); }
.pad { padding: 16px; }

/* The shell follows the visual viewport, so the composer always sits right above the iOS keyboard. */
.app {
  position: fixed; left: 0; right: 0; top: var(--vvt); height: var(--vvh);
  display: flex; flex-direction: column;
  padding: var(--sat) var(--sar) 0 var(--sal);
  max-width: 720px; margin: 0 auto;
}

.top { display: flex; align-items: center; gap: 12px; padding: 10px 16px 8px; position: relative; flex: none; }
.brand { font-size: 20px; font-weight: 600; letter-spacing: .5px; flex: 1; }
.caret { color: var(--accent); animation: blink 1.2s steps(1) infinite; }
@keyframes blink { 50% { opacity: 0; } }
.mascot { width: 34px; height: 28px; display: inline-block; flex: none; }
.mascot svg { width: 100%; height: 100%; stroke: none; }
.mascot.big { width: 72px; height: 60px; }
.icon-btn { width: 40px; height: 40px; border: 1px solid var(--line-2); border-radius: 10px; display: grid; place-items: center; color: var(--text); }
.icon-btn svg { fill: currentColor; stroke: none; }
.icon-btn:active { background: var(--panel-2); }
.menu {
  position: absolute; right: 16px; top: 56px; z-index: 20; min-width: 240px;
  background: var(--panel); border: 1px solid var(--line-2); border-radius: 12px; padding: 6px;
  box-shadow: 0 12px 32px rgba(0, 0, 0, .5);
}
.menu button { display: block; width: 100%; text-align: left; padding: 12px; border-radius: 8px; }
.menu button:active { background: var(--panel-2); }
.menu-note { padding: 8px 12px 6px; color: var(--dim); font-size: 12px; border-top: 1px solid var(--line); margin-top: 4px; }

.views { flex: 1; min-height: 0; display: flex; }
.view { flex: 1; min-width: 0; display: flex; flex-direction: column; min-height: 0; }

/* chat ------------------------------------------------------------------ */
.seg { display: flex; margin: 2px 16px 0; border: 1px solid var(--line-2); border-radius: 10px; padding: 3px; gap: 3px; flex: none; }
.seg-btn {
  flex: 1; display: flex; align-items: center; justify-content: center; gap: 8px; height: 38px;
  border-radius: 8px; color: var(--dim); border: 1px solid transparent; position: relative;
}
.seg-btn[aria-selected="true"] { color: var(--text); background: var(--accent-soft); border-color: var(--accent); }
.seg-btn[aria-selected="true"] svg { color: var(--accent); }
.unread { width: 7px; height: 7px; border-radius: 50%; background: var(--accent); display: inline-block; }

.modelline { display: flex; align-items: center; gap: 8px; padding: 9px 18px; color: var(--dim); font-size: 13.5px; border-bottom: 1px solid var(--line); flex: none; }
.modelline #model-name { color: var(--text); }
.dot { width: 8px; height: 8px; border-radius: 50%; background: var(--ok); flex: none; }
.dot.busy { background: var(--accent); animation: pulse 1s ease-in-out infinite; }
.dot.off { background: var(--bad); }
@keyframes pulse { 50% { opacity: .35; } }

.bar { height: 2px; background: var(--line); overflow: hidden; flex: none; }
.bar i { display: block; width: 35%; height: 100%; background: var(--accent); animation: slide 1.3s ease-in-out infinite; }
@keyframes slide { from { transform: translateX(-100%); } to { transform: translateX(290%); } }

.feed { flex: 1; min-height: 0; overflow-y: auto; -webkit-overflow-scrolling: touch; padding: 14px 16px 8px; overscroll-behavior: contain; }
.empty { color: var(--dim); text-align: center; padding: 48px 16px; }
.empty .mascot { margin-bottom: 14px; opacity: .9; }

.msg { display: flex; gap: 10px; margin: 0 0 12px; }
.msg.user { justify-content: flex-end; }
.msg .avatar { width: 26px; height: 22px; margin-top: 9px; flex: none; }
.bubble {
  max-width: 86%; background: var(--panel); border: 1px solid var(--line); border-radius: 12px; padding: 9px 12px 7px;
  overflow-wrap: anywhere; white-space: pre-wrap;
}
.msg.user .bubble { background: var(--panel-2); border-color: var(--line-2); }
.msg.user .bubble::before { content: "› "; color: var(--accent); }
.msg.bad .bubble { border-color: rgba(231, 100, 79, .5); }
.bubble code { background: #26231f; border: 1px solid var(--line); border-radius: 4px; padding: 0 4px; font-size: .94em; }
.bubble pre { margin: 6px 0; padding: 8px 10px; background: #0d0d0c; border: 1px solid var(--line); border-radius: 8px; overflow-x: auto; white-space: pre; font-size: 13px; }
.bubble b { font-weight: 650; }
.meta { display: block; text-align: right; color: var(--dim); font-size: 11.5px; margin-top: 3px; }

.typing { display: inline-flex; gap: 4px; padding: 6px 0 4px; }
.typing i { width: 6px; height: 6px; border-radius: 50%; background: var(--dim); animation: pulse 1s ease-in-out infinite; }
.typing i:nth-child(2) { animation-delay: .15s; } .typing i:nth-child(3) { animation-delay: .3s; }

/* journal of one task run */
.run { margin: 0 0 12px 36px; }
.run-head { display: flex; align-items: center; gap: 8px; width: 100%; padding: 6px 2px; color: var(--dim); font-size: 13px; text-align: left; }
.run-head .chev { margin-left: auto; transition: transform .15s; width: 16px; height: 16px; }
.run.open .run-head .chev { transform: rotate(180deg); }
.steps { display: none; flex-direction: column; gap: 6px; }
.run.open .steps { display: flex; }
.step { border: 1px solid var(--line); border-radius: 10px; background: var(--panel); }
.step.running { border-color: var(--accent); box-shadow: 0 0 0 1px var(--accent-soft) inset; }
.step-head { display: flex; align-items: flex-start; gap: 10px; width: 100%; padding: 9px 10px; text-align: left; }
.step-main { flex: 1; min-width: 0; }
.step-title { display: block; overflow-wrap: anywhere; }
.step-sub { display: block; color: var(--dim); font-size: 12.5px; overflow-wrap: anywhere; }
.step-sub::before { content: "└ "; }
.step-time { color: var(--dim); font-size: 12.5px; white-space: nowrap; padding-top: 1px; }
.step-body { border-top: 1px solid var(--line); margin: 0; padding: 8px 10px; font-size: 12.5px; color: #c9c4ba; white-space: pre-wrap; overflow-wrap: anywhere; max-height: 260px; overflow-y: auto; }
.ico { width: 18px; height: 18px; border-radius: 50%; flex: none; margin-top: 2px; display: grid; place-items: center; }
.ico svg { width: 12px; height: 12px; stroke-width: 2.6; }
.ico.ok { background: var(--ok); color: #0c1a0d; }
.ico.error { background: var(--bad); color: #1f0a07; }
.ico.waiting { border: 2px solid var(--warn); color: var(--warn); }
.ico.running { border: 2px dashed var(--accent); animation: spin 1.6s linear infinite; }
.ico.stale { border: 2px solid var(--dim); }
@keyframes spin { to { transform: rotate(360deg); } }

.composer { display: flex; align-items: flex-end; gap: 10px; padding: 8px 16px calc(8px + var(--kb-pad, 0px)); border-top: 1px solid var(--line); flex: none; background: var(--bg); }
.composer textarea {
  flex: 1; min-width: 0; resize: none; max-height: 132px; min-height: 44px;
  background: var(--panel); border: 1px solid var(--line-2); border-radius: 10px; padding: 10px 12px;
  font-size: 16px; line-height: 1.4; outline: none;
}
.composer textarea:focus { border-color: var(--accent); }
.composer textarea::placeholder, #code::placeholder { color: var(--dim); }
.send { width: 46px; height: 44px; border-radius: 10px; border: 1px solid var(--accent); color: var(--accent); background: var(--accent-soft); display: grid; place-items: center; flex: none; }
.send:disabled { border-color: var(--line-2); color: var(--dim); background: none; }
.send:not(:disabled):active { background: var(--accent); color: #111; }

/* system ---------------------------------------------------------------- */
.sys-head { display: flex; align-items: flex-start; justify-content: space-between; gap: 12px; padding: 6px 16px 12px; flex: none; }
.sys-head h2 { font-size: 21px; font-weight: 600; }
.prompt { color: var(--accent); }
.sys-head p { font-size: 13px; margin-top: 4px; }
.btn { display: inline-flex; align-items: center; justify-content: center; gap: 8px; height: 40px; padding: 0 14px; border: 1px solid var(--line-2); border-radius: 10px; white-space: nowrap; }
.btn:active { background: var(--panel-2); }
.btn.primary { border-color: var(--accent); background: var(--accent-soft); color: var(--text); width: 100%; height: 48px; }
.btn[disabled] { opacity: .55; }
.btn.spin svg { animation: spin .9s linear infinite; }
.sys-body { flex: 1; min-height: 0; overflow-y: auto; -webkit-overflow-scrolling: touch; padding: 0 16px 16px; }
.cards { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; }
.card { border: 1px solid var(--line); background: var(--panel); border-radius: 12px; padding: 12px; display: flex; gap: 10px; min-height: 84px; }
.card > svg { color: var(--text); margin-top: 1px; }
.card-main { min-width: 0; }
.card-title { display: block; font-size: 14px; }
.card-val { display: flex; align-items: center; gap: 7px; margin-top: 6px; font-size: 15.5px; overflow-wrap: anywhere; }
.card-val.ok { color: var(--ok); } .card-val.bad { color: var(--bad); } .card-val.warn { color: var(--warn); } .card-val.acc { color: var(--accent); }
.card-val .dot { width: 7px; height: 7px; background: currentColor; }
.card-sub { display: block; color: var(--dim); font-size: 12.5px; margin-top: 4px; overflow-wrap: anywhere; }
.sec { display: flex; flex-wrap: wrap; gap: 2px 12px; align-items: baseline; justify-content: space-between; margin: 20px 2px 8px; font-size: 16px; font-weight: 600; }
.sec small { color: var(--dim); font-weight: 400; font-size: 12px; }
.list { border: 1px solid var(--line); border-radius: 12px; background: var(--panel); }
.row { display: flex; gap: 10px; padding: 10px 12px; border-top: 1px solid var(--line); font-size: 13.5px; align-items: baseline; }
.row:first-child { border-top: 0; }
.row .dot { transform: translateY(-1px); }
.row .dot.error { background: var(--bad); } .row .dot.warn { background: var(--warn); } .row .dot.info { background: var(--accent); } .row .dot.none { background: var(--dim); }
.row time { color: var(--dim); white-space: nowrap; }
.row span.t { flex: 1; min-width: 0; overflow-wrap: anywhere; }

/* tabs ------------------------------------------------------------------ */
.tabs { display: flex; border-top: 1px solid var(--line); padding: 6px 0 calc(4px + var(--sab)); flex: none; background: var(--bg); }
.tab { flex: 1; display: flex; flex-direction: column; align-items: center; gap: 3px; color: var(--dim); font-size: 12px; padding: 4px 0; position: relative; }
.tab svg { width: 23px; height: 23px; }
.tab[aria-current="page"] { color: var(--accent); }
.tab .unread { position: absolute; top: 2px; left: calc(50% + 10px); background: var(--bad); }
.app.kb .tabs { display: none; }
.app.kb { --kb-pad: 0px; }

/* login / boot ---------------------------------------------------------- */
.login, .boot { position: fixed; inset: 0; display: grid; place-items: center; padding: calc(24px + var(--sat)) 24px calc(24px + var(--sab)); background: var(--bg); }
.boot .mascot { animation: pulse 1.4s ease-in-out infinite; }
.login-card { width: 100%; max-width: 360px; display: flex; flex-direction: column; align-items: center; gap: 16px; text-align: center; }
.login-card .brand { flex: none; font-size: 26px; }
.login-card form { width: 100%; display: flex; flex-direction: column; gap: 12px; }
#code { width: 100%; height: 54px; text-align: center; font-size: 24px; letter-spacing: 4px; background: var(--panel); border: 1px solid var(--line-2); border-radius: 10px; outline: none; }
#code:focus { border-color: var(--accent); }
.err { color: var(--bad); min-height: 1.4em; font-size: 13.5px; }

@media (min-width: 720px) { .app { border-left: 1px solid var(--line); border-right: 1px solid var(--line); } }
@media (prefers-reduced-motion: reduce) { * { animation: none !important; transition: none !important; } }
