:root {
    --bg: #07070f;
    --panel: #12121f;
    --text: #e8e8f0;
    --muted: #8b8ba3;
    --neon: #00f0ff;
    --pink: #ff2d95;
    --lime: #b6ff3b;
    --orange: #ffb040;
    --safe-top: env(safe-area-inset-top, 0px);
    --safe-bottom: env(safe-area-inset-bottom, 0px);
}

* { box-sizing: border-box; }
html, body {
    margin: 0;
    height: 100%;
    width: 100%;
    background: var(--bg);
    color: var(--text);
    font-family: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
    overflow: hidden;
    user-select: none;
    -webkit-user-select: none;
    -webkit-tap-highlight-color: transparent;
}

/* Full-viewport app shell */
#app {
    width: 100%;
    height: 100%;
    max-width: none;
    margin: 0;
    padding: 0;
    display: flex;
    flex-direction: column;
    min-height: 100dvh;
    height: 100dvh;
}

.bar {
    flex: 0 0 auto;
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 0.5rem;
    padding: calc(0.35rem + var(--safe-top)) 0.65rem 0.35rem;
    flex-wrap: wrap;
    background: rgba(7, 7, 15, 0.92);
    border-bottom: 1px solid rgba(0, 240, 255, 0.12);
    z-index: 20;
}
.back {
    color: var(--muted);
    text-decoration: none;
    font-weight: 700;
    font-size: 0.85rem;
    padding: 0.35rem 0.5rem;
    min-height: 40px;
    display: inline-flex;
    align-items: center;
}
.back:hover { color: var(--neon); }
.title {
    font-weight: 900;
    letter-spacing: 0.12em;
    font-size: clamp(0.8rem, 2.5vw, 1rem);
    color: var(--neon);
    text-shadow: 0 0 18px rgba(0, 240, 255, 0.45);
}
.build-tag {
    display: inline-block;
    margin-left: 0.3rem;
    padding: 0.1rem 0.4rem;
    font-size: 0.65rem;
    font-weight: 900;
    color: #041018;
    background: linear-gradient(90deg, var(--lime), var(--neon));
    border-radius: 6px;
}
.stats {
    display: flex;
    gap: 0.55rem;
    font-size: 0.7rem;
    font-weight: 800;
    letter-spacing: 0.04em;
    color: var(--muted);
    background: rgba(255,255,255,0.04);
    padding: 0.28rem 0.5rem;
    border-radius: 8px;
}
.stats span { color: var(--text); }

/* ── Big MPH gauge (bottom-right) ── */
.speedo {
    position: absolute;
    right: max(10px, 1.2vw);
    bottom: max(12px, 1.5vh);
    width: min(168px, 34vw);
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 0.25rem;
    filter: drop-shadow(0 0 18px rgba(0, 240, 255, 0.35));
}
.speedo-dial {
    position: relative;
    width: 100%;
    aspect-ratio: 1;
}
.speedo-arc {
    width: 100%;
    height: 100%;
    transform: rotate(-90deg);
}
.speedo-track {
    fill: none;
    stroke: rgba(255,255,255,0.1);
    stroke-width: 8;
}
.speedo-progress {
    fill: none;
    stroke: url(#none);
    stroke: #00f0ff;
    stroke-width: 8;
    stroke-linecap: round;
    stroke-dasharray: 326.7;
    stroke-dashoffset: 326.7;
    transition: stroke-dashoffset 0.08s linear, stroke 0.15s;
    filter: drop-shadow(0 0 6px currentColor);
}
.speedo.is-fast .speedo-progress { stroke: #b6ff3b; }
.speedo.is-nitro .speedo-progress { stroke: #ffb040; }
.speedo-readout {
    position: absolute;
    inset: 0;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    pointer-events: none;
}
.speedo-num {
    font-size: clamp(2.1rem, 8vw, 3.4rem);
    font-weight: 900;
    line-height: 0.95;
    letter-spacing: -0.02em;
    color: #fff;
    text-shadow:
        0 0 12px rgba(0, 240, 255, 0.9),
        0 0 28px rgba(0, 240, 255, 0.45),
        0 2px 0 #041018;
    font-variant-numeric: tabular-nums;
}
.speedo.is-fast .speedo-num {
    text-shadow:
        0 0 14px rgba(182, 255, 59, 0.95),
        0 0 30px rgba(182, 255, 59, 0.4);
}
.speedo.is-nitro .speedo-num {
    color: #fff8e8;
    text-shadow:
        0 0 16px rgba(255, 176, 64, 1),
        0 0 36px rgba(255, 100, 40, 0.55);
}
.speedo-unit {
    margin-top: 0.1rem;
    font-size: clamp(0.75rem, 2.4vw, 0.95rem);
    font-weight: 900;
    letter-spacing: 0.22em;
    color: var(--neon);
}
.speedo.is-nitro .speedo-unit { color: var(--orange); }
.speedo-max {
    font-size: 0.65rem;
    font-weight: 800;
    letter-spacing: 0.14em;
    color: rgba(255,255,255,0.45);
}

@media (max-width: 520px) {
    .speedo { width: min(140px, 38vw); bottom: max(10px, 8vh); }
    .speedo-num { font-size: clamp(1.8rem, 9vw, 2.6rem); }
}

/* Keep results actions tappable above phone home bar */
.results-actions .cta {
    min-height: 48px;
    pointer-events: auto;
}

/* Prevent iOS zoom on name/code inputs */
.online-input {
    font-size: 16px;
}

main {
    position: relative;
    flex: 1 1 auto;
    min-height: 0;
    overflow: hidden;
    border-radius: 0;
}

#gameCanvas {
    display: block;
    width: 100%;
    height: 100%;
    max-width: none;
    max-height: none;
    border: none;
    border-radius: 0;
    box-shadow: none;
    background: #05050c;
    cursor: crosshair;
    object-fit: fill;
}

.hud {
    position: absolute;
    inset: 0;
    pointer-events: none;
    z-index: 5;
}
.hud.hidden { display: none; }

.nitro-wrap {
    position: absolute;
    left: 12px;
    bottom: 14px;
    width: min(200px, 40%);
    font-size: 0.68rem;
    font-weight: 900;
    letter-spacing: 0.12em;
    color: var(--orange);
}
.nitro-wrap small {
    font-size: 0.58rem;
    letter-spacing: 0.04em;
    opacity: 0.75;
    font-weight: 700;
}
.nitro-wrap.is-low { color: #8a7060; }
.nitro-wrap.is-low small { opacity: 1; color: #ff8a60; }
.nitro-wrap.is-ready small { opacity: 0.45; }
.nitro-meter {
    margin-top: 0.2rem;
    height: 10px;
    border-radius: 6px;
    background: rgba(0,0,0,0.5);
    border: 1px solid rgba(255, 176, 64, 0.4);
    overflow: hidden;
    position: relative;
}
/* 20% mark — can't fire below this */
.nitro-meter::after {
    content: '';
    position: absolute;
    left: 20%;
    top: 0;
    bottom: 0;
    width: 2px;
    background: rgba(255,255,255,0.45);
    z-index: 1;
}
.nitro-meter i {
    display: block;
    height: 100%;
    width: 100%;
    transform-origin: left center;
    transform: scaleX(1);
    background: linear-gradient(90deg, #ff4060, #ffb040, #ffe08a);
}
.nitro-meter i.is-low {
    background: linear-gradient(90deg, #664040, #886050);
    opacity: 0.75;
}

.race-banner {
    position: absolute;
    left: 50%;
    top: 18%;
    transform: translateX(-50%) scale(0.9);
    opacity: 0;
    font-size: clamp(1.2rem, 4vw, 2rem);
    font-weight: 900;
    letter-spacing: 0.12em;
    color: #fff;
    text-shadow: 0 0 20px rgba(0, 240, 255, 0.7);
    transition: opacity 0.15s, transform 0.15s;
    white-space: nowrap;
}
.race-banner.show {
    opacity: 1;
    transform: translateX(-50%) scale(1);
}

.hint {
    position: absolute;
    left: 50%;
    bottom: 10px;
    transform: translateX(-50%);
    font-size: 0.7rem;
    font-weight: 700;
    color: rgba(255,255,255,0.55);
    background: rgba(0,0,0,0.35);
    padding: 0.2rem 0.6rem;
    border-radius: 999px;
}

/* Overlay: scrollable, start button always pinned at bottom of panel */
.overlay, .results {
    position: absolute;
    inset: 0;
    display: flex;
    align-items: stretch;
    justify-content: center;
    background: rgba(4, 4, 12, 0.82);
    backdrop-filter: blur(6px);
    z-index: 15;
    padding: 0.5rem;
    overflow: hidden;
}
.overlay.hidden, .results.hidden { display: none; }

.panel, .results-card {
    width: min(480px, 100%);
    max-height: 100%;
    display: flex;
    flex-direction: column;
    background: linear-gradient(165deg, #141424, #0c0c16);
    border: 1px solid rgba(0, 240, 255, 0.3);
    border-radius: 16px;
    text-align: center;
    box-shadow: 0 0 40px rgba(0, 240, 255, 0.12);
    overflow: hidden;
}
.results-card {
    padding: 1.25rem 1.1rem;
    overflow-y: auto;
    align-self: center;
    max-height: min(90%, 520px);
}

.panel-scroll {
    flex: 1 1 auto;
    min-height: 0;
    overflow-y: auto;
    overflow-x: hidden;
    -webkit-overflow-scrolling: touch;
    padding: 1rem 1.1rem 0.5rem;
    overscroll-behavior: contain;
}

.panel-footer {
    flex: 0 0 auto;
    padding: 0.65rem 1.1rem calc(0.75rem + var(--safe-bottom));
    background: linear-gradient(180deg, rgba(12,12,22,0), #0c0c16 28%);
    border-top: 1px solid rgba(0, 240, 255, 0.15);
    position: relative;
    z-index: 2;
}

.eyebrow {
    margin: 0 0 0.35rem;
    font-size: 0.7rem;
    font-weight: 800;
    letter-spacing: 0.14em;
    text-transform: uppercase;
    color: var(--pink);
}
.build-banner {
    display: inline-block;
    margin: 0 auto 0.45rem;
    padding: 0.3rem 0.7rem;
    font-size: 0.68rem;
    font-weight: 900;
    letter-spacing: 0.06em;
    color: #041018;
    background: linear-gradient(90deg, var(--lime), var(--neon) 55%, var(--pink));
    border-radius: 999px;
}
.panel h1, .results-card h2 {
    margin: 0 0 0.35rem;
    font-size: clamp(1.4rem, 5vw, 2rem);
    font-weight: 900;
    letter-spacing: 0.06em;
}
.menu-msg, .results-sub {
    margin: 0 0 0.7rem;
    font-size: 0.84rem;
    color: var(--muted);
    line-height: 1.4;
}
.label {
    margin: 0.35rem 0 0.3rem;
    font-size: 0.68rem;
    font-weight: 800;
    letter-spacing: 0.12em;
    text-transform: uppercase;
    color: var(--neon);
}

.mode-row {
    display: flex;
    gap: 0.4rem;
    justify-content: center;
    margin-bottom: 0.55rem;
    flex-wrap: wrap;
}
.mode {
    font: inherit;
    font-size: 0.82rem;
    font-weight: 800;
    padding: 0.5rem 0.9rem;
    min-height: 44px;
    border-radius: 999px;
    border: 1px solid rgba(0, 240, 255, 0.3);
    background: rgba(0, 240, 255, 0.06);
    color: var(--muted);
    cursor: pointer;
}
.mode.is-active {
    color: #041018;
    background: linear-gradient(90deg, var(--neon), #7af0ff);
    border-color: transparent;
}

.online-panel {
    margin: 0 0 0.65rem;
    padding: 0.55rem;
    border-radius: 12px;
    background: rgba(0, 0, 0, 0.35);
    border: 1px solid rgba(0, 240, 255, 0.18);
    text-align: left;
}
.online-panel.hidden { display: none; }
.online-row { margin-bottom: 0.4rem; }
.online-actions {
    display: flex;
    flex-wrap: wrap;
    gap: 0.35rem;
    align-items: center;
    margin-bottom: 0.4rem;
}
.online-input {
    font: inherit;
    font-size: 0.9rem;
    font-weight: 700;
    padding: 0.5rem 0.65rem;
    min-height: 44px;
    border-radius: 10px;
    border: 1px solid rgba(255,255,255,0.15);
    background: rgba(0,0,0,0.4);
    color: var(--text);
    width: 100%;
}
.online-code {
    width: 5.5rem;
    text-align: center;
    letter-spacing: 0.12em;
    text-transform: uppercase;
    flex: 0 0 auto;
}
.online-or {
    font-size: 0.72rem;
    color: var(--muted);
    font-weight: 700;
}
.online-status {
    margin: 0.25rem 0;
    font-size: 0.75rem;
    color: var(--muted);
    line-height: 1.35;
}
.room-code-display {
    margin: 0.35rem 0;
    font-size: 0.9rem;
    font-weight: 800;
    color: var(--lime);
    letter-spacing: 0.06em;
}
.room-code-display.hidden { display: none; }
.room-code-display strong {
    font-size: 1.25rem;
    color: #fff;
    letter-spacing: 0.2em;
}
.lobby-list {
    list-style: none;
    margin: 0.35rem 0 0;
    padding: 0;
}
.lobby-list li {
    font-size: 0.8rem;
    font-weight: 700;
    padding: 0.3rem 0.4rem;
    border-radius: 8px;
    margin-bottom: 0.2rem;
    background: rgba(0, 240, 255, 0.06);
    color: var(--text);
}
.lobby-list li.host { border-left: 3px solid var(--neon); }
.lobby-list li .tag {
    float: right;
    font-size: 0.68rem;
    color: var(--muted);
}

.track-row {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 0.35rem;
    margin-bottom: 0.35rem;
}
.track-card {
    font: inherit;
    text-align: left;
    padding: 0.45rem 0.5rem;
    min-height: 44px;
    border-radius: 10px;
    border: 1px solid rgba(255,255,255,0.1);
    background: rgba(0,0,0,0.35);
    color: var(--text);
    cursor: pointer;
    display: flex;
    flex-direction: column;
    gap: 0.1rem;
}
.track-card:hover { border-color: rgba(0, 240, 255, 0.45); }
.track-card.is-active {
    border-color: var(--neon);
    background: rgba(0, 240, 255, 0.1);
    box-shadow: 0 0 14px rgba(0, 240, 255, 0.2);
}
.tc-icon { font-size: 1rem; }
.tc-name { font-size: 0.8rem; font-weight: 800; }
.tc-meta { font-size: 0.65rem; color: var(--muted); }
.track-blurb {
    font-size: 0.75rem;
    color: var(--muted);
    margin: 0 0 0.55rem;
    min-height: 1.8em;
}

.diff-row {
    display: flex;
    gap: 0.35rem;
    justify-content: center;
    margin-bottom: 0.5rem;
    flex-wrap: wrap;
}
.diff {
    font: inherit;
    font-size: 0.8rem;
    font-weight: 800;
    padding: 0.45rem 0.8rem;
    min-height: 44px;
    border-radius: 999px;
    border: 1px solid rgba(0, 240, 255, 0.3);
    background: rgba(0, 240, 255, 0.06);
    color: var(--muted);
    cursor: pointer;
}
.diff.is-active {
    color: #041018;
    background: linear-gradient(90deg, var(--neon), #7af0ff);
    border-color: transparent;
}
.diff-princess.is-active {
    background: linear-gradient(90deg, #ff6ec7, #ffb6e6, #ffe0f5);
    color: #4a1035;
}
.diff-hint {
    margin: 0 0 0.55rem;
    font-size: 0.75rem;
    color: var(--muted);
    min-height: 1.4em;
}

/* Steering sensitivity bar */
.sens-wrap {
    display: flex;
    align-items: center;
    gap: 0.45rem;
    margin: 0.15rem 0 0.25rem;
    padding: 0.35rem 0.5rem;
    border-radius: 12px;
    background: rgba(0, 0, 0, 0.28);
    border: 1px solid rgba(0, 240, 255, 0.15);
}
.sens-end {
    font-size: 0.68rem;
    font-weight: 800;
    letter-spacing: 0.04em;
    color: var(--muted);
    flex: 0 0 auto;
    min-width: 2.8rem;
}
.sens-end:last-child { text-align: right; }
.sens-bar {
    flex: 1 1 auto;
    -webkit-appearance: none;
    appearance: none;
    height: 10px;
    border-radius: 999px;
    background: linear-gradient(90deg, rgba(0,240,255,0.15), rgba(0,240,255,0.45), rgba(255,45,149,0.55));
    outline: none;
    cursor: pointer;
    min-height: 28px;
}
.sens-bar::-webkit-slider-thumb {
    -webkit-appearance: none;
    appearance: none;
    width: 22px;
    height: 22px;
    border-radius: 50%;
    background: linear-gradient(145deg, #fff, var(--neon));
    border: 2px solid rgba(255,255,255,0.85);
    box-shadow: 0 0 12px rgba(0, 240, 255, 0.55);
    cursor: grab;
}
.sens-bar::-moz-range-thumb {
    width: 22px;
    height: 22px;
    border-radius: 50%;
    background: linear-gradient(145deg, #fff, var(--neon));
    border: 2px solid rgba(255,255,255,0.85);
    box-shadow: 0 0 12px rgba(0, 240, 255, 0.55);
    cursor: grab;
}
.sens-readout {
    margin: 0 0 0.65rem;
    font-size: 0.72rem;
    font-weight: 700;
    color: var(--neon);
}

/* Princess theme chrome */
#app.theme-princess {
    --neon: #ff6ec7;
    --pink: #ff9ad5;
}
#app.theme-princess .title {
    color: #ff6ec7;
    text-shadow: 0 0 18px rgba(255, 110, 199, 0.55);
}
#app.theme-princess .build-tag {
    background: linear-gradient(90deg, #ff9ad5, #ffe0f5);
    color: #4a1035;
}
#app.theme-princess .cta-start {
    background: linear-gradient(90deg, #ff6ec7, #ff9ad5, #ffd6f0);
    color: #4a1035;
    box-shadow: 0 0 28px rgba(255, 110, 199, 0.5);
}
#app.theme-princess .speedo-progress { stroke: #ff6ec7; }
#app.theme-princess .speedo-unit { color: #ff9ad5; }
.speedo.is-princess .speedo-progress { stroke: #ff6ec7; }
.speedo.is-princess .speedo-unit { color: #ff9ad5; }

.cta {
    font: inherit;
    font-size: 1rem;
    font-weight: 900;
    letter-spacing: 0.08em;
    text-transform: uppercase;
    padding: 0.75rem 1.5rem;
    min-height: 52px;
    border: none;
    border-radius: 999px;
    cursor: pointer;
    color: #041018;
    background: linear-gradient(90deg, var(--pink), #ff6eb4);
    box-shadow: 0 0 24px rgba(255, 45, 149, 0.35);
    width: 100%;
    pointer-events: auto;
    position: relative;
    z-index: 3;
}
.cta-sm {
    width: auto;
    min-height: 44px;
    padding: 0.45rem 0.85rem;
    font-size: 0.78rem;
    flex: 1 1 auto;
}
.cta-start {
    font-size: 1.1rem;
    min-height: 56px;
    box-shadow: 0 0 28px rgba(255, 45, 149, 0.5);
}
.cta-ghost {
    background: transparent;
    color: var(--neon);
    border: 1px solid rgba(0, 240, 255, 0.4);
    box-shadow: none;
}
.cta:active { transform: translateY(2px); }
.cta:disabled {
    opacity: 0.45;
    cursor: not-allowed;
    transform: none;
}
.controls {
    margin: 0.4rem 0 0.5rem;
    font-size: 0.72rem;
    color: var(--muted);
}

.results-eyebrow {
    margin: 0 0 0.35rem;
    font-size: 0.75rem;
    font-weight: 900;
    letter-spacing: 0.14em;
    color: var(--lime);
}
.results-stats {
    display: grid;
    grid-template-columns: repeat(3, 1fr);
    gap: 0.45rem;
    margin: 0.9rem 0 1rem;
}
.results-stats div {
    background: rgba(0,0,0,0.35);
    border-radius: 10px;
    padding: 0.55rem 0.3rem;
}
.results-stats b {
    display: block;
    font-size: 1.15rem;
}
.results-stats span {
    font-size: 0.65rem;
    color: var(--muted);
    letter-spacing: 0.06em;
    text-transform: uppercase;
}
.results-actions {
    display: flex;
    gap: 0.5rem;
    justify-content: center;
    flex-wrap: wrap;
}
.results-actions .cta { width: auto; min-width: 8rem; }

.bottom {
    flex: 0 0 auto;
    display: flex;
    flex-direction: column;
    gap: 0;
    align-items: stretch;
    background: rgba(7, 7, 15, 0.95);
    border-top: 1px solid rgba(0, 240, 255, 0.1);
    padding-bottom: var(--safe-bottom);
    z-index: 12;
}

.touch {
    display: none;
    width: 100%;
    justify-content: space-between;
    gap: 0.3rem;
    padding: 0.4rem 0.45rem;
}
.tbtn {
    flex: 1;
    min-height: 56px;
    border-radius: 12px;
    border: 1px solid rgba(0, 240, 255, 0.3);
    background: rgba(255,255,255,0.06);
    color: var(--text);
    font-size: 1.15rem;
    font-weight: 900;
    cursor: pointer;
    touch-action: none;
}
.tbtn.gas {
    background: rgba(0, 240, 255, 0.15);
    border-color: rgba(0, 240, 255, 0.5);
}
.tbtn.nitro {
    background: rgba(255, 176, 64, 0.15);
    border-color: rgba(255, 176, 64, 0.5);
    color: var(--orange);
    font-size: 0.75rem;
}

@media (max-width: 720px), (pointer: coarse) {
    .touch { display: flex; }
    .hint { display: none; }
    .stats { font-size: 0.62rem; gap: 0.4rem; }
}

/* When racing, hide chrome a bit more on short screens */
@media (max-height: 500px) {
    .bar { padding-top: 0.2rem; padding-bottom: 0.2rem; }
    .title { font-size: 0.75rem; }
}
