:root {
  --block-width: 5px;
  --grey: #ccdddd;
  --blue: #4e82ff;
  --green: #00aa66;
  --deep-blue: #000066;
  --lime-green: #a6ff75;
  --red: #ff6699;
  --pink: #ff66cc;
  --purple: #9900ff;
  --orange: #ffcc00;
  --yellow: #ffee66;
}

body {
  margin: 0;
  padding: 0;
  background: radial-gradient(var(--blue), var(--lime-green));
}

/* Space taken by everything except the board (nav, score, gaps), so the board always fits */
.container {
  --reserved: 150px;
  box-sizing: border-box;
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
  gap: 12px;
  min-height: 100vh;
  min-height: 100dvh;
  padding: calc(64px + env(safe-area-inset-top, 0px)) 16px
    calc(16px + env(safe-area-inset-bottom, 0px));
  touch-action: none; /* swipes steer the snake instead of scrolling the page */
  -webkit-user-select: none;
  user-select: none;
}

.board {
  position: relative;
}

/* Natural size is 13px per cell, shrinking to fit smaller screens */
.grid-container {
  display: grid;
  gap: 1px;
  width: min(
    calc(var(--size, 30) * 13px),
    calc(100vw - 32px),
    calc(100vh - var(--reserved))
  );
  width: min(
    calc(var(--size, 30) * 13px),
    calc(100vw - 32px),
    calc(100dvh - var(--reserved))
  );
}

.grid-item {
  display: flex;
  justify-content: center;
  align-items: center;
  background-color: #fff;
  color: var(--blue);
  border: 1px solid transparent;
  min-width: 0;
  aspect-ratio: 1 / 1;
  font-family: courier;
  font-size: 10px;
}

.active {
  background-color: var(--red);
  color: var(--deep-blue);
}
.food {
  background-color: var(--blue);
  color: #fff;
}
.obstacle {
  background-color: white;
  opacity: 0.5;
}

.crash {
  background-color: var(--deep-blue);
  opacity: 1;
}
.shake {
  animation: shake 0.4s ease-in-out;
}
@keyframes shake {
  0%, 100% { transform: translateX(0); }
  20% { transform: translateX(-8px); }
  40% { transform: translateX(8px); }
  60% { transform: translateX(-5px); }
  80% { transform: translateX(5px); }
}
@media (prefers-reduced-motion: reduce) {
  .shake {
    animation: none;
  }
}
.message {
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  width: max-content;
  margin: 0;
  cursor: pointer;
  padding: 12px 20px;
  max-width: 80vw;
  background: #fff;
  color: var(--deep-blue);
  border-radius: 8px;
  font-family: sans-serif;
  font-size: 16px;
  text-align: center;
  box-shadow: 0 4px 16px rgba(0, 0, 0, 0.25);
}
.message[hidden] {
  display: none;
}

/* ==== Score ==== */
.scoreboard {
  display: flex;
  gap: 20px;
  margin: 0;
  padding: 6px 14px;
  border-radius: 2px;
  background: rgba(255, 255, 255, 0.85);
  color: var(--deep-blue);
  font: 15px/1.3 system-ui, -apple-system, 'Segoe UI', sans-serif;
}
.scoreboard strong {
  display: inline-block;
  min-width: 2ch;
  font-variant-numeric: tabular-nums;
}

/* ==== On-screen arrows: only on touch screens ==== */
.dpad {
  display: none;
}
@media (pointer: coarse) {
  .container {
    --reserved: 280px;
  }
  .dpad {
    display: grid;
    grid-template-columns: repeat(3, 44px);
    grid-template-rows: repeat(2, 44px);
    grid-template-areas:
      '. up .'
      'left down right';
    gap: 6px;
  }
}
.dpad__btn {
  display: grid;
  place-items: center;
  padding: 0;
  border: 0;
  border-radius: 4px;
  background: rgba(255, 255, 255, 0.4);
  color: var(--deep-blue);
  touch-action: manipulation;
  -webkit-tap-highlight-color: transparent;
}
.dpad__btn:active {
  background: rgba(255, 255, 255, 0.85);
}
/* Arrow drawn in CSS (text arrows show up as emoji on some phones); points up, rotated per button */
.dpad__btn::before {
  content: '';
  width: 14px;
  height: 10px;
  background: currentColor;
  clip-path: polygon(50% 0, 100% 100%, 0 100%);
  opacity: 0.75;
}
.dpad__btn--right::before {
  transform: rotate(90deg);
}
.dpad__btn--down::before {
  transform: rotate(180deg);
}
.dpad__btn--left::before {
  transform: rotate(270deg);
}
.dpad__btn--up {
  grid-area: up;
}
.dpad__btn--left {
  grid-area: left;
}
.dpad__btn--right {
  grid-area: right;
}
.dpad__btn--down {
  grid-area: down;
}

/* The nav wraps onto two lines on narrow screens */
@media (max-width: 560px) {
  .container {
    padding-top: calc(96px + env(safe-area-inset-top, 0px));
  }
}
