:root {
  --sky: #7ec8e8;
  --ink: #1a2b33;
  --cream: #f4f7f2;
  --accent: #e85d4c;
  --teal: #1f8a7a;
  --panel: rgba(18, 32, 38, 0.78);
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  width: 100%;
  height: 100%;
  height: 100dvh;
  overflow: hidden;
  background: #5aa8c8;
  font-family: Outfit, system-ui, sans-serif;
  touch-action: none;
  user-select: none;
  -webkit-user-select: none;
}

canvas {
  display: block;
  width: 100%;
  height: 100%;
}

#ui-root {
  position: fixed;
  inset: 0;
  pointer-events: none;
  z-index: 10;
}

.panel {
  position: absolute;
  inset: 0;
  display: flex;
  align-items: safe center;
  justify-content: safe center;
  pointer-events: auto;
  padding: max(12px, env(safe-area-inset-top)) max(12px, env(safe-area-inset-right)) max(12px, env(safe-area-inset-bottom)) max(12px, env(safe-area-inset-left));
  overflow-x: hidden;
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
  overscroll-behavior: contain;
  background:
    radial-gradient(ellipse at 30% 20%, rgba(255, 220, 160, 0.28), transparent 50%),
    radial-gradient(ellipse at 70% 80%, rgba(30, 90, 80, 0.45), rgba(10, 28, 36, 0.88));
  backdrop-filter: blur(8px);
}

.panel.hidden { display: none; }

.card {
  width: min(420px, 90vw);
  max-height: min(100%, calc(100dvh - 24px));
  margin: auto;
  padding: 28px 28px 24px;
  border-radius: 22px;
  background: linear-gradient(160deg, rgba(255,255,255,0.16), rgba(255,255,255,0.06));
  border: 1px solid rgba(255,255,255,0.22);
  color: var(--cream);
  text-align: center;
  box-shadow: 0 24px 50px rgba(0,0,0,0.35);
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
  flex-shrink: 0;
}

.tag {
  display: inline-block;
  font-size: 0.72rem;
  letter-spacing: 0.14em;
  text-transform: uppercase;
  color: #ffd27a;
  margin-bottom: 8px;
}

.card h1 {
  margin: 0 0 8px;
  font-family: "Bebas Neue", sans-serif;
  font-size: clamp(2.6rem, 10vw, 3.6rem);
  letter-spacing: 0.04em;
  line-height: 0.95;
}

.card p {
  margin: 0 0 18px;
  color: rgba(244, 247, 242, 0.82);
  font-size: 0.95rem;
  line-height: 1.45;
}

.bar {
  height: 10px;
  border-radius: 999px;
  background: rgba(255,255,255,0.15);
  overflow: hidden;
  margin-bottom: 10px;
}

.bar i {
  display: block;
  height: 100%;
  width: 0%;
  background: linear-gradient(90deg, #1f8a7a, #ffd27a);
  border-radius: inherit;
  transition: width 0.15s ease;
}

#load-status {
  font-size: 0.82rem;
  color: rgba(244,247,242,0.7);
}

.btn {
  pointer-events: auto;
  border: 0;
  cursor: pointer;
  font-family: Outfit, sans-serif;
  font-weight: 800;
  font-size: 1rem;
  letter-spacing: 0.04em;
  padding: 14px 28px;
  border-radius: 14px;
  color: #122026;
  background: linear-gradient(180deg, #ffe08a, #f0b429);
  box-shadow: 0 8px 0 #b07a12;
  transition: transform 0.08s ease;
}

.btn:active { transform: translateY(3px); box-shadow: 0 5px 0 #b07a12; }
.btn:disabled { opacity: 0.5; cursor: default; }

.btn.secondary {
  background: rgba(255,255,255,0.12);
  color: var(--cream);
  box-shadow: 0 6px 0 rgba(0,0,0,0.25);
}

.game-over-actions {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 10px;
  width: 100%;
  margin-bottom: 14px;
}

.game-over-actions .btn {
  width: min(100%, 280px);
}

.leaderboard-panel {
  width: 100%;
  margin-top: 4px;
  padding: 12px 14px;
  border-radius: 14px;
  background: rgba(0, 0, 0, 0.22);
  border: 1px solid rgba(255, 255, 255, 0.14);
  text-align: left;
}

.leaderboard-panel h3 {
  margin: 0 0 8px;
  font-size: 0.72rem;
  letter-spacing: 0.12em;
  text-transform: uppercase;
  color: #ffd27a;
  text-align: center;
}

.leaderboard-list {
  list-style: none;
  margin: 0;
  padding: 0;
  display: flex;
  flex-direction: column;
  gap: 6px;
  max-height: 140px;
  overflow-y: auto;
}

.leaderboard-list li {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 7px 9px;
  border-radius: 10px;
  background: rgba(255, 255, 255, 0.08);
  font-size: 0.78rem;
  color: rgba(244, 247, 242, 0.92);
}

.leaderboard-list .rank {
  min-width: 24px;
  font-weight: 800;
  color: #ffd27a;
}

.leaderboard-list .player {
  flex: 1;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.leaderboard-list .meta {
  font-size: 0.68rem;
  color: rgba(244, 247, 242, 0.62);
  white-space: nowrap;
}

.leaderboard-empty {
  justify-content: center;
  color: rgba(244, 247, 242, 0.65);
  background: transparent;
}

#pause .card .btn + .btn {
  margin-top: 10px;
}

.stats-row {
  display: flex;
  gap: 10px;
  justify-content: center;
  margin-bottom: 18px;
}

.stat-pill {
  min-width: 110px;
  padding: 10px 14px;
  border-radius: 14px;
  background: rgba(0,0,0,0.22);
}

.stat-pill small {
  display: block;
  font-size: 0.7rem;
  letter-spacing: 0.08em;
  text-transform: uppercase;
  color: rgba(244,247,242,0.6);
}

.stat-pill b {
  font-size: 1.35rem;
  font-family: "Bebas Neue", sans-serif;
  letter-spacing: 0.04em;
}

#hud {
  position: absolute;
  inset: 0;
  pointer-events: none;
  opacity: 0;
  transition: opacity 0.25s ease;
}

#hud.visible { opacity: 1; }

.hud-top {
  position: absolute;
  top: max(16px, env(safe-area-inset-top));
  left: 16px;
  right: 16px;
  display: flex;
  justify-content: space-between;
  align-items: flex-start;
  gap: 10px;
  z-index: 20;
}

.hud-stats {
  display: flex;
  gap: 10px;
}

.hud-chip {
  pointer-events: none;
  padding: 10px 14px;
  border-radius: 14px;
  background: rgba(255,255,255,0.88);
  color: var(--ink);
  box-shadow: 0 8px 20px rgba(0,0,0,0.18);
  min-width: 80px;
}

.student-chip b {
  color: #d35400;
}

.toast-notify {
  position: absolute;
  top: 76px;
  left: 50%;
  transform: translateX(-50%);
  background: linear-gradient(135deg, #1f8a7a, #16a085);
  color: #ffffff;
  padding: 9px 20px;
  border-radius: 999px;
  font-weight: 700;
  font-size: 0.92rem;
  letter-spacing: 0.02em;
  box-shadow: 0 10px 25px rgba(0,0,0,0.3);
  pointer-events: none;
  transition: opacity 0.25s ease, transform 0.25s ease;
  z-index: 25;
}

.toast-notify.hidden {
  opacity: 0;
  pointer-events: none;
  transform: translate(-50%, -10px);
}

.hud-chip small {
  display: block;
  font-size: 0.68rem;
  letter-spacing: 0.08em;
  text-transform: uppercase;
  opacity: 0.55;
}

.hud-chip b {
  font-family: "Bebas Neue", sans-serif;
  font-size: 1.7rem;
  letter-spacing: 0.03em;
  line-height: 1;
}

.hud-controls {
  display: flex;
  gap: 8px;
  pointer-events: auto;
}

.hud-icon-btn {
  pointer-events: auto;
  width: 48px;
  height: 48px;
  border: 0;
  border-radius: 14px;
  background: rgba(255,255,255,0.9);
  box-shadow: 0 8px 20px rgba(0,0,0,0.18);
  cursor: pointer;
  font-size: 1.15rem;
  color: var(--ink);
  display: flex;
  align-items: center;
  justify-content: center;
  transition: transform 0.08s ease, background 0.15s ease;
}

.hud-icon-btn:active {
  transform: translateY(2px);
  background: rgba(255,255,255,1);
}

.menu-actions {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 12px;
  width: 100%;
}

.menu-actions .btn {
  width: min(100%, 260px);
}

.sound-toggle-btn {
  pointer-events: auto;
  border: 1px solid rgba(255, 255, 255, 0.25);
  background: rgba(255, 255, 255, 0.12);
  color: var(--cream);
  border-radius: 999px;
  padding: 8px 18px;
  font-family: Outfit, sans-serif;
  font-weight: 600;
  font-size: 0.85rem;
  cursor: pointer;
  display: inline-flex;
  align-items: center;
  gap: 6px;
  transition: background 0.15s ease, transform 0.08s ease;
}

.sound-toggle-btn:hover {
  background: rgba(255, 255, 255, 0.2);
}

.sound-toggle-btn:active {
  transform: scale(0.96);
}

#touch {
  position: absolute;
  inset: 0;
  z-index: 30;
  pointer-events: none;
  opacity: 0;
  visibility: hidden;
  transition: opacity 0.25s ease;
}

#touch.visible {
  opacity: 1;
  visibility: visible;
}

.touch-side {
  position: absolute;
  bottom: max(36px, calc(env(safe-area-inset-bottom) + 20px));
  display: flex;
  gap: 12px;
  pointer-events: auto;
}

.touch-side--left {
  left: max(28px, calc(env(safe-area-inset-left) + 16px));
}

.touch-side--right {
  right: max(28px, calc(env(safe-area-inset-right) + 16px));
  flex-direction: column;
}

.pad-btn {
  width: 64px;
  height: 64px;
  border: 0;
  border-radius: 18px;
  background: rgba(255,255,255,0.86);
  color: var(--ink);
  font-size: 1.35rem;
  font-weight: 800;
  box-shadow: 0 6px 0 rgba(0,0,0,0.18);
  cursor: pointer;
  -webkit-tap-highlight-color: transparent;
  touch-action: manipulation;
}

.pad-btn:active {
  transform: translateY(3px);
  box-shadow: 0 3px 0 rgba(0,0,0,0.18);
}

.hint {
  position: absolute;
  left: 50%;
  bottom: 160px;
  transform: translateX(-50%);
  color: rgba(255,255,255,0.85);
  font-size: 0.82rem;
  text-align: center;
  text-shadow: 0 2px 10px rgba(0,0,0,0.45);
}

.hint.hidden { display: none; }

kbd {
  display: inline-block;
  padding: 2px 7px;
  margin: 0 2px;
  border-radius: 6px;
  background: rgba(0,0,0,0.35);
  font-family: Outfit, sans-serif;
  font-size: 0.75rem;
}

@media (hover: none), (pointer: coarse), (max-width: 900px) {
  .hint--desktop { display: none !important; }
}

/* Hide on-screen pads only on clear desktop pointers — never by width alone
   (large phones in landscape are often >900px wide). */
@media (hover: hover) and (pointer: fine) {
  body:not(.force-touch) #touch { display: none; }
}

@media (pointer: coarse), (hover: none) {
  #touch { display: block; }
}

@media (orientation: landscape) and (max-height: 520px) {
  .panel {
    align-items: flex-start;
    justify-content: center;
    padding: max(6px, env(safe-area-inset-top)) max(10px, env(safe-area-inset-right)) max(6px, env(safe-area-inset-bottom)) max(10px, env(safe-area-inset-left));
  }

  .card {
    width: min(520px, 94vw);
    max-height: calc(100dvh - 12px);
    padding: 10px 16px 12px;
    border-radius: 16px;
  }

  .tag {
    margin-bottom: 2px;
    font-size: 0.62rem;
  }

  .card h1 {
    margin-bottom: 2px;
    font-size: clamp(1.55rem, 7vh, 2.2rem);
  }

  .card p {
    margin-bottom: 8px;
    font-size: 0.78rem;
    line-height: 1.3;
    display: -webkit-box;
    -webkit-line-clamp: 2;
    -webkit-box-orient: vertical;
    overflow: hidden;
  }

  #menu .card p,
  #loader .card p {
    display: none;
  }

  .stats-row {
    margin-bottom: 8px;
    gap: 8px;
  }

  .stat-pill {
    min-width: 88px;
    padding: 6px 10px;
  }

  .stat-pill b {
    font-size: 1.15rem;
  }

  .menu-actions {
    gap: 8px;
  }

  .btn {
    padding: 10px 22px;
    font-size: 0.92rem;
  }

  .sound-toggle-btn {
    padding: 6px 14px;
    font-size: 0.78rem;
  }

  .game-over-actions {
    gap: 8px;
    margin-bottom: 8px;
  }

  .leaderboard-panel {
    padding: 8px 10px;
  }

  .leaderboard-list {
    max-height: 56px;
  }

  .hud-top {
    top: max(8px, env(safe-area-inset-top));
    left: 10px;
    right: 10px;
  }

  .hud-chip {
    padding: 6px 10px;
    min-width: 64px;
  }

  .hud-chip b {
    font-size: 1.35rem;
  }

  .hud-icon-btn {
    width: 40px;
    height: 40px;
    font-size: 1rem;
  }

  .toast-notify {
    top: 56px;
    font-size: 0.78rem;
    padding: 6px 14px;
  }

  .pad-btn {
    width: 48px;
    height: 48px;
    border-radius: 14px;
    font-size: 1.1rem;
  }

  .touch-side {
    bottom: max(22px, calc(env(safe-area-inset-bottom) + 12px));
    gap: 8px;
  }

  .touch-side--left {
    left: max(22px, calc(env(safe-area-inset-left) + 12px));
  }

  .touch-side--right {
    right: max(22px, calc(env(safe-area-inset-right) + 12px));
  }

  .hint {
    bottom: 110px;
  }
}

@media (orientation: landscape) and (max-height: 380px) {
  .card {
    padding: 8px 14px 10px;
  }

  .card h1 {
    font-size: clamp(1.35rem, 6.5vh, 1.85rem);
  }

  .pad-btn {
    width: 42px;
    height: 42px;
    border-radius: 12px;
    font-size: 1rem;
  }
}
