:root {
    /* --- HIGH-PERFORMANCE VISUALS --- */

    /* Phase 1: Solar flare (Bottom-up glow) */
    --p1-bg: radial-gradient(circle at 50% 120%, #600f25 0%, #1a050a 50%, #000000 100%);
    --p1-accent: #ff0055;

    /* Phase 2: Cyber Grid (Vertical deep fade) */
    --p2-bg: linear-gradient(to bottom, #000000 0%, #001014 60%, #002b2b 100%);
    --p2-accent: #00ffea;

    /* Phase 3: The Void (Central vignette) */
    --p3-bg: radial-gradient(circle at 50% 50%, #300000 0%, #000000 80%);
    --p3-accent: #ff2222;

    /* Phase Logic */
    --active-bg: var(--p1-bg);
    --active-color: var(--p1-accent);
    --active-glow: rgba(255, 0, 85, 0.6);

    /* UI Variables */
    --ui-glass: rgba(10, 15, 20, 0.65);
    --ui-border: rgba(255, 255, 255, 0.15);
    --font-head: 'Orbitron', sans-serif;
    --font-body: 'Rajdhani', sans-serif;
}

/* Phase Coloring Logic (Updates Body BG and UI colors) */
body.phase-1 {
    --active-bg: var(--p1-bg);
    --active-color: var(--p1-accent);
    --active-glow: rgba(255, 0, 85, 0.6);
}

body.phase-2 {
    --active-bg: var(--p2-bg);
    --active-color: var(--p2-accent);
    --active-glow: rgba(0, 255, 234, 0.6);
}

body.phase-3 {
    --active-bg: var(--p3-bg);
    --active-color: var(--p3-accent);
    --active-glow: rgba(255, 42, 42, 0.6);
}

* {
    margin: 0;
    padding: 0;
    box-sizing: border-box;
    user-select: none;
    -webkit-tap-highlight-color: transparent;
}

html,
body {
    background: var(--active-bg);
    background-size: cover;
    background-attachment: fixed;
    /* This ensures the gradient transition is buttery smooth */
    transition: background-image 1.5s ease-in-out, background-color 1.5s ease;

    font-family: var(--font-body);
    width: 100%;
    height: 100dvh;
    overflow: hidden;
    position: fixed;
    top: 0;
    left: 0;
    touch-action: none;
    color: white;
}

/* --- BACKGROUND FX --- */
#game-container {
    position: absolute;
    inset: 0;
    z-index: 1;
}

#game-container::after {
    content: '';
    position: absolute;
    top: 0; left: 0; width: 100%; height: 40%;
    /* Fades from Black (Top) to Transparent */
    background: linear-gradient(to bottom, #000000 10%, transparent 100%);
    pointer-events: none; /* Let clicks pass through */
    z-index: 2; /* Sits above canvas but below UI */
}

canvas {
    display: block;
    width: 100%;
    height: 100%;
}

/* Only the flash layer remains (for phase changes), invisible otherwise */
#flash-layer {
    position: fixed;
    inset: 0;
    background: white;
    opacity: 0;
    pointer-events: none;
    z-index: 99;
    transition: opacity 0.05s ease-out;
}

/* --- UI LAYOUT & SCREENS --- */
#ui-layer {
    position: absolute;
    inset: 0;
    display: flex;
    justify-content: center;
    align-items: center;
    z-index: 20;
    pointer-events: none;
}

.screen {
    position: relative;
    pointer-events: auto;
    background: var(--ui-glass);
    backdrop-filter: blur(12px);
    -webkit-backdrop-filter: blur(12px);
    border: 1px solid var(--ui-border);
    border-left: 4px solid var(--active-color);
    border-right: 4px solid var(--active-color);
    padding: 2rem 4rem;
    text-align: center;
    max-width: 600px;
    width: 90%;

    /* Sci-Fi Shape */
    clip-path: polygon(20px 0, 100% 0,
            100% calc(100% - 20px), calc(100% - 20px) 100%,
            0 100%, 0 20px);

    box-shadow: 0 0 50px rgba(0, 0, 0, 0.8), inset 0 0 20px rgba(0, 0, 0, 0.5);
    transition: all 0.3s cubic-bezier(0.2, 0.8, 0.2, 1);

    display: flex;
    flex-direction: column;
    gap: 1.5rem;
}

.screen.hidden {
    opacity: 0;
    transform: scale(0.9) translateY(20px);
    pointer-events: none;
    display: none;
}

/* --- TYPOGRAPHY --- */
h1 {
    font-family: var(--font-head);
    font-weight: 900;
    font-size: 3.5rem;
    line-height: 0.9;
    letter-spacing: 0.2rem;
    text-shadow: 0 0 15px var(--active-glow);
}

h1.sub {
    font-size: 2.5rem;
    color: white;
    opacity: 0.8;
}

h1.red {
    color: #ff3333;
    text-shadow: 0 0 20px #ff0000;
}

.version-tag {
    font-family: var(--font-body);
    font-size: 0.7rem;
    letter-spacing: 3px;
    color: var(--active-color);
    margin-top: 5px;
    opacity: 0.8;
}

.instruction-text {
    font-size: 1rem;
    color: #aaa;
    letter-spacing: 1px;
}

.pc-instructions {
    font-size: 0.8rem;
    color: #666;
    margin-top: 0.5rem;
}

/* --- INTERACTIVE ELEMENTS --- */
.settings-grid {
    display: flex;
    justify-content: center;
    gap: 2rem;
    width: 100%;
}

.setting-col {
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 0.5rem;
}

.label {
    font-size: 0.7rem;
    font-weight: 700;
    color: var(--active-color);
    letter-spacing: 2px;
    text-transform: uppercase;
}

.toggle-group {
    display: flex;
    gap: 5px;
    background: rgba(0, 0, 0, 0.4);
    padding: 4px;
    border-radius: 4px;
    border: 1px solid rgba(255, 255, 255, 0.1);
}

.ctrl-btn {
    background: transparent;
    border: none;
    color: #666;
    padding: 8px 16px;
    font-family: var(--font-head);
    font-size: 0.75rem;
    cursor: pointer;
    transition: 0.2s;
    border-radius: 2px;
}

.ctrl-btn.selected {
    background: var(--active-color);
    color: #000;
    font-weight: 700;
    box-shadow: 0 0 15px var(--active-glow);
}

.mega-btn {
    background: linear-gradient(90deg, transparent, rgba(255, 255, 255, 0.05), transparent);
    border: 1px solid var(--active-color);
    color: var(--active-color);
    font-family: var(--font-head);
    font-size: 1.5rem;
    font-weight: 700;
    padding: 1rem 3rem;
    letter-spacing: 4px;
    cursor: pointer;
    position: relative;
    overflow: hidden;
    transition: all 0.2s;
    margin-top: 10px;

    /* Slanted Edges */
    clip-path: polygon(15px 0, 100% 0, 100% calc(100% - 15px), calc(100% - 15px) 100%, 0 100%, 0 15px);
}

.mega-btn:hover {
    background: var(--active-color);
    color: #000;
    box-shadow: 0 0 30px var(--active-glow);
    transform: scale(1.05);
}

.mega-btn:active {
    transform: scale(0.98);
}

/* --- HUD --- */
#hud {
    position: absolute;
    inset: 0;
    pointer-events: none;
    display: flex;
    flex-direction: column;
    justify-content: space-between;
}

#hud.hidden {
    display: none;
}

.hud-top {
    display: flex;
    justify-content: space-between;
    align-items: flex-start;
    padding: 15px 25px;
    width: 100%;
    /* Subtle gradient for readability ONLY behind numbers, not covering game */
    background: linear-gradient(to bottom, rgba(0, 0, 0, 0.6) 0%, transparent 100%);
}

.hud-module {
    display: flex;
    flex-direction: column;
    gap: 5px;
}

.hud-module.center {
    align-items: center;
    width: 40%;
}

.hud-module.right {
    align-items: flex-end;
    text-align: right;
}

.big-num {
    font-family: var(--font-head);
    font-size: 2rem;
    color: white;
    text-shadow: 0 0 10px rgba(255, 255, 255, 0.5);
    line-height: 1;
}

.phase-badge {
    background: rgba(0, 0, 0, 0.6);
    border: 1px solid var(--active-color);
    padding: 5px 20px;
    clip-path: polygon(10px 0, 100% 0, 100% 100%, 0 100%, 0 10px);
}

#phase-text {
    font-family: var(--font-head);
    font-size: 1.2rem;
    color: var(--active-color);
    text-shadow: 0 0 10px var(--active-glow);
}

#health-container {
    width: 100%;
    max-width: 300px;
    height: 4px;
    background: #333;
    margin-top: 10px;
    position: relative;
}

#progress-fill {
    height: 100%;
    background: var(--active-color);
    box-shadow: 0 0 15px var(--active-glow);
    transition: width 0.1s linear, background 0.5s;
}

.bpm-box,
.lives-box {
    display: flex;
    align-items: center;
    gap: 10px;
}

#beat-meter {
    width: 10px;
    height: 10px;
    background: #333;
    transform: rotate(45deg);
    transition: transform 0.05s;
}

#beat-meter.pulse {
    background: var(--active-color);
    transform: rotate(45deg) scale(1.6);
    box-shadow: 0 0 10px var(--active-glow);
}

/* --- MOBILE CONTROLS --- */
#mobile-controls {
    position: absolute;
    bottom: 0;
    left: 0;
    width: 100%;
    height: 100%;
    pointer-events: none;
    z-index: 50;
    display: flex;
    justify-content: space-between;
    align-items: flex-end;
    padding: 20px 40px;
}

/* Ensure mobile controls actually hide when JS adds .hidden */
#mobile-controls.hidden {
    display: none !important;
    pointer-events: none !important;
}


.touch-zone {
    width: 140px;
    height: 140px;
    background: radial-gradient(circle, rgba(255, 255, 255, 0.05) 0%, transparent 70%);
    border: 1px solid rgba(255, 255, 255, 0.1);
    border-radius: 50%;
    display: flex;
    justify-content: center;
    align-items: center;
    pointer-events: auto;
    backdrop-filter: blur(2px);
    transition: 0.1s;
    margin-bottom: 20px;
}

.touch-zone:active {
    background: rgba(255, 255, 255, 0.1);
    border-color: var(--active-color);
    transform: scale(0.95);
    box-shadow: 0 0 20px var(--active-glow);
}

.arrow-shape {
    width: 0;
    height: 0;
    border-top: 15px solid transparent;
    border-bottom: 15px solid transparent;
}

#touch-left .arrow-shape {
    border-right: 25px solid rgba(255, 255, 255, 0.8);
}

#touch-right .arrow-shape {
    border-left: 25px solid rgba(255, 255, 255, 0.8);
}

/* --- GAME OVER STATS --- */
.stats-grid {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 2rem;
    width: 100%;
    margin: 1rem 0;
}

.stat-val {
    font-family: var(--font-head);
    font-size: 2.5rem;
    color: white;
    text-shadow: 0 0 10px rgba(255, 255, 255, 0.5);
}

/* --- UTILITIES & ROTATE --- */
#rotate-message {
    display: none;
    position: fixed;
    inset: 0;
    z-index: 9999;
    background: #000;
    flex-direction: column;
    justify-content: center;
    align-items: center;
    text-align: center;
    gap: 20px;
}

.rotate-icon {
    font-size: 4rem;
    color: var(--p1-accent);
    animation: spin 2s infinite linear;
}

@keyframes spin {
    100% {
        transform: rotate(-90deg);
    }
}

.mobile-only {
    display: none !important;
}

/* --- MEDIA QUERIES --- */
@media (hover: none) and (pointer: coarse) {
    .mobile-only {
        display: flex !important;
    }

    .pc-instructions {
        display: none;
    }

    .screen {
        padding: 1.5rem;
        width: 95%;
    }

    h1 {
        font-size: 2.5rem;
    }

    h1.sub {
        font-size: 1.8rem;
    }

    .mega-btn {
        padding: 0.8rem 2rem;
        font-size: 1.2rem;
    }
}

@media (orientation: portrait) {
    #rotate-message {
        display: flex !important;
    }
}

/* Compact Landscape (Mobile Phone Game Mode) */
@media (max-height: 500px) and (orientation: landscape) {
    .screen {
        padding: 0.5rem 2rem;
        gap: 0.5rem;
        height: 90vh;
        justify-content: center;
    }

    .logo-box {
        transform: scale(0.8);
    }

    .instruction-text {
        display: none;
    }

    .settings-grid {
        gap: 1rem;
        margin: 5px 0;
    }

    .label {
        font-size: 0.6rem;
    }

    .ctrl-btn {
        padding: 5px 10px;
        font-size: 0.6rem;
    }

    .mega-btn {
        margin-top: 5px;
        font-size: 1rem;
        padding: 0.6rem 2rem;
    }

    /* HUD optimizations for short screens */
    .hud-top {
        padding: 10px 30px;
    }

    .big-num {
        font-size: 1.5rem;
    }

    .touch-zone {
        width: 90px;
        height: 90px;
        margin-bottom: 10px;
    }
}

/* Glitch Animation */
.glitch {
    position: relative;
}

.glitch::before,
.glitch::after {
    content: attr(data-text);
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background: var(--ui-glass);
    opacity: 0.7;
}

.glitch::before {
    left: 2px;
    text-shadow: -1px 0 #ff00c1;
    clip: rect(44px, 450px, 56px, 0);
    animation: glitch-anim 5s infinite linear alternate-reverse;
}

.glitch::after {
    left: -2px;
    text-shadow: -1px 0 #00fff9;
    clip: rect(44px, 450px, 56px, 0);
    animation: glitch-anim 5s infinite linear alternate-reverse;
}

@keyframes glitch-anim {
    0% {
        clip: rect(61px, 9999px, 52px, 0);
    }

    5% {
        clip: rect(33px, 9999px, 10px, 0);
    }

    10% {
        clip: rect(85px, 9999px, 94px, 0);
    }

    100% {
        clip: rect(10px, 9999px, 60px, 0);
    }
}