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

body {
    font-family: "Segoe UI", Tahoma, Geneva, Verdana, sans-serif;
    background: linear-gradient(135deg, #0a0a0a 0%, #1a1a2e 100%);
    color: #ffffff;
    overflow: hidden;
    -webkit-tap-highlight-color: transparent;
    -webkit-touch-callout: none;
    touch-action: manipulation;
}

#container {
    display: flex;
    flex-direction: column;
    height: 100vh;
    height: 100dvh;
    padding: 20px;
    overflow-y: auto;
}

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

h1 {
    font-size: 2.5em;
    background: linear-gradient(90deg, #ff6b6b, #ffd700, #4ecdc4);
    -webkit-background-clip: text;
    -webkit-text-fill-color: transparent;
    text-shadow: 0 0 30px rgba(255, 215, 0, 0.3);
    margin-bottom: 10px;
}

.subtitle {
    color: #888;
    font-size: 0.9em;
    letter-spacing: 2px;
}

#setupPanel {
    background: rgba(255, 255, 255, 0.05);
    backdrop-filter: blur(10px);
    border: 1px solid rgba(255, 255, 255, 0.1);
    border-radius: 15px;
    padding: 30px;
    max-width: 600px;
    margin: 0 auto;
}

.input-group {
    margin-bottom: 25px;
}

label {
    display: block;
    margin-bottom: 8px;
    color: #ffd700;
    font-weight: 600;
    font-size: 0.95em;
}

input[type="text"],
input[type="number"],
textarea,
select {
    width: 100%;
    padding: 12px 16px;
    background: rgba(0, 0, 0, 0.4);
    border: 2px solid rgba(255, 215, 0, 0.3);
    border-radius: 8px;
    color: #fff;
    font-size: 1em;
    transition: all 0.3s;
    font-family: inherit;
}

textarea {
    resize: vertical;
    line-height: 1.5;
}

input[type="text"]:focus,
input[type="number"]:focus,
textarea:focus,
select:focus {
    outline: none;
    border-color: #ffd700;
    box-shadow: 0 0 15px rgba(255, 215, 0, 0.3);
}

select option {
    background: #1a1a2e;
    color: #fff;
}

.hint {
    font-size: 0.85em;
    color: #888;
    margin-top: 5px;
}

#startButton {
    width: 100%;
    padding: 15px;
    background: linear-gradient(135deg, #ff6b6b, #ffd700);
    border: none;
    border-radius: 10px;
    color: #000;
    font-size: 1.2em;
    font-weight: bold;
    cursor: pointer;
    transition: all 0.3s;
    text-transform: uppercase;
    letter-spacing: 2px;
}

#startButton:hover {
    transform: translateY(-2px);
    box-shadow: 0 10px 30px rgba(255, 215, 0, 0.5);
}

#racePanel {
    display: none;
    flex: 1;
    flex-direction: column;
}

#canvasContainer {
    flex: 1;
    position: relative;
    border-radius: 15px;
    overflow: hidden;
    box-shadow: 0 10px 50px rgba(0, 0, 0, 0.5);
}

#raceCanvas {
    width: 100%;
    height: 100%;
    display: block;
}

#controlPanel {
    background: rgba(0, 0, 0, 0.7);
    backdrop-filter: blur(10px);
    border: 1px solid rgba(255, 255, 255, 0.1);
    border-radius: 15px;
    padding: 20px;
    margin-top: 20px;
    display: flex;
    justify-content: space-between;
    align-items: center;
    flex-wrap: wrap;
    gap: 20px;
}

.control-section {
    display: flex;
    align-items: center;
    gap: 15px;
}

.audio-controls {
    display: flex;
    gap: 10px;
    align-items: center;
}

button {
    padding: 10px 20px;
    background: rgba(255, 255, 255, 0.1);
    border: 1px solid rgba(255, 255, 255, 0.2);
    border-radius: 8px;
    color: #fff;
    cursor: pointer;
    transition: all 0.3s;
    font-size: 0.9em;
}

button:hover {
    background: rgba(255, 255, 255, 0.2);
    transform: scale(1.05);
}

input[type="range"] {
    width: 150px;
    height: 6px;
    background: rgba(255, 255, 255, 0.2);
    border-radius: 5px;
    outline: none;
}

input[type="range"]::-webkit-slider-thumb {
    -webkit-appearance: none;
    appearance: none;
    width: 18px;
    height: 18px;
    background: #ffd700;
    cursor: pointer;
    border-radius: 50%;
}

#mobileRankBar {
    display: none;
}

#leaderboard {
    position: absolute;
    top: 20px;
    right: 20px;
    background: rgba(0, 0, 0, 0.8);
    backdrop-filter: blur(10px);
    border: 2px solid rgba(255, 215, 0, 0.5);
    border-radius: 15px;
    padding: 20px;
    min-width: 250px;
    max-height: 80%;
    overflow-y: auto;
}

.leaderboard-title {
    color: #ffd700;
    font-size: 1.2em;
    margin-bottom: 15px;
    text-align: center;
    font-weight: bold;
    border-bottom: 2px solid rgba(255, 215, 0, 0.3);
    padding-bottom: 10px;
}

#positionsList {
    position: relative;
}

.car-position {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 10px;
    background: rgba(255, 255, 255, 0.05);
    border-radius: 8px;
    border-left: 4px solid transparent;
    transition: transform 0.45s cubic-bezier(0.25, 0.8, 0.25, 1),
                background 0.3s, border-left-color 0.3s, box-shadow 0.3s;
    position: absolute;
    left: 0;
    right: 0;
}

.car-position.rank-up {
    box-shadow: 0 0 12px rgba(0, 160, 60, 0.5);
}

.car-position.rank-down {
    box-shadow: 0 0 12px rgba(255, 50, 50, 0.3);
}

.car-position.first {
    border-left-color: #ffd700;
    background: rgba(255, 215, 0, 0.1);
}

.car-position.second {
    border-left-color: #c0c0c0;
}

.car-position.third {
    border-left-color: #cd7f32;
}


.car-name {
    font-weight: 600;
    display: flex;
    align-items: center;
    gap: 6px;
}

.car-color-dot {
    display: inline-block;
    width: 12px;
    height: 12px;
    border-radius: 50%;
    border: 1.5px solid rgba(255, 255, 255, 0.5);
    flex-shrink: 0;
}

.car-progress {
    color: #4ecdc4;
    font-size: 0.9em;
}

#timer {
    position: absolute;
    top: 20px;
    left: 20px;
    background: rgba(0, 0, 0, 0.8);
    backdrop-filter: blur(10px);
    border: 2px solid rgba(255, 107, 107, 0.5);
    border-radius: 15px;
    padding: 15px 25px;
    font-size: 1.5em;
    font-weight: bold;
    color: #ff6b6b;
}

#lapInfo {
    position: absolute;
    top: 37px;
    left: 153px;
    z-index: 100;
    font-size: 1.2em;
    font-weight: 900;
    color: #fff;
    text-shadow: 0 0 6px rgba(0,0,0,0.8);
}

#lapInfo.last-lap {
    color: #ff4444;
    animation: lastLapBlink 0.6s ease-in-out infinite alternate;
}

@keyframes lastLapBlink {
    from { opacity: 1; }
    to { opacity: 0.5; }
}

#resultsModal {
    display: none;
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background: rgba(0, 0, 0, 0.9);
    backdrop-filter: blur(10px);
    z-index: 1000;
    justify-content: center;
    align-items: center;
}

.results-content {
    background: linear-gradient(135deg, #1a1a2e, #0a0a0a);
    border: 2px solid #ffd700;
    border-radius: 20px;
    padding: 40px;
    max-width: 600px;
    width: 90%;
    box-shadow: 0 20px 60px rgba(255, 215, 0, 0.3);
}

.results-title {
    font-size: 2em;
    color: #ffd700;
    text-align: center;
    margin-bottom: 30px;
    text-transform: uppercase;
}

.podium {
    margin-bottom: 30px;
}

.podium-place {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 15px;
    margin-bottom: 10px;
    border-radius: 10px;
    background: rgba(255, 255, 255, 0.05);
}

.podium-place .car-color-dot {
    width: 14px;
    height: 14px;
    vertical-align: middle;
    margin-right: 4px;
}

.podium-place.gold {
    background: linear-gradient(135deg, rgba(255, 215, 0, 0.2), rgba(255, 215, 0, 0.05));
    border: 2px solid #ffd700;
}

.podium-place.silver {
    border: 2px solid #c0c0c0;
}

.podium-place.bronze {
    border: 2px solid #cd7f32;
}

.medal {
    font-size: 1.5em;
    margin-right: 15px;
}

#rematchButton, #restartButton {
    width: 100%;
    padding: 15px;
    border: none;
    border-radius: 10px;
    color: #fff;
    font-size: 1.1em;
    font-weight: bold;
    cursor: pointer;
    margin-top: 10px;
}

#rematchButton {
    background: linear-gradient(135deg, #ff6b6b, #ee5a24);
}

#restartButton {
    background: linear-gradient(135deg, #4ecdc4, #44a39d);
}

#floatingMute {
    position: absolute;
    bottom: 20px;
    left: 20px;
    z-index: 10;
    padding: 10px 16px;
    background: rgba(0, 0, 0, 0.75);
    backdrop-filter: blur(8px);
    border: 1.5px solid rgba(255, 255, 255, 0.25);
    border-radius: 10px;
    color: #fff;
    cursor: pointer;
    font-size: 1em;
    transition: all 0.2s;
    display: flex;
    align-items: center;
    gap: 8px;
}

#floatingMute:hover {
    background: rgba(255, 255, 255, 0.15);
    transform: scale(1.05);
}

#floatingMute.muted {
    border-color: rgba(255, 80, 80, 0.5);
    color: #ff8888;
}

#countdownOverlay {
    display: none;
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background: rgba(0, 0, 0, 0.85);
    z-index: 2000;
    justify-content: center;
    align-items: center;
}

#countdownText {
    font-size: 8em;
    font-weight: 900;
    color: #ffd700;
    text-shadow: 0 0 40px rgba(255, 215, 0, 0.6), 0 0 80px rgba(255, 215, 0, 0.3);
    animation: countdownPop 0.8s ease-out;
}

@keyframes countdownPop {
    0% { transform: scale(2.5); opacity: 0; }
    40% { transform: scale(0.9); opacity: 1; }
    60% { transform: scale(1.1); }
    100% { transform: scale(1); opacity: 1; }
}

#countdownText.go {
    color: #00ff88;
    text-shadow: 0 0 40px rgba(0, 255, 136, 0.6), 0 0 80px rgba(0, 255, 136, 0.3);
}

#countdownText.last-lap-flash {
    color: #ff4444;
    text-shadow: 0 0 40px rgba(255, 68, 68, 0.8), 0 0 80px rgba(255, 68, 68, 0.4);
    animation: countdownPop 0.8s ease-out;
}

@keyframes pulse {
    0%, 100% { opacity: 1; }
    50% { opacity: 0.5; }
}

.racing {
    animation: pulse 1s infinite;
}

/* ============================================================
   Mobile Responsive
   ============================================================ */

@media screen and (max-width: 768px) {
    #container {
        padding: 10px;
    }

    #header {
        margin-bottom: 10px;
    }

    h1 {
        font-size: 1.4em;
    }

    .subtitle {
        font-size: 0.75em;
    }

    #setupPanel {
        padding: 18px;
        border-radius: 10px;
        max-width: 100%;
    }

    .input-group {
        margin-bottom: 16px;
    }

    label {
        font-size: 0.85em;
        margin-bottom: 5px;
    }

    input[type="text"],
    input[type="number"],
    textarea,
    select {
        padding: 10px 12px;
        font-size: 16px; /* Prevents iOS zoom on focus */
    }

    .hint {
        font-size: 0.75em;
    }

    #startButton {
        padding: 12px;
        font-size: 1em;
        letter-spacing: 1px;
    }

    /* Race Panel */
    #racePanel {
        gap: 0;
    }

    #canvasContainer {
        border-radius: 8px;
        min-height: 45vh;
        min-height: 45dvh;
    }

    /* Timer */
    #timer {
        top: 8px;
        left: 8px;
        padding: 8px 14px;
        font-size: 1em;
        border-radius: 10px;
    }

    /* Desktop leaderboard: completely hidden on mobile */
    #leaderboard {
        display: none !important;
    }

    /* Mobile rank bar: compact horizontal strip below canvas */
    #mobileRankBar {
        display: block;
        background: rgba(0, 0, 0, 0.85);
        border: 1px solid rgba(255, 215, 0, 0.4);
        border-radius: 8px;
        padding: 6px 8px;
        margin-top: 6px;
        overflow-x: auto;
        -webkit-overflow-scrolling: touch;
    }

    #mobileRankList {
        display: flex;
        gap: 4px;
        min-width: max-content;
    }

    .mobile-rank-item {
        display: flex;
        align-items: center;
        gap: 4px;
        padding: 4px 8px;
        background: rgba(255, 255, 255, 0.06);
        border-radius: 6px;
        white-space: nowrap;
        flex-shrink: 0;
    }

    .mobile-rank-item.first {
        background: rgba(255, 215, 0, 0.15);
        border: 1px solid rgba(255, 215, 0, 0.4);
    }

    .mobile-rank-item .rank-num {
        font-weight: 700;
        font-size: 0.7em;
        color: #ffd700;
        min-width: 14px;
    }

    .mobile-rank-item .rank-dot {
        width: 8px;
        height: 8px;
        border-radius: 50%;
        border: 1px solid rgba(255,255,255,0.4);
        flex-shrink: 0;
    }

    .mobile-rank-item .rank-name {
        font-size: 0.7em;
        font-weight: 600;
        max-width: 60px;
        overflow: hidden;
        text-overflow: ellipsis;
    }

    .mobile-rank-item .rank-info {
        font-size: 0.6em;
        color: #4ecdc4;
    }

    /* Floating mute */
    #floatingMute {
        bottom: 8px;
        left: 8px;
        padding: 8px 12px;
        font-size: 0.85em;
        border-radius: 8px;
    }

    /* Control panel */
    #controlPanel {
        padding: 12px;
        margin-top: 10px;
        border-radius: 10px;
        gap: 10px;
        justify-content: center;
    }

    .control-section {
        flex-wrap: wrap;
        gap: 8px;
        justify-content: center;
    }

    .audio-controls {
        gap: 6px;
    }

    button {
        padding: 8px 14px;
        font-size: 0.8em;
    }

    input[type="range"] {
        width: 100px;
    }

    /* Results modal */
    .results-content {
        padding: 24px 18px;
        border-radius: 14px;
        width: 94%;
        max-height: 85vh;
        max-height: 85dvh;
        overflow-y: auto;
    }

    .results-title {
        font-size: 1.4em;
        margin-bottom: 18px;
    }

    .podium-place {
        padding: 10px;
        margin-bottom: 8px;
        font-size: 0.85em;
        flex-direction: column;
        align-items: flex-start;
        gap: 4px;
    }

    .medal {
        font-size: 1.2em;
        margin-right: 8px;
    }

    #rematchButton, #restartButton {
        padding: 12px;
        font-size: 1em;
    }
}

@media screen and (max-width: 380px) {
    h1 {
        font-size: 1.1em;
    }

    #leaderboard {
        min-width: 130px;
        padding: 8px;
    }

    .car-name {
        font-size: 0.72em;
    }

    .car-progress {
        font-size: 0.65em;
    }

    #timer {
        font-size: 0.85em;
        padding: 6px 10px;
    }
}

/* Landscape mobile: maximize canvas height */
@media screen and (max-height: 500px) and (orientation: landscape) {
    #container {
        padding: 5px;
    }

    #header {
        display: none;
    }

    #canvasContainer {
        min-height: 70vh;
        min-height: 70dvh;
    }

    #controlPanel {
        padding: 8px;
        margin-top: 5px;
    }

    #timer {
        top: 5px;
        left: 5px;
        padding: 5px 10px;
        font-size: 0.85em;
    }

    #mobileRankBar {
        margin-top: 3px;
        padding: 4px 6px;
    }

    .mobile-rank-item {
        padding: 3px 6px;
    }

    #floatingMute {
        bottom: 5px;
        left: 5px;
        padding: 6px 10px;
        font-size: 0.75em;
    }
}
