:root {
    --bg: #f5f6f8;
    --surface: #ffffff;
    --surface-2: #f0f2f5;
    --border: #e1e4e9;
    --text: #1d2330;
    --muted: #6b7385;
    --primary: #2f5bd3;
    --primary-text: #ffffff;
    --ok: #1f8a4c;
    --ok-bg: #e5f5ec;
    --warn: #a86400;
    --warn-bg: #fdf1dc;
    --danger: #c23434;
    --danger-bg: #fbe7e7;
    --chart: #2f5bd3;
    --radius: 10px;
    --shadow: 0 1px 2px rgba(16, 24, 40, .06);
    color-scheme: light;
}

@media (prefers-color-scheme: dark) {
    :root {
        --bg: #111418;
        --surface: #1a1e24;
        --surface-2: #232830;
        --border: #2e343e;
        --text: #e6e9ee;
        --muted: #949cab;
        --primary: #6d8ff0;
        --primary-text: #0d1220;
        --ok: #5fc98b;
        --ok-bg: #16301f;
        --warn: #e8a948;
        --warn-bg: #33270f;
        --danger: #f07a7a;
        --danger-bg: #3a1a1a;
        --chart: #6d8ff0;
        --shadow: none;
        color-scheme: dark;
    }
}

* { box-sizing: border-box; }

body {
    margin: 0;
    font: 15px/1.5 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
    background: var(--bg);
    color: var(--text);
}

a { color: var(--primary); }
h1 { font-size: 1.5rem; margin: 0 0 .25rem; }
h2 { font-size: 1.1rem; margin: 0 0 .75rem; }
.muted { color: var(--muted); }
.num { text-align: right; font-variant-numeric: tabular-nums; white-space: nowrap; }
.error { color: var(--danger); }

/* Layout */
.layout { display: flex; min-height: 100vh; }
.sidebar {
    width: 220px; flex-shrink: 0; background: var(--surface); border-right: 1px solid var(--border);
    padding: 1.25rem .75rem; display: flex; flex-direction: column; gap: .15rem;
    position: sticky; top: 0; height: 100vh;
}
.brand { font-weight: 700; font-size: 1.15rem; padding: 0 .75rem 1rem; }
.sidebar a {
    color: var(--text); text-decoration: none; padding: .55rem .75rem; border-radius: 8px;
}
.sidebar a:hover { background: var(--surface-2); }
.sidebar a.active { background: var(--primary); color: var(--primary-text); }
.sidebar .logout { margin-top: auto; display: flex; flex-direction: column; padding: .5rem .75rem 0; border-top: 1px solid var(--border); }
.sidebar .logout span { font-size: .8rem; overflow: hidden; text-overflow: ellipsis; }
.sidebar .logout a { padding: .35rem 0; color: var(--muted); }
#azienda { margin: 0 0 .75rem; font-weight: 600; }
.content { flex: 1; padding: 1.75rem 2rem 3rem; max-width: 1200px; min-width: 0; }

@media (max-width: 800px) {
    .layout { flex-direction: column; }
    .sidebar {
        width: auto; height: auto; position: static; flex-direction: row; overflow-x: auto;
        padding: .5rem 16px; border-right: 0; border-bottom: 1px solid var(--border);
    }
    .brand { display: none; }
    .sidebar a { white-space: nowrap; }
    .sidebar .logout { margin-top: 0; flex-direction: row; gap: .5rem; align-items: center; border-top: 0; padding: 0; }
    .sidebar .logout span { display: none; }
    #azienda { margin: 0; width: auto; max-width: 180px; }
    .content { padding: 1.25rem 16px 2rem; }
}

.page-head { display: flex; align-items: center; justify-content: space-between; gap: 1rem; flex-wrap: wrap; margin-bottom: 1.25rem; }
.page-head .actions { display: flex; gap: .5rem; align-items: center; flex-wrap: wrap; }

/* Componenti */
.card {
    background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius);
    padding: 1.25rem; box-shadow: var(--shadow);
}
.grid { display: grid; gap: 1rem; }
.kpis { grid-template-columns: repeat(auto-fit, minmax(190px, 1fr)); margin-bottom: 1rem; }
.kpi .label { color: var(--muted); font-size: .85rem; }
.kpi .value { font-size: 1.5rem; font-weight: 650; font-variant-numeric: tabular-nums; margin-top: .15rem; }
.kpi .sub { color: var(--muted); font-size: .8rem; }
.two-col { grid-template-columns: 2fr 1fr; }
@media (max-width: 1000px) { .two-col { grid-template-columns: 1fr; } }

.btn {
    display: inline-flex; align-items: center; gap: .35rem; border: 1px solid var(--border);
    background: var(--surface); color: var(--text); padding: .5rem .9rem; border-radius: 8px;
    font: inherit; cursor: pointer; text-decoration: none; white-space: nowrap;
}
.btn:hover { background: var(--surface-2); }
.btn.primary { background: var(--primary); border-color: var(--primary); color: var(--primary-text); }
.btn.primary:hover { filter: brightness(1.08); }
.btn.small { padding: .25rem .6rem; font-size: .85rem; }
.btn.danger { color: var(--danger); }
.btn.ghost { border-color: transparent; background: transparent; }

input, select, textarea {
    font: inherit; color: var(--text); background: var(--surface); border: 1px solid var(--border);
    border-radius: 8px; padding: .5rem .65rem; width: 100%;
}
input:focus, select:focus, textarea:focus { outline: 2px solid var(--primary); outline-offset: -1px; }
input[type=checkbox] { width: auto; }
label { display: flex; flex-direction: column; gap: .3rem; font-size: .85rem; color: var(--muted); }
label.check { flex-direction: row; align-items: center; gap: .5rem; color: var(--text); font-size: .95rem; }
.form-grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(200px, 1fr)); gap: .85rem; }
.form-grid .wide { grid-column: 1 / -1; }
fieldset { border: 1px solid var(--border); border-radius: var(--radius); padding: 1rem 1.25rem 1.25rem; margin: 0 0 1rem; background: var(--surface); }
legend { font-weight: 600; padding: 0 .4rem; }
.hint { font-size: .8rem; color: var(--muted); margin: .5rem 0 0; }

table { width: 100%; border-collapse: collapse; }
th, td { padding: .6rem .5rem; border-bottom: 1px solid var(--border); text-align: left; vertical-align: middle; }
th { font-size: .8rem; color: var(--muted); font-weight: 600; }
tr:last-child td { border-bottom: 0; }
.table-wrap { overflow-x: auto; }
td .row-actions { display: flex; gap: .3rem; justify-content: flex-end; }

.badge { display: inline-block; padding: .1rem .55rem; border-radius: 99px; font-size: .78rem; font-weight: 600; white-space: nowrap; }
.badge.ok { background: var(--ok-bg); color: var(--ok); }
.badge.warn { background: var(--warn-bg); color: var(--warn); }
.badge.danger { background: var(--danger-bg); color: var(--danger); }

.progress { height: 10px; background: var(--surface-2); border-radius: 99px; overflow: hidden; margin: .5rem 0; }
.progress > div { height: 100%; background: var(--primary); }
.progress.warn > div { background: var(--warn); }
.progress.danger > div { background: var(--danger); }

.chart svg { width: 100%; height: auto; display: block; }
.chart .bar { fill: var(--chart); }
.chart text { fill: var(--muted); font-size: 11px; }

.f24-card { margin-bottom: 1rem; }
.f24-card header { display: flex; justify-content: space-between; align-items: center; gap: 1rem; flex-wrap: wrap; margin-bottom: .5rem; }
.f24-card .date { font-size: 1.1rem; font-weight: 650; }
.f24-card .tot { font-size: 1.1rem; font-weight: 650; font-variant-numeric: tabular-nums; }
.f24-card.pagato { opacity: .75; }

.calc dl { display: grid; grid-template-columns: 1fr auto; gap: .35rem 1rem; margin: 0; }
.calc dt { color: var(--muted); }
.calc dd { margin: 0; text-align: right; font-variant-numeric: tabular-nums; }
.calc dd.strong, .calc dt.strong { font-weight: 650; color: var(--text); }

.empty { text-align: center; padding: 2rem; color: var(--muted); }

/* Modale */
dialog {
    border: 1px solid var(--border); border-radius: 14px; padding: 0; width: min(760px, calc(100vw - 32px));
    background: var(--surface); color: var(--text);
}
dialog::backdrop { background: rgba(10, 14, 20, .45); }
dialog header { display: flex; justify-content: space-between; align-items: center; padding: 1rem 1.25rem; border-bottom: 1px solid var(--border); }
dialog header h2 { margin: 0; }
dialog .modal-body { padding: 1.25rem; max-height: 70vh; overflow: auto; }
dialog footer { display: flex; justify-content: flex-end; gap: .5rem; padding: 1rem 1.25rem; border-top: 1px solid var(--border); }
.righe td { padding: .35rem .25rem; }
.righe input { padding: .4rem .5rem; }
.totali { margin-top: 1rem; display: grid; grid-template-columns: 1fr auto; gap: .25rem 1.5rem; max-width: 320px; margin-left: auto; }
.totali .strong { font-weight: 700; }

#toast { position: fixed; bottom: 1.25rem; left: 50%; transform: translateX(-50%); z-index: 100; }
#toast div { background: var(--text); color: var(--bg); padding: .65rem 1rem; border-radius: 8px; margin-top: .5rem; box-shadow: 0 4px 12px rgba(0,0,0,.2); }
#toast div.error { background: var(--danger); color: #fff; }

/* Login */
.login { min-height: 100vh; display: grid; place-items: center; padding: 16px; }
.login form { width: min(360px, 100%); display: flex; flex-direction: column; gap: 1rem; }

.dropzone { border: 2px dashed var(--border); border-radius: var(--radius); padding: 2rem; text-align: center; background: var(--surface); }
.dropzone.over { border-color: var(--primary); background: var(--surface-2); }
.steps { margin: 0; padding-left: 1.25rem; }
.steps li { margin-bottom: .35rem; }
