:root {
  --bg: #f5f4f1;
  --card: #ffffff;
  --ink: #1b1a18;
  --muted: #6f6a63;
  --light: #9b958c;
  --border: #e4e0d8;
  --border-strong: #cfc9bf;
  --accent: #e85d22;
  --accent-dk: #b8471a;
  --accent-bg: #fbeee6;
  --side: #1b1a18;
  --ok: #2f7d4f;
  --ok-bg: #e5f3ea;
  --warn: #9a6200;
  --warn-bg: #fdf2dc;
  --err: #b42318;
  --err-bg: #fdecea;
  --info: #2458a6;
  --info-bg: #e8effb;
  --radius: 10px;
  --shadow: 0 1px 2px rgba(27, 26, 24, 0.06), 0 1px 1px rgba(27, 26, 24, 0.04);
  --font: Inter, system-ui, -apple-system, 'Segoe UI', Roboto, Arial, sans-serif;
}
* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; }
body { font-family: var(--font); font-size: 14px; color: var(--ink); background: var(--bg); -webkit-font-smoothing: antialiased; }
a { color: inherit; }
button, input, select, textarea { font: inherit; color: inherit; }
.boot { padding: 40px; color: var(--muted); }

/* ---------- Structure ---------- */
.layout { display: grid; grid-template-columns: 232px 1fr; min-height: 100vh; }
.side { background: var(--side); color: #d9d5ce; padding: 18px 12px; display: flex; flex-direction: column; gap: 4px; position: sticky; top: 0; height: 100vh; height: 100dvh; overflow-y: auto; overscroll-behavior: contain; scrollbar-width: thin; scrollbar-color: #3a3632 transparent; }
.side > * { flex-shrink: 0; }
.side .brand { display: flex; align-items: center; gap: 10px; padding: 4px 8px 18px; }
.side .brand img { width: 108px; background: #fff; border-radius: 6px; padding: 4px 6px; }
.side .sect { font-size: 11px; text-transform: uppercase; letter-spacing: 0.08em; color: #8a847b; padding: 14px 10px 6px; }
.side a { display: flex; align-items: center; gap: 10px; padding: 8px 10px; border-radius: 8px; text-decoration: none; color: #d9d5ce; font-weight: 500; }
.side a:hover { background: rgba(255, 255, 255, 0.06); color: #fff; }
.side a.on { background: rgba(232, 93, 34, 0.16); color: #fff; }
.side a.on .ic { color: var(--accent); }
.side .ic { width: 18px; height: 18px; flex: none; }
.side .grow { flex: 1 0 8px; }
@media (max-height: 700px) {
  .side { padding-top: 12px; }
  .side .brand { padding-bottom: 10px; }
  .side .sect { padding-top: 10px; }
  .side a { padding-top: 6px; padding-bottom: 6px; }
}
.side .me { border-top: 1px solid rgba(255, 255, 255, 0.08); padding: 12px 10px 0; font-size: 13px; }
.side .me b { display: block; color: #fff; }
.side .me span { color: #8a847b; font-size: 12px; }
.side .me button { margin-top: 8px; background: none; border: 1px solid rgba(255, 255, 255, 0.15); color: #d9d5ce; border-radius: 7px; padding: 5px 10px; cursor: pointer; font-size: 12px; }
.main { min-width: 0; }
.scrim { display: none; }
.topbar { display: none; }
.page { padding: 24px 28px 60px; max-width: 1280px; }
.page.wide { max-width: none; }
.ph { display: flex; align-items: center; justify-content: space-between; gap: 12px; flex-wrap: wrap; margin-bottom: 18px; }
.ph h1 { font-size: 22px; margin: 0; letter-spacing: -0.01em; }
.ph .sub { color: var(--muted); margin-top: 2px; }
.row { display: flex; gap: 8px; align-items: center; flex-wrap: wrap; }

/* ---------- Composants ---------- */
.card { background: var(--card); border: 1px solid var(--border); border-radius: var(--radius); box-shadow: var(--shadow); }
.card-h { padding: 14px 16px; border-bottom: 1px solid var(--border); display: flex; align-items: center; justify-content: space-between; gap: 8px; }
.card-h h3 { margin: 0; font-size: 15px; }
.card-b { padding: 16px; }
.btn { display: inline-flex; align-items: center; justify-content: center; gap: 6px; border: 1px solid var(--border-strong); background: #fff; border-radius: 8px; padding: 8px 14px; cursor: pointer; font-weight: 500; white-space: nowrap; text-decoration: none; min-height: 36px; }
.btn:hover { background: #faf9f7; border-color: #bdb6aa; }
.btn:disabled { opacity: 0.5; cursor: not-allowed; }
.btn.pri { background: var(--accent); border-color: var(--accent); color: #fff; }
.btn.pri:hover { background: var(--accent-dk); border-color: var(--accent-dk); }
.btn.dark { background: var(--ink); border-color: var(--ink); color: #fff; }
.btn.danger { color: var(--err); }
.btn.danger:hover { background: var(--err-bg); border-color: #f3b8b2; }
.btn.ghost { border-color: transparent; background: transparent; }
.btn.ghost:hover { background: rgba(0, 0, 0, 0.04); }
.btn.sm { padding: 4px 10px; min-height: 30px; font-size: 13px; }
.btn.icon { padding: 6px; min-height: 32px; min-width: 32px; }
.btn svg { width: 16px; height: 16px; }

label.f { display: block; }
label.f > span { display: block; font-size: 12px; font-weight: 600; color: var(--muted); margin-bottom: 4px; }
.in, select.in, textarea.in { width: 100%; border: 1px solid var(--border-strong); background: #fff; border-radius: 8px; padding: 8px 10px; min-height: 36px; outline: none; }
textarea.in { resize: vertical; min-height: 60px; line-height: 1.4; }
.in:focus { border-color: var(--accent); box-shadow: 0 0 0 3px rgba(232, 93, 34, 0.15); }
.in:disabled, textarea.in:disabled, select.in:disabled { background: #f7f6f3; color: var(--muted); }
.in.num { text-align: right; font-variant-numeric: tabular-nums; }
.grid2 { display: grid; grid-template-columns: 1fr 1fr; gap: 10px 12px; }
.grid3 { display: grid; grid-template-columns: repeat(3, 1fr); gap: 10px 12px; }
.span2 { grid-column: span 2; }
.check { display: inline-flex; align-items: center; gap: 8px; cursor: pointer; user-select: none; }
.check input { width: 16px; height: 16px; accent-color: var(--accent); }
.seg { display: inline-flex; border: 1px solid var(--border-strong); border-radius: 8px; overflow: hidden; }
.seg button { border: 0; background: #fff; padding: 7px 12px; cursor: pointer; font-weight: 500; }
.seg button + button { border-left: 1px solid var(--border-strong); }
.seg button.on { background: var(--ink); color: #fff; }

.tabs { display: flex; gap: 4px; border-bottom: 1px solid var(--border); margin-bottom: 16px; overflow-x: auto; }
.tabs button { border: 0; background: none; padding: 10px 12px; cursor: pointer; font-weight: 500; color: var(--muted); border-bottom: 2px solid transparent; white-space: nowrap; }
.tabs button.on { color: var(--ink); border-bottom-color: var(--accent); }
.tabs .n { font-size: 11px; background: #eeebe6; border-radius: 10px; padding: 1px 7px; margin-left: 4px; }

table.t { width: 100%; border-collapse: collapse; }
table.t th { text-align: left; font-size: 12px; font-weight: 600; color: var(--muted); padding: 10px 12px; border-bottom: 1px solid var(--border); background: #faf9f7; white-space: nowrap; }
table.t td { padding: 10px 12px; border-bottom: 1px solid #efece7; vertical-align: middle; }
table.t tr.click { cursor: pointer; }
table.t tr.click:hover td { background: #faf8f5; }
table.t .r { text-align: right; font-variant-numeric: tabular-nums; white-space: nowrap; }
table.t .muted { color: var(--muted); }
.tbl-wrap { overflow-x: auto; }
.empty { padding: 36px 16px; text-align: center; color: var(--muted); }

.badge { display: inline-flex; align-items: center; gap: 5px; border-radius: 20px; padding: 2px 9px; font-size: 12px; font-weight: 600; white-space: nowrap; }
.badge::before { content: ''; width: 6px; height: 6px; border-radius: 50%; background: currentColor; }
.b-draft { background: #efece7; color: #6f6a63; }
.b-issued { background: var(--info-bg); color: var(--info); }
.b-accepted { background: var(--warn-bg); color: var(--warn); }
.b-paid { background: var(--ok-bg); color: var(--ok); }
.b-cancelled { background: var(--err-bg); color: var(--err); text-decoration: line-through; }
.tag { display: inline-block; font-size: 11px; font-weight: 700; letter-spacing: 0.03em; text-transform: uppercase; color: var(--accent-dk); background: var(--accent-bg); border-radius: 5px; padding: 2px 6px; }

.kpis { display: grid; grid-template-columns: repeat(4, 1fr); gap: 12px; margin-bottom: 18px; }
.kpi { padding: 14px 16px; }
.kpi .l { color: var(--muted); font-size: 12px; font-weight: 600; }
.kpi .v { font-size: 22px; font-weight: 700; margin-top: 4px; font-variant-numeric: tabular-nums; letter-spacing: -0.01em; }
.kpi .s { color: var(--light); font-size: 12px; margin-top: 2px; }
.quick { display: grid; grid-template-columns: repeat(5, 1fr); gap: 10px; margin-bottom: 18px; }
.quick button { display: flex; flex-direction: column; align-items: flex-start; gap: 6px; padding: 14px; text-align: left; cursor: pointer; border: 1px solid var(--border); background: #fff; border-radius: var(--radius); box-shadow: var(--shadow); }
.quick button:hover { border-color: var(--accent); }
.quick b { font-size: 14px; }
.quick span { color: var(--muted); font-size: 12px; }
.quick .plus { width: 26px; height: 26px; border-radius: 7px; background: var(--accent-bg); color: var(--accent); display: flex; align-items: center; justify-content: center; font-weight: 700; }
.cols2 { display: grid; grid-template-columns: 1fr 1fr; gap: 16px; }

/* ---------- Modale, toast, menus ---------- */
.modal-bg { position: fixed; inset: 0; background: rgba(27, 26, 24, 0.45); display: flex; align-items: flex-start; justify-content: center; padding: 5vh 16px; z-index: 100; overflow-y: auto; }
.modal { background: #fff; border-radius: 12px; width: 100%; max-width: 640px; box-shadow: 0 20px 50px rgba(0, 0, 0, 0.25); }
.modal.lg { max-width: 900px; }
.modal-h { padding: 16px 18px; border-bottom: 1px solid var(--border); display: flex; justify-content: space-between; align-items: center; }
.modal-h h3 { margin: 0; font-size: 16px; }
.modal-b { padding: 18px; }
.modal-f { padding: 12px 18px; border-top: 1px solid var(--border); display: flex; justify-content: flex-end; gap: 8px; flex-wrap: wrap; }
.toast { position: fixed; bottom: 20px; left: 50%; transform: translateX(-50%); background: var(--ink); color: #fff; padding: 10px 16px; border-radius: 9px; z-index: 200; box-shadow: 0 8px 24px rgba(0, 0, 0, 0.25); max-width: calc(100% - 32px); }
.toast.err { background: var(--err); }
.menu { position: relative; display: inline-block; }
.menu-list { position: absolute; right: 0; top: calc(100% + 4px); background: #fff; border: 1px solid var(--border); border-radius: 9px; box-shadow: 0 10px 30px rgba(0, 0, 0, 0.12); min-width: 220px; z-index: 50; padding: 4px; }
.menu-list button { display: block; width: 100%; text-align: left; border: 0; background: none; padding: 8px 10px; border-radius: 6px; cursor: pointer; }
.menu-list button:hover { background: #f5f3ef; }
.menu-list button.danger { color: var(--err); }
.menu-list hr { border: 0; border-top: 1px solid var(--border); margin: 4px 0; }
.alert { padding: 10px 12px; border-radius: 8px; font-size: 13px; margin-bottom: 12px; }
.alert.warn { background: var(--warn-bg); color: #6b4500; }
.alert.info { background: var(--info-bg); color: #1d4480; }
.alert.err { background: var(--err-bg); color: var(--err); }

/* ---------- Autocomplétion ---------- */
.ac { position: relative; }
.ac-list { position: absolute; left: 0; right: 0; top: calc(100% + 2px); background: #fff; border: 1px solid var(--border); border-radius: 8px; box-shadow: 0 10px 30px rgba(0, 0, 0, 0.12); z-index: 40; max-height: 280px; overflow-y: auto; }
.ac-list div { padding: 8px 10px; cursor: pointer; border-bottom: 1px solid #f3f1ed; }
.ac-list div:last-child { border-bottom: 0; }
.ac-list div.on, .ac-list div:hover { background: #fbf4ef; }
.ac-list small { display: block; color: var(--muted); font-size: 12px; }

/* ---------- Éditeur de document ---------- */
.ed { display: grid; grid-template-columns: minmax(420px, 560px) 1fr; height: calc(100vh - 61px); }
.ed-bar { position: sticky; top: 0; z-index: 30; background: #fff; border-bottom: 1px solid var(--border); padding: 10px 16px; display: flex; align-items: center; gap: 10px; flex-wrap: wrap; min-height: 61px; }
.ed-bar .ttl { font-weight: 700; font-size: 16px; }
.ed-bar .grow { flex: 1; }
.ed-bar .saved { color: var(--light); font-size: 12px; }
.ed-form { overflow-y: auto; padding: 16px; border-right: 1px solid var(--border); background: var(--bg); }
.ed-prev { overflow: auto; background: #d9d6d0; padding: 20px; }
.ed-prev .pages { transform-origin: top left; }
.ed-prev .pages .pg { margin: 0 auto 16px; box-shadow: 0 2px 10px rgba(0, 0, 0, 0.18); }
.ed-sec { background: #fff; border: 1px solid var(--border); border-radius: var(--radius); margin-bottom: 12px; }
.ed-sec > h4 { margin: 0; padding: 11px 14px; font-size: 13px; text-transform: uppercase; letter-spacing: 0.04em; color: var(--muted); border-bottom: 1px solid var(--border); display: flex; justify-content: space-between; align-items: center; }
.ed-sec > .b { padding: 14px; }
.ed-tabs { display: none; }
.line { border: 1px solid var(--border); border-radius: 9px; padding: 10px; margin-bottom: 10px; background: #fcfbf9; }
.line-h { display: flex; justify-content: space-between; align-items: center; margin-bottom: 8px; color: var(--muted); font-size: 12px; font-weight: 600; }
.line-g { display: grid; grid-template-columns: 1.2fr 0.5fr 1fr; gap: 8px; margin-top: 8px; }
.line-tot { text-align: right; font-weight: 700; font-variant-numeric: tabular-nums; margin-top: 6px; }
.tot-box { background: #faf9f7; border-radius: 8px; padding: 10px 12px; margin-top: 10px; }
.tot-box div { display: flex; justify-content: space-between; padding: 2px 0; color: var(--muted); font-variant-numeric: tabular-nums; }
.tot-box div.big { color: var(--accent-dk); font-weight: 700; font-size: 16px; border-top: 1px solid var(--border); margin-top: 4px; padding-top: 6px; }
.cond { display: grid; grid-template-columns: 150px 1fr 32px; gap: 6px; margin-bottom: 6px; }
.spec { display: grid; grid-template-columns: 1fr 1.6fr 32px; gap: 6px; margin-bottom: 6px; }
.ft { border: 1px dashed var(--accent); border-radius: 9px; padding: 10px; margin-top: 10px; background: #fff; }
.photo-box { display: flex; gap: 10px; align-items: center; }
.photo-box img { width: 110px; height: 70px; object-fit: contain; border: 1px solid var(--border); border-radius: 6px; background: #fff; }
.drop { border: 2px dashed var(--border-strong); border-radius: 10px; padding: 26px; text-align: center; color: var(--muted); cursor: pointer; }
.drop:hover { border-color: var(--accent); color: var(--ink); }

/* ---------- Connexion ---------- */
.login { min-height: 100vh; display: flex; align-items: center; justify-content: center; padding: 16px; background: linear-gradient(160deg, #1b1a18 0%, #2c2925 100%); }
.login .card { width: 100%; max-width: 380px; padding: 28px; }
.login img { width: 150px; display: block; margin: 0 auto 18px; }
.login h1 { font-size: 18px; text-align: center; margin: 0 0 20px; }

/* ---------- Impression ---------- */
#print-root { display: none; }
@media print {
  @page { size: A4; margin: 0; }
  body.printing > *:not(#print-root) { display: none !important; }
  body.printing #print-root { display: block; }
  body.printing { background: #fff; }
  #print-root .pg { page-break-after: always; break-after: page; margin: 0; box-shadow: none; }
  #print-root .pg:last-child { page-break-after: auto; break-after: auto; }
}

/* ---------- Responsive ---------- */
@media (max-width: 1100px) {
  .kpis { grid-template-columns: repeat(2, 1fr); }
  .quick { grid-template-columns: repeat(3, 1fr); }
  .ed { grid-template-columns: 1fr; height: auto; }
  .ed-tabs { display: flex; position: sticky; top: 61px; z-index: 25; background: #fff; border-bottom: 1px solid var(--border); }
  .ed-tabs button { flex: 1; border: 0; background: none; padding: 11px; font-weight: 600; color: var(--muted); border-bottom: 2px solid transparent; }
  .ed-tabs button.on { color: var(--ink); border-bottom-color: var(--accent); }
  .ed-form, .ed-prev { border: 0; overflow: visible; }
  .ed.show-prev .ed-form, .ed:not(.show-prev) .ed-prev { display: none; }
  .ed-prev { min-height: calc(100vh - 110px); padding: 12px; }
}
@media (max-width: 860px) {
  .layout { grid-template-columns: 1fr; }
  .side { position: fixed; left: 0; top: 0; bottom: 0; width: 260px; z-index: 90; transform: translateX(-100%); transition: transform 0.2s; }
  .layout.nav-open .side { transform: none; box-shadow: 10px 0 40px rgba(0, 0, 0, 0.3); }
  .layout.nav-open .scrim { display: block; position: fixed; inset: 0; background: rgba(0, 0, 0, 0.35); z-index: 80; }
  .topbar { display: flex; align-items: center; gap: 10px; padding: 10px 12px; background: var(--side); color: #fff; position: sticky; top: 0; z-index: 70; }
  .topbar button { background: none; border: 0; color: #fff; padding: 6px; cursor: pointer; }
  .topbar img { height: 26px; background: #fff; border-radius: 5px; padding: 2px 4px; }
  .page { padding: 16px 14px 60px; }
  .cols2 { grid-template-columns: 1fr; }
  .ed-bar { top: 50px; }
  .ed-tabs { top: 111px; }
}
@media (max-width: 600px) {
  .kpis { grid-template-columns: 1fr 1fr; }
  .kpi .v { font-size: 18px; }
  .quick { grid-template-columns: 1fr 1fr; }
  .grid2, .grid3 { grid-template-columns: 1fr; }
  .span2 { grid-column: auto; }
  .line-g { grid-template-columns: 1fr 1fr; }
  .cond { grid-template-columns: 1fr 32px; }
  .cond .in:nth-child(2) { grid-column: 1 / 2; }
  .hide-sm { display: none !important; }
  table.t td, table.t th { padding: 9px 8px; }
  .ph h1 { font-size: 19px; }
}
