/* ============================================================
   SPACE INVIDERS — Arcade 8-bit
   Palette classica arcade su sfondo nero + effetto CRT
   ============================================================ */

:root {
  --green:   #2bff5f;
  --green-d: #00b33c;
  --white:   #f4f4f4;
  --red:     #ff2742;
  --yellow:  #ffd400;
  --cyan:    #25e0ff;
  --magenta: #ff4fd8;
  --bg:      #05060a;
  --bg-2:    #0b0f1c;
  /* synthwave */
  --neon-pink:   #ff2d95;
  --neon-purple: #b026ff;
  --neon-blue:   #2de2ff;
  --sky-top:     #0a0420;
  --sky-mid:     #2a0a4a;
  --horizon:     #ff2d95;
}

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

html, body {
  height: 100%;
  background: var(--bg);
  color: var(--white);
  font-family: "Press Start 2P", monospace;
  overflow: hidden;
  -webkit-font-smoothing: none;
  image-rendering: pixelated;
}

body { cursor: default; }

/* ---------- CRT overlay (scanlines + vignette + flicker) ---------- */
.crt {
  position: fixed;
  inset: 0;
  z-index: 9999;
  pointer-events: none;
  background:
    repeating-linear-gradient(
      to bottom,
      rgba(0, 0, 0, 0) 0px,
      rgba(0, 0, 0, 0) 2px,
      rgba(0, 0, 0, 0.28) 3px,
      rgba(0, 0, 0, 0.28) 4px
    );
  mix-blend-mode: multiply;
  animation: flicker 0.15s infinite steps(2);
}
.crt::after {
  content: "";
  position: absolute;
  inset: 0;
  background: radial-gradient(ellipse at center, transparent 55%, rgba(0, 0, 0, 0.65) 100%);
}
body.no-crt .crt { display: none; }

@keyframes flicker {
  0%   { opacity: 0.92; }
  100% { opacity: 1; }
}

/* ---------- Arcade frame ---------- */
.arcade {
  position: relative;
  width: 100vw;
  height: 100vh;
  height: 100dvh;
  display: flex;
  align-items: center;
  justify-content: center;
  overflow: hidden;
  background: var(--bg);
}

/* ============================================================
   SFONDO SYNTHWAVE (dietro a tutte le schermate)
   ============================================================ */
.bg { position: absolute; inset: 0; z-index: 0; pointer-events: none; overflow: hidden; }

.bg-sky {
  position: absolute; inset: 0;
  background: linear-gradient(180deg, var(--sky-top) 0%, var(--sky-mid) 48%, #5a0f5e 60%, #14071f 62%, var(--bg) 100%);
}

/* stelle che luccicano nella parte alta */
.bg-stars {
  position: absolute; left: 0; right: 0; top: 0; height: 62%;
  background-image:
    radial-gradient(1px 1px at 12% 18%, #fff, transparent),
    radial-gradient(1px 1px at 32% 42%, #cfe, transparent),
    radial-gradient(1px 1px at 58% 22%, #fff, transparent),
    radial-gradient(1px 1px at 73% 50%, #fdf, transparent),
    radial-gradient(2px 2px at 85% 12%, #fff, transparent),
    radial-gradient(1px 1px at 46% 8%, #fff, transparent),
    radial-gradient(1px 1px at 22% 60%, #aef, transparent),
    radial-gradient(1px 1px at 91% 38%, #fff, transparent);
  animation: twinkle 3.5s steps(4) infinite;
}

/* sole retro affettato all'orizzonte */
.bg-sun {
  position: absolute; left: 50%; bottom: 38%;
  width: min(340px, 60vw); aspect-ratio: 1;
  transform: translate(-50%, 50%);
  border-radius: 50%;
  background: linear-gradient(180deg, #fff2a8 0%, #ffd23f 24%, #ff5ed0 55%, #b026ff 100%);
  filter: drop-shadow(0 0 60px rgba(255, 94, 208, 0.55));
  -webkit-mask:
    linear-gradient(#000 0 0) top / 100% 52% no-repeat,
    repeating-linear-gradient(180deg, #000 0 9px, transparent 9px 16px);
          mask:
    linear-gradient(#000 0 0) top / 100% 52% no-repeat,
    repeating-linear-gradient(180deg, #000 0 9px, transparent 9px 16px);
}

/* griglia in prospettiva che scorre verso l'osservatore */
.bg-grid {
  position: absolute; left: -25%; right: -25%; bottom: 0; height: 42%;
  background-image:
    linear-gradient(to right, rgba(45, 226, 255, 0.5) 1px, transparent 1px),
    linear-gradient(to bottom, rgba(255, 45, 149, 0.55) 1px, transparent 1px);
  background-size: 44px 44px;
  transform: perspective(340px) rotateX(74deg);
  transform-origin: 50% 100%;
  -webkit-mask: linear-gradient(to top, #000 30%, transparent 92%);
          mask: linear-gradient(to top, #000 30%, transparent 92%);
  animation: gridmove 1.8s linear infinite;
}
@keyframes gridmove { from { background-position: 0 0; } to { background-position: 0 44px; } }

/* glow/vignette morbida sopra tutto lo sfondo */
.bg-glow {
  position: absolute; inset: 0;
  background:
    radial-gradient(60% 40% at 50% 58%, rgba(255, 45, 149, 0.16), transparent 70%),
    radial-gradient(ellipse at center, transparent 60%, rgba(0, 0, 0, 0.55) 100%);
}

/* ---------- Screens (state machine) ---------- */
.screen {
  position: absolute;
  inset: 0;
  z-index: 1;
  display: none;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: clamp(14px, 3vh, 34px);
  padding: 4vmin;
  text-align: center;
}
.screen.is-active { display: flex; animation: screenIn 0.25s ease-out; }
@keyframes screenIn {
  from { opacity: 0; transform: scale(0.985); }
  to   { opacity: 1; transform: scale(1); }
}

/* ---------- Logo / titles ---------- */
.logo {
  font-size: clamp(28px, 9vmin, 86px);
  line-height: 1.15;
  color: var(--green);
  letter-spacing: 2px;
  text-shadow:
    3px 3px 0 var(--green-d),
    0 0 18px rgba(43, 255, 95, 0.55);
}
.logo .logo-accent { color: var(--cyan); text-shadow: 3px 3px 0 #007a99, 0 0 18px rgba(37, 224, 255, 0.55); }
.logo.small { font-size: clamp(18px, 4.5vmin, 40px); margin-bottom: 1.2em; }
.logo.gameover { color: var(--red); text-shadow: 4px 4px 0 #7a0014, 0 0 22px rgba(255, 39, 66, 0.6); font-size: clamp(40px, 14vmin, 120px); }

.subtitle { color: var(--yellow); font-size: clamp(9px, 2vmin, 16px); letter-spacing: 3px; }

.title-block { display: flex; flex-direction: column; gap: 18px; }

/* ---------- Blink helpers ---------- */
.blink      { animation: blink 1s steps(2, start) infinite; }
.blink-slow { animation: blink 1.6s steps(2, start) infinite; }
@keyframes blink { 50% { opacity: 0; } }

.hint { color: var(--white); opacity: 0.85; font-size: clamp(7px, 1.6vmin, 12px); letter-spacing: 1px; }
.credits { color: var(--cyan); font-size: clamp(8px, 1.8vmin, 13px); }
.credits b { color: var(--white); }

/* ---------- Starfield (subtle) ---------- */
.stars {
  position: absolute;
  inset: 0;
  z-index: -1;
  background-image:
    radial-gradient(1px 1px at 20% 30%, #fff, transparent),
    radial-gradient(1px 1px at 70% 60%, #9fe, transparent),
    radial-gradient(1px 1px at 40% 80%, #fff, transparent),
    radial-gradient(1px 1px at 85% 15%, #ffd, transparent),
    radial-gradient(1px 1px at 10% 70%, #fff, transparent),
    radial-gradient(2px 2px at 55% 25%, #fff, transparent);
  opacity: 0.5;
  animation: twinkle 3s steps(3) infinite;
}
@keyframes twinkle { 50% { opacity: 0.25; } }

/* ============================================================
   COIN SLOT (riquadro rosso) + COIN
   ============================================================ */
.coin-area {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 26px;
}

.coin-slot {
  position: relative;
  width: 150px;
  height: 96px;
  background: linear-gradient(180deg, #ff3b54, #c40020);
  border: 5px solid #ff8a98;
  box-shadow:
    0 0 0 5px #5a000d,
    0 0 28px rgba(255, 39, 66, 0.55),
    inset 0 0 16px rgba(0, 0, 0, 0.45);
  display: flex;
  align-items: flex-end;
  justify-content: center;
  padding-bottom: 12px;
  transition: transform 0.08s, box-shadow 0.2s;
}
.coin-slot.hot {
  transform: scale(1.06);
  box-shadow: 0 0 0 5px #5a000d, 0 0 40px rgba(255, 215, 0, 0.85), inset 0 0 16px rgba(0, 0, 0, 0.45);
}
.slot-mouth {
  position: absolute;
  top: 14px;
  left: 50%;
  transform: translateX(-50%);
  width: 56px;
  height: 12px;
  background: #1a0006;
  border: 3px solid #2a000a;
  box-shadow: inset 0 3px 6px rgba(0, 0, 0, 0.9);
}
.slot-label {
  font-size: 12px;
  line-height: 1.4;
  color: #fff;
  text-shadow: 2px 2px 0 #7a0014;
  letter-spacing: 1px;
}

.coin {
  width: 64px;
  height: 64px;
  border-radius: 50%;
  background:
    radial-gradient(circle at 35% 30%, #fff3a8 0%, var(--yellow) 35%, #c79a00 100%);
  border: 4px solid #fff7c2;
  box-shadow: 0 6px 0 #8a6a00, 0 0 16px rgba(255, 212, 0, 0.6);
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: grab;
  user-select: none;
  touch-action: none;
  transition: transform 0.1s;
  animation: coinBob 1.4s ease-in-out infinite;
}
.coin:active { cursor: grabbing; }
.coin .coin-face { font-size: 13px; color: #7a5c00; text-shadow: 0 1px 0 #fff6b0; }
.coin.dragging { animation: none; cursor: grabbing; z-index: 50; }
.coin.inserting { animation: coinInsert 0.5s ease-in forwards; }
@keyframes coinBob { 50% { transform: translateY(-8px); } }
@keyframes coinInsert {
  0%   { transform: translate(var(--cx, 0), var(--cy, 0)) scale(1) rotate(0deg); opacity: 1; }
  70%  { transform: translate(var(--cx, 0), calc(var(--cy, 0) - 60px)) scale(0.5) rotate(360deg); opacity: 1; }
  100% { transform: translate(var(--cx, 0), calc(var(--cy, 0) - 60px)) scale(0.1) rotate(540deg); opacity: 0; }
}

/* ============================================================
   MENU
   ============================================================ */
.menu { list-style: none; display: flex; flex-direction: column; gap: 22px; }
.menu-item {
  font-size: clamp(13px, 3vmin, 24px);
  color: var(--white);
  cursor: pointer;
  letter-spacing: 2px;
  padding: 6px 10px;
  transition: color 0.1s, transform 0.1s;
}
.menu-item .sel { color: var(--yellow); opacity: 0; margin-right: 8px; }
.menu-item b { color: var(--cyan); }
.menu-item.is-selected { color: var(--yellow); transform: translateX(6px); }
.menu-item.is-selected .sel { opacity: 1; animation: blink 0.6s steps(2) infinite; }
.menu-item:hover { color: var(--yellow); }

/* ---------- Credits roll ---------- */
.credits-roll { display: flex; flex-direction: column; gap: 6px; font-size: clamp(10px, 2.2vmin, 16px); }
.credits-roll .dim { color: var(--cyan); }
.credits-roll p { line-height: 1.6; }

/* ---------- Buttons ---------- */
.arcade-btn {
  font-family: inherit;
  font-size: clamp(10px, 2vmin, 15px);
  color: var(--white);
  background: var(--bg-2);
  border: 3px solid var(--cyan);
  padding: 14px 22px;
  cursor: pointer;
  letter-spacing: 2px;
  box-shadow: 4px 4px 0 #06324a;
  transition: transform 0.06s, background 0.1s;
}
.arcade-btn:hover { background: #142036; transform: translate(-1px, -1px); box-shadow: 4px 4px 0 #06324a, 0 0 18px rgba(37, 224, 255, 0.55); }
.arcade-btn:active { transform: translate(2px, 2px); box-shadow: 1px 1px 0 #06324a; }
.arcade-btn.primary { border-color: var(--green); color: var(--green); box-shadow: 4px 4px 0 #064d1f; }
.arcade-btn.primary:hover { background: #0c2417; box-shadow: 4px 4px 0 #064d1f, 0 0 18px rgba(43, 255, 95, 0.55); }
.arcade-btn.skip { border-color: var(--neon-pink); color: var(--neon-pink); box-shadow: 4px 4px 0 #5a0030; }
.arcade-btn.skip:hover { background: #2a0820; box-shadow: 4px 4px 0 #5a0030, 0 0 18px rgba(255, 45, 149, 0.6); }
.reward-actions { display: flex; gap: 18px; flex-wrap: wrap; justify-content: center; }

/* ============================================================
   GAME
   ============================================================ */
#screen-game { gap: 10px; }
.hud {
  display: flex;
  gap: clamp(14px, 5vw, 60px);
  font-size: clamp(9px, 2vmin, 15px);
  color: var(--green);
  letter-spacing: 1px;
}
.hud b { color: var(--white); }
#hud-lives { color: var(--red); }

.canvas-wrap {
  position: relative;
  /* sta sempre nello schermo: vincolato sia in larghezza sia in altezza, ratio 4:5 */
  width: min(94vw, 70vh * 0.8);
  height: min(70vh, 94vw * 1.25);
  width: min(94vw, 70dvh * 0.8);
  height: min(70dvh, 94vw * 1.25);
  border: 4px solid #1b2545;
  box-shadow: 0 0 0 4px #05060a, 0 0 40px rgba(43, 255, 95, 0.15);
  background: #000;
}
#game-canvas {
  width: 100%;
  height: 100%;
  display: block;
  image-rendering: pixelated;
  cursor: crosshair;
}
.game-message {
  position: absolute;
  inset: 0;
  display: none;
  align-items: center;
  justify-content: center;
  text-align: center;
  font-size: clamp(14px, 4vmin, 30px);
  color: var(--yellow);
  text-shadow: 3px 3px 0 #000;
  background: rgba(0, 0, 0, 0.55);
  padding: 20px;
  line-height: 1.6;
}
.game-message.show { display: flex; animation: blink 0.8s steps(2) infinite; }

/* ============================================================
   REWARD — retro TV + Pac-Man bar
   ============================================================ */
#screen-reward {
  gap: clamp(12px, 2.5vh, 26px);
  /* velo scuro: il sole synthwave non deve disturbare la leggibilità del lettore */
  background: radial-gradient(ellipse at center, rgba(5, 4, 16, 0.82) 0%, rgba(5, 4, 16, 0.94) 100%);
}
.reward-title { color: var(--yellow); font-size: clamp(12px, 3.4vmin, 26px); text-shadow: 2px 2px 0 #7a5c00; }

.tv {
  position: relative;
  background: linear-gradient(160deg, #3a3f52, #1c1f2c);
  border-radius: 18px;
  padding: 18px 18px 22px;
  box-shadow: 0 10px 0 #0a0c14, inset 0 0 30px rgba(0, 0, 0, 0.6);
  width: min(720px, 92vw);
}
.tv-screen {
  position: relative;
  aspect-ratio: 16 / 9;
  background: #000;
  border: 6px solid #0a0c14;
  border-radius: 10px;
  overflow: hidden;
  box-shadow: inset 0 0 40px rgba(0, 0, 0, 0.9);
}
#yt-player, #yt-player iframe { position: absolute; inset: 0; width: 100% !important; height: 100% !important; border: 0; }
.yt-loading {
  position: absolute; inset: 0; display: flex; align-items: center; justify-content: center;
  color: var(--green); font-size: clamp(9px, 2vmin, 14px); letter-spacing: 1px;
  animation: blink 1s steps(2) infinite; z-index: 2; background: #000;
}
.tv-knobs { position: absolute; right: -2px; top: 24px; display: flex; flex-direction: column; gap: 12px; }
.tv-knobs span { width: 14px; height: 14px; border-radius: 50%; background: #11131c; box-shadow: inset 0 0 4px #000; }

/* ---------- PAC-MAN PROGRESS BAR ---------- */
.pac-bar { width: min(720px, 92vw); display: flex; flex-direction: column; gap: 10px; }
.pac-track {
  position: relative;
  height: 32px;
  background: #0a0c14;
  border: 3px solid #1b2545;
  border-radius: 4px;
  overflow: hidden;
  cursor: pointer;
  touch-action: none;
}
.pac-fill {
  position: absolute;
  left: 0; top: 0; bottom: 0;
  width: 0%;
  background: linear-gradient(90deg, rgba(255, 212, 0, 0.18), rgba(255, 212, 0, 0.06));
}
.dots {
  position: absolute;
  inset: 0;
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0 16px;
  pointer-events: none;
}
.dots .dot {
  width: 6px; height: 6px; border-radius: 50%;
  background: var(--yellow);
  box-shadow: 0 0 6px rgba(255, 212, 0, 0.7);
  transition: opacity 0.15s, transform 0.15s;
}
.dots .dot.eaten { opacity: 0; transform: scale(0); }

/* Pac-Man: CERCHIO perfetto con bocca a cuneo (mask conic) che mastica.
   border-radius:50% garantisce la forma tonda; la mask ritaglia solo la bocca. */
@property --mouth {
  syntax: "<angle>";
  inherits: false;
  initial-value: 55deg;
}
.pacman {
  position: absolute;
  top: 50%;
  left: 0%;
  width: 30px;
  height: 30px;
  transform: translate(-50%, -50%);
  transition: left 0.2s linear;
  z-index: 3;
  border-radius: 50%;
  background: radial-gradient(circle at 38% 30%, #fff6b0 0%, var(--yellow) 45%, #e0b400 100%);
  image-rendering: auto;
  filter: drop-shadow(0 0 5px rgba(255, 212, 0, 0.75));
  --mouth: 55deg;
  /* cuneo trasparente centrato a 90° = bocca rivolta a DESTRA (direzione di marcia).
     L'angolo --mouth è animato via JS (più affidabile della mask animata in CSS). */
  -webkit-mask: conic-gradient(from calc(90deg - var(--mouth) / 2), transparent 0deg, transparent var(--mouth), #000 var(--mouth));
          mask: conic-gradient(from calc(90deg - var(--mouth) / 2), transparent 0deg, transparent var(--mouth), #000 var(--mouth));
  cursor: grab;
}
.pacman:active { cursor: grabbing; }
.pac-eye {
  position: absolute;
  top: 20%;
  left: 50%;
  width: 4px; height: 4px;
  background: #1a1400;
  border-radius: 50%;
  z-index: 4;
}

.pac-time { font-size: clamp(8px, 1.8vmin, 12px); color: var(--cyan); display: flex; justify-content: space-between; }
.pac-time span { color: var(--white); }

/* ---------- Game over ---------- */
.final-score { color: var(--yellow); font-size: clamp(10px, 2.6vmin, 18px); }
.final-score b { color: var(--white); }

/* ---------- Responsive tweaks ---------- */
@media (max-width: 760px) {
  .screen { gap: clamp(10px, 2vh, 22px); padding: 3vmin; }
  #screen-game { gap: 6px; padding: 2vmin; }
  .hud { gap: clamp(10px, 4vw, 26px); }
  .hint { font-size: 7px; }
  .reward-actions { gap: 10px; }
  .arcade-btn { padding: 11px 14px; letter-spacing: 1px; }
  .tv { padding: 12px 12px 16px; border-radius: 12px; }
  .pacman { width: 26px; height: 26px; }
  .reward-title { font-size: clamp(11px, 4.6vw, 22px); }
}
@media (max-width: 540px) {
  .coin-slot { width: 120px; height: 80px; }
  .coin { width: 54px; height: 54px; }
  .hud { gap: 12px; font-size: 9px; }
}
/* landscape basso (telefono orizzontale): comprimi ancora */
@media (max-height: 520px) and (orientation: landscape) {
  .canvas-wrap { width: min(60vh, 92vw); height: min(75vh, 92vw); }
  .logo { font-size: clamp(22px, 7vmin, 48px); }
}
