:root {
  --void: #050407;
  --stone: #121015;
  --stone-light: #2c2832;
  --steel: #4a4552;
  --bone: #d8cdb4;
  --bone-dim: rgba(216, 205, 180, 0.62);
  --ink: #000;
  --ember: #3ddc84;
  --flame: #c8ffd9;
  --blood: #8e1b1b;
  --blood-deep: #4a0c0f;
  --venom: #4f7a3a;
  --venom-light: #8fc25a;
  --blackletter: "UnifrakturCook", "Old English Text MT", serif;
  --caps: "Cinzel", Georgia, serif;
  color-scheme: dark;
}

* {
  box-sizing: border-box;
}

body {
  margin: 0;
  min-height: 100vh;
  color: var(--bone);
  font-family: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
  background-color: var(--stone);
  background-image:
    radial-gradient(ellipse at 50% 0%, rgba(61, 220, 132, 0.16), transparent 55%),
    linear-gradient(#000 3px, transparent 3px),
    linear-gradient(90deg, #000 3px, transparent 3px);
  background-size: 100% 100%, 96px 28px, 96px 56px;
  overflow-x: hidden;
}

button,
input,
output {
  font: inherit;
}

/* ---------- Header ---------- */

.back {
  position: absolute;
  top: 14px;
  left: 16px;
  padding: 6px 12px;
  border: 1px solid var(--bone-dim);
  color: var(--bone);
  background: rgba(8, 7, 10, 0.86);
  font-family: var(--caps);
  font-size: 12px;
  font-weight: 700;
  letter-spacing: 0.14em;
  text-transform: uppercase;
  text-decoration: none;
}

.back:hover {
  border-color: var(--ember);
  background: #0c2a1b;
}

.hall {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 18px;
  padding: 56px 16px 18px;
}

.hall h1 {
  margin: 0;
  padding: 0 8px;
  color: var(--bone);
  font-family: var(--blackletter);
  font-size: clamp(2.2rem, 7vw, 3.2rem);
  font-weight: 700;
  text-shadow: 0 3px 0 #000, 0 0 26px rgba(61, 220, 132, 0.45);
}

.torch {
  position: relative;
  width: 10px;
  height: 34px;
  border: 2px solid #000;
  background: linear-gradient(90deg, #2a2730, #121015);
  border-radius: 0 0 3px 3px;
}

.torch::before {
  content: "";
  position: absolute;
  left: 50%;
  bottom: 100%;
  width: 18px;
  height: 24px;
  border-radius: 50% 50% 45% 45%;
  background: radial-gradient(ellipse at 50% 78%, #f4fff7 0 14%, var(--flame) 36%, var(--ember) 64%, transparent 74%);
  transform: translateX(-50%);
  transform-origin: 50% 100%;
  animation: flicker 0.9s infinite alternate ease-in-out;
}

.torch + h1 + .torch::before {
  animation-delay: -0.45s;
}

@keyframes flicker {
  from { transform: translateX(-50%) scale(0.9, 0.95) rotate(-3deg); opacity: 0.85; }
  to { transform: translateX(-50%) scale(1.05, 1.15) rotate(3deg); opacity: 1; }
}

/* ---------- Layout ---------- */

main {
  display: grid;
  grid-template-columns: minmax(0, 1fr) minmax(0, 1.25fr);
  gap: 20px;
  align-items: start;
  max-width: 1180px;
  margin: 0 auto;
  padding: 0 16px 48px;
}

.race-bar,
.race {
  grid-column: 1 / -1;
}

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

/* ---------- Race ---------- */

.race {
  display: grid;
  grid-template-columns: repeat(3, minmax(0, 1fr));
  gap: 16px;
}

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

  .race .board-frame {
    width: min(100%, 360px);
    margin: 0 auto;
  }
}

.lane {
  display: grid;
  grid-template-rows: auto auto auto 1fr;
  gap: 8px;
  min-width: 0;
}

.lane header h2 {
  margin: 0;
  color: var(--bone);
  font-family: var(--caps);
  font-size: 15px;
  letter-spacing: 0.1em;
  text-transform: uppercase;
}

.lane-jev header h2 {
  color: var(--flame);
}

.lane header p {
  min-height: 2.8em;
  margin: 2px 0 0;
  color: var(--bone-dim);
  font-size: 13px;
  line-height: 1.4;
}

.lane-stats {
  display: flex;
  align-items: baseline;
  gap: 14px;
  padding: 6px 10px;
  border: 1px solid var(--stone-light);
  background: rgba(0, 0, 0, 0.55);
  font-size: 15px;
  font-variant-numeric: tabular-nums;
}

.lane-stats .k {
  color: var(--bone-dim);
  font-family: var(--caps);
  font-size: 11px;
  letter-spacing: 0.06em;
  text-transform: uppercase;
}

.lane-status {
  margin-left: auto;
  font-family: var(--caps);
  font-size: 11px;
  letter-spacing: 0.08em;
  text-transform: uppercase;
}

.reason {
  min-height: 1.2em;
  margin: 0;
  color: var(--bone-dim);
  font-size: 12px;
}

[data-status="alive"] { color: var(--venom-light); }
[data-status="won"] { color: var(--flame); }
[data-status="game_over"],
[data-status="error"] { color: #ff6a5a; }

.learn {
  margin: 4px 0 0;
  color: var(--bone-dim);
  font-size: 12px;
}

.learn a {
  color: var(--ember);
  text-decoration-color: rgba(61, 220, 132, 0.4);
  text-underline-offset: 3px;
}

.learn a:hover {
  color: var(--flame);
}

/* ---------- Tally ---------- */

.tally {
  min-width: 0;
  padding: 16px;
  border: 1px solid rgba(216, 205, 180, 0.22);
  background: rgba(8, 7, 10, 0.9);
  box-shadow: 0 0 0 1px #000, 0 14px 30px -12px #000;
}

.tally h2 {
  margin: 0 0 10px;
  color: var(--ember);
  font-family: var(--caps);
  font-size: 13px;
  letter-spacing: 0.08em;
  text-transform: uppercase;
}

.tally table {
  width: 100%;
  border-collapse: collapse;
  font-size: 13px;
  font-variant-numeric: tabular-nums;
}

.tally th,
.tally td {
  padding: 7px 6px;
  border-bottom: 1px solid var(--stone-light);
  text-align: right;
}

.tally th:first-child {
  text-align: left;
}

.tally thead th {
  color: var(--bone-dim);
  font-family: var(--caps);
  font-size: 10px;
  font-weight: 500;
  letter-spacing: 0.06em;
  text-transform: uppercase;
}

.tally tbody th {
  font-weight: 500;
}

.tally tr.leader th::after {
  content: " ♛";
  color: var(--ember);
}

.tally .how-title {
  margin-top: 22px;
}

.how {
  margin-top: 14px;
  padding-top: 12px;
  border-top: 1px solid var(--stone-light);
  scroll-margin-top: 16px;
}

.how h3 {
  margin: 0 0 6px;
  color: var(--bone);
  font-family: var(--caps);
  font-size: 13px;
  letter-spacing: 0.08em;
  text-transform: uppercase;
}

.how p,
.how ol {
  margin: 0 0 8px;
  color: var(--bone);
  font-size: 13px;
  line-height: 1.55;
}

.how ol {
  padding-left: 20px;
}

.how:target h3 {
  color: var(--flame);
}

.tally .note {
  margin: 12px 0 0;
  color: var(--bone-dim);
  font-size: 12px;
  line-height: 1.5;
}

/* ---------- Arena ---------- */

.controls {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 8px;
  margin-bottom: 12px;
}

button {
  padding: 8px 14px;
  border: 1px solid var(--bone-dim);
  color: var(--bone);
  background: #0c2a1b;
  font-family: var(--caps);
  font-size: 13px;
  font-weight: 700;
  letter-spacing: 0.12em;
  text-transform: uppercase;
  cursor: pointer;
  box-shadow: 0 0 0 1px #000, 0 3px 0 #000;
}

button:hover {
  border-color: var(--ember);
  background: #1f6b43;
}

button:active {
  transform: translateY(2px);
  box-shadow: 0 0 0 1px #000;
}

#pause,
#reset {
  background: #0b090c;
}

#step {
  display: inline-flex;
  align-items: center;
  gap: 8px;
  border-color: var(--ember);
}

#step kbd {
  padding: 0 5px;
  border: 1px solid var(--bone-dim);
  font-family: var(--ui, inherit);
  font-size: 11px;
  letter-spacing: 0;
}

button:disabled {
  opacity: 0.4;
  cursor: not-allowed;
  border-color: var(--stone-light);
  background: #0b090c;
}

.stat .v[data-status="thinking"] { color: var(--flame); }

.speed {
  display: flex;
  flex: 1 1 160px;
  align-items: center;
  gap: 8px;
  min-width: 0;
  font-size: 13px;
  color: var(--bone-dim);
}

.speed output {
  min-width: 56px;
  font-variant-numeric: tabular-nums;
}

.speed input {
  flex: 1;
  min-width: 0;
  accent-color: var(--ember);
}

.stats,
.decision {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(84px, 1fr));
  gap: 8px;
  margin-bottom: 12px;
}

.stat {
  display: grid;
  gap: 2px;
  min-width: 0;
  padding: 8px 10px;
  border: 1px solid var(--stone-light);
  background: rgba(0, 0, 0, 0.55);
}

.stat .k {
  font-size: 11px;
  letter-spacing: 0.06em;
  text-transform: uppercase;
  color: var(--bone-dim);
  font-family: var(--caps);
}

.stat .v {
  overflow: hidden;
  font-size: 17px;
  font-variant-numeric: tabular-nums;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.stat .v[data-status="running"] { color: var(--venom-light); }
.stat .v[data-status="finished"] { color: var(--flame); }

.board-frame {
  width: 100%;
  padding: 10px;
  border: 3px solid #000;
  background:
    repeating-linear-gradient(90deg, #2c2832 0 22px, #000 22px 24px);
  box-shadow: inset 0 0 0 2px var(--steel), 0 0 40px -6px rgba(61, 220, 132, 0.25), 0 14px 30px -10px #000;
}

.board {
  display: grid;
  width: 100%;
  aspect-ratio: 1 / 1;
  gap: 2px;
  padding: 2px;
  background: #070608;
}

.cell {
  background: rgba(216, 205, 180, 0.035);
}

/* app.js sets --fade per segment: 1 behind the head, down toward the tail. */
.cell.snake {
  opacity: var(--fade, 1);
  background: var(--venom);
  box-shadow: inset 0 -3px 0 rgba(0, 0, 0, 0.25);
}

.cell.head {
  position: relative;
  background: var(--venom-light);
  border-radius: 4px;
}

.cell.head::before,
.cell.head::after {
  content: "";
  position: absolute;
  top: 25%;
  width: 22%;
  height: 22%;
  border-radius: 50%;
  background: #000;
  box-shadow: inset 0 0 0 2px var(--ember);
}

.cell.head::before { left: 18%; }
.cell.head::after { right: 18%; }

.cell.food {
  position: relative;
  border-radius: 50%;
  background: radial-gradient(circle at 35% 35%, #ff8a6a, var(--blood) 60%, var(--blood-deep));
  box-shadow: 0 0 10px rgba(255, 60, 30, 0.5);
  animation: bob 0.8s infinite alternate ease-in-out;
}

.cell.food::after {
  content: "";
  position: absolute;
  top: -12%;
  left: 55%;
  width: 14%;
  height: 32%;
  border-radius: 2px;
  background: var(--venom);
  transform: rotate(18deg);
}

@keyframes bob {
  from { transform: scale(0.88); }
  to { transform: scale(1); }
}

.board.dead .cell.snake,
.board.dead .cell.head {
  filter: grayscale(0.7) brightness(0.8);
}

.board.won {
  box-shadow: 0 0 0 2px var(--ember), 0 0 24px rgba(61, 220, 132, 0.35);
}

.error {
  margin: 12px 0 0;
  padding: 10px 12px;
  border: 1px solid var(--blood);
  color: var(--bone);
  background: rgba(142, 27, 27, 0.25);
  font-size: 14px;
  overflow-wrap: anywhere;
}

/* ---------- Scroll (model panel) ---------- */

.scroll {
  min-width: 0;
  padding: 16px;
  border: 1px solid rgba(216, 205, 180, 0.22);
  color: var(--bone);
  background: rgba(8, 7, 10, 0.9);
  box-shadow: 0 0 0 1px #000, 0 14px 30px -12px #000;
}

.scroll h2 {
  margin: 14px 0 8px;
  font-size: 13px;
  letter-spacing: 0.08em;
  text-transform: uppercase;
  color: var(--ember);
  font-family: var(--caps);
}

.scroll h2:first-child {
  margin-top: 0;
}

.bars {
  display: grid;
  gap: 6px;
  min-height: 24px;
}

.bar {
  display: grid;
  grid-template-columns: 76px minmax(0, 1fr) 54px;
  align-items: center;
  gap: 8px;
  font-size: 13px;
  font-variant-numeric: tabular-nums;
}

.bar .track {
  height: 14px;
  border: 1px solid var(--stone-light);
  background: #000;
  overflow: hidden;
}

.bar .fill {
  display: block;
  height: 100%;
  background: var(--steel);
  transition: width 0.18s ease-out;
}

.bar.chosen .fill {
  background: var(--venom-light);
}

.bar.game-over .fill {
  background: var(--blood);
}

.bar .p {
  text-align: right;
}

pre {
  max-height: 320px;
  margin: 0;
  padding: 10px;
  border: 1px solid var(--stone-light);
  color: var(--bone-dim);
  background: #000;
  font-size: 12px;
  line-height: 1.45;
  white-space: pre-wrap;
  overflow-wrap: anywhere;
  overflow: auto;
}

details {
  margin-top: 12px;
}

summary {
  cursor: pointer;
  font-size: 13px;
  letter-spacing: 0.08em;
  text-transform: uppercase;
  color: var(--ember);
  font-family: var(--caps);
}

details[open] summary {
  margin-bottom: 8px;
}

@media (prefers-reduced-motion: reduce) {
  .torch::before,
  .cell.food {
    animation: none;
  }
}
