:root {
    --page: #f3f6f9;
    --surface: #ffffff;
    --tile: #f6f9fc;
    --text: #07152d;
    --muted: #586b85;
    --line: #d9e3ee;
    --blue: #1f73ea;
    --green: #10b981;
    --red: #ef4444;
    --shadow: 0 8px 18px rgba(15, 27, 45, 0.08);
}

* { box-sizing: border-box; }

body {
    margin: 0;
    background: var(--page);
    color: var(--text);
    font-family: Inter, Arial, sans-serif;
}

button, input, select { font: inherit; }

button {
    min-height: 40px;
    border: 0;
    border-radius: 8px;
    background: var(--blue);
    color: #fff;
    font-weight: 800;
    cursor: pointer;
}

button:disabled {
    background: #cbd5e1;
    cursor: not-allowed;
    opacity: 0.6;
}

.login-view {
    min-height: 100vh;
    display: grid;
    place-items: center;
    padding: 20px;
}

.login-card {
    width: min(390px, 100%);
    display: grid;
    gap: 12px;
    padding: 26px;
    border: 1px solid var(--line);
    border-radius: 12px;
    background: var(--surface);
    box-shadow: var(--shadow);
}

.login-card h1 { margin: 0; }
.login-card p { margin: 0; font-size: 13px; }
.demo-hint { color: var(--muted); font-weight: 800; }
#login-error { color: var(--red); font-weight: 800; }
.eyebrow { color: var(--blue); font-size: 12px; font-weight: 900; text-transform: uppercase; }

input, select {
    width: 100%;
    min-height: 44px;
    border: 1px solid #cbd7e5;
    border-radius: 8px;
    background: #fff;
    color: var(--text);
    padding: 0 14px;
}

.topbar {
    position: sticky;
    top: 0;
    z-index: 5;
    height: 70px;
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 0 24px;
    border-bottom: 1px solid var(--line);
    background: #fff;
}

.brand-line {
    display: flex;
    align-items: center;
    gap: 9px;
    color: var(--blue);
    font-size: 20px;
}

.brand-icon, .page-icon {
    display: grid;
    place-items: center;
    width: 29px;
    height: 29px;
    border: 2px solid #111827;
    color: var(--blue);
    font-size: 19px;
}

.top-actions {
    display: flex;
    align-items: center;
    gap: 10px;
}

.top-actions span, .ghost-button {
    min-height: 42px;
    display: inline-flex;
    align-items: center;
    padding: 0 14px;
    border: 1px solid var(--line);
    border-radius: 10px;
    background: #f8fafc;
    color: var(--text);
    font-size: 13px;
    font-weight: 800;
}

.top-actions span.ok { color: var(--green); }
.top-actions span.error { color: var(--red); }

.app-layout {
    display: grid;
    grid-template-columns: 260px minmax(0, 1fr);
}

.sidebar {
    position: sticky;
    top: 70px;
    height: calc(100vh - 70px);
    padding: 24px 16px;
    border-right: 1px solid var(--line);
    background: #fff;
}

.month-box {
    min-height: 48px;
    display: grid;
    grid-template-columns: minmax(0, 1fr) 64px;
    align-items: center;
    gap: 8px;
    padding: 6px;
    border: 1px solid #cbd7e5;
    border-radius: 8px;
    font-size: 14px;
}

.month-box input {
    min-height: 36px;
    border: 0;
    padding: 0 8px;
}

.month-box button {
    min-height: 36px;
    border-radius: 7px;
    font-size: 12px;
}

.month-label {
    display: block;
    margin-top: 8px;
    color: var(--muted);
    font-size: 13px;
    font-weight: 800;
}

.side-nav {
    display: grid;
    gap: 10px;
    margin-top: 24px;
    padding-top: 24px;
    border-top: 1px solid var(--line);
}

.side-nav button {
    min-height: 48px;
    display: flex;
    align-items: center;
    gap: 8px;
    padding: 0 18px;
    background: transparent;
    color: var(--muted);
    text-align: left;
}

.side-nav button.active {
    background: var(--blue);
    color: #fff;
    box-shadow: 0 8px 18px rgba(31, 115, 234, 0.24);
}

.content {
    width: min(1400px, 100%);
    padding: 36px 6vw 72px;
}

.page-title {
    display: flex;
    align-items: center;
    gap: 12px;
    margin-bottom: 32px;
    flex-wrap: wrap;
}

.page-title h1 {
    margin: 0;
    font-size: 30px;
    line-height: 1;
}

.month-state {
    display: flex;
    align-items: center;
    gap: 8px;
    margin-left: auto;
    flex-wrap: wrap;
}

.state-badge {
    min-height: 32px;
    display: inline-flex;
    align-items: center;
    padding: 0 10px;
    border-radius: 999px;
    font-size: 12px;
    font-weight: 900;
}

.state-badge.open {
    border: 1px solid #bbf7d0;
    background: #ecfdf5;
    color: var(--green);
}

.state-badge.closed {
    border: 1px solid #fecaca;
    background: #fef2f2;
    color: var(--red);
}

.month-state button {
    min-height: 32px;
    padding: 0 10px;
    font-size: 12px;
}

.module {
    margin-bottom: 30px;
    padding: 16px;
    border: 1px solid var(--line);
    border-radius: 8px;
    background: var(--surface);
    box-shadow: var(--shadow);
}

.module-title {
    display: flex;
    align-items: baseline;
    justify-content: space-between;
    gap: 12px;
    margin-bottom: 16px;
}

.module-title h2 {
    width: max-content;
    max-width: 100%;
    margin: 0;
    padding-bottom: 8px;
    border-bottom: 2px solid var(--blue);
    font-size: 17px;
}

.kpi-grid {
    display: grid;
    gap: 24px;
    margin-bottom: 20px;
}

.kpi-grid.cols-4 { grid-template-columns: repeat(4, minmax(0, 1fr)); }
.kpi-grid.cols-3 { grid-template-columns: repeat(3, minmax(0, 1fr)); }
.kpi-grid.cols-2 { grid-template-columns: repeat(2, minmax(0, 1fr)); }
.compact-kpis .kpi-card { min-height: 68px; }

.kpi-card {
    min-height: 118px;
    display: grid;
    align-content: center;
    justify-items: center;
    gap: 12px;
    padding: 18px;
    border: 1px solid var(--line);
    border-radius: 15px;
    background: var(--tile);
    text-align: center;
}

.kpi-card.selected {
    border: 2px solid var(--blue);
}

.kpi-card span {
    color: var(--muted);
    font-size: 12px;
    font-weight: 900;
    text-transform: uppercase;
}

.kpi-card strong {
    font-size: 29px;
    line-height: 1;
}

.kpi-card.positive strong { color: var(--green); }
.kpi-card.negative strong { color: var(--red); }
.kpi-card small { color: var(--muted); font-size: 12px; }

.total-strip {
    min-height: 67px;
    display: grid;
    place-items: center;
    margin-bottom: 20px;
    border: 1px solid var(--line);
    border-radius: 7px;
    background: #fff;
}

.total-strip span {
    color: var(--muted);
    font-size: 10px;
    text-transform: uppercase;
}

.total-strip strong {
    font-size: 21px;
}

.account-grid {
    display: grid;
    grid-template-columns: repeat(3, minmax(0, 1fr));
    gap: 20px;
    align-items: center;
}

label {
    display: grid;
    gap: 8px;
}

.row-form {
    display: grid;
    grid-template-columns: repeat(3, minmax(0, 1fr)) 150px 140px;
    gap: 16px;
    margin-bottom: 24px;
    padding: 16px;
    border: 1px solid var(--line);
    border-radius: 15px;
    background: var(--tile);
}

.check-field {
    min-height: 44px;
    display: flex;
    align-items: center;
    gap: 8px;
    padding: 0 12px;
    border: 1px solid #cbd7e5;
    border-radius: 8px;
    background: #fff;
    color: var(--muted);
    font-size: 13px;
    font-weight: 900;
}

.check-field input {
    width: 16px;
    min-height: 16px;
    padding: 0;
}

.soci-form {
    grid-template-columns: 1fr 1fr 1fr 1fr 1fr 140px;
}

.table-list {
    overflow: hidden;
    border: 1px solid var(--line);
    border-radius: 10px;
}

.table-head, .table-row {
    display: grid;
    grid-template-columns: 1fr 2fr 1fr 80px;
    align-items: center;
    min-height: 76px;
    padding: 0 20px;
    border-bottom: 1px solid var(--line);
}

.table-head {
    min-height: 50px;
    background: var(--tile);
    color: var(--muted);
    font-size: 12px;
    font-weight: 900;
    text-transform: uppercase;
}

.table-row:last-child {
    border-bottom: 0;
}

.table-row button {
    min-width: 58px;
    background: #eeeeef;
    color: #d51f00;
    font-size: 24px;
}

.row-actions {
    display: flex;
    align-items: center;
    justify-content: flex-end;
    gap: 8px;
    flex-wrap: wrap;
}

.row-actions button {
    min-width: auto;
    min-height: 34px;
    padding: 0 10px;
    background: var(--blue);
    color: #fff;
    font-size: 12px;
}

.row-actions button:last-child {
    min-width: 42px;
    background: #eeeeef;
    color: #d51f00;
    font-size: 22px;
}

.row-badge {
    display: inline-flex;
    align-items: center;
    min-height: 22px;
    margin-left: 8px;
    padding: 0 7px;
    border: 1px solid #bfdbfe;
    border-radius: 999px;
    background: #eff6ff;
    color: var(--blue);
    font-size: 11px;
    font-weight: 900;
}

.edit-row input,
.edit-row select {
    min-height: 38px;
}

.soci-head, .soci-row {
    grid-template-columns: 1fr 1fr 1fr 2fr 1fr 80px;
}

.pipeline-head, .pipeline-row {
    grid-template-columns: 2fr 1fr 1fr 1fr 2fr;
}

.pipeline-row button {
    min-width: auto;
    min-height: 26px;
    padding: 0 10px;
    border-radius: 9px;
    background: var(--blue);
    color: #fff;
    font-size: 12px;
}

.pipeline-row button + button {
    background: transparent;
    color: #d51f00;
    font-size: 24px;
}

.tag {
    display: inline-flex;
    align-items: center;
    width: max-content;
    min-height: 25px;
    padding: 0 8px;
    border-radius: 6px;
    color: #fff;
    font-size: 12px;
    font-weight: 900;
}

.status-incassato, .status-pagata { background: var(--green); }
.status-parziale, .status-programmata { background: #a05a00; }
.status-da_incassare { background: var(--red); }

.note-list {
    display: grid;
    gap: 12px;
}

.note-row {
    display: grid;
    grid-template-columns: 160px minmax(0, 1fr) 90px 60px;
    align-items: center;
    gap: 12px;
    min-height: 64px;
    padding: 0 16px;
    border: 1px solid var(--line);
    border-radius: 10px;
    background: var(--tile);
}

.note-row a {
    color: var(--blue);
    font-weight: 800;
    text-decoration: none;
    margin-left: 8px;
}

.note-row small {
    color: var(--muted);
}

.note-row button {
    background: transparent;
    color: #d51f00;
    font-size: 22px;
}

.empty-state {
    padding: 18px;
    border: 1px solid var(--line);
    border-radius: 10px;
    background: var(--tile);
    color: var(--muted);
    font-weight: 800;
}

.close-month {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 16px;
    padding: 16px;
    border: 1px solid var(--line);
    border-radius: 12px;
    background: var(--tile);
}

.safety-actions {
    display: flex;
    align-items: center;
    gap: 12px;
    flex-wrap: wrap;
}

.safety-actions button {
    padding: 0 14px;
}

.safety-actions span {
    color: var(--muted);
    font-size: 13px;
    font-weight: 800;
}

.save-version-box {
    display: grid;
    grid-template-columns: minmax(0, 1fr) 170px;
    gap: 12px;
    margin-bottom: 14px;
}

.version-list {
    display: grid;
    gap: 10px;
}

.version-row {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 14px;
    padding: 12px;
    border: 1px solid var(--line);
    border-radius: 8px;
    background: var(--tile);
}

.version-row strong,
.version-row small {
    display: block;
}

.version-row small {
    margin-top: 4px;
    color: var(--muted);
    font-size: 12px;
}

.legacy-import {
    display: grid;
    gap: 12px;
}

.legacy-import textarea {
    width: 100%;
    min-height: 150px;
    padding: 12px;
    border: 1px solid #cbd7e5;
    border-radius: 8px;
    resize: vertical;
    font: 13px/1.4 Consolas, monospace;
}

.close-month strong,
.close-month span {
    display: block;
}

.close-month span {
    margin-top: 4px;
    color: var(--muted);
    font-size: 13px;
}

.close-preview {
    margin-top: 14px;
}

.preview-grid {
    display: grid;
    grid-template-columns: repeat(5, minmax(0, 1fr));
    gap: 10px;
}

.preview-grid span {
    padding: 12px;
    border: 1px solid var(--line);
    border-radius: 10px;
    background: #fff;
    color: var(--muted);
    font-size: 12px;
}

.preview-grid strong {
    display: block;
    margin-top: 5px;
    color: var(--text);
    font-size: 16px;
}

[hidden] { display: none !important; }

@media (max-width: 1160px) {
    .kpi-grid.cols-4, .kpi-grid.cols-3, .kpi-grid.cols-2 {
        grid-template-columns: repeat(2, minmax(0, 1fr));
    }

    .row-form, .soci-form, .account-grid {
        grid-template-columns: 1fr 1fr;
    }

    .preview-grid {
        grid-template-columns: repeat(2, minmax(0, 1fr));
    }
}

@media (max-width: 860px) {
    .app-layout { display: block; }
    .sidebar { position: static; height: auto; }
    .side-nav { grid-template-columns: repeat(4, minmax(150px, 1fr)); overflow-x: auto; }
    .content { padding: 24px 16px 54px; }
    .topbar { position: static; }
    .sidebar { padding: 16px; }
}

@media (max-width: 640px) {
    .topbar {
        height: auto;
        align-items: flex-start;
        gap: 12px;
        flex-direction: column;
        padding: 14px;
    }

    .top-actions {
        width: 100%;
        justify-content: space-between;
    }

    .brand-line strong { font-size: 16px; }
    .kpi-grid.cols-4, .kpi-grid.cols-3, .kpi-grid.cols-2, .row-form, .soci-form, .account-grid {
        grid-template-columns: 1fr;
    }
    .table-list { overflow-x: auto; }
    .table-head, .table-row { min-width: 760px; }
    .note-row { grid-template-columns: 1fr; padding: 14px; }

    .content {
        padding: 18px 12px 42px;
    }

    .page-title {
        margin-bottom: 20px;
    }

    .page-title h1 {
        font-size: 25px;
    }

    .module {
        padding: 12px;
        margin-bottom: 18px;
    }

    .kpi-card {
        min-height: 96px;
        padding: 14px;
    }

    .kpi-card strong {
        font-size: 24px;
    }

    .close-month {
        align-items: stretch;
        flex-direction: column;
    }

    .save-version-box,
    .version-row {
        grid-template-columns: 1fr;
    }

    .version-row {
        align-items: stretch;
        flex-direction: column;
    }

    .preview-grid {
        grid-template-columns: 1fr;
    }
}
