:root {
    --bg:         #f0f4f8;
    --card:       #ffffff;
    --border:     #dde3ea;
    --text:       #1a2433;
    --muted:      #5e7087;
    --primary:    #1655a2;
    --primary-h:  #0d3d73;
    --income:     #0b6e4f;
    --income-bg:  #e6f7f1;
    --expense:    #b91c3b;
    --expense-bg: #fdedf1;
    --warn-bg:    #fef3cd;
    --danger-bg:  #fff0f3;
    --danger-brd: #f5c6d0;
    --shadow:     0 1px 3px rgba(0,0,0,.08), 0 1px 2px rgba(0,0,0,.05);
    --radius:     8px;
    --nav-h:      54px;
}

*, *::before, *::after { 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);
    font-size: 14px;
    line-height: 1.5;
}

/* NAV */
.topnav {
    position: sticky; top: 0; z-index: 100;
    height: var(--nav-h);
    background: #1a2433;
    display: flex; align-items: center;
    padding: 0 24px;
    box-shadow: 0 2px 8px rgba(0,0,0,.18);
}
.topnav-brand {
    font-size: 15px; font-weight: 700; color: #fff;
    margin-right: 32px; white-space: nowrap; letter-spacing: -.3px;
}
.topnav nav { display: flex; align-items: center; gap: 4px; flex: 1; }
.topnav nav a {
    color: #a8bdd0; text-decoration: none;
    padding: 6px 14px; border-radius: 6px;
    font-size: 14px; font-weight: 500;
    transition: background .15s, color .15s;
}
.topnav nav a:hover { background: rgba(255,255,255,.08); color: #fff; }
.topnav nav a.active { background: rgba(255,255,255,.14); color: #fff; }
.topnav nav a.logout { margin-left: auto; color: #8090a0; font-size: 13px; }
.topnav nav a.logout:hover { color: #fff; background: rgba(255,255,255,.06); }

/* PAGE */
.page {
    max-width: 1100px; margin: 0 auto;
    padding: 24px 20px 48px;
    display: flex; flex-direction: column; gap: 20px;
}

/* ALERTS */
.alert { padding: 12px 16px; border-radius: var(--radius); font-size: 14px; font-weight: 500; }
.alert-ok  { background: var(--income-bg); color: var(--income); border: 1px solid #a7f0d3; }
.alert-err { background: var(--expense-bg); color: var(--expense); border: 1px solid var(--danger-brd); }

/* LOGIN */
.login-page { background: var(--bg); display: flex; align-items: center; justify-content: center; min-height: 100vh; }
.login-card {
    background: var(--card); border-radius: 12px;
    box-shadow: var(--shadow); padding: 40px;
    width: 100%; max-width: 400px;
}
.login-card h1 { font-size: 20px; font-weight: 700; margin-bottom: 24px; color: var(--primary); }
.login-card label { display: block; font-size: 13px; font-weight: 600; margin-bottom: 5px; margin-top: 14px; color: var(--muted); }
.login-card input[type=text],
.login-card input[type=password] {
    width: 100%; padding: 9px 12px; border: 1.5px solid var(--border);
    border-radius: 6px; font-size: 14px; outline: none;
    transition: border-color .15s;
}
.login-card input:focus { border-color: var(--primary); }
.login-card button[type=submit] {
    margin-top: 20px; width: 100%;
    background: var(--primary); color: #fff; border: none;
    border-radius: 6px; padding: 11px; font-size: 15px; font-weight: 600;
    cursor: pointer; transition: background .15s;
}
.login-card button[type=submit]:hover { background: var(--primary-h); }
.login-card .alert { margin-bottom: 16px; }
.login-card p { margin-top: 16px; font-size: 13px; color: var(--muted); }
.login-card code { background: #eef; padding: 1px 5px; border-radius: 4px; font-family: monospace; }

/* PAGE HEADER */
.page-header { display: flex; align-items: center; justify-content: space-between; gap: 12px; }
.page-header h2 { font-size: 22px; font-weight: 700; }

/* CARD */
.card { background: var(--card); border-radius: var(--radius); box-shadow: var(--shadow); padding: 20px; }
.form-card { max-width: 720px; }
.form-card h3 { font-size: 16px; font-weight: 700; margin-bottom: 16px; }

/* STATS */
.stats-row { display: flex; gap: 14px; flex-wrap: wrap; }
.stat-card {
    flex: 1; min-width: 160px;
    background: var(--card); border-radius: var(--radius);
    box-shadow: var(--shadow); padding: 16px 20px;
    display: flex; flex-direction: column; gap: 4px;
}
.stat-label { font-size: 11px; font-weight: 700; color: var(--muted); text-transform: uppercase; letter-spacing: .5px; }
.stat-value { font-size: 22px; font-weight: 700; }
.stat-card.income .stat-value { color: var(--income); }
.stat-card.expense .stat-value { color: var(--expense); }

/* BUTTONS */
.btn-primary {
    background: var(--primary); color: #fff; border: none;
    border-radius: 6px; padding: 8px 16px; font-size: 14px; font-weight: 600;
    cursor: pointer; text-decoration: none; display: inline-block; transition: background .15s;
}
.btn-primary:hover { background: var(--primary-h); }
.btn-ghost {
    background: transparent; color: var(--text);
    border: 1.5px solid var(--border); border-radius: 6px;
    padding: 7px 15px; font-size: 14px; font-weight: 500;
    cursor: pointer; text-decoration: none; display: inline-block;
}
.btn-ghost:hover { background: var(--bg); }
.link-btn { background: none; border: none; cursor: pointer; font-size: 13px; color: var(--primary); padding: 0; }
.link-btn:hover { text-decoration: underline; }
.link-btn.danger { color: var(--expense); }

/* FORMS */
.form-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 14px; }
.form-group { display: flex; flex-direction: column; gap: 5px; }
.form-group label { font-size: 13px; font-weight: 600; color: var(--muted); }
.form-group input,
.form-group select {
    padding: 8px 11px; border: 1.5px solid var(--border);
    border-radius: 6px; font-size: 14px; outline: none;
    transition: border-color .15s; background: #fff;
}
.form-group input:focus,
.form-group select:focus { border-color: var(--primary); }
.form-actions { margin-top: 18px; display: flex; gap: 10px; align-items: center; }
.checkbox-label { display: flex; align-items: center; gap: 8px; margin-top: 12px; font-size: 14px; cursor: pointer; }

/* SEARCH */
.search-bar { display: flex; gap: 8px; margin-bottom: 16px; }
.search-bar input {
    flex: 1; padding: 8px 12px; border: 1.5px solid var(--border);
    border-radius: 6px; font-size: 14px; outline: none;
}
.search-bar input:focus { border-color: var(--primary); }
.search-bar button {
    background: var(--primary); color: #fff; border: none;
    border-radius: 6px; padding: 8px 16px; font-size: 14px; font-weight: 600; cursor: pointer;
}
.search-bar button:hover { background: var(--primary-h); }

/* TABLE */
.data-table { width: 100%; border-collapse: collapse; font-size: 14px; }
.data-table th {
    text-align: left; padding: 10px 12px;
    border-bottom: 2px solid var(--border);
    font-size: 11px; font-weight: 700; color: var(--muted);
    text-transform: uppercase; letter-spacing: .5px;
}
.data-table td { padding: 10px 12px; border-bottom: 1px solid var(--border); }
.data-table tr:last-child td { border-bottom: none; }
.data-table tr:hover td { background: #f7f9fc; }
.data-table .num { text-align: right; font-variant-numeric: tabular-nums; white-space: nowrap; }
.data-table .date { white-space: nowrap; color: var(--muted); font-size: 13px; }
.data-table .muted { color: var(--muted); font-size: 13px; }
.data-table .empty { color: var(--muted); text-align: center; padding: 24px; }
.data-table .row-inactive td { opacity: .45; }
.data-table .row-actions { display: flex; gap: 10px; align-items: center; white-space: nowrap; }
.income { color: var(--income); font-weight: 600; }
.expense { color: var(--expense); font-weight: 600; }

/* PAGINATION */
.pagination { display: flex; gap: 6px; margin-top: 16px; flex-wrap: wrap; }
.pagination a {
    padding: 5px 10px; border-radius: 5px;
    border: 1.5px solid var(--border);
    text-decoration: none; color: var(--text); font-size: 13px;
}
.pagination a.active { background: var(--primary); color: #fff; border-color: var(--primary); }
.pagination a:hover:not(.active) { background: var(--bg); }

/* BALANCE FORM */
.balance-form { max-width: 560px; }
.balance-form .inline-form { display: flex; gap: 10px; align-items: flex-end; flex-wrap: wrap; }
.balance-form .inline-form label { font-size: 13px; font-weight: 600; color: var(--muted); display: block; margin-bottom: 5px; }
.balance-form .inline-form input {
    padding: 8px 12px; border: 1.5px solid var(--border);
    border-radius: 6px; font-size: 15px; font-weight: 600; width: 180px; outline: none;
}
.balance-form .inline-form input:focus { border-color: var(--primary); }
.balance-form .hint { margin-top: 10px; font-size: 13px; color: var(--muted); }

/* FORECAST */
.forecast-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(320px, 1fr)); gap: 16px; }
.forecast-card {
    background: var(--card); border-radius: var(--radius);
    box-shadow: var(--shadow); overflow: hidden;
    border: 1.5px solid var(--border);
}
.forecast-card.danger { border-color: var(--danger-brd); background: var(--danger-bg); }
.forecast-month {
    padding: 12px 16px; font-size: 15px; font-weight: 700;
    background: #f7f9fc; border-bottom: 1px solid var(--border);
}
.forecast-card.danger .forecast-month { background: #ffe0e8; }
.forecast-table { width: 100%; border-collapse: collapse; font-size: 13px; }
.forecast-table td { padding: 7px 12px; border-bottom: 1px solid #f0f2f5; }
.forecast-table tr:last-child td { border-bottom: none; }
.forecast-table tr.past td { opacity: .42; }
.forecast-table .fday { color: var(--muted); font-size: 12px; width: 28px; }
.forecast-table .famt { text-align: right; font-weight: 600; white-space: nowrap; }
.forecast-table .fbal { text-align: right; font-size: 12px; color: var(--muted); white-space: nowrap; }
.forecast-table .fbal.negative { color: var(--expense); font-weight: 700; }
.forecast-table .fbal.past-bal { color: var(--muted); font-style: italic; }
.forecast-table .empty { color: var(--muted); text-align: center; padding: 16px; font-style: italic; }
.forecast-footer {
    padding: 10px 16px; font-size: 14px;
    border-top: 2px solid var(--border);
    display: flex; align-items: center; gap: 10px; flex-wrap: wrap;
}
.forecast-footer.positive strong { color: var(--income); }
.forecast-footer.negative strong { color: var(--expense); }
.warn-badge {
    background: var(--expense-bg); color: var(--expense);
    border: 1px solid var(--danger-brd);
    border-radius: 20px; padding: 2px 10px; font-size: 12px; font-weight: 600;
}

/* MISC */
.hint { font-size: 13px; color: var(--muted); line-height: 1.5; }
.negative { color: var(--expense) !important; font-weight: 700; }

/* Forecast: righe stimate (NEXI auto, ecc.) */
.forecast-row.estimated td {
    background: #fafbfc;
    border-bottom-style: dashed;
    opacity: .82;
}
.forecast-row.estimated .col-drag { color: transparent; }
.ctr-badge.badge-est {
    background: #e8edf5;
    color: var(--muted);
    font-style: italic;
    border: 1px dashed #b0bccf;
}

/* Storico mensile: header con dati anni precedenti */
.ledger-table .month-header td { vertical-align: middle; }
.month-hist-data { font-size: 11px; color: var(--muted); font-weight: 400; }

/* ── LEDGER TABLE (movimenti + previsionale) ── */
.ledger-table { width: 100%; border-collapse: collapse; font-size: 13px; }
.ledger-table th {
    padding: 9px 10px; text-align: left;
    border-bottom: 2px solid var(--border);
    font-size: 11px; font-weight: 700; color: var(--muted);
    text-transform: uppercase; letter-spacing: .4px;
    background: #f7f9fc; position: sticky; top: var(--nav-h);
}
.ledger-table td { padding: 7px 10px; border-bottom: 1px solid #f0f2f5; vertical-align: middle; }
.ledger-table tr:last-child td { border-bottom: none; }
.ledger-table tr:hover td { background: #f7fafd; }

/* Column widths */
.col-drag   { width: 24px; color: var(--muted); cursor: grab; font-size: 16px; }
.col-date   { width: 90px; white-space: nowrap; color: var(--muted); }
.col-ctr    { width: 120px; }
.col-desc   { max-width: 340px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; color: var(--muted); font-size: 12px; }
.col-num    { width: 110px; text-align: right; font-variant-numeric: tabular-nums; white-space: nowrap; font-weight: 600; }
.col-saldo  { width: 130px; text-align: right; font-variant-numeric: tabular-nums; white-space: nowrap; font-weight: 700; }
.col-note   { width: 160px; }
.col-actions{ width: 30px; text-align: center; }

/* Month header row */
.ledger-table .month-header td {
    background: #eef2f7; font-weight: 700; font-size: 13px;
    color: var(--primary); padding: 8px 10px;
    border-top: 2px solid var(--border); border-bottom: 1px solid var(--border);
}
.ledger-table .month-header .col-saldo { color: var(--text); }

/* CTR badge */
.ctr-badge {
    display: inline-block; background: #e8edf5; color: #2c4a7a;
    border-radius: 4px; padding: 2px 7px; font-size: 11px;
    font-weight: 700; letter-spacing: .3px; white-space: nowrap;
}

/* Forecast rows */
.forecast-row.past td { opacity: .45; }
.forecast-row.deferred .col-date { color: var(--warn); }
.forecast-row.dragging { opacity: .4; }
.forecast-row.drag-over td { background: #dbeafe !important; }

/* Inline inputs in forecast */
.date-edit {
    border: none; background: transparent; font-size: 13px;
    color: var(--muted); width: 110px; cursor: pointer; outline: none;
    font-family: inherit;
}
.date-edit:hover, .date-edit:focus { color: var(--primary); }
.note-edit {
    border: none; border-bottom: 1px dashed var(--border);
    background: transparent; font-size: 12px; color: var(--muted);
    width: 140px; outline: none; font-family: inherit;
}
.note-edit:focus { border-bottom-color: var(--primary); color: var(--text); }

/* Empty state */
.empty { color: var(--muted); text-align: center; padding: 28px; font-style: italic; }

@media (max-width: 640px) {
    .topnav { padding: 0 12px; }
    .topnav-brand { font-size: 13px; margin-right: 12px; }
    .topnav nav a { padding: 5px 9px; font-size: 13px; }
    .page { padding: 14px 12px 32px; }
    .form-grid { grid-template-columns: 1fr; }
    .forecast-grid { grid-template-columns: 1fr; }
    .stats-row { flex-direction: column; }
    .page-header { flex-direction: column; align-items: flex-start; }
}
