:root {
  --bg: #09090b;
  --grid: #18181b;
  --snake-head: #22c55e;
  --snake-body: #4ade80;
  --accent: #f59e0b;
  --text: #f4f4f5;
}

* {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
  touch-action: none;
}

body {
  background: var(--bg);
  color: var(--text);
  font-family: 'Outfit', sans-serif;
  height: 100dvh;
  overflow: hidden;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
}

.game-container {
  position: relative;
  width: 100%;
  max-width: 500px;
  aspect-ratio: 1;
  margin: 1rem;
  transition: transform 0.1s;
}

/* Inventory Slot */
.inventory-box {
  text-align: center;
}

.inventory-slot {
  width: 40px;
  height: 40px;
  background: rgba(255, 255, 255, 0.1);
  border: 2px solid #52525b;
  border-radius: 8px;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 1.5rem;
  margin: 0 auto;
}

.inventory-slot.has-item {
  border-color: var(--accent);
  background: rgba(245, 158, 11, 0.2);
  box-shadow: 0 0 10px rgba(245, 158, 11, 0.4);
}

/* Screen Shake */
.shake {
  animation: shake 0.3s cubic-bezier(.36, .07, .19, .97) both;
}

@keyframes shake {

  10%,
  90% {
    transform: translate3d(-2px, 0, 0);
  }

  20%,
  80% {
    transform: translate3d(4px, 0, 0);
  }

  30%,
  50%,
  70% {
    transform: translate3d(-6px, 0, 0);
  }

  40%,
  60% {
    transform: translate3d(6px, 0, 0);
  }
}

#debugLog {
  position: absolute;
  top: 10px;
  left: 10px;
  color: #0f0;
  font-family: monospace;
  font-size: 10px;
  z-index: 2000;
  pointer-events: none;
  background: rgba(0, 0, 0, 0.8);
  padding: 4px;
}

canvas {
  background: var(--bg);
  background-image:
    linear-gradient(var(--grid) 1px, transparent 1px),
    linear-gradient(90deg, var(--grid) 1px, transparent 1px);
  background-size: 20px 20px;
  border-radius: 1rem;
  box-shadow: 0 0 0 4px #27272a, 0 20px 40px -10px rgba(0, 0, 0, 0.5);
  width: 100%;
  height: 100%;
  image-rendering: pixelated;
  transition: background-color 0.5s, box-shadow 0.2s;
}

.disco-bg {
  animation: disco 2s infinite alternate;
}

@keyframes disco {
  0% {
    background-color: #1a0b2e;
  }

  50% {
    background-color: #0b1a2e;
  }

  100% {
    background-color: #1a0b2e;
  }
}

/* ─── UI Layer ───────────────────────────────────────────── */
.ui-layer {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  pointer-events: none;
  display: flex;
  flex-direction: column;
  justify-content: space-between;
  padding: 1.5rem;
  z-index: 5;
}

/* Combo Bar */
.combo-container {
  position: absolute;
  top: 4rem;
  left: 50%;
  transform: translateX(-50%);
  width: 200px;
  height: 6px;
  background: rgba(255, 255, 255, 0.1);
  border-radius: 3px;
  overflow: hidden;
  opacity: 0;
  transition: opacity 0.3s;
}

.combo-container.active {
  opacity: 1;
}

.combo-fill {
  height: 100%;
  width: 0%;
  background: linear-gradient(90deg, #f59e0b, #ef4444);
  transition: width 0.1s linear;
  border-radius: 3px;
}

.combo-text {
  position: absolute;
  top: 4.7rem;
  left: 50%;
  transform: translateX(-50%);
  font-size: 1.1rem;
  font-weight: 900;
  color: #f59e0b;
  text-shadow: 0 2px 10px rgba(245, 158, 11, 0.5);
  opacity: 0;
  transition: opacity 0.2s, transform 0.2s;
}

.combo-text.active {
  opacity: 1;
  animation: comboPopIn 0.3s ease-out;
}

@keyframes comboPopIn {
  0% {
    transform: translateX(-50%) scale(1.5);
    opacity: 0;
  }

  100% {
    transform: translateX(-50%) scale(1);
    opacity: 1;
  }
}

/* ─── Header HUD ─────────────────────────────────────────── */
.header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  text-shadow: 0 2px 4px rgba(0, 0, 0, 0.5);
}

.lives-box {
  text-align: center;
}

.lives {
  font-size: 1.5rem;
  color: #ef4444;
  font-weight: 900;
  transition: transform 0.2s, color 0.2s;
}

.lives-flash {
  animation: livesFlash 0.4s ease-out;
}

@keyframes livesFlash {
  0% {
    transform: scale(1.5);
    color: #fff;
  }

  100% {
    transform: scale(1);
    color: #ef4444;
  }
}

.score-box {
  text-align: right;
}

.label {
  font-size: 0.75rem;
  text-transform: uppercase;
  opacity: 0.7;
  font-weight: 700;
  letter-spacing: 0.05em;
}

.score {
  font-size: 2.5rem;
  font-weight: 900;
  line-height: 1;
  background: linear-gradient(to bottom, #fff, #a1a1aa);
  -webkit-background-clip: text;
  -webkit-text-fill-color: transparent;
  transition: transform 0.15s;
}

.score-pop {
  animation: scorePop 0.3s ease-out;
}

@keyframes scorePop {
  0% {
    transform: scale(1.3);
  }

  50% {
    transform: scale(0.95);
  }

  100% {
    transform: scale(1);
  }
}

.high-score {
  font-size: 1rem;
  color: var(--accent);
  font-weight: 700;
}

/* ─── Buttons ────────────────────────────────────────────── */
.mute-btn {
  position: absolute;
  top: 1.5rem;
  left: 1.5rem;
  background: none;
  border: none;
  color: var(--text);
  font-size: 1.5rem;
  cursor: pointer;
  opacity: 0.5;
  z-index: 20;
  pointer-events: auto;
  transition: opacity 0.2s, transform 0.2s;
}

.mute-btn:hover {
  opacity: 1;
  transform: scale(1.1);
}

.pause-btn {
  position: absolute;
  top: 1.5rem;
  right: 1.5rem;
  background: none;
  border: none;
  font-size: 1.5rem;
  cursor: pointer;
  z-index: 20;
  opacity: 0.7;
  pointer-events: auto;
  transition: opacity 0.2s, transform 0.2s;
}

.pause-btn:hover {
  opacity: 1;
  transform: scale(1.1);
}

/* ─── Overlays ───────────────────────────────────────────── */
.overlay {
  position: absolute;
  inset: 0;
  background: rgba(9, 9, 11, 0.88);
  backdrop-filter: blur(10px);
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  z-index: 9999; /* Boost to infinity */
  border-radius: 1rem;
  pointer-events: auto;
  transition: opacity 0.3s ease;
}

.overlay.hidden {
  opacity: 0;
  pointer-events: none;
  display: none;
}

.pause-overlay {
  position: absolute;
  inset: 0;
  background: rgba(0, 0, 0, 0.7);
  backdrop-filter: blur(6px);
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  z-index: 20;
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.2s;
}

.pause-overlay.visible {
  opacity: 1;
  pointer-events: auto;
}

h1 {
  font-size: 3.5rem;
  font-weight: 900;
  line-height: 1;
  margin-bottom: 0.5rem;
  background: linear-gradient(135deg, #22c55e, #3b82f6);
  -webkit-background-clip: text;
  -webkit-text-fill-color: transparent;
  text-align: center;
}

.game-over-title {
  font-size: 3rem;
  background: linear-gradient(135deg, #ef4444, #f97316);
  -webkit-background-clip: text;
  -webkit-text-fill-color: transparent;
  margin-bottom: 1rem;
}

.new-high-badge {
  font-size: 1.2rem;
  font-weight: 900;
  color: #f59e0b;
  text-shadow: 0 0 20px rgba(245, 158, 11, 0.5);
  margin-bottom: 1rem;
  animation: badgePulse 1s ease-in-out infinite alternate;
}

@keyframes badgePulse {
  0% {
    transform: scale(1);
    opacity: 0.8;
  }

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

.final-score-container {
  text-align: center;
  margin-bottom: 2rem;
}

.final-score {
  font-size: 4rem;
  font-weight: 900;
  line-height: 1.1;
  background: linear-gradient(to bottom, #fff, #a1a1aa);
  -webkit-background-clip: text;
  -webkit-text-fill-color: transparent;
}

.subtitle {
  font-size: 1.1rem;
  color: #a1a1aa;
  margin-bottom: 2rem;
}

.btn {
  background: var(--text);
  color: var(--bg);
  border: none;
  padding: 1rem 2.5rem;
  font-size: 1.25rem;
  font-weight: 800;
  border-radius: 3rem;
  cursor: pointer;
  transition: transform 0.1s ease, box-shadow 0.2s ease;
  font-family: inherit;
  box-shadow: 0 0 20px rgba(255, 255, 255, 0.15);
}

.btn:hover {
  transform: scale(1.05);
  box-shadow: 0 0 30px rgba(255, 255, 255, 0.3);
}

.btn:active {
  transform: scale(0.95);
}

.btn.pulse {
  animation: btnPulse 2s ease-in-out infinite;
}

@keyframes btnPulse {

  0%,
  100% {
    box-shadow: 0 0 20px rgba(255, 255, 255, 0.15);
  }

  50% {
    box-shadow: 0 0 35px rgba(255, 255, 255, 0.35);
  }
}

.controls-hint {
  margin-top: 2rem;
  font-size: 0.8rem;
  color: #52525b;
  display: flex;
  gap: 1rem;
  align-items: center;
}

.key {
  border: 1px solid #3f3f46;
  padding: 0.2rem 0.5rem;
  border-radius: 0.3rem;
  background: #18181b;
  color: #a1a1aa;
  font-family: inherit;
  font-size: inherit;
}

/* ─── Particles ──────────────────────────────────────────── */
.particle {
  position: absolute;
  pointer-events: none;
  animation: float 1s ease-out forwards;
}

@keyframes float {
  0% {
    transform: translate(0, 0) scale(1);
    opacity: 1;
  }

  100% {
    transform: translate(var(--tx), var(--ty)) scale(0);
    opacity: 0;
  }
}

/* ─── Mobile Controls (NES Classic Style) ────────────────── */
.mobile-controls {
  display: none;
  width: 100%;
  max-width: 500px;
  height: 200px;
  position: relative;
  margin-top: 1rem;
  background: #e6e6e6; /* Classic NES Grey */
  border: 4px solid #d4d4d4;
  border-radius: 4px;
  box-shadow: 0 10px 20px rgba(0, 0, 0, 0.5), inset 0 2px 5px rgba(255, 255, 255, 0.8);
  padding: 15px;
  justify-content: space-between;
  align-items: center;
  /* Black stripe background for button area */
  background-image: linear-gradient(to bottom, #e6e6e6 30%, #222 30%, #222 75%, #e6e6e6 75%);
}

.controller-left {
  width: 130px;
  height: 130px;
  position: relative;
  z-index: 2;
}

.d-pad {
  position: relative;
  width: 120px;
  height: 120px;
}

/* Cross shape logic */
.d-btn {
  position: absolute;
  background: #111;
  border: none;
  cursor: pointer;
  box-shadow: inset 2px 2px 0 #333, inset -2px -2px 0 #000;
}

.d-btn:active {
  background: #000;
  box-shadow: inset 2px 2px 5px #000;
}

.d-up { top: 0; left: 40px; width: 40px; height: 40px; border-radius: 4px 4px 0 0; }
.d-down { bottom: 0; left: 40px; width: 40px; height: 40px; border-radius: 0 0 4px 4px; }
.d-left { top: 40px; left: 0; width: 40px; height: 40px; border-radius: 4px 0 0 4px; }
.d-right { top: 40px; right: 0; width: 40px; height: 40px; border-radius: 0 4px 4px 0; }
.d-center {
  top: 40px;
  left: 40px;
  width: 40px;
  height: 40px;
  background: #111; /* Matching D-Pad black */
  position: absolute;
  box-shadow: inset 4px 4px 5px #222; /* Central divot */
  border-radius: 50%; /* Subtle concave center */
  transform: scale(0.6); 
  z-index: 5;
  pointer-events: none;
}

/* Center Start/Select */
.controller-center {
  display: flex;
  gap: 15px;
  align-self: center;
  margin-top: 40px; /* Push down into the gray area */
  z-index: 2;
}

.center-btn-group {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 4px;
}

.pill-btn {
  width: 50px;
  height: 14px;
  background: #333;
  border: 2px solid #777;
  border-radius: 10px;
  transform: rotate(0deg); /* NES classic is straight usually, or slight tilt */
  cursor: pointer;
  box-shadow: 0 2px 0 rgba(0,0,0,0.3);
  transition: transform 0.05s;
}

.pill-btn:active {
  transform: translateY(2px);
  box-shadow: none;
}

.btn-label {
  font-size: 10px;
  font-weight: 900;
  color: #cc0000; /* Retro Red */
  text-transform: uppercase;
  letter-spacing: 0.5px;
  font-family: sans-serif;
}

/* Right D-Pad */
.controller-right {
  width: 130px;
  height: 130px;
  position: relative;
  z-index: 2;
}

/* Action button in center of right D-Pad */
#btnAction {
  pointer-events: auto;
  cursor: pointer;
  background: #cc0000;
  box-shadow: inset 2px 2px 5px #8b0000;
}

#btnAction:active {
  transform: scale(0.55);
  box-shadow: inset 4px 4px 8px #500;
}

/* ─── Responsive ─────────────────────────────────────────── */
@media (pointer: coarse) {
  .game-container {
    height: auto;
    aspect-ratio: auto;
    max-height: 60vh;
  }

  canvas {
    height: auto;
    aspect-ratio: 1;
  }

  .mobile-controls {
    display: flex;
  }

  .controls-hint {
    display: none;
  }
}

@media (max-width: 400px) {
  h1 {
    font-size: 2.5rem;
  }

  .score {
    font-size: 2rem;
  }

  .final-score {
    font-size: 3rem;
  }

  .btn {
    padding: 0.8rem 2rem;
    font-size: 1.1rem;
  }
}