:root {
    --bg: #f4f6f8;
    --card: #ffffff;
    --ink: #1c2733;
    --muted: #5a6b7b;
    --primary: #0b5d8a;
    --primary-ink: #ffffff;
    --line: #d9e1e8;
    --warn: #b23b00;
    --late-bg: #fbe3da;
    --late-ink: #8a2b00;
    --up-bg: #e2eefb;
    --up-ink: #114a7a;
    --ok-bg: #e2f4e7;
    --ok-ink: #1c6b38;
    --info-bg: #eef4fb;
    --err-bg: #fbe3e3;
    --err-ink: #8a1c1c;
    --radius: 10px;
}

* { box-sizing: border-box; }
body {
    margin: 0;
    font-family: system-ui, -apple-system, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
    color: var(--ink);
    background: var(--bg);
    line-height: 1.5;
}
body.plain { display: flex; flex-direction: column; min-height: 100vh; }

a { color: var(--primary); }
a:focus, button:focus, input:focus, select:focus, textarea:focus {
    outline: 3px solid #ffbf47; outline-offset: 1px;
}

.skip-link {
    position: absolute; left: -999px;
}
.skip-link:focus { left: 8px; top: 8px; background: #fff; padding: .5rem; z-index: 10; }

.container { max-width: 1040px; margin: 0 auto; padding: 1.5rem 1rem 3rem; width: 100%; }

/* Header / nav */
.app-header {
    display: flex; align-items: center; gap: 1rem;
    background: var(--primary); color: var(--primary-ink);
    padding: .75rem 1rem;
}
.app-header .brand a { color: #fff; font-weight: 700; text-decoration: none; }
.app-header .org-name { margin-left: auto; font-weight: 600; }
.logout-form { margin: 0; }
.btn-link { background: none; border: 1px solid rgba(255,255,255,.6); color: #fff; padding: .35rem .7rem; border-radius: 6px; cursor: pointer; }
.app-nav { display: flex; gap: .25rem; flex-wrap: wrap; background: #08496c; padding: 0 1rem; }
.app-nav a { color: #dceaf4; text-decoration: none; padding: .7rem 1rem; }
.app-nav a.is-active { background: var(--bg); color: var(--ink); border-radius: 8px 8px 0 0; font-weight: 600; }

h1 { font-size: 1.6rem; }
h2 { font-size: 1.2rem; margin-top: 2rem; }
.muted { color: var(--muted); }
.small { font-size: .85rem; }
.num { text-align: right; font-variant-numeric: tabular-nums; white-space: nowrap; }
.sr-only { position: absolute; width: 1px; height: 1px; padding: 0; margin: -1px; overflow: hidden; clip: rect(0 0 0 0); border: 0; }

.page-head { border-bottom: 1px solid var(--line); padding-bottom: .5rem; }
.freshness { color: var(--ok-ink); font-weight: 600; }

/* Cards */
.cards { display: grid; grid-template-columns: repeat(auto-fit, minmax(180px, 1fr)); gap: 1rem; margin: 1rem 0; }
.card { background: var(--card); border: 1px solid var(--line); border-radius: var(--radius); padding: 1rem; }
.card-primary { border-color: var(--primary); box-shadow: 0 1px 0 var(--primary) inset; }
.card-label { color: var(--muted); font-size: .9rem; }
.card-value { font-size: 1.5rem; font-weight: 700; margin-top: .25rem; }
.emphasis-warn { color: var(--warn); }

/* Tables */
.table-scroll { overflow-x: auto; }
.data-table { width: 100%; border-collapse: collapse; background: var(--card); border: 1px solid var(--line); border-radius: var(--radius); overflow: hidden; }
.data-table caption { text-align: left; }
.data-table th, .data-table td { text-align: left; padding: .6rem .75rem; border-bottom: 1px solid var(--line); }
.data-table thead th { background: #eef2f5; font-size: .9rem; }
.data-table tbody tr:hover { background: #f7fafc; }
.data-table tfoot th, .data-table tfoot td { background: #eef2f5; }

.badge { display: inline-block; padding: .15rem .5rem; border-radius: 999px; font-size: .8rem; background: #eef2f5; }
.badge-late { background: var(--late-bg); color: var(--late-ink); }
.badge-upcoming { background: var(--up-bg); color: var(--up-ink); }

/* Forms */
.field { margin: .75rem 0; }
.field label { display: block; font-weight: 600; margin-bottom: .25rem; }
.field-check { display: flex; align-items: center; gap: .5rem; }
.field-check label { font-weight: 400; margin: 0; }
input, select, textarea { font: inherit; padding: .5rem .6rem; border: 1px solid var(--line); border-radius: 6px; width: 100%; max-width: 420px; background: #fff; }
input[type=checkbox], input[type=radio] { width: auto; }
.filters { display: flex; flex-wrap: wrap; gap: 1rem; align-items: end; background: var(--card); border: 1px solid var(--line); border-radius: var(--radius); padding: 1rem; margin: 1rem 0; }
.radio { display: block; font-weight: 400; }
fieldset { border: 1px solid var(--line); border-radius: 6px; }

.btn { display: inline-block; padding: .55rem 1rem; border: 1px solid var(--primary); background: #fff; color: var(--primary); border-radius: 6px; cursor: pointer; text-decoration: none; }
.btn-primary { background: var(--primary); color: #fff; }
.actions-row { display: flex; gap: 1rem; align-items: center; margin-top: 1.5rem; flex-wrap: wrap; }

.alert { padding: .75rem 1rem; border-radius: 8px; margin: 1rem 0; }
.alert-error { background: var(--err-bg); color: var(--err-ink); }
.alert-success { background: var(--ok-bg); color: var(--ok-ink); }
.alert-info { background: var(--info-bg); }

.detail-grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(220px, 1fr)); gap: .5rem 1.5rem; }
.detail-grid dt { color: var(--muted); font-size: .9rem; }
.detail-grid dd { margin: 0 0 .5rem; font-weight: 600; }
/* Dans la grille de détail, valeurs alignées à gauche sous leur libellé
   (l'alignement à droite reste réservé aux colonnes de montants des tableaux). */
.detail-grid dd.num { text-align: left; }

.auth-card { max-width: 420px; margin: 3rem auto; background: var(--card); border: 1px solid var(--line); border-radius: var(--radius); padding: 2rem; }
.org-list { list-style: none; padding: 0; display: grid; gap: .75rem; max-width: 520px; }
.org-choice { width: 100%; text-align: left; background: var(--card); border: 1px solid var(--line); border-radius: var(--radius); padding: 1rem; cursor: pointer; display: flex; justify-content: space-between; align-items: center; }
.org-choice-name { font-weight: 600; }

.pagination { display: flex; gap: 1rem; align-items: center; justify-content: center; margin: 1.5rem 0; flex-wrap: wrap; }

.site-footer { border-top: 1px solid var(--line); padding: 1rem; color: var(--muted); font-size: .85rem; text-align: center; }
.site-footer nav { display: flex; gap: 1rem; justify-content: center; margin-bottom: .5rem; }

@media (max-width: 600px) {
    .app-header .org-name { display: none; }
}

/* Onboarding & sécurité */
.steps { padding-left: 1.2rem; }
.steps li { margin: .6rem 0; }
.secret-box { background: var(--card); border: 1px solid var(--line); border-radius: 8px; padding: .75rem 1rem; margin: .5rem 0; }
.secret-box code { font-size: 1rem; word-break: break-all; }
.uri { display: block; word-break: break-all; color: var(--muted); font-size: .8rem; }

/* Impression / export PDF */
.print-body { background: #fff; }
.print-toolbar { display: flex; gap: 1rem; padding: 1rem; }
.print-sheet { max-width: 900px; margin: 0 auto; padding: 0 1rem 2rem; }
.print-head h1 { margin-bottom: .25rem; }
@media print {
    .no-print { display: none !important; }
    body { background: #fff; }
    .print-sheet { max-width: none; margin: 0; padding: 0; }
    .data-table { border: 1px solid #000; }
    .data-table th, .data-table td { border-bottom: 1px solid #999; }
    a[href]:after { content: ""; }
}

/* Réclamations — contenu riche importé et fil d'échanges. */
.rich-content { line-height: 1.55; overflow-wrap: anywhere; }
.rich-content img { max-width: 100%; height: auto; }
.rich-content table { border-collapse: collapse; max-width: 100%; }
.rich-content td, .rich-content th { border: 1px solid var(--line); padding: .25rem .5rem; }
.rich-content blockquote { margin: .5rem 0; padding-left: .75rem; border-left: 3px solid var(--line); color: var(--muted); }
.rich-content pre { white-space: pre-wrap; overflow-wrap: anywhere; background: #f6f8fa; padding: .5rem; border-radius: 6px; }
.rich-content a { overflow-wrap: anywhere; }

.thread-msg { background: var(--card); border: 1px solid var(--line); border-radius: var(--radius); padding: .85rem 1rem; }
.thread-msg .thread-head { margin-bottom: .5rem; }
.thread-out { border-left: 3px solid var(--primary); }
.thread-in { border-left: 3px solid #6b7f95; }
.thread-note { border-left: 3px solid #d1a54a; background: #fffdf6; }
.thread-system { opacity: .8; }

/* Composeur de réponse replié derrière un bouton coloré compact : on lit
   d'abord le fil, on déplie pour répondre. La flèche native du <details> est
   masquée sur tous les navigateurs (list-style + ::marker + ::-webkit). */
.reply-card { padding: 0; border: none; background: none; }
.reply-toggle {
    list-style: none; cursor: pointer; user-select: none;
    display: inline-flex; align-items: center; gap: .45rem; width: fit-content;
    padding: .5rem .95rem; font-weight: 700; font-size: .95rem;
    color: var(--accent-ink, #1b2023); background: var(--accent, #f49f25);
    border: 1px solid var(--accent, #f49f25); border-radius: 8px;
    transition: background .12s ease, border-color .12s ease;
}
.reply-toggle::-webkit-details-marker { display: none; }
.reply-toggle::marker { content: ""; }
.reply-toggle:hover { background: var(--accent-hover, #fbb869); border-color: var(--accent-hover, #fbb869); }
.reply-toggle-ico { font-weight: 400; }
.reply-card[open] .reply-toggle { margin-bottom: .9rem; }
.reply-card[open] .reply-toggle::after { content: "✕"; margin-left: .15rem; font-weight: 700; opacity: .7; }
.reply-card[open] .reply-toggle .reply-toggle-ico { display: none; }
.reply-body { border: 1px solid var(--line); border-radius: var(--radius); padding: 1rem; background: var(--card); }

/* Éditeur de texte enrichi (réponses ET modèles) : masqué par défaut, révélé par
   recl-editor.js ; sans JS, la zone de texte de repli reste visible. */
.rte-toolbar { display: none; gap: .35rem; flex-wrap: wrap; }
.rte-toolbar .rte-btn { padding: .25rem .6rem; line-height: 1.2; }
.rte-surface { display: none; min-height: 8rem; border: 1px solid var(--line); border-radius: 6px; padding: .6rem .7rem; background: #fff; overflow: auto; }
.rte-surface:focus { outline: 2px solid var(--primary); outline-offset: 1px; }
.rte-surface mark, .rich-content mark { background: #fff3b0; padding: 0 .1em; }

/* Réclamations — barre de filtres lisible (passe à la ligne) + retour en haut. */
.filters-bar {
    position: sticky;
    top: 0;
    z-index: 20;
    display: flex;
    gap: .9rem 1.1rem;
    flex-wrap: wrap;
    align-items: flex-end;
    box-shadow: 0 2px 6px rgba(0,0,0,.06);
}
.filters-bar .field { margin: 0; display: flex; flex-direction: column; }
.filters-bar .field label { white-space: nowrap; }
.filters-bar .field-search { flex: 1 1 18rem; min-width: 14rem; }
.filters-bar .field-search input { width: 100%; }
/* Les listes déroulantes ne sont plus rabotées : largeur mini confortable. */
.filters-bar select { min-width: 10rem; max-width: 18rem; }
.filters-bar .field-check { align-self: flex-end; }
.filters-bar .field-check label { font-weight: 400; }
.filters-actions { display: flex; gap: .75rem; align-items: center; align-self: flex-end; }
.filters-bar .btn { flex: 0 0 auto; }
.filters-reset { white-space: nowrap; }

/* Filtre multi-sélection (liste de cases à cocher dépliable). */
.ms { position: relative; }
.ms-summary {
    list-style: none; cursor: pointer; user-select: none;
    display: inline-flex; align-items: center; justify-content: space-between; gap: .55rem;
    min-width: 7rem; padding: .5rem .8rem; font-weight: 600;
    color: var(--primary); background: var(--card);
    border: 1px solid var(--primary); border-radius: 8px;
    transition: background .12s ease;
}
.ms-summary::-webkit-details-marker { display: none; }
.ms-summary::marker { content: ""; }
.ms-summary:hover { background: var(--surface-2, #eef3f8); }
.ms[open] .ms-summary { background: var(--surface-2, #eef3f8); }
.ms-caret { font-size: .7rem; transition: transform .15s ease; }
.ms[open] .ms-caret { transform: rotate(180deg); }
.ms-panel {
    position: absolute; z-index: 30; margin-top: .35rem;
    min-width: 15rem; max-height: 18rem; overflow-y: auto;
    background: var(--card); border: 1px solid var(--line); border-radius: 10px;
    box-shadow: 0 8px 24px rgba(0,0,0,.14); padding: .4rem;
}
.ms-actions { display: flex; gap: .5rem; padding: .25rem .25rem .4rem; border-bottom: 1px solid var(--line); margin-bottom: .3rem; }
.ms-btn { background: none; border: 1px solid var(--line); color: var(--primary); font-size: .82rem; padding: .2rem .5rem; border-radius: 6px; cursor: pointer; }
.ms-btn:hover { border-color: var(--primary); }
.ms-opt { display: flex; align-items: center; gap: .5rem; padding: .35rem .5rem; border-radius: 6px; font-weight: 400; cursor: pointer; white-space: nowrap; }
.ms-opt:hover { background: var(--surface-2, #f1f4f8); }
.ms-opt input { margin: 0; }

.back-to-top {
    position: fixed;
    right: 1.25rem;
    bottom: 1.25rem;
    z-index: 50;
    width: 2.75rem;
    height: 2.75rem;
    border-radius: 999px;
    display: flex;
    align-items: center;
    justify-content: center;
    background: var(--primary);
    color: #fff;
    text-decoration: none;
    font-size: 1.35rem;
    line-height: 1;
    box-shadow: 0 2px 8px rgba(0,0,0,.25);
}
.back-to-top:hover { opacity: .9; }
