* {
    margin: 0;
    padding: 0;
    box-sizing: border-box;
}

body {
    background: #000;
    /* overflow: hidden; -> Removed to allow scrolling in menus */
    font-family: 'Arial', sans-serif;
    /* touch-action: none; -> Removed to allow scrolling in menus */
}

#gameCanvas {
    display: block;
    width: 100vw;
    height: 100vh;
    touch-action: none;
    /* Prevent default touch behaviors */
    position: fixed;
    top: 0;
    left: 0;
    z-index: 1;
    /* 배경 레이어 */
}

#ui-overlay {
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    pointer-events: none;
    z-index: 100;
    /* UI 레이어 */
}

.hud-panel {
    background: rgba(0, 0, 0, 0.5);
    color: #fff;
    padding: 10px 20px;
    border-radius: 5px;
    font-family: 'Courier New', monospace;
    font-weight: bold;
    border: 2px solid rgba(255, 255, 255, 0.3);
}

#race-ui div {
    margin-bottom: 5px;
    font-size: 20px;
}

#lap-counter {
    color: #ffff00;
}

#lap-timer {
    font-size: 28px;
    color: #00ff00;
}

#minimap-container {
    width: 250px;
    height: 180px;
}

#minimapCanvas {
    width: 100%;
    height: 100%;
}

.warning {
    position: absolute;
    top: 20%;
    left: 50%;
    transform: translateX(-50%);
    background: rgba(255, 0, 0, 0.7);
    color: #fff;
    padding: 10px 40px;
    font-size: 32px;
    font-weight: bold;
    border-radius: 10px;
    animation: blink 0.5s infinite alternate;
}

@keyframes blink {
    from {
        opacity: 1;
    }

    to {
        opacity: 0.3;
    }
}

#countdown-overlay.hidden {
    display: none;
}

#speedometer {
    top: auto;
    bottom: 20px;
    left: 20px;
}

/* Mobile Controls */
#mobile-controls {
    pointer-events: none;
    /* ⭐ 컨테이너는 투명, 자식(조이스틱)만 터치 가능 */
    position: fixed;
    z-index: 200;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
}

.touch-zone {
    width: 150px;
    height: 150px;
    background: rgba(255, 255, 255, 0.1);
    border: 2px solid rgba(255, 255, 255, 0.3);
    border-radius: 50%;
    position: absolute;
    touch-action: none;
    z-index: 2000;
    pointer-events: auto;
    /* 명시적으로 이벤트 활성화 */
    display: flex;
    justify-content: center;
    align-items: center;
    backdrop-filter: blur(2px);
}

.touch-knob {
    width: 50px;
    height: 50px;
    background: rgba(255, 255, 255, 0.8);
    border-radius: 50%;
    position: absolute;
    /* Center the knob */
    top: 50%;
    left: 50%;
    margin-top: -25px;
    margin-left: -25px;

    pointer-events: none;
    /* Let clicks pass through to zone */
    transition: transform 0.05s;
    /* Faster update */
    box-shadow: 0 0 10px rgba(0, 0, 0, 0.5);
}

.zone-label {
    position: absolute;
    color: rgba(255, 255, 255, 0.5);
    font-weight: bold;
    font-size: 20px;
    pointer-events: none;
    text-align: center;
}

.zone-label.top {
    top: 10px;
}

.zone-label.bottom {
    bottom: 10px;
}

.zone-label.left {
    left: 10px;
}

.zone-label.right {
    right: 10px;
}

.zone-center {
    width: 10px;
    height: 10px;
    background: rgba(255, 255, 255, 0.3);
    border-radius: 50%;
}

/* Zone Positioning */
#zone-pedal {
    bottom: 40px;
    left: 40px;
}

#zone-steer {
    bottom: 40px;
    right: 40px;
}

/* Adjust HUD for mobile */
@media (max-width: 768px) {

    /* Lap Info 30% 더 축소 (0.5 -> 0.7) */
    #race-ui {
        transform: scale(0.7);
        transform-origin: top left;
        top: 10px;
        left: 10px;
    }

    /* Minimap 30% 더 축소 (0.5 -> 0.35) */
    #minimap-container {
        transform: scale(0.35);
        transform-origin: top right;
        top: 10px;
        right: 10px;
    }

    /* Speedometer를 Lap Info 아래로, 30% 축소 */
    /* Speedometer를 Lap Info 아래로, 30% 축소 */
    #speedometer {
        transform: scale(0.7);
        transform-origin: top left;
        top: 120px;
        left: 10px;
        bottom: auto;

        /* 강제 크기 조정 - 배경 길이 문제 해결 */
        width: fit-content !important;
        height: fit-content !important;
        min-height: 0 !important;

        font-size: 20px !important;
        padding: 5px 10px !important;
        line-height: normal !important;
        display: inline-block !important;
    }

    /* Smaller screens: Reduce joystick size and separate them */
    .touch-zone {
        width: 120px;
        height: 120px;
    }

    #zone-pedal {
        bottom: 20px;
        left: 20px;
    }

    #zone-steer {
        bottom: 20px;
        right: 20px;
    }
}

/* The #lap-timer rule was moved into #race-ui div, so this old one is removed */

#off-road-warning {
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
    background: rgba(255, 0, 0, 0.7);
    color: #fff;
    padding: 30px 60px;
    border-radius: 15px;
    font-size: 48px;
    font-weight: bold;
    text-align: center;
    animation: pulse 0.5s ease-in-out infinite alternate;
}

#wrong-way-warning {
    position: absolute;
    top: 60%;
    left: 50%;
    transform: translate(-50%, -50%);
    background: rgba(255, 165, 0, 0.7);
    color: #fff;
    padding: 30px 60px;
    border-radius: 15px;
    font-size: 48px;
    font-weight: bold;
    text-align: center;
    animation: pulse 0.5s ease-in-out infinite alternate;
}

#off-road-warning.hidden,
#wrong-way-warning.hidden {
    display: none;
}

@keyframes pulse {
    from {
        transform: translate(-50%, -50%) scale(1);
    }

    to {
        transform: translate(-50%, -50%) scale(1.05);
    }
}

.hidden {
    display: none !important;
}

/* Main Menu */
#main-menu {
    position: fixed;
    top: 0;
    left: 0;
    width: 100vw;
    height: 100vh;
    background: url('../assets/backgrounds/title_bg.png') no-repeat center center/cover;
    display: flex;
    flex-direction: column;
    justify-content: flex-start;
    /* center -> flex-start (스크롤 시 잘림 방지) */
    align-items: center;
    z-index: 1000;
    overflow-y: auto;
    /* 스크롤 활성화 */
    -webkit-overflow-scrolling: touch;
    padding: 50px 0;
    /* 위아래 여백 */
}

#main-menu::before {
    content: '';
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background: rgba(0, 0, 0, 0.3);
    /* Dim background slightly for text readability */
    z-index: -1;
}

#main-menu h1 {
    color: #fff;
    font-size: 80px;
    margin-bottom: 50px;
    text-shadow: 0 0 20px #00ff00, 2px 2px 0 #000;
    font-style: italic;
    font-family: 'Arial Black', sans-serif;
}

.menu-buttons {
    display: flex;
    gap: 30px;
}

.menu-btn {
    padding: 20px 40px;
    font-size: 24px;
    font-weight: bold;
    color: #fff;
    background: rgba(51, 51, 51, 0.8);
    border: 3px solid #00ff00;
    border-radius: 10px;
    cursor: pointer;
    transition: all 0.3s ease;
    backdrop-filter: blur(5px);
    pointer-events: auto;
    /* Enable clicks even when parent has pointer-events: none */
}

.menu-btn:hover {
    background: #00ff00;
    color: #000;
    transform: scale(1.1) skew(-10deg);
    box-shadow: 0 0 20px #00ff00;
}

.menu-contact {
    flex-shrink: 0;
    margin-top: auto;
    padding: 28px 20px 0;
    max-width: 100%;
    text-align: center;
}

.menu-contact a {
    display: inline-block;
    padding: 12px 8px;
    color: #ccc;
    font-size: 14px;
    line-height: 20px;
    overflow-wrap: anywhere;
    text-underline-offset: 3px;
    text-shadow: 1px 1px 2px #000;
}

.menu-contact a:hover,
.menu-contact a:focus-visible {
    color: #00ff00;
}

body:has(#name-input-overlay) .menu-contact {
    visibility: hidden;
}

/* Editor UI */
#editor-container {
    position: relative;
    width: 100vw;
    height: 100vh;
    background: #222;
}

#editorCanvas {
    display: block;
    width: 100%;
    height: 100%;
    cursor: crosshair;
}

#editor-ui {
    position: absolute;
    top: 10px;
    left: 10px;
    right: 10px;
    display: flex;
    justify-content: space-between;
    align-items: center;
    pointer-events: none;
}

.editor-info {
    background: rgba(0, 0, 0, 0.8);
    color: #fff;
    padding: 5px 15px;
    border-radius: 5px;
    border: 1px solid #aaa;
    font-family: monospace;
    font-size: 14px;
}

.editor-info span {
    margin-right: 20px;
}

.editor-controls {
    pointer-events: auto;
    display: flex;
    gap: 5px;
}

.editor-controls button {
    padding: 5px 10px;
    font-size: 14px;
    background: #444;
    color: #fff;
    border: 1px solid #aaa;
    border-radius: 5px;
    cursor: pointer;
    opacity: 0.9;
}

.editor-controls button:hover {
    background: #666;
    opacity: 1;
}

.editor-controls button.danger {
    background: #aa3333;
    border-color: #ff5555;
}

.editor-controls button.danger:hover {
    background: #cc4444;
}

#editor-help {
    position: absolute;
    bottom: 20px;
    left: 50%;
    transform: translateX(-50%);
    background: rgba(0, 0, 0, 0.7);
    color: #aaa;
    padding: 5px 15px;
    border-radius: 20px;
    font-size: 14px;
    pointer-events: none;
}

/* Track Selection UI - Fullscreen Grid */
#track-selection-overlay {
    position: fixed;
    top: 0;
    left: 0;
    width: 100vw;
    height: 100vh;
    background: rgba(0, 0, 0, 0.95);
    display: block;
    /* Flex 제거 -> Block으로 변경 (자연스러운 스크롤) */
    overflow-y: auto;
    /* 세로 스크롤 활성화 */
    z-index: 1100;
    padding: 60px 20px 40px 20px;
    text-align: center;
    /* 내용 중앙 정렬 */
}

#track-selection-overlay h2 {
    color: #fff;
    font-size: 48px;
    margin-bottom: 40px;
    text-shadow: 0 0 20px #00ff00;
    text-align: center;
}

.track-list-fullscreen {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(280px, 320px));
    gap: 30px;
    justify-content: center;
    align-items: start;
    width: 100%;
    max-width: 1400px;
    padding: 20px;
    margin: 0 auto;
    /* 중앙 정렬 */
    /* overflow-y: auto; -> 부모가 스크롤 하므로 제거 */
    /* max-height: calc(100vh - 240px); -> 제거하여 내용만큼 늘어나게 함 */
}

.track-card {
    background: linear-gradient(135deg, #2a2a2a 0%, #1a1a1a 100%);
    border: 3px solid #444;
    border-radius: 15px;
    width: 100%;
    max-width: 320px;
    cursor: pointer;
    transition: all 0.3s ease;
    overflow: hidden;
    position: relative;
}

.track-card:hover {
    transform: translateY(-10px) scale(1.02);
    border-color: #00ff00;
    box-shadow: 0 10px 30px rgba(0, 255, 0, 0.4);
}

.track-card.selected {
    border-color: #00ff00;
    box-shadow: 0 0 20px rgba(0, 255, 0, 0.5);
    background: #444;
}

.track-card img {
    width: 100%;
    height: 180px;
    object-fit: cover;
    border-bottom: 1px solid #444;
}

.track-info {
    padding: 15px;
    text-align: left;
}

.track-name {
    color: #fff;
    font-size: 18px;
    font-weight: bold;
    margin-bottom: 5px;
}

.track-difficulty {
    font-size: 14px;
    padding: 2px 8px;
    border-radius: 10px;
    display: inline-block;
}

.difficulty-Easy {
    background: #2e7d32;
    color: #fff;
}

.difficulty-Medium {
    background: #f57f17;
    color: #fff;
}

.difficulty-Hard {
    background: #c62828;
    color: #fff;
}

#track-selection-overlay .menu-btn.small {
    /* position: absolute; -> 제거 (흐름 배치) */
    /* bottom: 40px; */
    /* left: 50%; */
    /* transform: translateX(-50%); */
    margin: 30px auto;
    /* 중앙 정렬 및 여백 */
    display: inline-block;
}

/* Mobile Responsive */
@media (max-width: 768px) {
    .track-list-fullscreen {
        /* Smaller columns for mobile (2 columns if possible, else 1) */
        grid-template-columns: repeat(auto-fit, minmax(130px, 1fr));
        gap: 15px;
        padding: 10px;
        /* max-height: calc(100vh - 180px); -> 제거 */
    }

    #track-selection-overlay {
        padding: 60px 10px 80px 10px;
        /* Adjust padding */
    }

    #track-selection-overlay h2 {
        font-size: 28px;
        margin-bottom: 15px;
    }

    /* Make track cards smaller */
    .track-card {
        border-radius: 10px;
    }

    .track-card img {
        height: 100px;
        /* Reduced height for preview */
    }

    .track-info {
        padding: 10px;
    }

    .track-name {
        font-size: 14px;
        margin-bottom: 3px;
    }

    .track-difficulty {
        font-size: 11px;
        padding: 1px 6px;
    }

    #track-selection-overlay .menu-btn.small {
        /* bottom: 20px; -> 제거 */
        margin: 20px auto;
    }
}

.menu-btn.small {
    padding: 10px 20px;
    font-size: 16px;
    margin-top: 10px;
}

.editor-corner-btn {
    position: absolute;
    top: 20px;
    right: 20px;
    z-index: 10;
    font-size: 14px;
    padding: 8px 16px;
    border: 2px solid rgba(255, 255, 255, 0.5);
    background: rgba(51, 51, 51, 0.6);
}

.editor-corner-btn:hover {
    border-color: #00ff00;
    background: rgba(0, 255, 0, 0.2);
    transform: scale(1.05);
    box-shadow: 0 0 10px rgba(0, 255, 0, 0.5);
}

/* Audio Settings Toggles */
.audio-settings {
    margin: 20px 0;
    display: flex;
    justify-content: center;
    gap: 30px;
}

.toggle-switch {
    display: flex;
    align-items: center;
    gap: 10px;
    cursor: pointer;
    font-size: 1.2rem;
    color: #fff;
    user-select: none;
}

.toggle-switch input {
    display: none;
}

.slider {
    position: relative;
    width: 60px;
    height: 30px;
    background-color: #333;
    border-radius: 30px;
    transition: .3s;
    border: 2px solid #555;
    box-shadow: inset 0 2px 5px rgba(0, 0, 0, 0.5);
}

.toggle-switch input:checked+.slider {
    background-color: #00ffcc;
    border-color: #00ffcc;
    box-shadow: 0 0 10px rgba(0, 255, 204, 0.5);
}

.slider::before {
    content: "";
    position: absolute;
    height: 22px;
    width: 22px;
    left: 2px;
    bottom: 2px;
    background-color: white;
    border-radius: 50%;
    transition: .3s;
    box-shadow: 0 2px 5px rgba(0, 0, 0, 0.3);
}

.toggle-switch input:checked+.slider::before {
    transform: translateX(30px);
}

.label {
    font-family: 'Orbitron', serif;
    font-weight: bold;
    text-shadow: 0 0 5px rgba(0, 0, 0, 0.5);
}

/* Fix scrollbar interaction by re-enabling pointer events */
#finish-overlay,
.finish-container {
    pointer-events: auto;
}

/* Leaderboard Ticker */
#leaderboard-ticker-container {
    width: 100%;
    max-width: 600px;
    background: #000;
    border: 4px solid #333;
    border-radius: 5px;
    margin: 0 auto 50px auto;
    overflow: hidden;
    position: relative;
    box-shadow: 0 0 20px rgba(0, 0, 0, 0.8), inset 0 0 20px rgba(0, 0, 0, 0.5);
    height: auto;
    min-height: 160px;
}

.ticker-header {
    background: #111;
    color: #00ff00;
    font-family: 'Orbitron', sans-serif;
    font-size: 18px;
    padding: 10px;
    text-align: center;
    border-bottom: 2px solid #333;
    letter-spacing: 2px;
    text-shadow: 0 0 10px rgba(0, 255, 0, 0.5);
    font-weight: bold;
}

#leaderboard-ticker {
    width: 100%;
    height: 100%;
    display: flex;
    flex-direction: column;
    padding: 10px;
}

.ticker-item {
    font-family: 'Courier New', monospace;
    font-size: 16px;
    color: #fff;
    padding: 5px 10px;
    border-bottom: 1px dashed #333;
    display: flex;
    justify-content: space-between;
    height: 33px;
    /* 3등분 */
    align-items: center;
    backface-visibility: hidden;
    transform-style: preserve-3d;
}

.ticker-item:last-child {
    border-bottom: none;
}

.ticker-track {
    color: #00ff00;
    font-weight: bold;
    width: 40%;
    text-align: left;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

.ticker-player {
    color: #fff;
    width: 35%;
    text-align: left;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

.ticker-time {
    color: #ffff00;
    width: 25%;
    text-align: right;
    font-weight: bold;
}

/* Flip Animation */
.flip-in {
    animation: flipIn 0.6s cubic-bezier(0.23, 1, 0.32, 1) forwards;
}

.flip-out {
    animation: flipOut 0.6s cubic-bezier(0.23, 1, 0.32, 1) forwards;
}

@keyframes flipIn {
    0% {
        opacity: 0;
        transform: rotateX(-90deg);
    }

    100% {
        opacity: 1;
        transform: rotateX(0deg);
    }
}

@keyframes flipOut {
    0% {
        opacity: 1;
        transform: rotateX(0deg);
    }

    100% {
        opacity: 0;
        transform: rotateX(90deg);
    }
}