:root {
    --bg: #000000;
    --bg-soft: #0a0a0b;
    --surface: #17181c;
    --border: rgba(255, 255, 255, 0.08);
    --border-strong: rgba(255, 255, 255, 0.18);
    --text-main: #f5f5f7;
    --text-muted: #8e8e93;

    --red: #e82127;         /* Tesla red — CTA + redline */
    --red-hover: #ff3b3f;
    --red-glow: rgba(232, 33, 39, 0.35);

    --blue: #3e6ae1;        /* Tesla blue — selection / interactive accent */
    --blue-glow: rgba(62, 106, 225, 0.35);

    --radius-lg: 28px;
    --radius-md: 18px;
    --radius-sm: 14px;
    --radius-pill: 999px;

    --ease: cubic-bezier(0.4, 0, 0.2, 1);
}

* {
    box-sizing: border-box;
    margin: 0;
    padding: 0;
}

html {
    overflow-x: hidden;
}

body {
    font-family: 'Inter', sans-serif;
    background-color: var(--bg);
    color: var(--text-main);
    display: flex;
    justify-content: center;
    align-items: center;
    min-height: 100vh;
    padding: 16px;
    background: radial-gradient(circle at top, #131417 0%, var(--bg) 75%);
    overflow-x: hidden;
    -webkit-tap-highlight-color: transparent;
}

button {
    font-family: inherit;
}

:focus-visible {
    outline: 2px solid var(--blue);
    outline-offset: 2px;
}

#starfield {
    position: fixed;
    inset: 0; /* not 100vw/100vh: those include scrollbar gutter width and can force a horizontal scrollbar */
    z-index: -1;
    pointer-events: none;
}

.app-container {
    background: rgba(17, 18, 20, 0.66);
    backdrop-filter: blur(18px);
    -webkit-backdrop-filter: blur(18px);
    border-radius: var(--radius-lg);
    padding: 22px;
    width: 100%;
    max-width: 720px;
    box-shadow: 0 30px 60px rgba(0, 0, 0, 0.55), inset 0 1px 1px rgba(255, 255, 255, 0.06);
    border: 1px solid var(--border);
}

header {
    text-align: center;
    margin-bottom: 20px;
}

header h1 {
    font-weight: 700;
    font-size: clamp(1.4rem, 4vw, 2rem);
    letter-spacing: 0.16em;
    color: var(--text-main);
    margin-bottom: 6px;
}

header p {
    color: var(--text-muted);
    font-weight: 400;
    font-size: 0.85rem;
    letter-spacing: 0.01em;
}

.status-panel {
    background: rgba(0, 0, 0, 0.35);
    border-radius: var(--radius-md);
    padding: 18px 16px;
    text-align: center;
    margin-bottom: 20px;
    border: 1px solid var(--border);
    position: relative;
}

.gauge-container {
    position: relative;
    width: 168px;
    height: 168px;
    margin: 0 auto;
}

.speed-gauge-ring {
    width: 100%;
    height: 100%;
    transform: rotate(135deg);
}

.gauge-track {
    fill: none;
    stroke: rgba(255, 255, 255, 0.08);
    stroke-width: 8;
    stroke-dasharray: 439.8; /* 2 * pi * 70 */
    stroke-dashoffset: 109.9; /* 25% gap */
}

.gauge-fill {
    fill: none;
    stroke: var(--blue);
    stroke-width: 8;
    stroke-linecap: round;
    stroke-dasharray: 439.8;
    stroke-dashoffset: 439.8;
    transition: stroke-dashoffset 0.1s linear, stroke 0.2s var(--ease);
}

.speed-display {
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
    display: flex;
    flex-direction: column;
    justify-content: center;
    align-items: center;
    color: var(--text-main);
}

.speed-main {
    font-size: 2.8rem;
    font-weight: 700;
    line-height: 1;
    color: var(--text-main);
    text-shadow: 0 0 24px rgba(255, 255, 255, 0.18);
    display: flex;
    flex-direction: column;
    align-items: center;
}

.speed-main .unit {
    font-size: 0.85rem;
    font-weight: 500;
    color: var(--text-muted);
    opacity: 0.9;
    letter-spacing: 0.04em;
}

.gear-display-n {
    margin-top: 8px;
    font-size: 1.2rem;
    font-weight: 700;
    line-height: 1;
    color: var(--blue);
    letter-spacing: 0.02em;
}

.status-text {
    margin-top: 6px;
    font-size: 0.8rem;
    color: var(--text-muted);
    transition: color 0.3s var(--ease);
}

.status-text.active { color: #4ade80; }
.status-text.error { color: var(--red); }

.rpm-display {
    margin-top: 12px;
    height: 6px;
    background: rgba(255, 255, 255, 0.08);
    border-radius: var(--radius-pill);
    overflow: hidden;
}
.rpm-bar {
    height: 100%;
    width: 100%;
    transform: scaleX(0);
    transform-origin: left;
    background: linear-gradient(90deg, var(--blue), var(--red));
    transition: transform 0.1s linear;
    will-change: transform;
}

.controls h2 {
    font-size: 0.95rem;
    margin-bottom: 12px;
    color: var(--text-main);
    font-weight: 600;
    letter-spacing: 0.01em;
}

.sound-selector {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(88px, 1fr));
    gap: 8px;
    margin-bottom: 16px;
}

.sound-btn {
    background: rgba(255, 255, 255, 0.03);
    border: 1px solid var(--border);
    border-radius: var(--radius-sm);
    padding: 10px 4px;
    color: var(--text-muted);
    cursor: pointer;
    transition: transform 0.2s var(--ease), background 0.2s var(--ease), border-color 0.2s var(--ease), color 0.2s var(--ease), box-shadow 0.2s var(--ease);
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 4px;
}

.sound-btn:hover {
    background: rgba(255, 255, 255, 0.07);
    border-color: var(--border-strong);
    transform: translateY(-2px);
}

.sound-btn:active {
    transform: scale(0.96);
}

.sound-btn.active {
    background: rgba(62, 106, 225, 0.12);
    border-color: var(--blue);
    color: var(--text-main);
    box-shadow: 0 0 18px var(--blue-glow);
}

.sound-btn .icon { font-size: 1.3rem; }
.sound-btn span { font-size: 0.68rem; font-weight: 600; text-align: center; }

.extra-controls {
    display: flex;
    justify-content: space-between;
    align-items: center;
    gap: 12px;
    margin-bottom: 18px;
    background: rgba(255, 255, 255, 0.03);
    padding: 12px 14px;
    border-radius: var(--radius-md);
    border: 1px solid var(--border);
}

.toggle-container {
    display: flex;
    align-items: center;
    gap: 10px;
    font-size: 0.9rem;
    cursor: pointer;
    user-select: none;
}

.toggle-container input {
    display: none;
}

.toggle-slider {
    position: relative;
    width: 44px;
    height: 26px;
    background-color: #333338;
    border-radius: var(--radius-pill);
    transition: background-color 0.2s var(--ease);
    flex-shrink: 0;
}

.toggle-slider:before {
    position: absolute;
    content: "";
    height: 20px;
    width: 20px;
    left: 3px;
    top: 3px;
    background-color: white;
    border-radius: 50%;
    transition: transform 0.2s var(--ease);
}

input:checked + .toggle-slider {
    background-color: var(--blue);
}

input:checked + .toggle-slider:before {
    transform: translateX(18px);
}

.secondary-btn {
    background: rgba(255, 255, 255, 0.08);
    border: 1px solid var(--border-strong);
    color: white;
    padding: 11px 22px;
    border-radius: var(--radius-pill);
    cursor: pointer;
    font-weight: 600;
    font-size: 0.9rem;
    white-space: nowrap;
    transition: background 0.2s var(--ease), transform 0.15s var(--ease);
}

.secondary-btn:hover {
    background: rgba(255, 255, 255, 0.16);
}

.secondary-btn:active {
    transform: scale(0.95);
    background: var(--red);
    border-color: var(--red);
}

.primary-btn {
    width: 100%;
    background: linear-gradient(135deg, var(--red), #c81a1f);
    color: white;
    border: none;
    border-radius: var(--radius-pill);
    padding: 14px;
    font-size: 0.95rem;
    font-weight: 600;
    cursor: pointer;
    transition: transform 0.2s var(--ease), box-shadow 0.2s var(--ease), background 0.2s var(--ease);
    text-transform: uppercase;
    letter-spacing: 0.06em;
    box-shadow: 0 10px 24px var(--red-glow);
}

.primary-btn:hover {
    background: linear-gradient(135deg, var(--red-hover), var(--red));
    transform: translateY(-2px);
    box-shadow: 0 15px 30px var(--red-glow);
}

.primary-btn:active {
    transform: translateY(0) scale(0.98);
}

.primary-btn.playing {
    background: #2c2c2e;
    box-shadow: none;
}

.warning {
    margin-top: 18px;
    font-size: 0.75rem;
    color: var(--text-muted);
    text-align: center;
    line-height: 1.4;
}

/* --- Light Mode Overrides (Automatic) --- */
@media (prefers-color-scheme: light) {
    :root {
        --bg: #f5f5f7;
        --surface: rgba(255, 255, 255, 0.85);
        --text-main: #1d1d1f;
        --text-muted: #6e6e73;
        --border: rgba(0, 0, 0, 0.08);
        --border-strong: rgba(0, 0, 0, 0.16);
    }

    body {
        background: radial-gradient(circle at top, #ffffff 0%, var(--bg) 80%);
    }

    .app-container {
        background: rgba(255, 255, 255, 0.7);
        box-shadow: 0 30px 60px rgba(0, 0, 0, 0.12), inset 0 1px 1px rgba(255, 255, 255, 0.6);
    }

    .status-panel {
        background: rgba(255, 255, 255, 0.55);
        border: 1px solid var(--border);
    }

    .gauge-track {
        stroke: rgba(0, 0, 0, 0.08);
    }

    .speed-main {
        text-shadow: none;
    }

    .rpm-display {
        background: rgba(0, 0, 0, 0.08);
    }

    .sound-btn {
        background: rgba(0, 0, 0, 0.03);
        border: 1px solid var(--border);
        color: var(--text-main);
    }

    .sound-btn:hover {
        background: rgba(0, 0, 0, 0.06);
    }

    .sound-btn.active {
        background: var(--blue);
        border-color: var(--blue);
        color: white;
    }

    .extra-controls {
        background: rgba(0, 0, 0, 0.03);
    }

    .secondary-btn {
        background: rgba(0, 0, 0, 0.06);
        border: 1px solid var(--border-strong);
        color: var(--text-main);
    }

    .secondary-btn:hover {
        background: rgba(0, 0, 0, 0.1);
    }
}

.manual-controls {
    display: none;
    gap: 15px;
    margin-top: 20px;
}

.manual-controls.active {
    display: flex;
}

.pedal-btn {
    flex: 1;
    padding: 22px;
    font-size: 1.1rem;
    font-weight: 700;
    letter-spacing: 0.04em;
    border: none;
    border-radius: var(--radius-md);
    color: white;
    cursor: pointer;
    user-select: none;
    transition: transform 0.15s var(--ease), background 0.15s var(--ease);
    box-shadow: 0 8px 20px rgba(0, 0, 0, 0.35);
}

.gas { background: #10b981; }
.gas:active { background: #059669; transform: scale(0.95); }
.brake { background: var(--red); }
.brake:active { background: #c81a1f; transform: scale(0.95); }

/* Native, swipeable sound picker on small screens */
@media (max-width: 520px) {
    .app-container {
        padding: 18px 14px;
        border-radius: 20px;
    }

    .sound-selector {
        display: flex;
        overflow-x: auto;
        scroll-snap-type: x proximity;
        padding-bottom: 6px;
        margin: 0 -14px 16px;
        padding-left: 14px;
        padding-right: 14px;
    }

    .sound-btn {
        flex: 0 0 78px;
        scroll-snap-align: start;
    }

    .extra-controls {
        flex-wrap: wrap;
    }
}
