:root {
    --sidebar-width: 250px;
    --topbar-height: 56px;
}

body {
    background-color: #f4f6f9;
    font-family: 'Segoe UI', Roboto, sans-serif;
}

/* ---------------- Sidebar ---------------- */
#sidebar {
    width: var(--sidebar-width);
    height: 100vh;
    background: #1e2530;
    color: #cfd6e0;
    position: fixed;
    top: 0;
    left: 0;
    overflow-y: auto;
    overflow-x: hidden;
    transition: margin-left .2s ease;
    z-index: 1030;
}
#sidebar::-webkit-scrollbar { width: 6px; }
#sidebar::-webkit-scrollbar-thumb { background: #3a4456; border-radius: 3px; }
#sidebar.collapsed { margin-left: calc(var(--sidebar-width) * -1); }
#sidebar .nav-link { color: #cfd6e0; padding: .6rem 1rem; border-radius: 6px; margin: 2px 8px; }
#sidebar .nav-link.active, #sidebar .nav-link:hover { background: #2c3543; color: #fff; }
#sidebar .sidebar-brand { padding: 1rem; font-weight: 600; color: #fff; border-bottom: 1px solid #2c3543; }
#sidebar .nav-section-title { font-size: .72rem; text-transform: uppercase; color: #6b7684; padding: 1rem 1rem .3rem; }

/* ---------------- Content wrapper ---------------- */
#content-wrapper { margin-left: var(--sidebar-width); transition: margin-left .2s ease; min-height: 100vh; }
#content-wrapper.expanded { margin-left: 0; }

/* ---------------- Topbar ---------------- */
#topbar { height: var(--topbar-height); background: #fff; border-bottom: 1px solid #e3e6ea; }

/* ---------------- Cards KPI ---------------- */
.kpi-card { border: none; border-radius: 10px; box-shadow: 0 1px 3px rgba(0,0,0,.08); }
.kpi-card .kpi-value { font-size: 1.9rem; font-weight: 700; }
.kpi-card .kpi-label { font-size: .8rem; color: #6b7684; text-transform: uppercase; }

/* ---------------- Timeline ticket ---------------- */
.ticket-timeline { list-style: none; padding-left: 0; margin: 0; }
.ticket-timeline li { position: relative; padding: 0 0 1.2rem 1.8rem; border-left: 2px solid #e3e6ea; }
.ticket-timeline li:last-child { border-color: transparent; padding-bottom: 0; }
.ticket-timeline li::before {
    content: ''; position: absolute; left: -7px; top: 2px; width: 12px; height: 12px;
    border-radius: 50%; background: #3b82f6; border: 2px solid #fff;
}

/* ---------------- Dark mode ---------------- */
body.dark-mode { background-color: #12161c; color: #dfe3e8; }

body.dark-mode .card,
body.dark-mode #topbar,
body.dark-mode table,
body.dark-mode .dropdown-menu,
body.dark-mode .accordion-item,
body.dark-mode .accordion-body,
body.dark-mode .list-group-item {
    background-color: #1a1f28;
    color: #dfe3e8;
    border-color: #2a3140;
}
body.dark-mode #topbar { border-bottom-color: #2a3140 !important; }

body.dark-mode .table { color: #dfe3e8; }
body.dark-mode .table-light,
body.dark-mode .table-light > th,
body.dark-mode .table thead.table-light th { background-color: #232936; color: #dfe3e8; }
body.dark-mode .table-hover > tbody > tr:hover > * { background-color: #232936; color: #dfe3e8; }
body.dark-mode .table > :not(caption) > * > * { border-bottom-color: #2a3140; }

body.dark-mode .modal-content { background-color: #1a1f28; color: #dfe3e8; }
body.dark-mode .modal-header, body.dark-mode .modal-footer { border-color: #2a3140; }

body.dark-mode .form-control,
body.dark-mode .form-select,
body.dark-mode .input-group-text {
    background-color: #232936; color: #dfe3e8; border-color: #333c4a;
}
body.dark-mode .form-control::placeholder { color: #7a8494; }
body.dark-mode .form-control:focus, body.dark-mode .form-select:focus { background-color: #232936; color: #dfe3e8; }
body.dark-mode .form-label, body.dark-mode label { color: #dfe3e8; }

/* Testi/pulsanti che in modalità chiara usano volutamente colori scuri fissi
   (es. text-dark su sfondo bianco) vanno riportati a un colore leggibile */
body.dark-mode .text-dark,
body.dark-mode .text-muted,
body.dark-mode .breadcrumb-item,
body.dark-mode .breadcrumb-item.active,
body.dark-mode .kpi-label {
    color: #b6bfcc !important;
}
body.dark-mode .breadcrumb-item a { color: #8ab4ff !important; }
body.dark-mode .dropdown-item { color: #dfe3e8; }
body.dark-mode .dropdown-item:hover, body.dark-mode .dropdown-item:focus { background-color: #232936; color: #fff; }
body.dark-mode .dropdown-divider { border-color: #2a3140; }

body.dark-mode .badge.bg-secondary { background-color: #3a4456 !important; }
body.dark-mode .btn-outline-secondary { color: #b6bfcc; border-color: #3a4456; }
body.dark-mode .btn-outline-secondary:hover { background-color: #232936; color: #fff; }
body.dark-mode .btn-link { color: #8ab4ff; }

body.dark-mode .nav-tabs { border-color: #2a3140; }
body.dark-mode .nav-tabs .nav-link { color: #b6bfcc; }
body.dark-mode .nav-tabs .nav-link.active { background-color: #1a1f28; color: #fff; border-color: #2a3140 #2a3140 #1a1f28; }

body.dark-mode .card.table-responsive-card,
body.dark-mode .bg-white { background-color: #1a1f28 !important; color: #dfe3e8; }

body.dark-mode hr { border-color: #2a3140; opacity: 1; }

/* ---------------- Utility ---------------- */
.cursor-pointer { cursor: pointer; }
.table-responsive-card { border-radius: 10px; overflow: hidden; box-shadow: 0 1px 3px rgba(0,0,0,.08); }

@media (max-width: 768px) {
    #sidebar { margin-left: calc(var(--sidebar-width) * -1); }
    #sidebar.show { margin-left: 0; }
    #content-wrapper { margin-left: 0; }

    /* Overlay scuro dietro la sidebar aperta: tap fuori per chiuderla */
    #sidebar-backdrop {
        display: none;
        position: fixed; inset: 0; background: rgba(0,0,0,.4); z-index: 1025;
    }
    #sidebar-backdrop.show { display: block; }

    /* Aree di tocco più grandi per un uso comodo da smartphone (tecnici sul campo) */
    .btn, .form-select, .form-control, .nav-link, .dropdown-item {
        min-height: 44px;
    }
    .btn-sm { min-height: 38px; padding-top: .4rem; padding-bottom: .4rem; }
    .table td, .table th { padding: .65rem .5rem; }

    /* Le card riempiono la larghezza disponibile senza margini eccessivi */
    .container-fluid { padding-left: .75rem; padding-right: .75rem; }
    .kpi-card { padding: .9rem !important; }

    /* Pulsante Chiudi Ticket ben visibile anche su schermi piccoli */
    #btn-chiudi-rapido { font-size: 1.1rem; }
}

/* ---------------- Ruoli e Permessi ---------------- */
.ruolo-card { border: 2px solid transparent; transition: border-color .15s, box-shadow .15s; height: 100%; }
.ruolo-card:hover { border-color: #b6c6e6; }
.ruolo-card.attivo { border-color: #0d6efd; box-shadow: 0 0 0 3px rgba(13,110,253,.12); }
.permesso-riga { padding: .8rem 0; border-bottom: 1px solid #eef0f3; }
.permesso-riga:last-child { border-bottom: 0; }
.permesso-riga .form-check-input { width: 2.6em; height: 1.4em; cursor: pointer; }
.permesso-riga .stato-permesso { min-width: 100px; }
.barra-salvataggio { position: sticky; bottom: .75rem; z-index: 5; box-shadow: 0 4px 16px rgba(0,0,0,.18); }
.matrice td, .matrice th { text-align: center; }
.matrice td:first-child, .matrice th:first-child { text-align: left; }

body.dark-mode .ruolo-card:hover { border-color: #3a4d75; }
body.dark-mode .ruolo-card.attivo { border-color: #4d8dff; box-shadow: 0 0 0 3px rgba(77,141,255,.18); }
body.dark-mode .permesso-riga { border-bottom-color: #2a3140; }
body.dark-mode .card-header.bg-transparent { border-bottom-color: #2a3140; }
body.dark-mode .matrice .gruppo-riga td { background-color: #232936; }
body.dark-mode .alert-info { background-color: #16283f; border-color: #24466f; color: #b9d6ff; }
body.dark-mode .alert-warning { background-color: #3a3016; border-color: #6b5a1f; color: #f3dc8f; }

@media (max-width: 768px) {
    .permesso-riga { flex-wrap: wrap; }
    .permesso-riga .stato-permesso { margin-left: 3.7rem; }
}

/* ---- Dark mode: correzioni di dettaglio ---- */
/* Bootstrap 5.3 dipinge le celle con variabili proprie (--bs-table-*): vanno ridefinite,
   altrimenti le righe delle tabelle restano bianche anche in modalità scura. */
body.dark-mode .table {
    --bs-table-bg: transparent;
    --bs-table-color: #dfe3e8;
    --bs-table-border-color: #2a3140;
    --bs-table-hover-bg: #232936;
    --bs-table-hover-color: #ffffff;
    --bs-table-striped-bg: #1f2531;
    --bs-table-striped-color: #dfe3e8;
}
body.dark-mode .table-warning { --bs-table-bg: #3a3016; --bs-table-color: #f3dc8f; }
/* I badge chiari con testo scuro (es. «Sensibile») devono restare leggibili */
body.dark-mode .badge.text-dark { color: #212529 !important; }

/* ---------------- Calendario: ticket chiusi per giorno ---------------- */
.cal-griglia { display: grid; grid-template-columns: repeat(7, minmax(0, 1fr)); gap: .4rem; }
.cal-intestazione { text-align: center; font-size: .8rem; font-weight: 600; color: #6c757d; padding: .25rem 0; }
.cal-giorno { position: relative; min-height: 96px; display: flex; flex-direction: column; padding: .4rem .5rem;
    background: #fff; border: 1px solid #dee2e6; border-radius: .5rem; transition: box-shadow .15s; }
.cal-giorno:not(.cal-futuro):hover { box-shadow: 0 2px 10px rgba(0,0,0,.15); }
.cal-num { font-size: .8rem; font-weight: 600; color: #495057; }
.cal-chiusi { flex: 1; display: flex; flex-direction: column; align-items: center; justify-content: center; text-decoration: none; color: #198754; }
.cal-n { font-size: 1.7rem; font-weight: 700; line-height: 1; }
.cal-lbl { font-size: .68rem; text-transform: uppercase; letter-spacing: .04em; }
.cal-oggi { outline: 2px solid #0d6efd; outline-offset: -1px; }
.cal-futuro { background: #f8f9fa; opacity: .7; }
.cal-zero { background: #fdecec; border-color: #f1aeb5; }
.cal-zero .cal-num, .cal-zero .cal-chiusi { color: #b02a37; }
.cal-evento { position: relative; z-index: 2; }
.cal-legenda { display: inline-block; width: 14px; height: 14px; border-radius: 3px; vertical-align: -2px; margin-right: 4px; }
.cal-legenda.cal-zero { border: 1px solid #f1aeb5; }

body.dark-mode .cal-intestazione { color: #8a94a6; }
body.dark-mode .cal-giorno { background: #1a1f28; border-color: #2a3140; }
body.dark-mode .cal-num { color: #b6bfcc; }
body.dark-mode .cal-chiusi { color: #4cc38a; }
body.dark-mode .cal-futuro { background: #151a21; }
body.dark-mode .cal-zero { background: #3a1f24; border-color: #7a3038; }
body.dark-mode .cal-zero .cal-num, body.dark-mode .cal-zero .cal-chiusi { color: #ff8b96; }
body.dark-mode .cal-legenda.cal-zero { border-color: #7a3038; }

@media (max-width: 576px) {
    .cal-griglia { gap: .2rem; }
    .cal-giorno { min-height: 62px; padding: .25rem; }
    .cal-n { font-size: 1.15rem; }
    .cal-lbl { display: none; }
    .cal-intestazione { font-size: .68rem; }
}

/* ---------------- Dettaglio ticket: barra "Torna all'elenco / Precedente / Successivo" ---------------- */
@media (max-width: 576px) {
    #ticket-nav > a#btn-torna-elenco { width: 100%; }
    #ticket-nav > div { width: 100%; }
    #ticket-nav > div .btn { flex: 1 1 0; white-space: nowrap; }
}

/* ---------------- Utenti: scelta punti vendita, requisiti password ---------------- */
.pv-scheda { cursor: pointer; background: #fff; transition: border-color .12s, background-color .12s; }
.pv-scheda:hover { border-color: #86b7fe !important; }
.pv-scheda:has(.pv-check:checked) { border-color: #0d6efd !important; background: #eef4ff; }
.pv-barra { background: #fff; }
.requisiti-password li { transition: color .15s; }
.requisiti-password li i { margin-right: .25rem; }
.codice-pw { background: #fff; letter-spacing: .04em; }
body.dark-mode .pv-scheda, body.dark-mode .pv-barra, body.dark-mode .codice-pw { background: #1a1f28; color: #dfe3e8; }
body.dark-mode .pv-scheda:has(.pv-check:checked) { background: #1d2a44; border-color: #4d8dff !important; }
body.dark-mode .border, body.dark-mode .border-bottom { border-color: #2a3140 !important; }

/* ---------------- Pagine di accesso ---------------- */
.login-card { width: 380px; max-width: 100%; }
.login-logo { max-width: 230px; width: 100%; max-height: 110px; height: auto; object-fit: contain; }

/* ---------------- Impianti ---------------- */
.impianti-indice .nav-link { color: inherit; font-size: .875rem; }
.impianti-indice .nav-link:hover { background: rgba(13,110,253,.08); }
.impianti-sezione { scroll-margin-top: 1rem; }
.impianto-valori th { width: 45%; font-size: .85rem; }
.impianto-valori td { font-size: .9rem; word-break: break-word; }
.impianto-valori tr:last-child th, .impianto-valori tr:last-child td { border-bottom: 0; }
.valore-riservato { letter-spacing: .05em; }
body.dark-mode .impianto-scheda { background: #1a1f28; }
body.dark-mode .impianti-indice .nav-link { border-color: #2a3140 !important; }

/* Log di sistema: dettaglio dei valori registrati */
.table td.log-dettaglio { --bs-table-bg: #f6f8fa; }
.table td.log-dettaglio pre { white-space: pre-wrap; word-break: break-word; color: inherit; }
body.dark-mode .table td.log-dettaglio { --bs-table-bg: #161b24; --bs-table-color: #dfe3e8; }

/* Dashboard: card dei ticket sollecitati, lampeggiante in rosso quando ce ne sono */
@keyframes kpi-lampeggio {
    0%, 100% { background-color: #dc3545; color: #fff; box-shadow: 0 0 0 0 rgba(220, 53, 69, .6); }
    50%      { background-color: #fff0f1; color: #dc3545; box-shadow: 0 0 0 6px rgba(220, 53, 69, 0); }
}
.kpi-card.kpi-lampeggia { animation: kpi-lampeggio 1.2s ease-in-out infinite; border: 2px solid #dc3545; }
.kpi-card.kpi-lampeggia .kpi-label, .kpi-card.kpi-lampeggia .kpi-value { color: inherit !important; }
body.dark-mode .kpi-card.kpi-lampeggia { animation-name: kpi-lampeggio-scuro; }
@keyframes kpi-lampeggio-scuro {
    0%, 100% { background-color: #dc3545; color: #fff; }
    50%      { background-color: #3a1519; color: #ff8a95; }
}
/* Con «riduci animazioni» attivo nel sistema (Windows: Effetti animazione disattivati) il riquadro lampeggia
   comunque, perché è un avviso, ma più lentamente e senza l'alone */
@keyframes kpi-lampeggio-lento {
    0%, 100% { background-color: #dc3545; color: #fff; }
    50%      { background-color: #fff0f1; color: #dc3545; }
}
@media (prefers-reduced-motion: reduce) {
    .kpi-card.kpi-lampeggia, body.dark-mode .kpi-card.kpi-lampeggia { animation: kpi-lampeggio-lento 2s steps(2, jump-none) infinite; }
    body.dark-mode .kpi-card.kpi-lampeggia { animation-name: kpi-lampeggio-scuro; }
}
.badge-sollecito { vertical-align: middle; font-size: .55em; }

/* Calendario: totale dei ticket aperti nel giorno */
.cal-aperti { flex: 1; display: flex; flex-direction: column; align-items: center; justify-content: center; text-decoration: none; color: #0d6efd; position: relative; z-index: 2; }
.cal-aperti + .cal-chiusi { position: relative; z-index: 2; }
.cal-zero .cal-aperti { color: #0d6efd; }
body.dark-mode .cal-aperti, body.dark-mode .cal-zero .cal-aperti { color: #6ea8fe; }

/* Programmazione Lavori */
.tabella-programma .riga-gruppo td { background: #f8cbad; font-size: .95rem; text-align: center; letter-spacing: .02em; }
.tabella-programma .riga-evidenziata td { background: #fff2a8; }
.tabella-programma .riga-fatta td { background: #e8f5e9; }
.tabella-programma .riga-fatta td:not(:first-child):not(:last-child) { opacity: .8; }
.tabella-programma .riga-appena-salvata td { box-shadow: inset 0 0 0 2px #0d6efd; }
.screenshot-nc img { max-height: 90px; max-width: 220px; object-fit: contain; background: #fff; }
.anteprima-screenshot { max-height: 70px; max-width: 160px; object-fit: contain; }
.zona-incolla { border: 2px dashed #ced4da; border-radius: .5rem; padding: .6rem; font-size: .85rem; color: #6c757d; text-align: center; cursor: text; }
.zona-incolla:focus { border-color: #0d6efd; outline: none; }
.zona-incolla.incollato { border-color: #198754; color: #198754; }
.legenda-evidenziata { display: inline-block; width: 14px; height: 14px; background: #fff2a8; border: 1px solid #e0c95c; vertical-align: -2px; margin-right: 4px; }
.barra-scelta { top: 60px; z-index: 5; }
body.dark-mode .tabella-programma .riga-gruppo td { background: #5a3a28; color: #ffe2cf; }
body.dark-mode .tabella-programma .riga-evidenziata td { background: #4a4214; color: #fff4bd; }
body.dark-mode .tabella-programma .riga-fatta td { background: #17321f; }
body.dark-mode .zona-incolla { border-color: #3a4352; color: #9aa4b2; }
body.dark-mode .legenda-evidenziata { background: #4a4214; border-color: #7a6a20; }

/* ===== Guida (Aiuto) ===== */
.aiuto-indice { position: sticky; top: 1rem; max-height: calc(100vh - 2rem); overflow-y: auto; }
.aiuto-link { color: inherit; border-radius: 4px; padding-left: .25rem; }
.aiuto-link:hover { background: rgba(13,110,253,.08); }
.aiuto-sezione { scroll-margin-top: 80px; transition: box-shadow .3s, outline-color .3s; outline: 2px solid transparent; }
.aiuto-sezione.aiuto-attiva { outline-color: #0d6efd; box-shadow: 0 0 0 .3rem rgba(13,110,253,.18); }
.aiuto-testo p:last-child, .aiuto-testo ul:last-child, .aiuto-testo ol:last-child { margin-bottom: 0; }
.aiuto-testo dt { margin-top: .5rem; }
body.dark-mode .aiuto-link:hover { background: rgba(255,255,255,.08); }
body.dark-mode #btn-aiuto { color: #e9ecef !important; }
@media (max-width: 991.98px) { .aiuto-indice { position: static; max-height: 260px; } }
@media print {
  #sidebar, #topbar, nav[aria-label=breadcrumb], footer, #btn-aiuto, .d-print-none { display: none !important; }
  #content-wrapper { margin-left: 0 !important; }
  .aiuto-sezione { break-inside: avoid; box-shadow: none !important; border: 1px solid #ccc !important; }
  .col-lg-9 { width: 100% !important; }
}

/* ===== Menu a tendina con ricerca (select-ricerca.js) ===== */
.select-ricerca .select-ricerca-originale { position: absolute; left: 0; bottom: 0; width: 100%; height: 1px; min-height: 0; padding: 0; border: 0; opacity: 0; pointer-events: none; }
.select-ricerca-input { padding-right: 2.25rem; background-image: url("data:image/svg+xml,%3csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 16 16'%3e%3cpath fill='none' stroke='%236c757d' stroke-linecap='round' stroke-linejoin='round' stroke-width='2' d='m2 5 6 6 6-6'/%3e%3c/svg%3e"); background-repeat: no-repeat; background-position: right .75rem center; background-size: 16px 12px; }
.select-ricerca-input:focus { background-image: url("data:image/svg+xml,%3csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 16 16'%3e%3cpath fill='%236c757d' d='M11.7 10.3a6 6 0 1 0-1.4 1.4l3.5 3.5 1.4-1.4-3.5-3.5zM6.5 11a4.5 4.5 0 1 1 0-9 4.5 4.5 0 0 1 0 9z'/%3e%3c/svg%3e"); background-size: 14px 14px; }
.select-ricerca-input.is-invalid { background-image: none; }
.select-ricerca-lista { max-height: 320px; overflow-y: auto; top: 100%; left: 0; margin-top: 2px; z-index: 1056; }
.select-ricerca-lista .dropdown-item { white-space: normal; }
.select-ricerca-lista .dropdown-item.active { background-color: #0d6efd; color: #fff; }
