:root {
  --bg: #f5f5f2;
  --panel: #ffffff;
  --panel-2: #fafaf8;
  --ink: #1d1e20;
  --muted: #6b6e75;
  --line: #e3e3de;
  --line-strong: #cfcfc8;
  --accent: #3451d1;
  --accent-ink: #ffffff;
  --current: #fff3c4;
  --current-edge: #e2b400;
  --prev: #f1f1ec;
  --read: #2563eb;
  --read-bg: #e2ebff;
  --write: #dd5a0b;
  --write-bg: #ffe7d4;
  --error: #c62828;
  --error-bg: #fdecec;
  --heat-pos: 22 140 110;
  --heat-neg: 190 50 120;
  --done-bg: #fdf1e6;
  --bracket: #8c8f96;
  --t-kw: #8b2fc9;
  --t-str: #1f7a3a;
  --t-num: #b45309;
  --t-com: #8a8d93;
  --t-fn: #1d5fbf;
  --radius: 10px;
  --mono: ui-monospace, "SF Mono", SFMono-Regular, Menlo, Consolas, monospace;
  --sans: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  color-scheme: light;
}

@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #121316;
    --panel: #1a1b1f;
    --panel-2: #16171a;
    --ink: #e7e7ea;
    --muted: #9a9ca4;
    --line: #2b2d33;
    --line-strong: #3a3d44;
    --accent: #7b93ff;
    --accent-ink: #0f1020;
    --current: rgba(255, 210, 80, 0.16);
    --current-edge: #e2b400;
    --prev: rgba(255, 255, 255, 0.04);
    --read: #6ea0ff;
    --read-bg: rgba(110, 160, 255, 0.18);
    --write: #ff8a3d;
    --write-bg: rgba(255, 138, 61, 0.2);
    --error: #ff6b6b;
    --error-bg: rgba(255, 107, 107, 0.12);
    --heat-pos: 60 200 160;
    --heat-neg: 240 100 170;
    --done-bg: rgba(255, 138, 61, 0.08);
    --bracket: #6f737b;
    --t-kw: #c792ea;
    --t-str: #8fd18f;
    --t-num: #f2b36b;
    --t-com: #6f737b;
    --t-fn: #82aaff;
    color-scheme: dark;
  }
}

:root[data-theme="dark"] {
  --bg: #121316;
  --panel: #1a1b1f;
  --panel-2: #16171a;
  --ink: #e7e7ea;
  --muted: #9a9ca4;
  --line: #2b2d33;
  --line-strong: #3a3d44;
  --accent: #7b93ff;
  --accent-ink: #0f1020;
  --current: rgba(255, 210, 80, 0.16);
  --current-edge: #e2b400;
  --prev: rgba(255, 255, 255, 0.04);
  --read: #6ea0ff;
  --read-bg: rgba(110, 160, 255, 0.18);
  --write: #ff8a3d;
  --write-bg: rgba(255, 138, 61, 0.2);
  --error: #ff6b6b;
  --error-bg: rgba(255, 107, 107, 0.12);
  --heat-pos: 60 200 160;
  --heat-neg: 240 100 170;
  --done-bg: rgba(255, 138, 61, 0.08);
  --bracket: #6f737b;
  --t-kw: #c792ea;
  --t-str: #8fd18f;
  --t-num: #f2b36b;
  --t-com: #6f737b;
  --t-fn: #82aaff;
  color-scheme: dark;
}

* { box-sizing: border-box; }

html, body { height: 100%; }

body {
  margin: 0;
  background: var(--bg);
  color: var(--ink);
  font: 14px/1.45 var(--sans);
  display: flex;
  flex-direction: column;
}

button, select, input, textarea { font: inherit; color: inherit; }

button, select {
  background: var(--panel);
  border: 1px solid var(--line-strong);
  border-radius: 7px;
  padding: 5px 10px;
  cursor: pointer;
  white-space: nowrap;
}
button:hover:not(:disabled), select:hover { border-color: var(--muted); }
button:disabled { opacity: 0.45; cursor: default; }
button.primary {
  background: var(--accent);
  border-color: var(--accent);
  color: var(--accent-ink);
  font-weight: 600;
  padding: 5px 14px;
}
button:focus-visible, select:focus-visible, input:focus-visible, textarea:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 1px;
}

code { font-family: var(--mono); font-size: 0.92em; }
.muted { color: var(--muted); }
.spacer { flex: 1; }

/* ---------- top bar */

.topbar {
  display: flex;
  align-items: center;
  gap: 16px;
  padding: 10px 18px;
  border-bottom: 1px solid var(--line);
  background: var(--panel);
}
.brand { display: flex; align-items: center; gap: 12px; }
.brand h1 { margin: 0; font-size: 16px; letter-spacing: -0.01em; }
.brand p { margin: 0; color: var(--muted); font-size: 12.5px; }
.logo {
  width: 26px; height: 26px;
  border: 2px solid var(--accent);
  border-top-color: transparent;
  border-bottom-color: transparent;
  border-radius: 4px;
  background:
    linear-gradient(var(--accent), var(--accent)) 50% 30% / 10px 3px no-repeat,
    linear-gradient(var(--write), var(--write)) 50% 70% / 10px 3px no-repeat;
}
.status {
  margin-left: auto;
  display: inline-flex;
  align-items: center;
  gap: 8px;
  font-size: 12.5px;
  color: var(--muted);
  padding: 4px 10px;
  border: 1px solid var(--line);
  border-radius: 999px;
  max-width: 55%;
}
.status .dot { width: 8px; height: 8px; border-radius: 50%; background: var(--muted); flex: none; }
.status.loading .dot, .status.busy .dot { background: var(--current-edge); animation: pulse 1s infinite alternate; }
.status.ready .dot { background: #2e9d5b; }
.status.error { color: var(--error); border-color: var(--error); }
.status.error .dot { background: var(--error); }
@keyframes pulse { from { opacity: 0.35; } to { opacity: 1; } }

/* ---------- layout */

.layout {
  flex: 1;
  min-height: 0;
  display: grid;
  grid-template-columns: minmax(340px, 40%) 1fr;
  gap: 14px;
  padding: 14px;
}
.pane {
  min-height: 0;
  min-width: 0;
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  display: flex;
  flex-direction: column;
  overflow: hidden;
}
.pane-head {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 10px 12px;
  border-bottom: 1px solid var(--line);
}
.pane-head select { max-width: 60%; }

/* ---------- editor & code view */

#editor, .code-view {
  flex: 1;
  min-height: 0;
  font: 13px/1.6 var(--mono);
  tab-size: 4;
}
#editor {
  width: 100%;
  resize: none;
  border: 0;
  padding: 12px 14px;
  background: var(--panel);
  white-space: pre;
  overflow: auto;
}
#editor:focus { outline: none; }
.code-view { overflow: auto; padding: 8px 0; }
.ln {
  display: grid;
  grid-template-columns: 3.2em 2.6em 1fr;
  align-items: baseline;
  padding-right: 12px;
  border-left: 3px solid transparent;
  cursor: pointer;
  white-space: pre;
}
.ln:hover { background: var(--prev); }
.ln .no { color: var(--muted); text-align: right; padding-right: 10px; user-select: none; }
.ln .hits { color: var(--muted); font-size: 10.5px; user-select: none; }
.ln .src { min-width: 0; }
.ln.prev { background: var(--prev); }
.ln.current { background: var(--current); border-left-color: var(--current-edge); }
.ln.current .no { color: var(--ink); font-weight: 700; }
.ln.err { background: var(--error-bg); border-left-color: var(--error); }
.t-kw { color: var(--t-kw); }
.t-str { color: var(--t-str); }
.t-num { color: var(--t-num); }
.t-com { color: var(--t-com); font-style: italic; }
.t-fn { color: var(--t-fn); }

.stdin {
  border-top: 1px solid var(--line);
  padding: 8px 12px 10px;
  display: flex;
  flex-direction: column;
  gap: 4px;
  background: var(--panel-2);
}
.stdin label { font-size: 12px; color: var(--muted); }
.stdin textarea {
  resize: vertical;
  border: 1px solid var(--line-strong);
  border-radius: 6px;
  padding: 5px 8px;
  font: 13px/1.4 var(--mono);
  background: var(--panel);
}

/* ---------- controls */

.controls {
  display: flex;
  align-items: center;
  flex-wrap: wrap;
  gap: 10px;
  padding: 10px 12px;
  border-bottom: 1px solid var(--line);
}
.buttons { display: flex; gap: 4px; }
.buttons button { min-width: 36px; padding: 5px 8px; }
.buttons .play { min-width: 44px; }
#step-slider { flex: 1; min-width: 120px; accent-color: var(--accent); }
.step-label { font-variant-numeric: tabular-nums; color: var(--muted); font-size: 12.5px; min-width: 7.5em; text-align: right; }
.speed { display: inline-flex; align-items: center; gap: 6px; color: var(--muted); font-size: 12.5px; }

.explain {
  padding: 12px 14px;
  border-bottom: 1px solid var(--line);
  font: 15px/1.7 var(--mono);
  background: var(--panel-2);
  min-height: 3.2em;
  overflow-wrap: anywhere;
}
.explain .where {
  font: 600 11px/1 var(--sans);
  text-transform: uppercase;
  letter-spacing: 0.06em;
  color: var(--muted);
  margin-right: 10px;
}
.explain .arrow { color: var(--muted); font-weight: 700; margin: 0 2px; }
.explain.err { color: var(--error); }
.tok { border-radius: 4px; padding: 1px 5px; }
.tok.r { background: var(--read-bg); color: var(--read); box-shadow: inset 0 0 0 1.5px var(--read); }
.tok.w { background: var(--write-bg); color: var(--write); box-shadow: inset 0 0 0 1.5px var(--write); font-weight: 700; }
.tok.n { padding: 0 1px; border-radius: 0; border-bottom: 1.5px dotted var(--muted); }
.tok[data-ref] { cursor: help; }

.legend {
  display: flex;
  align-items: center;
  flex-wrap: wrap;
  gap: 6px 16px;
  padding: 8px 14px;
  border-bottom: 1px solid var(--line);
  font-size: 12.5px;
  color: var(--muted);
}
.legend .key { display: inline-flex; align-items: center; gap: 6px; }
.sw { width: 14px; height: 14px; border-radius: 3px; display: inline-block; }
.sw.read { background: var(--read-bg); box-shadow: inset 0 0 0 2px var(--read); }
.sw.write { background: var(--write-bg); box-shadow: inset 0 0 0 2px var(--write); }
.sw.done { background: var(--done-bg); box-shadow: inset 0 0 0 1px var(--line-strong); }
.ptr-demo { font: 700 12px/1 var(--mono); color: var(--read); }
.opt { display: inline-flex; align-items: center; gap: 6px; }
.opt select, .opt input[type="number"] { padding: 2px 6px; }
.opt input[type="number"] {
  width: 6.5em;
  border: 1px solid var(--line-strong);
  border-radius: 6px;
  background: var(--panel);
}

.banner {
  margin: 10px 14px 0;
  padding: 8px 12px;
  border-radius: 8px;
  font-size: 13px;
  border: 1px solid var(--line-strong);
  background: var(--panel-2);
}
.banner.error { border-color: var(--error); background: var(--error-bg); color: var(--error); }
.banner p { margin: 0; }
.banner p + p { margin-top: 4px; }

/* ---------- frames & variables */

.frames {
  flex: 1;
  min-height: 0;
  overflow: auto;
  padding: 12px 14px 16px;
  display: flex;
  flex-direction: column;
  gap: 12px;
}
.empty {
  margin: auto;
  max-width: 440px;
  text-align: center;
  color: var(--muted);
}
.empty-title { color: var(--ink); font-weight: 600; font-size: 15px; margin-bottom: 4px; }

.frame {
  border: 1px solid var(--line);
  border-radius: var(--radius);
  padding: 10px 12px 12px;
}
.frame.active { border-color: var(--accent); box-shadow: 0 0 0 1px var(--accent) inset; }
.frame-title {
  font: 600 11.5px/1 var(--sans);
  text-transform: uppercase;
  letter-spacing: 0.06em;
  color: var(--muted);
  margin-bottom: 10px;
}
.frame.active .frame-title { color: var(--accent); }
.frame-empty { color: var(--muted); font-size: 12.5px; }

.chips { display: flex; flex-wrap: wrap; gap: 6px; margin-bottom: 10px; }
.chips:last-child { margin-bottom: 0; }
.chip {
  font: 13px/1.3 var(--mono);
  border: 1px solid var(--line);
  border-radius: 6px;
  padding: 3px 8px;
  background: var(--panel-2);
  max-width: 100%;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.chip .k { font-weight: 700; }
.chip .eq { color: var(--muted); margin: 0 4px; }
.chip .ty { color: var(--muted); font-size: 11px; margin-left: 6px; }
.chip.r { background: var(--read-bg); border-color: var(--read); }
.chip.w { background: var(--write-bg); border-color: var(--write); }

.mats { display: flex; flex-wrap: wrap; gap: 18px 28px; align-items: flex-start; }
.mat { min-width: 0; max-width: 100%; }
.mat-head { display: flex; align-items: baseline; gap: 8px; margin-bottom: 2px; padding-left: 2px; }
.mat-name { font: 700 17px/1 var(--mono); }
.mat-name.r { color: var(--read); }
.mat-name.w { color: var(--write); }
.mat-meta { font-size: 11.5px; color: var(--muted); }
.mat-scroll { overflow: auto; max-width: 100%; padding: 4px 10px 8px 2px; }

/* A matrix: row 1 / column 1 hold the pointers (i →, k ↓), row 2 / column 2 the
   indices; every item is placed explicitly by app.js. */
.grid {
  display: inline-grid;
  grid-template-rows: 1.7em auto;
  grid-auto-rows: auto;
  gap: 3px;
  font-family: var(--mono);
  font-variant-numeric: tabular-nums;
  line-height: 1;
}
.grid.size-l { font-size: 15px; }
.grid.size-m { font-size: 13.5px; }
.grid.size-s { font-size: 11.5px; }
.grid .ix {
  color: var(--muted);
  font-size: 11px;
  display: flex;
  align-items: center;
  justify-content: center;
  border-radius: 4px;
}
.grid .ix.col { padding: 2px 4px 7px; }
.grid .ix.row { padding: 0 10px 0 4px; }
.grid .ix.r { color: var(--read); font-weight: 700; }
.grid .ix.w { color: var(--write); font-weight: 700; }
.ptr {
  display: flex;
  align-items: center;
  font: 700 12.5px/1 var(--mono);
  white-space: nowrap;
}
.ptr.col { flex-direction: column; justify-content: flex-end; gap: 1px; }
.ptr.row { justify-content: flex-end; gap: 3px; padding-right: 2px; }
.ptr .r { color: var(--read); }
.ptr .w { color: var(--write); }
.ptr .sep { color: var(--muted); }
.ptr .arr { color: var(--muted); font-size: 11px; }

.cell {
  min-width: 4.2em;
  padding: 0.7em 0.65em;
  text-align: right;
  border-radius: 6px;
  background: var(--panel-2);
  border: 1px solid var(--line);
  white-space: nowrap;
  transition: background-color 150ms ease, box-shadow 150ms ease;
}
.grid.size-s .cell { min-width: 3.4em; padding: 0.5em 0.45em; border-radius: 4px; }
.cell.done { background: var(--done-bg); }
.cell.r { background: var(--read-bg); box-shadow: inset 0 0 0 2px var(--read); border-color: transparent; }
.cell.w {
  background: var(--write-bg);
  box-shadow: inset 0 0 0 2px var(--write);
  border-color: transparent;
  font-weight: 700;
  animation: pop 420ms cubic-bezier(0.2, 0.8, 0.2, 1);
}
.cell.r.w { box-shadow: inset 0 0 0 2px var(--write), 0 0 0 2px var(--read); }
.cell.focus, .chip.focus { outline: 3px solid var(--accent); outline-offset: 2px; position: relative; z-index: 1; }
@keyframes pop {
  from { transform: scale(1.16); }
  to { transform: scale(1); }
}
@media (prefers-reduced-motion: reduce) {
  .cell.w { animation: none; }
}

/* The [ ] around the cells */
.bracket { position: relative; margin: -4px -6px; pointer-events: none; }
.bracket::before, .bracket::after {
  content: "";
  position: absolute;
  top: 0;
  bottom: 0;
  width: 7px;
  border: 2px solid var(--bracket);
}
.bracket::before { left: 0; border-right: 0; border-radius: 3px 0 0 3px; }
.bracket::after { right: 0; border-left: 0; border-radius: 0 3px 3px 0; }
.big { font: 12px/1.4 var(--mono); margin: 0; padding: 8px; background: var(--panel-2); border-radius: 6px; overflow: auto; }

/* ---------- console */

.console { border-top: 1px solid var(--line); max-height: 28%; display: flex; flex-direction: column; min-height: 72px; }
.console-head {
  font: 600 11px/1 var(--sans);
  text-transform: uppercase;
  letter-spacing: 0.06em;
  color: var(--muted);
  padding: 8px 14px 4px;
}
#stdout {
  margin: 0;
  padding: 2px 14px 10px;
  overflow: auto;
  font: 12.5px/1.45 var(--mono);
  white-space: pre-wrap;
  flex: 1;
}
#stdout mark { background: var(--write-bg); color: inherit; border-radius: 3px; }

/* ---------- small screens */

@media (max-width: 900px) {
  body { height: auto; }
  .layout { grid-template-columns: 1fr; padding: 10px 16px; }
  .code-pane { height: 55vh; }
  .viz-pane { min-height: 80vh; }
  .topbar { padding: 10px 16px; flex-wrap: wrap; }
  .status { max-width: 100%; margin-left: 0; }
  .step-label { min-width: 0; }
  .pane-head { flex-wrap: wrap; }
  .pane-head select { max-width: none; min-width: 0; width: 100%; flex: 1 1 100%; }
}
