:root {
  --bg: #f5f6f8; --panel: #fff; --ink: #1d2330; --muted: #6b7385; --line: #e2e5eb;
  --brand: #1f4e79; --brand-2: #2a6ba3; --ok: #1e7b4c; --warn: #a86a00; --ko: #b3261e;
  --radius: 8px; font-size: 15px;
}
* { box-sizing: border-box; }
body { margin: 0; font-family: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif; color: var(--ink); background: var(--bg); line-height: 1.45; }
a { color: var(--brand-2); }
h1 { font-size: 1.45rem; margin: 0 0 1rem; }
h1 small { font-size: .9rem; color: var(--muted); font-weight: 500; }
h2 { font-size: 1.05rem; margin: 0 0 .75rem; }
code { background: #eef1f5; padding: 1px 5px; border-radius: 4px; font-size: .9em; }
.hidden { display: none !important; }
.muted { color: var(--muted); }
.small { font-size: .85rem; }
.right { text-align: right; }

.topbar { display: flex; justify-content: space-between; align-items: center; background: var(--brand); color: #fff; padding: .6rem 1.2rem; }
.topbar a { color: #fff; text-decoration: none; }
.brand { font-weight: 700; letter-spacing: .02em; }
.user { display: flex; gap: .8rem; align-items: center; }
.user .link { color: #dfe8f3; }
.badge { background: rgba(255,255,255,.15); padding: 1px 8px; border-radius: 10px; font-size: .78rem; }

.shell { display: flex; min-height: calc(100vh - 90px); }
.side { width: 220px; flex: none; background: var(--panel); border-right: 1px solid var(--line); padding: 1rem .6rem; }
.side a { display: block; padding: .45rem .7rem; border-radius: 6px; color: var(--ink); text-decoration: none; }
.side a:hover { background: #eef2f8; }
.side a.active { background: #e1eaf5; color: var(--brand); font-weight: 600; }
.side .group { font-size: .72rem; text-transform: uppercase; letter-spacing: .08em; color: var(--muted); margin: 1rem .7rem .3rem; }
main { flex: 1; padding: 1.5rem 2rem; max-width: 1400px; min-width: 0; }
.foot { text-align: center; font-size: .75rem; color: var(--muted); padding: .6rem; }

.card { background: var(--panel); border: 1px solid var(--line); border-radius: var(--radius); padding: 1.1rem 1.3rem; margin-bottom: 1.2rem; }
.grid2 { display: grid; grid-template-columns: repeat(auto-fit, minmax(340px, 1fr)); gap: 1.2rem; }
.grid2 > .card { margin-bottom: 0; }
.grid2 { margin-bottom: 1.2rem; }
.head { display: flex; justify-content: space-between; align-items: flex-start; gap: 1rem; flex-wrap: wrap; margin-bottom: .5rem; }
.head > div { display: flex; gap: .5rem; flex-wrap: wrap; }

.kpis { display: grid; grid-template-columns: repeat(auto-fit, minmax(180px, 1fr)); gap: 1rem; margin-bottom: 1.2rem; }
.kpi { background: var(--panel); border: 1px solid var(--line); border-radius: var(--radius); padding: .9rem 1rem; }
.kpi a { text-decoration: none; color: inherit; display: block; }
.kpi span { display: block; color: var(--muted); font-size: .82rem; }
.kpi strong { display: block; font-size: 1.45rem; margin-top: .2rem; font-variant-numeric: tabular-nums; }
.kpi meter { width: 100%; height: 10px; margin-top: .4rem; }
.kpi small { color: var(--muted); }

.table { width: 100%; border-collapse: collapse; background: var(--panel); border: 1px solid var(--line); border-radius: var(--radius); overflow: hidden; margin-bottom: 1rem; }
.card .table { border: none; }
.table th, .table td { padding: .5rem .65rem; border-bottom: 1px solid var(--line); text-align: left; vertical-align: middle; }
.table thead th { background: #f0f3f7; font-size: .8rem; text-transform: uppercase; letter-spacing: .04em; color: var(--muted); }
.table tfoot th { background: #f7f8fa; }
.table.compact td, .table.compact th { padding: .3rem .55rem; }
.num { text-align: right !important; font-variant-numeric: tabular-nums; white-space: nowrap; }
.neg { color: var(--ko); }
tr.clickable { cursor: pointer; }
tr.clickable:hover td { background: #f6f9fd; }
tr.cancelled td { color: var(--muted); text-decoration: line-through; }
tr.pending td { background: #fff9ec; }
tr.over td { color: var(--ko); font-weight: 600; }
.actions { white-space: nowrap; }
.actions > * { margin-right: .4rem; }
.details pre { white-space: pre-wrap; font-size: .78rem; max-width: 520px; }

.tag { display: inline-block; font-size: .72rem; padding: 1px 7px; border-radius: 10px; background: #e8ebf0; color: var(--muted); }
.tag.ok { background: #e3f3ea; color: var(--ok); }
.tag.warn { background: #fdf1dc; color: var(--warn); }
.tag.ko { background: #fbe4e2; color: var(--ko); }

.alert { padding: .75rem 1rem; border-radius: var(--radius); margin-bottom: 1rem; border: 1px solid; }
.alert-success { background: #e9f6ef; border-color: #b7e0c8; color: #165c39; }
.alert-warning { background: #fff6e5; border-color: #f2d49b; color: #7a4e00; }
.alert-danger { background: #fdecea; border-color: #f3b8b3; color: #8c1d16; }

input, select, textarea, button, .button { font: inherit; }
input, select, textarea { padding: .45rem .55rem; border: 1px solid #c9cfd8; border-radius: 6px; background: #fff; max-width: 100%; }
input:focus, select:focus, textarea:focus { outline: 2px solid #9cc0e6; border-color: var(--brand-2); }
textarea { width: 100%; }
label { display: flex; flex-direction: column; gap: .25rem; font-size: .88rem; color: #39404f; }
label small { color: var(--muted); }
label.check { flex-direction: row; align-items: center; gap: .4rem; }
button, .button { display: inline-block; padding: .45rem .9rem; border: 1px solid #c3cad5; border-radius: 6px; background: #fff; color: var(--ink); cursor: pointer; text-decoration: none; }
button:hover, .button:hover { background: #f0f3f7; }
button.primary, .button.primary { background: var(--brand-2); border-color: var(--brand-2); color: #fff; }
button.primary:hover, .button.primary:hover { background: var(--brand); }
button.danger { background: var(--ko); border-color: var(--ko); color: #fff; }
button.link { background: none; border: none; padding: 0; color: var(--brand-2); text-decoration: underline; }
.danger-text { color: var(--ko) !important; }
button.wide { width: 100%; margin-top: .5rem; }
button.tick { border-radius: 50%; width: 28px; height: 28px; padding: 0; line-height: 1; color: var(--muted); }
button.tick.on { color: var(--ok); border-color: var(--ok); background: #e9f6ef; }
form.inline { display: inline; }

.filters { display: flex; gap: .5rem; flex-wrap: wrap; align-items: flex-end; margin-bottom: 1rem; }
.filters label { flex-direction: row; align-items: center; }
.form-grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(300px, 1fr)); gap: 1rem 1.5rem; }
.form-grid.narrow { max-width: 560px; grid-template-columns: 1fr; }
.form-grid fieldset { border: 1px solid var(--line); border-radius: var(--radius); padding: .8rem 1rem 1rem; display: flex; flex-direction: column; gap: .6rem; margin: 0; }
.form-grid legend { font-weight: 600; padding: 0 .3rem; }
.form-grid .full { grid-column: 1 / -1; }
.row-form { display: flex; flex-wrap: wrap; gap: .6rem; align-items: flex-end; }
.row-form .grow { flex: 1 1 220px; }
.row-form .grow input { width: 100%; }
.danger-zone { border-color: #f1c4c0 !important; background: #fffafa; }
.totals { font-weight: 600; color: var(--brand); }
.tabs { display: flex; gap: .3rem; flex-wrap: wrap; margin-bottom: 1rem; border-bottom: 1px solid var(--line); }
.tabs a { padding: .45rem .8rem; text-decoration: none; color: var(--muted); border-bottom: 2px solid transparent; }
.tabs a.active { color: var(--brand); border-color: var(--brand); font-weight: 600; }
.kv { display: grid; grid-template-columns: 140px 1fr; gap: .35rem .8rem; margin: 0; }
.kv dt { color: var(--muted); font-size: .88rem; }
.kv dd { margin: 0; }
.big { font-size: 1.35rem; font-weight: 700; font-variant-numeric: tabular-nums; }
.note { background: #fffbe8; border-left: 3px solid #e6c34a; padding: .5rem .8rem; }
.codes { columns: 2; list-style: none; padding: 0; font-size: 1.15rem; }
.codes li { margin: .3rem 0; }
.steps li { margin-bottom: .8rem; }
.qr canvas { margin: .6rem 0; border: 1px solid var(--line); border-radius: 6px; }
.secret { letter-spacing: .08em; }
.code-input { font-size: 1.4rem; letter-spacing: .25em; text-align: center; }

.chart { width: 100%; height: auto; }
.chart .grid { stroke: #e8ebf0; }
.chart .axis { font-size: 11px; fill: var(--muted); }
.chart .line { fill: none; stroke: var(--brand-2); stroke-width: 2; }
.chart .dot { fill: var(--brand-2); }
.chart .threshold { stroke: var(--ko); stroke-dasharray: 6 4; }
.chart .threshold-label { font-size: 11px; fill: var(--ko); }

body.public { display: flex; align-items: center; justify-content: center; min-height: 100vh; background: linear-gradient(160deg, #1f4e79, #2a6ba3); }
.login-card { background: #fff; width: 100%; max-width: 420px; padding: 2rem 2.2rem; border-radius: 12px; box-shadow: 0 12px 40px rgba(0,0,0,.2); }
.login-card h1 { color: var(--brand); text-align: center; }
.login-card form { display: flex; flex-direction: column; gap: .8rem; }
.login-card input { width: 100%; }

@media (max-width: 860px) {
  .shell { flex-direction: column; }
  .side { width: auto; display: flex; flex-wrap: wrap; border-right: none; border-bottom: 1px solid var(--line); }
  .side .group { width: 100%; }
  main { padding: 1rem; }
}
@media print {
  .topbar, .side, .foot, .filters, button, .button { display: none !important; }
  main { padding: 0; }
  .card { border: none; }
}

/* Aperçu de document (facture) à l'écran */
.invoice { max-width: 820px; }
.invoice .doc-head { display: flex; justify-content: space-between; gap: 2rem; margin-bottom: 1.5rem; }
.invoice .doc-head .addr { min-width: 260px; padding-top: 2.5rem; }
.invoice table { width: 100%; border-collapse: collapse; margin: 1rem 0; }
.invoice th, .invoice td { padding: .45rem .6rem; border-bottom: 1px solid var(--line); text-align: left; }
.invoice thead th { border-bottom: 2px solid var(--ink); }
.invoice tfoot th { text-align: left; }
.invoice tr.grand th { font-size: 1.1rem; border-top: 2px solid var(--ink); }
.narrow-block { max-width: 560px; }
.narrow-block form { display: flex; flex-direction: column; gap: .8rem; }
