:root {
  --navy: #0f4c81;
  --navy-dark: #0b3a63;
  --green: #3f9b47;
  --green-dark: #2f7a36;
  --red: #c0392b;
  --amber: #b9770e;
  --ink: #1d2733;
  --muted: #667085;
  --line: #d9dee5;
  --bg: #f3f5f8;
  --card: #fff;
  --radius: 8px;
}
* { box-sizing: border-box; }
html { font-size: 15px; }
body { margin: 0; background: var(--bg); color: var(--ink); font-family: "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif; line-height: 1.45; }
a { color: var(--navy); }
h1 { font-size: 1.5rem; margin: 0; }
h3 { font-size: 1.05rem; margin: 1.2em 0 .6em; color: var(--navy-dark); }
h3:first-child { margin-top: 0; }
code { background: #eef2f7; padding: 1px 5px; border-radius: 4px; }
.muted { color: var(--muted); }
.small { font-size: .85rem; }
.nowrap { white-space: nowrap; }
.late { color: var(--red); font-weight: 600; }
.ok { color: var(--green-dark); }

/* ---------- Barre du haut ---------- */
.topbar { background: var(--navy); color: #fff; display: flex; align-items: center; gap: 24px; padding: 0 20px; min-height: 56px; flex-wrap: wrap; }
.topbar .brand { color: #fff; text-decoration: none; display: flex; align-items: center; gap: 8px; font-size: 1.05rem; }
.topbar .brand img { width: 34px; height: 34px; background: #fff; border-radius: 6px; padding: 2px; }
.topbar nav { display: flex; gap: 2px; flex: 1; flex-wrap: wrap; }
.topbar nav a { color: #dce8f5; text-decoration: none; padding: 8px 11px; border-radius: 6px; font-size: .93rem; }
.topbar nav a:hover { background: rgba(255,255,255,.1); color: #fff; }
.topbar nav a.active { background: #fff; color: var(--navy); font-weight: 600; }
.topbar .who { display: flex; gap: 14px; align-items: center; font-size: .88rem; }
.topbar .who a, .topbar .who .linklike { color: #dce8f5; }

.container { max-width: 1280px; margin: 0 auto; padding: 22px 20px 60px; }
.page-head { display: flex; align-items: center; justify-content: space-between; gap: 12px; margin-bottom: 16px; flex-wrap: wrap; }
.actions { display: flex; gap: 8px; flex-wrap: wrap; align-items: center; }

/* ---------- Cartes, formulaires ---------- */
.card { background: var(--card); border: 1px solid var(--line); border-radius: var(--radius); padding: 18px 20px; margin-bottom: 16px; }
.card.flush { padding: 0; overflow: hidden; }
.card.narrow { max-width: 480px; }
.card .pad { padding: 14px 16px 0; margin: 0 0 8px; }
.card-head { display: flex; justify-content: space-between; align-items: center; gap: 12px; flex-wrap: wrap; margin-bottom: 8px; }
.card-head h3 { margin: 0; }
.grid2 { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 12px 18px; }
.grid3 { display: grid; grid-template-columns: repeat(3, minmax(0, 1fr)); gap: 12px 18px; }
.grid2.top { align-items: start; }
.span2 { grid-column: span 2; }
label { display: block; font-size: .87rem; color: #344054; font-weight: 500; }
label small { font-weight: 400; color: var(--muted); }
input, select, textarea { display: block; width: 100%; margin-top: 4px; padding: 8px 10px; border: 1px solid #c4ccd6; border-radius: 6px; font: inherit; font-size: .95rem; background: #fff; color: var(--ink); }
input:focus, select:focus, textarea:focus { outline: 2px solid rgba(15,76,129,.25); border-color: var(--navy); }
input[type=checkbox] { display: inline; width: auto; margin: 0 6px 0 0; }
input[type=file] { padding: 6px; }
fieldset { border: 0; padding: 0; margin: 0; }
label.check { display: flex; align-items: center; font-weight: 400; margin: 8px 0; }
.form-actions { display: flex; gap: 10px; margin-top: 16px; flex-wrap: wrap; }
.form-actions.sticky { position: sticky; bottom: 0; background: var(--bg); padding: 12px 0; border-top: 1px solid var(--line); z-index: 5; }
.inline { display: inline; }
.inline-form { display: flex; flex-wrap: wrap; gap: 10px; align-items: flex-end; }
.inline-form label { flex: 0 1 auto; }
.inline-form label.grow { flex: 1 1 220px; }
.filters { display: flex; gap: 8px; flex-wrap: wrap; margin-bottom: 14px; align-items: center; }
.filters input, .filters select { width: auto; margin: 0; min-width: 150px; }
.filters label.check { margin: 0; }

.btn { display: inline-flex; align-items: center; gap: 6px; padding: 8px 14px; border: 1px solid #c4ccd6; background: #fff; color: var(--ink); border-radius: 6px; font: inherit; font-size: .92rem; font-weight: 500; cursor: pointer; text-decoration: none; white-space: nowrap; }
.btn:hover { background: #f2f4f7; }
.btn.primary { background: var(--navy); border-color: var(--navy); color: #fff; }
.btn.primary:hover { background: var(--navy-dark); }
.btn.success { background: var(--green); border-color: var(--green); color: #fff; }
.btn.success:hover { background: var(--green-dark); }
.btn.danger { color: var(--red); border-color: #e5b4ae; }
.btn.danger:hover { background: #fdf0ee; }
.btn.small { padding: 5px 10px; font-size: .85rem; }
.btn.block { width: 100%; justify-content: center; margin-top: 10px; }
.linklike { background: none; border: 0; padding: 0; font: inherit; color: var(--navy); cursor: pointer; text-decoration: underline; }
.linklike.danger, .danger { color: var(--red); }

.flash { padding: 10px 14px; border-radius: 6px; margin-bottom: 12px; border: 1px solid; }
.flash-ok { background: #ecf8ee; border-color: #b7e0bd; color: #1e5b26; }
.flash-error { background: #fdeceb; border-color: #f2b8b2; color: #8c2219; }
.flash-warn { background: #fff7e6; border-color: #f5d48f; color: #7a5300; }
.flash-info { background: #eaf2fb; border-color: #b8d2ee; color: #123e66; }

/* ---------- Listes ---------- */
table.list { width: 100%; border-collapse: collapse; font-size: .92rem; }
table.list th { text-align: left; font-weight: 600; color: #475467; background: #f8fafc; border-bottom: 1px solid var(--line); padding: 9px 12px; font-size: .83rem; text-transform: uppercase; letter-spacing: .02em; }
table.list td { padding: 9px 12px; border-bottom: 1px solid #edf0f4; vertical-align: top; }
table.list tbody tr:hover { background: #fafbfd; }
table.list tfoot td { font-weight: 700; background: #f8fafc; padding: 10px 12px; }
table.list.compact td, table.list.compact th { padding: 6px 10px; }
table.list .num, .num { text-align: right; font-variant-numeric: tabular-nums; }
table.list td.empty { text-align: center; color: var(--muted); padding: 24px; }
tr.inactive td { color: #98a2b3; }
.row-actions { white-space: nowrap; text-align: right; }
.row-actions > * { margin-left: 10px; }

.badge { display: inline-block; padding: 2px 9px; border-radius: 99px; font-size: .78rem; font-weight: 600; white-space: nowrap; vertical-align: middle; }
.st-draft { background: #eef0f3; color: #475467; }
.st-info { background: #e3eefa; color: #0f4c81; }
.st-ok { background: #e3f4e5; color: #23692b; }
.st-warn { background: #fff1d6; color: #8a5a00; }
.st-bad { background: #fde4e1; color: #a12a1d; }
.st-muted { background: #f0f0f0; color: #777; }

.kpis { display: grid; grid-template-columns: repeat(5, minmax(0, 1fr)); gap: 12px; margin-bottom: 16px; }
.kpi { background: #fff; border: 1px solid var(--line); border-radius: var(--radius); padding: 14px 16px; }
.kpi span { display: block; font-size: .8rem; color: var(--muted); text-transform: uppercase; letter-spacing: .02em; }
.kpi b { display: block; font-size: 1.45rem; color: var(--navy-dark); margin: 4px 0 0; font-variant-numeric: tabular-nums; }
.kpi small { color: var(--muted); }
.kpi.alert b { color: var(--red); }
.roles { margin: 0; padding-left: 18px; }
.preview-img { max-height: 110px; max-width: 220px; display: block; margin-bottom: 8px; border: 1px solid var(--line); border-radius: 6px; padding: 4px; background: #fff; }

/* ---------- Connexion ---------- */
.login-box { max-width: 380px; margin: 8vh auto; background: #fff; border: 1px solid var(--line); border-radius: 12px; padding: 28px; text-align: left; }
.login-box.wide { max-width: 480px; }
.login-box h1 { text-align: center; color: var(--navy); margin-bottom: 16px; }
.login-box label { margin-bottom: 12px; }
.login-logo { display: block; width: 110px; margin: 0 auto 8px; }

/* ---------- Éditeur ---------- */
.table-scroll { overflow-x: auto; }
table.editor { width: 100%; border-collapse: collapse; min-width: 760px; }
table.editor th { font-size: .8rem; text-transform: uppercase; color: #475467; text-align: left; padding: 6px 4px; border-bottom: 1px solid var(--line); }
table.editor th.num { text-align: right; }
table.editor td { padding: 3px 4px; border-bottom: 1px solid #f0f2f5; vertical-align: middle; }
table.editor input { margin: 0; padding: 6px 8px; }
table.editor input.num { text-align: right; width: 100%; }
table.editor td:nth-child(3), table.editor td:nth-child(4) { width: 80px; }
table.editor td:nth-child(5) { width: 130px; }
table.editor td:nth-child(6) { width: 80px; }
table.editor td:nth-child(7) { width: 130px; }
table.editor .f-amount { padding-right: 8px; font-variant-numeric: tabular-nums; }
table.editor .strong { font-weight: 700; }
table.editor tr.row-section td { background: #eef3f9; }
table.editor tr.row-section input { font-weight: 700; background: transparent; border-color: transparent; }
table.editor tr.row-subtotal td { background: #f6f7f9; font-weight: 600; }
table.editor tr.row-subtotal input { font-weight: 700; }
.handle { width: 30px; white-space: nowrap; }
.ico { background: none; border: 0; cursor: pointer; color: #98a2b3; font-size: .75rem; padding: 2px 3px; line-height: 1; }
.ico:hover { color: var(--navy); }
.ico.danger:hover { color: var(--red); }
.editor-actions { display: flex; gap: 8px; align-items: center; margin-top: 10px; flex-wrap: wrap; }
.options { display: flex; gap: 16px; }
.options label.check { margin: 0; }
.labels-box { margin-bottom: 10px; font-size: .88rem; }
.labels-box summary { cursor: pointer; color: var(--navy); margin-bottom: 8px; }
.fx-row { display: flex; gap: 10px; align-items: flex-end; margin-top: 10px; }
.fx-row label.grow { flex: 1; }
.fx-row .btn { margin-bottom: 1px; }
#fx-info { margin: 6px 0 0; }
.detail-row { display: grid; grid-template-columns: 1fr 2fr 30px; gap: 8px; margin-bottom: 6px; align-items: center; }
.detail-row input { margin: 0; }
table.totals { width: 100%; border-collapse: collapse; margin-top: 10px; }
table.totals th { text-align: left; font-weight: 500; padding: 5px 0; color: #475467; }
table.totals td { text-align: right; padding: 5px 0; font-variant-numeric: tabular-nums; }
table.totals tr.grand th, table.totals tr.grand td { font-weight: 700; font-size: 1.1rem; color: var(--navy-dark); border-top: 2px solid var(--navy); padding-top: 8px; }
.in-words { font-size: .82rem; color: var(--muted); margin: 8px 0 0; }

/* ---------- Visualisation d'un document ---------- */
.doc-toolbar { display: flex; justify-content: space-between; gap: 16px; align-items: flex-start; margin-bottom: 14px; flex-wrap: wrap; }
.doc-toolbar h1 { margin: 4px 0; }

/* Feuille A4 */
.sheet { position: relative; width: 210mm; min-height: 297mm; margin: 0 auto 30px; background: #fff; padding: 12mm 13mm 10mm; box-shadow: 0 2px 14px rgba(16,24,40,.12); font-family: Calibri, "Segoe UI", Arial, sans-serif; font-size: 9.5pt; color: #111; line-height: 1.3; display: flex; flex-direction: column; }
.sheet .watermark { position: absolute; top: 40%; left: 0; right: 0; text-align: center; font-size: 80pt; font-weight: 800; color: rgba(192,57,43,.08); transform: rotate(-28deg); pointer-events: none; letter-spacing: 8px; }
.sh-head { display: grid; grid-template-columns: 34mm 1fr 50mm; align-items: center; gap: 4mm; }
.sh-logo { width: 32mm; }
.sh-tagline { text-align: center; font-weight: 700; font-size: 8.5pt; color: var(--navy-dark); line-height: 1.4; }
.sh-date { text-align: right; font-weight: 600; align-self: end; }
.sh-parties { display: grid; grid-template-columns: 1fr 1fr; gap: 6mm; margin-top: 6mm; }
.sh-title { font-size: 17pt; font-weight: 800; color: var(--navy); letter-spacing: .5px; margin-bottom: 2mm; }
.sh-docinfo table { border-collapse: collapse; }
.sh-docinfo th { text-align: left; font-weight: 600; color: #444; padding: 1px 4mm 1px 0; white-space: nowrap; }
.sh-docinfo td { padding: 1px 0; }
.sh-client { border: 1px solid #222; padding: 3mm 4mm; }
.sh-label { font-size: 8pt; text-transform: uppercase; letter-spacing: .5px; color: #555; font-weight: 700; }
.sh-client-name { font-weight: 800; font-size: 11pt; margin: 1mm 0; }
.sh-object { margin-top: 4mm; padding: 2mm 3mm; background: #f2f6fa; border-left: 3px solid var(--navy); }
.sh-details { width: 100%; border-collapse: collapse; margin-top: 3mm; font-size: 8.8pt; }
.sh-details th, .sh-details td { border: 1px solid #9aa4b0; padding: 1.2mm 2mm; text-align: left; }
.sh-details th { background: #eef2f6; font-weight: 600; width: 22%; }
.sh-details td { width: 28%; }
.sh-lines { width: 100%; border-collapse: collapse; margin-top: 4mm; font-size: 8.8pt; }
.sh-lines th { background: #e8eef5; border: 1px solid #555; padding: 1.5mm 1.5mm; font-weight: 700; text-align: center; vertical-align: middle; }
.sh-lines td { border-left: 1px solid #555; border-right: 1px solid #555; border-bottom: 1px solid #c9ced4; padding: .8mm 1.5mm; text-align: center; font-variant-numeric: tabular-nums; }
.sh-lines tbody tr:last-child td { border-bottom: 1px solid #555; }
.sh-lines td.l, .sh-lines th.l { text-align: left; }
.sh-lines td.n { text-align: right; white-space: nowrap; }
.sh-lines tr.section td { font-weight: 700; text-align: left; background: #f4f6f9; }
.sh-lines tr.subtotal td { font-weight: 700; background: #f4f6f9; border-bottom: 1px solid #555; }
.sh-lines th:first-child { width: auto; }
.sh-bottom { display: grid; grid-template-columns: 1fr auto; gap: 6mm; margin-top: 3mm; align-items: start; }
.sh-words { margin: 0 0 2mm; }
.sh-exempt { margin: 0 0 2mm; padding: 1.5mm 2.5mm; border: 1px dashed #888; font-size: 8.5pt; }
.sh-small { font-size: 7.8pt; color: #555; margin: 0 0 2mm; }
.sh-bank { font-size: 8.5pt; }
.sh-totals { border-collapse: collapse; min-width: 72mm; font-variant-numeric: tabular-nums; }
.sh-totals th, .sh-totals td { border: 1px solid #555; padding: 1.2mm 2.5mm; }
.sh-totals th { text-align: left; font-weight: 600; background: #f4f6f9; white-space: nowrap; }
.sh-totals td { text-align: right; white-space: nowrap; min-width: 26mm; }
.sh-totals tr.hdr th { background: none; border: 0; }
.sh-totals tr.hdr td { text-align: right; font-weight: 700; border: 0; font-size: 8pt; }
.sh-totals tr.grand th, .sh-totals tr.grand td { background: var(--navy); color: #fff; font-weight: 800; font-size: 10pt; }
.sh-notes { margin-top: 3mm; padding: 2mm 3mm; border: 1px solid #ccc; font-size: 8.5pt; }
.sh-sign { display: grid; grid-template-columns: 1fr 1fr; gap: 10mm; margin-top: 5mm; break-inside: avoid; }
.sh-sign > div { text-align: center; }
.sh-sign-area { height: 32mm; display: flex; align-items: center; justify-content: center; }
.sh-sign-area img { max-height: 32mm; max-width: 60mm; }
.sh-sign-area span { color: #999; font-size: 8pt; font-style: italic; }
/* Longues factures : mise en page resserrée pour tenir sur une page */
.sheet.dense { font-size: 8.6pt; }
.sheet.dense .sh-parties { margin-top: 4mm; }
.sheet.dense .sh-lines { font-size: 8pt; margin-top: 3mm; }
.sheet.dense .sh-lines td { padding: .35mm 1.5mm; }
.sheet.dense .sh-sign-area { height: 24mm; }
.sheet.dense .sh-sign-area img { max-height: 24mm; }
.sheet.xdense { padding-top: 7mm; padding-bottom: 6mm; font-size: 8pt; line-height: 1.22; }
.sheet.xdense .sh-logo { width: 21mm; }
.sheet.xdense .sh-head { grid-template-columns: 24mm 1fr 50mm; }
.sheet.xdense .sh-tagline { font-size: 7.6pt; }
.sheet.xdense .sh-parties { margin-top: 2mm; }
.sheet.xdense .sh-title { font-size: 13pt; margin-bottom: .5mm; }
.sheet.xdense .sh-client { padding: 1.5mm 3mm; }
.sheet.xdense .sh-client-name { font-size: 10pt; margin: 0; }
.sheet.xdense .sh-lines { font-size: 7pt; margin-top: 2mm; }
.sheet.xdense .sh-lines td { padding: 0 1.4mm; line-height: 1.16; }
.sheet.xdense .sh-lines th { padding: .8mm 1mm; }
.sheet.xdense .sh-lines tr.section td { line-height: .9; font-size: 6pt; }
.sheet.xdense .sh-object { margin-top: 2mm; padding: .8mm 3mm; }
.sheet.xdense .sh-bottom { margin-top: 1.5mm; }
.sheet.xdense .sh-words, .sheet.xdense .sh-exempt, .sheet.xdense .sh-bank { font-size: 7.4pt; margin-bottom: 1mm; }
.sheet.xdense .sh-exempt { padding: .8mm 2mm; }
.sheet.xdense .sh-small { font-size: 6.8pt; margin-bottom: 1mm; }
.sheet.xdense .sh-totals th, .sheet.xdense .sh-totals td { padding: .7mm 2.5mm; }
.sheet.xdense .sh-sign { margin-top: 1mm; }
.sheet.xdense .sh-sign-area { height: 16mm; }
.sheet.xdense .sh-sign-area img { max-height: 17mm; }
.sheet.xdense .sh-lines td { line-height: 1.1; }
.sheet.xdense .sh-totals th, .sheet.xdense .sh-totals td { padding: .5mm 2.5mm; }
.sheet.xdense .sh-foot { font-size: 6.8pt; padding-top: 1.5mm; }
.sh-foot { margin-top: auto; padding-top: 3mm; border-top: 2px solid var(--green); text-align: center; font-size: 7.6pt; color: #333; line-height: 1.45; }

/* ---------- Écrans étroits ---------- */
@media (max-width: 900px) {
  .grid3, .kpis { grid-template-columns: repeat(2, minmax(0, 1fr)); }
  .sheet { transform-origin: top left; }
}
@media (max-width: 640px) {
  .grid2, .grid3, .kpis { grid-template-columns: 1fr; }
  .span2 { grid-column: auto; }
  .container { padding: 14px 12px 50px; }
  .sheet { width: 100%; min-height: 0; padding: 6mm; }
}

/* ---------- Impression ---------- */
@page { size: A4 portrait; margin: 0; }
@media print {
  html, body { background: #fff; font-size: 12px; }
  .no-print, .topbar, .flash { display: none !important; }
  .container { max-width: none; padding: 0; margin: 0; }
  .sheet { box-shadow: none; margin: 0; width: 210mm; min-height: 296mm; padding: 11mm 12mm 8mm; }
  .sh-lines thead { display: table-header-group; }
  .sh-lines tr, .sh-totals, .sh-bottom { break-inside: avoid; }
  * { -webkit-print-color-adjust: exact; print-color-adjust: exact; }
}
