/* =====================================================================
   Syndic — feuille de style unique. Propriétés logiques (inline/block)
   pour un rendu identique en LTR (français) et RTL (arabe).
   ===================================================================== */
:root {
  --bg: #f5f6f8;
  --surface: #ffffff;
  --surface-2: #f0f2f5;
  --text: #1c2430;
  --muted: #5f6b7a;
  --line: #e1e5ea;
  --primary: #1f5f8b;
  --primary-ink: #ffffff;
  --primary-soft: #e6f0f7;
  --ok: #1e7a4c; --ok-soft: #e3f4ea;
  --warn: #9a6200; --warn-soft: #fdf1dc;
  --danger: #b4232c; --danger-soft: #fbe7e8;
  --info: #3d5aa8; --info-soft: #e8ecf8;
  --radius: 10px;
  --shadow: 0 1px 2px rgba(16, 24, 40, .06), 0 1px 3px rgba(16, 24, 40, .08);
  --drawer-w: 264px;
  --top-h: 56px;
  --off: -100%;
  --font: "Inter", system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  color-scheme: light;
}
[dir="rtl"] { --off: 100%; --font: "Noto Sans Arabic", "Inter", system-ui, "Segoe UI", Tahoma, sans-serif; }

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #12161c; --surface: #1a2028; --surface-2: #222a34; --text: #e6eaf0; --muted: #9aa6b4;
    --line: #2d3742; --primary: #5aa2d6; --primary-ink: #0d1a24; --primary-soft: #1c3346;
    --ok: #5cc98f; --ok-soft: #173326; --warn: #e5b45a; --warn-soft: #3a2d14;
    --danger: #f07a80; --danger-soft: #3d1c1f; --info: #93a9ec; --info-soft: #1f2741;
    --shadow: none; color-scheme: dark;
  }
}

*, *::before, *::after { box-sizing: border-box; }
[hidden] { display: none !important; }
html { -webkit-text-size-adjust: 100%; }
body {
  margin: 0; background: var(--bg); color: var(--text);
  font: 15px/1.5 var(--font);
}
a { color: var(--primary); text-decoration: none; }
a:hover { text-decoration: underline; }
h1 { font-size: 1.4rem; margin: 0 0 .25rem; }
h2 { font-size: 1.1rem; margin: 0 0 .75rem; }
h3 { font-size: 1rem; margin: 0 0 .5rem; }
small, .muted { color: var(--muted); }
.ic { inline-size: 20px; block-size: 20px; flex: none; }
.skip { position: absolute; inset-inline-start: -999px; }
.skip:focus { inset-inline-start: 8px; inset-block-start: 8px; z-index: 100; background: var(--surface); padding: 8px; }
:focus-visible { outline: 2px solid var(--primary); outline-offset: 2px; }

/* Nombres, dates, références : toujours lisibles en LTR, même dans l'interface arabe */
.num, .ref { unicode-bidi: isolate; direction: ltr; font-variant-numeric: tabular-nums; white-space: nowrap; }
.neg { color: var(--danger); }
.cur { color: var(--muted); font-size: .85em; }
td.amount, th.amount { text-align: end; white-space: nowrap; }

/* ------------------------------------------------------------ Barre haute */
.topbar {
  position: sticky; inset-block-start: 0; z-index: 30; block-size: var(--top-h);
  display: flex; align-items: center; gap: .5rem; padding-inline: .75rem;
  background: var(--surface); border-block-end: 1px solid var(--line);
}
.brand { display: flex; align-items: center; gap: .5rem; font-weight: 600; color: var(--text); min-inline-size: 0; }
.brand span { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.brand .logo { block-size: 32px; inline-size: auto; border-radius: 6px; }
.top-actions { margin-inline-start: auto; display: flex; align-items: center; gap: .25rem; }
.icon-btn {
  position: relative; display: inline-flex; align-items: center; justify-content: center;
  inline-size: 44px; block-size: 44px; border: 0; border-radius: 10px; background: transparent; color: var(--text); cursor: pointer;
}
.icon-btn:hover { background: var(--surface-2); text-decoration: none; }
.icon-btn .dot {
  position: absolute; inset-block-start: 6px; inset-inline-end: 6px; min-inline-size: 18px; block-size: 18px;
  border-radius: 9px; background: var(--danger); color: #fff; font-size: 11px; line-height: 18px; text-align: center; padding-inline: 4px;
}
.chip {
  display: inline-flex; align-items: center; gap: .35rem; border: 1px solid var(--line); background: var(--surface);
  color: var(--text); border-radius: 999px; padding: .35rem .75rem; cursor: pointer; font: inherit; font-size: .9rem; min-block-size: 36px;
}
.chip .ic { inline-size: 16px; block-size: 16px; }

/* ------------------------------------------------------------ Drawer */
.drawer {
  position: fixed; z-index: 40; inset-block: 0; inset-inline-start: 0; inline-size: min(86vw, var(--drawer-w));
  background: var(--surface); border-inline-end: 1px solid var(--line);
  display: flex; flex-direction: column; transform: translateX(var(--off)); transition: transform .22s ease;
  overscroll-behavior: contain;
}
.drawer.open { transform: none; box-shadow: 0 0 40px rgba(0, 0, 0, .2); }
.drawer-head { display: flex; align-items: center; justify-content: space-between; padding: .9rem 1rem; border-block-end: 1px solid var(--line); }
.drawer-head .who { display: flex; flex-direction: column; min-inline-size: 0; }
.drawer-head .who strong { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.menu { flex: 1; overflow-y: auto; padding: .5rem; }
.menu a, .drawer-foot a, .drawer-foot .link {
  display: flex; align-items: center; gap: .75rem; padding: .6rem .75rem; min-block-size: 44px;
  border-radius: 8px; color: var(--text); font: inherit; inline-size: 100%; text-align: start;
}
.menu a:hover, .drawer-foot a:hover, .drawer-foot .link:hover { background: var(--surface-2); text-decoration: none; }
.menu a.active { background: var(--primary-soft); color: var(--primary); font-weight: 600; }
.drawer-foot { border-block-start: 1px solid var(--line); padding: .5rem; }
.drawer-foot .link { border: 0; background: none; cursor: pointer; }
.scrim { position: fixed; inset: 0; z-index: 35; background: rgba(10, 15, 25, .45); }

.main { padding: 1rem; max-inline-size: 1280px; margin-inline: auto; }

@media (min-width: 1024px) {
  .only-mobile { display: none; }
  body.has-drawer .drawer { transform: none; inset-block-start: var(--top-h); box-shadow: none; inline-size: var(--drawer-w); }
  body.has-drawer .main { margin-inline-start: var(--drawer-w); margin-inline-end: 0; padding: 1.5rem 2rem; max-inline-size: none; }
  body.has-drawer.drawer-collapsed .drawer { transform: translateX(var(--off)); }
  body.has-drawer.drawer-collapsed .main { margin-inline-start: 0; }
  .scrim { display: none !important; }
}

/* ------------------------------------------------------------ Page */
.page-head { display: flex; flex-wrap: wrap; align-items: flex-end; justify-content: space-between; gap: .75rem; margin-block-end: 1rem; }
.page-head .actions { display: flex; flex-wrap: wrap; gap: .5rem; }
.card { background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius); box-shadow: var(--shadow); padding: 1rem; margin-block-end: 1rem; }
.card > h2:first-child { display: flex; align-items: center; justify-content: space-between; gap: .5rem; }
.grid { display: grid; gap: 1rem; }
.grid-2 { grid-template-columns: repeat(auto-fit, minmax(min(100%, 380px), 1fr)); }
.grid-3 { grid-template-columns: repeat(auto-fit, minmax(min(100%, 280px), 1fr)); }
.kpis { display: grid; gap: .75rem; grid-template-columns: repeat(auto-fit, minmax(min(100%, 170px), 1fr)); margin-block-end: 1rem; }
.kpi { background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius); padding: .85rem 1rem; box-shadow: var(--shadow); }
.kpi .label { color: var(--muted); font-size: .85rem; }
.kpi .value { font-size: 1.3rem; font-weight: 700; margin-block-start: .2rem; }
.kpi.ok .value { color: var(--ok); } .kpi.danger .value { color: var(--danger); } .kpi.warn .value { color: var(--warn); }
.progress { block-size: 8px; background: var(--surface-2); border-radius: 4px; overflow: hidden; margin-block-start: .5rem; }
.progress > span { display: block; block-size: 100%; background: var(--ok); }
dl.props { display: grid; grid-template-columns: max-content 1fr; gap: .35rem 1rem; margin: 0; }
dl.props dt { color: var(--muted); }
dl.props dd { margin: 0; }

/* ------------------------------------------------------------ Tableaux */
.table-wrap { overflow-x: auto; -webkit-overflow-scrolling: touch; border: 1px solid var(--line); border-radius: var(--radius); background: var(--surface); }
table { inline-size: 100%; border-collapse: collapse; }
th, td { padding: .6rem .75rem; border-block-end: 1px solid var(--line); text-align: start; vertical-align: top; }
th { font-size: .82rem; color: var(--muted); font-weight: 600; background: var(--surface-2); white-space: nowrap; }
tr:last-child td { border-block-end: 0; }
tbody tr:hover { background: color-mix(in srgb, var(--surface-2) 60%, transparent); }
tfoot td { font-weight: 700; background: var(--surface-2); }
tr.is-cancelled td { color: var(--muted); text-decoration: line-through; }
tr.is-cancelled td .badge, tr.is-cancelled td a { text-decoration: none; }
.empty { padding: 1.5rem; text-align: center; color: var(--muted); }

/* Sur téléphone : tableaux .stack présentés en cartes */
@media (max-width: 640px) {
  table.stack thead { display: none; }
  table.stack, table.stack tbody, table.stack tr, table.stack td { display: block; inline-size: 100%; }
  table.stack tr { border-block-end: 1px solid var(--line); padding-block: .4rem; }
  table.stack td { border: 0; padding: .25rem .75rem; display: flex; justify-content: space-between; gap: 1rem; text-align: end; }
  table.stack td::before { content: attr(data-label); color: var(--muted); font-size: .82rem; text-align: start; }
  table.stack td.amount { text-align: end; }
  table.stack tfoot { display: block; }
  table.stack tfoot tr { display: flex; justify-content: space-between; }
}

/* ------------------------------------------------------------ Formulaires */
.form-grid { display: grid; gap: .9rem 1rem; grid-template-columns: repeat(auto-fit, minmax(min(100%, 240px), 1fr)); }
.form-grid .full { grid-column: 1 / -1; }
.field { display: flex; flex-direction: column; gap: .3rem; }
.field label, .label { font-size: .88rem; font-weight: 500; }
.field .hint { font-size: .8rem; color: var(--muted); }
.req::after { content: " *"; color: var(--danger); }
input[type=text], input[type=email], input[type=password], input[type=date], input[type=number], input[type=month], input[type=tel], input[type=search], select, textarea {
  inline-size: 100%; min-block-size: 44px; padding: .55rem .7rem; font: inherit; color: var(--text);
  background: var(--surface); border: 1px solid var(--line); border-radius: 8px;
}
textarea { min-block-size: 90px; resize: vertical; }
input:focus, select:focus, textarea:focus { border-color: var(--primary); outline: 2px solid var(--primary-soft); }
input.amount-input { text-align: end; direction: ltr; font-variant-numeric: tabular-nums; }
input[type=date], input[type=month], input[type=tel], input[type=email] { direction: ltr; }
[dir="rtl"] input[type=date], [dir="rtl"] input[type=month], [dir="rtl"] input[type=tel], [dir="rtl"] input[type=email] { text-align: end; }
.check { display: flex; align-items: center; gap: .5rem; min-block-size: 44px; }
.check input { inline-size: 20px; block-size: 20px; }
.form-actions { display: flex; flex-wrap: wrap; gap: .5rem; margin-block-start: 1rem; }
form.inline { display: inline; }
fieldset { border: 1px solid var(--line); border-radius: var(--radius); padding: .75rem 1rem 1rem; margin: 0 0 1rem; }
legend { font-weight: 600; padding-inline: .35rem; }
.filters { display: flex; flex-wrap: wrap; gap: .5rem; align-items: flex-end; margin-block-end: 1rem; }
.filters .field { min-inline-size: 140px; flex: 1 1 140px; }
.filters .btn { flex: none; }

/* ------------------------------------------------------------ Boutons */
.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: .4rem; min-block-size: 44px;
  padding: .5rem 1rem; border-radius: 8px; border: 1px solid var(--line); background: var(--surface);
  color: var(--text); font: inherit; font-weight: 500; cursor: pointer; white-space: nowrap;
}
.btn:hover { text-decoration: none; background: var(--surface-2); }
.btn .ic { inline-size: 18px; block-size: 18px; }
.btn-primary { background: var(--primary); border-color: var(--primary); color: var(--primary-ink); }
.btn-primary:hover { background: color-mix(in srgb, var(--primary) 88%, #000); }
.btn-danger { color: var(--danger); border-color: color-mix(in srgb, var(--danger) 40%, var(--line)); }
.btn-danger:hover { background: var(--danger-soft); }
.btn-sm { min-block-size: 34px; padding: .25rem .65rem; font-size: .88rem; }
.btn[disabled] { opacity: .5; cursor: not-allowed; }

/* ------------------------------------------------------------ Badges / alertes */
.badge { display: inline-block; padding: .1rem .55rem; border-radius: 999px; font-size: .78rem; font-weight: 600; white-space: nowrap; }
.b-ok { background: var(--ok-soft); color: var(--ok); }
.b-warn { background: var(--warn-soft); color: var(--warn); }
.b-danger { background: var(--danger-soft); color: var(--danger); }
.b-info { background: var(--info-soft); color: var(--info); }
.b-muted { background: var(--surface-2); color: var(--muted); }
.flash, .alert { padding: .75rem 1rem; border-radius: var(--radius); margin-block-end: 1rem; border: 1px solid transparent; }
.flash-success, .alert-ok { background: var(--ok-soft); color: var(--ok); }
.flash-error, .alert-danger { background: var(--danger-soft); color: var(--danger); }
.flash-info, .alert-info { background: var(--info-soft); color: var(--info); }
.alert-warn { background: var(--warn-soft); color: var(--warn); }
ul.alerts { list-style: none; padding: 0; margin: 0; display: grid; gap: .5rem; }
ul.alerts li { padding: .6rem .8rem; border-radius: 8px; border-inline-start: 4px solid; }
ul.alerts .danger { background: var(--danger-soft); border-color: var(--danger); }
ul.alerts .warn { background: var(--warn-soft); border-color: var(--warn); }
ul.alerts .info { background: var(--info-soft); border-color: var(--info); }

.tabs { display: flex; gap: .25rem; border-block-end: 1px solid var(--line); margin-block-end: 1rem; overflow-x: auto; }
.tabs a { padding: .6rem .9rem; color: var(--muted); border-block-end: 2px solid transparent; white-space: nowrap; }
.tabs a.active { color: var(--primary); border-color: var(--primary); font-weight: 600; }
.pager { display: flex; gap: .25rem; justify-content: center; margin-block: 1rem; }
.pager a, .pager .cur { min-inline-size: 36px; padding: .35rem .6rem; text-align: center; border-radius: 6px; border: 1px solid var(--line); }
.pager .cur { background: var(--primary); color: var(--primary-ink); border-color: var(--primary); }

/* Graphiques */
.chart-box { position: relative; block-size: 280px; }
.bars { display: grid; gap: .45rem; }
.bar-row { display: grid; grid-template-columns: minmax(90px, 30%) 1fr auto; gap: .5rem; align-items: center; font-size: .88rem; }
.bar-track { block-size: 10px; background: var(--surface-2); border-radius: 5px; overflow: hidden; }
.bar-track > span { display: block; block-size: 100%; background: var(--primary); }
.bar-track > span.over { background: var(--danger); }

/* Connexion */
.auth-wrap { min-block-size: 100vh; display: grid; place-items: center; padding: 1rem; }
.auth-card { inline-size: min(100%, 400px); }
.auth-card .brand-big { text-align: center; margin-block-end: 1rem; }

/* Divers */
.toolbar { display: flex; flex-wrap: wrap; gap: .5rem; align-items: center; }
.stack-sm > * + * { margin-block-start: .5rem; }
.right { text-align: end; }
.nowrap { white-space: nowrap; }
.preview { white-space: pre-wrap; background: var(--surface-2); border-radius: 8px; padding: .75rem; font-size: .92rem; }
.estimate-tag { font-size: .75rem; padding: .1rem .45rem; border-radius: 4px; background: var(--info-soft); color: var(--info); font-weight: 600; }

@media print {
  .topbar, .drawer, .scrim, .no-print, .page-head .actions, .filters, .form-actions { display: none !important; }
  body { background: #fff; }
  body.has-drawer .main { margin: 0; padding: 0; }
  .card, .kpi, .table-wrap { box-shadow: none; border-color: #ccc; break-inside: avoid; }
}

/* ------------------------------------------------------------ Reçu */
.receipt-doc { font-family: "Inter", system-ui, sans-serif; color: #111; }
.receipt-doc[dir="rtl"], .receipt-doc[dir="rtl"] * { letter-spacing: normal !important; }
.receipt-doc[dir="rtl"] bdi, .receipt-doc[dir="rtl"] .ref { margin-inline: .25em; }
.receipt-doc[dir="rtl"] { font-family: "Noto Sans Arabic", "Inter", Tahoma, sans-serif; }
.receipt {
  position: relative; background: #fff; color: #111; border: 1px solid #c9ced6; border-radius: 6px;
  padding: 14px 18px; max-inline-size: 190mm; margin-inline: auto; overflow: hidden; break-inside: avoid; page-break-inside: avoid;
}
.receipt .cur { color: #444; }
.r-head { display: flex; justify-content: space-between; gap: 12px; border-block-end: 2px solid #1f5f8b; padding-block-end: 8px; margin-block-end: 8px; }
.r-org { display: flex; gap: 10px; align-items: flex-start; }
.r-logo { block-size: 48px; inline-size: auto; }
.r-title { text-align: end; }
.r-title h2 { margin: 0; font-size: 1.15rem; color: #1f5f8b; }
.r-copy { display: inline-block; margin-block-start: 4px; padding: 1px 8px; border: 1px solid #1f5f8b; border-radius: 4px; font-size: .78rem; color: #1f5f8b; }
.r-table, .r-lines { inline-size: 100%; border-collapse: collapse; font-size: .9rem; margin-block-end: 8px; }
.r-table th, .r-table td, .r-lines th, .r-lines td { border: 1px solid #d5dae1; padding: 4px 8px; background: none; color: #111; }
.r-table th { inline-size: 22%; background: #f3f5f8; font-weight: 600; font-size: .82rem; }
.r-lines th { background: #f3f5f8; }
.r-lines tfoot td { font-weight: 700; background: #f3f5f8; }
.r-amount td { font-size: 1.05rem; }
.r-note { font-size: .82rem; margin: 4px 0; color: #6b4a00; }
.r-cancel { color: #b4232c; font-weight: 600; }
.r-foot { display: flex; justify-content: space-between; gap: 12px; align-items: flex-end; margin-block-start: 6px; font-size: .88rem; }
.r-sign { inline-size: 48%; block-size: 80px; border: 1px dashed #9aa3ae; border-radius: 4px; padding: 4px 8px; color: #555; font-size: .8rem; }
.stamp {
  position: absolute; inset-block-start: 40%; inset-inline-start: 50%; transform: translate(-50%, -50%) rotate(-18deg);
  font-size: 3.2rem; font-weight: 800; opacity: .16; pointer-events: none; white-space: nowrap; border: 6px solid; padding: 0 18px; border-radius: 10px;
}
[dir="rtl"] .stamp { transform: translate(50%, -50%) rotate(18deg); }
.stamp-cancel { color: #b4232c; opacity: .3; }
.stamp-dup { color: #1f5f8b; }
.stamp-verified { color: #1e7a4c; opacity: .22; }
.r-qr { display: flex; flex-direction: column; align-items: center; gap: 2px; font-size: .68rem; color: #555; text-align: center; max-inline-size: 110px; }
.r-qr [data-qr] { inline-size: 96px; block-size: 96px; }
.r-qr [data-qr] img, .r-qr [data-qr] canvas { inline-size: 96px !important; block-size: 96px !important; }
.cut-line { border-block-start: 1px dashed #888; margin: 14px 0; text-align: center; block-size: 0; }
.cut-line span { position: relative; inset-block-start: -.75em; background: #fff; padding-inline: 6px; color: #888; }
@media print {
  @page { size: A4; margin: 10mm; }
  body:has(#receiptDoc) * { visibility: hidden; }
  #receiptDoc, #receiptDoc * { visibility: visible; }
  #receiptDoc { position: absolute; inset-inline-start: 0; inset-block-start: 0; inline-size: 100%; }
  .receipt { border-color: #999; }
}
