:root {
  --bg: #f5f3ef;
  --surface: #ffffff;
  --surface-2: #faf8f5;
  --border: #e3ded6;
  --text: #1f1c18;
  --muted: #6d665c;
  --accent: #8a5a2b;
  --accent-ink: #ffffff;
  --accent-soft: #f3e8dc;
  --ok: #2e7d4f;
  --ok-soft: #e3f2e8;
  --warn: #9a6400;
  --warn-soft: #fbf0d9;
  --err: #b3261e;
  --err-soft: #fbe5e3;
  --info: #2459a6;
  --info-soft: #e4ecf8;
  --side: #2a2520;
  --side-text: #e9e3da;
  --radius: 10px;
  --shadow: 0 1px 2px rgba(0, 0, 0, .06), 0 2px 8px rgba(0, 0, 0, .04);
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #171513; --surface: #211e1b; --surface-2: #282420; --border: #3a352f; --text: #ece6dd; --muted: #a39a8e;
    --accent: #d39a5e; --accent-ink: #1a1410; --accent-soft: #3a2c1f; --ok: #6cc391; --ok-soft: #1d3326; --warn: #e2b04f; --warn-soft: #3a2f17;
    --err: #f08a80; --err-soft: #3d1f1c; --info: #8ab4f0; --info-soft: #1c2a40; --side: #0f0e0c; --side-text: #e9e3da;
    color-scheme: dark;
  }
}
:root[data-theme="dark"] {
  --bg: #171513; --surface: #211e1b; --surface-2: #282420; --border: #3a352f; --text: #ece6dd; --muted: #a39a8e;
  --accent: #d39a5e; --accent-ink: #1a1410; --accent-soft: #3a2c1f; --ok: #6cc391; --ok-soft: #1d3326; --warn: #e2b04f; --warn-soft: #3a2f17;
  --err: #f08a80; --err-soft: #3d1f1c; --info: #8ab4f0; --info-soft: #1c2a40; --side: #0f0e0c; --side-text: #e9e3da;
  color-scheme: dark;
}
.print-preview { width: 100%; height: 70vh; border: 1px solid var(--border); border-radius: 8px; background: #fff; }

* { box-sizing: border-box; }
html, body { margin: 0; }
body { font: 14px/1.5 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif; background: var(--bg); color: var(--text); display: flex; flex-direction: column; min-height: 100vh; }
a { color: var(--accent); text-decoration: none; }
a:hover { text-decoration: underline; }
code { font: 12px ui-monospace, Consolas, monospace; background: var(--surface-2); padding: 1px 4px; border-radius: 4px; }
small, .small { font-size: 12px; }
.muted { color: var(--muted); }
.neg { color: var(--err); font-weight: 600; }
.pos { color: var(--ok); font-weight: 600; }

/* ---------- Structure ---------- */
/* En-tête blanc : marque + utilisateur, puis menu horizontal en ruban */
#appbar { position: sticky; top: 0; z-index: 20; background: var(--surface); border-bottom: 1px solid var(--border); box-shadow: var(--shadow); }
.brand { display: flex; gap: 10px; align-items: center; min-width: 0; }
.brand strong { color: var(--accent); }
.brand small { display: block; color: var(--muted); font-size: 11px; }
.logo { width: 40px; height: 40px; flex: 0 0 40px; border-radius: 8px; background: #fff; object-fit: cover; border: 1px solid var(--border); }
#topbar { display: flex; align-items: center; gap: 12px; padding: 8px 24px; border-bottom: 1px solid var(--border); }
.ribbon { display: flex; align-items: stretch; padding: 6px 8px 0; overflow-x: auto; scrollbar-width: thin; }
.rgroup { display: flex; flex-direction: column; align-items: center; padding: 0 4px; border-right: 1px solid var(--border); flex: 0 0 auto; }
/* Centré quand il tient, défilable sans rognage quand l'écran est trop étroit */
.rgroup:first-child { margin-left: auto; }
.rgroup:last-child { border-right: 0; margin-right: auto; }
.rbtns { display: flex; gap: 1px; flex: 1; }
#nav a { display: flex; flex-direction: column; align-items: center; justify-content: flex-start; gap: 3px; min-width: 62px; max-width: 92px; padding: 6px 5px; border-radius: 8px; color: var(--text); font-size: 12px; line-height: 1.2; text-align: center; border: 1px solid transparent; }
#nav a .ri { font-size: 20px; line-height: 1; }
#nav a:hover { background: var(--accent-soft); text-decoration: none; }
#nav a.active { background: var(--accent-soft); border-color: var(--accent); color: var(--accent); font-weight: 600; }
.nav-group { margin: 3px 0 4px; font-size: 10.5px; text-transform: uppercase; letter-spacing: .06em; color: var(--muted); white-space: nowrap; }
#footer { margin-top: auto; padding: 14px 16px; text-align: center; font-size: 12px; color: var(--muted); border-top: 1px solid var(--border); background: var(--surface); }
#footer a { font-weight: 600; }
#userbar { display: flex; align-items: center; gap: 12px; margin-left: auto; flex-wrap: wrap; }
.user-switch { display: flex; align-items: center; gap: 8px; color: var(--muted); font-size: 12px; }
.todo-pill { background: var(--accent-soft); color: var(--accent); padding: 4px 10px; border-radius: 99px; font-weight: 600; font-size: 12px; }
.menu { display: none; }
#main { padding: 24px; max-width: 1400px; width: 100%; margin: 0 auto; outline: none; }

/* ---------- En-têtes et cartes ---------- */
.page-head { display: flex; justify-content: space-between; align-items: flex-start; gap: 16px; margin-bottom: 18px; flex-wrap: wrap; }
.page-head h1 { margin: 0; font-size: 22px; }
.page-head .sub { margin: 4px 0 0; color: var(--muted); }
.page-actions, .card-actions { display: flex; gap: 6px; flex-wrap: wrap; align-items: center; }
.card { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); box-shadow: var(--shadow); padding: 16px; margin-bottom: 16px; min-width: 0; }
.card-head { display: flex; justify-content: space-between; align-items: center; gap: 12px; margin-bottom: 12px; flex-wrap: wrap; }
.card-head h3 { margin: 0; font-size: 15px; }
.grid-2 { display: grid; grid-template-columns: 1fr 1fr; gap: 16px; }
.grid-2.wide-left { grid-template-columns: 1.6fr 1fr; }
.grid-2 > .card { margin-bottom: 0; }
.grid-2 + * { margin-top: 16px; }
.empty { color: var(--muted); font-style: italic; margin: 6px 0; }
.intro { margin-bottom: 12px; color: var(--muted); }

/* ---------- Indicateurs ---------- */
.kpis { display: grid; grid-template-columns: repeat(auto-fill, minmax(180px, 1fr)); gap: 12px; margin-bottom: 16px; }
.kpis.compact { grid-template-columns: repeat(auto-fill, minmax(160px, 1fr)); }
.kpi { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); padding: 12px 14px; display: flex; flex-direction: column; gap: 2px; min-width: 0; }
.kpi-label { font-size: 12px; color: var(--muted); }
.kpi-value { font-size: 18px; font-variant-numeric: tabular-nums; overflow-wrap: anywhere; }
.kpi-sub { font-size: 11px; color: var(--muted); }
.kpi.warn { border-left: 3px solid var(--warn); }
.kpi.err { border-left: 3px solid var(--err); }
.kpi.ok { border-left: 3px solid var(--ok); }

/* ---------- Tableaux ---------- */
.table-wrap { overflow-x: auto; }
table { width: 100%; border-collapse: collapse; }
th { text-align: left; font-size: 12px; font-weight: 600; color: var(--muted); padding: 8px; border-bottom: 1px solid var(--border); white-space: nowrap; }
td { padding: 8px; border-bottom: 1px solid var(--border); vertical-align: top; }
tr:last-child td { border-bottom: 0; }
td.num, th.num { text-align: right; font-variant-numeric: tabular-nums; white-space: nowrap; }
tbody tr:hover { background: var(--surface-2); }

/* ---------- Badges, boutons ---------- */
.badge { display: inline-block; padding: 2px 8px; border-radius: 99px; font-size: 11.5px; font-weight: 600; white-space: nowrap; background: var(--surface-2); color: var(--muted); }
.badge.ok { background: var(--ok-soft); color: var(--ok); }
.badge.warn { background: var(--warn-soft); color: var(--warn); }
.badge.err { background: var(--err-soft); color: var(--err); }
.badge.info { background: var(--info-soft); color: var(--info); }
.btn { display: inline-flex; align-items: center; gap: 4px; font: inherit; font-weight: 600; padding: 7px 12px; border-radius: 8px; border: 1px solid var(--border); background: var(--surface); color: var(--text); cursor: pointer; white-space: nowrap; }
.btn:hover:not([disabled]) { border-color: var(--accent); }
.btn.primary { background: var(--accent); border-color: var(--accent); color: var(--accent-ink); }
.btn.primary:hover:not([disabled]) { filter: brightness(1.08); }
.btn.ghost { background: transparent; }
.btn.danger { background: var(--err); border-color: var(--err); color: #fff; }
.btn.sm { padding: 4px 9px; font-size: 12.5px; margin: 1px 2px 1px 0; }
.btn[disabled], .btn.disabled { opacity: .5; cursor: not-allowed; }
.lock { font-size: 11px; }
.icon-btn { background: none; border: 0; font-size: 22px; line-height: 1; cursor: pointer; color: inherit; padding: 4px 8px; }
select, input, textarea { font: inherit; color: var(--text); background: var(--surface); border: 1px solid var(--border); border-radius: 8px; padding: 7px 9px; max-width: 100%; }
select:focus, input:focus, textarea:focus, .btn:focus-visible, a:focus-visible { outline: 2px solid var(--accent); outline-offset: 1px; }
.search { min-width: 220px; }

/* ---------- Notices ---------- */
.notice { padding: 10px 14px; border-radius: 8px; margin: 0 0 14px; background: var(--surface-2); border: 1px solid var(--border); }
.notice.ok { background: var(--ok-soft); border-color: transparent; color: var(--ok); }
.notice.warn { background: var(--warn-soft); border-color: transparent; color: var(--warn); }
.notice.err { background: var(--err-soft); border-color: transparent; color: var(--err); }
.notice.info { background: var(--info-soft); border-color: transparent; color: var(--info); }

/* ---------- Dossier ---------- */
.stepper { display: flex; list-style: none; padding: 0; margin: 0 0 16px; overflow-x: auto; gap: 2px; }
.stepper li { flex: 1; min-width: 96px; font-size: 11.5px; text-align: center; padding: 8px 4px; background: var(--surface); border: 1px solid var(--border); color: var(--muted); }
.stepper li:first-child { border-radius: 8px 0 0 8px; }
.stepper li:last-child { border-radius: 0 8px 8px 0; }
.stepper li.done { background: var(--ok-soft); color: var(--ok); border-color: transparent; }
.stepper li.current { background: var(--accent); color: var(--accent-ink); font-weight: 700; border-color: var(--accent); }
.tabs { display: flex; gap: 2px; border-bottom: 1px solid var(--border); margin-bottom: 16px; overflow-x: auto; }
.tabs a { padding: 8px 12px; color: var(--muted); border-bottom: 2px solid transparent; white-space: nowrap; }
.tabs a.active { color: var(--accent); border-bottom-color: var(--accent); font-weight: 600; }
.tabs a:hover { text-decoration: none; color: var(--text); }
.locks { list-style: none; margin: 0; padding: 0; display: flex; flex-direction: column; gap: 8px; }
.locks li { display: flex; align-items: center; gap: 12px; padding: 10px; border-radius: 8px; background: var(--surface-2); }
.locks li div { flex: 1; display: flex; flex-direction: column; }
.locks small { color: var(--muted); }
.lock-n { width: 26px; height: 26px; border-radius: 50%; display: grid; place-items: center; font-weight: 700; background: var(--border); flex: 0 0 26px; }
.locks li.ok .lock-n { background: var(--ok); color: #fff; }
.locks li.ko .lock-n { background: var(--warn); color: #fff; }
.lock-state { font-size: 18px; font-weight: 700; }
.locks li.ok .lock-state { color: var(--ok); }
.locks li.ko .lock-state { color: var(--warn); }
dl.totals { display: grid; grid-template-columns: 1fr auto; gap: 4px 16px; margin: 0; }
dl.totals dt { color: var(--muted); }
dl.totals dd { margin: 0; text-align: right; font-variant-numeric: tabular-nums; }
dl.totals .strong { font-weight: 700; color: var(--text); }
dl.totals .sep { border-top: 1px solid var(--border); padding-top: 6px; margin-top: 4px; }
.checklist { list-style: none; margin: 0; padding: 0; columns: 2; }
.checklist li { padding: 3px 0; }
.reserve { margin-top: 4px; font-size: 12.5px; }
.reserve.levee { text-decoration: line-through; color: var(--muted); }

/* ---------- Production ---------- */
.of-meta { display: flex; gap: 14px; align-items: center; flex-wrap: wrap; margin-bottom: 12px; color: var(--muted); }
.of-prog { display: inline-flex; align-items: center; gap: 6px; min-width: 160px; }
.progress { height: 8px; background: var(--surface-2); border: 1px solid var(--border); border-radius: 99px; overflow: hidden; flex: 1; min-width: 80px; display: inline-block; vertical-align: middle; }
.progress span { display: block; height: 100%; background: var(--accent); }
.etapes { list-style: none; margin: 0; padding: 0; display: grid; gap: 8px; }
.etape { border: 1px solid var(--border); border-radius: 8px; padding: 10px 12px; display: grid; grid-template-columns: 1fr auto; gap: 4px 12px; align-items: center; }
.etape.terminee { background: var(--ok-soft); border-color: transparent; }
.etape.en_cours { border-color: var(--info); box-shadow: inset 3px 0 0 var(--info); }
.etape-head { display: flex; gap: 8px; align-items: center; flex-wrap: wrap; }
.etape-info { grid-column: 1; }
.etape-actions { grid-column: 2; grid-row: 1 / span 2; display: flex; gap: 6px; flex-wrap: wrap; justify-content: flex-end; }
.etapes.tactile .btn { padding: 12px 18px; font-size: 15px; min-height: 48px; }

/* ---------- Tableau de bord ---------- */
.todo { list-style: none; margin: 0; padding: 0; max-height: 420px; overflow-y: auto; }
.todo li { padding: 7px 10px; border-left: 3px solid var(--border); margin-bottom: 4px; background: var(--surface-2); border-radius: 0 6px 6px 0; }
.todo li.urgent { border-left-color: var(--err); }
.bars { list-style: none; margin: 0; padding: 0; display: grid; gap: 8px; }
.bars li { display: grid; grid-template-columns: 170px 1fr 30px; gap: 10px; align-items: center; }
.bar { height: 10px; background: var(--surface-2); border-radius: 99px; overflow: hidden; }
.bar span { display: block; height: 100%; background: var(--accent); border-radius: 99px; }
.bar-n { text-align: right; font-variant-numeric: tabular-nums; font-weight: 600; }

/* ---------- Modale et formulaires ---------- */
#modal { position: fixed; inset: 0; z-index: 50; display: grid; place-items: center; padding: 16px; }
#modal[hidden] { display: none; }
.modal-back { position: absolute; inset: 0; background: rgba(0, 0, 0, .45); }
.modal { position: relative; background: var(--surface); border-radius: 12px; width: min(560px, 100%); max-height: calc(100vh - 32px); overflow-y: auto; box-shadow: 0 20px 60px rgba(0, 0, 0, .3); }
.modal.wide { width: min(860px, 100%); }
.modal header { display: flex; justify-content: space-between; align-items: center; padding: 14px 18px; border-bottom: 1px solid var(--border); position: sticky; top: 0; background: var(--surface); }
.modal h2 { margin: 0; font-size: 17px; }
.modal-body { padding: 18px; }
.form-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 12px; }
.field { display: flex; flex-direction: column; gap: 4px; min-width: 0; }
.field > span { font-size: 12.5px; font-weight: 600; color: var(--muted); }
.field small { color: var(--muted); }
.full { grid-column: 1 / -1; }
.check { display: flex; gap: 8px; align-items: center; }
.form-error { background: var(--err-soft); color: var(--err); padding: 8px 12px; border-radius: 8px; }
.form-actions { display: flex; justify-content: flex-end; gap: 8px; margin-top: 4px; }

/* ---------- Toasts ---------- */
#toasts { position: fixed; right: 16px; bottom: 16px; z-index: 60; display: flex; flex-direction: column; gap: 8px; max-width: min(420px, calc(100vw - 32px)); }
.toast { padding: 10px 14px; border-radius: 8px; background: var(--side); color: #fff; box-shadow: var(--shadow); transition: opacity .4s; }
.toast.err { background: var(--err); }
.toast.warn { background: var(--warn); }
.toast.out { opacity: 0; }

/* ---------- Compte à rebours de livraison et notifications ---------- */
.notif-pill { position: relative; font-size: 18px; line-height: 1; padding: 4px 6px; color: var(--text); }
.notif-pill b { position: absolute; top: -4px; right: -6px; min-width: 18px; height: 18px; padding: 0 5px; border-radius: 99px; background: var(--err); color: #fff; font-size: 11px; line-height: 18px; text-align: center; }
.rebours-list { list-style: none; margin: 0; padding: 0; display: grid; gap: 10px; }
.rebours { display: grid; grid-template-columns: 104px 1fr; gap: 14px; align-items: center; padding: 12px 14px; border: 1px solid var(--border); border-radius: var(--radius); background: var(--surface-2); }
.rebours.alerte { border-color: var(--warn); background: var(--warn-soft); }
.rebours.retard { border-color: var(--err); background: var(--err-soft); }
.rebours.attente { border-style: dashed; }
.rebours-j { text-align: center; }
.rebours-j strong { display: block; font-size: 30px; line-height: 1.1; font-variant-numeric: tabular-nums; }
.rebours-j small { color: var(--muted); font-size: 11px; }
.rebours.alerte .rebours-j strong { color: var(--warn); }
.rebours.retard .rebours-j strong { color: var(--err); font-size: 20px; }
.rebours-info { min-width: 0; display: grid; gap: 4px; }
.rebours-info .progress { width: 100%; }
.rebours.alerte .progress span { background: var(--warn); }
.rebours.retard .progress span { background: var(--err); }
.notice .rebours { margin-top: 8px; background: var(--surface); }
.notif-list { list-style: none; margin: 0; padding: 0; }
.notif-list li { display: flex; gap: 10px; align-items: baseline; padding: 8px 10px; border-left: 3px solid var(--border); margin-bottom: 4px; background: var(--surface-2); border-radius: 0 6px 6px 0; }
.notif-list li.nonlu { border-left-color: var(--warn); font-weight: 600; }
.notif-list li.retard { border-left-color: var(--err); }
.notif-list time { color: var(--muted); font-size: 12px; font-weight: 400; white-space: nowrap; }

/* ---------- Responsive : tablette atelier et téléphone ---------- */
@media (max-width: 1000px) {
  .grid-2, .grid-2.wide-left { grid-template-columns: 1fr; }
  .grid-2 > .card { margin-bottom: 0; }
}
@media (max-width: 800px) {
  /* Téléphone : le ruban se replie derrière le bouton ☰ et s'affiche en grille */
  .ribbon { display: none; flex-wrap: wrap; padding: 8px 12px; gap: 8px; max-height: calc(100vh - 64px); overflow-y: auto; }
  body.nav-open .ribbon { display: flex; }
  .rgroup { flex: 1 1 100%; align-items: stretch; border-right: 0; border-bottom: 1px solid var(--border); padding: 0 0 8px; }
  .rgroup:last-child { border-bottom: 0; }
  .rgroup .nav-group { order: -1; margin: 0 0 4px; }
  .rbtns { flex-wrap: wrap; }
  #nav a { flex: 1 1 72px; max-width: none; }
  .menu { display: block; }
  #topbar { padding: 8px 16px; gap: 8px; }
  .brand small { display: none; }
  #main { padding: 16px; }
  .form-grid { grid-template-columns: 1fr; }
  .bars li { grid-template-columns: 120px 1fr 24px; }
  .user-switch span { display: none; }
  .user-switch select { max-width: 200px; }
  .search { min-width: 0; width: 100%; }
  .etape { grid-template-columns: 1fr; }
  .etape-actions { grid-column: 1; grid-row: auto; justify-content: flex-start; }
  .checklist { columns: 1; }
  .rebours { grid-template-columns: 78px 1fr; gap: 10px; padding: 10px; }
  .rebours-j strong { font-size: 24px; }
  /* Tableaux en cartes empilées */
  table thead { display: none; }
  table, tbody, tr, td { display: block; width: 100%; }
  tr { border-bottom: 1px solid var(--border); padding: 6px 0; }
  td { border: 0; padding: 3px 0; display: grid; grid-template-columns: 110px 1fr; gap: 8px; text-align: left !important; white-space: normal !important; }
  td::before { content: attr(data-label); color: var(--muted); font-size: 12px; }
  td[data-label=""]::before { content: none; }
  td[data-label=""] { grid-template-columns: 1fr; }
}
