: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;
    height: 100dvh;
    overflow: hidden;
}

/* Strict Viewport Lock: Freeze background when modal is open */
html.has-modal,
body.has-modal {
    overflow: hidden !important;
    position: fixed !important;
    width: 100% !important;
    height: 100% !important;
    /* This prevents any internal element from pushing the document width */
}

.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 {
    position: relative;
    flex-grow: 1;
    display: flex;
    flex-direction: column;
    height: 100vh;
    height: 100dvh;
    overflow: hidden;
    background-color: #000;
}

.map-viewer {
    flex-grow: 1;
    width: 100%;
    height: 100%;
    z-index: 10;
    background: #000;
    overflow: hidden;
}

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

.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 {
    position: absolute;
    bottom: 25px;
    left: 50%;
    transform: translateX(-50%);
    width: auto;
    background: transparent;
    border: none;
    z-index: 1000;
    pointer-events: none;
}

.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;
}

/* Dynamic Scale */
.dynamic-scale {
    display: flex;
    flex-direction: row;
    align-items: center;
    gap: 8px;
    padding: 6px 10px;
    background: rgba(31, 41, 55, 0.85);
    border-radius: 8px;
    backdrop-filter: blur(8px);
    border: 1px solid rgba(255, 255, 255, 0.1);
    box-shadow: 0 4px 12px rgba(0, 0, 0, 0.3);
    pointer-events: auto;
}

.scale-header {
    min-width: 30px;
    margin-right: 0;
    text-align: left;
}

.scale-unit {
    color: var(--text-secondary);
    font-size: 0.65rem;
    font-weight: 800;
}

.scale-body {
    display: flex;
    align-items: center;
    width: 350px;
    max-width: 75vw;
}

.scale-gradient-container {
    height: 24px;
    width: 100%;
    background: #111;
    border-radius: 12px;
    padding: 2px;
    box-sizing: border-box;
    border: 1px solid rgba(255, 255, 255, 0.5);
    overflow: hidden;
}

.scale-gradient {
    width: 100%;
    height: 100%;
    border-radius: 10px;
    position: relative;
}

.scale-labels-overlay {
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    pointer-events: none;
}

.scale-labels-overlay span {
    position: absolute;
    top: 50%;
    transform: translate(-50%, -50%);
    color: #fff;
    font-size: 0.5rem;
    font-weight: 800;
    text-shadow: 0 0 3px #000, 1px 1px 2px #000;
    z-index: 10;
}

/* Data Layers Transparency */
#img-variable {
    opacity: 0.7;
    mix-blend-mode: multiply;
}

#wind-particles {
    pointer-events: none;
    mix-blend-mode: normal;
    opacity: 1.0;
    z-index: 1000;
    /* Over Leaflet tiles */
}

/* 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: 0.5rem;
    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: 0.8rem;
    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: 0.85rem;
    min-width: 50px;
    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) - Modal at Root */
.overlay-container {
    position: fixed;
    inset: 0;
    z-index: 10000;
    background-color: rgba(15, 23, 42, 0.95);
    display: block;
    backdrop-filter: blur(8px);
    overflow: hidden !important;
    contain: layout size;
    /* Strict isolation */
}

.modal-close-btn {
    position: absolute;
    top: 2rem;
    right: 2rem;
    background: rgba(255, 255, 255, 0.1);
    border: none;
    color: white;
    font-size: 1.5rem;
    width: 40px;
    height: 40px;
    border-radius: 50%;
    cursor: pointer;
    z-index: 10000;
    transition: background 0.2s ease;
    display: flex;
    align-items: center;
    justify-content: center;
}

.modal-close-btn:hover {
    background: rgba(255, 255, 255, 0.25);
}

.modal-content {
    width: 100%;
    height: 100%;
    overflow: auto;
    /* Internal scroll area only */
    padding: 6rem 2rem 4rem 2rem;
    display: block;
    /* Better for massive overflows than flex */
    text-align: center;
    /* Center images horizontally */
    box-sizing: border-box;
}

.modal-content>* {
    display: block;
    margin-left: auto;
    margin-right: auto;
}

.overlay-image {
    max-width: 100%;
    height: auto;
    object-fit: contain;
    background-color: white;
    border-radius: 8px;
    box-shadow: 0 10px 25px rgba(0, 0, 0, 0.5);
    transition: max-width 0.3s ease;
    margin-bottom: 3rem;
    /* Spacing in block layout */
}

.overlay-image.zoomable {
    cursor: zoom-in;
}

.overlay-image.expanded {
    max-width: none;
    cursor: zoom-out;
    margin: 0 auto;
    /* Ensure it stays centered if smaller than screen */
    display: block;
    /* Safer for large dimensions */
}

/* Mobile Responsive */
.menu-toggle {
    display: none;
    position: fixed;
    /* Adjusted top */
    left: 4px;
    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;
        height: 100vh;
        height: 100dvh;
        overflow: hidden;
    }

    .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: 100vh;
        height: 100dvh;
        width: 100%;
        display: flex;
        flex-direction: column;
        overflow: hidden;
    }

    .map-viewer {
        height: 100%;
        width: 100%;
        flex-grow: 1;
    }

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

    .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 */
    .modal-content {
        padding: 4rem 1rem 1rem 1rem;
        gap: 1rem;
    }

    .modal-close-btn {
        top: 1rem;
        right: 1rem;
        width: 32px;
        height: 32px;
        font-size: 1.2rem;
    }

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

/* Wind Tooltip Styles */
.wind-tooltip {
    position: absolute;
    z-index: 1000;
    background: rgba(0, 0, 0, 0.85);
    backdrop-filter: blur(4px);
    border: 1px solid var(--border);
    border-radius: 6px;
    padding: 3px 6px;
    color: white;
    font-family: 'Inter', sans-serif;
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 2px;
    pointer-events: none;
    box-shadow: 0 4px 12px rgba(0, 0, 0, 0.5);
    transform: translate(-50%, calc(-100% - 6px));
}

.wind-tooltip.hidden {
    display: none;
}

.wt-name {
    font-size: 0.55rem;
    color: var(--text-secondary);
    text-transform: uppercase;
    font-weight: 700;
    letter-spacing: 0.05em;
    margin-bottom: -2px;
}

.wt-val {
    font-size: 0.85rem;
    font-weight: 800;
    white-space: nowrap;
    color: var(--text-primary);
}


.wt-unit {
    font-size: 0.55rem;
    color: var(--text-secondary);
    font-weight: 500;
}

.wt-dir {
    font-size: 0.65rem;
    display: flex;
    align-items: center;
    gap: 4px;
    color: var(--text-secondary);
}

#wt-dir-arrow {
    display: inline-block;
    transition: transform 0.2s ease-out;
    font-weight: 700;
    color: var(--accent);
}

/* Leaflet Labels Overrides */
.leaflet-tooltip.label-city {
    background: transparent;
    border: none;
    box-shadow: none;
    color: #fff;
    font-weight: 600;
    text-shadow: 0 0 4px #000;
    font-size: 0.75rem;
}

.leaflet-tooltip.label-takeoff {
    background: rgba(31, 41, 55, 0.8);
    border: 1px solid var(--accent);
    color: #fff;
    border-radius: 4px;
    font-size: 0.7rem;
    font-weight: 600;
}

.leaflet-tooltip.label-sounding {
    background: rgba(43, 85, 255, 0.85);
    border: 1px solid #fff;
    color: #fff;
    border-radius: 4px;
    font-size: 0.75rem;
    font-weight: 700;
    box-shadow: 0 2px 8px rgba(0, 0, 0, 0.3);
}

.label-sounding::before {
    border-top-color: rgba(43, 85, 255, 0.85) !important;
}


.leaflet-layer.z-particles {
    z-index: 500;
}

/* Leaflet Controls Resizing */
.leaflet-bar a,
.leaflet-bar a:hover {
    width: 22px !important;
    height: 22px !important;
    line-height: 22px !important;
    font-size: 12px !important;
}

.leaflet-control-layers-toggle {
    width: 26px !important;
    height: 26px !important;
}

.leaflet-control-zoom {
    margin-top: 10px !important;
}

/* Sounding Popup Styles */
.custom-sounding-popup .leaflet-popup-content-wrapper {
    background: var(--bg-panel);
    color: var(--text-primary);
    border-radius: 8px;
    padding: 8px;
    box-shadow: 0 4px 15px rgba(0, 0, 0, 0.4);
    border: 1px solid var(--border);
}

.custom-sounding-popup .leaflet-popup-content {
    margin: 8px 12px;
}

.custom-sounding-popup .leaflet-popup-tip {
    background: var(--bg-panel);
}

.sounding-popup {
    text-align: center;
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 8px;
    min-width: 120px;
}

.sounding-popup strong {
    font-size: 0.95rem;
    color: var(--accent);
    display: block;
    margin-bottom: 4px;
}

.popup-btn {
    background-color: var(--accent);
    color: white;
    border: none;
    padding: 6px 12px;
    border-radius: 6px;
    font-size: 0.8rem;
    font-weight: 600;
    cursor: pointer;
    transition: background-color 0.2s;
    width: 100%;
}

.popup-btn:hover {
    background-color: var(--accent-hover);
}

.wt-station-name {
    font-weight: 800;
    color: var(--accent);
    margin-bottom: 2px;
    border-bottom: 1px solid var(--border);
    padding-bottom: 2px;
}

.popup-data {
    margin: 4px 0 8px 0;
    font-size: 0.9rem;
    color: var(--text-secondary);
}

.popup-data strong {
    color: var(--text-primary);
}

.popup-dir {
    font-weight: bold;
    color: var(--accent);
    margin-left: 4px;
}