:root {
    --bg: #f4f5f9;
    --card-bg: #ffffff;
    --card-border: #e5e7f0;
    --border: #d8dbe6;
    --text: #1c1e2b;
    --text-muted: #6b7086;
    --accent: #6d28d9;
    --accent-2: #0e7490;
    --accent2: #c2410c;
    --accent2-2: #be185d;
    --positive: #15803d;
    --negative: #dc2626;
    --caisse: #c2410c;
    --livret: #15803d;
    --bancaire: #0e7490;
    --radius: 16px;
    --radius-sm: 11px;
    --shadow: 0 1px 2px rgba(16,24,40,.04), 0 10px 26px -12px rgba(16,24,40,.14);
    --sidebar-w: 250px;
    --font-sans: 'Inter', -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
    --font-display: 'Space Grotesk', var(--font-sans);
}

* { box-sizing: border-box; }

html, body { height: 100%; }

body {
    margin: 0;
    background: var(--bg);
    color: var(--text);
    font-family: var(--font-sans);
    line-height: 1.5;
    -webkit-font-smoothing: antialiased;
    padding-right: 0;
}

body.has-sidebar { padding-right: var(--sidebar-w); }

h1, h2 { font-family: var(--font-display); font-weight: 600; letter-spacing: -.01em; margin: 0 0 .3em; }
h1 { font-size: 1.9rem; display: flex; align-items: center; gap: 12px; }
h1 i { font-size: .78em; color: var(--accent-2); }
h2 { font-size: 1.05rem; color: var(--text); display: flex; align-items: center; gap: 9px; }
h2 i { color: var(--accent-2); font-size: .95em; }

a { color: var(--accent-2); text-decoration: none; }
a:hover { color: var(--accent); }

.container { max-width: 900px; margin: 0 auto; padding: 44px 30px 90px; }

/* --- Onglets de navigation, collés au bord droit --- */
.app-shell { display: block; }
.side-tabs {
    position: fixed;
    top: 0; right: 0;
    z-index: 20;
    height: 100vh;
    width: var(--sidebar-w);
    flex: 0 0 var(--sidebar-w);
    display: flex;
    flex-direction: column;
    justify-content: space-between;
    padding: 26px 16px;
    background: #ffffff;
    border-left: 1px solid var(--card-border);
    box-shadow: var(--shadow);
}
.side-tabs-top { display: flex; flex-direction: column; gap: 3px; overflow-y: auto; }
.side-brand {
    font-family: var(--font-display);
    font-size: 1.1rem; font-weight: 700; color: var(--text);
    display: flex; align-items: center; gap: 9px;
    padding: 4px 10px 20px;
}
.side-brand i { color: var(--accent-2); }
.tab-group-label {
    font-size: .68rem; font-weight: 700; text-transform: uppercase; letter-spacing: .08em;
    color: #9297ab; margin: 18px 10px 8px;
}
.tab-link {
    display: flex; align-items: center; gap: 11px;
    padding: 10px 12px;
    border-radius: var(--radius-sm);
    color: var(--text-muted);
    font-size: .88rem;
    font-weight: 500;
    position: relative;
}
.tab-link .tab-ico { width: 16px; text-align: center; opacity: .8; }
.tab-link:hover { background: rgba(0,0,0,.04); color: var(--text); }
.tab-link.active { background: var(--accent); color: #fff; }
.tab-link.active .tab-ico { opacity: 1; }
.tab-link-compte.tab-caisse.active { background: var(--caisse); }
.tab-link-compte.tab-livret.active { background: var(--livret); }
.tab-link-compte.tab-bancaire.active { background: var(--bancaire); }
.side-tabs-bottom {
    display: flex; align-items: center; justify-content: space-between; gap: 10px;
    padding: 16px 10px 4px; border-top: 1px solid var(--card-border); font-size: .85rem;
}
.user-name { font-weight: 500; color: var(--text-muted); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; display: flex; align-items: center; gap: 8px; }

/* --- Connexion --- */
.login-box {
    max-width: 380px;
    margin: 12vh auto;
    padding: 40px 36px;
    background: var(--card-bg);
    border: 1px solid var(--card-border);
    border-radius: var(--radius);
    box-shadow: var(--shadow);
}
.login-box h1 { justify-content: center; margin-bottom: 28px; }

/* --- Formulaires --- */
form label { display: block; margin-bottom: 14px; font-size: .82rem; color: var(--text-muted); font-weight: 500; }
form label.grow { flex: 1; }
input, select {
    width: 100%;
    margin-top: 6px;
    padding: 11px 13px;
    background: #f8f9fc;
    border: 1px solid var(--border);
    border-radius: var(--radius-sm);
    color: var(--text);
    font-size: .95rem;
    font-family: inherit;
}
input::placeholder { color: #9297ab; }
input:focus, select:focus { outline: none; border-color: var(--accent-2); box-shadow: 0 0 0 3px rgba(14,116,144,.15); background: #fff; }
select option { background: #fff; color: var(--text); }

.form-row { display: flex; gap: 16px; flex-wrap: wrap; align-items: flex-end; }
.form-row > label { flex: 1; min-width: 140px; }
.export-form .export-buttons { display: flex; gap: 10px; margin-bottom: 14px; }
.hint { font-size: .8rem; color: var(--text-muted); margin: 0; }

/* --- Boutons --- */
.btn {
    display: inline-flex;
    align-items: center;
    gap: 8px;
    padding: 10px 22px;
    border-radius: 980px;
    border: 1px solid transparent;
    cursor: pointer;
    font-size: .88rem;
    font-weight: 600;
    text-decoration: none;
    font-family: inherit;
    transition: filter .15s ease, background .15s ease, border-color .15s ease;
}
.btn:active { transform: scale(.97); }
.btn-primary { background: var(--accent); color: #fff; }
.btn-primary:hover { filter: brightness(1.1); color: #fff; }
.btn-accent2 { background: var(--accent2); color: #fff; }
.btn-accent2:hover { filter: brightness(1.1); color: #fff; }
.btn-ghost { background: rgba(0,0,0,.03); color: var(--text); border-color: var(--border); }
.btn-ghost:hover { border-color: #b7bccc; background: rgba(0,0,0,.05); }
.btn-small { padding: 6px 14px; font-size: .78rem; }

/* --- Boutons d'action en haut à droite d'une page --- */
.page-actions { display: flex; gap: 10px; flex-wrap: wrap; }

/* --- Onglets locaux (ex. Opérations / Graphiques) --- */
.local-tabs-row { display: flex; justify-content: flex-end; margin-bottom: 18px; }
.local-tabs {
    display: inline-flex;
    background: rgba(0,0,0,.04);
    border: 1px solid var(--border);
    padding: 4px;
    border-radius: 980px;
    gap: 2px;
}
.local-tab {
    display: inline-flex; align-items: center; gap: 7px;
    border: none;
    background: transparent;
    padding: 8px 18px;
    border-radius: 980px;
    font-size: .84rem;
    font-weight: 600;
    font-family: inherit;
    color: var(--text-muted);
    cursor: pointer;
}
.local-tab.active { background: var(--accent); color: #fff; }
.tab-panel { display: none; }
.tab-panel.active { display: block; }

/* --- Modales --- */
.modal-overlay {
    display: none;
    position: fixed; inset: 0; z-index: 100;
    background: rgba(28,30,43,.45);
    align-items: center; justify-content: center;
    padding: 20px;
}
.modal-overlay.open { display: flex; }
.modal {
    background: #ffffff;
    border: 1px solid var(--card-border);
    border-radius: var(--radius);
    box-shadow: var(--shadow);
    width: 100%; max-width: 520px;
    max-height: 88vh;
    overflow-y: auto;
    padding: 28px 28px 26px;
}
.modal-header { display: flex; align-items: center; justify-content: space-between; margin-bottom: 18px; }
.modal-header h2 { margin: 0; }
.modal-close {
    border: none; background: rgba(0,0,0,.05); color: var(--text-muted);
    width: 32px; height: 32px; border-radius: 50%; cursor: pointer;
    font-size: .95rem; line-height: 1;
}
.modal-close:hover { background: rgba(0,0,0,.1); color: var(--text); }

/* --- Alertes --- */
.alert { padding: 12px 18px; border-radius: var(--radius-sm); margin-bottom: 18px; font-size: .88rem; border: 1px solid transparent; }
.alert-error { background: rgba(220,38,38,.08); color: #b91c1c; border-color: rgba(220,38,38,.25); }
.alert-success { background: rgba(21,128,61,.08); color: #15803d; border-color: rgba(21,128,61,.25); }

/* --- En-têtes de page --- */
.page-head { margin-bottom: 26px; display: flex; flex-direction: column; gap: 14px; }
.subtitle { color: var(--text-muted); margin: 0; font-size: .95rem; }
.back-link { font-size: .85rem; color: var(--text-muted); display: inline-block; }
.back-link:hover { color: var(--text); }
.head-row { display: flex; justify-content: space-between; align-items: flex-end; flex-wrap: wrap; gap: 14px; }

/* --- Solde par organisation (dashboard) --- */
.org-totals-grid {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
    gap: 16px;
    margin-bottom: 30px;
}
.org-total-card {
    display: flex;
    flex-direction: column;
    gap: 6px;
    padding: 22px 24px;
    background: var(--card-bg);
    border: 1px solid var(--card-border);
    border-radius: var(--radius);
    box-shadow: var(--shadow);
}
.org-total-label { color: var(--text-muted); font-size: .85rem; font-weight: 600; display: flex; align-items: center; gap: 8px; }
.org-total-card strong { font-family: var(--font-display); font-size: 1.9rem; font-weight: 700; color: var(--accent); }
.org-total-card strong.negatif { color: var(--negative); }

/* --- Regroupement par organisation : aperçu de tous les comptes --- */
.org-section { margin-bottom: 30px; }
.org-section-title { margin-bottom: 12px; padding-bottom: 8px; border-bottom: 1px solid var(--card-border); }

/* --- Cartes comptes --- */
.grid-comptes {
    display: grid;
    grid-template-columns: repeat(auto-fill, minmax(230px, 1fr));
    gap: 18px;
    margin-bottom: 26px;
}
.card {
    background: var(--card-bg);
    border: 1px solid var(--card-border);
    border-radius: var(--radius);
    box-shadow: var(--shadow);
    padding: 22px 24px;
}
.card-compte { display: block; color: var(--text); transition: transform .15s ease, box-shadow .15s ease, border-color .15s ease; }
.card-compte:hover { transform: translateY(-2px); border-color: #c9cee0; box-shadow: 0 4px 14px -4px rgba(16,24,40,.18); color: var(--text); }
.card-compte-icon {
    display: inline-flex; align-items: center; justify-content: center;
    width: 38px; height: 38px; border-radius: 12px; margin-bottom: 12px;
    background: rgba(0,0,0,.04); color: var(--accent-2); font-size: 1rem;
}
.card-caisse .card-compte-icon { color: var(--caisse); }
.card-livret .card-compte-icon { color: var(--livret); }
.card-bancaire .card-compte-icon { color: var(--bancaire); }
.card-compte .solde { font-size: 1.7rem; font-weight: 700; font-family: var(--font-display); margin: 8px 0 0; }

.dot { display: inline-block; width: 9px; height: 9px; border-radius: 50%; margin-right: 6px; }
.dot-caisse { background: var(--caisse); }
.dot-livret { background: var(--livret); }
.dot-bancaire { background: var(--bancaire); }

.badge {
    display: inline-block;
    font-size: .68rem;
    font-weight: 700;
    text-transform: uppercase;
    letter-spacing: .03em;
    padding: 4px 11px;
    border-radius: 20px;
    margin-bottom: 8px;
}
.badge-caisse { background: rgba(194,65,12,.1); color: #9a3412; }
.badge-livret { background: rgba(21,128,61,.1); color: #166534; }
.badge-bancaire { background: rgba(14,116,144,.1); color: #155e75; }

.solde-hero { font-size: 2.3rem; font-weight: 700; font-family: var(--font-display); margin: 0; color: var(--accent); }
.solde-hero.negatif { color: var(--negative); }
.negatif { color: var(--negative) !important; }
.positif { color: var(--positive); }

/* --- Graphiques --- */
.charts-grid {
    display: grid;
    grid-template-columns: 1.3fr 1fr;
    gap: 18px;
    margin-bottom: 24px;
}
.chart-card { min-height: 280px; }
.chart-canvas-wrap { position: relative; height: 260px; margin-top: 6px; }
.chart-canvas-wrap canvas { max-height: 100%; }

/* --- Formulaires (dans les modales) --- */
.form-card form { margin-top: 4px; }

/* --- Tableau des opérations --- */
.tx-table { width: 100%; border-collapse: collapse; margin-top: 6px; background: var(--card-bg); border: 1px solid var(--card-border); border-radius: var(--radius); overflow: hidden; }
.tx-table th, .tx-table td {
    text-align: left;
    padding: 13px 16px;
    border-bottom: 1px solid var(--card-border);
    font-size: .89rem;
}
.tx-table tbody tr:hover { background: rgba(0,0,0,.015); }
.tx-table tbody tr:last-child td { border-bottom: none; }
.tx-table th { color: var(--text-muted); font-weight: 600; font-size: .7rem; text-transform: uppercase; letter-spacing: .06em; background: rgba(0,0,0,.015); }
.tx-table .num { text-align: right; font-variant-numeric: tabular-nums; }
.tx-table td form { margin: 0 0 6px; }
.tx-table td form:last-child { margin-bottom: 0; }
.empty { color: var(--text-muted); text-align: center; padding: 24px; }
.muted { color: var(--text-muted); font-size: .82rem; }

@media (max-width: 720px) {
    .charts-grid { grid-template-columns: 1fr; }
}

@media (max-width: 900px) {
    body.has-sidebar { padding-right: 0; }
    .app-shell { display: flex; flex-direction: column-reverse; }
    .side-tabs {
        position: static;
        height: auto;
        width: 100%;
        flex: none;
        flex-direction: row;
        align-items: center;
        overflow-x: auto;
        border-left: none;
        border-bottom: 1px solid var(--card-border);
        box-shadow: none;
        padding: 10px 16px;
    }
    .side-tabs-top { flex-direction: row; align-items: center; overflow: visible; }
    .side-brand { padding: 4px 14px 4px 0; }
    .tab-group-label { display: none; }
    .tab-link { white-space: nowrap; }
    .side-tabs-bottom { border-top: none; padding: 4px 0 4px 14px; }
}

@media (max-width: 640px) {
    .tx-table, .tx-table thead { display: block; }
    .tx-table thead { display: none; }
    .tx-table tbody tr { display: block; border-bottom: 1px solid var(--card-border); padding: 12px 4px; }
    .tx-table td { display: flex; justify-content: space-between; border: none; padding: 4px 12px; }
    .tx-table td::before { content: attr(data-label); color: var(--text-muted); font-size: .75rem; }
}
