/* --- Reset & base --- */
*,
*::before,
*::after {
  box-sizing: border-box;
  margin: 0;
  padding: 0;
}

:root {
  --bg: #0f1419;
  --surface: #1a2332;
  --surface-elevated: #243044;
  --border: #2d3a4f;
  --text: #e8edf4;
  --text-muted: #8b9cb3;
  --accent: #4ade80;
  --accent-dim: #22c55e;
  --accent-glow: rgba(74, 222, 128, 0.35);
  --danger: #f87171;
  --food: #fb923c;
  --snake-head: #4ade80;
  --snake-body: #34d399;
  --shadow: 0 8px 32px rgba(0, 0, 0, 0.45);
  --radius: 16px;
  --radius-sm: 10px;
  --font: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  --touch-min: 48px;
}

html {
  height: 100%;
  -webkit-text-size-adjust: 100%;
}

body {
  min-height: 100%;
  min-height: 100dvh;
  font-family: var(--font);
  font-size: 1rem;
  line-height: 1.5;
  color: var(--text);
  background: var(--bg);
  background-image: radial-gradient(ellipse 120% 80% at 50% -20%, #1e3a2f 0%, transparent 55%);
  overflow-x: hidden;
}

body.playing {
  overflow: hidden;
  touch-action: none;
  overscroll-behavior: none;
}

/* --- Layout --- */
.app {
  display: flex;
  flex-direction: column;
  align-items: center;
  width: 100%;
  max-width: 480px;
  min-height: 100dvh;
  margin: 0 auto;
  padding: 0.75rem 1rem 1.25rem;
  gap: 0.75rem;
}

.header {
  width: 100%;
  text-align: center;
}

.title {
  font-size: clamp(1.75rem, 6vw, 2.25rem);
  font-weight: 700;
  letter-spacing: 0.04em;
  background: linear-gradient(135deg, var(--accent) 0%, #86efac 100%);
  -webkit-background-clip: text;
  background-clip: text;
  color: transparent;
  margin-bottom: 0.5rem;
}

.version-tag {
  font-size: 0.45em;
  font-weight: 600;
  letter-spacing: 0.02em;
  opacity: 0.65;
  -webkit-text-fill-color: var(--text-muted);
  color: var(--text-muted);
}

.scores {
  display: flex;
  flex-wrap: wrap;
  justify-content: center;
  align-items: flex-start;
  gap: 1.25rem 1.75rem;
  max-width: 100%;
}

.score-block {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 0.15rem;
}

.score-label {
  font-size: 0.8rem;
  text-transform: uppercase;
  letter-spacing: 0.08em;
  color: var(--text-muted);
}

.score-value {
  font-size: clamp(1.5rem, 5vw, 1.85rem);
  font-weight: 700;
  font-variant-numeric: tabular-nums;
  color: var(--text);
}

.game-area {
  display: flex;
  flex-direction: column;
  align-items: center;
  width: 100%;
  flex: 1;
  gap: 0.5rem;
}

/* Board + controls grouped for mobile thumb reach */
.play-stack {
  display: flex;
  flex-direction: column;
  align-items: stretch;
  width: 100%;
  max-width: min(100%, 400px);
  gap: 0.5rem;
}

.controls-row,
.canvas-wrap,
.dpad-shell {
  width: 100%;
}

/* --- Canvas --- */
.canvas-wrap {
  position: relative;
  width: 100%;
  aspect-ratio: 1;
  border-radius: var(--radius);
  box-shadow: var(--shadow), 0 0 0 1px var(--border);
  overflow: hidden;
  background: var(--surface);
  touch-action: none;
}

#game-canvas {
  display: block;
  width: 100%;
  height: 100%;
}

/* --- Overlays --- */
.overlay {
  position: absolute;
  inset: 0;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 0.5rem;
  background: rgba(15, 20, 25, 0.82);
  backdrop-filter: blur(6px);
  -webkit-backdrop-filter: blur(6px);
  animation: fadeIn 0.25s ease;
  z-index: 2;
  padding: 1rem;
  text-align: center;
}

.overlay[hidden] {
  display: none !important;
}

.overlay-title {
  font-size: clamp(1.5rem, 5vw, 1.85rem);
  font-weight: 700;
}

.overlay-hint,
.overlay-score {
  font-size: 1.05rem;
  color: var(--text-muted);
}

.overlay-best {
  color: var(--accent);
  font-weight: 600;
  font-size: 1.1rem;
}

.overlay-text {
  font-size: clamp(3rem, 15vw, 4.5rem);
  font-weight: 800;
  color: var(--accent);
  animation: pulse 0.6s ease;
}

.overlay--countdown .overlay-text {
  text-shadow: 0 0 40px var(--accent-glow);
}

@keyframes fadeIn {
  from { opacity: 0; }
  to { opacity: 1; }
}

@keyframes pulse {
  0%, 100% { transform: scale(1); }
  50% { transform: scale(1.12); }
}

/* --- Instructions --- */
.instructions {
  font-size: 0.9rem;
  color: var(--text-muted);
  text-align: center;
  line-height: 1.4;
  padding: 0 0.25rem;
  width: 100%;
  max-width: min(100%, 400px);
  margin-inline: auto;
}

/* --- Buttons (above play screen) --- */
.controls-row {
  display: flex;
  flex-wrap: wrap;
  justify-content: center;
  gap: 0.45rem;
  width: 100%;
  order: 0;
}

.btn {
  min-height: var(--touch-min);
  min-width: 5.5rem;
  padding: 0.65rem 1.25rem;
  font-family: inherit;
  font-size: 1rem;
  font-weight: 600;
  color: var(--text);
  background: var(--surface-elevated);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  cursor: pointer;
  transition: background 0.15s, transform 0.1s, box-shadow 0.15s;
  -webkit-tap-highlight-color: transparent;
  touch-action: manipulation;
}

.btn:hover:not(:disabled) {
  background: #2f4058;
}

.btn:active:not(:disabled) {
  transform: scale(0.97);
}

.btn:disabled {
  opacity: 0.45;
  cursor: not-allowed;
}

.btn--primary {
  background: linear-gradient(180deg, var(--accent) 0%, var(--accent-dim) 100%);
  color: #0f1419;
  border-color: transparent;
  box-shadow: 0 4px 16px var(--accent-glow);
}

.btn--primary:hover:not(:disabled) {
  filter: brightness(1.06);
}

/* --- D-pad (flex cross, centered under board) --- */
.dpad-shell {
  display: flex;
  justify-content: center;
  align-items: center;
}

.dpad {
  --dpad-btn: clamp(64px, 18vw, 76px);
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 0.4rem;
}

.dpad-row {
  display: flex;
  justify-content: center;
  align-items: center;
  gap: 0.4rem;
}

/* Middle row gap = one button + row gap so the cross is symmetric */
.dpad-row--middle {
  gap: calc(var(--dpad-btn) + 0.4rem);
}

.dpad-btn {
  display: flex;
  align-items: center;
  justify-content: center;
  flex: 0 0 var(--dpad-btn);
  width: var(--dpad-btn);
  height: var(--dpad-btn);
  font-size: 1.35rem;
  color: var(--text);
  background: var(--surface-elevated);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  cursor: pointer;
  user-select: none;
  -webkit-tap-highlight-color: transparent;
  touch-action: manipulation;
  transition: background 0.12s, transform 0.1s, box-shadow 0.12s;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.25);
}

.dpad-btn:active,
.dpad-btn.pressed {
  background: var(--border);
  transform: scale(0.94);
  box-shadow: inset 0 2px 6px rgba(0, 0, 0, 0.3);
}

/* --- Item legend --- */
.legend {
  list-style: none;
  width: 100%;
  max-width: min(100%, 400px);
  margin-inline: auto;
  padding: 0.35rem 0.5rem;
  display: flex;
  flex-direction: column;
  gap: 0.35rem;
  font-size: 0.85rem;
  color: var(--text-muted);
}

.legend li {
  display: flex;
  align-items: center;
  gap: 0.5rem;
}

.legend-icon {
  flex-shrink: 0;
  width: 14px;
  height: 14px;
  border-radius: 50%;
}

.legend-icon--apple {
  background: radial-gradient(circle at 35% 30%, #f87171, #b91c1c 70%);
  box-shadow: 0 0 0 1px rgba(255, 255, 255, 0.15);
}

.legend-icon--gold {
  background: radial-gradient(circle at 35% 30%, #fde68a, #d97706 70%);
  box-shadow: 0 0 0 2px rgba(251, 191, 36, 0.5);
}

.legend-icon--bomb {
  background: radial-gradient(circle at 40% 35%, #71717a, #18181b 75%);
  border-radius: 4px;
}

.legend-icon--ice {
  background: radial-gradient(circle at 35% 30%, #7dd3fc, #0369a1 75%);
  box-shadow: 0 0 6px rgba(56, 189, 248, 0.6);
}

.legend-icon--portal {
  background: radial-gradient(circle at 35% 30%, #e879f9, #7e22ce 75%);
  box-shadow: 0 0 0 2px rgba(192, 132, 252, 0.45);
}

/* Hide D-pad on large screens where keyboard is primary (optional: still show for touch laptops) */
@media (min-width: 768px) and (hover: hover) and (pointer: fine) {
  .dpad {
    opacity: 0.85;
  }
}

/* --- Tablet / desktop tweaks --- */
@media (min-width: 480px) {
  .app {
    padding: 1rem 1.25rem 1.5rem;
    gap: 1rem;
  }
}
