/* ==========================================================================
   styles.css — Zippy Keys
   Colours live in :root below; change them there to re-theme the game.
   ========================================================================== */
:root {
  --ink: #23224a;            /* main text */
  --ink-soft: #5b5a85;
  --card: #ffffff;
  --sky-top: #6cc8ff;
  --sky-bottom: #d8f3ff;
  --green: #22c55e;
  --green-dark: #15803d;
  --yellow: #ffcf3f;
  --orange: #ff8c42;
  --pink: #ff5e8a;
  --blue: #3fa0ff;
  --purple: #8b5cf6;
  --done: #16a34a;           /* typed letters */
  --todo: #a3a8c3;           /* untyped letters */
  --current-bg: #fff2a8;     /* the next letter */
  --radius: 28px;
  --shadow: 0 8px 0 rgba(35, 34, 74, .12), 0 14px 30px rgba(35, 34, 74, .12);
  --font: 'Fredoka', 'Baloo 2', 'Comic Sans MS', 'Trebuchet MS', 'Segoe UI', sans-serif;
}

* { box-sizing: border-box; }
html, body { height: 100%; margin: 0; }
body {
  font-family: var(--font);
  color: var(--ink);
  background: var(--sky-bottom);
  overflow: hidden;
  user-select: none;
  -webkit-user-select: none;
}
button { font-family: inherit; color: inherit; cursor: pointer; border: 0; }
button:focus-visible { outline: 4px solid var(--blue); outline-offset: 3px; }
input { font-family: inherit; }

/* ===================== background ===================== */
#sky {
  position: fixed; inset: 0; z-index: 0; overflow: hidden;
  background: linear-gradient(var(--sky-top), var(--sky-bottom) 70%);
}
.sun {
  position: absolute; top: 6vh; left: 6vw; width: 110px; height: 110px; border-radius: 50%;
  background: radial-gradient(circle, #fff6b0 0 40%, #ffd84d 60%);
  box-shadow: 0 0 60px 20px rgba(255, 220, 80, .45);
  animation: sunpulse 6s ease-in-out infinite;
}
@keyframes sunpulse { 50% { transform: scale(1.08); } }
.cloud {
  position: absolute; width: 160px; height: 50px; background: #fff; border-radius: 50px; opacity: .9;
  animation: drift linear infinite;
}
.cloud::before, .cloud::after { content: ''; position: absolute; background: #fff; border-radius: 50%; }
.cloud::before { width: 70px; height: 70px; top: -35px; left: 25px; }
.cloud::after { width: 90px; height: 90px; top: -50px; left: 65px; }
.c1 { top: 12vh; animation-duration: 70s; animation-delay: -10s; }
.c2 { top: 24vh; animation-duration: 95s; animation-delay: -50s; transform: scale(.7); }
.c3 { top: 6vh;  animation-duration: 120s; animation-delay: -80s; }
.c4 { top: 34vh; animation-duration: 85s; animation-delay: -30s; }
.c5 { top: 18vh; animation-duration: 110s; animation-delay: -95s; }
@keyframes drift { from { left: -220px; } to { left: 105vw; } }
.hills { position: absolute; bottom: 0; left: 0; width: 100%; height: 34vh; }
body[data-screen="race"] .cloud { opacity: .55; }   /* calmer while typing */

/* ===================== corner buttons ===================== */
.corner-buttons { position: fixed; top: 14px; right: 16px; z-index: 50; display: flex; gap: 12px; }
.round-btn {
  width: 64px; height: 64px; border-radius: 50%; background: #fff; font-size: 30px;
  box-shadow: var(--shadow); position: relative; display: grid; place-items: center;
  transition: transform .1s;
}
.round-btn:active { transform: translateY(3px); }
.round-btn.off { background: #ffe1e1; }
.gear { width: 48px; height: 48px; font-size: 22px; opacity: .7; align-self: center; touch-action: none; }
body:not([data-screen="home"]) .gear { display: none; }
.hold-ring {
  position: absolute; inset: -5px; border-radius: 50%; pointer-events: none;
  background: conic-gradient(var(--blue) calc(var(--hold, 0) * 1%), transparent 0);
  -webkit-mask: radial-gradient(circle, transparent 55%, #000 57%);
          mask: radial-gradient(circle, transparent 55%, #000 57%);
}
.gear.holding .hold-ring { animation: hold 3s linear forwards; }
@property --hold { syntax: '<number>'; inherits: false; initial-value: 0; }
@keyframes hold { from { --hold: 0; } to { --hold: 100; } }

/* ===================== screens ===================== */
.screen {
  position: fixed; inset: 0; z-index: 1; display: none; flex-direction: column;
  padding: 16px 24px 20px;
}
.screen.active { display: flex; }

/* ===================== mascot ===================== */
.mascot-slot { width: 120px; height: 126px; flex: none; }
.mascot-slot.small { width: 90px; height: 95px; }
.mascot { width: 100%; height: 100%; overflow: visible; }
.bounce .mascot { animation: bounce 1.4s ease-in-out infinite; transform-origin: 50% 100%; }
@keyframes bounce {
  0%, 100% { transform: translateY(0) scale(1.05, .95); }
  40% { transform: translateY(-18px) scale(.96, 1.04); }
  60% { transform: translateY(-18px) scale(.96, 1.04); }
}
.mascot .m-eyes { animation: blink 4s infinite; transform-origin: 100px 104px; }
@keyframes blink { 0%, 92%, 100% { transform: scaleY(1); } 95% { transform: scaleY(.1); } }
.mascot .m-star { animation: twinkle 1.6s ease-in-out infinite; transform-origin: 113px 13px; }
@keyframes twinkle { 50% { transform: rotate(20deg) scale(1.2); } }
.dance .mascot { animation: dance .9s ease-in-out infinite; transform-origin: 50% 100%; }
@keyframes dance {
  0%, 100% { transform: rotate(-10deg) translateY(0); }
  25% { transform: rotate(0) translateY(-22px); }
  50% { transform: rotate(10deg) translateY(0); }
  75% { transform: rotate(0) translateY(-22px); }
}
.dance .m-arm-l, .dance .m-arm-r { animation: wave .45s ease-in-out infinite alternate; transform-box: fill-box; transform-origin: 50% 90%; }
@keyframes wave { to { rotate: -35deg; } }

/* ===================== HOME ===================== */
.home-head { display: flex; align-items: center; gap: 18px; padding-right: 240px; }
.title {
  margin: 0; font-size: clamp(44px, 6vw, 76px); font-weight: 700; letter-spacing: 1px; line-height: 1;
  color: #fff; -webkit-text-stroke: 3px var(--ink); paint-order: stroke fill;
  text-shadow: 0 6px 0 var(--ink);
}
.tagline { margin: 6px 0 0; font-size: 24px; font-weight: 600; color: var(--ink); }

.card {
  background: var(--card); border-radius: var(--radius); box-shadow: var(--shadow);
  padding: 14px 20px 18px; min-height: 0; overflow: auto;
}
.card h2 { margin: 0 0 10px; font-size: 26px; font-weight: 600; }

/* home: players on top, two giant mode buttons below */
.home-main { flex: 1; min-height: 0; margin-top: 12px; display: flex; flex-direction: column; gap: 16px; }
.card-player { flex: none; display: flex; flex-wrap: wrap; align-items: center; gap: 6px 20px; }
.card-player h2 { width: 100%; }
.trophies { display: flex; flex-direction: column; gap: 4px; font-size: 20px; font-weight: 600; margin-left: auto; text-align: right; }
.trophy-stickers { font-size: 26px; letter-spacing: 2px; }
.trophy-stickers { max-width: 520px; }
.trophy-hint { font-size: 17px; font-weight: 500; color: var(--ink-soft); }
.modes { flex: 1; min-height: 0; display: grid; grid-template-columns: 1fr 1fr 1fr; gap: 20px; }
.mode-btn {
  border-radius: 36px; display: flex; flex-direction: column; align-items: center; justify-content: center;
  gap: 6px; padding: 16px; transition: transform .12s; min-height: 180px;
}
.mode-btn:hover { transform: translateY(-4px); }
.mode-btn:active { transform: translateY(6px); }
.mode-btn.interests { background: linear-gradient(160deg, #ffe27a, #ffb36b); box-shadow: 0 10px 0 #d9822f, 0 18px 30px rgba(0,0,0,.15); }
.mode-btn.quest { background: linear-gradient(160deg, #8fe0ff, #7aa8ff); box-shadow: 0 10px 0 #4a6fd0, 0 18px 30px rgba(0,0,0,.15); }
.mode-emoji { font-size: clamp(60px, 11vh, 100px); line-height: 1; animation: bob 2.4s ease-in-out infinite; }
.mode-btn.quest .mode-emoji { animation-delay: -1.2s; }
.mode-btn.challenge { background: linear-gradient(160deg, #ffb3d1, #c49bff); box-shadow: 0 10px 0 #8a5bd6, 0 18px 30px rgba(0,0,0,.15); }
.mode-btn.challenge .mode-emoji { animation-delay: -.6s; }
.mode-btn { text-align: center; }
@keyframes bob { 50% { transform: translateY(-8px) rotate(-4deg); } }
.mode-title { font-size: clamp(28px, 3.2vw, 46px); font-weight: 700; line-height: 1.1; }
.mode-sub { font-size: 20px; font-weight: 500; }

/* 🏆 challenges screen */
.challenge-layout { flex: 1; min-height: 0; display: grid; grid-template-columns: 1fr 1.5fr; gap: 16px; margin-top: 12px; }
.challenge-list { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; }
.challenge-tile {
  min-height: 74px; border-radius: 20px; background: #f6edff; border: 4px solid transparent;
  display: flex; align-items: center; gap: 10px; padding: 6px 12px; text-align: left;
}
.challenge-tile.selected { border-color: var(--purple); background: #fff; box-shadow: var(--shadow); }
.ct-emoji { font-size: 36px; line-height: 1; }
.ct-text { display: flex; flex-direction: column; }
.ct-name { font-size: 20px; font-weight: 700; }
.ct-info { font-size: 15px; font-weight: 500; color: var(--ink-soft); }
.challenge-card { display: flex; flex-direction: column; gap: 8px; }
.challenge-head { display: flex; align-items: center; gap: 14px; }
.challenge-head h2 { margin: 0; }
.challenge-head .stage-tip { margin: 2px 0 0; }
.challenge-head .go-btn { margin-left: auto; min-width: 180px; }
.challenge-emoji { font-size: 56px; line-height: 1; }
.board-head { display: flex; align-items: center; gap: 12px; flex-wrap: wrap; }
.board-head h3 { margin: 0; font-size: 22px; }
.board-sort { display: flex; gap: 6px; margin-left: auto; }
.sort-btn {
  min-height: 44px; padding: 0 14px; border-radius: 999px; font-size: 16px; font-weight: 600;
  background: #f1f0ff; border: 3px solid transparent;
}
.sort-btn.active { background: #fff; border-color: var(--purple); }
.challenge-board { flex: 1; min-height: 0; overflow: auto; display: flex; flex-direction: column; gap: 4px; }
.board-row {
  display: grid; grid-template-columns: 44px 1.6fr 1fr 1fr .8fr .9fr; align-items: center; gap: 6px;
  padding: 6px 10px; border-radius: 14px; background: #faf9ff; font-size: 19px; font-weight: 600;
}
.board-row.board-th { background: none; font-size: 14px; color: var(--ink-soft); padding-top: 0; padding-bottom: 0; }
.board-row.me { background: #fff3c4; }
.board-rank { font-size: 24px; text-align: center; }
.board-name { white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.board-empty { font-size: 20px; color: var(--ink-soft); text-align: center; margin: 24px 0; }
.challenge-peek { font-size: 17px; color: var(--ink-soft); }
.challenge-peek summary { cursor: pointer; font-weight: 600; }
.challenge-peek ol { margin: 6px 0 0; columns: 2; font-size: 16px; }

/* top bar used by setup screens */
.screen-top { display: flex; align-items: center; gap: 18px; padding-right: 240px; min-height: 64px; flex: none; }
.screen-title { margin: 0; font-size: 36px; }
.star-count { margin-left: auto; font-size: 28px; font-weight: 700; background: #fff; padding: 6px 20px; border-radius: 999px; box-shadow: var(--shadow); }

/* interests setup */
.interests-grid {
  flex: 1; min-height: 0; margin-top: 12px; display: grid; gap: 16px;
  grid-template-columns: 1fr 1.2fr;
  grid-template-rows: auto 1fr auto;
  grid-template-areas: "level topic" "pace topic" "pace go";
}
.card-topic { grid-area: topic; display: flex; flex-direction: column; }
.card-level { grid-area: level; }
.card-pace { grid-area: pace; display: flex; flex-direction: column; }
.card-pace .pace-picker { flex: 1; align-content: center; }

/* racing buddy picker */
.pace-picker { display: grid; grid-template-columns: repeat(4, 1fr); gap: 10px; }
.pace-bar .pace-picker { grid-template-columns: repeat(7, 1fr); }
.pace-btn {
  min-height: 80px; border-radius: 20px; background: #f1f0ff; border: 4px solid transparent;
  display: flex; flex-direction: column; align-items: center; justify-content: center; font-size: 18px; font-weight: 600;
}
.pace-btn .pace-emoji { font-size: 34px; line-height: 1.1; }
.pace-btn.selected { background: #e0f7e8; border-color: var(--green); }
.pace-btn:active { transform: scale(.95); }

/* key quest */
.tabs { display: flex; gap: 10px; }
.tab-btn {
  min-height: 60px; padding: 0 24px; border-radius: 20px; font-size: 24px; font-weight: 600;
  background: rgba(255,255,255,.6); border: 4px solid transparent;
}
.tab-btn.active { background: #fff; border-color: var(--blue); box-shadow: var(--shadow); }
.tab-panel { display: none; flex: 1; min-height: 0; margin-top: 12px; }
.tab-panel.active { display: flex; flex-direction: column; }
.quest-layout { flex: 1; min-height: 0; display: grid; grid-template-columns: 1fr 1.1fr; gap: 16px; }
.stage-path { display: grid; grid-template-columns: repeat(4, 1fr); gap: 10px; }
.stage-tile {
  min-height: 80px; border-radius: 20px; background: #e3f1ff; border: 4px solid transparent;
  display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 2px; position: relative;
}
.stage-tile .st-num { font-size: 26px; font-weight: 700; line-height: 1.1; }
.stage-tile .st-keys { font-size: 16px; font-weight: 600; color: var(--ink-soft); }
.stage-tile .st-dots { display: flex; gap: 4px; }
.stage-tile .st-dots i { width: 9px; height: 9px; border-radius: 50%; background: #c9cde6; }
.stage-tile .st-dots i.on { background: var(--green); }
.stage-tile.done { background: #dcfce7; }
.stage-tile.locked { background: #eceaf3; opacity: .6; }
.stage-tile.selected { border-color: var(--blue); transform: scale(1.05); box-shadow: var(--shadow); }
.stage-tile.wiggle, .step-btn.wiggle { animation: wiggle .35s; }
.stage-card { display: flex; flex-direction: column; gap: 10px; }
.stage-head { display: flex; align-items: center; gap: 14px; }
.stage-head h2 { margin: 0; }
.stage-sticker { font-size: 56px; filter: grayscale(1); opacity: .35; }
.stage-sticker.earned { filter: none; opacity: 1; animation: bob 2s ease-in-out infinite; }
.stage-tip { margin: 2px 0 0; font-size: 19px; color: var(--ink-soft); }
.stage-steps { display: grid; grid-template-columns: repeat(3, 1fr); gap: 12px; margin-top: auto; }
.step-btn {
  min-height: 110px; border-radius: 24px; background: var(--green); color: #fff;
  box-shadow: 0 8px 0 var(--green-dark); display: flex; flex-direction: column; align-items: center; justify-content: center;
  font-size: 24px; font-weight: 700;
}
.step-btn:active { transform: translateY(6px); box-shadow: 0 2px 0 var(--green-dark); }
.step-btn .step-emoji { font-size: 34px; line-height: 1.1; }
.step-btn .step-stars { color: #fff59d; letter-spacing: 2px; }
.step-btn.passed { background: #16a34a; }
.step-btn.locked { background: #cfd2e3; color: #6b6f8f; box-shadow: 0 8px 0 #aeb2c9; }
.pace-bar { flex: none; margin-top: 14px; padding-top: 10px; padding-bottom: 12px; }
.pace-bar h2 { font-size: 20px; margin-bottom: 6px; }
.pace-bar .pace-btn { min-height: 64px; flex-direction: row; gap: 8px; }
.pace-bar .pace-btn .pace-emoji { font-size: 28px; }

/* practice keys */
.practice-card { flex: 1; display: flex; flex-direction: column; gap: 12px; }
.practice-card h2 .small-note { font-size: 17px; font-weight: 500; }
.area-buttons { display: grid; grid-template-columns: repeat(6, 1fr); gap: 10px; }
.area-btn {
  min-height: 80px; border-radius: 20px; background: #f1f0ff; border: 4px solid transparent;
  display: flex; flex-direction: column; align-items: center; justify-content: center; font-size: 18px; font-weight: 600;
}
.area-btn .area-emoji { font-size: 30px; }
.area-btn.selected { background: #e3f1ff; border-color: var(--blue); }
.practice-bottom { display: flex; align-items: center; justify-content: space-between; gap: 20px; margin-top: auto; }
.toggle.big { font-size: 22px; font-weight: 600; }
.go-btn.small { min-height: 84px; min-width: 260px; font-size: 48px; }

.player-list { display: flex; flex-wrap: wrap; gap: 10px; }
.player-chip {
  min-height: 80px; min-width: 96px; padding: 6px 14px; border-radius: 22px;
  background: #f1f0ff; border: 4px solid transparent; display: flex; flex-direction: column;
  align-items: center; justify-content: center; font-size: 20px; font-weight: 600;
  transition: transform .1s;
}
.player-chip:active { transform: scale(.96); }
.player-chip.selected { background: #e0f7e8; border-color: var(--green); }
.player-chip.add { background: #fff8e0; border: 4px dashed var(--yellow); }
.pc-avatar { font-size: 34px; line-height: 1.1; }

.topic-input {
  width: 100%; height: 72px; font-size: 30px; font-weight: 500; padding: 0 20px;
  border-radius: 20px; border: 4px solid #d9d8f0; color: var(--ink); background: #fbfbff;
  user-select: text; -webkit-user-select: text;
}
.topic-input:focus { outline: none; border-color: var(--blue); }
.topic-buttons {
  margin-top: 12px; flex: 1; display: grid; gap: 10px;
  grid-template-columns: repeat(4, 1fr); grid-auto-rows: minmax(80px, 1fr);
}
.topic-btn {
  border-radius: 20px; background: #f1f0ff; border: 4px solid transparent;
  display: flex; flex-direction: column; align-items: center; justify-content: center;
  font-size: 19px; font-weight: 600; transition: transform .1s;
}
.topic-btn:active { transform: scale(.95); }
.topic-btn .tb-emoji { font-size: 34px; line-height: 1.1; }
.topic-btn.selected { background: #e3f1ff; border-color: var(--blue); }

.levels { display: grid; grid-template-columns: repeat(3, 1fr); gap: 12px; }
.level-btn {
  min-height: 96px; border-radius: 22px; display: flex; flex-direction: column;
  align-items: center; justify-content: center; gap: 2px; font-size: 26px; font-weight: 700;
  border: 5px solid transparent; box-shadow: 0 6px 0 rgba(0, 0, 0, .15); transition: transform .1s;
}
.level-btn:active { transform: translateY(4px); box-shadow: 0 2px 0 rgba(0, 0, 0, .15); }
.level-btn .lv-sub { font-size: 15px; font-weight: 500; }
.lv1 { background: #fff1b8; }
.lv2 { background: #ffd8bd; }
.lv3 { background: #ffd0dc; }
.level-btn.selected { border-color: var(--ink); transform: scale(1.04); }

.go-btn {
  grid-area: go; min-height: 100px; border-radius: 34px;
  background: var(--green); color: #fff; font-size: 64px; font-weight: 700; letter-spacing: 4px;
  box-shadow: 0 10px 0 var(--green-dark), 0 18px 30px rgba(21, 128, 61, .35);
  animation: gopulse 1.6s ease-in-out infinite; transition: transform .1s;
}
.go-btn:active { transform: translateY(8px); box-shadow: 0 2px 0 var(--green-dark); }
@keyframes gopulse { 50% { transform: scale(1.03); } }

/* ===================== RACE ===================== */
.race-top { display: flex; align-items: center; gap: 14px; padding-right: 160px; min-height: 64px; }
.pill-btn {
  min-height: 56px; padding: 0 20px; border-radius: 999px; background: #fff; font-size: 20px;
  font-weight: 600; box-shadow: var(--shadow);
}
.pill-btn:active { transform: translateY(3px); }
.pill-btn.speak { background: #e3f1ff; }
.pill-btn.danger { background: #ffe1e1; }
.race-info { flex: 1; font-size: 22px; font-weight: 600; text-align: center; }
.timer { font-size: 26px; font-weight: 600; background: #fff; border-radius: 999px; padding: 6px 18px; box-shadow: var(--shadow); min-width: 120px; text-align: center; }

.track {
  margin-top: 12px; background: #5c5f73; border-radius: 26px; padding: 8px 10px;
  box-shadow: var(--shadow); display: flex; flex-direction: column; gap: 6px;
}
.lane {
  position: relative; height: clamp(58px, 10vh, 84px); border-radius: 16px;
  background: repeating-linear-gradient(90deg, transparent 0 40px, rgba(255, 255, 255, .25) 40px 70px) center / 100% 4px no-repeat, #6e7186;
}
.lane-label {
  position: absolute; left: 10px; top: 4px; font-size: 15px; font-weight: 600; color: #fff;
  background: rgba(0, 0, 0, .25); padding: 1px 10px; border-radius: 999px; z-index: 2;
}
.finish-flag {
  position: absolute; right: 0; top: 0; bottom: 0; width: 56px; display: grid; place-items: center;
  font-size: 34px; border-radius: 0 16px 16px 0;
  background: repeating-conic-gradient(#fff 0 25%, #23224a 0 50%) 0 0 / 14px 14px;
}
.racer {
  --p: 0;
  position: absolute; top: 50%; font-size: clamp(42px, 7vh, 60px); line-height: 1;
  left: calc(14px + var(--p) * (100% - 130px));
  transform: translateY(-50%);
  transition: left .18s ease-out;
  z-index: 1;
}
.lane.hidden { display: none; }
.track[data-lanes="3"] .lane { height: clamp(46px, 7.4vh, 70px); }
.track[data-lanes="3"] .racer { font-size: clamp(36px, 5.6vh, 52px); }
.racer.buddy { transition: none; }
.racer.ghost { opacity: .4; filter: grayscale(.4) brightness(1.3); transition: none; }
.combo {
  font-size: 22px; font-weight: 700; color: #fff; background: var(--orange); padding: 6px 16px;
  border-radius: 999px; box-shadow: var(--shadow); opacity: 0; transition: opacity .3s; white-space: nowrap;
}
.combo.show { opacity: 1; }
.racer.ghost::after { content: '👻'; position: absolute; font-size: 22px; right: -10px; top: -8px; opacity: .9; }
.racer.hop { animation: hop .22s ease-out; }
@keyframes hop { 50% { transform: translateY(-80%) rotate(-8deg); } }

.sentence-box {
  position: relative; flex: 1; min-height: 0; margin-top: 14px;
  background: var(--card); border-radius: var(--radius); box-shadow: var(--shadow);
  display: flex; align-items: center; justify-content: center; padding: 18px 36px;
}
.sentence {
  --len: 20;
  font-size: clamp(38px, calc(9vw - var(--len) * 0.06vw), 84px);
  font-weight: 600; line-height: 1.5; text-align: center; max-width: 100%;
}
.word, .ch { display: inline-block; }
.word { white-space: nowrap; }
.ch { position: relative; padding: 0 .02em; border-radius: 10px; transition: color .1s; }
.ch.todo { color: var(--todo); }
.ch.done { color: var(--done); }
.ch.current {
  color: var(--ink); background: var(--current-bg);
  box-shadow: inset 0 -.12em 0 var(--orange);
  animation: pulse 1.1s ease-in-out infinite;
}
@keyframes pulse { 50% { background: #ffe066; } }
.ch.space { width: .5em; height: 1em; vertical-align: middle; }
.ch.space .dot {
  position: absolute; left: 50%; top: 55%; width: .2em; height: .2em; border-radius: 50%;
  background: var(--todo); transform: translate(-50%, -50%);
}
.ch.space.current .dot { background: var(--orange); width: .26em; height: .26em; }
.ch.space.done .dot { background: var(--done); }
.ch.wiggle { animation: wiggle .35s ease-in-out; }
@keyframes wiggle {
  20% { transform: translateX(-6px) rotate(-6deg); }
  40% { transform: translateX(6px) rotate(6deg); }
  60% { transform: translateX(-4px); }
  80% { transform: translateX(4px); }
}

.hint {
  position: absolute; transform: translate(-50%, -115%); opacity: 0; pointer-events: none;
  background: var(--purple); color: #fff; font-size: 22px; font-weight: 600; white-space: nowrap;
  padding: 6px 16px; border-radius: 16px; transition: opacity .15s; z-index: 3;
}
.hint::after {
  content: ''; position: absolute; left: 50%; bottom: -10px; transform: translateX(-50%);
  border: 10px solid transparent; border-bottom: 0; border-top-color: var(--purple);
}
.hint.show { opacity: 1; }
.hint.nudge { animation: hintnudge .4s; }
@keyframes hintnudge { 50% { transform: translate(-50%, -125%) scale(1.15); } }

.spark {
  position: absolute; pointer-events: none; font-size: 30px; transform: translate(-50%, -50%);
  animation: spark .7s ease-out forwards; z-index: 2;
}
@keyframes spark {
  to { transform: translate(calc(-50% + var(--dx)), -170%) scale(1.4) rotate(40deg); opacity: 0; }
}

/* on-screen keyboard */
.keyboard {
  margin: 14px auto 0; background: rgba(255, 255, 255, .85); border-radius: 22px; padding: 10px 14px;
  box-shadow: var(--shadow); display: flex; flex-direction: column; gap: 6px; align-items: center;
}
.keyboard.hidden { display: none; }

/* keyboard + hand guide row (race screen) */
.kb-area { display: flex; justify-content: center; align-items: stretch; gap: 14px; margin-top: 14px; flex: none; }
.kb-area .keyboard { margin: 0; }
.hand {
  width: clamp(120px, 14vw, 200px); min-height: 190px; display: flex; flex-direction: column; align-items: center;
  background: rgba(255, 255, 255, .85); border-radius: 22px; box-shadow: var(--shadow); padding: 6px 8px 6px;
}
.hand.hidden { display: none; }
.hand-svg { flex: 1 1 0; min-height: 0; width: 100%; overflow: visible; }
.hand-svg .palm { fill: #fff; stroke: #b9bbd3; stroke-width: 3; }
.hand-svg .finger rect { stroke: #b9bbd3; stroke-width: 3; }
.hand-svg [data-finger="pinky"] rect  { fill: #d9c2ff; }   /* same colours as the keys */
.hand-svg [data-finger="ring"] rect   { fill: #b5dbff; }
.hand-svg [data-finger="middle"] rect { fill: #a9ecc0; }
.hand-svg [data-finger="index"] rect  { fill: #ffe08a; }
.hand-svg [data-finger="thumb"] rect  { fill: #ffd2c2; }
.hand-svg text { font-family: var(--font); font-size: 22px; font-weight: 700; fill: var(--ink); text-anchor: middle; }
.hand-svg line { stroke: var(--ink); stroke-width: 3; stroke-linecap: round; }
.hand-svg .finger { transition: opacity .2s; transform-box: fill-box; transform-origin: 50% 100%; }
.hand.has-active .finger:not(.active) { opacity: .35; }
.hand .finger.active rect { stroke: var(--orange); stroke-width: 6; }
.hand .finger.active { animation: fingerbob 1s ease-in-out infinite; }
@keyframes fingerbob { 50% { transform: translateY(-9px); } }
.hand.nudge { animation: keynudge .4s; }
.hand-caption { font-size: 17px; font-weight: 600; color: var(--ink-soft); white-space: nowrap; min-height: 1.3em; }
.hand-caption.active { color: var(--ink); font-weight: 700; }
/* matching coloured edge under each key links keys to fingers */
.key.f-pinky { box-shadow: 0 4px 0 #c4a1ff; }
.key.f-ring { box-shadow: 0 4px 0 #8cc4ff; }
.key.f-middle { box-shadow: 0 4px 0 #6fd796; }
.key.f-index { box-shadow: 0 4px 0 #f5c542; }
.kb-row { display: flex; gap: 6px; }
.kb-row.r1 { margin-left: 2.2vw; }
.kb-row.r2 { margin-left: -1vw; }
.key {
  --k: clamp(34px, min(6vh, 4.2vw), 56px);
  width: calc(var(--k) * 1.08); height: var(--k); border-radius: 12px; display: grid; place-items: center;
  font-size: calc(var(--k) * .42); font-weight: 600; background: #f3f3f8;
  box-shadow: 0 4px 0 #c9c9da; position: relative; transition: transform .1s;
}
.key.f-pinky { background: #f3e8ff; }
.key.f-ring { background: #e0f0ff; }
.key.f-middle { background: #dcfce7; }
.key.f-index { background: #fff4d6; }
.key.wide { width: calc(var(--k) * 2.2); font-size: calc(var(--k) * .32); }
.key.space { width: calc(var(--k) * 7); font-size: calc(var(--k) * .32); color: var(--ink-soft); }
.key.bump::after { content: ''; position: absolute; bottom: 18%; width: 30%; height: 3px; border-radius: 3px; background: var(--ink-soft); }
.key.next {
  background: var(--yellow); color: var(--ink); box-shadow: 0 4px 0 #d39e00, 0 0 0 4px var(--orange);
  transform: translateY(-2px); animation: keyglow 1s ease-in-out infinite;
}
@keyframes keyglow { 50% { box-shadow: 0 4px 0 #d39e00, 0 0 0 7px rgba(255, 140, 66, .5); } }
.key.dim { opacity: .28; }
.key.new::before { content: '⭐'; position: absolute; top: -12px; right: -8px; font-size: 18px; }
.keyboard.mini { margin: 0 auto; box-shadow: none; background: #f7f7fc; }
.keyboard.mini .key { --k: clamp(30px, 5vh, 46px); }
.keyboard.pickable { margin: 0 auto; box-shadow: none; background: #f7f7fc; }
.keyboard.pickable .key { --k: clamp(40px, 7vh, 62px); cursor: pointer; opacity: .45; }
.keyboard.pickable .key.picked { opacity: 1; background: var(--yellow); box-shadow: 0 4px 0 #d39e00, 0 0 0 3px var(--orange); }
.keyboard.pickable .key.space, .keyboard.pickable .key.shift,
.keyboard.pickable .key[data-key="'"], .keyboard.pickable .key[data-key=","], .keyboard.pickable .key[data-key="."] { cursor: default; opacity: .2; }
.key.nudge { animation: keynudge .4s; }
@keyframes keynudge { 50% { transform: scale(1.25); } }

/* overlays on the race screen */
.overlay {
  position: absolute; inset: 0; display: none; align-items: center; justify-content: center;
  flex-direction: column; z-index: 20;
}
.overlay.show { display: flex; }
.loading { background: rgba(216, 243, 255, .75); font-size: 32px; font-weight: 600; }
.loading p { margin: 10px 0 0; }
.dots i { font-style: normal; animation: dot 1.2s infinite; }
.dots i:nth-child(2) { animation-delay: .2s; }
.dots i:nth-child(3) { animation-delay: .4s; }
@keyframes dot { 50% { opacity: .1; } }
.countdown { pointer-events: none; }
.countdown span {
  font-size: clamp(140px, 30vh, 260px); font-weight: 700; color: #fff;
  -webkit-text-stroke: 8px var(--ink); paint-order: stroke fill; text-shadow: 0 12px 0 var(--ink);
}
.pop { animation: pop .5s cubic-bezier(.3, 1.8, .5, 1); }
@keyframes pop { from { transform: scale(.2); opacity: 0; } to { transform: scale(1); opacity: 1; } }
.refocus { background: rgba(35, 34, 74, .55); cursor: pointer; }
.refocus p { background: #fff; padding: 30px 50px; border-radius: var(--radius); font-size: 40px; font-weight: 700; }

/* ===================== FINISH ===================== */
#screen-finish { align-items: center; justify-content: center; }
#confetti { position: fixed; inset: 0; pointer-events: none; z-index: 30; }
.finish-card {
  position: relative; background: var(--card); border-radius: 40px; box-shadow: var(--shadow);
  padding: 26px 60px 30px; display: flex; flex-direction: column; align-items: center;
  min-width: min(640px, 92vw); max-height: 94vh;
}
.record-banner {
  display: none; position: absolute; top: -34px; background: var(--pink); color: #fff;
  font-size: 40px; font-weight: 700; padding: 8px 36px; border-radius: 999px;
  box-shadow: 0 8px 0 #c23a64; white-space: nowrap;
}
.record-banner.show { display: block; }
.finish-top { display: flex; align-items: center; gap: 24px; margin-top: 10px; }
.finish-main { display: flex; flex-direction: column; align-items: center; }
.finish-card .mascot-slot { width: 130px; height: 136px; }
.finish-title { margin: 0; font-size: 50px; font-weight: 700; }
.finish-lines { display: flex; flex-direction: column; align-items: center; gap: 4px; margin-top: 8px; font-size: 22px; font-weight: 600; color: var(--ink-soft); }
.stars { display: flex; gap: 14px; margin: 6px 0; }
.star {
  font-size: 84px; line-height: 1; color: #e3e3ee; opacity: 0;
  animation: starin .5s cubic-bezier(.3, 1.8, .5, 1) forwards;
}
.star.on { color: var(--yellow); text-shadow: 0 5px 0 #d39e00; }
@keyframes starin { from { transform: scale(0) rotate(-90deg); opacity: 0; } to { transform: scale(1); opacity: 1; } }
.finish-time { font-size: 44px; font-weight: 700; color: var(--green-dark); }
.finish-ghost { font-size: 20px; color: var(--ink-soft); }
.finish-msg { font-size: 26px; font-weight: 500; margin: 10px 0 18px; text-align: center; }
.finish-buttons { display: flex; gap: 18px; }
.big-btn.next { background: var(--blue); color: #fff; box-shadow: 0 8px 0 #1f6fd1; }
.settings-row .pill-btn.on { background: #dcfce7; }
.big-btn {
  min-height: 88px; min-width: 200px; padding: 0 26px; border-radius: 28px; font-size: 30px; font-weight: 700;
  transition: transform .1s;
}
.big-btn:active { transform: translateY(6px); }
.big-btn.again { background: var(--green); color: #fff; box-shadow: 0 8px 0 var(--green-dark); }
.big-btn.home { background: #e8e7fb; box-shadow: 0 8px 0 #c1bff0; }

/* ---- personal-best tiles (v4) ---- */
.finish-bests { display: grid; grid-template-columns: repeat(4, minmax(130px, 1fr)); gap: 12px; margin-top: 10px; width: 100%; }
.best-tile {
  position: relative; background: #f4f3ff; border-radius: 22px; padding: 8px 10px 10px; text-align: center;
  border: 4px solid transparent;
}
.best-tile.t-time   { background: #e6f6ff; }
.best-tile.t-clean  { background: #fff3d6; }
.best-tile.t-acc    { background: #e7f9ec; }
.best-tile.t-points { background: #f3e9ff; }
.best-label { font-size: 17px; font-weight: 600; color: var(--ink-soft); white-space: nowrap; }
.best-value { font-size: 36px; font-weight: 700; line-height: 1.15; }
.best-prev { font-size: 16px; font-weight: 500; color: var(--ink-soft); min-height: 1.2em; }
.best-tile.is-new { border-color: var(--orange); animation: tilepop .5s .6s cubic-bezier(.3, 1.8, .5, 1) both; }
.best-new {
  position: absolute; top: -14px; left: 50%; transform: translateX(-50%) rotate(-4deg);
  background: var(--orange); color: #fff; font-size: 14px; font-weight: 700; padding: 2px 10px;
  border-radius: 999px; white-space: nowrap; box-shadow: 0 3px 0 #c9621c;
}
@keyframes tilepop { 0% { transform: scale(.85); } 100% { transform: scale(1); } }

/* ---- 💯 badge during the race: shown until the first mistake ---- */
.perfect-badge {
  font-size: 22px; font-weight: 700; color: #7a4b00; background: #fff3c4; padding: 6px 16px;
  border-radius: 999px; box-shadow: var(--shadow); white-space: nowrap; opacity: 0; transition: opacity .3s;
}
.perfect-badge.show { opacity: 1; }
.perfect-badge.lost { animation: badgefall .9s ease-in forwards; }
@keyframes badgefall {
  0% { opacity: 1; transform: none; }
  100% { opacity: 0; transform: translateY(40px) rotate(25deg) scale(.7); }
}

/* ---- perfect run celebration ---- */
.finish-card.perfect {
  background:
    linear-gradient(var(--card), var(--card)) padding-box,
    linear-gradient(120deg, #ff5e7e, #ffcf3f, #2ecc71, #3fa9ff, #a66bff, #ff5e7e) border-box;
  border: 8px solid transparent; background-size: 100% 100%, 300% 300%;
  animation: rainbow 3s linear infinite;
}
@keyframes rainbow { to { background-position: 0 0, 300% 0; } }
.finish-card.perfect .record-banner {
  background: linear-gradient(90deg, #ff5e7e, #ff8c42, #ffcf3f, #2ecc71, #3fa9ff, #a66bff);
  box-shadow: 0 8px 0 #8b5cf6;
}
.perfect-burst {
  position: fixed; inset: 0; z-index: 40; pointer-events: none; display: none;
  flex-direction: column; align-items: center; justify-content: center;
}
.perfect-burst.show { display: flex; animation: burstout 2.4s ease-in forwards; }
.perfect-burst span { font-size: 220px; line-height: 1; animation: stamp .7s cubic-bezier(.3, 1.8, .5, 1) both; }
.perfect-burst b {
  font-size: 96px; font-weight: 700; color: #fff; letter-spacing: 4px;
  text-shadow: 0 6px 0 #8b5cf6, 0 0 30px rgba(255, 207, 63, .9);
  animation: stamp .7s .15s cubic-bezier(.3, 1.8, .5, 1) both;
}
@keyframes stamp { from { transform: scale(3) rotate(-15deg); opacity: 0; } to { transform: scale(1) rotate(0); opacity: 1; } }
@keyframes burstout { 0%, 70% { opacity: 1; } 100% { opacity: 0; transform: scale(.6) translateY(-30vh); } }

/* ---- version label ---- */
.home-version { margin-left: 10px; font-size: 14px; font-weight: 500; color: var(--ink-soft); opacity: .8; }

/* ===================== dialogs ===================== */
.dialog {
  border: 0; border-radius: 32px; padding: 24px 30px; box-shadow: 0 30px 80px rgba(0, 0, 0, .35);
  width: min(720px, 94vw); max-height: 92vh; color: var(--ink); font-family: var(--font);
}
.dialog::backdrop { background: rgba(35, 34, 74, .5); }
.dialog h2 { margin: 0 0 10px; font-size: 34px; }
.field-label { display: block; font-size: 22px; font-weight: 600; margin: 12px 0 8px; }
.avatar-grid { display: grid; grid-template-columns: repeat(6, 1fr); gap: 8px; }
.avatar-btn {
  height: 80px; font-size: 44px; border-radius: 20px; background: #f1f0ff; border: 4px solid transparent;
}
.avatar-btn.selected { border-color: var(--green); background: #e0f7e8; }
.dialog-error { color: #c2410c; font-size: 20px; min-height: 1.2em; margin: 8px 0 0; }
.dialog-buttons { display: flex; gap: 16px; justify-content: flex-end; margin-top: 14px; }
.dialog .big-btn { min-height: 72px; min-width: 160px; font-size: 26px; }

.settings { width: min(860px, 94vw); font-size: 18px; }
.settings-section { border-top: 2px solid #ecebf7; padding: 10px 0; }
.settings-section h3 { margin: 4px 0 8px; font-size: 22px; }
.settings-row { display: flex; align-items: center; gap: 10px; padding: 6px 0; flex-wrap: wrap; }
.settings-row .pill-btn { min-height: 44px; font-size: 16px; box-shadow: 0 3px 0 rgba(0, 0, 0, .12); background: #f1f0ff; }
.settings-row .pill-btn.danger { background: #ffe1e1; }
.sp-name { font-size: 22px; font-weight: 600; min-width: 150px; }
.sp-bests { flex: 1; color: var(--ink-soft); font-size: 16px; white-space: pre; }
.toggle { display: flex; align-items: center; gap: 12px; font-size: 19px; margin: 8px 0; cursor: pointer; }
.toggle input { width: 28px; height: 28px; accent-color: var(--green); }
.small-note { font-size: 16px; color: var(--ink-soft); margin: 4px 0 8px; }
.settings #clear-cache { min-height: 44px; font-size: 16px; background: #f1f0ff; box-shadow: 0 3px 0 rgba(0, 0, 0, .12); }
code { background: #f1f0ff; padding: 1px 6px; border-radius: 6px; }

/* ===================== smaller screens ===================== */
@media (max-height: 760px) {
  .mascot-slot { width: 90px; height: 95px; }
  .title { font-size: 52px; }
  .tagline { font-size: 20px; }
  .card h2 { font-size: 22px; margin-bottom: 8px; }
  .topic-input { height: 62px; }
  .go-btn { font-size: 54px; min-height: 88px; }
  .finish-card .mascot-slot { width: 100px; height: 105px; }
  .finish-title { font-size: 40px; }
  .star { font-size: 56px; }
  .finish-card { padding: 20px 40px 22px; }
  .best-value { font-size: 30px; }
  .finish-lines { font-size: 19px; }
  .finish-msg { font-size: 22px; margin: 6px 0 12px; }
  .big-btn { min-height: 80px; }
}
@media (max-width: 1100px) {
  .topic-buttons { grid-template-columns: repeat(2, 1fr); }
}
