/* =====================================================================
   Plateforme — feuille de style unique.
   Priorité mobile : les agents de terrain travaillent sur des téléphones
   d'entrée de gamme, souvent en plein soleil et en réseau instable.
   D'où : contraste élevé, cibles tactiles de 44 px, zéro police
   téléchargée (chaque requête compte en 3G).
   ===================================================================== */

:root {
  --ink-900: #101720;
  --ink-800: #1a2430;
  --ink-700: #2c3a4a;
  --ink-500: #5b6b7d;
  --ink-400: #8496a8;
  --ink-200: #d3dbe3;
  --ink-100: #e8edf2;
  --ink-050: #f4f7fa;
  --paper:   #ffffff;

  --saffron-600: #b45f06;
  --saffron-500: #e07a10;
  --saffron-100: #fdf0dc;

  --teal-600: #0f6e56;
  --teal-500: #16967a;
  --teal-100: #dcf2eb;

  --red-600: #a32d2d;
  --red-500: #d24343;
  --red-100: #fbe6e6;

  --blue-600: #1c5aa8;
  --blue-100: #e3eefc;

  --radius: 10px;
  --radius-lg: 16px;
  --shadow: 0 1px 2px rgba(16, 23, 32, .06), 0 4px 12px rgba(16, 23, 32, .04);
  --shadow-lift: 0 4px 16px rgba(16, 23, 32, .12);

  --font: system-ui, -apple-system, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  --mono: ui-monospace, SFMono-Regular, "SF Mono", Menlo, Consolas, monospace;

  --nav-h: 60px;
  --tab-h: 62px;
}

@media (prefers-color-scheme: dark) {
  :root {
    --ink-900: #f2f5f8;
    --ink-800: #e2e8ee;
    --ink-700: #c3ccd6;
    --ink-500: #94a2b1;
    --ink-400: #6f7f8f;
    --ink-200: #2f3b48;
    --ink-100: #232d38;
    --ink-050: #1a232c;
    --paper:   #141c24;
    --saffron-100: #3a2607;
    --teal-100: #0d2f26;
    --red-100: #351616;
    --blue-100: #12263f;
    --shadow: 0 1px 2px rgba(0, 0, 0, .4);
    --shadow-lift: 0 4px 16px rgba(0, 0, 0, .5);
  }
}

* { box-sizing: border-box; }

html { -webkit-text-size-adjust: 100%; }

body {
  margin: 0;
  font-family: var(--font);
  font-size: 16px;
  line-height: 1.55;
  color: var(--ink-900);
  background: var(--ink-050);
  -webkit-font-smoothing: antialiased;
}

h1, h2, h3, h4 { margin: 0 0 .5rem; font-weight: 600; line-height: 1.25; letter-spacing: -.01em; }
h1 { font-size: 1.5rem; }
h2 { font-size: 1.2rem; }
h3 { font-size: 1.05rem; }
p  { margin: 0 0 1rem; }
a  { color: var(--blue-600); text-decoration: none; }
a:hover { text-decoration: underline; }

.muted { color: var(--ink-500); }
.small { font-size: .82rem; }
.mono  { font-family: var(--mono); }
.right { text-align: right; }
.center { text-align: center; }
.nowrap { white-space: nowrap; }
.mt-0 { margin-top: 0; }
.mt-1 { margin-top: .5rem; }
.mt-2 { margin-top: 1rem; }
.mt-3 { margin-top: 1.5rem; }
.mb-0 { margin-bottom: 0; }
.mb-2 { margin-bottom: 1rem; }
.hide { display: none !important; }

/* ---------- Structure ---------- */

.shell { min-height: 100vh; display: flex; flex-direction: column; }

.topbar {
  position: sticky; top: 0; z-index: 40;
  height: var(--nav-h);
  display: flex; align-items: center; gap: .75rem;
  padding: 0 1rem;
  background: var(--ink-900);
  color: #fff;
}
@media (prefers-color-scheme: dark) {
  .topbar { background: #0c1218; color: var(--ink-900); }
}

.topbar .brand {
  font-weight: 700; letter-spacing: -.02em; font-size: 1.05rem;
  color: inherit; text-decoration: none; display: flex; align-items: center; gap: .5rem;
}
.topbar .brand .dot {
  width: 10px; height: 10px; border-radius: 50%;
  background: var(--saffron-500); flex: none;
}
.topbar .spacer { flex: 1; }

.org-switch {
  position: relative;
  max-width: 46vw;
}
.org-switch summary {
  list-style: none; cursor: pointer;
  display: flex; align-items: center; gap: .4rem;
  padding: .35rem .6rem;
  border: 1px solid rgba(255,255,255,.22);
  border-radius: 999px;
  font-size: .85rem;
  white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
}
.org-switch summary::-webkit-details-marker { display: none; }
.org-switch[open] summary { background: rgba(255,255,255,.12); }
.org-switch .menu {
  position: absolute; right: 0; top: calc(100% + .5rem);
  min-width: 240px; max-width: 80vw;
  background: var(--paper); color: var(--ink-900);
  border: 1px solid var(--ink-200); border-radius: var(--radius);
  box-shadow: var(--shadow-lift);
  padding: .35rem; z-index: 50;
}
.org-switch .menu a {
  display: block; padding: .6rem .7rem; border-radius: 8px;
  color: var(--ink-900); font-size: .9rem;
}
.org-switch .menu a:hover { background: var(--ink-050); text-decoration: none; }
.org-switch .menu a.active { background: var(--saffron-100); font-weight: 600; }
.org-switch .menu hr { border: 0; border-top: 1px solid var(--ink-200); margin: .35rem 0; }

.layout { flex: 1; display: flex; width: 100%; }

.sidebar { display: none; }

.main {
  flex: 1; min-width: 0;
  padding: 1rem 1rem calc(var(--tab-h) + 1.5rem);
  max-width: 100%;
}

.page-head {
  display: flex; align-items: flex-start; gap: 1rem;
  flex-wrap: wrap; margin-bottom: 1rem;
}
.page-head h1 { margin: 0; }
.page-head .actions { margin-left: auto; display: flex; gap: .5rem; flex-wrap: wrap; }

/* Navigation basse : le pouce de l'agent y arrive sans déplacer la main. */
.tabbar {
  position: fixed; bottom: 0; left: 0; right: 0; z-index: 40;
  height: var(--tab-h);
  display: flex;
  background: var(--paper);
  border-top: 1px solid var(--ink-200);
  padding-bottom: env(safe-area-inset-bottom);
}
.tabbar a {
  flex: 1; display: flex; flex-direction: column;
  align-items: center; justify-content: center; gap: 2px;
  font-size: .68rem; color: var(--ink-500); text-decoration: none;
  padding: .35rem .2rem; min-width: 0;
}
.tabbar a span { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; max-width: 100%; }
.tabbar a svg { width: 22px; height: 22px; stroke-width: 1.7; }
.tabbar a.active { color: var(--saffron-600); font-weight: 600; }

@media (min-width: 900px) {
  .tabbar { display: none; }
  .main { padding: 1.5rem 2rem 3rem; }
  .sidebar {
    display: block; width: 232px; flex: none;
    background: var(--paper);
    border-right: 1px solid var(--ink-200);
    padding: 1rem .7rem;
    position: sticky; top: var(--nav-h);
    height: calc(100vh - var(--nav-h)); overflow-y: auto;
  }
  .sidebar a {
    display: flex; align-items: center; gap: .65rem;
    padding: .6rem .7rem; margin-bottom: 2px;
    border-radius: 8px; color: var(--ink-700);
    font-size: .92rem; text-decoration: none;
  }
  .sidebar a svg { width: 19px; height: 19px; flex: none; stroke-width: 1.7; }
  .sidebar a:hover { background: var(--ink-050); }
  .sidebar a.active { background: var(--saffron-100); color: var(--saffron-600); font-weight: 600; }
  .sidebar .group-label {
    font-size: .7rem; text-transform: uppercase; letter-spacing: .07em;
    color: var(--ink-400); padding: 1rem .7rem .35rem; font-weight: 600;
  }
}

/* ---------- Cartes ---------- */

.card {
  background: var(--paper);
  border: 1px solid var(--ink-200);
  border-radius: var(--radius-lg);
  padding: 1.1rem;
  margin-bottom: 1rem;
}
.card > h2, .card > h3 { margin-top: 0; }
.card-head {
  display: flex; align-items: center; gap: .75rem;
  margin: -.25rem 0 1rem;
}
.card-head h2, .card-head h3 { margin: 0; }
.card-head .actions { margin-left: auto; }

.grid { display: grid; gap: .85rem; }
.grid-2 { grid-template-columns: 1fr; }
.grid-3 { grid-template-columns: repeat(2, minmax(0, 1fr)); }
.grid-4 { grid-template-columns: repeat(2, minmax(0, 1fr)); }
@media (min-width: 700px) {
  .grid-2 { grid-template-columns: repeat(2, minmax(0, 1fr)); }
  .grid-3 { grid-template-columns: repeat(3, minmax(0, 1fr)); }
  .grid-4 { grid-template-columns: repeat(4, minmax(0, 1fr)); }
}

.stat {
  background: var(--paper); border: 1px solid var(--ink-200);
  border-radius: var(--radius); padding: .85rem .95rem;
}
.stat .label {
  font-size: .72rem; text-transform: uppercase; letter-spacing: .06em;
  color: var(--ink-500); font-weight: 600;
}
.stat .value { font-size: 1.6rem; font-weight: 700; letter-spacing: -.02em; margin-top: .15rem; }
.stat .value.sm { font-size: 1.15rem; }

/* ---------- Formulaires ---------- */

.field { margin-bottom: 1rem; }
.field label {
  display: block; font-size: .85rem; font-weight: 600;
  margin-bottom: .35rem; color: var(--ink-700);
}
.field .help { font-size: .78rem; color: var(--ink-500); margin-top: .3rem; }
.field .req { color: var(--red-500); }

input[type="text"], input[type="email"], input[type="password"], input[type="tel"],
input[type="date"], input[type="datetime-local"], input[type="number"], input[type="search"],
input[type="url"], select, textarea {
  width: 100%;
  min-height: 46px;
  padding: .6rem .75rem;
  font-family: inherit; font-size: 16px; /* 16px empêche le zoom iOS */
  color: var(--ink-900);
  background: var(--paper);
  border: 1px solid var(--ink-200);
  border-radius: var(--radius);
  appearance: none;
}
textarea { min-height: 110px; resize: vertical; line-height: 1.5; }
select {
  background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='14' height='9' viewBox='0 0 14 9'%3E%3Cpath d='M1 1l6 6 6-6' fill='none' stroke='%235b6b7d' stroke-width='2'/%3E%3C/svg%3E");
  background-repeat: no-repeat; background-position: right .8rem center;
  padding-right: 2.2rem;
}
input:focus, select:focus, textarea:focus {
  outline: none; border-color: var(--saffron-500);
  box-shadow: 0 0 0 3px var(--saffron-100);
}
input.error, select.error, textarea.error { border-color: var(--red-500); }
.field-error { color: var(--red-600); font-size: .8rem; margin-top: .3rem; }

.check { display: flex; align-items: flex-start; gap: .6rem; margin-bottom: .6rem; }
.check input[type="checkbox"], .check input[type="radio"] {
  width: 20px; height: 20px; margin: 2px 0 0; flex: none; accent-color: var(--saffron-500);
}
.check label { margin: 0; font-weight: 500; font-size: .92rem; }

.switch { display: flex; align-items: center; gap: .7rem; padding: .6rem 0; }
.switch input { width: 20px; height: 20px; accent-color: var(--teal-500); flex: none; }

/* ---------- Boutons ---------- */

.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: .45rem;
  min-height: 44px; padding: .6rem 1.05rem;
  font-family: inherit; font-size: .92rem; font-weight: 600;
  color: var(--ink-900); background: var(--paper);
  border: 1px solid var(--ink-200); border-radius: var(--radius);
  cursor: pointer; text-decoration: none; white-space: nowrap;
  transition: background .12s ease, transform .08s ease;
}
.btn:hover { background: var(--ink-050); text-decoration: none; }
.btn:active { transform: scale(.985); }
.btn svg { width: 18px; height: 18px; flex: none; stroke-width: 1.9; }
.btn-primary { background: var(--saffron-500); border-color: var(--saffron-500); color: #fff; }
.btn-primary:hover { background: var(--saffron-600); border-color: var(--saffron-600); }
.btn-dark { background: var(--ink-900); border-color: var(--ink-900); color: var(--paper); }
.btn-danger { background: var(--paper); border-color: var(--red-500); color: var(--red-600); }
.btn-danger:hover { background: var(--red-100); }
.btn-ghost { background: transparent; border-color: transparent; }
.btn-sm { min-height: 36px; padding: .35rem .7rem; font-size: .84rem; }
.btn-block { width: 100%; }
.btn-lg { min-height: 56px; font-size: 1.05rem; }
.btn[disabled] { opacity: .5; pointer-events: none; }

.btn-row { display: flex; gap: .6rem; flex-wrap: wrap; align-items: center; }

/* ---------- Tableaux : cartes empilées en dessous de 760 px ---------- */

.table-wrap { overflow-x: auto; -webkit-overflow-scrolling: touch; }
table.data { width: 100%; border-collapse: collapse; font-size: .92rem; }
table.data th {
  text-align: left; font-size: .72rem; text-transform: uppercase;
  letter-spacing: .05em; color: var(--ink-500); font-weight: 700;
  padding: .55rem .7rem; border-bottom: 1px solid var(--ink-200); white-space: nowrap;
}
table.data td { padding: .7rem; border-bottom: 1px solid var(--ink-100); vertical-align: middle; }
table.data tr:last-child td { border-bottom: 0; }
table.data tbody tr:hover { background: var(--ink-050); }

@media (max-width: 759px) {
  table.data, table.data tbody, table.data tr, table.data td { display: block; width: 100%; }
  table.data thead { display: none; }
  table.data tr {
    background: var(--paper); border: 1px solid var(--ink-200);
    border-radius: var(--radius); margin-bottom: .6rem; padding: .3rem .2rem;
  }
  table.data td {
    display: flex; justify-content: space-between; align-items: center; gap: 1rem;
    border-bottom: 1px solid var(--ink-100); padding: .55rem .8rem;
  }
  table.data td:last-child { border-bottom: 0; }
  table.data td::before {
    content: attr(data-label);
    font-size: .72rem; text-transform: uppercase; letter-spacing: .05em;
    color: var(--ink-500); font-weight: 700; flex: none;
  }
  table.data td.cell-main { display: block; }
  table.data td.cell-main::before { display: none; }
}

/* ---------- Éléments d'interface ---------- */

.badge {
  display: inline-flex; align-items: center; gap: .3rem;
  padding: .2rem .55rem; border-radius: 999px;
  font-size: .74rem; font-weight: 600; white-space: nowrap;
  background: var(--ink-100); color: var(--ink-700);
}
.badge-success { background: var(--teal-100); color: var(--teal-600); }
.badge-warning { background: var(--saffron-100); color: var(--saffron-600); }
.badge-danger  { background: var(--red-100); color: var(--red-600); }
.badge-info    { background: var(--blue-100); color: var(--blue-600); }

.avatar {
  width: 38px; height: 38px; border-radius: 50%; flex: none;
  display: flex; align-items: center; justify-content: center;
  background: var(--saffron-100); color: var(--saffron-600);
  font-weight: 700; font-size: .82rem;
}
.avatar.lg { width: 56px; height: 56px; font-size: 1.1rem; }

.person { display: flex; align-items: center; gap: .7rem; min-width: 0; }
.person .name { font-weight: 600; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.person .sub { font-size: .8rem; color: var(--ink-500); }

.alert {
  display: flex; gap: .7rem; align-items: flex-start;
  padding: .8rem .9rem; border-radius: var(--radius);
  margin-bottom: 1rem; font-size: .9rem;
  border: 1px solid transparent;
}
.alert svg { width: 18px; height: 18px; flex: none; margin-top: 2px; }
.alert-success { background: var(--teal-100); color: var(--teal-600); border-color: rgba(22,150,122,.25); }
.alert-error   { background: var(--red-100); color: var(--red-600); border-color: rgba(210,67,67,.25); }
.alert-warning { background: var(--saffron-100); color: var(--saffron-600); border-color: rgba(224,122,16,.25); }
.alert-info    { background: var(--blue-100); color: var(--blue-600); border-color: rgba(28,90,168,.2); }

.empty {
  text-align: center; padding: 2.5rem 1rem;
  color: var(--ink-500);
}
.empty svg { width: 40px; height: 40px; stroke-width: 1.4; opacity: .5; margin-bottom: .6rem; }
.empty h3 { color: var(--ink-900); }

.toolbar {
  display: flex; gap: .6rem; flex-wrap: wrap; align-items: center;
  margin-bottom: 1rem;
}
.toolbar .grow { flex: 1 1 220px; min-width: 0; }

.pagination { display: flex; gap: .35rem; align-items: center; justify-content: center; margin-top: 1.2rem; flex-wrap: wrap; }
.pagination a, .pagination span {
  min-width: 40px; min-height: 40px; padding: 0 .6rem;
  display: inline-flex; align-items: center; justify-content: center;
  border: 1px solid var(--ink-200); border-radius: 8px;
  font-size: .88rem; color: var(--ink-700); text-decoration: none; background: var(--paper);
}
.pagination .current { background: var(--ink-900); color: var(--paper); border-color: var(--ink-900); font-weight: 600; }
.pagination .disabled { opacity: .4; pointer-events: none; }

.progress { height: 8px; background: var(--ink-100); border-radius: 999px; overflow: hidden; }
.progress > div { height: 100%; background: var(--teal-500); transition: width .3s ease; }

.divider { border: 0; border-top: 1px solid var(--ink-200); margin: 1.2rem 0; }

.kv { display: grid; grid-template-columns: minmax(90px, 34%) 1fr; gap: .5rem 1rem; font-size: .92rem; }
.kv dt { color: var(--ink-500); font-size: .82rem; }
.kv dd { margin: 0; word-break: break-word; }

.chip-row { display: flex; gap: .4rem; flex-wrap: wrap; }

/* ---------- Pages publiques (QR, connexion) ---------- */

.public-shell {
  min-height: 100vh; display: flex; flex-direction: column;
  align-items: center; justify-content: center;
  padding: 1.25rem; background: var(--ink-050);
}
.public-card {
  width: 100%; max-width: 460px;
  background: var(--paper); border: 1px solid var(--ink-200);
  border-radius: var(--radius-lg); padding: 1.5rem;
  box-shadow: var(--shadow);
}
.public-head { text-align: center; margin-bottom: 1.25rem; }
.public-head .org {
  font-size: .78rem; text-transform: uppercase; letter-spacing: .08em;
  color: var(--saffron-600); font-weight: 700;
}
.public-head h1 { margin: .3rem 0 .2rem; font-size: 1.35rem; }
.public-head .when { color: var(--ink-500); font-size: .9rem; }

.ok-mark {
  width: 72px; height: 72px; border-radius: 50%;
  background: var(--teal-100); color: var(--teal-600);
  display: flex; align-items: center; justify-content: center;
  margin: 0 auto 1rem;
}
.ok-mark svg { width: 36px; height: 36px; stroke-width: 2.5; }

/* ---------- Scanner ---------- */

.scanner {
  border-radius: var(--radius-lg); overflow: hidden;
  background: #000; aspect-ratio: 4/3; max-height: 58vh;
}
.scan-result { margin-top: 1rem; }
.scan-result .banner {
  display: flex; align-items: center; gap: .8rem;
  padding: 1rem; border-radius: var(--radius); font-weight: 600;
}
.scan-result .banner.ok  { background: var(--teal-100); color: var(--teal-600); }
.scan-result .banner.bad { background: var(--red-100); color: var(--red-600); }
.scan-result .banner svg { width: 28px; height: 28px; flex: none; stroke-width: 2.4; }

.qr-box {
  background: #fff; padding: 1rem; border-radius: var(--radius);
  display: inline-flex; align-items: center; justify-content: center;
  border: 1px solid var(--ink-200);
}

/* ---------- Impression ---------- */

@media print {
  .topbar, .tabbar, .sidebar, .actions, .no-print { display: none !important; }
  body { background: #fff; }
  .card { border: 1px solid #ccc; box-shadow: none; }
}

@media (prefers-reduced-motion: reduce) {
  * { animation-duration: .01ms !important; transition-duration: .01ms !important; }
}

:focus-visible { outline: 3px solid var(--saffron-500); outline-offset: 2px; border-radius: 4px; }

.sr-only {
  position: absolute; width: 1px; height: 1px; padding: 0; margin: -1px;
  overflow: hidden; clip: rect(0, 0, 0, 0); white-space: nowrap; border: 0;
}
