:root {
    --primary:      #1e3a5f;
    --blue:         #2563eb;
    --blue-hover:   #1d4ed8;
    --green:        #16a34a;
    --red:          #dc2626;
    --amber:        #d97706;
    --bg:           #f1f5f9;
    --card:         #ffffff;
    --text:         #1e293b;
    --text-muted:   #64748b;
    --border:       #e2e8f0;
    --sidebar-w:    220px;
}

* { box-sizing: border-box; margin: 0; padding: 0; }
body {
    font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
    background: var(--bg);
    color: var(--text);
    min-height: 100vh;
    display: flex;
    font-size: 14px;
    overflow-x: hidden;
}
a { text-decoration: none; color: inherit; }

/* ── SIDEBAR ─────────────────────────────────── */
.sidebar {
    width: var(--sidebar-w);
    background: var(--primary);
    display: flex;
    flex-direction: column;
    position: fixed;
    top: 0; left: 0;
    height: 100vh;
    z-index: 100;
    overflow-y: auto;
}
.sidebar-logo {
    padding: 14px 16px;
    border-bottom: 1px solid rgba(255,255,255,0.1);
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 6px;
}
.sidebar-logo img {
    width: 110px;
    height: 44px;
    object-fit: contain;
    filter: brightness(0) invert(1);
    opacity: 0.9;
}
.sidebar-logo .app-name {
    font-size: 0.68rem;
    text-transform: uppercase;
    letter-spacing: 0.12em;
    color: rgba(255,255,255,0.4);
}
.nav-section {
    font-size: 0.62rem;
    text-transform: uppercase;
    letter-spacing: 0.12em;
    color: rgba(255,255,255,0.3);
    padding: 10px 16px 3px;
    margin-top: 4px;
}
.nav-link {
    display: flex;
    align-items: center;
    gap: 9px;
    padding: 8px 16px;
    color: #94a3b8;
    font-size: 0.855rem;
    border-left: 3px solid transparent;
    transition: all 0.15s;
}
.nav-link:hover { background: rgba(255,255,255,0.07); color: #e2e8f0; }
.nav-link.active { background: rgba(37,99,235,0.25); color: #fff; border-left-color: var(--blue); }
.nav-link .ni { font-size: 0.95rem; width: 17px; text-align: center; flex-shrink: 0; }
.sidebar-footer {
    padding: 12px 16px;
    border-top: 1px solid rgba(255,255,255,0.08);
    font-size: 0.78rem;
    color: rgba(255,255,255,0.35);
}
.sidebar-footer a { color: rgba(255,255,255,0.45); }
.sidebar-footer a:hover { color: #fff; }

/* ── MAIN ─────────────────────────────────────── */
.main-wrap {
    margin-left: var(--sidebar-w);
    flex: 1;
    display: flex;
    flex-direction: column;
    min-height: 100vh;
}
.topbar {
    background: #fff;
    border-bottom: 1px solid var(--border);
    padding: 13px 24px;
    display: flex;
    align-items: center;
    justify-content: space-between;
    position: sticky;
    top: 0;
    z-index: 50;
}
.topbar h1 { font-size: 1.05rem; font-weight: 600; }
.topbar .user-chip {
    display: flex;
    align-items: center;
    gap: 8px;
    font-size: 0.82rem;
    color: var(--text-muted);
}
.page { padding: 22px 24px; flex: 1; }

/* ── CARDS ────────────────────────────────────── */
.card {
    background: var(--card);
    border-radius: 8px;
    border: 1px solid var(--border);
    overflow: hidden;
}
.card + .card { margin-top: 16px; }
.card-header {
    padding: 13px 18px;
    border-bottom: 1px solid var(--border);
    display: flex;
    align-items: center;
    justify-content: space-between;
}
.card-title { font-size: 0.92rem; font-weight: 600; }
.card-body { padding: 18px; }

.tabs {
    display: flex;
    gap: 8px;
    flex-wrap: wrap;
    margin-bottom: 16px;
}
.tab-link {
    display: inline-flex;
    align-items: center;
    gap: 7px;
    padding: 9px 13px;
    border: 1px solid var(--border);
    border-radius: 6px;
    background: #fff;
    color: var(--text-muted);
    font-weight: 600;
    font-size: 0.86rem;
}
.tab-link.active {
    color: #fff;
    border-color: var(--blue);
    background: var(--blue);
}

/* ── STAT CARDS ───────────────────────────────── */
.stats-row {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(150px, 1fr));
    gap: 14px;
    margin-bottom: 18px;
}
.stat-card {
    background: #fff;
    border: 1px solid var(--border);
    border-radius: 8px;
    padding: 14px 18px;
}
.stat-lbl {
    font-size: 0.68rem;
    text-transform: uppercase;
    letter-spacing: 0.07em;
    color: var(--text-muted);
    margin-bottom: 5px;
}
.stat-val { font-size: 1.5rem; font-weight: 700; line-height: 1; }
.stat-val.red   { color: var(--red); }
.stat-val.green { color: var(--green); }
.stat-val.blue  { color: var(--blue); }
.stat-val.amber { color: var(--amber); }
.stat-sub { font-size: 0.75rem; color: var(--text-muted); margin-top: 3px; }

.chart-wrap {
    padding: 14px 16px 18px;
    min-height: 260px;
    position: relative;
}
.chart-wrap canvas {
    max-height: 320px;
}

/* ── TABLES ───────────────────────────────────── */
.table-wrap { overflow-x: auto; }
table { width: 100%; border-collapse: collapse; }
th, td { overflow-wrap: anywhere; }
thead th {
    padding: 9px 12px;
    text-align: left;
    font-size: 0.68rem;
    text-transform: uppercase;
    letter-spacing: 0.07em;
    color: var(--text-muted);
    background: #f8fafc;
    font-weight: 600;
    border-bottom: 1px solid var(--border);
    white-space: nowrap;
}
tbody td {
    padding: 9px 12px;
    border-bottom: 1px solid var(--border);
    vertical-align: middle;
}
tbody tr:last-child td { border-bottom: none; }
tbody tr:hover td { background: #f8fafc; }
tfoot td {
    padding: 9px 12px;
    font-weight: 600;
    background: #f8fafc;
    border-top: 2px solid var(--border);
}

/* ── FORMS ────────────────────────────────────── */
.form-row {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
    gap: 14px;
    align-items: end;
}
.form-row .full { grid-column: 1 / -1; }
.fg { display: flex; flex-direction: column; gap: 5px; }
.fg label { font-size: 0.78rem; font-weight: 500; color: var(--text-muted); }
input[type=text],
input[type=number],
input[type=date],
input[type=time],
input[type=password],
input[type=email],
select,
textarea {
    width: 100%;
    padding: 8px 11px;
    border: 1px solid var(--border);
    border-radius: 6px;
    font-size: 0.875rem;
    color: var(--text);
    background: #fff;
    transition: border-color .15s, box-shadow .15s;
    font-family: inherit;
}
input:focus, select:focus, textarea:focus {
    outline: none;
    border-color: var(--blue);
    box-shadow: 0 0 0 3px rgba(37,99,235,.1);
}
input[disabled], select[disabled] {
    background: #f8fafc;
    color: var(--text-muted);
    cursor: not-allowed;
}
textarea { resize: vertical; min-height: 72px; }

/* ── BUTTONS ──────────────────────────────────── */
.btn {
    display: inline-flex;
    align-items: center;
    gap: 5px;
    padding: 8px 16px;
    border-radius: 6px;
    font-size: 0.855rem;
    font-weight: 500;
    cursor: pointer;
    border: 1px solid transparent;
    transition: all .15s;
    font-family: inherit;
    white-space: nowrap;
    max-width: 100%;
}
.btn-primary  { background: var(--blue);  color: #fff; border-color: var(--blue); }
.btn-primary:hover  { background: var(--blue-hover); }
.btn-success  { background: var(--green); color: #fff; }
.btn-success:hover  { background: #15803d; }
.btn-danger   { background: var(--red);   color: #fff; }
.btn-danger:hover   { background: #b91c1c; }
.btn-secondary{ background: #fff; color: var(--text); border-color: var(--border); }
.btn-secondary:hover{ background: #f8fafc; }
.btn-amber    { background: var(--amber); color: #fff; }
.btn-sm  { padding: 5px 11px; font-size: 0.78rem; }

/* ── BADGES ───────────────────────────────────── */
.badge {
    display: inline-block;
    padding: 2px 7px;
    border-radius: 4px;
    font-size: 0.7rem;
    font-weight: 600;
    white-space: nowrap;
}
.badge-gray    { background: #f1f5f9; color: #475569; }
.badge-blue    { background: #dbeafe; color: #1d4ed8; }
.badge-green   { background: #dcfce7; color: #15803d; }
.badge-red     { background: #fee2e2; color: #b91c1c; }
.badge-amber   { background: #fef3c7; color: #b45309; }
.badge-purple  { background: #ede9fe; color: #7c3aed; }

/* causale-specific */
.badge-fattura  { background: #fee2e2; color: #b91c1c; }
.badge-bolla    { background: #fef3c7; color: #b45309; }
.badge-manuale  { background: #dbeafe; color: #1d4ed8; }
.badge-pagamento{ background: #dcfce7; color: #15803d; }
.badge-abbuono  { background: #ede9fe; color: #7c3aed; }
.badge-storno   { background: #f1f5f9; color: #475569; }

/* ── AMOUNTS ──────────────────────────────────── */
.debit  { color: var(--red);   font-weight: 600; }
.credit { color: var(--green); font-weight: 600; }

/* ── ALERTS ───────────────────────────────────── */
.alert {
    padding: 11px 14px;
    border-radius: 6px;
    margin-bottom: 14px;
    font-size: 0.855rem;
}
.alert-ok   { background: #dcfce7; color: #15803d; border: 1px solid #bbf7d0; }
.alert-err  { background: #fee2e2; color: #b91c1c; border: 1px solid #fecaca; }
.alert-info { background: #dbeafe; color: #1d4ed8; border: 1px solid #bfdbfe; }

/* ── EMPTY STATE ──────────────────────────────── */
.empty {
    text-align: center;
    padding: 44px 20px;
    color: var(--text-muted);
}
.empty .ei { font-size: 2.2rem; margin-bottom: 10px; }
.empty p { font-size: 0.88rem; }

/* ── DASHBOARD GRID ───────────────────────────── */
.dash-grid {
    display: grid;
    grid-template-columns: repeat(auto-fill, minmax(130px, 1fr));
    gap: 10px;
}
.dash-btn {
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    gap: 7px;
    padding: 18px 12px;
    background: #fff;
    border: 1px solid var(--border);
    border-radius: 8px;
    color: var(--text);
    font-size: 0.82rem;
    font-weight: 500;
    text-align: center;
    transition: all .15s;
    min-height: 82px;
}
.dash-btn:hover { border-color: var(--blue); color: var(--blue); box-shadow: 0 2px 8px rgba(37,99,235,.1); }
.dash-btn .di { font-size: 1.4rem; }

/* ── FILTER BAR ───────────────────────────────── */
.filter-bar {
    display: flex;
    gap: 10px;
    flex-wrap: wrap;
    align-items: flex-end;
}
.filter-bar .fg { min-width: 150px; }

/* ── LOGIN PAGE ───────────────────────────────── */
body.login-page {
    display: flex;
    align-items: center;
    justify-content: center;
    background: var(--primary);
}
.login-box {
    background: #fff;
    border-radius: 12px;
    padding: 36px 40px;
    width: 100%;
    max-width: 380px;
    box-shadow: 0 20px 60px rgba(0,0,0,0.3);
}
.login-box .logo-wrap { text-align: center; margin-bottom: 24px; }
.login-box .logo-wrap img { height: 52px; object-fit: contain; }
.login-box h2 { font-size: 1.1rem; font-weight: 600; margin-bottom: 20px; text-align: center; color: var(--text); }

/* ── UTILS ────────────────────────────────────── */
.flex        { display: flex; }
.flex-center { display: flex; align-items: center; }
.flex-bw     { display: flex; align-items: center; justify-content: space-between; }
.gap-2       { gap: 8px; }
.gap-3       { gap: 10px; }
.mt-3        { margin-top: 10px; }
.mt-4        { margin-top: 16px; }
.mt-6        { margin-top: 22px; }
.mb-4        { margin-bottom: 16px; }
.text-right  { text-align: right; }
.text-center { text-align: center; }
.font-bold   { font-weight: 700; }
.fw6         { font-weight: 600; }
.text-sm     { font-size: 0.82rem; }
.text-xs     { font-size: 0.72rem; }
.text-muted  { color: var(--text-muted); }
.text-red    { color: var(--red); }
.text-green  { color: var(--green); }
.w-full      { width: 100%; }

/* ── HAMBURGER ────────────────────────────────── */
.hamburger {
    display: none;
    flex-direction: column;
    justify-content: center;
    gap: 5px;
    width: 36px;
    height: 36px;
    background: none;
    border: none;
    cursor: pointer;
    padding: 4px;
    flex-shrink: 0;
}
.hamburger span {
    display: block;
    height: 2px;
    background: var(--text);
    border-radius: 2px;
    transition: all 0.2s;
}

.sidebar-overlay {
    display: none;
    position: fixed;
    inset: 0;
    background: rgba(0,0,0,0.45);
    z-index: 99;
}
.sidebar-overlay.active { display: block; }

/* ── RESPONSIVE ───────────────────────────────── */
@media (max-width: 768px) {
    body {
        display: block;
        font-size: 12.5px;
        width: 100%;
    }
    body.is-menu-open { overflow: hidden; }
    .hamburger { display: flex; }
    .sidebar {
        width: min(82vw, 280px);
        transform: translateX(-100%);
        transition: transform 0.25s ease;
        z-index: 200;
        box-shadow: 12px 0 30px rgba(15,23,42,.28);
    }
    .sidebar.open { transform: translateX(0); }
    .sidebar-logo { padding: 12px 14px; }
    .sidebar-logo img { width: 96px; height: 38px; }
    .nav-section { font-size: 0.58rem; padding: 9px 14px 3px; }
    .nav-link { font-size: 0.82rem; padding: 8px 14px; gap: 8px; }
    .nav-link .ni { width: 18px; font-size: 1rem; }
    .main-wrap { margin-left: 0; }
    .topbar {
        min-height: 54px;
        padding: 9px 10px;
        gap: 8px;
        align-items: center;
    }
    .topbar h1 {
        min-width: 0;
        flex: 1;
        font-size: 0.92rem;
        line-height: 1.15;
        overflow-wrap: anywhere;
    }
    .topbar .user-chip {
        gap: 4px;
        flex: 0 1 auto;
        max-width: 42vw;
        justify-content: flex-end;
        font-size: 0.68rem;
        line-height: 1.1;
        text-align: right;
        flex-wrap: wrap;
    }
    .topbar .user-chip span {
        max-width: 100%;
        overflow-wrap: anywhere;
    }
    .page { padding: 10px; width: 100%; }
    .card {
        width: 100%;
        border-radius: 7px;
    }
    .card-header {
        padding: 10px 11px;
        gap: 6px;
        flex-wrap: wrap;
        align-items: flex-start;
    }
    .card-title { font-size: 0.84rem; line-height: 1.2; }
    .card-body { padding: 11px; }
    .tabs {
        display: grid;
        grid-template-columns: 1fr;
        gap: 7px;
        margin-bottom: 10px;
    }
    .tab-link {
        justify-content: center;
        width: 100%;
        font-size: 0.8rem;
        padding: 8px 10px;
    }
    .stats-row {
        grid-template-columns: 1fr;
        gap: 9px;
        margin-bottom: 12px;
    }
    .stat-card { padding: 10px 11px; }
    .stat-lbl { font-size: 0.58rem; line-height: 1.2; }
    .stat-val {
        font-size: clamp(1.05rem, 7vw, 1.32rem);
        line-height: 1.08;
        overflow-wrap: anywhere;
    }
    .stat-sub { font-size: 0.68rem; }
    .chart-wrap {
        padding: 10px;
        min-height: 230px;
    }
    .chart-wrap canvas { max-height: 280px; }
    .form-row {
        display: grid;
        grid-template-columns: 1fr !important;
        gap: 10px;
        max-width: none !important;
    }
    .fg { min-width: 0 !important; }
    .fg label { font-size: 0.72rem; }
    input[type=text],
    input[type=number],
    input[type=date],
    input[type=time],
    input[type=password],
    input[type=email],
    select,
    textarea {
        min-width: 0;
        font-size: 0.82rem;
        padding: 8px 9px;
    }
    .btn {
        justify-content: center;
        white-space: normal;
        text-align: center;
        font-size: 0.78rem;
        line-height: 1.15;
        padding: 7px 10px;
    }
    .btn-sm {
        font-size: 0.72rem;
        padding: 6px 8px;
    }
    .flex {
        flex-wrap: wrap;
        min-width: 0;
    }
    .table-wrap {
        overflow-x: visible;
        width: 100%;
    }
    table,
    thead,
    tbody,
    tfoot,
    tr,
    th,
    td {
        display: block;
        width: 100%;
    }
    thead {
        position: absolute;
        width: 1px;
        height: 1px;
        overflow: hidden;
        clip: rect(0 0 0 0);
    }
    tbody tr,
    tfoot tr {
        border-bottom: 1px solid var(--border);
        padding: 8px 10px;
        background: #fff;
    }
    tbody tr:hover td { background: transparent; }
    tbody td,
    tfoot td {
        border: 0;
        padding: 4px 0;
        min-width: 0;
        font-size: 0.78rem;
        line-height: 1.25;
        text-align: left;
    }
    tfoot td {
        background: transparent;
        border-top: 0;
    }
    td form,
    td .flex,
    td div[style*="display:flex"],
    .card-body div[style*="display:flex"] {
        display: flex !important;
        flex-wrap: wrap !important;
        width: 100%;
        min-width: 0;
    }
    td input[style],
    td select[style] {
        width: 100% !important;
        max-width: 100%;
    }
    .badge {
        max-width: 100%;
        white-space: normal;
        font-size: 0.66rem;
        line-height: 1.15;
    }
    .dash-grid {
        grid-template-columns: repeat(2, minmax(0, 1fr));
        gap: 8px;
    }
    .grid-2 {
        grid-template-columns: 1fr !important;
    }
    .dash-btn {
        min-height: 74px;
        padding: 12px 8px;
        font-size: 0.74rem;
        line-height: 1.15;
    }
    .login-box {
        max-width: calc(100vw - 22px);
        padding: 26px 20px;
    }
}

@media (max-width: 420px) {
    .topbar .user-chip { max-width: 36vw; }
    .dash-grid { grid-template-columns: 1fr; }
    .empty { padding: 30px 14px; }
}
