:root {
    --bg-dark: #111827;
    --bg-panel: #1f2937;
    --text-primary: #f3f4f6;
    --text-secondary: #9ca3af;
    --accent: #3b82f6;
    --accent-hover: #2563eb;
    --border: #374151;
}

body {
    margin: 0;
    font-family: 'Inter', sans-serif;
    background-color: var(--bg-dark);
    color: var(--text-primary);
    height: 100vh;
    overflow: hidden;
}

.app-container {
    display: grid;
    grid-template-columns: 230px 1fr;
    height: 100%;
}

/* Sidebar */
.sidebar {
    box-sizing: border-box;
    background-color: var(--bg-panel);
    border-right: 1px solid var(--border);
    padding: 1rem;
    padding-bottom: 2rem;
    /* Add extra room at the bottom for safety */
    display: flex;
    flex-direction: column;
    gap: 1rem;
    overflow-y: auto;
}

.logo h2 {
    margin: 0;
    font-size: 1.5rem;
    font-weight: 700;
    color: var(--accent);
}

.subtitle {
    font-size: 0.75rem;
    color: var(--text-secondary);
    margin-top: 0.25rem;
}

.control-group {
    display: flex;
    flex-direction: column;
    gap: 0.5rem;
}

.control-group.hidden {
    display: none;
}

.control-group label {
    font-size: 0.875rem;
    font-weight: 500;
    color: var(--text-secondary);
}

select {
    background-color: var(--bg-dark);
    border: 1px solid var(--border);
    color: var(--text-primary);
    padding: 0;
    border-radius: 0.5rem;
    font-size: 0.9rem;
    outline: none;
    cursor: pointer;
    width: 100%;
}

select:focus {
    border-color: var(--accent);
}

/* Checkbox List */
.checkbox-list {
    display: flex;
    flex-direction: column;
    gap: 0.5rem;
    background: var(--bg-dark);
    padding: 0.5rem;
    border-radius: 0.5rem;
    border: 1px solid var(--border);
}

.checkbox-list label {
    display: flex;
    align-items: center;
    gap: 0.5rem;
    cursor: pointer;
    color: var(--text-primary);
    font-weight: 400;
}

/* Toggle Buttons (Restored) */
.toggle-group {
    display: flex;
    background-color: var(--bg-dark);
    border-radius: 0.5rem;
    padding: 0;
    border: 1px solid var(--border);
}

.toggle-group button {
    flex: 1;
    background: transparent;
    border: none;
    color: var(--text-secondary);
    padding: 0;
    border-radius: 0.25rem;
    cursor: pointer;
    font-size: 0.85rem;
    transition: all 0.2s;
}

.toggle-group button.active {
    background-color: var(--accent);
    color: white;
    font-weight: 600;
}

/* Radio Group */
.radio-group {
    display: flex;
    flex-direction: column;
    gap: 0.5rem;
    background-color: var(--bg-dark);
    border-radius: 0.5rem;
    padding: 0.5rem;
    border: 1px solid var(--border);
}

.radio-item {
    display: flex;
    align-items: center;
    gap: 0.5rem;
}

.radio-item input[type="radio"] {
    accent-color: var(--accent);
    cursor: pointer;
}

.radio-item label {
    cursor: pointer;
    color: var(--text-primary);
    font-size: 0.9rem;
}

/* Viewer Area */
.viewer {
    display: flex;
    flex-direction: column;
    position: relative;
    background-color: #000;
    overflow-y: auto;
    overflow-x: hidden;
}

.image-container {
    /* flex: 1;  Removed to allow stacking */
    display: flex;
    align-items: center;
    justify-content: center;
    /* overflow: hidden; Removed to allow sticky labels if needed, but safe to keep */
    position: relative;
    width: 100%;
    /* Min height removed to allow content to define height */
    /* min-height: 60vh; */
    /* Width Control */
    max-width: 1200px;
    margin: 0 auto;

    flex-shrink: 0;
}

/* Map Layers */
.map-layer {
    position: absolute;
    top: 0;
    left: 0;
    /* transform: translate(-50%, -50%); Removed */
    width: 100%;
    height: 100%;
    object-fit: contain;
    transition: opacity 0.3s;
}

/* Base Tyer (Terrain) determines size */
#img-terrain {
    position: relative;
    width: 100%;
    height: auto;
    display: block;
}

.map-layer.hidden {
    opacity: 0;
    visibility: hidden;
}

/* Z-Indices */
.z-10 {
    z-index: 10;
}

/* Terrain */
.z-20 {
    z-index: 20;
}

/* Variable */
.z-30 {
    z-index: 30;
}

/* Static Overlays (Rivers, CCAA, Roads) */
.z-40 {
    z-index: 40;
}

/* Clouds/Rain */
.z-50 {
    z-index: 50;
}

/* Labels */
.z-60 {
    z-index: 60;
}

/* Fullscreen overrides (Sounding/Meteogram) */

/* Loader */
.loader {
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
    /* Center perfectly */
    width: 48px;
    height: 48px;
    border: 5px solid rgba(255, 255, 255, 0.3);
    border-radius: 50%;
    border-top-color: var(--accent);
    animation: spin 1s ease-in-out infinite;
    display: none;
    z-index: 100;
}

.loader.active {
    display: block;
}

@keyframes spin {
    to {
        transform: rotate(360deg);
    }
}

/* Scale Bar */
.scale-container {
    background: rgba(0, 0, 0, 0.5);
    padding: 5px;
    display: flex;
    justify-content: center;
    border-top: 1px solid #333;
    width: 100%;
    box-sizing: border-box;
}

.scale-container.hidden {
    display: none;
}

#img-scale {
    max-width: 100%;
    width: auto;
    height: auto;
    /* Quitamos los 30px rígidos, damos hasta 800px de amplitud real y escalado de la barra según alto viewport */
    max-height: 8vh;
    min-height: 30px;
    background-color: white;
    padding: 3px;
    border-radius: 4px;
    object-fit: contain;
}

/* Data Layers Transparency */
#img-variable,
.map-layer.z-40 {
    opacity: 0.7;
    mix-blend-mode: multiply;
    /* Optional: might improve visibility of terrain */
}

/* Timeline Controls */
.timeline-controls {
    background-color: rgba(31, 41, 55, 0.95);
    padding: 0.25rem 1rem;
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 1rem;
    border-bottom: 1px solid var(--border);
    position: sticky;
    top: 0;
    /* Ensure z-index works and stays above map layers while scrolling */
    z-index: 1000;
}

button#prev-btn,
button#next-btn {
    background: var(--bg-panel);
    border: 1px solid var(--border);
    color: var(--text-primary);
    width: 32px;
    height: 32px;
    border-radius: 50%;
    display: flex;
    align-items: center;
    justify-content: center;
    cursor: pointer;
    font-size: 1rem;
    transition: background 0.2s;
}

button:hover {
    background-color: var(--border);
}

.timeline-date {
    background-color: transparent;
    border: none;
    color: var(--text-primary);
    font-size: 1.1rem;
    font-weight: 500;
    padding: 0.1rem;
    cursor: pointer;
    outline: none;
    appearance: none;
    /* Remove default arrow in some browsers if desired, or keep */
    /* If keeping arrow, maybe style it white? browsers are tricky with select arrows */
    -moz-appearance: none;
    -webkit-appearance: none;
    text-align: center;
    width: auto;
    max-width: 120px;
}

.timeline-date:hover {
    background-color: rgba(255, 255, 255, 0.1);
    border-radius: 4px;
}

.time-display {
    color: var(--text-primary);
    font-weight: 600;
    font-size: 1.1rem;
    min-width: 60px;
    text-align: center;
    font-variant-numeric: tabular-nums;
}

.slider-container {
    flex: 1;
    display: flex;
    flex-direction: column;
    gap: 0.5rem;
}

input[type=range] {
    width: 100%;
    accent-color: var(--accent);
    cursor: pointer;
}

.time-labels {
    display: flex;
    justify-content: space-between;
    font-size: 0.85rem;
    color: var(--text-secondary);
    font-variant-numeric: tabular-nums;
}

.status-panel {
    margin-top: auto;
    font-size: 0.75rem;
    color: var(--text-secondary);
    color: var(--text-secondary);
    word-break: break-all;
}

.hidden {
    display: none !important;
}

/* Overlay Views (Sounding/Meteogram) - Now Stacked */
.overlay-container {
    /* flex: 1; Removed */
    z-index: 200;
    background-color: var(--bg-dark);
    display: flex;
    flex-direction: column;
    /* Stack images if multiple */
    align-items: center;
    justify-content: center;
    padding: 1rem;
    gap: 1rem;

    max-width: 1200px;
    margin: 0 auto;
}

.overlay-image {
    max-width: 100%;
    height: auto;
    object-fit: contain;
    background-color: white;
    border-radius: 4px;
}

#img-sounding {
    margin-bottom: 2rem;
}

/* Mobile Responsive */
.menu-toggle {
    display: none;
    position: fixed;
    /* Adjusted top */
    left: 1rem;
    z-index: 1100;
    /* Above timeline controls (1000) */
    background: rgba(31, 41, 55, 0.9);
    color: white;
    border: 1px solid var(--border);
    border-radius: 4px;
    width: 40px;
    font-size: 1.5rem;
    cursor: pointer;
    align-items: center;
    justify-content: center;
}

.sidebar-overlay {
    display: none;
}

@media (max-width: 1024px) {
    .app-container {
        grid-template-columns: 1fr;
        display: block;
        position: relative;
        overflow-y: auto;
        /* Allow scrolling */
    }

    .sidebar {
        position: fixed;
        top: 0;
        left: -100%;
        /* Hidden by default */
        width: 70%;
        max-width: 200px;
        height: 100%;
        /* fallback for older browsers */
        height: 100dvh;
        /* prevents 100vh address bar issue */
        padding-bottom: 5rem;
        /* Ensure space for safe scrolling to the very last buttons on mobile */
        z-index: 1001;
        /* Higher than overlay */
        transition: left 0.3s ease-in-out;
        box-shadow: 2px 0 10px rgba(0, 0, 0, 0.5);
    }

    .sidebar.open {
        left: 0;
    }

    .viewer {
        height: auto;
        /* Remove fixed height */
        min-height: 100vh;
        /* Ensure at least full screen */
        width: 100%;
        display: flex;
        flex-direction: column;
        overflow: visible;
        /* Permite que el position: sticky del hijo salga hacia app-container */
    }

    .image-container {
        /* Remove flex: 1 to let content drive height */
        flex: 0 0 auto;
        display: block;
        position: relative;
        height: auto;

        max-width: none;
        margin: 0;
    }

    /* Base Layer (Terrain) defines height */
    #img-terrain {
        position: relative;
        top: auto;
        left: auto;
        transform: none;
        width: 100%;
        height: auto;
        display: block;
        /* Remove inline gap */
    }

    /* Overlays follow base layer's dimensions */
    .map-layer:not(#img-terrain) {
        position: absolute;
        top: 0;
        left: 0;
        width: 100%;
        height: 100%;
        transform: none;
    }

    .menu-toggle {
        display: flex;
    }

    /* Add overlay when sidebar open */
    .sidebar-overlay {
        display: none;
        position: fixed;
        top: 0;
        left: 0;
        width: 100%;
        height: 100vh;
        background: rgba(0, 0, 0, 0.5);
        z-index: 1000;
        backdrop-filter: blur(2px);
    }

    .sidebar.open~.sidebar-overlay {
        display: block;
    }

    /* Sounding/Meteogram Mobile Fix */
    .overlay-container {
        flex: 0 0 auto;
        height: auto;
        padding: 1rem;
        display: block;

        max-width: none;
        margin: 0;
    }

    .overlay-image {
        max-width: 100%;
        max-height: none;
        height: auto;
        display: block;
        margin: 0 auto;
    }

    #img-sounding {
        margin-bottom: 2rem;
    }

    /* Timeline Controls is already styled sticky globally, just ensuring width here */
    .timeline-controls {
        left: 0;
        width: 100%;
    }
}