:root {
  color-scheme: light;
  --page: #f9f9f7;
  --surface-1: #fcfcfb;
  --text-primary: #0b0b0b;
  --text-secondary: #52514e;
  --text-muted: #898781;
  --grid: #e1e0d9;
  --axis: #c3c2b7;
  --border: rgba(11, 11, 11, 0.10);
  --accent: #2a78d6;
  --accent-wash: rgba(42, 120, 214, 0.10);
  --series-1: #2a78d6;
  --series-2: #eb6834;
  --series-3: #1baf7a;
  --series-4: #eda100;
  --series-5: #e87ba4;
  --series-6: #008300;
  --series-7: #6250d6;
  --series-8: #e34948;
  --series-other: #b4b3ab;
  --good: #0ca30c;
  --good-text: #006300;
  --warning: #fab219;
  --critical: #d03b3b;
  --diff-add: rgba(12, 163, 12, 0.12);
  --diff-del: rgba(208, 59, 59, 0.12);
  --code-key: #2a78d6;
  --code-string: #006300;
  --code-number: #a14d00;
  --code-literal: #8136b5;
  --code-selection: rgba(42, 120, 214, 0.25);
  --font: system-ui, -apple-system, "Segoe UI", sans-serif;
  --mono: ui-monospace, SFMono-Regular, Menlo, monospace;
}

@media (prefers-color-scheme: dark) {
  :root {
    color-scheme: dark;
    --page: #0d0d0d;
    --surface-1: #1a1a19;
    --text-primary: #f0efec;
    --text-secondary: #c3c2b7;
    --text-muted: #898781;
    --grid: #2c2c2a;
    --axis: #383835;
    --border: rgba(255, 255, 255, 0.10);
    --accent: #3987e5;
    --accent-wash: rgba(57, 135, 229, 0.14);
    --series-1: #3987e5;
    --series-2: #d95926;
    --series-3: #199e70;
    --series-4: #c98500;
    --series-5: #d55181;
    --series-6: #008300;
    --series-7: #9085e9;
    --series-8: #e66767;
    --series-other: #5c5b57;
    --good-text: #0ca30c;
    --diff-add: rgba(12, 163, 12, 0.18);
    --diff-del: rgba(208, 59, 59, 0.20);
    --code-key: #6ba6ee;
    --code-string: #5cc35c;
    --code-number: #e8994f;
    --code-literal: #c08ae6;
    --code-selection: rgba(57, 135, 229, 0.35);
  }
}

* { box-sizing: border-box; }

body {
  margin: 0;
  font: 14px/1.45 var(--font);
  background: var(--page);
  color: var(--text-primary);
}

a { color: var(--accent); text-decoration: none; }
a:hover { text-decoration: underline; }

header.top {
  display: flex;
  align-items: center;
  gap: 24px;
  padding: 10px 24px;
  background: var(--surface-1);
  border-bottom: 1px solid var(--border);
}
header.top .brand { display: flex; }
header.top nav { display: flex; gap: 16px; flex: 1; }
header.top nav a { color: var(--text-secondary); }
header.top nav a.active { color: var(--text-primary); font-weight: 600; }
header.top .who { color: var(--text-muted); display: flex; gap: 12px; align-items: center; }

main { padding: 24px; max-width: 1400px; margin: 0 auto; }

h1 { font-size: 20px; margin: 0 0 16px; }
h2 { font-size: 16px; margin: 24px 0 8px; }
h3 { font-size: 14px; margin: 16px 0 6px; }

.muted { color: var(--text-muted); }
.secondary { color: var(--text-secondary); }
.mono, code, pre, textarea.code { font-family: var(--mono); font-size: 12.5px; }

.card {
  background: var(--surface-1);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 16px;
  margin-bottom: 16px;
}
.card > h2:first-child, .card > .spread:first-child h2 { margin-top: 0; }

.row { display: flex; gap: 16px; align-items: flex-start; flex-wrap: wrap; }
.row > .grow { flex: 1; min-width: 320px; }
.spread { display: flex; justify-content: space-between; align-items: center; gap: 12px; }

table { border-collapse: collapse; width: 100%; }
th, td { text-align: left; padding: 6px 10px; border-bottom: 1px solid var(--grid); vertical-align: top; }
th { color: var(--text-secondary); font-weight: 600; font-size: 12.5px; }
td.num, th.num { text-align: right; font-variant-numeric: tabular-nums; }
td.mono { white-space: nowrap; }
tr.clickable { cursor: pointer; }
tr.clickable:hover td { background: var(--accent-wash); }

pre {
  background: var(--page);
  border: 1px solid var(--border);
  border-radius: 6px;
  padding: 10px;
  overflow: auto;
  max-height: 480px;
  white-space: pre-wrap;
  word-break: break-word;
  margin: 0;
}

form.inline { display: inline; }
.filters { display: flex; gap: 8px; flex-wrap: wrap; align-items: end; margin-bottom: 16px; }
.filters label { display: flex; flex-direction: column; font-size: 12px; color: var(--text-secondary); gap: 2px; }
.filters label.check { flex-direction: row; align-items: center; gap: 6px; padding-bottom: 8px; }

input, select, textarea, button {
  font: inherit;
  color: var(--text-primary);
  background: var(--surface-1);
  border: 1px solid var(--axis);
  border-radius: 6px;
  padding: 5px 8px;
}
textarea { width: 100%; min-height: 80px; resize: vertical; }
textarea.tall { min-height: 220px; }
input[type=text], input[type=url], input[type=email], input[type=number] { min-width: 180px; }
button, .button {
  cursor: pointer;
  background: var(--accent);
  border-color: var(--accent);
  color: #fff;
  font-weight: 600;
  display: inline-block;
}
a.button { padding: 5px 10px; border: 1px solid var(--accent); border-radius: 6px; line-height: 1.45; }
a.button:hover { text-decoration: none; filter: brightness(1.08); }
button.secondary, .button.secondary { background: transparent; color: var(--text-primary); border-color: var(--axis); }
button.small { padding: 1px 8px; }
button.danger { background: transparent; color: var(--critical); border-color: var(--critical); }
button:disabled { opacity: 0.5; cursor: default; }

.field { display: flex; flex-direction: column; gap: 4px; margin-bottom: 12px; }
.field > span { font-weight: 600; font-size: 12.5px; color: var(--text-secondary); }
.field small { color: var(--text-muted); }
.field small.field-error { color: var(--critical); }
.field small.field-error:empty { display: none; }
.field:has(.field-error:not(:empty)) :is(textarea, select) { border-color: var(--critical); }

/* Highlighted JSON textareas (static/json-editor.js): the <pre> must lay text out exactly
   like the textarea above it. */
.json-editor { --gutter: 1ch; position: relative; background: var(--surface-1); border-radius: 6px; }
.json-editor pre, .json-editor textarea {
  margin: 0;
  /* Room for the line-number gutter: 8px, the digits, then a 12px gap. */
  padding: 5px 8px 5px calc(var(--gutter) + 20px);
  border-width: 1px;
  font-family: var(--mono);
  font-size: 12.5px;
  line-height: 1.45;
  letter-spacing: normal;
  tab-size: 4;
  white-space: pre-wrap;
  word-break: normal;
  overflow-wrap: anywhere;
}
.json-editor pre {
  max-height: none;
  overflow: hidden;
  border-color: transparent;
  background: linear-gradient(to right, var(--page) calc(var(--gutter) + 14px), transparent 0);
  color: var(--text-primary);
  pointer-events: none;
  counter-reset: line;
}
.json-editor .line { display: block; position: relative; }
.json-editor .line:empty::after { content: "\200b"; }
.json-editor .line.error { background: var(--diff-del); }
.json-editor .line.error::before { color: var(--critical); font-weight: 600; }
.json-editor .line::before {
  counter-increment: line;
  content: counter(line);
  position: absolute;
  right: calc(100% + 12px);
  white-space: nowrap;
  color: var(--text-muted);
  user-select: none;
}
.json-editor textarea {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  min-height: 0;
  overflow: hidden;
  resize: none;
  background: transparent;
  color: transparent;
  caret-color: var(--text-primary);
}
.json-editor textarea::placeholder { color: var(--text-muted); }
.json-editor textarea::selection { color: transparent; background: var(--code-selection); }
.j-key { color: var(--code-key); }
.j-str { color: var(--code-string); }
.j-num { color: var(--code-number); }
.j-lit { color: var(--code-literal); }
.j-punct { color: var(--text-muted); }

.flash { padding: 10px 12px; border-radius: 6px; margin-bottom: 16px; border: 1px solid var(--border); background: var(--surface-1); }
.flash.error { border-color: var(--critical); }
.flash.secret { border-color: var(--warning); }
.flash.secret code { font-size: 14px; user-select: all; }

/* Status badges: color is always paired with an icon and label. */
.badge { display: inline-flex; align-items: center; gap: 4px; padding: 0 8px; border-radius: 999px; font-size: 12px; border: 1px solid var(--border); color: var(--text-primary); white-space: nowrap; }
.badge::before { content: ""; width: 8px; height: 8px; border-radius: 50%; background: var(--text-muted); }
.badge.succeeded::before { background: var(--good); }
.badge.failed::before { background: var(--critical); }
.badge.running::before { background: var(--accent); }
.badge.queued::before { background: var(--axis); }
.badge.cancelled::before { background: var(--text-muted); }
.badge.pending::before { background: var(--warning); }
.label-chip { display: inline-block; padding: 0 8px; border-radius: 4px; font-size: 12px; background: var(--accent-wash); color: var(--text-primary); margin-right: 4px; }

/* Conversation view */
.msg { border: 1px solid var(--border); border-radius: 6px; margin-bottom: 8px; background: var(--page); }
.msg .role { font-size: 11.5px; font-weight: 700; text-transform: uppercase; letter-spacing: 0.04em; color: var(--text-secondary); padding: 6px 10px 0; }
.msg pre { border: 0; background: transparent; max-height: none; }

/* KPI row */
.kpis { display: grid; grid-template-columns: repeat(auto-fit, minmax(160px, 1fr)); gap: 12px; margin-bottom: 16px; }
.tile { background: var(--surface-1); border: 1px solid var(--border); border-radius: 8px; padding: 12px 14px; }
.tile .label { color: var(--text-secondary); font-size: 12.5px; }
.tile .value { font-size: 24px; font-weight: 600; margin-top: 2px; }
.tile .sub { color: var(--text-muted); font-size: 12px; }

/* Charts. Series colors come from a slot class (s0 = "Other", s1–s8 categorical). */
.s0 { --c: var(--series-other); }
.s1 { --c: var(--series-1); }
.s2 { --c: var(--series-2); }
.s3 { --c: var(--series-3); }
.s4 { --c: var(--series-4); }
.s5 { --c: var(--series-5); }
.s6 { --c: var(--series-6); }
.s7 { --c: var(--series-7); }
.s8 { --c: var(--series-8); }
.chart { position: relative; }
.chart svg { display: block; width: 100%; height: auto; overflow: visible; }
.chart .grid line { stroke: var(--grid); stroke-width: 1; shape-rendering: crispEdges; }
.chart .baseline { stroke: var(--axis); stroke-width: 1; shape-rendering: crispEdges; }
.chart .tick { fill: var(--text-muted); font-size: 11px; font-variant-numeric: tabular-nums; }
.chart .seg { fill: var(--c); }
.chart .line { fill: none; stroke: var(--c); stroke-width: 2; stroke-linejoin: round; stroke-linecap: round; }
.chart .dot { fill: var(--c); stroke: var(--surface-1); stroke-width: 2; }
.chart .end-label { fill: var(--text-secondary); font-size: 11px; }
.chart .crosshair { stroke: var(--axis); stroke-width: 1; visibility: hidden; shape-rendering: crispEdges; }
.chart .hit { fill: transparent; outline: none; }
.chart .hit:hover + .stack, .chart .hit:focus + .stack { opacity: 0.75; }
.chart .hit:hover + .crosshair, .chart .hit:focus + .crosshair { visibility: visible; }
.chart .hit:focus-visible { stroke: var(--text-primary); stroke-width: 1; }
.tooltip {
  position: absolute; z-index: 1; pointer-events: none; background: var(--surface-1); border: 1px solid var(--border);
  border-radius: 6px; padding: 6px 10px; font-size: 12px; box-shadow: 0 2px 8px rgba(0,0,0,0.12); white-space: nowrap;
}
.tooltip > strong { display: block; font-size: 14px; }
.tooltip .t-label { color: var(--text-secondary); }
.tooltip .t-rows:not(:empty) { margin-top: 4px; display: grid; gap: 1px; }
.tooltip .t-rows div { display: flex; align-items: center; gap: 6px; }
.tooltip .t-rows span:last-child { color: var(--text-secondary); }
.line-key { display: inline-block; width: 12px; height: 2px; border-radius: 1px; background: var(--c); flex: none; }
.legend { list-style: none; display: flex; flex-wrap: wrap; gap: 4px 16px; margin: 0 0 8px; padding: 0; font-size: 12.5px; color: var(--text-primary); }
.legend li { display: flex; align-items: center; gap: 6px; }
.legend .swatch { width: 10px; height: 10px; border-radius: 2px; background: var(--c); flex: none; }
.legend .mono { font-size: 12px; }
.chart-head { margin-bottom: 8px; flex-wrap: wrap; }
.chart-head h2 { margin: 0; }
.chart-controls { display: flex; gap: 8px; }
.chart-controls label { display: flex; align-items: center; gap: 6px; font-size: 12px; color: var(--text-secondary); }
/* While the filters refetch, keep the previous render, dimmed. */
#dash.htmx-request > :not(.filters) { opacity: 0.55; transition: opacity 150ms; }

.presets { border: 0; margin: 0; padding: 0; min-width: 0; }
.presets legend { padding: 0; font-size: 12px; color: var(--text-secondary); margin-bottom: 2px; }
.presets div { display: flex; }
.presets label { display: block; }
.presets input { position: absolute; opacity: 0; width: 0; height: 0; }
.presets span { display: block; padding: 5px 10px; border: 1px solid var(--axis); margin-left: -1px; color: var(--text-secondary); font-size: 14px; cursor: pointer; }
.presets label:first-child span { border-radius: 6px 0 0 6px; margin-left: 0; }
.presets label:last-child span { border-radius: 0 6px 6px 0; }
.presets span:hover { background: var(--accent-wash); }
.presets input:checked + span { position: relative; background: var(--accent-wash); border-color: var(--accent); color: var(--text-primary); font-weight: 600; }
.presets input:focus-visible + span { outline: 2px solid var(--accent); outline-offset: 1px; }
.filters a.clear { padding: 6px 0; }
.filters select { max-width: 220px; }
a.filter-link { color: var(--text-primary); }
a.filter-link:hover { color: var(--accent); }
a.filter-link.active { padding: 1px 6px; margin-left: -6px; border-radius: 4px; background: var(--accent-wash); }
a.filter-link.active span { color: var(--text-muted); }

/* Diff */
table.diff td { font-family: var(--mono); font-size: 12.5px; white-space: pre-wrap; padding: 1px 8px; border: 0; }
table.diff tr.insert td { background: var(--diff-add); }
table.diff tr.delete td { background: var(--diff-del); }
table.diff td.sign { width: 16px; color: var(--text-muted); user-select: none; }

.login { max-width: 360px; margin: 12vh auto; text-align: center; }
.login .card { text-align: left; }
.login .logo { display: block; margin: 0 auto 12px; }

.htmx-request.htmx-indicator, .htmx-request .htmx-indicator { opacity: 1; }
.htmx-indicator { opacity: 0; transition: opacity 150ms; }
.msg pre.part-media, .msg pre.part-tool { color: var(--text-secondary); }
.msg pre.part-refusal { color: var(--critical); }
.nowrap { white-space: nowrap; }

dialog.card { margin: auto; width: min(720px, calc(100vw - 48px)); max-height: calc(100vh - 48px); overflow: auto; color: var(--text-primary); }
dialog.card .spread h2 { margin: 0; }
dialog.card .spread { margin-bottom: 12px; }
dialog::backdrop { background: rgba(0, 0, 0, 0.35); }
td details summary { cursor: pointer; }
td details pre { margin-top: 6px; }

/* Trace waterfall. Waits are a thin line, work is a bar; retried attempts are a wash of
   the failed color. */
.waterfall { --label-w: 360px; position: relative; margin-bottom: 8px; }
.wf-head, .wf-row { display: grid; grid-template-columns: var(--label-w) 1fr; align-items: center; }
.wf-head { height: 22px; border-bottom: 1px solid var(--axis); }
.wf-axis { position: relative; height: 100%; }
.wf-axis .tick { position: absolute; bottom: 4px; transform: translateX(-50%); color: var(--text-muted); font-size: 11px; font-variant-numeric: tabular-nums; white-space: nowrap; }
.wf-axis .tick.first { transform: none; }
.wf-axis .tick.last { transform: translateX(-100%); }
.wf-body { position: relative; }
.wf-grid { position: absolute; top: 0; bottom: 0; left: var(--label-w); right: 0; pointer-events: none; }
.wf-grid span { position: absolute; top: 0; bottom: 0; width: 1px; background: var(--grid); }
.wf-row { height: 26px; color: var(--text-primary); position: relative; }
.wf-row:hover, .wf-row:focus-visible { background: var(--accent-wash); text-decoration: none; outline: none; }
.wf-label { display: flex; gap: 6px; align-items: center; min-width: 0; overflow: hidden; white-space: nowrap; padding-right: 12px; font-size: 12.5px; }
.wf-label .mono { font-size: 12px; }
.wf-label .hint { color: var(--text-muted); overflow: hidden; text-overflow: ellipsis; }
.wf-track { position: relative; height: 100%; }
.wf-seg { position: absolute; top: 7px; height: 12px; min-width: 4px; background: var(--c); background-clip: padding-box; }
.wf-seg:not(.wait) + .wf-seg:not(.wait) { border-left: 2px solid transparent; min-width: 6px; }
.wf-seg.end { border-radius: 0 4px 4px 0; }
.wf-seg.retried { min-width: 4px; }
.wf-seg.wait { top: 12px; height: 2px; min-width: 0; background: var(--axis); }
.wf-seg.retried, .key.retried { background: color-mix(in srgb, var(--series-8) 55%, var(--surface-1)); background-clip: padding-box; }
.key { display: inline-block; width: 10px; height: 10px; border-radius: 2px; background: var(--c); flex: none; }
.key.wait { height: 2px; width: 12px; border-radius: 1px; background: var(--axis); }

/* API docs: a sticky contents column beside the reference. */
.docs { display: grid; grid-template-columns: 320px minmax(0, 1fr); gap: 24px; align-items: start; }
.docs-toc { position: sticky; top: 16px; max-height: calc(100vh - 32px); overflow: auto; display: flex; flex-direction: column; gap: 1px; font-size: 12px; }
.docs-toc a { flex: none; color: var(--text-secondary); padding: 1px 0; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; font-family: var(--mono); }
.docs-toc a.toc-section { color: var(--text-primary); font-family: var(--font); font-size: 13px; font-weight: 600; margin-top: 10px; }
.docs-toc a.toc-section:first-child { margin-top: 0; }
.docs-body section { scroll-margin-top: 16px; margin-bottom: 32px; }
.docs-body .card { scroll-margin-top: 16px; }
.docs-body .card > h2:first-child { margin-top: 0; }
.docs-body p { margin: 0 0 10px; max-width: 80ch; }
.docs-body pre { margin-bottom: 10px; }
.docs-body table.fields { margin-bottom: 10px; }
.docs-body table.fields td:first-child { white-space: nowrap; }
.route { display: flex; align-items: center; gap: 8px; margin-bottom: 10px; }
.route code { font-size: 13.5px; }
.method { display: inline-block; min-width: 46px; margin-right: 6px; font-family: var(--mono); font-size: 11px; font-weight: 700; color: var(--c, var(--text-secondary)); }
.route .method { margin-right: 0; padding: 1px 6px; border-radius: 4px; text-align: center; background: color-mix(in srgb, var(--c, var(--text-muted)) 14%, transparent); }
.method.get { --c: var(--series-1); }
.method.post { --c: var(--good-text); }
.method.put, .method.patch { --c: var(--series-4); }
.method.delete { --c: var(--critical); }
.response { margin-bottom: 8px; }
.response .status { font-family: var(--mono); font-size: 12.5px; font-weight: 600; margin-right: 6px; }
.response pre { margin-top: 6px; }
@media (max-width: 900px) { .docs { grid-template-columns: 1fr; } .docs-toc { position: static; max-height: none; } }
