:root {
  --bg: #0b0806;
  --text: #fff4e4;
  --muted: #c9b8a2;
  --amber: #ffb347;
  --gold: #ffd166;
  --panel: rgba(24, 16, 11, 0.78);
  --edge: rgba(255, 190, 120, 0.25);
  --danger: #ff5a4f;
  --safe-top: env(safe-area-inset-top, 0px);
  --safe-bottom: env(safe-area-inset-bottom, 0px);
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  height: 100%;
  overflow: hidden;
  background: var(--bg);
  color: var(--text);
  font-family: 'Fredoka', system-ui, sans-serif;
  -webkit-user-select: none;
  user-select: none;
  -webkit-tap-highlight-color: transparent;
}

#scene {
  position: fixed;
  inset: 0;
  width: 100%;
  height: 100%;
  display: block;
  touch-action: none; /* we handle drag ourselves */
  cursor: none;
}

.hidden { display: none !important; }

/* ---------- HUD ---------- */
.hud {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  padding: calc(10px + var(--safe-top)) 16px 10px;
  pointer-events: none;
  text-shadow: 0 2px 6px rgba(0, 0, 0, 0.7);
  z-index: 5;
}

.stats { display: flex; gap: 18px; }
.stat { display: flex; flex-direction: column; line-height: 1.05; }
.stat .label { font-size: 11px; letter-spacing: 0.12em; text-transform: uppercase; color: var(--muted); }
.stat .value { font-size: 26px; font-weight: 700; font-variant-numeric: tabular-nums; }
.stat.dim .value { color: var(--muted); font-size: 20px; }
.stat .value.bump { animation: bump 0.25s ease-out; }

.lives { font-size: 24px; color: #ff4d6d; letter-spacing: 2px; white-space: nowrap; }
.lives .empty { color: rgba(255, 255, 255, 0.18); }
.lives.hurt { animation: hurt 0.45s ease-out; }

.buttons { display: flex; gap: 8px; pointer-events: auto; }
.icon-btn {
  width: 42px;
  height: 42px;
  border-radius: 50%;
  border: 1px solid var(--edge);
  background: rgba(20, 13, 9, 0.6);
  color: var(--text);
  display: grid;
  place-items: center;
  cursor: pointer;
  padding: 0;
  touch-action: manipulation;
}
.icon-btn:hover { background: rgba(60, 38, 22, 0.75); }
.icon-btn svg { width: 22px; height: 22px; }
.icon-btn .fill { fill: currentColor; }
.icon-btn .wave, .icon-btn .cross { fill: none; stroke: currentColor; stroke-width: 2; stroke-linecap: round; }
.icon-btn .cross { display: none; }
.icon-btn.muted .wave { display: none; }
.icon-btn.muted .cross { display: inline; }

.rush {
  position: fixed;
  top: calc(70px + var(--safe-top));
  left: 50%;
  transform: translateX(-50%);
  padding: 6px 14px;
  border-radius: 999px;
  background: linear-gradient(90deg, #ffb300, #ffd166);
  color: #3a2200;
  font-weight: 700;
  box-shadow: 0 0 24px rgba(255, 196, 0, 0.6);
  pointer-events: none;
  z-index: 5;
  animation: glow 0.8s ease-in-out infinite alternate;
}

/* ---------- Pop-ups and banner ---------- */
.popups { position: fixed; inset: 0; pointer-events: none; z-index: 4; overflow: hidden; }
.popup {
  position: absolute;
  transform: translate(-50%, -50%);
  font-weight: 700;
  font-size: 26px;
  white-space: nowrap;
  text-shadow: 0 0 10px currentColor, 0 2px 4px rgba(0, 0, 0, 0.8);
  animation: rise 0.95s ease-out forwards;
}
.popup.big { font-size: 34px; }

.banner {
  position: fixed;
  left: 50%;
  top: 38%;
  transform: translate(-50%, -50%);
  text-align: center;
  pointer-events: none;
  opacity: 0;
  z-index: 6;
}
.banner.show { animation: banner 1.8s ease-out forwards; }
.banner-title {
  font-size: clamp(40px, 9vw, 76px);
  font-weight: 700;
  color: var(--gold);
  text-shadow: 0 0 24px rgba(255, 190, 60, 0.8), 0 4px 10px rgba(0, 0, 0, 0.8);
}
.banner-sub { font-size: 20px; color: var(--text); text-shadow: 0 2px 6px rgba(0, 0, 0, 0.8); }

/* ---------- Overlays ---------- */
.overlay {
  position: fixed;
  inset: 0;
  display: grid;
  place-items: center;
  padding: 16px;
  background: radial-gradient(ellipse at center, rgba(10, 6, 4, 0.35), rgba(5, 3, 2, 0.8));
  z-index: 10;
}

.panel {
  width: min(520px, 100%);
  max-height: calc(100% - 8px);
  overflow-y: auto;
  padding: 26px 24px 20px;
  border-radius: 20px;
  background: var(--panel);
  border: 1px solid var(--edge);
  box-shadow: 0 20px 60px rgba(0, 0, 0, 0.6), inset 0 1px 0 rgba(255, 220, 170, 0.08);
  -webkit-backdrop-filter: blur(6px);
  backdrop-filter: blur(6px);
  text-align: center;
}
.panel.small { width: min(360px, 100%); }
.panel h2 { margin: 0 0 14px; font-size: 32px; }
.panel p { margin: 10px 0; }

.title {
  margin: 0;
  font-size: clamp(40px, 11vw, 64px);
  line-height: 1;
  letter-spacing: 0.01em;
  background: linear-gradient(180deg, #fff1d6, #ffb347);
  -webkit-background-clip: text;
  background-clip: text;
  color: transparent;
  filter: drop-shadow(0 4px 12px rgba(255, 150, 50, 0.35));
}
.title span {
  background: linear-gradient(180deg, #b6f7ff, #6f8cff 60%, #c77dff);
  -webkit-background-clip: text;
  background-clip: text;
}
.tagline { color: var(--muted); font-size: 16px; }

.legend {
  list-style: none;
  padding: 0;
  margin: 14px 0;
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(130px, 1fr));
  gap: 6px 10px;
  text-align: left;
  font-size: 15px;
}
.legend li { display: flex; align-items: center; gap: 8px; }
.legend .gem {
  width: 16px;
  height: 16px;
  flex: none;
  clip-path: polygon(50% 0, 100% 40%, 50% 100%, 0 40%);
  background: linear-gradient(145deg, #fff 0%, var(--c) 55%);
  box-shadow: 0 0 10px var(--c);
}
.legend .rock { --c: #8a7d70; clip-path: polygon(20% 0, 85% 10%, 100% 60%, 70% 100%, 10% 90%, 0 35%); }
.legend .pts { margin-left: auto; color: var(--gold); font-weight: 600; }
.legend .bad { color: var(--danger); }

.controls { font-size: 14px; color: var(--muted); line-height: 1.5; }
.controls b { color: var(--text); font-weight: 600; }

.btn {
  display: block;
  width: 100%;
  margin: 12px 0 0;
  padding: 14px 20px;
  border: 0;
  border-radius: 14px;
  font: 700 22px 'Fredoka', system-ui, sans-serif;
  color: #2a1700;
  background: linear-gradient(180deg, #ffd27a, #ff9f2e);
  box-shadow: 0 6px 0 #a85a12, 0 10px 24px rgba(255, 140, 40, 0.35);
  cursor: pointer;
  touch-action: manipulation;
  transition: transform 0.08s ease, box-shadow 0.08s ease;
}
.btn:hover { filter: brightness(1.06); }
.btn:active { transform: translateY(4px); box-shadow: 0 2px 0 #a85a12, 0 4px 12px rgba(255, 140, 40, 0.35); }
.btn:focus-visible, .icon-btn:focus-visible { outline: 3px solid #fff; outline-offset: 3px; }
.btn.ghost {
  background: transparent;
  color: var(--text);
  border: 1px solid var(--edge);
  box-shadow: none;
  font-size: 18px;
  padding: 10px;
}

.best-line { color: var(--muted); font-size: 15px; }
.final { font-size: 24px; }
.final span { font-size: 40px; font-weight: 700; color: var(--gold); display: block; }
.new-best { color: var(--gold); font-weight: 700; animation: glow 0.7s ease-in-out infinite alternate; }

/* ---------- Animations ---------- */
@keyframes rise {
  0% { opacity: 0; transform: translate(-50%, -30%) scale(0.6); }
  15% { opacity: 1; transform: translate(-50%, -60%) scale(1.15); }
  100% { opacity: 0; transform: translate(-50%, -220%) scale(1); }
}
@keyframes banner {
  0% { opacity: 0; transform: translate(-50%, -50%) scale(0.5); }
  15% { opacity: 1; transform: translate(-50%, -50%) scale(1.1); }
  25% { transform: translate(-50%, -50%) scale(1); }
  75% { opacity: 1; }
  100% { opacity: 0; transform: translate(-50%, -70%) scale(1); }
}
@keyframes bump { 50% { transform: scale(1.25); } }
@keyframes hurt {
  20% { transform: translateX(-6px); color: #fff; }
  40% { transform: translateX(6px); }
  60% { transform: translateX(-4px); }
  80% { transform: translateX(3px); }
}
@keyframes glow { from { filter: brightness(1); } to { filter: brightness(1.25); } }

/* ---------- Small screens ---------- */
@media (max-width: 560px) {
  .hud { padding-left: 12px; padding-right: 12px; gap: 8px; }
  .stats { gap: 12px; }
  .stat .value { font-size: 21px; }
  .stat.dim { display: none; }
  .lives { font-size: 19px; letter-spacing: 0; }
  .icon-btn { width: 38px; height: 38px; }
  .panel { padding: 20px 16px 16px; }
  .legend { font-size: 14px; grid-template-columns: 1fr 1fr; }
  .rush { top: calc(62px + var(--safe-top)); font-size: 14px; }
}

@media (prefers-reduced-motion: reduce) {
  .rush, .new-best { animation: none; }
}
