:root {
    --blu: #14606b;
    --grigio: #f3f4f6;
    --testo: #1f2937;
    --bordo: #e5e7eb;
    --verde: #16a34a;
    --rosso: #dc2626;
    --giallo: #d97706;
}

* { box-sizing: border-box; }

body {
    font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
    background: var(--grigio);
    color: var(--testo);
    margin: 0;
}

.topbar {
    background: #fff;
    border-bottom: 1px solid var(--bordo);
    padding: 12px 24px;
    display: flex;
    align-items: center;
    justify-content: space-between;
    flex-wrap: wrap;
}

.topbar .logo {
    font-weight: 700;
    font-size: 1.2rem;
    color: var(--blu);
    text-decoration: none;
}

.topbar nav {
    flex: 1;
    display: flex;
    justify-content: center;
    align-items: center;
}
.topbar nav a {
    margin-left: 16px;
    text-decoration: none;
    color: var(--testo);
}

.topbar nav a:hover { color: var(--blu); }

.topbar .utente {
    margin-left: 16px;
    color: #6b7280;
    font-size: 0.9rem;
}

.ricerca-globale {
    margin-left: auto;
}
.ricerca-globale input {
    padding: 6px 12px;
    border-radius: 20px;
    border: 1px solid var(--bordo);
    font-size: 0.85rem;
    width: 200px;
}

.container {
    max-width: 1100px;
    margin: 24px auto;
    padding: 0 16px;
}

h1 { margin-top: 0; }

.login-box {
    max-width: 360px;
    margin: 80px auto;
    background: #fff;
    padding: 32px;
    border-radius: 10px;
    box-shadow: 0 1px 3px rgba(0,0,0,0.1);
}

.login-box h1 { text-align: center; color: var(--blu); }

form.form-scheda, .login-box form {
    display: flex;
    flex-direction: column;
    gap: 12px;
    background: #fff;
    padding: 20px;
    border-radius: 10px;
    box-shadow: 0 1px 3px rgba(0,0,0,0.08);
}

.form-scheda label, .login-box label {
    display: flex;
    flex-direction: column;
    font-size: 0.85rem;
    color: #4b5563;
    gap: 4px;
}

.riga-doppia {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 12px;
}

input, select, textarea, button {
    font-family: inherit;
    font-size: 1rem;
    padding: 8px 10px;
    border: 1px solid var(--bordo);
    border-radius: 6px;
}

button {
    background: var(--blu);
    color: #fff;
    border: none;
    cursor: pointer;
    font-weight: 600;
}
button:hover { background: #0e4850; }

.due-colonne {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 24px;
    align-items: start;
}
@media (max-width: 800px) {
    .due-colonne { grid-template-columns: 1fr; }
    .riga-doppia { grid-template-columns: 1fr; }
}

.tabella {
    width: 100%;
    border-collapse: collapse;
    background: #fff;
    border-radius: 8px;
    overflow: hidden;
}
.tabella th, .tabella td {
    padding: 10px 12px;
    border-bottom: 1px solid var(--bordo);
    text-align: left;
    font-size: 0.9rem;
}
.tabella th { background: #f9fafb; }
.tabella tr.scaduta { background: #fef2f2; }

.filtri {
    display: flex;
    gap: 10px;
    margin-bottom: 16px;
    flex-wrap: wrap;
}
.filtri a {
    padding: 6px 14px;
    border-radius: 20px;
    background: #fff;
    border: 1px solid var(--bordo);
    text-decoration: none;
    color: var(--testo);
    font-size: 0.9rem;
}
.filtri a.attivo { background: var(--blu); color: #fff; border-color: var(--blu); }

.badge {
    padding: 3px 10px;
    border-radius: 20px;
    font-size: 0.78rem;
    color: #fff;
    background: #9ca3af;
}
.badge.stato-da_contattare { background: #9ca3af; }
.badge.stato-contattato { background: #60a5fa; }
.badge.stato-interessato { background: #fbbf24; color:#1f2937; }
.badge.stato-in_trattativa { background: var(--giallo); }
.badge.stato-attivato { background: var(--verde); }
.badge.stato-non_interessato { background: #d1d5db; color:#1f2937; }
.badge.stato-perso { background: var(--rosso); }

.scheda-azione {
    background: #fff;
    border-radius: 8px;
    padding: 12px 14px;
    margin-bottom: 10px;
    border-left: 4px solid #9ca3af;
}
.scheda-azione.fatta { border-left-color: var(--verde); opacity: 0.8; }
.scheda-azione.da_fare { border-left-color: var(--giallo); }

.nota-data { color: #6b7280; font-size: 0.82rem; margin: 4px 0; }
.nota-esito { background: #f0fdf4; padding: 8px; border-radius: 6px; font-size: 0.88rem; }

.form-inline {
    display: flex;
    gap: 8px;
    margin-top: 8px;
}
.form-inline textarea { flex: 1; }

.errore { color: var(--rosso); font-weight: 600; }
.successo { color: var(--verde); font-weight: 600; }

.storico { font-size: 0.85rem; color: #4b5563; padding-left: 18px; }

/* --- Pulsanti azioni rapide (chiama, whatsapp, email, sito) --- */
.azioni-rapide {
    display: flex;
    flex-wrap: wrap;
    gap: 8px;
    margin-bottom: 16px;
}
.pulsante-rapido {
    background: #fff;
    border: 1px solid var(--bordo);
    border-radius: 20px;
    padding: 8px 16px;
    text-decoration: none;
    color: var(--testo);
    font-size: 0.9rem;
    font-weight: 600;
}
.pulsante-rapido:hover { border-color: var(--blu); color: var(--blu); }

.link-copia-wa {
    font-size: 0.78rem;
    color: var(--blu);
    text-decoration: none;
    align-self: flex-start;
}
.link-copia-wa:hover { text-decoration: underline; }

/* --- Tabella con righe cliccabili --- */
.riga-cliccabile { cursor: pointer; }
.riga-cliccabile:hover { background: #f9fafb; }

/* Su desktop le etichette non servono: le mostra già il thead */
.etichetta-mobile { display: none; }

/* --- Nota troncata, espandibile al click --- */
.nota-troncata {
    display: -webkit-box;
    -webkit-line-clamp: 1;
    -webkit-box-orient: vertical;
    overflow: hidden;
    max-width: 220px;
    cursor: pointer;
    color: var(--blu);
    text-decoration: underline dotted;
}
.nota-troncata.espansa {
    -webkit-line-clamp: unset;
    max-width: none;
    white-space: normal;
}

/* --- Tabella scorrevole orizzontalmente su schermi piccoli --- */
.tabella-scroll {
    overflow-x: auto;
    -webkit-overflow-scrolling: touch;
    border-radius: 8px;
}

/* --- Responsive: telefono --- */
@media (max-width: 640px) {
    .container {
        margin: 12px auto;
        padding: 0 8px;
    }

    .topbar {
        padding: 10px 12px;
        gap: 8px;
    }
    .topbar nav {
        display: flex;
        flex-wrap: wrap;
        gap: 4px 14px;
        align-items: center;
        width: 100%;
    }
    .topbar nav a {
        margin-left: 0;
        padding: 6px 0;
    }
    .topbar .utente {
        margin-left: 0;
        width: 100%;
    }
    .ricerca-globale {
        margin-left: 0;
        width: 100%;
        order: 9;
    }
    .ricerca-globale input {
        width: 100%;
        box-sizing: border-box;
    }

    h1 { font-size: 1.35rem; }
    h2 { font-size: 1.1rem; }

    input, select, textarea, button {
        font-size: 16px; /* evita lo zoom automatico di iOS sui campi */
        padding: 10px 12px;
    }
    button {
        width: 100%;
        padding: 12px;
    }
    .form-inline {
        flex-direction: column;
    }
    .form-inline button {
        width: 100%;
    }

    .filtri {
        flex-direction: column;
        align-items: stretch;
    }
    .filtri input, .filtri select, .filtri button {
        width: 100%;
    }
    .filtri a {
        text-align: center;
    }

    .tabella th, .tabella td {
        padding: 8px;
        font-size: 0.82rem;
    }

    /* Tabella negozi: su mobile ogni riga diventa un blocco con
       etichetta + valore impilati, invece di scorrere in orizzontale.
       Le etichette sono HTML vero (non CSS ::before) per un bug di
       Safari iOS che nasconde il testo "nudo" accanto a contenuto
       generato via CSS dentro celle flex. */
    .tabella-righe-cliccabili { min-width: 0; }
    .tabella-righe-cliccabili thead { display: none; }
    .tabella-righe-cliccabili tr {
        display: block;
        background: #fff;
        border-radius: 10px;
        margin-bottom: 10px;
        padding: 8px 10px;
        box-shadow: 0 1px 3px rgba(0,0,0,0.08);
        border-bottom: none;
    }
    .tabella-righe-cliccabili td {
        display: flex;
        justify-content: space-between;
        align-items: flex-start;
        gap: 8px;
        padding: 6px 0;
        border-bottom: 1px solid var(--bordo);
        font-size: 0.82rem;
    }
    .tabella-righe-cliccabili td:last-child { border-bottom: none; }
    .tabella-righe-cliccabili .etichetta-mobile {
        display: block;
        font-weight: 600;
        color: var(--testo);
        text-align: left;
        flex-shrink: 0;
        max-width: 38%;
    }
    .tabella-righe-cliccabili .valore-mobile {
        text-align: right;
        word-break: break-word;
        overflow-wrap: anywhere;
        min-width: 0;
        flex: 1;
    }
    .tabella-righe-cliccabili td.cella-nome {
        font-size: 1rem;
        font-weight: 600;
    }
    .tabella-righe-cliccabili td.cella-nome .etichetta-mobile {
        display: none;
    }
}

/* ==========================================================
   MODALITÀ SCURA
   Attivata aggiungendo data-theme="scuro" sul tag <html>
   (gestito dal pulsante 🌙/☀️ e salvato in localStorage)
   ========================================================== */
:root[data-theme="scuro"] {
    --grigio: #14181d;
    --testo: #e5e7eb;
    --bordo: #313842;
}

[data-theme="scuro"] body { background: var(--grigio); color: var(--testo); }

[data-theme="scuro"] .topbar {
    background: #1b2129;
    border-bottom-color: var(--bordo);
}
[data-theme="scuro"] .topbar nav a { color: var(--testo); }
[data-theme="scuro"] .topbar .utente { color: #9aa4b2; }
[data-theme="scuro"] .pulsante-tema {
    background: transparent;
    border: 1px solid var(--bordo);
    color: var(--testo);
    border-radius: 20px;
    padding: 4px 10px;
    margin-left: 12px;
    cursor: pointer;
}

[data-theme="scuro"] .login-box,
[data-theme="scuro"] form.form-scheda,
[data-theme="scuro"] .login-box form,
[data-theme="scuro"] .tabella,
[data-theme="scuro"] .scheda-azione,
[data-theme="scuro"] .pulsante-rapido,
[data-theme="scuro"] .tabella-righe-cliccabili tr {
    background: #1b2129;
    color: var(--testo);
    box-shadow: none;
    border: 1px solid var(--bordo);
}

[data-theme="scuro"] input,
[data-theme="scuro"] select,
[data-theme="scuro"] textarea {
    background: #242b34;
    color: var(--testo);
    border-color: var(--bordo);
}
[data-theme="scuro"] input::placeholder,
[data-theme="scuro"] textarea::placeholder {
    color: #6b7280;
}

[data-theme="scuro"] .tabella th { background: #20262e; color: var(--testo); }
[data-theme="scuro"] .tabella td,
[data-theme="scuro"] .tabella-righe-cliccabili td { border-bottom-color: var(--bordo); }
[data-theme="scuro"] .riga-cliccabile:hover { background: #242b34; }
[data-theme="scuro"] .tabella tr.scaduta { background: #2a1e1e; }

[data-theme="scuro"] .filtri a {
    background: #1b2129;
    border-color: var(--bordo);
    color: var(--testo);
}
[data-theme="scuro"] .ricerca-globale input {
    background: #1b2129;
    border-color: var(--bordo);
    color: var(--testo);
}

[data-theme="scuro"] .nota-esito { background: #16231a; color: #d1fae5; }
[data-theme="scuro"] .nota-data { color: #9aa4b2; }
[data-theme="scuro"] .storico { color: #9aa4b2; }
[data-theme="scuro"] .link-copia-wa { color: #7dd3d8; }
[data-theme="scuro"] a { color: #7dd3d8; }

[data-theme="scuro"] h1,
[data-theme="scuro"] h2,
[data-theme="scuro"] h3 { color: var(--testo); }

/* --- Banda riepilogativa stati negozi (solo desktop) --- */
.banda-stati {
    display: flex;
    width: 100%;
}
.banda-stati-cella {
    flex: 1;
    padding: 10px 8px;
    text-align: center;
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 2px;
}
.banda-stati-numero {
    font-size: 1.15rem;
    font-weight: 700;
}
.banda-stati-etichetta {
    font-size: 0.72rem;
    text-transform: uppercase;
    letter-spacing: 0.03em;
}
.banda-stati-cella.testo-scuro { color: #1f2937; }
.banda-stati-cella.testo-chiaro { color: #ffffff; }
.banda-stati-percentuale {
    background: #0e4850;
    flex: 1.3;
}

/* Nascosta su schermi piccoli: pensata solo per desktop */
@media (max-width: 900px) {
    .banda-stati { display: none; }
}

/* Seconda banda: solo i dati dell'utente collegato, alta la metà,
   con numero ed etichetta affiancati sulla stessa riga */
.banda-stati-personale .banda-stati-cella {
    flex-direction: row;
    gap: 6px;
    padding: 5px 8px;
}
.banda-stati-personale .banda-stati-numero {
    font-size: 0.95rem;
}
.banda-stati-personale .banda-stati-etichetta {
    font-size: 0.68rem;
}

/* --- Campi "a tendina con possibilità di aggiungere una voce nuova" ---
   Stessa freccina e stesso aspetto di un <select>, ma restano campi di
   testo liberi con suggerimenti (datalist): l'utente può scegliere un
   valore già usato in passato oppure scriverne uno nuovo al volo. */
.campo-con-suggerimenti {
    -webkit-appearance: none;
    appearance: none;
    background-color: #fff;
    background-image: url("data:image/svg+xml;charset=UTF-8,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24' fill='none' stroke='%236b7280' stroke-width='2' stroke-linecap='round' stroke-linejoin='round'%3E%3Cpolyline points='6 9 12 15 18 9'%3E%3C/polyline%3E%3C/svg%3E");
    background-repeat: no-repeat !important;
    background-position: right 10px center !important;
    background-size: 16px !important;
    padding-right: 34px;
}
/* Safari (iOS/iPadOS) aggiunge una sua freccina nativa ai campi con
   datalist quando hanno più di un carattere di larghezza disponibile:
   questa regola specifica la sopprime del tutto */
.campo-con-suggerimenti::-webkit-calendar-picker-indicator {
    display: none !important;
}
[data-theme="scuro"] .campo-con-suggerimenti {
    background-color: #242b34;
    background-image: url("data:image/svg+xml;charset=UTF-8,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24' fill='none' stroke='%23e5e7eb' stroke-width='2' stroke-linecap='round' stroke-linejoin='round'%3E%3Cpolyline points='6 9 12 15 18 9'%3E%3C/polyline%3E%3C/svg%3E");
    background-repeat: no-repeat !important;
    background-position: right 10px center !important;
    background-size: 16px !important;
}

/* --- Menu a tendina personalizzato per i campi con suggerimenti ---
   Sostituisce il datalist nativo (inaffidabile su Safari) con un
   elenco costruito a mano, con lo stesso aspetto di un menu a tendina
   reale ma che permette comunque di scrivere un valore nuovo. */
.suggerimenti-popup {
    display: none;
    position: absolute;
    z-index: 9999;
    background: #fff;
    border: 1px solid var(--bordo);
    border-radius: 8px;
    max-height: 220px;
    overflow-y: auto;
    box-shadow: 0 4px 12px rgba(0,0,0,0.15);
}
.suggerimenti-popup-voce {
    padding: 10px 12px;
    font-size: 0.95rem;
    cursor: pointer;
    border-bottom: 1px solid var(--bordo);
}
.suggerimenti-popup-voce:last-child { border-bottom: none; }
.suggerimenti-popup-voce:hover,
.suggerimenti-popup-voce:active {
    background: var(--grigio);
}

[data-theme="scuro"] .suggerimenti-popup {
    background: #242b34;
    border-color: var(--bordo);
}
[data-theme="scuro"] .suggerimenti-popup-voce { color: var(--testo); border-bottom-color: var(--bordo); }
[data-theme="scuro"] .suggerimenti-popup-voce:hover,
[data-theme="scuro"] .suggerimenti-popup-voce:active {
    background: #1b2129;
}
