:root {
  --ink: #2a2440;
  --muted: #6d6786;
  --card: rgba(255, 255, 255, 0.9);
  --line: rgba(42, 36, 64, 0.12);
  --green: #2fb36b;
  --orange: #ff8a2a;
  --blue: #2f7fe0;
  --purple: #8a5cf0;
  --red: #e8453c;
  --gold: #ffc93c;
  --radius: 22px;
  --font: 'Baloo 2', ui-rounded, 'Segoe UI', system-ui, sans-serif;
}

* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; overflow: hidden; background: #bfe6ff; color: var(--ink); font-family: var(--font); -webkit-tap-highlight-color: transparent; }
button, input, select { font-family: inherit; color: inherit; }
button { cursor: pointer; border: 0; }
[hidden] { display: none !important; }

#scene { position: fixed; inset: 0; width: 100vw; height: 100vh; display: block; touch-action: none; }

/* ---------- screens & cards ---------- */
.screen { position: fixed; inset: 0; display: grid; place-items: center; padding: 16px; overflow-y: auto; z-index: 20; background: radial-gradient(ellipse at center, rgba(255,255,255,0) 30%, rgba(30, 40, 80, 0.25) 100%); }
.card { background: var(--card); backdrop-filter: blur(14px); -webkit-backdrop-filter: blur(14px); border-radius: var(--radius); padding: 22px 24px; box-shadow: 0 20px 60px rgba(30, 30, 80, 0.25), 0 2px 0 rgba(255,255,255,0.6) inset; width: min(520px, 100%); animation: pop 0.35s cubic-bezier(.2,1.4,.4,1); }
.card.wide { width: min(860px, 100%); }
@keyframes pop { from { transform: scale(0.92) translateY(10px); opacity: 0; } }
.card-head { display: flex; align-items: center; gap: 12px; margin-bottom: 8px; }
.card-head h2 { margin: 0; font-size: 30px; line-height: 1; flex: 1; }
.back { width: 42px; height: 42px; border-radius: 14px; background: #fff; font-size: 22px; font-weight: 800; box-shadow: 0 3px 0 var(--line); }
.back:hover { background: #f3f0ff; }
.muted { color: var(--muted); margin: 6px 0 14px; }
.small { font-size: 14px; }
.center { text-align: center; }

.logo { display: flex; align-items: center; justify-content: center; gap: 10px; }
.logo h1 { font-size: clamp(46px, 9vw, 72px); margin: 0; line-height: 0.9; letter-spacing: -1px; color: var(--red); text-shadow: 0 4px 0 #a92d26, 0 8px 18px rgba(0,0,0,0.2); }
.logo h1 span { color: var(--blue); text-shadow: 0 4px 0 #1d58a3, 0 8px 18px rgba(0,0,0,0.2); }
.logo-train { font-size: clamp(46px, 9vw, 68px); display: inline-block; animation: chug 0.6s ease-in-out infinite alternate; }
@keyframes chug { to { transform: translateY(-4px) rotate(-3deg); } }
.tagline { text-align: center; font-size: 19px; color: var(--muted); margin: 8px 0 18px; }
.title-card { width: min(560px, 100%); }

.menu-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 12px; }
.big-btn { display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 0; padding: 14px 10px 12px; border-radius: 18px; font-size: 22px; font-weight: 800; color: #fff; background: var(--c, var(--green)); box-shadow: 0 6px 0 var(--cs, #1f8a50), 0 10px 20px rgba(0,0,0,0.15); transition: transform 0.08s, box-shadow 0.08s, filter 0.15s; text-shadow: 0 2px 0 rgba(0,0,0,0.15); }
.big-btn span { font-size: 34px; line-height: 1.1; }
.big-btn small { font-size: 14px; font-weight: 600; opacity: 0.9; }
.big-btn:hover { filter: brightness(1.07); transform: translateY(-1px); }
.big-btn:active { transform: translateY(5px); box-shadow: 0 1px 0 var(--cs, #1f8a50); }
.big-btn.start { width: 100%; margin-top: 16px; flex-direction: row; gap: 8px; font-size: 24px; }
.c-green { --c: var(--green); --cs: #1f8a50; }
.c-orange { --c: var(--orange); --cs: #c9631a; }
.c-blue { --c: var(--blue); --cs: #1d58a3; }
.c-purple { --c: var(--purple); --cs: #6439c2; }
.c-red { --c: var(--red); --cs: #a92d26; }
.menu-row { display: flex; gap: 10px; justify-content: center; margin-top: 16px; flex-wrap: wrap; }
.pill { padding: 8px 16px; border-radius: 999px; background: #fff; font-weight: 700; font-size: 16px; box-shadow: 0 3px 0 var(--line); }
.pill:hover { background: #f3f0ff; }
.pill.danger { color: var(--red); margin-top: 10px; }

/* levels */
.total-stars { font-weight: 800; color: #d89a00; font-size: 20px; }
.worlds { display: grid; grid-template-columns: repeat(4, 1fr); gap: 10px; margin: 10px 0 16px; }
.world { border-radius: 16px; padding: 10px 8px; background: #fff; text-align: center; box-shadow: 0 4px 0 var(--line); border: 3px solid transparent; }
.world b { display: block; font-size: 16px; line-height: 1.1; }
.world i { font-style: normal; font-size: 30px; display: block; }
.world small { color: var(--muted); font-size: 12px; display: block; }
.world.on { border-color: var(--wc); background: color-mix(in srgb, var(--wc) 12%, #fff); }
.level-grid { display: grid; grid-template-columns: repeat(5, 1fr); gap: 10px; }
.lvl { aspect-ratio: 1.1; border-radius: 16px; background: var(--wc, var(--green)); color: #fff; font-size: 30px; font-weight: 800; box-shadow: 0 5px 0 rgba(0,0,0,0.2); display: flex; flex-direction: column; align-items: center; justify-content: center; line-height: 1; transition: transform 0.08s; }
.lvl:hover { transform: translateY(-2px) scale(1.03); }
.lvl:active { transform: translateY(3px); }
.lvl .st { font-size: 15px; letter-spacing: 1px; margin-top: 4px; color: rgba(255,255,255,0.45); }
.lvl .st b { color: #ffe066; font-weight: 800; }
.lvl.locked { background: #c9c4d9; cursor: not-allowed; }

/* quick */
.diff-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 12px; }
.diff-btn { position: relative; text-align: left; padding: 16px; border-radius: 18px; color: #fff; overflow: hidden; box-shadow: 0 6px 0 rgba(0,0,0,0.2); min-height: 100px; transition: transform 0.08s; }
.diff-btn:hover { transform: translateY(-2px); }
.diff-btn:active { transform: translateY(4px); }
.diff-btn b { display: block; font-size: 24px; }
.diff-btn span { font-size: 15px; opacity: 0.95; }
.diff-btn i { position: absolute; right: 8px; bottom: -6px; font-size: 52px; font-style: normal; opacity: 0.9; }
.d-easy { background: linear-gradient(135deg, #46c76f, #2a9e57); }
.d-moderate { background: linear-gradient(135deg, #ff9f43, #e2672a); }
.d-hard { background: linear-gradient(135deg, #59a7ff, #3366d6); }
.d-expert { background: linear-gradient(135deg, #f0739a, #b1428a); }

/* forms */
.seg { display: inline-flex; background: #ece8f7; border-radius: 14px; padding: 4px; gap: 4px; margin-bottom: 12px; }
.seg button { padding: 8px 18px; border-radius: 10px; background: transparent; font-weight: 700; font-size: 17px; }
.seg button.on { background: #fff; box-shadow: 0 2px 0 var(--line); }
.form-row { display: flex; gap: 12px; flex-wrap: wrap; margin-top: 12px; }
.form-row label, .field { display: flex; flex-direction: column; font-weight: 700; font-size: 14px; color: var(--muted); gap: 4px; flex: 1; min-width: 150px; }
.field { margin: 8px 0; }
.note { margin: -2px 0 10px; font-size: 14px; line-height: 1.35; opacity: 0.75; }
select, input[type=text], input:not([type]) { padding: 10px 12px; border-radius: 12px; border: 2px solid var(--line); background: #fff; font-size: 17px; font-weight: 600; color: var(--ink); outline: none; }
select:focus, input:focus { border-color: var(--blue); }
.toggle { display: flex; gap: 10px; align-items: center; margin: 10px 0; font-weight: 600; cursor: pointer; }
.toggle input { width: 22px; height: 22px; accent-color: var(--green); }
.local-players { display: grid; grid-template-columns: repeat(auto-fit, minmax(200px, 1fr)); gap: 10px; }
.lp { border-radius: 16px; padding: 12px; background: #fff; border: 3px solid var(--pc); box-shadow: 0 4px 0 var(--line); }
.lp input { width: 100%; }
.lp .keys { margin-top: 8px; font-size: 14px; color: var(--muted); }
.lp .tag { display: inline-block; background: var(--pc); color: #fff; border-radius: 8px; padding: 0 8px; font-weight: 800; margin-bottom: 6px; }
kbd { display: inline-block; min-width: 24px; padding: 0 6px; border-radius: 6px; background: #fff; border: 1px solid #d5d0e6; border-bottom-width: 3px; font-family: inherit; font-weight: 700; font-size: 13px; text-align: center; line-height: 20px; color: var(--ink); }
.or { text-align: center; color: var(--muted); margin: 16px 0 8px; font-weight: 700; }
.join-row { display: flex; gap: 10px; }
.join-row input { flex: 1; min-width: 0; text-transform: uppercase; letter-spacing: 6px; font-size: 26px; font-weight: 800; text-align: center; }
.join-row .big-btn { padding: 8px 22px; flex-direction: row; }
.code { color: var(--purple); letter-spacing: 4px; }
.invite { display: flex; align-items: center; gap: 10px; background: #f3f0ff; padding: 8px 12px; border-radius: 14px; font-size: 14px; word-break: break-all; }
.invite span { flex: 1; color: var(--muted); }
.lobby-players { list-style: none; padding: 0; margin: 14px 0 0; display: grid; gap: 8px; }
.lobby-players li { display: flex; align-items: center; gap: 10px; padding: 10px 14px; border-radius: 14px; background: #fff; box-shadow: 0 3px 0 var(--line); font-weight: 700; font-size: 18px; }
.lobby-players .dot { width: 18px; height: 18px; border-radius: 50%; background: var(--pc); box-shadow: 0 0 0 3px color-mix(in srgb, var(--pc) 30%, transparent); }
.lobby-players .host { margin-left: auto; font-size: 13px; color: var(--muted); }

.help-grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(240px, 1fr)); gap: 12px; }
.help-grid > div { background: #fff; border-radius: 16px; padding: 12px 14px; box-shadow: 0 3px 0 var(--line); }
.help h3 { margin: 0 0 4px; font-size: 19px; }
.help p { margin: 0; line-height: 1.45; color: #4a4466; }
.help .q { color: var(--orange); }

/* ---------- in-game ---------- */
#topbar { position: fixed; top: 10px; left: 10px; right: 10px; display: flex; align-items: center; gap: 10px; z-index: 10; pointer-events: none; }
#topbar > * { pointer-events: auto; }
.icon-btn { width: 46px; height: 46px; border-radius: 15px; background: var(--card); backdrop-filter: blur(10px); font-size: 22px; box-shadow: 0 4px 0 var(--line), 0 6px 16px rgba(0,0,0,0.12); transition: transform 0.08s; }
.icon-btn:active { transform: translateY(3px); }
.icon-btn.mic.on { background: var(--red); color: #fff; animation: micpulse 1.3s infinite; }
@keyframes micpulse { 0%, 100% { box-shadow: 0 0 0 0 rgba(232, 69, 60, 0.6), 0 4px 0 #a92d26; } 50% { box-shadow: 0 0 0 10px rgba(232, 69, 60, 0), 0 4px 0 #a92d26; } }
.tb-title { background: var(--card); backdrop-filter: blur(10px); border-radius: 15px; padding: 6px 14px; font-weight: 800; font-size: 20px; display: flex; gap: 10px; align-items: center; box-shadow: 0 4px 0 var(--line); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.badge { font-size: 13px; padding: 2px 10px; border-radius: 999px; color: #fff; background: var(--green); }
.badge.moderate { background: var(--orange); } .badge.hard { background: var(--blue); } .badge.expert { background: #c2408a; }
.tb-right { margin-left: auto; display: flex; gap: 8px; align-items: center; }
.tb-timer, .tb-round { background: var(--card); backdrop-filter: blur(10px); border-radius: 15px; padding: 8px 14px; font-weight: 800; font-size: 18px; box-shadow: 0 4px 0 var(--line); font-variant-numeric: tabular-nums; }

#panels { position: fixed; inset: 0; pointer-events: none; z-index: 5; }
.panel { position: absolute; pointer-events: auto; background: var(--card); backdrop-filter: blur(12px); -webkit-backdrop-filter: blur(12px); border-radius: 20px; padding: 10px 12px; box-shadow: 0 10px 30px rgba(30,30,80,0.2); border-top: 5px solid var(--pc); display: flex; flex-direction: column; gap: 8px; align-items: center; }
.panel-head { display: flex; align-items: center; gap: 8px; width: 100%; font-weight: 800; font-size: 18px; }
.panel-head .dot { width: 14px; height: 14px; border-radius: 50%; background: var(--pc); flex: none; }
.panel-head .score { margin-left: auto; color: #d89a00; }
.panel-head .status { font-size: 14px; color: var(--muted); font-weight: 700; }
.chips { display: flex; gap: 6px; flex-wrap: wrap; justify-content: center; min-height: 40px; align-items: center; }
.chip { width: 40px; height: 40px; border-radius: 12px; display: grid; place-items: center; font-size: 22px; font-weight: 800; background: #fff; border: 3px dashed #c9c2e0; color: #b3abcf; position: relative; transition: transform 0.15s; }
.chip.filled { border: 3px solid var(--pc); background: var(--pc); color: #fff; animation: chipin 0.3s cubic-bezier(.2,1.6,.4,1); }
.chip.next { border-color: var(--gold); color: #d89a00; background: #fff8dc; animation: bob 0.9s ease-in-out infinite; }
.chip.extra { opacity: 0.5; background: #eee; border: 3px solid #ccc; color: #888; text-decoration: line-through; }
.chip small { position: absolute; top: -8px; right: -6px; font-size: 11px; background: var(--ink); color: #fff; border-radius: 8px; padding: 0 5px; line-height: 16px; }
@keyframes chipin { from { transform: scale(0.4); } }
@keyframes bob { 50% { transform: translateY(-3px); } }
.btns { display: flex; gap: 8px; align-items: stretch; justify-content: center; flex-wrap: nowrap; }
.pbtn { min-width: 74px; padding: 6px 10px 4px; border-radius: 16px; background: #fff; font-size: 30px; font-weight: 800; line-height: 1; display: flex; flex-direction: column; align-items: center; box-shadow: 0 5px 0 #d8d2ea; transition: transform 0.06s, box-shadow 0.06s; color: var(--ink); }
.pbtn small { font-size: 13px; font-weight: 700; color: var(--muted); margin-top: 2px; }
.pbtn:hover { background: #f8f6ff; }
.pbtn:active, .pbtn.flash { transform: translateY(4px); box-shadow: 0 1px 0 #d8d2ea; background: color-mix(in srgb, var(--pc) 18%, #fff); }
.pbtn.sm { min-width: 52px; font-size: 24px; }
.pbtn.go { background: var(--green); color: #fff; box-shadow: 0 5px 0 #1f8a50; min-width: 92px; position: relative; overflow: hidden; }
.pbtn.go small { color: rgba(255,255,255,0.9); }
.pbtn.go:active, .pbtn.go.flash { box-shadow: 0 1px 0 #1f8a50; background: #29a060; }
.pbtn.go.counting::after { content: ''; position: absolute; left: 0; bottom: 0; height: 5px; background: #fff; animation: countdown var(--auto, 1.4s) linear forwards; }
@keyframes countdown { from { width: 0; } to { width: 100%; } }
.pbtn:disabled { opacity: 0.45; pointer-events: none; }
.pbtn.mic, .icon-btn.mic { touch-action: none; user-select: none; -webkit-user-select: none; -webkit-touch-callout: none; }
.pbtn.mic.on { background: var(--red); color: #fff; box-shadow: 0 5px 0 #a92d26; }
.pbtn.mic.on small { color: #fff; }
.keys { font-size: 13px; color: var(--muted); text-align: center; }
.panel.compact .pbtn { min-width: 0; flex: 1; font-size: 24px; padding: 5px 4px 3px; }
.panel.compact .pbtn small { font-size: 11px; }
.panel.compact .btns { width: 100%; gap: 5px; }
.panel.compact .chip { width: 34px; height: 34px; font-size: 18px; }
.panel.won { border-top-color: var(--gold); box-shadow: 0 0 0 4px var(--gold), 0 10px 30px rgba(30,30,80,0.2); }
.panel.failed { border-top-color: var(--red); }
.retry-banner { width: 100%; border-radius: 14px; background: var(--red); color: #fff; font-weight: 800; font-size: 18px; padding: 8px 12px; box-shadow: 0 4px 0 #a92d26; }
.retry-banner:active { transform: translateY(3px); box-shadow: 0 1px 0 #a92d26; }
.retry-banner:disabled { opacity: 0.45; pointer-events: none; }
#crash-flash { position: fixed; inset: 0; z-index: 25; pointer-events: none; }
#crash-flash.go { animation: crashflash 0.55s ease-out forwards; }
@keyframes crashflash { 0% { background: rgba(226, 50, 30, 0.42); } 100% { background: rgba(226, 50, 30, 0); } }

#opponents { position: fixed; top: 66px; right: 10px; z-index: 10; display: grid; gap: 6px; width: 220px; }
.opp { background: var(--card); backdrop-filter: blur(10px); border-radius: 14px; padding: 6px 10px; display: flex; align-items: center; gap: 8px; font-weight: 700; box-shadow: 0 3px 0 var(--line); border-left: 6px solid var(--pc); }
.opp .nm { flex: 1; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.opp .pr { font-size: 13px; color: var(--muted); }
.opp .sc { color: #d89a00; }

#voice-bubble { position: fixed; top: 66px; left: 50%; transform: translateX(-50%); z-index: 11; background: rgba(42, 36, 64, 0.88); color: #fff; padding: 8px 18px; border-radius: 999px; font-weight: 700; font-size: 17px; display: flex; gap: 10px; align-items: center; max-width: min(90vw, 620px); box-shadow: 0 8px 20px rgba(0,0,0,0.2); }
#vb-text { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
#vb-text .cmd { display: inline-block; background: var(--gold); color: var(--ink); border-radius: 8px; padding: 0 8px; margin-left: 6px; animation: chipin 0.3s; }
.vb-dot { width: 12px; height: 12px; border-radius: 50%; background: #ff5d5d; flex: none; animation: blink 1s infinite; }
@keyframes blink { 50% { opacity: 0.3; } }

#toast { position: fixed; left: 50%; top: 42%; transform: translate(-50%, -50%); z-index: 30; pointer-events: none; display: grid; gap: 8px; justify-items: center; }
.toast { background: #fff; padding: 12px 22px; border-radius: 18px; font-weight: 800; font-size: 24px; box-shadow: 0 10px 30px rgba(0,0,0,0.2); animation: toastin 0.3s cubic-bezier(.2,1.6,.4,1), toastout 0.4s ease-in forwards; animation-delay: 0s, var(--life, 1.8s); text-align: center; }
.toast.bad { background: #fff0ef; color: #c0332b; }
.toast.good { background: #effff4; color: #1f8a50; }
.toast small { display: block; font-size: 15px; font-weight: 600; color: var(--muted); }
@keyframes toastin { from { transform: scale(0.5); opacity: 0; } }
@keyframes toastout { to { transform: translateY(-20px); opacity: 0; } }

#modal { position: fixed; inset: 0; z-index: 40; display: grid; place-items: center; padding: 16px; background: rgba(30, 30, 70, 0.35); }
.modal-card { text-align: center; width: min(440px, 100%); }
.modal-card h2 { font-size: 38px; margin: 0; line-height: 1.1; }
.modal-card p { color: var(--muted); font-size: 18px; margin: 6px 0; }
.modal-btns { display: flex; gap: 10px; margin-top: 16px; flex-wrap: wrap; justify-content: center; }
.modal-btns .big-btn { flex: 1; flex-direction: row; gap: 6px; font-size: 20px; min-width: 130px; }
.stars { font-size: 56px; letter-spacing: 6px; margin: 4px 0; }
.stars span { color: #ddd6ee; display: inline-block; }
.stars span.on { color: var(--gold); text-shadow: 0 3px 0 #d89a00; animation: starpop 0.5s cubic-bezier(.2,1.8,.4,1) backwards; }
.stars span:nth-child(2) { animation-delay: 0.15s; } .stars span:nth-child(3) { animation-delay: 0.3s; }
@keyframes starpop { from { transform: scale(0) rotate(-90deg); } }
.score-table { width: 100%; border-collapse: collapse; margin-top: 10px; font-size: 19px; font-weight: 700; }
.score-table td { padding: 6px 8px; border-bottom: 1px solid var(--line); text-align: left; }
.score-table td:last-child { text-align: right; color: #d89a00; }
.score-table .dot { display: inline-block; width: 14px; height: 14px; border-radius: 50%; background: var(--pc); margin-right: 6px; vertical-align: middle; }

#loading { position: fixed; inset: 0; display: grid; place-items: center; background: linear-gradient(#6ec3f4, #dff4ff); z-index: 100; transition: opacity 0.5s; }
#loading.gone { opacity: 0; pointer-events: none; }
.spinner { font-size: 64px; animation: chug 0.4s infinite alternate; }

@media (max-width: 640px) {
  .worlds { grid-template-columns: repeat(2, 1fr); }
  .level-grid { grid-template-columns: repeat(5, 1fr); gap: 6px; }
  .lvl { font-size: 22px; }
  .tb-title { font-size: 16px; }
  .tb-timer { display: none; }
  .panel { padding: 8px; }
  .btns { flex-wrap: wrap; gap: 6px; }
  .pbtn { min-width: 0; flex: 1 1 52px; font-size: 22px; padding: 5px 4px 3px; }
  .pbtn.go { flex-basis: 100%; flex-direction: row; justify-content: center; gap: 8px; }
  .keys { display: none; }
  #opponents { width: 160px; }
}
