:root {
    color-scheme: dark;
    font-family: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
}

* {
    box-sizing: border-box;
}

html,
body,
#map {
    width: 100%;
    height: 100%;
    margin: 0;
}

body {
    overflow: hidden;
    background: #101418;
}

#map {
    position: absolute;
    inset: 0;
}

.visually-hidden {
    position: absolute;
    width: 1px;
    height: 1px;
    overflow: hidden;
    clip: rect(0 0 0 0);
    white-space: nowrap;
}

/* --- Panel superior: selector de vista, buscador y estado --- */

.overlay {
    position: absolute;
    top: max(16px, env(safe-area-inset-top));
    left: max(16px, env(safe-area-inset-left));
    z-index: 3;
    display: flex;
    flex-direction: column;
    align-items: flex-start;
    gap: 12px;
    max-width: calc(100vw - 88px);
    pointer-events: none;
}

.toolbar {
    display: flex;
    flex-wrap: wrap;
    align-items: center;
    gap: 8px;
}

.view-switcher,
.search,
.status {
    color: #e8edf2;
    background: rgba(16, 20, 24, 0.92);
    border: 1px solid rgba(255, 255, 255, 0.12);
    box-shadow: 0 10px 30px rgba(0, 0, 0, 0.28);
    backdrop-filter: blur(12px);
    pointer-events: auto;
}

.view-switcher {
    margin: 0;
    padding: 5px;
    border-radius: 14px;
}

.view-switcher__options {
    display: flex;
    gap: 4px;
}

.view-switcher label {
    position: relative;
    display: block;
    cursor: pointer;
}

.view-switcher input {
    position: absolute;
    opacity: 0;
    pointer-events: none;
}

.view-switcher span {
    display: block;
    padding: 8px 12px;
    color: #9ea9b3;
    border-radius: 9px;
    font-size: 12px;
    font-weight: 700;
    transition: color 150ms ease, background 150ms ease;
}

.view-switcher input:checked + span {
    color: #fff;
    background: #d9413b;
}

.view-switcher input:focus-visible + span {
    outline: 2px solid #ff8b86;
    outline-offset: 2px;
}

.search {
    display: flex;
    align-items: center;
    gap: 6px;
    width: min(320px, calc(100vw - 88px));
    margin: 0;
    padding: 4px 4px 4px 12px;
    border-radius: 14px;
    transition: border-color 150ms ease;
}

.search:focus-within {
    border-color: rgba(255, 139, 134, 0.6);
}

.search input {
    flex: 1;
    min-width: 0;
    height: 32px;
    padding: 0;
    color: #eef2f5;
    background: transparent;
    border: 0;
    outline: 0;
    font: inherit;
    font-size: 13px;
}

.search input::placeholder {
    color: #7d8893;
}

.search input[aria-invalid="true"] {
    color: #ffb3b0;
}

.search button {
    display: grid;
    flex: 0 0 auto;
    width: 32px;
    height: 32px;
    place-items: center;
    padding: 0;
    color: #fff;
    background: #d9413b;
    border: 0;
    border-radius: 10px;
    cursor: pointer;
    transition: background 150ms ease;
}

.search button:hover {
    background: #e6524c;
}

.search button:focus-visible {
    outline: 2px solid #ff8b86;
    outline-offset: 2px;
}

.status {
    display: flex;
    align-items: center;
    gap: 10px;
    max-width: 360px;
    min-height: 44px;
    padding: 10px 14px;
    border-radius: 12px;
    transition: opacity 180ms ease, transform 180ms ease;
}

.status[hidden] {
    display: none;
}

.status--ready {
    opacity: 0;
    transform: translateY(-6px);
    pointer-events: none;
}

.status--info .status__spinner {
    display: none;
}

.status--error {
    align-items: flex-start;
    color: #ffd7d7;
    background: rgba(53, 17, 21, 0.94);
    border-color: rgba(255, 112, 112, 0.35);
}

.status__spinner {
    width: 17px;
    height: 17px;
    flex: 0 0 auto;
    border: 2px solid rgba(255, 255, 255, 0.25);
    border-top-color: #ff625d;
    border-radius: 50%;
    animation: spin 750ms linear infinite;
}

.status--error .status__spinner {
    border: 0;
    border-radius: 0;
    animation: none;
}

.status--error .status__spinner::before {
    content: "!";
    display: grid;
    width: 18px;
    height: 18px;
    place-items: center;
    color: #351115;
    background: #ff7777;
    border-radius: 50%;
    font-size: 12px;
    font-weight: 800;
}

/* --- Controles de MapLibre --- */

.maplibregl-ctrl-top-right {
    top: max(12px, env(safe-area-inset-top));
    right: max(12px, env(safe-area-inset-right));
}

.maplibregl-ctrl-group {
    overflow: hidden;
    background: rgba(16, 20, 24, 0.9);
    border: 1px solid rgba(255, 255, 255, 0.12);
    border-radius: 12px;
    box-shadow: 0 10px 30px rgba(0, 0, 0, 0.28);
    backdrop-filter: blur(12px);
}

.maplibregl-ctrl-group button + button {
    border-top-color: rgba(255, 255, 255, 0.12);
}

.maplibregl-ctrl-group button .maplibregl-ctrl-icon {
    filter: invert(1);
}

.maplibregl-popup {
    max-width: min(390px, calc(100vw - 32px)) !important;
}

.maplibregl-popup-content {
    overflow: hidden;
    padding: 0;
    color: #e8edf2;
    background: rgba(16, 20, 24, 0.96);
    border: 1px solid rgba(255, 255, 255, 0.13);
    border-radius: 14px;
    box-shadow: 0 18px 50px rgba(0, 0, 0, 0.38);
    backdrop-filter: blur(14px);
}

.maplibregl-popup-close-button {
    z-index: 2;
    display: grid;
    width: 30px;
    height: 30px;
    place-items: center;
    padding: 0;
    color: #aeb8c2;
    background: transparent;
    border-radius: 8px;
    font-size: 22px;
    line-height: 1;
}

.maplibregl-popup-close-button:hover {
    color: #fff;
    background: rgba(255, 255, 255, 0.09);
}

.maplibregl-popup-tip {
    display: none;
}

/* --- Ficha de parcela / recinto --- */

/* --card-max-height lo fija feature-popup.js según el hueco libre junto al punto. */
.parcel-card {
    min-width: min(320px, calc(100vw - 32px));
    max-height: min(var(--card-max-height, 420px), 420px, calc(100vh - 48px));
    overflow-y: auto;
    scrollbar-color: #4e5963 transparent;
    user-select: text;
}

.parcel-card__header {
    position: sticky;
    top: 0;
    z-index: 1;
    padding: 16px 48px 14px 18px;
    background: rgba(16, 20, 24, 0.98);
    border-bottom: 1px solid rgba(255, 255, 255, 0.09);
}

.parcel-card__eyebrow {
    display: flex;
    align-items: center;
    gap: 8px;
    margin-bottom: 4px;
    color: #ff8580;
    font-size: 10px;
    font-weight: 800;
    letter-spacing: 0.12em;
    text-transform: uppercase;
}

.parcel-card__eyebrow::before {
    content: "";
    width: 7px;
    height: 7px;
    background: #ff625d;
    border-radius: 50%;
    box-shadow: 0 0 0 4px rgba(255, 98, 93, 0.14);
}

.parcel-card__title {
    margin: 0;
    color: #fff;
    font-size: 17px;
    font-weight: 650;
}

.parcel-card__rows {
    margin: 0;
    padding: 8px 18px 12px;
}

.parcel-card__row {
    display: grid;
    grid-template-columns: minmax(95px, 0.8fr) minmax(120px, 1.2fr);
    gap: 16px;
    padding: 9px 0;
    border-bottom: 1px solid rgba(255, 255, 255, 0.07);
}

.parcel-card__row:last-child {
    border-bottom: 0;
}

.parcel-card__row--block {
    grid-template-columns: 1fr;
    gap: 8px;
}

.parcel-card dt {
    overflow-wrap: anywhere;
    color: #8f9ba6;
    font-size: 11px;
    font-weight: 650;
    letter-spacing: 0.035em;
    text-transform: uppercase;
}

.parcel-card dd {
    margin: 0;
    overflow-wrap: anywhere;
    color: #eef2f5;
    font-size: 12px;
    line-height: 1.4;
    text-align: right;
}

.parcel-card__row--block dd {
    text-align: left;
}

.parcel-card__list {
    display: grid;
    gap: 6px;
    margin: 0;
    padding: 0;
    list-style: none;
}

.parcel-card__list li {
    display: grid;
    grid-template-columns: auto 1fr auto auto;
    gap: 10px;
    align-items: baseline;
}

.parcel-card__list-value {
    font-variant-numeric: tabular-nums;
    text-align: right;
}

.parcel-card__list-link {
    font-size: 11px;
}

.parcel-card__list-id {
    min-width: 24px;
    padding: 1px 6px;
    color: #ffb3b0;
    background: rgba(255, 98, 93, 0.14);
    border-radius: 6px;
    font-size: 11px;
    font-weight: 700;
    text-align: center;
}

.parcel-card__link {
    color: #62b0ff;
    font-weight: 700;
    text-decoration: none;
    text-underline-offset: 3px;
}

.parcel-card__link:hover,
.parcel-card__link:focus-visible {
    color: #9cceff;
    text-decoration: underline;
}

.parcel-card__muted {
    color: #8f9ba6;
}

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

@media (max-width: 520px) {
    .overlay {
        top: max(10px, env(safe-area-inset-top));
        left: max(10px, env(safe-area-inset-left));
        gap: 8px;
        width: calc(100vw - 62px);
        max-width: none;
    }

    .toolbar {
        width: 100%;
    }

    .view-switcher span {
        padding: 7px 9px;
        font-size: 11px;
    }

    .search {
        flex: 1 1 100%;
        width: auto;
    }

    .status {
        max-width: 100%;
        padding: 8px 11px;
        font-size: 12px;
    }

    .maplibregl-ctrl-top-right {
        top: max(6px, env(safe-area-inset-top));
        right: max(6px, env(safe-area-inset-right));
    }

    .maplibregl-ctrl-attrib {
        font-size: 9px;
    }

    .parcel-card {
        min-width: min(290px, calc(100vw - 24px));
        max-height: min(var(--card-max-height, 340px), 340px, calc(100vh - 32px));
    }

    .parcel-card__header,
    .parcel-card__rows {
        padding-right: 14px;
        padding-left: 14px;
    }

    .parcel-card__header {
        padding-right: 44px;
    }
}

@media (prefers-reduced-motion: reduce) {
    .status,
    .status__spinner {
        transition: none;
        animation: none;
    }
}
