@import url('https://fonts.googleapis.com/css2?family=Fredoka:wght@500;600;700&family=Nunito:wght@800;900&display=swap');

:root {
  --water:      #2244dc;
  --water-hi:   #2e5ae9;
  --water-lo:   #1c3bce;
  --plank:      #4b555f;
  --plank-side: #31373d;
  --ribbon:     #13216d;
  
  --green:      #22e63c;
  --pill:       #0b1340;
  --white:      #fffdfd;
  --red:        #ff4d5e;
  --finish:     #4b5a68;

  --f-round: 'Fredoka', 'Arial Rounded MT Bold', 'Nunito', system-ui, sans-serif;
  --f-fat: 'Nunito', 'Fredoka', system-ui, sans-serif;
  --char-step: 34px;
}

* { margin: 0; padding: 0; box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
html, body { height: 100%; }

body {
  font-family: var(--f-round);
  background: var(--water-lo);
  display: flex; justify-content: center;
  overflow: hidden; user-select: none; -webkit-user-select: none;
  color: var(--white);
  transition: background 0.8s;
}

#stage {
  position: relative; width: min(100vw, 560px); height: 100dvh;
  overflow: hidden;
  background-size: 220px 220px;
  animation: waterdrift 26s linear infinite;
  transition: background-image 0.8s;
}
@keyframes waterdrift { to { background-position: 220px 110px; } }

.screen { position: absolute; inset: 0; display: none; flex-direction: column; }
.screen.active { display: flex; }

.pill {
  background: var(--pill); color: var(--white);
  border-radius: 999px; padding: .45em 1.1em;
  font-weight: 600; letter-spacing: .04em;
  box-shadow: 0 4px 14px rgba(0,0,0,.45);
}

.btn {
  font-family: var(--f-round); font-size: clamp(18px, 5vw, 24px); font-weight: 600;
  border: none; cursor: pointer; color: var(--white);
  background: var(--pill); border-radius: 999px;
  padding: .72em 2.2em; min-width: min(74vw, 320px);
  box-shadow: 0 6px 0 rgba(0,0,20,.35), 0 10px 22px rgba(4,8,40,.4);
  transition: transform .08s ease, box-shadow .08s ease, filter .15s;
  letter-spacing: .03em;
}
.btn:hover { filter: brightness(1.12); }
.btn:active { transform: translateY(4px); box-shadow: 0 2px 0 rgba(0,0,20,.35); }
.btn:focus-visible { outline: 3px solid var(--green); outline-offset: 3px; }
.btn.primary { background: linear-gradient(#2df04a, #17c531); color: #04310c; text-shadow: 0 1px 0 rgba(255,255,255,.35); }
.btn.small { min-width: 0; font-size: 16px; padding: .55em 1.4em; }

.plank {
  position: absolute; background: var(--plank); border-radius: 10px;
  box-shadow: inset 0 3px 0 rgba(255,255,255,.10); transition: background 0.8s;
}
.plank::after {
  content: ''; position: absolute; left: 0; right: 0; top: 100%; height: 14px;
  background: var(--plank-side); border-radius: 0 0 10px 10px; transition: background 0.8s;
}

h2.title {
  font-size: clamp(26px, 7vw, 34px); font-weight: 700; text-align: center;
  letter-spacing: .06em; text-shadow: 0 3px 0 rgba(0,0,25,.35);
}

#menu { align-items: center; justify-content: center; gap: 14px; padding: 24px; }
#menuLogo { position: relative; width: min(86vw, 420px); height: 150px; margin-bottom: 10px; }
#menuLogo .plank { left: 0; right: 0; bottom: 0; height: 56px; }
#logoRibbon { position: absolute; bottom: 14px; left: 47%; right: -14px; height: 34px; background: var(--ribbon); border-radius: 8px; transition: background 0.8s; }
#logoText { position: absolute; bottom: 16px; left: 14px; right: 0; font-weight: 600; font-style: italic; font-size: 30px; letter-spacing: .42em; white-space: nowrap; }
#logoText .g { color: var(--green); } #logoText .w { color: var(--white); }
#menuChar { position: absolute; bottom: 52px; left: 16%; font-size: 56px; line-height: 1; filter: drop-shadow(0 8px 6px rgba(0,0,25,.4)); animation: bounce 1s infinite; }
#menuTag { position: absolute; bottom: 126px; left: 8%; display: flex; flex-direction: column; align-items: center; }
.nametag { display: flex; align-items: center; gap: .4em; font-size: 14px; font-weight: 700; text-transform: uppercase; }
.nametag .down { color: var(--green); font-size: 15px; margin-top: 2px; text-shadow: 0 2px 3px rgba(0,0,25,.5); }

#skins, #levels { padding: max(24px, env(safe-area-inset-top)) 20px 24px; gap: 20px; align-items: center; overflow: auto; }
#skinGrid { display: grid; grid-template-columns: repeat(3, 1fr); gap: 16px; width: min(92vw, 440px); }
.skinCard { position: relative; background: rgba(0,0,0,.4); border: 3px solid transparent; border-radius: 18px; padding: 20px 6px 10px; cursor: pointer; text-align: center; transition: transform .1s, border-color .15s, background .15s; }
.skinCard:hover { transform: translateY(-3px); }
.skinCard .mini { position: relative; height: 64px; display: flex; align-items: flex-end; justify-content: center; }
.skinCard .mini::after { content: ''; position: absolute; bottom: 0; left: 12%; right: 12%; height: 12px; background: var(--plank); border-radius: 5px; box-shadow: 0 5px 0 var(--plank-side); }
.skinCard .em { position: relative; z-index: 1; font-size: 42px; line-height: 1; margin-bottom: 8px; filter: drop-shadow(0 5px 4px rgba(0,0,25,.4)); }
.skinCard .nm { margin-top: 12px; font-size: 13px; font-weight: 600; letter-spacing: .05em; }
.skinCard.selected { border-color: var(--green); background: rgba(0,0,0,.6); }
.skinCard.selected::before { content: '▼'; position: absolute; top: -14px; left: 50%; transform: translateX(-50%); color: var(--green); font-size: 16px; text-shadow: 0 2px 3px rgba(0,0,25,.5); }

#levelGrid { display: flex; flex-direction: column; gap: 12px; width: min(92vw, 440px); padding-bottom: 40px; }
.lvlCard { display: flex; align-items: center; justify-content: space-between; background: rgba(0,0,0,.4); border: 2px solid transparent; border-radius: 16px; padding: 14px 18px; cursor: pointer; transition: transform .1s, background .1s; }
.lvlCard:hover { transform: scale(1.02); background: rgba(0,0,0,.6); }
.lvlCard.locked { opacity: 0.5; pointer-events: none; filter: grayscale(1); }
.lvlCard.active { border-color: var(--green); background: rgba(34,230,60,.15); }
.lvlCard .l-info { display: flex; flex-direction: column; }
.lvlCard .l-num { font-size: 12px; opacity: 0.8; text-transform: uppercase; letter-spacing: 0.1em; }
.lvlCard .l-name { font-size: 18px; font-weight: 700; text-shadow: 0 2px 2px rgba(0,0,0,0.5); }
.lvlCard .l-stats { text-align: right; font-size: 13px; opacity: 0.9; }
.lvlCard .l-stats b { color: var(--green); font-weight: 700; }

#race { flex-direction: column; }
#hud { position: relative; z-index: 5; display: flex; gap: 8px; justify-content: center; padding: max(12px, env(safe-area-inset-top)) 10px 6px; flex-wrap: wrap;}
.stat { min-width: 72px; text-align: center; padding: .35em .7em; }
.stat b { display: block; font-size: 18px; font-weight: 700; line-height: 1.05; }
.stat span { display: block; font-size: 9px; letter-spacing: .1em; opacity: .75; text-transform: uppercase; }

#scene { position: relative; flex: 1; overflow: hidden; }
#world { position: absolute; inset: 0; will-change: transform; }

.lane { position: absolute; left: 0; }
.racer { position: absolute; display: flex; flex-direction: column; align-items: center; transform: translateX(-50%); will-change: left; }
.racer .tag { margin-bottom: 2px; }
.racer .tag .nametag { font-size: 12px; }
.racer .em { line-height: 1; filter: drop-shadow(0 7px 5px rgba(0,0,25,.45)); animation: bounce .55s infinite; display: inline-block; }
.racer .crown { position: absolute; top: -14px; left: 58%; font-size: 22px; transform: rotate(18deg); filter: drop-shadow(0 3px 3px rgba(0,0,25,.5)); display: none; }
.racer.leader .crown { display: block; }
@keyframes bounce { 50% { translate: 0 -7%; } }

#letters span {
  position: absolute; bottom: 0; transform: translateX(-50%);
  font-weight: 600; font-style: italic; font-size: 26px; letter-spacing: 0;
  color: var(--white); transition: color .12s;
  text-shadow: 0 2px 3px rgba(0,0,30,.45);
}
#letters span.done { color: var(--green); }
#letters span.cur { color: #fff; }
#letters span.cur::after { content: ''; position: absolute; left: 15%; right: 15%; bottom: -7px; height: 4px; border-radius: 2px; background: var(--green); animation: blink .8s steps(1) infinite; }
#letters span.err { color: var(--red); font-weight: 700; }
@keyframes blink { 50% { opacity: 0; } }

#ribbon { position: absolute; background: var(--ribbon); border-radius: 8px; will-change: left, width; transition: background 0.8s;}

#finishPad { position: absolute; top: -40px; bottom: -40px; background: var(--finish); border-radius: 16px 0 0 16px; box-shadow: inset 6px 0 0 rgba(255,255,255,.08); }
#finishPad .checker { position: absolute; left: 10px; top: 0; bottom: 0; width: 34px; border-radius: 8px; background: repeating-conic-gradient(#17171b 0 25%, #f5f4f6 0 50%) 0 0/34px 34px; box-shadow: 0 0 0 3px rgba(10,10,14,.35); }
#finishPad .word { position: absolute; left: 64px; top: 50%; transform: translateY(-50%) rotate(90deg); transform-origin: left center; font-family: var(--f-fat); font-weight: 900; font-size: 44px; letter-spacing: .18em; color: var(--white); text-shadow: 0 3px 0 rgba(0,0,15,.4); }

#count { position: absolute; inset: 0; display: none; align-items: center; justify-content: center; z-index: 20; pointer-events: none; }
#count b { font-family: var(--f-fat); font-weight: 900; font-size: clamp(90px, 30vw, 150px); color: var(--white); text-shadow: 0 8px 0 rgba(0,0,30,.4), 0 14px 30px rgba(0,0,30,.45); animation: pop .9s ease both; }
#count b.go { color: var(--green); }
@keyframes pop { 0% { transform: scale(.2); opacity: 0; } 45% { transform: scale(1.15); opacity: 1; } 70% { transform: scale(1); } 100% { transform: scale(1); opacity: .9; } }

#typeInput { position: absolute; top: 10px; left: 10px; opacity: 0; width: 1px; height: 1px; font-size: 16px; border: none; pointer-events: none; }

#result { position: absolute; inset: 0; display: none; align-items: center; justify-content: center; background: rgba(0,0,0,.65); backdrop-filter: blur(4px); z-index: 30; padding: 20px; }
#result.show { display: flex; }
#resultCard { width: min(92vw, 420px); background: var(--pill); border-radius: 26px; padding: 26px 22px calc(22px); text-align: center; box-shadow: 0 18px 50px rgba(0,0,0,.6); animation: pop .5s ease both; }
#resPlace { font-family: var(--f-fat); font-weight: 900; font-size: clamp(30px, 9vw, 40px); letter-spacing: .03em; }
#resSub { opacity: .85; margin: 4px 0 14px; font-size: 15px; }
#resStats { display: flex; gap: 8px; justify-content: center; margin-bottom: 16px; flex-wrap: wrap;}
#resStats .stat { background: rgba(255,255,255,.08); border-radius: 16px; flex: 1; padding: .6em .4em; min-width: 30%;}
#board { margin: 0 0 18px; display: flex; flex-direction: column; gap: 7px; }
.row { display: flex; align-items: center; gap: 10px; background: rgba(255,255,255,.07); border-radius: 12px; padding: 7px 12px; font-size: 15px; font-weight: 600; }
.row.me { background: rgba(34,230,60,.18); box-shadow: inset 0 0 0 2px rgba(34,230,60,.55); }
.row .pl { width: 2.1em; font-family: var(--f-fat); font-weight: 900; }
.row .fl { font-size: 18px; }
.row .nm { flex: 1; text-align: left; text-transform: uppercase; letter-spacing: .05em; }
.row .wp { opacity: .8; font-size: 13px; }
#resultBtns { display: flex; gap: 10px; justify-content: center; flex-wrap: wrap; }

#confetti { position: absolute; inset: 0; pointer-events: none; z-index: 40; }
.back { position: absolute; top: max(14px, env(safe-area-inset-top)); left: 14px; font-size: 15px; cursor: pointer; border: none; background: rgba(0,0,0,0.5); border-radius: 8px; padding: 8px 12px; }
.back:hover { background: rgba(0,0,0,0.7); }

@media (prefers-reduced-motion: reduce) {
  #stage { animation: none; transition: none; }
  .racer .em, #menuChar { animation: none; }
  #letters span.cur::after { animation: none; }
  body, .plank, .plank::after, #logoRibbon, #ribbon { transition: none; }
}
