:root {
    color-scheme: light;
    --bg: #08111d;
    --panel: rgba(9, 20, 34, 0.9);
    --panel-strong: rgba(7, 15, 27, 0.96);
    --panel-border: rgba(91, 140, 182, 0.22);
    --text: #eff6ff;
    --muted: #aac0d5;
    --accent: #4db0ff;
    --accent-strong: #1e88e5;
    --accent-soft: rgba(77, 176, 255, 0.14);
    --shadow: 0 18px 40px rgba(1, 8, 16, 0.4);
    --radius: 18px;
    --font-body: "Segoe UI", "Aptos", "Helvetica Neue", sans-serif;
    --font-display: "Georgia", "Times New Roman", serif;
}

* {
    box-sizing: border-box;
}

html,
body,
#app-shell,
#map {
    height: 100%;
    margin: 0;
}

body {
    font-family: var(--font-body);
    color: var(--text);
    background:
        radial-gradient(circle at top left, rgba(30, 136, 229, 0.22), transparent 30%),
        radial-gradient(circle at bottom right, rgba(77, 176, 255, 0.16), transparent 25%),
        var(--bg);
}

#app-shell {
    position: relative;
}

#map {
    z-index: 1;
    position: relative;
}

.app-header {
    position: absolute;
    z-index: 900;
    backdrop-filter: blur(18px);
    background: var(--panel);
    border: 1px solid var(--panel-border);
    box-shadow: var(--shadow);
}

.app-header {
    top: 18px;
    left: 18px;
    width: min(520px, calc(100vw - 96px));
    padding: 12px 16px;
    border-radius: var(--radius);
}

.header-copy {
    max-width: 100%;
}

.app-header h1 {
    margin: 0;
    font-family: var(--font-display);
    font-size: clamp(1.15rem, 1.7vw, 1.75rem);
    font-weight: 600;
    line-height: 1.05;
}

.eyebrow {
    margin: 0 0 4px;
    font-size: 0.68rem;
    text-transform: uppercase;
    letter-spacing: 0.12em;
    color: var(--accent);
}

.left-sidebar-stack {
    position: absolute;
    z-index: 900;
    top: 146px;
    left: 18px;
    width: min(320px, calc(100vw - 36px));
    display: flex;
    flex-direction: column;
    gap: 18px;
}

.control-panel {
    position: relative;
    width: 100%;
    height: auto;
    max-height: calc(100vh - 164px);
    overflow-x: hidden;
    overflow-y: auto;
    padding: 14px;
    border-radius: var(--radius);
    transition: opacity 140ms ease, transform 140ms ease;
    backdrop-filter: blur(18px);
    background: var(--panel);
    border: 1px solid var(--panel-border);
    box-shadow: var(--shadow);
}

.control-panel.is-collapsed {
    display: none;
}

.control-panel-body {
    display: block;
}

.control-panel.is-collapsed .control-panel-body {
    display: none;
}

.panel-toolbar {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 10px;
    position: sticky;
    top: -14px;
    z-index: 2;
    margin: -14px -14px 0;
    padding: 14px;
    background: linear-gradient(180deg, rgba(7, 15, 27, 0.98), rgba(9, 20, 34, 0.94));
    border-bottom: 1px solid rgba(91, 140, 182, 0.16);
}

.panel-title {
    margin: 0;
}

.toolbar-actions {
    display: flex;
    gap: 8px;
}

.control-panel section+section {
    margin-top: 18px;
    padding-top: 18px;
    border-top: 1px solid rgba(91, 140, 182, 0.16);
}

.control-panel h2 {
    margin: 0 0 10px;
    font-size: 0.95rem;
}

.panel-copy,
.microcopy {
    margin: 0;
    color: var(--muted);
    line-height: 1.5;
}

.microcopy {
    margin-top: 8px;
    font-size: 0.9rem;
}

label {
    display: block;
    margin-bottom: 8px;
    font-weight: 600;
}

select,
input,
button {
    font: inherit;
}

select,
input {
    width: 100%;
    padding: 10px 12px;
    border-radius: 12px;
    border: 1px solid rgba(108, 145, 182, 0.22);
    background: rgba(6, 15, 27, 0.92);
    color: var(--text);
}

input::placeholder {
    color: #88a2b8;
}

button {
    border: 0;
    border-radius: 12px;
    padding: 10px 14px;
    background: linear-gradient(135deg, var(--accent), var(--accent-strong));
    color: #fff;
    cursor: pointer;
    transition:
        transform 120ms ease,
        opacity 120ms ease,
        background 120ms ease;
}

button:hover {
    opacity: 0.95;
    transform: translateY(-1px);
}

.ghost-button {
    background: rgba(255, 255, 255, 0.08);
    color: var(--text);
    border: 1px solid rgba(108, 145, 182, 0.18);
}

.compact-button {
    padding: 8px 12px;
    font-size: 0.9rem;
}

#panel-launch-button {
    position: absolute;
    top: 146px;
    left: 18px;
    z-index: 901;
    width: 46px;
    min-width: 46px;
    height: 46px;
    padding: 0;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    font-size: 1.1rem;
    line-height: 1;
    border-radius: 14px;
    background: rgba(7, 15, 27, 0.96);
    border: 1px solid rgba(108, 145, 182, 0.28);
    box-shadow: 0 12px 28px rgba(1, 8, 16, 0.42);
    opacity: 1;
}

#panel-launch-button[hidden] {
    display: none;
}

#panel-launch-button:hover {
    background: rgba(20, 40, 65, 0.98);
}

.search-box {
    position: relative;
}

.search-results {
    position: absolute;
    bottom: calc(100% + 8px);
    left: 0;
    right: 0;
    z-index: 20;
    display: grid;
    gap: 6px;
    padding: 8px;
    width: auto;
    max-height: min(30vh, 220px);
    overflow-y: auto;
    overscroll-behavior: contain;
    border-radius: 14px;
    border: 1px solid rgba(108, 145, 182, 0.22);
    background: rgba(7, 15, 27, 0.96);
    box-shadow: 0 16px 32px rgba(1, 8, 16, 0.35);
}

.search-results[hidden] {
    display: none;
}

.search-result {
    width: 100%;
    padding: 10px 12px;
    border-radius: 10px;
    border: 0;
    background: rgba(255, 255, 255, 0.04);
    color: var(--text);
    text-align: left;
    transform: none;
}

.search-result:hover,
.search-result.is-active {
    background: rgba(77, 176, 255, 0.16);
    opacity: 1;
}

.search-result-title {
    display: block;
    font-weight: 600;
}

.search-result-meta {
    display: block;
    margin-top: 4px;
    color: var(--muted);
    font-size: 0.82rem;
}

.legend-header {
    display: flex;
    align-items: center;
    justify-content: flex-start;
    gap: 10px;
}

.legend-panel {
    position: absolute;
    left: 50%;
    bottom: 18px;
    transform: translateX(-50%);
    z-index: 900;
    width: min(372px, calc(100vw - 36px));
    max-height: min(26vh, 230px);
    overflow-y: auto;
    padding: 14px 16px;
    border-radius: var(--radius);
    backdrop-filter: blur(18px);
    background: var(--panel);
    border: 1px solid var(--panel-border);
    box-shadow: var(--shadow);
}

.legend-panel h2 {
    margin: 0;
    font-size: 0.95rem;
}

.legend-panel .legend-header {
    margin-bottom: 10px;
}

.scenario-chip {
    display: inline-flex;
    align-items: center;
    padding: 4px 10px;
    border-radius: 999px;
    background: var(--accent-soft);
    color: var(--accent);
    font-size: 0.85rem;
    font-weight: 700;
}

.inline-toggle {
    display: flex;
    align-items: center;
    gap: 10px;
    margin-bottom: 12px;
}

.inline-toggle input {
    width: auto;
    margin: 0;
}

.range-row {
    display: grid;
    grid-template-columns: 1fr auto;
    gap: 10px;
    align-items: center;
}

output {
    color: var(--accent);
    font-weight: 700;
    min-width: 44px;
    text-align: right;
}

.legend {
    column-count: 2;
    column-gap: 10px;
}

.legend-copy {
    margin-bottom: 6px;
    break-inside: avoid;
}

.legend-item {
    display: flex;
    align-items: center;
    gap: 8px;
    font-size: 0.88rem;
    min-width: 0;
    break-inside: avoid;
    margin-bottom: 8px;
    white-space: nowrap;
}

.legend-swatch {
    width: 18px;
    height: 18px;
    border-radius: 5px;
    border: 1px solid rgba(189, 215, 238, 0.24);
    flex: 0 0 auto;
}

/* --------------------------------------------------------------------------
   Loading overlay
   -------------------------------------------------------------------------- */

.loading-overlay {
    position: absolute;
    inset: 0;
    z-index: 950;
    display: grid;
    place-items: center;
    pointer-events: none;
}

.loading-overlay[hidden] {
    display: none;
}

.loading-card {
    display: flex;
    align-items: center;
    gap: 14px;
    min-width: min(360px, calc(100vw - 48px));
    max-width: calc(100vw - 48px);
    padding: 18px 20px;
    border-radius: 18px;
    border: 1px solid rgba(91, 140, 182, 0.24);
    background: rgba(7, 15, 27, 0.9);
    box-shadow: 0 18px 40px rgba(1, 8, 16, 0.45);
    backdrop-filter: blur(20px);
    color: var(--text);
}

.loading-message {
    margin: 0;
    color: var(--muted);
    line-height: 1.5;
}

.loading-spinner {
    width: 22px;
    height: 22px;
    border-radius: 50%;
    border: 3px solid rgba(255, 255, 255, 0.18);
    border-top-color: var(--accent);
    flex: 0 0 auto;
    animation: status-spin 0.8s linear infinite;
}

.loading-overlay[data-tone="error"] .loading-card {
    border-color: rgba(185, 28, 28, 0.32);
    background: rgba(56, 14, 14, 0.9);
}

.loading-overlay[data-tone="error"] .loading-message {
    color: #ffb4b4;
}

.loading-overlay[data-tone="error"] .loading-spinner {
    display: none;
}

.loading-overlay[data-tone="error"] {
    pointer-events: auto;
}

.status-dismiss {
    display: none;
    margin-left: auto;
    flex: 0 0 auto;
    background: none;
    border: none;
    color: #ffb4b4;
    font-size: 1rem;
    cursor: pointer;
    padding: 2px 4px;
    line-height: 1;
    opacity: 0.7;
}

.status-dismiss:hover {
    opacity: 1;
}

.loading-overlay[data-tone="error"] .status-dismiss {
    display: block;
}

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

/* --------------------------------------------------------------------------
   Hover tooltip (positioned via JS near the cursor)
   -------------------------------------------------------------------------- */

#hover-tooltip {
    position: absolute;
    z-index: 910;
    pointer-events: none;
    min-width: 200px;
    max-width: 280px;
    padding: 10px 12px;
    border-radius: 12px;
    border: 1px solid rgba(91, 140, 182, 0.24);
    background: rgba(7, 15, 27, 0.92);
    backdrop-filter: blur(14px);
    box-shadow: 0 10px 24px rgba(1, 8, 16, 0.4);
    color: var(--text);
    font-size: 0.84rem;
    line-height: 1.5;
}

#hover-tooltip[hidden] {
    display: none;
}

.tt-title {
    display: block;
    margin-bottom: 6px;
    font-size: 0.9rem;
}

.tt-row {
    display: flex;
    justify-content: space-between;
    gap: 8px;
    padding: 2px 0;
    color: var(--muted);
}

.tt-row.tt-active {
    color: var(--text);
    font-weight: 600;
}

.tt-label {
    flex: 0 0 auto;
}

.tt-value {
    text-align: right;
}

/* --------------------------------------------------------------------------
   MapLibre popup overrides
   -------------------------------------------------------------------------- */

.maplibregl-popup-content {
    padding: 0;
    border-radius: 16px;
    background: var(--panel-strong);
    color: var(--text);
    box-shadow: var(--shadow);
    border: 1px solid var(--panel-border);
    max-width: 400px;
}

.maplibregl-popup-tip {
    border-top-color: var(--panel-strong) !important;
    border-bottom-color: var(--panel-strong) !important;
}

.glacier-popup .maplibregl-popup-close-button {
    color: var(--muted);
    font-size: 1.1rem;
    padding: 6px 10px;
}

.glacier-popup .maplibregl-popup-close-button:hover {
    background: rgba(255, 255, 255, 0.08);
    border-radius: 8px;
    color: var(--text);
}

.popup-content {
    max-height: min(58vh, 520px);
    overflow: auto;
    padding: 12px 14px;
    padding-right: 10px;
}

.popup-content h3 {
    margin: 0 0 10px;
    font-size: 1rem;
}

.popup-kicker,
.popup-note {
    margin: 0 0 8px;
    color: var(--muted);
    font-size: 0.82rem;
}

.popup-highlight {
    margin: 0 0 12px;
    padding: 9px 11px;
    border-radius: 12px;
    background: rgba(77, 176, 255, 0.12);
}

.popup-table {
    width: 100%;
    border-collapse: collapse;
    margin-top: 10px;
    font-size: 0.84rem;
}

.popup-table th,
.popup-table td {
    vertical-align: top;
    padding: 7px 6px;
    text-align: left;
    border-top: 1px solid rgba(91, 140, 182, 0.16);
}

.popup-table th {
    width: 38%;
}

.popup-table td span {
    display: block;
    margin-bottom: 4px;
}

.popup-table .active-row th,
.popup-table .active-row td {
    background: rgba(77, 176, 255, 0.08);
}

.metadata-table th {
    width: 48%;
}

.popup-details {
    margin-top: 12px;
}

.popup-details summary {
    cursor: pointer;
    color: var(--accent);
    font-weight: 700;
}

/* --------------------------------------------------------------------------
   MapLibre navigation controls
   -------------------------------------------------------------------------- */

.maplibregl-ctrl-group {
    background: rgb(9, 20, 34) !important;
    box-shadow: 0 12px 24px rgba(2, 10, 20, 0.3) !important;
    border-radius: 12px !important;
    overflow: hidden;
    border: 1px solid rgba(91, 140, 182, 0.22) !important;
    backdrop-filter: blur(18px);
}

.maplibregl-ctrl-group button {
    background: rgb(9, 20, 34) !important;
    color: var(--text) !important;
    border: 0 !important;
    border-bottom: 1px solid rgba(91, 140, 182, 0.14) !important;
    border-radius: 0 !important;
    transform: none !important;
    padding: 0 !important;
}

.maplibregl-ctrl-group button:last-child {
    border-bottom: 0 !important;
}

.maplibregl-ctrl-group button:hover {
    background: rgba(20, 40, 65, 0.96) !important;
    opacity: 1 !important;
    transform: none !important;
}

/* Override blue focus ring from MapLibre default stylesheet */
.maplibregl-ctrl-group button:focus,
.maplibregl-ctrl-group button:focus:focus-visible {
    box-shadow: 0 0 2px 2px rgba(77, 176, 255, 0.6) !important;
}

.maplibregl-ctrl-icon {
    filter: invert(1) brightness(0.85);
}

/* --------------------------------------------------------------------------
   Responsive
   -------------------------------------------------------------------------- */

@media (max-width: 840px) {
    .app-header {
        /* Leave ~52px on the right for MapLibre nav controls (10px margin + 29px wide + 13px gap) */
        width: calc(100vw - 70px);
    }

    .left-sidebar-stack {
        top: auto;
        left: 18px;
        /* 10px margin + 29px nav ctrl width + 9px gap = 48px — keeps clear of bbox/terrain buttons */
        right: 48px;
        width: auto;
        bottom: calc(18px + min(22vh, 184px) + 12px);
        gap: 12px;
    }

    .control-panel {
        max-height: calc(100vh - 18px - min(22vh, 184px) - 12px - 146px);
    }

    .panel-toolbar {
        top: -14px;
    }

    #panel-launch-button {
        top: auto;
        bottom: 18px;
    }

    .legend-panel {
        left: 18px;
        right: 48px;
        width: auto;
        transform: none;
        max-height: min(22vh, 184px);
    }

    .legend {
        column-count: 1;
    }

    /* When attribution is expanded, lift the bottom-right container above the legend */
    .maplibregl-ctrl-bottom-right:has(.maplibregl-compact-show) {
        bottom: calc(min(22vh, 184px) + 28px) !important;
    }

    /* Popup: constrain width and height on small screens */
    .maplibregl-popup {
        max-width: calc(100vw - 24px) !important;
    }

    .maplibregl-popup-content {
        max-width: 100% !important;
    }

    .popup-content {
        max-height: min(44vh, 280px);
    }

}

@media (max-width: 840px) and (max-height: 760px) {
    .left-sidebar-stack {
        bottom: calc(18px + min(20vh, 160px) + 10px);
    }

    .control-panel {
        max-height: calc(100vh - 18px - min(20vh, 160px) - 10px - 146px);
    }

    .legend-panel {
        max-height: min(20vh, 160px);
        padding-top: 12px;
        padding-bottom: 12px;
    }

    .maplibregl-ctrl-bottom-right:has(.maplibregl-compact-show) {
        bottom: calc(min(20vh, 160px) + 28px) !important;
    }

    .popup-content {
        max-height: min(36vh, 200px);
    }
}

/* ---------------------------------------------------------------------------
   Bbox zoom rectangle + active button state
   -------------------------------------------------------------------------- */

#bbox-rect {
    position: absolute;
    border: 2px solid var(--accent);
    background: rgba(77, 176, 255, 0.12);
    pointer-events: none;
    z-index: 10;
}

/* Bbox-zoom button: sits inside #map (position relative), aligned below
   MapLibre's top-right NavigationControl group (10px margin + 3×29px buttons + 2px borders ≈ 99px).
   A 10px gap avoids overlap. */
#bbox-zoom-button {
    position: absolute;
    top: 110px;
    right: 10px;
    z-index: 2;
    width: 29px;
    height: 29px;
    padding: 0;
    margin: 0;
    border: none;
    border-radius: 4px;
    background: rgba(10, 21, 37, 0.88);
    color: var(--text);
    font-size: 16px;
    line-height: 29px;
    text-align: center;
    cursor: pointer;
    box-shadow: 0 0 0 2px rgba(0, 0, 0, .3);
    display: block;
}

#bbox-zoom-button:hover {
    background: rgba(20, 40, 65, 0.96);
}

#bbox-zoom-button.is-active {
    background: rgba(0, 102, 204, 0.3);
    color: #4db0ff;
    box-shadow: 0 0 0 2px #0066cc;
}

#terrain-button {
    position: absolute;
    top: 149px;
    right: 10px;
    z-index: 2;
    width: 29px;
    height: 29px;
    padding: 0;
    margin: 0;
    border: none;
    border-radius: 4px;
    background: rgba(10, 21, 37, 0.88);
    color: var(--text);
    font-size: 16px;
    line-height: 29px;
    text-align: center;
    cursor: pointer;
    box-shadow: 0 0 0 2px rgba(0, 0, 0, .3);
    display: block;
}

#terrain-button:hover {
    background: rgba(20, 40, 65, 0.96);
}

#terrain-button.is-active {
    background: rgba(0, 102, 204, 0.3);
    color: #4db0ff;
    box-shadow: 0 0 0 2px #0066cc;
}

/* --------------------------------------------------------------------------
   App header: About button
   -------------------------------------------------------------------------- */

.app-header {
    display: flex;
    align-items: flex-start;
    justify-content: space-between;
    gap: 12px;
}

.about-button {
    flex: 0 0 auto;
    align-self: center;
    white-space: nowrap;
}

/* --------------------------------------------------------------------------
   About modal
   -------------------------------------------------------------------------- */

.about-modal {
    position: fixed;
    inset: 0;
    z-index: 1100;
    width: min(680px, calc(100vw - 48px));
    max-height: calc(100vh - 48px);
    margin: auto;
    padding: 0;
    border: 1px solid var(--panel-border);
    border-radius: var(--radius);
    background: rgba(7, 15, 27, 0.97);
    backdrop-filter: blur(24px);
    box-shadow: 0 24px 60px rgba(1, 8, 16, 0.6);
    color: var(--text);
    overflow: hidden;
}

.about-modal::backdrop {
    background: rgba(1, 8, 16, 0.65);
    backdrop-filter: blur(4px);
}

.about-modal-inner {
    display: flex;
    flex-direction: column;
    max-height: calc(100vh - 48px);
}

.about-modal-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 12px;
    padding: 16px 20px;
    border-bottom: 1px solid var(--panel-border);
    position: sticky;
    top: 0;
    background: rgba(7, 15, 27, 0.98);
}

.about-modal-header h2 {
    margin: 0;
    font-family: var(--font-display);
    font-size: 1.1rem;
}

.about-modal-body {
    padding: 20px;
    overflow-y: auto;
    overscroll-behavior: contain;
    line-height: 1.65;
}

.about-modal-body p {
    margin: 0 0 14px;
    color: var(--muted);
}

.about-modal-body h3 {
    margin: 20px 0 10px;
    font-size: 0.95rem;
    text-transform: uppercase;
    letter-spacing: 0.08em;
    color: var(--accent);
}

.about-sources {
    margin: 0;
    padding-left: 18px;
    display: flex;
    flex-direction: column;
    gap: 10px;
    color: var(--muted);
}

.about-modal-body a {
    color: var(--accent);
    text-decoration: none;
}

.about-modal-body a:hover {
    text-decoration: underline;
}

.about-modal-footer {
    padding: 12px 20px;
    border-top: 1px solid var(--panel-border);
    display: flex;
    justify-content: flex-end;
}

.about-version {
    font-size: 0.8rem;
    color: var(--muted);
    font-variant-numeric: tabular-nums;
}