:root {
  --bg: #f7f7f5;
  --panel: #ffffff;
  --text: #1d1d1f;
  --muted: #62626a;
  --border: #e2e2de;
  --accent: #2f5fd0;
  --accent-text: #ffffff;
  --code-bg: #f0f0ec;
  --error: #b42318;
  /* Plan bars: categorical slots 1 and 2, validated as a pair in both modes. */
  --series-act: #2a78d6;
  --series-est: #eb6834;
  --critical: #d03b3b;
  --grid: #ecebe7;
  --row-hover: #f3f3ef;
  --column: #cfcec8;
  --switch-off: #c9c8c2;
  --mono: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
  --sans: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  color-scheme: light;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #141416;
    --panel: #1c1c1f;
    --text: #ececef;
    --muted: #9d9da6;
    --border: #2e2e33;
    --accent: #7aa2ff;
    --accent-text: #0d1020;
    --code-bg: #26262a;
    --error: #ff8a80;
    --series-act: #3987e5;
    --series-est: #d95926;
    --critical: #e66767;
    --grid: #2a2a2e;
    --row-hover: #232327;
    --column: #45454b;
    --switch-off: #4a4a50;
    color-scheme: dark;
  }
}

* { box-sizing: border-box; }

body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font: 15px/1.5 var(--sans);
}

code, pre, textarea { font-family: var(--mono); font-size: 13px; }

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

.masthead {
  max-width: 1200px;
  margin: 0 auto;
  padding: 32px 16px 8px;
}

.masthead h1 { margin: 0 0 8px; font-size: 26px; letter-spacing: -0.01em; }
.masthead .plus { color: var(--muted); font-weight: 400; }
.lede { max-width: 70ch; margin: 0 0 8px; color: var(--muted); }
.links { display: flex; gap: 16px; margin: 0; }

.layout {
  max-width: 1200px;
  margin: 0 auto;
  padding: 16px;
  display: grid;
  gap: 16px;
  grid-template-columns: minmax(0, 1fr) minmax(0, 1fr);
  grid-template-areas:
    "query results"
    "controls controls"
    "plan plan"
    "tables tables";
  align-items: start;
}

.query { grid-area: query; }
.plan { grid-area: plan; }
.results { grid-area: results; }
.tables { grid-area: tables; }
.controls { grid-area: controls; }

@media (max-width: 860px) {
  .layout {
    grid-template-columns: minmax(0, 1fr);
    grid-template-areas: "query" "controls" "plan" "results" "tables";
  }
}

.panel {
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 16px;
  min-width: 0;
}

.panel h2 {
  margin: 0 0 12px;
  font-size: 13px;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.06em;
  color: var(--muted);
}

.panel-head {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  align-items: baseline;
  justify-content: space-between;
}

.presets { display: flex; flex-wrap: wrap; gap: 6px; margin-bottom: 12px; }

.presets button {
  font: 12px var(--sans);
  padding: 4px 10px;
  border: 1px solid var(--border);
  border-radius: 999px;
  background: transparent;
  color: var(--text);
  cursor: pointer;
}

.presets button[aria-pressed="true"] { border-color: var(--accent); color: var(--accent); }

textarea {
  width: 100%;
  resize: vertical;
  padding: 10px;
  border: 1px solid var(--border);
  border-radius: 6px;
  background: var(--code-bg);
  color: var(--text);
  line-height: 1.5;
}

.actions { display: flex; align-items: center; gap: 12px; margin-top: 10px; }

#run {
  font: 600 14px var(--sans);
  padding: 7px 18px;
  border: 0;
  border-radius: 6px;
  background: var(--accent);
  color: var(--accent-text);
  cursor: pointer;
}

#run:disabled { opacity: 0.5; cursor: default; }
.hint { color: var(--muted); font-size: 12px; }
.status { margin-left: auto; color: var(--muted); font-size: 13px; text-align: right; }
.status.error { color: var(--error); }

.plan-text {
  margin: 0;
  padding: 12px;
  overflow-x: auto;
  background: var(--code-bg);
  border-radius: 6px;
  line-height: 1.6;
}

.results-body { overflow-x: auto; }

table { border-collapse: collapse; width: 100%; font-size: 13px; }
th, td { padding: 6px 10px; border-bottom: 1px solid var(--border); text-align: left; white-space: nowrap; }
th { font-weight: 600; color: var(--muted); }
th.num, td.num { text-align: right; font-variant-numeric: tabular-nums; }
.note { color: var(--muted); font-size: 12px; margin: 8px 0 0; }
.error-text { color: var(--error); font-family: var(--mono); font-size: 13px; white-space: pre-wrap; margin: 0; }

.table-list { margin: 0; }
.table-list dt { font-weight: 600; margin-top: 8px; }
.table-list dt:first-child { margin-top: 0; }
.table-list dd { margin: 2px 0 0; color: var(--muted); font-size: 13px; }

.tag {
  font: 600 11px var(--sans);
  padding: 1px 6px;
  border-radius: 4px;
  background: var(--code-bg);
  color: var(--muted);
}

.refreshed { animation: refreshed 0.6s ease-out; }

@keyframes refreshed {
  from { box-shadow: 0 0 0 2px var(--accent); }
  to { box-shadow: 0 0 0 2px transparent; }
}

@media (prefers-reduced-motion: reduce) {
  .refreshed { animation: none; }
}

/* ------------------------------------------------- time travel and planner */

.controls-grid {
  display: grid;
  grid-template-columns: minmax(0, 1.5fr) minmax(0, 1fr);
  gap: 16px 32px;
  align-items: start;
}

@media (max-width: 860px) {
  .controls-grid { grid-template-columns: minmax(0, 1fr); }
}

.timeline-head { display: flex; flex-wrap: wrap; align-items: center; gap: 8px 12px; margin-bottom: 10px; }
.version-label { font-weight: 600; font-variant-numeric: tabular-nums; margin-right: auto; }

button.secondary {
  font: 12px var(--sans);
  padding: 4px 12px;
  border: 1px solid var(--border);
  border-radius: 6px;
  background: transparent;
  color: var(--text);
  cursor: pointer;
}

button.secondary:disabled { opacity: 0.5; cursor: default; }

.timeline-chart {
  position: relative;
  height: 72px;
  border-bottom: 1px solid var(--border);
}

.column {
  position: absolute;
  top: 0;
  bottom: 0;
  width: 28px;
  transform: translateX(-50%);
  display: flex;
  flex-direction: column;
  justify-content: flex-end;
  align-items: center;
  padding: 0;
  border: 0;
  background: none;
  cursor: pointer;
}

.column-bar {
  width: 18px; /* narrowed by timeline.js as versions are added */
  max-height: calc(100% - 16px);
  background: var(--column);
  border-radius: 4px 4px 0 0; /* rounded data end, square at the baseline */
}

.column-value { font-size: 11px; line-height: 14px; color: var(--muted); font-variant-numeric: tabular-nums; }
.column:hover .column-bar { background: var(--muted); }
.column.selected .column-bar { background: var(--accent); }
.column.selected .column-value { color: var(--text); font-weight: 600; }

/* A plain track and a 16px round thumb, so timeline.js knows where it sits. */
#version {
  -webkit-appearance: none;
  appearance: none;
  display: block;
  width: 100%;
  height: 20px;
  margin: 4px 0 0;
  background: transparent;
}

#version::-webkit-slider-runnable-track { height: 4px; border-radius: 2px; background: var(--border); }
#version::-moz-range-track { height: 4px; border-radius: 2px; background: var(--border); }

#version::-webkit-slider-thumb {
  -webkit-appearance: none;
  width: 16px;
  height: 16px;
  margin-top: -6px;
  border: 2px solid var(--panel);
  border-radius: 50%;
  background: var(--accent);
  cursor: pointer;
}

#version::-moz-range-thumb {
  width: 12px; /* plus the 2px border on each side: 16px */
  height: 12px;
  border: 2px solid var(--panel);
  border-radius: 50%;
  background: var(--accent);
  cursor: pointer;
}

#version:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; border-radius: 4px; }

.timeline-labels { position: relative; height: 34px; }

.timeline-label {
  position: absolute;
  top: 2px;
  transform: translateX(-50%);
  display: flex;
  flex-direction: column;
  align-items: center;
  font-size: 12px;
  line-height: 15px;
  color: var(--muted);
  font-variant-numeric: tabular-nums;
  white-space: nowrap;
}

.timeline-label small { font-size: 11px; }
.timeline-label.selected { color: var(--text); font-weight: 600; }

.switches { border: 0; margin: 0; padding: 0; display: grid; gap: 14px; }
.switch { display: flex; gap: 12px; align-items: flex-start; cursor: pointer; }
.switch-text { display: grid; gap: 2px; }
.switch-text small { color: var(--muted); font-size: 12.5px; }

.switch input {
  -webkit-appearance: none;
  appearance: none;
  flex: none;
  position: relative;
  width: 34px;
  height: 20px;
  margin: 1px 0 0;
  border-radius: 10px;
  background: var(--switch-off);
  cursor: pointer;
  transition: background 0.15s;
}

.switch input::before {
  content: "";
  position: absolute;
  top: 2px;
  left: 2px;
  width: 16px;
  height: 16px;
  border-radius: 50%;
  background: #ffffff;
  transition: transform 0.15s;
}

.switch input:checked { background: var(--accent); }
.switch input:checked::before { transform: translateX(14px); }
.switch input:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }
.switch input:disabled { opacity: 0.5; cursor: default; }

@media (prefers-reduced-motion: reduce) {
  .switch input, .switch input::before { transition: none; }
}

/* ---------------------------------------------------------------- plan tree */

.legend {
  display: flex;
  flex-wrap: wrap;
  gap: 4px 14px;
  margin-right: auto;
  margin-left: 16px;
  font-size: 12px;
  color: var(--muted);
}

.swatch {
  display: inline-block;
  width: 12px;
  height: 8px;
  margin-right: 6px;
  border-radius: 0 4px 4px 0;
  vertical-align: 1px;
}

.swatch.est, .bar-line.est .bar { background: var(--series-est); }
.swatch.act, .bar-line.act .bar { background: var(--series-act); }

.segmented { display: inline-flex; border: 1px solid var(--border); border-radius: 6px; margin-bottom: 12px; }

.segmented button {
  font: 12px var(--sans);
  padding: 3px 12px;
  border: 0;
  background: transparent;
  color: var(--muted);
  cursor: pointer;
}

.segmented button + button { border-left: 1px solid var(--border); }
.segmented button[aria-pressed="true"] { background: var(--code-bg); color: var(--text); font-weight: 600; }

.plan-view { position: relative; border-radius: 6px; container: plan / inline-size; }
.plan-scroll { overflow-x: auto; }

.plan-table { min-width: 720px; border-collapse: collapse; }
.plan-table th { font-size: 12px; vertical-align: bottom; }
.plan-table td { padding: 5px 10px; vertical-align: middle; }
.plan-table tbody tr:hover, .plan-table tbody tr:focus { background: var(--row-hover); outline: none; }
.plan-table tbody tr:focus-visible { box-shadow: inset 2px 0 0 var(--accent); }

.op { font-family: var(--mono); font-size: 12.5px; }
.guide { color: var(--muted); white-space: pre; }
.op-name { font-weight: 600; }
.op-detail { color: var(--muted); }

.rows-head, .bars { width: 38%; min-width: 240px; }

/* Leave room right of the plot for the value printed at a bar's tip. */
.plot { position: relative; margin-right: 52px; }
.axis { height: 16px; }
.tick { position: absolute; bottom: 0; transform: translateX(-50%); font-weight: 400; font-variant-numeric: tabular-nums; }
.grid { position: absolute; top: 0; bottom: 0; width: 1px; background: var(--grid); }

.bar-line { position: relative; height: 10px; }
.bar-line + .bar-line { margin-top: 2px; }
.bar {
  position: absolute;
  left: 0;
  top: 1px;
  height: 8px;
  border-radius: 0 4px 4px 0; /* rounded data end, square at the baseline */
}

.tip {
  position: absolute;
  top: -2px;
  padding-left: 5px;
  font-size: 11px;
  line-height: 14px;
  color: var(--muted);
  font-variant-numeric: tabular-nums;
  white-space: nowrap;
}

.bar-line.act .tip { color: var(--text); }

.error { font-size: 12.5px; }
.error-exact, .error-minor { color: var(--muted); }
.flag { font-weight: 600; }
.flag-icon { color: var(--critical); }

.tooltip {
  position: absolute;
  z-index: 2;
  min-width: 220px;
  max-width: 340px;
  padding: 10px 12px;
  border: 1px solid var(--border);
  border-radius: 8px;
  background: var(--panel);
  box-shadow: 0 6px 20px rgb(0 0 0 / 0.15);
  pointer-events: none;
  font-size: 12px;
}

.tt-title { font-family: var(--mono); font-weight: 600; margin-bottom: 6px; overflow-wrap: anywhere; }
.tooltip dl { display: grid; grid-template-columns: auto auto; gap: 2px 16px; margin: 0; }
.tooltip dt { color: var(--muted); }
.tooltip dd { margin: 0; text-align: right; font-variant-numeric: tabular-nums; }

/* Narrow: one operator per block, bars full width beneath its name, so the
   bars stay on screen instead of behind a horizontal scroll. */
@container plan (max-width: 640px) {
  .plan-table, .plan-table thead, .plan-table tbody { display: block; min-width: 0; }
  .plan-table thead tr { display: block; }
  .plan-table thead th:not(.rows-head) { display: none; }
  .plan-table thead .rows-head { display: block; padding: 0 10px 4px; border: 0; }
  .plan-table tbody tr {
    display: grid;
    grid-template-columns: minmax(0, 1fr) auto;
    grid-template-areas: "op op" "bars bars" "error time";
    padding: 6px 0;
    border-bottom: 1px solid var(--border);
  }
  .plan-table td { border: 0; padding: 2px 10px; }
  .plan-table .op { grid-area: op; white-space: normal; }
  .plan-table .bars { grid-area: bars; width: auto; min-width: 0; }
  .plan-table .error { grid-area: error; }
  .plan-table td.num { grid-area: time; }
  .rows-head, .bars { width: auto; min-width: 0; }
}

.visually-hidden {
  position: absolute;
  width: 1px;
  height: 1px;
  overflow: hidden;
  clip: rect(0 0 0 0);
  white-space: nowrap;
}
