:root {
  --bg: #f5f3ee; --panel: #ffffff; --ink: #1c2430; --muted: #687384; --line: #e3e1da;
  --accent: #0f6e5a; --accent-soft: #e3f1ec; --accent-ink: #0a4f41;
  --warn: #b7791f; --warn-soft: #fdf3e0; --bad: #b3261e; --bad-soft: #fbe9e7; --ok: #1d7a46; --ok-soft: #e5f4ea;
  --ia: #5b3fb5; --ia-soft: #efeafd; --rules: #1f5fa8; --rules-soft: #e7f0fb; --ref: #6b6457; --ref-soft: #efece5; --human: #0f6e5a;
  --wa: #e7f8dc; --wa-ink: #1b3a12;
  --radius: 12px; --shadow: 0 1px 2px rgba(20, 30, 40, .06), 0 4px 16px rgba(20, 30, 40, .05);
  --font: system-ui, -apple-system, "Segoe UI", Roboto, Ubuntu, "Helvetica Neue", Arial, sans-serif;
  --mono: ui-monospace, "SF Mono", Menlo, Consolas, monospace;
}
* { box-sizing: border-box; }
html, body { margin: 0; background: var(--bg); color: var(--ink); font: 14px/1.5 var(--font); }
.hidden { display: none !important; }
.muted { color: var(--muted); }
.tiny { font-size: 12px; }
.error { color: var(--bad); min-height: 1.2em; }
h1 { font-size: 22px; margin: 0 0 4px; letter-spacing: -.01em; }
h2 { font-size: 16px; margin: 0 0 10px; }
h3 { font-size: 13px; margin: 0 0 8px; text-transform: uppercase; letter-spacing: .06em; color: var(--muted); }
a { color: var(--accent); }
code, .mono { font-family: var(--mono); font-size: 12.5px; }

/* Marque */
.brand { display: flex; gap: 10px; align-items: center; }
.brand .logo { width: 34px; height: 34px; border-radius: 10px; background: var(--accent); color: #fff; display: grid; place-items: center; font-weight: 800; font-size: 18px; }
.brand strong { display: block; font-size: 16px; }
.brand small { display: block; color: var(--muted); font-size: 12px; }
.brand.big .logo { width: 46px; height: 46px; font-size: 24px; border-radius: 13px; }
.brand.big strong { font-size: 22px; }

/* Connexion */
.login { min-height: 100vh; display: grid; place-items: center; padding: 24px; background: radial-gradient(1200px 600px at 20% 0%, #dcefe8 0%, transparent 60%), var(--bg); }
.login-card { width: min(420px, 100%); background: var(--panel); border-radius: 18px; padding: 28px; box-shadow: var(--shadow); display: grid; gap: 14px; }
.login-card label { display: grid; gap: 4px; font-weight: 600; font-size: 13px; }
.quick { display: flex; flex-wrap: wrap; gap: 6px; align-items: center; font-size: 12px; }

/* Barre du haut et navigation */
.topbar { position: sticky; top: 0; z-index: 5; display: flex; justify-content: space-between; align-items: center; padding: 10px 20px; background: var(--panel); border-bottom: 1px solid var(--line); }
.top-info { display: flex; gap: 8px; align-items: center; flex-wrap: wrap; justify-content: flex-end; }
.user { font-weight: 600; font-size: 13px; }
.layout { display: grid; grid-template-columns: 228px 1fr; min-height: calc(100vh - 57px); }
.sidebar { background: var(--panel); border-right: 1px solid var(--line); padding: 14px 10px; display: flex; flex-direction: column; gap: 2px; }
.sidebar a { display: flex; align-items: center; gap: 10px; padding: 9px 12px; border-radius: 10px; color: var(--ink); text-decoration: none; font-weight: 500; }
.sidebar a small { color: var(--muted); font-weight: 400; }
.sidebar a:hover { background: #f1efe9; }
.sidebar a.active { background: var(--accent); color: #fff; }
.sidebar a.active small { color: #d4ece4; }
.sidebar .ico { width: 18px; text-align: center; opacity: .85; }
.side-foot { margin-top: auto; padding: 12px; display: grid; gap: 8px; }
main { padding: 22px 26px 60px; max-width: 1400px; width: 100%; }
.page-head { display: flex; justify-content: space-between; align-items: flex-end; gap: 16px; margin-bottom: 18px; flex-wrap: wrap; }

/* Éléments */
.panel { background: var(--panel); border-radius: var(--radius); box-shadow: var(--shadow); padding: 18px; }
.grid { display: grid; gap: 16px; }
.cols-2 { grid-template-columns: minmax(320px, 5fr) 7fr; }
.cols-3 { grid-template-columns: repeat(3, 1fr); }
.cols-4 { grid-template-columns: repeat(4, 1fr); }
@media (max-width: 1100px) { .cols-2, .cols-3, .cols-4 { grid-template-columns: 1fr; } .layout { grid-template-columns: 1fr; } .sidebar { flex-direction: row; overflow-x: auto; } .side-foot { display: none; } }
.btn { border: 1px solid var(--line); background: var(--panel); color: var(--ink); padding: 8px 14px; border-radius: 9px; font: inherit; font-weight: 600; cursor: pointer; display: inline-flex; gap: 6px; align-items: center; }
.btn:hover { background: #f4f2ec; }
.btn.primary { background: var(--accent); border-color: var(--accent); color: #fff; }
.btn.primary:hover { background: var(--accent-ink); }
.btn.danger { color: var(--bad); border-color: #efc6c2; }
.btn.ghost { background: transparent; }
.btn.small { padding: 5px 10px; font-size: 12.5px; }
.btn:disabled { opacity: .45; cursor: not-allowed; }
.chip { border: 1px solid var(--line); background: #faf9f6; border-radius: 999px; padding: 3px 10px; font: inherit; font-size: 12px; cursor: pointer; }
.pill { display: inline-flex; align-items: center; gap: 6px; padding: 3px 10px; border-radius: 999px; background: #f0eee8; font-size: 12px; color: var(--muted); white-space: nowrap; }
.pill.offline { background: var(--warn-soft); color: var(--warn); font-weight: 600; }
input, select, textarea { font: inherit; border: 1px solid #d6d3ca; border-radius: 8px; padding: 7px 9px; background: #fff; color: var(--ink); width: 100%; }
input:focus, select:focus, textarea:focus { outline: 2px solid #9fd3c4; border-color: var(--accent); }
textarea { min-height: 90px; resize: vertical; }
.switch { display: inline-flex; gap: 6px; align-items: center; font-size: 12.5px; color: var(--muted); }
.switch input { width: auto; }
table { width: 100%; border-collapse: collapse; }
th { text-align: left; font-size: 12px; color: var(--muted); font-weight: 600; padding: 8px; border-bottom: 1px solid var(--line); white-space: nowrap; }
td { padding: 8px; border-bottom: 1px solid #efede7; vertical-align: middle; }
tr.clickable { cursor: pointer; }
tr.clickable:hover td { background: #faf9f5; }
td.num, th.num { text-align: right; font-variant-numeric: tabular-nums; }
.empty { text-align: center; padding: 40px; color: var(--muted); }

/* Statuts */
.st { display: inline-block; padding: 2px 9px; border-radius: 999px; font-size: 11.5px; font-weight: 700; letter-spacing: .02em; }
.st.BROUILLON { background: #eceae4; color: #575245; }
.st.VALIDEE { background: var(--rules-soft); color: var(--rules); }
.st.EMISE { background: var(--ok-soft); color: var(--ok); }
.st.ANNULEE { background: var(--bad-soft); color: var(--bad); text-decoration: line-through; }
.st.SOLDEE { background: var(--ok-soft); color: var(--ok); }
.st.PARTIEL { background: var(--warn-soft); color: var(--warn); }
.st.NON_PAYEE { background: #f1efe9; color: var(--muted); }
.st.AVOIR { background: var(--ia-soft); color: var(--ia); }
.st.ENVOYEE { background: var(--ok-soft); color: var(--ok); }
.st.EN_ATTENTE { background: var(--warn-soft); color: var(--warn); }

/* Origine et confiance */
.orig { display: inline-block; font-size: 10.5px; font-weight: 700; padding: 1px 7px; border-radius: 6px; letter-spacing: .03em; white-space: nowrap; }
.orig.IA { background: var(--ia-soft); color: var(--ia); }
.orig.REGLES { background: var(--rules-soft); color: var(--rules); }
.orig.REFERENTIEL { background: var(--ref-soft); color: var(--ref); }
.orig.HUMAIN { background: var(--accent-soft); color: var(--accent-ink); }
.orig.ABSENT { background: #f3f1ec; color: #9a9384; }
.conf { display: inline-flex; align-items: center; gap: 5px; font-size: 11.5px; color: var(--muted); white-space: nowrap; }
.conf .bar { width: 38px; height: 6px; border-radius: 3px; background: #ebe8e1; overflow: hidden; display: inline-block; }
.conf .bar i { display: block; height: 100%; background: var(--ok); }
.conf.low .bar i { background: var(--warn); }
.conf.low { color: var(--warn); font-weight: 700; }
.w10 i { width: 10%; } .w20 i { width: 20%; } .w30 i { width: 30%; } .w40 i { width: 40%; } .w50 i { width: 50%; }
.w60 i { width: 60%; } .w70 i { width: 70%; } .w80 i { width: 80%; } .w90 i { width: 90%; } .w100 i { width: 100%; }

/* Bulle WhatsApp et surlignage de la source */
.wa-wrap { background: #efeae2; border-radius: var(--radius); padding: 14px; }
.wa { background: var(--wa); color: var(--wa-ink); border-radius: 10px 10px 10px 2px; padding: 10px 12px; white-space: pre-wrap; word-wrap: break-word; box-shadow: 0 1px 1px rgba(0,0,0,.08); font-size: 14.5px; line-height: 1.6; }
.wa.doc { background: #fff; color: var(--ink); border-radius: 10px; margin-top: 10px; font-size: 13px; }
.wa-meta { font-size: 11px; color: #6b7c63; margin-top: 6px; text-align: right; }
mark { background: transparent; border-radius: 4px; padding: 1px 2px; cursor: pointer; transition: background .15s, box-shadow .15s; }
mark.k-client { background: #d9e8fb; }
mark.k-ligne { background: #fff1b8; }
mark.k-cond, mark.k-remise, mark.k-ref, mark.k-montant { background: #e6defb; }
mark.k-implicite, mark.k-signal { background: #ffd8cc; }
mark.k-nonr { background: #ffd8cc; text-decoration: underline wavy var(--bad); }
mark.on { box-shadow: 0 0 0 2px var(--accent); background: #bfe6d8; }
.legend { display: flex; gap: 10px; flex-wrap: wrap; font-size: 11.5px; color: var(--muted); margin-top: 10px; }
.legend span { display: inline-flex; gap: 5px; align-items: center; }
.legend i { width: 12px; height: 12px; border-radius: 3px; display: inline-block; }
.legend .c1 { background: #d9e8fb; } .legend .c2 { background: #fff1b8; } .legend .c3 { background: #e6defb; } .legend .c4 { background: #ffd8cc; }

/* Formulaire de facture */
.fields { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 12px 16px; }
.field { display: grid; gap: 4px; }
.field > span { display: flex; justify-content: space-between; align-items: center; gap: 6px; font-size: 12px; font-weight: 600; color: var(--muted); }
.field .meta { display: inline-flex; gap: 6px; align-items: center; }
.field.hot input, .field.hot select { background: #f3fbf8; }
.lines td { padding: 6px 5px; }
.lines input, .lines select { padding: 6px 7px; }
.lines .q { width: 76px; } .lines .p { width: 110px; } .lines .r { width: 70px; }
.lines tr.hot td { background: #f3fbf8; }
.src-snippet { font-size: 11.5px; color: var(--muted); max-width: 210px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; display: block; }
.totals { display: grid; grid-template-columns: 1fr auto; gap: 4px 18px; margin-left: auto; max-width: 380px; font-variant-numeric: tabular-nums; }
.totals .grand { font-size: 18px; font-weight: 800; padding-top: 6px; border-top: 1px solid var(--line); }
.totals .r { text-align: right; }
.engine-note { font-size: 11.5px; color: var(--muted); display: flex; align-items: center; gap: 6px; }

/* Anomalies */
.anoms { display: grid; gap: 8px; }
.anom { border-radius: 10px; padding: 10px 12px; display: grid; gap: 6px; background: var(--bad-soft); }
.anom.AVERTISSEMENT { background: var(--warn-soft); }
.anom.RESOLUE { background: #f3f2ee; color: var(--muted); }
.anom-head { display: flex; justify-content: space-between; gap: 8px; align-items: center; }
.anom-code { font-weight: 800; font-size: 12px; letter-spacing: .03em; }
.anom.BLOQUANTE .anom-code { color: var(--bad); }
.anom.AVERTISSEMENT .anom-code { color: var(--warn); }
.anom-actions { display: flex; gap: 6px; flex-wrap: wrap; }
.anom-actions input { flex: 1; min-width: 180px; padding: 5px 8px; }
.ok-box { background: var(--ok-soft); color: var(--ok); border-radius: 10px; padding: 12px; font-weight: 600; }

/* Bandeaux */
.banner { border-radius: var(--radius); padding: 14px 18px; display: flex; justify-content: space-between; align-items: center; gap: 12px; flex-wrap: wrap; margin-bottom: 16px; }
.banner.draft { background: #ecebe5; }
.banner.done { background: var(--accent-soft); }
.banner.cancel { background: var(--bad-soft); }
.banner .big { font-size: 20px; font-weight: 800; letter-spacing: -.01em; }
.actions { display: flex; gap: 8px; flex-wrap: wrap; align-items: center; }
.cert { background: #fff8e6; border-radius: 10px; padding: 12px; display: grid; gap: 4px; font-size: 12.5px; }
.cert strong { color: #7a5200; }

/* Boîte de réception */
.inbox { display: grid; grid-template-columns: repeat(auto-fill, minmax(330px, 1fr)); gap: 14px; }
.msg-card { background: var(--panel); border-radius: var(--radius); box-shadow: var(--shadow); padding: 14px; display: grid; gap: 10px; align-content: start; }
.msg-card .wa { font-size: 13px; }
.msg-card .head { display: flex; justify-content: space-between; gap: 8px; align-items: baseline; }
.msg-card .tag { font-size: 11px; font-weight: 700; color: var(--accent-ink); background: var(--accent-soft); padding: 2px 8px; border-radius: 999px; }

/* KPI */
.kpi { background: var(--panel); border-radius: var(--radius); box-shadow: var(--shadow); padding: 16px; }
.kpi .v { font-size: 22px; font-weight: 800; letter-spacing: -.01em; font-variant-numeric: tabular-nums; }
.kpi .l { color: var(--muted); font-size: 12.5px; }
.compare { display: grid; grid-template-columns: 1.4fr 1fr 1fr; gap: 0; }
.compare > div { padding: 10px; border-bottom: 1px solid #efede7; }
.compare .h { font-size: 12px; color: var(--muted); font-weight: 700; }
.compare .best { color: var(--ok); font-weight: 800; }

/* Timeline */
.timeline { display: grid; gap: 8px; }
.tl { display: grid; grid-template-columns: 136px 1fr; gap: 10px; font-size: 12.5px; }
.tl .t { color: var(--muted); font-family: var(--mono); font-size: 11.5px; }
.tl .a { font-weight: 700; }
.tl .d { color: var(--muted); word-break: break-word; }

/* Téléphone simulé */
.phone { background: #efeae2; border-radius: 22px; padding: 14px; display: grid; gap: 10px; max-height: 70vh; overflow: auto; }
.phone .wa { border-radius: 10px 10px 2px 10px; background: #d9fdd3; }
.phone .to { font-size: 11px; color: #6b7c63; }

/* Notifications */
#toasts { position: fixed; bottom: 18px; right: 18px; display: grid; gap: 8px; z-index: 50; }
.toast { background: var(--ink); color: #fff; padding: 10px 14px; border-radius: 10px; box-shadow: var(--shadow); max-width: 440px; font-size: 13px; }
.toast.err { background: var(--bad); }
.toast.ok { background: var(--ok); }
.timer { font-family: var(--mono); font-size: 26px; font-weight: 700; }
.row { display: flex; gap: 10px; align-items: center; flex-wrap: wrap; }
.spacer { flex: 1; }
details summary { cursor: pointer; color: var(--muted); font-size: 12.5px; }

.section-gap { margin-top: 18px; }
.st.nostrike { text-decoration: none; }
.small-select { width: auto; }
.row.tight { gap: 4px; flex-wrap: nowrap; }
td.num { white-space: nowrap; }
.row > input, .row > select { width: auto; flex: 1 1 150px; }
.row > .switch { flex: 0 0 auto; }

.thumb { width: 100%; max-height: 230px; object-fit: cover; object-position: top; border-radius: 8px; background: #eee; }
.src-img { max-width: 100%; max-height: 520px; border-radius: 8px; display: block; }
audio { width: 100%; }
.wa.doc { tab-size: 4; }
.chip.low { background: #ffe3d9; border-color: #f3b6a3; cursor: default; }
.pill.tiny { font-size: 10.5px; padding: 1px 7px; }

.scroll { max-height: 360px; overflow: auto; }
pre.scroll { background: #f6f5f1; padding: 8px; border-radius: 8px; white-space: pre-wrap; }
#grp input { padding: 5px 7px; }
input[type=color] { height: 36px; padding: 2px; }
