* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; overflow: hidden; background: #3d3c4a; font-family: system-ui, "Segoe UI", Tahoma, sans-serif; color: #fff; user-select: none; -webkit-user-select: none; touch-action: none; }
#game { position: fixed; inset: 0; }
.hidden { display: none !important; }

#hud { position: fixed; inset: 0; pointer-events: none; text-shadow: 0 2px 4px rgba(0,0,0,.5); }
#hud-left { position: absolute; top: 16px; left: 16px; font-weight: 700; font-size: 18px; line-height: 1.3; }
#hud-left .big { font-size: 56px; line-height: 1; }
#hud-left .big small { font-size: 22px; opacity: .8; }
#time { font-variant-numeric: tabular-nums; opacity: .9; }

#board { position: absolute; top: 16px; right: 16px; display: flex; flex-direction: column; gap: 4px; font-size: 14px; font-weight: 600; }
#board .row { display: flex; align-items: center; gap: 8px; background: rgba(20,18,35,.55); padding: 4px 10px 4px 6px; border-radius: 6px; min-width: 150px; }
#board .row.me { background: rgba(255,170,40,.85); color: #222; text-shadow: none; }
#board img { width: 24px; height: 16px; object-fit: cover; border-radius: 2px; }

#minimap { position: absolute; left: 16px; bottom: 16px; width: 150px; height: 150px; background: rgba(20,18,35,.45); border-radius: 10px; }

#speedo { position: absolute; right: 20px; bottom: 16px; font-weight: 800; font-size: 54px; text-align: right; font-variant-numeric: tabular-nums; }
#speedo small { font-size: 16px; margin-left: 4px; opacity: .8; }
#boostbar { width: 160px; height: 8px; background: rgba(0,0,0,.4); border-radius: 4px; margin-left: auto; overflow: hidden; }
#boostfill { height: 100%; width: 0; background: linear-gradient(90deg, #4fd1ff, #ff5ad9); }

#center-msg { position: absolute; top: 34%; left: 0; right: 0; text-align: center; font-size: 96px; font-weight: 900; letter-spacing: 2px; }
#center-msg.small { font-size: 40px; color: #ff6a5a; }
#net-info { position: absolute; top: 10px; left: 50%; transform: translateX(-50%); font-size: 13px; font-weight: 700; opacity: .75; font-variant-numeric: tabular-nums; }
#net-info.warn { color: #ff6a5a; opacity: 1; }

#touch { position: absolute; left: 0; right: 0; bottom: 180px; display: none; justify-content: space-between; padding: 0 16px; pointer-events: none; }
#touch .tgroup { display: flex; gap: 10px; pointer-events: auto; }
#touch button { width: 72px; height: 72px; border-radius: 50%; border: 2px solid rgba(255,255,255,.5); background: rgba(20,18,35,.45); color: #fff; font-size: 22px; font-weight: 800; touch-action: none; }
#touch button[data-k="drift"] { font-size: 12px; }
#touch button.on { background: rgba(255,170,40,.7); }
body.touch #touch { display: flex; }

.overlay { position: fixed; inset: 0; display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 14px; background: rgba(30,28,48,.72); backdrop-filter: blur(3px); padding: 16px; text-align: center; overflow-y: auto; }
.overlay h1 { margin: 0; font-size: clamp(40px, 9vw, 84px); font-weight: 900; letter-spacing: 4px; color: #ffb02e; text-shadow: 0 4px 0 #8a4b00; }
.sub { margin: 0; opacity: .85; }
.help { max-width: 560px; font-size: 13px; opacity: .75; line-height: 1.6; margin: 0; }
.cars { display: flex; flex-wrap: wrap; gap: 10px; justify-content: center; }
.car { width: 130px; padding: 8px; border-radius: 10px; background: rgba(255,255,255,.08); border: 2px solid transparent; cursor: pointer; font-weight: 700; font-size: 13px; }
.car img { width: 100%; display: block; }
.car.sel { border-color: #ffb02e; background: rgba(255,176,46,.18); }
.car .stats { font-weight: 400; font-size: 11px; opacity: .8; }
.btn { font: inherit; font-weight: 800; font-size: 20px; padding: 12px 36px; border: none; border-radius: 10px; background: #ffb02e; color: #2a1a00; cursor: pointer; box-shadow: 0 4px 0 #8a4b00; }
.btn:disabled { opacity: .4; cursor: default; }
.btn.ghost { background: transparent; color: #fff; box-shadow: none; border: 2px solid rgba(255,255,255,.4); font-size: 16px; padding: 8px 24px; }
#res-table { border-collapse: collapse; font-size: 18px; }
#res-table td { padding: 6px 14px; text-align: left; }
#res-table tr.me { color: #ffb02e; font-weight: 800; }
#res-table img { width: 28px; vertical-align: middle; border-radius: 2px; }
#loading { opacity: .7; margin: 0; }

#online { display: flex; flex-direction: column; align-items: center; gap: 8px; }
.row { display: flex; gap: 10px; justify-content: center; flex-wrap: wrap; }
#online input { font: inherit; font-size: 16px; padding: 8px 12px; border-radius: 8px; border: 2px solid rgba(255,255,255,.3); background: rgba(0,0,0,.3); color: #fff; width: 200px; text-align: center; }
#code { width: 130px !important; text-transform: uppercase; letter-spacing: 3px; }
#net-msg { margin: 0; min-height: 1.2em; font-size: 14px; color: #ff8a7a; }
.room-list { display: flex; flex-direction: column; gap: 6px; width: min(420px, 100%); margin: 0 auto; max-height: 30vh; overflow-y: auto; }
.room-list .sub { margin: 4px 0 0; }
.room { display: flex; align-items: center; gap: 8px; background: rgba(0,0,0,.3); border-radius: 8px; padding: 6px 10px; font-size: 14px; text-align: left; }
.room img { width: 22px; height: auto; }
.room-host { flex: 1; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.room-meta { font-size: 12px; opacity: .8; white-space: nowrap; }
.btn.small { font-size: 13px; padding: 4px 12px; }
#lobby-code { font-size: 28px; letter-spacing: 4px; color: #ffb02e; margin: 0 6px; user-select: text; -webkit-user-select: text; }
#lobby-players { display: flex; flex-direction: column; gap: 6px; min-width: min(360px, 100%); }
#lobby-players .lp { display: flex; align-items: center; gap: 10px; background: rgba(255,255,255,.08); padding: 6px 12px; border-radius: 8px; font-weight: 600; }
#lobby-players .lp.me { outline: 2px solid #ffb02e; }
#lobby-players .dot { width: 12px; height: 12px; border-radius: 50%; flex: none; }
#lobby-players .lp .car-name { opacity: .7; font-weight: 400; font-size: 13px; margin-left: auto; }
#lobby-players .lp .st { font-size: 12px; font-weight: 800; padding: 2px 8px; border-radius: 6px; background: rgba(255,255,255,.12); }
#lobby-players .lp .st.ok { background: #7cff6b; color: #123; }
#lobby-cars .car { width: 104px; font-size: 12px; }

/* phase 6: ranked */
#lb-tab-lap.on, #lb-tab-ranked.on { border-color: #ffb02e; color: #ffb02e; }
#queue-status { font-size: 18px; }

/* phase 6: accounts */
#account-row { align-items: center; gap: 8px; }
#account-row .btn.small { padding: 4px 10px; }

/* phase 6: time trial ghost */
#ghost-pick { font: inherit; padding: 6px 10px; border-radius: 8px; background: rgba(0,0,0,.35); color: #fff; border: 2px solid rgba(255,255,255,.3); }
#ghost-row { align-items: center; font-weight: 700; }
#ghost-info { position: absolute; top: 34px; left: 50%; transform: translateX(-50%); font-size: 13px; font-weight: 700; color: #bfe9ff; }

/* phase 5: profile, leaderboard, chat, emotes, spectator */
.small-text { font-size: 13px; }
#lap-time { font-size: 13px; opacity: .85; font-variant-numeric: tabular-nums; }
#flag-btn { display: inline-flex; align-items: center; gap: 6px; }
#flag-btn img, #flag-grid img, #lb-table img { width: 28px; height: 19px; object-fit: cover; border-radius: 2px; }
.swatches { display: flex; gap: 6px; align-items: center; }
.swatch { width: 24px; height: 24px; border-radius: 50%; border: 2px solid rgba(255,255,255,.25); cursor: pointer; padding: 0; }
.swatch.sel { border-color: #fff; box-shadow: 0 0 0 2px rgba(255,255,255,.35); }
#flag-picker h2 { margin: 0; }
#flag-search { font: inherit; padding: 8px 12px; border-radius: 8px; border: 2px solid rgba(255,255,255,.3); background: rgba(0,0,0,.3); color: #fff; width: 220px; text-align: center; text-transform: uppercase; }
#flag-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(64px, 1fr)); gap: 8px; width: min(720px, 100%); max-height: 60vh; overflow-y: auto; padding: 4px; }
#flag-grid button { font: inherit; font-size: 11px; color: #fff; background: rgba(255,255,255,.08); border: 2px solid transparent; border-radius: 8px; padding: 6px 2px; cursor: pointer; display: flex; flex-direction: column; align-items: center; gap: 4px; }
#flag-grid button.sel { border-color: #ffb02e; }
#lb-table { border-collapse: collapse; font-size: 16px; max-width: 100%; }
#lb-table td, #lb-table th { padding: 5px 12px; text-align: left; }
#lb-table th { font-size: 12px; opacity: .7; }
#lb-table tr.me { color: #ffb02e; font-weight: 800; }
#lb-table .dot, #chat-log .dot { display: inline-block; width: 10px; height: 10px; border-radius: 50%; vertical-align: middle; }
#bots-row { align-items: center; font-weight: 700; }
#bots-row b { min-width: 24px; font-size: 20px; }
#res-stats { color: #ffe14d; font-weight: 700; }
body.chat-on .overlay { padding-bottom: 240px; }
@media (min-width: 1100px) { body.chat-on .overlay { padding-bottom: 16px; } }
#chat { position: fixed; left: 16px; bottom: 16px; width: min(320px, calc(100vw - 32px)); z-index: 5; display: flex; flex-direction: column; gap: 6px; }
#chat-log { max-height: 160px; overflow-y: auto; display: flex; flex-direction: column; gap: 2px; font-size: 13px; background: rgba(20,18,35,.6); border-radius: 8px; padding: 6px 8px; text-align: left; user-select: text; -webkit-user-select: text; }
#chat-log:empty { display: none; }
#chat-log .sys { opacity: .6; font-style: italic; }
#chat-input { font: inherit; font-size: 14px; padding: 8px 10px; border-radius: 8px; border: 2px solid rgba(255,255,255,.3); background: rgba(0,0,0,.45); color: #fff; }
#spec-info { position: absolute; bottom: 70px; left: 50%; transform: translateX(-50%); background: rgba(20,18,35,.6); padding: 6px 14px; border-radius: 8px; font-weight: 700; font-size: 15px; white-space: nowrap; }
#emotes { position: absolute; inset: 0; overflow: hidden; }
.emote { position: absolute; font-size: 34px; transform: translate(-50%, -100%); animation: emote-pop 2s ease-out forwards; }
.emote small { display: block; font-size: 11px; text-align: center; font-weight: 700; }
@keyframes emote-pop { 0% { opacity: 0; margin-top: 10px; } 10% { opacity: 1; margin-top: 0; } 80% { opacity: 1; } 100% { opacity: 0; margin-top: -20px; } }

@media (max-width: 600px) {
  #hud-left .big { font-size: 40px; }
  #speedo { font-size: 36px; }
  #boostbar { width: 110px; }
  #board { font-size: 12px; }
  #board .row { min-width: 120px; }
  #minimap { width: 110px; height: 110px; }
  #center-msg { font-size: 64px; }
}
