:root {
  --bg: #fafafa;
  --panel: #f5f5f5;
  --line: #e8e8e8;
  --ink: #171717;
  --muted: #737373;
  --accent: #2563eb;
  --accent-hover: #1d4ed8;
  --accent-soft: rgba(37, 99, 235, 0.12);
  --ok: #15803d;
  --ok-soft: rgba(21, 128, 61, 0.12);
  --warn: #b45309;
  --danger: #b91c1c;
  --radius: 8px;
  --shadow-sm: 0 1px 2px rgba(0, 0, 0, 0.06);
  --shadow-md: 0 4px 12px rgba(0, 0, 0, 0.07);
  --max: 1100px;
  --mono: ui-monospace, "Cascadia Code", "SF Mono", Consolas, monospace;
  color-scheme: light;
}

[data-theme="dark"] {
  --bg: #0b0b0b;
  --panel: #141414;
  --line: #262626;
  --ink: #fafafa;
  --muted: #d4d4d4;
  --accent: #60a5fa;
  --accent-hover: #93c5fd;
  --accent-soft: rgba(96, 165, 250, 0.18);
  --ok: #4ade80;
  --ok-soft: rgba(74, 222, 128, 0.16);
  --warn: #fbbf24;
  --danger: #f87171;
  --shadow-sm: 0 1px 2px rgba(0, 0, 0, 0.35);
  --shadow-md: 0 4px 12px rgba(0, 0, 0, 0.4);
  color-scheme: dark;
}

*,
*::before,
*::after {
  box-sizing: border-box;
  margin: 0;
  padding: 0;
}

html {
  scroll-behavior: smooth;
}

@media (prefers-reduced-motion: reduce) {
  html {
    scroll-behavior: auto;
  }
  .bar,
  .cell,
  .node-card,
  .viz-node {
    transition: none !important;
  }
}

body {
  font-family: -apple-system, BlinkMacSystemFont, "SF Pro Display", "Inter", "Segoe UI", Roboto, sans-serif;
  background: var(--bg);
  color: var(--ink);
  min-height: 100vh;
  line-height: 1.5;
  overflow-x: hidden;
}

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

.skip-link {
  position: absolute;
  left: -9999px;
  top: 0;
  z-index: 10000;
  padding: 10px 14px;
  background: var(--ink);
  color: var(--bg);
  font-weight: 600;
  font-size: 13px;
  border-radius: 6px;
  text-decoration: none;
}

.skip-link:focus {
  left: max(12px, env(safe-area-inset-left, 0px));
  top: max(12px, env(safe-area-inset-top, 0px));
  outline: 2px solid var(--accent);
  outline-offset: 2px;
}

.lab-header {
  border-bottom: 1px solid var(--line);
  background: color-mix(in srgb, var(--bg) 92%, transparent);
  backdrop-filter: blur(8px);
  position: sticky;
  top: 0;
  z-index: 20;
}

.lab-header-inner {
  max-width: var(--max);
  margin: 0 auto;
  padding: 0.85rem max(1rem, env(safe-area-inset-left, 0px)) 0.85rem max(1rem, env(safe-area-inset-right, 0px));
  display: flex;
  align-items: center;
  gap: 0.75rem;
}

.back-link {
  color: var(--muted);
  text-decoration: none;
  font-size: 0.9rem;
  flex-shrink: 0;
}

.back-link:hover {
  color: var(--ink);
}

.lab-brand {
  font-weight: 700;
  font-size: 0.95rem;
  color: var(--ink);
  margin-right: auto;
}

.lab-header-actions {
  display: flex;
  gap: 0.25rem;
  position: relative;
  z-index: 3;
}

.header-icon-btn {
  width: 40px;
  min-width: 40px;
  height: 40px;
  padding: 0;
  text-decoration: none;
}

.lab-main {
  max-width: var(--max);
  margin: 0 auto;
  padding: 1.5rem max(1rem, env(safe-area-inset-left, 0px)) 3rem max(1rem, env(safe-area-inset-right, 0px));
}

.lab-footer {
  border-top: 1px solid var(--line);
  padding: 1.25rem 1rem calc(1.25rem + env(safe-area-inset-bottom, 0px));
  text-align: center;
  color: var(--muted);
  font-size: 0.85rem;
}

.lab-footer a {
  color: var(--muted);
}

.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 0.4rem;
  min-height: 44px;
  min-width: 44px;
  padding: 0.55rem 0.9rem;
  border-radius: var(--radius);
  border: 1px solid var(--line);
  background: var(--bg);
  color: var(--ink);
  font: inherit;
  font-size: 0.9rem;
  font-weight: 500;
  cursor: pointer;
  touch-action: manipulation;
}

.btn:hover {
  border-color: var(--accent);
}

.btn:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 2px;
}

.btn:disabled {
  opacity: 0.45;
  cursor: not-allowed;
}

.btn-primary {
  background: var(--accent);
  border-color: var(--accent);
  color: #fff;
  box-shadow: var(--shadow-sm);
}

.btn-primary:hover {
  background: var(--accent-hover);
  border-color: var(--accent-hover);
}

.btn-ghost {
  background: transparent;
}

.landing-hero {
  margin-bottom: 2rem;
}

.landing-eyebrow {
  color: var(--muted);
  font-size: 0.85rem;
  font-weight: 600;
  letter-spacing: 0.02em;
  margin-bottom: 0.35rem;
}

.landing-hero h1 {
  font-size: clamp(1.75rem, 4vw, 2.25rem);
  line-height: 1.2;
  margin-bottom: 0.5rem;
}

.landing-lead {
  color: var(--muted);
  max-width: 42rem;
  margin-bottom: 0.75rem;
}

.landing-support {
  color: var(--muted);
  max-width: 42rem;
  font-size: 0.95rem;
  margin-bottom: 1.25rem;
}

.recommend-card {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  justify-content: space-between;
  gap: 0.75rem;
  padding: 1rem 1.1rem;
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  margin-bottom: 2rem;
}

.recommend-card p {
  color: var(--muted);
  font-size: 0.95rem;
}

.path-switcher {
  display: flex;
  gap: 0.35rem;
  overflow-x: auto;
  -webkit-overflow-scrolling: touch;
  padding-bottom: 0.5rem;
  margin-bottom: 0.75rem;
  scrollbar-width: thin;
}

.path-tab {
  flex: 0 0 auto;
  min-height: 40px;
  padding: 0.45rem 0.85rem;
  border-radius: 999px;
  border: 1px solid var(--line);
  background: var(--bg);
  color: var(--ink);
  font: inherit;
  font-size: 0.85rem;
  font-weight: 600;
  cursor: pointer;
  white-space: nowrap;
}

.path-tab.active,
.path-tab[aria-pressed="true"] {
  background: var(--accent-soft);
  border-color: var(--accent);
  color: var(--accent);
}

.paths {
  display: grid;
  gap: 1rem;
  margin-bottom: 2rem;
}

.path-card {
  background: var(--bg);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  padding: 1.1rem 1.15rem;
  box-shadow: var(--shadow-sm);
}

.path-card--active {
  margin-bottom: 2rem;
}

.path-card h2 {
  font-size: 1.1rem;
  margin-bottom: 0.25rem;
}

.path-meta {
  color: var(--muted);
  font-size: 0.85rem;
  margin-bottom: 0.5rem;
}

.path-goal {
  font-size: 0.92rem;
  margin-bottom: 0.85rem;
}

.topic-list {
  list-style: none;
  display: grid;
  gap: 0.35rem;
  margin-bottom: 0.9rem;
}

.topic-list a {
  text-decoration: none;
  color: var(--ink);
  border-bottom: 1px solid transparent;
}

.topic-list a:hover {
  color: var(--accent);
  border-bottom-color: var(--accent-soft);
}

.topic-later {
  color: var(--muted);
  display: flex;
  justify-content: space-between;
  gap: 0.75rem;
  align-items: baseline;
}

.later-note {
  font-size: 0.78rem;
  color: var(--muted);
  white-space: nowrap;
}

.badge {
  display: inline-flex;
  align-items: center;
  padding: 0.1rem 0.45rem;
  border-radius: 999px;
  font-size: 0.72rem;
  font-weight: 600;
  background: var(--panel);
  color: var(--muted);
  border: 1px solid var(--line);
  white-space: nowrap;
}

.badge-ready {
  background: var(--ok-soft);
  color: var(--ok);
  border-color: transparent;
}

.breadcrumb {
  display: flex;
  flex-wrap: wrap;
  gap: 0.35rem;
  align-items: center;
  color: var(--muted);
  font-size: 0.85rem;
  margin-bottom: 1rem;
}

.breadcrumb a {
  color: var(--muted);
  text-decoration: none;
}

.breadcrumb a:hover {
  color: var(--accent);
}

.workspace-header h1 {
  font-size: clamp(1.4rem, 3vw, 1.85rem);
  margin-bottom: 0.35rem;
}

.workspace-desc {
  color: var(--muted);
  margin-bottom: 1rem;
}

.topic-nav {
  display: flex;
  flex-wrap: nowrap;
  gap: 0.45rem;
  margin-bottom: 1.25rem;
  overflow-x: auto;
  -webkit-overflow-scrolling: touch;
  padding-bottom: 0.25rem;
  scrollbar-width: thin;
}

.topic-chip {
  flex: 0 0 auto;
  min-height: 36px;
  padding: 0.35rem 0.7rem;
  border-radius: 999px;
  border: 1px solid var(--line);
  background: var(--bg);
  color: var(--ink);
  font: inherit;
  font-size: 0.82rem;
  cursor: pointer;
  text-decoration: none;
  white-space: nowrap;
}

.topic-chip.active,
.topic-chip[aria-current="page"] {
  background: var(--accent-soft);
  border-color: var(--accent);
  color: var(--accent);
}

.workspace-grid {
  display: grid;
  grid-template-columns: minmax(0, 2fr) minmax(0, 1fr);
  gap: 1rem;
  align-items: start;
}

@media (max-width: 800px) {
  .workspace-grid {
    grid-template-columns: 1fr;
  }

  .lab-main {
    padding-bottom: calc(5.5rem + env(safe-area-inset-bottom, 0px));
  }

  .lab-footer {
    padding-bottom: calc(5rem + env(safe-area-inset-bottom, 0px));
  }

  .desktop-only {
    display: none !important;
  }

  .mobile-only {
    display: flex;
  }

  .controls {
    position: fixed;
    left: 0;
    right: 0;
    bottom: 0;
    z-index: 30;
    margin: 0;
    padding: 0.55rem 0.75rem calc(0.55rem + env(safe-area-inset-bottom, 0px));
    background: color-mix(in srgb, var(--bg) 94%, transparent);
    backdrop-filter: blur(10px);
    border-top: 1px solid var(--line);
    justify-content: stretch;
  }

  .controls .btn {
    flex: 1;
  }

  .panel-inline {
    order: 2;
  }

  .viz-stage {
    display: flex;
    flex-direction: column;
  }

  .viz-canvas {
    order: 1;
  }

  .step-meta {
    order: 3;
  }
}

.panel {
  background: var(--bg);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  box-shadow: var(--shadow-sm);
  overflow: hidden;
}

.panel-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 0.75rem;
  padding: 0.75rem 1rem;
  background: var(--panel);
  border-bottom: 1px solid var(--line);
}

.panel-title {
  font-size: 0.78rem;
  font-weight: 700;
  letter-spacing: 0.04em;
  text-transform: uppercase;
  color: var(--muted);
}

.panel-body {
  padding: 1rem;
}

.viz-stage {
  min-height: 220px;
  display: flex;
  flex-direction: column;
  gap: 0.75rem;
}

.viz-canvas {
  min-height: 180px;
  padding: 0.5rem;
  border: 1px dashed var(--line);
  border-radius: var(--radius);
  background: var(--panel);
  overflow: auto;
}

.bars {
  display: flex;
  align-items: flex-end;
  justify-content: center;
  gap: 0.45rem;
  min-height: 180px;
  padding: 0.75rem 0.5rem 0.35rem;
}

.bar {
  flex: 1 1 0;
  max-width: 48px;
  min-width: 18px;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: flex-end;
  gap: 0.25rem;
  transition: background 0.15s, transform 0.15s;
}

.bar-fill {
  width: 100%;
  border-radius: 6px 6px 2px 2px;
  background: color-mix(in srgb, var(--accent) 35%, var(--line));
  border: 1px solid var(--line);
  min-height: 8px;
}

.bar.compare .bar-fill {
  background: var(--accent);
  border-color: var(--accent);
}

.bar.sorted .bar-fill {
  background: color-mix(in srgb, var(--ok) 45%, var(--line));
  border-color: color-mix(in srgb, var(--ok) 50%, var(--line));
}

.bar.pivot .bar-fill {
  outline: 2px solid var(--warn);
  outline-offset: 1px;
}

.bar-label {
  font-family: var(--mono);
  font-size: 0.75rem;
  color: var(--ink);
}

.bar-index {
  font-family: var(--mono);
  font-size: 0.68rem;
  color: var(--muted);
}

.cells {
  display: flex;
  flex-wrap: wrap;
  gap: 0.45rem;
  justify-content: center;
  padding: 0.5rem;
}

.cell {
  width: 56px;
  border: 1px solid var(--line);
  border-radius: 6px;
  background: var(--bg);
  text-align: center;
  overflow: hidden;
  transition: border-color 0.15s, background 0.15s;
}

.cell-index {
  display: block;
  font-family: var(--mono);
  font-size: 0.68rem;
  color: var(--muted);
  background: var(--panel);
  border-bottom: 1px solid var(--line);
  padding: 0.15rem;
}

.cell-value {
  display: block;
  font-family: var(--mono);
  font-size: 0.95rem;
  font-weight: 600;
  padding: 0.55rem 0.25rem;
}

.cell.active,
.cell.compare {
  border-color: var(--accent);
  background: var(--accent-soft);
}

.cell.sorted {
  border-color: color-mix(in srgb, var(--ok) 50%, var(--line));
  background: var(--ok-soft);
}

.cell.gap {
  opacity: 0.45;
  border-style: dashed;
}

.nodes-row {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 0.35rem;
  justify-content: flex-start;
  padding: 0.5rem;
  overflow-x: auto;
}

.node-card {
  min-width: 64px;
  border: 1px solid var(--line);
  border-radius: var(--radius);
  background: var(--bg);
  padding: 0.45rem 0.55rem;
  text-align: center;
}

.node-card.active {
  border-color: var(--accent);
  background: var(--accent-soft);
}

.node-card.head {
  box-shadow: inset 0 0 0 1px var(--accent);
}

.node-value {
  font-family: var(--mono);
  font-weight: 700;
}

.node-meta {
  font-size: 0.68rem;
  color: var(--muted);
}

.node-arrow {
  color: var(--muted);
  font-family: var(--mono);
  font-size: 0.9rem;
  flex: 0 0 auto;
}

.graph-wrap {
  width: 100%;
  overflow: auto;
}

.graph-svg {
  width: 100%;
  min-height: 220px;
  display: block;
}

.viz-edge {
  stroke: var(--muted);
  stroke-width: 2;
  fill: none;
}

.viz-edge.active {
  stroke: var(--accent);
  stroke-width: 3;
}

.viz-edge.visited {
  stroke: color-mix(in srgb, var(--ok) 70%, var(--muted));
}

.viz-node-circle {
  fill: var(--bg);
  stroke: var(--line);
  stroke-width: 2;
}

.viz-node.active .viz-node-circle {
  stroke: var(--accent);
  fill: var(--accent-soft);
}

.viz-node.visited .viz-node-circle {
  stroke: var(--ok);
  fill: var(--ok-soft);
}

.viz-node-label {
  fill: var(--ink);
  font-family: var(--mono);
  font-size: 14px;
  font-weight: 700;
  text-anchor: middle;
  dominant-baseline: central;
}

.frontier-strip,
.status-strip {
  display: flex;
  flex-wrap: wrap;
  gap: 0.4rem;
  align-items: center;
  font-size: 0.85rem;
  color: var(--muted);
}

.chip {
  display: inline-flex;
  align-items: center;
  padding: 0.15rem 0.5rem;
  border-radius: 999px;
  border: 1px solid var(--line);
  background: var(--bg);
  color: var(--ink);
  font-family: var(--mono);
  font-size: 0.78rem;
}

.controls {
  display: flex;
  flex-wrap: wrap;
  gap: 0.45rem;
  align-items: center;
  margin-top: 0.85rem;
}

.speed-label {
  display: inline-flex;
  align-items: center;
  gap: 0.4rem;
  margin-left: auto;
  font-size: 0.85rem;
  color: var(--muted);
  font-weight: 600;
}

.speed-label select {
  min-height: 40px;
  padding: 0.35rem 0.55rem;
  border-radius: var(--radius);
  border: 1px solid var(--line);
  background: var(--bg);
  color: var(--ink);
  font: inherit;
}

.panel-inline {
  margin-top: 0.85rem;
  padding-top: 0.85rem;
  border-top: 1px solid var(--line);
}

.copy-exp-btn {
  margin-top: 0.35rem;
  align-self: start;
}

.input-details {
  margin-top: 0.85rem;
}

.mobile-only {
  display: none;
}

.desktop-only {
  display: inline-flex;
}

.step-meta {
  font-size: 0.8rem;
  color: var(--muted);
  margin-top: 0.55rem;
}

.explanation {
  display: grid;
  gap: 0.65rem;
}

.explanation h3 {
  font-size: 0.78rem;
  text-transform: uppercase;
  letter-spacing: 0.04em;
  color: var(--muted);
  margin-bottom: 0.2rem;
}

.explanation p {
  font-size: 0.95rem;
}

.input-row {
  display: grid;
  gap: 0.55rem;
  margin-top: 0.85rem;
}

.input-row label {
  font-size: 0.85rem;
  color: var(--muted);
  font-weight: 600;
}

.input-row input,
.input-row textarea,
.input-row select {
  width: 100%;
  min-height: 44px;
  padding: 0.55rem 0.7rem;
  border-radius: var(--radius);
  border: 1px solid var(--line);
  background: var(--bg);
  color: var(--ink);
  font: inherit;
}

.input-row textarea {
  min-height: 96px;
  resize: vertical;
  font-family: var(--mono);
  font-size: 0.85rem;
}

.input-actions {
  display: flex;
  flex-wrap: wrap;
  gap: 0.45rem;
}

.input-error {
  color: var(--danger);
  font-size: 0.85rem;
}

.side-stack {
  display: grid;
  gap: 0.75rem;
}

.details {
  border: 1px solid var(--line);
  border-radius: var(--radius);
  background: var(--bg);
  overflow: hidden;
}

.details > summary {
  list-style: none;
  cursor: pointer;
  padding: 0.75rem 1rem;
  background: var(--panel);
  font-size: 0.78rem;
  font-weight: 700;
  letter-spacing: 0.04em;
  text-transform: uppercase;
  color: var(--muted);
}

.details > summary::-webkit-details-marker {
  display: none;
}

.details[open] > summary {
  border-bottom: 1px solid var(--line);
}

.details-body {
  padding: 0.85rem 1rem 1rem;
}

.pseudocode {
  max-height: 280px;
  overflow: auto;
  font-family: var(--mono);
  font-size: 0.8rem;
  line-height: 1.55;
}

.pseudo-line {
  display: grid;
  grid-template-columns: 1.5rem 1fr;
  gap: 0.4rem;
  padding: 0.15rem 0.35rem;
  border-radius: 4px;
  white-space: pre-wrap;
}

.pseudo-line.active {
  background: var(--accent-soft);
}

.pseudo-marker {
  color: transparent;
  font-weight: 700;
}

.pseudo-line.active .pseudo-marker {
  color: var(--accent);
}

.code-toolbar {
  display: flex;
  flex-wrap: wrap;
  gap: 0.4rem;
  margin-bottom: 0.65rem;
}

.code-block {
  font-family: var(--mono);
  font-size: 0.78rem;
  line-height: 1.5;
  white-space: pre-wrap;
  overflow: auto;
  max-height: 320px;
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 6px;
  padding: 0.75rem;
}

.complexity-grid {
  display: grid;
  grid-template-columns: auto 1fr;
  gap: 0.35rem 0.75rem;
  font-size: 0.9rem;
}

.complexity-grid dt {
  color: var(--muted);
}

.use-list {
  padding-left: 1.1rem;
  font-size: 0.9rem;
}

.use-list li + li {
  margin-top: 0.25rem;
}

.check-banner {
  margin-bottom: 1rem;
  padding: 0.85rem 1rem;
  border-radius: var(--radius);
  border: 1px solid var(--line);
  background: var(--panel);
  font-family: var(--mono);
  font-size: 0.82rem;
  white-space: pre-wrap;
}

.check-banner.ok {
  border-color: color-mix(in srgb, var(--ok) 50%, var(--line));
  background: var(--ok-soft);
}

.check-banner.fail {
  border-color: color-mix(in srgb, var(--danger) 50%, var(--line));
  background: color-mix(in srgb, var(--danger) 12%, var(--bg));
}

.sr-only {
  position: absolute;
  width: 1px;
  height: 1px;
  padding: 0;
  margin: -1px;
  overflow: hidden;
  clip: rect(0, 0, 0, 0);
  white-space: nowrap;
  border: 0;
}
