:root {
  --bg-dark: #0f1f10;
  --bg-mid: #1c3a1e;
  --accent: #f5c542;
  --panel: #14321a;
  --text: #f2ead9;
}
* { box-sizing: border-box; }
html, body {
  margin: 0;
  height: 100%;
  background: radial-gradient(circle at top, var(--bg-mid), var(--bg-dark));
  color: var(--text);
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
}
body {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  padding: 16px;
  position: relative;
  overflow: hidden;
}
.wrap {
  /* Exact width is set inline by resizeCanvas() in JS. No max-width here:
     it would clamp the element narrower than the canvas's internal
     resolution and squash every sprite horizontally. */
  width: 100%;
}
h1 {
  margin: 0 0 8px;
  text-align: center;
  font-size: 1.4rem;
}
.hud {
  display: flex;
  align-items: center;
  justify-content: space-between;
  font-weight: 600;
  font-size: 0.95rem;
  padding: 0 4px 6px;
}
.hud span { color: var(--accent); }
.life-icon { width: 1.3em; height: 1.3em; vertical-align: -0.3em; }
.icon-btn {
  background: none;
  border: none;
  font-size: 1.1rem;
  line-height: 1;
  cursor: pointer;
  color: var(--text);
  padding: 4px;
}
.timerbar-outer {
  width: 100%;
  height: 8px;
  background: rgba(255,255,255,0.12);
  border-radius: 4px;
  overflow: hidden;
  margin-bottom: 8px;
}
.timerbar-inner {
  height: 100%;
  width: 100%;
  background: var(--accent);
  transition: width 0.1s linear, background 0.2s;
}
#game {
  width: 100%;
  display: block;
  background: var(--panel);
  border: 4px solid #0a1c0c;
  border-radius: 10px;
  touch-action: none;
}
.dpad {
  display: grid;
  grid-template-columns: repeat(3, 48px);
  grid-template-rows: repeat(2, 48px);
  justify-content: center;
  gap: 5px;
  margin: 12px auto 0;
}
.dbtn {
  font-size: 1.2rem;
  border: none;
  border-radius: 10px;
  background: #3a2416;
  color: var(--text);
  touch-action: none;
  cursor: pointer;
}
.dbtn:active { background: var(--accent); color: #1c1c1c; }
.up { grid-column: 2; grid-row: 1; }
.left { grid-column: 1; grid-row: 2; }
.down { grid-column: 2; grid-row: 2; }
.right { grid-column: 3; grid-row: 2; }
.overlay {
  position: fixed;
  inset: 0;
  background: rgba(5, 12, 6, 0.9);
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  text-align: center;
  padding: 24px;
  z-index: 10;
}
.overlay.hidden { display: none; }
.overlay .new-best {
  margin: 0 0 4px;
  font-size: 1.15rem;
  font-weight: 800;
  color: var(--accent);
}
.new-best.hidden { display: none; }
#confetti {
  position: fixed;
  inset: 0;
  width: 100%;
  height: 100%;
  pointer-events: none;
  z-index: 30;
}
.overlay p { max-width: 320px; color: #d8d0b8; }
.overlay button {
  margin-top: 12px;
  padding: 10px 24px;
  font-size: 1rem;
  font-weight: 700;
  border: none;
  border-radius: 8px;
  background: var(--accent);
  color: #1c1c1c;
  cursor: pointer;
}
.monkey-stage {
  position: relative;
  display: inline-block;
  margin-bottom: 8px;
}
.monkey { display: block; overflow: visible; }

/* Level-clear is a non-blocking toast, not a full-screen modal: gameplay
   never pauses for it, and it always self-dismisses via the CSS animation
   below (plus an animationend listener), so there's no shared state with
   the game loop that could ever leave it stuck on screen. */
.toast {
  position: fixed;
  top: 6%;
  left: 50%;
  transform: translateX(-50%);
  display: flex;
  flex-direction: column;
  align-items: center;
  text-align: center;
  background: rgba(20, 50, 26, 0.95);
  border: 2px solid rgba(245, 197, 66, 0.6);
  border-radius: 18px;
  padding: 10px 28px 18px;
  box-shadow: 0 10px 30px rgba(0, 0, 0, 0.45);
  z-index: 20;
  pointer-events: none;
  opacity: 0;
}
.toast.hidden { display: none; }
.toast h1 { margin: 4px 0 0; font-size: 1.3rem; }
#levelClearOverlay.toast .monkey { width: 110px; height: 121px; }
.toast.toast-anim { animation: toastPop 1.3s ease forwards; }

@keyframes toastPop {
  0% { opacity: 0; transform: translateX(-50%) translateY(-12px) scale(0.9); }
  12% { opacity: 1; transform: translateX(-50%) translateY(0) scale(1); }
  82% { opacity: 1; transform: translateX(-50%) translateY(0) scale(1); }
  100% { opacity: 0; transform: translateX(-50%) translateY(-8px) scale(0.96); }
}

/* --- splash screen --- */
.overlay.splash {
  background:
    radial-gradient(ellipse 80% 60% at 50% 28%, rgba(58, 107, 58, 0.55), transparent 70%),
    radial-gradient(ellipse at 50% 100%, rgba(245, 197, 66, 0.08), transparent 60%),
    #0a170c;
  padding: 16px;
}
.splash-card {
  width: min(92vw, 420px);
  max-height: 100%;
  overflow-y: auto;
  padding: 26px 24px 20px;
  border-radius: 24px;
  background: linear-gradient(180deg, rgba(24, 48, 28, 0.92), rgba(12, 26, 14, 0.92));
  border: 1px solid rgba(245, 197, 66, 0.22);
  box-shadow: 0 24px 60px rgba(0, 0, 0, 0.55), inset 0 1px 0 rgba(255, 255, 255, 0.06);
  display: flex;
  flex-direction: column;
  align-items: center;
  text-align: center;
  animation: splashIn 0.5s ease-out both;
}
@keyframes splashIn {
  from { opacity: 0; transform: translateY(14px) scale(0.98); }
  to { opacity: 1; transform: none; }
}
.splash-card .splash-kicker {
  margin: 0 0 6px;
  font-size: 0.72rem;
  font-weight: 700;
  letter-spacing: 0.2em;
  text-transform: uppercase;
  color: rgba(245, 197, 66, 0.8);
}
.splash-card .splash-title {
  margin: 0;
  line-height: 0.92;
  font-weight: 900;
  letter-spacing: -0.02em;
  text-shadow: 0 4px 0 rgba(0, 0, 0, 0.35);
}
.splash-title-top,
.splash-title-bottom {
  display: block;
  font-size: clamp(2.3rem, 9vw, 3.3rem);
}
.splash-title-top {
  background: linear-gradient(180deg, #ffe07a, #f5c542 55%, #d99a1e);
  -webkit-background-clip: text;
  background-clip: text;
  color: transparent;
  /* text-shadow would show through the transparent gradient fill */
  text-shadow: none;
  filter: drop-shadow(0 4px 0 rgba(0, 0, 0, 0.35));
}
.splash-title-bottom { color: var(--text); }
.splash-stage {
  width: clamp(140px, 30vh, 230px);
  aspect-ratio: 1;
  margin: 6px 0 2px;
  background: radial-gradient(ellipse 60% 22% at 50% 90%, rgba(245, 197, 66, 0.22), transparent 70%);
}
#splashDancer, #introDancer { width: 100%; height: 100%; display: block; }
.splash-card .splash-tagline {
  margin: 0 0 14px;
  max-width: 330px;
  font-size: 0.95rem;
  line-height: 1.45;
  color: #d8d0b8;
}
.splash-stats {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 10px;
  width: 100%;
  margin-bottom: 16px;
}
.splash-stat {
  display: flex;
  flex-direction: column;
  gap: 2px;
  padding: 10px 8px;
  border-radius: 14px;
  background: rgba(255, 255, 255, 0.05);
  border: 1px solid rgba(255, 255, 255, 0.08);
}
.splash-stat-label {
  font-size: 0.68rem;
  font-weight: 700;
  letter-spacing: 0.12em;
  text-transform: uppercase;
  color: rgba(242, 234, 217, 0.6);
}
.splash-stat-value {
  font-size: 1.5rem;
  font-weight: 900;
  color: var(--accent);
  font-variant-numeric: tabular-nums;
}
.splash-card .splash-play {
  width: 100%;
  margin: 0;
  padding: 14px 20px;
  font-size: 1.2rem;
  font-weight: 900;
  letter-spacing: 0.04em;
  text-transform: uppercase;
  color: #2a1d05;
  background: linear-gradient(180deg, #ffd766, #f5c542);
  border: none;
  border-radius: 16px;
  box-shadow: 0 6px 0 #b8871a, 0 12px 24px rgba(0, 0, 0, 0.35);
  cursor: pointer;
  transition: transform 0.08s ease, box-shadow 0.08s ease, filter 0.15s ease;
  animation: playPulse 2.4s ease-in-out infinite;
}
.splash-card .splash-play:hover { filter: brightness(1.06); }
.splash-card .splash-play:active {
  transform: translateY(5px);
  box-shadow: 0 1px 0 #b8871a, 0 4px 10px rgba(0, 0, 0, 0.35);
}
.splash-card .splash-play:focus-visible { outline: 3px solid #fff; outline-offset: 3px; }
@keyframes playPulse {
  0%, 100% { box-shadow: 0 6px 0 #b8871a, 0 12px 24px rgba(0, 0, 0, 0.35), 0 0 0 0 rgba(245, 197, 66, 0.45); }
  50% { box-shadow: 0 6px 0 #b8871a, 0 12px 24px rgba(0, 0, 0, 0.35), 0 0 0 10px rgba(245, 197, 66, 0); }
}
.splash-card .splash-hint {
  margin: 12px 0 0;
  font-size: 0.78rem;
  color: rgba(242, 234, 217, 0.55);
}
.splash-hint kbd {
  display: inline-block;
  min-width: 1.5em;
  margin: 0 1px;
  padding: 1px 5px;
  font: inherit;
  font-weight: 700;
  color: var(--text);
  background: rgba(255, 255, 255, 0.08);
  border: 1px solid rgba(255, 255, 255, 0.18);
  border-bottom-width: 2px;
  border-radius: 5px;
}
.splash-hint-touch { display: none; }
@media (hover: none) and (pointer: coarse) {
  .splash-hint-keys { display: none; }
  .splash-card .splash-hint-touch { display: block; }
}
@media (prefers-reduced-motion: reduce) {
  .splash-card, .splash-card .splash-play { animation: none; }
}

@keyframes tailWag { 0%, 100% { transform: rotate(-14deg); } 50% { transform: rotate(14deg); } }

/* --- happy (level clear) --- */
.monkey-happy { animation: happyJump 0.7s ease-in-out infinite; }
.monkey-happy .m-arm-left { animation: cheerArmL 0.7s ease-in-out infinite; transform-origin: 58px 120px; }
.monkey-happy .m-arm-right { animation: cheerArmR 0.7s ease-in-out infinite; transform-origin: 142px 120px; }
.monkey-happy .m-leg-left { animation: kickLegL 0.7s ease-in-out infinite; transform-origin: 87px 184px; }
.monkey-happy .m-leg-right { animation: kickLegR 0.7s ease-in-out infinite; transform-origin: 113px 184px; }
.monkey-happy .m-tail { animation: tailWag 0.5s ease-in-out infinite; transform-origin: 65px 175px; }

@keyframes happyJump {
  0%, 100% { transform: translateY(0) scaleY(1); }
  30% { transform: translateY(-2px) scaleY(0.9); }
  55% { transform: translateY(-22px) scaleY(1.08); }
  80% { transform: translateY(0) scaleY(0.95); }
}
@keyframes cheerArmL { 0%, 100% { transform: rotate(-150deg); } 50% { transform: rotate(-172deg); } }
@keyframes cheerArmR { 0%, 100% { transform: rotate(150deg); } 50% { transform: rotate(172deg); } }
@keyframes kickLegL { 0%, 100% { transform: rotate(-25deg); } 50% { transform: rotate(15deg); } }
@keyframes kickLegR { 0%, 100% { transform: rotate(25deg); } 50% { transform: rotate(-15deg); } }

.m-sparkle {
  position: absolute;
  font-size: 1.4rem;
  animation: sparkleTwinkle 1s ease-in-out infinite;
}
.m-sparkle-1 { top: 6%; left: -8%; animation-delay: 0s; }
.m-sparkle-2 { top: -4%; right: -10%; animation-delay: 0.3s; }
.m-sparkle-3 { bottom: 10%; left: 46%; animation-delay: 0.6s; }
@keyframes sparkleTwinkle {
  0%, 100% { opacity: 0.2; transform: scale(0.8) rotate(0deg); }
  50% { opacity: 1; transform: scale(1.2) rotate(20deg); }
}

/* --- game over: a card in the same visual language as the splash card,
   tinted rose/red instead of jungle green-and-gold, centered on the
   player gorilla (drawPlushGorilla) knocked out rather than the old SVG
   monkey rig. --- */
.overlay.gameover {
  background:
    radial-gradient(ellipse 80% 60% at 50% 28%, rgba(120, 40, 40, 0.5), transparent 70%),
    radial-gradient(ellipse at 50% 100%, rgba(245, 197, 66, 0.06), transparent 60%),
    #0a170c;
  padding: 16px;
}
.gameover-card {
  width: min(92vw, 380px);
  max-height: 100%;
  overflow-y: auto;
  padding: 24px 24px 20px;
  border-radius: 24px;
  background: linear-gradient(180deg, rgba(48, 22, 22, 0.92), rgba(18, 12, 13, 0.92));
  border: 1px solid rgba(255, 120, 120, 0.22);
  box-shadow: 0 24px 60px rgba(0, 0, 0, 0.55), inset 0 1px 0 rgba(255, 255, 255, 0.06);
  display: flex;
  flex-direction: column;
  align-items: center;
  text-align: center;
  animation: splashIn 0.5s ease-out both;
}
.gameover-card .gameover-kicker {
  margin: 0 0 4px;
  max-width: none;
  font-size: 1.6rem;
  font-weight: 900;
  letter-spacing: -0.01em;
  color: #ff8f8f;
  text-shadow: 0 4px 0 rgba(0, 0, 0, 0.35);
}
.gameover-stage {
  position: relative;
  width: clamp(120px, 26vh, 190px);
  aspect-ratio: 1;
  margin: 4px 0 6px;
  background: radial-gradient(ellipse 60% 22% at 50% 90%, rgba(255, 120, 120, 0.16), transparent 70%);
}
#gameOverDancer { width: 100%; height: 100%; display: block; }
.dizzy-star {
  position: absolute;
  top: -6px;
  font-size: 1.2rem;
  animation: dizzyOrbit 2.4s ease-in-out infinite;
}
.dizzy-star-1 { left: 4%; animation-delay: 0s; }
.dizzy-star-2 { right: 4%; animation-delay: 1.2s; }
@keyframes dizzyOrbit {
  0% { opacity: 0; transform: translate(0, 6px) scale(0.7); }
  25% { opacity: 1; transform: translate(10px, -8px) scale(1); }
  50% { opacity: 1; transform: translate(0, -14px) scale(1.05); }
  75% { opacity: 1; transform: translate(-10px, -8px) scale(1); }
  100% { opacity: 0; transform: translate(0, 6px) scale(0.7); }
}
.gameover-final {
  margin: 2px 0 14px;
  font-size: 0.95rem;
  color: #d8d0b8;
}
.gameover-stats {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 10px;
  width: 100%;
  margin-bottom: 16px;
}
.gameover-stat {
  display: flex;
  flex-direction: column;
  gap: 2px;
  padding: 10px 8px;
  border-radius: 14px;
  background: rgba(255, 255, 255, 0.05);
  border: 1px solid rgba(255, 255, 255, 0.08);
}
.gameover-stat-label {
  font-size: 0.68rem;
  font-weight: 700;
  letter-spacing: 0.12em;
  text-transform: uppercase;
  color: rgba(242, 234, 217, 0.6);
}
.gameover-stat-value {
  font-size: 1.5rem;
  font-weight: 900;
  color: var(--accent);
  font-variant-numeric: tabular-nums;
}
.gameover-card .gameover-play {
  width: 100%;
  margin: 0;
  padding: 14px 20px;
  font-size: 1.2rem;
  font-weight: 900;
  letter-spacing: 0.04em;
  text-transform: uppercase;
  color: #2a1d05;
  background: linear-gradient(180deg, #ffd766, #f5c542);
  border: none;
  border-radius: 16px;
  box-shadow: 0 6px 0 #b8871a, 0 12px 24px rgba(0, 0, 0, 0.35);
  cursor: pointer;
  transition: transform 0.08s ease, box-shadow 0.08s ease, filter 0.15s ease;
}
.gameover-card .gameover-play:hover { filter: brightness(1.06); }
.gameover-card .gameover-play:active {
  transform: translateY(5px);
  box-shadow: 0 1px 0 #b8871a, 0 4px 10px rgba(0, 0, 0, 0.35);
}
.gameover-card .gameover-play:focus-visible { outline: 3px solid #fff; outline-offset: 3px; }

/* --- scale up chrome (text, buttons, monkey rig) on tablets (iPad) and
   desktop; .wrap's actual px width is computed by resizeCanvas() in JS,
   not by a max-width here --- */
@media (min-width: 640px) {
  h1 { font-size: 1.7rem; }
  .hud { font-size: 1.1rem; }
  .dpad {
    grid-template-columns: repeat(3, 58px);
    grid-template-rows: repeat(2, 58px);
    gap: 7px;
  }
  .dbtn { font-size: 1.4rem; }
  .monkey { width: 170px; height: 187px; }
  .overlay p { max-width: 400px; }
}

@media (min-width: 1024px) {
  body { padding: 32px; }
  h1 { font-size: 2rem; }
  .hud { font-size: 1.25rem; }
  .dpad {
    grid-template-columns: repeat(3, 64px);
    grid-template-rows: repeat(2, 64px);
    gap: 8px;
    margin-top: 16px;
  }
  .dbtn { font-size: 1.55rem; border-radius: 12px; }
  .monkey { width: 200px; height: 220px; }
  .overlay p { max-width: 460px; font-size: 1.05rem; }
  #game { border-width: 6px; }
}
