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

body {
  background: linear-gradient(135deg, #1a237e 0%, #0d47a1 50%, #1565c0 100%);
  min-height: 100vh;
  display: flex;
  justify-content: center;
  align-items: center;
  padding: 12px;
  font-family: 'Arial Rounded MT Bold', Arial, sans-serif;
}

.game-wrap {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 10px;
  width: 100%;
  max-width: 820px;
}


/* ---- Canvas area ---- */
.canvas-wrap {
  position: relative;
  border: 4px solid #fff;
  border-radius: 12px;
  overflow: hidden;
  box-shadow: 0 8px 32px rgba(0,0,0,0.4);
  width: 100%;
  max-width: 800px;
}

#gameCanvas {
  display: block;
  width: 100%;
  height: auto;
  aspect-ratio: 800 / 340;
  image-rendering: auto;
}

/* ---- Desktop: gioco a tutto schermo (mobile resta invariato) ---- */
@media (min-width: 801px) {
  body { padding: 0; }
  .game-wrap { max-width: none; width: 100%; gap: 0; }
  .canvas-wrap {
    max-width: none;
    width: min(100vw, calc(100vh * 800 / 340));
    border: none;
    border-radius: 0;
  }
}

/* ---- Popup overlay (inizio / fine) ---- */
.popup-overlay {
  position: fixed;
  inset: 0;
  z-index: 1000;
  display: flex;
  align-items: center;
  justify-content: center;
  background: rgba(10, 20, 80, 0.88);
  padding: 12px;
  transform: translateZ(0);   /* forza layer GPU sopra il canvas */
  -webkit-transform: translateZ(0);
}
.popup-overlay.hidden { display: none; }

.popup {
  background: #1a237e;
  border: 2px solid rgba(255,255,255,0.25);
  border-radius: 16px;
  padding: 24px 20px;
  max-width: 420px;
  width: 100%;
  max-height: 90vh;
  overflow-y: auto;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 14px;
  color: #fff;
  box-shadow: 0 8px 40px rgba(0,0,0,0.6);
}

.popup h1 {
  font-size: 1.6rem;
  text-align: center;
  text-shadow: 2px 2px 0 rgba(0,0,0,0.4);
}

.end-title {
  font-size: 2rem;
  font-weight: bold;
  text-align: center;
  text-shadow: 2px 3px 0 rgba(0,0,0,0.5);
}

.end-msg {
  font-size: 1rem;
  text-align: center;
  opacity: 0.9;
}

.end-score {
  font-size: 1.2rem;
  font-weight: bold;
  color: #ffd54f;
}

/* ---- Classifica ---- */
.leaderboard { width: 100%; }

.leaderboard h2 {
  font-size: 0.95rem;
  text-transform: uppercase;
  letter-spacing: 1px;
  opacity: 0.75;
  margin-bottom: 6px;
  text-align: center;
}

.leaderboard table {
  width: 100%;
  border-collapse: collapse;
  font-size: 0.9rem;
}

.leaderboard th {
  text-align: left;
  padding: 4px 8px;
  opacity: 0.6;
  font-weight: normal;
  border-bottom: 1px solid rgba(255,255,255,0.2);
}

.leaderboard td {
  padding: 5px 8px;
  border-bottom: 1px solid rgba(255,255,255,0.08);
}

.leaderboard tr:first-child td { color: #ffd54f; font-weight: bold; }

/* ---- Nome e bottoni popup ---- */
.popup-name-label { font-size: 0.9rem; opacity: 0.8; text-align: center; }

.popup-input {
  width: 100%;
  padding: 10px 14px;
  border-radius: 10px;
  border: 2px solid rgba(255,255,255,0.3);
  background: rgba(255,255,255,0.12);
  color: #fff;
  font-size: 1rem;
  outline: none;
  text-align: center;
}
.popup-input::placeholder { color: rgba(255,255,255,0.45); }
.popup-input:focus { border-color: rgba(255,255,255,0.6); }

.play-btn {
  padding: 12px 36px;
  background: #ff9800;
  color: #fff;
  border: none;
  border-radius: 24px;
  font-size: 1.05rem;
  font-weight: bold;
  cursor: pointer;
  box-shadow: 0 4px 12px rgba(0,0,0,0.35);
  transition: background 0.15s;
}
.play-btn:hover:not(:disabled) { background: #f57c00; }
.play-btn:disabled { opacity: 0.45; cursor: default; }

/* ---- Touch controls (inside canvas, left/right) ---- */
.touch-btn {
  position: absolute;
  bottom: 10px;
  width: 81px;
  height: 81px;
  border-radius: 50%;
  background: rgba(0, 0, 0, 0.38);
  border: 3px solid rgba(255, 255, 255, 0.65);
  color: #fff;
  font-size: 2.1rem;
  line-height: 1;
  cursor: pointer;
  user-select: none;
  -webkit-user-select: none;
  touch-action: none;
  display: none;
  align-items: center;
  justify-content: center;
  box-shadow: 0 2px 8px rgba(0,0,0,0.3);
  transition: background 0.08s;
  z-index: 10;
}
.touch-btn:active,
.touch-btn.pressed { background: rgba(255, 152, 0, 0.65); }

#btnWalk { left: 10px; }
#btnJump { right: 10px; }
#btnExit { right: 10px; bottom: 101px; font-size: 0.85rem; background: rgba(160, 0, 0, 0.45); }


/* Show touch controls on mobile only */
@media (max-width: 800px) {
  .touch-btn { display: flex; }
}

/* ---- Landscape / small-height adaptation ---- */
@media (max-height: 460px) {
  body { padding: 4px 8px; align-items: flex-start; }
  .game-wrap { gap: 4px; }
  #preGameHeader h1 { font-size: 1.1rem; }
  #preGameHeader { gap: 8px; }
.end-title { font-size: 1.6rem; }
}
