:root {
  --grid-columns: 14;
  --grid-rows: 14;
  --background: #f7f8fb;
  --surface: #ffffff;
  --text: #2a374e;
  --text-muted: #56637a;
  --primary: #e1251b;
  --primary-dark: #ad1711;
  --primary-soft: #fbe3e1;
  --secondary-soft: #e8edf4;
  --border: #c5ccd8;
  --shadow: 0 14px 35px rgba(42, 55, 78, 0.14);
  --safe-top: env(safe-area-inset-top, 0px);
  --safe-bottom: env(safe-area-inset-bottom, 0px);
}

* {
  box-sizing: border-box;
  -webkit-tap-highlight-color: transparent;
}

html {
  min-height: 100%;
  background: var(--background);
}

body {
  min-width: 280px;
  min-height: 100vh;
  min-height: 100dvh;
  margin: 0;
  color: var(--text);
  background:
    radial-gradient(circle at 5% 5%, rgba(225, 37, 27, 0.06), transparent 28%),
    radial-gradient(circle at 95% 90%, rgba(42, 55, 78, 0.08), transparent 28%),
    var(--background);
  font-family:
    Inter, ui-rounded, "SF Pro Rounded", "Segoe UI", Roboto, Helvetica, Arial,
    sans-serif;
  font-size: 20px;
  line-height: 1.4;
  -webkit-font-smoothing: antialiased;
  overscroll-behavior: none;
}

button {
  font: inherit;
  user-select: none;
  -webkit-user-select: none;
  cursor: pointer;
}

.game-shell {
  width: 100%;
  min-height: 100vh;
  min-height: 100dvh;
  margin: 0 auto;
  padding:
    max(18px, var(--safe-top))
    max(18px, env(safe-area-inset-right, 0px))
    max(28px, var(--safe-bottom))
    max(18px, env(safe-area-inset-left, 0px));
}

.top-bar,
.brand,
.game-actions,
.sound-button,
.pause-button,
.game-instructions {
  display: flex;
  align-items: center;
}

.top-bar {
  justify-content: space-between;
  gap: 14px;
  margin-bottom: 18px;
}

.brand {
  gap: 12px;
}

.game-actions {
  flex: 0 0 auto;
  gap: 8px;
}

.brand-mark {
  display: grid;
  width: 52px;
  height: 52px;
  flex: 0 0 auto;
  place-items: center;
  border-radius: 14px;
  color: #ffffff;
  background: var(--primary);
  box-shadow: 0 8px 18px rgba(225, 37, 27, 0.24);
  font-family: Georgia, serif;
  font-size: 29px;
  font-weight: 800;
  font-style: italic;
}

.eyebrow {
  margin: 0 0 3px;
  color: var(--primary);
  font-size: 12px;
  font-weight: 900;
  letter-spacing: 0.13em;
}

h1,
h2,
p {
  margin-top: 0;
}

h1 {
  margin-bottom: 0;
  font-family: Georgia, "Times New Roman", serif;
  font-size: clamp(28px, 7vw, 36px);
  line-height: 1;
  letter-spacing: -0.03em;
}

.pause-button {
  min-height: 52px;
  padding: 0 15px;
  justify-content: center;
  gap: 8px;
  border: 2px solid var(--primary);
  border-radius: 14px;
  color: var(--primary);
  background: var(--surface);
  font-size: 16px;
  font-weight: 850;
}

.sound-button {
  width: 52px;
  height: 52px;
  padding: 0;
  justify-content: center;
  border: 2px solid var(--text);
  border-radius: 14px;
  color: var(--text);
  background: var(--surface);
}

.sound-button.is-muted {
  border-color: var(--border);
  color: #788398;
}

.sound-button svg {
  width: 27px;
  fill: none;
  stroke: currentColor;
  stroke-width: 1.9;
  stroke-linecap: round;
  stroke-linejoin: round;
}

.sound-button .sound-slash {
  display: none;
}

.sound-button.is-muted .sound-waves {
  display: none;
}

.sound-button.is-muted .sound-slash {
  display: block;
}

.pause-button:disabled {
  border-color: var(--border);
  color: #8a94a5;
  cursor: default;
  opacity: 0.75;
}

.pause-button svg {
  width: 23px;
  fill: none;
  stroke: currentColor;
  stroke-width: 2;
  stroke-linecap: round;
}

.score-card {
  display: grid;
  grid-template-columns: 1fr auto 1fr;
  align-items: center;
  margin-bottom: 18px;
  padding: 15px 18px;
  border: 2px solid rgba(225, 37, 27, 0.24);
  border-radius: 18px;
  background: rgba(255, 255, 255, 0.84);
  box-shadow: 0 8px 24px rgba(42, 55, 78, 0.07);
}

.score-item {
  display: flex;
  align-items: center;
  justify-content: center;
  flex-direction: column;
}

.score-item span {
  color: var(--text-muted);
  font-size: 13px;
  font-weight: 900;
  letter-spacing: 0.1em;
}

.score-item strong {
  color: var(--primary);
  font-size: 30px;
  font-variant-numeric: tabular-nums;
  line-height: 1.15;
}

.score-divider {
  width: 2px;
  height: 42px;
  background: var(--border);
}

.game-instructions {
  gap: 13px;
  margin-bottom: 16px;
  padding: 14px 16px;
  border: 2px solid #aeb7c6;
  border-radius: 16px;
  background: #f1f4f8;
}

.instruction-icon {
  display: grid;
  width: 48px;
  height: 48px;
  flex: 0 0 auto;
  place-items: center;
  border-radius: 12px;
  color: var(--primary);
  background: var(--primary-soft);
}

.instruction-icon svg {
  width: 29px;
  fill: currentColor;
}

.game-instructions p {
  margin: 0;
  font-size: 18px;
  line-height: 1.35;
}

.game-instructions strong {
  display: block;
  margin-bottom: 2px;
  font-size: 20px;
}

.board-wrapper {
  position: relative;
  width: min(100%, 520px);
  margin: 0 auto;
  overflow: hidden;
  scroll-margin-top: max(10px, var(--safe-top));
  border: 3px solid var(--primary);
  border-radius: 22px;
  background: var(--surface);
  box-shadow: var(--shadow);
  touch-action: none;
  -webkit-touch-callout: none;
}

.mobile-score-badge {
  display: none;
}

.snake-board {
  display: grid;
  width: 100%;
  aspect-ratio: var(--grid-columns) / var(--grid-rows);
  padding: 0;
  background-image:
    linear-gradient(rgba(42, 55, 78, 0.28) 1px, transparent 1px),
    linear-gradient(90deg, rgba(42, 55, 78, 0.28) 1px, transparent 1px);
  background-size:
    calc(100% / var(--grid-columns))
    calc(100% / var(--grid-rows));
}

.board-cell {
  position: relative;
  min-width: 0;
  min-height: 0;
}

.snake-layer {
  position: absolute;
  z-index: 2;
  inset: 0;
  display: block;
  width: 100%;
  height: 100%;
  overflow: hidden;
  pointer-events: none;
  contain: layout paint style;
  shape-rendering: geometricPrecision;
}

.board-cell::before {
  position: absolute;
  inset: 10%;
  border-radius: 42%;
  content: "";
  transform: scale(0);
}

.snake-shadow,
.snake-body {
  fill: none;
  stroke-linecap: round;
  stroke-linejoin: round;
}

.snake-shadow {
  stroke: rgba(225, 37, 27, 0.2);
  stroke-width: 1.02;
  transform: translateY(0.08px);
}

.snake-body {
  stroke: var(--text);
  stroke-width: 0.82;
}

.snake-head {
  filter: drop-shadow(0 0.08px 0.12px rgba(42, 55, 78, 0.38));
}

.snake-eye {
  fill: #ffffff;
  stroke: var(--text);
  stroke-width: 0.045;
}

.snake-pupil {
  fill: var(--text);
}

.board-cell.is-food::before {
  inset: 25% 12%;
  border: 2px solid var(--text);
  border-radius: 999px;
  background: linear-gradient(
    90deg,
    var(--primary) 0 48%,
    #ffffff 48% 100%
  );
  box-shadow:
    0 0 0 3px rgba(225, 37, 27, 0.1),
    0 3px 8px rgba(225, 37, 27, 0.28);
  transform: rotate(-32deg) scale(1);
  animation: capsule-pulse 800ms ease-in-out infinite alternate;
}

@keyframes capsule-pulse {
  to {
    filter: brightness(1.1);
    transform: rotate(-32deg) scale(1);
  }
}

.game-overlay {
  position: absolute;
  z-index: 5;
  inset: 0;
  display: grid;
  padding: 18px;
  overflow-y: auto;
  align-items: safe center;
  justify-items: center;
  visibility: hidden;
  background: rgba(247, 248, 251, 0.93);
  opacity: 0;
  overscroll-behavior: contain;
  touch-action: pan-y;
  transition:
    opacity 180ms ease,
    visibility 180ms;
}

.game-overlay.is-visible {
  visibility: visible;
  opacity: 1;
}

.overlay-card {
  width: min(100%, 370px);
  text-align: center;
}

.overlay-icon {
  position: relative;
  width: 64px;
  height: 64px;
  margin: 0 auto 17px;
  border-radius: 20px;
  background: var(--primary-soft);
  box-shadow: 0 10px 22px rgba(42, 55, 78, 0.16);
}

.overlay-icon::before {
  position: absolute;
  width: 43px;
  height: 23px;
  top: 20px;
  left: 10px;
  border: 3px solid var(--text);
  border-radius: 999px;
  background: linear-gradient(
    90deg,
    var(--primary) 0 48%,
    #ffffff 48% 100%
  );
  box-shadow: 0 4px 8px rgba(42, 55, 78, 0.18);
  content: "";
  transform: rotate(-32deg);
}

.overlay-card h2 {
  margin-bottom: 10px;
  font-family: Georgia, "Times New Roman", serif;
  font-size: clamp(27px, 7vw, 34px);
  line-height: 1.1;
}

.overlay-card > p:not(.eyebrow) {
  margin-bottom: 20px;
  color: var(--text-muted);
  font-size: 18px;
}

.primary-button {
  width: 100%;
  min-height: 58px;
  padding: 14px 20px;
  border: 0;
  border-radius: 16px;
  color: #ffffff;
  background: var(--primary);
  box-shadow: 0 10px 22px rgba(225, 37, 27, 0.24);
  font-size: 20px;
  font-weight: 900;
}

.controls-section {
  position: fixed;
  z-index: 15;
  left: 50%;
  bottom: max(10px, var(--safe-bottom));
  width: min(calc(100% - 24px), 390px);
  margin: 0;
  padding: 10px;
  border: 2px solid rgba(42, 55, 78, 0.2);
  border-radius: 22px;
  background: rgba(247, 248, 251, 0.96);
  box-shadow: 0 10px 28px rgba(42, 55, 78, 0.24);
  opacity: 0;
  pointer-events: none;
  text-align: center;
  transform: translate(-50%, calc(100% + 28px));
  transition:
    opacity 180ms ease,
    transform 180ms ease;
  visibility: hidden;
}

body.is-playing .controls-section {
  opacity: 1;
  pointer-events: auto;
  transform: translate(-50%, 0);
  visibility: visible;
}

body.is-playing .game-shell {
  padding-bottom: calc(102px + var(--safe-bottom));
}

.controls-section > p {
  position: absolute;
  width: 1px;
  height: 1px;
  overflow: hidden;
  clip: rect(0 0 0 0);
}

.direction-pad {
  display: grid;
  width: 100%;
  margin: 0 auto;
  grid-template-columns: repeat(4, minmax(56px, 1fr));
  grid-template-rows: 62px;
  gap: 9px;
}

.direction-button {
  display: grid;
  min-width: 56px;
  min-height: 62px;
  padding: 0;
  place-items: center;
  border: 2px solid var(--text);
  border-radius: 18px;
  color: #ffffff;
  background: var(--text);
  box-shadow: 0 7px 15px rgba(42, 55, 78, 0.18);
}

.direction-button:active {
  background: var(--primary);
  transform: scale(0.95);
}

.direction-button svg {
  width: 32px;
  fill: none;
  stroke: currentColor;
  stroke-width: 2.5;
  stroke-linecap: round;
  stroke-linejoin: round;
}

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

.direction-left {
  grid-column: 1;
  grid-row: 1;
}

.direction-right {
  grid-column: 4;
  grid-row: 1;
}

.direction-down {
  grid-column: 3;
  grid-row: 1;
}

.toast {
  position: fixed;
  z-index: 20;
  top: max(16px, var(--safe-top));
  left: 50%;
  display: flex;
  width: min(calc(100% - 28px), 520px);
  min-height: 54px;
  padding: 12px 18px;
  align-items: center;
  justify-content: center;
  border-radius: 999px;
  color: #ffffff;
  background: var(--text);
  box-shadow: 0 8px 25px rgba(42, 55, 78, 0.25);
  font-size: 19px;
  font-weight: 800;
  line-height: 1.25;
  opacity: 0;
  pointer-events: none;
  text-align: center;
  transform: translate(-50%, -16px);
  transition:
    opacity 180ms ease,
    transform 180ms ease;
}

.toast.is-visible {
  opacity: 1;
  transform: translate(-50%, 0);
}

button:focus-visible {
  outline: 3px solid rgba(42, 55, 78, 0.65);
  outline-offset: 3px;
}

@media (max-width: 899px) {
  body.is-playing {
    overflow: hidden;
  }

  body.is-playing .game-shell {
    display: grid;
    height: 100vh;
    height: 100dvh;
    min-height: 0;
    grid-template-columns: minmax(0, 1fr) auto;
    grid-template-rows: auto minmax(0, 1fr);
    column-gap: 8px;
    row-gap: 16px;
    padding:
      max(10px, var(--safe-top))
      max(8px, env(safe-area-inset-right, 0px))
      calc(102px + var(--safe-bottom))
      max(8px, env(safe-area-inset-left, 0px));
    align-items: start;
  }

  body.is-playing .brand,
  body.is-playing .score-card,
  body.is-playing .game-instructions {
    display: none;
  }

  body.is-playing .top-bar {
    position: static;
    z-index: 14;
    width: auto;
    margin: 0;
    align-self: start;
    grid-column: 2;
    grid-row: 1;
    justify-self: end;
    pointer-events: none;
  }

  body.is-playing .game-actions {
    pointer-events: auto;
  }

  body.is-playing .pause-button {
    width: 52px;
    padding: 0;
  }

  body.is-playing .pause-button span {
    position: absolute;
    width: 1px;
    height: 1px;
    overflow: hidden;
    clip: rect(0 0 0 0);
  }

  body.is-playing .board-wrapper {
    width: min(calc(100vw - 16px), var(--mobile-board-width, 100%));
    margin: 0 auto;
    align-self: center;
    grid-column: 1 / -1;
    grid-row: 2;
    justify-self: center;
  }

  body.is-playing .mobile-score-badge {
    position: static;
    z-index: 14;
    display: flex;
    min-width: 82px;
    min-height: 50px;
    padding: 5px 12px;
    align-items: center;
    justify-content: center;
    flex-direction: column;
    border: 2px solid rgba(225, 37, 27, 0.32);
    border-radius: 14px;
    background: rgba(255, 255, 255, 0.92);
    box-shadow: 0 5px 15px rgba(42, 55, 78, 0.16);
    align-self: start;
    grid-column: 1;
    grid-row: 1;
    justify-self: start;
    pointer-events: none;
  }

  .mobile-score-badge span {
    color: var(--text-muted);
    font-size: 11px;
    font-weight: 900;
    letter-spacing: 0.08em;
    line-height: 1;
  }

  .mobile-score-badge strong {
    color: var(--primary);
    font-size: 25px;
    font-variant-numeric: tabular-nums;
    line-height: 1.1;
  }
}

@media (min-width: 900px) {
  .game-shell {
    display: grid;
    padding:
      max(24px, var(--safe-top))
      max(24px, env(safe-area-inset-right, 0px))
      max(24px, var(--safe-bottom))
      max(24px, env(safe-area-inset-left, 0px));
    align-content: start;
    column-gap: clamp(24px, 3vw, 44px);
    grid-template-areas:
      "header header"
      "score board"
      "instructions board";
    grid-template-columns: minmax(300px, 0.72fr) minmax(500px, 1.28fr);
    grid-template-rows: auto auto 1fr;
    row-gap: 18px;
  }

  .top-bar {
    grid-area: header;
    margin-bottom: 0;
  }

  .score-card {
    grid-area: score;
    margin-bottom: 0;
  }

  .game-instructions {
    grid-area: instructions;
    align-self: start;
    margin-bottom: 0;
  }

  .board-wrapper {
    width: min(100%, 860px, calc(100dvh - 122px));
    min-width: 380px;
    align-self: start;
    grid-area: board;
  }

  .controls-section {
    display: none;
  }

  body.is-playing .game-shell {
    padding-bottom: max(24px, var(--safe-bottom));
  }
}

@media (min-width: 1200px) {
  .game-shell {
    grid-template-areas:
      "header board"
      "score board"
      "instructions board";
  }

  .board-wrapper {
    width: min(100%, var(--desktop-board-width, 1200px));
  }
}

@media (max-width: 600px) {
  body:not(.is-playing) .top-bar {
    align-items: stretch;
    flex-direction: column;
    gap: 10px;
  }

  body:not(.is-playing) .brand,
  body:not(.is-playing) .brand > div {
    min-width: 0;
  }

  body:not(.is-playing) .game-actions {
    align-self: flex-end;
  }
}

@media (max-width: 430px) {
  .pause-button {
    width: 52px;
    padding: 0;
  }

  .pause-button span {
    position: absolute;
    width: 1px;
    height: 1px;
    overflow: hidden;
    clip: rect(0 0 0 0);
  }
}

@media (max-width: 340px) {
  .game-shell {
    padding-inline: 12px;
  }

  .controls-section {
    width: calc(100% - 12px);
    padding: 6px;
  }

  .direction-pad {
    grid-template-columns: repeat(4, minmax(48px, 1fr));
    gap: 5px;
  }

  .direction-button {
    min-width: 48px;
    min-height: 58px;
  }

  .top-bar {
    gap: 8px;
  }

  .game-actions {
    gap: 5px;
  }

  .sound-button,
  .pause-button {
    width: 46px;
    height: 46px;
    min-height: 46px;
  }

  .brand {
    gap: 8px;
  }

  .brand-mark {
    width: 42px;
    height: 42px;
    font-size: 25px;
  }

  h1 {
    font-size: 25px;
  }

  .brand .eyebrow {
    max-width: 100px;
    font-size: 9px;
    line-height: 1.1;
    white-space: normal;
  }

  .game-instructions p {
    font-size: 17px;
  }

  .game-overlay {
    padding: 8px;
  }

  .overlay-icon {
    width: 48px;
    height: 48px;
    margin-bottom: 7px;
    border-radius: 15px;
  }

  .overlay-icon::before {
    width: 34px;
    height: 19px;
    top: 14px;
    left: 7px;
    border-width: 2px;
  }

  .overlay-card .eyebrow {
    margin-bottom: 1px;
    font-size: 10px;
  }

  .overlay-card h2 {
    margin-bottom: 6px;
    font-size: 24px;
    line-height: 1;
  }

  .overlay-card > p:not(.eyebrow) {
    margin-bottom: 9px;
    font-size: 16px;
    line-height: 1.25;
  }

  .primary-button {
    min-height: 48px;
    padding: 9px 12px;
    font-size: 17px;
  }
}

@media (prefers-reduced-motion: reduce) {
  *,
  *::before,
  *::after {
    transition-duration: 0.01ms !important;
    animation-duration: 0.01ms !important;
    animation-iteration-count: 1 !important;
  }
}

@media (prefers-contrast: more) {
  :root {
    --text: #111a2b;
    --text-muted: #35425a;
    --primary: #b11912;
    --primary-dark: #85100b;
    --border: #606b7e;
  }
}
