:root {
    --primary: #00ff88;
    --primary-glow: rgba(0, 255, 136, 0.3);
    --secondary: #00d4ff;
    --danger: #ff3366;
    --danger-glow: rgba(255, 51, 102, 0.3);
    --bonus: #ffd700;
    --ui-bg: rgba(10, 20, 40, 0.75);
    --ui-border: rgba(255, 255, 255, 0.1);
    --glass-bg: rgba(255, 255, 255, 0.05);
    --text-primary: #ffffff;
    --text-secondary: rgba(255, 255, 255, 0.6);
}

* {
    box-sizing: border-box;
}

body {
    margin: 0;
    padding: 0;
    background-color: #0a0a0f;
    color: var(--text-primary);
    font-family: 'Inter', 'SF Pro Display', -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
    overflow: hidden;
    user-select: none;
    -webkit-font-smoothing: antialiased;
}

#game-container {
    position: relative;
    width: 100vw;
    height: 100vh;
    display: flex;
    justify-content: center;
    align-items: center;
}

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

/* --- UI OVERLAYS --- */
#ui-layer {
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    pointer-events: none;
    display: flex;
    flex-direction: column;
    justify-content: space-between;
}

/* Top HUD - Glassmorphism Style */
.hud-top {
    display: flex;
    justify-content: space-between;
    align-items: flex-start;
    padding: 24px 32px;
    font-size: 24px;
    font-weight: 600;
    background: linear-gradient(180deg, rgba(0, 0, 0, 0.7) 0%, transparent 100%);
}

.hud-center {
    display: flex;
    flex-direction: column;
    align-items: center;
    flex: 1;
}

.distance-display {
    display: flex;
    align-items: baseline;
    gap: 8px;
    background: var(--glass-bg);
    backdrop-filter: blur(20px);
    -webkit-backdrop-filter: blur(20px);
    padding: 12px 28px;
    border-radius: 50px;
    border: 1px solid var(--ui-border);
}

.distance-display #distVal {
    font-family: 'JetBrains Mono', 'SF Mono', 'Fira Code', monospace;
    font-size: 36px;
    font-weight: 700;
    color: var(--text-primary);
}

.distance-unit {
    font-size: 14px;
    font-weight: 600;
    color: var(--text-secondary);
    text-transform: uppercase;
    letter-spacing: 1px;
}

.score-box {
    display: flex;
    flex-direction: column;
    gap: 4px;
    background: var(--glass-bg);
    backdrop-filter: blur(20px);
    -webkit-backdrop-filter: blur(20px);
    padding: 16px 24px;
    border-radius: 16px;
    border: 1px solid var(--ui-border);
    min-width: 120px;
}

.score-label {
    font-size: 11px;
    font-weight: 500;
    color: var(--text-secondary);
    text-transform: uppercase;
    letter-spacing: 1.5px;
}

.score-val {
    font-family: 'JetBrains Mono', 'SF Mono', 'Fira Code', monospace;
    font-size: 28px;
    font-weight: 700;
    background: linear-gradient(135deg, var(--primary) 0%, var(--secondary) 100%);
    -webkit-background-clip: text;
    -webkit-text-fill-color: transparent;
    background-clip: text;
    text-shadow: none;
}

/* Keyboard Controls Hint */
#controls-hint {
    position: absolute;
    bottom: 24px;
    left: 24px;
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 6px;
    opacity: 0.6;
    transition: opacity 0.3s ease;
}

#controls-hint:hover {
    opacity: 1;
}

.key-row {
    display: flex;
    gap: 6px;
}

.key {
    width: 40px;
    height: 40px;
    display: flex;
    align-items: center;
    justify-content: center;
    background: var(--glass-bg);
    backdrop-filter: blur(10px);
    -webkit-backdrop-filter: blur(10px);
    border: 1px solid var(--ui-border);
    border-radius: 8px;
    font-family: 'JetBrains Mono', 'SF Mono', monospace;
    font-size: 14px;
    font-weight: 600;
    color: var(--text-secondary);
    transition: all 0.1s ease;
}

.key.active {
    background: var(--primary);
    color: #000;
    border-color: var(--primary);
    box-shadow: 0 0 20px var(--primary-glow);
    transform: scale(0.95);
}

/* Camera Preview - Modern Card */
#webcam-preview {
    position: absolute;
    top: 24px;
    right: 24px;
    width: 180px;
    height: 135px;
    border-radius: 16px;
    border: 2px solid var(--ui-border);
    background: #000;
    transform: scaleX(-1);
    opacity: 0.9;
    z-index: 10;
    box-shadow: 0 8px 32px rgba(0, 0, 0, 0.4), 0 0 0 1px rgba(0, 255, 136, 0.2);
    transition: all 0.3s ease;
}

#webcam-preview:hover {
    box-shadow: 0 8px 32px rgba(0, 0, 0, 0.4), 0 0 20px var(--primary-glow);
}

/* Gesture Debugger - Glass Card */
#gesture-state {
    position: absolute;
    top: 175px;
    right: 24px;
    width: 180px;
    background: var(--ui-bg);
    backdrop-filter: blur(20px);
    -webkit-backdrop-filter: blur(20px);
    padding: 16px;
    border-radius: 16px;
    font-size: 12px;
    border: 1px solid var(--ui-border);
    box-shadow: 0 8px 32px rgba(0, 0, 0, 0.3);
}

.state-row {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 8px 0;
    border-bottom: 1px solid var(--ui-border);
}

.state-row:last-child {
    border-bottom: none;
}

.state-row span:first-child {
    color: var(--text-secondary);
    font-weight: 500;
}

.val {
    color: var(--primary);
    font-family: 'JetBrains Mono', 'SF Mono', monospace;
    font-weight: 600;
    font-size: 13px;
}

/* Floating Messages - Enhanced Animation */
#message-area {
    position: absolute;
    top: 30%;
    width: 100%;
    text-align: center;
    pointer-events: none;
}

.float-msg {
    display: inline-block;
    font-size: 48px;
    font-weight: 800;
    color: var(--bonus);
    text-shadow: 0 0 30px rgba(255, 215, 0, 0.6), 0 4px 20px rgba(0, 0, 0, 0.5);
    animation: floatUp 1.2s cubic-bezier(0.34, 1.56, 0.64, 1) forwards;
    letter-spacing: 2px;
}

@keyframes floatUp {
    0% { 
        opacity: 0; 
        transform: translateY(20px) scale(0.8); 
        filter: blur(10px);
    }
    20% { 
        opacity: 1; 
        transform: translateY(0) scale(1.1); 
        filter: blur(0);
    }
    100% { 
        opacity: 0; 
        transform: translateY(-60px) scale(1.3); 
        filter: blur(5px);
    }
}

/* Main Menu / Overlays - Modern Glass Design */
#overlay-screen, #game-over-screen {
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background: radial-gradient(ellipse at center, rgba(10, 20, 40, 0.9) 0%, rgba(5, 5, 15, 0.95) 100%);
    backdrop-filter: blur(10px);
    -webkit-backdrop-filter: blur(10px);
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    pointer-events: auto;
    z-index: 100;
    transition: opacity 0.4s ease;
}

#game-over-screen {
    background: radial-gradient(ellipse at center, rgba(60, 10, 20, 0.95) 0%, rgba(20, 5, 10, 0.98) 100%);
    z-index: 101;
}

/* Title - Gradient Text */
h1 {
    font-size: 72px;
    font-weight: 900;
    margin: 0 0 8px 0;
    background: linear-gradient(135deg, #fff 0%, var(--primary) 50%, var(--secondary) 100%);
    -webkit-background-clip: text;
    -webkit-text-fill-color: transparent;
    background-clip: text;
    font-style: italic;
    letter-spacing: -2px;
    text-shadow: none;
    animation: titleGlow 3s ease-in-out infinite alternate;
}

@keyframes titleGlow {
    0% { filter: drop-shadow(0 0 20px var(--primary-glow)); }
    100% { filter: drop-shadow(0 0 40px rgba(0, 212, 255, 0.4)); }
}

h2 {
    font-size: 18px;
    font-weight: 500;
    color: var(--text-secondary);
    margin-bottom: 48px;
    text-transform: uppercase;
    letter-spacing: 6px;
}

/* Crash Screen */
.crash-title {
    font-size: 96px;
    font-weight: 900;
    color: var(--danger);
    margin: 0;
    text-shadow: 0 0 60px var(--danger-glow), 0 0 120px var(--danger-glow);
    font-style: italic;
    letter-spacing: -3px;
    animation: crashPulse 0.5s ease-out;
}

@keyframes crashPulse {
    0% { transform: scale(1.5); opacity: 0; }
    50% { transform: scale(0.95); }
    100% { transform: scale(1); opacity: 1; }
}

.final-score {
    font-size: 36px;
    font-weight: 300;
    margin: 32px 0 12px 0;
    color: var(--text-primary);
}

.final-score span {
    font-weight: 700;
    color: var(--primary);
}

.high-score-display {
    font-size: 18px;
    color: var(--bonus);
    margin-bottom: 24px;
    display: flex;
    align-items: center;
    gap: 8px;
}

.high-score-display::before {
    content: '👑';
}

/* Wave hint animation */
.wave-hint {
    font-size: 24px;
    color: var(--primary);
    margin-bottom: 32px;
    animation: waveHint 1.5s ease-in-out infinite;
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 12px;
}

@keyframes waveHint {
    0%, 100% { transform: translateX(0); opacity: 0.7; }
    25% { transform: translateX(-10px); opacity: 1; }
    75% { transform: translateX(10px); opacity: 1; }
}

/* Instruction Cards - Glass Morphism */
.instructions {
    display: grid;
    grid-template-columns: repeat(2, 1fr);
    gap: 16px;
    max-width: 560px;
    margin-bottom: 48px;
}

.instruction-card {
    background: var(--glass-bg);
    backdrop-filter: blur(20px);
    -webkit-backdrop-filter: blur(20px);
    padding: 20px 24px;
    border-radius: 20px;
    border: 1px solid var(--ui-border);
    transition: all 0.3s ease;
    position: relative;
    overflow: hidden;
}

.instruction-card::before {
    content: '';
    position: absolute;
    top: 0;
    left: 0;
    right: 0;
    height: 1px;
    background: linear-gradient(90deg, transparent, rgba(255, 255, 255, 0.2), transparent);
}

.instruction-card:hover {
    transform: translateY(-4px);
    border-color: rgba(0, 255, 136, 0.3);
    box-shadow: 0 12px 40px rgba(0, 0, 0, 0.3), 0 0 20px var(--primary-glow);
}

.instruction-card h3 {
    margin: 0 0 8px 0;
    font-size: 20px;
    font-weight: 600;
    color: var(--text-primary);
}

.instruction-card p {
    margin: 0;
    font-size: 14px;
    color: var(--text-secondary);
    line-height: 1.5;
}

/* Modern Button */
.btn {
    position: relative;
    background: linear-gradient(135deg, var(--primary) 0%, #00cc6a 100%);
    color: #000;
    border: none;
    padding: 18px 56px;
    font-size: 18px;
    font-weight: 700;
    border-radius: 50px;
    cursor: pointer;
    text-transform: uppercase;
    letter-spacing: 2px;
    box-shadow: 0 8px 32px var(--primary-glow), 0 0 0 1px rgba(255, 255, 255, 0.1) inset;
    transition: all 0.3s cubic-bezier(0.34, 1.56, 0.64, 1);
    overflow: hidden;
}

.btn::before {
    content: '';
    position: absolute;
    top: 0;
    left: -100%;
    width: 100%;
    height: 100%;
    background: linear-gradient(90deg, transparent, rgba(255, 255, 255, 0.3), transparent);
    transition: left 0.5s ease;
}

.btn:hover {
    transform: translateY(-3px) scale(1.02);
    box-shadow: 0 12px 48px var(--primary-glow), 0 0 0 1px rgba(255, 255, 255, 0.2) inset;
}

.btn:hover::before {
    left: 100%;
}

.btn:active {
    transform: translateY(0) scale(0.98);
}

/* Danger Button Variant */
#restart-btn {
    background: linear-gradient(135deg, var(--danger) 0%, #cc2952 100%) !important;
    box-shadow: 0 8px 32px var(--danger-glow), 0 0 0 1px rgba(255, 255, 255, 0.1) inset !important;
}

#restart-btn:hover {
    box-shadow: 0 12px 48px var(--danger-glow), 0 0 0 1px rgba(255, 255, 255, 0.2) inset !important;
}

.hidden { display: none !important; }

#loading-msg {
    color: var(--text-secondary);
    margin-top: 16px;
    font-size: 16px;
    font-weight: 400;
    text-align: center;
    line-height: 1.6;
}

#loading-msg strong {
    color: var(--primary);
    font-weight: 600;
}

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

/* Responsive adjustments */
@media (max-width: 768px) {
    h1 { font-size: 48px; }
    h2 { font-size: 14px; letter-spacing: 4px; }
    .instructions { grid-template-columns: 1fr; max-width: 300px; }
    .crash-title { font-size: 64px; }
    .score-box { padding: 12px 16px; min-width: 100px; }
    .score-val { font-size: 24px; }
    #webcam-preview { width: 140px; height: 105px; }
    #gesture-state { width: 140px; top: 145px; }
}

#loading-msg { color: #888; margin-top: 10px; font-style: italic; }
