/* ==========================================================================
   Mappatura M9 – stile
   Font: Atkinson Hyperlegible (progettato per la massima leggibilità).
   ========================================================================== */

:root {
    --ink: #14213d;
    --ink-soft: #475569;
    --paper: #f3f5f8;
    --surface: #ffffff;
    --line: #d9dfe8;
    --brand: #A60D27;
    --brand-ink: #ffffff;
    --focus: #1d4ed8;
    --radius: 14px;
    --shadow: 0 12px 32px rgba(20, 33, 61, .22);
    --topbar-h: 64px;
    --sidebar-w: 360px;
    --detail-w: 420px;
    --font: "Atkinson Hyperlegible", system-ui, -apple-system, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
}

*, *::before, *::after { box-sizing: border-box; }

html, body { height: 100%; }

body {
    margin: 0;
    display: flex;
    flex-direction: column;
    height: 100vh;
    height: 100dvh;
    background: var(--paper);
    color: var(--ink);
    font: 400 1rem/1.5 var(--font);
    -webkit-text-size-adjust: 100%;
}

h1, h2, h3, p, ul, dl, dd { margin: 0; }
ul { padding: 0; list-style: none; }
button, input { font: inherit; color: inherit; }
a { color: #1d4ed8; text-underline-offset: 2px; }

:focus-visible {
    outline: 3px solid var(--focus);
    outline-offset: 2px;
}

.sr {
    position: absolute !important;
    width: 1px; height: 1px;
    margin: -1px; padding: 0; border: 0;
    overflow: hidden; clip: rect(0 0 0 0); white-space: nowrap;
}

.skip {
    position: absolute;
    left: 12px; top: -60px;
    z-index: 5000;
    padding: 10px 16px;
    background: #fff; color: var(--ink);
    border-radius: 8px;
    font-weight: 700;
}
.skip:focus { top: 8px; }

/* ---------- Barra superiore ---------- */

.topbar {
    flex: 0 0 var(--topbar-h);
    display: flex;
    align-items: center;
    gap: 16px;
    padding: 0 16px;
    padding-left: max(16px, env(safe-area-inset-left));
    padding-right: max(16px, env(safe-area-inset-right));
    background: var(--brand);
    color: var(--brand-ink);
    z-index: 30;
}
.brand { min-width: 0; flex: 1; }
.brand h1 { font-size: 1.25rem; line-height: 1.2; font-weight: 700; }
.brand p { font-size: .875rem; opacity: .85; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }

.btn {
    display: inline-flex; align-items: center; gap: 8px;
    min-height: 44px; padding: 0 14px;
    border: 1px solid transparent; border-radius: 10px;
    background: transparent; cursor: pointer;
    text-decoration: none; font-weight: 700;
}
.btn-ghost { color: var(--brand-ink); border-color: rgba(255, 255, 255, .35); }
.btn-ghost:hover { background: rgba(255, 255, 255, .12); }
.topbar :focus-visible { outline-color: #fff; }

.count-badge {
    display: inline-grid; place-items: center;
    min-width: 22px; height: 22px; padding: 0 6px;
    border-radius: 11px;
    background: #fff; color: var(--ink);
    font-size: .8rem; font-weight: 700;
}

#btn-sidebar { display: none; }

/* ---------- Layout ---------- */

.app {
    flex: 1 1 auto;
    min-height: 0;
    display: grid;
    grid-template-columns: var(--sidebar-w) minmax(0, 1fr);
    position: relative;
}

.mapwrap { position: relative; min-width: 0; min-height: 0; }
#map { position: absolute; inset: 0; z-index: 0; background: #dfe5ec; }

.fatal { padding: 32px 20px; max-width: 640px; margin: 0 auto; }
.fatal h2 { margin-bottom: 8px; }

.noscript { position: absolute; inset: 0; z-index: 10; overflow: auto; padding: 20px; background: var(--surface); }
.noscript li { margin: 8px 0; }

/* ---------- Pannello laterale (filtri / elenco) ---------- */

.sidebar {
    display: flex; flex-direction: column;
    min-height: 0;
    background: var(--surface);
    border-right: 1px solid var(--line);
    z-index: 20;
}

.sidebar-head {
    display: flex; align-items: center; justify-content: space-between; gap: 8px;
    padding: 14px 16px 8px;
}

.seg {
    display: inline-flex;
    padding: 3px;
    background: var(--paper);
    border: 1px solid var(--line);
    border-radius: 12px;
}
.seg button {
    min-height: 40px; padding: 0 18px;
    border: 0; border-radius: 9px;
    background: transparent; cursor: pointer; font-weight: 700;
    color: var(--ink-soft);
}
.seg button[aria-pressed="true"] { background: var(--brand); color: var(--brand-ink); }

.btn-icon {
    display: inline-grid; place-items: center;
    width: 44px; height: 44px;
    border: 0; border-radius: 10px;
    background: transparent; cursor: pointer;
}
.btn-icon:hover { background: var(--paper); }
.sidebar-close { display: none; }

.summary {
    display: flex; align-items: center; justify-content: space-between; gap: 12px;
    padding: 4px 16px 10px;
    border-bottom: 1px solid var(--line);
    color: var(--ink-soft);
    font-size: .9375rem;
}
.btn-link {
    min-height: 40px; padding: 0 4px;
    border: 0; background: none; cursor: pointer;
    color: #1d4ed8; font-weight: 700; text-decoration: underline; text-underline-offset: 3px;
}
.btn-link[disabled] { color: var(--ink-soft); text-decoration: none; cursor: default; opacity: .6; }

.panel { flex: 1 1 auto; min-height: 0; overflow-y: auto; overscroll-behavior: contain; padding-bottom: 24px; }
.panel[hidden] { display: none; }

.search { padding: 14px 16px 6px; }
.search label { display: block; margin-bottom: 6px; font-weight: 700; font-size: .9375rem; }
.search input {
    width: 100%; min-height: 46px; padding: 0 14px;
    border: 1.5px solid #94a3b8; border-radius: 10px; background: #fff;
}
.search input::placeholder { color: #64748b; }

.facet { border-bottom: 1px solid var(--line); }
.facet summary {
    display: flex; align-items: center; justify-content: space-between; gap: 8px;
    min-height: 52px; padding: 0 16px;
    cursor: pointer; font-weight: 700; list-style: none;
}
.facet summary::-webkit-details-marker { display: none; }
.facet summary::after {
    content: ""; width: 9px; height: 9px; margin-left: auto;
    border-right: 2px solid currentColor; border-bottom: 2px solid currentColor;
    transform: rotate(45deg); transition: transform .15s;
}
.facet[open] summary::after { transform: rotate(-135deg); }
.facet summary .sel-n {
    display: inline-grid; place-items: center;
    min-width: 22px; height: 22px; padding: 0 6px;
    border-radius: 11px; background: var(--brand); color: #fff;
    font-size: .8rem;
}
.facet-body { padding: 0 8px 12px; }

.opt { position: relative; }
.opt input {
    position: absolute; left: 14px; top: 50%; width: 20px; height: 20px; margin: -10px 0 0;
    accent-color: var(--brand); cursor: pointer;
}
.opt label {
    display: flex; align-items: center; gap: 10px;
    min-height: 44px; padding: 6px 10px 6px 46px;
    border-radius: 10px; cursor: pointer;
    line-height: 1.3;
}
.opt label:hover { background: var(--paper); }
.opt input:focus-visible + label { outline: 3px solid var(--focus); outline-offset: -1px; }
.opt-txt { flex: 1; min-width: 0; }
.opt-n { color: var(--ink-soft); font-size: .875rem; font-variant-numeric: tabular-nums; }
.opt.is-zero label { opacity: .55; }

.inclall { margin: 4px 6px 0; }
.inclall label {
    display: flex; align-items: center; gap: 10px;
    min-height: 44px; padding: 6px 10px; cursor: pointer;
    font-size: .9375rem; color: var(--ink-soft);
}
.inclall input { width: 20px; height: 20px; accent-color: var(--brand); }

/* Pallino colorato con icona (legenda dei pin) */
.badge-ico {
    flex: 0 0 auto;
    display: inline-grid; place-items: center;
    width: 26px; height: 26px; border-radius: 50%;
    background: var(--c, #475569);
}
.badge-ico svg { display: block; }
/* Icona personalizzata (file): forma colorata a piacere, direttamente sul cerchio del pin */
.badge-ico .glyph {
    display: block; width: 15px; height: 15px;
    -webkit-mask-repeat: no-repeat; mask-repeat: no-repeat;
    -webkit-mask-position: center; mask-position: center;
    -webkit-mask-size: contain; mask-size: contain;
}
/* ...oppure con i colori originali del file: su fondo bianco con bordo del colore del pin */
.badge-ico.has-img { background: #fff; box-shadow: inset 0 0 0 3px var(--c); }
.badge-ico.has-img .glyph { width: 14px; height: 14px; object-fit: contain; }
/* Elenco risultati */
.results {
    display: flex; flex-direction: column; gap: 10px;
    padding: 12px 14px 8px;
}
.result {
    display: flex; align-items: flex-start; gap: 12px;
    width: 100%; padding: 12px 14px 12px 12px;
    border: 1px solid var(--line);
    border-left: 6px solid var(--c, var(--line));
    border-radius: 12px;
    background: var(--surface);
    text-align: left; cursor: pointer;
    transition: background-color .15s, border-color .15s, box-shadow .15s;
}
.result:hover { background: var(--paper); box-shadow: 0 2px 8px rgba(20, 33, 61, .08); }
.result[aria-current="true"] { background: #e8edf6; border-color: var(--brand); border-left-color: var(--c, var(--brand)); }
.result-ico { flex: 0 0 auto; padding-top: 1px; }
.result-main { flex: 1; min-width: 0; display: flex; flex-direction: column; gap: 4px; }
.result-name { font-size: 1rem; line-height: 1.25; font-weight: 700; overflow-wrap: anywhere; }
.result-ente { font-size: .875rem; color: var(--ink-soft); line-height: 1.3; }
.result-tags { display: flex; flex-wrap: wrap; gap: 6px; margin-top: 2px; }
.tag {
    padding: 2px 10px;
    border-radius: 999px;
    background: var(--paper);
    border: 1px solid var(--line);
    font-size: .8125rem; line-height: 1.4;
}
.result-addr {
    display: flex; align-items: flex-start; gap: 6px;
    margin-top: 2px;
    font-size: .875rem; color: var(--ink-soft); line-height: 1.35;
}
.result-addr svg { flex: 0 0 auto; margin-top: 3px; }
.result-addr em { font-style: normal; color: #92400e; }
.empty { padding: 28px 20px; text-align: center; color: var(--ink-soft); list-style: none; }

/* ---------- Pannello dei dettagli ---------- */

.detail {
    position: absolute; z-index: 5;
    top: 12px; right: 12px; bottom: 12px;
    width: min(var(--detail-w), calc(100% - 24px));
    display: flex; flex-direction: column;
    background: var(--surface);
    border-radius: var(--radius);
    box-shadow: var(--shadow);
    overflow: hidden;
    visibility: hidden;
    transform: translateX(calc(100% + 24px));
    transition: transform .25s ease, visibility 0s linear .25s;
}
.detail.open {
    visibility: visible;
    transform: none;
    transition: transform .25s ease, visibility 0s;
}
#detail-content { display: flex; flex-direction: column; min-height: 0; flex: 1; }

.detail-head {
    position: relative;
    padding: 18px 64px 16px 20px;
    border-top: 8px solid var(--c, #475569);
    border-bottom: 1px solid var(--line);
}
.type-chip {
    display: inline-flex; align-items: center; gap: 8px;
    margin-bottom: 8px; padding: 3px 12px 3px 3px;
    border-radius: 999px; background: var(--paper);
    font-size: .875rem; font-weight: 700; line-height: 1.25;
}
.detail-head h2 { font-size: 1.375rem; line-height: 1.2; font-weight: 700; }
.detail-head h2:focus { outline: none; }
.detail-head h2:focus-visible { outline: 3px solid var(--focus); outline-offset: 4px; border-radius: 4px; }
.detail-close {
    position: absolute; top: 12px; right: 10px;
}

.detail-body { flex: 1; min-height: 0; overflow-y: auto; overscroll-behavior: contain; padding: 8px 20px 24px; }

.fields > div { padding: 12px 0; border-bottom: 1px solid var(--line); }
.fields dt { font-size: .875rem; font-weight: 700; color: var(--ink-soft); margin-bottom: 2px; }
.fields dd { overflow-wrap: anywhere; }
.fields dd.pre { white-space: pre-line; }
.fields dd ul li + li { margin-top: 2px; }
.fields .inline { display: inline-flex; align-items: center; gap: 8px; }

.note {
    margin-top: 12px; padding: 10px 12px;
    border-radius: 10px; background: #fff7e0; border: 1px solid #ecd48a;
    font-size: .9375rem;
}

.actions { display: flex; flex-wrap: wrap; gap: 10px; margin-top: 18px; }
.actions .btn { background: var(--brand); color: #fff; }
.actions .btn:hover { background: #1e3157; }
.actions .btn-outline { background: #fff; color: var(--brand); border-color: var(--brand); }
.actions .btn-outline:hover { background: var(--paper); }
.actions .btn[disabled] { opacity: .7; cursor: progress; }

.same { margin-top: 22px; }
.same h3 { font-size: 1rem; margin-bottom: 6px; }
.linkbtn {
    display: flex; align-items: center; gap: 10px;
    width: 100%; min-height: 44px; padding: 6px 8px;
    border: 0; border-radius: 10px; background: transparent; cursor: pointer; text-align: left;
}
.linkbtn:hover { background: var(--paper); }

/* ---------- Avviso di caricamento ---------- */

.toast {
    position: absolute; z-index: 4;
    left: 50%; top: 12px; transform: translateX(-50%);
    max-width: calc(100% - 24px);
    padding: 9px 16px;
    border-radius: 999px;
    background: var(--brand); color: #fff;
    font-size: .9375rem; box-shadow: var(--shadow);
}
.toast[hidden] { display: none; }

/* ---------- Pin e cluster sulla mappa ---------- */

.pin, .cluster-wrap { background: none; border: 0; }
.pin-inner {
    position: relative;
    width: 38px; height: 46px;
    filter: drop-shadow(0 2px 2px rgba(0, 0, 0, .35));
    transform-origin: 50% 100%;
    transition: transform .15s ease;
}
.pin-inner svg { display: block; }
.pin-glyph {
    position: absolute; left: 8.5px; top: 7px;
    width: 21px; height: 21px;
    object-fit: contain;
    -webkit-mask-repeat: no-repeat; mask-repeat: no-repeat;
    -webkit-mask-position: center; mask-position: center;
    -webkit-mask-size: contain; mask-size: contain;
}
.pin:hover .pin-inner, .pin:focus-visible .pin-inner { transform: scale(1.15); }
.pin.is-selected .pin-inner { transform: scale(1.3); }
.pin.is-approx .pin-inner { opacity: .8; }
.leaflet-marker-icon:focus-visible { outline: 3px solid var(--focus); outline-offset: 2px; border-radius: 12px; }

.cluster {
    width: 42px; height: 42px;
    display: grid; place-items: center;
    border: 3px solid #fff; border-radius: 50%;
    background: var(--brand); color: #fff;
    font-weight: 700;
    box-shadow: 0 0 0 4px rgba(20, 33, 61, .25), 0 3px 8px rgba(0, 0, 0, .3);
}

.leaflet-control-zoom a { width: 40px !important; height: 40px !important; line-height: 40px !important; }
.leaflet-container { font-family: var(--font); }

/* ---------- Mobile / tablet ---------- */

.backdrop {
    position: fixed; inset: var(--topbar-h) 0 0 0; z-index: 40;
    background: rgba(15, 23, 42, .45);
}
.backdrop[hidden] { display: none; }

@media (max-width: 860px) {
    :root { --topbar-h: 60px; }

    #btn-sidebar { display: inline-flex; }
    .btn-download span { display: none; }
    .btn-download { padding: 0 12px; }
    .brand h1 { font-size: 1.0625rem; }
    .brand p { font-size: .8125rem; }

    .app { display: block; }
    .mapwrap { position: absolute; inset: 0; }

    .sidebar {
        position: fixed; z-index: 50;
        top: var(--topbar-h); bottom: 0; left: 0;
        width: min(92vw, 400px);
        box-shadow: var(--shadow);
        visibility: hidden;
        transform: translateX(-102%);
        transition: transform .25s ease, visibility 0s linear .25s;
    }
    .sidebar.open {
        visibility: visible;
        transform: none;
        transition: transform .25s ease, visibility 0s;
    }
    .sidebar-close { display: inline-grid; }

    /* I dettagli diventano un pannello che sale dal basso */
    .detail {
        top: auto; left: 0; right: 0; bottom: 0;
        width: auto; max-height: 62%;
        border-radius: 18px 18px 0 0;
        padding-bottom: env(safe-area-inset-bottom);
        transform: translateY(105%);
    }
    .detail.open { transform: none; }
    .detail-head { padding: 14px 60px 12px 18px; border-top-width: 6px; border-radius: 18px 18px 0 0; }
    .detail-head h2 { font-size: 1.25rem; }
    .detail-body { padding: 4px 18px 20px; }
}

@media (max-width: 480px) {
    .brand p { display: none; }
}

@media (prefers-reduced-motion: reduce) {
    *, *::before, *::after { transition: none !important; animation: none !important; }
}

@media (forced-colors: active) {
    .badge-ico, .cluster { border: 2px solid CanvasText; }
}
