* {
    margin: 0;
    padding: 0;
    box-sizing: border-box;
    -webkit-font-smoothing: antialiased;
}

body {
    overflow: hidden;
    background: #0a0d16;
    font-family: system-ui, -apple-system, 'Segoe UI', Roboto, Helvetica, Arial, sans-serif;
    user-select: none;
    color: #f8fafc;
}

#gameCanvas {
    display: block;
    width: 100vw;
    height: 100vh;
}

/* ── Top-right Utility Bar ── */
.host-controls {
    position: fixed;
    top: 10px;
    right: 12px;
    z-index: 250;
    display: flex;
    align-items: center;
    gap: 6px;
    flex-wrap: nowrap;
    white-space: nowrap;
}

.fps-badge {
    background: rgba(15, 23, 42, 0.85);
    border: 1px solid rgba(56, 189, 248, 0.4);
    color: #38bdf8;
    font-size: 0.78rem;
    font-weight: 800;
    padding: 4px 9px;
    border-radius: 8px;
    font-variant-numeric: tabular-nums;
}

.util-btn {
    background: rgba(15, 23, 42, 0.82);
    color: #f8fafc;
    border: 1px solid rgba(255, 255, 255, 0.18);
    border-radius: 10px;
    padding: 5px 10px;
    font-size: 0.82rem;
    font-weight: 700;
    cursor: pointer;
    backdrop-filter: blur(8px);
    transition: background 0.15s, transform 0.1s;
    white-space: nowrap;
}
.util-btn:hover { background: rgba(30, 41, 59, 0.98); transform: translateY(-1px); }
.util-btn:active { transform: scale(0.96); }

/* ── F3 Debug Panel ── */
.debug-panel {
    position: fixed;
    top: 52px;
    right: 12px;
    z-index: 260;
    background: rgba(9, 13, 24, 0.88);
    border: 1px solid rgba(56, 189, 248, 0.35);
    border-radius: 10px;
    padding: 10px 14px;
    font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
    font-size: 0.74rem;
    color: #bae6fd;
    pointer-events: none;
    line-height: 1.5;
    white-space: pre;
}

/* ── Overlay & Modals ── */
.overlay {
    position: fixed;
    inset: 0;
    display: flex;
    align-items: center;
    justify-content: center;
    z-index: 150;
    background: rgba(5, 8, 18, 0.84);
    backdrop-filter: blur(8px);
    overflow-y: auto;
    padding: 1rem 0;
    transition: opacity 0.2s ease;
}

.menu-content {
    text-align: center;
    color: #fff;
    width: 94%;
    max-width: 860px;
    padding: 1.1rem 1.4rem;
    margin: auto;
}

.loading-box { max-width: 470px; }

.game-title {
    font-size: clamp(1.7rem, 3.8vw, 2.7rem);
    margin-bottom: 0.15rem;
    background: linear-gradient(135deg, #ff3333, #ff7700, #ffcc00);
    -webkit-background-clip: text;
    -webkit-text-fill-color: transparent;
    background-clip: text;
    font-weight: 900;
    letter-spacing: 2px;
}

.subtitle {
    font-size: 0.84rem;
    color: #94a3b8;
    margin-bottom: 0.9rem;
    letter-spacing: 1.4px;
    text-transform: uppercase;
}

.progress-track {
    width: 100%;
    height: 12px;
    background: rgba(255, 255, 255, 0.1);
    border-radius: 99px;
    overflow: hidden;
    margin: 1rem 0 0.7rem;
    border: 1px solid rgba(255, 255, 255, 0.15);
}
.progress-fill {
    width: 0%;
    height: 100%;
    background: linear-gradient(90deg, #ff3333, #ff7700, #ffcc00);
    transition: width 0.16s ease-out;
}
.loading-text { font-size: 0.85rem; color: #cbd5e1; }

/* ── Lobby Options Grid ── */
.lobby-options-grid {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(210px, 1fr));
    gap: 10px;
    margin-bottom: 0.9rem;
}

.option-card {
    background: rgba(255, 255, 255, 0.05);
    border: 1px solid rgba(255, 255, 255, 0.12);
    border-radius: 14px;
    padding: 0.65rem 0.85rem;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    gap: 6px;
}

.compact-opts {
    flex-direction: row;
    flex-wrap: wrap;
    gap: 10px;
}

.section-title {
    font-size: 0.72rem;
    text-transform: uppercase;
    color: #94a3b8;
    letter-spacing: 1.2px;
    font-weight: 700;
    display: block;
}

.lobby-select {
    padding: 0.35rem 0.65rem;
    font-size: 0.85rem;
    background: #1e293b;
    color: #fff;
    border: 1px solid #475569;
    border-radius: 8px;
    cursor: pointer;
    max-width: 100%;
}

.record-badge {
    font-size: 0.72rem;
    color: #fbbf24;
    font-weight: 700;
}

.toggle-chip {
    display: inline-flex;
    align-items: center;
    gap: 6px;
    background: rgba(255, 255, 255, 0.08);
    padding: 0.35rem 0.75rem;
    border-radius: 99px;
    font-size: 0.82rem;
    font-weight: 700;
    cursor: pointer;
}

.mode-buttons {
    display: flex;
    justify-content: center;
    gap: 8px;
}

.mode-btn {
    padding: 0.45rem 1.05rem;
    background: rgba(255, 255, 255, 0.08);
    border: 1px solid rgba(255, 255, 255, 0.2);
    border-radius: 99px;
    color: #cbd5e1;
    font-size: 0.86rem;
    font-weight: 800;
    cursor: pointer;
    transition: all 0.18s ease;
}
.mode-btn:hover { background: rgba(255, 255, 255, 0.16); color: #fff; }
.mode-btn.active {
    background: linear-gradient(135deg, #e63333, #ff6600);
    border-color: transparent;
    color: #fff;
    box-shadow: 0 0 18px rgba(230, 51, 51, 0.5);
}

/* ── Connect / QR Card ── */
.connect-card {
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 1.5rem;
    flex-wrap: wrap;
    background: rgba(255, 255, 255, 0.05);
    border: 1px solid rgba(255, 255, 255, 0.12);
    border-radius: 16px;
    padding: 0.9rem 1.3rem;
    margin-bottom: 0.9rem;
    backdrop-filter: blur(10px);
}

.qr-wrapper {
    display: flex;
    flex-direction: column;
    align-items: center;
    background: #fff;
    padding: 8px;
    border-radius: 12px;
}

#qrcode {
    width: 156px;
    height: 156px;
    display: flex;
    align-items: center;
    justify-content: center;
}
#qrcode img, #qrcode canvas {
    display: block;
    width: 156px !important;
    height: 156px !important;
}

.scan-hint {
    color: #0f172a;
    font-size: 0.68rem;
    font-weight: 800;
    margin-top: 5px;
    text-transform: uppercase;
}

.connect-details { text-align: left; max-width: 390px; }
.detail-label { font-size: 0.7rem; color: #94a3b8; font-weight: 700; letter-spacing: 1px; }
.connect-url { font-size: 1.05rem; font-weight: 800; color: #38bdf8; word-break: break-all; }

.room-code-badge {
    display: inline-block;
    font-size: 1.85rem;
    font-weight: 900;
    color: #ffcc00;
    letter-spacing: 3px;
    background: rgba(0, 0, 0, 0.45);
    padding: 3px 16px;
    border-radius: 8px;
    border: 1px dashed rgba(255, 204, 0, 0.45);
    margin-top: 3px;
}

.host-conn-hint { font-size: 0.74rem; color: #94a3b8; margin-top: 5px; line-height: 1.35; }
.invite-actions { display: flex; gap: 8px; flex-wrap: wrap; margin-top: 8px; }

.code-entry {
    width: 100%;
    max-width: 260px;
    padding: 0.75rem 1rem;
    border-radius: 12px;
    border: 1px solid rgba(255, 255, 255, 0.22);
    background: rgba(0, 0, 0, 0.45);
    color: #fff;
    font-size: 1.35rem;
    font-weight: 900;
    text-align: center;
    letter-spacing: 3px;
    user-select: text;
}

/* ── Player Slots ── */
.slots-container {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(145px, 1fr));
    gap: 10px;
    margin-bottom: 0.9rem;
}

.player-slot-card {
    background: rgba(255, 255, 255, 0.05);
    border: 1px dashed rgba(255, 255, 255, 0.2);
    border-radius: 12px;
    padding: 0.65rem 0.5rem;
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 3px;
    transition: all 0.2s ease;
}
.player-slot-card.connected {
    border-style: solid;
    border-width: 2px;
    background: rgba(255, 255, 255, 0.12);
}
.player-slot-card.disconnected-mid {
    border-style: dashed;
    border-color: #f59e0b;
    background: rgba(245, 158, 11, 0.12);
}
.player-slot-card.bot-slot {
    border-style: solid;
    border-color: rgba(148, 163, 184, 0.4);
    background: rgba(148, 163, 184, 0.08);
}

.slot-tag { font-size: 0.72rem; font-weight: 900; padding: 2px 8px; border-radius: 6px; color: #fff; }
.slot-name { font-size: 0.95rem; font-weight: 700; max-width: 140px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.slot-meta { font-size: 0.7rem; color: #cbd5e1; }
.slot-status { font-size: 0.72rem; color: #94a3b8; }

/* ── Buttons ── */
.start-actions { display: flex; flex-direction: column; align-items: center; gap: 6px; }
.fallback-hint { font-size: 0.72rem; color: #64748b; }

.btn {
    display: inline-block;
    padding: 0.8rem 2.2rem;
    font-size: 1.02rem;
    font-weight: 700;
    color: #fff;
    background: linear-gradient(135deg, #e63333, #ff6600);
    border: none;
    border-radius: 50px;
    cursor: pointer;
    transition: transform 0.14s, box-shadow 0.14s;
    letter-spacing: 0.7px;
}
.btn:not(:disabled):hover { transform: scale(1.04); box-shadow: 0 0 28px rgba(255, 60, 60, 0.45); }
.btn:active { transform: scale(0.97); }
.btn:disabled { background: #475569; color: #94a3b8; cursor: not-allowed; animation: none; }

.btn-secondary { background: linear-gradient(135deg, #334155, #475569); }
.btn-sm { padding: 0.45rem 1.1rem; font-size: 0.82rem; }

.pulse:not(:disabled) { animation: btnPulse 2s ease-in-out infinite; }
@keyframes btnPulse {
    0%, 100% { box-shadow: 0 0 10px rgba(255, 60, 60, 0.3); }
    50% { box-shadow: 0 0 28px rgba(255, 60, 60, 0.6); }
}

.lobby-footer {
    margin-top: 0.9rem;
    font-size: 0.74rem;
    color: #64748b;
    display: flex;
    justify-content: center;
    gap: 8px;
    flex-wrap: wrap;
}
.lobby-footer a { color: #38bdf8; text-decoration: none; }
.lobby-footer kbd { background: #1e293b; padding: 1px 5px; border-radius: 4px; border: 1px solid #334155; }

/* ── Settings & HowTo Modals ── */
.settings-list { display: flex; flex-direction: column; gap: 12px; text-align: left; }
.setting-row {
    display: flex;
    justify-content: space-between;
    align-items: center;
    background: rgba(255, 255, 255, 0.05);
    padding: 0.65rem 1rem;
    border-radius: 10px;
    font-weight: 600;
}
.howto-list { list-style: none; display: flex; flex-direction: column; gap: 8px; font-size: 0.9rem; color: #cbd5e1; line-height: 1.4; }

/* ── Countdown ── */
.countdown-text {
    font-size: clamp(6rem, 20vw, 12rem);
    font-weight: 900;
    color: #fff;
    text-shadow: 0 0 80px rgba(255, 50, 50, 0.7);
    animation: countPop 0.4s cubic-bezier(.17,.67,.35,1.5);
}
@keyframes countPop {
    0% { transform: scale(2.4); opacity: 0; }
    60% { transform: scale(0.95); opacity: 1; }
    100% { transform: scale(1); }
}

/* ── Race HUD ── */
#hud { position: fixed; inset: 0; pointer-events: none; z-index: 50; }

.hud-center {
    position: absolute;
    top: 10px;
    left: 50%;
    transform: translateX(-50%);
    padding: 6px 20px;
    background: rgba(0, 0, 0, 0.72);
    border-radius: 12px;
    color: #fff;
    font-size: clamp(1rem, 1.8vw, 1.35rem);
    font-weight: 800;
    font-variant-numeric: tabular-nums;
    backdrop-filter: blur(6px);
    border: 1px solid rgba(255, 255, 255, 0.18);
    z-index: 60;
}

#minimapCanvas {
    position: absolute;
    bottom: 12px;
    left: 50%;
    transform: translateX(-50%);
    background: rgba(15, 23, 42, 0.68);
    border: 1px solid rgba(255, 255, 255, 0.22);
    border-radius: 12px;
    backdrop-filter: blur(4px);
    z-index: 58;
}

.hud-panel {
    position: absolute;
    padding: 8px 13px;
    background: rgba(9, 13, 24, 0.72);
    border-radius: 12px;
    color: #fff;
    font-size: 0.86rem;
    backdrop-filter: blur(6px);
    border: 1px solid rgba(255, 255, 255, 0.16);
    min-width: 162px;
    z-index: 55;
}

.player-tag {
    font-weight: 800;
    font-size: 0.96rem;
    margin-bottom: 4px;
    display: flex;
    justify-content: space-between;
    align-items: center;
    gap: 8px;
}

.pos-pill {
    font-size: 0.78rem;
    font-weight: 900;
    padding: 1px 7px;
    border-radius: 6px;
    background: rgba(255, 255, 255, 0.16);
    color: #fbbf24;
}

.hud-row { display: flex; justify-content: space-between; gap: 10px; margin-bottom: 2px; }
.hud-label { color: #94a3b8; }
.hud-value { font-weight: 700; font-variant-numeric: tabular-nums; }

.drift-meter-track {
    width: 100%;
    height: 5px;
    background: rgba(255, 255, 255, 0.12);
    border-radius: 99px;
    overflow: hidden;
    margin-top: 5px;
}
.drift-meter-fill {
    width: 0%;
    height: 100%;
    background: #38bdf8;
    transition: width 0.06s linear;
}

.hud-alert {
    margin-top: 5px;
    padding: 2px 6px;
    border-radius: 5px;
    font-size: 0.72rem;
    font-weight: 900;
    text-align: center;
}
.alert-wrongway { background: rgba(239, 68, 68, 0.9); color: #fff; }
.alert-offline { background: rgba(245, 158, 11, 0.9); color: #111; }

/* ── Lap Notification ── */
.lap-notify {
    position: fixed;
    top: 16%;
    left: 50%;
    transform: translateX(-50%);
    z-index: 65;
    font-size: clamp(1.4rem, 3vw, 2.2rem);
    font-weight: 900;
    color: #ffcc00;
    text-shadow: 0 0 30px rgba(255, 200, 0, 0.6), 0 2px 8px rgba(0,0,0,0.85);
    pointer-events: none;
    animation: notifySlide 1.8s ease-out forwards;
}
@keyframes notifySlide {
    0%   { opacity: 0; transform: translateX(-50%) translateY(20px) scale(0.75); }
    15%  { opacity: 1; transform: translateX(-50%) translateY(0) scale(1.08); }
    30%  { transform: translateX(-50%) translateY(0) scale(1); }
    80%  { opacity: 1; }
    100% { opacity: 0; transform: translateX(-50%) translateY(-28px); }
}

/* ── Results ── */
.results-card { max-width: 640px; }
.results-text { font-size: 1rem; margin: 1.2rem 0; color: #e2e8f0; }
.winner-line { font-size: 1.4rem; color: #ffcc00; font-weight: 800; margin-bottom: 0.6rem; }
.new-record-banner {
    background: linear-gradient(90deg, rgba(250, 204, 21, 0.2), rgba(249, 115, 22, 0.25));
    border: 1px solid #fbbf24;
    color: #fde047;
    font-weight: 800;
    padding: 0.45rem 0.8rem;
    border-radius: 10px;
    margin-bottom: 0.9rem;
}
.results-table { width: 100%; border-collapse: collapse; text-align: left; background: rgba(255, 255, 255, 0.05); border-radius: 12px; overflow: hidden; }
.results-table th, .results-table td { padding: 0.6rem 0.85rem; border-bottom: 1px solid rgba(255, 255, 255, 0.08); }
.results-table th { font-size: 0.74rem; text-transform: uppercase; color: #94a3b8; }
.color-dot { display: inline-block; width: 10px; height: 10px; border-radius: 50%; margin-right: 6px; }
.results-actions { display: flex; justify-content: center; gap: 12px; flex-wrap: wrap; }

/* ── Split Dividers ── */
#splitDividers { position: fixed; inset: 0; pointer-events: none; z-index: 45; }
.divider-v { position: absolute; top: 0; bottom: 0; width: 2px; background: rgba(255, 255, 255, 0.35); }
.divider-h { position: absolute; left: 0; right: 0; height: 2px; background: rgba(255, 255, 255, 0.35); }
