/* Wemerce — dashboard client. Design system en CSS natif, sans build.
   Ordre des couches : reset < tokens < base < layout < components < utilities.
   Thème : `color-scheme` + light-dark() ; forcé par <html data-theme="light|dark"> (cookie). */
@layer reset, tokens, base, layout, components, utilities;

/* ======================================================================== tokens */
@layer tokens {
  :root {
    color-scheme: light dark;
    --ground:        light-dark(#f6f7f5, #0f1211);
    --surface:       light-dark(#ffffff, #161a18);
    --surface-2:     light-dark(#fafbfa, #1b201d);
    --side:          light-dark(#f0f2ef, #121614);
    --side-hover:    light-dark(#e7eae5, #1a1f1c);
    --border:        light-dark(#e4e7e2, #262c29);
    --border-strong: light-dark(#d0d5cd, #363e39);
    --ink:           light-dark(#141816, #e9ece9);
    --ink-soft:      light-dark(#545b55, #a2aaa4);
    --ink-faint:     light-dark(#676e68, #8a938d);
    --brand:         light-dark(#2f5d45, #7fb79a);
    --brand-soft:    light-dark(#e6efe9, #1d2a23);
    --on-brand:      light-dark(#ffffff, #0f1211);
    --d-green:       light-dark(#0f7548, #4fae82);
    --d-amber:       light-dark(#91620a, #c9952e);
    --d-blue:        light-dark(#1a6ea8, #5aa0d4);
    --d-red:         light-dark(#b0381f, #e07a64);
    --d-green-soft:  light-dark(#e6f2ec, #15271e);
    --d-amber-soft:  light-dark(#f8efdc, #2b2415);
    --d-blue-soft:   light-dark(#e3edf5, #16242f);
    --d-red-soft:    light-dark(#f8e9e4, #2c1a16);
    --grid:          light-dark(#edefea, #222825);
    --shadow: 0 1px 2px rgb(0 0 0 / .05), 0 8px 24px rgb(0 0 0 / .08);
    --shadow-pop: 0 16px 48px rgb(0 0 0 / .22);
    --r: 4px;
    --r-sm: 3px;
    --font: "Inter", system-ui, -apple-system, "Segoe UI", sans-serif;
    --mono: "JetBrains Mono", ui-monospace, monospace;
    --fs-sm: 12.5px;
    --fs-md: 14px;
    --side-w: 232px;
    --top-h: 52px;
    --t-fast: 140ms;
    --bottombar-h: 0px;
  }
  :root[data-theme="light"] { color-scheme: light; }
  :root[data-theme="dark"]  { color-scheme: dark; }
}

/* ======================================================================== reset */
@layer reset {
  *, *::before, *::after { box-sizing: border-box; }
  body, h1, h2, h3, p, dl, dd, ul { margin: 0; }
  ul { padding: 0; list-style: none; }
  button, input, select { font: inherit; color: inherit; }
  [hidden] { display: none !important; }
}

/* ======================================================================== base */
@layer base {
  html { padding-top: env(safe-area-inset-top, 0px); }
  body {
    background: var(--ground);
    color: var(--ink);
    font-family: var(--font);
    font-size: var(--fs-md);
    line-height: 1.5;
    font-feature-settings: "cv11" 1;
    -webkit-font-smoothing: antialiased;
    padding-bottom: calc(var(--bottombar-h) + env(safe-area-inset-bottom, 0px));
  }
  a { color: var(--brand); }
  :focus-visible { outline: 2px solid var(--brand); outline-offset: 2px; }
  h1 { font-size: 22px; font-weight: 600; letter-spacing: -.02em; text-wrap: balance; }
  h2 { font-size: 14px; font-weight: 600; letter-spacing: -.005em; }
  code, kbd { font-family: var(--mono); }
  .num, .k-val, td.num, .kv dd { font-variant-numeric: tabular-nums; }
  svg { stroke-linecap: round; stroke-linejoin: round; }
  @media (prefers-reduced-motion: no-preference) {
    @view-transition { navigation: auto; }
    ::view-transition-old(root), ::view-transition-new(root) { animation-duration: 120ms; }
  }
}

/* ======================================================================== layout */
@layer layout {
  /* Menu latéral fixe + colonne principale. Sans menu (connexion, erreurs) : pleine largeur. */
  body:has(.side) { padding-left: var(--side-w); }
  .top {
    position: sticky; top: 0; z-index: 40; height: var(--top-h);
    display: flex; align-items: center; gap: 12px; padding: 0 28px;
    background: color-mix(in srgb, var(--surface) 92%, transparent);
    backdrop-filter: blur(8px);
    border-bottom: 1px solid var(--border);
  }
  .top-right { margin-left: auto; display: flex; gap: 8px; align-items: center; }
  .page { max-width: 1240px; margin: 0 auto; padding: 26px 28px 56px; }
  .page-head { display: flex; align-items: flex-end; justify-content: space-between; gap: 14px; flex-wrap: wrap; margin-bottom: 20px; }
  .page-head .sub { color: var(--ink-faint); font-size: 13px; margin-top: 3px; }
  .grid-side { display: grid; grid-template-columns: 1.55fr 1fr; gap: 14px; align-items: start; }
  .grid-side > * { min-width: 0; }
  .kpis { display: grid; grid-template-columns: repeat(auto-fill, minmax(230px, 1fr)); gap: 14px; margin-bottom: 18px; }
  @media (max-width: 1100px) { .grid-side { grid-template-columns: 1fr; } }
}

/* ======================================================================== components */
@layer components {
  /* --- menu latéral --- */
  .side {
    position: fixed; left: 0; top: 0; bottom: 0; width: var(--side-w); z-index: 50;
    display: flex; flex-direction: column; gap: 2px; padding: 14px 10px 12px;
    background: var(--side); border-right: 1px solid var(--border); overflow-y: auto;
  }
  .side-brand { display: flex; align-items: center; gap: 10px; padding: 4px 8px 14px; }
  .logo { width: 28px; height: 28px; flex: none; border-radius: var(--r); background: var(--brand); color: var(--on-brand); display: grid; place-items: center; font-weight: 600; font-size: 13px; }
  .side-brand-txt { line-height: 1.25; min-width: 0; }
  .side-brand-txt b { display: block; font-weight: 600; font-size: 13.5px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
  .side-brand-txt span { font-size: 12px; color: var(--ink-faint); }
  .side-client { padding: 0 4px 10px; }
  .side-client .select { width: 100%; }
  .side-label { font-size: 11px; color: var(--ink-faint); padding: 14px 10px 4px; letter-spacing: .02em; }
  .side-link {
    position: relative; display: flex; align-items: center; gap: 10px; padding: 7px 10px; border-radius: var(--r);
    color: var(--ink-soft); text-decoration: none; font-size: 13.5px; transition: background var(--t-fast), color var(--t-fast);
  }
  .side-link:hover { background: var(--side-hover); color: var(--ink); }
  .side-link[aria-current="page"] { background: var(--surface); color: var(--ink); font-weight: 500; box-shadow: 0 0 0 1px var(--border); }
  .side-link[aria-current="page"]::before { content: ""; position: absolute; left: -10px; top: 8px; bottom: 8px; width: 2px; border-radius: 2px; background: var(--brand); }
  .side-link svg { width: 16px; height: 16px; flex: none; fill: none; stroke: currentColor; stroke-width: 1.6; }
  .side-link kbd { margin-left: auto; font-size: 10.5px; color: var(--ink-faint); opacity: 0; transition: opacity var(--t-fast); }
  .side-link:hover kbd { opacity: 1; }
  .side-foot { margin-top: auto; padding: 10px 4px 0 8px; border-top: 1px solid var(--border); display: flex; align-items: center; gap: 8px; }
  .side-foot form { margin: 0; }
  .avatar { width: 28px; height: 28px; flex: none; border-radius: 50%; background: var(--brand-soft); color: var(--brand); display: grid; place-items: center; font-size: 11.5px; font-weight: 600; }
  .side-who { min-width: 0; flex: 1; line-height: 1.25; font-size: 12.5px; }
  .side-who span { display: block; color: var(--ink-faint); font-size: 11.5px; }
  .side-who .side-mail { color: var(--ink); font-size: 12.5px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
  .icon-btn { width: 30px; height: 30px; flex: none; display: grid; place-items: center; border: none; background: none; border-radius: var(--r); color: var(--ink-soft); cursor: pointer; text-decoration: none; font-size: 14px; }
  .icon-btn:hover { background: var(--side-hover); color: var(--ink); }
  .icon-btn svg { width: 16px; height: 16px; fill: none; stroke: currentColor; stroke-width: 1.6; }
  .side-backdrop { display: none; }

  /* --- barre du haut --- */
  .crumbs { display: flex; align-items: center; gap: 8px; font-size: 13.5px; color: var(--ink-faint); min-width: 0; white-space: nowrap; }
  .crumbs a { color: var(--ink-soft); text-decoration: none; }
  .crumbs a:hover { color: var(--ink); }
  .crumbs b { color: var(--ink); font-weight: 500; overflow: hidden; text-overflow: ellipsis; }
  .crumbs .sep { color: var(--border-strong); }
  .search-btn {
    display: flex; align-items: center; gap: 8px; width: 290px; padding: 6px 10px;
    border: 1px solid var(--border); border-radius: var(--r); background: var(--surface-2);
    color: var(--ink-faint); font-size: 13px; cursor: text; white-space: nowrap; overflow: hidden; transition: border-color var(--t-fast);
  }
  .search-btn:hover { border-color: var(--border-strong); }
  .search-btn svg { width: 14px; height: 14px; flex: none; fill: none; stroke: currentColor; stroke-width: 1.8; }
  .search-btn span { overflow: hidden; text-overflow: ellipsis; }
  .search-btn kbd { margin-left: auto; font-size: 10.5px; border: 1px solid var(--border); border-radius: var(--r-sm); padding: 0 5px; }

  /* sélecteur de période */
  .periode { position: relative; }
  .periode summary { list-style: none; white-space: nowrap; }
  .periode summary::-webkit-details-marker { display: none; }
  .periode summary b { font-weight: 500; }
  .periode-dates { color: var(--ink-faint); }
  /* écran moyen : les dates restent dans le sous-titre de la page, le bouton garde le libellé */
  @media (max-width: 1180px) { .periode-dates { display: none; } }
  .periode summary svg { width: 14px; height: 14px; fill: none; stroke: currentColor; stroke-width: 1.8; transition: transform var(--t-fast); }
  .periode[open] summary svg { transform: rotate(180deg); }
  .pop {
    position: absolute; right: 0; top: calc(100% + 6px); z-index: 60; width: 280px; padding: 6px;
    background: var(--surface); border: 1px solid var(--border-strong); border-radius: var(--r); box-shadow: var(--shadow-pop);
  }
  @media (prefers-reduced-motion: no-preference) { .periode[open] .pop { animation: pop-in 140ms ease-out; } }
  @keyframes pop-in { from { opacity: 0; transform: translateY(-4px); } }
  .pop-item { display: block; padding: 7px 10px; border-radius: var(--r-sm); color: var(--ink); text-decoration: none; font-size: 13.5px; }
  .pop-item:hover { background: var(--surface-2); }
  .pop-item[aria-current="true"] { background: var(--brand-soft); color: var(--brand); font-weight: 500; }
  .pop-dates { border-top: 1px solid var(--border); margin-top: 6px; padding: 10px 6px 4px; display: grid; gap: 8px; }
  .pop-label { font-size: 11.5px; color: var(--ink-faint); }
  .pop-row { display: grid; grid-template-columns: 1fr 1fr; gap: 6px; }
  .pop-row .dateinput { min-width: 0; width: 100%; }

  /* indicateur de fraîcheur des données */
  .fraicheur { position: relative; }
  .fraicheur summary { list-style: none; white-space: nowrap; gap: 8px; color: var(--ink-soft); }
  .fraicheur summary::-webkit-details-marker { display: none; }
  .fr-ts { color: var(--ink-faint); }
  .pulse { width: 8px; height: 8px; border-radius: 50%; background: var(--ink-faint); flex: none; position: relative; }
  .etat-live .pulse { background: var(--d-green); }
  .etat-demo .pulse { background: var(--d-blue); }
  .etat-erreur .pulse { background: var(--d-red); }
  @media (prefers-reduced-motion: no-preference) {
    .etat-live .pulse::after, .etat-demo .pulse::after {
      content: ""; position: absolute; inset: -3px; border-radius: 50%; border: 2px solid currentColor; opacity: 0;
      animation: pulse 2.4s ease-out infinite;
    }
    .etat-live .pulse::after { color: var(--d-green); }
    .etat-demo .pulse::after { color: var(--d-blue); }
    .pulse.bip::after { animation: pulse 0.9s ease-out 2; }
  }
  @keyframes pulse { 0% { opacity: .7; transform: scale(.6); } 100% { opacity: 0; transform: scale(1.8); } }
  .pop-sources { width: 320px; display: grid; gap: 2px; padding: 10px; }
  .src { display: grid; grid-template-columns: 10px 1fr auto; gap: 2px 8px; align-items: center; padding: 6px 4px; font-size: 13px; }
  .src + .src { border-top: 1px solid var(--border); }
  .src-dot { width: 7px; height: 7px; border-radius: 50%; background: var(--ink-faint); }
  .src-actif .src-dot { background: var(--d-green); }
  .src-erreur .src-dot { background: var(--d-red); }
  .src-nom em { font-style: normal; color: var(--ink-faint); font-size: 12px; }
  .src-etat { font-size: 12px; color: var(--ink-faint); white-space: nowrap; }
  .src-erreur .src-etat { color: var(--d-red); }
  .src-msg { grid-column: 2 / -1; font-size: 12px; color: var(--d-red); }
  .pop-note { font-size: 12px; color: var(--ink-faint); margin: 6px 4px 4px; line-height: 1.45; }
  .maj-dispo {
    position: fixed; left: 50%; top: calc(var(--top-h) + 10px); transform: translateX(-50%); z-index: 70;
    display: flex; align-items: center; gap: 10px; padding: 7px 8px 7px 14px; border-radius: var(--r);
    background: var(--ink); color: var(--ground); font-size: 13px; box-shadow: var(--shadow-pop);
  }
  .maj-dispo button { border: none; border-radius: var(--r-sm); background: var(--brand); color: var(--on-brand); padding: 4px 10px; cursor: pointer; font-size: 12.5px; }
  @media (prefers-reduced-motion: no-preference) { .maj-dispo { animation: pop-in 160ms ease-out; } .k-val.flash { animation: flash 1.6s ease-out; } }
  @keyframes flash { 0% { background: var(--brand-soft); box-shadow: 0 0 0 4px var(--brand-soft); } 100% { background: transparent; box-shadow: 0 0 0 4px transparent; } }
  .k-val { border-radius: var(--r-sm); }

  /* formulaires en ligne (tableaux admin), relevé */
  .form-ligne { display: flex; gap: 8px; align-items: center; flex-wrap: wrap; margin: 0; }
  .check { display: inline-flex; gap: 6px; align-items: center; font-size: 13px; color: var(--ink-soft); }
  .w-num { width: 110px; } .w-dev { width: 64px; text-transform: uppercase; }
  .sec-titre { font-size: 13px; font-weight: 600; margin: 16px 0 8px; display: flex; gap: 8px; align-items: baseline; flex-wrap: wrap; }
  tfoot td { border-top: 1px solid var(--border-strong); border-bottom: none; }
  details.lignes summary { cursor: pointer; }
  details.lignes .ligne { display: flex; justify-content: space-between; gap: 12px; font-size: 12px; color: var(--ink-soft); padding: 2px 0 2px 14px; }
  .lib-l { flex: 1; min-width: 0; }
  .pill.s-a_valider, .pill.s-a_faire, .pill.s-ouvert, .pill.s-demande { background: var(--d-amber-soft); color: var(--d-amber); }
  .pill.s-valide, .pill.s-en_cours_lot, .pill.s-etiquette, .pill.s-recu, .pill.s-en_transit, .pill.s-en_douane, .pill.s-envoyee, .pill.s-confirmee, .pill.s-en_production, .pill.s-prete, .pill.s-expediee, .pill.s-preparation, .pill.s-remboursement_a_valider { background: var(--d-blue-soft); color: var(--d-blue); }
  .pill.s-paye, .pill.s-fait, .pill.s-clos, .pill.s-dedouanee, .pill.s-livree_imp, .pill.s-recue, .pill.s-accepte { background: var(--d-green-soft); color: var(--d-green); }
  .pill.s-annule, .pill.s-refuse, .pill.s-brouillon, .pill.s-sans_objet { background: var(--surface-2); color: var(--ink-soft); box-shadow: inset 0 0 0 1px var(--border); }

  /* paiements, import */
  td.actions { display: flex; gap: 6px; flex-wrap: wrap; align-items: flex-start; white-space: normal; min-width: 160px; }
  details.payer { position: relative; }
  details.payer summary { list-style: none; }
  details.payer summary::-webkit-details-marker { display: none; }
  .form-payer { display: grid; gap: 6px; margin-top: 6px; padding: 10px; border: 1px solid var(--border); border-radius: var(--r); background: var(--surface-2); max-width: 280px; }
  .form-payer input[type=file] { font-size: 12px; max-width: 100%; }
  .lot { margin-top: 14px; padding-top: 14px; border-top: 1px solid var(--border); }
  .journal .event { grid-template-columns: 110px 1fr auto; }
  .exps { display: grid; gap: 12px; }
  .exp { border: 1px solid var(--border); border-radius: var(--r); padding: 14px; display: grid; gap: 10px; }
  .exp-tete { display: grid; gap: 2px; }
  .checklist { display: flex; flex-wrap: wrap; gap: 6px; }
  .ck { font-size: 12px; padding: 2px 8px; border-radius: var(--r-sm); background: var(--d-amber-soft); color: var(--d-amber); }
  .ck.ok { background: var(--d-green-soft); color: var(--d-green); }
  .exp-debours .row { padding: 6px 2px; }
  .field.checks { flex-direction: row; flex-wrap: wrap; gap: 10px; }
  .field textarea { width: 100%; padding: 8px 10px; border: 1px solid var(--border); border-radius: var(--r); background: var(--surface); color: var(--ink); font-family: var(--mono); font-size: 13px; }

  /* étapes d'un retour */
  /* --- documents (section réutilisable + page Documents) --- */
  .sr-only { position: absolute; width: 1px; height: 1px; overflow: hidden; clip: rect(0 0 0 0); white-space: nowrap; }
  .docs { margin-top: 16px; }
  .docs-compact { margin-top: 10px; }
  .docs-head { display: flex; align-items: center; justify-content: space-between; gap: 10px; }
  .docs-head .sec-titre { margin: 0; }
  .docs-attendus { list-style: none; margin: 8px 0 0; padding: 0; display: flex; flex-wrap: wrap; gap: 6px; font-size: 12.5px; }
  .docs-attendus li { display: inline-flex; gap: 6px; padding: 3px 8px; border-radius: var(--r-sm); background: var(--surface-2); box-shadow: inset 0 0 0 1px var(--border); }
  .docs-liste { list-style: none; margin: 8px 0 0; padding: 0; }
  .docs-liste > li { display: flex; flex-wrap: wrap; align-items: baseline; gap: 4px 10px; padding: 8px 2px; border-bottom: 1px solid var(--border); font-size: 13px; min-width: 0; }
  .docs-liste > li:last-child { border-bottom: none; }
  .doc-nom { font-weight: 500; color: var(--ink); overflow-wrap: anywhere; min-width: 0; }
  .doc-liens { display: flex; flex-wrap: wrap; gap: 4px; flex-basis: 100%; }
  .doc-liens .chip, .docs-liste button.chip { color: var(--ink-soft); background: var(--surface-2); box-shadow: inset 0 0 0 1px var(--border); text-decoration: none; white-space: normal; border: 0; cursor: pointer; font: inherit; font-size: 12px; }
  .doc-admin { flex-basis: 100%; }
  .docs-ajout { margin-top: 10px; flex-wrap: wrap; }
  .docs-ajout input[type=file] { max-width: 100%; min-width: 0; }
  .fil { display: flex; flex-wrap: wrap; align-items: center; gap: 6px; font-size: 12.5px; color: var(--ink-faint); margin-bottom: 4px; }
  .fil a { color: var(--ink-soft); }
  .fil .sep { color: var(--border-strong); }
  .dossiers { display: grid; grid-template-columns: repeat(auto-fill, minmax(190px, 1fr)); gap: 10px; margin-bottom: 18px; }
  .dossier { display: flex; align-items: center; gap: 10px; padding: 14px; border-radius: var(--r-md, 10px); background: var(--surface); box-shadow: inset 0 0 0 1px var(--border); color: var(--ink); text-decoration: none; font-weight: 500; }
  .dossier:hover { box-shadow: inset 0 0 0 1px var(--border-strong); }
  .dossier svg { width: 20px; height: 20px; flex: none; fill: none; stroke: var(--ink-faint); stroke-width: 1.6; }
  .dossier em { margin-left: auto; font-style: normal; font-family: var(--mono); font-size: 12px; color: var(--ink-faint); }
  .attendus-type { border-bottom: 1px solid var(--border); padding: 8px 0; }
  .attendus-type summary { cursor: pointer; font-weight: 500; }
  .attendus-grille { display: grid; grid-template-columns: repeat(auto-fill, minmax(200px, 1fr)); gap: 4px 12px; padding: 10px 0; }
  .doc-compte { font-family: var(--mono); font-size: 12px; color: var(--ink-soft); }
  .etapes-retour { display: flex; flex-wrap: wrap; gap: 4px; list-style: none; margin: 0; padding: 0; font-size: 12px; }
  .etapes-retour li { padding: 3px 8px; border-radius: var(--r-sm); background: var(--surface-2); color: var(--ink-faint); box-shadow: inset 0 0 0 1px var(--border); }
  .etapes-retour li.fait { background: var(--d-green-soft); color: var(--d-green); box-shadow: none; }
  .etapes-retour li.actuel { background: var(--d-blue-soft); color: var(--d-blue); font-weight: 600; box-shadow: none; }

  /* alertes, mise en place */
  .alertes { border-left: 2px solid var(--d-amber); }
  .alerte-n { min-width: 28px; text-align: left; font-weight: 600; color: var(--d-amber); }
  .progression { height: 6px; border-radius: 3px; background: var(--surface-2); box-shadow: inset 0 0 0 1px var(--border); overflow: hidden; }
  .progression b { display: block; height: 100%; background: var(--brand); border-radius: 3px; }

  /* page Connexions */
  .ok-cle { border-left-color: var(--d-green); }
  code.cle { display: inline-block; margin-top: 6px; font-size: 13px; user-select: all; overflow-wrap: anywhere; }
  .etapes { margin: 0 0 14px; padding-left: 20px; font-size: 13px; color: var(--ink-soft); display: grid; gap: 6px; }
  .etapes code, .note code, .kv code { font-size: 12px; background: var(--surface-2); border: 1px solid var(--border); border-radius: var(--r-sm); padding: 0 4px; overflow-wrap: anywhere; }
  .connexion { padding: 12px 0; border-top: 1px solid var(--border); }
  .connexion .form-section { display: flex; align-items: center; gap: 8px; margin: 0; }

  /* historique d'une commande */
  .events { margin-top: 14px; border-top: 1px solid var(--border); padding-top: 10px; }
  .events h3 { font-size: 12.5px; color: var(--ink-faint); font-weight: 500; margin: 0 0 6px; }
  .event { display: grid; grid-template-columns: 96px 1fr auto; gap: 8px; padding: 5px 0; font-size: 12.5px; }
  .event time { font-family: var(--mono); font-size: 12px; color: var(--ink-faint); }
  .event .src-tag { font-size: 11px; color: var(--ink-faint); }

  /* palette de recherche */
  .cmdk {
    width: min(600px, calc(100vw - 32px)); max-width: none; margin: 12vh auto auto; padding: 0;
    border: 1px solid var(--border-strong); border-radius: var(--r); background: var(--surface); color: var(--ink); box-shadow: var(--shadow-pop); overflow: hidden;
  }
  .cmdk::backdrop { background: rgb(0 0 0 / .32); }
  @media (prefers-reduced-motion: no-preference) { .cmdk[open] { animation: pop-in 140ms ease-out; } }
  .cmdk-in { display: flex; align-items: center; gap: 10px; padding: 12px 16px; border-bottom: 1px solid var(--border); }
  .cmdk-in svg { width: 16px; height: 16px; flex: none; fill: none; stroke: var(--ink-faint); stroke-width: 1.8; }
  .cmdk-in input { flex: 1; border: none; outline: none; background: none; font-size: 15px; padding: 4px 0; }
  .cmdk-list { max-height: 50vh; overflow-y: auto; padding-bottom: 6px; }
  .cmdk-sec { font-size: 11.5px; color: var(--ink-faint); padding: 10px 16px 4px; }
  .cmdk-item { display: flex; align-items: center; gap: 10px; padding: 8px 16px; font-size: 13.5px; color: var(--ink); text-decoration: none; cursor: pointer; }
  .cmdk-item[aria-selected="true"] { background: var(--brand-soft); }
  .cmdk-item .mono { font-family: var(--mono); font-size: 12.5px; }
  .cmdk-item em { font-style: normal; color: var(--ink-faint); font-size: 12px; margin-left: auto; white-space: nowrap; }
  .cmdk-vide { padding: 14px 16px; color: var(--ink-faint); font-size: 13px; }
  .cmdk-foot { display: flex; gap: 14px; flex-wrap: wrap; padding: 8px 16px; border-top: 1px solid var(--border); font-size: 11.5px; color: var(--ink-faint); }

  /* --- boutons et champs --- */
  .btn {
    display: inline-flex; align-items: center; justify-content: center; gap: 6px;
    padding: 6px 11px; border: 1px solid var(--border); border-radius: var(--r);
    background: var(--surface); color: var(--ink); font-size: 13px; cursor: pointer; text-decoration: none;
    transition: border-color var(--t-fast), background var(--t-fast);
  }
  .btn:hover { border-color: var(--border-strong); background: var(--surface-2); }
  .btn-primary { background: var(--brand); border-color: var(--brand); color: var(--on-brand); }
  .btn-primary:hover { background: var(--brand); opacity: .92; }
  .btn-small { padding: 3px 8px; font-size: 12px; }
  .select, .dateinput {
    padding: 6px 9px; border: 1px solid var(--border); border-radius: var(--r);
    background: var(--surface); color: var(--ink); font-size: 13px;
  }

  /* --- onglets --- */
  .tabs { display: flex; gap: 2px; border-bottom: 1px solid var(--border); margin-bottom: 18px; overflow-x: auto; }
  .tab { padding: 8px 12px; font-size: 13.5px; color: var(--ink-soft); border: none; background: none; border-bottom: 2px solid transparent; margin-bottom: -1px; cursor: pointer; white-space: nowrap; text-decoration: none; }
  .tab:hover { color: var(--ink); }
  .tab[aria-selected="true"] { color: var(--ink); border-bottom-color: var(--ink); font-weight: 500; }

  /* --- cartes KPI --- */
  .kpi {
    container-type: inline-size;
    display: flex; flex-direction: column; gap: 6px; min-width: 0;
    background: var(--surface); border: 1px solid var(--border); border-radius: var(--r); padding: 16px 18px 14px;
  }
  .kpi.lead { border-left: 2px solid var(--brand); }
  .kpi.remu { border-left: 2px solid var(--d-amber); }
  .k-top { display: flex; justify-content: space-between; gap: 8px; font-size: 12.5px; color: var(--ink-soft); }
  .k-val { font-size: 26px; font-weight: 600; letter-spacing: -.025em; line-height: 1.2; overflow-wrap: anywhere; }
  .k-val.txt { font-size: 18px; letter-spacing: -.01em; }
  .k-row { display: flex; align-items: center; gap: 8px; flex-wrap: wrap; font-size: 12.5px; min-height: 20px; }
  .k-second { color: var(--ink-faint); }
  .k-foot { font-size: 12px; color: var(--ink-faint); }
  .chip { display: inline-flex; align-items: center; gap: 3px; padding: 1px 6px; border-radius: var(--r-sm); font-weight: 500; font-size: 12px; white-space: nowrap; }
  .chip.tone-good { color: var(--d-green); background: var(--d-green-soft); }
  .chip.tone-bad { color: var(--d-red); background: var(--d-red-soft); }
  .chip.tone-neutral { color: var(--ink-soft); background: var(--surface-2); box-shadow: inset 0 0 0 1px var(--border); }
  .spark { display: block; margin-top: 2px; }
  .spark svg { display: block; width: 100%; height: 34px; }
  @container (width < 140px) { .spark { display: none; } }
  .kpi-link { color: inherit; text-decoration: none; cursor: pointer; transition: border-color var(--t-fast), box-shadow var(--t-fast); }
  .kpi-link:hover { border-color: var(--border-strong); }
  .kpi-link:active { transform: translateY(1px); }
  .kpi-more { color: var(--brand); opacity: 0; transition: opacity var(--t-fast); white-space: nowrap; }
  .kpi-link:hover .kpi-more, .kpi-link:focus-visible .kpi-more { opacity: 1; }
  @media (hover: none) { .kpi-more { opacity: .8; } }

  /* apparition douce au chargement et à chaque changement de période */
  @media (prefers-reduced-motion: no-preference) {
    .kpi, .panel { animation: rise 300ms ease-out backwards; } /* backwards : rien ne reste après l'animation (sinon les cartes passent au-dessus des fenêtres modales) */
    .kpis > :nth-child(2) { animation-delay: 30ms; } .kpis > :nth-child(3) { animation-delay: 60ms; }
    .kpis > :nth-child(4) { animation-delay: 90ms; } .kpis > :nth-child(n+5) { animation-delay: 120ms; }
    .panel { animation-delay: 120ms; }
  }
  @keyframes rise { from { opacity: 0; transform: translateY(4px); } }

  /* --- panneaux --- */
  .panel { background: var(--surface); border: 1px solid var(--border); border-radius: var(--r); padding: 18px; margin-bottom: 14px; min-width: 0; }
  .panel-head { display: flex; align-items: center; justify-content: space-between; gap: 10px; margin-bottom: 14px; flex-wrap: wrap; }
  .eyebrow { font-size: 12px; color: var(--ink-faint); font-weight: 400; margin-top: 1px; }

  /* barres de répartition (statuts…) */
  .rows { display: flex; flex-direction: column; }
  .row { display: flex; align-items: center; gap: 10px; padding: 9px 4px; border-bottom: 1px solid var(--border); font-size: 13px; color: var(--ink); text-decoration: none; transition: background var(--t-fast); }
  .row:last-child { border-bottom: none; }
  a.row:hover { background: var(--surface-2); }
  .row .dot { width: 7px; height: 7px; border-radius: 50%; flex: none; }
  .row .lib { width: 96px; flex: none; }
  .row .bar { flex: 1; height: 6px; border-radius: 3px; background: var(--surface-2); box-shadow: inset 0 0 0 1px var(--border); overflow: hidden; }
  .row .bar b { display: block; height: 100%; border-radius: 3px; }
  .row .v { font-family: var(--mono); font-size: 12.5px; min-width: 48px; text-align: right; }
  .row .p { font-family: var(--mono); font-size: 12px; color: var(--ink-faint); min-width: 40px; text-align: right; }

  /* --- tableaux --- */
  .tablewrap { overflow-x: auto; }
  table { width: 100%; border-collapse: collapse; font-size: 13px; }
  th, td { text-align: left; padding: 8px 10px; height: 38px; border-bottom: 1px solid var(--border); white-space: nowrap; }
  th { font-size: 12px; color: var(--ink-faint); font-weight: 500; position: sticky; top: 0; background: var(--surface); }
  tbody tr:last-child td { border-bottom: none; }
  tr[data-href] { cursor: pointer; }
  tbody tr { transition: background var(--t-fast); }
  tr[data-href]:hover td { background: var(--surface-2); }
  td.num, th.num { text-align: right; font-family: var(--mono); font-size: 12.5px; }
  td.ref { font-family: var(--mono); font-size: 12.5px; color: var(--ink-soft); }
  td.ref a { color: inherit; text-decoration: none; }
  td.ref a:hover { color: var(--brand); text-decoration: underline; }

  /* --- pastilles de statut --- */
  .pill { display: inline-flex; align-items: center; gap: 5px; padding: 2px 8px; border-radius: var(--r-sm); font-size: 12px; font-weight: 500; text-decoration: none; }
  .pill::before { content: ""; width: 6px; height: 6px; border-radius: 50%; background: currentColor; flex: none; }
  .pill.s-livree, .pill.s-payee, .pill.s-remis { background: var(--d-green-soft); color: var(--d-green); }
  .pill.s-en_cours, .pill.s-en_attente { background: var(--d-blue-soft); color: var(--d-blue); }
  .pill.s-a_expedier { background: var(--d-amber-soft); color: var(--d-amber); }
  .pill.s-retournee, .pill.s-annulee, .pill.s-en_retard, .pill.s-perdu { background: var(--d-red-soft); color: var(--d-red); }
  /* forme distincte pour ce qui sort du flux normal (la couleur n'est jamais seule) */
  .pill.s-retournee::before, .pill.s-annulee::before, .pill.s-en_retard::before, .pill.s-perdu::before { border-radius: 1px; transform: rotate(45deg); }

  .filters { display: flex; gap: 6px; flex-wrap: wrap; margin-bottom: 14px; }
  .chip-f {
    padding: 5px 10px; border: 1px solid var(--border); border-radius: var(--r);
    background: var(--surface); color: var(--ink-soft); font-size: 13px; text-decoration: none; transition: border-color var(--t-fast);
  }
  .chip-f:hover { border-color: var(--border-strong); color: var(--ink); }
  .chip-f[aria-current="true"] { background: var(--ink); border-color: var(--ink); color: var(--ground); }
  .chip-f em { font-style: normal; font-family: var(--mono); font-size: 12px; opacity: .7; margin-left: 2px; }

  /* --- graphiques --- */
  .chart { position: relative; width: 100%; }
  .chart svg { display: block; width: 100%; height: auto; overflow: visible; }
  .chart .hit { outline: none; }
  .chart .hit:focus-visible { fill: var(--brand-soft); fill-opacity: .5; }
  .chart .xhair { opacity: 0; pointer-events: none; transition: opacity var(--t-fast); }
  .chart.hover .xhair { opacity: 1; }
  .legend { display: flex; gap: 14px; flex-wrap: wrap; font-size: 12px; color: var(--ink-soft); margin-top: 10px; }
  .legend i { width: 10px; height: 10px; border-radius: 2px; display: inline-block; margin-right: 6px; vertical-align: -1px; }
  .legend i.dashed { width: 14px; height: 0; border-top: 2px dashed; border-radius: 0; vertical-align: middle; }
  .chart-data { margin-top: 8px; font-size: 12px; color: var(--ink-faint); }
  .chart-data summary { cursor: pointer; width: max-content; }
  .chart-data table { margin-top: 6px; font-size: 12px; }
  .chart-data th, .chart-data td { height: auto; padding: 4px 8px; }
  .tip {
    position: absolute; pointer-events: none; opacity: 0; transform: translate(-50%, -100%);
    background: var(--ink); color: var(--ground); padding: 6px 9px; border-radius: var(--r);
    font-size: 12px; white-space: nowrap; z-index: 20; box-shadow: var(--shadow); line-height: 1.45; transition: opacity var(--t-fast);
  }
  .tip b { font-family: var(--mono); font-weight: 500; }

  /* --- carte (Leaflet + OpenStreetMap) --- */
  .map { height: clamp(320px, 50vh, 540px); border: 1px solid var(--border); border-radius: var(--r); background: var(--surface-2); z-index: 0; }
  .map.small { height: 260px; }
  .map .leaflet-tooltip { font-family: var(--font); font-size: 12px; line-height: 1.45; }
  .map-msg { display: grid; place-items: center; height: 100%; color: var(--ink-soft); font-size: 13px; padding: 20px; text-align: center; }
  .maplegend { display: flex; gap: 14px; flex-wrap: wrap; font-size: 12px; color: var(--ink-soft); margin-top: 10px; }
  .maplegend i { width: 9px; height: 9px; border-radius: 50%; display: inline-block; margin-right: 5px; }

  /* --- listes et définitions --- */
  .timeline li { position: relative; padding: 0 0 16px 22px; border-left: 1px solid var(--border); }
  .timeline li:last-child { border-left-color: transparent; padding-bottom: 0; }
  .timeline li::before { content: ""; position: absolute; left: -4.5px; top: 5px; width: 8px; height: 8px; border-radius: 50%; background: var(--border-strong); }
  .timeline li.done::before { background: var(--d-green); }
  .timeline .t-lab { font-size: 13px; }
  .timeline .t-date { font-size: 12px; color: var(--ink-faint); font-family: var(--mono); }
  .kv { display: grid; grid-template-columns: auto 1fr; gap: 8px 16px; font-size: 13px; }
  .kv dt { color: var(--ink-soft); }
  .kv dd { text-align: right; font-family: var(--mono); font-size: 12.5px; overflow-wrap: anywhere; }
  .kv dd.plain { font-family: var(--font); font-size: 13px; }
  .kv .total { font-weight: 600; color: var(--ink); }
  .liste li + li { border-top: 1px solid var(--border); }
  .liste a { display: flex; flex-direction: column; gap: 2px; padding: 7px 2px; color: var(--ink); text-decoration: none; min-height: 44px; }
  .liste-main { display: flex; justify-content: space-between; align-items: center; gap: 8px; }
  .liste-main .ref { font-family: var(--mono); font-size: 13px; }
  .liste-sub { display: flex; gap: 6px; font-size: 12px; color: var(--ink-soft); }
  .liste-sub .num { margin-left: auto; font-family: var(--mono); color: var(--ink); }
  .note {
    font-size: 12.5px; color: var(--ink-soft); background: var(--surface-2);
    border: 1px solid var(--border); border-left: 2px solid var(--d-amber);
    padding: 10px 12px; border-radius: var(--r-sm); margin-top: 12px;
  }
  .note.red { border-left-color: var(--d-red); }
  .empty-state {
    display: flex; align-items: center; justify-content: space-between; gap: 12px; flex-wrap: wrap;
    padding: 16px; border: 1px dashed var(--border-strong); border-radius: var(--r); color: var(--ink-soft); font-size: 13px;
  }
  .toast {
    position: fixed; left: 50%; bottom: calc(24px + var(--bottombar-h) + env(safe-area-inset-bottom, 0px)); transform: translateX(-50%);
    background: var(--ink); color: var(--ground); padding: 10px 16px; border-radius: var(--r); box-shadow: var(--shadow);
    font-size: 13px; z-index: 90; transition: opacity 200ms;
  }
  .toast.hide { opacity: 0; }

  /* --- formulaires --- */
  .admin-zone { border-top: 2px solid var(--d-amber); }
  .form-grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(170px, 1fr)); gap: 12px; align-items: end; }
  .form-section { grid-column: 1 / -1; margin-top: 6px; }
  .field { display: flex; flex-direction: column; gap: 5px; min-width: 0; }
  .field label { font-size: 12px; color: var(--ink-soft); }
  .field input, .field select {
    width: 100%; padding: 8px 10px; border: 1px solid var(--border); border-radius: var(--r);
    font-size: 14px; background: var(--surface); color: var(--ink);
  }
  .inline-form { display: inline; margin: 0; }

  /* --- connexion, erreurs --- */
  .login-wrap { min-height: 100vh; display: grid; place-items: center; padding: 24px 20px; }
  .login-card { width: 100%; max-width: 380px; background: var(--surface); border: 1px solid var(--border); border-radius: var(--r); padding: 28px 26px; box-shadow: var(--shadow); }
  .login-card h1 { font-size: 20px; margin: 0 0 20px; }
  .login-card .sub { color: var(--ink-soft); font-size: 13px; margin-bottom: 20px; }
  .login-card .field { margin-bottom: 13px; }
  .login-card .field input { padding: 10px 11px; font-size: 15px; }
  .error-msg { background: var(--d-red-soft); color: var(--d-red); padding: 9px 11px; border-radius: var(--r-sm); font-size: 13px; margin-bottom: 14px; }
  .accounts { display: flex; flex-direction: column; gap: 6px; margin-top: 10px; }
  .account {
    display: flex; align-items: center; justify-content: space-between; gap: 10px; width: 100%;
    padding: 9px 11px; border: 1px solid var(--border); border-radius: var(--r);
    background: var(--surface); cursor: pointer; text-align: left; font-size: var(--fs-sm);
  }
  .account:hover { border-color: var(--brand); }
  .account b { font-weight: 500; }
  .account em { font-style: normal; color: var(--ink-faint); font-size: 11.5px; font-family: var(--mono); }
  .role-badge { padding: 2px 8px; border-radius: var(--r-sm); background: var(--brand-soft); color: var(--brand); font-size: 11px; font-weight: 600; white-space: nowrap; }
  .error-card { max-width: 560px; margin: 80px auto; text-align: center; display: grid; gap: 12px; justify-items: center; }
  .error-card .ref-erreur { font-size: 12px; color: var(--ink-soft); }
  .error-card pre {
    white-space: pre-wrap; word-break: break-word; text-align: left; font-size: 11px; color: var(--ink-soft); justify-self: stretch;
    background: var(--surface-2); padding: 10px; border-radius: var(--r-sm); border: 1px solid var(--border);
  }
  .build-tag {
    position: fixed; right: 10px; bottom: calc(8px + var(--bottombar-h) + env(safe-area-inset-bottom, 0px)); font-size: 10px;
    color: var(--ink-faint); font-family: var(--mono); pointer-events: none; z-index: 30;
  }

  /* --- panneau de détail d'un KPI --- */
  .drawer {
    position: fixed; inset: 0 0 0 auto; margin: 0; height: 100dvh; max-height: none; max-width: 100vw;
    width: min(620px, 100vw); padding: 0; border: none; border-left: 1px solid var(--border-strong);
    background: var(--surface); color: var(--ink); box-shadow: -12px 0 32px rgb(0 0 0 / .14); overflow-y: auto; overscroll-behavior: contain;
  }
  .drawer::backdrop { background: rgb(0 0 0 / .3); }
  @media (prefers-reduced-motion: no-preference) {
    .drawer[open] { animation: drawer-in 200ms ease-out; }
    .drawer[open]::backdrop { animation: fade-in 200ms ease-out; }
  }
  @keyframes drawer-in { from { transform: translateX(30px); opacity: 0; } }
  @keyframes fade-in { from { opacity: 0; } }
  .drawer-load { padding: 24px; display: grid; gap: 12px; }
  .skel { height: 14px; border-radius: var(--r-sm); background: linear-gradient(90deg, var(--grid), var(--surface-2), var(--grid)); background-size: 200% 100%; animation: skel 1.2s linear infinite; }
  .skel.big { height: 36px; width: 50%; }
  .skel.chart { height: 180px; }
  @keyframes skel { to { background-position: -200% 0; } }
  .kpi-detail { padding: 22px 24px 28px; display: flex; flex-direction: column; gap: 10px; }
  .page .kpi-detail { max-width: 760px; margin: 0 auto; background: var(--surface); border: 1px solid var(--border); border-radius: var(--r); }
  .kd-head { display: flex; justify-content: space-between; align-items: flex-start; gap: 12px; }
  .kd-titre { font-size: 18px; margin-top: 2px; }
  .kd-valeur { display: flex; align-items: baseline; gap: 12px; flex-wrap: wrap; }
  .kd-val { font-size: 32px; font-weight: 600; letter-spacing: -.03em; font-variant-numeric: tabular-nums; }
  .kd-sous { font-size: 12.5px; color: var(--ink-faint); }
  .kd-def { font-size: 13px; color: var(--ink-soft); background: var(--surface-2); border: 1px solid var(--border); border-radius: var(--r); padding: 10px 12px; }
  .kd-cles { margin-top: 4px; }
  .kd-section { margin-top: 12px; }
  .kd-section h3 { font-size: 12.5px; color: var(--ink-faint); font-weight: 500; margin: 0 0 8px; }
  .kd-foot { margin-top: 16px; }

  /* navigation sans rechargement : barre de progression */
  .loadbar { position: fixed; top: 0; left: 0; height: 2px; width: 0; background: var(--brand); z-index: 100; opacity: 0; transition: width 400ms ease-out, opacity 200ms; }
  .loadbar.on { opacity: 1; width: 70%; }
  .loadbar.done { opacity: 0; width: 100%; transition: width 120ms, opacity 250ms 120ms; }

  /* barre du bas (mobile uniquement) */
  .bottombar { display: none; }
}

/* ======================================================================== mobile (couche la plus prioritaire) */
@layer utilities {
  @media (max-width: 900px) {
    :root { --bottombar-h: 62px; }
    body:has(.side) { padding-left: 0; }
    /* le menu latéral devient un tiroir ouvert par « Plus » */
    .side { transform: translateX(-100%); transition: transform 200ms ease-out; width: min(290px, 84vw); box-shadow: var(--shadow-pop); }
    body.side-open .side { transform: none; }
    body.side-open .side-backdrop { display: block; position: fixed; inset: 0; z-index: 45; background: rgb(0 0 0 / .32); }
    .side-link { padding: 11px 10px; }
    .side-link kbd { display: none; }
    .top { padding: 0 14px; gap: 8px; }
    .crumb-marque, .crumb-marque + .sep { display: none; }
    .search-btn { width: 36px; padding: 6px 10px; }
    .search-btn span, .search-btn kbd { display: none; }
    .periode-dates { display: none; }
    .fr-txt, .fr-ts { display: none; }
    .pop { position: fixed; left: 12px; right: 12px; top: calc(var(--top-h) + 6px + env(safe-area-inset-top, 0px)); width: auto; }
    .page { padding: 18px 14px 32px; }
    .kpis { grid-template-columns: 1fr; gap: 10px; }
    .kpis-2 { grid-template-columns: 1fr 1fr; gap: 8px; }
    .kpis-2 .kpi { padding: 11px 12px; }
    .kpis-2 .k-val { font-size: 21px; }
    td, th { height: 44px; }
    .bottombar {
      display: grid; grid-template-columns: repeat(5, 1fr);
      position: fixed; left: 0; right: 0; bottom: 0; z-index: 44;
      height: calc(var(--bottombar-h) + env(safe-area-inset-bottom, 0px));
      padding-bottom: env(safe-area-inset-bottom, 0px);
      background: var(--surface); border-top: 1px solid var(--border);
    }
    .bottombar a, .bottombar button {
      display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 3px;
      min-height: 44px; font-size: 10.5px; color: var(--ink-faint); text-decoration: none;
      background: none; border: none; cursor: pointer;
    }
    .bottombar svg { width: 20px; height: 20px; fill: none; stroke: currentColor; stroke-width: 1.6; }
    .bottombar [aria-current="page"] { color: var(--brand); font-weight: 500; }
    .drawer { width: 100vw; border-left: none; }
    .kpi-detail { padding: 16px 14px 24px; }
    .kd-val { font-size: 26px; }
    .cmdk { margin-top: 8vh; }
  }
  @media (max-width: 480px) {
    .row .lib { width: 84px; }
  }
}

/* ======================================================================== impression (rapport) */
@layer utilities {
  .rapport-tete { display: none; }
  .rapport-liste { margin: 8px 0 0; padding-left: 18px; display: grid; gap: 4px; font-size: 13px; }
  .rapport-table { margin-top: 8px; table-layout: auto; }
  .rapport-table th, .rapport-table td { white-space: normal; }
  .rapport-table td.num, .rapport-table th.num { white-space: nowrap; }
  .rapport h2 { margin-bottom: 4px; }
  @media print {
    @page { size: A4; margin: 14mm; }
    :root { color-scheme: light; --bottombar-h: 0px; }
    body { background: #fff; color: #000; padding: 0 !important; font-size: 11pt; }
    .side, .side-backdrop, .top, .bottombar, .build-tag, .toast, .no-print, .maj-dispo, .loadbar, .chart-data { display: none !important; }
    .page { max-width: none; padding: 0; }
    .rapport-tete { display: flex; justify-content: space-between; align-items: flex-end; border-bottom: 2px solid #000; padding-bottom: 8px; margin-bottom: 14px; }
    .rapport-meta { text-align: right; font-size: 9pt; color: #444; }
    .panel { border: 1px solid #ccc; break-inside: avoid; box-shadow: none; animation: none !important; }
    .grid-side { grid-template-columns: 1fr 1fr; }
    th, td { height: auto; padding: 4px 6px; }
  }
}