:root {
  --bg: #f6f7f9;
  --panel: #ffffff;
  --ink: #1b1d21;
  --muted: #5c6270;
  --line: #d8dbe2;
  --accent: #2f6feb;
  --accent-soft: #dbe6ff;
  --accent-faint: #eef3ff;
  --bad: #b3261e;
  --bad-soft: #fdecea;
  --good: #1d6b3f;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #16181c;
    --panel: #1e2126;
    --ink: #e7e9ee;
    --muted: #a0a6b4;
    --line: #333842;
    --accent: #7aa2ff;
    --accent-soft: #2a3550;
    --accent-faint: #222835;
    --bad: #ff9a90;
    --bad-soft: #3a2422;
    --good: #8fd7a8;
  }
}

* { box-sizing: border-box; }

body {
  margin: 0;
  padding: 0 16px 48px;
  background: var(--bg);
  color: var(--ink);
  font-family: ui-sans-serif, system-ui, sans-serif;
  line-height: 1.5;
}

header, main { max-width: 1100px; margin: 0 auto; }
h1 { font-size: 1.4rem; }

main {
  display: grid;
  gap: 20px;
  grid-template-columns: minmax(0, 1fr);
}
@media (min-width: 900px) {
  main { grid-template-columns: minmax(0, 420px) minmax(0, 1fr); align-items: start; }
}

form, #output {
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 10px;
  padding: 16px;
}

label { display: block; font-weight: 600; margin-bottom: 8px; }

/* ---------- script panel ----------
   #mirror and the textarea must share identical metrics so the highlight lines
   up with the text the caret sits on. */
.editor-stack { position: relative; }

.editor-stack textarea,
.editor-stack #mirror {
  width: 100%;
  margin: 0;
  font-family: ui-monospace, "SF Mono", Menlo, Consolas, monospace;
  font-size: 0.95rem;
  line-height: 1.5;
  letter-spacing: normal;
  tab-size: 4;
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 10px;
  white-space: pre-wrap;
  overflow-wrap: break-word;
  word-break: normal;
}

.editor-stack textarea {
  position: relative;
  display: block;
  color: inherit;
  background: transparent;
  resize: vertical;
  overflow: auto;
  min-height: 15em;
}

.editor-stack #mirror {
  position: absolute;
  inset: 0;
  overflow: hidden;
  background: var(--bg);
  color: transparent;
  pointer-events: none;
  border-color: transparent;
}

.editor-stack #mirror mark {
  background: var(--accent-soft);
  color: transparent;
  border-radius: 3px;
  box-shadow: 0 0 0 1px var(--accent);
}

.actions { display: flex; flex-wrap: wrap; gap: 8px; margin-top: 12px; }

button {
  font: inherit;
  padding: 8px 14px;
  border-radius: 8px;
  border: 1px solid var(--line);
  background: var(--panel);
  color: inherit;
  cursor: pointer;
}
button:hover { border-color: var(--accent); color: var(--accent); }

.hint, .meta { color: var(--muted); font-size: 0.9rem; }

/* htmx sets this class on the target while a request is running */
#output.htmx-request { opacity: 0.55; }

h2 { font-size: 1rem; margin: 0 0 10px; }

pre, code { font-family: ui-monospace, "SF Mono", Menlo, Consolas, monospace; }
pre {
  margin: 0;
  padding: 10px;
  background: var(--bg);
  border: 1px solid var(--line);
  border-radius: 8px;
  overflow-x: auto;
  white-space: pre-wrap;
}

.status { font-weight: 600; margin: 0 0 10px; }
.status.ok { color: var(--good); }
.status.bad { color: var(--bad); }

.errors { margin: 0 0 12px; padding: 10px 10px 10px 28px; border-radius: 8px; background: var(--bad-soft); color: var(--bad); }
.errors li { font-family: ui-monospace, monospace; font-size: 0.9rem; }


/* ---------- tree canvas ---------- */
.tree-toolbar { display: flex; gap: 8px; margin-bottom: 8px; }
.tree-toolbar button { padding: 4px 10px; font-size: 0.85rem; }

.tree-canvas {
  position: relative;
  height: 520px;
  background: var(--bg);
  border: 1px solid var(--line);
  border-radius: 8px;
  overflow: hidden;
  touch-action: none; /* a drag pans the tree instead of scrolling the page */
  cursor: grab;
}
.tree-canvas.panning { cursor: grabbing; }
.tree-canvas.stale { opacity: 0.5; }
.stale-note { color: var(--bad); }

.tree-svg { width: 100%; height: 100%; display: block; }

.edge { fill: none; stroke: var(--line); stroke-width: 2; }

.node rect {
  fill: var(--panel);
  stroke: var(--line);
  stroke-width: 1;
}
.node text {
  text-anchor: middle;
  fill: var(--ink);
  font-family: ui-sans-serif, system-ui, sans-serif;
  font-size: 12px;
  font-weight: 600;
  pointer-events: none;
}
.node text.text {
  font-family: ui-monospace, "SF Mono", Menlo, Consolas, monospace;
  font-size: 11px;
  font-weight: 400;
  fill: var(--muted);
}

.node.hl rect { fill: var(--accent-soft); stroke: var(--accent); stroke-width: 2; }
.node.hl-sub rect { fill: var(--accent-faint); stroke: var(--accent); }

/* ---------- run panel ---------- */
code.result {
  font-size: 1rem;
  padding: 2px 8px;
  border-radius: 6px;
  background: var(--accent-faint);
  border: 1px solid var(--accent);
  color: var(--ink);
}

.vars { border-collapse: collapse; margin-top: 12px; font-size: 0.9rem; }
.vars caption { text-align: left; color: var(--muted); padding-bottom: 6px; }
.vars th, .vars td { padding: 4px 14px 4px 0; text-align: left; border-top: 1px solid var(--line); }
.vars th { font-weight: 600; }
.vars td { font-family: ui-monospace, "SF Mono", Menlo, Consolas, monospace; color: var(--muted); }
