/* ---------- Thème ---------- */
:root {
  --bg: #f4f6fa; --surface: #ffffff; --surface-2: #f8fafc; --border: #e3e8ef; --text: #0f172a; --muted: #64748b;
  --primary: #2563eb; --primary-hover: #1d4ed8; --primary-soft: #dbeafe; --danger: #dc2626; --danger-soft: #fee2e2;
  --success: #16a34a; --success-soft: #dcfce7; --warning: #d97706; --warning-soft: #fef3c7; --info-soft: #e0f2fe;
  --radius: 12px; --shadow: 0 1px 2px rgba(15, 23, 42, .06), 0 4px 16px rgba(15, 23, 42, .05);
  --sidebar: 240px; --font: "Segoe UI", system-ui, -apple-system, "Noto Sans Arabic", "Noto Sans", Roboto, sans-serif;
}
* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; }
body { font-family: var(--font); background: var(--bg); color: var(--text); font-size: 15.5px; line-height: 1.45; -webkit-font-smoothing: antialiased; }
[dir=rtl] body, body[dir=rtl] { font-size: 16.5px; }
a { color: var(--primary); text-decoration: none; }
button { font: inherit; cursor: pointer; }
input, select, textarea { font: inherit; }
h1, h2, h3 { margin: 0; font-weight: 700; line-height: 1.25; }
h1 { font-size: 1.6rem; } h2 { font-size: 1.2rem; } h3 { font-size: 1.02rem; }
.muted { color: var(--muted); }
.small { font-size: .88rem; }
.splash { display: grid; place-items: center; height: 100vh; color: var(--muted); }

/* ---------- Disposition ---------- */
.layout { display: grid; grid-template-columns: var(--sidebar) 1fr; min-height: 100vh; }
.sidebar { background: var(--surface); border-inline-end: 1px solid var(--border); padding: 18px 14px; display: flex; flex-direction: column; gap: 6px; position: sticky; top: 0; height: 100vh; }
.brand { display: flex; align-items: center; gap: 10px; font-weight: 800; font-size: 1.05rem; padding: 6px 10px 16px; }
.brand .logo { width: 34px; height: 34px; border-radius: 9px; background: var(--primary); color: #fff; display: grid; place-items: center; font-size: 1.1rem; }
.nav a { display: flex; align-items: center; gap: 12px; padding: 11px 12px; border-radius: 10px; color: var(--text); font-weight: 600; font-size: 1rem; }
.nav a:hover { background: var(--surface-2); }
.nav a.active { background: var(--primary-soft); color: var(--primary); }
.nav .ico { width: 24px; text-align: center; font-size: 1.15rem; }
.sidebar .spacer { flex: 1; }
.userbox { border-top: 1px solid var(--border); padding-top: 12px; display: flex; align-items: center; gap: 10px; }
.avatar { width: 36px; height: 36px; border-radius: 50%; background: var(--primary-soft); color: var(--primary); display: grid; place-items: center; font-weight: 700; overflow: hidden; flex-shrink: 0; }
.avatar img { width: 100%; height: 100%; object-fit: cover; }
.userbox .who { flex: 1; min-width: 0; } .userbox .who div { white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.main { min-width: 0; }
.topbar { display: flex; align-items: center; gap: 12px; padding: 14px 28px; background: var(--surface); border-bottom: 1px solid var(--border); position: sticky; top: 0; z-index: 20; }
.topbar .burger { display: none; }
.topbar .grow { flex: 1; }
.content { padding: 24px 28px 60px; max-width: 1280px; }
.page-head { display: flex; align-items: flex-start; justify-content: space-between; gap: 16px; margin-bottom: 20px; flex-wrap: wrap; }
.page-head p { margin: 4px 0 0; color: var(--muted); }
.toolbar { display: flex; gap: 10px; align-items: center; flex-wrap: wrap; margin-bottom: 16px; }
.toolbar .search { flex: 1; min-width: 200px; max-width: 420px; }
.lang-switch { display: flex; border: 1px solid var(--border); border-radius: 999px; overflow: hidden; }
.lang-switch button { border: 0; background: transparent; padding: 6px 12px; font-weight: 600; color: var(--muted); }
.lang-switch button.on { background: var(--primary); color: #fff; }

/* ---------- Composants ---------- */
.btn { display: inline-flex; align-items: center; justify-content: center; gap: 8px; padding: 10px 16px; border-radius: 10px; border: 1px solid var(--border); background: var(--surface); color: var(--text); font-weight: 600; transition: .12s; white-space: nowrap; }
.btn:hover { background: var(--surface-2); }
.btn:disabled { opacity: .5; cursor: not-allowed; }
.btn-primary { background: var(--primary); border-color: var(--primary); color: #fff; }
.btn-primary:hover { background: var(--primary-hover); }
.btn-success { background: var(--success); border-color: var(--success); color: #fff; }
.btn-danger { background: var(--danger-soft); border-color: transparent; color: var(--danger); }
.btn-danger:hover { background: #fecaca; }
.btn-ghost { border-color: transparent; background: transparent; }
.btn-lg { padding: 14px 22px; font-size: 1.05rem; border-radius: 12px; }
.btn-sm { padding: 6px 10px; font-size: .88rem; border-radius: 8px; }
.btn-block { width: 100%; }
.btn-google { background: #fff; border-color: #dadce0; color: #3c4043; }
.card { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); box-shadow: var(--shadow); padding: 20px; }
.card + .card { margin-top: 16px; }
.card h2, .card h3 { margin-bottom: 12px; }
.grid { display: grid; gap: 16px; }
.grid-2 { grid-template-columns: repeat(2, 1fr); }
.grid-3 { grid-template-columns: repeat(3, 1fr); }
.grid-4 { grid-template-columns: repeat(4, 1fr); }
.kpi { padding: 18px 20px; }
.kpi .label { color: var(--muted); font-size: .9rem; font-weight: 600; }
.kpi .value { font-size: 1.7rem; font-weight: 800; margin-top: 4px; letter-spacing: -.02em; }
.kpi.good .value { color: var(--success); } .kpi.warn .value { color: var(--warning); } .kpi.bad .value { color: var(--danger); }
.badge { display: inline-block; padding: 3px 10px; border-radius: 999px; font-size: .8rem; font-weight: 700; background: var(--surface-2); color: var(--muted); white-space: nowrap; }
.badge.draft { background: #f1f5f9; color: #475569; } .badge.sent { background: var(--info-soft); color: #0369a1; }
.badge.accepted, .badge.paid, .badge.active { background: var(--success-soft); color: var(--success); }
.badge.partial, .badge.pending { background: var(--warning-soft); color: var(--warning); }
.badge.overdue, .badge.refused, .badge.cancelled, .badge.blocked { background: var(--danger-soft); color: var(--danger); }
.badge.invoiced { background: var(--primary-soft); color: var(--primary); }
.table { width: 100%; border-collapse: collapse; }
.table th, .table td { padding: 12px 10px; border-bottom: 1px solid var(--border); text-align: start; vertical-align: middle; }
.table th { color: var(--muted); font-size: .82rem; text-transform: uppercase; letter-spacing: .04em; font-weight: 700; }
.table tr.click { cursor: pointer; } .table tr.click:hover { background: var(--surface-2); }
.table td.num, .table th.num { text-align: end; font-variant-numeric: tabular-nums; white-space: nowrap; }
.table .actions { white-space: nowrap; text-align: end; }
.table-wrap { overflow-x: auto; }
.empty { text-align: center; padding: 44px 20px; color: var(--muted); }
.empty .big { font-size: 2.4rem; margin-bottom: 8px; }
.empty h3 { color: var(--text); margin-bottom: 6px; }
.empty .btn { margin-top: 14px; }
.list { display: flex; flex-direction: column; }
.list .row { display: flex; align-items: center; gap: 12px; padding: 10px 0; border-bottom: 1px solid var(--border); }
.list .row:last-child { border-bottom: 0; }
.list .row .grow { flex: 1; min-width: 0; }
.list .row.click { cursor: pointer; } .list .row.click:hover { background: var(--surface-2); margin: 0 -8px; padding-inline: 8px; border-radius: 8px; }
.money { font-variant-numeric: tabular-nums; font-weight: 700; white-space: nowrap; }
.form { display: grid; gap: 14px; }
.form .cols { display: grid; grid-template-columns: 1fr 1fr; gap: 14px; }
.form .cols-3 { display: grid; grid-template-columns: 1fr 1fr 1fr; gap: 14px; }
.field label { display: block; font-weight: 600; margin-bottom: 5px; font-size: .92rem; }
.field .hint { color: var(--muted); font-size: .82rem; margin-top: 4px; }
.input, .field input:not([type=checkbox]), .field select, .field textarea { width: 100%; padding: 10px 12px; border: 1px solid var(--border); border-radius: 10px; background: #fff; color: var(--text); outline: 0; transition: .12s; }
.input:focus, .field input:focus, .field select:focus, .field textarea:focus { border-color: var(--primary); box-shadow: 0 0 0 3px var(--primary-soft); }
.field textarea { min-height: 80px; resize: vertical; }
.check { display: flex; align-items: center; gap: 10px; font-weight: 600; cursor: pointer; }
.check input { width: 20px; height: 20px; accent-color: var(--primary); }
.notice { padding: 12px 14px; border-radius: 10px; background: var(--info-soft); color: #0c4a6e; font-size: .92rem; }
.notice.warn { background: var(--warning-soft); color: #78350f; }
.notice.danger { background: var(--danger-soft); color: #7f1d1d; }
.notice.ok { background: var(--success-soft); color: #14532d; }
.quick { display: grid; grid-template-columns: repeat(auto-fill, minmax(170px, 1fr)); gap: 12px; }
.quick button { display: flex; flex-direction: column; align-items: center; gap: 8px; padding: 18px 10px; border-radius: 12px; border: 1px solid var(--border); background: var(--surface); font-weight: 700; transition: .12s; }
.quick button:hover { border-color: var(--primary); background: var(--primary-soft); color: var(--primary); }
.quick button .ico { font-size: 1.7rem; }
.chips { display: flex; gap: 8px; flex-wrap: wrap; }
.chip { padding: 7px 14px; border-radius: 999px; border: 1px solid var(--border); background: var(--surface); font-weight: 600; font-size: .9rem; }
.chip.on { background: var(--text); color: #fff; border-color: var(--text); }
.chart { display: flex; align-items: flex-end; gap: 6px; height: 180px; padding-top: 10px; }
.chart .col { flex: 1; display: flex; flex-direction: column; justify-content: flex-end; align-items: center; gap: 4px; height: 100%; }
.chart .bars { display: flex; align-items: flex-end; gap: 2px; height: 100%; width: 100%; justify-content: center; }
.chart .bar { width: 30%; border-radius: 3px 3px 0 0; min-height: 2px; }
.chart .b-inv { background: var(--primary); } .chart .b-cash { background: var(--success); } .chart .b-exp { background: #f59e0b; }
.chart .lbl { font-size: .72rem; color: var(--muted); }
.legend { display: flex; gap: 16px; font-size: .85rem; color: var(--muted); margin-top: 8px; }
.legend i { display: inline-block; width: 12px; height: 12px; border-radius: 3px; margin-inline-end: 5px; vertical-align: -1px; }
.stock-bar { height: 8px; background: var(--surface-2); border-radius: 4px; overflow: hidden; }
.stock-bar i { display: block; height: 100%; background: var(--success); }
.stock-bar.low i { background: var(--warning); } .stock-bar.out i { background: var(--danger); }

/* ---------- Éditeur de document ---------- */
.doc-lines { width: 100%; border-collapse: collapse; }
.doc-lines th { text-align: start; color: var(--muted); font-size: .8rem; text-transform: uppercase; padding: 8px 6px; }
.doc-lines td { padding: 6px; vertical-align: top; }
.doc-lines input, .doc-lines select { width: 100%; padding: 9px 10px; border: 1px solid var(--border); border-radius: 8px; }
.doc-lines .num input { text-align: end; }
.doc-lines .rm { border: 0; background: transparent; color: var(--muted); font-size: 1.1rem; padding: 8px; }
.doc-lines .rm:hover { color: var(--danger); }
.totals { margin-inline-start: auto; width: 320px; max-width: 100%; }
.totals .row { display: flex; justify-content: space-between; padding: 8px 0; border-bottom: 1px solid var(--border); }
.totals .row.grand { font-size: 1.25rem; font-weight: 800; border-bottom: 0; }
.doc-actions { display: flex; gap: 10px; flex-wrap: wrap; }
.status-strip { display: flex; gap: 6px; align-items: center; flex-wrap: wrap; }

/* ---------- Modales & toasts ---------- */
.modal-bg { position: fixed; inset: 0; background: rgba(15, 23, 42, .45); display: grid; place-items: center; z-index: 50; padding: 16px; animation: fade .15s; }
.modal { background: var(--surface); border-radius: 16px; width: 100%; max-width: 560px; max-height: 92vh; overflow: auto; box-shadow: 0 20px 60px rgba(0,0,0,.25); animation: pop .18s; }
.modal.wide { max-width: 860px; }
.modal .m-head { display: flex; align-items: center; justify-content: space-between; padding: 18px 22px 8px; }
.modal .m-body { padding: 8px 22px 18px; }
.modal .m-foot { display: flex; justify-content: flex-end; gap: 10px; padding: 12px 22px 20px; }
.modal .x { border: 0; background: transparent; font-size: 1.4rem; color: var(--muted); }
@keyframes fade { from { opacity: 0 } } @keyframes pop { from { transform: translateY(10px); opacity: 0 } }
#toast-root { position: fixed; bottom: 20px; inset-inline-end: 20px; display: flex; flex-direction: column; gap: 8px; z-index: 100; }
.toast { background: var(--text); color: #fff; padding: 12px 18px; border-radius: 10px; box-shadow: var(--shadow); font-weight: 600; animation: pop .2s; max-width: 380px; }
.toast.err { background: var(--danger); } .toast.ok { background: var(--success); }

/* ---------- Auth ---------- */
.auth { min-height: 100vh; display: grid; place-items: center; padding: 20px; background: linear-gradient(160deg, #eef2ff, #f4f6fa 60%); }
.auth .box { width: 100%; max-width: 420px; background: var(--surface); border-radius: 18px; box-shadow: var(--shadow); padding: 30px; }
.auth .brand { justify-content: center; padding-bottom: 6px; }
.auth .tagline { text-align: center; color: var(--muted); margin: 0 0 22px; }
.auth .lang-switch { margin: 0 auto 18px; width: max-content; }
.divider { display: flex; align-items: center; gap: 12px; color: var(--muted); font-size: .85rem; margin: 16px 0; }
.divider::before, .divider::after { content: ""; flex: 1; height: 1px; background: var(--border); }

/* ---------- Impression ---------- */
.print-doc { display: none; }
@media print {
  body { background: #fff; font-size: 12.5pt; }
  .layout, #toast-root, #modal-root { display: none !important; }
  .print-doc { display: block; padding: 0; color: #000; }
  .print-doc .head { display: flex; justify-content: space-between; gap: 30px; margin-bottom: 30px; }
  .print-doc h1 { font-size: 26pt; letter-spacing: .05em; }
  .print-doc table { width: 100%; border-collapse: collapse; margin-top: 20px; }
  .print-doc th, .print-doc td { border-bottom: 1px solid #ccc; padding: 8px 6px; text-align: start; }
  .print-doc th { background: #f1f5f9; font-size: 10pt; text-transform: uppercase; }
  .print-doc td.num, .print-doc th.num { text-align: end; }
  .print-doc .totals { margin-top: 16px; }
  .print-doc .stamp { border: 3px solid #16a34a; color: #16a34a; font-weight: 900; font-size: 20pt; padding: 6px 16px; transform: rotate(-8deg); display: inline-block; }
  .print-doc .foot { margin-top: 40px; font-size: 10pt; color: #444; white-space: pre-line; }
}

/* ---------- Mobile ---------- */
@media (max-width: 900px) {
  .layout { grid-template-columns: 1fr; }
  .sidebar { position: fixed; inset-block: 0; inset-inline-start: 0; width: var(--sidebar); z-index: 40; transform: translateX(-100%); transition: transform .2s; box-shadow: 0 0 40px rgba(0,0,0,.2); }
  [dir=rtl] .sidebar { transform: translateX(100%); }
  .sidebar.open, [dir=rtl] .sidebar.open { transform: none; }
  .topbar .burger { display: inline-flex; }
  .topbar, .content { padding-inline: 16px; }
  .grid-2, .grid-3, .grid-4, .form .cols, .form .cols-3 { grid-template-columns: 1fr; }
  .kpi .value { font-size: 1.4rem; }
  .table th:nth-child(n+4):not(.keep), .table td:nth-child(n+4):not(.keep) { display: none; }
  .table.all-cols th, .table.all-cols td { display: table-cell; }
  .totals { width: 100%; }
  .modal { max-height: 96vh; }
}
