:root {
  --fond: #f4f5f7;
  --surface: #ffffff;
  --texte: #16202b;
  --doux: #5b6675;
  --bord: #d5dae1;
  --accent: #1d4f91;
  --accent-texte: #ffffff;
  --barre: #1d2a3a;
  --ok: #1f8a4c;
  --alerte: #c77700;
  --danger: #b3261e;
  --ue: #7b2d26;
  --ua: #d32f2f;
  --ur: #f2d600;
  --ud: #111111;
  --t1: #12897a;
  --t2: #d98a00;
  --t3: #1d7fd1;
  --t4: #d33b4a;
  --rayon: 10px;
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --fond: #0f141a; --surface: #18202a; --texte: #e8edf3; --doux: #9aa6b5; --bord: #2e3a48;
    --accent: #5b9bf0; --accent-texte: #0f141a; --barre: #0b1016; --ud: #000;
    --t1: #3cc2b0; --t2: #f0a830; --t3: #5aaef5; --t4: #f26a77;
    color-scheme: dark;
  }
}
:root[data-theme="dark"] {
  --fond: #0f141a; --surface: #18202a; --texte: #e8edf3; --doux: #9aa6b5; --bord: #2e3a48;
  --accent: #5b9bf0; --accent-texte: #0f141a; --barre: #0b1016; --ud: #000;
  --t1: #3cc2b0; --t2: #f0a830; --t3: #5aaef5; --t4: #f26a77;
  color-scheme: dark;
}

* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; }
body {
  background: var(--fond); color: var(--texte);
  font: 16px/1.4 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  -webkit-text-size-adjust: 100%;
}
button, input, textarea { font: inherit; color: inherit; }
h2 { margin: 0 0 12px; font-size: 1.2rem; }

/* Barre du haut */
.barre {
  position: sticky; top: 0; z-index: 20;
  display: flex; align-items: center; justify-content: space-between; gap: 8px;
  padding: 8px 16px; padding-top: max(8px, env(safe-area-inset-top));
  background: var(--barre); color: #fff;
}
.barre-gauche { display: flex; align-items: center; gap: 10px; min-width: 0; }
.marque { background: #fff; color: var(--barre); padding: 2px 8px; border-radius: 6px; font-weight: 800; letter-spacing: .05em; }
.evt { background: none; border: 0; color: #fff; padding: 6px 4px; cursor: pointer; text-align: left; min-width: 0;
  white-space: nowrap; overflow: hidden; text-overflow: ellipsis; font-weight: 600; }
.evt .poste { font-weight: 400; opacity: .8; }
.pastille { display: flex; align-items: center; gap: 6px; border: 1px solid #ffffff40; background: #ffffff14; color: #fff;
  border-radius: 999px; padding: 5px 12px; font-size: .85rem; cursor: pointer; white-space: nowrap; }
.pastille .point { width: 9px; height: 9px; border-radius: 50%; background: #9aa6b5; flex: none; }
.pastille[data-etat="ok"] .point { background: #3ddc84; }
.pastille[data-etat="attente"] .point, .pastille[data-etat="encours"] .point { background: #ffc94d; }
.pastille[data-etat="horsligne"] .point { background: #9aa6b5; }
.pastille[data-etat="erreur"] .point, .pastille[data-etat="code"] .point { background: #ff6b5e; }

/* Onglets */
.onglets { display: flex; gap: 4px; padding: 0 16px; background: var(--barre); overflow-x: auto; position: sticky; top: 48px; z-index: 19; }
.onglets button { flex: none; background: none; border: 0; color: #c9d3df; padding: 10px 14px; font-weight: 600; cursor: pointer; border-bottom: 3px solid transparent; }
.onglets button[aria-selected="true"] { color: #fff; border-bottom-color: #fff; }

main { padding: 16px; max-width: 1500px; margin: 0 auto; }

/* Boutons */
.btn { border: 1px solid var(--bord); background: var(--surface); border-radius: var(--rayon); padding: 10px 14px; font-weight: 600; cursor: pointer; min-height: 44px; }
.btn:active { transform: translateY(1px); }
.btn-principal { background: var(--accent); color: var(--accent-texte); border-color: var(--accent); }
.btn-danger { color: var(--danger); border-color: var(--danger); background: transparent; }
.btn-petit { padding: 6px 10px; min-height: 40px; font-size: .9rem; }
.outils { display: flex; flex-wrap: wrap; gap: 8px; margin-bottom: 12px; }

/* Mise en page liste / éditeur */
.split { display: grid; grid-template-columns: minmax(300px, 420px) 1fr; gap: 16px; align-items: start; }
.colonne-editeur { background: var(--surface); border: 1px solid var(--bord); border-radius: 14px; min-height: 60vh;
  position: sticky; top: 104px; max-height: calc(100vh - 120px); overflow-y: auto; }
.ed-vide { padding: 48px 24px; color: var(--doux); text-align: center; }
@media (max-width: 900px) {
  .split { grid-template-columns: 1fr; }
  .colonne-editeur { display: none; }
  body.edition .colonne-editeur { display: block; position: fixed; inset: 0; top: 0; z-index: 50; border-radius: 0; max-height: none; min-height: 0;
    padding-bottom: env(safe-area-inset-bottom); }
  body.edition { overflow: hidden; }
}
@media (min-width: 901px) { .retour { display: none; } }

/* Entête feuille / filtres */
.entete-feuille { background: var(--surface); border: 1px solid var(--bord); border-radius: var(--rayon); padding: 8px 12px; margin-bottom: 12px; }
.entete-feuille summary { cursor: pointer; font-weight: 600; color: var(--doux); }
.entete-feuille .grille-id { margin-top: 10px; }
.filtres { display: grid; gap: 8px; margin-bottom: 12px; }
.filtres input[type="search"] { width: 100%; padding: 10px 12px; border: 1px solid var(--bord); border-radius: var(--rayon); background: var(--surface); }
.seg.filtre { flex-wrap: wrap; }
.seg.filtre button { min-height: 36px; padding: 4px 10px; font-size: .9rem; }

/* Liste */
.liste { list-style: none; margin: 0; padding: 0; display: grid; gap: 8px; }
.liste .vide { color: var(--doux); padding: 24px 8px; text-align: center; }
.carte { width: 100%; display: flex; gap: 12px; align-items: flex-start; text-align: left; cursor: pointer;
  background: var(--surface); border: 1px solid var(--bord); border-radius: var(--rayon); padding: 10px 12px; }
.carte.active { outline: 3px solid var(--accent); outline-offset: -1px; }
.carte.evacue { opacity: .62; }
.carte-corps { display: grid; gap: 2px; min-width: 0; }
.carte-l1 b { font-size: 1.02rem; }
.carte-l1 .num, .carte-l2, .carte-l3 { color: var(--doux); font-size: .88rem; }
.marche-txt { color: var(--danger); font-weight: 600; }
.non-envoye { color: var(--alerte); font-weight: 600; }

/* Badges de catégorie */
.badge { flex: none; display: inline-grid; place-items: center; min-width: 44px; height: 32px; padding: 0 6px; border-radius: 6px; font-weight: 800; letter-spacing: .03em;
  background: var(--bord); color: var(--texte); }
.cat-UE { background: var(--ue); color: #fff; }
.cat-UA { background: var(--ua); color: #fff; }
.cat-UR { background: var(--ur); color: #111; }
.cat-UD { background: var(--ud); color: #fff; }

/* Éditeur */
.ed-tete { position: sticky; top: 0; z-index: 2; display: flex; align-items: center; gap: 12px; padding: 10px 16px; background: var(--surface); border-bottom: 1px solid var(--bord); }
.ed-resume { display: flex; align-items: center; gap: 10px; flex-wrap: wrap; }
.bloc { padding: 14px 16px; border-bottom: 1px solid var(--bord); }
.bloc h3 { margin: 0 0 10px; font-size: .8rem; text-transform: uppercase; letter-spacing: .08em; color: var(--doux); }
.sous { font-size: .8rem; color: var(--doux); margin: 10px 0 4px; }
.ed-pied { padding: 16px; }
.champ { display: grid; gap: 4px; }
.champ > span { font-size: .8rem; color: var(--doux); }
.champ input, textarea { width: 100%; padding: 10px 12px; border: 1px solid var(--bord); border-radius: 8px; background: var(--fond); min-height: 44px; }
textarea { resize: vertical; }
.grille-id { display: grid; grid-template-columns: repeat(auto-fit, minmax(150px, 1fr)); gap: 10px; }
.champ.sinus { grid-column: 1 / -1; }
.retour { white-space: nowrap; }
.ligne-h { display: flex; gap: 6px; }
.ligne-h input { flex: 1; min-width: 0; }
.grille-h { display: grid; grid-template-columns: repeat(auto-fit, minmax(190px, 1fr)); gap: 10px; margin-top: 12px; }
.rangee { display: flex; flex-wrap: wrap; gap: 8px 20px; align-items: flex-end; }
.rang { display: flex; flex-wrap: wrap; gap: 6px; align-items: center; }
.grille-puces { display: grid; gap: 6px; }

/* Segments et puces */
.seg { display: inline-flex; flex-wrap: wrap; gap: 4px; }
.seg button, .puce { min-height: 44px; padding: 6px 14px; border: 1.5px solid var(--bord); background: var(--surface); border-radius: 8px; cursor: pointer; font-weight: 600; }
.seg button[aria-pressed="true"], .puce[aria-pressed="true"] { background: var(--accent); border-color: var(--accent); color: var(--accent-texte); }
.puce[aria-pressed="true"]::before { content: "✓ "; }
.seg.mini button { min-width: 40px; padding: 6px 10px; }
.rang.marche .puce { flex: 1 1 140px; }
.rang.marche .puce[aria-pressed="true"] { background: var(--danger); border-color: var(--danger); color: #fff; }
.seg.cats { display: grid; grid-template-columns: repeat(4, 1fr); width: 100%; gap: 8px; }
.seg.cats button { min-height: 56px; font-size: 1.2rem; font-weight: 800; opacity: .35; border-width: 3px; }
.seg.cats button.cat-UE { background: var(--ue); color: #fff; border-color: var(--ue); }
.seg.cats button.cat-UA { background: var(--ua); color: #fff; border-color: var(--ua); }
.seg.cats button.cat-UR { background: var(--ur); color: #111; border-color: var(--ur); }
.seg.cats button.cat-UD { background: var(--ud); color: #fff; border-color: var(--ud); }
.seg.cats button[aria-pressed="true"] { opacity: 1; outline: 3px solid var(--texte); outline-offset: 2px; }
.seg.tranches button.t1 { color: var(--t1); } .seg.tranches button.t2 { color: var(--t2); }
.seg.tranches button.t3 { color: var(--t3); } .seg.tranches button.t4 { color: var(--t4); }
.seg.tranches button[aria-pressed="true"] { color: var(--accent-texte); }

.journal { margin: 0; padding-left: 0; list-style: none; display: grid; gap: 4px; font-size: .9rem; color: var(--doux); }
.journal time { font-variant-numeric: tabular-nums; font-weight: 700; color: var(--texte); margin-right: 6px; }

/* Tableau de bord */
.tuiles { display: grid; grid-template-columns: repeat(auto-fit, minmax(150px, 1fr)); gap: 10px; margin-bottom: 10px; }
.tuile { background: var(--surface); border: 1px solid var(--bord); border-radius: 12px; padding: 14px; display: grid; gap: 2px; }
.tuile .n { font-size: 2.2rem; font-weight: 800; line-height: 1; font-variant-numeric: tabular-nums; }
.tuile.cat-UE { background: var(--ue); color: #fff; border: 0; }
.tuile.cat-UA { background: var(--ua); color: #fff; border: 0; }
.tuile.cat-UR { background: var(--ur); color: #111; border: 0; }
.tuile.cat-UD { background: var(--ud); color: #fff; border: 1px solid var(--bord); }
.tuiles.petites .tuile .n { font-size: 1.6rem; }
.tableau .bloc { background: var(--surface); border: 1px solid var(--bord); border-radius: 12px; margin: 12px 0; }
.table-defile { overflow-x: auto; }
.recap { border-collapse: collapse; width: 100%; font-variant-numeric: tabular-nums; }
.recap th, .recap td { padding: 8px 10px; border-bottom: 1px solid var(--bord); text-align: center; }
.recap tbody th { text-align: left; }
.hopitaux { list-style: none; margin: 0; padding: 0; display: grid; gap: 6px; }
.hopitaux li { display: flex; justify-content: space-between; border-bottom: 1px solid var(--bord); padding: 6px 0; }

/* Dialogues */
dialog { border: 0; border-radius: 16px; padding: 20px; width: min(480px, calc(100vw - 32px)); background: var(--surface); color: var(--texte); }
dialog::backdrop { background: #0009; }
dialog form { display: grid; gap: 12px; }
dialog .actions { display: flex; justify-content: flex-end; gap: 8px; margin-top: 8px; }
#video-scan { width: 100%; border-radius: 10px; background: #000; max-height: 50vh; }
.aide { color: var(--doux); font-size: .9rem; margin: 4px 0; }
.erreur { color: var(--danger); font-weight: 600; margin: 0; }

#toast { position: fixed; left: 50%; bottom: 24px; transform: translate(-50%, 40px); opacity: 0; transition: .25s; z-index: 100;
  background: var(--texte); color: var(--fond); padding: 10px 16px; border-radius: 10px; max-width: calc(100vw - 32px); pointer-events: none; }
#toast.visible { opacity: 1; transform: translate(-50%, 0); }

/* ---------- Impression ---------- */
#impression { display: none; }
@media print {
  @page { size: A4 landscape; margin: 8mm; }
  :root { color-scheme: light; }
  body { background: #fff; color: #000; font-size: 9pt; }
  #app, dialog, #toast { display: none !important; }
  #impression { display: block; }
  .feuille { page-break-after: always; break-after: page; font-family: Calibri, "Segoe UI", Arial, sans-serif; }
  .feuille:last-child { page-break-after: auto; break-after: auto; }
  .feuille h1 { text-align: center; font-size: 14pt; margin: 0 0 3mm; }
  .resp { display: flex; justify-content: space-between; gap: 4mm; font-size: 9.5pt; margin-bottom: 2mm; }
  .imp { width: 100%; border-collapse: collapse; table-layout: fixed; }
  .imp th, .imp td { border: 1px solid #000; padding: 1mm 1.5mm; vertical-align: top; }
  .imp thead th { font-size: 10pt; }
  .imp tbody tr.victime { height: 33mm; page-break-inside: avoid; }
  .imp tbody tr.victime > td { border-top: 2px solid #000; border-bottom: 2px solid #000; }
  .c-auto { width: 15%; padding: 0 !important; }
  .auto-haut { display: grid; grid-template-columns: 1fr auto; gap: 0 2mm; padding: 1mm 1.5mm; border-bottom: 1px solid #000; }
  .auto-haut span:nth-child(3) { grid-column: 1 / -1; }
  .auto-bas { padding: 1mm 1.5mm; display: grid; }
  .auto-bas small { color: #555; }
  .c-cat { width: 3.5%; text-align: center; }
  .pc { font-weight: 700; margin: .6mm auto; width: 7mm; -webkit-print-color-adjust: exact; print-color-adjust: exact; }
  .pc.pale { opacity: .55; }
  .c-sexe { width: 3.5%; text-align: center; vertical-align: middle !important; }
  .c-sexe div { margin: 2mm 0; }
  .c-age { width: 6.5%; text-align: center; line-height: 1.35; }
  .c-age .t1 { color: #12897a; } .c-age .t2 { color: #d98a00; } .c-age .t3 { color: #1d7fd1; } .c-age .t4 { color: #d33b4a; }
  .c-liste { font-size: 8.5pt; line-height: 1.45; }
  .c-liste.droite { text-align: right; }
  .c-texte { white-space: pre-wrap; font-size: 9pt; }
  .c-texte .notes { margin-top: 1mm; color: #444; font-style: italic; }
  .c-dest { width: 9%; } .c-vec { width: 8%; line-height: 1.45; } .c-heure { width: 7%; }
  .c-dest div, .c-heure div { min-height: 4mm; }
  .entoure { outline: 1.5px solid #000; border-radius: 3mm; font-weight: 700; }
  .pc.entoure { outline: 2px solid #000; outline-offset: 1px; opacity: 1; }
  .feuille footer { display: flex; justify-content: space-between; margin-top: 2mm; font-size: 9pt; }
}
