/* CSS Reset and Variables */
:root {
    /* Neon Aurora Theme (Default) */
    --bg-color: #000000;
    --grid-bg: rgba(255, 255, 255, 0.02);
    --grid-border: rgba(255, 255, 255, 0.06);
    --cell-hover: rgba(255, 255, 255, 0.08);
    --text-color: #f5f5f7;
    --text-muted: #8e8e93;
    --glass-bg: rgba(20, 20, 30, 0.7);
    --glass-border: rgba(255, 255, 255, 0.1);
    --accent-glow: linear-gradient(135deg, #ff007f, #7f00ff, #00f0ff);
    --font-heading: 'Outfit', -apple-system, BlinkMacSystemFont, sans-serif;
    --font-body: 'Space Grotesk', monospace, sans-serif;
    --safe-glow: 0 0 20px rgba(127, 0, 255, 0.4);
    
    /* UI Black & White Minimalist Design Variables (Unchanging) */
    --ui-bg: rgba(12, 12, 14, 0.85); /* Black translucent glass */
    --ui-border: rgba(255, 255, 255, 0.12); /* Subtle white border */
    --ui-border-hover: rgba(255, 255, 255, 0.35);
    --ui-text: #f5f5f7; /* Crisp white */
    --ui-text-muted: #8e8e93; /* Muted gray */
    --ui-text-hover: #ffffff;
    --ui-accent: #ffffff;
    --ui-glow: 0 0 15px rgba(255, 255, 255, 0.15); /* Clean white glow */
    --ui-accent-glow: #ffffff; /* White background for toggles */
    
    /* Animation settings */
    --transition-speed: 0.3s;
}

/* Cyberpunk Theme */
[data-theme-style="cyber"] {
    --bg-color: #000000;
    --grid-bg: rgba(0, 240, 255, 0.01);
    --grid-border: rgba(0, 240, 255, 0.15);
    --cell-hover: rgba(255, 0, 85, 0.15);
    --text-color: #3ef2ff;
    --text-muted: #ff0055;
    --glass-bg: rgba(5, 5, 10, 0.85);
    --glass-border: #3ef2ff;
    --accent-glow: linear-gradient(90deg, #ff0055, #f5ed00, #00f0ff);
    --safe-glow: 0 0 15px rgba(0, 240, 255, 0.5);
}

/* Water Ripple Theme */
[data-theme-style="water"] {
    --bg-color: #000000;
    --grid-bg: rgba(0, 229, 255, 0.01);
    --grid-border: rgba(0, 229, 255, 0.1);
    --cell-hover: rgba(0, 229, 255, 0.08);
    --text-color: #00ffd5;
    --text-muted: #00aeff;
    --glass-bg: rgba(2, 10, 15, 0.85);
    --glass-border: #00e5ff;
    --accent-glow: linear-gradient(135deg, #00ffd5, #00e5ff, #0055ff);
    --safe-glow: 0 0 15px rgba(0, 229, 255, 0.4);
}

/* Monochrome Space Theme */
[data-theme-style="mono"] {
    --bg-color: #000000;
    --grid-bg: rgba(255, 255, 255, 0.01);
    --grid-border: rgba(255, 255, 255, 0.08);
    --cell-hover: rgba(255, 255, 255, 0.15);
    --text-color: #ffffff;
    --text-muted: #666666;
    --glass-bg: rgba(10, 10, 10, 0.9);
    --glass-border: rgba(255, 255, 255, 0.2);
    --accent-glow: linear-gradient(135deg, #ffffff, #888888, #222222);
    --safe-glow: 0 0 15px rgba(255, 255, 255, 0.3);
}

/* Aurora Forest Theme */
[data-theme-style="aurora"] {
    --bg-color: #000000;
    --grid-bg: rgba(0, 255, 135, 0.01);
    --grid-border: rgba(0, 255, 135, 0.1);
    --cell-hover: rgba(0, 255, 135, 0.08);
    --text-color: #00ff87;
    --text-muted: #39e09b;
    --glass-bg: rgba(3, 10, 8, 0.85);
    --glass-border: #00ff87;
    --accent-glow: linear-gradient(135deg, #00ff87, #00ffd5, #60efff);
    --safe-glow: 0 0 15px rgba(0, 255, 135, 0.4);
}

/* Sunset Golden Theme */
[data-theme-style="sunset"] {
    --bg-color: #000000;
    --grid-bg: rgba(255, 90, 0, 0.01);
    --grid-border: rgba(255, 90, 0, 0.1);
    --cell-hover: rgba(255, 90, 0, 0.08);
    --text-color: #ff9a00;
    --text-muted: #ff0051;
    --glass-bg: rgba(15, 5, 7, 0.85);
    --glass-border: #ff5a00;
    --accent-glow: linear-gradient(135deg, #ff003c, #ff5a00, #ffbb00);
    --safe-glow: 0 0 15px rgba(255, 90, 0, 0.4);
}

/* Deep Ocean Theme */
[data-theme-style="ocean"] {
    --bg-color: #000000;
    --grid-bg: rgba(0, 174, 255, 0.01);
    --grid-border: rgba(0, 174, 255, 0.1);
    --cell-hover: rgba(0, 174, 255, 0.08);
    --text-color: #00e5ff;
    --text-muted: #0055ff;
    --glass-bg: rgba(2, 7, 18, 0.85);
    --glass-border: #00aeff;
    --accent-glow: linear-gradient(135deg, #0055ff, #00aeff, #00e5ff);
    --safe-glow: 0 0 15px rgba(0, 174, 255, 0.4);
}

/* Cosmic Nebula Theme */
[data-theme-style="cosmic"] {
    --bg-color: #000000;
    --grid-bg: rgba(139, 92, 246, 0.01);
    --grid-border: rgba(139, 92, 246, 0.1);
    --cell-hover: rgba(139, 92, 246, 0.08);
    --text-color: #d946ef;
    --text-muted: #8b5cf6;
    --glass-bg: rgba(11, 5, 18, 0.85);
    --glass-border: #8b5cf6;
    --accent-glow: linear-gradient(135deg, #d946ef, #8b5cf6, #ec4899);
    --safe-glow: 0 0 15px rgba(139, 92, 246, 0.4);
}

* {
    margin: 0;
    padding: 0;
    box-sizing: border-box;
    -webkit-tap-highlight-color: transparent;
    user-select: none;
}

body, html {
    width: 100%;
    height: 100%;
    overflow: hidden;
    background-color: var(--bg-color);
    font-family: var(--font-body);
    color: var(--text-color);
    transition: background-color 0.8s cubic-bezier(0.2, 0.8, 0.2, 1);
}

/* Canvas Background */
#visual-canvas {
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    z-index: 1;
    pointer-events: none;
    transition: filter 0.8s cubic-bezier(0.25, 0.8, 0.25, 1);
}

/* Background Overlay for Theme-specific Filters and Textures */
#theme-overlay {
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    z-index: 1.5; /* Sits between canvas and interactive grid */
    pointer-events: none;
    opacity: 0;
    transition: opacity 0.8s cubic-bezier(0.25, 0.8, 0.25, 1), background 0.8s cubic-bezier(0.25, 0.8, 0.25, 1);
}

/* ----------------------------------------------------
   Theme-Specific Canvas Filters & Overlays
   ---------------------------------------------------- */

/* 1. Neon Aurora */
[data-theme-style="neon"] #visual-canvas {
    filter: drop-shadow(0 0 10px rgba(127, 0, 255, 0.45)) drop-shadow(0 0 2px rgba(0, 240, 255, 0.45)) contrast(1.15) saturate(1.2);
}

/* 2. Cyberpunk (CRT Scanlines, Green Shift & moving Scan Beam) */
[data-theme-style="cyber"] #visual-canvas {
    filter: contrast(1.5) saturate(1.4) brightness(1.2) hue-rotate(15deg);
}
[data-theme-style="cyber"] #theme-overlay {
    background: linear-gradient(rgba(18, 16, 16, 0) 40%, rgba(0, 0, 0, 0.5) 40%);
    background-size: 100% 6px;
    opacity: 0.75;
    animation: crt-flicker 0.15s infinite;
}
[data-theme-style="cyber"] #theme-overlay::after {
    content: " ";
    display: block;
    position: absolute;
    top: 0; left: 0; right: 0; bottom: 0;
    background: linear-gradient(0deg, transparent 0%, rgba(62, 242, 255, 0.08) 10%, transparent 20%);
    background-size: 100% 500%;
    animation: crt-scanline 8s linear infinite;
    pointer-events: none;
    z-index: 3;
}

/* 3. Water Ripple (Crisp water light refraction & shifting wave overlays) */
[data-theme-style="water"] #visual-canvas {
    filter: contrast(1.1) saturate(1.2) drop-shadow(0 0 8px rgba(0, 229, 255, 0.45));
}
[data-theme-style="water"] #theme-overlay {
    background: radial-gradient(circle at 20% 30%, rgba(0, 229, 255, 0.1) 0%, transparent 60%),
                radial-gradient(circle at 80% 70%, rgba(0, 174, 255, 0.1) 0%, transparent 60%);
    opacity: 0.85;
    animation: water-ripple 6s infinite alternate ease-in-out;
}

/* 4. Monochrome Space (Heavy Vignette Portal & Dark Grayscale) */
[data-theme-style="mono"] #visual-canvas {
    filter: grayscale(100%) contrast(4) brightness(0.95);
}
[data-theme-style="mono"] #theme-overlay {
    background: radial-gradient(circle, transparent 20%, rgba(8, 8, 8, 0.95) 95%);
    opacity: 0.98;
}

/* 5. Aurora Forest (Metaball Blur & Glow) */
[data-theme-style="aurora"] #visual-canvas {
    filter: blur(3px) contrast(1.6) saturate(1.4) brightness(1.1);
}
[data-theme-style="aurora"] #theme-overlay {
    background: linear-gradient(135deg, rgba(0, 255, 135, 0.12) 0%, rgba(0, 168, 255, 0.08) 50%, rgba(3, 10, 8, 0.8) 100%);
    opacity: 0.85;
    animation: aurora-flow 8s infinite alternate ease-in-out;
}

/* 6. Sunset Golden (Solar Flare Glow & Intense Warmth) */
[data-theme-style="sunset"] #visual-canvas {
    filter: saturate(2.5) contrast(1.4) hue-rotate(340deg) drop-shadow(0 0 20px rgba(255, 90, 0, 0.7));
}
[data-theme-style="sunset"] #theme-overlay {
    background: radial-gradient(circle at 90% 10%, rgba(255, 90, 0, 0.2) 0%, rgba(255, 0, 81, 0.1) 40%, transparent 70%);
    opacity: 0.8;
}

/* 7. Deep Ocean (Underwater Blur, Dark Vignette) */
[data-theme-style="ocean"] #visual-canvas {
    filter: contrast(1.1) saturate(0.8) hue-rotate(170deg) brightness(0.8) blur(1px) drop-shadow(0 0 12px rgba(0, 174, 255, 0.35));
}
[data-theme-style="ocean"] #theme-overlay {
    background: linear-gradient(180deg, rgba(0, 85, 255, 0.08) 0%, transparent 50%, rgba(2, 7, 18, 0.85) 100%);
    opacity: 0.9;
}

/* 8. Cosmic Nebula (Intense Stellar Light Dust & Bright Glow) */
[data-theme-style="cosmic"] #visual-canvas {
    filter: drop-shadow(0 0 15px rgba(139, 92, 246, 0.65)) drop-shadow(0 0 30px rgba(217, 70, 239, 0.4)) contrast(1.3) saturate(1.8) brightness(1.2);
}
[data-theme-style="cosmic"] #theme-overlay {
    background: radial-gradient(circle at 20% 70%, rgba(139, 92, 246, 0.18) 0%, transparent 60%),
                radial-gradient(circle at 80% 30%, rgba(217, 70, 239, 0.15) 0%, transparent 60%);
    opacity: 0.9;
}

/* Keyframes for Overlay Textures */
@keyframes crt-flicker {
    0% { opacity: 0.7; }
    50% { opacity: 0.75; }
    100% { opacity: 0.7; }
}

@keyframes crt-scanline {
    0% { transform: translateY(-100%); }
    100% { transform: translateY(100%); }
}

@keyframes aurora-flow {
    0% { transform: scale(1) translate(0, 0) rotate(0deg); opacity: 0.6; }
    100% { transform: scale(1.08) translate(1.5%, 1%) rotate(2deg); opacity: 0.9; }
}

@keyframes water-ripple {
    0% { transform: scale(1) translate(0, 0); opacity: 0.7; }
    100% { transform: scale(1.1) translate(2%, 1%); opacity: 0.9; }
}

/* Interactive Grid */
#grid-container {
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    z-index: 2;
    display: grid;
    /* Grid cells will be defined by javascript to stay dynamic */
    opacity: 0;
    transform: scale(1.02);
    transition: opacity 1s ease, transform 1s cubic-bezier(0.2, 0.8, 0.2, 1);
}

#grid-container.active {
    opacity: 1;
    transform: scale(1);
}

/* Individual Grid Cell */
.grid-cell {
    border-right: 1px solid transparent;
    border-bottom: 1px solid transparent;
    background-color: transparent;
    transition: background-color 0.15s ease, box-shadow 0.15s ease;
    cursor: pointer;
    position: relative;
    overflow: hidden;
}

.grid-cell:hover {
    background-color: rgba(255, 255, 255, 0.01);
}

/* Triggered State animation on hover/click */
.grid-cell.hit {
    animation: cell-hit-anim 0.4s cubic-bezier(0.1, 0.8, 0.3, 1) forwards;
}

@keyframes cell-hit-anim {
    0% {
        background-color: rgba(255, 255, 255, 0.15);
        box-shadow: inset 0 0 8px rgba(255, 255, 255, 0.2);
    }
    100% {
        background-color: transparent;
        box-shadow: inset 0 0 0px rgba(255, 255, 255, 0);
    }
}



/* Glass Header Controls (Top-Right) */
.glass-header-controls {
    position: fixed;
    top: 25px;
    right: 25px;
    z-index: 30;
    display: flex;
    align-items: center;
    gap: 8px;
    background: var(--ui-bg);
    border: 1px solid var(--ui-border);
    padding: 6px 12px;
    border-radius: 50px;
    box-shadow: 0 4px 20px rgba(0,0,0,0.3);
    backdrop-filter: blur(10px);
    -webkit-backdrop-filter: blur(10px);
    transition: all 0.4s cubic-bezier(0.2, 0.8, 0.2, 1);
    opacity: 0;
    transform: scale(0.8);
    pointer-events: none;
}

.glass-header-controls.visible {
    opacity: 1;
    transform: scale(1);
    pointer-events: auto;
}

.glass-header-controls:hover {
    border-color: var(--ui-border-hover);
    box-shadow: var(--ui-glow);
}

/* Header Icon Button */
.header-icon-btn {
    background: none;
    border: none;
    color: var(--ui-text);
    width: 38px;
    height: 38px;
    border-radius: 50%;
    display: flex;
    justify-content: center;
    align-items: center;
    cursor: pointer;
    transition: all 0.2s ease;
}

.header-icon-btn:hover {
    background: rgba(255, 255, 255, 0.1);
    color: var(--ui-text-hover);
    transform: scale(1.08);
}

.header-icon-btn:active {
    transform: scale(0.95);
}

/* Settings Glass Panel */
.glass-panel {
    position: fixed;
    top: 85px;
    right: 25px;
    width: 320px;
    background: var(--ui-bg);
    border: 1px solid var(--ui-border);
    border-radius: 20px;
    padding: 24px;
    z-index: 25;
    box-shadow: 0 10px 40px rgba(0,0,0,0.5);
    backdrop-filter: blur(16px);
    -webkit-backdrop-filter: blur(16px);
    display: flex;
    flex-direction: column;
    gap: 20px;
    transform: translateY(-20px) scale(0.95);
    opacity: 0;
    pointer-events: none;
    transition: all 0.4s cubic-bezier(0.2, 0.8, 0.2, 1);
    color: var(--ui-text);
}

.glass-panel.show {
    transform: translateY(0) scale(1);
    opacity: 1;
    pointer-events: auto;
}

/* UI Auto-Hide Utility Class */
.ui-hidden {
    opacity: 0 !important;
    pointer-events: none !important;
    transform: translateY(-10px) scale(0.95) !important;
}

.glass-autoplay.ui-hidden {
    transform: translateY(-10px) !important;
}

.panel-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
}

.panel-header h3 {
    font-family: var(--font-heading);
    font-size: 1.1rem;
    font-weight: 600;
}

#close-panel-btn {
    background: none;
    border: none;
    color: var(--ui-text-muted);
    font-size: 1.5rem;
    cursor: pointer;
    transition: color 0.2s;
    line-height: 1;
}

#close-panel-btn:hover {
    color: var(--ui-text-hover);
}

.control-group {
    display: flex;
    flex-direction: column;
    gap: 8px;
}

.control-group label {
    font-size: 0.8rem;
    color: var(--ui-text-muted);
    text-transform: uppercase;
    letter-spacing: 0.5px;
}

/* Beautiful custom select */
.glass-select {
    background: rgba(255, 255, 255, 0.04);
    border: 1px solid var(--ui-border);
    border-radius: 10px;
    padding: 10px 12px;
    color: var(--ui-text);
    font-family: var(--font-body);
    font-size: 0.9rem;
    outline: none;
    cursor: pointer;
    transition: border-color 0.2s;
}

.glass-select:focus {
    border-color: var(--ui-border-hover);
}

.glass-select option {
    background: #0c0b13;
    color: #fff;
}

/* Slider Controls */
.slider-container {
    display: flex;
    align-items: center;
    gap: 12px;
}

.volume-icon {
    font-size: 1rem;
}

input[type="range"] {
    flex-grow: 1;
    -webkit-appearance: none;
    appearance: none;
    background: rgba(255, 255, 255, 0.08);
    height: 6px;
    border-radius: 3px;
    outline: none;
}

input[type="range"]::-webkit-slider-thumb {
    -webkit-appearance: none;
    appearance: none;
    width: 16px;
    height: 16px;
    border-radius: 50%;
    background: #fff;
    cursor: pointer;
    box-shadow: 0 0 10px rgba(0, 0, 0, 0.5);
    transition: transform 0.1s;
}

input[type="range"]::-webkit-slider-thumb:hover {
    transform: scale(1.2);
}

/* Toggles & Switch styling */
.toggles {
    display: flex;
    flex-direction: column;
    gap: 14px;
}

.toggle-item {
    display: flex;
    justify-content: space-between;
    align-items: center;
    font-size: 0.9rem;
}

.switch {
    position: relative;
    display: inline-block;
    width: 44px;
    height: 24px;
}

.switch input {
    opacity: 0;
    width: 0;
    height: 0;
}

.slider-round {
    position: absolute;
    cursor: pointer;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    background-color: rgba(255, 255, 255, 0.08);
    transition: .3s;
    border-radius: 24px;
    border: 1px solid var(--ui-border);
}

.slider-round:before {
    position: absolute;
    content: "";
    height: 16px;
    width: 16px;
    left: 3px;
    bottom: 3px;
    background-color: white;
    transition: .3s;
    border-radius: 50%;
    box-shadow: 0 1px 3px rgba(0,0,0,0.4);
}

input:checked + .slider-round {
    background: var(--ui-accent-glow);
    border-color: rgba(255, 255, 255, 0.3);
}

input:checked + .slider-round:before {
    transform: translateX(20px);
    background-color: #121214; /* Dark dot when background is white */
}

/* Theme buttons */
.theme-buttons {
    display: grid;
    grid-template-columns: repeat(2, 1fr);
    gap: 8px;
}

.theme-btn {
    background: rgba(255, 255, 255, 0.04);
    border: 1px solid var(--ui-border);
    border-radius: 8px;
    padding: 8px;
    color: var(--ui-text-muted);
    font-family: var(--font-body);
    font-size: 0.8rem;
    cursor: pointer;
    transition: all 0.2s;
}

.theme-btn:hover {
    color: var(--ui-text-hover);
    background: rgba(255, 255, 255, 0.08);
    border-color: var(--ui-border-hover);
}

.theme-btn.active {
    color: #000000;
    background: #ffffff;
    border-color: #ffffff;
    font-weight: 600;
    box-shadow: 0 2px 10px rgba(255, 255, 255, 0.2);
}

/* Standalone Autoplay in Top-Left */
.glass-autoplay {
    position: fixed;
    top: 25px;
    left: 25px;
    z-index: 30;
    display: flex;
    align-items: center;
    gap: 12px;
    background: var(--ui-bg);
    border: 1px solid var(--ui-border);
    padding: 10px 18px;
    border-radius: 50px;
    box-shadow: 0 4px 20px rgba(0,0,0,0.3);
    backdrop-filter: blur(10px);
    -webkit-backdrop-filter: blur(10px);
    transition: all 0.3s cubic-bezier(0.2, 0.8, 0.2, 1);
}

.glass-autoplay:hover {
    border-color: var(--ui-border-hover);
    box-shadow: var(--ui-glow);
}

.autoplay-label {
    font-size: 0.85rem;
    color: var(--ui-text);
    letter-spacing: 0.5px;
    font-weight: 500;
}

/* Mobile adaptations */
@media (max-width: 600px) {
    .glass-autoplay {
        top: 20px;
        left: 20px;
        padding: 8px 14px;
        gap: 8px;
    }

    .glass-header-controls {
        top: 20px;
        right: 20px;
        padding: 4px 8px;
    }

    .glass-panel {
        width: calc(100% - 40px);
        left: 20px;
        right: 20px;
        top: 75px;
        bottom: auto;
    }
}
