:root {
  --bg: #0f1115;
  --bg-raised: #161920;
  --bg-sunken: #0b0d11;
  --border: #262b36;
  --border-strong: #363d4d;
  --text: #e4e7ee;
  --text-dim: #9aa3b4;
  --text-faint: #6b7384;
  --accent: #ffb000;
  --accent-ink: #1a1203;
  --created: #3ddc97;
  --modified: #6aa9ff;
  --error: #ff6b6b;
  --warn: #ffc857;
  --thinking: #b48ead;
  --mono: ui-monospace, SFMono-Regular, "SF Mono", Menlo, Consolas, monospace;
  --sans: -apple-system, BlinkMacSystemFont, "Segoe UI", Inter, Roboto, sans-serif;
  --radius: 8px;
  color-scheme: dark;
}

/* Three states, in cascade order.
   no data-theme      -> follow the operating system
   data-theme="light" -> light regardless
   data-theme="dark"  -> dark regardless (the :root block above already is)
   The light palette is written twice because a media query cannot be applied
   conditionally to a second selector; tests/js/theme.test.mjs asserts the two
   copies stay identical so they cannot drift apart. */
@media (prefers-color-scheme: light) {
  :root:not([data-theme="dark"]) {
    color-scheme: light;
    --bg: #f6f7f9;
    --bg-raised: #ffffff;
    --bg-sunken: #eceef2;
    --border: #dcdfe6;
    --border-strong: #c3c8d2;
    --text: #1b1f27;
    --text-dim: #5a6373;
    --text-faint: #868e9d;
    --accent: #9a6000;
    --accent-ink: #ffffff;
    --created: #107a4d;
    --modified: #1b63c4;
    --error: #c0362c;
    --warn: #8a5b00;
    --thinking: #7a5680;
  }
}

:root[data-theme="light"] {
  color-scheme: light;
    --bg: #f6f7f9;
    --bg-raised: #ffffff;
    --bg-sunken: #eceef2;
    --border: #dcdfe6;
    --border-strong: #c3c8d2;
    --text: #1b1f27;
    --text-dim: #5a6373;
    --text-faint: #868e9d;
    --accent: #9a6000;
    --accent-ink: #ffffff;
    --created: #107a4d;
    --modified: #1b63c4;
    --error: #c0362c;
    --warn: #8a5b00;
    --thinking: #7a5680;
}

* { box-sizing: border-box; }

/* The UA stylesheet's [hidden] { display: none } is the lowest-priority rule
   there is, so any `display` we set on a class silently defeats it. Several
   panels here are flex/grid containers that get toggled with the hidden
   attribute, so this has to win outright. */
[hidden] { display: none !important; }

html, body {
  margin: 0;
  height: 100%;
  background: var(--bg);
  color: var(--text);
  font: 14px/1.5 var(--sans);
}

body { display: flex; flex-direction: column; overflow: hidden; }

a { color: var(--accent); }

code, pre, textarea, .mono { font-family: var(--mono); }

/* ── top bar ─────────────────────────────────────────────── */

.topbar {
  display: flex; align-items: center; justify-content: space-between;
  gap: 16px; padding: 10px 16px;
  background: var(--bg-raised); border-bottom: 1px solid var(--border);
  flex: 0 0 auto;
}
.brand { display: flex; align-items: baseline; gap: 10px; min-width: 0; }
.logo { color: var(--accent); font-size: 18px; }
.brand h1 { font-size: 15px; margin: 0; letter-spacing: .2px; }
.tagline { color: var(--text-faint); font-size: 12px; white-space: nowrap;
  overflow: hidden; text-overflow: ellipsis; }
.topbar-right { display: flex; align-items: center; gap: 10px; min-width: 0; }
.identity { display: flex; align-items: center; gap: 10px; font-size: 12px;
  color: var(--text-dim); white-space: nowrap; }
.theme-toggle { display: inline-flex; align-items: center; gap: 6px;
  padding: 5px 10px; font-size: 12px; color: var(--text-dim); }
.theme-toggle:hover { color: var(--text); }
.theme-toggle .glyph { font-size: 13px; line-height: 1; }
@media (max-width: 560px) { .theme-toggle .label { display: none; } }

/* ── token gate ──────────────────────────────────────────── */

.gate { flex: 1 1 auto; display: grid; place-items: center; padding: 24px;
  overflow: auto; }
.gate-card { max-width: 560px; background: var(--bg-raised);
  border: 1px solid var(--border); border-radius: var(--radius); padding: 24px; }
.gate-card h2 { margin: 0 0 8px; font-size: 18px; }
.gate-card p { color: var(--text-dim); margin: 0 0 16px; }
#token-form { display: flex; gap: 8px; }
#token-input { flex: 1; min-width: 0; }
.gate-error { color: var(--error); margin: 12px 0 0; }
.gate-note { margin-top: 20px; border-top: 1px solid var(--border); padding-top: 14px; }
.gate-note summary { cursor: pointer; color: var(--text-dim); font-size: 13px; }
.gate-note ul { color: var(--text-dim); font-size: 13px; padding-left: 20px;
  margin: 12px 0 0; }
.gate-note li { margin-bottom: 8px; }

/* ── form controls ───────────────────────────────────────── */

input[type="text"], input[type="password"], input[type="number"],
select, textarea {
  background: var(--bg-sunken); color: var(--text);
  border: 1px solid var(--border-strong); border-radius: 6px;
  padding: 8px 10px; font-size: 13px; font-family: inherit; width: 100%;
}
textarea { resize: vertical; font-family: var(--mono); font-size: 12.5px;
  line-height: 1.5; }
input:focus, select:focus, textarea:focus {
  outline: 2px solid var(--accent); outline-offset: -1px; border-color: var(--accent); }

button {
  font: inherit; font-size: 13px; cursor: pointer; border-radius: 6px;
  padding: 8px 14px; border: 1px solid var(--border-strong);
  background: var(--bg-sunken); color: var(--text);
}
button:hover:not(:disabled) { border-color: var(--accent); }
button:disabled { opacity: .45; cursor: not-allowed; }
button.primary { background: var(--accent); color: var(--accent-ink);
  border-color: var(--accent); font-weight: 600; }
button.ghost { background: transparent; color: var(--text-dim); }
button.small { padding: 5px 9px; font-size: 12px; }
button.danger:hover { border-color: var(--error); color: var(--error); }

label { display: block; font-size: 12px; color: var(--text-dim);
  margin-bottom: 4px; }
.row { display: flex; gap: 10px; }
.row > * { flex: 1; min-width: 0; }
.check { display: flex; align-items: center; gap: 8px; font-size: 13px;
  color: var(--text); }
.check input { width: auto; }

/* ── three-pane layout ───────────────────────────────────── */

.layout {
  flex: 1 1 auto; min-height: 0;
  display: grid; grid-template-columns: 340px minmax(420px, 1fr) 320px;
}
.pane { display: flex; flex-direction: column; min-width: 0; min-height: 0;
  border-right: 1px solid var(--border); }
.pane:last-child { border-right: none; }
.pane-head {
  flex: 0 0 auto; display: flex; align-items: center; justify-content: space-between;
  gap: 8px; padding: 9px 12px; background: var(--bg-raised);
  border-bottom: 1px solid var(--border); min-height: 42px;
}
.pane-head h2 { margin: 0; font-size: 12px; text-transform: uppercase;
  letter-spacing: .8px; color: var(--text-dim); }
.head-actions { display: flex; gap: 6px; align-items: center; }
.pane-body { flex: 1 1 auto; overflow-y: auto; padding: 12px; }
.pane-foot { flex: 0 0 auto; display: flex; gap: 8px; padding: 10px 12px;
  border-top: 1px solid var(--border); background: var(--bg-raised); }
.pane-foot button { flex: 1; }
.dirty-badge { font-size: 11px; color: var(--accent-ink); background: var(--accent);
  padding: 2px 7px; border-radius: 10px; font-weight: 600; }

/* ── config panel ────────────────────────────────────────── */

.group { border: 1px solid var(--border); border-radius: var(--radius);
  margin-bottom: 10px; background: var(--bg-raised); }
.group > summary { cursor: pointer; padding: 9px 11px; font-size: 13px;
  font-weight: 600; list-style: none; display: flex; justify-content: space-between;
  align-items: center; gap: 8px; }
.group > summary::-webkit-details-marker { display: none; }
.group > summary::before { content: "▸"; color: var(--text-faint); font-size: 10px; }
.group[open] > summary::before { content: "▾"; }
.group > summary > span:first-of-type { flex: 1; }
.group-inner { padding: 0 11px 11px; }
.field { margin-bottom: 12px; }
.note { font-size: 11.5px; color: var(--text-faint); margin: 4px 0 0; }
.note.warn { color: var(--warn); }
.pill { font-size: 10.5px; padding: 1px 6px; border-radius: 9px;
  border: 1px solid var(--border-strong); color: var(--text-faint); }
.pill.on { color: var(--created); border-color: var(--created); }
.pill.off { color: var(--text-faint); }
.pill.edited { color: var(--accent); border-color: var(--accent); }

.list-editor { display: flex; flex-direction: column; gap: 5px; }

.task-panel { padding: 10px 14px; border-bottom: 1px solid var(--border); max-height: 45%; overflow: auto; flex: 0 0 auto; }
.task-panel > label { font-size: 12px; margin-right: 8px; }
.task-panel > select { max-width: 75%; }
.task-panel details { margin-top: 8px; }
.task-panel summary { cursor: pointer; font-size: 12px; }
.task-panel pre { white-space: pre-wrap; overflow-wrap: anywhere; font-size: 11px; max-height: 240px; overflow: auto; }
.task-instruction { line-height: 1.5; }
.trial-buttons { display: flex; flex-wrap: wrap; gap: 6px; margin: 8px 0; }
.trial-buttons button { font-size: 11px; }
.trial-columns { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 10px; margin-top: 10px; }
.trial-result { border: 1px solid var(--border); border-radius: 6px; padding: 8px; }
.trial-outcome { font-weight: 600; text-transform: capitalize; margin: 6px 0; }
.trial-outcome.solved { color: var(--created); }
.trial-outcome.failed, .trial-outcome.error { color: var(--warn); }
.trial-check summary { overflow-wrap: anywhere; }
.list-row { display: flex; gap: 5px; align-items: flex-start; }
.list-row textarea { min-height: 38px; }
.list-row button { padding: 4px 7px; font-size: 11px; line-height: 1; }

/* ── run bar + transcript ────────────────────────────────── */

.runbar { gap: 10px; flex-wrap: wrap; }
.runbar .meter { display: flex; align-items: center; gap: 6px; font-size: 11.5px;
  color: var(--text-dim); }
.bar { width: 70px; height: 5px; border-radius: 3px; background: var(--bg-sunken);
  overflow: hidden; }
.bar > i { display: block; height: 100%; background: var(--accent); }
.status-dot { width: 7px; height: 7px; border-radius: 50%;
  background: var(--text-faint); }
.status-dot.live { background: var(--created);
  animation: pulse 1.2s ease-in-out infinite; }
@keyframes pulse { 50% { opacity: .25; } }

.transcript { flex: 1 1 auto; overflow-y: auto; padding: 14px 16px;
  display: flex; flex-direction: column; gap: 10px; background: var(--bg); }
/* Grow the scrollable history instead of compressing earlier rows. Tool cards
   use overflow:hidden, which otherwise lets flex shrink them to their borders. */
.transcript > * { flex-shrink: 0; }
.empty { margin: auto; text-align: center; color: var(--text-faint); max-width: 420px; }
.empty .hint { color: var(--text-dim); font-style: italic; margin-top: 10px; }

.msg { border-radius: var(--radius); padding: 10px 12px; line-height: 1.55;
  white-space: pre-wrap; overflow-wrap: anywhere; }
.msg.user { background: var(--bg-sunken); border: 1px solid var(--border);
  align-self: flex-end; max-width: 85%; }
.msg.assistant { background: transparent; padding: 0 2px; }
.msg.assistant.streaming::after { content: "▍"; color: var(--accent); }
.markdown { white-space: normal; min-width: 0; }
.markdown > :first-child { margin-top: 0; }
.markdown > :last-child { margin-bottom: 0; }
.markdown p, .markdown ul, .markdown ol, .markdown blockquote,
.markdown pre, .markdown table { margin: 0.65em 0; }
.markdown h1, .markdown h2, .markdown h3, .markdown h4,
.markdown h5, .markdown h6 { margin: 1em 0 0.4em; line-height: 1.3; }
.markdown h1 { font-size: 1.5em; }
.markdown h2 { font-size: 1.3em; }
.markdown h3 { font-size: 1.15em; }
.markdown ul, .markdown ol { padding-left: 1.8em; }
.markdown li + li { margin-top: 0.2em; }
.markdown li > p { margin: 0.3em 0; }
.markdown code { padding: 0.1em 0.3em; border-radius: 3px;
  background: var(--bg-sunken); font-size: 0.9em; }
.markdown pre { padding: 10px 12px; border: 1px solid var(--border);
  border-radius: var(--radius); background: var(--bg-sunken);
  overflow-x: auto; white-space: pre; overflow-wrap: normal; }
.markdown pre code { padding: 0; background: transparent; }
.markdown blockquote { padding-left: 12px; margin-left: 0;
  border-left: 3px solid var(--border-strong); color: var(--text-dim); }
.markdown table { display: block; max-width: 100%; overflow-x: auto;
  border-collapse: collapse; }
.markdown th, .markdown td { border: 1px solid var(--border); padding: 6px 10px; }
.markdown th { background: var(--bg-sunken); }
.markdown a { color: var(--accent); text-decoration: underline; }
.markdown img { max-width: 100%; height: auto; }
.markdown hr { border: 0; border-top: 1px solid var(--border); margin: 1em 0; }
.msg.status { color: var(--text-faint); font-size: 12.5px; font-style: italic;
  padding: 2px; }
.msg.error { border: 1px solid var(--error); background: color-mix(in srgb,
  var(--error) 10%, transparent); color: var(--text); }
.msg.error .msg-label { color: var(--error); font-weight: 600; font-size: 12px;
  display: block; margin-bottom: 4px; }
.msg.error .msg-hint { display: block; margin-top: 8px; font-size: 12.5px;
  color: var(--text-dim); font-style: normal; }
.msg.limit { border: 1px solid var(--warn); background: color-mix(in srgb,
  var(--warn) 10%, transparent); font-size: 12.5px; }
.msg.advisory { border: 1px dashed var(--border-strong); color: var(--text-dim);
  font-size: 12.5px; }

.thinking { border-left: 2px solid var(--thinking); padding: 2px 0 2px 10px; }
.thinking summary { cursor: pointer; color: var(--thinking); font-size: 12px;
  list-style: none; }
.thinking summary::-webkit-details-marker { display: none; }
.thinking pre { margin: 6px 0 0; white-space: pre-wrap; font-size: 12px;
  color: var(--text-dim); }

/* tool rows */
.tool { border: 1px solid var(--border); border-radius: var(--radius);
  background: var(--bg-raised); overflow: hidden; }
.tool.err { border-color: var(--error); }
.tool-head { display: flex; align-items: center; gap: 8px; padding: 7px 10px;
  cursor: pointer; font-size: 12.5px; }
.tool-name { font-family: var(--mono); color: var(--accent); font-weight: 600; }
.tool-args { font-family: var(--mono); color: var(--text-dim); flex: 1;
  overflow: hidden; text-overflow: ellipsis; white-space: nowrap; font-size: 12px; }
.tool-state { font-size: 11px; color: var(--text-faint); }
.tool.err .tool-state { color: var(--error); }
.tool-body { border-top: 1px solid var(--border); padding: 8px 10px;
  background: var(--bg-sunken); }
.tool-body pre { margin: 0; white-space: pre-wrap; font-size: 12px;
  max-height: 320px; overflow: auto; color: var(--text-dim); }
.tool-body pre.diff .add { color: var(--created); }
.tool-body pre.diff .del { color: var(--error); }
.tool-body pre.diff .hd { color: var(--modified); }
.spin { display: inline-block; animation: spin 1s linear infinite; }
@keyframes spin { to { transform: rotate(360deg); } }

/* composer */
.composer { flex: 0 0 auto; border-top: 1px solid var(--border);
  background: var(--bg-raised); padding: 10px 12px; }
.composer-actions { display: flex; align-items: center; gap: 8px; margin-top: 8px; }
.composer-hint { flex: 1; font-size: 11.5px; color: var(--text-faint); }

/* ── workspace ───────────────────────────────────────────── */

.quota { flex: 0 0 auto; padding: 7px 12px; font-size: 11.5px;
  color: var(--text-faint); border-bottom: 1px solid var(--border);
  display: flex; align-items: center; gap: 8px; }
.tree { flex: 1 1 auto; overflow-y: auto; padding: 6px 0; min-height: 90px; }
.tree-empty { padding: 20px 14px; color: var(--text-faint); font-size: 12.5px;
  text-align: center; }
.node { display: flex; align-items: center; gap: 6px; padding: 3px 12px;
  font-size: 12.5px; cursor: pointer; font-family: var(--mono); }
.node:hover { background: var(--bg-raised); }
.node.active { background: var(--bg-sunken); color: var(--accent); }
.node.dir { color: var(--text-dim); cursor: default; }
.node .badge { width: 7px; height: 7px; border-radius: 50%; flex: 0 0 auto; }
.node .badge.created { background: var(--created); }
.node .badge.modified { background: var(--modified); }
.node .size { margin-left: auto; color: var(--text-faint); font-size: 11px; }

/* Only one workspace view is mounted at a time; both need to fill the pane. */
.ws-view { flex: 1 1 auto; display: flex; flex-direction: column; min-height: 0; }

.editor { flex: 1 1 auto; display: flex; flex-direction: column; min-height: 0;
  border-top: 1px solid var(--border); }
.editor-head { display: flex; align-items: center; gap: 8px; padding: 7px 10px;
  background: var(--bg-raised); border-bottom: 1px solid var(--border); }
.editor-path { flex: 1; font-family: var(--mono); font-size: 12px;
  overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
#editor-text { flex: 1 1 auto; border: none; border-radius: 0; resize: none;
  min-height: 140px; }
.editor-note { margin: 0; padding: 5px 10px; font-size: 11.5px;
  color: var(--text-faint); border-top: 1px solid var(--border); min-height: 24px; }

.diagnostics { flex: 0 0 auto; max-height: 150px; overflow-y: auto;
  border-top: 1px solid var(--border); }
.diagnostics:empty { display: none; }
.diag { padding: 7px 12px; font-size: 11.5px; border-bottom: 1px solid var(--border);
  color: var(--text-dim); }
.diag.warning { color: var(--warn); }
.diag.error { color: var(--error); }

/* ── terminal ────────────────────────────────────────────── */

.term-note { flex: 0 0 auto; margin: 0; padding: 8px 12px; font-size: 11.5px;
  color: var(--text-faint); border-bottom: 1px solid var(--border);
  background: var(--bg-raised); }
.term-note code { color: var(--text-dim); }
.term-screen { flex: 1 1 auto; min-height: 80px; min-width: 0; padding: 8px;
  overflow: hidden; background: var(--bg-sunken); }
.term-screen .xterm { height: 100%; }
.term-status { flex: 0 0 auto; margin: 0; padding: 6px 12px;
  font-size: 11px; color: var(--text-faint); border-top: 1px solid var(--border); }

/* ── prompt drawer ───────────────────────────────────────── */

/* A row of its own rather than an overlay: as a flex child of body it shortens
   the three panes instead of covering the config pane's Apply button. */
.drawer { flex: 0 0 46vh; min-height: 0;
  background: var(--bg-raised); border-top: 1px solid var(--border-strong);
  display: flex; flex-direction: column;
  box-shadow: 0 -12px 32px rgba(0, 0, 0, .25); }
.drawer-head { display: flex; align-items: center; justify-content: space-between;
  padding: 7px 12px; border-bottom: 1px solid var(--border); }
.tabs { display: flex; gap: 4px; }
.tab { background: transparent; border: none; color: var(--text-dim);
  padding: 5px 11px; font-size: 12.5px; }
.tab.active { color: var(--accent); background: var(--bg-sunken); }
.drawer-meta { font-size: 11.5px; color: var(--text-faint); }
.drawer-body { flex: 1 1 auto; overflow-y: auto; padding: 12px 14px; }
.drawer-body pre { margin: 0; white-space: pre-wrap; font-size: 12px;
  line-height: 1.55; color: var(--text); }
.src { border: 1px solid var(--border); border-radius: 6px; margin-bottom: 8px; }
.src > summary { cursor: pointer; padding: 7px 10px; font-size: 12.5px;
  display: flex; gap: 8px; align-items: center; }
.src > summary .kind { font-family: var(--mono); color: var(--accent);
  font-size: 11px; }
.src > summary .where { color: var(--text-faint); font-size: 11px; margin-left: auto; }
.src pre { padding: 0 10px 10px; }
.diffline { display: block; }
.diffline.add { color: var(--created); background: color-mix(in srgb,
  var(--created) 8%, transparent); }
.diffline.del { color: var(--error); background: color-mix(in srgb,
  var(--error) 8%, transparent); }
.drawer-toggle { position: fixed; right: 14px; bottom: 14px; z-index: 19;
  background: var(--bg-raised); box-shadow: 0 4px 14px rgba(0,0,0,.3); }

/* ── narrow screens ──────────────────────────────────────── */

@media (max-width: 1080px) {
  body { overflow: auto; }
  .layout { grid-template-columns: 1fr; }
  .pane { border-right: none; border-bottom: 1px solid var(--border);
    max-height: 72vh; }
  .tagline { display: none; }
  .drawer { flex-basis: 60vh; }
}
@media (max-width: 560px) {
  .topbar { padding: 10px 16px; }
  .pane-body, .transcript { padding-left: 16px; padding-right: 16px; }
}
