/* Global & Reset */

:focus-visible {
    outline: 2px solid rgba(255, 255, 255, 0.7);
    outline-offset: 3px;
    border-radius: 3px;
}

body {
    background-color: black;
    margin: 0;
    padding: 0;
    overflow: hidden;
    /* App handles scrolling */
    font-family: 'Roboto', sans-serif;
    color: white;
    user-select: none;
    -webkit-font-smoothing: antialiased;
}

h1,
h2,
p {
    margin: 0;
    font-weight: 300;
}

a {
    color: #007bff;
    text-decoration: none;
    transition: opacity 0.2s;
}

a:hover {
    opacity: 0.7;
}

button {
    border: none;
    background: transparent;
    cursor: pointer;
    color: white;
    outline: none;
    font-family: inherit;
    /* Ensure Roboto is used */
}

/* Utility: Hidden */
.hidden {
    display: none !important;
    opacity: 0 !important;
    pointer-events: none !important;
}

/* UI Layer */
#ui-layer {
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    pointer-events: none;
    /* Let clicks pass through to canvas/grid */
    z-index: 500;
}

/* Previous definitions removed in favor of Unified Viewer Controls */

#thumbnail-selector {
    opacity: 0;
    position: absolute;
    bottom: 0;
    left: 0;
    width: 100%;
    height: 80px;
    background: rgba(0, 0, 0, 0.8);
    display: flex;
    align-items: center;
    overflow-x: auto;
    padding: 5px;
    box-sizing: border-box;
    transition: transform 0.3s ease;
    z-index: 240;
}

#thumbnail-selector.hidden {
    transform: translateY(100%);
}

#thumbnail-selector .thumb-small {
    height: 60px;
    width: auto;
    margin-right: 5px;
    border: 2px solid transparent;
    cursor: pointer;
    opacity: 0.6;
    transition: opacity 0.2s, border-color 0.2s;
    flex-shrink: 0;
}

#thumbnail-selector .thumb-small:hover {
    opacity: 1.0;
}

#thumbnail-selector .thumb-small.active {
    opacity: 1.0;
    border-color: #007bff;
}

#ui-layer button,
#ui-layer a,
#ui-layer h1 {
    pointer-events: auto;
    /* Re-enable clicks for controls */
}

/* Title */
#title {
    position: absolute;
    top: 20px;
    width: 100%;
    text-align: center;
    font-size: 2em;
    /* Adjusted roughly from 3em to fit better */
    font-weight: 900;
    text-shadow: 0 2px 10px rgba(0, 0, 0, 0.8);
    opacity: 0;
    transition: opacity 0.5s;
}

#title.hidden {
    opacity: 0;
    pointer-events: none;
}

/* Mode Toggle & Global Controls */
/* Mode Toggle & Global Controls */
#controls-container {
    position: fixed;
    top: 20px;
    top: max(20px, env(safe-area-inset-top));
    right: 20px;
    right: max(20px, env(safe-area-inset-right));
    z-index: 210;
    display: flex;
    flex-direction: column;
    align-items: flex-end;
    gap: 10px;
    opacity: 0;
    transition: opacity 0.4s ease, visibility 0.4s ease;
}

#controls-toggle {
    width: 48px;
    height: 48px;
    border-radius: 24px;
    background: rgba(0, 0, 0, 0.6);
    border: 1px solid rgba(255, 255, 255, 0.2);
    display: flex;
    align-items: center;
    justify-content: center;
    cursor: pointer;
    transition: all 0.3s ease;
    backdrop-filter: blur(5px);
    z-index: 211;
}

#controls-toggle:hover {
    background: rgba(50, 50, 50, 0.8);
    border-color: rgba(255, 255, 255, 0.5);
    transform: scale(1.05);
}

#controls-toggle svg {
    color: white;
    transition: transform 0.3s ease;
}

#controls-toggle .hidden {
    display: none;
}

#controls-list {
    display: flex;
    flex-direction: column;
    gap: 8px;
    align-items: flex-end;
    overflow: hidden;
    height: 0;
    opacity: 0;
    transform: translateY(-10px);
    transition: all 0.3s cubic-bezier(0.4, 0, 0.2, 1);
    pointer-events: none;
}

#controls-container.expanded #controls-list {
    height: auto;
    /* Fallback */
    /* We can use max-height for transition if height:auto doesn't animate well, but for small lists it's ok. 
       Actually, height:auto usually doesn't animate. Let's use opacity and transform primarily, and max-height. */
    height: auto;
    max-height: 300px;
    opacity: 1;
    transform: translateY(0);
    pointer-events: auto;
    padding-top: 5px;
    /* Spacing */
}

#controls-list button {
    width: 40px;
    height: 40px;
    border-radius: 20px;
    background: rgba(0, 0, 0, 0.5);
    border: 1px solid rgba(255, 255, 255, 0.15);
    display: flex;
    align-items: center;
    justify-content: center;
    cursor: pointer;
    transition: all 0.2s ease;
    color: white;
}

#controls-list button:hover {
    background: rgba(255, 255, 255, 0.15);
    border-color: rgba(255, 255, 255, 0.4);
    transform: translateX(-2px);
}

/* Text buttons like "3D" */
#controls-list button.text-btn {
    width: auto;
    padding: 0 12px;
    font-weight: 700;
    font-size: 0.9em;
}

/* Mobile Adjustments for Controls */
@media (max-width: 768px) {
    #controls-container {
        top: max(15px, env(safe-area-inset-top));
        right: max(15px, env(safe-area-inset-right));
    }

    #controls-toggle {
        width: 44px;
        height: 44px;
    }

    /* Ensure list handles touch well */
    #controls-list button {
        width: 44px;
        height: 44px;
        border-radius: 22px;
    }

    #controls-list button.text-btn {
        min-width: 44px;
    }
}

/* Layout Containers */
#gallery-3d {
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    z-index: 1;
    opacity: 0;
    cursor: grab;
}

#gallery-3d:active {
    cursor: grabbing;
}

#gallery-3d.hidden {
    opacity: 0;
    pointer-events: none;
}

#gallery-2d {
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    z-index: 5;
    display: grid;
    grid-template-columns: repeat(var(--col-count, 4), 1fr);
    grid-auto-rows: min-content;
    gap: 15px;
    padding: 100px 30px 30px 30px;
    box-sizing: border-box;
    overflow-y: auto;
    overflow-x: hidden;
    opacity: 0;
    pointer-events: none;
    transition: opacity 0.5s ease-in-out;
    background: black;
}

/* Solid background for 2d mode */

#gallery-2d.active {
    opacity: 1;
    pointer-events: auto;
}

/* 2D Thumbs */
.thumb {
    position: relative;
    width: 100%;
    overflow: hidden;
    border-radius: 8px;
    cursor: pointer;
    transition: transform 0.2s, box-shadow 0.2s;
    background: #111;
}

.thumb.active img {
    box-shadow: 0 0 25px #007bff;
    opacity: 1;
    transform: scale(1.05);
}

.thumb img {
    width: 100%;
    height: 100%;
    object-fit: cover;
    display: block;
    opacity: 0;
    transition: opacity 0.4s ease;
}

.thumb.loaded img {
    opacity: 1;
}

.thumb:hover {
    transform: scale(1.02);
    z-index: 2;
}

.thumb:hover img {
    opacity: 1;
    box-shadow: 0 15px 30px rgba(0, 0, 0, 0.4);
}


/* 2D Fullscreen Overlay (#imageViewer legacy refactor) */
#imageViewer {
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background: rgba(0, 0, 0, 0.95);
    z-index: 600;
    display: flex;
    justify-content: center;
    align-items: center;

    /* Animation */
    opacity: 0;
    pointer-events: none;
    transition: opacity 0.3s cubic-bezier(0.2, 0.8, 0.2, 1);
}

#imageViewer.visible {
    opacity: 1;
    pointer-events: auto;
}

#imageViewer.transparent {
    background: rgba(0, 0, 0, 0) !important;
    pointer-events: none;
    /* Allow clicks to pass through to 3D canvas */
}

#imageViewer.transparent>* {
    pointer-events: auto;
    /* Re-enable clicks for controls (children) */
}

#imageViewer.transparent #viewer-controls,
#imageViewer.transparent #viewer-controls button {
    pointer-events: auto !important;
}

/* Ensure empty container doesn't block 3D events in transparent mode */
#imageViewer.transparent #full-image-container {
    pointer-events: none;
}

#full-image-container {
    width: 100%;
    height: 100%;
    display: flex;
    justify-content: center;
    align-items: center;
    overflow: hidden;
    /* Prevent dragging outside */
    touch-action: none;
    /* Disable browser zoom/scroll handling */
    display: none;
}

#full-image-container img {
    max-width: 95%;
    max-height: 95%;
    object-fit: contain;
    box-shadow: 0 0 30px rgba(0, 0, 0, 0.5);
    transform-origin: center center;
    transition: transform 0.1s linear;
    /* Smooth pan, fast zoom */
    cursor: grab;
}

#full-image-container img.zoomed {
    cursor: grabbing;
    transition: none;
    /* No interaction lag during drag */
}

/* Viewer counter, EXIF strip, keyboard hint */
#image-counter {
    position: absolute;
    bottom: 24px;
    left: 50%;
    transform: translateX(-50%);
    color: rgba(255, 255, 255, 0.55);
    font-size: 0.75rem;
    font-family: 'Roboto', sans-serif;
    font-weight: 300;
    letter-spacing: 0.12em;
    pointer-events: none;
    user-select: none;
    z-index: 10;
    white-space: nowrap;
}

#viewer-exif-strip {
    position: absolute;
    bottom: 48px;
    left: 50%;
    transform: translateX(-50%);
    display: flex;
    gap: 12px;
    pointer-events: none;
    user-select: none;
    z-index: 10;
    white-space: nowrap;
}

#viewer-exif-strip span {
    color: rgba(255, 255, 255, 0.4);
    font-size: 0.7rem;
    font-family: 'Roboto', sans-serif;
    font-weight: 300;
    letter-spacing: 0.08em;
    background: rgba(0, 0, 0, 0.3);
    padding: 2px 6px;
    border-radius: 3px;
}

#viewer-hint {
    position: absolute;
    top: 20px;
    left: 50%;
    transform: translateX(-50%);
    color: rgba(255, 255, 255, 0.3);
    font-size: 0.65rem;
    font-family: 'Roboto', sans-serif;
    font-weight: 300;
    letter-spacing: 0.1em;
    pointer-events: none;
    user-select: none;
    z-index: 10;
    white-space: nowrap;
    transition: opacity 1s ease;
}

/* Loader */
.loader {
    border: 3px solid rgba(255, 255, 255, 0.1);
    border-radius: 50%;
    border-top: 3px solid #fff;
    width: 30px;
    height: 30px;
    animation: spin 1s linear infinite;
    position: absolute;
    top: 50%;
    left: 50%;
    margin-top: -15px;
    margin-left: -15px;
    z-index: 1;
}

@keyframes spin {
    0% {
        transform: rotate(0deg);
    }

    100% {
        transform: rotate(360deg);
    }
}

/* Metadata Overlay (Shared) */
#metadataViewer {
    position: fixed;
    bottom: 0;
    left: 0;
    width: 100%;
    background: linear-gradient(to top, rgba(0, 0, 0, 1), rgba(0, 0, 0, 0));
    padding: 20px;
    padding-bottom: 40px;
    box-sizing: border-box;
    pointer-events: none;
    transition: opacity 0.3s, transform 0.3s;
    z-index: 700;
    text-align: left;
    transform: translateY(100%);
    /* Hidden by default */
}

#metadataViewer.visible {
    transform: translateY(0);
    pointer-events: auto;
}

#metadataViewer h2 {
    font-size: 1.5em;
    margin-bottom: 5px;
}

#metadataViewer p {
    font-size: 1em;
    opacity: 0.8;
    line-height: 1.4;
}

/* Unified Viewer Controls Container */
#viewer-controls-container {
    position: absolute;
    top: 20px;
    top: max(20px, env(safe-area-inset-top));
    right: 20px;
    right: max(20px, env(safe-area-inset-right));
    display: flex;
    flex-direction: column;
    align-items: flex-end;
    gap: 10px;
    z-index: 220;
    /* Higher than main controls */
}

#viewer-controls-toggle {
    width: 48px;
    height: 48px;
    border-radius: 24px;
    background: rgba(0, 0, 0, 0.6);
    border: 1px solid rgba(255, 255, 255, 0.2);
    display: flex;
    align-items: center;
    justify-content: center;
    cursor: pointer;
    transition: all 0.3s ease;
    backdrop-filter: blur(5px);
    z-index: 221;
    color: white;
}

#viewer-controls-toggle:hover {
    background: rgba(50, 50, 50, 0.8);
    border-color: rgba(255, 255, 255, 0.5);
    transform: scale(1.05);
}

#viewer-controls-toggle svg {
    color: white;
    transition: transform 0.3s ease;
}

#viewer-controls-toggle .hidden {
    display: none;
}

#viewer-controls-list {
    display: flex;
    flex-direction: column;
    gap: 8px;
    align-items: flex-end;
    overflow: hidden;
    height: 0;
    opacity: 0;
    transform: translateY(-10px);
    transition: all 0.3s cubic-bezier(0.4, 0, 0.2, 1);
    pointer-events: none;
}

#viewer-controls-container.expanded #viewer-controls-list {
    height: auto;
    max-height: 400px;
    /* Taller list */
    opacity: 1;
    transform: translateY(0);
    pointer-events: auto;
    padding-top: 5px;
}

#viewer-controls-list button {
    width: 40px;
    height: 40px;
    border-radius: 20px;
    background: rgba(0, 0, 0, 0.5);
    border: 1px solid rgba(255, 255, 255, 0.15);
    display: flex;
    align-items: center;
    justify-content: center;
    cursor: pointer;
    transition: all 0.2s ease;
    color: white;
}

#viewer-controls-list button:hover {
    background: rgba(255, 255, 255, 0.15);
    border-color: rgba(255, 255, 255, 0.4);
    transform: translateX(-2px);
}


#toggleMetadata {
    display: flex !important;
}

/* Hover effects */
/* Managed by generic selector above */

/* Mobile Enhancements for Viewer */
@media (max-width: 768px) {
    #viewer-controls-container {
        top: 10px;
        right: 10px;
        gap: 10px;
    }

    #viewer-controls-toggle {
        width: 44px;
        height: 44px;
    }

    #viewer-controls-list button {
        width: 44px;
        /* Larger touch target relative to screen */
        height: 44px;
    }
}

/* Mobile Enhancements */
@media (max-width: 768px) {

    /* Force visible if users want to see performance */
    #fps-counter {
        display: block !important;
        background: rgba(0, 0, 0, 0.5);
        padding: 4px 8px;
        border-radius: 4px;
        top: max(10px, env(safe-area-inset-top));
        left: 10px;
    }

    /* Hide Nav Arrows on mobile (Swipe is primary) */
    .nav-arrow {
        display: none !important;
    }

    #gallery-2d {
        padding-top: 150px;
        /* Increased to accommodate Title + Centered Controls */
    }

    #gallery-2d {
        padding-top: 150px;
        /* Increased to accommodate Title + Centered Controls */
    }

    #title {
        font-size: 1.3em;
        /* Slightly larger for a clear header */
        top: max(15px, env(safe-area-inset-top));
        /* The very top */
        left: 0;
        right: 0;
        width: 100%;
        text-align: center;
        /* Center focus */
        white-space: nowrap;
        overflow: hidden;
        text-overflow: ellipsis;
        padding: 0 10px;
        pointer-events: none;
        text-shadow: 0 1px 4px rgba(0, 0, 0, 0.9);
        z-index: 10;
    }

    /* Footer adjustments */

    /* Footer adjustments */
    footer {
        bottom: 10px;
        width: 70%;
        /* Give space for Home button */
        text-align: left;
        left: 20px;
    }

    footer h2 {
        font-size: 0.7em;
    }
}

/* Light Pollution Map Overlay */
#lightPollutionMapOverlay {
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background: rgba(0, 0, 0, 0.8);
    z-index: 1000;
    display: flex;
    justify-content: center;
    align-items: center;
    opacity: 0;
    visibility: hidden;
    transition: opacity 0.3s;
}

#lightPollutionMapOverlay.visible {
    opacity: 1;
    visibility: visible;
}

#lightPollutionMapContainer {
    width: 90%;
    max-width: 1000px;
    height: 80%;
    position: relative;
    background: black;
    border-radius: 8px;
    overflow: hidden;
}

#lightPollutionMapIframe {
    width: 100%;
    height: 100%;
}

#lightPollutionMapCloseButton {
    position: absolute;
    top: 10px;
    right: 10px;
    font-size: 1.5em;
    z-index: 2;
}

/* Nav Arrows */
.nav-arrow {
    position: fixed;
    top: 50%;
    transform: translateY(-50%);
    font-size: 4em;
    opacity: 0.3;
    transition: opacity 0.2s;
    z-index: 210;
    /* Above viewer (50) and controls (200) */
}

.nav-arrow:hover {
    opacity: 0.8;
}

#leftArrow {
    left: 20px;
}

#rightArrow {
    right: 20px;
}

/* Footer */
footer {
    position: fixed;
    bottom: 20px;
    left: 0;
    width: 100%;
    text-align: center;
    z-index: 15;
    pointer-events: none;
    transition: opacity 0.4s ease, visibility 0.4s ease;
}

footer.hidden,
body:has(#imageViewer.visible) footer,
body:has(#immersive-frame:not(.hidden)) footer {
    opacity: 0 !important;
    visibility: hidden !important;
    pointer-events: none !important;
}

footer h2 {
    font-size: 0.9em;
    opacity: 0.5;
    pointer-events: auto;
    display: inline-block;
}

footer a {
    pointer-events: auto;
    color: white;
    /* Or specific brand color */
    text-decoration: underline;
}

/* Controls UI (Close, Fullscreen) */
/* Controls moved to Unified Viewer Controls section above */

/* Loading Screen */
#loading-screen {
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background: black;
    z-index: 9999;
    display: flex;
    flex-direction: column;
    justify-content: center;
    align-items: center;
}

#loading-screen.hidden {
    opacity: 0;
    pointer-events: none;
}

#loading-text {
    margin-top: 20px;
    font-size: 1.2em;
    letter-spacing: 1px;
    color: #888;
}

#loading-bar-container {
    width: 300px;
    height: 4px;
    background: #333;
    margin-top: 20px;
    border-radius: 2px;
    overflow: hidden;
}

#loading-bar {
    width: 0%;
    height: 100%;
    background: white;
    transition: width 0.1s linear;
}

.spinner {
    width: 40px;
    height: 40px;
    border: 3px solid rgba(255, 255, 255, 0.1);
    border-top: 3px solid white;
    border-radius: 50%;
    animation: spin 1s linear infinite;
}

/* --- Settings Modal --- */
#settingsModal {
    position: fixed;
    top: 0;
    left: 0;
    width: 100vw;
    height: 100vh;
    background: rgba(0, 0, 0, 0.6);
    backdrop-filter: blur(5px);
    z-index: 300;
    display: flex;
    justify-content: center;
    align-items: center;
    opacity: 0;
    pointer-events: none;
    transition: opacity 0.3s ease;
}

#settingsModal.visible {
    opacity: 1;
    pointer-events: auto;
}

.settings-content {
    background: rgba(20, 20, 20, 0.9);
    border: 1px solid rgba(255, 255, 255, 0.1);
    box-shadow: 0 0 30px rgba(0, 0, 0, 0.8);
    border-radius: 12px;
    width: 90%;
    max-width: 400px;
    padding: 20px;
    color: white;
}

.settings-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-bottom: 20px;
    border-bottom: 1px solid rgba(255, 255, 255, 0.1);
    padding-bottom: 10px;
}

.settings-header h2 {
    margin: 0;
    font-size: 1.2rem;
}

.setting-group {
    margin-bottom: 25px;
}

.setting-group h3 {
    font-size: 0.9rem;
    text-transform: uppercase;
    color: #888;
    margin-bottom: 15px;
}

label {
    display: flex;
    align-items: center;
    justify-content: space-between;
    margin-bottom: 15px;
    gap: 10px;
}

label span:first-child {
    font-size: 0.9rem;
    flex: 1;
}

.val-display {
    width: 50px;
    text-align: right;
    font-size: 0.85rem;
    color: #4facfe;
    font-family: monospace;
}

input[type="range"] {
    flex: 2;
    -webkit-appearance: none;
    appearance: none;
    /* Added appearance property */
    background: rgba(255, 255, 255, 0.1);
    height: 4px;
    border-radius: 2px;
}

input[type="range"]::-webkit-slider-thumb {
    -webkit-appearance: none;
    width: 16px;
    height: 16px;
    background: #4facfe;
    border-radius: 50%;
    cursor: pointer;
    transition: transform 0.1s;
}

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


/* Immersive Iframe */
#immersive-frame {
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    border: none;
    z-index: 100;
    /* Above all */
    background: black;
    display: none;
    /* transition: opacity 0.3s ease; (Optional) */
}

#immersive-frame.hidden {
    display: none !important;
}

.settings-footer {
    display: flex;
    justify-content: center;
    margin-top: 20px;
    border-top: 1px solid rgba(255, 255, 255, 0.1);
    padding-top: 15px;
}

/* Utility for fading out controls */
.fade-out {
    opacity: 0 !important;
    pointer-events: none !important;
    transition: opacity 0.5s ease;
}

.btn-danger {
    background: rgba(255, 60, 60, 0.2);
    border: 1px solid rgba(255, 60, 60, 0.5);
    color: #ff6b6b;
    padding: 8px 16px;
    border-radius: 6px;
    font-size: 0.9rem;
    cursor: pointer;
    transition: all 0.2s;
}

.btn-danger:hover {
    background: rgba(255, 60, 60, 0.4);
}

#closeSettings {
    background: none;
    border: none;
    color: white;
    font-size: 1.2rem;
    cursor: pointer;
}

/* License Overlay (Matches Light Pollution Overlay) */
#licenseOverlay {
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background: rgba(0, 0, 0, 0.8);
    z-index: 1000;
    display: flex;
    justify-content: center;
    align-items: center;
    opacity: 0;
    visibility: hidden;
    transition: opacity 0.3s;
}

#licenseOverlay.visible {
    opacity: 1;
    visibility: visible;
    pointer-events: auto;
}

#licenseContainer {
    width: 90%;
    max-width: 800px;
    height: 80%;
    position: relative;
    background: #000;
    border-radius: 8px;
    display: flex;
    flex-direction: column;
    overflow: hidden;
    border: 1px solid #333;
    box-shadow: 0 0 50px rgba(0, 0, 0, 0.8);
}

.license-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 15px 20px;
    background: #111;
    border-bottom: 1px solid #222;
}

.license-header h2 {
    margin: 0;
    font-size: 1.2rem;
    color: #fff;
    font-weight: 400;
}

#closeLicense {
    background: transparent;
    border: none;
    color: #888;
    font-size: 1.2rem;
    cursor: pointer;
    transition: color 0.2s;
    padding: 0;
}

#closeLicense:hover {
    color: #fff;
}

#licenseIframe {
    flex: 1;
    width: 100%;
    border: none;
    background: #000;
}