:root {
  color-scheme: dark;
  --bg: #111827;
  --panel: #1f2937;
  --panel-border: #374151;
  --text: #e5e7eb;
  --muted: #9ca3af;
  --snake: #22c55e;
  --snake-head: #16a34a;
  --food: #ef4444;
  --bonus: #f59e0b;
  --slow: #38bdf8;
  --dash: #fb7185;
  --cell: #0f172a;
}

* {
  box-sizing: border-box;
}

body {
  margin: 0;
  min-height: 100vh;
  font-family: "Segoe UI", Tahoma, Geneva, Verdana, sans-serif;
  background: var(--bg);
  color: var(--text);
}

button {
  font: inherit;
}

.app {
  min-height: 100vh;
  display: grid;
  place-items: center;
  padding: 24px;
}

.game-shell {
  width: min(100%, 560px);
  display: grid;
  gap: 16px;
}

.hud {
  display: flex;
  justify-content: space-between;
  align-items: end;
  gap: 16px;
}

.metrics {
  display: flex;
  gap: 12px;
  align-items: stretch;
  flex-wrap: wrap;
}

.eyebrow {
  margin: 0 0 4px;
  color: var(--muted);
  font-size: 0.875rem;
  text-transform: uppercase;
  letter-spacing: 0.08em;
}

.hud h1 {
  margin: 0;
  font-size: 2rem;
}

.scoreboard {
  min-width: 88px;
  padding: 12px 14px;
  border: 1px solid var(--panel-border);
  border-radius: 12px;
  background: var(--panel);
  text-align: center;
}

.scoreboard span {
  display: block;
  color: var(--muted);
  font-size: 0.875rem;
}

.scoreboard strong {
  display: block;
  margin-top: 4px;
  font-size: 1.5rem;
}

.stat-card {
  min-width: 110px;
  padding: 12px 14px;
  border: 1px solid var(--panel-border);
  border-radius: 12px;
  background: var(--panel);
  text-align: center;
}

.stat-card span {
  display: block;
  color: var(--muted);
  font-size: 0.875rem;
}

.stat-card strong {
  display: block;
  margin-top: 4px;
  font-size: 1.35rem;
}

#high-score-card.is-celebrating {
  border-color: rgb(251 191 36 / 0.55);
  box-shadow:
    0 0 0 1px rgb(251 191 36 / 0.24),
    0 0 28px rgb(251 191 36 / 0.2);
  animation: bestPulse 0.9s ease-out 2;
}

.dash-meter {
  min-width: 170px;
  padding: 12px 14px;
  border: 1px solid var(--panel-border);
  border-radius: 12px;
  background: var(--panel);
}

.dash-meter__header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  margin-bottom: 8px;
  color: var(--muted);
  font-size: 0.875rem;
}

.dash-meter__track {
  overflow: hidden;
  height: 10px;
  border-radius: 999px;
  background: #0b1220;
}

.dash-meter__fill {
  height: 100%;
  width: 100%;
  border-radius: inherit;
  background: linear-gradient(90deg, #f97316 0%, var(--dash) 100%);
}

.play-layout {
  display: grid;
  gap: 16px;
}

.board-frame {
  padding: 16px;
  border: 1px solid var(--panel-border);
  border-radius: 16px;
  background: var(--panel);
}

.status {
  min-height: 24px;
  margin-bottom: 12px;
  color: var(--muted);
}

.board-stack {
  position: relative;
}

.board {
  display: grid;
  gap: 2px;
  width: 100%;
  aspect-ratio: 1;
  padding: 2px;
  border-radius: 12px;
  background: #020617;
  outline: none;
}

.board[data-dash="true"] {
  box-shadow: inset 0 0 0 1px rgb(251 113 133 / 0.16);
}

.board[data-warning="true"] {
  box-shadow:
    inset 0 0 0 1px rgb(251 113 133 / 0.24),
    inset 0 0 0 6px rgb(251 113 133 / 0.05);
  animation: chainWarning 0.8s ease-in-out infinite alternate;
}

.popup-layer {
  position: absolute;
  inset: 0;
  pointer-events: none;
  overflow: hidden;
}

.overlay {
  position: fixed;
  inset: 0;
  z-index: 30;
  display: grid;
  place-items: center;
  padding:
    max(20px, env(safe-area-inset-top))
    max(16px, env(safe-area-inset-right))
    max(20px, env(safe-area-inset-bottom))
    max(16px, env(safe-area-inset-left));
  background: rgb(2 6 23 / 0.74);
  backdrop-filter: blur(6px);
}

.overlay[data-mode="gameover"] {
  pointer-events: none;
}

.overlay[data-mode="gameover"] .overlay__card {
  pointer-events: auto;
}

.overlay[hidden] {
  display: none;
}

.overlay__card {
  width: min(420px, 100%);
  max-height: calc(100vh - 40px);
  overflow: auto;
  padding: 22px 24px 24px;
  border: 1px solid rgb(255 255 255 / 0.15);
  border-radius: 24px;
  background:
    radial-gradient(circle at top, rgb(251 191 36 / 0.12), transparent 42%),
    rgb(15 23 42 / 0.94);
  text-align: center;
  box-shadow: 0 18px 50px rgb(0 0 0 / 0.28);
}

.overlay__icon {
  width: 64px;
  height: 64px;
  margin: 0 auto 12px;
  display: grid;
  place-items: center;
  border-radius: 999px;
  background: rgb(255 255 255 / 0.08);
  font-size: 2rem;
  box-shadow: inset 0 1px 0 rgb(255 255 255 / 0.12);
}

.overlay__art {
  margin: 0 auto 12px;
  width: min(220px, 100%);
  height: 72px;
  border-radius: 18px;
  background:
    radial-gradient(circle at 15% 50%, rgb(74 222 128 / 0.95) 0 10px, transparent 11px),
    radial-gradient(circle at 26% 50%, rgb(34 197 94 / 0.95) 0 9px, transparent 10px),
    radial-gradient(circle at 37% 50%, rgb(22 163 74 / 0.95) 0 8px, transparent 9px),
    radial-gradient(circle at 48% 50%, rgb(21 128 61 / 0.95) 0 7px, transparent 8px),
    radial-gradient(circle at 82% 34%, rgb(248 113 113 / 0.95) 0 12px, transparent 13px),
    radial-gradient(circle at 87% 23%, rgb(34 197 94 / 0.8) 0 4px, transparent 5px),
    linear-gradient(180deg, rgb(255 255 255 / 0.08), rgb(255 255 255 / 0));
  box-shadow:
    inset 0 1px 0 rgb(255 255 255 / 0.1),
    0 12px 24px rgb(0 0 0 / 0.18);
}

.overlay__eyebrow {
  margin: 0 0 8px;
  color: var(--muted);
  font-size: 0.8rem;
  text-transform: uppercase;
  letter-spacing: 0.08em;
}

.overlay__title {
  margin: 0;
  font-size: clamp(2rem, 8vw, 3.5rem);
  line-height: 1;
}

.overlay__text {
  margin: 10px 0 0;
  color: var(--muted);
}

.overlay__controls {
  margin-top: 18px;
  padding: 14px;
  border: 1px solid rgb(255 255 255 / 0.08);
  border-radius: 18px;
  background: rgb(255 255 255 / 0.04);
}

.overlay__controls-label {
  display: block;
  margin-bottom: 10px;
  color: var(--muted);
  font-size: 0.78rem;
  letter-spacing: 0.08em;
  text-transform: uppercase;
}

.control-mode-picker {
  display: grid;
  grid-template-columns: repeat(3, minmax(0, 1fr));
  gap: 8px;
}

.control-mode-button {
  padding: 10px 12px;
  border: 1px solid rgb(255 255 255 / 0.08);
  border-radius: 14px;
  background: rgb(15 23 42 / 0.88);
  color: var(--muted);
  cursor: pointer;
}

.control-mode-button.is-active {
  border-color: rgb(251 191 36 / 0.36);
  background: linear-gradient(180deg, rgb(251 191 36 / 0.18), rgb(251 191 36 / 0.06));
  color: var(--text);
  box-shadow: inset 0 1px 0 rgb(255 255 255 / 0.08);
}

.overlay__actions {
  margin-top: 16px;
}

.overlay__button {
  min-width: 160px;
  padding: 12px 18px;
  border: 1px solid rgb(251 191 36 / 0.28);
  border-radius: 999px;
  background: linear-gradient(180deg, rgb(251 191 36 / 0.18), rgb(251 191 36 / 0.08));
  color: var(--text);
  cursor: pointer;
}

.cell {
  position: relative;
  overflow: hidden;
  border-radius: 2px;
  background: var(--cell);
}

.cell::before {
  content: attr(data-icon);
  position: absolute;
  inset: 0;
  display: grid;
  place-items: center;
  font-size: 0.95rem;
  line-height: 1;
}

.cell--snake {
  background: linear-gradient(180deg, #4ade80 0%, var(--snake) 100%);
}

.cell--snake::after {
  content: "";
  position: absolute;
  inset: 2px 3px auto 3px;
  height: 30%;
  border-radius: 999px;
  background: rgb(255 255 255 / 0.2);
}

.cell--head {
  background: radial-gradient(circle at 35% 35%, #86efac 0%, var(--snake-head) 60%, #15803d 100%);
}

.cell--head::before {
  content: attr(data-icon);
  position: absolute;
  inset: 0;
  display: grid;
  place-items: center;
  color: #052e16;
  font-size: 0.56rem;
  font-weight: 700;
  letter-spacing: -0.12em;
  transform: translateY(-1px);
}

.cell--head::after {
  content: "";
  position: absolute;
  left: 22%;
  right: 22%;
  bottom: 18%;
  height: 18%;
  border-radius: 999px;
  background:
    radial-gradient(circle at 20% 50%, rgb(251 113 133 / 0.7) 0 30%, transparent 31%),
    radial-gradient(circle at 80% 50%, rgb(251 113 133 / 0.7) 0 30%, transparent 31%);
}

.cell--dash-head,
.cell--dash-body {
  position: relative;
  overflow: hidden;
}

.cell--dash-head {
  box-shadow: 0 0 12px rgb(251 113 133 / 0.5);
  animation: dashPulse 0.35s ease-in-out infinite alternate;
}

.cell--dash-body {
  box-shadow: 0 0 8px rgb(251 113 133 / 0.22);
}

.cell--dash-head::after,
.cell--dash-body::after {
  content: "";
  position: absolute;
  inset: 0;
  opacity: 0.95;
  pointer-events: none;
}

.board[data-dash="true"][data-direction="right"] .cell--dash-head::after {
  background:
    linear-gradient(90deg, rgb(255 255 255 / 0) 0%, rgb(255 255 255 / 0.12) 35%, rgb(255 255 255 / 0.92) 100%),
    linear-gradient(270deg, rgb(251 113 133 / 0.55) 0%, rgb(251 113 133 / 0.18) 42%, rgb(251 113 133 / 0) 100%);
}

.board[data-dash="true"][data-direction="left"] .cell--dash-head::after {
  background:
    linear-gradient(270deg, rgb(255 255 255 / 0) 0%, rgb(255 255 255 / 0.12) 35%, rgb(255 255 255 / 0.92) 100%),
    linear-gradient(90deg, rgb(251 113 133 / 0.55) 0%, rgb(251 113 133 / 0.18) 42%, rgb(251 113 133 / 0) 100%);
}

.board[data-dash="true"][data-direction="up"] .cell--dash-head::after {
  background:
    linear-gradient(0deg, rgb(255 255 255 / 0) 0%, rgb(255 255 255 / 0.12) 35%, rgb(255 255 255 / 0.92) 100%),
    linear-gradient(180deg, rgb(251 113 133 / 0.55) 0%, rgb(251 113 133 / 0.18) 42%, rgb(251 113 133 / 0) 100%);
}

.board[data-dash="true"][data-direction="down"] .cell--dash-head::after {
  background:
    linear-gradient(180deg, rgb(255 255 255 / 0) 0%, rgb(255 255 255 / 0.12) 35%, rgb(255 255 255 / 0.92) 100%),
    linear-gradient(0deg, rgb(251 113 133 / 0.55) 0%, rgb(251 113 133 / 0.18) 42%, rgb(251 113 133 / 0) 100%);
}

.board[data-dash="true"][data-direction="right"] .cell--dash-body::after {
  background: linear-gradient(270deg, rgb(251 113 133 / 0.42) 0%, rgb(251 113 133 / 0.1) 45%, rgb(251 113 133 / 0) 100%);
}

.board[data-dash="true"][data-direction="left"] .cell--dash-body::after {
  background: linear-gradient(90deg, rgb(251 113 133 / 0.42) 0%, rgb(251 113 133 / 0.1) 45%, rgb(251 113 133 / 0) 100%);
}

.board[data-dash="true"][data-direction="up"] .cell--dash-body::after {
  background: linear-gradient(180deg, rgb(251 113 133 / 0.42) 0%, rgb(251 113 133 / 0.1) 45%, rgb(251 113 133 / 0) 100%);
}

.board[data-dash="true"][data-direction="down"] .cell--dash-body::after {
  background: linear-gradient(0deg, rgb(251 113 133 / 0.42) 0%, rgb(251 113 133 / 0.1) 45%, rgb(251 113 133 / 0) 100%);
}

.cell--food {
  background: radial-gradient(circle at 35% 35%, #fca5a5 0%, var(--food) 58%, #b91c1c 100%);
}

.cell--bonus {
  background: radial-gradient(circle at 35% 35%, #fde68a 0%, var(--bonus) 60%, #b45309 100%);
}

.cell--slow {
  background: radial-gradient(circle at 35% 35%, #bfdbfe 0%, var(--slow) 58%, #0369a1 100%);
}

.cell--food::before,
.cell--bonus::before,
.cell--slow::before {
  filter: drop-shadow(0 1px 2px rgb(0 0 0 / 0.25));
}

.cell--trail-1,
.cell--trail-2,
.cell--trail-3,
.cell--trail-4 {
  background: rgb(251 113 133 / 0.18);
}

.cell--trail-2 {
  background: rgb(251 113 133 / 0.28);
}

.cell--trail-3 {
  background: rgb(251 113 133 / 0.4);
}

.cell--trail-4 {
  background: rgb(251 113 133 / 0.56);
}

.score-popup {
  position: absolute;
  transform: translate(-50%, -50%);
  font-weight: 700;
  font-size: 0.95rem;
  text-shadow: 0 2px 8px rgb(0 0 0 / 0.35);
  animation: popupRise 0.9s ease-out forwards;
  white-space: nowrap;
}

.score-popup--score {
  color: #fde68a;
}

.score-popup--effect {
  color: #7dd3fc;
}

.score-popup--chain-break {
  color: #fca5a5;
}

.spawn-burst {
  position: absolute;
  width: 14px;
  height: 14px;
  border-radius: 999px;
  transform: translate(-50%, -50%);
  animation: spawnBurst 0.7s ease-out forwards;
}

.spawn-burst::before,
.spawn-burst::after {
  content: "";
  position: absolute;
  inset: -10px;
  border-radius: inherit;
  border: 2px solid currentColor;
  opacity: 0.45;
}

.spawn-burst::after {
  inset: -4px;
  opacity: 0.8;
}

.spawn-burst--goal {
  color: #f87171;
}

.spawn-burst--bonus {
  color: #fbbf24;
}

.spawn-burst--slow {
  color: #7dd3fc;
}

@keyframes dashPulse {
  from {
    transform: scale(1);
    filter: saturate(1);
  }

  to {
    transform: scale(1.06);
    filter: saturate(1.22);
  }
}

@keyframes popupRise {
  0% {
    opacity: 0;
    transform: translate(-50%, -45%) scale(0.9);
  }

  20% {
    opacity: 1;
  }

  100% {
    opacity: 0;
    transform: translate(-50%, -180%) scale(1.05);
  }
}

@keyframes spawnBurst {
  0% {
    opacity: 0;
    transform: translate(-50%, -50%) scale(0.2);
  }

  20% {
    opacity: 1;
  }

  100% {
    opacity: 0;
    transform: translate(-50%, -50%) scale(1.8);
  }
}

@keyframes chainWarning {
  from {
    box-shadow:
      inset 0 0 0 1px rgb(251 113 133 / 0.22),
      inset 0 0 0 6px rgb(251 113 133 / 0.04),
      0 0 0 rgb(251 113 133 / 0);
  }

  to {
    box-shadow:
      inset 0 0 0 1px rgb(251 113 133 / 0.5),
      inset 0 0 0 6px rgb(251 113 133 / 0.1),
      0 0 24px rgb(251 113 133 / 0.16);
  }
}

@keyframes bestPulse {
  0% {
    transform: scale(1);
  }

  35% {
    transform: scale(1.04);
  }

  100% {
    transform: scale(1);
  }
}

.control-rail {
  display: grid;
  gap: 12px;
  align-content: start;
  justify-items: stretch;
}

.actions,
.touch-controls,
.touch-controls__row {
  display: flex;
  gap: 12px;
}

.actions button,
.touch-controls button {
  border: 1px solid var(--panel-border);
  border-radius: 12px;
  background: var(--panel);
  color: var(--text);
  cursor: pointer;
}

.actions button:disabled,
.touch-controls button:disabled {
  opacity: 0.6;
  cursor: default;
}

#sound-button {
  border-color: rgb(125 211 252 / 0.25);
}

#music-button {
  border-color: rgb(167 139 250 / 0.28);
}

.effects {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 12px 14px;
  border: 1px solid var(--panel-border);
  border-radius: 12px;
  background: var(--panel);
}

.effects__label {
  color: var(--muted);
  font-size: 0.875rem;
  text-transform: uppercase;
  letter-spacing: 0.08em;
}

.effects__list {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
}

.effect-chip {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  padding: 6px 10px;
  border-radius: 999px;
  background: #111827;
  color: var(--text);
  font-size: 0.875rem;
}

.effect-chip::before {
  content: "";
  width: 8px;
  height: 8px;
  border-radius: 999px;
  background: currentColor;
}

.effect-chip--slow {
  color: var(--slow);
}

.effect-chip--combo {
  color: var(--bonus);
}

.effect-chip--idle {
  color: var(--muted);
}

.legend {
  display: grid;
  gap: 8px;
  padding: 12px 14px;
  border: 1px solid var(--panel-border);
  border-radius: 12px;
  background: var(--panel);
  color: var(--muted);
  font-size: 0.92rem;
}

.legend__item {
  display: flex;
  align-items: center;
  gap: 10px;
}

.legend__swatch {
  width: 14px;
  height: 14px;
  border-radius: 4px;
  flex: 0 0 auto;
}

.legend__swatch--food {
  background: var(--food);
}

.legend__swatch--dash {
  background: var(--dash);
}

.legend__swatch--bonus {
  background: var(--bonus);
}

.legend__swatch--slow {
  background: var(--slow);
}

.actions button {
  flex: 1;
  padding: 12px 16px;
}

.touch-controls {
  min-width: 0;
  padding: 12px;
  border: 1px solid var(--panel-border);
  border-radius: 16px;
  background: var(--panel);
  flex-direction: column;
  align-items: stretch;
}

.touch-controls button {
  min-width: 0;
  width: 100%;
  padding: 12px 14px;
}

#dash-button {
  border-color: rgb(251 113 133 / 0.35);
  color: #ffe4ea;
}

.touch-controls__up,
#dash-button {
  min-height: 56px;
}

.touch-controls__row {
  justify-content: center;
}

@media (min-width: 641px) {
  .game-shell[data-control-mode="pointer"],
  .game-shell[data-control-mode="both"] {
    width: min(100%, 920px);
  }

  .game-shell[data-control-mode="pointer"] .play-layout,
  .game-shell[data-control-mode="both"] .play-layout {
    grid-template-columns: minmax(0, 1fr) 320px;
    align-items: start;
  }

  .game-shell[data-control-mode="keyboard"] .control-rail {
    display: none;
  }

  .control-rail {
    width: 100%;
    justify-self: stretch;
  }
}

@media (max-width: 640px) {
  .app {
    min-height: 100svh;
    place-items: start center;
    padding: 12px;
  }

  .hud {
    gap: 10px;
    align-items: start;
    flex-direction: column;
  }

  .metrics {
    width: 100%;
    display: grid;
    grid-template-columns: repeat(4, minmax(0, 1fr));
    gap: 8px;
  }

  .hud h1 {
    font-size: 1.5rem;
  }

  .scoreboard,
  .stat-card,
  .dash-meter {
    min-width: 0;
    padding: 10px 10px 11px;
  }

  .scoreboard strong,
  .stat-card strong {
    font-size: 1.1rem;
  }

  .dash-meter {
    grid-column: 1 / -1;
  }

  .play-layout {
    grid-template-columns: minmax(0, 1fr) 98px;
    gap: 10px;
    align-items: start;
  }

  .board-frame {
    padding: 12px;
  }

  .status {
    min-height: 0;
    margin-bottom: 8px;
    font-size: 0.88rem;
  }

  .effects {
    padding: 10px 12px;
    align-items: start;
    flex-direction: column;
  }

  .effects__list {
    width: 100%;
  }

  .dash-meter {
    min-width: 0;
  }

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

  .actions button {
    min-height: 48px;
    padding: 10px 12px;
  }

  .control-rail {
    gap: 10px;
  }

  .touch-controls {
    padding: 10px;
    gap: 8px;
  }

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

  .touch-controls button {
    min-height: 54px;
    padding: 0;
    font-size: 0.82rem;
  }

  #dash-button {
    min-height: 62px;
  }

  .legend {
    display: none;
  }

  .overlay {
    align-items: center;
    padding:
      max(12px, env(safe-area-inset-top))
      max(12px, env(safe-area-inset-right))
      max(12px, env(safe-area-inset-bottom))
      max(12px, env(safe-area-inset-left));
  }

  .overlay__card {
    width: min(100%, 360px);
    max-height: calc(100vh - 24px);
    padding: 18px 18px 20px;
    border-radius: 20px;
  }

  .overlay__icon {
    width: 56px;
    height: 56px;
    margin-bottom: 10px;
    font-size: 1.7rem;
  }

  .overlay__art {
    height: 56px;
    margin-bottom: 10px;
  }

  .overlay__title {
    font-size: clamp(1.8rem, 10vw, 3rem);
  }

  .overlay__text {
    font-size: 0.98rem;
  }

  .overlay__controls {
    margin-top: 14px;
    padding: 12px;
  }
}
