:root {
  --bg: #f5f5f3;
  --surface: #ffffff;
  --surface-2: #fafaf8;
  --border: #e2e2de;
  --text: #1d1d1b;
  --text-2: #5c5c58;
  --muted: #8a8a85;
  --accent: #2f6fb0;
  --accent-ink: #ffffff;
  --accent-soft: #e8f0f9;
  --good: #2e7d46;
  --good-soft: #e5f3e9;
  --warn: #9a6200;
  --warn-soft: #fbf0dc;
  --bad: #b3261e;
  --bad-soft: #fbe7e5;
  --chart: #2f6fb0;
  --radius: 8px;
  --shadow: 0 1px 2px rgba(0, 0, 0, .05), 0 2px 8px rgba(0, 0, 0, .04);
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #141413; --surface: #1c1c1b; --surface-2: #232321; --border: #33332f;
    --text: #ececea; --text-2: #b4b4ae; --muted: #85857f;
    --accent: #5a95d0; --accent-ink: #0f1a26; --accent-soft: #1d2b3a;
    --good: #6cc38a; --good-soft: #1b2e22; --warn: #e0a84a; --warn-soft: #33281a; --bad: #ef7d74; --bad-soft: #3a1f1d;
    --chart: #5a95d0; color-scheme: dark;
  }
}
:root[data-theme="dark"] {
  --bg: #141413; --surface: #1c1c1b; --surface-2: #232321; --border: #33332f;
  --text: #ececea; --text-2: #b4b4ae; --muted: #85857f;
  --accent: #5a95d0; --accent-ink: #0f1a26; --accent-soft: #1d2b3a;
  --good: #6cc38a; --good-soft: #1b2e22; --warn: #e0a84a; --warn-soft: #33281a; --bad: #ef7d74; --bad-soft: #3a1f1d;
  --chart: #5a95d0; color-scheme: dark;
}

* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; }
body { font: 14px/1.45 system-ui, "Segoe UI", Roboto, sans-serif; background: var(--bg); color: var(--text); }
a { color: var(--accent); text-decoration: none; }
a:hover { text-decoration: underline; }
h1 { font-size: 20px; margin: 0; font-weight: 650; }
h2 { font-size: 15px; margin: 0 0 12px; font-weight: 650; }
.tnum, td.r, .r { font-variant-numeric: tabular-nums; }
.r { text-align: right; }
.muted { color: var(--muted); }
.small { font-size: 12px; }
.hidden { display: none !important; }

/* ---------- Layout ---------- */
.app { display: grid; grid-template-columns: 220px 1fr; min-height: 100vh; }
.sidebar { background: var(--surface); border-right: 1px solid var(--border); padding: 16px 10px; position: sticky; top: 0; height: 100vh; overflow-y: auto; }
.brand { display: flex; align-items: center; gap: 8px; font-weight: 750; font-size: 17px; padding: 4px 10px 16px; }
.brand .logo { width: 28px; height: 28px; border-radius: 7px; background: var(--accent); color: var(--accent-ink); display: grid; place-items: center; font-size: 14px; }
.nav-group { font-size: 11px; text-transform: uppercase; letter-spacing: .06em; color: var(--muted); padding: 14px 10px 4px; }
.nav a { display: flex; align-items: center; justify-content: space-between; padding: 7px 10px; border-radius: 6px; color: var(--text-2); }
.nav a:hover { background: var(--surface-2); text-decoration: none; color: var(--text); }
.nav a.active { background: var(--accent-soft); color: var(--accent); font-weight: 600; }
.nav .badge { font-size: 11px; background: var(--bad-soft); color: var(--bad); padding: 0 6px; border-radius: 10px; }
.main { padding: 22px 28px 60px; min-width: 0; }
.topbar { display: none; }
.page-head { display: flex; align-items: center; justify-content: space-between; gap: 12px; margin-bottom: 18px; flex-wrap: wrap; }
.page-head .actions { display: flex; gap: 8px; flex-wrap: wrap; }

@media (max-width: 860px) {
  .app { grid-template-columns: 1fr; }
  .sidebar { position: fixed; inset: 0 auto 0 0; width: 250px; z-index: 50; transform: translateX(-100%); transition: transform .2s; }
  .app.nav-open .sidebar { transform: none; box-shadow: 0 0 40px rgba(0,0,0,.3); }
  .topbar { display: flex; align-items: center; gap: 10px; padding: 10px 16px; background: var(--surface); border-bottom: 1px solid var(--border); position: sticky; top: 0; z-index: 40; }
  .main { padding: 16px 16px 60px; }
}

/* ---------- Components ---------- */
.card { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); padding: 16px; box-shadow: var(--shadow); }
.grid { display: grid; gap: 16px; }
.grid.kpis { grid-template-columns: repeat(auto-fit, minmax(180px, 1fr)); }
.grid.two { grid-template-columns: repeat(auto-fit, minmax(320px, 1fr)); }
.kpi .label { color: var(--text-2); font-size: 12px; }
.kpi .value { font-size: 24px; font-weight: 700; margin-top: 4px; font-variant-numeric: tabular-nums; }
.kpi .sub { color: var(--muted); font-size: 12px; margin-top: 2px; }

button, .btn { font: inherit; display: inline-flex; align-items: center; gap: 6px; padding: 7px 14px; border-radius: 6px; border: 1px solid var(--border);
  background: var(--surface); color: var(--text); cursor: pointer; white-space: nowrap; }
button:hover, .btn:hover { background: var(--surface-2); text-decoration: none; }
button.primary, .btn.primary { background: var(--accent); border-color: var(--accent); color: var(--accent-ink); font-weight: 600; }
button.primary:hover { filter: brightness(1.08); }
button.danger { color: var(--bad); }
button.ghost { border-color: transparent; background: transparent; }
button.sm { padding: 3px 8px; font-size: 12px; }
button:disabled { opacity: .5; cursor: not-allowed; }
button:focus-visible, a:focus-visible, input:focus-visible, select:focus-visible, textarea:focus-visible { outline: 2px solid var(--accent); outline-offset: 1px; }

input, select, textarea { font: inherit; color: var(--text); background: var(--surface); border: 1px solid var(--border); border-radius: 6px; padding: 6px 9px; width: 100%; min-width: 0; }
textarea { resize: vertical; min-height: 60px; }
input[type="checkbox"] { width: auto; }
label.field { display: flex; flex-direction: column; gap: 4px; font-size: 12px; color: var(--text-2); }
label.check { display: flex; align-items: center; gap: 8px; font-size: 13px; }
.form-grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(200px, 1fr)); gap: 12px 14px; }
.form-grid .span2 { grid-column: span 2; }
.form-grid .full { grid-column: 1 / -1; }
@media (max-width: 560px) { .form-grid .span2 { grid-column: auto; } }
fieldset { border: 1px solid var(--border); border-radius: var(--radius); padding: 12px 14px 14px; margin: 0 0 16px; }
legend { font-weight: 600; padding: 0 6px; }

.toolbar { display: flex; gap: 8px; align-items: center; flex-wrap: wrap; margin-bottom: 12px; }
.toolbar input[type="search"] { max-width: 280px; }
.toolbar select, .toolbar input[type="date"] { width: auto; }
.tabs { display: flex; gap: 4px; border-bottom: 1px solid var(--border); margin-bottom: 14px; overflow-x: auto; }
.tabs button { border: none; background: none; border-bottom: 2px solid transparent; border-radius: 0; padding: 8px 12px; color: var(--text-2); }
.tabs button.active { border-bottom-color: var(--accent); color: var(--accent); font-weight: 600; }

.table-wrap { overflow-x: auto; background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); }
table.list { width: 100%; border-collapse: collapse; }
table.list th { text-align: left; font-size: 12px; font-weight: 600; color: var(--text-2); background: var(--surface-2); padding: 8px 10px; border-bottom: 1px solid var(--border); white-space: nowrap; }
table.list th.r { text-align: right; }
table.list td { padding: 8px 10px; border-bottom: 1px solid var(--border); vertical-align: middle; }
table.list tr:last-child td { border-bottom: none; }
table.list tbody tr.click { cursor: pointer; }
table.list tbody tr.click:hover td { background: var(--surface-2); }
table.list tfoot td { font-weight: 650; background: var(--surface-2); border-top: 1px solid var(--border); }
.empty { padding: 32px; text-align: center; color: var(--muted); }

.pill { display: inline-flex; align-items: center; gap: 5px; font-size: 12px; padding: 1px 8px; border-radius: 10px; background: var(--surface-2); color: var(--text-2); border: 1px solid var(--border); white-space: nowrap; }
.pill.good { background: var(--good-soft); color: var(--good); border-color: transparent; }
.pill.warn { background: var(--warn-soft); color: var(--warn); border-color: transparent; }
.pill.bad { background: var(--bad-soft); color: var(--bad); border-color: transparent; }
.pill.info { background: var(--accent-soft); color: var(--accent); border-color: transparent; }

/* ---------- Document editor ---------- */
.doc-layout { display: grid; grid-template-columns: 1fr 300px; gap: 16px; align-items: start; }
.doc-layout > *, .grid > * { min-width: 0; }
@media (max-width: 1100px) { .doc-layout { grid-template-columns: 1fr; } }
.lines-table { width: 100%; min-width: 860px; border-collapse: collapse; }
.lines-table .w-label { min-width: 220px; }
.lines-table th { font-size: 12px; color: var(--text-2); text-align: left; padding: 6px 4px; border-bottom: 1px solid var(--border); font-weight: 600; }
.lines-table td { padding: 4px; vertical-align: top; border-bottom: 1px solid var(--border); }
.lines-table input, .lines-table textarea, .lines-table select { padding: 5px 6px; }
.lines-table textarea { min-height: 32px; margin-top: 4px; font-size: 12px; }
.lines-table .w-ref { width: 110px; } .lines-table .w-qty { width: 72px; } .lines-table .w-unit { width: 70px; }
.lines-table .w-pu { width: 100px; } .lines-table .w-disc { width: 64px; } .lines-table .w-tva { width: 78px; } .lines-table .w-total { width: 100px; }
.lines-table .w-act { width: 70px; white-space: nowrap; }
.lines-table tr.text td, .lines-table tr.subtotal td { background: var(--surface-2); }
.lines-table td.total { text-align: right; padding-top: 10px; font-variant-numeric: tabular-nums; }
.line-actions { display: flex; gap: 6px; margin-top: 10px; flex-wrap: wrap; }
.totals-box table { width: 100%; border-collapse: collapse; }
.totals-box td { padding: 4px 0; }
.totals-box tr.grand td { font-size: 17px; font-weight: 700; border-top: 1px solid var(--border); padding-top: 8px; }
.side-actions { display: flex; flex-direction: column; gap: 8px; }
.side-actions button { justify-content: center; }
.locked-banner { background: var(--warn-soft); color: var(--warn); padding: 8px 12px; border-radius: 6px; margin-bottom: 14px; font-size: 13px; }

/* ---------- Chart ---------- */
.chart { position: relative; }
.chart svg { display: block; width: 100%; height: auto; overflow: visible; }
.chart .grid-line { stroke: var(--border); stroke-width: 1; }
.chart .axis-label { fill: var(--muted); font-size: 11px; }
.chart .bar { fill: var(--chart); }
.chart .hit { fill: transparent; cursor: default; }
.chart .hit:hover + .bar, .chart .bar.hover { filter: brightness(1.15); }
.tooltip { position: absolute; pointer-events: none; background: var(--text); color: var(--bg); font-size: 12px; padding: 5px 8px; border-radius: 6px; white-space: nowrap; transform: translate(-50%, -110%); }

/* ---------- Overlays ---------- */
.modal-back { position: fixed; inset: 0; background: rgba(0, 0, 0, .4); display: grid; place-items: center; z-index: 100; padding: 16px; }
.modal { background: var(--surface); border-radius: 10px; width: min(640px, 100%); max-height: 90vh; overflow-y: auto; box-shadow: 0 10px 40px rgba(0,0,0,.25); }
.modal.wide { width: min(900px, 100%); }
.modal header { display: flex; justify-content: space-between; align-items: center; padding: 14px 18px; border-bottom: 1px solid var(--border); }
.modal .body { padding: 16px 18px; }
.modal footer { display: flex; justify-content: flex-end; gap: 8px; padding: 12px 18px; border-top: 1px solid var(--border); }
.toasts { position: fixed; bottom: 16px; right: 16px; display: flex; flex-direction: column; gap: 8px; z-index: 200; }
.toast { background: var(--text); color: var(--bg); padding: 10px 14px; border-radius: 8px; box-shadow: var(--shadow); max-width: 360px; }
.toast.error { background: var(--bad); color: #fff; }

.auth { min-height: 100vh; display: grid; place-items: center; padding: 16px; }
.auth .card { width: min(380px, 100%); }
.auth form { display: flex; flex-direction: column; gap: 12px; }
.error-text { color: var(--bad); font-size: 13px; }
