:root{
    --bg: #12161C;
    --panel: rgba(27, 38, 51, 0.85);
    --panel-border: rgba(255,255,255,0.08);
    --text: #EAECEF;
    --muted: #8F9AA3;
    --accent: rgba(0, 152, 186, 0.33);
    --cyan: #5FB8C0;
    --blue: #467E89;
    --red: rgba(4, 99, 133, 0.64);    
    --shadow: 0 12px 40px rgba(0,0,0,0.38);
}

#splash{
    position: fixed;
    inset: 0;
    display: grid;
    place-items: center;
    background: rgba(0,0,0,0.7);
    backdrop-filter: blur(8px);
    -webkit-backdrop-filter: blur(8px);
    z-index: 9999;
    opacity: 1;
    transition: opacity .4s ease;
}

.splash-card{
    display: flex;
    flex-direction: column;
    align-items: center;
    text-align: center;
    background: var(--panel, #111418);
    color: var(--text, #EAECEF);
    border: 1px solid var(--panel-border, rgba(255,255,255,0.12));
    box-shadow: var(--shadow, 0 10px 30px rgba(0,0,0,0.35));
    border-radius: 16px;
    padding: 24px 28px;
    gap: 12px;
    max-width: min(420px, 90vw);
}

#splash-enter{
    font: inherit;
    padding: 10px 16px;
    border-radius: 12px;
    border: 1px solid transparent;
    background: linear-gradient(180deg, var(--red), var(--accent));
    color: #12161C;
    cursor: pointer;
    transition: opacity .2s ease, transform .2s ease;
}

#splash-enter:hover{
    opacity: .9;
    transform: translateY(-1px);
}

#splash-enter:focus-visible{
    outline: 2px solid var(--cyan);
    outline-offset: 2px;
}

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

#splash.is-hidden{
    opacity: 0;
    pointer-events: none;
    visibility: hidden;
}

#terrain-ui{
    right: var(--safe-right);
    top: auto;
    bottom: calc(var(--safe-bottom) + var(--gap));
    max-height: calc(100vh - var(--safe-top) - var(--safe-bottom) - 120px);
    overflow: auto;
    z-index: 12;
}

* { box-sizing: border-box; }
html, body { height: 100%; margin: 0; }
body{
    font-family: ui-sans-serif, system-ui, -apple-system, Segoe UI, Roboto, Helvetica, Arial, "Apple Color Emoji","Segoe UI Emoji";
    background: var(--bg);
    color: var(--text);
}

#map{
    position: absolute;
    inset: 0;
    height: 100vh;
}

.ui-panel{
    position: absolute;
    z-index: 10;
    background: var(--panel);
    backdrop-filter: blur(10px);
    -webkit-backdrop-filter: blur(10px);
    border: 1px solid var(--panel-border);
    border-radius: 16px;
    box-shadow: var(--shadow);
    padding: 14px 16px;
}

#controls-panel{ top: 16px; left: 16px; width: 320px; }
#analysis-panel{ bottom: 16px; left: 16px; width: 320px; }
#legend{ bottom: 16px; right: 16px; width: 200px; }

.terrain-ui{
    position: absolute;
    top: 92px;
    right: 12px;
    width: 260px;
    background: var(--panel);
    border: 1px solid var(--panel-border);
    border-radius: 12px;
    box-shadow: var(--shadow);
    padding: 12px;
    color: var(--text);
    z-index: 10;
    backdrop-filter: blur(10px);
    -webkit-backdrop-filter: blur(10px);
}

.terrain-ui label{
    display: flex;
    justify-content: space-between;
    align-items: center;
    font-size: 12px;
    color: var(--muted);
    margin-bottom: 6px;
}

.terrain-ui #z-value{
    font-weight: 600;
    color: var(--text);
    font-size: 13px;
}

.terrain-ui input[type="range"]{
    width: 100%;
    accent-color: var(--cyan);
    margin: 4px 0 10px;
}

.terrain-buttons{
    display: grid;
    grid-template-columns: repeat(3, 1fr);
    gap: 8px;
    margin-bottom: 12px;
}

.terrain-buttons .btn{
    padding: 6px 0;
}

.terrain-toggle{
    display: flex;
    align-items: center;
    gap: 8px;
    font-size: 12px;
    color: var(--muted);
    margin-bottom: 12px;
}

.terrain-reset-btn{
    width: 100%;
}

h2,h3{ margin: 0 0 8px 0; font-weight: 600; letter-spacing: .2px; }
h2{ font-size: 16px; }
h3{ font-size: 14px; color: var(--muted); }

.divider{
    height: 1px;
    background: var(--panel-border);
    margin: 10px 0 12px;
}

label{ display:block; font-size: 12px; color: var(--muted); margin-bottom: 6px; }
select, .btn, input[type="range"]{
    width: 100%;
    border-radius: 10px;
    border: 1px solid var(--panel-border);
    background: rgba(255,255,255,0.06);
    color: var(--text);
    padding: 8px 10px;
    outline: none;
    transition: border-color .2s, transform .06s;
}
select:focus, input[type="range"]:focus{ border-color: var(--cyan); }

.btn{
    cursor: pointer;
    margin-top: 8px;
    background: linear-gradient(180deg, var(--red), var(--accent));
    border: 1px solid var(--red);
}
.btn:hover{ transform: translateY(-1px); border-color: var(--accent); }
.btn:active{ transform: translateY(0px) scale(0.995); }
.btn.btn-secondary{
    background: rgba(255,255,255,0.05);
    border: 1px solid var(--panel-border);
}

.control-row{
    display: grid;
    grid-template-columns: auto 1fr auto;
    gap: 8px;
    align-items: center;
    margin-bottom: 8px;
}
.control-row .label{
    font-size: 13px;
}
.control-row .toggle{
    appearance: none;
    width: 34px; height: 20px; position: relative;
    background: rgba(255,255,255,0.12);
    border-radius: 999px; cursor: pointer; outline: none;
    transition: background .25s;
}
.control-row .toggle:checked{ background: var(--cyan); }
.control-row .toggle::after{
    content:""; position: absolute; top: 2px; left: 2px;
    width: 16px; height: 16px; background: #fff; border-radius: 50%;
    transition: left .25s;
}
.control-row .toggle:checked::after{ left: 16px; }

.range{
    grid-column: 1 / -1;
}
.range input[type="range"]{
    accent-color: var(--cyan);
}

#legend ul{ margin: 6px 0 0; padding: 0; list-style: none; }
#legend li{ display: flex; align-items: center; gap: 6px; margin: 6px 0; font-size: 12px;}
#legend li.legend-group{ flex-direction: column; align-items: flex-start; gap: 4px; }
#legend li.legend-group > span{ font-weight: 600; }
#legend li ul{ margin: 4px 0 0 16px; list-style: disc; color: var(--muted); display: block; padding: 0; }
#legend li ul li{ display: flex; align-items: center; gap: 6px; margin: 3px 0; font-size: 11px; list-style: none; }
.key{
    width: 16px; height: 12px; border-radius: 2px; display:inline-block; border:1px solid var(--panel-border);
}
.key-muni{ background: rgba(243,139,42,0.35); }
.key-micro{ background: rgba(95,184,192,0.64); }
.key-stream{ background: rgba(70,126,137,0.6); }
.key-excl{ background: rgba(245,180,87,0.35); }
.key-proj{ background: rgba(255,255,255,0.9); }
.key-hl{ background: rgba(243,139,42,0.65); }
.key-wc{ background: rgba(120,120,120,0.35); }
.key-type-21{ background: #F0F7B2; border-color: #D8E47D; }
.key-type-hs{ background: #DAF3A4; border-color: #BFE06E; }
.key-type-gf{ background: #C0EE97; border-color: #A2D65E; }
.key-type-rf{ background: #A3E688; border-color: #82CB52; }
.key-type-dr{ background: #83DD7A; border-color: #64C245; }
.key-type-rm{ background: #63D46D; border-color: #45B83A; }
.key-type-rhf{ background: #47C269; border-color: #32A73E; }
.key-type-greenbelt{ background: #32B064; border-color: #24953F; }
.key-type-other{ background: #2AA467; border-color: #1F8B46; }

.mapboxgl-popup-content{
    background: var(--panel);
    color: var(--text);
    border: 1px solid var(--panel-border);
    border-radius: 12px;
    box-shadow: var(--shadow);
}
.mapboxgl-popup-close-button{ color: var(--text); }

.hidden{ display: none; }
.muted{ color: var(--muted); font-size: 12px; }

:root{
    --gap: 56px;
    --safe-top: calc(56px + env(safe-area-inset-top, 0px));
    --safe-right: calc(56px + env(safe-area-inset-right, 0px));
    --safe-bottom: calc(56px + env(safe-area-inset-bottom, 0px));
}

.mapboxgl-ctrl-top-right{ top: var(--safe-top); right: var(--safe-right); }
.mapboxgl-ctrl-bottom-right{ right: var(--safe-right); bottom: var(--safe-bottom); }

#terrain-ui{
    right: var(--safe-right);
    top: auto;
    bottom: calc(var(--safe-bottom) + var(--gap));
    max-height: calc(100vh - var(--safe-top) - var(--safe-bottom) - 120px);
    overflow: auto;
    z-index: 12;
}

#legend{
    right: var(--safe-right);
    bottom: calc(var(--safe-bottom) + var(--gap));
    max-height: calc(100vh - var(--safe-top) - var(--safe-bottom) - 220px);
    overflow: auto;
}

#controls-panel,
#analysis-panel{
    top: var(--safe-top);
    bottom: auto;
    max-height: calc(100vh - var(--safe-top) - var(--safe-bottom));
    overflow: auto;
}
#analysis-panel{ top: auto; bottom: var(--safe-bottom); }

@media (max-height: 760px){
    #legend{
        top: calc(var(--safe-top) + 56px + var(--gap) + 220px);
        bottom: auto;
        right: var(--safe-right);
    }
}

@media (max-width: 900px){
    #legend{
        width: 220px;
    }
}

:root{
    --gap: 12px;
    --safe-top: 12px;
    --safe-right: 16px;
    --safe-bottom: 64px; /* atribución de Mapbox!!! */
    --safe-left: 16px;
}


.mapboxgl-ctrl-top-right{ top: var(--safe-top); right: var(--safe-right); }
.mapboxgl-ctrl-bottom-right{ right: var(--safe-right); bottom: 8px; }


.terrain-ui{
    top: calc(var(--safe-top) + 48px + var(--gap)); /* ~altura del NavigationControl */
    right: var(--safe-right);
    max-height: calc(100vh - (var(--safe-top) + 48px + var(--gap)) - var(--safe-bottom) - var(--gap));
    overflow: auto;
}


#legend{
    right: var(--safe-right);
    bottom: calc(var(--safe-bottom) + var(--gap));
    max-height: calc(100vh - (var(--safe-top) + 48px + var(--gap)) - var(--safe-bottom) - 2*var(--gap));
    overflow: auto;
}

#controls-panel,
#analysis-panel{
    max-height: calc(100vh - 2*var(--safe-top));
    overflow: auto;
}

@media (max-height: 760px){
    #legend{
        top: calc(var(--safe-top) + 48px + var(--gap) + 260px + var(--gap)); /* ~altura aprox. de .terrain-ui */
        bottom: auto;
    }
}


@media (max-width: 900px){
    #legend{ width: 260px; }
}

.layer-item { display: flex; align-items: center; gap: .5rem; }
.layer-label { display: inline-flex; align-items: center; gap: .4rem; }
.li-spinner {
    width: 14px; height: 14px;
    border: 2px solid var(--panel-border);
    border-top-color: var(--cyan);
    border-radius: 50%;
    animation: spin .9s linear infinite;
}
.count-badge {
    font: 600 11px/1 Inter, system-ui, sans-serif;
    background: rgba(255,255,255,.06);
    border: 1px solid var(--panel-border);
    padding: .2rem .44rem;
    border-radius: .5rem;
    color: var(--muted);
}
.hidden { opacity: 0; pointer-events: none; }

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