:root {
    --bleu: #0e7490;
    --bleu-fonce: #155e75;
    --bleu-clair: #e0f2fe;
    --fond: #f4f7f9;
    --carte: #ffffff;
    --texte: #1f2933;
    --muted: #64748b;
    --bord: #dbe3ea;
    --vert: #15803d;
    --orange: #c2410c;
    --rouge: #b91c1c;
    --rayon: 8px;
    font-family: "Segoe UI", system-ui, -apple-system, Roboto, Arial, sans-serif;
    font-size: 14px;
    color: var(--texte);
}

* { box-sizing: border-box; }
body { margin: 0; background: var(--fond); }
.hidden { display: none !important; }
.muted { color: var(--muted); }
.error { color: var(--rouge); min-height: 1.2em; }
.num { text-align: right; font-variant-numeric: tabular-nums; white-space: nowrap; }
.center { text-align: center; }

/* ---------- Connexion ---------- */
.login { min-height: 100vh; display: grid; place-items: center; background: linear-gradient(160deg, #0e7490, #164e63); }
.login-box { background: #fff; padding: 32px; border-radius: 12px; width: 340px; display: flex; flex-direction: column; gap: 12px; box-shadow: 0 20px 40px rgba(0,0,0,.25); }
.login-box h1 { margin: 0; text-align: center; color: var(--bleu-fonce); }
.login-box p { margin: 0; text-align: center; }
.logo-drop { width: 48px; height: 48px; margin: 0 auto; background: var(--bleu); border-radius: 0 50% 50% 50%; transform: rotate(45deg); }
.logo-drop.small { width: 22px; height: 22px; margin: 0; flex: none; background: #67e8f9; }

/* ---------- Mise en page ---------- */
.app { display: grid; grid-template-columns: 220px 1fr; min-height: 100vh; }
.sidebar { background: #0f3b4a; color: #e2f3f8; display: flex; flex-direction: column; position: sticky; top: 0; height: 100vh; }
.brand { display: flex; gap: 12px; align-items: center; padding: 18px 16px; border-bottom: 1px solid rgba(255,255,255,.1); }
.brand small { display: block; color: #9cc9d6; font-size: 11px; }
nav { display: flex; flex-direction: column; padding: 8px; gap: 2px; flex: 1; }
nav a { color: #cfe7ee; text-decoration: none; padding: 10px 12px; border-radius: 6px; }
nav a:hover { background: rgba(255,255,255,.08); }
nav a.active { background: var(--bleu); color: #fff; font-weight: 600; }
.nav-guide { font: inherit; font-weight: 600; text-align: left; cursor: pointer; border: 1px solid #67e8f9; background: rgba(103, 232, 249, .12);
    color: #a5f3fc; padding: 9px 12px; border-radius: 6px; margin-bottom: 8px; }
.nav-guide:hover { background: #67e8f9; color: #0f3b4a; }
.user-box { padding: 14px 16px; border-top: 1px solid rgba(255,255,255,.1); }
.user-box small { color: #9cc9d6; display: block; }
.user-box .btn.link { color: #67e8f9; padding: 6px 0 0; }
.copyright { margin: 10px 0 0; font-size: 10.5px; line-height: 1.35; color: var(--muted); text-align: center; }
.user-box .copyright { color: #7fa9b6; text-align: left; }

main { min-width: 0; }
.topbar { display: flex; justify-content: space-between; align-items: center; padding: 14px 24px; background: #fff; border-bottom: 1px solid var(--bord); position: sticky; top: 0; z-index: 5; }
.topbar h2 { margin: 0; font-size: 20px; color: var(--bleu-fonce); }
.periode { display: flex; gap: 8px; align-items: center; font-weight: 600; color: var(--muted); }
.page { padding: 20px 24px 40px; }

/* ---------- Composants ---------- */
.card { background: var(--carte); border: 1px solid var(--bord); border-radius: var(--rayon); padding: 16px; margin-bottom: 16px; }
.card h3 { margin: 0 0 12px; font-size: 15px; color: var(--bleu-fonce); }
.toolbar { display: flex; flex-wrap: wrap; gap: 10px; align-items: end; margin-bottom: 14px; }
.toolbar .spacer { flex: 1; }
label { display: flex; flex-direction: column; gap: 4px; font-size: 12px; color: var(--muted); font-weight: 600; }
input, select, textarea { font: inherit; padding: 7px 9px; border: 1px solid var(--bord); border-radius: 6px; background: #fff; color: var(--texte); }
input:focus, select:focus, textarea:focus { outline: 2px solid #67e8f9; border-color: var(--bleu); }
input[type=checkbox] { width: 16px; height: 16px; }
label.check { flex-direction: row; align-items: center; gap: 8px; }

.btn { font: inherit; border: 1px solid var(--bord); background: #fff; color: var(--texte); padding: 7px 14px; border-radius: 6px; cursor: pointer; white-space: nowrap; }
.btn:hover { background: #f1f5f9; }
.btn.primary { background: var(--bleu); border-color: var(--bleu); color: #fff; }
.btn.primary:hover { background: var(--bleu-fonce); }
.btn.danger { color: var(--rouge); border-color: #fecaca; }
.btn.small { padding: 3px 8px; font-size: 12px; }
.btn.link { border: none; background: none; color: var(--bleu); padding: 0; }
.btn:disabled { opacity: .5; cursor: not-allowed; }

table.grid { width: 100%; border-collapse: collapse; background: #fff; }
.table-wrap { overflow: auto; border: 1px solid var(--bord); border-radius: var(--rayon); max-height: calc(100vh - 250px); }
table.grid th { position: sticky; top: 0; background: #eef6f9; color: var(--bleu-fonce); font-size: 12px; text-transform: uppercase; letter-spacing: .02em; padding: 8px; text-align: left; border-bottom: 1px solid var(--bord); z-index: 1; }
table.grid td { padding: 6px 8px; border-bottom: 1px solid #edf1f4; }
table.grid td:first-child { white-space: nowrap; }
table.grid tbody tr:hover { background: #f8fbfc; }
table.grid tfoot td { font-weight: 700; background: #f1f7f9; position: sticky; bottom: 0; }
table.grid tr.clickable { cursor: pointer; }
table.grid input { width: 100px; padding: 4px 6px; text-align: right; }
tr.anomalie td { background: #fef2f2; }
tr.alerte td { background: #fff7ed; }
tr.facture td { color: var(--muted); }

.badge { display: inline-block; padding: 2px 8px; border-radius: 999px; font-size: 11px; font-weight: 700; }
.badge.PAYEE { background: #dcfce7; color: var(--vert); }
.badge.PARTIELLE { background: #ffedd5; color: var(--orange); }
.badge.IMPAYEE { background: #fee2e2; color: var(--rouge); }
.badge.BA { background: #ede9fe; color: #6d28d9; }
.badge.BP { background: var(--bleu-clair); color: var(--bleu-fonce); }
.badge.BF { background: #fef9c3; color: #a16207; }
.badge.gris { background: #e2e8f0; color: #475569; }

.kpis { display: grid; grid-template-columns: repeat(auto-fill, minmax(190px, 1fr)); gap: 12px; margin-bottom: 16px; }
.kpi { background: #fff; border: 1px solid var(--bord); border-radius: var(--rayon); padding: 14px 16px; }
.kpi small { color: var(--muted); font-weight: 600; text-transform: uppercase; font-size: 11px; }
.kpi div { font-size: 22px; font-weight: 700; margin-top: 4px; color: var(--bleu-fonce); }
.kpi.alerte div { color: var(--rouge); }
.kpi.ok div { color: var(--vert); }

.grid-2 { display: grid; grid-template-columns: 1fr 1fr; gap: 16px; }
@media (max-width: 1100px) { .grid-2 { grid-template-columns: 1fr; } }
.form-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 12px; }
.form-grid .full { grid-column: 1 / -1; }
.actions { display: flex; gap: 8px; justify-content: flex-end; margin-top: 16px; }
.resume { background: var(--bleu-clair); border-radius: var(--rayon); padding: 12px 16px; margin: 12px 0; }
.resume ul { margin: 6px 0 0; padding-left: 18px; }

.chart { width: 100%; height: 240px; }
.legende { display: flex; gap: 16px; font-size: 12px; color: var(--muted); }
.legende i { display: inline-block; width: 10px; height: 10px; border-radius: 2px; margin-right: 6px; }

/* ---------- Modale & toast ---------- */
.modal { position: fixed; inset: 0; background: rgba(15, 23, 42, .45); display: grid; place-items: center; z-index: 50; padding: 20px; }
.modal-box { background: #fff; border-radius: 10px; width: min(820px, 100%); max-height: 90vh; overflow: auto; padding: 0 20px 20px; }
.modal-box header { display: flex; justify-content: space-between; align-items: center; position: sticky; top: 0; background: #fff; padding: 16px 0 10px; border-bottom: 1px solid var(--bord); margin-bottom: 14px; }
.modal-box h3 { margin: 0; color: var(--bleu-fonce); }
.toast { position: fixed; bottom: 20px; right: 20px; background: #0f3b4a; color: #fff; padding: 12px 18px; border-radius: 8px; z-index: 100; box-shadow: 0 8px 20px rgba(0,0,0,.2); max-width: 420px; }
.toast.erreur { background: var(--rouge); }

/* ---------- Habilitations ---------- */
.onglets { display: flex; gap: 4px; border-bottom: 2px solid var(--bord); margin-bottom: 16px; overflow-x: auto; }
.onglets button { font: inherit; font-weight: 600; background: none; border: none; border-bottom: 3px solid transparent;
    margin-bottom: -2px; padding: 10px 16px; color: var(--muted); cursor: pointer; white-space: nowrap; }
.onglets button:hover { color: var(--bleu-fonce); }
.onglets button.actif { color: var(--bleu-fonce); border-bottom-color: var(--bleu); }
.badge.profil { background: #ede9fe; color: #5b21b6; }
.badge[class*="action-"] { background: #e0f2fe; color: var(--bleu-fonce); }
.badge.action-COMPTE_DESACTIVE, .badge.action-PROFIL_SUPPRIME { background: #fee2e2; color: var(--rouge); }
.badge.action-DROITS_MODIFIES, .badge.action-PROFIL_ATTRIBUE { background: #ffedd5; color: var(--orange); }
.badge.action-UTILISATEUR_CREE, .badge.action-PROFIL_CREE, .badge.action-COMPTE_ACTIVE { background: #dcfce7; color: var(--vert); }

.matrice-wrap { max-height: calc(100vh - 230px); }
table.matrice th.col-profil { text-align: center; min-width: 120px; vertical-align: top; text-transform: none; letter-spacing: 0; }
table.matrice th.col-profil small { display: block; font-weight: 400; color: var(--muted); }
.profil-entete { font-size: 13px; }
.profil-actions { display: flex; gap: 4px; justify-content: center; margin-top: 4px; }
table.matrice .col-droit { position: sticky; left: 0; background: #fff; z-index: 1; min-width: 220px; }
table.matrice thead th.col-droit { background: #eef6f9; z-index: 2; }
table.matrice td.center { text-align: center; }
table.matrice input[type=checkbox] { width: 18px; height: 18px; cursor: pointer; }
table.matrice input[type=checkbox]:disabled { cursor: not-allowed; }
tr.ligne-module td { background: #f1f7f9; font-weight: 700; color: var(--bleu-fonce); text-transform: uppercase; font-size: 12px; letter-spacing: .03em; }
.droits-liste { display: grid; grid-template-columns: repeat(auto-fill, minmax(200px, 1fr)); gap: 4px 16px; }
.droits-module ul { margin: 2px 0 8px; padding-left: 18px; }

/* ---------- Téléphones et petites tablettes ---------- */
.btn-menu { display: none; font-size: 22px; line-height: 1; background: none; border: none; color: var(--bleu-fonce);
    cursor: pointer; padding: 4px 8px 4px 0; }
.voile { display: none; }

@media (max-width: 760px) {
    :root { font-size: 14px; }
    .app { grid-template-columns: 1fr; }

    /* menu en tiroir */
    .sidebar { position: fixed; top: 0; left: 0; bottom: 0; width: 260px; height: 100%; z-index: 60;
        transform: translateX(-100%); transition: transform .2s ease; box-shadow: 4px 0 20px rgba(0,0,0,.25); }
    .app.menu-ouvert .sidebar { transform: none; }
    .app.menu-ouvert .voile { display: block; position: fixed; inset: 0; background: rgba(15, 23, 42, .45); z-index: 55; }
    nav a { padding: 12px; }
    .btn-menu { display: inline-block; }

    /* barre du haut */
    .topbar { padding: 8px 12px; gap: 8px; }
    .topbar h2 { font-size: 17px; flex: 1; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
    .periode { font-size: 0; gap: 0; }
    .periode input { font-size: 15px; width: 150px; }

    .page { padding: 12px 10px 90px; }
    .card { padding: 12px; }

    /* champs : 16 px évite le zoom automatique de l'iPhone */
    input, select, textarea { font-size: 16px; }
    .toolbar { gap: 8px; }
    .toolbar > label { flex: 1 1 140px; }
    .toolbar > label input, .toolbar > label select { width: 100%; }
    .toolbar .spacer { display: none; }
    .toolbar .btn { flex: 1 1 auto; }
    .btn { padding: 9px 12px; }
    .btn.small { padding: 6px 10px; font-size: 13px; }

    .kpis { grid-template-columns: repeat(2, 1fr); gap: 8px; }
    .kpi { padding: 10px 12px; }
    .kpi div { font-size: 18px; }
    .grid-2, .form-grid { grid-template-columns: 1fr; }

    /* tableaux : colonnes secondaires masquées, défilement horizontal si besoin */
    .col-sec { display: none; }
    .table-wrap { max-height: 70vh; }
    table.grid th { font-size: 11px; padding: 6px 4px; white-space: normal; }
    table.grid td { padding: 6px 5px; font-size: 13px; }
    table.grid td:first-child { white-space: normal; }

    /* fenêtres en plein écran */
    .modal { padding: 0; place-items: stretch; }
    .modal-box { width: 100%; max-height: none; height: 100%; border-radius: 0; padding: 0 14px 20px; }
    .actions { flex-wrap: wrap; }
    .actions .btn { flex: 1 1 auto; }

    .toast { left: 10px; right: 10px; bottom: 10px; max-width: none; }
    .login-box { width: min(340px, 92vw); padding: 24px; }

    /* saisie des relevés au téléphone */
    table.grid input { width: 62px; padding: 6px 3px; -moz-appearance: textfield; }
    table.grid input::-webkit-inner-spin-button, table.grid input::-webkit-outer-spin-button { -webkit-appearance: none; margin: 0; }
    .page { padding-left: 8px; padding-right: 8px; }
    table.grid td { padding: 6px 4px; }
    .btn-flottant { position: fixed; right: 12px; bottom: 14px; z-index: 20; padding: 12px 20px;
        box-shadow: 0 6px 18px rgba(14, 116, 144, .45); border-radius: 999px; }
    .btn-flottant:disabled { display: none; }

    table.matrice .col-droit { min-width: 140px; }
    table.matrice th.col-profil { min-width: 90px; }
    .onglets button { padding: 10px 10px; }
}

/* ---------- Impression ---------- */
#print-zone { display: none; }

@media print {
    @page { size: A4; margin: 8mm; }
    body { background: #fff; }
    #app, #login, #modal, #toast { display: none !important; }
    #print-zone { display: block; }
}

.facture-doc { height: 136mm; padding: 4mm 6mm; border: 1px solid #000; margin-bottom: 6mm; font-size: 10.5px; color: #000; display: flex; flex-direction: column; page-break-inside: avoid; break-inside: avoid; }
.facture-doc.saut { page-break-after: always; break-after: page; }
.facture-doc table { width: 100%; border-collapse: collapse; }
.facture-doc .entete, .recu-doc .entete { display: flex; justify-content: space-between; align-items: center; gap: 3mm; border-bottom: 2px solid #000; padding-bottom: 2mm; }
.entete .entete-societe { flex: 1; }
.logo-facture { max-height: 16mm; max-width: 38mm; object-fit: contain; flex: none; }
.recu-doc .logo-facture { max-height: 12mm; max-width: 28mm; }
.recu-doc .entete { font-size: 9px; }

/* Paramètres : aperçu du logo */
.entete-config { display: flex; gap: 20px; align-items: center; flex-wrap: wrap; }
.logo-apercu { width: 220px; height: 110px; border: 1px dashed var(--bord); border-radius: 8px; display: grid; place-items: center;
    background: repeating-conic-gradient(#f1f5f9 0% 25%, #fff 0% 50%) 50% / 16px 16px; position: relative; }
.logo-apercu img { max-width: 200px; max-height: 96px; object-fit: contain; }
.logo-apercu .badge { position: absolute; bottom: 4px; right: 4px; }
.apercu-facture { overflow-x: auto; }
.apercu-facture .facture-doc { width: 190mm; background: #fff; }
.facture-doc .entete strong { font-size: 13px; }
.facture-doc .titre { text-align: center; font-weight: 700; font-size: 13px; margin: 2mm 0; letter-spacing: .1em; }
.facture-doc .infos td { border: 1px solid #000; padding: 1mm 2mm; }
.facture-doc .infos th { border: 1px solid #000; padding: 1mm 2mm; background: #eee; font-size: 9.5px; }
.facture-doc .lignes { margin-top: 2mm; }
.facture-doc .lignes th, .facture-doc .lignes td { border: 1px solid #000; padding: 1mm 2mm; }
.facture-doc .lignes th { background: #eee; }
.facture-doc .total td { font-weight: 700; }
.facture-doc .conditions { font-size: 9px; margin-top: 2mm; }
.facture-doc .talon { margin-top: auto; border-top: 1px dashed #000; padding-top: 1.5mm; }
.facture-doc .talon td, .facture-doc .talon th { border: 1px solid #000; padding: .8mm 2mm; font-size: 9.5px; }

.fiche-doc { font-size: 11px; color: #000; }
.fiche-doc h2 { text-align: center; text-decoration: underline; margin: 0 0 4mm; }
.fiche-doc table { width: 100%; border-collapse: collapse; }
.fiche-doc th, .fiche-doc td { border: 1px solid #000; padding: 1.5mm 2mm; }
.fiche-doc th { background: #222; color: #fff; }
.fiche-doc td.vide { width: 22mm; }

.recu-doc { width: 100mm; font-size: 11px; color: #000; border: 1px solid #000; padding: 4mm; }
.recu-doc h3 { text-align: center; margin: 2mm 0; }
.recu-doc table { width: 100%; border-collapse: collapse; }
.recu-doc td { padding: .8mm 0; }
