:root {
    --primary: #6366f1;
    --primary-dark: #4f46e5;
    --bg-dark: #0f172a;
    --sidebar-bg: rgba(30, 41, 59, 0.7);
    --card-bg: rgba(255, 255, 255, 0.05);
    --text-main: #f8fafc;
    --text-muted: #94a3b8;
    --glass-border: rgba(255, 255, 255, 0.1);
    --shadow-lg: 0 10px 15px -3px rgba(0, 0, 0, 0.1), 0 4px 6px -2px rgba(0, 0, 0, 0.05);
    --bg-gradient: radial-gradient(circle at top right, #1e1b4b, #0f172a);
}

body.light-mode {
    --bg-dark: #f1f5f9;
    --sidebar-bg: rgba(255, 255, 255, 0.8);
    --card-bg: rgba(255, 255, 255, 0.6);
    --text-main: #1e293b;
    --text-muted: #64748b;
    --glass-border: rgba(100, 116, 139, 0.1);
    --bg-gradient: radial-gradient(circle at top right, #e2e8f0, #f1f5f9);
}

* {
    margin: 0;
    padding: 0;
    box-sizing: border-box;
}

body {
    font-family: 'Outfit', sans-serif;
    background-color: var(--bg-dark);
    color: var(--text-main);
    overflow: hidden;
    height: 100vh;
    transition: all 0.3s ease;
}

.app-container {
    display: flex;
    height: 100vh;
    background: var(--bg-gradient);
    transition: background 0.3s ease;
}

/* Sidebar */
.sidebar {
    width: 280px;
    background: var(--sidebar-bg);
    backdrop-filter: blur(12px);
    border-right: 1px solid var(--glass-border);
    display: flex;
    flex-direction: column;
    z-index: 10;
}

.sidebar-header {
    padding: 3rem 2rem;
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 1.5rem;
    text-align: center;
}

.sidebar-header img.logo {
    max-height: 60px;
    width: auto;
    object-fit: contain;
}

.sidebar-header h1 {
    font-size: 1.25rem;
    font-weight: 600;
    background: linear-gradient(to right, #818cf8, #c084fc);
    -webkit-background-clip: text;
    -webkit-text-fill-color: transparent;
}

.sidebar-nav {
    flex: 1;
    padding: 1rem;
}

.sidebar-nav ul {
    list-style: none;
}

.sidebar-nav li {
    margin-bottom: 0.5rem;
}

.sidebar-nav a {
    display: flex;
    align-items: center;
    padding: 0.75rem 1rem;
    color: var(--text-muted);
    text-decoration: none;
    border-radius: 0.75rem;
    transition: all 0.3s ease;
}

.sidebar-nav a:hover, .sidebar-nav li.active a {
    background: var(--primary);
    color: white;
    transform: translateX(5px);
}

/* Main Content */
.main-content {
    flex: 1;
    display: flex;
    flex-direction: column;
    overflow: hidden;
}

.top-bar {
    height: 80px;
    padding: 0 2rem;
    display: flex;
    align-items: center;
    justify-content: space-between;
    background: rgba(15, 23, 42, 0.5);
    backdrop-filter: blur(8px);
    border-bottom: 1px solid var(--glass-border);
    position: relative;
    z-index: 1000; /* High z-index to ensure dropdowns are on top */
}

.top-bar-right {
    display: flex;
    align-items: center;
    gap: 1.5rem;
}

.cache-info {
    font-size: 0.85rem;
    color: var(--text-muted);
}

.theme-btn {
    background: rgba(255, 255, 255, 0.05);
    border: 1px solid var(--glass-border);
    color: var(--text-main);
    width: 40px;
    height: 40px;
    border-radius: 12px;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    transition: all 0.3s ease;
}

.theme-btn:hover {
    background: rgba(255, 255, 255, 0.1);
    transform: translateY(-2px);
    box-shadow: 0 4px 12px rgba(0,0,0,0.1);
}

.update-btn {
    background: var(--primary);
    color: white;
    border: none;
    padding: 0.5rem 1rem;
    border-radius: 0.5rem;
    cursor: pointer;
    font-family: inherit;
    font-size: 0.85rem;
    transition: all 0.3s ease;
    display: flex;
    align-items: center;
    gap: 0.5rem;
}

.update-btn:hover {
    background: var(--primary-dark);
    transform: translateY(-2px);
}

.filters-container {
    display: flex;
    gap: 1.5rem;
    align-items: flex-end;
}

.filter-wrapper {
    display: flex;
    flex-direction: column;
    gap: 0.25rem;
}

.filter-wrapper label {
    font-size: 0.75rem;
    color: var(--text-muted);
    font-weight: 600;
}

.filters-container select[multiple] {
    height: 60px;
    min-width: 120px;
}

.multi-select-combo {
    position: relative;
    min-width: 180px;
}

.combo-header {
    background: var(--card-bg);
    border: 1px solid var(--glass-border);
    color: white;
    padding: 0.5rem 1rem;
    border-radius: 0.5rem;
    cursor: pointer;
    display: flex;
    justify-content: space-between;
    align-items: center;
    font-size: 0.85rem;
    transition: all 0.3s ease;
}

.combo-header:hover {
    border-color: var(--primary);
}

.combo-dropdown {
    position: absolute;
    top: calc(100% + 5px);
    left: 0;
    right: 0;
    background: #1e293b;
    border: 1px solid var(--glass-border);
    border-radius: 0.5rem;
    padding: 0.5rem;
    max-height: 250px;
    overflow-y: auto;
    z-index: 1100;
    box-shadow: 0 10px 25px -5px rgba(0, 0, 0, 0.3);
    backdrop-filter: blur(10px);
}

.checkbox-item {
    display: flex;
    align-items: center;
    gap: 0.5rem;
    padding: 0.25rem 0;
    font-size: 0.85rem;
    cursor: pointer;
    color: var(--text-main);
}

.checkbox-item input {
    cursor: pointer;
}

.filters-container select, .filters-container input {
    background: var(--card-bg);
    border: 1px solid var(--glass-border);
    color: white;
    padding: 0.5rem 1rem;
    border-radius: 0.5rem;
    font-family: inherit;
    outline: none;
}

.filters-container select:focus, .filters-container input:focus {
    border-color: var(--primary);
}

.dashboard-viewport {
    flex: 1;
    padding: 2rem;
    overflow-y: auto;
    overflow-x: hidden;
    position: relative;
}

.content-grid {
    display: grid;
    grid-template-columns: repeat(auto-fill, minmax(300px, 1fr));
    gap: 1.5rem;
}

/* Glass Card */
.glass-card {
    background: var(--card-bg);
    backdrop-filter: blur(8px);
    border: 1px solid var(--glass-border);
    border-radius: 1rem;
    padding: 1.5rem;
    box-shadow: var(--shadow-lg);
    transition: transform 0.3s ease;
}

.glass-card:hover {
    transform: translateY(-5px);
}

.welcome-msg {
    grid-column: 1 / -1;
    text-align: center;
    padding: 4rem;
    opacity: 0.7;
}

.welcome-msg i {
    font-size: 4rem;
    margin-bottom: 1rem;
    color: var(--primary);
}

/* Loader */
.loader {
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
}

.spinner {
    width: 40px;
    height: 40px;
    border: 4px solid rgba(255, 255, 255, 0.1);
    border-left-color: var(--primary);
    border-radius: 50%;
    animation: spin 1s linear infinite;
}

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

.hidden { display: none; }

/* Table Styling (if used) */
.data-table {
    width: 100%;
    border-collapse: collapse;
    background: var(--card-bg);
    border-radius: 1rem;
    overflow: hidden;
}

.data-table th, .data-table td {
    padding: 1rem;
    text-align: left;
    border-bottom: 1px solid var(--glass-border);
    white-space: nowrap;
}

.data-table th:not(:first-child), .data-table td:not(:first-child) {
    text-align: right;
}

.data-table th {
    background: rgba(255, 255, 255, 0.05);
    color: var(--text-muted);
    font-weight: 600;
    text-transform: uppercase;
    font-size: 0.75rem;
    letter-spacing: 0.05em;
    vertical-align: top;
}

/* le celle di dettaglio ereditano l'allineamento a destra delle colonne
   numeriche: elenchi e descrizioni vanno invece letti da sinistra */
.details-cell,
.details-cell .year-details,
.details-cell li,
.details-cell .docs-val {
    text-align: left;
}

.details-cell .year-details ul {
    list-style: none;
    padding: 0;
    margin: 0.25rem 0 0.75rem 0;
}

.details-cell .year-details li {
    display: flex;
    justify-content: space-between;
    gap: 1rem;
    padding: 0.15rem 0;
}

/* pannello di filtro combinabile sui flag */
.filtro-flag {
    grid-column: 1 / -1;
    margin-top: 2rem;
}

.filtro-flag-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    flex-wrap: wrap;
    gap: 1rem;
}

.filtro-flag-header h3 { margin: 0; }

.filtro-flag-modo {
    display: flex;
    align-items: center;
    gap: 1rem;
    font-size: 0.85rem;
    color: var(--text-muted);
}

.filtro-flag-modo label {
    display: flex;
    align-items: center;
    gap: 0.35rem;
    cursor: pointer;
}

.filtro-flag-riepilogo {
    margin: 0.75rem 0;
    font-size: 0.85rem;
    color: var(--text-muted);
}

.filtro-flag-griglia {
    display: grid;
    grid-template-columns: repeat(auto-fill, minmax(260px, 1fr));
    gap: 0.35rem;
    max-height: 18rem;
    overflow-y: auto;
}

.filtro-flag-voce {
    display: flex;
    align-items: center;
    gap: 0.4rem;
    padding: 0.35rem 0.5rem;
    border-radius: 8px;
    font-size: 0.82rem;
    cursor: pointer;
    transition: background 0.15s;
}

.filtro-flag-voce:hover { background: rgba(255, 255, 255, 0.05); }

.filtro-flag-voce.attivo {
    background: rgba(99, 102, 241, 0.18);
    border: 1px solid rgba(99, 102, 241, 0.35);
    padding: calc(0.35rem - 1px) calc(0.5rem - 1px);
}

/* terzo livello di esplosione: gruppo -> singoli codici GO */
.riga-gruppo { cursor: pointer; }
.riga-gruppo:hover { background: rgba(99, 102, 241, 0.08); }

.toggle-mini {
    font-size: 0.6rem;
    margin-right: 0.4rem;
    color: var(--text-muted);
    transition: transform 0.2s;
}

.sub-table-livello3 {
    width: 100%;
    background: rgba(0, 0, 0, 0.12);
}

.sub-table-livello3 th {
    background: transparent;
    font-size: 0.62rem;
    position: static;
}

.sub-table-livello3 td {
    font-size: 0.8rem;
    padding: 0.35rem 0.75rem;
}

.sub-table-livello3 td:first-child { padding-left: 2rem; }

/* quarto livello: i singoli codici GO dentro il gruppo */
.sub-table-livello4 {
    width: 100%;
    background: rgba(0, 0, 0, 0.2);
}

.sub-table-livello4 td {
    font-size: 0.78rem;
    padding: 0.3rem 0.75rem;
    text-align: right;
    white-space: nowrap;
    border-bottom: 1px solid var(--glass-border);
}

.sub-table-livello4 td:first-child { padding-left: 3.5rem; text-align: left; }

/* tabella annidata nel dettaglio di una riga */
.sub-table-wrap {
    overflow-x: auto;
    max-height: 26rem;
    overflow-y: auto;
    margin: 0.5rem 0;
}

.sub-table {
    width: 100%;
    border-collapse: collapse;
    font-size: 0.85rem;
}

.sub-table th {
    position: sticky;
    top: 0;
    background: var(--card-bg, rgba(30, 32, 60, 0.95));
    backdrop-filter: blur(4px);
    color: var(--text-muted);
    font-weight: 600;
    text-transform: uppercase;
    font-size: 0.68rem;
    letter-spacing: 0.04em;
    padding: 0.5rem 0.75rem;
    text-align: right;
    white-space: nowrap;
    border-bottom: 1px solid var(--glass-border);
}

.sub-table td {
    padding: 0.5rem 0.75rem;
    text-align: right;
    white-space: nowrap;
    border-bottom: 1px solid var(--glass-border);
}

.sub-table tbody tr:hover {
    background: rgba(255, 255, 255, 0.03);
}

.th-hint {
    display: block;
    text-transform: none;
    letter-spacing: 0;
    font-weight: 400;
    font-size: 0.68rem;
    opacity: 0.65;
}

th.sortable {
    cursor: pointer;
    transition: color 0.2s;
}

th.sortable:hover {
    color: white;
}

th.sortable i {
    margin-left: 0.5rem;
    font-size: 0.8rem;
    opacity: 0.5;
}

th.sortable:hover i, th.sortable.asc i, th.sortable.desc i {
    opacity: 1;
    color: var(--primary);
}

.table-search {
    width: 100%;
    margin-top: 0.5rem;
    padding: 0.25rem 0.5rem;
    background: rgba(0,0,0,0.2);
    border: 1px solid var(--glass-border);
    border-radius: 0.25rem;
    color: white;
    font-size: 0.75rem;
    font-family: inherit;
    text-transform: none;
    letter-spacing: normal;
}

.table-search:focus {
    outline: none;
    border-color: var(--primary);
}

.amount-val {
    font-weight: 600;
    font-size: 1rem;
}

.docs-val {
    font-size: 0.75rem;
    color: var(--text-muted);
    margin-top: 2px;
}

.chart-container {
    grid-column: 1 / -1;
    height: 500px;
    padding: 2rem;
}

.kpi-container {
    grid-column: 1 / -1;
    padding: 1.5rem;
    margin-bottom: 1rem;
}

.kpi-container h3 {
    margin-bottom: 1.5rem;
    font-weight: 600;
    color: var(--text-main);
}

.kpi-grid {
    display: grid;
    grid-template-columns: repeat(3, 1fr);
    gap: 1.5rem;
}

.kpi-card {
    background: rgba(255, 255, 255, 0.03);
    padding: 1.5rem;
    border-radius: 1rem;
    border: 1px solid var(--glass-border);
}

.kpi-label {
    font-size: 0.85rem;
    color: var(--text-muted);
    margin-bottom: 0.5rem;
    text-transform: uppercase;
    letter-spacing: 0.05em;
}

.kpi-value {
    font-size: 2.5rem;
    font-weight: 600;
    margin-bottom: 0.5rem;
    display: flex;
    flex-direction: column;
}

.kpi-detail {
    font-size: 0.9rem;
    font-weight: 400;
    color: var(--text-muted);
    margin-top: -0.25rem;
    margin-bottom: 0.5rem;
}

.kpi-sub {
    font-size: 0.75rem;
    color: var(--text-muted);
}

.text-success { color: #10b981; }
.text-danger { color: #ef4444; }
.text-primary { color: #6366f1; }
.text-warning { color: #f59e0b; }
.muted { color: var(--text-muted); font-weight: 400; }

/* Classe ABC per impatto: A = alto, C = marginale */
.badge-a, .badge-b, .badge-c, .badge-info {
    display: inline-block;
    padding: 0.1rem 0.45rem;
    border-radius: 6px;
    font-size: 0.7rem;
    font-weight: 600;
    margin-right: 0.35rem;
    vertical-align: middle;
}

.badge-a { background: rgba(239, 68, 68, 0.18); color: #ef4444; border: 1px solid rgba(239, 68, 68, 0.35); }
.badge-b { background: rgba(245, 158, 11, 0.18); color: #f59e0b; border: 1px solid rgba(245, 158, 11, 0.35); }
.badge-c { background: rgba(148, 163, 184, 0.18); color: var(--text-muted); border: 1px solid rgba(148, 163, 184, 0.3); }
.badge-info {
    background: rgba(99, 102, 241, 0.15);
    color: #818cf8;
    border: 1px solid rgba(99, 102, 241, 0.3);
    font-weight: 400;
    margin-left: 0.35rem;
}

.table-wrapper {
    grid-column: 1 / -1;
    display: flex;
    flex-direction: column;
    /* le celle sono nowrap: senza questi due, le tabelle larghe spingono
       la pagina invece di scorrere al proprio interno */
    min-width: 0;
    overflow-x: auto;
}

.table-actions {
    display: flex;
    justify-content: flex-end;
    align-items: center;
    gap: 1rem;
    margin-bottom: 1rem;
}

.table-note {
    margin-right: auto;
    font-size: 0.8rem;
    color: var(--text-muted);
}

.export-btn {
    background: rgba(255, 255, 255, 0.05);
    color: var(--text-main);
    border: 1px solid var(--glass-border);
    padding: 0.6rem 1.2rem;
    border-radius: 0.75rem;
    cursor: pointer;
    font-family: inherit;
    font-size: 0.85rem;
    transition: all 0.3s ease;
    display: inline-flex;
    align-items: center;
    gap: 0.5rem;
}

.export-btn:hover {
    background: rgba(255, 255, 255, 0.1);
    border-color: var(--primary);
    transform: translateY(-2px);
}

.data-table tr:last-child td {
    border-bottom: none;
}

.data-table tr:hover {
    background: rgba(255, 255, 255, 0.02);
}

/* Expandable Table Styles */
.expandable-table .main-row {
    cursor: pointer;
    transition: background 0.2s ease;
}

.expandable-table .main-row:hover {
    background: rgba(99, 102, 241, 0.05) !important;
}

.toggle-cell {
    width: 40px;
    text-align: center;
    color: var(--primary);
}

.details-cell {
    padding: 1rem 0.5rem;
    vertical-align: top;
}

.details-row {
    background: rgba(0, 0, 0, 0.1);
}

.details-row.hidden {
    display: none;
}

.year-details ul {
    list-style: none;
    margin: 0;
    padding: 0;
}

.year-details li {
    display: flex;
    flex-direction: column;
    padding: 0.6rem 0;
    border-bottom: 1px solid var(--glass-border);
    font-size: 0.85rem;
    line-height: 1.4;
}

.year-details li:last-child {
    border-bottom: none;
}

.year-details li span {
    color: var(--text-muted);
    margin-bottom: 0.2rem;
}

.year-details li strong {
    color: var(--primary);
    font-weight: 600;
}
