:root {
  --bg: #f4f6f9; --panel: #fff; --line: #d5dbe3; --ink: #1f2933; --muted: #5b6672;
  --blue: #2b6cb0; --orange: #c05621; --red: #e53e3e; --gray: #b8c2cf;
  --node: #fff; --on-fill: #fff; --btn: #2b6cb0; --btn-text: #fff;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #171b21; --panel: #20262e; --line: #38414d; --ink: #e6eaef; --muted: #9aa5b2;
    --blue: #63b3ed; --orange: #f6ad55; --red: #fc8181; --gray: #4a5563;
    --node: #20262e; --on-fill: #11151a; --btn: #63b3ed; --btn-text: #11151a;
  }
}
* { box-sizing: border-box; }
body { margin: 0; padding: 16px; background: var(--bg); color: var(--ink); font: 15px/1.5 system-ui, sans-serif; }
header, #controls, main, .player { max-width: 1000px; margin: 0 auto 12px; }
header { display: flex; flex-wrap: wrap; align-items: center; justify-content: space-between; gap: 8px 12px; }
h1 { font-size: 20px; margin: 0; }
h2 { font-size: 18px; margin: 0 0 8px; }

button { font: inherit; color: var(--ink); background: var(--panel); border: 1px solid var(--line); border-radius: 6px; padding: 6px 12px; cursor: pointer; }
button.primary { background: var(--btn); color: var(--btn-text); border-color: var(--btn); }
button:disabled { opacity: .45; cursor: default; }
:focus-visible { outline: 2px solid var(--blue); outline-offset: 2px; }

#controls { display: flex; flex-wrap: wrap; align-items: center; gap: 12px; }
#params { display: contents; }
#params label, #params > div { display: inline-flex; align-items: center; gap: 6px; color: var(--muted); }
#params [hidden] { display: none; }
#params input[type=range] { width: 110px; }
#params output { min-width: 2.5ch; color: var(--ink); }
#new { margin-left: auto; }
input[type=range] { accent-color: var(--btn); }

main { display: grid; grid-template-columns: minmax(0, 1fr) 380px; gap: 12px; align-items: start; }
.left { display: grid; gap: 12px; min-width: 0; }
#stage { width: 100%; height: auto; background: var(--panel); border: 1px solid var(--line); border-radius: 8px; }
aside, #data-box { background: var(--panel); border: 1px solid var(--line); border-radius: 8px; padding: 16px; }
[hidden] { display: none !important; }
.seg { display: inline-flex; flex-wrap: wrap; gap: 4px; }
.seg button { padding: 4px 10px; }
.seg button[aria-pressed=true] { background: var(--btn); color: var(--btn-text); border-color: var(--btn); }
#disclaimer { max-width: 1000px; margin: 16px auto 0; color: var(--muted); font-size: 13px; text-align: center; }
h3 { font-size: 13px; margin: 0 0 8px; color: var(--muted); font-weight: 600; text-transform: uppercase; letter-spacing: .04em; }
#code-box { margin-top: 12px; padding-top: 10px; }

/* pseudocode */
#code { margin: 0; padding: 0; list-style: none; counter-reset: l; font: 13px/1.6 ui-monospace, monospace; white-space: pre; overflow-x: auto; }
#code li { counter-increment: l; padding: 0 6px; border-left: 3px solid transparent; border-radius: 3px; }
#code li::before { content: counter(l); display: inline-block; width: 2ch; margin-right: 1.5ch; color: var(--muted); text-align: right; }
#code li.on { background: color-mix(in srgb, var(--blue) 18%, transparent); border-left-color: var(--blue); font-weight: 600; }

/* adjacency arrays */
.adj { display: grid; grid-template-columns: repeat(auto-fit, minmax(230px, 1fr)); gap: 12px 24px; font: 14px/1.7 ui-monospace, monospace; }
.arr h4 { margin: 0 0 6px; font: 700 15px ui-monospace, monospace; }
.arr .row { display: flex; align-items: center; gap: 8px; margin-bottom: 3px; }
.arr .v { min-width: 2.2ch; text-align: center; font-weight: 700; padding: 0 5px; border-radius: 4px; }
.arr .v.blue { background: var(--blue); color: var(--on-fill); }
.arr .v.orange { background: var(--orange); color: var(--on-fill); }
.arr .cells { display: flex; flex-wrap: wrap; }
.arr .cell { min-width: 2.6ch; padding: 0 6px; text-align: center; border: 1px solid var(--line); margin-left: -1px; }
.arr .cell.empty { color: var(--muted); }
.arr .cell.picked { color: var(--red); font-weight: 700; border-color: var(--red); position: relative; z-index: 1; }
.arr .cell.new { background: color-mix(in srgb, var(--blue) 22%, transparent); border-color: var(--blue); font-weight: 700; position: relative; z-index: 1; }
/* pair grids (insertion sort) */
.adj .arr { min-width: 0; }
.tri { display: grid; gap: 1px; font: 12px/19px ui-monospace, monospace; overflow-x: auto; }
.tri span { height: 21px; text-align: center; }
.tri .h { color: var(--muted); font-weight: 700; line-height: 21px; }
.tri .c { border: 1px solid var(--line); border-radius: 3px; color: var(--muted); }
.tri .c.void { border: 0; }
.tri .c.one { color: var(--ink); font-weight: 700; }
.tri .c.picked { color: var(--red); border-color: var(--red); }
.tri .c.new { background: color-mix(in srgb, var(--blue) 22%, transparent); border-color: var(--blue); }
#text p { margin: 0 0 8px; }

.player { position: sticky; bottom: 0; z-index: 2; display: flex; align-items: center; gap: 8px; padding: 8px 0; background: var(--bg); border-top: 1px solid var(--line); }
#next { min-width: 5.5em; }
#seek { flex: 1; min-width: 120px; }
#count { color: var(--muted); min-width: 6ch; text-align: right; }

@media (max-width: 760px) {
  main { grid-template-columns: minmax(0, 1fr); }
  .left { display: contents; }
  #stage { order: 1; } aside { order: 2; } #data-box { order: 3; }
  header .seg { width: 100%; }
  .player { padding-bottom: max(8px, env(safe-area-inset-bottom)); }
}

/* shared SVG styling for visualizers */
svg text { fill: var(--ink); font-family: system-ui, sans-serif; }
