:root {
  --bg: #f4f6fa; --surface: #ffffff; --surface-2: #f8fafc; --border: #e2e8f0; --text: #1e293b; --muted: #64748b;
  --primary: #0f4c81; --primary-2: #1d6fb8; --primary-soft: #e6effa; --ok: #15803d; --ok-soft: #dcfce7;
  --warn: #b45309; --warn-soft: #fef3c7; --danger: #b91c1c; --danger-soft: #fee2e2; --info: #0369a1; --info-soft: #e0f2fe;
  --violet: #6d28d9; --violet-soft: #ede9fe; --side: #0b2540; --side-text: #cbd5e1;
  --radius: 10px; --shadow: 0 1px 2px rgba(15,23,42,.06), 0 4px 14px rgba(15,23,42,.05);
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #0d1420; --surface: #141d2b; --surface-2: #1a2536; --border: #273449; --text: #e2e8f0; --muted: #94a3b8;
    --primary: #4c9be8; --primary-2: #6cb0f0; --primary-soft: #16304d; --ok: #4ade80; --ok-soft: #14321f;
    --warn: #fbbf24; --warn-soft: #3a2c0c; --danger: #f87171; --danger-soft: #3b1515; --info: #38bdf8; --info-soft: #0c2c3d;
    --violet: #a78bfa; --violet-soft: #2a1f4a; --side: #08111d; --shadow: none; color-scheme: dark;
  }
}
* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; }
body { font: 14px/1.45 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif; background: var(--bg); color: var(--text); }
a { color: var(--primary); text-decoration: none; }
a:hover { text-decoration: underline; }
.hidden { display: none !important; }
.muted { color: var(--muted); }
.mono, .serial { font-family: ui-monospace, "SFMono-Regular", Menlo, Consolas, monospace; font-variant-numeric: tabular-nums; }
.right { text-align: right; } .nowrap { white-space: nowrap; }
h2 { font-size: 16px; margin: 0 0 12px; } h3 { font-size: 14px; margin: 16px 0 8px; }

/* ---------- Layout ---------- */
#app { display: flex; min-height: 100vh; }
#sidebar { width: 236px; background: var(--side); color: var(--side-text); display: flex; flex-direction: column; position: fixed; inset: 0 auto 0 0; z-index: 30; transition: transform .2s; }
.brand { display: flex; align-items: center; gap: 8px; font-weight: 700; font-size: 17px; padding: 18px 18px 14px; color: #fff; }
.brand.big { font-size: 24px; color: var(--primary); padding: 0 0 4px; }
.logo { display: inline-grid; place-items: center; width: 30px; height: 30px; border-radius: 8px; background: #1d6fb8; font-size: 15px; }
#nav { flex: 1; overflow-y: auto; padding: 4px 10px; }
#nav a { display: flex; align-items: center; gap: 10px; padding: 8px 10px; border-radius: 8px; color: var(--side-text); font-size: 13.5px; }
#nav a:hover { background: rgba(255,255,255,.06); text-decoration: none; }
#nav a.active { background: #1d6fb8; color: #fff; }
#nav .ico { width: 20px; text-align: center; }
.side-foot { border-top: 1px solid rgba(255,255,255,.08); padding: 12px 16px; font-size: 12.5px; }
.side-foot b { color: #fff; display: block; }
.side-foot button { margin-top: 8px; background: transparent; border: 1px solid rgba(255,255,255,.2); color: var(--side-text); border-radius: 6px; padding: 4px 10px; cursor: pointer; }
main { flex: 1; margin-left: 236px; min-width: 0; }
.topbar { position: sticky; top: 0; z-index: 20; background: var(--surface); border-bottom: 1px solid var(--border); display: flex; align-items: center; gap: 12px; padding: 10px 20px; }
.topbar h1 { font-size: 18px; margin: 0; flex: 1; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.quick-search { display: flex; gap: 4px; }
.quick-search input { width: 220px; }
#menu-btn { display: none; }
#view { padding: 20px; max-width: 1500px; }
#backdrop { display: none; }
@media (max-width: 900px) {
  #sidebar { transform: translateX(-100%); }
  body.menu-open #sidebar { transform: none; }
  body.menu-open #backdrop { display: block; position: fixed; inset: 0; background: rgba(0,0,0,.4); z-index: 25; }
  main { margin-left: 0; }
  #menu-btn { display: inline-grid; }
  .topbar { padding: 8px 12px; }
  .quick-search input { width: 130px; }
  #view { padding: 12px; }
}

/* ---------- Composants ---------- */
.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 > h2:first-child { display: flex; align-items: center; gap: 8px; }
.grid { display: grid; gap: 16px; }
.g2 { grid-template-columns: repeat(2, minmax(0, 1fr)); } .g3 { grid-template-columns: repeat(3, minmax(0, 1fr)); }
.kpis { display: grid; grid-template-columns: repeat(auto-fill, minmax(190px, 1fr)); gap: 12px; margin-bottom: 16px; }
.kpi { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); padding: 14px 16px; box-shadow: var(--shadow); }
.kpi .label { color: var(--muted); font-size: 12px; text-transform: uppercase; letter-spacing: .03em; }
.kpi .value { font-size: 22px; font-weight: 700; margin-top: 4px; font-variant-numeric: tabular-nums; }
.kpi .sub { font-size: 12px; color: var(--muted); }
.kpi.accent .value { color: var(--primary); } .kpi.ok .value { color: var(--ok); } .kpi.warn .value { color: var(--warn); } .kpi.danger .value { color: var(--danger); }
@media (max-width: 1100px) { .g3 { grid-template-columns: 1fr 1fr; } }
@media (max-width: 760px) { .g2, .g3 { grid-template-columns: 1fr; } .kpis { grid-template-columns: 1fr 1fr; gap: 8px; } .kpi { padding: 10px 12px; } .kpi .value { font-size: 17px; } }

.toolbar { display: flex; flex-wrap: wrap; gap: 8px; align-items: center; margin-bottom: 12px; }
.toolbar .spacer { flex: 1; }
.btn { display: inline-flex; align-items: center; gap: 6px; border: 1px solid var(--border); background: var(--surface); color: var(--text); padding: 7px 12px; border-radius: 8px; cursor: pointer; font: inherit; font-weight: 500; white-space: nowrap; }
.btn:hover { border-color: var(--primary-2); text-decoration: none; }
.btn.primary { background: var(--primary); border-color: var(--primary); color: #fff; }
.btn.primary:hover { background: var(--primary-2); }
.btn.danger { color: var(--danger); border-color: var(--danger-soft); }
.btn.sm { padding: 4px 9px; font-size: 12.5px; }
.btn.block { width: 100%; justify-content: center; }
.btn:disabled { opacity: .5; cursor: not-allowed; }
.icon-btn { border: 1px solid var(--border); background: var(--surface); color: var(--text); border-radius: 8px; width: 34px; height: 34px; cursor: pointer; display: inline-grid; place-items: center; font-size: 16px; }
input, select, textarea { font: inherit; color: var(--text); background: var(--surface); border: 1px solid var(--border); border-radius: 8px; padding: 7px 10px; width: 100%; min-width: 0; }
input:focus, select:focus, textarea:focus { outline: 2px solid var(--primary-soft); border-color: var(--primary-2); }
textarea { min-height: 70px; resize: vertical; }
textarea.serials { font-family: ui-monospace, Menlo, Consolas, monospace; min-height: 90px; }
label { display: block; font-size: 12.5px; color: var(--muted); font-weight: 500; }
label > input, label > select, label > textarea { margin-top: 4px; }
.form-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(210px, 1fr)); gap: 12px; }
.form-grid .full { grid-column: 1 / -1; }
.check { display: flex; align-items: center; gap: 8px; color: var(--text); }
.check input { width: auto; }
.search { max-width: 280px; }

.table-wrap { overflow-x: auto; border: 1px solid var(--border); border-radius: 8px; }
table { width: 100%; border-collapse: collapse; font-size: 13px; }
th, td { padding: 8px 10px; text-align: left; border-bottom: 1px solid var(--border); vertical-align: top; }
th { background: var(--surface-2); font-weight: 600; color: var(--muted); font-size: 12px; text-transform: uppercase; letter-spacing: .02em; white-space: nowrap; cursor: pointer; user-select: none; position: sticky; top: 0; }
tbody tr:hover { background: var(--surface-2); }
tbody tr.clickable { cursor: pointer; }
tbody tr:last-child td { border-bottom: 0; }
td.num, th.num { text-align: right; font-variant-numeric: tabular-nums; white-space: nowrap; }
tfoot td { font-weight: 700; background: var(--surface-2); }
.empty { padding: 24px; text-align: center; color: var(--muted); }
.pager { display: flex; gap: 8px; align-items: center; justify-content: flex-end; margin-top: 8px; font-size: 12.5px; color: var(--muted); }

.badge { display: inline-block; padding: 2px 8px; border-radius: 999px; font-size: 11.5px; font-weight: 600; white-space: nowrap; background: var(--surface-2); color: var(--muted); border: 1px solid var(--border); }
.b-ok { background: var(--ok-soft); color: var(--ok); border-color: transparent; }
.b-warn { background: var(--warn-soft); color: var(--warn); border-color: transparent; }
.b-danger { background: var(--danger-soft); color: var(--danger); border-color: transparent; }
.b-info { background: var(--info-soft); color: var(--info); border-color: transparent; }
.b-violet { background: var(--violet-soft); color: var(--violet); border-color: transparent; }
.b-primary { background: var(--primary-soft); color: var(--primary); border-color: transparent; }

.tabs { display: flex; gap: 4px; border-bottom: 1px solid var(--border); margin-bottom: 14px; overflow-x: auto; }
.tabs button { background: none; border: 0; padding: 9px 14px; cursor: pointer; font: inherit; color: var(--muted); border-bottom: 2px solid transparent; white-space: nowrap; }
.tabs button.active { color: var(--primary); border-bottom-color: var(--primary); font-weight: 600; }

dl.info { display: grid; grid-template-columns: max-content 1fr; gap: 6px 16px; margin: 0; }
dl.info dt { color: var(--muted); font-size: 12.5px; }
dl.info dd { margin: 0; font-weight: 500; overflow-wrap: anywhere; }
.alert { padding: 10px 12px; border-radius: 8px; margin-bottom: 12px; font-size: 13px; }
.alert.warn { background: var(--warn-soft); color: var(--warn); } .alert.danger { background: var(--danger-soft); color: var(--danger); }
.alert.ok { background: var(--ok-soft); color: var(--ok); } .alert.info { background: var(--info-soft); color: var(--info); }
.timeline { list-style: none; padding: 0; margin: 0; border-left: 2px solid var(--border); }
.timeline li { padding: 0 0 14px 16px; position: relative; }
.timeline li::before { content: ""; position: absolute; left: -7px; top: 4px; width: 12px; height: 12px; border-radius: 50%; background: var(--primary); border: 2px solid var(--surface); }
.timeline .when { font-size: 12px; color: var(--muted); }
.serial-big { font-size: 28px; font-weight: 700; letter-spacing: .04em; }
.photo { width: 100%; max-width: 220px; aspect-ratio: 1; object-fit: cover; border-radius: 10px; border: 1px solid var(--border); background: var(--surface-2); display: grid; place-items: center; color: var(--muted); font-size: 40px; }
.chart-box { position: relative; height: 260px; }
.ranges { display: flex; flex-wrap: wrap; gap: 6px; }
.ranges .badge { font-family: ui-monospace, Menlo, Consolas, monospace; }
.lines-table input, .lines-table select, .lines-table textarea { padding: 5px 7px; }

/* ---------- Modale, toasts, connexion ---------- */
.modal-bg { position: fixed; inset: 0; background: rgba(15,23,42,.45); z-index: 50; display: grid; place-items: center; padding: 16px; overflow-y: auto; }
.modal { background: var(--surface); border-radius: 12px; width: 100%; max-width: 560px; box-shadow: 0 20px 50px rgba(0,0,0,.25); display: flex; flex-direction: column; max-height: calc(100vh - 32px); }
.modal.wide { max-width: 980px; }
.modal header { padding: 14px 18px; border-bottom: 1px solid var(--border); display: flex; align-items: center; gap: 8px; }
.modal header h3 { margin: 0; flex: 1; font-size: 16px; }
.modal .body { padding: 16px 18px; overflow-y: auto; }
.modal footer { padding: 12px 18px; border-top: 1px solid var(--border); display: flex; gap: 8px; justify-content: flex-end; flex-wrap: wrap; }
#toasts { position: fixed; right: 16px; bottom: 16px; z-index: 60; display: flex; flex-direction: column; gap: 8px; max-width: calc(100vw - 32px); }
.toast { background: var(--text); color: var(--surface); padding: 10px 14px; border-radius: 8px; box-shadow: var(--shadow); max-width: 420px; font-size: 13px; }
.toast.error { background: var(--danger); color: #fff; } .toast.ok { background: var(--ok); color: #fff; }
.login { min-height: 100vh; display: grid; place-items: center; padding: 16px; background: linear-gradient(135deg, #0b2540, #1d6fb8); }
.login-card { background: var(--surface); padding: 28px; border-radius: 14px; width: 100%; max-width: 380px; display: grid; gap: 12px; box-shadow: 0 20px 50px rgba(0,0,0,.3); }
.error { color: var(--danger); font-size: 13px; min-height: 1em; }
#scan-video { width: 100%; border-radius: 8px; overflow: hidden; }
@media print { #sidebar, .topbar, .toolbar, .btn { display: none !important; } main { margin: 0; } }

/* iPhone / iPad : un champ dont le texte fait moins de 16 px déclenche un zoom automatique
   qui reste actif après la saisie (page « trop large »). 16 px sur les écrans tactiles l'évite. */
@media (max-width: 900px), (pointer: coarse) {
  input, select, textarea { font-size: 16px; }
}
