/* ============================================================
   Sort Lab — Modern lab aesthetic
   ============================================================ */

:root {
  --bg: #eef2f7;
  --bg-accent: #dce6f3;
  --surface: #ffffff;
  --surface-elevated: #f8fafc;
  --text: #0f172a;
  --text-muted: #64748b;
  --border: #cbd5e1;
  --primary: #2563eb;
  --primary-hover: #1d4ed8;
  --accent: #7c3aed;
  --accent-hover: #6d28d9;
  --danger: #dc2626;
  --danger-hover: #b91c1c;
  --secondary: #e2e8f0;
  --secondary-hover: #cbd5e1;
  --bar-default: #3b82f6;
  --bar-compare: #f59e0b;
  --bar-swap: #ef4444;
  --bar-write: #8b5cf6;
  --bar-pivot: #ec4899;
  --bar-digit: #06b6d4;
  --bar-gap: #f97316;
  --bar-bucket: #14b8a6;
  --bar-sorted: #10b981;
  --heatmap-low: #dbeafe;
  --heatmap-high: #dc2626;
  --shadow: 0 8px 24px rgba(15, 23, 42, 0.08);
  --radius: 14px;
  --radius-sm: 8px;
  --sidebar-width: 320px;
  --font-mono: "SF Mono", "Fira Code", "Cascadia Code", monospace;
  --path-empty: #f1f5f9;
  --path-wall: #1e293b;
  --path-start: #34d399;
  --path-end: #f87171;
  --path-weight: #fbbf24;
  --path-visited: #93c5fd;
  --path-frontier: #c4b5fd;
  --path-path: #f59e0b;
}

[data-theme="dark"] {
  --bg: #0b1220;
  --bg-accent: #111b2e;
  --surface: #131c2e;
  --surface-elevated: #1a2540;
  --text: #e8eef8;
  --text-muted: #8ba3c7;
  --border: #2a3a5c;
  --primary: #60a5fa;
  --primary-hover: #3b82f6;
  --accent: #a78bfa;
  --accent-hover: #8b5cf6;
  --danger: #f87171;
  --danger-hover: #ef4444;
  --secondary: #243352;
  --secondary-hover: #31466b;
  --bar-default: #60a5fa;
  --bar-compare: #fbbf24;
  --bar-swap: #f87171;
  --bar-write: #c4b5fd;
  --bar-pivot: #f472b6;
  --bar-digit: #22d3ee;
  --bar-gap: #fb923c;
  --bar-bucket: #2dd4bf;
  --bar-sorted: #34d399;
  --heatmap-low: #1e3a5f;
  --heatmap-high: #f87171;
  --shadow: 0 10px 30px rgba(0, 0, 0, 0.35);
  --path-empty: #1a2540;
  --path-wall: #020617;
  --path-start: #34d399;
  --path-end: #f87171;
  --path-weight: #f59e0b;
  --path-visited: #1d4ed8;
  --path-frontier: #6d28d9;
  --path-path: #fbbf24;
}

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

body {
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
  background:
    radial-gradient(ellipse at top left, var(--bg-accent) 0%, transparent 55%),
    var(--bg);
  color: var(--text);
  min-height: 100vh;
  transition: background 0.3s ease, color 0.3s ease;
}

.app-shell {
  min-height: 100vh;
  display: flex;
  flex-direction: column;
}

/* ---------- Top bar ---------- */
.topbar {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 1rem;
  padding: 1rem 1.5rem;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  box-shadow: var(--shadow);
  position: sticky;
  top: 0;
  z-index: 20;
  flex-wrap: wrap;
}

.brand {
  display: flex;
  align-items: center;
  gap: 0.85rem;
}

.brand-mark {
  display: grid;
  place-items: center;
  width: 42px;
  height: 42px;
  border-radius: 12px;
  background: linear-gradient(135deg, var(--primary), var(--accent));
  color: #fff;
  font-size: 1.25rem;
}

.brand h1 {
  font-size: 1.35rem;
  font-weight: 800;
  letter-spacing: -0.03em;
  line-height: 1.1;
}

.brand-sub {
  font-size: 0.8rem;
  color: var(--text-muted);
}

.topbar-start {
  display: flex;
  align-items: center;
  gap: 1.15rem;
  min-width: 0;
  flex-wrap: wrap;
}

.lab-switcher {
  display: inline-flex;
  padding: 0.18rem;
  gap: 0.15rem;
  background: var(--secondary);
  border-radius: 999px;
}

.lab-pill {
  display: inline-flex;
  align-items: center;
  padding: 0.38rem 0.85rem;
  border-radius: 999px;
  color: var(--text-muted);
  text-decoration: none;
  font-size: 0.8rem;
  font-weight: 700;
  white-space: nowrap;
}

.lab-pill:hover {
  color: var(--text);
}

.lab-pill.is-active {
  background: var(--surface);
  color: var(--text);
  box-shadow: 0 1px 4px rgba(15, 23, 42, 0.12);
}

.topbar-actions {
  display: flex;
  align-items: center;
  gap: 0.75rem;
}

.toggle-pill {
  display: inline-flex;
  align-items: center;
  gap: 0.45rem;
  padding: 0.45rem 0.75rem;
  border-radius: 999px;
  background: var(--secondary);
  font-size: 0.82rem;
  font-weight: 600;
  cursor: pointer;
  user-select: none;
}

.toggle-pill input {
  accent-color: var(--primary);
}

.icon-btn {
  width: 40px;
  height: 40px;
  border: none;
  border-radius: 50%;
  background: var(--secondary);
  font-size: 1.1rem;
  cursor: pointer;
  transition: transform 0.15s ease, background 0.2s ease;
}

.icon-btn:hover {
  background: var(--secondary-hover);
  transform: scale(1.05);
}

/* ---------- Layout ---------- */
.workspace {
  display: grid;
  grid-template-columns: var(--sidebar-width) 1fr;
  gap: 1.25rem;
  padding: 1.25rem;
  flex: 1;
  align-items: start;
}

.sidebar {
  display: flex;
  flex-direction: column;
  gap: 1rem;
  position: sticky;
  top: 5.5rem;
}

.main-stage {
  display: flex;
  flex-direction: column;
  gap: 1rem;
  min-width: 0;
}

/* ---------- Panels ---------- */
.panel {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 1rem;
}

.panel-title {
  font-size: 0.78rem;
  text-transform: uppercase;
  letter-spacing: 0.08em;
  color: var(--text-muted);
  margin-bottom: 0.85rem;
}

.panel-header-row {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 0.5rem;
  margin-bottom: 0.75rem;
}

.panel-header-row .panel-title {
  margin-bottom: 0;
}

.panel-actions {
  display: flex;
  gap: 0.35rem;
}

.field {
  display: flex;
  flex-direction: column;
  gap: 0.4rem;
  margin-bottom: 0.85rem;
}

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

.field select,
.field input[type="range"],
.field input[type="number"],
.field textarea {
  width: 100%;
}

.field input[type="number"],
.field input[type="text"] {
  padding: 0.55rem 0.7rem;
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  background: var(--surface-elevated);
  color: var(--text);
  font-size: 0.92rem;
  font-family: var(--font-mono);
}

.field textarea {
  padding: 0.55rem 0.7rem;
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  background: var(--surface-elevated);
  color: var(--text);
  font-size: 0.86rem;
  font-family: var(--font-mono);
  resize: vertical;
  min-height: 4.5rem;
}

.field-error {
  font-size: 0.75rem;
  color: var(--danger);
  margin-top: 0.25rem;
}

.panel-sub {
  font-size: 0.78rem;
  color: var(--text-muted);
  margin: -0.5rem 0 0.75rem;
}

.field select {
  padding: 0.55rem 0.7rem;
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  background: var(--surface-elevated);
  color: var(--text);
  font-size: 0.92rem;
}

.field input[type="range"] {
  accent-color: var(--primary);
}

.checkbox-row {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  font-size: 0.88rem;
  font-weight: 600;
  color: var(--text);
  text-transform: none;
  letter-spacing: 0;
  cursor: pointer;
}

.checkbox-row input {
  accent-color: var(--primary);
}

.race-field select {
  margin-top: 0.35rem;
}

.btn-grid {
  display: grid;
  grid-template-columns: repeat(2, 1fr);
  gap: 0.5rem;
}

.shortcut-hint {
  margin-top: 0.75rem;
  font-size: 0.75rem;
  color: var(--text-muted);
  text-align: center;
}

/* ---------- Buttons ---------- */
.btn {
  padding: 0.58rem 0.85rem;
  border: none;
  border-radius: var(--radius-sm);
  font-size: 0.86rem;
  font-weight: 700;
  cursor: pointer;
  transition: background 0.2s ease, transform 0.1s ease, opacity 0.2s ease;
}

.btn:active:not(:disabled) {
  transform: scale(0.97);
}

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

.btn-primary {
  background: var(--primary);
  color: #fff;
}

.btn-primary:hover:not(:disabled) {
  background: var(--primary-hover);
}

.btn-secondary {
  background: var(--secondary);
  color: var(--text);
}

.btn-secondary:hover:not(:disabled) {
  background: var(--secondary-hover);
}

.btn-accent {
  background: var(--accent);
  color: #fff;
}

.btn-accent:hover:not(:disabled) {
  background: var(--accent-hover);
}

.btn-danger {
  background: var(--danger);
  color: #fff;
}

.btn-danger:hover:not(:disabled) {
  background: var(--danger-hover);
}

.btn-ghost {
  background: transparent;
  color: var(--primary);
  border: 1px solid var(--border);
}

.btn-ghost:hover:not(:disabled) {
  background: var(--surface-elevated);
}

.btn-sm {
  padding: 0.35rem 0.55rem;
  font-size: 0.72rem;
}

.btn-full {
  width: 100%;
  margin-top: 0.5rem;
}

/* ---------- Profile ---------- */
.profile-name {
  font-size: 1.05rem;
  margin-bottom: 0.35rem;
}

.profile-desc {
  font-size: 0.86rem;
  color: var(--text-muted);
  line-height: 1.45;
  margin-bottom: 0.75rem;
}

.profile-grid {
  display: grid;
  grid-template-columns: repeat(2, 1fr);
  gap: 0.55rem;
}

.profile-grid div {
  background: var(--surface-elevated);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  padding: 0.45rem 0.55rem;
}

.profile-grid dt {
  font-size: 0.68rem;
  text-transform: uppercase;
  letter-spacing: 0.05em;
  color: var(--text-muted);
}

.profile-grid dd {
  font-family: var(--font-mono);
  font-size: 0.82rem;
  font-weight: 700;
  margin-top: 0.15rem;
}

/* ---------- History ---------- */
.history-list {
  list-style: none;
  display: flex;
  flex-direction: column;
  gap: 0.55rem;
  max-height: 240px;
  overflow-y: auto;
}

.history-item {
  display: flex;
  flex-direction: column;
  gap: 0.15rem;
  padding: 0.55rem 0.65rem;
  border-radius: var(--radius-sm);
  background: var(--surface-elevated);
  border: 1px solid var(--border);
  font-size: 0.78rem;
}

.history-meta {
  color: var(--text-muted);
}

.history-primary,
.history-secondary {
  font-family: var(--font-mono);
  font-size: 0.74rem;
}

.history-empty {
  color: var(--text-muted);
  font-size: 0.85rem;
  font-style: italic;
}

/* ---------- Main viz ---------- */
.status-bar {
  padding: 0.55rem 0.85rem;
  border-radius: var(--radius-sm);
  background: var(--surface);
  border: 1px solid var(--border);
  font-size: 0.85rem;
  color: var(--text-muted);
}

.viz-grid {
  display: grid;
  grid-template-columns: 1fr;
  gap: 1rem;
}

body.race-active .viz-grid {
  grid-template-columns: 1fr 1fr;
}

.viz-pane {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  overflow: hidden;
}

.viz-pane.hidden {
  display: none;
}

.pane-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 0.75rem;
  padding: 0.65rem 1rem;
  border-bottom: 1px solid var(--border);
  background: var(--surface-elevated);
}

.pane-header h3 {
  font-size: 0.82rem;
  text-transform: uppercase;
  letter-spacing: 0.06em;
  color: var(--text-muted);
}

.metrics-strip {
  display: flex;
  flex-wrap: wrap;
  gap: 0.45rem 0.75rem;
  font-size: 0.72rem;
  color: var(--text-muted);
}

.metric strong {
  font-family: var(--font-mono);
  color: var(--text);
}

.visualizer {
  display: flex;
  align-items: flex-end;
  justify-content: center;
  gap: 2px;
  height: 420px;
  padding: 1.25rem;
  overflow: hidden;
}

body.race-active .visualizer {
  height: 340px;
}

.bar {
  flex: 1;
  background: var(--bar-default);
  border-radius: 3px 3px 0 0;
  transition: height 0.08s ease, background 0.08s ease;
  min-width: 2px;
  font-size: 10px;
  color: #fff;
  display: flex;
  align-items: flex-end;
  justify-content: center;
  overflow: hidden;
}

.bar.identity { background: var(--bar-identity); }
.bar.compare { background: var(--bar-compare); }
.bar.swap    { background: var(--bar-swap); }
.bar.write   { background: var(--bar-write); }
.bar.pivot   { background: var(--bar-pivot); }
.bar.digit   { background: var(--bar-digit); }
.bar.gap     { background: var(--bar-gap); }
.bar.bucket  { background: var(--bar-bucket); }
.bar.sorted  { background: var(--bar-sorted); }
.bar.identity.sorted { background: var(--bar-identity); }

/* ---------- Legend ---------- */
.legend {
  display: flex;
  justify-content: center;
  flex-wrap: wrap;
  gap: 1rem 1.5rem;
  padding: 0.85rem 1rem;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
}

.legend-item {
  display: flex;
  align-items: center;
  gap: 0.45rem;
  font-size: 0.82rem;
  color: var(--text-muted);
}

.swatch {
  display: inline-block;
  width: 14px;
  height: 14px;
  border-radius: 4px;
}

.swatch.default { background: var(--bar-default); }
.swatch.compare { background: var(--bar-compare); }
.swatch.swap    { background: var(--bar-swap); }
.swatch.write   { background: var(--bar-write); }
.swatch.pivot   { background: var(--bar-pivot); }
.swatch.digit   { background: var(--bar-digit); }
.swatch.gap     { background: var(--bar-gap); }
.swatch.bucket  { background: var(--bar-bucket); }
.swatch.sorted  { background: var(--bar-sorted); }

/* ---------- Viz body & heatmap ---------- */
.viz-body {
  display: flex;
  flex-direction: column;
}

.heatmap-strip {
  display: flex;
  align-items: stretch;
  gap: 2px;
  height: 14px;
  padding: 0 1.25rem 0.65rem;
  margin-top: -0.35rem;
}

.heatmap-cell {
  flex: 1;
  min-width: 2px;
  border-radius: 2px;
  background: var(--heatmap-low);
  transition: background 0.12s ease;
}

/* ---------- Counting array panel ---------- */
.counting-panel {
  display: flex;
  align-items: center;
  gap: 0.75rem;
  padding: 0.55rem 1rem 0.75rem;
  border-top: 1px solid var(--border);
  background: var(--surface-elevated);
  overflow-x: auto;
}

.counting-panel.hidden {
  display: none;
}

.counting-panel-label {
  font-size: 0.68rem;
  text-transform: uppercase;
  letter-spacing: 0.05em;
  color: var(--text-muted);
  white-space: nowrap;
  flex-shrink: 0;
}

.counting-array {
  display: flex;
  align-items: flex-end;
  gap: 4px;
  flex: 1;
  min-height: 48px;
}

.counting-cell {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 0.2rem;
  min-width: 28px;
}

.counting-bar {
  width: 100%;
  min-width: 20px;
  max-width: 36px;
  background: var(--bar-bucket);
  border-radius: 3px 3px 0 0;
  transition: height 0.1s ease, opacity 0.1s ease;
}

.counting-bar.active {
  opacity: 1;
  box-shadow: 0 0 0 2px var(--accent);
}

.counting-bar.inactive {
  opacity: 0.35;
}

.counting-label {
  font-size: 0.62rem;
  font-family: var(--font-mono);
  color: var(--text-muted);
}

.counting-value {
  font-size: 0.68rem;
  font-family: var(--font-mono);
  font-weight: 700;
  color: var(--text);
}

/* ---------- Recommender ---------- */
.recommender-content {
  display: flex;
  flex-direction: column;
  gap: 0.55rem;
}

.recommender-pick {
  font-size: 1rem;
  font-weight: 800;
  color: var(--primary);
}

.recommender-reason {
  font-size: 0.84rem;
  line-height: 1.45;
  color: var(--text-muted);
}

.recommender-stats {
  display: flex;
  flex-wrap: wrap;
  gap: 0.35rem;
}

.recommender-stat {
  font-size: 0.72rem;
  font-family: var(--font-mono);
  padding: 0.25rem 0.45rem;
  border-radius: 999px;
  background: var(--surface-elevated);
  border: 1px solid var(--border);
  color: var(--text-muted);
}

.recommender-stat strong {
  color: var(--text);
}

/* ---------- Learning cards ---------- */
.learning-card {
  perspective: 800px;
  cursor: pointer;
  outline: none;
}

.learning-card:focus-visible .learning-card-inner {
  box-shadow: 0 0 0 3px var(--primary);
}

.learning-card-inner {
  position: relative;
  min-height: 120px;
  transform-style: preserve-3d;
  transition: transform 0.5s ease;
  border-radius: var(--radius-sm);
}

.learning-card.flipped .learning-card-inner {
  transform: rotateY(180deg);
}

.learning-card-front,
.learning-card-back {
  position: absolute;
  inset: 0;
  backface-visibility: hidden;
  display: flex;
  flex-direction: column;
  gap: 0.45rem;
  padding: 0.85rem;
  border-radius: var(--radius-sm);
  border: 1px solid var(--border);
  background: linear-gradient(135deg, var(--surface-elevated), var(--surface));
}

.learning-card-back {
  transform: rotateY(180deg);
  border-left: 3px solid var(--accent);
}

.learning-card-label {
  font-size: 0.68rem;
  text-transform: uppercase;
  letter-spacing: 0.06em;
  color: var(--accent);
  font-weight: 700;
}

.learning-card-text {
  font-size: 0.86rem;
  line-height: 1.5;
  color: var(--text);
}

/* ---------- Quiz mode ---------- */
.quiz-score {
  font-size: 0.78rem;
  font-family: var(--font-mono);
  color: var(--text-muted);
  margin-top: 0.25rem;
}

.quiz-guess-grid {
  display: grid;
  grid-template-columns: repeat(2, 1fr);
  gap: 0.4rem;
}

.quiz-guess-btn {
  padding: 0.45rem 0.5rem;
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  background: var(--surface-elevated);
  color: var(--text);
  font-size: 0.74rem;
  font-weight: 600;
  cursor: pointer;
  transition: background 0.15s ease, border-color 0.15s ease;
}

.quiz-guess-btn:hover:not(:disabled) {
  background: var(--secondary);
  border-color: var(--primary);
}

.quiz-guess-btn:disabled {
  opacity: 0.5;
  cursor: not-allowed;
}

.quiz-guess-btn.correct {
  background: rgba(16, 185, 129, 0.2);
  border-color: var(--bar-sorted);
}

.quiz-guess-btn.wrong {
  background: rgba(239, 68, 68, 0.15);
  border-color: var(--bar-swap);
}

.quiz-feedback {
  margin-top: 0.65rem;
  font-size: 0.84rem;
  font-weight: 600;
  min-height: 1.25rem;
}

.quiz-hidden .profile-name {
  filter: blur(6px);
  user-select: none;
}

/* ---------- Presentation mode ---------- */
.presentation-overlay {
  position: fixed;
  inset: 0;
  z-index: 100;
  background: var(--bg);
  display: flex;
  flex-direction: column;
  padding: 1.5rem 2rem 2rem;
}

.presentation-overlay.hidden {
  display: none;
}

.presentation-hud {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 1rem;
  margin-bottom: 1rem;
  flex-shrink: 0;
}

.presentation-algo {
  font-size: 1.35rem;
  font-weight: 800;
  letter-spacing: -0.02em;
}

.presentation-metrics {
  font-family: var(--font-mono);
  font-size: 0.9rem;
  color: var(--text-muted);
}

.presentation-visualizer {
  flex: 1;
  display: flex;
  align-items: flex-end;
  justify-content: center;
  gap: 3px;
  min-height: 0;
  padding: 1rem 0;
}

.presentation-visualizer .bar {
  flex: 1;
  max-width: 48px;
  border-radius: 6px 6px 0 0;
}

.presentation-heatmap {
  display: flex;
  gap: 3px;
  height: 20px;
  flex-shrink: 0;
  margin-top: 0.5rem;
}

.presentation-heatmap .heatmap-cell {
  flex: 1;
  max-width: 48px;
  border-radius: 3px;
}

body.presentation-active {
  overflow: hidden;
}

/* ---------- Teaching panel ---------- */
.teaching-panel {
  padding: 0.85rem 1rem;
  background: linear-gradient(135deg, var(--surface) 0%, var(--surface-elevated) 100%);
  border: 1px solid var(--border);
  border-left: 4px solid var(--accent);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
}

.teaching-panel.hidden {
  display: none;
}

.teaching-title {
  font-size: 0.78rem;
  text-transform: uppercase;
  letter-spacing: 0.08em;
  color: var(--accent);
  margin-bottom: 0.45rem;
}

.teaching-text {
  font-size: 0.92rem;
  line-height: 1.5;
  color: var(--text);
}

/* ---------- Operations chart ---------- */
.ops-chart-wrap {
  display: flex;
  align-items: center;
  gap: 0.75rem;
  padding: 0.55rem 1rem 0.75rem;
  border-top: 1px solid var(--border);
  background: var(--surface-elevated);
}

.ops-chart-label {
  font-size: 0.68rem;
  text-transform: uppercase;
  letter-spacing: 0.05em;
  color: var(--text-muted);
  white-space: nowrap;
  flex-shrink: 0;
}

.ops-chart {
  flex: 1;
  height: 56px;
  max-width: 100%;
  border-radius: var(--radius-sm);
  background: var(--surface);
  border: 1px solid var(--border);
}

/* ---------- Tournament & matrix ---------- */
.leaderboard-wrap,
.matrix-wrap {
  overflow-x: auto;
}

.data-table {
  width: 100%;
  border-collapse: collapse;
  font-size: 0.78rem;
}

.data-table th,
.data-table td {
  padding: 0.45rem 0.5rem;
  text-align: left;
  border-bottom: 1px solid var(--border);
}

.data-table th {
  font-size: 0.68rem;
  text-transform: uppercase;
  letter-spacing: 0.05em;
  color: var(--text-muted);
  background: var(--surface-elevated);
}

.data-table td {
  font-family: var(--font-mono);
}

.data-table tr.rank-1 td:first-child::before {
  content: "🥇 ";
}

.data-table tr.rank-2 td:first-child::before {
  content: "🥈 ";
}

.data-table tr.rank-3 td:first-child::before {
  content: "🥉 ";
}

.data-table .match-good {
  color: var(--bar-sorted);
  font-weight: 700;
}

.data-table .match-warn {
  color: var(--bar-compare);
}

/* ---------- Accessibility ---------- */
.skip-link {
  position: absolute;
  left: -999px;
  top: 8px;
  padding: 0.5rem 0.85rem;
  background: var(--primary);
  color: #fff;
  font-weight: 700;
  text-decoration: none;
  border-radius: var(--radius-sm);
}

.skip-link:focus {
  left: 8px;
  top: 8px;
  z-index: 100;
}

.cheat-sheet {
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: var(--surface);
  color: var(--text);
  padding: 1.1rem 1.25rem 1.25rem;
  max-width: 26rem;
  width: calc(100% - 2rem);
  box-shadow: var(--shadow);
}

.cheat-sheet::backdrop {
  background: rgba(15, 23, 42, 0.45);
}

.cheat-sheet-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 0.75rem;
  margin-bottom: 0.85rem;
}

.cheat-sheet-header h2 {
  font-size: 1.05rem;
  font-weight: 800;
  letter-spacing: -0.02em;
}

.cheat-sheet-list {
  list-style: none;
  display: flex;
  flex-direction: column;
  gap: 0.45rem;
}

.cheat-sheet-list li {
  display: flex;
  align-items: center;
  flex-wrap: wrap;
  gap: 0.4rem 0.55rem;
  font-size: 0.88rem;
}

.cheat-sheet kbd {
  font-family: var(--font-mono);
  font-size: 0.75rem;
  font-weight: 700;
  padding: 0.12rem 0.4rem;
  border: 1px solid var(--border);
  border-bottom-width: 2px;
  border-radius: 6px;
  background: var(--surface-elevated);
  min-width: 1.5rem;
  text-align: center;
}

:focus-visible {
  outline: 2px solid var(--primary);
  outline-offset: 2px;
}

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after {
    animation-duration: 0.01ms !important;
    transition-duration: 0.01ms !important;
  }
}

.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;
}

/* ---------- Toast ---------- */
.toast {
  position: fixed;
  bottom: 1.25rem;
  left: 50%;
  transform: translateX(-50%) translateY(12px);
  padding: 0.65rem 1rem;
  border-radius: 999px;
  background: var(--text);
  color: var(--bg);
  font-size: 0.85rem;
  font-weight: 600;
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.25s ease, transform 0.25s ease;
  z-index: 50;
}

.toast.visible {
  opacity: 1;
  transform: translateX(-50%) translateY(0);
}

/* ---------- Responsive ---------- */
@media (max-width: 1024px) {
  .workspace {
    grid-template-columns: 1fr;
  }

  .sidebar {
    position: static;
  }

  body.race-active .viz-grid {
    grid-template-columns: 1fr;
  }
}

@media (max-width: 640px) {
  .topbar {
    padding: 0.85rem 1rem;
    flex-wrap: wrap;
  }

  .workspace {
    padding: 0.85rem;
  }

  .visualizer {
    height: 280px;
    padding: 0.85rem;
  }

  body.race-active .visualizer {
    height: 220px;
  }

  .btn-grid {
    grid-template-columns: 1fr;
  }

  .profile-grid {
    grid-template-columns: 1fr;
  }

  .panel-header-row {
    flex-direction: column;
    align-items: flex-start;
  }

  .lab-switcher {
    width: 100%;
    justify-content: center;
  }
}

/* ---------- Brush pills ---------- */
.brush-pills {
  display: flex;
  flex-wrap: wrap;
  gap: 0.35rem;
}

.brush-pill {
  display: inline-flex;
  align-items: center;
  gap: 0.35rem;
  padding: 0.4rem 0.65rem;
  border-radius: 999px;
  border: 1px solid var(--border);
  background: var(--surface-elevated);
  color: var(--text);
  font-size: 0.78rem;
  font-weight: 700;
  text-transform: none;
  letter-spacing: 0;
  cursor: pointer;
}

.brush-pill input {
  accent-color: var(--primary);
}

.brush-pill:has(input:checked) {
  background: var(--primary);
  color: #fff;
  border-color: var(--primary);
}

/* ---------- Pathfinding grid ---------- */
.path-grid-wrap {
  padding: 0.75rem;
  overflow: auto;
}

.path-grid {
  display: grid;
  grid-template-columns: repeat(var(--path-cols, 41), minmax(0, 1fr));
  gap: 1px;
  background: var(--border);
  border: 1px solid var(--border);
  user-select: none;
  touch-action: none;
  max-width: 100%;
}

.path-cell {
  aspect-ratio: 1;
  border: none;
  padding: 0;
  margin: 0;
  background: var(--path-empty);
  color: var(--text);
  font-size: clamp(0.42rem, 1.3vw, 0.72rem);
  font-weight: 800;
  line-height: 1;
  cursor: pointer;
  display: grid;
  place-items: center;
  min-width: 0;
  min-height: 0;
}

.path-cell.is-wall {
  background: var(--path-wall);
}

.path-cell.is-weight {
  background: var(--path-weight);
  color: #1f2937;
}

.path-cell.is-weight.is-visited:not(.is-path):not(.is-start):not(.is-end),
.path-cell.is-weight.is-frontier:not(.is-path):not(.is-start):not(.is-end) {
  color: #fff;
}

.path-cell.is-visited:not(.is-start):not(.is-end):not(.is-path) {
  background: var(--path-visited);
}

.path-cell.is-frontier:not(.is-start):not(.is-end):not(.is-path) {
  background: var(--path-frontier);
}

.path-cell.is-path:not(.is-start):not(.is-end) {
  background: var(--path-path);
  color: #1f2937;
}

.path-cell.is-start {
  background: var(--path-start);
  color: #052e16;
  cursor: grab;
}

.path-cell.is-end {
  background: var(--path-end);
  color: #fff;
  cursor: grab;
}

.path-cell:focus-visible {
  outline: 2px solid var(--primary);
  outline-offset: -2px;
  z-index: 1;
}

.path-cell.is-cursor {
  box-shadow: inset 0 0 0 2px var(--primary);
  z-index: 1;
}

.swatch.path-empty { background: var(--path-empty); border: 1px solid var(--border); }
.swatch.path-wall { background: var(--path-wall); }
.swatch.path-start { background: var(--path-start); }
.swatch.path-end { background: var(--path-end); }
.swatch.path-weight { background: var(--path-weight); }
.swatch.path-frontier { background: var(--path-frontier); }
.swatch.path-visited { background: var(--path-visited); }
.swatch.path-path { background: var(--path-path); }

body.race-active .path-cell {
  font-size: clamp(0.38rem, 1vw, 0.65rem);
}

/* ---------- Search Lab cells ---------- */
.search-note {
  padding: 0.55rem 0.85rem;
  border-radius: var(--radius-sm);
  border: 1px solid var(--border);
  border-left: 4px solid var(--accent);
  background: var(--surface);
  font-size: 0.82rem;
  color: var(--text-muted);
  line-height: 1.45;
}

.search-grid {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  padding: 1rem 1.15rem 1.15rem;
  min-height: 140px;
  align-content: flex-start;
}

.search-cell {
  position: relative;
  min-width: 48px;
  min-height: 52px;
  padding: 0.4rem 0.45rem 0.55rem;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 0.12rem;
  border-radius: 10px;
  background: var(--surface-elevated);
  border: 1px solid var(--border);
  color: var(--text);
  transition: background 0.12s ease, opacity 0.12s ease, border-color 0.12s ease, transform 0.12s ease;
}

.search-cell-value {
  font-family: var(--font-mono);
  font-weight: 800;
  font-size: 0.92rem;
  line-height: 1.1;
}

.search-cell-index {
  font-size: 0.62rem;
  color: var(--text-muted);
  font-family: var(--font-mono);
}

.search-cell.is-elim {
  opacity: 0.32;
}

.search-cell.is-lo {
  box-shadow: 0 0 0 2px var(--primary);
  border-color: var(--primary);
}

.search-cell.is-hi {
  box-shadow: 0 0 0 2px var(--accent);
  border-color: var(--accent);
}

.search-cell.is-lo.is-hi {
  box-shadow: 0 0 0 2px var(--bar-pivot);
  border-color: var(--bar-pivot);
}

.search-cell.is-probe {
  background: var(--bar-compare);
  color: #1f2937;
  border-color: var(--bar-compare);
  opacity: 1;
  transform: translateY(-3px);
}

.search-cell.is-probe .search-cell-index {
  color: #374151;
}

.search-cell.is-found {
  background: var(--bar-sorted);
  color: #052e16;
  border-color: var(--bar-sorted);
  opacity: 1;
  transform: none;
}

.search-cell.is-found .search-cell-index {
  color: #14532d;
}

.swatch.search-default {
  background: var(--surface-elevated);
  border: 1px solid var(--border);
}
.swatch.search-probe { background: var(--bar-compare); }
.swatch.search-lo {
  background: var(--surface-elevated);
  box-shadow: 0 0 0 2px var(--primary);
}
.swatch.search-hi {
  background: var(--surface-elevated);
  box-shadow: 0 0 0 2px var(--accent);
}
.swatch.search-found { background: var(--bar-sorted); }
.swatch.search-elim {
  background: var(--surface-elevated);
  opacity: 0.35;
}