/* ============================================
   GESTIONALE - Custom Styles
   Tema chiaro/scuro (data-bs-theme) + layout
   ============================================ */

/* ===== Variabili di tema (unica fonte di verità) ===== */
:root,
[data-bs-theme="light"] {
    color-scheme: light;
    --primary: #0d6efd;
    --success: #198754;
    --danger: #dc3545;
    --warning: #ffc107;
    --info: #0dcaf0;
    --dark: #212529;
    --light: #f8f9fa;

    /* brand (navbar / header / bottoni principali) */
    --brand-start: #667eea;
    --brand-end: #764ba2;

    /* superfici */
    --bg-body: #f8f9fa;
    --surface: #ffffff;
    --surface-2: #f8f9fa;
    --surface-3: #e9ecef;

    /* bordi */
    --border: #dee2e6;
    --border-soft: #e9ecef;
    --border-faint: #f1f3f5;

    /* testo */
    --text-body: #212529;
    --text-soft: #495057;
    --text-muted: #6c757d;

    /* ombre */
    --shadow: rgba(0, 0, 0, 0.08);
    --shadow-lg: rgba(0, 0, 0, 0.15);
}

[data-bs-theme="dark"] {
    color-scheme: dark;
    --bg-body: #15171a;
    --surface: #1f2327;
    --surface-2: #262b31;
    --surface-3: #2f353c;
    --border: #343a41;
    --border-soft: #2a3037;
    --border-faint: #242a2f;
    --text-body: #e9ecef;
    --text-soft: #c9d1d9;
    --text-muted: #9aa4af;
    --shadow: rgba(0, 0, 0, 0.35);
    --shadow-lg: rgba(0, 0, 0, 0.5);
}

body {
    background-color: var(--bg-body);
    color: var(--text-body);
    transition: background-color 0.2s ease, color 0.2s ease;
    font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
}

/* ============ NAVBAR ============ */
.navbar {
    box-shadow: 0 2px 4px rgba(0,0,0,0.1);
    background: linear-gradient(135deg, var(--brand-start) 0%, var(--brand-end) 100%);
}

.navbar-brand {
    font-weight: 700;
    font-size: 1.3rem;
}

.nav-link {
    margin-left: 0.5rem;
    transition: all 0.3s ease;
}

.nav-link:hover {
    transform: translateY(-2px);
}

/* ============ CARDS ============ */
.card {
    border: none;
    border-radius: 10px;
    box-shadow: 0 1px 4px var(--shadow);
    transition: box-shadow 0.2s ease;
    margin-bottom: 1.25rem;
}

.card:hover {
    box-shadow: 0 3px 10px var(--shadow-lg);
}

.card-header {
    background: linear-gradient(135deg, var(--brand-start) 0%, var(--brand-end) 100%);
    color: white;
    border-radius: 10px 10px 0 0 !important;
    border: none;
    padding: 0.8rem 1.25rem;
}

/* ============ STAT CARDS ============ */
.stat-card {
    background: var(--surface);
    border-radius: 10px;
    padding: 1.5rem;
    text-align: center;
    border: 2px solid var(--border-soft);
    border-left: 4px solid;
    transition: all 0.3s ease;
}

.stat-card:hover {
    transform: translateY(-5px);
    box-shadow: 0 5px 15px var(--shadow-lg);
}

.stat-number {
    font-size: 2rem;
    font-weight: 700;
    color: var(--text-body);
}

.stat-label {
    font-size: 0.9rem;
    color: var(--text-muted);
    text-transform: uppercase;
    letter-spacing: 0.5px;
    margin-top: 0.5rem;
}

.stat-card.success { border-color: var(--success); }
.stat-card.success .stat-number { color: var(--success); }

.stat-card.info { border-color: var(--info); }
.stat-card.info .stat-number { color: var(--info); }

.stat-card.danger { border-color: var(--danger); }
.stat-card.danger .stat-number { color: var(--danger); }

/* ============ TABLES ============ */
.table {
    background-color: var(--surface);
    border-radius: 8px;
    overflow: hidden;
}

.table thead th {
    background-color: var(--surface-2);
    border-bottom: 2px solid var(--border);
    font-weight: 600;
    color: var(--text-soft);
}

.table tbody tr:hover {
    background-color: var(--surface-2);
}

/* Celle più compatte per maggiore densità informativa */
.table > :not(caption) > * > * {
    padding: 0.45rem 0.6rem;
}

/* Dettagli JSON del registro attività */
.audit-changes-pre {
    font-size: 0.8rem;
    margin: 0.25rem 0 0;
    padding: 0.5rem;
    border-radius: 6px;
    background-color: var(--surface-2);
    max-height: 200px;
    overflow: auto;
}

/* Righe cliccabili (click sulla riga -> dettaglio) */
table[data-row-click] tbody tr[data-id] {
    cursor: pointer;
}

/* Sentinella lazy-load: nasconde il testo "Caricamento..." quando lo spinner è attivo */
tr.table-load-sentinel > td {
    background-color: var(--surface-2);
}

/* Colonna larghezze tabelle righe computo documento */
.table-righe-col-num { width: 40px; }
.table-righe-col-um { width: 80px; }
.table-righe-col-qta { width: 90px; }
.table-righe-col-prezzo { width: 130px; }
.table-righe-col-totale { width: 130px; }
.table-righe-col-azioni { width: 50px; }

/* ============ FORMS ============ */
.form-control:focus,
.form-select:focus {
    border-color: var(--primary);
    box-shadow: 0 0 0 0.2rem rgba(13, 110, 253, 0.25);
}

.page-header {
    margin-bottom: 1rem;
}

.page-title {
    color: var(--text-body);
    font-weight: 700;
    margin-bottom: 0;
    display: flex;
    align-items: center;
    gap: 0.5rem;
    font-size: 1.75rem;
}

.main-container {
    padding: 1.5rem 1rem 2.5rem;
}

@media (max-width: 991.98px) {
    .main-container {
        padding: 1rem 0.75rem 2rem;
    }
}

/* ============ BUTTONS ============ */
.btn {
    border-radius: 8px;
    font-weight: 500;
    transition: all 0.3s ease;
    padding: 0.5rem 1rem;
}

.btn-primary {
    background: linear-gradient(135deg, var(--brand-start) 0%, var(--brand-end) 100%);
    border: none;
}

.btn-primary:hover {
    transform: translateY(-2px);
    box-shadow: 0 5px 15px rgba(102, 126, 234, 0.4);
}

.btn-sm {
    padding: 0.35rem 0.7rem;
    font-size: 0.85rem;
}

/* ============ BADGES ============ */
.badge {
    padding: 0.5rem 0.75rem;
    font-weight: 500;
    border-radius: 6px;
}

/* ============ SEARCH FORM ============ */
.search-form {
    background: var(--surface);
    padding: 0.75rem 1rem;
    border-radius: 10px;
    margin-bottom: 1.25rem;
    box-shadow: 0 1px 3px var(--shadow);
}

.form-control-sm,
.form-select-sm {
    height: auto;
    padding: 0.35rem 0.6rem;
    font-size: 0.875rem;
    border-radius: 6px;
}

/* ============ ACTION BUTTONS ============ */
.action-buttons {
    display: flex;
    gap: 0.5rem;
    flex-wrap: wrap;
    align-items: center;
}

.action-buttons.justify-content-end {
    justify-content: flex-end;
}

/* ============ PAGINATION ============ */
.pagination {
    gap: 0.5rem;
}

.page-link {
    border-radius: 6px;
    color: var(--primary);
    border: 1px solid var(--border);
}

.page-link:hover {
    color: white;
    background-color: var(--primary);
    border-color: var(--primary);
}

.page-link.active {
    background-color: var(--primary);
    border-color: var(--primary);
}

/* ============ FORMS ============ */
.form-section {
    background: var(--surface);
    padding: 2rem;
    border-radius: 10px;
    margin-bottom: 2rem;
    box-shadow: 0 1px 3px var(--shadow);
}

.form-section h5 {
    color: var(--primary);
    font-weight: 700;
    margin-bottom: 1.5rem;
    padding-bottom: 1rem;
    border-bottom: 2px solid var(--border-soft);
}

/* ============ FOOTER ============ */
.footer {
    background-color: var(--surface);
    padding: 2rem;
    text-align: center;
    color: var(--text-muted);
    margin-top: 3rem;
    border-top: 1px solid var(--border-soft);
}

/* ============ SIDEBAR ============ */
.sidebar {
    background-color: var(--surface);
    border-radius: 10px;
    padding: 1.5rem;
    box-shadow: 0 1px 3px var(--shadow);
    margin-bottom: 2rem;
}

/* ============ RESPONSIVE ============ */
@media (max-width: 768px) {
    .stat-number {
        font-size: 1.8rem;
    }

    .page-title {
        font-size: 1.25rem;
    }

    .table {
        font-size: 0.85rem;
    }

    .btn {
        padding: 0.4rem 0.8rem;
        font-size: 0.9rem;
    }

    .page-header .row {
        flex-direction: column;
    }

    .page-header .col-auto,
    .page-header .col {
        width: 100%;
        margin-left: 0 !important;
    }

    .action-buttons.justify-content-end {
        justify-content: flex-start;
        margin-top: 1rem;
    }
}

/* ============ ANIMATIONS ============ */
@keyframes fadeIn {
    from {
        opacity: 0;
        transform: translateY(10px);
    }
    to {
        opacity: 1;
        transform: translateY(0);
    }
}

.card {
    animation: fadeIn 0.3s ease-in-out;
}

/* ============ TABLE UTILITIES ============ */
.table-sortable-header {
    cursor: pointer;
    -webkit-user-select: none;
    -moz-user-select: none;
    -ms-user-select: none;
    user-select: none;
}

.table-sortable-header:hover {
    background-color: var(--surface-3);
}

.sort-icon {
    opacity: 0.3;
    transition: opacity 0.3s ease;
}

.table-sortable-header:hover .sort-icon {
    opacity: 0.6;
}

/* Column width utilities */
.col-checkbox {
    width: 40px;
}

/* ============ COLONNE PERSONALIZZABILI ============ */
.columns-menu {
    min-width: 260px;
    max-height: 70vh;
    overflow-y: auto;
}

.columns-menu .dropdown-item {
    padding: 0.35rem 0.75rem;
}

.columns-item label {
    cursor: pointer;
    width: 100%;
}

/* Testo lungo (descrizioni/note): troncamento con ellissi + tooltip nativo */
.table-col-truncate {
    max-width: 260px;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}

/* Listino a tutta larghezza su schermi grandi */
@media (min-width: 1200px) {
    .main-container.listino-container {
        max-width: 100%;
    }
}

.col-action-icon {
    width: 50px;
    text-align: center;
}

/* ============ ACCESSIBILITY ============ */
.dropdown-user {
    cursor: pointer;
    -webkit-user-select: none;
    -moz-user-select: none;
    -ms-user-select: none;
    user-select: none;
}

.badge.dropdown-toggle {
    cursor: pointer;
}

/* ============ DETAIL PAGES ============ */
.detail-label {
    font-size: 0.8rem;
    font-weight: 600;
    text-transform: uppercase;
}

.detail-title {
    font-size: 1.1rem;
    margin: 0;
}

.detail-text {
    margin: 0;
}

.detail-list {
    font-size: 0.9rem;
    list-style: none;
    padding: 0;
}

.detail-list li {
    margin-bottom: 0.5rem;
}

.preformatted-text {
    white-space: pre-wrap;
    word-wrap: break-word;
}

/* ============ TABLES ============ */
.table-row-bold {
    font-weight: 700;
    border-top: 2px solid var(--border);
}

/* ============ DETAIL PAGES (modernizzate) ============ */
.info-list {
    list-style: none;
    padding: 0;
    margin: 0;
}

.info-list li {
    display: flex;
    align-items: center;
    gap: 0.75rem;
    padding: 0.6rem 0;
    border-bottom: 1px solid var(--border-faint);
}

.info-list li:last-child {
    border-bottom: 0;
}

.info-list li > i {
    width: 1.4rem;
    color: var(--brand-start);
    font-size: 1.1rem;
    flex-shrink: 0;
}

.info-list li > span:first-of-type {
    min-width: 8.5rem;
    color: var(--text-muted);
    font-size: 0.85rem;
    flex-shrink: 0;
}

.meta-label {
    font-size: 0.78rem;
    font-weight: 600;
    text-transform: uppercase;
    letter-spacing: 0.3px;
    margin-bottom: 0.25rem;
}

.map-frame {
    border-radius: 0.5rem;
    overflow: hidden;
    border: 1px solid var(--border);
}

.map-frame iframe {
    border: 0;
    width: 100%;
    height: 100%;
}

/* ============ EMPTY STATE ============ */
.empty-state {
    text-align: center;
    padding: 3rem 1rem;
    color: var(--text-muted);
}

.empty-state i {
    margin-bottom: 1rem;
    opacity: 0.5;
}

/* ============ DROPDOWN UTENTE ============ */
.dropdown-menu-custom {
    border: none;
    border-radius: 8px;
    box-shadow: 0 4px 12px var(--shadow-lg);
    margin-top: 0.5rem;
}

.dropdown-menu-custom .dropdown-item {
    padding: 0.75rem 1rem;
    transition: all 0.2s ease;
}

.dropdown-menu-custom .dropdown-item:hover {
    background-color: var(--surface-2);
    color: var(--brand-start);
}

.dropdown-menu-custom .dropdown-item i {
    margin-right: 0.5rem;
    width: 1.2rem;
}

/* ============ TOGGLE TEMA ============ */
.theme-toggle {
    border: none;
    background: transparent;
    color: rgba(255, 255, 255, 0.92);
    font-size: 1.15rem;
    line-height: 1;
    padding: 0.35rem 0.6rem;
    border-radius: 8px;
    transition: color 0.2s ease, transform 0.2s ease;
}

.theme-toggle:hover {
    color: #fff;
    transform: translateY(-2px);
}

/* ============ ADJUSTMENTS TEMA SCURO ============ */
[data-bs-theme="dark"] .table-light {
    --bs-table-bg: var(--surface-2);
    --bs-table-color: var(--text-body);
    --bs-table-border-color: var(--border);
    --bs-table-hover-bg: var(--surface-3);
    --bs-table-hover-color: var(--text-body);
}

[data-bs-theme="dark"] .card-header.bg-light {
    background-color: var(--surface-2) !important;
}

[data-bs-theme="dark"] .card-header.bg-light.text-dark {
    color: var(--text-body) !important;
}
