:root {
  color-scheme: light;
  --bg: #f6f4f1;
  --surface: #ffffff;
  --surface-2: #f0ede8;
  --border: #ddd7cf;
  --text: #1f1b16;
  --muted: #6b6259;
  --accent: #c2410c;
  --accent-ink: #ffffff;
  --accent-soft: #fbe6da;
  --ok: #15803d;
  --bad: #b91c1c;
  --bad-soft: #fde8e8;
  --rust-bar: #c2410c;
  --js-bar: #8a8178;
  --tk-string: #0f766e;
  --tk-number: #1d4ed8;
  --tk-regex: #a21caf;
  --tk-variable: #9a3412;
  --tk-function: #7c2d12;
  --tk-keyword: #6d28d9;
  --tk-operator: #b45309;
  --sel: rgba(194, 65, 12, 0.22);
  --focus: rgba(194, 65, 12, 0.14);
  --mono: ui-monospace, "SF Mono", "Cascadia Code", "JetBrains Mono", Menlo, Consolas, monospace;
  --sans: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
}
@media (prefers-color-scheme: dark) {
  :root {
    color-scheme: dark;
    --bg: #16130f;
    --surface: #1e1a16;
    --surface-2: #26211b;
    --border: #3a332b;
    --text: #ede7df;
    --muted: #a59a8e;
    --accent: #f97316;
    --accent-ink: #1a0d03;
    --accent-soft: #3b2312;
    --ok: #4ade80;
    --bad: #f87171;
    --bad-soft: #3a1717;
    --rust-bar: #f97316;
    --js-bar: #7d7369;
    --tk-string: #5eead4;
    --tk-number: #93c5fd;
    --tk-regex: #f0abfc;
    --tk-variable: #fdba74;
    --tk-function: #fb923c;
    --tk-keyword: #c4b5fd;
    --tk-operator: #fcd34d;
    --sel: rgba(249, 115, 22, 0.3);
    --focus: rgba(249, 115, 22, 0.18);
  }
}

* { box-sizing: border-box; }
[hidden] { display: none !important; }
html, body { height: 100%; }
body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font: 14px/1.45 var(--sans);
  display: flex;
  flex-direction: column;
}
a { color: var(--accent); }
.visually-hidden { position: absolute; width: 1px; height: 1px; overflow: hidden; clip: rect(0 0 0 0); }

.bar {
  display: flex; align-items: center; justify-content: space-between; gap: 12px; flex-wrap: wrap;
  padding: 10px 16px;
  border-bottom: 1px solid var(--border);
  background: var(--surface);
}
.brand { display: flex; align-items: center; gap: 10px; }
.logo {
  display: inline-grid; place-items: center; width: 30px; height: 30px; border-radius: 8px; flex-shrink: 0;
  background: var(--accent); color: var(--accent-ink); font: 700 14px var(--mono);
}
.name { margin: 0; font-weight: 650; font-size: 16px; letter-spacing: -0.01em; }
.name em { font-style: normal; font-weight: 400; color: var(--muted); }
.tag {
  font: 12px var(--mono); color: var(--muted); padding: 2px 8px;
  border: 1px solid var(--border); border-radius: 999px;
}
.picker { display: inline-flex; align-items: center; gap: 6px; padding: 0 4px 0 2px; }
.picker select { border: 0; background: transparent; font: 12px var(--mono); padding: 2px 4px; color: var(--text); }
.picker .status { color: var(--muted); }
#js-version { font: 12px var(--mono); padding: 2px 6px; }
.star {
  display: inline-flex; align-items: center; gap: 6px; text-decoration: none; font-weight: 600;
  padding: 6px 12px; border-radius: 6px; border: 1px solid var(--accent); color: var(--accent);
}
.star:hover { background: var(--accent-soft); }
.install { display: flex; flex-wrap: wrap; align-items: center; gap: 8px 12px; margin-bottom: 6px; color: var(--text); font-size: 13px; }
.install code { font: 12px var(--mono); background: var(--surface); border: 1px solid var(--border); border-radius: 6px; padding: 3px 8px; }
.actions { display: flex; align-items: center; gap: 8px; flex-wrap: wrap; }
.link { text-decoration: none; padding: 6px 8px; border-radius: 6px; }
.link:hover { background: var(--surface-2); }

button, select {
  font: inherit; color: var(--text); background: var(--surface);
  border: 1px solid var(--border); border-radius: 6px; padding: 6px 12px; cursor: pointer;
}
button:hover, select:hover { background: var(--surface-2); }
button:focus-visible, select:focus-visible, textarea:focus-visible, pre:focus-visible, input:focus-visible {
  outline: 2px solid var(--accent); outline-offset: 1px;
}
button.small { padding: 2px 8px; font-size: 12px; }
button.primary { background: var(--accent); color: var(--accent-ink); border-color: var(--accent); font-weight: 600; }
button.primary:hover { filter: brightness(1.08); }
button:disabled { opacity: 0.6; cursor: progress; }
select { max-width: 260px; }
.check { display: inline-flex; align-items: center; gap: 6px; color: var(--muted); font-size: 13px; cursor: pointer; }

.grid {
  flex: 1; min-height: 0;
  display: grid; gap: 12px; padding: 12px 16px;
  grid-template-columns: minmax(0, 1fr) minmax(0, 1fr);
  grid-template-rows: minmax(140px, 0.8fr) minmax(200px, 1.4fr) auto;
  grid-template-areas:
    "input expr"
    "input result"
    "bench bench";
}
.input-pane { grid-area: input; }
.expr-pane { grid-area: expr; }
.result-pane { grid-area: result; }
.bench-pane { grid-area: bench; }

.pane {
  min-height: 0; display: flex; flex-direction: column;
  background: var(--surface); border: 1px solid var(--border); border-radius: 10px; overflow: hidden;
}
.pane-head {
  display: flex; align-items: center; justify-content: space-between; gap: 8px; flex-wrap: wrap;
  padding: 6px 10px 6px 14px; border-bottom: 1px solid var(--border); background: var(--surface-2);
}
.pane-head h2 { margin: 0; font-size: 12px; font-weight: 650; text-transform: uppercase; letter-spacing: 0.06em; color: var(--muted); }
.pane-tools { display: flex; align-items: center; gap: 10px; flex-wrap: wrap; }
.hint { font-size: 12px; color: var(--muted); }

#input, .result {
  flex: 1; min-height: 0; width: 100%; margin: 0; border: 0; resize: none;
  padding: 12px 14px; background: var(--surface); color: var(--text);
  font: 13px/1.5 var(--mono); tab-size: 2; overflow: auto;
}
/* Expression editor: a transparent textarea over a highlighted copy. */
.editor { position: relative; flex: 1; min-height: 0; }
.editor > .hl, .editor > textarea {
  position: absolute; inset: 0; margin: 0; padding: 12px 14px; border: 0;
  font: 15px/1.5 var(--mono); tab-size: 2; letter-spacing: 0;
  white-space: pre-wrap; word-break: break-word; overflow-wrap: anywhere; overflow: auto;
}
.editor > .hl { background: var(--surface); color: var(--text); pointer-events: none; }
.editor > .hl code { font: inherit; }
.editor > textarea { background: transparent; color: transparent; caret-color: var(--text); resize: none; }
.editor > textarea::selection { background: var(--sel); color: transparent; }
.editor.plain > textarea { color: var(--text); }
.editor.plain > .hl { visibility: hidden; }
.t-string, .t-template { color: var(--tk-string); }
.t-number, .t-literal { color: var(--tk-number); }
.t-regex { color: var(--tk-regex); }
.t-name { color: var(--text); }
.t-variable { color: var(--tk-variable); }
.t-function { color: var(--tk-function); font-weight: 600; }
.t-keyword { color: var(--tk-keyword); font-weight: 600; }
.t-operator { color: var(--tk-operator); }
.t-bracket, .t-punctuation { color: var(--muted); }
.t-comment { color: var(--muted); font-style: italic; }
.hl .err { text-decoration: underline wavy var(--bad); text-decoration-skip-ink: none; text-underline-offset: 3px; background: var(--bad-soft); }
.hl .err-end { border-left: 2px solid var(--bad); margin-left: 1px; }
.hl .focus { background: var(--focus); border-radius: 2px; }

/* Tools pane tabs */
.tabs { display: flex; gap: 2px; }
.tabs button {
  border: 0; background: transparent; border-radius: 6px; padding: 4px 10px;
  font-size: 12px; font-weight: 650; text-transform: uppercase; letter-spacing: 0.06em; color: var(--muted);
}
.tabs button[aria-selected="true"] { background: var(--surface); color: var(--text); box-shadow: inset 0 -2px 0 var(--accent); }

/* Explain tree */
.explain { padding: 8px 10px 12px; overflow: auto; max-height: 34vh; }
.explain.stale { opacity: 0.5; }
.explain.stale .xrow { cursor: default; }
.explain ul { list-style: none; margin: 0; padding-left: 18px; border-left: 1px dashed var(--border); }
.explain > ul { padding-left: 0; border-left: 0; }
.explain li { margin: 1px 0; }
.xrow {
  display: flex; align-items: baseline; gap: 8px; padding: 3px 6px; border-radius: 6px; cursor: pointer; min-width: 0;
}
.xrow:hover, .xrow:focus-visible, .xrow.active { background: var(--focus); outline: none; }
.xrow code {
  font: 12.5px var(--mono); white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
  max-width: 45%; flex-shrink: 0; color: var(--text);
}
.xrow .xtext { color: var(--muted); font-size: 13px; min-width: 0; }
.xrow .xtext code { max-width: none; font-size: 12px; color: var(--text); }
.xrow a { font-size: 12px; white-space: nowrap; }
.kind {
  flex-shrink: 0; font: 600 10px var(--sans); text-transform: uppercase; letter-spacing: 0.05em;
  padding: 1px 6px; border-radius: 4px; background: var(--surface-2); color: var(--muted); min-width: 64px; text-align: center;
}
.kind.k-function { color: var(--tk-function); }
.kind.k-path, .kind.k-field { color: var(--tk-variable); }
.kind.k-filter, .kind.k-sort, .kind.k-group { color: var(--tk-keyword); }
.kind.k-string, .kind.k-regex { color: var(--tk-string); }
.kind.k-number, .kind.k-literal { color: var(--tk-number); }
.result { white-space: pre-wrap; word-break: break-word; }
.result.undefined { color: var(--muted); font-style: italic; font-family: var(--sans); }
.result.stale { opacity: 0.45; }

.timing { font: 12px var(--mono); color: var(--muted); }
.timing b { color: var(--text); font-weight: 600; }

.error {
  margin: 10px 12px 0; padding: 10px 12px; border-radius: 8px;
  background: var(--bad-soft); color: var(--bad); font-size: 13px;
}
.error code { font: 600 12px var(--mono); margin-right: 6px; padding: 1px 6px; border-radius: 4px; border: 1px solid currentColor; }

.bench { padding: 12px 14px; }
.bench p { margin: 0; }
.muted { color: var(--muted); font-size: 13px; }
.rows { display: grid; grid-template-columns: max-content minmax(0, 1fr) max-content; gap: 8px 12px; align-items: center; }
.rows .label { font-weight: 600; font-size: 13px; }
.rows .label small { display: block; font-weight: 400; color: var(--muted); font-size: 11px; }
.track { height: 18px; background: var(--surface-2); border-radius: 4px; overflow: hidden; }
.fill { height: 100%; border-radius: 4px; transition: width 0.4s ease; }
.fill.rust { background: var(--rust-bar); }
.fill.js { background: var(--js-bar); }
.num { font: 13px var(--mono); text-align: right; white-space: nowrap; }
.verdict { margin-top: 10px; display: flex; gap: 16px; flex-wrap: wrap; align-items: baseline; }
.speedup { font-size: 20px; font-weight: 700; color: var(--accent); }
.match-ok { color: var(--ok); font-size: 13px; }
.match-bad { color: var(--bad); font-size: 13px; }

.foot { padding: 8px 16px 12px; color: var(--muted); font-size: 12px; }
.noscript { padding: 16px; max-width: 60ch; }

.toast {
  position: fixed; bottom: 18px; left: 50%; transform: translateX(-50%);
  background: var(--text); color: var(--bg); padding: 8px 14px; border-radius: 8px; font-size: 13px;
  box-shadow: 0 4px 16px rgba(0,0,0,0.2);
}

@media (max-width: 760px) {
  html, body { height: auto; }
  .grid {
    flex: none;
    grid-template-columns: minmax(0, 1fr);
    grid-template-rows: 120px 260px 260px auto;
    grid-template-areas: "expr" "input" "result" "bench";
  }
  .hint { display: none; }
  .rows { grid-template-columns: max-content minmax(0, 1fr); }
  .rows .num { grid-column: 2; text-align: left; }
}
