:root {
  --ink: #1a1a2e;
  --paper: #fffaf0;
  --red: #ff3b30;
  --yellow: #ffd60a;
  --blue: #0a84ff;
  --pink: #ff5fa2;
  --green: #34c759;
  --muted: #6b6b80;
}
* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; overflow: hidden; background: #7fd3ff; color: var(--ink);
  font-family: 'Nunito', system-ui, sans-serif; font-weight: 800; -webkit-user-select: none; user-select: none; }
#gl { position: fixed; inset: 0; width: 100%; height: 100%; display: block; }
button, input, select { font: inherit; color: inherit; }

.screen { position: fixed; inset: 0; display: none; align-items: center; justify-content: center; flex-direction: column;
  padding: 76px 16px 16px; overflow-y: auto; }
.screen.show { display: flex; }
#menu { justify-content: flex-start; padding-top: calc(9vh + 40px); }

.logo { font-family: 'Luckiest Guy', cursive; font-size: clamp(40px, 10.5vw, 124px); margin: 0; line-height: .9; white-space: nowrap; color: var(--yellow);
  -webkit-text-stroke: 4px var(--ink); text-shadow: 6px 6px 0 var(--ink); letter-spacing: 2px; transform: rotate(-3deg); }
.logo span { color: var(--red); margin-left: 12px; }
.tag { background: var(--ink); color: #fff; padding: 6px 14px; border-radius: 999px; transform: rotate(-2deg); margin: 14px 0 0; }
.menu-btns { display: flex; gap: 16px; margin-top: auto; margin-bottom: 18px; flex-wrap: wrap; justify-content: center; }
.hint { background: rgba(255,255,255,.85); padding: 6px 12px; border-radius: 10px; border: 3px solid var(--ink); margin: 0 0 4vh; font-size: 14px; }

.big { font-family: 'Luckiest Guy', cursive; font-size: 24px; letter-spacing: 1px; padding: 14px 26px 10px; border-radius: 16px;
  border: 4px solid var(--ink); background: var(--red); color: #fff; box-shadow: 0 6px 0 var(--ink); cursor: pointer;
  transition: transform .08s, box-shadow .08s; }
.big.alt { background: var(--yellow); color: var(--ink); }
.big:hover { transform: translateY(-2px); box-shadow: 0 8px 0 var(--ink); }
.big:active { transform: translateY(4px); box-shadow: 0 2px 0 var(--ink); }
.big:disabled { opacity: .45; pointer-events: none; }
.big.on { background: var(--green); color: #fff; }
.small { border: 3px solid var(--ink); background: #fff; border-radius: 10px; padding: 6px 10px; cursor: pointer; box-shadow: 0 3px 0 var(--ink); }

.panel { background: var(--paper); border: 5px solid var(--ink); border-radius: 24px; box-shadow: 10px 10px 0 var(--ink);
  padding: 22px 26px 26px; width: min(980px, 100%); position: relative; margin: auto; }
.panel.narrow { width: min(460px, 100%); }
.panel h2 { font-family: 'Luckiest Guy', cursive; font-size: 30px; margin: 8px 0 12px; letter-spacing: 1px; }
.panel h3 { margin: 14px 0 8px; font-size: 18px; }
.back { background: none; border: none; cursor: pointer; color: var(--muted); padding: 0; }
.muted { color: var(--muted); font-size: 13px; }
.row { display: flex; gap: 14px; align-items: center; justify-content: flex-end; margin-top: 18px; flex-wrap: wrap; }
.row label, .settings label, .field { display: flex; flex-direction: column; gap: 4px; font-size: 13px; color: var(--muted); }
select, input { border: 3px solid var(--ink); border-radius: 10px; padding: 8px 10px; background: #fff; color: var(--ink); font-size: 16px; }
.field { margin: 10px 0; }
.field input { width: 100%; }
.narrow .big { width: 100%; margin-top: 4px; }
.or { text-align: center; color: var(--muted); margin: 14px 0 0; }
.err { color: var(--red); min-height: 1.2em; margin: 10px 0 0; }

.cars, .tracks { display: grid; grid-template-columns: repeat(auto-fill, minmax(150px, 1fr)); gap: 12px; }
.cars.small { grid-template-columns: repeat(auto-fill, minmax(104px, 1fr)); gap: 8px; }
.card { border: 4px solid var(--ink); border-radius: 16px; background: #fff; padding: 10px; cursor: pointer; text-align: left;
  box-shadow: 0 4px 0 var(--ink); transition: transform .08s; }
.card:hover { transform: translateY(-2px); }
.card.sel { outline: 4px solid var(--yellow); outline-offset: 2px; background: #fff6c8; }
.card .sw { height: 44px; border-radius: 10px; border: 3px solid var(--ink); margin-bottom: 8px; position: relative; overflow: hidden; }
.card .sw i { position: absolute; width: 14px; height: 14px; background: #fff; border: 3px solid var(--ink); border-radius: 50%; top: 9px; }
.card .sw i::after { content: ''; position: absolute; width: 5px; height: 5px; background: var(--ink); border-radius: 50%; top: 3px; left: 4px; }
.card b { display: block; font-size: 17px; }
.card small { color: var(--muted); }
.stats { display: grid; grid-template-columns: auto 1fr; gap: 2px 6px; margin-top: 6px; font-size: 11px; color: var(--muted); align-items: center; }
.stats span { height: 7px; border-radius: 4px; background: #eee; overflow: hidden; }
.stats span::after { content: ''; display: block; height: 100%; width: var(--v); background: var(--ink); }
.cars.small .stats { display: none; }
.cars.small .card { padding: 7px; }
.cars.small .card .sw { height: 30px; }
.cars.small .card .sw i { top: 3px; }
.tracks .card .sw { height: 70px; }

.lobby-head { display: flex; justify-content: space-between; align-items: flex-end; margin: 8px 0 6px; gap: 10px; flex-wrap: wrap; }
.code { font-family: 'Luckiest Guy', cursive; font-size: 54px; letter-spacing: 6px; line-height: 1; color: var(--red); -webkit-text-stroke: 2px var(--ink); }
.lobby-grid { display: grid; grid-template-columns: 1fr 1.3fr; gap: 24px; }
@media (max-width: 760px) { .lobby-grid { grid-template-columns: 1fr; } }
.players { list-style: none; padding: 0; margin: 0; display: grid; gap: 6px; }
.players li { display: flex; align-items: center; gap: 10px; border: 3px solid var(--ink); border-radius: 12px; padding: 8px 10px; background: #fff; }
.players .dot { width: 22px; height: 22px; border-radius: 6px; border: 3px solid var(--ink); flex: none; }
.players .nm { flex: 1; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.players .st { font-size: 13px; color: var(--muted); }
.players .st.ok { color: var(--green); }
.pot { margin-top: 14px; border: 4px dashed var(--ink); border-radius: 16px; padding: 12px 14px; background: #fff6c8; }
.pot-amt { font-family: 'Luckiest Guy', cursive; font-size: 40px; line-height: 1.1; }
.settings { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; }
.settings.locked select, .settings.locked input { background: #f0f0f0; pointer-events: none; }

.winner { font-family: 'Luckiest Guy', cursive; font-size: 26px; text-align: center; background: var(--yellow); border: 4px solid var(--ink);
  border-radius: 16px; padding: 12px; margin-bottom: 12px; }
.winner:empty { display: none; }
.res { padding-left: 0; list-style: none; margin: 0 0 16px; display: grid; gap: 6px; counter-reset: r; }
.res li { counter-increment: r; display: flex; gap: 10px; align-items: center; border: 3px solid var(--ink); border-radius: 12px; padding: 8px 12px; background: #fff; }
.res li::before { content: counter(r); font-family: 'Luckiest Guy', cursive; font-size: 22px; width: 26px; }
.res li.me { background: #fff6c8; }
.res li span { flex: 1; }

/* HUD */
#hud { position: fixed; inset: 0; pointer-events: none; display: none; }
#hud.show { display: block; }
.hud-tl { position: absolute; left: 16px; top: 16px; background: var(--paper); border: 4px solid var(--ink); border-radius: 16px;
  padding: 8px 14px; box-shadow: 5px 5px 0 var(--ink); }
.pos { font-family: 'Luckiest Guy', cursive; font-size: 46px; line-height: 1; color: var(--red); }
.pos small { font-size: 22px; color: var(--ink); }
.lap { font-size: 16px; }
.time { font-variant-numeric: tabular-nums; color: var(--muted); }
#minimap { position: absolute; right: 16px; top: 16px; width: 150px; height: 150px; background: rgba(255,250,240,.85);
  border: 4px solid var(--ink); border-radius: 50%; box-shadow: 5px 5px 0 var(--ink); }
.hud-br { position: absolute; right: 20px; bottom: 18px; font-family: 'Luckiest Guy', cursive; font-size: 64px; color: #fff;
  -webkit-text-stroke: 3px var(--ink); text-shadow: 4px 4px 0 var(--ink); }
.hud-br small { font-size: 22px; margin-left: 4px; }
#count { position: absolute; left: 50%; top: 34%; transform: translate(-50%,-50%); font-family: 'Luckiest Guy', cursive; font-size: 150px;
  color: var(--yellow); -webkit-text-stroke: 5px var(--ink); text-shadow: 8px 8px 0 var(--ink); }
#count.pop { animation: pop .5s ease-out; }
@keyframes pop { from { transform: translate(-50%,-50%) scale(1.8); opacity: 0; } to { transform: translate(-50%,-50%) scale(1); opacity: 1; } }
#toast { position: fixed; z-index: 35; pointer-events: none; left: 50%; top: 16px; transform: translateX(-50%); background: var(--ink); color: #fff; padding: 8px 16px;
  border-radius: 999px; opacity: 0; transition: opacity .3s; white-space: nowrap; }
#toast.show { opacity: 1; }
.mute { position: absolute; right: 180px; top: 20px; pointer-events: auto; border: 3px solid var(--ink); background: #fff; border-radius: 10px;
  font-size: 18px; cursor: pointer; padding: 4px 8px; }
.touch { display: none; }
.touch.show { display: flex; position: absolute; left: 0; right: 0; bottom: 16px; justify-content: space-between; padding: 0 16px; }
.touch div { display: flex; gap: 12px; pointer-events: auto; }
.touch button { width: 72px; height: 72px; border-radius: 50%; border: 4px solid var(--ink); background: rgba(255,255,255,.85); font-size: 26px; touch-action: none; }
.touch button.gas { background: rgba(52,199,89,.9); }
.touch.show ~ .hud-br, #hud.touchmode .hud-br { bottom: 110px; }
@media (max-width: 600px) {
  #minimap { width: 100px; height: 100px; }
  .mute { right: 126px; }
  .hud-br { font-size: 42px; }
  .pos { font-size: 34px; }
  #count { font-size: 100px; }
}

.boxinfo { font-size: 12px; margin-top: 8px; word-break: break-all; }
.boxinfo a, .payout a { color: var(--blue); }
.rules { margin-top: 10px; font-size: 12px; }
.rules summary { cursor: pointer; font-weight: 900; }
.rules ul { margin: 6px 0 0; padding-left: 18px; color: #4a4a5e; display: grid; gap: 4px; }
.payout { text-align: center; margin: -4px 0 12px; font-size: 14px; }
.payout:empty { display: none; }
.players .st.pay { color: #b07a00; }
.modes { display: grid; grid-template-columns: repeat(3, 1fr); gap: 8px; margin-bottom: 12px; }
.modes button { border: 3px solid var(--ink); border-radius: 12px; background: #fff; padding: 9px 6px; cursor: pointer; box-shadow: 0 3px 0 var(--ink); }
.modes button.sel { background: var(--yellow); }
.modes.locked button { pointer-events: none; }
.modes.locked button:not(.sel) { opacity: .5; }

/* top bar */
#topbar { position: fixed; top: 0; left: 0; right: 0; z-index: 20; display: flex; align-items: center; justify-content: space-between;
  gap: 12px; padding: 12px 20px; }
body.racing #topbar { display: none; }
.brand { font-family: 'Luckiest Guy', cursive; font-size: 26px; color: var(--yellow); -webkit-text-stroke: 1.5px var(--ink); text-decoration: none;
  letter-spacing: 1px; visibility: hidden; }
body:not(.home) .brand { visibility: visible; }
.brand span { color: var(--red); margin-left: 4px; }
#topbar nav, #navGuest, #navUser { display: flex; align-items: center; gap: 8px; }
#navGuest[hidden], #navUser[hidden] { display: none; }
.navlink { background: rgba(255,255,255,.75); border: 3px solid var(--ink); border-radius: 12px; padding: 7px 12px; cursor: pointer; }
.navlink:hover { background: #fff; }
.navbtn { background: var(--red); color: #fff; border: 3px solid var(--ink); border-radius: 12px; padding: 7px 14px; cursor: pointer;
  box-shadow: 0 3px 0 var(--ink); }
.navname { background: var(--ink); color: #fff; border-radius: 12px; padding: 8px 12px; font-size: 14px; white-space: nowrap; }
@media (max-width: 560px) {
  #topbar { padding: 10px 12px; }
  .brand { display: none; }
  .navlink, .navbtn { padding: 6px 9px; font-size: 13px; }
  .navname { font-size: 12px; max-width: 120px; overflow: hidden; text-overflow: ellipsis; }
}

/* modal */
.modal { position: fixed; inset: 0; z-index: 30; background: rgba(26,26,46,.45); display: flex; align-items: center; justify-content: center; padding: 16px; }
.modal[hidden] { display: none; }
.modal-panel { position: relative; }
.close { position: absolute; top: 10px; right: 14px; border: none; background: none; font-size: 30px; line-height: 1; cursor: pointer; color: var(--muted); }
#about p { line-height: 1.5; margin: 0 0 10px; font-weight: 600; }
.switch { text-align: center; margin: 12px 0 0; font-size: 14px; }
.switch a { color: var(--blue); cursor: pointer; }
.nogl { position: fixed; inset: 0; z-index: 40; display: flex; align-items: center; justify-content: center; padding: 16px; background: #7fd3ff; }
.nogl[hidden] { display: none; }
.nogl p { line-height: 1.5; font-weight: 600; }
.mute { font-size: 13px; padding: 6px 10px; }
.navlink.wallet { background: #ab9ff2; color: var(--ink); }
.navlink.wallet.on { background: #fff; font-variant-numeric: tabular-nums; }
@media (max-width: 560px) { #navAbout { display: none; } }
