:root {
  --ink: #3b2d63;
  --yellow: #ffd23f;
  --green: #3ccf5f;   --green-d: #23a145;
  --orange: #ff8a2a;  --orange-d: #d9620b;
  --blue: #3fa9f5;    --blue-d: #1f7fc4;
  --pink: #ff6fae;
  --paper: #ffffff;
  --sky: #8fdcff;
  --shadow: rgba(59, 45, 99, .2);
  --outline: 0 3px 0 var(--ink), 0 -3px 0 var(--ink), 3px 0 0 var(--ink), -3px 0 0 var(--ink),
             2px 2px 0 var(--ink), -2px -2px 0 var(--ink), 2px -2px 0 var(--ink), -2px 2px 0 var(--ink),
             0 7px 0 var(--ink);
}

* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
html, body { margin: 0; height: 100%; overflow: hidden; }
body {
  background: var(--sky);
  color: var(--ink);
  font-family: "Nunito", "Varela Round", "Arial Rounded MT Bold", "Trebuchet MS", "Segoe UI", system-ui, sans-serif;
  font-weight: 800;
  -webkit-user-select: none; user-select: none;
  -webkit-touch-callout: none;
  touch-action: manipulation;
  overscroll-behavior: none;
}
img { -webkit-user-drag: none; }
button { font: inherit; color: inherit; border: 0; padding: 0; background: none; cursor: pointer; }

#game { position: fixed; inset: 0; width: 100%; height: 100%; display: block; touch-action: none; }

.screen {
  position: fixed; inset: 0;
  display: none; flex-direction: column; align-items: center; justify-content: center;
  gap: clamp(6px, 2vh, 18px);
  padding: max(16px, env(safe-area-inset-top)) max(16px, env(safe-area-inset-right))
           max(16px, env(safe-area-inset-bottom)) max(16px, env(safe-area-inset-left));
}
.screen.show { display: flex; }

/* ---------- заголовки ---------- */
.logo {
  margin: 0; text-align: center;
  font-size: clamp(30px, min(9vh, 9.6vw), 80px); line-height: 1.05; font-weight: 900; letter-spacing: .5px;
  color: var(--yellow); text-shadow: var(--outline);
}
.logo .pink { color: var(--pink); }
.sub {
  margin: 0; padding: 4px 20px; border-radius: 999px;
  background: var(--ink); color: #fff;
  font-size: clamp(17px, 3.6vh, 28px); font-weight: 900;
}
.note {
  margin: 0; padding: 4px 14px; border-radius: 999px;
  background: rgba(255, 255, 255, .85); font-size: 13px; text-align: center;
}

/* ---------- кнопки ---------- */
.big {
  --c: var(--green); --cd: var(--green-d);
  width: clamp(64px, 12vh, 96px); aspect-ratio: 1; border-radius: 50%;
  display: grid; place-items: center; flex: none;
  background: var(--c); color: #fff;
  box-shadow: 0 7px 0 var(--cd), 0 10px 18px rgba(0, 0, 0, .15);
  transition: transform .08s;
}
.big:active { transform: translateY(5px); box-shadow: 0 2px 0 var(--cd); }
.big svg { width: 54%; height: 54%; }
.big.blue { --c: var(--blue); --cd: var(--blue-d); }
.big.orange { --c: var(--orange); --cd: var(--orange-d); }

.small {
  width: 50px; height: 50px; border-radius: 50%; flex: none;
  display: grid; place-items: center;
  background: rgba(255, 255, 255, .94); color: var(--ink);
  box-shadow: 0 4px 0 var(--shadow);
}
.small:active { transform: translateY(3px); box-shadow: 0 1px 0 var(--shadow); }
.small svg { width: 26px; height: 26px; }
.small.off { opacity: .6; }
.small .off-only { display: none; }
.small.off .off-only { display: inline; }

.pulse { animation: pulse 1.2s ease-in-out infinite; }
@keyframes pulse { 50% { transform: scale(1.08); } }

/* ---------- выбор героя ---------- */
.heroes {
  display: flex; flex-wrap: wrap; justify-content: center;
  gap: clamp(8px, 1.6vw, 18px); max-width: 1100px;
}
.hero {
  width: clamp(100px, 16vw, 190px);
  padding: 8px 8px 6px; border-radius: 24px;
  background: var(--paper);
  box-shadow: 0 6px 0 var(--shadow);
  transition: transform .2s, box-shadow .2s;
}
.hero img {
  display: block; width: 100%; aspect-ratio: 560 / 494; object-fit: contain;
  animation: idle 1.6s ease-in-out infinite; animation-delay: var(--d, 0s);
}
.hero span { display: block; margin-top: 2px; font-size: clamp(14px, 2.4vh, 20px); font-weight: 900; }
.hero.on { transform: translateY(-6px) scale(1.07); box-shadow: 0 0 0 5px var(--yellow), 0 10px 0 var(--shadow); }
@keyframes idle { 50% { transform: translateY(-4px); } }

/* ---------- выбор трассы ---------- */
.title-row { display: flex; align-items: center; gap: 12px; }
.levels {
  display: flex; flex-wrap: wrap; justify-content: center;
  gap: clamp(10px, 2vw, 22px); max-width: 1200px;
}
.lvl {
  position: relative; width: clamp(118px, 17vw, 210px);
  padding: 7px 7px 5px; border-radius: 22px;
  background: var(--paper); box-shadow: 0 6px 0 var(--shadow);
}
.lvl img { display: block; width: 100%; aspect-ratio: 7 / 5; object-fit: cover; border-radius: 16px; }
.lvl .name { display: block; margin-top: 3px; font-size: clamp(13px, 2.2vh, 18px); font-weight: 900; line-height: 1.15; }
.lvl .num {
  position: absolute; left: -8px; top: -8px; width: 40px; height: 40px; border-radius: 50%;
  display: grid; place-items: center;
  background: var(--orange); color: #fff; font-size: 22px; font-weight: 900;
  box-shadow: 0 3px 0 var(--orange-d);
}
.lvl .lock {
  position: absolute; left: 7px; right: 7px; top: 7px; aspect-ratio: 7 / 5;
  display: grid; place-items: center; color: #fff;
}
.lvl .lock svg { width: 34%; height: auto; filter: drop-shadow(0 3px 0 rgba(0, 0, 0, .35)); }
.lvl.locked img { filter: grayscale(1) brightness(.8); }
.lvl .medal { position: absolute; right: -10px; top: -12px; width: 50px; }
.lvl .medal svg { display: block; width: 100%; height: auto; }
.shake { animation: shake .4s; }
@keyframes shake { 20% { transform: translateX(-7px); } 40% { transform: translateX(7px); } 60% { transform: translateX(-4px); } 80% { transform: translateX(4px); } }

/* ---------- гонка ---------- */
#hud { justify-content: flex-start; pointer-events: none; }
#hud button { pointer-events: auto; }
.hud-top {
  position: absolute; top: max(10px, env(safe-area-inset-top));
  left: max(12px, env(safe-area-inset-left)); right: max(12px, env(safe-area-inset-right));
  display: flex; align-items: center; gap: clamp(8px, 2vw, 20px);
}
.progress { position: relative; flex: 1; height: 54px; max-width: 720px; margin: 0 auto; }
.progress .bar {
  position: absolute; left: 0; right: 30px; top: 50%; height: 12px; margin-top: -6px;
  border-radius: 6px; background: rgba(255, 255, 255, .88); box-shadow: 0 3px 0 var(--shadow);
}
.progress .flag { position: absolute; right: 0; top: 50%; width: 30px; transform: translateY(-62%); }
.progress .flag svg { display: block; width: 100%; height: auto; }
.progress .pi { position: absolute; top: 50%; width: 46px; transform: translate(-50%, -62%); }
.progress .pi.me { width: 62px; z-index: 2; filter: drop-shadow(0 0 2px #fff) drop-shadow(0 0 3px #fff); }

.pill {
  display: flex; align-items: center; gap: 6px; flex: none;
  padding: 4px 16px 4px 6px; border-radius: 999px;
  background: rgba(255, 255, 255, .94); box-shadow: 0 4px 0 var(--shadow);
  font-size: clamp(20px, 4vh, 28px); font-weight: 900;
}
.pill svg { width: 1.5em; height: 1.5em; }
.pill.pop { animation: pop .3s; }
@keyframes pop { 50% { transform: scale(1.2); } }

.lights {
  position: absolute; top: 22%; left: 50%; transform: translateX(-50%);
  display: none; gap: 10px; padding: 10px; border-radius: 999px;
  background: var(--ink); box-shadow: 0 6px 0 rgba(0, 0, 0, .25);
}
.lights.show { display: flex; }
.lights i { width: clamp(40px, 9vh, 64px); aspect-ratio: 1; border-radius: 50%; background: #251c42; }
.lights .r.on { background: #ff4d4d; box-shadow: 0 0 24px #ff4d4d; }
.lights .y.on { background: var(--yellow); box-shadow: 0 0 24px var(--yellow); }
.lights .g.on { background: var(--green); box-shadow: 0 0 24px var(--green); }

.go-text {
  position: absolute; top: 42%; left: 50%; opacity: 0;
  transform: translate(-50%, -50%) scale(.3);
  font-size: clamp(40px, 12vh, 100px); font-weight: 900; white-space: nowrap;
  color: var(--yellow); text-shadow: var(--outline); pointer-events: none;
}
.go-text.show { animation: go 1.1s ease-out forwards; }
@keyframes go {
  0% { opacity: 0; transform: translate(-50%, -50%) scale(.3); }
  20% { opacity: 1; transform: translate(-50%, -50%) scale(1.15); }
  35% { transform: translate(-50%, -50%) scale(1); }
  80% { opacity: 1; }
  100% { opacity: 0; transform: translate(-50%, -62%) scale(1); }
}
.horn { position: absolute; left: max(18px, env(safe-area-inset-left)); bottom: max(18px, env(safe-area-inset-bottom)); }

/* ---------- финиш ---------- */
.panel {
  max-width: min(92vw, 520px); padding: clamp(12px, 3vh, 24px) clamp(18px, 4vw, 36px);
  border-radius: 32px; text-align: center;
  background: var(--paper); box-shadow: 0 10px 0 var(--shadow);
  animation: pop-in .45s cubic-bezier(.3, 1.6, .5, 1);
}
@keyframes pop-in { from { transform: scale(.4); opacity: 0; } to { transform: scale(1); opacity: 1; } }
.fin-icon svg { width: clamp(64px, 17vh, 120px); height: auto; display: block; margin: 0 auto; }
.fin-title { margin: 0; font-size: clamp(32px, 9vh, 62px); font-weight: 900; color: var(--yellow); text-shadow: var(--outline); }
.fin-sub { margin: 4px 0 0; font-size: clamp(17px, 3.8vh, 26px); }
.fin-stars { display: flex; align-items: center; justify-content: center; gap: 6px; margin: 4px 0 12px; font-size: clamp(24px, 5vh, 36px); }
.fin-stars svg { width: 1.3em; height: 1.3em; }
.row { display: flex; gap: 16px; justify-content: center; padding-bottom: 7px; }

/* ---------- служебное ---------- */
.loadbar {
  width: min(70vw, 360px); height: 18px; border-radius: 9px; overflow: hidden;
  background: rgba(255, 255, 255, .75);
}
.loadbar i { display: block; width: 0; height: 100%; border-radius: 9px; background: var(--orange); transition: width .2s; }

#sound-btns {
  position: fixed; z-index: 5; display: flex; gap: 8px;
  top: max(12px, env(safe-area-inset-top)); right: max(12px, env(safe-area-inset-right));
}
body:not(.menu) #sound-btns { display: none; }

.rotate {
  position: fixed; z-index: 6; left: 50%; bottom: max(12px, env(safe-area-inset-bottom));
  transform: translateX(-50%); display: none; align-items: center; gap: 8px;
  padding: 6px 14px; border-radius: 999px; white-space: nowrap;
  background: rgba(255, 255, 255, .94); box-shadow: 0 4px 0 var(--shadow); font-size: 14px;
}
.rotate svg { width: 22px; height: 22px; animation: rot 2s ease-in-out infinite; }
@keyframes rot { 40%, 100% { transform: rotate(-90deg); } }

@media (orientation: portrait) and (max-width: 600px) {
  body.menu .rotate { display: flex; }
  .note { display: none; }
  .horn { bottom: auto; top: calc(max(10px, env(safe-area-inset-top)) + 70px); }
  .lvl { width: min(44vw, 180px); }
  .lvl .name { font-size: 15px; white-space: nowrap; }
}
@media (max-height: 430px) {
  .note { display: none; }
  .hero span, .lvl .name { font-size: 13px; }
}
noscript { position: fixed; inset: 0; display: grid; place-items: center; background: var(--sky); font-size: 20px; }
