:root {
    --ink: #2f2c6e;
    --ink-soft: #8d90ad;
    --ink-muted: #6c6f91;
    --surface: #f5f6fc;
    --card: #ffffff;
    --accent: #5a6acf;
    --accent-green: #8fd14f;
    --teal: #c8f2ec;
    --salmon: #ffd9cd;
    --line: #e8eaf5;
    --shadow: 0 8px 24px rgba(66, 72, 140, .08);
}

body {
    margin: 0;
    background: var(--surface);
    color: var(--ink);
    font-family: "Segoe UI", system-ui, -apple-system, Roboto, "Helvetica Neue", Arial, sans-serif;
}

h1, h2 { color: var(--ink); letter-spacing: -.02em; }

.page-subtitle { color: var(--ink-soft); font-size: .9rem; margin: .25rem 0 0; width: 100%; }

/* Struttura */
.app-shell { display: flex; min-height: 100vh; }

.sidebar {
    width: 230px;
    flex: none;
    background: var(--card);
    padding: 2rem 1.5rem 1.5rem;
    display: flex;
    flex-direction: column;
    position: sticky;
    top: 0;
    height: 100vh;
}

.content { flex: 1; min-width: 0; padding: 2rem 2.25rem; }

.profile { text-align: center; margin-bottom: 1.5rem; }

.avatar {
    width: 64px;
    height: 64px;
    margin: 0 auto .75rem;
    border-radius: 50%;
    display: grid;
    place-items: center;
    background: #e4e8fb;
    color: var(--accent);
    font-size: 1.75rem;
}

.profile-name { font-size: .85rem; font-weight: 600; overflow-wrap: anywhere; }

.profile-role {
    color: var(--ink-soft);
    font-size: .65rem;
    letter-spacing: .08em;
    text-transform: uppercase;
    margin-top: .2rem;
}

.nav-title {
    color: var(--ink-soft);
    font-size: .65rem;
    letter-spacing: .08em;
    text-transform: uppercase;
    margin: 1.5rem 0 .5rem;
}

.nav-entry {
    display: flex;
    align-items: center;
    gap: .75rem;
    width: 100%;
    padding: .5rem 0;
    border: 0;
    background: none;
    color: var(--ink-muted);
    font-size: .9rem;
    text-align: left;
    text-decoration: none;
}

.nav-entry i { font-size: 1.05rem; }
.nav-entry.active { color: var(--ink); font-weight: 600; }
.nav-entry.disabled { color: #a9abc4; cursor: default; }
.nav-entry small { margin-left: auto; font-size: .65rem; }
button.nav-entry:hover, a.nav-entry:hover { color: var(--accent); }
.logout { margin-top: auto; }

/* Intestazione */
.page-header {
    display: flex;
    flex-wrap: wrap;
    align-items: center;
    gap: .75rem;
    margin-bottom: 1.25rem;
}

.page-header h1 { font-size: 1.5rem; font-weight: 600; margin: 0; }

.badge-area {
    background: var(--accent-green);
    color: #24510a;
    border-radius: 4px;
    padding: .15rem .6rem;
    font-size: .7rem;
    font-weight: 600;
}

/* Griglia */
.layout { display: grid; grid-template-columns: minmax(0, 1fr) 270px; gap: 1.25rem; }
.main-column, .side-column { display: flex; flex-direction: column; gap: 1.25rem; min-width: 0; }
.bottom-cards { display: grid; grid-template-columns: 1fr 1fr; gap: 1.25rem; }

/* Mappa */
.map-panel { position: relative; }

#map {
    height: calc(100vh - 300px);
    min-height: 420px;
    background: transparent;
    border-radius: 16px;
}

#map.street-mode { background: #e8eaf5; }

.leaflet-container { font-family: inherit; }
.leaflet-control-attribution { font-size: .65rem; border-radius: 6px 0 0 0; }

.map-controls {
    position: absolute;
    left: 50%;
    bottom: 1.25rem;
    transform: translateX(-50%);
    z-index: 500;
    display: flex;
    align-items: center;
    gap: .15rem;
    padding: .25rem .5rem;
    background: #e4e7f6;
    border-radius: 999px;
    white-space: nowrap;
}

.map-controls button {
    border: 0;
    background: none;
    color: var(--ink);
    border-radius: 999px;
    padding: .25rem .6rem;
    font-size: .8rem;
}

.map-controls button:hover { background: #fff; }

.town-dot {
    display: block;
    width: 10px;
    height: 10px;
    border-radius: 50%;
    background: var(--accent);
    border: 2px solid #fff;
}

.focus-marker {
    display: grid;
    place-items: center;
    width: 64px;
    height: 64px;
    border-radius: 50%;
    background: rgba(143, 209, 79, .45);
    border: 2px solid var(--accent-green);
    box-shadow: 0 0 0 5px rgba(143, 209, 79, .25);
}

.focus-marker span {
    width: 14px;
    height: 14px;
    border-radius: 50%;
    background: #fff;
    border: 3px solid #5da42a;
}

/* Card */
.card-soft {
    background: var(--card);
    border-radius: 16px;
    padding: 1.25rem 1.4rem;
    box-shadow: var(--shadow);
}

.card-soft h2 { font-size: 1.05rem; font-weight: 600; }
.card-eyebrow { color: var(--ink-muted); font-size: .8rem; margin: 0 0 .25rem; }
.town-focus.town-focus { font-size: 1.9rem; font-weight: 500; margin-bottom: 1rem; }
.empty-note { color: var(--ink-soft); font-size: .8rem; margin: 0; }

.stat-card {
    display: flex;
    align-items: center;
    gap: .75rem;
    padding: .9rem 1.25rem;
    border-radius: 12px;
    font-size: .9rem;
}

.stat-card i { font-size: 1.25rem; }
.stat-card strong { margin-left: auto; font-size: 1.9rem; font-weight: 500; line-height: 1; }
.stat-teal { background: var(--teal); }
.stat-salmon { background: var(--salmon); }

.towns-card { flex: 1; }
.towns-table { font-size: .8rem; margin-bottom: .75rem; --bs-table-color: var(--ink); --bs-table-border-color: var(--line); }
.towns-table th { color: var(--ink-soft); font-weight: 500; font-size: .7rem; }
.town-link { border: 0; background: none; padding: 0; color: var(--ink); text-align: left; }
.town-link:hover { color: var(--accent); text-decoration: underline; }

.source-list { list-style: none; margin: 0; padding: 0; font-size: .8rem; }
.source-list li { display: flex; justify-content: space-between; gap: 1rem; padding: .3rem 0; }
.source-list span { color: var(--ink-soft); white-space: nowrap; }

/* Ricerca */
.search-form { display: flex; flex-wrap: wrap; align-items: flex-end; gap: .75rem; }
.search-form > div { flex: 1 1 130px; min-width: 0; }
.search-form .form-label { font-size: .75rem; color: var(--ink-muted); margin-bottom: .25rem; }
.search-form .form-select, .search-form .form-control { font-size: .85rem; }
.btn-search { --bs-btn-bg: var(--accent); --bs-btn-border-color: var(--accent); --bs-btn-hover-bg: #4958b8; --bs-btn-hover-border-color: #4958b8; font-size: .85rem; white-space: nowrap; }
.results-head { display: flex; flex-wrap: wrap; align-items: baseline; justify-content: space-between; gap: .5rem; margin-bottom: .5rem; }
.results-count { color: var(--ink-soft); font-weight: 400; }
.results-table td { overflow-wrap: anywhere; }

.import-bar { display: flex; flex-wrap: wrap; align-items: center; gap: .75rem; margin-bottom: .75rem; }
.import-bar .btn-search { margin-left: auto; }
.import-all { font-size: .8rem; display: flex; align-items: center; gap: .4rem; }
.badge-imported { background: var(--teal); border-radius: 4px; padding: .05rem .4rem; font-size: .65rem; white-space: nowrap; }
.badge-review { background: var(--salmon); border-radius: 4px; padding: .05rem .4rem; font-size: .65rem; white-space: nowrap; }
.detail-grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(min(320px, 100%), 1fr)); gap: 1.25rem; }
.page-actions { display: flex; flex-wrap: wrap; gap: .75rem; align-items: center; margin-top: 1rem; font-size: .85rem; }

.stack-form { display: flex; flex-direction: column; gap: .85rem; max-width: 100%; }
.stack-form .form-label, .settings-form .form-label { font-size: .75rem; color: var(--ink-muted); margin-bottom: .25rem; }
.mapping-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(200px, 1fr)); gap: .75rem; }
.reason-list { font-size: .85rem; padding-left: 1.1rem; margin-bottom: .5rem; }
.check-list { display: flex; flex-direction: column; gap: .4rem; font-size: .85rem; }
.check-list label { display: flex; gap: .5rem; align-items: baseline; }
.top-pick { border: 2px solid var(--accent-green); }
.top-score { font-size: 2.2rem; font-weight: 500; line-height: 1; }
.points-input { width: 5.5rem; }
.run-status { font-size: .7rem; font-weight: 600; white-space: nowrap; }

/* Segnaposto per categoria */
.map-pin { display: inline-grid; place-items: center; flex: none; width: 24px; height: 24px; border-radius: 50%; border: 2px solid #fff; color: #fff; font-size: .72rem; box-shadow: 0 1px 4px rgba(47, 44, 110, .45); }
.cat-electrical { background: #e0a100; }
.cat-wholesale { background: #0f766e; }
.cat-trade { background: #475569; }
.cat-car-repair { background: #d9534f; }
.cat-furniture { background: #b5651d; }
.cat-window-construction { background: #16859a; }
.cat-carpenter { background: #7a5c2e; }
.cat-metal-construction { background: #56637a; }
.cat-works { background: #5a6acf; }
.cat-office-company { background: #2f2c6e; }
.cat-office-accountant { background: #2e8b57; }
.cat-office-it { background: #8e44ad; }
.cat-other { background: #6c6f91; }
.cluster-marker { display: grid; place-items: center; border-radius: 50%; background: rgba(90, 106, 207, .9); border: 3px solid rgba(255, 255, 255, .85); color: #fff; font-size: .75rem; font-weight: 600; box-shadow: 0 1px 6px rgba(47, 44, 110, .4); }
.cluster-large { background: rgba(47, 44, 110, .92); font-size: .8rem; }
.map-legend { display: flex; flex-wrap: wrap; gap: .35rem; margin-top: .6rem; }
.map-legend[hidden] { display: none; }
.map-legend button { display: flex; align-items: center; gap: .35rem; border: 1px solid var(--line); background: var(--card); border-radius: 999px; padding: .15rem .6rem .15rem .2rem; font-size: .72rem; color: var(--ink); }
.map-legend button[aria-pressed="false"] { opacity: .45; }

.kpi-grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(140px, 1fr)); gap: .75rem; }
.kpi-grid .card-soft { padding: .8rem 1rem; display: flex; flex-direction: column; gap: .2rem; }
.kpi-grid strong { font-size: 1.5rem; font-weight: 500; }

.order-code { width: 7.5rem; }

/* Sito pubblico */
.hero-sub { font-size: .8rem; color: var(--accent); font-weight: 600; letter-spacing: .06em; text-transform: uppercase; margin-bottom: .75rem; }
.steps { padding-left: 1.2rem; color: var(--ink-muted); }
.steps li { margin-bottom: .5rem; }
.sidebar { overflow-y: auto; }
.nav-title:first-child { margin-top: 0; }
.site-nav { display: flex; flex-wrap: wrap; justify-content: space-between; align-items: center; gap: 1rem; padding: 1rem clamp(1rem, 5vw, 4rem); background: var(--card); }
.site-nav nav { display: flex; flex-wrap: wrap; align-items: center; gap: 1rem; font-size: .9rem; }
.site-nav a { color: var(--ink); text-decoration: none; }
.site-nav .btn:hover { color: #fff; }
.hero { text-align: center; padding: clamp(3rem, 9vw, 6rem) 1.5rem; }
.hero h1 { font-size: clamp(1.9rem, 5vw, 3.1rem); font-weight: 600; max-width: 22ch; margin: 0 auto 1rem; }
.hero p { color: var(--ink-muted); font-size: 1.1rem; max-width: 60ch; margin: 0 auto 2rem; }
.site-section { max-width: 1040px; margin: 0 auto; padding: 2.5rem 1.5rem; }
.site-section h2 { font-size: 1.5rem; font-weight: 600; margin-bottom: 1.25rem; }
.site-grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(240px, 1fr)); gap: 1.25rem; }
.site-grid h3 { font-size: 1.05rem; font-weight: 600; color: var(--ink); }
.site-grid p { color: var(--ink-muted); font-size: .9rem; margin: 0; }
.site-icon { color: var(--accent); font-size: 1.5rem; display: block; margin-bottom: .5rem; }
.demo-form { max-width: 620px; }
.honeypot { position: absolute; left: -9999px; }
.site-footer { display: flex; justify-content: center; gap: 1.5rem; padding: 2rem; color: var(--ink-muted); font-size: .85rem; }

/* Login */
.login-page { display: grid; place-items: center; min-height: 100vh; padding: 1rem; }

.login-card {
    width: min(100%, 400px);
    background: var(--card);
    border-radius: 20px;
    padding: 2.5rem;
    box-shadow: var(--shadow);
}

.login-card h1 { font-size: 1.5rem; font-weight: 600; margin: 1.5rem 0 0; }
.login-card form { margin-top: 1.5rem; }
.login-card .alert { margin: 1rem 0 0; font-size: .85rem; }
.login-card .form-label { font-size: .85rem; color: var(--ink-muted); }
.login-card .btn-primary { --bs-btn-bg: var(--accent); --bs-btn-border-color: var(--accent); --bs-btn-hover-bg: #4958b8; --bs-btn-hover-border-color: #4958b8; }

.brand { display: flex; align-items: center; gap: .5rem; font-weight: 600; }
.brand-mark { width: 14px; height: 14px; border-radius: 50%; background: var(--accent-green); box-shadow: 0 0 0 4px rgba(143, 209, 79, .3); }

@media (max-width: 1100px) {
    .layout { grid-template-columns: 1fr; }
}

.mobile-bar { display: none; }
/* Tiene dentro lo scorrimento anche le intestazioni .visually-hidden, che sono in position: absolute. */
.table-responsive { position: relative; }

@media (max-width: 760px) {
    .app-shell { flex-direction: column; }
    .sidebar { width: 100%; height: auto; max-height: 100vh; position: sticky; top: 0; z-index: 1100; padding: .5rem 1rem; box-shadow: var(--shadow); }
    .mobile-bar { display: flex; align-items: center; justify-content: space-between; }
    .nav-toggle { display: grid; place-items: center; width: 44px; height: 44px; border: 0; background: none; color: var(--ink); font-size: 1.6rem; }
    .sidebar.nav-closed > :not(.mobile-bar) { display: none; }
    .sidebar .profile { display: none; }
    .nav-entry { min-height: 44px; }
    .nav-title { margin: .75rem 0 .1rem; }
    .logout { margin-top: .5rem; }

    .content { padding: 1rem; }
    .bottom-cards { grid-template-columns: 1fr; }
    .page-header h1 { font-size: 1.3rem; }
    .card-soft { padding: 1rem; }
    .layout, .main-column, .side-column { gap: .9rem; }

    /* Niente zoom automatico su iPhone e bersagli facili da toccare. */
    input.form-control, select.form-select, textarea.form-control { font-size: 16px !important; min-height: 44px; }
    input.form-check-input { width: 1.4rem; height: 1.4rem; flex: none; }
    .btn, .btn-sm { min-height: 44px; display: inline-flex; align-items: center; justify-content: center; gap: .35rem; }
    .search-form > div { flex-basis: 100%; }
    .search-form > .btn, .import-bar .btn-search { width: 100%; margin-left: 0; }
    .stack-form .btn-search { width: 100%; }
    .page-actions form, .page-actions .btn { flex: 1 1 auto; }
    .page-actions form .btn { width: 100%; }
    .town-link { min-height: 44px; }
    .points-input { width: 5rem; }

    #map { height: 62vh; min-height: 320px; }
    .map-controls { bottom: .75rem; padding: .2rem .4rem; }
    .map-controls button { min-height: 44px; min-width: 44px; }
    .map-legend button { min-height: 40px; font-size: .78rem; }
    .leaflet-control-attribution { font-size: .58rem; }

    .table-responsive { -webkit-overflow-scrolling: touch; }
    .table-responsive table { min-width: 560px; }
    .towns-table td, .towns-table th { white-space: normal; }
    .towns-card .towns-table { min-width: 0; }

    /* Sito pubblico */
    .site-nav { padding: .5rem 1rem; gap: .25rem .75rem; }
    .site-nav nav { width: 100%; justify-content: space-between; gap: .25rem .75rem; font-size: .85rem; }
    .site-nav nav a { min-height: 44px; display: inline-flex; align-items: center; }
    .hero { padding: 2.5rem 1rem; }
    .hero .btn { width: 100%; white-space: normal; }
    .site-section { padding: 1.5rem 1rem; }
    .site-grid { grid-template-columns: 1fr; gap: .9rem; }
    .site-footer a { min-height: 44px; display: inline-flex; align-items: center; }
    .login-card { padding: 1.5rem; }
}
