:root {
  --plum: #3d1a4f;
  --plum-d: #26102f;
  --rasp: #ff4f8b;
  --mint: #2ec4b6;
  --lemon: #ffd23f;
  --icing: #fff3e6;
  --glass: rgba(42, 16, 54, 0.72);
  --sat: env(safe-area-inset-top, 0px);
  --sab: env(safe-area-inset-bottom, 0px);
  --sal: env(safe-area-inset-left, 0px);
  --sar: env(safe-area-inset-right, 0px);
  --display: "Lilita One", "Arial Black", system-ui, sans-serif;
  --ui: "Fredoka", "Trebuchet MS", system-ui, sans-serif;
}

* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
html, body {
  margin: 0; height: 100%; overflow: hidden;
  background: var(--plum-d);
  color: var(--icing);
  font-family: var(--ui);
  touch-action: none;
  user-select: none; -webkit-user-select: none;
  overscroll-behavior: none;
}
button { font-family: inherit; color: inherit; border: 0; background: none; cursor: pointer; touch-action: manipulation; }
[hidden] { display: none !important; }

#scene { position: fixed; inset: 0; width: 100%; height: 100%; display: block; outline: none; }
#vig {
  position: fixed; inset: 0; pointer-events: none;
  background: radial-gradient(ellipse 120% 95% at 50% 58%, transparent 52%, rgba(38, 12, 52, 0.38) 82%, rgba(30, 8, 42, 0.62) 100%);
}
#flash { position: fixed; inset: 0; pointer-events: none; opacity: 0; background: radial-gradient(circle at 50% 60%, rgba(255, 210, 63, 0.28), rgba(255, 79, 139, 0.12) 60%, transparent 80%); transition: opacity 0.35s ease; }
#flash.hit { background: radial-gradient(circle at 50% 60%, transparent 35%, rgba(255, 60, 110, 0.38) 100%); }

/* ---------------- shared type */
.stroke { paint-order: stroke fill; -webkit-text-stroke: 6px var(--plum); }

/* ---------------- HUD */
#hud { position: fixed; inset: 0; pointer-events: none; }
#hudTop {
  position: absolute; top: calc(var(--sat) + 10px); left: calc(var(--sal) + 12px); right: calc(var(--sar) + 12px);
  display: flex; justify-content: space-between; align-items: flex-start;
}
#hudLeft { display: flex; flex-direction: column; gap: 8px; align-items: flex-start; }
#placeBadge {
  font-family: var(--display); font-size: 58px; line-height: 0.9; letter-spacing: 0.5px;
  color: var(--icing);
  paint-order: stroke fill; -webkit-text-stroke: 7px var(--plum);
  filter: drop-shadow(0 4px 0 rgba(38, 12, 52, 0.55));
  transform-origin: 20% 60%;
}
#placeBadge #placeSuf { font-size: 0.45em; margin-left: 2px; vertical-align: top; position: relative; top: 4px; }
#placeBadge.p1 { color: var(--lemon); }
#placeBadge.p2 { color: #e9f6ff; }
#placeBadge.p3 { color: #ffb070; }
#placeBadge.pop { animation: pop 0.45s cubic-bezier(.2, 1.6, .4, 1); }
@keyframes pop { 0% { transform: scale(1); } 35% { transform: scale(1.32) rotate(-4deg); } 100% { transform: scale(1); } }

#minimap {
  width: 128px; height: 128px;
  background: radial-gradient(circle at 50% 45%, rgba(61, 26, 79, 0.55), rgba(38, 12, 52, 0.35));
  border-radius: 22px;
  border: 2px solid rgba(255, 243, 230, 0.16);
}

#hudRight { display: flex; flex-direction: column; align-items: flex-end; gap: 6px; }
#lapChip {
  font-family: var(--display); font-size: 22px;
  background: var(--glass); padding: 5px 14px 4px; border-radius: 999px;
  border: 2px solid rgba(255, 243, 230, 0.16);
}
#lapChip b { color: var(--lemon); font-weight: 400; font-size: 26px; }
#lapChip span { opacity: 0.7; }
#lapChip.final { background: var(--rasp); border-color: var(--icing); animation: pulse 1s ease-in-out infinite; }
@keyframes pulse { 50% { transform: scale(1.07); } }
#timer {
  font-family: var(--ui); font-weight: 700; font-size: 20px; font-variant-numeric: tabular-nums;
  paint-order: stroke fill; -webkit-text-stroke: 4px var(--plum);
  padding-right: 4px;
}
#hudBtns { display: flex; gap: 8px; margin-top: 2px; pointer-events: auto; }
.icon-btn {
  width: 42px; height: 42px; border-radius: 14px;
  display: grid; place-items: center;
  background: var(--glass); border: 2px solid rgba(255, 243, 230, 0.18);
  color: var(--icing);
}
.icon-btn:active { transform: scale(0.92); }
.icon-btn .slash { display: none; }
.icon-btn.muted .slash { display: block; }
.icon-btn.muted .wave { display: none; }

#banner, #kudos, #count {
  position: absolute; left: 0; right: 0; text-align: center; pointer-events: none;
  font-family: var(--display); color: var(--icing);
  paint-order: stroke fill;
}
#banner {
  top: 26%; font-size: clamp(42px, 9vw, 86px); -webkit-text-stroke: 9px var(--plum);
  filter: drop-shadow(0 6px 0 rgba(38, 12, 52, 0.5));
  opacity: 0;
}
#banner.show { animation: banner 1.9s cubic-bezier(.2, 1.4, .4, 1) forwards; }
@keyframes banner {
  0% { opacity: 0; transform: scale(0.4) rotate(-6deg); }
  14% { opacity: 1; transform: scale(1.08) rotate(2deg); }
  24% { transform: scale(1) rotate(0); }
  82% { opacity: 1; transform: scale(1); }
  100% { opacity: 0; transform: scale(1.12) translateY(-20px); }
}
#kudos {
  top: calc(var(--sat) + 170px); font-size: clamp(26px, 5vw, 40px); -webkit-text-stroke: 6px var(--plum);
  opacity: 0;
}
#kudos.show { animation: kudos 1.2s cubic-bezier(.2, 1.5, .4, 1) forwards; }
#kudos.show.long { animation-duration: 2.2s; }
#kudos.mint { color: #8ff0e4; } #kudos.pink { color: #ff9ec4; } #kudos.gold { color: var(--lemon); } #kudos.hot { color: var(--rasp); }
@keyframes kudos {
  0% { opacity: 0; transform: translateY(16px) scale(0.6); }
  18% { opacity: 1; transform: translateY(0) scale(1.15); }
  30% { transform: scale(1); }
  78% { opacity: 1; }
  100% { opacity: 0; transform: translateY(-26px) scale(1); }
}
#count {
  top: 30%; font-size: clamp(90px, 22vw, 180px); -webkit-text-stroke: 12px var(--plum);
  filter: drop-shadow(0 8px 0 rgba(38, 12, 52, 0.45));
  opacity: 0;
}
#count.show { animation: count 0.82s cubic-bezier(.2, 1.6, .4, 1) forwards; }
#count.go { color: var(--lemon); }
@keyframes count {
  0% { opacity: 0; transform: scale(2.2); }
  20% { opacity: 1; transform: scale(0.92); }
  34% { transform: scale(1.04); }
  80% { opacity: 1; transform: scale(1); }
  100% { opacity: 0; transform: scale(0.8); }
}
#wrongWay {
  position: absolute; top: 40%; left: 50%; transform: translateX(-50%);
  background: var(--rasp); color: var(--icing); font-family: var(--display); font-size: 30px;
  padding: 8px 22px; border-radius: 999px; border: 3px solid var(--icing);
  animation: pulse 0.6s ease-in-out infinite;
}
#hint {
  position: absolute; left: 50%; bottom: calc(var(--sab) + 150px); transform: translateX(-50%);
  max-width: min(92vw, 520px); text-align: center;
  background: var(--glass); border: 2px solid rgba(255, 243, 230, 0.16);
  padding: 8px 16px; border-radius: 999px; font-weight: 600; font-size: 16px;
  opacity: 0; transition: opacity 0.5s ease;
}
#hint.show { opacity: 1; }

#driftMeter {
  position: absolute; left: 50%; transform: translateX(-50%);
  bottom: calc(var(--sab) + 26px);
  width: min(240px, 46vw);
  opacity: 0.55; transition: opacity 0.2s;
}
#driftMeter.on { opacity: 1; }
#driftTrack {
  position: relative; height: 16px; border-radius: 999px; overflow: hidden;
  background: rgba(38, 12, 52, 0.7); border: 2px solid rgba(255, 243, 230, 0.35);
}
#driftFill { position: absolute; left: 0; top: 0; bottom: 0; width: 0%; border-radius: 999px; background: #fff3e6; }
#driftFill.t1 { background: var(--mint); box-shadow: 0 0 12px var(--mint); }
#driftFill.t2 { background: var(--rasp); box-shadow: 0 0 12px var(--rasp); }
#driftFill.t3 { background: var(--lemon); box-shadow: 0 0 14px var(--lemon); }
#driftTrack i { position: absolute; top: 2px; bottom: 2px; width: 2px; background: rgba(255, 243, 230, 0.5); }

#itemBtn {
  position: absolute; right: calc(var(--sar) + 18px); bottom: calc(var(--sab) + 22px);
  width: 92px; height: 92px; border-radius: 50%;
  background: radial-gradient(circle at 35% 30%, rgba(255, 255, 255, 0.25), transparent 45%), var(--glass);
  border: 4px solid rgba(255, 210, 63, 0.55);
  display: grid; place-items: center; pointer-events: auto;
  box-shadow: 0 6px 0 rgba(38, 12, 52, 0.5);
}
#itemBtn.has { border-color: var(--lemon); animation: ready 1.4s ease-in-out infinite; }
#itemBtn.rolling #itemIcon { animation: roll 0.12s linear infinite; }
@keyframes ready { 50% { box-shadow: 0 6px 0 rgba(38, 12, 52, 0.5), 0 0 0 8px rgba(255, 210, 63, 0.18); } }
@keyframes roll { 50% { transform: translateY(-4px) scale(0.9); } }
#itemIcon { width: 60px; height: 60px; display: grid; place-items: center; }
#itemIcon svg { width: 100%; height: 100%; }
#itemIcon:empty::after { content: ""; width: 26px; height: 26px; border-radius: 9px; border: 3px dashed rgba(255, 243, 230, 0.35); }
#itemCount {
  position: absolute; right: -4px; top: -4px; min-width: 28px; height: 28px; border-radius: 14px;
  background: var(--rasp); font-family: var(--display); font-size: 18px; display: grid; place-items: center;
  border: 2px solid var(--icing);
}
#itemCount:empty { display: none; }

#brakeBtn {
  position: absolute; left: calc(var(--sal) + 18px); bottom: calc(var(--sab) + 26px);
  width: 76px; height: 76px; border-radius: 26px;
  background: var(--glass); border: 3px solid rgba(255, 243, 230, 0.28);
  display: grid; place-items: center; align-content: center; gap: 0;
  pointer-events: auto; color: var(--icing);
  box-shadow: 0 6px 0 rgba(38, 12, 52, 0.5);
}
#brakeBtn span { font-size: 12px; font-weight: 700; letter-spacing: 0.5px; margin-top: -2px; }
#brakeBtn.on { background: var(--rasp); transform: translateY(3px); box-shadow: 0 3px 0 rgba(38, 12, 52, 0.5); }
/* Play appears once the loader has faded (so captures never catch the loader) */
body.booting #playBtn { visibility: hidden; }

/* the brake pad belongs to touch devices (set from JS), whatever key was last pressed */
body:not(.touch) #brakeBtn { display: none; }

#stick { position: absolute; left: 0; top: 0; width: 0; height: 0; opacity: 0; transition: opacity 0.15s; }
#stick.on { opacity: 1; }
#stickOuter, #stickInner, #stickKnob { position: absolute; border-radius: 50%; transform: translate(-50%, -50%); }
#stickOuter { border: 3px solid rgba(255, 243, 230, 0.4); background: rgba(38, 12, 52, 0.2); }
#stickInner { border: 2px dashed rgba(255, 243, 230, 0.35); }
#stickKnob { width: 54px; height: 54px; background: var(--icing); box-shadow: 0 4px 0 rgba(38, 12, 52, 0.5); border: 4px solid var(--plum); }
#stick.d0 #stickOuter { border-color: #fff3e6; }
#stick.d1 #stickOuter, #stick.d1 #stickKnob { border-color: var(--mint); }
#stick.d2 #stickOuter, #stick.d2 #stickKnob { border-color: var(--rasp); }
#stick.d3 #stickOuter, #stick.d3 #stickKnob { border-color: var(--lemon); }
#stick.drift #stickOuter { background: rgba(255, 243, 230, 0.12); border-width: 5px; }

/* ---------------- overlays */
.overlay { position: fixed; inset: 0; display: flex; }

#menu {
  flex-direction: column; justify-content: space-between; align-items: center;
  padding: calc(var(--sat) + 22px) calc(var(--sar) + 16px) calc(var(--sab) + 18px) calc(var(--sal) + 16px);
  background: linear-gradient(180deg, rgba(38, 12, 52, 0.55) 0%, rgba(38, 12, 52, 0) 30%, rgba(38, 12, 52, 0) 46%, rgba(38, 12, 52, 0.82) 78%);
  transition: opacity 0.4s ease;
}
#menu.out { opacity: 0; pointer-events: none; }
.menu-top { text-align: center; animation: drop 0.9s cubic-bezier(.2, 1.4, .4, 1) both; }
@keyframes drop { 0% { transform: translateY(-50px) scale(0.8); opacity: 0; } 100% { transform: none; opacity: 1; } }
.logo {
  margin: 0; font-family: var(--display); font-weight: 400; line-height: 0.82;
  display: flex; flex-direction: column; align-items: center;
}
.logo .l1 {
  font-size: clamp(60px, 15vw, 112px); color: var(--icing);
  paint-order: stroke fill; -webkit-text-stroke: 12px var(--plum);
  filter: drop-shadow(0 7px 0 var(--rasp)) drop-shadow(0 10px 18px rgba(38, 12, 52, 0.5));
  letter-spacing: 1px;
}
.logo .l2 {
  margin-top: 6px; font-size: clamp(40px, 10vw, 66px); color: var(--plum);
  background: var(--lemon); padding: 6px 22px 2px; border-radius: 20px; border: 5px solid var(--plum);
  transform: rotate(-4deg); box-shadow: 0 6px 0 var(--rasp);
}
.tag {
  margin: 16px auto 0; max-width: 340px; font-weight: 600; font-size: 17px;
  paint-order: stroke fill; -webkit-text-stroke: 4px var(--plum);
}
.menu-card {
  width: min(440px, 100%);
  display: flex; flex-direction: column; gap: 12px; align-items: stretch;
  animation: rise 0.8s 0.1s cubic-bezier(.2, 1.2, .4, 1) both;
}
@keyframes rise { 0% { transform: translateY(60px); opacity: 0; } 100% { transform: none; opacity: 1; } }
.seg { display: flex; background: var(--glass); border-radius: 999px; padding: 4px; border: 2px solid rgba(255, 243, 230, 0.16); }
.seg-btn { flex: 1; padding: 10px 6px; border-radius: 999px; font-weight: 700; font-size: 16px; opacity: 0.75; }
.seg-btn.on { background: var(--icing); color: var(--plum); opacity: 1; }
.cups { display: grid; grid-template-columns: repeat(3, 1fr); gap: 8px; }
.cup {
  position: relative; padding: 10px 4px 8px; border-radius: 18px;
  background: var(--glass); border: 3px solid rgba(255, 243, 230, 0.14);
  display: flex; flex-direction: column; align-items: center; gap: 3px;
}
.cup .dot { width: 30px; height: 30px; border-radius: 50% 50% 46% 46% / 62% 62% 38% 38%; box-shadow: inset -4px -5px 0 rgba(0, 0, 0, 0.18), inset 4px 4px 0 rgba(255, 255, 255, 0.35); }
.cup .name { font-family: var(--display); font-size: 17px; letter-spacing: 0.3px; }
.cup .cstars { font-size: 12px; letter-spacing: 1px; color: var(--lemon); min-height: 14px; }
.cup .cstars .off { color: rgba(255, 243, 230, 0.25); }
.cup.on { border-color: var(--rasp); background: rgba(255, 79, 139, 0.24); }
.cup.locked { opacity: 0.55; }
.cup.locked .dot { filter: grayscale(1) brightness(0.7); }
.cup .lock { position: absolute; top: 4px; right: 8px; font-size: 13px; }
.primary {
  font-family: var(--display); font-size: 30px; letter-spacing: 0.5px;
  padding: 12px 20px 10px; min-height: 64px; border-radius: 22px;
  background: linear-gradient(180deg, #ff6b9e, var(--rasp));
  border: 4px solid var(--plum); color: var(--icing);
  paint-order: stroke fill; -webkit-text-stroke: 4px var(--plum);
  box-shadow: 0 7px 0 var(--plum), 0 12px 24px rgba(38, 12, 52, 0.4);
  transition: transform 0.08s;
}
.primary:active { transform: translateY(5px); box-shadow: 0 2px 0 var(--plum); }
.ghost {
  padding: 11px 14px; border-radius: 16px; font-weight: 700; font-size: 16px;
  background: var(--glass); border: 2px solid rgba(255, 243, 230, 0.22);
}
.ghost.small { font-size: 14px; padding: 8px 12px; }
.menu-best { text-align: center; font-weight: 600; font-size: 15px; min-height: 18px; paint-order: stroke fill; -webkit-text-stroke: 3px var(--plum); }
.menu-best b { color: var(--lemon); }
.menu-hints { display: flex; flex-direction: column; gap: 2px; text-align: center; font-size: 13px; font-weight: 500; opacity: 0.85; paint-order: stroke fill; -webkit-text-stroke: 3px var(--plum); }

.sheet { align-items: flex-start; justify-content: center; background: rgba(30, 8, 42, 0.5); padding: calc(var(--sat) + 16px) 16px calc(var(--sab) + 16px); overflow-y: auto; touch-action: pan-y; animation: fade 0.25s ease both; }
.sheet > .sheet-card { margin: auto 0; }
@keyframes fade { from { opacity: 0; } }
.sheet-card {
  width: min(400px, 100%);
  background: linear-gradient(180deg, rgba(74, 32, 94, 0.95), rgba(42, 16, 54, 0.96));
  border: 3px solid rgba(255, 243, 230, 0.2); border-radius: 30px;
  padding: 22px 20px 20px; display: flex; flex-direction: column; gap: 12px; align-items: stretch;
  box-shadow: 0 18px 40px rgba(20, 4, 28, 0.55);
  animation: rise 0.45s cubic-bezier(.2, 1.3, .4, 1) both;
}
.sheet-card h2 { margin: 0 0 4px; text-align: center; font-family: var(--display); font-weight: 400; font-size: 40px; paint-order: stroke fill; -webkit-text-stroke: 6px var(--plum); }
.sheet-card p { margin: 0; text-align: center; }
.row { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; }
.row > [hidden] + button, .row > button:only-child { grid-column: 1 / -1; }
.controls { font-size: 13px; opacity: 0.8; text-align: center; line-height: 1.5; }

.results { text-align: center; gap: 10px; }
.res-place { font-family: var(--display); font-size: 96px; line-height: 0.9; color: var(--lemon); paint-order: stroke fill; -webkit-text-stroke: 10px var(--plum); filter: drop-shadow(0 6px 0 var(--rasp)); }
.res-place.mid { color: var(--icing); }
.res-place #resSuf { font-size: 0.42em; vertical-align: top; position: relative; top: 8px; }
.res-title { font-family: var(--display); font-size: 26px; }
.stars { display: flex; justify-content: center; gap: 8px; }
.stars i {
  width: 34px; height: 34px; background: rgba(255, 243, 230, 0.2);
  clip-path: polygon(50% 0, 61% 35%, 98% 35%, 68% 57%, 79% 91%, 50% 70%, 21% 91%, 32% 57%, 2% 35%, 39% 35%);
}
.stars i.on { background: var(--lemon); animation: starpop 0.5s cubic-bezier(.2, 1.8, .4, 1) both; }
.stars i.on:nth-child(2) { animation-delay: 0.15s; } .stars i.on:nth-child(3) { animation-delay: 0.3s; }
@keyframes starpop { 0% { transform: scale(0) rotate(-90deg); } 100% { transform: none; } }
.res-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 6px; margin: 4px 0; }
.res-grid div { background: rgba(255, 243, 230, 0.08); border-radius: 14px; padding: 7px 8px; }
.res-grid small { display: block; font-size: 12px; opacity: 0.7; font-weight: 600; }
.res-grid b { font-size: 19px; font-variant-numeric: tabular-nums; }
.res-new { color: var(--lemon); font-weight: 700; }
.standings { list-style: none; margin: 0; padding: 0; display: grid; gap: 3px; text-align: left; }
.standings li { display: grid; grid-template-columns: 22px 14px 1fr auto; align-items: center; gap: 8px; padding: 4px 10px; border-radius: 11px; font-weight: 600; font-size: 15px; }
.standings li.me { background: rgba(255, 79, 139, 0.3); }
.standings .pos { font-family: var(--display); font-size: 17px; color: var(--lemon); text-align: center; }
.standings i { width: 12px; height: 12px; border-radius: 50%; border: 2px solid var(--plum); }
.standings .t { font-variant-numeric: tabular-nums; opacity: 0.85; font-size: 14px; }

#lost { align-items: center; justify-content: center; background: rgba(30, 8, 42, 0.9); z-index: 50; }

/* ---------------- loader */
#loader {
  position: fixed; inset: 0; z-index: 40;
  display: grid; place-items: center;
  background: radial-gradient(circle at 50% 40%, #5a2a70, var(--plum-d) 70%);
  transition: opacity 0.6s ease;
}
#loader.done { opacity: 0; pointer-events: none; }
.loader-inner { display: flex; flex-direction: column; align-items: center; gap: 14px; }
.gumdrop { position: relative; width: 110px; height: 110px; }
.gumdrop .drop {
  position: absolute; left: 15px; bottom: 14px; width: 80px; height: 74px;
  background: radial-gradient(circle at 35% 30%, #ffb3cf, var(--rasp) 45%, #c8245e);
  border-radius: 50% 50% 44% 44% / 64% 64% 36% 36%;
  transform-origin: 50% 100%;
  animation: melt 1.6s ease-in-out infinite;
}
.gumdrop .puddle {
  position: absolute; left: 50%; bottom: 8px; width: 70px; height: 16px; transform: translateX(-50%);
  background: radial-gradient(ellipse at 50% 40%, #ff7aa8, #c8245e);
  border-radius: 50%;
  animation: puddle 1.6s ease-in-out infinite;
}
@keyframes melt { 0%, 100% { transform: scale(1, 1); } 50% { transform: scale(1.18, 0.72); } }
@keyframes puddle { 0%, 100% { width: 70px; opacity: 0.7; } 50% { width: 110px; opacity: 1; } }
.loader-title { font-family: var(--display); font-size: 40px; paint-order: stroke fill; -webkit-text-stroke: 6px var(--plum-d); }
.loader-bar { width: 200px; height: 12px; border-radius: 999px; background: rgba(255, 243, 230, 0.12); overflow: hidden; border: 2px solid rgba(255, 243, 230, 0.2); }
.loader-fill { height: 100%; width: 0%; background: linear-gradient(90deg, var(--mint), var(--lemon), var(--rasp)); transition: width 0.3s ease; }
.loader-sub { font-weight: 600; opacity: 0.8; }

/* ---------------- portrait / small screens */
@media (max-width: 600px) {
  #placeBadge { font-size: 48px; }
  #minimap { width: 92px; height: 92px; border-radius: 18px; }
  #lapChip { font-size: 18px; padding: 4px 11px 3px; }
  #lapChip b { font-size: 22px; }
  #timer { font-size: 17px; }
  .icon-btn { width: 40px; height: 40px; }
  #kudos { top: calc(var(--sat) + 182px); }
  #hint { bottom: calc(var(--sab) + 136px); font-size: 14px; }
  #itemBtn { width: 86px; height: 86px; right: calc(var(--sar) + 14px); }
  .tag { font-size: 15px; }
}
@media (max-height: 520px) {
  .menu-top .logo .l1 { font-size: 56px; }
  .menu-top .logo .l2 { font-size: 34px; }
  .tag { display: none; }
  .menu-card { gap: 8px; }
  .primary { min-height: 52px; font-size: 26px; }
}
