:root {
  --bg: #f6f7f9;
  --surface: #ffffff;
  --surface-2: #f1f3f6;
  --border: #e1e4ea;
  --text: #191919;
  --text-2: #5f6670;
  --accent: #257de0;
  --accent-ink: #ffffff;
  --good: #0a8a1f;
  --good-bg: #e3f5e6;
  --warn: #9a6400;
  --warn-bg: #fff3d6;
  --run: #1b64be;
  --run-bg: #e2eefc;
  --wait: #6f42c1;
  --wait-bg: #efe8fb;
  --bad: #c62f30;
  --bad-bg: #fde6e6;
  --mute: #6b717a;
  --mute-bg: #eceef1;
  --series-1: #257de0;
  --critical: #dc3c3d;
  --mono: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
  --hl-kw: #7c3aed;
  --hl-type: #0e7490;
  --hl-fn: #1d4ed8;
  --hl-str: #0a7d33;
  --hl-num: #b45309;
  --hl-com: #6b717a;
  --hl-dq: #be185d;
  --hl-ident: #374151;
  --hl-key: #1d4ed8;
  --radius: 8px;
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #121212;
    --surface: #191919;
    --surface-2: #222326;
    --border: #2f3136;
    --text: #efefef;
    --text-2: #9c9c9c;
    --accent: #338ced;
    --good: #4cc760;
    --good-bg: #16301b;
    --warn: #f2b53c;
    --warn-bg: #352a10;
    --run: #6aabf1;
    --run-bg: #14273f;
    --wait: #b794f4;
    --wait-bg: #2a1f40;
    --bad: #f07070;
    --bad-bg: #3a1717;
    --mute: #a0a5ad;
    --mute-bg: #2a2c30;
    --series-1: #338ced;
    --critical: #dc3c3d;
    --hl-kw: #c4a5ff;
    --hl-type: #67d4e6;
    --hl-fn: #8ab4ff;
    --hl-str: #7ee093;
    --hl-num: #f5b971;
    --hl-com: #8b929a;
    --hl-dq: #f472b6;
    --hl-ident: #d1d5db;
    --hl-key: #8ab4ff;
    color-scheme: dark;
  }
}

* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; }
body {
  font: 14px/1.45 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  background: var(--bg);
  color: var(--text);
  display: flex;
}
a { color: var(--accent); text-decoration: none; }
a:hover { text-decoration: underline; }
code, pre, .mono { font-family: var(--mono); font-size: 12.5px; }
pre {
  background: var(--surface-2); border: 1px solid var(--border);
  border-radius: 6px; padding: 10px; overflow: auto; max-height: 420px;
  white-space: pre-wrap; word-break: break-word; margin: 0;
}

/* layout */
nav.side {
  width: 210px; flex: none; background: var(--surface);
  border-right: 1px solid var(--border); padding: 14px 10px;
  display: flex; flex-direction: column; gap: 2px; height: 100vh;
  position: sticky; top: 0;
}
nav.side .brand { font-weight: 700; font-size: 16px; padding: 4px 10px 14px; }
nav.side .brand small { display: block; font-weight: 400; color: var(--text-2); font-size: 12px; }
nav.side a {
  color: var(--text); padding: 7px 10px; border-radius: 6px; display: block;
}
nav.side a:hover { background: var(--surface-2); text-decoration: none; }
nav.side a.active { background: var(--run-bg); color: var(--run); font-weight: 600; }
nav.side .spacer { flex: 1; }
nav.side .who { font-size: 12px; color: var(--text-2); padding: 8px 10px; border-top: 1px solid var(--border); }

main { flex: 1; min-width: 0; padding: 20px 28px 60px; }
.page-head { display: flex; align-items: center; gap: 12px; margin-bottom: 16px; flex-wrap: wrap; }
.page-head h1 { font-size: 20px; margin: 0; }
.page-head .grow { flex: 1; }
.crumbs { color: var(--text-2); font-size: 13px; margin-bottom: 4px; }

.card {
  background: var(--surface); border: 1px solid var(--border);
  border-radius: var(--radius); padding: 14px 16px; margin-bottom: 16px;
}
.card h2 { font-size: 15px; margin: 0 0 10px; display: flex; align-items: center; gap: 8px; }
.card h2 .grow { flex: 1; }
.grid { display: grid; gap: 16px; }
.grid.two { grid-template-columns: repeat(auto-fit, minmax(420px, 1fr)); }

.tiles { display: grid; grid-template-columns: repeat(auto-fit, minmax(125px, 1fr)); gap: 12px; margin-bottom: 16px; }
.tile { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); padding: 12px 14px; }
.tile .label { color: var(--text-2); font-size: 12px; }
.tile .value { font-size: 24px; font-weight: 650; margin-top: 2px; font-variant-numeric: tabular-nums; }
.tile .value.bad { color: var(--bad); }
/* a tile whose value is a name or a short phrase */
.tile .value.small { font-size: 15px; font-weight: 600; margin-top: 6px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }

/* flow page: the last run, next to Run */
/* a long list in a page that scrolls: it scrolls inside its card */
.scroll.capped { max-height: 420px; overflow: auto; }

/* flows page: the flows a multi-flow run starts, and their runs */
ul.run-list { margin: 8px 0 0; padding-left: 18px; max-height: 50vh; overflow: auto; }

/* pool page: what kind of pool, and whether it's at its limit */
.pool-tags { align-items: center; font-size: 13px; }

.last-run { display: flex; flex-wrap: wrap; align-items: center; gap: 4px 8px; font-size: 13px; margin-right: 4px; }

dl.kv { display: grid; grid-template-columns: max-content 1fr; gap: 4px 16px; margin: 0; }
dl.kv dt { color: var(--text-2); }
dl.kv dd { margin: 0; min-width: 0; overflow-wrap: anywhere; }

/* tables */
.table-wrap { overflow-x: auto; }
table.data { border-collapse: collapse; width: 100%; font-size: 13px; }
table.data th, table.data td { text-align: left; padding: 6px 10px; border-bottom: 1px solid var(--border); vertical-align: top; }
table.data th { color: var(--text-2); font-weight: 600; font-size: 12px; white-space: nowrap; background: var(--surface); position: sticky; top: 0; }
table.data tr.click { cursor: pointer; }
table.data tr.click:hover td { background: var(--surface-2); }
table.data td.num { text-align: right; font-variant-numeric: tabular-nums; }
table.data td.trunc { max-width: 300px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
/* keep truncated cells on one line even when they hold styled spans (e.g. .err-text) */
table.data td.trunc > * { white-space: nowrap; }
.empty { color: var(--text-2); padding: 18px; text-align: center; }

/* badges */
.badge {
  display: inline-flex; align-items: center; gap: 5px; padding: 1px 8px;
  border-radius: 999px; font-size: 12px; font-weight: 600; white-space: nowrap;
  background: var(--mute-bg); color: var(--mute);
}
.badge::before { content: ""; width: 7px; height: 7px; border-radius: 50%; background: currentColor; }
.badge.good { background: var(--good-bg); color: var(--good); }
.badge.run { background: var(--run-bg); color: var(--run); }
.badge.wait { background: var(--wait-bg); color: var(--wait); }
.badge.warn { background: var(--warn-bg); color: var(--warn); }
.badge.bad { background: var(--bad-bg); color: var(--bad); }
.badge.run::before { animation: pulse 1.4s ease-in-out infinite; }
@keyframes pulse { 50% { opacity: .3; } }

/* controls */
button, .btn {
  font: inherit; font-size: 13px; border: 1px solid var(--border);
  background: var(--surface); color: var(--text); padding: 5px 12px;
  border-radius: 6px; cursor: pointer; white-space: nowrap;
}
button:hover { background: var(--surface-2); }
button.primary { background: var(--accent); border-color: var(--accent); color: var(--accent-ink); }
button.primary:hover { filter: brightness(1.08); }
button.danger { color: var(--bad); border-color: color-mix(in srgb, var(--bad) 40%, var(--border)); }
button.danger:hover { background: var(--bad-bg); }
button.small { padding: 2px 8px; font-size: 12px; }
button:disabled { opacity: .5; cursor: default; }
input, select, textarea {
  font: inherit; font-size: 13px; color: var(--text); background: var(--surface);
  border: 1px solid var(--border); border-radius: 6px; padding: 5px 8px;
}
textarea { font-family: var(--mono); font-size: 12.5px; width: 100%; }
input:focus, select:focus, textarea:focus, button:focus-visible { outline: 2px solid var(--accent); outline-offset: 1px; }
.filters { display: flex; gap: 8px; flex-wrap: wrap; align-items: center; margin-bottom: 12px; }
.filters label { font-size: 12px; color: var(--text-2); display: flex; gap: 6px; align-items: center; }
.actions { display: flex; gap: 8px; flex-wrap: wrap; }
.pager { display: flex; gap: 8px; align-items: center; justify-content: flex-end; margin-top: 10px; color: var(--text-2); font-size: 12px; }
.muted { color: var(--text-2); }
.err-text { color: var(--bad); white-space: pre-wrap; }
.refresh { font-size: 12px; color: var(--text-2); display: flex; gap: 6px; align-items: center; }

/* graph */
.graph { overflow: auto; text-align: center; min-height: 120px; max-height: 640px; background: var(--surface); }
.graph svg .node { cursor: pointer; }

/* pan / zoom viewer (panZoom() in app.js) */
.graph.pz {
  position: relative; overflow: hidden; max-height: none; text-align: left;
  height: clamp(320px, 60vh, 720px);
  cursor: grab; touch-action: none; user-select: none; -webkit-user-select: none;
  border-radius: 6px; outline: none;
}
.graph.pz:focus-visible { box-shadow: 0 0 0 2px var(--accent); }
.graph.pz.dragging, .graph.pz.dragging svg .node { cursor: grabbing; }
.pz-stage { position: absolute; inset: 0; }
.pz-stage svg { position: absolute; left: 0; top: 0; transform-origin: 0 0; max-width: none; }
.pz-error { position: absolute; inset: 0; overflow: auto; padding: 8px; cursor: auto; }
.pz-ctl {
  position: absolute; right: 8px; bottom: 8px; display: flex; gap: 4px; align-items: center;
  background: var(--surface); border: 1px solid var(--border); border-radius: 8px; padding: 3px;
  box-shadow: 0 2px 8px rgba(0,0,0,.12); cursor: default;
}
.pz-ctl button { min-width: 28px; }
.pz-level { font-size: 12px; color: var(--text-2); min-width: 42px; text-align: center; font-variant-numeric: tabular-nums; }
.legend { display: flex; gap: 14px; flex-wrap: wrap; font-size: 12px; color: var(--text-2); margin-top: 8px; }
.legend span { display: inline-flex; gap: 6px; align-items: center; }
.legend i { width: 12px; height: 12px; border-radius: 3px; display: inline-block; border: 1px solid var(--border); }

/* modal */
dialog {
  border: 1px solid var(--border); border-radius: 10px; padding: 0;
  background: var(--surface); color: var(--text); width: min(640px, 94vw);
  box-shadow: 0 10px 40px rgba(0,0,0,.25);
}
dialog::backdrop { background: rgba(0,0,0,.35); }
dialog form { padding: 18px 20px; display: flex; flex-direction: column; gap: 12px; }
dialog h3 { margin: 0; font-size: 16px; }
dialog .field { display: flex; flex-direction: column; gap: 4px; }
dialog .field > span { font-size: 12px; color: var(--text-2); }
dialog.wide { width: min(1040px, 96vw); }
.args-split { display: grid; grid-template-columns: minmax(0, 3fr) minmax(0, 2fr); gap: 14px; align-items: start; }
.args-split .args-json { min-height: 220px; resize: vertical; }
@media (max-width: 760px) { .args-split { grid-template-columns: 1fr; } }
.arg-form { display: grid; grid-template-columns: repeat(auto-fill, minmax(180px, 1fr)); gap: 10px 14px; max-height: 50vh; overflow: auto; align-content: start; }
dialog .field small { font-size: 11.5px; }
.field-check { display: flex; gap: 6px; align-items: center; font-size: 12.5px; flex-wrap: wrap; }
.field-check small { flex-basis: 100%; }
dialog .buttons { display: flex; justify-content: flex-end; gap: 8px; margin-top: 6px; }
.checklist { display: flex; flex-wrap: wrap; gap: 6px 14px; max-height: 160px; overflow: auto; padding: 6px; border: 1px solid var(--border); border-radius: 6px; }
.checklist label { font-size: 12.5px; display: flex; gap: 5px; align-items: center; }

/* toast */
#toasts { position: fixed; right: 18px; bottom: 18px; display: flex; flex-direction: column; gap: 8px; z-index: 50; }
.toast { background: var(--surface); border: 1px solid var(--border); border-left: 4px solid var(--good); border-radius: 6px; padding: 10px 14px; box-shadow: 0 4px 16px rgba(0,0,0,.15); max-width: 460px; white-space: pre-wrap; }
.toast.error { border-left-color: var(--bad); }

/* log */
.log { font-family: var(--mono); font-size: 12px; background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); height: calc(100vh - 190px); overflow: auto; padding: 6px 0; }
.log div { padding: 1px 12px; white-space: pre-wrap; word-break: break-word; }
.log div:hover { background: var(--surface-2); }
.log .t { color: var(--text-2); margin-right: 8px; }
.log .WARNING { color: var(--warn); }
.log .ERROR { color: var(--bad); font-weight: 600; }

/* chart */
.chart { position: relative; }
.chart svg { display: block; width: 100%; height: 180px; }
.chart .tip { position: absolute; pointer-events: none; background: var(--surface); border: 1px solid var(--border); border-radius: 6px; padding: 6px 9px; font-size: 12px; box-shadow: 0 4px 14px rgba(0,0,0,.15); display: none; white-space: nowrap; }
/* dashboard worker panel: slots by state */
.wk-big { display: flex; gap: 18px; flex-wrap: wrap; font-size: 12px; color: var(--text-2); margin: 2px 0 8px; }
.wk-big b { font-size: 18px; font-weight: 600; color: var(--text); margin-right: 4px; }
.wk-bar { display: flex; height: 14px; border-radius: 4px; overflow: hidden; background: var(--mute-bg); }
.wk-bar .r { background: var(--run); }
.wk-bar .i { background: color-mix(in srgb, var(--run) 32%, var(--surface)); }
.wk-row { display: grid; grid-template-columns: minmax(80px, 140px) minmax(0, 1fr) 64px; gap: 8px; align-items: center; font-size: 12px; margin: 5px 0; }
.wk-row .name { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.wk-row .name.all { font-weight: 600; }
.wk-row .n { color: var(--text-2); text-align: right; font-variant-numeric: tabular-nums; }
.wk-sep { border-top: 1px solid var(--border); margin: 10px 0 6px; }
.wk-pools { display: flex; gap: 8px; align-items: center; flex-wrap: wrap; font-size: 12px; color: var(--text-2); }
.wk-pools .pill { border-radius: 6px; padding: 2px 8px; font-weight: 600; background: var(--mute-bg); color: var(--text-2); }
.wk-pools .pill.warn { background: var(--warn-bg); color: var(--warn); }
/* the legend sits at the panel's bottom, however many rows are above it */
#workers { display: flex; flex-direction: column; }
#workers .wk-legend { margin-top: auto; padding-top: 8px; }
.legend i.wk-r { background: var(--run); border-color: var(--run); }
.legend i.wk-i { background: color-mix(in srgb, var(--run) 32%, var(--surface)); }
.legend i.wk-f { background: var(--mute-bg); }
.bar-fill { height: 6px; background: var(--mute-bg); border-radius: 3px; overflow: hidden; min-width: 80px; }
.bar-fill > div { height: 100%; background: var(--series-1); border-radius: 3px; }

.split { display: grid; grid-template-columns: minmax(0, 1fr) minmax(0, 1fr); gap: 16px; }
@media (max-width: 1100px) { .split { grid-template-columns: 1fr; } }
.notice { background: var(--warn-bg); color: var(--text); border: 1px solid color-mix(in srgb, var(--warn) 40%, var(--border)); border-radius: 6px; padding: 8px 12px; font-size: 13px; margin-bottom: 12px; }
.ok-text { color: var(--good); }

/* ---- fit-to-window pages (flow runs list, run detail) ----------------------
 * main.fit pages fill exactly the viewport: no page scrollbar.  Panels share
 * the space and any overflow scrolls inside the panel that has it.  On small
 * windows these pages fall back to the normal scrolling layout.
 */
.tabs { display: flex; gap: 2px; align-items: center; }
.tabs button { border: none; background: none; padding: 4px 10px; border-radius: 6px; font-weight: 600; color: var(--text-2); font-size: 14px; }
.tabs button.on { background: var(--run-bg); color: var(--run); }
.tabs button .n { font-weight: 500; font-size: 12px; margin-left: 4px; color: var(--text-2); }
.graph-tools { display: flex; gap: 6px; }

@media (min-width: 1000px) and (min-height: 560px) {
  main.fit { height: 100vh; overflow: hidden; display: flex; flex-direction: column; padding-bottom: 16px; }
  main.fit > .crumbs, main.fit > .page-head { flex: none; }
  main.fit .fit-body { flex: 1; min-height: 0; }
  main.fit .card.fill { display: flex; flex-direction: column; min-height: 0; margin-bottom: 0; overflow: hidden; }
  main.fit .card.fill > h2, main.fit .card.fill > .filters, main.fit .card.fill > .legend, main.fit .card.fill > .pager { flex: none; }
  main.fit .scroll { flex: 1; min-height: 0; overflow: auto; }
  main.fit .scroll .table-wrap { overflow: visible; }
  main.fit .nowrap td { white-space: nowrap; }

  /* runs list: one panel, rows sized to fit */
  main.fit .runs-body { display: flex; flex-direction: column; height: 100%; }

  /* dashboard: orchestrator | tiles, chart | workers, recent runs */
  main.fit .dash-grid {
    display: grid; gap: 14px; height: 100%;
    grid-template-columns: minmax(0, 1fr) minmax(0, 1fr);
    grid-template-rows: auto minmax(150px, 1fr) minmax(140px, 1fr);
  }
  main.fit .dash-grid .card { padding: 12px 14px; }
  main.fit .dash-grid .card h2 { margin-bottom: 8px; }
  main.fit .dash-tiles { margin: 0; grid-template-columns: repeat(4, minmax(0, 1fr)); gap: 10px; align-content: stretch; }
  main.fit .dash-tiles .tile { padding: 8px 12px; display: flex; flex-direction: column; justify-content: center; }
  main.fit .dash-tiles .tile .value { font-size: 22px; }
  main.fit .dash-orch dl.kv { grid-template-columns: max-content minmax(0, 1fr); gap: 2px 14px; font-size: 13px; }
  main.fit .dash-orch dd { white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
  main.fit .dash-recent { grid-column: 1 / -1; }
  main.fit .chart-box { overflow: hidden; }

  /* run detail: side column (summary, arguments) | graph over nodes/tasks */
  main.fit .run-grid {
    display: grid; gap: 16px; height: 100%;
    grid-template-columns: minmax(260px, 24%) minmax(0, 1fr);
    grid-template-rows: minmax(0, 3fr) minmax(0, 2fr);
  }
  main.fit .run-grid > .side { grid-row: 1 / span 2; display: flex; flex-direction: column; gap: 16px; min-height: 0; }
  main.fit .run-grid > .side > #summary { flex: 0 1 auto; max-height: 65%; }
  main.fit .run-grid > .side > .args-card { flex: 1 1 0; }
  main.fit .run-grid > .side .kv { grid-template-columns: max-content minmax(0, 1fr); }
  main.fit .run-grid pre { max-height: none; }
  main.fit .graph { flex: 1; min-height: 0; max-height: none; height: auto; }
}
@media not ((min-width: 1000px) and (min-height: 560px)) {
  main.fit .run-grid > .side { display: grid; grid-template-columns: repeat(auto-fit, minmax(320px, 1fr)); gap: 16px; }
}
.tabnum { font-variant-numeric: tabular-nums; }

/* flow page code viewer */
.code-meta { font-size: 12px; margin-bottom: 6px; }
pre.code-src { max-height: 560px; white-space: pre; word-break: normal; tab-size: 2; }
/* Code panel highlighting (PL/pgSQL, SQL, JSON) */
.hl-kw { color: var(--hl-kw); font-weight: 600; }
.hl-type { color: var(--hl-type); }
.hl-fn { color: var(--hl-fn); }
.hl-str { color: var(--hl-str); }
.hl-num, .hl-param { color: var(--hl-num); }
.hl-com { color: var(--hl-com); font-style: italic; }
.hl-dq { color: var(--hl-dq); font-weight: 600; }
.hl-ident { color: var(--hl-ident); }
.hl-key { color: var(--hl-key); }
#code-sel { max-width: 70%; }

@media (min-width: 1000px) and (min-height: 560px) {
  /* targets & pools: targets on top, pools | workers below */
  main.fit .tp-grid {
    display: grid; gap: 14px; height: 100%;
    grid-template-columns: minmax(420px, 1fr) minmax(0, 2.5fr);
    grid-template-rows: fit-content(45%) minmax(150px, 1fr);
  }
  main.fit .tp-grid .card { padding: 12px 14px; }
  main.fit .tp-grid .card h2 { margin-bottom: 8px; }
  main.fit .tp-wide { grid-column: 1 / -1; }
}
@media (min-width: 1000px) and (min-height: 560px) {
  /* node, target and pool pages: tiles across the top, panels below */
  main.fit .fit-tiles { grid-column: 1 / -1; margin: 0; }
  main.fit .scroll.capped { max-height: none; }

  /* node: configuration over runs / steps tabs | code down the right */
  main.fit .node-grid {
    display: grid; gap: 14px; height: 100%;
    grid-template-columns: minmax(0, 1fr) minmax(0, 1fr);
    grid-template-rows: auto fit-content(40%) minmax(0, 1fr);
  }
  main.fit .node-code { grid-column: 2; grid-row: 2 / span 2; }
  main.fit .node-code > .code-meta { flex: none; }
  main.fit .node-code pre.code-src { flex: 1; min-height: 0; max-height: none; }

  /* target: configuration over flow nodes | running now */
  main.fit .tg-grid {
    display: grid; gap: 14px; height: 100%;
    grid-template-columns: minmax(0, 2fr) minmax(0, 3fr);
    grid-template-rows: auto fit-content(50%) minmax(0, 1fr);
  }
  main.fit .tg-run { grid-column: 2; grid-row: 2 / span 2; }

  /* pool: running over waiting */
  main.fit .pl-grid {
    display: grid; gap: 14px; height: 100%;
    grid-template-columns: minmax(0, 1fr);
    grid-template-rows: auto fit-content(35%) minmax(0, 1fr);
  }
}

/* the draggable divider between a fit page's two columns (columnSplitter) */
.vsplit { display: none; }
@media (min-width: 1000px) and (min-height: 560px) {
  main.fit .flow-grid, main.fit .node-grid { position: relative; }
  main.fit .vsplit { display: block; position: absolute; bottom: 0; width: 12px; margin-left: -6px; cursor: col-resize; z-index: 5; touch-action: none; }
  main.fit .vsplit::after { content: ""; position: absolute; left: 5px; top: 0; bottom: 0; width: 2px; border-radius: 1px; }
  main.fit .vsplit:hover::after, main.fit .vsplit.dragging::after { background: var(--accent); }
}

/* settings page */
.settings-card { max-width: 900px; }
.settings-card dl.kv { gap: 6px 24px; }

@media (min-width: 1000px) and (min-height: 560px) {
  /* flow page: graph over nodes / runs tabs | code down the right */
  main.fit .flow-grid {
    display: grid; gap: 16px; height: 100%;
    grid-template-columns: minmax(0, 3fr) minmax(0, 2fr);
    grid-template-rows: minmax(0, 3fr) minmax(0, 2fr);
  }
  main.fit .flow-code { grid-column: 2; grid-row: 1 / span 2; }
  main.fit .flow-code > .code-meta { flex: none; }
  main.fit .flow-code pre.code-src { flex: 1; min-height: 0; max-height: none; }
  main.fit .flow-tabs > h2 { flex: none; }
}
