:root {
  --night: #15123b;
  --panel: #1d1950;
  --floor: #0d0b2b;
  --rule: #352f7a;
  --wall: #3b4bff;
  --pac: #ffd23f;
  --dot: #ffc9b5;
  --text: #edebff;
  --muted: #a6a1da;
  --early: hsl(190 95% 60%);
  --late: hsl(330 95% 60%);
  --danger: #ff4f6d;

  --pixel: 'Press Start 2P', ui-monospace, monospace;
  --body: 'Atkinson Hyperlegible', system-ui, sans-serif;

  --gutter: clamp(16px, 4vw, 40px);
  color-scheme: dark;
}

* { box-sizing: border-box; }

[hidden] { display: none !important; }

html { background: var(--night); }

body {
  margin: 0;
  background: var(--night);
  color: var(--text);
  font: 400 1rem/1.55 var(--body);
  min-height: 100vh;
}

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

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

/* ------------------------------------------------------------ masthead */

.masthead {
  padding: clamp(28px, 5vw, 56px) var(--gutter) 0;
  max-width: 1280px;
  margin: 0 auto;
}

.title {
  font: 400 clamp(1.4rem, 4.2vw, 2.6rem)/1.25 var(--pixel);
  color: var(--pac);
  margin: 0 0 0.9rem;
  letter-spacing: 0.02em;
  text-shadow: 0 0 18px rgb(255 210 63 / 0.35);
}

.lede {
  margin: 0 0 1.6rem;
  max-width: 62ch;
  color: var(--muted);
  font-size: 1.05rem;
}

.tabs {
  display: flex;
  gap: 4px;
  border-bottom: 2px solid var(--rule);
}

.tabs button {
  appearance: none;
  border: 0;
  background: transparent;
  color: var(--muted);
  font: 700 1rem/1 var(--body);
  padding: 0.85rem 1.1rem 0.75rem;
  cursor: pointer;
  border-bottom: 4px solid transparent;
  margin-bottom: -2px;
}

.tabs button[aria-selected='true'] {
  color: var(--text);
  border-bottom-color: var(--pac);
}

.tabs button:hover { color: var(--text); }

.status {
  max-width: 1280px;
  margin: 0 auto;
  padding: 0.75rem var(--gutter) 0;
  color: var(--muted);
  min-height: 2.3rem;
  font-size: 0.95rem;
}

.status.error { color: var(--danger); }

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

main {
  max-width: 1280px;
  margin: 0 auto;
  padding: 0.5rem var(--gutter) 2rem;
}

.mode {
  display: grid;
  grid-template-columns: minmax(250px, 300px) minmax(0, 1fr);
  gap: clamp(20px, 3vw, 40px);
  align-items: start;
}

.mode[hidden] { display: none; }

@media (max-width: 820px) {
  .mode { grid-template-columns: minmax(0, 1fr); }
}

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

.controls {
  display: grid;
  gap: 1.1rem;
  background: var(--panel);
  border: 1px solid var(--rule);
  border-radius: 14px;
  padding: 1.25rem;
}

.field {
  display: grid;
  gap: 0.4rem;
  font-weight: 700;
  font-size: 0.92rem;
  margin: 0;
  padding: 0;
  border: 0;
  min-width: 0;
}

.field[hidden], .sub[hidden] { display: none; }

legend {
  padding: 0;
  margin-bottom: 0.4rem;
  font-weight: 700;
}

select {
  appearance: none;
  width: 100%;
  font: 400 1rem/1.3 var(--body);
  color: var(--text);
  background: var(--floor) url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='12' height='8'%3E%3Cpath d='M1 1l5 5 5-5' stroke='%23a6a1da' stroke-width='2' fill='none'/%3E%3C/svg%3E") no-repeat right 0.8rem center;
  border: 1px solid var(--rule);
  border-radius: 8px;
  padding: 0.6rem 2.2rem 0.6rem 0.7rem;
}

.segmented {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  background: var(--floor);
  border: 1px solid var(--rule);
  border-radius: 8px;
  padding: 3px;
  gap: 3px;
}

.segmented label {
  position: relative;
  display: block;
  text-align: center;
  cursor: pointer;
}

.segmented input {
  position: absolute;
  opacity: 0;
  inset: 0;
  cursor: pointer;
}

.segmented span {
  display: block;
  padding: 0.5rem 0;
  border-radius: 6px;
  font-weight: 700;
  font-size: 0.9rem;
  color: var(--muted);
}

.segmented input:checked + span {
  background: var(--pac);
  color: var(--floor);
}

.segmented input:focus-visible + span {
  outline: 3px solid var(--pac);
  outline-offset: 1px;
}

.side .sub {
  display: grid;
  gap: 0.35rem;
  margin-top: 0.6rem;
  font-size: 0.88rem;
}

.actions {
  display: flex;
  gap: 0.6rem;
}

.run, .stop {
  appearance: none;
  flex: 1;
  border: 0;
  border-radius: 999px;
  font: 700 1rem/1 var(--body);
  padding: 0.85rem 1rem;
  cursor: pointer;
}

.run {
  background: var(--pac);
  color: var(--floor);
}

.run:disabled {
  background: var(--rule);
  color: var(--muted);
  cursor: progress;
}

.stop {
  background: transparent;
  color: var(--danger);
  border: 2px solid var(--danger);
  flex: 0 0 auto;
}

.stop[hidden] { display: none; }

.explain {
  margin: 0;
  color: var(--muted);
  font-size: 0.92rem;
  line-height: 1.5;
}

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

/* ------------------------------------------------------------ stage */

.stage {
  min-width: 0;
  display: grid;
  gap: 0.9rem;
}

.hud {
  display: flex;
  flex-wrap: wrap;
  gap: 0.6rem 2.2rem;
  margin: 0;
}

.hud div { display: grid; gap: 0.45rem; }

.hud dt {
  color: var(--muted);
  font-size: 0.85rem;
}

.hud dd {
  margin: 0;
  font: 400 clamp(0.95rem, 2.2vw, 1.35rem)/1 var(--pixel);
  color: var(--text);
}

.hud dd.good { color: var(--pac); }

.hud.small { gap: 0.5rem 1.4rem; }
.hud.small dd { font-size: clamp(0.75rem, 1.6vw, 0.95rem); }

.board {
  position: relative;
  background: var(--floor);
  border-radius: 14px;
  padding: clamp(8px, 1.5vw, 16px);
  display: flex;
  justify-content: center;
  overflow: hidden;
  min-height: 120px;
}

.board canvas { display: block; max-width: 100%; }

.overlay {
  position: absolute;
  inset: 0;
  display: grid;
  place-items: center;
  margin: 0;
  font: 400 clamp(0.9rem, 2.4vw, 1.4rem)/1.6 var(--pixel);
  color: var(--pac);
  text-align: center;
  background: rgb(13 11 43 / 0.72);
  padding: 1rem;
}

.overlay[hidden] { display: none; }

.overlay.lost { color: var(--danger); }

.playback {
  display: flex;
  align-items: center;
  gap: 0.9rem;
  flex-wrap: wrap;
}

.playback[hidden] { display: none; }

.playback input[type='range'] {
  flex: 1 1 200px;
  accent-color: var(--pac);
}

.icon {
  appearance: none;
  width: 2.6rem;
  height: 2.6rem;
  border-radius: 50%;
  border: 2px solid var(--pac);
  background: transparent;
  color: var(--pac);
  font-size: 0.85rem;
  cursor: pointer;
}

.speed {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  font-size: 0.9rem;
  color: var(--muted);
}

.speed select { width: auto; padding-block: 0.4rem; }

.legend {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 0.4rem 0.5rem;
  margin: 0;
  color: var(--muted);
  font-size: 0.88rem;
}

.swatch {
  display: inline-block;
  width: 14px;
  height: 14px;
  border-radius: 3px;
  margin-left: 0.6rem;
}

.swatch:first-child { margin-left: 0; }
.swatch.early { background: var(--early); }
.swatch.late { background: var(--late); }
.swatch.route { background: var(--pac); border-radius: 50%; }

.code summary {
  cursor: pointer;
  color: var(--muted);
  font-weight: 700;
}

.code pre {
  margin: 0.8rem 0 0;
  background: var(--floor);
  border: 1px solid var(--rule);
  border-radius: 10px;
  padding: 1rem;
  overflow: auto;
  max-height: 460px;
  font: 400 0.84rem/1.55 ui-monospace, 'Cascadia Code', Consolas, monospace;
  color: #d9d6ff;
}

/* ------------------------------------------------------------ compare */

.verdict {
  margin: 0;
  font-size: 1.05rem;
  min-height: 1.6em;
  max-width: 70ch;
}

.duel {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 1rem;
}

@media (max-width: 1000px) {
  .duel { grid-template-columns: minmax(0, 1fr); }
}

.duel figure {
  margin: 0;
  display: grid;
  gap: 0.6rem;
  min-width: 0;
}

.duel figcaption {
  font-weight: 700;
  font-size: 1.05rem;
}

/* ------------------------------------------------------------ play */

.dpad {
  display: none;
  grid-template-columns: repeat(3, 3.4rem);
  grid-template-rows: repeat(2, 3.4rem);
  gap: 6px;
  justify-content: center;
}

.dpad button {
  appearance: none;
  border: 1px solid var(--rule);
  background: var(--panel);
  color: var(--pac);
  border-radius: 10px;
  font-size: 1.1rem;
  touch-action: manipulation;
}

.dpad [data-dir='North'] { grid-column: 2; grid-row: 1; }
.dpad [data-dir='West'] { grid-column: 1; grid-row: 2; }
.dpad [data-dir='South'] { grid-column: 2; grid-row: 2; }
.dpad [data-dir='East'] { grid-column: 3; grid-row: 2; }

@media (pointer: coarse) {
  .dpad { display: grid; }
}

/* ------------------------------------------------------------ footer */

.credits {
  max-width: 1280px;
  margin: 0 auto;
  padding: 1.5rem var(--gutter) 2.5rem;
  color: var(--muted);
  font-size: 0.9rem;
  border-top: 1px solid var(--rule);
}

.credits p { margin: 0; max-width: 75ch; }

@media (prefers-reduced-motion: reduce) {
  .title { text-shadow: none; }
}

/* ------------------------------------------------------------ loading screen */

.loader {
  position: fixed;
  inset: 0;
  z-index: 50;
  display: grid;
  place-items: center;
  padding: 16px;
  background:
    radial-gradient(ellipse at center, rgb(21 18 59 / 0.55), var(--night) 70%),
    repeating-linear-gradient(0deg, transparent 0 39px, rgb(59 75 255 / 0.13) 39px 41px),
    repeating-linear-gradient(90deg, transparent 0 39px, rgb(59 75 255 / 0.13) 39px 41px),
    var(--night);
  transition: opacity 0.45s ease, visibility 0.45s;
}

.loader.done {
  opacity: 0;
  visibility: hidden;
}

.loader-card {
  width: min(460px, 100%);
  display: grid;
  gap: 1rem;
  justify-items: center;
  text-align: center;
}

.loader-title {
  margin: 0.4rem 0 0;
  font: 400 clamp(1.2rem, 5vw, 1.8rem)/1.3 var(--pixel);
  color: var(--pac);
  text-shadow: 0 0 18px rgb(255 210 63 / 0.35);
}

.loader-stage {
  margin: 0;
  min-height: 1.6em;
  font-size: 1.05rem;
}

.loader-bar {
  width: 100%;
  height: 10px;
  border-radius: 999px;
  background: var(--panel);
  border: 1px solid var(--rule);
  overflow: hidden;
}

.loader-bar span {
  display: block;
  height: 100%;
  width: 0;
  border-radius: inherit;
  background: var(--pac);
  box-shadow: 0 0 12px rgb(255 210 63 / 0.6);
  transition: width 0.35s ease;
}

.loader.failed .loader-bar span { background: var(--danger); box-shadow: none; }

.loader-steps {
  list-style: none;
  margin: 0;
  padding: 0;
  display: grid;
  gap: 0.35rem;
  text-align: left;
  font-size: 0.92rem;
  color: var(--muted);
  width: 100%;
}

.loader-steps li {
  display: grid;
  grid-template-columns: 1.4rem 1fr;
  align-items: baseline;
}

.loader-steps li::before { content: '○'; color: var(--rule); }
.loader-steps li[data-state='active'] { color: var(--text); }
.loader-steps li[data-state='active']::before { content: '●'; color: var(--pac); }
.loader-steps li[data-state='done']::before { content: '✓'; color: var(--pac); font-weight: 700; }
.loader-steps li[data-state='failed'] { color: var(--danger); }
.loader-steps li[data-state='failed']::before { content: '✕'; color: var(--danger); }

.loader-tip {
  margin: 0.4rem 0 0;
  min-height: 3em;
  max-width: 44ch;
  color: var(--muted);
  font-size: 0.92rem;
  transition: opacity 0.3s ease;
}

.loader-tip strong { color: var(--text); }

.loader-skip {
  appearance: none;
  background: transparent;
  border: 0;
  color: var(--muted);
  font: 400 0.9rem/1 var(--body);
  text-decoration: underline;
  text-underline-offset: 3px;
  padding: 0.5rem;
  cursor: pointer;
}

.loader-skip:hover { color: var(--text); }

/* Pac-Man chasing dots, a ghost close behind */
.chase {
  position: relative;
  width: 260px;
  height: 56px;
  display: flex;
  align-items: center;
}

.pac {
  position: relative;
  z-index: 1;
  flex: 0 0 auto;
  width: 48px;
  height: 48px;
  border-radius: 50%;
  background: var(--pac);
  animation: chomp 0.28s ease-in-out infinite alternate;
}

.pac.small { width: 22px; height: 22px; }

@keyframes chomp {
  from { clip-path: polygon(100% 18%, 50% 50%, 100% 82%, 100% 100%, 0 100%, 0 0, 100% 0); }
  to { clip-path: polygon(100% 50%, 50% 50%, 100% 50%, 100% 100%, 0 100%, 0 0, 100% 0); }
}

.dots {
  flex: 1;
  display: flex;
  gap: 18px;
  overflow: hidden;
  margin-left: -10px;
  padding-left: 14px;
  height: 8px;
}

.dots i {
  flex: 0 0 8px;
  height: 8px;
  border-radius: 50%;
  background: var(--dot);
  animation: dots-in 0.52s linear infinite;
}

@keyframes dots-in {
  to { transform: translateX(-26px); }
}

.ghost {
  flex: 0 0 auto;
  width: 40px;
  height: 42px;
  margin-left: 10px;
  background: var(--danger);
  border-radius: 20px 20px 0 0;
  clip-path: polygon(0 0, 100% 0, 100% 100%, 83% 86%, 67% 100%, 50% 86%, 33% 100%, 17% 86%, 0 100%);
  position: relative;
  animation: bob 0.6s ease-in-out infinite alternate;
}

.ghost::before, .ghost::after {
  content: '';
  position: absolute;
  top: 12px;
  width: 11px;
  height: 13px;
  border-radius: 50%;
  background: radial-gradient(circle at 30% 55%, #1b1bb8 0 3px, #fff 3.5px);
}

.ghost::before { left: 7px; }
.ghost::after { right: 7px; }

@keyframes bob {
  to { transform: translateY(-4px); }
}

/* ------------------------------------------------------------ searching overlay */

.busy {
  position: absolute;
  inset: 0;
  z-index: 2;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 0.8rem;
  background: rgb(13 11 43 / 0.7);
  font: 400 clamp(0.7rem, 1.6vw, 0.9rem)/1.4 var(--pixel);
  color: var(--pac);
  border-radius: inherit;
}

@media (prefers-reduced-motion: reduce) {
  .pac, .dots i, .ghost { animation: none; }
  .loader, .loader-bar span, .loader-tip { transition: none; }
}
