:root {
  font-family:
    Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI",
    sans-serif;
  color: #1f2933;
  background: #f4f6f8;
  --ink: #1e293b;
  --muted: #64748b;
  --card: #fffaf2;
  --card-border: #e7dfd2;
  --accent: #2563eb;
  --accent-strong: #1d4ed8;
  --rose: #fb7185;
}

* {
  box-sizing: border-box;
}

body {
  margin: 0;
  min-height: 100vh;
  display: grid;
  place-items: center;
  overflow-x: hidden;
}

body.red-alert {
  animation: cozy-hit-flash 360ms ease-out;
}

.app {
  width: min(92vw, 460px);
}

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

.hud h1 {
  margin: 0;
  font-size: clamp(1.55rem, 5vw, 2rem);
  letter-spacing: 0;
}

.hud p {
  margin: 0;
}

.hud-actions {
  display: flex;
  align-items: center;
  justify-content: flex-end;
  flex-wrap: wrap;
  gap: 10px;
}

.hud-button {
  min-width: 82px;
  padding: 6px 10px;
  font-size: 0.85rem;
}

.dedication {
  margin-top: 4px;
  color: #7b8794;
  font-size: 0.9rem;
}

.game-shell {
  position: relative;
  margin-top: 16px;
  transform-origin: center;
}

.game-shell.impact {
  animation: screen-shake 320ms cubic-bezier(0.36, 0.07, 0.19, 0.97);
}

.game-shell.game-over .grid {
  filter: saturate(0.8) brightness(0.88);
  transform: scale(0.99);
  transition:
    filter 320ms ease,
    transform 420ms ease;
}

.grid {
  display: grid;
  grid-template-columns: repeat(20, 1fr);
  width: 100%;
  aspect-ratio: 1;
  overflow: hidden;
  background: #ffffff;
  border: 1px solid #cbd2d9;
  border-radius: 12px;
  box-shadow: 0 18px 48px rgb(31 41 51 / 10%);
}

.grid:focus {
  outline: none;
  box-shadow:
    0 0 0 2px #9fb3c8,
    0 18px 48px rgb(31 41 51 / 10%);
}

.cell {
  border: 1px solid #eef2f5;
  transition:
    background-color 90ms ease,
    transform 90ms ease,
    box-shadow 90ms ease,
    opacity 180ms ease;
}

.cell.nina {
  background: #fce7f3;
}

.cell.snake {
  background: #2f855a;
  transform: scale(0.92);
  box-shadow: inset 0 0 0 1px rgb(255 255 255 / 24%);
}

.cell.snake-pop {
  animation: snake-pop 460ms var(--pop-delay, 0ms) ease-out both;
}

.cell.food {
  background: #c53030;
  animation: food-pulse 900ms ease-in-out infinite;
}

.overlay {
  position: absolute;
  inset: 0;
  z-index: 3;
  display: grid;
  place-content: center;
  padding: 24px;
  text-align: center;
  background: rgb(255 255 255 / 86%);
  border-radius: 12px;
  backdrop-filter: blur(2px);
}

.overlay-panel {
  display: grid;
  justify-items: center;
  gap: 12px;
  width: min(100%, 320px);
}

.overlay-title {
  margin: 0;
  font-size: clamp(1.6rem, 7vw, 2.4rem);
  font-weight: 800;
  letter-spacing: 0;
}

.overlay-detail {
  margin: 0;
  color: #52606d;
  line-height: 1.45;
}

.score-summary {
  margin: 0;
  color: #243b53;
  font-size: 0.95rem;
  font-weight: 700;
  line-height: 1.45;
  white-space: pre-line;
}

.screen-flash {
  position: absolute;
  inset: 0;
  z-index: 2;
  border-radius: 12px;
  pointer-events: none;
}

body.red-alert .screen-flash {
  animation: soft-screen-flash 280ms ease-out;
}

.game-over-overlay {
  overflow: hidden;
  background:
    radial-gradient(circle at 50% 44%, rgb(30 41 59 / 42%), transparent 42%),
    radial-gradient(circle at 50% 50%, transparent 48%, rgb(2 6 23 / 28%) 100%),
    rgb(15 23 42 / 78%);
  animation: overlay-dim 420ms ease-out both;
}

.game-over-overlay::before {
  display: none;
}

.game-over-overlay .overlay-panel {
  position: relative;
  width: min(100%, 328px);
  gap: 11px;
  padding: clamp(22px, 5vw, 26px);
  color: #f8fafc;
  background: var(--card);
  border: 1px solid rgb(255 255 255 / 80%);
  border-radius: 20px;
  box-shadow: 0 18px 42px rgb(2 6 23 / 26%);
  text-shadow: none;
  animation: card-arrive 460ms 80ms cubic-bezier(0.2, 1.08, 0.34, 1) both;
}

.game-over-overlay .overlay-title {
  color: var(--ink);
  font-size: clamp(2rem, 9vw, 3.05rem);
  line-height: 1;
  text-transform: none;
  text-shadow: none;
  animation: title-settle 460ms 160ms cubic-bezier(0.2, 1.08, 0.34, 1) both;
}

.game-over-overlay .overlay-detail {
  max-width: 240px;
  color: #475569;
  font-size: clamp(1rem, 3.4vw, 1.08rem);
  font-weight: 700;
}

.game-over-overlay .score-summary {
  display: grid;
  gap: 5px;
  width: 100%;
  margin-top: 4px;
  padding: 10px 0 0;
  color: var(--muted);
  background: transparent;
  border: 0;
  border-top: 1px solid var(--card-border);
  border-radius: 0;
  font-size: 0.9rem;
  font-weight: 700;
  white-space: normal;
}

.score-line {
  display: flex;
  align-items: baseline;
  justify-content: space-between;
  gap: 18px;
}

.score-value {
  color: var(--ink);
  font-size: 1.02rem;
  font-variant-numeric: tabular-nums;
}

.score-hint {
  margin-top: 5px;
  color: #94a3b8;
  font-size: 0.78rem;
  font-weight: 800;
  letter-spacing: 0;
  text-transform: uppercase;
}

.game-over-overlay #restart-button {
  min-width: 144px;
  margin-top: 6px;
  padding: 11px 20px;
  color: #ffffff;
  background: var(--accent);
  border: 0;
  border-radius: 999px;
  box-shadow: 0 8px 18px rgb(37 99 235 / 22%);
  font-weight: 800;
  transform: translateY(0) scale(1);
}

.game-over-overlay #restart-button:hover {
  transform: translateY(-2px) scale(1.02);
  background: var(--accent-strong);
  box-shadow: 0 10px 20px rgb(37 99 235 / 26%);
}

.game-over-overlay #restart-button:active {
  transform: translateY(1px) scale(0.98);
  box-shadow: 0 5px 12px rgb(37 99 235 / 20%);
}

.kostko {
  display: none;
}

.game-over-overlay .kostko {
  display: grid;
  justify-items: center;
  gap: 5px;
  margin-bottom: 4px;
  opacity: 0;
  animation:
    mascot-arrive 460ms 120ms cubic-bezier(0.2, 1.08, 0.34, 1) forwards,
    mascot-float 2400ms 800ms ease-in-out infinite;
}

.kostko-head {
  position: relative;
  width: clamp(86px, 22vw, 106px);
  aspect-ratio: 1 / 0.88;
  background: #fffdf7;
  border: 3px solid #334155;
  border-radius: 46% 46% 38% 38%;
  box-shadow:
    inset 0 -7px 0 #e0f2fe,
    0 10px 18px rgb(15 23 42 / 12%);
}

.kostko-head::before,
.kostko-head::after {
  position: absolute;
  bottom: -12px;
  width: 21%;
  height: 27%;
  content: "";
  background: #f8fafc;
  border-right: 3px solid #334155;
  border-bottom: 3px solid #334155;
  border-left: 3px solid #334155;
  border-radius: 0 0 7px 7px;
}

.kostko-head::before {
  left: 27%;
}

.kostko-head::after {
  right: 27%;
}

.kostko-eye {
  position: absolute;
  top: 33%;
  width: 20%;
  height: 19%;
  background:
    radial-gradient(circle at 62% 32%, #fff 0 18%, transparent 20%),
    radial-gradient(circle at 48% 58%, #fb7185 0 42%, #be123c 74%);
  border: 3px solid #334155;
  border-radius: 50%;
  box-shadow:
    0 0 10px rgb(248 113 113 / 42%),
    0 0 18px rgb(190 18 60 / 22%);
  animation: kostko-eye-pulse 1900ms ease-in-out infinite;
}

.kostko-eye-left {
  left: 23%;
}

.kostko-eye-right {
  right: 23%;
}

.kostko-nose {
  position: absolute;
  top: 54%;
  left: 50%;
  width: 11%;
  height: 14%;
  background: #334155;
  clip-path: polygon(50% 0, 100% 100%, 0 100%);
  transform: translateX(-50%);
}

.kostko-grin {
  position: absolute;
  right: 27%;
  bottom: 16%;
  left: 27%;
  height: 12%;
  border-bottom: 3px solid #334155;
  border-radius: 0 0 50% 50%;
}

.kostko-grin::before {
  position: absolute;
  top: 8px;
  left: 50%;
  width: 3px;
  height: 10px;
  content: "";
  background: #334155;
  box-shadow:
    -12px -1px 0 #334155,
    12px -1px 0 #334155;
}

.kostko-laughing {
  animation:
    mascot-arrive 1ms both,
    mascot-laugh-bounce 560ms cubic-bezier(0.2, 1.05, 0.35, 1) infinite;
}

.kostko-laughing .kostko-eye {
  animation: kostko-laugh-eye-pulse 620ms ease-in-out infinite;
}

.kostko-laugh-settle .kostko-eye {
  animation: kostko-eye-settle 720ms ease-out both;
}

.kostko-laugh {
  margin: 0;
  color: var(--muted);
  background: transparent;
  border: 0;
  border-radius: 0;
  font-size: 0.68rem;
  font-weight: 800;
  letter-spacing: 0;
  text-transform: uppercase;
}

.confetti-layer {
  position: absolute;
  inset: 0;
  z-index: 1;
  overflow: hidden;
  pointer-events: none;
}

.confetti {
  position: absolute;
  width: 7px;
  height: 10px;
  border-radius: 2px;
  animation: confetti-pop 720ms ease-out forwards;
}

.death-particle {
  position: absolute;
  border-radius: 999px;
  box-shadow: 0 0 10px rgb(255 255 255 / 28%);
  opacity: 0;
  animation: death-burst 620ms var(--delay, 0ms) cubic-bezier(0.18, 0.84, 0.32, 1) forwards;
}

.overlay.hidden {
  display: none;
}

button {
  border: 1px solid #9aa5b1;
  background: #fff;
  color: inherit;
  padding: 8px 12px;
  cursor: pointer;
  border-radius: 8px;
  transition:
    background-color 120ms ease,
    border-color 120ms ease,
    box-shadow 120ms ease,
    transform 150ms cubic-bezier(0.2, 0.9, 0.3, 1.2);
}

button:hover {
  border-color: #52606d;
  transform: translateY(-1px);
}

button:active {
  transform: translateY(1px) scale(0.98);
}

.controls {
  display: none;
  margin-top: 14px;
  text-align: center;
  touch-action: manipulation;
}

.controls-top {
  display: flex;
  justify-content: center;
  width: fit-content;
  margin: 0 auto 5px;
}

.dpad {
  display: grid;
  grid-template-columns: repeat(3, 74px);
  grid-template-rows: repeat(2, 66px);
  gap: 9px;
  justify-content: center;
  width: fit-content;
  margin: 0 auto;
}

.dpad-button {
  display: grid;
  place-items: center;
  min-width: 0;
  min-height: 0;
  width: 74px;
  height: 66px;
  color: #f8fafc;
  background:
    linear-gradient(180deg, rgb(51 65 85 / 96%), rgb(30 41 59 / 96%));
  border: 1px solid rgb(148 163 184 / 46%);
  border-radius: 18px;
  box-shadow:
    inset 0 1px 0 rgb(255 255 255 / 18%),
    inset 0 -2px 0 rgb(15 23 42 / 22%),
    0 7px 16px rgb(15 23 42 / 15%);
  font-size: 1.55rem;
  font-weight: 800;
  line-height: 1;
  transition:
    background-color 110ms ease,
    border-color 110ms ease,
    box-shadow 130ms ease,
    transform 120ms cubic-bezier(0.2, 0.9, 0.3, 1.2);
}

.dpad-button:hover {
  border-color: rgb(148 163 184 / 70%);
}

.dpad-button:active {
  background:
    linear-gradient(180deg, rgb(30 41 59 / 98%), rgb(15 23 42 / 98%));
  transform: translateY(2px) scale(0.965);
  box-shadow:
    inset 0 2px 7px rgb(15 23 42 / 38%),
    0 3px 8px rgb(15 23 42 / 12%);
}

.dpad-up {
  grid-column: 2;
  grid-row: 1;
}

.dpad-left {
  grid-column: 1;
  grid-row: 2;
}

.dpad-down {
  grid-column: 2;
  grid-row: 2;
}

.dpad-right {
  grid-column: 3;
  grid-row: 2;
}

#pause-button {
  min-width: 70px;
  min-height: 32px;
  padding: 6px 14px;
  color: #1e293b;
  background: rgb(255 255 255 / 88%);
  border-color: rgb(203 213 225 / 72%);
  border-radius: 999px;
  box-shadow:
    inset 0 1px 0 rgb(255 255 255 / 70%),
    0 4px 10px rgb(15 23 42 / 6%);
  font-size: 0.84rem;
  font-weight: 800;
}

.hint {
  color: #52606d;
  font-size: 0.95rem;
}

.nina-note {
  margin-top: 8px;
  color: #7b8794;
  font-size: 0.9rem;
}

@keyframes confetti-pop {
  0% {
    opacity: 1;
    transform: translate(0, 0) rotate(0deg) scale(1);
  }

  100% {
    opacity: 0;
    transform: translate(var(--x), var(--y)) rotate(var(--rotation)) scale(0.8);
  }
}

@keyframes death-burst {
  0% {
    opacity: 0;
    transform: translate(-50%, -50%) scale(0.45);
  }

  18% {
    opacity: 0.95;
  }

  100% {
    opacity: 0;
    transform: translate(calc(var(--x) - 50%), calc(var(--y) - 50%)) scale(0.78);
  }
}

@keyframes food-pulse {
  0%,
  100% {
    border-radius: 50%;
    transform: scale(0.72);
  }

  50% {
    border-radius: 35%;
    transform: scale(0.92);
  }
}

@keyframes screen-shake {
  0%,
  100% {
    transform: translate(0, 0) rotate(0deg);
  }

  18% {
    transform: translate(-3px, 1px) rotate(-0.25deg);
  }

  36% {
    transform: translate(3px, -1px) rotate(0.2deg);
  }

  56% {
    transform: translate(-2px, 1px) rotate(-0.12deg);
  }

  76% {
    transform: translate(1px, 0) rotate(0.08deg);
  }
}

@keyframes cozy-hit-flash {
  0%,
  100% {
    background: #f4f6f8;
  }

  22% {
    background: #e0f2fe;
  }
}

@keyframes soft-screen-flash {
  0% {
    background: rgb(255 255 255 / 0%);
  }

  25% {
    background: rgb(224 242 254 / 34%);
  }

  100% {
    background: rgb(255 255 255 / 0%);
  }
}

@keyframes snake-pop {
  0% {
    opacity: 1;
    transform: scale(0.92);
  }

  45% {
    opacity: 1;
    transform: scale(1.08) rotate(3deg);
  }

  100% {
    opacity: 0.42;
    transform: scale(0.48) rotate(7deg);
  }
}

@keyframes overlay-dim {
  0% {
    opacity: 0;
    backdrop-filter: blur(0);
  }

  100% {
    opacity: 1;
    backdrop-filter: blur(4px);
  }
}

@keyframes card-arrive {
  0% {
    opacity: 0;
    transform: translateY(10px) scale(0.97);
  }

  100% {
    opacity: 1;
    transform: translateY(0) scale(1);
  }
}

@keyframes title-settle {
  0% {
    opacity: 0;
    transform: translateY(5px);
  }

  100% {
    opacity: 1;
    transform: translateY(0) scale(1);
  }
}

@keyframes mascot-arrive {
  0% {
    opacity: 0;
    transform: translateY(10px) scale(0.94);
  }

  100% {
    opacity: 1;
    transform: translateY(0) scale(1);
  }
}

@keyframes mascot-float {
  0%,
  100% {
    transform: translateY(0);
  }

  50% {
    transform: translateY(-4px);
  }
}

@keyframes mascot-laugh-bounce {
  0%,
  100% {
    transform: translateY(0);
  }

  45% {
    transform: translateY(-3px);
  }
}

@keyframes kostko-eye-pulse {
  0%,
  100% {
    transform: scale(0.95);
    box-shadow:
      0 0 9px rgb(248 113 113 / 34%),
      0 0 16px rgb(190 18 60 / 18%);
  }

  50% {
    transform: scale(1.06);
    box-shadow:
      0 0 13px rgb(248 113 113 / 52%),
      0 0 22px rgb(190 18 60 / 26%);
  }
}

@keyframes kostko-laugh-eye-pulse {
  0%,
  100% {
    transform: scale(1.02);
    box-shadow:
      0 0 12px rgb(248 113 113 / 48%),
      0 0 22px rgb(190 18 60 / 24%);
  }

  50% {
    transform: scale(1.12);
    box-shadow:
      0 0 16px rgb(248 113 113 / 66%),
      0 0 28px rgb(190 18 60 / 34%);
  }
}

@keyframes kostko-eye-settle {
  0% {
    transform: scale(1.1);
    box-shadow:
      0 0 16px rgb(248 113 113 / 62%),
      0 0 28px rgb(190 18 60 / 32%);
  }

  100% {
    transform: scale(0.95);
    box-shadow:
      0 0 9px rgb(248 113 113 / 34%),
      0 0 16px rgb(190 18 60 / 18%);
  }
}

@media (pointer: coarse) {
  .controls {
    display: block;
  }
}

@media (max-width: 520px) {
  body {
    align-items: start;
    padding: 12px 0 16px;
  }

  .app {
    width: min(94vw, 430px);
  }

  .hud {
    align-items: flex-start;
  }

  .hud-actions {
    align-items: flex-end;
    flex-direction: column;
    gap: 6px;
  }

  .overlay {
    padding: 14px;
  }

  .game-over-overlay .overlay-panel {
    gap: 10px;
    padding: 18px;
    border-radius: 18px;
  }

  .game-over-overlay .score-summary {
    font-size: 0.86rem;
  }

  .game-over-overlay #restart-button {
    margin-top: 4px;
  }

  .hint {
    margin: 10px 0 0;
    text-align: center;
  }
}

@media (pointer: coarse) and (max-width: 520px) {
  .game-shell {
    margin-top: 10px;
  }

  .controls {
    margin-top: clamp(10px, 3vw, 15px);
  }

  .controls-top {
    margin-bottom: 5px;
  }

  .dpad {
    grid-template-columns: repeat(3, minmax(62px, 70px));
    grid-template-rows: repeat(2, 62px);
    gap: 8px;
  }

  .dpad-button {
    width: 100%;
    height: 62px;
    border-radius: 16px;
  }
}

@media (pointer: coarse) and (max-width: 380px) {
  body {
    padding-top: 10px;
  }

  .app {
    width: min(95vw, 360px);
  }

  .hud {
    gap: 10px;
  }

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

  .dedication,
  .hud-actions,
  .hint {
    font-size: 0.84rem;
  }

  .hud-button {
    min-width: 76px;
    padding: 5px 9px;
  }

  .dpad {
    grid-template-columns: repeat(3, minmax(56px, 64px));
    grid-template-rows: repeat(2, 56px);
    gap: 7px;
  }

  .dpad-button {
    height: 56px;
    border-radius: 15px;
    font-size: 1.42rem;
  }

  #pause-button {
    min-height: 31px;
    padding: 6px 13px;
  }

  .game-over-overlay .overlay-panel {
    gap: 9px;
    padding: 16px;
  }

  .game-over-overlay .kostko {
    margin-bottom: 2px;
  }

  .game-over-overlay .overlay-detail {
    font-size: 0.96rem;
  }
}

@media (prefers-reduced-motion: reduce) {
  .cell,
  button,
  .game-shell.game-over .grid {
    transition: none;
  }

  .cell.food {
    animation: none;
    transform: scale(0.85);
  }

  .confetti {
    animation-duration: 1ms;
  }

  body.red-alert,
  body.red-alert .screen-flash,
  .game-shell.impact,
  .cell.snake-pop,
  .game-over-overlay,
  .game-over-overlay .overlay-panel,
  .game-over-overlay .overlay-title,
  .game-over-overlay .kostko,
  .kostko-eye,
  .kostko-laughing,
  .kostko-laughing .kostko-eye,
  .kostko-laugh-settle .kostko-eye {
    animation-duration: 1ms;
    animation-iteration-count: 1;
  }
}
