* { margin: 0; padding: 0; box-sizing: border-box; }
:root {
  --safe-t: env(safe-area-inset-top);
  --safe-b: env(safe-area-inset-bottom);
  --safe-l: env(safe-area-inset-left);
  --safe-r: env(safe-area-inset-right);
}
html, body {
  width: 100%; height: 100%; overflow: hidden;
  background: #1a1230;
  font-family: 'Nunito', system-ui, -apple-system, sans-serif;
  -webkit-user-select: none; user-select: none;
  -webkit-tap-highlight-color: transparent;
  overscroll-behavior: none;
}
#scene { position: fixed; inset: 0; width: 100%; height: 100%; display: block; touch-action: none; }

/* ---------- HUD ---------- */
#hud { position: fixed; inset: 0; pointer-events: none; z-index: 10; }
#topbar {
  position: absolute; top: calc(14px + var(--safe-t)); left: calc(16px + var(--safe-l));
  right: calc(16px + var(--safe-r));
  display: flex; align-items: center; justify-content: space-between; gap: 12px;
}
.brand {
  display: flex; align-items: center; gap: 10px; pointer-events: auto;
}
.title {
  font-weight: 900; font-size: 19px; color: #fff;
  text-shadow: 0 2px 12px rgba(60,20,90,.6); letter-spacing: .2px;
}
.stage-badge {
  background: rgba(255, 255, 255, 0.15);
  border: 1px solid rgba(255, 255, 255, 0.25);
  color: #fff; font-family: inherit; font-weight: 800; font-size: 14px;
  padding: 5px 12px; border-radius: 999px; cursor: pointer;
  display: flex; align-items: center; gap: 6px; backdrop-filter: blur(8px);
  -webkit-backdrop-filter: blur(8px); transition: all 0.2s ease;
  box-shadow: 0 4px 12px rgba(0,0,0,0.15);
}
.stage-badge:hover {
  background: rgba(255, 255, 255, 0.25);
  transform: translateY(-1px);
}
.stage-badge .chevron {
  font-size: 10px; opacity: 0.8;
}
.counter {
  font-weight: 900; font-size: 16px; color: #fff;
  background: rgba(40,22,66,.42); border: 1px solid rgba(255,255,255,.18);
  padding: 7px 14px; border-radius: 999px; backdrop-filter: blur(10px);
  -webkit-backdrop-filter: blur(10px); display: flex; align-items: center; gap: 6px;
  box-shadow: 0 6px 20px rgba(30,10,50,.35);
  transform-origin: center;
  will-change: transform;
}
.counter .spark { color: #ffe27a; text-shadow: 0 0 10px rgba(255,210,90,.9); }

#hint {
  position: absolute; left: 0; right: 0; bottom: calc(150px + var(--safe-b));
  display: flex; flex-direction: column; align-items: center; gap: 4px;
  transition: opacity 1s ease; opacity: 1;
}
.hint-row {
  color: rgba(255,255,255,.92); font-weight: 700; font-size: 13.5px;
  text-shadow: 0 2px 10px rgba(40,15,70,.7); text-align: center;
  padding: 0 16px;
}
#hint.hide { opacity: 0; }

/* ---------- Touch controls ---------- */
#joystick {
  position: fixed; left: calc(22px + var(--safe-l)); bottom: calc(30px + var(--safe-b));
  width: 124px; height: 124px; border-radius: 50%;
  background: radial-gradient(circle at 50% 50%, rgba(255,255,255,.10), rgba(255,255,255,.04));
  border: 1.5px solid rgba(255,255,255,.22); z-index: 12; touch-action: none;
  box-shadow: inset 0 2px 14px rgba(0,0,0,.18);
}
#stick {
  position: absolute; left: 50%; top: 50%; width: 54px; height: 54px; border-radius: 50%;
  transform: translate(-50%, -50%);
  background: radial-gradient(circle at 38% 32%, #fff, #d9c8ff 70%);
  box-shadow: 0 6px 18px rgba(40,15,70,.45); transition: transform .05s linear;
}
#jumpBtn {
  position: fixed; right: calc(26px + var(--safe-r)); bottom: calc(46px + var(--safe-b));
  width: 84px; height: 84px; border-radius: 50%; z-index: 12;
  font-size: 30px; color: #fff; border: 1.5px solid rgba(255,255,255,.28);
  background: radial-gradient(circle at 40% 35%, rgba(255,180,120,.55), rgba(180,90,180,.45));
  box-shadow: 0 8px 22px rgba(40,15,70,.45); backdrop-filter: blur(6px);
  -webkit-backdrop-filter: blur(6px); cursor: pointer; touch-action: none;
  transition: transform .08s ease, filter .08s ease;
}
#jumpBtn:active { transform: scale(.92); filter: brightness(1.15); }
/* keyboard users on desktop don't need the on-screen pad */
body.desktop #joystick, body.desktop #jumpBtn { display: none; }

/* ---------- Modals & Overlays ---------- */
.modal {
  position: fixed; inset: 0; z-index: 30; display: none;
  align-items: center; justify-content: center; padding: 20px;
  background: rgba(15, 10, 25, 0.6); backdrop-filter: blur(8px);
  -webkit-backdrop-filter: blur(8px);
}
.modal.show { display: flex; animation: fade .3s ease; }
.modal-card {
  background: linear-gradient(160deg, rgba(45, 28, 75, 0.95), rgba(25, 15, 45, 0.98));
  border: 1px solid rgba(255, 255, 255, 0.15);
  border-radius: 24px; width: 100%; max-width: 420px;
  padding: 24px; box-shadow: 0 20px 50px rgba(0,0,0,0.5);
  color: #fff; transform: scale(0.9); animation: popUp 0.3s cubic-bezier(0.34, 1.56, 0.64, 1) forwards;
}
.modal-header {
  display: flex; align-items: center; justify-content: space-between;
  margin-bottom: 20px; border-bottom: 1px solid rgba(255,255,255,0.1);
  padding-bottom: 12px;
}
.modal-header h3 { font-weight: 900; font-size: 20px; letter-spacing: 0.5px; }
.close-btn {
  background: none; border: none; color: rgba(255,255,255,0.6);
  font-size: 28px; cursor: pointer; transition: color 0.2s;
  line-height: 1; padding: 4px;
}
.close-btn:hover { color: #fff; }

.stage-grid {
  display: grid; grid-template-columns: repeat(5, 1fr); gap: 10px;
}
@media (max-width: 400px) {
  .stage-grid { grid-template-columns: repeat(4, 1fr); }
}
.stage-item {
  aspect-ratio: 1; border-radius: 14px; border: 1px solid rgba(255,255,255,0.1);
  background: rgba(255,255,255,0.05); color: #fff; font-family: inherit;
  font-weight: 800; font-size: 16px; display: flex; flex-direction: column;
  align-items: center; justify-content: center; gap: 4px; cursor: pointer;
  transition: all 0.2s cubic-bezier(0.25, 0.8, 0.25, 1); position: relative;
}
.stage-item:hover:not(.locked) {
  background: rgba(255, 255, 255, 0.15);
  border-color: rgba(255, 255, 255, 0.3);
  transform: translateY(-2px);
}
.stage-item.active {
  background: linear-gradient(135deg, #ffe27a, #ff9d6c);
  color: #1a1230; border-color: #ffe27a;
  box-shadow: 0 0 12px rgba(255, 226, 122, 0.4);
}
.stage-item.locked {
  opacity: 0.4; cursor: not-allowed; background: rgba(0,0,0,0.2);
}
.stage-item .status-icon {
  font-size: 11px; opacity: 0.8;
}

/* ---------- Win banner ---------- */
#win {
  position: fixed; inset: 0; z-index: 20; display: none;
  align-items: center; justify-content: center; padding: 24px;
  background: radial-gradient(circle at 50% 40%, rgba(40,20,70,.30), rgba(20,10,40,.66));
  backdrop-filter: blur(4px); -webkit-backdrop-filter: blur(4px);
}
#win.show { display: flex; animation: fade .5s ease; }
.win-card {
  text-align: center; color: #fff; padding: 32px 36px; border-radius: 26px;
  background: linear-gradient(160deg, rgba(70,42,110,.95), rgba(40,22,70,.98));
  border: 1px solid rgba(255,255,255,.2); box-shadow: 0 24px 60px rgba(10,4,30,.6);
  max-width: 360px; width: 100%;
  transform: scale(0.9); animation: popUp 0.4s cubic-bezier(0.34, 1.56, 0.64, 1) forwards;
}
.win-emoji { font-size: 56px; filter: drop-shadow(0 6px 16px rgba(0,0,0,0.4)); }
.win-title { font-weight: 900; font-size: 24px; margin-top: 12px; }
.win-sub { font-weight: 700; font-size: 15px; opacity: .82; margin-top: 8px; line-height: 1.4; }
.win-buttons {
  margin-top: 24px; display: flex; flex-direction: column; gap: 10px;
}
.btn-primary {
  font-family: inherit; font-weight: 900; font-size: 16px; color: #3a1860;
  border: none; padding: 14px 28px; border-radius: 999px; cursor: pointer;
  background: linear-gradient(135deg, #ffe27a, #ffb86c);
  box-shadow: 0 8px 20px rgba(255,170,80,.35);
  transition: transform 0.15s, filter 0.15s;
}
.btn-primary:active { transform: scale(.96); }
.btn-secondary {
  font-family: inherit; font-weight: 800; font-size: 14px; color: rgba(255,255,255,0.85);
  border: 1px solid rgba(255,255,255,0.2); padding: 10px 20px; border-radius: 999px; cursor: pointer;
  background: rgba(255,255,255,0.05); transition: all 0.2s;
}
.btn-secondary:hover { background: rgba(255,255,255,0.1); color: #fff; }
.btn-secondary:active { transform: scale(.97); }

#confetti { position: fixed; inset: 0; z-index: 19; pointer-events: none; overflow: hidden; }
.confetti-bit { position: absolute; width: 10px; height: 14px; border-radius: 2px; will-change: transform, opacity; }

/* ---------- Loading ---------- */
#loader {
  position: fixed; inset: 0; z-index: 50; display: flex; align-items: center; justify-content: center;
  background: linear-gradient(180deg, #2a1b4a 0%, #4a2a5e 45%, #c87a6a 100%);
  transition: opacity .7s ease; padding: 24px;
}
#loader.hide { opacity: 0; pointer-events: none; }
.loader-inner { text-align: center; color: #fff; width: 100%; max-width: 300px; }
.loader-emoji { font-size: 58px; animation: bob 2.4s ease-in-out infinite; }
.loader-title { font-weight: 900; font-size: 26px; margin-top: 6px; letter-spacing: .3px; }
.loader-bar {
  margin: 20px auto 0; width: 100%; height: 8px; border-radius: 999px;
  background: rgba(255,255,255,.18); overflow: hidden;
}
.loader-fill {
  height: 100%; width: 0%; border-radius: 999px;
  background: linear-gradient(90deg, #ffe27a, #ff9d6c); transition: width .3s ease;
}
.loader-sub { font-weight: 700; font-size: 13.5px; opacity: .85; margin-top: 12px; }

@keyframes bob { 0%,100% { transform: translateY(0); } 50% { transform: translateY(-12px); } }
@keyframes fade { from { opacity: 0; } to { opacity: 1; } }
@keyframes popUp { from { transform: scale(0.85); opacity: 0; } to { transform: scale(1); opacity: 1; } }