:root {
  --bg: #10131a;
  --panel: #171b24;
  --grid-a: #1c212c;
  --grid-b: #191e28;
  --line: #262c39;
  --text: #e6e9f0;
  --muted: #8b93a5;
  --snake: #4ade80;
  --snake-head: #86efac;
  --food: #f87171;
  --accent: #4ade80;
}

* {
  box-sizing: border-box;
}

html,
body {
  height: 100%;
}

body {
  margin: 0;
  min-height: 100vh;
  min-height: 100dvh; /* survives the mobile URL bar sliding in and out */
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 16px;
  padding-left: max(16px, env(safe-area-inset-left));
  padding-right: max(16px, env(safe-area-inset-right));
  padding-bottom: max(16px, env(safe-area-inset-bottom));
  background: radial-gradient(circle at 50% 0%, #1a2030 0%, var(--bg) 60%);
  color: var(--text);
  font-family: ui-monospace, SFMono-Regular, "SF Mono", Menlo, Consolas, monospace;
  -webkit-user-select: none;
  user-select: none;
  overscroll-behavior: none;
  touch-action: manipulation; /* no double-tap zoom on rapid taps */
  -webkit-tap-highlight-color: transparent;
}

.game {
  width: min(92vmin, 620px);
  display: flex;
  flex-direction: column;
  gap: 14px;
}

/* ---- HUD ---- */

.hud {
  display: grid;
  grid-template-columns: 1fr auto 1fr;
  align-items: center;
  gap: 12px;
}

.title {
  margin: 0;
  font-size: clamp(18px, 3.2vmin, 24px);
  letter-spacing: 0.4em;
  text-indent: 0.4em;
  font-weight: 700;
  color: var(--text);
}

.stat {
  display: flex;
  flex-direction: column;
  gap: 2px;
  line-height: 1;
}

.stat-right {
  align-items: flex-end;
}

.stat-label {
  font-size: 11px;
  letter-spacing: 0.18em;
  text-transform: uppercase;
  color: var(--muted);
}

.stat-value {
  font-size: clamp(20px, 4vmin, 28px);
  font-weight: 700;
  font-variant-numeric: tabular-nums;
}

/* ---- Board ---- */

.board {
  position: relative;
  aspect-ratio: 1 / 1;
  border: 1px solid var(--line);
  border-radius: 12px;
  background: var(--panel);
  overflow: hidden;
  box-shadow: 0 20px 60px rgba(0, 0, 0, 0.45);
}

canvas {
  display: block;
  width: 100%;
  height: 100%;
  touch-action: none;
}

/* ---- Overlay ---- */

.overlay {
  position: absolute;
  inset: 0;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 10px;
  padding: 24px;
  text-align: center;
  background: rgba(16, 19, 26, 0.82);
  backdrop-filter: blur(3px);
  transition: opacity 0.18s ease;
}

.overlay[hidden] {
  display: none;
}

.overlay-title {
  margin: 0;
  font-size: clamp(24px, 5vmin, 34px);
  letter-spacing: 0.12em;
}

.overlay-text {
  margin: 0;
  max-width: 34ch;
  font-size: 13px;
  line-height: 1.6;
  color: var(--muted);
}

.btn {
  margin-top: 8px;
  padding: 11px 30px;
  border: 0;
  border-radius: 999px;
  background: var(--accent);
  color: #0b1220;
  font-family: inherit;
  font-size: 14px;
  font-weight: 700;
  letter-spacing: 0.1em;
  text-transform: uppercase;
  cursor: pointer;
  transition: transform 0.12s ease, filter 0.12s ease;
}

.btn:hover {
  filter: brightness(1.08);
}

.btn:active {
  transform: translateY(1px);
}

.btn:focus-visible {
  outline: 2px solid var(--text);
  outline-offset: 3px;
}

/* ---- Controls ---- */

.controls {
  display: flex;
  justify-content: center;
  gap: 10px;
}

.ctrl {
  min-width: 108px;
  min-height: 44px; /* comfortable touch target */
  padding: 0 20px;
  border: 1px solid var(--line);
  border-radius: 999px;
  background: var(--panel);
  color: var(--text);
  font-family: inherit;
  font-size: 12px;
  font-weight: 700;
  letter-spacing: 0.12em;
  text-transform: uppercase;
  cursor: pointer;
  transition: border-color 0.12s ease, color 0.12s ease, transform 0.12s ease;
}

.ctrl:hover:not(:disabled) {
  border-color: var(--accent);
  color: var(--accent);
}

.ctrl:active:not(:disabled) {
  transform: translateY(1px);
}

.ctrl:disabled {
  opacity: 0.4;
  cursor: default;
}

.ctrl:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 3px;
}

/* ---- Help ---- */

.help {
  display: flex;
  flex-wrap: wrap;
  justify-content: center;
  gap: 8px 20px;
  font-size: 11px;
  color: var(--muted);
}

.help-touch {
  display: none;
}

/* Touch devices: the keyboard legend is meaningless, the swipe hint is not. */
@media (pointer: coarse) {
  .help-keys {
    display: none;
  }

  .help-touch {
    display: inline;
  }
}

kbd {
  display: inline-block;
  min-width: 20px;
  padding: 2px 5px;
  border: 1px solid var(--line);
  border-bottom-width: 2px;
  border-radius: 4px;
  background: var(--panel);
  color: var(--text);
  font-family: inherit;
  font-size: 10px;
  text-align: center;
}

@media (max-width: 400px) {
  .ctrl {
    min-width: 0;
    flex: 1;
  }
}
