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

body {
    font-family: 'Inter', -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
    background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
    color: #333;
    padding: 20px;
    min-height: 100vh;
    -webkit-font-smoothing: antialiased;
    -moz-osx-font-smoothing: grayscale;
}

.container {
    max-width: 1400px;
    margin: 0 auto;
}

header {
    text-align: center;
    color: white;
    margin-bottom: 30px;
}

header h1 {
    font-size: 2.2em;
    font-weight: 800;
    letter-spacing: -0.03em;
    margin-bottom: 8px;
    text-shadow: 2px 2px 4px rgba(0, 0, 0, 0.3);
}

header p {
    font-size: 1.05em;
    font-weight: 400;
    opacity: 0.85;
    letter-spacing: -0.01em;
}

.main-content {
    display: grid;
    grid-template-columns: 1fr 350px;
    gap: 20px;
    margin-bottom: 20px;
}

.canvas-container {
    position: relative;
    background: white;
    border-radius: 15px;
    padding: 20px;
    box-shadow: 0 10px 40px rgba(0, 0, 0, 0.3);
}

#carCanvas {
    display: block;
    border: 2px solid #e0e0e0;
    border-radius: 10px;
    background: #f8f9fa;
}

.scenario-selector {
    margin-bottom: 15px;
    padding: 15px;
    background: #f8f9fa;
    border-radius: 10px;
    border: 2px solid #e0e0e0;
}

.scenario-selector h3 {
    margin: 0 0 12px 0;
    color: #667eea;
    font-size: 1em;
    font-weight: 600;
    letter-spacing: -0.01em;
}

.scenario-buttons {
    display: grid;
    grid-template-columns: repeat(2, 1fr);
    gap: 10px;
}

.scenario-btn {
    padding: 12px 16px;
    border: 2px solid #667eea;
    background: white;
    color: #667eea;
    border-radius: 8px;
    cursor: pointer;
    font-weight: 600;
    font-size: 0.85em;
    letter-spacing: -0.01em;
    transition: all 0.3s ease;
}

.scenario-btn:hover {
    background: #f0f0ff;
    transform: translateY(-2px);
    box-shadow: 0 4px 8px rgba(102, 126, 234, 0.2);
}

.scenario-btn.active {
    background: #667eea;
    color: white;
}

/* ── Canvas wrapper (for overlay positioning) ── */
.canvas-wrapper {
    position: relative;
}

/* ── Controls overlay (top-right of canvas) ── */
.controls-overlay {
    position: absolute;
    top: 8px;
    right: 8px;
    display: flex;
    align-items: flex-start;
    gap: 8px;
    pointer-events: none;
    z-index: 10;
    user-select: none;
}

.overlay-gear {
    display: flex;
    align-items: center;
    justify-content: center;
    width: 44px;
    height: 44px;
    border-radius: 10px;
    background: rgba(39, 174, 96, 0.92);
    color: white;
    font-size: 1.6em;
    font-weight: 800;
    letter-spacing: 1px;
    flex-shrink: 0;
    transition: background 0.15s;
    backdrop-filter: blur(4px);
    box-shadow: 0 2px 8px rgba(0,0,0,0.25);
}

.overlay-gear.reverse {
    background: rgba(230, 126, 34, 0.92);
}

.overlay-keys {
    display: flex;
    flex-direction: column;
    gap: 3px;
}

.overlay-row {
    display: flex;
    gap: 3px;
}

.okey {
    display: flex;
    align-items: center;
    justify-content: center;
    padding: 4px 8px;
    min-width: 32px;
    height: 28px;
    background: rgba(20, 20, 35, 0.75);
    border: 1px solid rgba(100, 100, 130, 0.5);
    border-radius: 5px;
    color: rgba(220, 220, 230, 0.8);
    font-size: 0.72em;
    font-weight: 600;
    font-family: 'SF Mono', 'Menlo', 'Consolas', monospace;
    line-height: 1;
    backdrop-filter: blur(4px);
    transition: all 0.08s ease;
    white-space: nowrap;
}

.okey.wide {
    flex: 1;
}

.okey.small {
    font-size: 0.65em;
    padding: 4px 6px;
    min-width: 28px;
}

.okey.active {
    background: rgba(102, 126, 234, 0.9);
    border-color: rgba(130, 150, 255, 0.8);
    color: #fff;
    box-shadow: 0 0 8px rgba(102, 126, 234, 0.5);
}

/* Scenario status as overlay (bottom-center of canvas) */
.overlay-status {
    position: absolute;
    bottom: 10px;
    left: 50%;
    transform: translateX(-50%);
    z-index: 10;
}

.canvas-controls {
    margin-top: 8px;
    display: flex;
    justify-content: space-between;
    align-items: center;
    gap: 15px;
}

.trail-toggle {
    display: flex;
    gap: 16px;
    flex-wrap: wrap;
}

.trail-toggle label {
    display: inline-flex;
    align-items: center;
    gap: 6px;
    cursor: pointer;
    font-weight: 500;
    font-size: 0.88em;
    letter-spacing: -0.01em;
    color: #555;
}

.trail-toggle input[type="checkbox"] {
    width: 18px;
    height: 18px;
    cursor: pointer;
}

.scenario-status {
    font-weight: 600;
    padding: 8px 16px;
    border-radius: 8px;
    font-size: 0.85em;
    letter-spacing: -0.01em;
}

.scenario-status.success {
    background: #d4edda;
    color: #155724;
    border: 2px solid #c3e6cb;
}

.scenario-status.trying {
    background: #fff3cd;
    color: #856404;
    border: 2px solid #ffeaa7;
}

.scenario-status.collision {
    background: #f8d7da;
    color: #721c24;
    border: 2px solid #f5c6cb;
    animation: shake 0.5s;
}

@keyframes shake {
    0%, 100% { transform: translateX(-50%); }
    25% { transform: translateX(calc(-50% - 5px)); }
    75% { transform: translateX(calc(-50% + 5px)); }
}

.info-panel {
    display: flex;
    flex-direction: column;
    gap: 20px;
}

.telemetry,
.learning-tips {
    background: white;
    border-radius: 15px;
    padding: 20px;
    box-shadow: 0 10px 40px rgba(0, 0, 0, 0.3);
}

.telemetry h3,
.learning-tips h3 {
    margin-bottom: 15px;
    color: #667eea;
    font-size: 1.15em;
    font-weight: 700;
    letter-spacing: -0.02em;
}

.stat {
    display: flex;
    justify-content: space-between;
    padding: 10px 0;
    border-bottom: 1px solid #f0f0f0;
}

.stat:last-child {
    border-bottom: none;
}

.stat .label {
    font-weight: 500;
    color: #888;
    font-size: 0.9em;
    letter-spacing: -0.01em;
}

.stat .value {
    font-weight: 700;
    color: #667eea;
    font-size: 1em;
    font-variant-numeric: tabular-nums;
}

.tip {
    background: #f8f9fa;
    padding: 15px;
    border-radius: 10px;
    border-left: 4px solid #667eea;
    line-height: 1.55;
    font-size: 0.92em;
    letter-spacing: -0.01em;
}

.tip strong {
    color: #667eea;
    font-weight: 700;
}

.educational-info {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(300px, 1fr));
    gap: 20px;
}

.info-card {
    background: white;
    border-radius: 15px;
    padding: 20px;
    box-shadow: 0 10px 40px rgba(0, 0, 0, 0.3);
}

.info-card h4 {
    color: #667eea;
    margin-bottom: 10px;
    font-size: 1.05em;
    font-weight: 700;
    letter-spacing: -0.02em;
}

.info-card p {
    line-height: 1.55;
    color: #666;
    font-size: 0.92em;
    letter-spacing: -0.01em;
}

.info-card strong {
    color: #444;
    font-weight: 600;
}

/* ── Custom scenario buttons (dashed border) ── */
.scenario-btn.custom {
    border-style: dashed;
}

/* ── Editor toolbar ── */
.editor-toolbar {
    margin-bottom: 12px;
    padding: 10px 14px;
    background: #f0f0ff;
    border-radius: 10px;
    border: 2px solid #dde0f5;
}

.editor-bar {
    display: flex;
    align-items: center;
    gap: 8px;
    flex-wrap: wrap;
}

.editor-btn {
    padding: 7px 14px;
    border: 2px solid #667eea;
    background: white;
    color: #667eea;
    border-radius: 6px;
    cursor: pointer;
    font-weight: 600;
    font-size: 0.82em;
    letter-spacing: -0.01em;
    transition: all 0.2s ease;
}

.editor-btn:hover {
    background: #f0f0ff;
}

.editor-btn.accent {
    background: #667eea;
    color: white;
}

.editor-btn.accent:hover {
    background: #5a6fd6;
}

.editor-btn.danger {
    border-color: #e74c3c;
    color: #e74c3c;
}

.editor-btn.danger:hover {
    background: #fdf0ef;
}

.editor-tool-btn {
    padding: 7px 12px;
    border: 2px solid #aab0e0;
    background: white;
    color: #555;
    border-radius: 6px;
    cursor: pointer;
    font-weight: 600;
    font-size: 0.85em;
    transition: all 0.2s ease;
}

.editor-tool-btn:hover {
    border-color: #667eea;
    color: #667eea;
}

.editor-tool-btn.active {
    background: #667eea;
    color: white;
    border-color: #667eea;
}

.editor-separator {
    width: 1px;
    height: 24px;
    background: #ccc;
    margin: 0 4px;
}

.editor-hint {
    margin-top: 6px;
    font-size: 0.78em;
    color: #888;
}

.editor-inspector {
    display: grid;
    grid-template-columns: repeat(6, minmax(74px, 1fr));
    gap: 8px;
    margin-top: 10px;
    padding-top: 10px;
    border-top: 1px solid #dde0f5;
}

.editor-inspector label {
    display: flex;
    flex-direction: column;
    gap: 4px;
    color: #666;
    font-size: 0.72em;
    font-weight: 700;
    letter-spacing: 0;
}

.editor-inspector input,
.editor-inspector textarea,
.editor-inspector select {
    width: 100%;
    border: 1px solid #cfd4ef;
    border-radius: 6px;
    background: white;
    color: #333;
    font: inherit;
    font-size: 1.05em;
    font-weight: 600;
    padding: 7px 8px;
}

.editor-inspector textarea {
    resize: vertical;
    min-height: 36px;
}

.editor-inspector input:disabled,
.editor-inspector select:disabled {
    background: #f5f6fb;
    color: #999;
    cursor: not-allowed;
}

.editor-inspector .editor-wide {
    grid-column: span 3;
}

.editor-selection-title {
    align-self: end;
    padding: 8px 10px;
    border-radius: 6px;
    background: rgba(102, 126, 234, 0.1);
    color: #667eea;
    font-size: 0.78em;
    font-weight: 700;
    min-height: 34px;
}

/* ═══════════════════════════════════════════════════════════════
   TOUCH CONTROLS (mobile game pad)
   ═══════════════════════════════════════════════════════════════ */
.touch-controls {
    display: none;
    flex-direction: column;
    gap: 6px;
    padding: 8px 0 0;
    -webkit-user-select: none;
    user-select: none;
}

.touch-row {
    display: flex;
    gap: 6px;
    justify-content: center;
    align-items: stretch;
}

.touch-steer-row {
    justify-content: stretch;
}

.touch-aux-row {
    justify-content: center;
    align-items: center;
    gap: 8px;
}

.touch-middle {
    display: flex;
    flex: 1;
    gap: 6px;
}

.touch-btn {
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    border: none;
    border-radius: 14px;
    background: rgba(255, 255, 255, 0.7);
    color: #667eea;
    cursor: pointer;
    font-family: inherit;
    font-weight: 700;
    font-size: 1.3em;
    transition: background 0.06s, transform 0.06s, box-shadow 0.06s;
    -webkit-tap-highlight-color: transparent;
    touch-action: manipulation;
    padding: 0;
    line-height: 1;
    box-shadow: 0 2px 6px rgba(0, 0, 0, 0.06);
}

.touch-btn .touch-sub {
    font-size: 0.42em;
    font-weight: 600;
    opacity: 0.5;
    margin-top: 1px;
    letter-spacing: 0.5px;
    text-transform: uppercase;
    color: #888;
}

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

/* ── Steer buttons (left / right) ── */
.touch-left,
.touch-right {
    flex: 0 0 72px;
    height: 72px;
    border-radius: 50%;
    font-size: 1.6em;
    background: rgba(255, 255, 255, 0.8);
    box-shadow: 0 2px 8px rgba(102, 126, 234, 0.12);
}

.touch-left:active, .touch-left.active,
.touch-right:active, .touch-right.active {
    background: #667eea;
    color: #fff;
    box-shadow: 0 0 16px rgba(102, 126, 234, 0.35);
}

/* ── Gas ── */
.touch-gas {
    flex: 1;
    min-height: 72px;
    background: rgba(39, 174, 96, 0.12);
    color: #27ae60;
    box-shadow: 0 2px 8px rgba(39, 174, 96, 0.1);
}

.touch-gas:active, .touch-gas.active {
    background: #27ae60;
    color: #fff;
    box-shadow: 0 0 16px rgba(39, 174, 96, 0.35);
}

/* ── Brake ── */
.touch-brake {
    flex: 1;
    min-height: 72px;
    background: rgba(231, 76, 60, 0.10);
    color: #e74c3c;
    box-shadow: 0 2px 8px rgba(231, 76, 60, 0.08);
}

.touch-brake:active, .touch-brake.active {
    background: #e74c3c;
    color: #fff;
    box-shadow: 0 0 16px rgba(231, 76, 60, 0.35);
}

/* ── Gear toggle ── */
.touch-gear {
    width: 44px;
    height: 44px;
    border-radius: 10px;
    font-size: 1.2em;
    background: #27ae60;
    color: #fff;
    box-shadow: 0 2px 8px rgba(39, 174, 96, 0.25);
}

.touch-gear .touch-gear-label {
    font-size: 1em;
    font-weight: 800;
}

.touch-gear.reverse {
    background: #e67e22;
    box-shadow: 0 2px 8px rgba(230, 126, 34, 0.25);
}

.touch-gear:active, .touch-gear.active {
    transform: scale(0.9);
}

/* ── Reset ── */
.touch-reset {
    width: 44px;
    height: 44px;
    border-radius: 10px;
    font-size: 1.1em;
    background: rgba(255, 255, 255, 0.7);
    color: #888;
    box-shadow: 0 2px 6px rgba(0, 0, 0, 0.06);
}

.touch-reset:active, .touch-reset.active {
    background: #ddd;
    color: #555;
}

/* ── Toggle checkboxes (camera, trail) as icons ── */
.touch-toggle {
    display: flex;
    align-items: center;
    justify-content: center;
    width: 38px;
    height: 38px;
    border-radius: 10px;
    background: rgba(255, 255, 255, 0.5);
    cursor: pointer;
    -webkit-tap-highlight-color: transparent;
    transition: background 0.15s, box-shadow 0.15s;
    position: relative;
}

.touch-toggle input[type="checkbox"] {
    position: absolute;
    opacity: 0;
    width: 0;
    height: 0;
}

.touch-toggle span {
    font-size: 1.1em;
    opacity: 0.35;
    transition: opacity 0.15s;
    line-height: 1;
}

.touch-toggle input:checked + span {
    opacity: 1;
}

.touch-toggle:has(input:checked) {
    background: rgba(255, 255, 255, 0.85);
    box-shadow: 0 2px 6px rgba(0, 0, 0, 0.06);
}

/* ═══════════════════════════════════════════════════════════════
   RESPONSIVE — desktop-only helper
   ═══════════════════════════════════════════════════════════════ */

@media (max-width: 1200px) {
    .main-content {
        grid-template-columns: 1fr;
    }

    .info-panel {
        flex-direction: row;
        flex-wrap: wrap;
    }

    .telemetry,
    .learning-tips {
        flex: 1;
        min-width: 280px;
    }
}

/* ═══════════════════════════════════════════════════════════════
   MOBILE — game-first layout
   ═══════════════════════════════════════════════════════════════ */
@media (max-width: 768px) {
    body {
        padding: 0;
        background: linear-gradient(160deg, #e8eaf6 0%, #f5f0ff 40%, #fce4ec 100%);
        min-height: 100dvh;
    }

    .container {
        display: flex;
        flex-direction: column;
        min-height: 100dvh;
    }

    /* ── Header: minimal ── */
    header {
        margin: 0;
        padding: 8px 14px;
        text-align: left;
        display: flex;
        align-items: center;
        gap: 8px;
    }

    header h1 {
        font-size: 0.95em;
        margin: 0;
        text-shadow: none;
        font-weight: 700;
        color: #4a4a6a;
        letter-spacing: -0.02em;
    }

    header p {
        display: none;
    }

    .desktop-only {
        display: none !important;
    }

    /* ── Main: fill space, center vertically ── */
    .main-content {
        display: flex;
        flex-direction: column;
        flex: 1;
        justify-content: center;
        gap: 0;
        margin: 0;
        padding: 0 8px;
    }

    /* ── Canvas container ── */
    .canvas-container {
        background: transparent;
        border-radius: 0;
        padding: 0;
        box-shadow: none;
        display: flex;
        flex-direction: column;
    }

    /* ── Scenario selector: horizontal scroll pills ── */
    .scenario-selector {
        margin: 0 0 6px;
        padding: 0;
        background: transparent;
        border: none;
        border-radius: 0;
    }

    .scenario-buttons {
        display: flex;
        gap: 6px;
        overflow-x: auto;
        -webkit-overflow-scrolling: touch;
        scrollbar-width: none;
        padding-bottom: 2px;
    }

    .scenario-buttons::-webkit-scrollbar {
        display: none;
    }

    .scenario-btn {
        flex: 0 0 auto;
        padding: 6px 14px;
        font-size: 0.78em;
        border-radius: 20px;
        border-width: 1.5px;
        border-color: rgba(102, 126, 234, 0.25);
        background: rgba(255, 255, 255, 0.6);
        color: #667eea;
        white-space: nowrap;
    }

    .scenario-btn:hover {
        transform: none;
        box-shadow: none;
    }

    .scenario-btn.active {
        background: #667eea;
        border-color: #667eea;
        color: #fff;
        box-shadow: 0 2px 8px rgba(102, 126, 234, 0.3);
    }

    .scenario-btn.custom {
        border-style: dashed;
    }

    /* ── Canvas ── */
    .canvas-wrapper {
        position: relative;
        overflow: hidden;
        border-radius: 16px;
        box-shadow: 0 4px 20px rgba(0, 0, 0, 0.10), 0 1px 4px rgba(0,0,0,0.06);
        /* 4:3 aspect ratio, capped to reasonable size */
        aspect-ratio: 4 / 3;
        max-height: 55dvh;
    }

    #carCanvas {
        width: 100%;
        height: 100%;
        border: none;
        border-radius: 16px;
        background: #f0f1f3;
    }

    /* ── Hide keyboard overlay on touch ── */
    .controls-overlay {
        display: none;
    }

    /* ── Show touch controls ── */
    .touch-controls {
        display: flex;
        padding: 10px 2px 6px;
        flex-shrink: 0;
    }

    /* ── Status overlay ── */
    .overlay-status {
        font-size: 0.78em;
        padding: 5px 12px;
        border-radius: 12px;
    }

    /* ── Info panel: hide entirely on mobile ── */
    .info-panel {
        display: none;
    }

    /* ── Educational info: hide on mobile ── */
    .educational-info {
        display: none;
    }
}

/* ── Extra small phones ── */
@media (max-width: 400px) {
    .touch-left,
    .touch-right {
        flex: 0 0 60px;
        height: 60px;
    }

    .touch-gas,
    .touch-brake {
        min-height: 60px;
    }

    .touch-gear,
    .touch-reset {
        width: 38px;
        height: 38px;
    }

    .touch-toggle {
        width: 34px;
        height: 34px;
    }
}
