:root {
  --bg: #ffffff;
  --fg: #0f172a;          /* slate-900 */
  --accent: #2563eb;      /* blue-600 */
  --ok: #16a34a;          /* green-600 */
  --warn: #dc2626;        /* red-600 */
  --muted: #64748b;       /* slate-500 */
  --grid: #e2e8f0;        /* slate-200 */
  --codeBg: #0b1020;      /* deep code bg (dark by default for code) */

  --card: #f8fafc;
  --shadow: 0 4px 14px rgba(0,0,0,.08);

  --cell-size: 56px;
  --cell-gap: 8px;

  --anim-pointer: 200ms cubic-bezier(0.2, 0.8, 0.2, 1);
  --anim-pulse: 400ms cubic-bezier(0.2, 0.8, 0.2, 1);
  --anim-flash: 120ms linear;
  --anim-comment: 150ms ease;
  --step-interval: 700ms; /* base, scaled by speed */

  color-scheme: light;
}

:root.dark {
  --bg: #0b1220;
  --fg: #e5e7eb;
  --accent: #60a5fa;
  --ok: #22c55e;
  --warn: #f87171;
  --muted: #94a3b8;
  --grid: #1f2937;
  --card: #0f172a;
  --codeBg: #0b1020;
  color-scheme: dark;
}

:root.hc {
  --accent: #ffb000;
  --ok: #00ff6a;
  --warn: #ff375f;
  --muted: #d0d0d0;
  --grid: #8a8a8a;
  --shadow: 0 0 0 2px var(--accent);
}

* { box-sizing: border-box; }

html, body {
  height: 100%;
}

body {
  margin: 0;
  font: 15px/1.45 system-ui, -apple-system, Segoe UI, Roboto, Ubuntu, Cantarell, Noto Sans, 'Helvetica Neue', Arial, 'Apple Color Emoji', 'Segoe UI Emoji';
  background: var(--bg);
  color: var(--fg);
}

.app {
  display: grid;
  grid-template-rows: auto auto 1fr auto auto;
  min-height: 100vh;
}

.header {
  display: grid;
  gap: 12px;
  padding: 16px clamp(12px, 2vw, 24px);
  border-bottom: 1px solid var(--grid);
  background: var(--card);
}

.title-wrap .title {
  margin: 0;
  font-weight: 800;
  font-size: clamp(18px, 2.2vw, 28px);
}
.subtitle { margin: 4px 0 0 0; color: var(--muted); }

.input-form {
  display: grid;
  grid-template-columns: 1fr 2fr auto auto;
  gap: 12px;
  align-items: end;
}
@media (max-width: 900px) {
  .input-form { grid-template-columns: 1fr; }
}
.field { display: grid; gap: 6px; }
.field.grow { width: 100%; }
.field input, .field select {
  width: 100%;
  height: 36px;
  padding: 8px 10px;
  border: 1px solid var(--grid);
  border-radius: 10px;
  background: var(--bg);
  color: var(--fg);
  box-shadow: var(--shadow);
}
.toggles { display: flex; align-items: center; gap: 10px; }
.toggle { display: inline-flex; gap: 6px; align-items: center; font-weight: 600; }
.actions { display: flex; gap: 8px; justify-content: flex-end; }
.btn {
  appearance: none;
  border: 1px solid var(--grid);
  border-radius: 10px;
  background: var(--bg);
  color: var(--fg);
  padding: 8px 12px;
  cursor: pointer;
  box-shadow: var(--shadow);
  transition: transform .06s ease, background .15s ease;
}
.btn:hover { transform: translateY(-1px); }
.btn:active { transform: translateY(0); }
.btn.accent { background: var(--accent); border-color: transparent; color: white; }

.error { color: var(--warn); font-weight: 700; }
.error[hidden] { display: none; }

.controls {
  display: grid;
  grid-template-columns: auto 1fr auto;
  gap: 12px;
  align-items: center;
  padding: 10px clamp(12px, 2vw, 24px);
  border-bottom: 1px solid var(--grid);
}
.controls .buttons { display: flex; gap: 8px; }
.speed {
  display: grid; place-items: center;
  gap: 4px;
}
.speed input[type="range"] { width: min(420px, 80vw); }
.speed-labels { display: flex; justify-content: space-between; gap: 8px; width: min(420px, 80vw); font-size: 12px; color: var(--muted); }
.hints { color: var(--muted); text-align: right; }

.main {
  display: grid;
  grid-template-columns: 1fr minmax(260px, 360px);
  grid-template-rows: auto 1fr;
  gap: 12px;
  padding: 12px clamp(12px, 2vw, 24px);
}
@media (max-width: 960px) {
  .main { grid-template-columns: 1fr; grid-template-rows: auto auto auto; }
}

.array-pane {
  min-height: 180px;
  border: 1px dashed var(--grid);
  border-radius: 14px;
  position: relative;
  overflow: auto;
  padding: 18px;
  background:
    linear-gradient(transparent 23px, var(--grid) 24px, transparent 24px) top left/100% 24px no-repeat;
}
.array-inner {
  display: grid;
  grid-auto-flow: column;
  grid-auto-columns: var(--cell-size);
  gap: var(--cell-gap);
  position: relative;
  padding-bottom: 52px; /* space for underline & pointers */
}
.cell {
  position: relative;
  display: grid;
  place-items: center;
  height: var(--cell-size);
  width: var(--cell-size);
  border-radius: 12px;
  background: var(--card);
  color: var(--fg);
  border: 1px solid var(--grid);
  box-shadow: var(--shadow);
  font-weight: 800;
  font-family: ui-monospace, SFMono-Regular, Menlo, Monaco, Consolas, "Liberation Mono", "Courier New", monospace;
}
.cell .idx {
  position: absolute;
  top: -18px;
  font: 11px/1 ui-monospace, Consolas, monospace;
  color: var(--muted);
}
.pointer {
  position: absolute;
  top: -28px;
  width: var(--cell-size);
  height: 20px;
  display: grid;
  place-items: center;
  color: var(--accent);
  font-weight: 900;
  transition: transform var(--anim-pointer);
  will-change: transform;
  pointer-events: none;
}
.pointer.right { color: #a855f7; } /* purple-500 */
.pointer .label {
  position: absolute;
  top: -14px;
  font-size: 11px;
  color: var(--muted);
}

.windowOverlay {
  position: absolute;
  height: calc(var(--cell-size) + 4px);
  background: color-mix(in oklab, var(--accent) 18%, transparent);
  border: 2px solid var(--accent);
  border-radius: 12px;
  top: 0;
  transition: transform var(--anim-pointer), width var(--anim-pointer), opacity .2s ease;
  pointer-events: none;
}
.windowOverlay .tooMany {
  position: absolute;
  top: -18px;
  left: 12px;
  right: 12px;
  height: 6px;
  border-radius: 6px;
  background: var(--warn);
  opacity: 0;
  transition: opacity .2s ease;
}
.windowOverlay.too-many .tooMany { opacity: 1; }
.windowOverlay::after {
  content: attr(aria-label);
  position: absolute;
  bottom: -18px;
  left: 8px;
  right: 8px;
  font-size: 11px;
  color: var(--muted);
}

.zeroBadge {
  position: absolute;
  bottom: -6px;
  left: 50%;
  transform: translateX(-50%) scale(1);
  background: var(--warn);
  color: #fff;
  font-size: 10px;
  font-weight: 900;
  height: 16px;
  min-width: 16px;
  padding: 0 4px;
  border-radius: 10px;
  display: grid; place-items: center;
  box-shadow: 0 2px 6px rgba(0,0,0,.2);
  transition: transform .12s ease;
}
.zeroBadge.pop { transform: translateX(-50%) scale(1.2); }

.bestLayer {
  position: absolute;
  bottom: 8px;
  left: 0;
  right: 0;
  height: 8px;
  pointer-events: none;
}
.bestUnderline {
  position: absolute;
  height: 4px;
  background: var(--ok);
  border-radius: 6px;
  box-shadow: 0 0 0 2px color-mix(in oklab, var(--ok) 35%, transparent);
}
.bestUnderline.pulse { animation: pulse var(--anim-pulse); }
.bestBracket {
  position: absolute;
  bottom: 14px;
  font-size: 12px;
  color: var(--ok);
}

.debugger {
  display: grid;
  gap: 12px;
}

.registers, .code-pane, .comment-pane {
  border: 1px solid var(--grid);
  border-radius: 14px;
  padding: 12px;
  background: var(--card);
  box-shadow: var(--shadow);
}

.registers h2, .code-pane h2, .comment-pane h2 { margin: 0 0 8px 0; font-size: 14px; color: var(--muted); }
.reg-grid {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 6px;
}
.reg { display: flex; justify-content: space-between; gap: 12px; padding: 6px 8px; border-radius: 10px; }
.reg .k { font-weight: 700; color: var(--muted); }
.reg .v { font-weight: 900; }
.reg.flash { animation: flash var(--anim-flash); }

.code-pane .code {
  margin: 0;
  padding: 12px;
  border-radius: 10px;
  background: var(--codeBg);
  color: #dbeafe; /* blue-100 */
  font: 13px/1.4 ui-monospace, SFMono-Regular, Menlo, Monaco, Consolas, "Liberation Mono", "Courier New";
  overflow: auto;
  max-height: 320px;
  counter-reset: line;
}
.code .hl { background: rgba(96, 165, 250, 0.22); display: inline-block; width: 100%; }
.code .ln { color: #64748b; user-select: none; display: inline-block; width: 28px; text-align: right; padding-right: 8px; }
.code .src { display: inline-block; white-space: pre; width: calc(100% - 28px); }

.comment-pane .comment {
  min-height: 40px;
  font-weight: 600;
  transition: opacity var(--anim-comment);
}

.timeline {
  position: relative;
  display: grid;
  gap: 8px;
  padding: 10px clamp(12px, 2vw, 24px) 16px;
  border-top: 1px solid var(--grid);
  background: var(--card);
}
.timeline .bars {
  position: relative;
  display: grid;
  grid-auto-flow: column;
  gap: 2px;
}
.timeline .bar {
  height: 16px;
  border-radius: 4px;
  background: var(--grid);
  cursor: pointer;
}
.bar.init, .bar.final { background: #cbd5e1; }
.bar.expand { background: #93c5fd; }
.bar.shrink { background: #fbbf24; }
.bar.compute { background: #c4b5fd; }
.bar.updateBest { background: #86efac; }

.timeline .handle {
  position: absolute;
  top: -2px;
  height: 22px;
  width: 2px;
  background: var(--accent);
  border-radius: 2px;
  box-shadow: 0 0 0 2px color-mix(in oklab, var(--accent) 40%, transparent);
  pointer-events: none;
  transform: translateX(0);
  transition: transform var(--anim-pointer);
}
.timeline .milestone {
  position: absolute;
  top: -6px;
  height: 28px;
  width: 6px;
  border-radius: 3px;
  background: #ef4444;
  opacity: .9;
  pointer-events: none;
}

.footer {
  padding: 12px clamp(12px, 2vw, 24px) 24px;
  color: var(--muted);
}

.sr-only {
  position: absolute !important;
  width: 1px; height: 1px;
  padding: 0; margin: -1px;
  overflow: hidden; clip: rect(0,0,0,0); white-space: nowrap; border: 0;
}

/* Animations */
@keyframes pulse {
  0% { transform: scaleX(1) }
  35% { transform: scaleX(1.06) }
  100% { transform: scaleX(1) }
}
@keyframes flash {
  0% { background: rgba(96,165,250,.55); }
  100% { background: transparent; }
}

/* Reduced motion */
@media (prefers-reduced-motion: reduce) {
  :root {
    --anim-pointer: 1ms linear;
    --anim-pulse: 1ms linear;
    --anim-flash: 1ms linear;
    --anim-comment: 1ms linear;
  }
}

