/* =========================================================================
   Trocellen CRM - sistema di design
   Mobile first: il layout parte dal telefono e si espande.
   I colori sono token: il tema chiaro e scuro cambia solo le variabili.

   Criteri della tavolozza:
   - le superfici salgono per gradini piccoli (sfondo, scheda, rilievo), cosi la
     gerarchia si legge senza bordi marcati;
   - gli accenti sono desaturati: su uno schermo scuro un blu pieno vibra e stanca,
     e fa sembrare urgente ogni elemento colorato;
   - il colore porta significato solo dove serve (stato, soglia, errore). Tutto il
     resto vive di neutri e di spazio.
   ========================================================================= */

:root {
    color-scheme: light dark;

    --brand-700: #0a3a63;
    --brand-600: #10598f;
    --brand-500: #1a72b0;
    --brand-400: #3f97d4;
    --brand-100: #e9f2fa;

    --ink-900: #141a22;
    --ink-800: #1e2633;
    --ink-700: #2d3748;
    --ink-600: #4a5668;
    --ink-500: #697789;
    --ink-400: #94a1b2;
    --ink-300: #c6cedb;
    --ink-200: #e1e6ee;
    --ink-100: #eff2f7;
    --ink-50: #f7f9fc;

    --success-600: #1b7a4b;
    --success-100: #e6f4ec;
    --warning-600: #8f6212;
    --warning-100: #fbf1df;
    --danger-600: #a93228;
    --danger-100: #fbeceb;
    --info-600: #15618f;
    --info-100: #e8f2f9;

    --surface: #ffffff;
    --surface-sunken: #f5f7fa;
    --surface-raised: #ffffff;
    --surface-hover: var(--ink-50);
    --border: #e3e8ef;
    --border-strong: #c9d2df;
    --text: var(--ink-800);
    --text-muted: var(--ink-500);
    --text-inverse: #ffffff;

    /* La barra laterale ha token propri: nei due temi non e la stessa superficie,
       ed esprimerlo con variabili evita di riscrivere le regole due volte. */
    --sidebar-bg: #0c3557;
    --sidebar-edge: rgb(255 255 255 / 10%);
    --sidebar-text: #cadcec;
    --sidebar-muted: #86aac9;
    --sidebar-strong: #ffffff;
    --sidebar-hover: rgb(255 255 255 / 9%);
    --sidebar-active: rgb(255 255 255 / 16%);
    --sidebar-accent: #5fb0e8;

    --radius-sm: 8px;
    --radius: 12px;
    --radius-lg: 18px;

    --shadow-sm: 0 1px 2px rgb(16 24 40 / 5%);
    --shadow: 0 2px 4px -1px rgb(16 24 40 / 6%), 0 8px 16px -6px rgb(16 24 40 / 10%);
    --shadow-lg: 0 8px 24px -6px rgb(16 24 40 / 12%), 0 24px 48px -12px rgb(16 24 40 / 14%);

    --ring: 0 0 0 3px rgb(63 151 212 / 28%);

    --space-1: 0.25rem;
    --space-2: 0.5rem;
    --space-3: 0.75rem;
    --space-4: 1rem;
    --space-5: 1.5rem;
    --space-6: 2rem;
    --space-8: 3rem;

    --sidebar-width: 264px;
    --topbar-height: 62px;

    /* Oltre questa larghezza il contenuto smette di allargarsi: una riga di
       tabella larga due metri non si legge. */
    --content-max: 1500px;

    --font: "Inter", "Segoe UI Variable", "Segoe UI", system-ui, -apple-system,
            "Helvetica Neue", sans-serif;
    --font-mono: ui-monospace, "Cascadia Mono", "Consolas", monospace;
}

/* Tema scuro: superfici neutre leggermente fredde, accenti piu chiari e meno
   saturi. Il nero pieno viene evitato: alza il contrasto oltre il necessario e
   rende visibile ogni imperfezione di bordo. */
@media (prefers-color-scheme: dark) {
    :root:not([data-theme="light"]) {
        --brand-600: #2b7cbc;
        --brand-500: #3a8cce;
        --brand-400: #68ace6;
        --brand-100: #16293a;

        --surface: #161d26;
        --surface-sunken: #10151c;
        --surface-raised: #1c242f;
        --surface-hover: #1d2631;
        --border: #262f3d;
        --border-strong: #38455a;
        --text: #dce4f0;
        --text-muted: #8b99ad;

        --success-600: #4caf7d;
        --success-100: #14281d;
        --warning-600: #d2a046;
        --warning-100: #2a2316;
        --danger-600: #e07a70;
        --danger-100: #2c1a18;
        --info-600: #63a9e0;
        --info-100: #14242f;

        --sidebar-bg: #121922;
        --sidebar-edge: rgb(255 255 255 / 7%);
        --sidebar-text: #c2ccda;
        --sidebar-muted: #7a899c;
        --sidebar-strong: #f0f4fa;
        --sidebar-hover: rgb(255 255 255 / 6%);
        --sidebar-active: rgb(104 172 230 / 16%);
        --sidebar-accent: #68ace6;

        --shadow-sm: 0 1px 2px rgb(0 0 0 / 24%);
        --shadow: 0 2px 4px -1px rgb(0 0 0 / 28%), 0 8px 16px -6px rgb(0 0 0 / 36%);
        --shadow-lg: 0 8px 24px -6px rgb(0 0 0 / 40%), 0 24px 48px -12px rgb(0 0 0 / 44%);
        --ring: 0 0 0 3px rgb(104 172 230 / 26%);
    }
}

:root[data-theme="dark"] {
    --brand-600: #2b7cbc;
    --brand-500: #3a8cce;
    --brand-400: #68ace6;
    --brand-100: #16293a;

    --surface: #161d26;
    --surface-sunken: #10151c;
    --surface-raised: #1c242f;
    --surface-hover: #1d2631;
    --border: #262f3d;
    --border-strong: #38455a;
    --text: #dce4f0;
    --text-muted: #8b99ad;

    --success-600: #4caf7d;
    --success-100: #14281d;
    --warning-600: #d2a046;
    --warning-100: #2a2316;
    --danger-600: #e07a70;
    --danger-100: #2c1a18;
    --info-600: #63a9e0;
    --info-100: #14242f;

    --sidebar-bg: #121922;
    --sidebar-edge: rgb(255 255 255 / 7%);
    --sidebar-text: #c2ccda;
    --sidebar-muted: #7a899c;
    --sidebar-strong: #f0f4fa;
    --sidebar-hover: rgb(255 255 255 / 6%);
    --sidebar-active: rgb(104 172 230 / 16%);
    --sidebar-accent: #68ace6;

    --shadow-sm: 0 1px 2px rgb(0 0 0 / 24%);
    --shadow: 0 2px 4px -1px rgb(0 0 0 / 28%), 0 8px 16px -6px rgb(0 0 0 / 36%);
    --shadow-lg: 0 8px 24px -6px rgb(0 0 0 / 40%), 0 24px 48px -12px rgb(0 0 0 / 44%);
    --ring: 0 0 0 3px rgb(104 172 230 / 26%);
}

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

html { -webkit-text-size-adjust: 100%; }

body {
    margin: 0;
    font-family: var(--font);
    font-size: 15px;
    line-height: 1.55;
    color: var(--text);
    background: var(--surface-sunken);
    -webkit-font-smoothing: antialiased;
    -moz-osx-font-smoothing: grayscale;
}

h1, h2, h3, h4 {
    margin: 0 0 var(--space-2);
    line-height: 1.25;
    font-weight: 600;
    letter-spacing: -0.011em;
}

h1 { font-size: 1.55rem; letter-spacing: -0.018em; }
h2 { font-size: 1.15rem; }
h3 { font-size: 1rem; }
h4 { font-size: 0.9rem; }
p { margin: 0 0 var(--space-3); }

a { color: var(--brand-600); text-decoration: none; }
a:hover { text-decoration: underline; }

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

/* Il titolo di pagina riceve il fuoco a ogni navigazione, perche un lettore di
   schermo annunci dove si e arrivati. Non e un elemento interattivo, quindi il
   contorno visibile sarebbe solo un riquadro inspiegabile attorno al titolo. */
[tabindex="-1"]:focus,
[tabindex="-1"]:focus-visible { outline: none; }

/* ============================ Struttura ============================ */

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

.app-sidebar {
    position: fixed;
    inset: 0 auto 0 0;
    width: var(--sidebar-width);
    background: var(--sidebar-bg);
    color: var(--sidebar-text);
    display: flex;
    flex-direction: column;
    z-index: 40;
    transform: translateX(-100%);
    transition: transform 200ms cubic-bezier(0.32, 0.72, 0, 1);
    overflow-y: auto;
    overscroll-behavior: contain;
    border-right: 1px solid var(--sidebar-edge);
}

/* Il cassetto è comandato da una casella di spunta nascosta e non da JavaScript:
   deve aprirsi anche mentre il circuito interattivo si sta ancora collegando e
   sulle pagine rese staticamente, come il portale del cliente. */
.nav-drawer-switch { position: absolute; opacity: 0; pointer-events: none; }

.nav-drawer-switch:checked ~ .app-sidebar {
    transform: translateX(0);
    box-shadow: var(--shadow-lg);
}

.nav-drawer-switch:not(:checked) ~ .sidebar-backdrop { display: none; }

.app-main { flex: 1; min-width: 0; display: flex; flex-direction: column; }

.app-topbar {
    position: sticky;
    top: 0;
    z-index: 30;
    height: var(--topbar-height);
    display: flex;
    align-items: center;
    gap: var(--space-3);
    padding: 0 var(--space-4);
    background: color-mix(in srgb, var(--surface) 88%, transparent);
    backdrop-filter: saturate(140%) blur(10px);
    border-bottom: 1px solid var(--border);
}

/* Il contenuto resta centrato e smette di allargarsi: su uno schermo molto largo
   una riga che attraversa tutto lo schermo e faticosa da seguire. */
.app-content {
    padding: var(--space-4);
    flex: 1;
    min-width: 0;
    width: 100%;
    max-width: var(--content-max);
    margin-inline: auto;
}

.sidebar-backdrop {
    position: fixed;
    inset: 0;
    background: rgb(8 14 22 / 55%);
    backdrop-filter: blur(2px);
    z-index: 35;
    border: 0;
    padding: 0;
}

@media (min-width: 1024px) {
    .app-sidebar, .nav-drawer-switch:checked ~ .app-sidebar { transform: translateX(0); }
    .app-main { margin-left: var(--sidebar-width); }
    .app-content { padding: var(--space-6); }
    .app-topbar { padding-inline: var(--space-6); }
    .sidebar-backdrop, .nav-toggle,
    .nav-drawer-switch:checked ~ .sidebar-backdrop { display: none; }
}

/* ============================ Navigazione ============================ */

.brand {
    display: flex;
    align-items: center;
    gap: var(--space-3);
    padding: var(--space-4) var(--space-4) var(--space-4) var(--space-5);
    min-height: var(--topbar-height);
    border-bottom: 1px solid var(--sidebar-edge);
}

.brand-mark {
    width: 32px; height: 32px;
    border-radius: 9px;
    background: linear-gradient(145deg, var(--sidebar-accent), #9bd2f5);
    display: grid; place-items: center;
    font-weight: 700; font-size: 0.95rem;
    color: #0a2740;
    flex: none;
    box-shadow: 0 2px 8px -2px rgb(95 176 232 / 45%);
}

.brand-text { display: flex; flex-direction: column; line-height: 1.2; min-width: 0; }
.brand-name { color: var(--sidebar-strong); font-weight: 600; letter-spacing: -0.01em; }
.brand-sub { font-size: 0.68rem; color: var(--sidebar-muted); text-transform: uppercase; letter-spacing: 0.08em; }

.nav-section { padding: var(--space-4) 0 0; }
.nav-section + .nav-section { padding-top: var(--space-5); }

.nav-section-title {
    font-size: 0.65rem;
    text-transform: uppercase;
    letter-spacing: 0.1em;
    font-weight: 600;
    color: var(--sidebar-muted);
    padding: 0 var(--space-5) var(--space-2);
}

.nav-item {
    position: relative;
    display: flex;
    align-items: center;
    gap: var(--space-3);
    padding: 0.5rem var(--space-3);
    margin: 1px var(--space-3);
    border-radius: var(--radius-sm);
    color: var(--sidebar-text);
    font-size: 0.875rem;
    transition: background 120ms ease, color 120ms ease;
}

.nav-item:hover { background: var(--sidebar-hover); text-decoration: none; color: var(--sidebar-strong); }

.nav-item.active {
    background: var(--sidebar-active);
    color: var(--sidebar-strong);
    font-weight: 600;
}

/* Un filo di accento a sinistra dice "sei qui" senza riempire la voce di colore. */
.nav-item.active::before {
    content: "";
    position: absolute;
    left: calc(var(--space-3) * -1 + 3px);
    top: 20%; bottom: 20%;
    width: 3px;
    border-radius: 0 3px 3px 0;
    background: var(--sidebar-accent);
}

.nav-item svg { flex: none; width: 18px; height: 18px; opacity: 0.85; }
.nav-item.active svg { opacity: 1; }

.nav-badge {
    margin-left: auto;
    background: var(--sidebar-accent);
    color: #0a2740;
    border-radius: 999px;
    padding: 0 0.4rem;
    font-size: 0.7rem;
    font-weight: 700;
    min-width: 20px;
    text-align: center;
}

.nav-badge.is-alert { background: #f0a39b; color: #4a0f0a; }

.nav-footer {
    margin-top: auto;
    padding: var(--space-4) var(--space-5);
    border-top: 1px solid var(--sidebar-edge);
    color: var(--sidebar-muted);
}

/* ============================ Barra superiore ============================ */

.nav-toggle, .icon-button {
    cursor: pointer;
    display: grid; place-items: center;
    width: 36px; height: 36px;
    border: 1px solid var(--border);
    border-radius: var(--radius-sm);
    background: var(--surface);
    color: var(--text-muted);
    transition: background 120ms ease, color 120ms ease, border-color 120ms ease;
}

.nav-toggle:hover, .icon-button:hover {
    background: var(--surface-hover);
    color: var(--text);
    border-color: var(--border-strong);
}

.topbar-title { font-weight: 600; font-size: 0.98rem; letter-spacing: -0.01em; }
.topbar-spacer { flex: 1; }

.user-chip {
    display: flex; align-items: center; gap: var(--space-2);
    padding: 0.25rem var(--space-3) 0.25rem 0.25rem;
    border: 1px solid var(--border);
    border-radius: 999px;
    background: var(--surface);
    color: var(--text);
    transition: border-color 120ms ease, background 120ms ease;
}

.user-chip:hover { text-decoration: none; background: var(--surface-hover); border-color: var(--border-strong); }

.avatar {
    width: 30px; height: 30px;
    border-radius: 50%;
    background: linear-gradient(145deg, var(--brand-500), var(--brand-600));
    color: #fff;
    display: grid; place-items: center;
    font-size: 0.72rem; font-weight: 700;
    letter-spacing: 0.02em;
    flex: none;
}

.user-chip-text { display: none; line-height: 1.2; }
.user-chip-name { font-size: 0.8rem; font-weight: 600; }
.user-chip-role { font-size: 0.7rem; color: var(--text-muted); }

@media (min-width: 640px) { .user-chip-text { display: block; } }

.env-pill {
    font-size: 0.65rem;
    text-transform: uppercase;
    letter-spacing: 0.08em;
    padding: 0.2rem 0.55rem;
    border-radius: 999px;
    background: var(--warning-100);
    color: var(--warning-600);
    border: 1px solid color-mix(in srgb, var(--warning-600) 28%, transparent);
    font-weight: 600;
}

/* ============================ Superfici ============================ */

.card {
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    box-shadow: var(--shadow-sm);
}

.card-header {
    display: flex; align-items: center; gap: var(--space-3);
    padding: var(--space-4) var(--space-5);
    border-bottom: 1px solid var(--border);
}

.card-header h2, .card-header h3 { margin: 0; }
.card-header-actions { margin-left: auto; display: flex; gap: var(--space-2); flex-wrap: wrap; }
.card-body { padding: var(--space-5); }
.card-body.is-flush { padding: 0; }

.card-footer {
    padding: var(--space-3) var(--space-5);
    border-top: 1px solid var(--border);
    background: var(--surface-sunken);
    border-radius: 0 0 var(--radius) var(--radius);
}

.page-header {
    display: flex; flex-wrap: wrap; align-items: flex-start;
    gap: var(--space-3) var(--space-4);
    margin-bottom: var(--space-5);
}

.page-header-text { min-width: 0; flex: 1; }
.page-title { display: flex; align-items: center; gap: var(--space-3); flex-wrap: wrap; }
.page-subtitle { color: var(--text-muted); font-size: 0.9rem; margin: 0; }
.page-actions { display: flex; gap: var(--space-2); flex-wrap: wrap; }

/* ============================ Griglie ============================ */

.grid { display: grid; gap: var(--space-4); }
.grid-kpi { grid-template-columns: repeat(auto-fit, minmax(210px, 1fr)); }
.grid-2 { grid-template-columns: 1fr; }
.grid-3 { grid-template-columns: 1fr; }
.grid-sidebar { grid-template-columns: 1fr; }

@media (min-width: 768px) { .grid-2 { grid-template-columns: repeat(2, 1fr); } }

@media (min-width: 1200px) {
    .grid-3 { grid-template-columns: repeat(3, 1fr); }
    .grid-sidebar { grid-template-columns: minmax(0, 2.2fr) minmax(300px, 1fr); }
}

/* ============================ Indicatori ============================ */

/* L'indicatore non ha piu la barra colorata a sinistra: con quattro schede
   affiancate quattro barre sature gridano tutte allo stesso modo e nessuna
   significa qualcosa. Il colore resta, ma solo sul pallino e sul numero, e solo
   quando lo stato lo richiede. */
.kpi {
    --kpi-accent: var(--brand-400);
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    padding: var(--space-4) var(--space-5);
    display: flex; flex-direction: column; gap: var(--space-1);
    box-shadow: var(--shadow-sm);
    transition: border-color 120ms ease, box-shadow 120ms ease;
}

.kpi:hover { border-color: var(--border-strong); box-shadow: var(--shadow); }

.kpi.is-warning { --kpi-accent: var(--warning-600); }
.kpi.is-danger { --kpi-accent: var(--danger-600); }
.kpi.is-success { --kpi-accent: var(--success-600); }

.kpi-label {
    display: flex; align-items: center; gap: 0.45rem;
    font-size: 0.7rem;
    font-weight: 600;
    color: var(--text-muted);
    text-transform: uppercase;
    letter-spacing: 0.08em;
}

.kpi-label::before {
    content: "";
    width: 6px; height: 6px;
    border-radius: 50%;
    background: var(--kpi-accent);
    flex: none;
}

.kpi-value {
    font-size: 2rem;
    font-weight: 650;
    line-height: 1.15;
    letter-spacing: -0.02em;
    font-variant-numeric: tabular-nums;
}

.kpi.is-warning .kpi-value,
.kpi.is-danger .kpi-value,
.kpi.is-success .kpi-value { color: var(--kpi-accent); }

.kpi-hint { font-size: 0.78rem; color: var(--text-muted); }
.kpi-link { font-size: 0.78rem; font-weight: 600; }

/* ============================ Tabelle ============================ */

.table-wrap { overflow-x: auto; }

table.data {
    width: 100%;
    border-collapse: collapse;
    font-size: 0.875rem;
}

table.data th {
    text-align: left;
    font-size: 0.68rem;
    font-weight: 600;
    text-transform: uppercase;
    letter-spacing: 0.08em;
    color: var(--text-muted);
    padding: var(--space-3) var(--space-4);
    border-bottom: 1px solid var(--border-strong);
    white-space: nowrap;
    background: var(--surface);
    position: sticky; top: 0;
    z-index: 1;
}

table.data td {
    padding: var(--space-4);
    border-bottom: 1px solid var(--border);
    vertical-align: top;
}

table.data tbody tr:last-child td { border-bottom: 0; }
table.data tbody tr { transition: background 100ms ease; }
table.data tbody tr:hover { background: var(--surface-hover); }
table.data td.numeric, table.data th.numeric { text-align: right; font-variant-numeric: tabular-nums; }
table.data td.actions { text-align: right; white-space: nowrap; }
/* La prima colonna porta il nome dell'entita e non deve essere compressa dalle
   intestazioni lunghe delle colonne successive: senza una larghezza minima un
   nome di due parole finisce su tre righe. */
table.data th:first-child, table.data td:first-child { min-width: 9.5rem; }
table.data th.narrow:first-child, table.data td.narrow:first-child { min-width: 0; }

table.data .primary-cell { font-weight: 600; }
table.data .secondary-cell { font-size: 0.78rem; color: var(--text-muted); margin-top: 0.1rem; }

/* Su schermo stretto le tabelle diventano schede. */
@media (max-width: 767px) {
    table.data.responsive thead { display: none; }
    table.data.responsive td:first-child { min-width: 0; }

    table.data.responsive tbody tr {
        display: block;
        border: 1px solid var(--border);
        border-radius: var(--radius);
        margin-bottom: var(--space-3);
        padding: var(--space-3);
        background: var(--surface);
    }

    table.data.responsive tbody tr:hover { background: var(--surface); }

    table.data.responsive td {
        display: flex; justify-content: space-between; gap: var(--space-3);
        border: 0; padding: 0.3rem 0; text-align: right;
    }

    table.data.responsive td::before {
        content: attr(data-label);
        font-size: 0.68rem;
        font-weight: 600;
        text-transform: uppercase;
        letter-spacing: 0.08em;
        color: var(--text-muted);
        text-align: left;
        flex: none;
        padding-top: 0.15rem;
    }

    table.data.responsive td.actions { justify-content: flex-end; padding-top: var(--space-3); }
    table.data.responsive td.hide-mobile { display: none; }
}

/* ============================ Etichette di stato ============================ */

.badge {
    display: inline-flex; align-items: center; gap: 0.35rem;
    padding: 0.15rem 0.55rem;
    border-radius: 999px;
    font-size: 0.72rem;
    font-weight: 600;
    line-height: 1.5;
    white-space: nowrap;
    background: var(--surface-hover);
    color: var(--text-muted);
    border: 1px solid transparent;
}

/* Il tono si costruisce dal colore di stato: una traccia di tinta per il fondo,
   un bordo appena percettibile, il testo pieno. Il fondo pieno e saturo degli
   stati rendeva ogni riga di tabella una luminaria. */
.badge.neutral { background: var(--surface-hover); color: var(--text-muted); border-color: var(--border); }
.badge.info { background: color-mix(in srgb, var(--info-600) 12%, transparent); color: var(--info-600); border-color: color-mix(in srgb, var(--info-600) 26%, transparent); }
.badge.success { background: color-mix(in srgb, var(--success-600) 12%, transparent); color: var(--success-600); border-color: color-mix(in srgb, var(--success-600) 26%, transparent); }
.badge.warning { background: color-mix(in srgb, var(--warning-600) 14%, transparent); color: var(--warning-600); border-color: color-mix(in srgb, var(--warning-600) 28%, transparent); }
.badge.danger { background: color-mix(in srgb, var(--danger-600) 12%, transparent); color: var(--danger-600); border-color: color-mix(in srgb, var(--danger-600) 26%, transparent); }
.badge.brand { background: color-mix(in srgb, var(--brand-500) 12%, transparent); color: var(--brand-500); border-color: color-mix(in srgb, var(--brand-500) 26%, transparent); }

.badge-dot { width: 6px; height: 6px; border-radius: 50%; background: currentcolor; flex: none; }

/* ============================ Comandi ============================ */

.btn {
    display: inline-flex; align-items: center; justify-content: center; gap: var(--space-2);
    padding: 0.5rem 0.95rem;
    border-radius: var(--radius-sm);
    border: 1px solid var(--border-strong);
    background: var(--surface);
    color: var(--text);
    font-size: 0.875rem;
    font-weight: 550;
    font-family: inherit;
    cursor: pointer;
    white-space: nowrap;
    transition: background 120ms ease, border-color 120ms ease, box-shadow 120ms ease;
}

.btn:hover:not(:disabled) { background: var(--surface-hover); border-color: var(--border-strong); text-decoration: none; }
.btn:active:not(:disabled) { transform: translateY(0.5px); }
.btn:disabled { opacity: 0.5; cursor: not-allowed; }

.btn.primary {
    background: var(--brand-600);
    border-color: var(--brand-600);
    color: #fff;
    box-shadow: var(--shadow-sm);
}

.btn.primary:hover:not(:disabled) {
    background: var(--brand-500);
    border-color: var(--brand-500);
    box-shadow: var(--shadow);
}

.btn.success { background: var(--success-600); border-color: var(--success-600); color: #fff; }
.btn.success:hover:not(:disabled) { filter: brightness(1.08); }

.btn.danger { background: var(--danger-600); border-color: var(--danger-600); color: #fff; }
.btn.danger:hover:not(:disabled) { filter: brightness(1.08); }

.btn.ghost { border-color: transparent; background: transparent; color: var(--text-muted); }
.btn.ghost:hover:not(:disabled) { background: var(--surface-hover); color: var(--text); }

.btn.small { padding: 0.3rem 0.65rem; font-size: 0.8rem; }
.btn.block { width: 100%; }

.btn-group { display: flex; gap: var(--space-2); flex-wrap: wrap; }

/* ============================ Moduli ============================ */

.field { display: flex; flex-direction: column; gap: 0.35rem; margin-bottom: var(--space-4); min-width: 0; }
.field-label { font-size: 0.78rem; font-weight: 600; color: var(--text); }
.field-label .required { color: var(--danger-600); }
.field-hint { font-size: 0.74rem; color: var(--text-muted); line-height: 1.4; }
.field-error { font-size: 0.74rem; color: var(--danger-600); }

.input, .select, .textarea {
    width: 100%;
    padding: 0.5rem 0.7rem;
    border: 1px solid var(--border-strong);
    border-radius: var(--radius-sm);
    background: var(--surface);
    color: var(--text);
    font-size: 0.875rem;
    font-family: inherit;
    transition: border-color 120ms ease, box-shadow 120ms ease;
}

.input::placeholder, .textarea::placeholder { color: var(--text-muted); opacity: 0.75; }
.input:hover:not(:disabled), .select:hover:not(:disabled), .textarea:hover:not(:disabled) { border-color: var(--text-muted); }

.input:focus, .select:focus, .textarea:focus {
    border-color: var(--brand-400);
    outline: none;
    box-shadow: var(--ring);
}

.input:disabled, .select:disabled, .textarea:disabled {
    background: var(--surface-sunken);
    color: var(--text-muted);
    cursor: not-allowed;
}

.input.numeric { text-align: right; font-variant-numeric: tabular-nums; }
.textarea { min-height: 86px; resize: vertical; line-height: 1.5; }

.select {
    appearance: none;
    padding-right: 2rem;
    background-image: linear-gradient(45deg, transparent 50%, currentcolor 50%),
                      linear-gradient(135deg, currentcolor 50%, transparent 50%);
    background-position: calc(100% - 15px) calc(50% + 1px), calc(100% - 10px) calc(50% + 1px);
    background-size: 5px 5px, 5px 5px;
    background-repeat: no-repeat;
}

.form-grid { display: grid; gap: var(--space-4); grid-template-columns: 1fr; }
@media (min-width: 700px) { .form-grid.two { grid-template-columns: repeat(2, 1fr); } }
@media (min-width: 1000px) { .form-grid.three { grid-template-columns: repeat(3, 1fr); } }

.checkbox { display: flex; align-items: flex-start; gap: var(--space-2); font-size: 0.875rem; cursor: pointer; }
.checkbox input { margin-top: 0.22rem; accent-color: var(--brand-500); flex: none; }

/* La barra dei filtri e una griglia, non una fila che va a capo come capita.
   Con il flex-wrap i campi si allineavano in modo diverso a ogni larghezza e le
   etichette finivano a quote differenti: era il difetto piu visibile della
   pagina. Qui le colonne hanno tutte la stessa larghezza minima e i campi si
   appoggiano in alto, cosi un suggerimento lungo sotto un campo non sposta i
   vicini. */
.filter-bar {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(190px, 1fr));
    gap: var(--space-4);
    align-items: start;
    padding: var(--space-4) var(--space-5);
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    margin-bottom: var(--space-5);
    box-shadow: var(--shadow-sm);
}

.filter-bar .field { margin-bottom: 0; }
.filter-bar .field.grow { grid-column: span 2; }

@media (max-width: 520px) {
    .filter-bar { grid-template-columns: 1fr; padding: var(--space-4); }
    .filter-bar .field.grow { grid-column: span 1; }
}

/* I filtri rapidi e i comandi della barra occupano una riga propria in fondo. */
.filter-quick {
    display: flex; flex-wrap: wrap; align-items: center;
    gap: var(--space-2) var(--space-4);
    grid-column: 1 / -1;
    padding-top: var(--space-1);
    border-top: 1px solid var(--border);
    margin-top: calc(var(--space-2) * -1);
}

.filter-quick > .filter-quick-title {
    font-size: 0.78rem; font-weight: 600; color: var(--text);
    margin-right: var(--space-1);
}

.filter-quick .checkbox { font-size: 0.82rem; }
.filter-quick .filter-reset { margin-left: auto; }

/* ============================ Scelta delle colonne ============================ */

.column-chooser { position: relative; }

.column-chooser > summary {
    list-style: none;
    user-select: none;
}

.column-chooser > summary::-webkit-details-marker { display: none; }
.column-chooser[open] > summary { background: var(--surface-hover); border-color: var(--border-strong); }

/* Il pallino dice che questa griglia e stata personalizzata, senza doverla aprire. */
.column-chooser-mark {
    width: 6px; height: 6px;
    border-radius: 50%;
    background: var(--brand-400);
    flex: none;
}

.column-chooser-panel {
    position: absolute;
    right: 0;
    top: calc(100% + var(--space-2));
    z-index: 50;
    width: min(92vw, 320px);
    padding: var(--space-4);
    background: var(--surface-raised);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    box-shadow: var(--shadow-lg);
}

.column-chooser-head {
    display: flex; align-items: center; justify-content: space-between; gap: var(--space-2);
    font-size: 0.78rem;
    color: var(--text-muted);
    padding-bottom: var(--space-3);
    margin-bottom: var(--space-3);
    border-bottom: 1px solid var(--border);
}

.column-chooser-list {
    display: grid;
    gap: var(--space-2);
    max-height: 46vh;
    overflow-y: auto;
}

.column-chooser-list .checkbox { font-size: 0.85rem; }
.column-chooser-list .checkbox.is-locked { color: var(--text-muted); cursor: default; }

.column-chooser-note {
    margin: var(--space-3) 0 0;
    padding-top: var(--space-3);
    border-top: 1px solid var(--border);
    font-size: 0.74rem;
    line-height: 1.45;
    color: var(--text-muted);
}

/* ============================ Avvisi ============================ */

.alert {
    --alert-tone: var(--info-600);
    display: flex; gap: var(--space-3);
    padding: var(--space-4);
    border-radius: var(--radius);
    border: 1px solid color-mix(in srgb, var(--alert-tone) 26%, transparent);
    background: color-mix(in srgb, var(--alert-tone) 9%, var(--surface));
    color: var(--text);
    font-size: 0.875rem;
    line-height: 1.5;
    margin-bottom: var(--space-4);
}

/* Il testo resta del colore di lettura e solo il titolo prende il tono: un
   paragrafo intero scritto nel colore dello stato si legge peggio e trasforma
   ogni avviso in un allarme. */
.alert-icon { flex: none; margin-top: 1px; color: var(--alert-tone); }
.alert-body { min-width: 0; }
.alert-title { font-weight: 600; margin-bottom: 0.15rem; color: var(--alert-tone); }

.alert.info { --alert-tone: var(--info-600); }
.alert.success { --alert-tone: var(--success-600); }
.alert.warning { --alert-tone: var(--warning-600); }
.alert.danger { --alert-tone: var(--danger-600); }

/* ============================ Timeline ============================ */

.timeline { list-style: none; margin: 0; padding: 0; position: relative; }

.timeline::before {
    content: "";
    position: absolute;
    left: 15px; top: 8px; bottom: 8px;
    width: 2px;
    background: var(--border);
}

.timeline-item { position: relative; padding: 0 0 var(--space-4) 44px; }

.timeline-marker {
    position: absolute; left: 4px; top: 2px;
    width: 24px; height: 24px;
    border-radius: 50%;
    background: var(--surface);
    border: 2px solid var(--border-strong);
    display: grid; place-items: center;
    font-size: 0.65rem;
    color: var(--text-muted);
}

.timeline-marker.brand { border-color: var(--brand-400); color: var(--brand-600); }
.timeline-marker.success { border-color: var(--success-600); color: var(--success-600); }
.timeline-marker.danger { border-color: var(--danger-600); color: var(--danger-600); }
.timeline-marker.warning { border-color: var(--warning-600); color: var(--warning-600); }

.timeline-head { display: flex; flex-wrap: wrap; align-items: baseline; gap: var(--space-2); }
.timeline-title { font-weight: 600; font-size: 0.9rem; }
.timeline-meta { font-size: 0.75rem; color: var(--text-muted); }
.timeline-body { font-size: 0.85rem; color: var(--text-muted); margin-top: 0.15rem; }

/* ============================ Avanzamento ============================ */

.progress { height: 8px; border-radius: 999px; background: var(--ink-200); overflow: hidden; }
.progress-bar { height: 100%; background: var(--brand-500); border-radius: 999px; transition: width 240ms ease; }
.progress-bar.is-complete { background: var(--success-600); }
.progress-bar.is-low { background: var(--warning-600); }

.steps { display: flex; flex-direction: column; gap: var(--space-2); }

.step {
    display: flex; gap: var(--space-3); align-items: flex-start;
    padding: var(--space-2) var(--space-3);
    border: 1px solid var(--border);
    border-radius: var(--radius-sm);
    background: var(--surface);
}

.step.is-complete { border-color: var(--success-600); background: var(--success-100); }
.step.is-current { border-color: var(--brand-400); background: var(--brand-100); }

.step-index {
    flex: none; width: 24px; height: 24px; border-radius: 50%;
    display: grid; place-items: center;
    background: var(--ink-200); color: var(--ink-600);
    font-size: 0.75rem; font-weight: 700;
}

.step.is-complete .step-index { background: var(--success-600); color: #fff; }
.step.is-current .step-index { background: var(--brand-600); color: #fff; }

/* ============================ Elementi vari ============================ */

.stack { display: flex; flex-direction: column; gap: var(--space-4); }
.stack.tight { gap: var(--space-2); }
.row { display: flex; gap: var(--space-3); flex-wrap: wrap; align-items: center; }
.row.tight { gap: var(--space-2); }
.row.between { justify-content: space-between; }
.spacer { flex: 1; }

.muted { color: var(--text-muted); }
.small { font-size: 0.8rem; }
.numeric { font-variant-numeric: tabular-nums; }
.mono { font-family: var(--font-mono); font-size: 0.82rem; }
.truncate { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.nowrap { white-space: nowrap; }
.text-success { color: var(--success-600); }
.text-danger { color: var(--danger-600); }
.text-warning { color: var(--warning-600); }

.definition-list { display: grid; gap: var(--space-2); grid-template-columns: 1fr; margin: 0; }
@media (min-width: 560px) { .definition-list.two { grid-template-columns: repeat(2, 1fr); } }

.definition { display: flex; flex-direction: column; gap: 0.1rem; min-width: 0; }
.definition-label { font-size: 0.68rem; font-weight: 600; text-transform: uppercase; letter-spacing: 0.08em; color: var(--text-muted); }
.definition-value { font-size: 0.9rem; word-break: break-word; }

.empty-state {
    text-align: center;
    padding: var(--space-8) var(--space-5);
    color: var(--text-muted);
    display: flex; flex-direction: column; align-items: center; gap: var(--space-1);
}

.empty-state-title { font-weight: 600; color: var(--text); font-size: 1rem; }
.empty-state p { margin: 0; max-width: 46ch; font-size: 0.88rem; }

.tabs { display: flex; gap: var(--space-1); border-bottom: 1px solid var(--border); overflow-x: auto; margin-bottom: var(--space-4); }

.tab {
    padding: var(--space-2) var(--space-3);
    border: 0; background: transparent;
    color: var(--text-muted);
    font-size: 0.875rem; font-weight: 500; font-family: inherit;
    cursor: pointer; white-space: nowrap;
    border-bottom: 2px solid transparent;
}

.tab:hover { color: var(--text); }
.tab.active { color: var(--text); border-bottom-color: var(--brand-500); font-weight: 600; }

.pagination {
    display: flex; align-items: center; gap: var(--space-2);
    flex-wrap: wrap; justify-content: flex-end;
}

.pagination .pagination-status {
    font-size: 0.8rem;
    color: var(--text-muted);
    font-variant-numeric: tabular-nums;
    padding-inline: var(--space-2);
}

.loading-row { display: flex; align-items: center; gap: var(--space-3); padding: var(--space-5); color: var(--text-muted); }

.spinner {
    width: 18px; height: 18px;
    border: 2px solid var(--border-strong);
    border-top-color: var(--brand-500);
    border-radius: 50%;
    animation: spin 700ms linear infinite;
    flex: none;
}

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

.skeleton {
    background: linear-gradient(90deg, var(--ink-100) 25%, var(--ink-200) 37%, var(--ink-100) 63%);
    background-size: 400% 100%;
    animation: shimmer 1.3s ease infinite;
    border-radius: var(--radius-sm);
    height: 1rem;
}

@keyframes shimmer { from { background-position: 100% 0; } to { background-position: 0 0; } }

/* ============================ Costruttore offerta ============================ */

.pricing-table { width: 100%; border-collapse: collapse; font-size: 0.82rem; }
.pricing-table th { padding: var(--space-2); font-size: 0.68rem; text-transform: uppercase; letter-spacing: 0.03em; color: var(--text-muted); border-bottom: 1px solid var(--border); white-space: nowrap; }
.pricing-table td { padding: 0.3rem var(--space-2); border-bottom: 1px solid var(--border); }
.pricing-table .input { padding: 0.3rem 0.4rem; font-size: 0.82rem; }
.pricing-table tr.needs-approval { background: var(--warning-100); }
.pricing-table tr.is-alternative { opacity: 0.75; font-style: italic; }
.pricing-table .col-narrow { width: 86px; }
.pricing-table .col-mid { width: 110px; }

.margin-indicator { display: inline-flex; align-items: center; gap: 0.3rem; font-weight: 600; font-variant-numeric: tabular-nums; }
.margin-indicator.is-below { color: var(--danger-600); }
.margin-indicator.is-above { color: var(--success-600); }

/* ============================ Accesso ============================ */

.auth-page {
    min-height: 100vh;
    display: grid;
    place-items: center;
    padding: var(--space-5) var(--space-4);
    background:
        radial-gradient(1100px 620px at 15% -10%, rgb(63 151 212 / 20%), transparent 62%),
        radial-gradient(900px 520px at 105% 108%, rgb(26 114 176 / 16%), transparent 58%),
        var(--surface-sunken);
}

.auth-card {
    width: min(100%, 420px);
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: var(--radius-lg);
    box-shadow: var(--shadow-lg);
    overflow: hidden;
}

/* La scelta del ruolo mostra sedici schede: li serve tutta la larghezza. */
.auth-card.is-wide { width: min(100%, 1060px); }

.auth-header {
    padding: var(--space-6) var(--space-6) var(--space-4);
    text-align: left;
}

.auth-card.is-wide .auth-header {
    padding-bottom: var(--space-5);
    border-bottom: 1px solid var(--border);
}

.auth-brand {
    display: flex; align-items: center; gap: var(--space-3);
    margin-bottom: var(--space-4);
}

.auth-brand .brand-mark { width: 38px; height: 38px; border-radius: 11px; font-size: 1.05rem; }
.auth-header h1 { font-size: 1.3rem; margin-bottom: var(--space-2); }
.auth-header .page-subtitle { font-size: 0.88rem; line-height: 1.55; }

.auth-body { padding: 0 var(--space-6) var(--space-6); }
.auth-card.is-wide .auth-body { padding-top: var(--space-5); }

.auth-note {
    margin: var(--space-5) 0 0;
    padding-top: var(--space-4);
    border-top: 1px solid var(--border);
    font-size: 0.78rem;
    line-height: 1.5;
    color: var(--text-muted);
}

.profile-grid { display: grid; gap: var(--space-3); grid-template-columns: 1fr; }
@media (min-width: 760px) { .profile-grid { grid-template-columns: repeat(2, 1fr); } }
@media (min-width: 1100px) { .profile-grid { grid-template-columns: repeat(3, 1fr); } }

.profile-card {
    display: flex; flex-direction: column; gap: var(--space-2);
    padding: var(--space-4);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    background: var(--surface);
    color: var(--text);
    text-align: left;
    font-family: inherit;
    font-size: inherit;
    cursor: pointer;
    transition: border-color 120ms ease, box-shadow 120ms ease, transform 120ms ease;
}

.profile-card:hover {
    border-color: var(--brand-400);
    box-shadow: var(--shadow);
    transform: translateY(-1px);
}

.profile-card-head { display: flex; align-items: center; gap: var(--space-3); }
.profile-card-name { font-weight: 600; }
.profile-card-upn { font-size: 0.74rem; color: var(--text-muted); word-break: break-all; }
.profile-card-scenario { font-size: 0.8rem; color: var(--text-muted); line-height: 1.45; }
.profile-card-roles { display: flex; gap: 0.3rem; flex-wrap: wrap; margin-top: auto; padding-top: var(--space-1); }

.print-only { display: none; }

@media print {
    .app-sidebar, .app-topbar, .page-actions, .filter-bar, .btn { display: none !important; }
    .app-main { margin-left: 0; }
    .card { box-shadow: none; border-color: #ccc; }
    .print-only { display: block; }
}
