* {
    box-sizing: border-box;
    -webkit-tap-highlight-color: transparent;
}
html { -webkit-text-size-adjust: 100%; }
body {
    font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
    background: #f4f6f8;
    color: #222;
    margin: 0;
    padding: 0;
    font-size: 16px;
    line-height: 1.4;
    /* Evita rubber-band oltre la bottom tab bar su iOS. */
    overscroll-behavior-y: contain;
}

/* ---------- top bar ---------- */
.top-bar {
    background: #2c3e50;
    color: #fff;
}
.top-inner {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: .65rem 1rem;
    gap: 1rem;
}
.top-inner h1 { margin: 0; font-size: 1.05rem; font-weight: 700; letter-spacing: .01em; }
.top-user { display: flex; align-items: center; gap: .75rem; }
.top-user .utente { color: #cbd5e0; font-size: .9rem; }
.top-user .utente .badge-admin { margin-left: .3rem; }

.logout-btn {
    color: #cbd5e0;
    width: 40px; height: 40px;
    display: inline-flex;
    align-items: center; justify-content: center;
    border-radius: 4px;
    text-decoration: none;
}
.logout-btn:hover, .logout-btn:focus { color: #fff; background: #c0392b; outline: none; }

/* Tabs in top bar (desktop) */
.top-tabs {
    display: flex;
    gap: .25rem;
    padding: 0 .5rem;
    border-top: 1px solid #34495e;
}
.top-tabs a {
    color: #cbd5e0;
    text-decoration: none;
    padding: .7rem 1rem;
    min-height: 44px;
    display: inline-flex;
    align-items: center;
    font-size: .95rem;
    border-bottom: 3px solid transparent;
    margin-bottom: -1px;
}
.top-tabs a:hover, .top-tabs a:focus { color: #fff; outline: none; }
.top-tabs a.attivo { color: #fff; border-bottom-color: #2c7be5; }

/* Bottom tab bar (mobile) */
.bottom-tabs {
    display: none;
    position: fixed;
    left: 0; right: 0; bottom: 0;
    background: #fff;
    border-top: 1px solid #e1e5ea;
    padding-bottom: env(safe-area-inset-bottom, 0);
    z-index: 100;
    box-shadow: 0 -2px 10px rgba(0,0,0,0.04);
}
.bottom-tabs a {
    flex: 1;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    padding: .5rem .25rem;
    gap: .2rem;
    color: #888;
    text-decoration: none;
    font-size: .7rem;
    font-weight: 500;
    min-height: 56px;
    letter-spacing: .01em;
}
.bottom-tabs a svg { display: block; }
.bottom-tabs a.attivo { color: #2c7be5; }
.bottom-tabs a:active { background: #f4f6f8; }

/* Switch mobile / desktop */
@media (max-width: 640px) {
    .top-tabs { display: none; }
    .bottom-tabs { display: flex; }
    body { padding-bottom: calc(64px + env(safe-area-inset-bottom, 0)); }
    .top-inner h1 { font-size: 1rem; }
    .top-user .utente { font-size: .85rem; }
}

/* ---------- layout ---------- */
main { max-width: 900px; margin: 1rem auto; padding: 0 1rem; }
.card {
    background: #fff;
    border-radius: 8px;
    padding: 1rem 1.25rem;
    margin-bottom: 1rem;
    box-shadow: 0 1px 3px rgba(0,0,0,0.08);
}
.card h2 { margin: 0 0 .75rem; font-size: 1.05rem; }
.card h3 { margin: 1.25rem 0 .5rem; font-size: 1rem; color: #555; }

.barra {
    display: flex;
    justify-content: space-between;
    align-items: center;
    flex-wrap: wrap;
    gap: .75rem;
    margin-bottom: .5rem;
}

/* ---------- form ---------- */
.form-spesa {
    display: grid;
    grid-template-columns: 1fr;
    gap: .75rem;
    align-items: end;
}
.form-spesa label {
    display: flex;
    flex-direction: column;
    font-size: .85rem;
    color: #555;
    gap: .3rem;
}
.form-spesa input,
.form-spesa select,
.filtro select,
.login-card input {
    padding: .7rem;
    border: 1px solid #ccc;
    border-radius: 4px;
    font-size: 1rem;
    background: #fff;
    color: #222;
    min-height: 44px;
    width: 100%;
}
.form-spesa input:focus,
.form-spesa select:focus,
.filtro select:focus,
.login-card input:focus {
    outline: 2px solid #2c7be5;
    outline-offset: 1px;
    border-color: #2c7be5;
}

button {
    background: #2c7be5;
    color: #fff;
    border: 0;
    padding: .75rem 1rem;
    border-radius: 4px;
    cursor: pointer;
    font-size: 1rem;
    min-height: 44px;
    font-weight: 600;
}
button:hover, button:focus { background: #1a5fc4; outline: none; }
button:focus-visible { outline: 2px solid #0b3d8a; outline-offset: 2px; }
button.link {
    background: transparent;
    color: #c0392b;
    padding: .5rem .25rem;
    font-size: .85rem;
    text-decoration: underline;
    min-height: 44px;
    font-weight: 500;
}
button.link:hover, button.link:focus { background: transparent; color: #922b21; }

.tipo-switch {
    border: 1px solid #e1e5ea;
    border-radius: 6px;
    padding: .75rem;
    margin: 0;
    display: flex;
    gap: .75rem;
    flex-wrap: wrap;
}
.tipo-switch legend { padding: 0 .3rem; font-size: .8rem; color: #777; }
.tipo-switch .radio {
    flex-direction: row;
    align-items: center;
    gap: .45rem;
    margin: 0;
    font-size: .95rem;
    cursor: pointer;
    min-height: 44px;
    padding: 0 .25rem;
}
.tipo-switch .radio input { margin: 0; width: 1.1rem; height: 1.1rem; }

/* Desktop: grid 2 colonne */
@media (min-width: 560px) {
    .form-spesa { grid-template-columns: repeat(2, 1fr); }
    .form-spesa label.wide,
    .form-spesa fieldset.wide { grid-column: span 2; }
}
@media (min-width: 780px) {
    .form-spesa { grid-template-columns: repeat(4, 1fr); }
    .form-spesa label.wide { grid-column: span 2; }
    .form-spesa fieldset.wide { grid-column: 1 / -1; }
}

/* ---------- tabelle responsive ---------- */
table { width: 100%; border-collapse: collapse; margin-top: .75rem; font-size: .95rem; }
th, td { padding: .6rem .4rem; text-align: left; border-bottom: 1px solid #eee; }
th { background: #f9fafc; font-weight: 600; font-size: .8rem; color: #555; text-transform: uppercase; letter-spacing: .03em; }
td.num, th.num { text-align: right; font-variant-numeric: tabular-nums; white-space: nowrap; }
tfoot th { background: #f4f6f8; text-transform: none; font-size: .9rem; }

.pos { color: #1e8449; font-weight: 600; }
.neg { color: #c0392b; font-weight: 600; }

/* Mobile: righe tabella come "card". Richiede data-label su ogni td. */
@media (max-width: 640px) {
    table, thead, tbody, tfoot, tr, th, td { display: block; }
    thead tr { position: absolute; left: -9999px; }
    tr {
        background: #fff;
        border: 1px solid #eef1f4;
        border-radius: 6px;
        margin-bottom: .6rem;
        padding: .5rem .75rem;
    }
    td {
        border: 0;
        padding: .35rem 0;
        display: flex;
        justify-content: space-between;
        align-items: baseline;
        gap: 1rem;
    }
    td:before {
        content: attr(data-label);
        font-weight: 600;
        font-size: .75rem;
        text-transform: uppercase;
        letter-spacing: .04em;
        color: #777;
        flex-shrink: 0;
    }
    td.num, td.num.pos, td.num.neg { text-align: right; }
    td[data-label=""]:before { display: none; }
    tfoot tr { background: #f4f6f8; }
    tfoot th {
        display: flex;
        justify-content: space-between;
        padding: .5rem 0;
        background: transparent;
        border: 0;
    }
    tfoot th[colspan]:before {
        content: "";
    }
    tfoot th.num:before { content: "Totale"; font-weight: 700; }
}

/* ---------- riepilogo ---------- */
.totali {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(140px, 1fr));
    gap: .6rem;
    margin: .75rem 0;
}
.totali > div {
    background: #f9fafc;
    border: 1px solid #eef1f4;
    border-radius: 6px;
    padding: .65rem .8rem;
}
.totali span { display: block; color: #777; font-size: .75rem; text-transform: uppercase; letter-spacing: .04em; }
.totali strong { font-size: 1.15rem; display: block; margin-top: .2rem; font-variant-numeric: tabular-nums; }

.trasferimenti { list-style: none; padding: 0; margin: .5rem 0 0; }
.trasferimenti li {
    background: #eef6ff;
    border-left: 4px solid #2c7be5;
    padding: .7rem .9rem;
    margin-bottom: .5rem;
    border-radius: 4px;
}

/* ---------- badge ---------- */
.badge {
    display: inline-block;
    padding: .2rem .55rem;
    border-radius: 10px;
    font-size: .72rem;
    font-weight: 700;
    text-transform: uppercase;
    letter-spacing: .03em;
    white-space: nowrap;
}
.badge-spesa { background: #eaf2fb; color: #1a5fc4; }
.badge-incasso { background: #e9f7ef; color: #1e8449; }
.badge-versamento { background: #fdf3e3; color: #9a6a12; }
.badge-saldato { background: #e9f7ef; color: #1e8449; }
.badge-pendente { background: #fdecea; color: #922b21; }
.badge-admin { background: #34495e; color: #fff; margin-left: .3rem; font-size: .65rem; }

.btn-ripristina {
    background: #1e8449; color: #fff; border: 0; padding: .4rem .75rem;
    border-radius: 4px; cursor: pointer; font-size: .85rem; min-height: 36px; margin-right: .4rem;
}
.btn-ripristina:hover, .btn-ripristina:focus { background: #166b3a; outline: none; }
.azioni-cestino { white-space: nowrap; }
.ok-msg { background: #e9f7ef; color: #1e8449; padding: .6rem .9rem; border-radius: 4px; margin: 0 0 .75rem; }

.btn-primario {
    display: inline-flex;
    align-items: center;
    background: #2c7be5;
    color: #fff;
    padding: .55rem 1rem;
    border-radius: 4px;
    text-decoration: none;
    font-weight: 600;
    font-size: .95rem;
    min-height: 40px;
    white-space: nowrap;
}
.btn-primario:hover, .btn-primario:focus { background: #1a5fc4; color: #fff; outline: none; }

.proposte-saldo {
    background: #eef6ff;
    border: 1px solid #cfe1fb;
    border-radius: 6px;
    padding: .6rem .75rem;
}
.proposte-titolo { font-size: .8rem; color: #555; margin-bottom: .4rem; }
.proposte-chips { display: flex; flex-wrap: wrap; gap: .4rem; }
.chip-proposta {
    background: #fff;
    border: 1px solid #cfe1fb;
    color: #1a5fc4;
    padding: .4rem .7rem;
    border-radius: 999px;
    font-size: .85rem;
    cursor: pointer;
    display: inline-flex;
    align-items: center;
    gap: .4rem;
    min-height: 36px;
}
.chip-proposta:hover, .chip-proposta:focus { background: #2c7be5; color: #fff; border-color: #2c7be5; outline: none; }
.chip-proposta.chip-attiva { background: #2c7be5; color: #fff; border-color: #2c7be5; }
.chip-proposta strong { font-variant-numeric: tabular-nums; }
.chip-proposta .anno-chip { font-size: .7rem; opacity: .75; padding: 0 .35rem; border-left: 1px solid currentColor; }

.muto { color: #999; font-weight: 400; font-size: .8rem; margin-left: .25rem; }
.link-inline { color: #2c7be5; text-decoration: none; font-size: .9rem; }
.link-inline:hover, .link-inline:focus { text-decoration: underline; }

.lista-saldi { list-style: none; padding: 0; margin: 0; font-size: .85rem; }
.lista-saldi li {
    display: flex;
    justify-content: space-between;
    gap: .75rem;
    padding: .15rem 0;
    border-bottom: 1px dashed #eef1f4;
}
.lista-saldi li:last-child { border-bottom: 0; }
.lista-saldi li.residuo { color: #922b21; }
.lista-saldi .freccia { white-space: nowrap; }
.lista-saldi .imp { font-variant-numeric: tabular-nums; white-space: nowrap; }

.selettore-anno {
    display: inline-flex;
    align-items: center;
    gap: .4rem;
    margin: 0 0 .75rem;
}
.selettore-anno .form-anno { margin: 0; }
.freccia-anno {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    width: 40px;
    height: 40px;
    border: 1px solid #ccc;
    border-radius: 4px;
    background: #fff;
    color: #2c3e50;
    font-size: 1.25rem;
    font-weight: 700;
    text-decoration: none;
    line-height: 1;
    user-select: none;
}
.freccia-anno:hover, .freccia-anno:focus {
    background: #2c7be5;
    color: #fff;
    border-color: #2c7be5;
    outline: none;
}
.freccia-anno.disabilitata {
    color: #ccc;
    background: #f4f6f8;
    border-color: #e1e5ea;
    cursor: not-allowed;
}

.sr-only {
    position: absolute;
    width: 1px; height: 1px;
    padding: 0; margin: -1px;
    overflow: hidden;
    clip: rect(0,0,0,0);
    white-space: nowrap;
    border: 0;
}

.form-anno {
    margin: 0 0 .75rem;
    max-width: 240px;
}
.form-anno label { display: flex; flex-direction: column; font-size: .8rem; color: #666; gap: .25rem; }
.form-anno select {
    padding: .55rem .6rem;
    border: 1px solid #ccc;
    border-radius: 4px;
    font-size: .95rem;
    min-height: 40px;
    background: #fff;
}

/* Filtri avanzati: solo desktop, espandibili. */
.filtri-avanzati { display: none; margin: .25rem 0 1rem; }
@media (min-width: 641px) {
    .filtri-avanzati { display: block; }
}
.filtri-avanzati summary {
    cursor: pointer;
    padding: .5rem .75rem;
    background: #f4f6f8;
    border: 1px solid #eef1f4;
    border-radius: 6px;
    font-weight: 600;
    font-size: .9rem;
    color: #555;
    user-select: none;
    list-style: none;
    display: flex;
    align-items: center;
    gap: .5rem;
}
.filtri-avanzati summary::-webkit-details-marker { display: none; }
.filtri-avanzati summary:before {
    content: "▸";
    display: inline-block;
    transition: transform .15s;
    color: #888;
}
.filtri-avanzati[open] summary:before { transform: rotate(90deg); }
.filtri-avanzati summary:hover, .filtri-avanzati summary:focus { background: #eef1f4; outline: none; }
.badge-attivi { background: #2c7be5; color: #fff; }

.form-filtri {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: .6rem .75rem;
    margin: .5rem 0 0;
    padding: .75rem;
    background: #f9fafc;
    border: 1px solid #eef1f4;
    border-top: 0;
    border-radius: 0 0 6px 6px;
}
.form-filtri label { display: flex; flex-direction: column; font-size: .8rem; color: #666; gap: .25rem; }
.form-filtri input, .form-filtri select {
    padding: .55rem .6rem;
    border: 1px solid #ccc;
    border-radius: 4px;
    font-size: .95rem;
    min-height: 40px;
    background: #fff;
}
.form-filtri .wide-filter { grid-column: 1 / -1; }
.form-filtri .azioni-filtri {
    grid-column: 1 / -1;
    display: flex;
    gap: .75rem;
    align-items: center;
    flex-wrap: wrap;
}
.form-filtri .azioni-filtri button { padding: .55rem 1rem; font-size: .95rem; min-height: 40px; }
@media (min-width: 780px) {
    .form-filtri { grid-template-columns: repeat(5, 1fr); }
    .form-filtri .wide-filter { grid-column: span 2; }
    .form-filtri .azioni-filtri { grid-column: 1 / -1; }
}

th .sort {
    color: inherit;
    text-decoration: none;
    display: inline-flex;
    align-items: center;
    gap: .2rem;
}
th .sort:hover, th .sort:focus { color: #2c7be5; }

/* Vista movimenti raggruppati per persona (solo mobile). */
.vista-gruppi { display: none; }
@media (max-width: 640px) {
    .vista-gruppi { display: block; }
    .tabella-mov { display: none; }
}
.gruppo-persona {
    margin: 0 0 1rem;
    border: 1px solid #eef1f4;
    border-radius: 8px;
    overflow: hidden;
    background: #fff;
}
.gruppo-persona .testa-persona {
    display: flex;
    justify-content: space-between;
    align-items: baseline;
    padding: .55rem .85rem;
    background: #f4f6f8;
    border-bottom: 1px solid #eef1f4;
}
.gruppo-persona .testa-persona h3 { margin: 0; font-size: .95rem; font-weight: 700; }
.gruppo-persona .testa-persona .sub { font-size: .75rem; color: #888; font-variant-numeric: tabular-nums; }
.gruppo-persona .lista-mov { list-style: none; margin: 0; padding: 0; }
.gruppo-persona .lista-mov li {
    padding: .65rem .85rem;
    border-bottom: 1px solid #f4f6f8;
}
.gruppo-persona .lista-mov li:last-child { border-bottom: 0; }
.gruppo-persona .riga-top {
    display: flex;
    justify-content: space-between;
    align-items: baseline;
    gap: .75rem;
}
.gruppo-persona .riga-top .desc { font-weight: 600; font-size: .95rem; }
.gruppo-persona .riga-top .imp { font-weight: 700; font-size: 1rem; font-variant-numeric: tabular-nums; white-space: nowrap; }
.gruppo-persona .riga-meta {
    display: flex;
    align-items: center;
    gap: .5rem;
    margin-top: .25rem;
    font-size: .78rem;
    color: #777;
    flex-wrap: wrap;
}
.gruppo-persona .riga-meta .meta-info { color: #888; }
.gruppo-persona .riga-meta .azione-icona { margin-left: auto; }
.btn-icona {
    background: transparent;
    border: 0;
    padding: .4rem;
    color: #c0392b;
    border-radius: 4px;
    min-height: 32px;
    min-width: 32px;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    cursor: pointer;
}
.btn-icona:hover, .btn-icona:focus { background: #fdecea; outline: none; }

.saldo-mobile {
    background: #f4f6f8;
    border: 1px solid #eef1f4;
    border-radius: 6px;
    padding: .65rem .85rem;
    font-size: .9rem;
    text-align: center;
}
.saldo-mobile strong { font-size: 1.05rem; font-variant-numeric: tabular-nums; }
.riga-incasso { background: #fafdfb; }
.riga-versamento { background: #fffcf5; }

/* ---------- messaggi ---------- */
.errore { background: #fdecea; color: #922b21; padding: .7rem .9rem; border-radius: 4px; margin: 0 0 .75rem; }
.vuoto { color: #888; font-style: italic; }
.vuoto.ok { color: #1e8449; font-style: normal; font-weight: 600; }
.nota { font-size: .85rem; color: #777; font-style: italic; margin-top: .5rem; }

/* ---------- login ---------- */
.login-body {
    display: flex;
    align-items: center;
    justify-content: center;
    min-height: 100vh;        /* fallback */
    min-height: 100dvh;       /* iOS Safari corretto */
    margin: 0;
    padding: 1rem;
}
.login-main { width: 100%; max-width: 400px; }
.login-card h1 { margin-top: 0; font-size: 1.25rem; }
.login-card .sottotitolo { color: #666; margin-top: -.25rem; font-size: .95rem; }
.login-card form { display: flex; flex-direction: column; gap: .8rem; margin-top: 1rem; }
.login-card label { display: flex; flex-direction: column; font-size: .85rem; color: #555; gap: .3rem; }
.login-card select, .login-card input {
    padding: .7rem;
    border: 1px solid #ccc;
    border-radius: 4px;
    font-size: 1rem;
    min-height: 44px;
}

/* ---------- a11y helpers ---------- */
:focus-visible { outline: 2px solid #2c7be5; outline-offset: 2px; }
@media (prefers-reduced-motion: reduce) {
    * { animation-duration: 0.01ms !important; transition-duration: 0.01ms !important; }
}
