/* Design-System und Variablen */
:root {
    --bg-dark: #0f172a;
    --primary: #f97316; /* Marillengelb / Orange */
    --primary-hover: #ea580c;
    --secondary: #10b981; /* Naturgrün */
    --secondary-hover: #059669;
    --text-light: #f8fafc;
    --text-muted: #94a3b8;
    --panel-bg: rgba(15, 23, 42, 0.65);
    --panel-border: rgba(249, 115, 22, 0.2);
    --glass-shadow: 0 8px 32px 0 rgba(0, 0, 0, 0.5);
    --font-main: 'Outfit', -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
}

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

html, body {
    width: 100%;
    height: 100%;
    overflow: hidden;
}

body {
    background-color: var(--bg-dark);
    color: var(--text-light);
    font-family: var(--font-main);
    display: flex;
    justify-content: center;
    align-items: center;
    min-height: 100vh;
    background-image: 
        radial-gradient(at 0% 0%, rgba(249, 115, 22, 0.15) 0px, transparent 50%),
        radial-gradient(at 100% 100%, rgba(16, 185, 129, 0.1) 0px, transparent 50%);
}

#game-container {
    position: relative;
    width: 1024px;
    height: 768px;
    background-color: #14532d; /* Dunkles waldgrün als Fallback */
    border-radius: 20px;
    box-shadow: 0 20px 50px rgba(0, 0, 0, 0.8), 0 0 0 1px rgba(255, 255, 255, 0.05);
    overflow: hidden;
}

canvas {
    display: block;
    width: 100%;
    height: 100%;
    image-rendering: pixelated;
}

/* Overlays (Startmenü, Game Over) */
.overlay {
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    display: flex;
    justify-content: center;
    align-items: center;
    background-color: rgba(15, 23, 42, 0.6);
    backdrop-filter: blur(8px);
    transition: opacity 0.4s ease, visibility 0.4s;
    z-index: 10;
}

.overlay.hidden {
    opacity: 0;
    visibility: hidden;
    pointer-events: none;
}

.glass-panel {
    background: var(--panel-bg);
    border: 1px solid var(--panel-border);
    backdrop-filter: blur(16px);
    -webkit-backdrop-filter: blur(16px);
    border-radius: 24px;
    padding: 40px;
    width: 90%;
    max-width: 640px;
    box-shadow: var(--glass-shadow);
    animation: scaleUp 0.3s cubic-bezier(0.34, 1.56, 0.64, 1);
}

@keyframes scaleUp {
    from {
        transform: scale(0.9);
        opacity: 0;
    }
    to {
        transform: scale(1);
        opacity: 1;
    }
}

.text-center {
    text-align: center;
}

.logo-container {
    margin-bottom: 24px;
}

.logo-sub {
    font-size: 0.8rem;
    font-weight: 800;
    letter-spacing: 0.3em;
    color: var(--secondary);
    text-transform: uppercase;
}

.logo-title {
    font-size: 3.5rem;
    font-weight: 800;
    background: linear-gradient(135deg, #ffedd5 0%, #f97316 50%, #ea580c 100%);
    -webkit-background-clip: text;
    -webkit-text-fill-color: transparent;
    text-shadow: 0 4px 10px rgba(0, 0, 0, 0.2);
    margin-top: 4px;
}

.game-description {
    font-size: 1rem;
    line-height: 1.6;
    color: var(--text-muted);
    margin-bottom: 30px;
}

/* Fahrzeugauswahl */
.selection-section {
    margin-bottom: 30px;
}

.selection-section h3 {
    font-size: 1.1rem;
    color: var(--text-light);
    margin-bottom: 12px;
    text-align: left;
    font-weight: 600;
}

.vehicle-selector {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 16px;
}

.vehicle-card {
    background: rgba(30, 41, 59, 0.5);
    border: 1px solid rgba(255, 255, 255, 0.08);
    border-radius: 16px;
    padding: 16px;
    display: flex;
    align-items: center;
    gap: 16px;
    cursor: pointer;
    text-align: left;
    transition: all 0.25s cubic-bezier(0.4, 0, 0.2, 1);
    color: var(--text-light);
    outline: none;
}

.vehicle-card:hover {
    background: rgba(30, 41, 59, 0.8);
    transform: translateY(-2px);
    border-color: rgba(249, 115, 22, 0.3);
}

.vehicle-card.active {
    background: rgba(249, 115, 22, 0.15);
    border-color: var(--primary);
    box-shadow: 0 0 15px rgba(249, 115, 22, 0.25);
}

.vehicle-icon {
    font-size: 2.2rem;
    background: rgba(15, 23, 42, 0.6);
    border-radius: 12px;
    width: 60px;
    height: 60px;
    display: flex;
    justify-content: center;
    align-items: center;
}

.vehicle-details {
    display: flex;
    flex-direction: column;
    gap: 4px;
}

.vehicle-name {
    font-weight: 600;
    font-size: 1rem;
}

.vehicle-stats {
    font-size: 0.75rem;
    color: var(--text-muted);
}

.controls-info {
    background: rgba(15, 23, 42, 0.4);
    border-radius: 12px;
    padding: 12px;
    font-size: 0.85rem;
    color: var(--text-muted);
    margin-bottom: 30px;
    border: 1px solid rgba(255, 255, 255, 0.03);
}

.controls-info strong {
    color: var(--text-light);
}

/* Buttons */
.btn {
    font-family: var(--font-main);
    font-size: 1.1rem;
    font-weight: 600;
    padding: 14px 32px;
    border-radius: 14px;
    border: none;
    cursor: pointer;
    transition: all 0.2s;
    width: 100%;
}

.btn-primary {
    background: linear-gradient(135deg, var(--primary) 0%, #ea580c 100%);
    color: var(--text-light);
    box-shadow: 0 4px 15px rgba(249, 115, 22, 0.4);
}

.btn-primary:hover {
    background: linear-gradient(135deg, var(--primary-hover) 0%, #d97706 100%);
    box-shadow: 0 6px 20px rgba(249, 115, 22, 0.5);
    transform: translateY(-1px);
}

.btn-secondary {
    background: rgba(51, 65, 85, 0.6);
    color: var(--text-light);
    border: 1px solid rgba(255, 255, 255, 0.1);
}

.btn-secondary:hover {
    background: rgba(51, 65, 85, 0.9);
    border-color: rgba(255, 255, 255, 0.2);
}

/* HUD System */
.hud-container {
    position: absolute;
    top: 24px;
    left: 24px;
    right: 24px;
    display: flex;
    justify-content: space-between;
    pointer-events: none;
    z-index: 5;
    animation: slideDown 0.4s cubic-bezier(0.16, 1, 0.3, 1);
}

@keyframes slideDown {
    from {
        transform: translateY(-20px);
        opacity: 0;
    }
    to {
        transform: translateY(0);
        opacity: 1;
    }
}

.glass-hud {
    background: rgba(15, 23, 42, 0.7);
    border: 1px solid rgba(255, 255, 255, 0.08);
    backdrop-filter: blur(10px);
    -webkit-backdrop-filter: blur(10px);
    border-radius: 16px;
    padding: 12px 20px;
    display: flex;
    gap: 24px;
    box-shadow: 0 10px 25px rgba(0, 0, 0, 0.3);
}

.center-hud {
    justify-content: center;
    min-width: 140px;
}

.hud-panel {
    display: flex;
}

.hud-item {
    display: flex;
    flex-direction: column;
    gap: 2px;
}

.text-right {
    text-align: right;
}

.hud-label {
    font-size: 0.65rem;
    font-weight: 800;
    letter-spacing: 0.15em;
    color: var(--primary);
}

.hud-value {
    font-size: 1.3rem;
    font-weight: 700;
    font-variant-numeric: tabular-nums;
    text-shadow: 0 2px 4px rgba(0, 0, 0, 0.5);
}

/* Game Over / Results */
.results-title {
    font-size: 2.2rem;
    font-weight: 800;
    color: var(--secondary);
    margin-bottom: 8px;
}

.results-vehicle {
    font-size: 1rem;
    color: var(--text-muted);
    margin-bottom: 24px;
    background: rgba(15, 23, 42, 0.4);
    display: inline-block;
    padding: 6px 16px;
    border-radius: 20px;
    border: 1px solid rgba(255, 255, 255, 0.05);
}

.results-stats {
    display: flex;
    flex-direction: column;
    gap: 12px;
    background: rgba(15, 23, 42, 0.4);
    border-radius: 16px;
    padding: 20px;
    margin-bottom: 24px;
    border: 1px solid rgba(255, 255, 255, 0.03);
}

.results-row {
    display: flex;
    justify-content: space-between;
    font-size: 1rem;
}

.results-label {
    color: var(--text-muted);
}

.results-val {
    font-weight: 600;
}

.total-score {
    border-top: 1px solid rgba(255, 255, 255, 0.1);
    padding-top: 12px;
    margin-top: 4px;
    font-size: 1.2rem;
}

.total-score .results-label {
    color: var(--text-light);
    font-weight: 700;
}

.total-score .results-val {
    color: var(--primary);
    font-weight: 800;
}

.results-badge {
    font-weight: 600;
    font-size: 1.05rem;
    color: var(--text-light);
    background: linear-gradient(135deg, rgba(16, 185, 129, 0.2) 0%, rgba(16, 185, 129, 0.05) 100%);
    border: 1px solid rgba(16, 185, 129, 0.3);
    border-radius: 12px;
    padding: 12px;
    margin-bottom: 30px;
    display: flex;
    justify-content: center;
    align-items: center;
    gap: 8px;
}

.action-buttons {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 16px;
}

/* Cockpit / Fahrerperspektive (PIP) */
#cockpit-container {
    position: absolute;
    bottom: 24px;
    right: 24px;
    background: rgba(15, 23, 42, 0.85);
    border: 1px solid rgba(249, 115, 22, 0.25);
    backdrop-filter: blur(12px);
    -webkit-backdrop-filter: blur(12px);
    border-radius: 20px;
    padding: 10px;
    box-shadow: 0 15px 35px rgba(0, 0, 0, 0.6);
    display: flex;
    flex-direction: column;
    gap: 6px;
    z-index: 5;
    animation: slideUpIn 0.4s cubic-bezier(0.16, 1, 0.3, 1);
    transition: opacity 0.4s ease, visibility 0.4s;
    pointer-events: auto;
}

#cockpit-container.hidden {
    opacity: 0;
    visibility: hidden;
    pointer-events: none;
}

.cockpit-header {
    font-size: 0.7rem;
    font-weight: 800;
    letter-spacing: 0.15em;
    color: var(--primary);
    text-align: center;
    text-transform: uppercase;
}

#cockpitCanvas {
    width: 280px;
    height: 180px;
    background: #000;
    border-radius: 12px;
    border: 1px solid rgba(255, 255, 255, 0.05);
    display: block;
}

@keyframes slideUpIn {
    from {
        transform: translateY(20px);
        opacity: 0;
    }
    to {
        transform: translateY(0);
        opacity: 1;
    }
}

/* Highscore Tabelle */
.highscores-section {
    margin-bottom: 24px;
    background: rgba(15, 23, 42, 0.4);
    border: 1px solid rgba(255, 255, 255, 0.05);
    border-radius: 16px;
    padding: 16px;
}

.highscores-title {
    font-size: 0.8rem;
    font-weight: 800;
    letter-spacing: 0.15em;
    color: var(--primary);
    margin-bottom: 12px;
    text-align: center;
}

.highscores-table {
    width: 100%;
    border-collapse: collapse;
    font-size: 0.85rem;
    color: var(--text-light);
    text-align: left;
}

.highscores-table th {
    font-weight: 600;
    color: var(--text-muted);
    border-bottom: 1px solid rgba(255, 255, 255, 0.08);
    padding: 8px 6px;
    font-size: 0.75rem;
    text-transform: uppercase;
    letter-spacing: 0.05em;
}

.highscores-table td {
    padding: 8px 6px;
    border-bottom: 1px solid rgba(255, 255, 255, 0.03);
    font-weight: 400;
}

.highscores-table tr:last-child td {
    border-bottom: none;
}

/* Tabellenplatz-Farben */
.highscores-table tr:nth-child(1) td:first-child {
    color: #ffd700; /* Gold */
    font-weight: 800;
}
.highscores-table tr:nth-child(2) td:first-child {
    color: #c0c0c0; /* Silber */
    font-weight: 800;
}
.highscores-table tr:nth-child(3) td:first-child {
    color: #cd7f32; /* Bronze */
    font-weight: 800;
}

/* Highlight-Zeile für das neue Ergebnis */
.highscores-table tr.highlight-row {
    background: rgba(249, 115, 22, 0.15);
    border-radius: 8px;
    animation: pulseHighlight 1.5s infinite alternate;
}

.highscores-table tr.highlight-row td {
    color: var(--primary);
    font-weight: 600;
    border-bottom: 1px solid rgba(249, 115, 22, 0.2);
}

@keyframes pulseHighlight {
    from {
        background: rgba(249, 115, 22, 0.1);
    }
    to {
        background: rgba(249, 115, 22, 0.25);
    }
}

/* ==========================================
   📱 Touch-Steuerung Styles & Virtueller Joystick
   ========================================== */

#touch-controls-container {
    position: absolute;
    bottom: 24px;
    left: 24px;
    right: 24px;
    height: 120px;
    display: flex;
    justify-content: space-between;
    align-items: flex-end;
    pointer-events: none;
    z-index: 8;
}

#touch-controls-container.hidden {
    display: none !important;
}

/* Virtueller Joystick */
#joystick-base {
    width: 110px;
    height: 110px;
    background: rgba(15, 23, 42, 0.55);
    border: 2px solid rgba(255, 255, 255, 0.15);
    backdrop-filter: blur(12px);
    -webkit-backdrop-filter: blur(12px);
    border-radius: 50%;
    position: relative;
    pointer-events: auto;
    box-shadow: 0 8px 24px rgba(0, 0, 0, 0.4);
    touch-action: none;
}

#joystick-knob {
    width: 44px;
    height: 44px;
    background: linear-gradient(135deg, var(--primary) 0%, #ea580c 100%);
    border: 1px solid rgba(255, 255, 255, 0.25);
    border-radius: 50%;
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
    box-shadow: 0 4px 10px rgba(0, 0, 0, 0.3);
    transition: transform 0.05s ease;
}

/* UI stays in CSS pixels; only the course canvas is scaled. */
#game-container { width: 100%; height: 100%; border-radius: 0; background: #0f231c; }
#gameCanvas { position: absolute; left: 50%; transform: translate(-50%, -50%); }
.hidden { display: none !important; }
.overlay { padding: 16px; overflow-y: auto; align-items: safe center; }
.glass-panel { max-height: 100%; overflow-y: auto; padding: 24px; width: min(100%, 640px); }
.btn, .vehicle-card, select { min-height: 48px; }
button:focus-visible, select:focus-visible { outline: 3px solid #fbbf24; outline-offset: 3px; }
.mobile-controls-info { display: none; }
/* Unified Top HUD & Toolbar Bar (Docked cleanly above the course canvas) */
#race-topbar {
    position: absolute;
    top: 0;
    left: 0;
    right: 0;
    min-height: 54px;
    padding: 6px 12px;
    background: rgba(15, 23, 42, 0.94);
    border-bottom: 1px solid rgba(255, 255, 255, 0.14);
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 10px;
    z-index: 12;
    box-sizing: border-box;
}

#race-topbar .hud-container {
    position: static !important;
    top: auto !important;
    left: auto !important;
    right: auto !important;
    display: flex;
    align-items: center;
    justify-content: flex-start;
    flex-wrap: wrap;
    gap: 8px;
    flex: 1;
    min-width: 0;
    pointer-events: none;
    animation: none;
}

#race-topbar .glass-hud {
    padding: 4px 10px;
    gap: 12px;
    border-radius: 10px;
    background: rgba(255, 255, 255, 0.06);
    border: 1px solid rgba(255, 255, 255, 0.1);
    backdrop-filter: none;
    box-shadow: none;
    display: flex;
    align-items: center;
}

#race-topbar .center-hud {
    min-width: 0;
}

#race-topbar .hud-item {
    display: flex;
    flex-direction: column;
    line-height: 1.1;
}

#race-topbar .hud-label {
    font-size: 9px;
    letter-spacing: 0.08em;
    color: rgba(226, 232, 240, 0.68);
}

#race-topbar .hud-value {
    font-size: 14px;
    font-weight: 800;
    color: #ffffff;
}

#race-topbar .hud-checkpoints {
    font-size: 11px;
    color: #94a3b8;
    letter-spacing: 0.06em;
    white-space: nowrap;
}

#race-topbar #race-toolbar {
    position: static !important;
    top: auto !important;
    right: auto !important;
    display: flex;
    align-items: center;
    gap: 6px;
    flex-shrink: 0;
}

#race-topbar #race-toolbar button {
    min-height: 36px;
    padding: 6px 10px;
    background: #1e293b;
    border: 1px solid rgba(255, 255, 255, 0.22);
    border-radius: 9px;
    color: white;
    font: 700 12px var(--font-main);
    cursor: pointer;
    white-space: nowrap;
}

#race-topbar #race-toolbar button:hover {
    background: #334155;
}

#race-topbar #race-toolbar select {
    min-height: 36px;
    max-width: 82px;
    font: 700 12px var(--font-main);
    background: #1e293b;
    color: white;
    border: 1px solid rgba(255, 255, 255, 0.22);
    border-radius: 9px;
    padding: 4px 6px;
}

#cockpit-container {
    transition: top 0.28s cubic-bezier(0.22, 1, 0.36, 1), left 0.28s cubic-bezier(0.22, 1, 0.36, 1);
}

@media (max-width: 1023px), (pointer: coarse) {
    #race-topbar {
        padding: 4px 8px;
        gap: 6px;
        flex-wrap: wrap;
    }
    #race-topbar .glass-hud {
        padding: 3px 7px;
        gap: 8px;
    }
    #race-topbar .hud-value {
        font-size: 12px;
    }
    #race-topbar #race-toolbar button,
    #race-topbar #race-toolbar select {
        min-height: 34px;
        padding: 4px 8px;
        font-size: 11px;
    }
}
#cockpit-container { box-sizing: border-box; padding: 5px; gap: 4px; border-radius: 10px; max-width: none !important; pointer-events: none; animation: none; }
#cockpit-container #cockpitCanvas { width: 100%; height: auto; aspect-ratio: 280 / 180; }
.hud-container { top: 58px; gap: 8px; animation: none; }
#pause-overlay { z-index: 20; }
#pause-overlay .btn + .btn { margin-top: 12px; }
.touch-pedals { display: flex; align-items: flex-end; gap: 8px; pointer-events: auto; }
.touch-pedals button { touch-action: none; user-select: none; -webkit-user-select: none; width: 66px; height: 66px; border: 2px solid #9bb6ac; border-radius: 18px; background: #233d33; color: white; font: 700 14px var(--font-main); }
#pedal-gas { background: #166534; height: 86px; }
#pedal-brake { background: #9a3412; }
.touch-pedals button.pressed { background: #f97316; transform: translateY(2px); }
#joystick-base { width: 120px; height: 120px; flex-shrink: 0; user-select: none; -webkit-user-select: none; }
#joystick-knob { width: 52px; height: 52px; transition: none; }
#touch-controls-container { display: none; }

@media (max-width: 1023px), (pointer: coarse) {
    .glass-panel { padding: 18px; border-radius: 18px; }
    .logo-container { margin-bottom: 10px; }
    .logo-title { font-size: 30px; }
    .game-description { font-size: 14px; line-height: 1.5; margin-bottom: 16px; }
    .vehicle-selector { gap: 8px; }
    .vehicle-card { flex-direction: column; align-items: flex-start; gap: 8px; padding: 12px; }
    .vehicle-icon { width: 42px; height: 42px; font-size: 28px; }
    .vehicle-name { font-size: 14px; }
    .vehicle-stats { font-size: 12px; }
    .selection-section { margin-bottom: 14px; }
    .controls-info { display: none; }
    .mobile-controls-info { display: grid; gap: 8px; font-size: 14px; margin-bottom: 16px; }
    .mobile-controls-info select { width: 100%; background: #172d25; color: white; border-radius: 8px; border: 1px solid #6b867a; padding: 8px; font: inherit; }
    .hud-container { top: 54px; left: 8px; right: 8px; gap: 4px; }
    .glass-hud { padding: 6px 8px; gap: 8px; border-radius: 8px; backdrop-filter: none; }
    .hud-label { font-size: 9px; letter-spacing: 0; }
    .hud-value { font-size: 13px; }
    .center-hud { min-width: 0; }
    .hud-panel:first-child .hud-item:nth-child(2), #hud-checkpoints { display: none !important; }
    #touch-controls-container { display: flex; bottom: max(12px, env(safe-area-inset-bottom)); left: max(10px, env(safe-area-inset-left)); right: max(10px, env(safe-area-inset-right)); }
    #cockpit-container { max-width: 140px; padding: 4px; pointer-events: none; }
    #cockpitCanvas { width: 128px; height: 82px; }
    .cockpit-header { font-size: 9px; letter-spacing: 0; }
    .results-title { font-size: 26px; }
    .results-stats { padding: 12px; }
    .results-row { font-size: 14px; }
    .action-buttons { gap: 8px; }
    .action-buttons .btn { padding: 12px 8px; font-size: 14px; }
}
@media (max-width: 370px) {
    .touch-pedals { gap: 4px; }
    .touch-pedals button { width: 54px; font-size: 12px; }
    #joystick-base { width: 110px; height: 110px; }
}
@media (orientation: landscape) and (max-height: 500px) {
    .overlay { align-items: flex-start; }
    .vehicle-card { flex-direction: row; }
    .game-description { display: none; }
    .logo-title { font-size: 24px; }
    #touch-controls-container { bottom: 8px; }
    .center-hud { transform: translateY(-48px); }
}
@media (prefers-reduced-motion: reduce) {
    *, *::before, *::after { animation: none !important; transition: none !important; }
}
