/* Global Box Sizing for correct padding handling */
* {
    box-sizing: border-box;
}

:root {
    --bg-color: #0f172a;
    /* Dark background */
    --text-color: #f8fafc;
    --accent-color: #ef4444;
    /* Racing Red */
    --danger-color: #ef4444;
}

body {
    margin: 0;
    padding: 0;
    background-color: var(--bg-color);
    color: var(--text-color);
    font-family: 'Segoe UI', system-ui, sans-serif;
    display: flex;
    flex-direction: column;
    align-items: center;
    height: 100vh;
    height: 100dvh;
    /* Dynamic viewport for mobile (avoids address bar) */
    min-height: -webkit-fill-available;
    overflow: hidden;
    user-select: none;
    -webkit-user-select: none;
    /* iOS Safari */
}

header {
    width: 100%;
    padding: 0.8rem 1.5rem;
    display: flex;
    justify-content: space-between;
    align-items: center;
    background: #020617;
    border-bottom: 2px solid var(--accent-color);
    z-index: 1000;
    position: relative;
    /* For absolute menu positioning */
}

h1 {
    margin: 0;
    font-size: 1.5rem;
    font-weight: 800;
    letter-spacing: -0.02em;
    color: #fff;
    text-transform: uppercase;
    font-style: italic;
}

h1 .full-name,
h1 .short-name {
    color: var(--accent-color);
}

/* Container for right-side header items */
.header-right {
    display: flex;
    align-items: center;
    gap: 1.5rem;
}

.header-controls {
    display: flex;
    align-items: center;
    gap: 1.5rem;
}

.settings-group {
    display: flex;
    gap: 1rem;
    align-items: center;
}

.settings-group label {
    font-size: 0.8rem;
    color: #94a3b8;
    display: flex;
    align-items: center;
    gap: 0.5rem;
    cursor: pointer;
}

.settings-group input[type="checkbox"] {
    accent-color: var(--accent-color);
    width: 1rem;
    height: 1rem;
    cursor: pointer;
}

select {
    background-color: #1e293b;
    color: var(--text-color);
    border: 1px solid #334155;
    padding: 0.5rem;
    border-radius: 0.25rem;
    font-family: inherit;
    cursor: pointer;
    outline: none;
    font-weight: 600;
}

select:hover {
    border-color: var(--accent-color);
}

.stats {
    position: absolute;
    top: 1rem;
    left: 1rem;
    z-index: 10;
    pointer-events: none;
    display: flex;
    gap: 1rem;
    font-size: 0.9rem;
    font-family: 'Courier New', monospace;
    background: rgba(0, 0, 0, 0.7);
    padding: 0.5rem 0.8rem;
    border-radius: 4px;
    border: 1px solid #334155;
    white-space: nowrap;
}

.stat-item span {
    color: var(--accent-color);
    font-weight: bold;
}

/* Menu Toggle Button (Mobile) */
#menu-toggle {
    display: none;
    background: transparent;
    border: 1px solid #334155;
    color: white;
    font-size: 1.2rem;
    padding: 0.4rem 0.8rem;
    border-radius: 4px;
    cursor: pointer;
}

#game-container {
    position: relative;
    flex-grow: 1;
    flex-shrink: 1;
    min-height: 0;
    /* Allow flex child to shrink below content size */
    width: 100%;
    display: flex;
    justify-content: center;
    align-items: center;
    overflow: hidden;
    background-color: #0f172a;
    touch-action: none;
    /* Prevent browser handling gestures */
}

canvas {
    box-shadow: 0 0 50px rgba(0, 0, 0, 0.5);
}



button {
    background-color: #334155;
    color: var(--text-color);
    border: none;
    border-bottom: 3px solid #1e293b;
    padding: 0.5rem 1rem;
    border-radius: 0.25rem;
    font-weight: 700;
    cursor: pointer;
    transition: all 0.1s;
    text-transform: uppercase;
    font-size: 0.8rem;
    letter-spacing: 0.05em;
}



button:active {
    transform: translateY(2px);
    border-bottom-width: 0;
}

button.danger {
    background-color: #7f1d1d;
    border-bottom-color: #450a0a;
}

button.danger:hover {
    background-color: #ef4444;
}

/* Mobile Controls Styling */
.mobile-controls {
    display: none;
    /* Hidden on desktop */
    position: absolute;
    bottom: 0;
    width: 100%;
    justify-content: space-between;
    padding: 0 5% max(20px, env(safe-area-inset-bottom));
    box-sizing: border-box;
    pointer-events: none;
    /* Click-through empty space */
    z-index: 30;
}

.control-group {
    display: flex;
    gap: 15px;
    align-items: flex-end;
    pointer-events: auto;
    width: 100%;
    justify-content: space-between;
}

.touch-btn {
    width: 120px;
    height: 120px;
    background: rgba(255, 255, 255, 0.1);
    border: 2px solid rgba(255, 255, 255, 0.2);
    border-radius: 50%;
    color: white;
    font-size: 48px;
    display: flex;
    justify-content: center;
    align-items: center;
    user-select: none;
    touch-action: manipulation;
    cursor: pointer;
    backdrop-filter: blur(4px);
    transition: transform 0.1s, background 0.1s;
}

.touch-btn:active,
.touch-btn.active {
    background: rgba(239, 68, 68, 0.6);
    transform: scale(0.95);
    border-color: var(--accent-color);
}

/* Start Screen & Lights */
#start-overlay {
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background: rgba(0, 0, 0, 0.6);
    display: flex;
    flex-direction: column;
    justify-content: center;
    align-items: center;
    z-index: 50;
    backdrop-filter: blur(5px);
}

#start-btn {
    font-size: 1.5rem;
    padding: 1.5rem 3rem;
    background: var(--accent-color);
    color: white;
    border: none;
    border-radius: 8px;
    cursor: pointer;
    text-transform: uppercase;
    font-weight: 800;
    letter-spacing: 2px;
    box-shadow: 0 0 20px rgba(239, 68, 68, 0.5);
    animation: pulse 2s infinite;
    max-width: 90vw;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

@keyframes pulse {
    0% {
        transform: scale(1);
        box-shadow: 0 0 20px rgba(239, 68, 68, 0.5);
    }

    50% {
        transform: scale(1.05);
        box-shadow: 0 0 40px rgba(239, 68, 68, 0.8);
    }

    100% {
        transform: scale(1);
        box-shadow: 0 0 20px rgba(239, 68, 68, 0.5);
    }
}

#start-lights {
    position: absolute;
    top: 20%;
    left: 50%;
    transform: translateX(-50%);
    display: flex;
    gap: 15px;
    background: #111;
    padding: 15px 25px;
    border-radius: 10px;
    border: 2px solid #333;
    z-index: 40;
    opacity: 0;
    pointer-events: none;
    transition: opacity 0.3s;
}

#start-lights.visible {
    opacity: 1;
}

.light {
    width: 40px;
    height: 40px;
    background: #333;
    border-radius: 50%;
    border: 2px solid #000;
    box-shadow: inset 0 0 10px #000;
    transition: background 0.1s, box-shadow 0.1s;
}

.light.on {
    background: #ff0000;
    box-shadow: 0 0 20px #ff0000;
}

/* Green Light Style */
.light.green {
    background: #22c55e;
    box-shadow: 0 0 20px #22c55e;
}

/* GO Message Styling - Positioned higher */
#go-message {
    position: absolute;
    top: 20%;
    left: 50%;
    transform: translate(-50%, -50%) scale(0.5);
    font-size: 8rem;
    font-weight: 900;
    color: #22c55e;
    text-shadow: 0 0 20px #fff;
    pointer-events: none;
    z-index: 60;
    opacity: 0;
    transition: all 0.2s cubic-bezier(0.175, 0.885, 0.32, 1.275);
}

#go-message.visible {
    opacity: 1;
    transform: translate(-50%, -50%) scale(1.2);
}

.modal {
    position: fixed;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    background: rgba(0, 0, 0, 0.9);
    display: flex;
    flex-direction: column;
    justify-content: center;
    align-items: center;
    padding: env(safe-area-inset-top) env(safe-area-inset-right) env(safe-area-inset-bottom) env(safe-area-inset-left);
    overflow-y: auto;
    z-index: 100;
    opacity: 0;
    pointer-events: none;
    transition: opacity 0.3s ease;
}

.modal.active {
    opacity: 1;
    pointer-events: all;
}

.modal-card {
    display: flex;
    flex-direction: column;
    align-items: center;
    width: 100%;
    max-width: 440px;
    background: #0f172a;
    border: 2px solid #334155;
    border-radius: 8px;
    padding: 2rem;
    margin: auto;
    transform: scale(0.95) translateY(20px);
    transition: transform 0.3s ease;
}

.modal.active .modal-card {
    transform: scale(1) translateY(0);
}

.modal h2 {
    font-size: 3.5rem;
    margin: 0 0 0.5rem;
    color: #fff;
    text-transform: uppercase;
    font-style: italic;
    font-weight: 900;
    text-align: center;
    letter-spacing: 2px;
}

.modal p {
    font-size: 1.2rem;
    margin: 0 0 2rem;
    font-family: 'Courier New', monospace;
    color: var(--accent-color);
    text-align: center;
    font-weight: bold;
    letter-spacing: 1px;
    text-transform: uppercase;
}

.share-panel {
    width: 100%;
    margin-bottom: 0.5rem;
    display: none;
    flex-direction: column;
    align-items: stretch;
    gap: 0.8rem;
}

#share-btn {
    width: 100%;
    padding: 1rem;
    font-size: 0.95rem;
    background: #334155;
    border: none;
    border-bottom: 3px solid #1e293b;
    color: #f8fafc;
    border-radius: 4px;
    text-transform: uppercase;
    font-weight: bold;
    transition: all 0.1s;
}

#share-btn:hover {
    background: #475569;
    border-bottom-color: #334155;
    transform: translateY(-1px);
}

#share-btn:active {
    transform: translateY(2px);
    border-bottom-width: 0;
}

.modal-lap-times-container {
    width: 100%;
    margin-bottom: 1.5rem;
}

.lap-times-list {
    max-height: 220px;
    overflow-y: auto;
    padding: 0.5rem;
    background: rgba(15, 23, 42, 0.5);
    border-radius: 4px;
    border: 1px solid rgba(255, 255, 255, 0.1);
}

.lap-time-item {
    padding: 0.5rem;
    margin-bottom: 0.25rem;
    color: #94a3b8;
    font-family: 'Courier New', monospace;
    font-size: 0.9rem;
    border-radius: 2px;
    display: flex;
    justify-content: space-between;
    align-items: center;
}

.lap-time-item.current {
    color: #fff;
    background: rgba(239, 68, 68, 0.2);
    font-weight: bold;
}

.lap-time-item.best {
    color: #22c55e;
}

.best-badge {
    font-size: 0.7rem;
    background: #22c55e;
    color: #000;
    padding: 0.15rem 0.4rem;
    border-radius: 3px;
    font-weight: bold;
    margin-left: 0.5rem;
}

.modal-view {
    display: none;
    flex-direction: column;
    align-items: center;
    width: 100%;
}

.modal-view.active-view {
    display: flex;
    animation: fadeIn 0.3s ease;
}

@keyframes fadeIn {
    from {
        opacity: 0;
        transform: translateY(10px);
    }

    to {
        opacity: 1;
        transform: translateY(0);
    }
}

.text-link-btn {
    background: transparent;
    border: none;
    color: #94a3b8;
    font-size: 0.9rem;
    cursor: pointer;
    margin-top: 1rem;
    text-decoration: underline;
    text-underline-offset: 4px;
    font-family: inherit;
    padding: 0.5rem;
}

.text-link-btn:hover {
    color: #fff;
}

.modal-subtitle {
    color: #fff;
    font-size: 1.5rem;
    margin-bottom: 1.5rem;
    margin-top: 0;
    text-transform: uppercase;
    font-style: italic;
}

.modal-best-time {
    color: #fff;
    font-size: 1.1rem;
    font-family: 'Courier New', monospace;
    padding: 0.75rem 1.5rem;
    margin-bottom: 1.5rem;
    display: flex;
    justify-content: center;
}

.modal-best-time strong {
    color: var(--accent-color);
    font-weight: bold;
    margin-right: 0.5rem;
}

#modal-reset-btn {
    background: var(--accent-color);
    border: none;
    border-bottom: 3px solid #991b1b;
    padding: 1.1rem 2rem;
    font-size: 1.1rem;
    border-radius: 4px;
    text-transform: uppercase;
    font-weight: 800;
    letter-spacing: 1.5px;
    color: white;
    width: 100%;
    margin-top: 0.5rem;
    cursor: pointer;
}

#modal-reset-btn:hover {
    transform: translateY(-1px);
    background: #f87171;
    border-bottom-color: #7f1d1d;
}

#modal-reset-btn:active {
    transform: translateY(2px);
    border-bottom-width: 0;
}

.modal-btn-kbd {
    display: inline-block;
    padding: 0.15rem 0.4rem;
    font-size: 0.75em;
    background: rgba(255, 255, 255, 0.15);
    border-radius: 3px;
    border: 1px solid rgba(255, 255, 255, 0.35);
    margin-left: 0.4rem;
    font-family: inherit;
    vertical-align: middle;
}

.key-hint {
    position: absolute;
    bottom: 1rem;
    right: 1rem;
    color: rgba(255, 255, 255, 0.4);
    font-size: 0.8rem;
    text-align: right;
    font-family: monospace;
}

@media (max-width: 768px),
(hover: none) and (pointer: coarse),
(max-height: 600px) and (orientation: landscape) {
    .full-name {
        display: none;
    }

    .short-name {
        display: inline !important;
    }

    /* Slightly smaller CTA text on narrower screens so the copy wraps cleanly */
    #start-btn {
        font-size: 1.1rem;
        padding: 1.1rem 2rem;
        letter-spacing: 1px;
    }

    .header-right {
        gap: 0.5rem;
    }

    /* Track selector always visible on mobile */
    .header-controls {
        display: flex;
        position: static;
        width: auto;
        background: transparent;
        backdrop-filter: none;
        flex-direction: row;
        padding: 0;
        gap: 0.5rem;
        border-bottom: none;
        box-shadow: none;
        align-items: center;
    }

    .header-controls.visible {
        display: flex;
    }

    .settings-group {
        display: none; /* Already removed trace route, but keeping this just in case */
    }

    #track-select {
        width: auto;
        padding: 0.4rem;
        font-size: 0.85rem;
    }

    /* Hide Menu Toggle */
    #menu-toggle {
        display: none !important;
    }

    .mobile-controls {
        display: flex;
    }

    .key-hint {
        display: none;
    }


    .light {
        width: 30px;
        height: 30px;
    }

    /* Modal: streamlined on mobile portrait */
    .modal-btn-kbd {
        display: none;
    }

    .modal-card {
        padding: 1.5rem;
        max-width: min(92vw, 400px);
    }

    #modal-main-view {
        padding-bottom: 0;
    }

    .modal h2 {
        font-size: 2.5rem;
        margin-bottom: 0.5rem;
    }

    .modal p {
        font-size: 1.1rem;
        margin-bottom: 1.5rem;
    }

    .modal-best-time {
        margin-bottom: 1rem;
    }

    .share-panel {
        margin-bottom: 1rem;
    }

    .lap-times-list {
        max-height: 200px;
        padding: 0.5rem;
    }

    .lap-time-item {
        padding: 0.4rem 0.5rem;
        font-size: 0.9rem;
    }

    .modal #modal-reset-btn {
        padding: 1rem 1.5rem;
        font-size: 1.1rem;
        margin-top: 1rem;
    }
}

/* Landscape mobile: shorter viewport – smaller buttons, ensure visibility */
@media (orientation: landscape) and (max-height: 500px) {
    .touch-btn {
        width: 80px;
        height: 80px;
        font-size: 32px;
    }

    .mobile-controls {
        padding: 0 3% max(12px, env(safe-area-inset-bottom));
    }
}

/* Modal in landscape: compact layout so content fits */
@media (orientation: landscape) and (max-height: 500px) {
    .modal {
        justify-content: flex-start;
        padding-top: 0.5rem;
        padding-bottom: max(0.5rem, env(safe-area-inset-bottom));
    }

    .modal-card {
        padding: 0.75rem 1rem;
        margin: 0 auto;
    }

    .modal h2 {
        font-size: 1.75rem;
        margin-bottom: 0.15rem;
    }

    .modal p {
        font-size: 0.95rem;
        margin-bottom: 0.5rem;
    }

    .modal #modal-reset-btn {
        padding: 0.5rem 1.25rem;
        font-size: 0.85rem;
    }

    .modal-best-time {
        margin-bottom: 0.5rem;
        padding: 0.4rem 1rem;
    }

    #share-btn {
        padding: 0.7rem 1rem;
        font-size: 0.85rem;
    }

    .lap-times-list {
        max-height: 100px;
        padding: 0.3rem;
    }

    .lap-time-item {
        padding: 0.25rem 0.4rem;
        font-size: 0.8rem;
    }
}