* { box-sizing: border-box; }
html, body { height: 100%; }
body {
  margin: 0;
  background: linear-gradient(180deg, #070b22 0%, #1b1348 48%, #3a1d5c 100%);
  color: #fff6e0;
  font-family: 'Fredoka', system-ui, -apple-system, Segoe UI, Roboto, sans-serif;
  overflow: hidden;
}

#game {
  position: absolute;
  inset: 0;
  width: 100vw;
  height: 100vh;
  display: block;
  z-index: 0;
  touch-action: none;
  cursor: pointer;
}

#ui {
  position: absolute;
  top: 12px;
  left: 12px;
  right: 12px;
  display: flex;
  gap: 12px;
  align-items: flex-start;
  justify-content: space-between;
  flex-wrap: wrap;
  pointer-events: none;
  z-index: 2;
}

#scoreboard { display: flex; gap: 8px; flex-wrap: wrap; }

.score-item {
  pointer-events: auto;
  background: rgba(255, 246, 224, 0.94);
  color: #24164a;
  border-radius: 12px;
  padding: 8px 12px;
  box-shadow: 0 8px 24px rgba(8, 10, 40, 0.4);
  display: flex;
  gap: 8px;
  align-items: baseline;
  border: 2px solid rgba(255, 196, 80, 0.45);
}
.score-item .label { opacity: 0.7; font-size: 13px; }
.score-item span:last-child { font-weight: 700; font-size: 20px; }

#meters { display: flex; gap: 8px; align-items: center; flex-wrap: wrap; }

.hearts,
.fuel-chip,
.combo-chip,
.boost-chip {
  pointer-events: none;
  background: rgba(255, 246, 224, 0.94);
  border-radius: 999px;
  padding: 7px 12px;
  font-size: 17px;
  letter-spacing: 2px;
  line-height: 1;
  box-shadow: 0 8px 24px rgba(8, 10, 40, 0.4);
  border: 2px solid rgba(255, 196, 80, 0.45);
  color: #24164a;
}
.fuel-chip,
.combo-chip {
  letter-spacing: 0;
  font-weight: 700;
  font-size: 15px;
}
.hidden { display: none !important; }

.combo-chip {
  background: #ffe8b8;
  border-color: #f4a261;
  animation: comboPop 220ms ease;
}
@keyframes comboPop {
  from { transform: scale(0.8); }
  to { transform: scale(1); }
}

.boost-chip {
  display: inline-flex;
  align-items: center;
  gap: 7px;
  letter-spacing: 0;
  padding: 7px 12px;
}
.boost-icon { font-size: 15px; filter: grayscale(0.6); transition: filter 160ms ease; }
.boost-track {
  display: block;
  width: 74px;
  height: 10px;
  border-radius: 999px;
  background: rgba(36, 22, 74, 0.18);
  overflow: hidden;
}
.boost-fill {
  display: block;
  height: 100%;
  width: 0%;
  border-radius: 999px;
  background: linear-gradient(90deg, #90e0ef, #4cc9f0);
  transition: width 120ms linear;
}
.boost-chip.is-ready { border-color: #ffd166; }
.boost-chip.is-ready .boost-icon { filter: none; }
.boost-chip.is-ready .boost-fill { background: linear-gradient(90deg, #ffd166, #f4a261); }
.boost-chip.is-firing {
  background: #fff0c2;
  animation: boostPulse 320ms ease-in-out infinite;
}
@keyframes boostPulse {
  0%, 100% { transform: scale(1); }
  50% { transform: scale(1.06); }
}
.fuel-chip.is-low {
  background: #ffe0b0;
  border-color: #ff8a4c;
  animation: fuelPulse 700ms ease-in-out infinite;
}
@keyframes fuelPulse {
  0%, 100% { transform: scale(1); }
  50% { transform: scale(1.05); }
}

.powers {
  pointer-events: none;
  display: flex;
  gap: 6px;
}
.power {
  display: inline-flex;
  align-items: center;
  gap: 4px;
  background: rgba(255, 246, 224, 0.94);
  border-radius: 999px;
  padding: 7px 11px;
  font-size: 17px;
  line-height: 1;
  box-shadow: 0 8px 24px rgba(8, 10, 40, 0.4);
  border: 2px solid rgba(255, 196, 80, 0.45);
  transition: opacity 160ms ease, transform 160ms ease;
}
.power .power-time {
  font-size: 13px;
  font-weight: 700;
  color: #7a3e00;
}
.power.is-off { opacity: 0.3; transform: scale(0.94); filter: grayscale(0.85); }
.power.is-off .power-time { display: none; }

#controls { pointer-events: auto; display: flex; gap: 8px; align-items: center; }
#restart, #play, #homeBtn, #mute, #music, .home-link {
  cursor: pointer;
  border: none;
  background: linear-gradient(180deg, #ffd166, #f4a261);
  color: #3b1d0a;
  border-radius: 12px;
  padding: 10px 14px;
  font-weight: 700;
  font-size: 15px;
  font-family: inherit;
  text-decoration: none;
  display: inline-flex;
  align-items: center;
  box-shadow: 0 8px 24px rgba(244, 162, 97, 0.4);
}
#mute, #music { padding: 10px 12px; }
#music.is-off { opacity: 0.45; filter: grayscale(0.7); }
#restart:hover, #play:hover, #homeBtn:hover, #mute:hover, #music:hover, .home-link:hover { filter: brightness(1.08); }
#restart:active, #play:active, #homeBtn:active, #mute:active, #music:active, .home-link:active { filter: brightness(0.94); }

#stageBar {
  position: absolute;
  top: 78px;
  left: 50%;
  transform: translateX(-50%);
  z-index: 3;
  display: flex;
  flex-direction: column;
  gap: 6px;
  align-items: center;
  padding: 10px 18px;
  border-radius: 16px;
  background: rgba(255, 246, 224, 0.94);
  color: #24164a;
  box-shadow: 0 10px 28px rgba(8, 10, 40, 0.4);
  border: 2px solid rgba(255, 196, 80, 0.45);
  pointer-events: none;
}
#stageBar.hidden { display: none; }
#stageBar.is-space {
  background: rgba(20, 16, 52, 0.82);
  color: #fff6e0;
  border-color: rgba(180, 160, 255, 0.4);
}
#stageBar.is-space #stageLabel { color: #fff6e0; }
#stageBar.is-low {
  border-color: #ff8a4c;
}
#stageLabel { font-size: 14px; font-weight: 700; color: #24164a; white-space: nowrap; }
.bar-track {
  display: block;
  width: min(46vw, 320px);
  height: 12px;
  border-radius: 999px;
  background: rgba(36, 22, 74, 0.16);
  overflow: hidden;
}
.bar-fill {
  display: block;
  height: 100%;
  width: 0%;
  border-radius: 999px;
  background: linear-gradient(90deg, #ffd166, #f4a261);
  transition: width 120ms linear;
}
#stageBar.is-space .bar-fill { background: linear-gradient(90deg, #90e0ef, #7b2cbf); }
#stageBar.is-low .bar-fill { background: linear-gradient(90deg, #ff8a4c, #e63946); }

#raceBar {
  position: absolute;
  top: 142px;
  left: 50%;
  transform: translateX(-50%);
  width: min(420px, calc(100vw - 32px));
  pointer-events: none;
  z-index: 3;
}
#raceBar.hidden { display: none; }
.race-track {
  position: relative;
  height: 18px;
  border-radius: 999px;
  background: rgba(255, 246, 224, 0.2);
  border: 2px solid rgba(255, 196, 80, 0.35);
  overflow: visible;
}
.race-glow {
  position: absolute;
  inset: 0;
  border-radius: inherit;
  background: linear-gradient(90deg, #e63946, #ffd166, #caf0f8);
  opacity: 0.28;
}
.mark {
  position: absolute;
  top: 50%;
  transform: translate(-50%, -50%);
  font-size: 18px;
  filter: drop-shadow(0 2px 4px rgba(0, 0, 0, 0.45));
  z-index: 1;
}
.mark.moon { right: 2px; left: auto; transform: translate(0, -50%); font-size: 16px; }
#raceCaption {
  text-align: center;
  margin-top: 6px;
  font-size: 13px;
  font-weight: 700;
  color: #fff6e0;
  text-shadow: 0 2px 8px rgba(0, 0, 0, 0.45);
}

#toast {
  position: absolute;
  bottom: 28px;
  left: 50%;
  transform: translateX(-50%);
  z-index: 3;
  padding: 10px 18px;
  border-radius: 999px;
  background: rgba(255, 246, 224, 0.95);
  color: #24164a;
  font-weight: 700;
  font-size: 16px;
  box-shadow: 0 10px 28px rgba(8, 10, 40, 0.45);
  pointer-events: none;
  transition: opacity 180ms ease;
  max-width: min(90vw, 420px);
  text-align: center;
}
#toast.hidden { opacity: 0; }
#toast.over-belt { bottom: 168px; }

#overlay {
  position: absolute;
  inset: 0;
  display: grid;
  place-items: center;
  background: rgba(8, 8, 32, 0.62);
  backdrop-filter: blur(4px);
  opacity: 0;
  pointer-events: none;
  transition: opacity 200ms ease;
  z-index: 4;
  padding: 16px;
}
#overlay.show { opacity: 1; pointer-events: auto; }
#overlay .panel {
  background: #fff6e0;
  color: #24164a;
  width: min(560px, calc(100vw - 32px));
  max-height: calc(100vh - 32px);
  overflow: auto;
  border-radius: 18px;
  padding: 24px;
  box-shadow: 0 24px 70px rgba(8, 10, 34, 0.55);
  border: 3px solid rgba(255, 196, 80, 0.5);
}
#overlay h1 { margin: 0 0 8px; font-size: 32px; color: #7a3e00; }
#overlay p { opacity: 0.88; line-height: 1.45; margin: 0 0 10px; }
#overlay .hint { font-size: 14px; opacity: 0.72; margin-bottom: 16px; }
#overlay .legend {
  display: grid;
  gap: 6px;
  margin: 12px 0 16px;
  font-size: 15px;
}
#overlay .legend.hidden { display: none; }
#overlay .legend span { opacity: 0.92; }
#overlay .legend em { font-style: normal; font-weight: 700; color: #b5451b; }

.diff-row {
  display: flex;
  gap: 8px;
  flex-wrap: wrap;
  margin: 4px 0 16px;
}
.diff-row.hidden { display: none; }
.diff-row .diff {
  cursor: pointer;
  flex: 1 1 auto;
  border: 2px solid rgba(122, 62, 0, 0.25);
  background: rgba(255, 209, 102, 0.18);
  color: #7a3e00;
  border-radius: 12px;
  padding: 10px 12px;
  font-family: inherit;
  font-weight: 700;
  font-size: 15px;
  transition: transform 140ms ease, background 140ms ease, border-color 140ms ease;
}
.diff-row .diff:hover { transform: translateY(-1px); }
.diff-row .diff.is-on {
  background: linear-gradient(180deg, #ffd166, #f4a261);
  border-color: #e76f51;
  color: #3b1d0a;
  box-shadow: 0 6px 18px rgba(244, 162, 97, 0.4);
}

.overlay-actions { display: flex; flex-wrap: wrap; gap: 8px; margin-top: 4px; }

@media (max-width: 560px) {
  .score-item { padding: 6px 10px; }
  .score-item span:last-child { font-size: 17px; }
  .hearts, .fuel-chip, .combo-chip, .boost-chip { font-size: 14px; padding: 6px 10px; }
  .boost-track { width: 54px; }
  .power { font-size: 14px; padding: 6px 8px; }
  .powers { gap: 4px; }
  #stageBar { top: auto; bottom: 118px; padding: 8px 14px; }
  #raceBar { top: auto; bottom: 76px; }
  .bar-track { width: min(64vw, 260px); }
  #toast { bottom: 18px; font-size: 14px; }
  #toast.over-belt { bottom: 200px; }
  #restart, #play, #homeBtn, #mute, #music, .home-link { padding: 9px 12px; font-size: 14px; }
  #overlay h1 { font-size: 26px; }
}
