:root {
    --panel-bg: #0e1c2e;
    --panel-border: rgba(120, 160, 230, 0.22);
    --panel-border-hi: rgba(62, 166, 255, 0.55);
    --panel-text: #e2e8f0;
    --panel-text-dim: #8ba6c9;
    --accent: #3ea6ff;
    --accent-soft: rgba(62, 166, 255, 0.15);
    --dot-empty: #4a90e2;
    --dot-empty-glow: rgba(74, 144, 226, 0.18);
    --dot-filled: #22c55e;
    --dot-filled-glow: rgba(34, 197, 94, 0.2);
    --shadow: 0 12px 40px rgba(0, 0, 0, 0.55);
}

html,
body {
    margin: 0;
    height: 100%;
    overflow: hidden;
    background: #0a0a0a;
    font-family: 'Exo 2', -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
    color: var(--panel-text);
    -webkit-tap-highlight-color: transparent;
}

#map {
    position: absolute;
    inset: 0;
}

#loading {
    position: fixed;
    inset: 0;
    display: flex;
    align-items: center;
    justify-content: center;
    background: #0a1626;
    color: var(--panel-text-dim);
    font-size: 15px;
    z-index: 10;
    transition: opacity .3s;
}

#loading.hidden {
    opacity: 0;
    pointer-events: none;
}

.viewer-link {
    position: fixed;
    z-index: 480;
    padding: 8px 14px;
    background: rgba(14, 28, 46, 0.92);
    border: 1px solid var(--panel-border);
    border-radius: 10px;
    color: var(--panel-text);
    font-size: 13px;
    font-weight: 500;
    font-family: inherit;
    text-decoration: none;
    opacity: 0.72;
    transition: opacity .18s ease, background .18s ease, border-color .18s ease;
    left: 16px;
    bottom: 16px;
}

.viewer-link:hover {
    opacity: 1;
    background: rgba(14, 28, 46, 0.85);
    border-color: var(--panel-border-hi);
}

.legend {
    position: fixed;
    top: 16px;
    right: 16px;
    width: 320px;
    max-height: calc(100dvh - 32px);
    display: flex;
    flex-direction: column;
    background: var(--panel-bg);
    border: 1px solid var(--panel-border);
    border-radius: 14px;
    box-shadow: var(--shadow);
    z-index: 500;
    overflow: hidden;
    transition: transform .3s cubic-bezier(0.4, 0, 0.2, 1);
    transform: translateZ(0);
    backface-visibility: hidden;
    will-change: transform;
    contain: layout paint;
}

.legend__header {
    display: flex;
    align-items: center;
    gap: 10px;
    padding: 12px 14px;
    border-bottom: 1px solid var(--panel-border);
    flex-shrink: 0;
    cursor: pointer;
    user-select: none;
}

.legend__icon {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    width: 26px;
    height: 26px;
    border-radius: 8px;
    background: var(--accent-soft);
    color: var(--accent);
    flex-shrink: 0;
}

.legend__icon svg {
    display: block;
}

.legend__title {
    flex: 1;
    min-width: 0;
    font-size: 14px;
    font-weight: 600;
    letter-spacing: 0.01em;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

.legend__count {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    min-width: 24px;
    height: 20px;
    padding: 0 7px;
    border-radius: 10px;
    background: rgba(62, 166, 255, 0.18);
    color: var(--accent);
    font-size: 11px;
    font-weight: 700;
    flex-shrink: 0;
}

.legend__chevron {
    flex-shrink: 0;
    color: var(--panel-text-dim);
    transition: transform .28s cubic-bezier(0.4, 0, 0.2, 1);
}

.legend.is-collapsed .legend__chevron {
    transform: rotate(-90deg);
}

.legend__search-wrap {
    position: relative;
    flex-shrink: 0;
    padding: 8px 10px;
    border-bottom: 1px solid var(--panel-border);
    max-height: 80px;
    overflow: hidden;
    transition: max-height .3s cubic-bezier(0.4, 0, 0.2, 1),
        padding .3s cubic-bezier(0.4, 0, 0.2, 1),
        opacity .2s ease,
        border-color .3s ease;
}

.legend.is-collapsed .legend__search-wrap {
    max-height: 0;
    padding-top: 0;
    padding-bottom: 0;
    opacity: 0;
    border-bottom-color: transparent;
}

.legend__search-icon {
    position: absolute;
    left: 22px;
    top: 50%;
    transform: translateY(-50%);
    width: 14px;
    height: 14px;
    color: var(--panel-text-dim);
    pointer-events: none;
}

.legend__search {
    width: 100%;
    padding: 8px 34px 8px 32px;
    background: rgba(10, 22, 38, 0.7);
    border: 1px solid var(--panel-border);
    border-radius: 8px;
    color: var(--panel-text);
    font-family: inherit;
    font-size: 13px;
    outline: none;
    box-sizing: border-box;
    -webkit-appearance: none;
    appearance: none;
    transition: border-color .15s, background .15s;
}

.legend__search-clear {
    position: absolute;
    right: 18px;
    top: 50%;
    transform: translateY(-50%);
    width: 22px;
    height: 22px;
    padding: 0;
    display: flex;
    align-items: center;
    justify-content: center;
    background: transparent;
    border: none;
    border-radius: 50%;
    color: var(--panel-text-dim);
    font-size: 12px;
    line-height: 1;
    font-family: inherit;
    cursor: pointer;
    transition: color .15s, background .15s;
}

.legend__search-clear:hover {
    color: var(--panel-text);
    background: rgba(62, 166, 255, 0.14);
}

.legend__search-clear[hidden] {
    display: none;
}

.legend__search:focus {
    border-color: var(--panel-border-hi);
    background: rgba(10, 22, 38, 0.9);
}

.legend__search::placeholder {
    color: var(--panel-text-dim);
}

.legend__item {
    display: flex;
    align-items: center;
    gap: 10px;
    padding: 10px 12px;
    border-radius: 10px;
    background: transparent;
    border: 1px solid transparent;
    cursor: pointer;
    transition: background .15s, border-color .15s, transform .12s;
    position: relative;
}

.legend__item:hover {
    background: rgba(62, 166, 255, 0.08);
    border-color: rgba(62, 166, 255, 0.25);
}

.legend__item:active {
    transform: scale(0.985);
}

.legend__item.is-active {
    background: rgba(62, 166, 255, 0.16);
    border-color: var(--panel-border-hi);
}

.legend__dot {
    flex-shrink: 0;
    width: 9px;
    height: 9px;
    border-radius: 50%;
    background: var(--dot-empty);
    box-shadow: 0 0 0 3px var(--dot-empty-glow);
    transition: box-shadow .2s, background .2s;
}

.legend__item.has-pres .legend__dot {
    background: var(--dot-filled);
    box-shadow: 0 0 0 3px var(--dot-filled-glow);
}

.legend__item.is-active .legend__dot {
    box-shadow: 0 0 0 4px rgba(62, 166, 255, 0.3),
        0 0 12px rgba(62, 166, 255, 0.6);
}

.legend__item.has-pres.is-active .legend__dot {
    box-shadow: 0 0 0 4px rgba(34, 197, 94, 0.32),
        0 0 14px rgba(34, 197, 94, 0.7);
}

.legend__text {
    flex: 1;
    min-width: 0;
    display: flex;
    flex-direction: column;
    gap: 2px;
}

.legend__name {
    font-size: 13.5px;
    font-weight: 500;
    color: var(--panel-text);
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
    line-height: 1.3;
}

.legend__meta {
    font-size: 11px;
    color: var(--panel-text-dim);
    font-variant-numeric: tabular-nums;
    line-height: 1.3;
}

.legend__empty {
    padding: 20px 14px;
    text-align: center;
    color: var(--panel-text-dim);
    font-size: 13px;
    line-height: 1.5;
}

.pres-modal {
    position: fixed;
    inset: 0;
    z-index: 100000;
    display: flex;
    align-items: center;
    justify-content: center;
    padding: 20px;
    background: rgba(2, 6, 15, 0.86);
    opacity: 0;
    visibility: hidden;
    transition: opacity .28s ease, visibility 0s .28s;
}

.pres-modal.is-open {
    opacity: 1;
    visibility: visible;
    transition: opacity .28s ease, visibility 0s 0s;
}

.pres-modal__inner {
    position: relative;
    display: block;
    width: min(1600px, calc(100vw - 40px));
    height: min(1000px, calc(100dvh - 40px));
    background: #0a1626;
    border: 1px solid var(--panel-border);
    border-radius: 14px;
    overflow: hidden;
    box-shadow: 0 30px 80px rgba(0, 0, 0, 0.75);
    transform: scale(0.96);
    transition: transform .28s cubic-bezier(0.34, 1.56, 0.64, 1);
}

.pres-modal.is-open .pres-modal__inner {
    transform: scale(1);
}

.pres-modal__close {
    position: absolute;
    top: 12px;
    right: 12px;
    z-index: 10;
    width: 38px;
    height: 38px;
    padding: 0;
    display: flex;
    align-items: center;
    justify-content: center;
    background: rgba(10, 22, 38, 0.82);
    border: 1px solid var(--panel-border);
    border-radius: 10px;
    color: #cbd5e1;
    cursor: pointer;
    font-size: 16px;
    line-height: 1;
    font-family: inherit;
    opacity: 0.55;
    transition: opacity .2s ease, background .15s ease, border-color .15s ease, color .15s ease;
}

.pres-modal__close.is-hidden {
    display: none;
}

.pres-modal__inner:hover .pres-modal__close,
.pres-modal__close:focus-visible {
    opacity: 1;
}

.pres-modal__close:hover {
    background: rgba(220, 80, 80, 0.22);
    border-color: rgba(220, 80, 80, 0.7);
    color: #fca5a5;
}

.pres-modal__frame {
    display: block;
    width: 100%;
    height: 100%;
    border: 0;
    background: #0a1626;
}

.legend__sections {
    flex: 1 1 auto;
    min-height: 0;
    display: flex;
    flex-direction: column;
    overflow: hidden;
    transition: max-height .3s cubic-bezier(0.4, 0, 0.2, 1),
        opacity .2s ease;
}

.legend.is-collapsed .legend__sections {
    flex: 0 0 0;
    opacity: 0;
    pointer-events: none;
}

.legend__sections-body {
    flex: 1;
    min-height: 0;
    overflow-y: auto;
    padding: 6px 6px 8px;
    scrollbar-width: thin;
    scrollbar-color: rgba(62, 166, 255, 0.55) transparent;
    scrollbar-gutter: stable;
    overscroll-behavior: contain;
}

.legend__sections-body::-webkit-scrollbar {
    width: 6px;
}

.legend__sections-body::-webkit-scrollbar-track {
    background: transparent;
}

.legend__sections-body::-webkit-scrollbar-thumb {
    background: rgba(62, 166, 255, 0.55);
    border-radius: 3px;
}

.legend__sections-body::-webkit-scrollbar-thumb:hover {
    background: rgba(62, 166, 255, 0.85);
}

.legend__section {
    border-radius: 8px;
    margin-bottom: 4px;
    overflow: hidden;
}

.legend__section-head {
    display: flex;
    align-items: center;
    gap: 8px;
    padding: 8px 10px;
    cursor: pointer;
    user-select: none;
    transition: background .15s;
}

.legend__section-head:hover {
    background: rgba(62, 166, 255, 0.08);
}

.legend__section.is-open .legend__section-head {
    background: rgba(62, 166, 255, 0.12);
}

.legend__section-caret {
    flex-shrink: 0;
    width: 12px;
    height: 12px;
    display: flex;
    align-items: center;
    justify-content: center;
    color: var(--panel-text-dim);
    transition: transform .25s cubic-bezier(0.4, 0, 0.2, 1);
}

.legend__section.is-open .legend__section-caret {
    transform: rotate(90deg);
    color: var(--accent);
}

.legend__section-caret svg {
    display: block;
}

.legend__section-name {
    flex: 1;
    min-width: 0;
    font-size: 13px;
    font-weight: 500;
    color: var(--panel-text);
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

.legend__section-count {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    min-width: 22px;
    height: 18px;
    padding: 0 6px;
    border-radius: 9px;
    background: rgba(62, 166, 255, 0.18);
    color: var(--accent);
    font-size: 10.5px;
    font-weight: 700;
    flex-shrink: 0;
}

.legend__section-body {
    display: grid;
    grid-template-rows: 0fr;
    transition: grid-template-rows .3s cubic-bezier(0.4, 0, 0.2, 1);
}

.legend__section.is-open .legend__section-body {
    grid-template-rows: 1fr;
}

.legend__section-body>div {
    min-height: 0;
    overflow: hidden;
}

.legend__section-list {
    padding: 2px 0 6px;
    display: flex;
    flex-direction: column;
    gap: 2px;
}

.legend__section-item {
    display: flex;
    align-items: center;
    gap: 8px;
    padding: 7px 10px;
    border-radius: 6px;
    cursor: pointer;
    transition: background .15s;
    font-size: 12.5px;
}

.legend__section-item:hover {
    background: rgba(62, 166, 255, 0.08);
}

.legend__section-item.is-active {
    background: rgba(62, 166, 255, 0.16);
}

.legend__section-item-dot {
    flex-shrink: 0;
    width: 6px;
    height: 6px;
    border-radius: 50%;
    background: var(--dot-empty);
    box-shadow: 0 0 0 2px var(--dot-empty-glow);
}

.legend__section-item.has-pres .legend__section-item-dot {
    background: var(--dot-filled);
    box-shadow: 0 0 0 2px var(--dot-filled-glow);
}

.legend__section-item-name {
    flex: 1;
    min-width: 0;
    color: var(--panel-text);
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

.legend__section-empty {
    padding: 6px 12px 10px;
    font-size: 11.5px;
    color: var(--panel-text-dim);
    font-style: italic;
}

.pres-modal__loader {
    position: absolute;
    inset: 0;
    z-index: 5;
    display: flex;
    align-items: center;
    justify-content: center;
    background: #0a1626;
    transition: opacity 0.35s ease, visibility 0.35s ease;
    pointer-events: none;
}

.pres-modal__loader.is-hidden {
    opacity: 0;
    visibility: hidden;
}

.pres-modal__loader-content {
    display: flex;
    align-items: center;
    gap: 8px;
}

.pres-modal__loader-text {
    color: var(--panel-text);
    font-size: clamp(15px, 2vw, 18px);
    font-weight: 500;
    letter-spacing: 0.02em;
}

.pres-modal__loader-dots {
    display: inline-flex;
    align-items: center;
    gap: 4px;
    height: 12px;
}

.pres-modal__loader-dot {
    width: 5px;
    height: 5px;
    border-radius: 50%;
    background: var(--accent);
    animation: presLoaderBounce 1.2s ease-in-out infinite;
}

.pres-modal__loader-dot:nth-child(2) {
    animation-delay: 0.15s;
}

.pres-modal__loader-dot:nth-child(3) {
    animation-delay: 0.3s;
}

@keyframes presLoaderBounce {

    0%,
    60%,
    100% {
        transform: translateY(0);
    }

    30% {
        transform: translateY(-5px);
    }
}

@media (prefers-reduced-motion: reduce) {
    .pres-modal__loader-dot {
        animation: none;
    }
}

@media (max-width: 720px) {
    .viewer-link {
        left: auto;
        bottom: auto;
        top: calc(10px + env(safe-area-inset-top, 0px));
        right: calc(10px + env(safe-area-inset-right, 0px));
        padding: 6px 12px;
        font-size: 12px;
    }

    .legend {
        top: auto;
        left: 10px;
        right: 10px;
        bottom: calc(10px + env(safe-area-inset-bottom));
        width: auto;
        max-height: 60dvh;
        border-radius: 16px;
    }

    .legend__header {
        padding: 12px 14px;
    }

    .legend.is-collapsed {
        max-height: 52px;
    }

    .legend__item {
        padding: 12px 14px;
    }

    .legend__name {
        font-size: 14px;
    }

    .legend__search-wrap {
        padding: 10px 12px;
        max-height: 90px;
    }

    .legend__search {
        font-size: 16px;
        padding: 10px 12px 10px 36px;
    }

    .legend__search-icon {
        left: 26px;
        width: 16px;
        height: 16px;
    }

    .legend__search-clear {
        right: 20px;
        width: 26px;
        height: 26px;
        font-size: 14px;
    }

    .pres-modal {
        padding: 8px;
    }

    .pres-modal__inner {
        width: calc(100vw - 16px);
        height: calc(100dvh - 16px);
        border-radius: 10px;
    }

    .pres-modal__close {
        top: 10px;
        right: 10px;
        width: 40px;
        height: 40px;
    }
}

@media (max-width: 380px) {
    .legend__count {
        display: none;
    }
}

.maplibregl-popup-content {
    background: #0e1c2e !important;
    color: var(--panel-text) !important;
    border: 1px solid var(--panel-border) !important;
    border-radius: 10px !important;
    padding: 8px 14px !important;
    font-size: 13px !important;
    box-shadow: 0 10px 30px rgba(0, 0, 0, 0.5) !important;
}

.maplibregl-popup-tip {
    display: none !important;
}

.maplibregl-ctrl-attrib {
    background: rgba(14, 28, 46, 0.75) !important;
    color: var(--panel-text-dim) !important;
}

.maplibregl-ctrl-attrib a {
    color: #8ba6c9 !important;
}

@media (max-width: 720px) {

    .maplibregl-ctrl-bottom-right,
    .maplibregl-ctrl-bottom-left {
        bottom: 70px;
    }
}