:root {
    --bg: #f4f5f7;
    --card: #ffffff;
    --ink: #1f2733;
    --muted: #6b7686;
    --line: #e3e7ee;
    --accent: #0e7490;
    --accent-soft: #e0f2f7;
    --ok: #15803d;
    --ok-soft: #dcfce7;
    --warn: #b45309;
    --warn-soft: #fef3c7;
    --bad: #b91c1c;
    --bad-soft: #fee2e2;
    --radius: 10px;
    font-size: 15px;
}

* { box-sizing: border-box; }

body {
    margin: 0;
    background: var(--bg);
    color: var(--ink);
    font-family: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
    line-height: 1.45;
    overflow-x: clip; /* aucun débordement ne doit élargir la page */
}

.topbar {
    background: var(--card);
    border-bottom: 1px solid var(--line);
    padding: 0.6rem 1.2rem 0.55rem;
    position: sticky;
    top: 0;
    z-index: 10;
}
.topbar h1 { font-size: 1.15rem; margin: 0; text-align: center; }
.topbar-row {
    display: flex;
    align-items: center;
    gap: 1rem;
    flex-wrap: wrap;
    margin-top: 0.45rem;
}
.topbar-row:has(#year-tabs:empty) { margin-top: 0; }

#year-tabs { display: flex; gap: 0.4rem; flex-wrap: wrap; flex: 1; justify-content: center; }
#year-tabs button {
    border: 1px solid var(--line);
    background: var(--bg);
    color: var(--ink);
    padding: 0.35rem 0.9rem;
    border-radius: 999px;
    cursor: pointer;
    font-size: 0.95rem;
}
#year-tabs button.active {
    background: var(--accent);
    border-color: var(--accent);
    color: #fff;
    font-weight: 600;
}
#year-tabs button.add { color: var(--accent); font-weight: 600; }

main { max-width: 1280px; margin: 0 auto; padding: 1.2rem; }
/* Vue admin (mise en page à colonne latérale) : pleine largeur */
main:has(.layout) { max-width: none; }

/* Mise en page admin : colonne latérale + contenu */
.layout {
    display: grid;
    grid-template-columns: 320px minmax(0, 1fr);
    gap: 1.2rem;
    /* pas de align-items: start — la colonne de gauche doit occuper toute la
       hauteur de la rangée pour que son contenu sticky puisse suivre le scroll */
}
.sidebar-inner {
    position: sticky;
    top: calc(var(--topbar-h, 64px) + 1rem);
    max-height: calc(100vh - var(--topbar-h, 64px) - 2rem);
    overflow-y: auto;
    display: flex;
    flex-direction: column;
    gap: 1rem;
}
.side-block {
    background: var(--card);
    border: 1px solid var(--line);
    border-radius: var(--radius);
    padding: 0.8rem 1rem;
}
.side-block h3 { margin: 0 0 0.5rem; font-size: 0.85rem; }
.side-block .sub { color: var(--muted); font-size: 0.8rem; margin: -0.3rem 0 0.5rem; }
.side-kv { display: flex; justify-content: space-between; gap: 0.6rem; font-size: 0.88rem; padding: 0.14rem 0; }
.side-kv span { color: var(--muted); }
.side-kv b { font-variant-numeric: tabular-nums; text-align: right; }
.side-foyers { list-style: none; margin: 0; padding: 0; }
.side-foyers li { border-bottom: 1px dashed var(--line); }
.side-foyers li:last-child { border-bottom: none; }
.side-foyers a {
    display: flex;
    justify-content: space-between;
    align-items: baseline;
    gap: 0.3rem 0.5rem;
    flex-wrap: wrap;
    padding: 0.28rem 0;
    color: var(--ink);
    text-decoration: none;
    font-size: 0.88rem;
}
.side-foyers a:hover .f-name { color: var(--accent); text-decoration: underline; }
.side-foyers .f-count { color: var(--muted); font-size: 0.8rem; white-space: nowrap; }
.side-foyers .badge { font-size: 0.7rem; padding: 0.06rem 0.45rem; }

/* Bannière « vue foyer » (admin) */
.viewas-banner {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 1rem;
    flex-wrap: wrap;
    background: var(--warn-soft);
    border: 1px solid var(--warn);
    border-radius: var(--radius);
    padding: 0.6rem 1rem;
    margin-bottom: 1.2rem;
    font-size: 0.92rem;
}

@media (max-width: 900px) {
    /* minmax(0,1fr) : sans lui, la largeur mini de la colonne est celle de son
       contenu (tableaux larges), ce qui élargit toute la page */
    .layout { grid-template-columns: minmax(0, 1fr); }
    .sidebar-inner {
        position: static;
        max-height: none;
        flex-direction: row;
        flex-wrap: wrap;
    }
    .side-block { flex: 1 1 240px; min-width: 0; }
    /* Tableaux larges : défilement horizontal interne au lieu d'élargir la page */
    table.members, table.txs {
        display: block;
        overflow-x: auto;
        -webkit-overflow-scrolling: touch;
    }
    table.members th, table.txs th { white-space: nowrap; }
}
.loading { color: var(--muted); }

section.card {
    background: var(--card);
    border: 1px solid var(--line);
    border-radius: var(--radius);
    padding: 1rem 1.2rem;
    margin-bottom: 1.2rem;
}
section.card > h2 {
    font-size: 1rem;
    margin: 0 0 0.8rem;
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 0.6rem;
    flex-wrap: wrap;
}

/* Paramètres */
.params-grid {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(180px, 1fr));
    gap: 0.8rem 1.2rem;
}
.field label { display: block; font-size: 0.8rem; color: var(--muted); margin-bottom: 0.15rem; }
.field input {
    width: 100%;
    padding: 0.4rem 0.6rem;
    border: 1px solid var(--line);
    border-radius: 6px;
    font-size: 0.95rem;
    background: #fff;
    color: var(--ink);
}
.field input:focus { outline: 2px solid var(--accent-soft); border-color: var(--accent); }

.bareme { display: flex; gap: 1rem; flex-wrap: wrap; }
.bareme .tranche {
    display: flex;
    align-items: center;
    gap: 0.5rem;
    background: var(--bg);
    border: 1px solid var(--line);
    border-radius: 8px;
    padding: 0.4rem 0.7rem;
}
.bareme .tranche input.t-name { width: 7.5rem; border: none; background: transparent; font-size: 0.9rem; color: var(--ink); }
.bareme .tranche input.t-coef { width: 4.2rem; text-align: right; padding: 0.25rem 0.4rem; border: 1px solid var(--line); border-radius: 6px; }
.bareme .tranche .x { color: var(--muted); font-size: 0.85rem; }

/* Résumé */
.stats {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(140px, 1fr));
    gap: 0.8rem;
}
.stat {
    background: var(--card);
    border: 1px solid var(--line);
    border-radius: var(--radius);
    padding: 0.7rem 0.9rem;
}
.stat .t {
    font-size: 0.72rem;
    font-weight: 600;
    text-transform: uppercase;
    letter-spacing: 0.04em;
    color: var(--muted);
    margin-bottom: 0.15rem;
}
.stat .v { font-size: 1.25rem; font-weight: 700; }
.stat .l { font-size: 0.78rem; color: var(--muted); }
.stat.accent { background: var(--accent-soft); border-color: var(--accent); }
.stat.good .v { color: var(--ok); }
.stat.bad .v { color: var(--bad); }
.stat.warn .v { color: var(--warn); }
/* Variantes à fond coloré (solde de la vue foyer) */
.stat.bg-good { background: var(--ok-soft); border-color: var(--ok); }
.stat.bg-good .v, .stat.bg-good .l, .stat.bg-good .t { color: var(--ok); }
.stat.bg-bad { background: var(--bad-soft); border-color: var(--bad); }
.stat.bg-bad .v, .stat.bg-bad .l, .stat.bg-bad .t { color: var(--bad); }
.stat.bg-warn { background: var(--warn-soft); border-color: var(--warn); }
.stat.bg-warn .v, .stat.bg-warn .l, .stat.bg-warn .t { color: var(--warn); }

/* Foyers */
.foyer { margin-bottom: 1rem; scroll-margin-top: calc(var(--topbar-h, 64px) + 1rem); }
.foyer-access {
    display: flex;
    align-items: center;
    gap: 0.6rem;
    flex-wrap: wrap;
    margin-top: 0.45rem;
    font-size: 0.85rem;
}
.foyer-access input.fa-phone {
    width: 10rem;
    padding: 0.3rem 0.5rem;
    border: 1px solid var(--line);
    border-radius: 6px;
    font-size: 0.85rem;
    background: #fff;
    color: var(--ink);
}
.foyer-access .fa-code { color: var(--muted); }
.foyer-access .fa-code b { color: var(--ink); letter-spacing: 0.08em; }
.foyer-access .btn { padding: 0.25rem 0.7rem; font-size: 0.82rem; }
.foyer-access .btn[disabled] { opacity: 0.45; cursor: not-allowed; }
.foyer-head {
    display: flex;
    align-items: baseline;
    gap: 0.8rem;
    flex-wrap: wrap;
}
.foyer-head h3 { margin: 0; font-size: 1rem; flex: 1; }
.badge {
    display: inline-block;
    padding: 0.15rem 0.6rem;
    border-radius: 999px;
    font-size: 0.8rem;
    font-weight: 600;
    white-space: nowrap;
}
.badge.ok { background: var(--ok-soft); color: var(--ok); }
.badge.due { background: var(--bad-soft); color: var(--bad); }
.badge.over { background: var(--warn-soft); color: var(--warn); }
.badge.neutral { background: var(--bg); color: var(--muted); }
.badge.sm { font-size: 0.72rem; padding: 0.08rem 0.5rem; }
.badge.age { background: #ede9fe; color: #6d28d9; }
.badge.days { background: var(--accent-soft); color: var(--accent); }

/* Puce « membre » : nom + badges âge et jours, insécable */
.member-chip { display: inline-block; white-space: nowrap; margin: 0.1rem 0.5rem 0.1rem 0; }

table.members { width: 100%; border-collapse: collapse; margin-top: 0.6rem; font-size: 0.92rem; }
table.members th {
    text-align: left;
    font-size: 0.75rem;
    text-transform: uppercase;
    letter-spacing: 0.03em;
    color: var(--muted);
    font-weight: 600;
    padding: 0.3rem 0.5rem;
    border-bottom: 1px solid var(--line);
}
table.members td { padding: 0.25rem 0.5rem; border-bottom: 1px solid var(--line); }
table.members tr:last-child td { border-bottom: none; }
table.members td.num, table.members th.num { text-align: right; font-variant-numeric: tabular-nums; }
table.members tfoot td { font-weight: 700; border-top: 2px solid var(--line); }
table.members input, table.members select {
    padding: 0.25rem 0.4rem;
    border: 1px solid transparent;
    border-radius: 6px;
    background: transparent;
    font-size: 0.92rem;
    color: var(--ink);
}
table.members input:hover, table.members select:hover,
table.members input:focus, table.members select:focus { border-color: var(--line); background: #fff; }
table.members input.m-name { width: 9rem; }
table.members input.m-days { width: 3.5rem; text-align: right; }
table.members input.m-courses { width: 4.5rem; text-align: right; }
table.members input.m-courses.override { font-weight: 700; color: var(--accent); }

button.icon {
    border: none;
    background: none;
    cursor: pointer;
    color: var(--muted);
    font-size: 0.95rem;
    padding: 0.15rem 0.3rem;
    border-radius: 6px;
}
button.icon:hover { background: var(--bad-soft); color: var(--bad); }
button.icon.safe:hover { background: var(--accent-soft); color: var(--accent); }

.add-line { margin-top: 0.5rem; display: flex; gap: 0.5rem; flex-wrap: wrap; align-items: center; }
.add-line input, .add-line select {
    padding: 0.35rem 0.55rem;
    border: 1px solid var(--line);
    border-radius: 6px;
    font-size: 0.9rem;
    background: #fff;
    color: var(--ink);
}
.add-line input.small { width: 4.5rem; }

button.btn {
    background: var(--accent);
    color: #fff;
    border: none;
    border-radius: 6px;
    padding: 0.4rem 0.9rem;
    font-size: 0.9rem;
    font-weight: 600;
    cursor: pointer;
}
button.btn:hover { filter: brightness(1.1); }
button.btn.ghost {
    background: transparent;
    color: var(--accent);
    border: 1px dashed var(--accent);
}

/* Transactions */
details.ledger { margin-top: 0.5rem; }
details.ledger summary {
    cursor: pointer;
    font-size: 0.85rem;
    color: var(--accent);
    user-select: none;
}
table.txs { width: 100%; border-collapse: collapse; font-size: 0.88rem; margin-top: 0.4rem; }
table.txs th {
    text-align: left;
    font-size: 0.75rem;
    text-transform: uppercase;
    letter-spacing: 0.03em;
    color: var(--muted);
    font-weight: 600;
    padding: 0.3rem 0.5rem;
    border-bottom: 1px solid var(--line);
}
table.txs th.num { text-align: right; }
table.txs td { padding: 0.2rem 0.5rem; border-bottom: 1px dashed var(--line); }
table.txs td.num { text-align: right; font-variant-numeric: tabular-nums; white-space: nowrap; }
table.txs td.date { color: var(--muted); white-space: nowrap; }
.tx-credit { color: var(--ok); }
.tx-debit { color: var(--bad); }

.hint { color: var(--muted); font-size: 0.82rem; }

/* Lien vers la location */
.side-kv a, .gite-link, .field label a { color: var(--accent); text-decoration: none; }
.side-kv a:hover, .gite-link:hover, .field label a:hover { text-decoration: underline; }
.gite-link { font-size: 0.85rem; font-weight: 500; white-space: nowrap; }

/* Session (en-tête) */
#who { margin-left: auto; display: flex; align-items: center; gap: 0.6rem; font-size: 0.88rem; color: var(--muted); }
#who .role { font-weight: 600; color: var(--ink); }
#who button {
    border: 1px solid var(--line);
    background: var(--bg);
    color: var(--muted);
    padding: 0.25rem 0.7rem;
    border-radius: 999px;
    cursor: pointer;
    font-size: 0.82rem;
}
#who button:hover { color: var(--bad); border-color: var(--bad); }

/* Écran d'accueil */
.login-wrap {
    max-width: 760px;
    margin: 8vh auto 0;
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 1.2rem;
}
.login-card {
    background: var(--card);
    border: 1px solid var(--line);
    border-radius: var(--radius);
    padding: 1.4rem;
    display: flex;
    flex-direction: column;
    gap: 0.8rem;
}
.login-card h2 { margin: 0; font-size: 1.05rem; }
.login-card p { margin: 0; color: var(--muted); font-size: 0.88rem; }
.login-card select, .login-card input {
    padding: 0.5rem 0.7rem;
    border: 1px solid var(--line);
    border-radius: 6px;
    font-size: 0.95rem;
    background: #fff;
    color: var(--ink);
}
.login-card .err { color: var(--bad); font-size: 0.85rem; min-height: 1.2em; margin: 0; }
@media (max-width: 640px) { .login-wrap { grid-template-columns: 1fr; margin-top: 3vh; } }

/* Vue foyer (lecture seule) */
.ro-table td { padding: 0.35rem 0.5rem; }

.m-meta { color: var(--muted); font-size: 0.8rem; font-weight: 400; }

/* Onglets internes de la vue foyer */
.hh-tabs { display: flex; gap: 0.5rem; margin-bottom: 1.2rem; flex-wrap: wrap; }
.hh-tabs button {
    border: 1px solid var(--line);
    background: var(--card);
    color: var(--ink);
    padding: 0.4rem 1rem;
    border-radius: 999px;
    cursor: pointer;
    font-size: 0.92rem;
}
.hh-tabs button.active {
    background: var(--accent);
    border-color: var(--accent);
    color: #fff;
    font-weight: 600;
}

/* Relevé de compte du foyer */
table.compte td.date { width: 6.5rem; }
table.compte tr.solde-row td {
    border-top: 2px solid var(--line);
    border-bottom: none;
    font-weight: 700;
    padding-top: 0.4rem;
}

/* Notice de calcul */
.notice { display: flex; gap: 1.8rem; flex-wrap: wrap; align-items: flex-start; }
.notice ol {
    margin: 0;
    padding-left: 1.2rem;
    font-size: 0.92rem;
    flex: 1 1 320px;
    display: flex;
    flex-direction: column;
    gap: 0.4rem;
}
.bareme-table { border-collapse: collapse; font-size: 0.9rem; }
.bareme-table caption {
    caption-side: top;
    text-align: left;
    font-size: 0.78rem;
    color: var(--muted);
    padding-bottom: 0.3rem;
    white-space: nowrap;
}
.bareme-table td { border: 1px solid var(--line); background: var(--bg); padding: 0.3rem 0.7rem; }
.bareme-table td.num { text-align: right; font-variant-numeric: tabular-nums; }

/* Foyers présents */
table.others td { vertical-align: top; }
table.others tr.own td { background: var(--accent-soft); }

@media (max-width: 640px) {
    table.members input.m-name { width: 6rem; }
    main { padding: 0.6rem; }
    section.card { padding: 0.8rem; }
    .topbar { padding-left: 0.7rem; padding-right: 0.7rem; }
}
