:root {
  --bg: #17120d;
  --surface: #231c15;
  --surface-2: #2e251b;
  --border: #3d3226;
  --text: #f4efe0;
  --muted: #a89a85;
  --accent: #93bf54;
  --accent-2: #bbe383;
  --accent-ink: #17220a;
  --danger: #e5694f;
  --radius: 14px;
  --tab-h: 62px;
  --top-h: 48px;
  --safe-b: env(safe-area-inset-bottom, 0px);
  --safe-t: env(safe-area-inset-top, 0px);
}

* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
html, body { margin: 0; height: 100%; background: var(--bg); color: var(--text);
  font: 16px/1.4 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif; overscroll-behavior: none; }
button, input, textarea, select { font: inherit; color: inherit; }
[hidden] { display: none !important; }
svg { width: 24px; height: 24px; fill: none; stroke: currentColor; stroke-width: 1.8; stroke-linecap: round; stroke-linejoin: round; flex: none; }
.muted { color: var(--muted); }
.small { font-size: .85rem; }
.mono { font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace; word-break: break-all; }

/* ---------- Structure ---------- */
#app { position: fixed; inset: 0; display: flex; flex-direction: column; }
#topbar { position: absolute; z-index: 5; top: 0; left: 0; right: 0; height: calc(var(--top-h) + var(--safe-t));
  padding: var(--safe-t) 14px 0; display: flex; align-items: center; gap: 10px;
  background: linear-gradient(#000a, #0000); pointer-events: none; }
#topbar > * { pointer-events: auto; }
.brand { font-weight: 700; letter-spacing: .02em; flex: 1; }
.badge { display: inline-flex; align-items: center; gap: 6px; padding: 4px 10px 4px 8px; border-radius: 99px;
  background: #0006; border: 1px solid #fff2; font-size: .78rem; color: var(--muted); }
.badge svg { width: 18px; height: 18px; }
.badge[data-state="ok"] { color: var(--accent-2); }
.badge[data-state="busy"] { color: var(--accent-2); }
.badge[data-state="busy"] svg { animation: pulse 1s ease-in-out infinite; }
.badge[data-state="warn"] { color: #f0c05a; }
.badge[data-state="err"] { color: var(--danger); }
@keyframes pulse { 50% { opacity: .35; } }

#views { flex: 1; position: relative; min-height: 0; }
.view { position: absolute; inset: 0; }
#view-history, #view-settings { overflow-y: auto; padding: calc(var(--top-h) + var(--safe-t) + 8px) 14px 20px; background: var(--bg); }

#tabbar { height: calc(var(--tab-h) + var(--safe-b)); padding-bottom: var(--safe-b); display: flex;
  background: var(--surface); border-top: 1px solid var(--border); z-index: 6; }
.tab { flex: 1; display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 2px;
  background: none; border: 0; color: var(--muted); font-size: .72rem; }
.tab.active { color: var(--accent); }

/* ---------- Scanner ---------- */
#view-scan { background: #000; overflow: hidden; }
/* La bibliothèque calcule la zone de décodage à partir de la taille *affichée* de la vidéo : elle doit donc garder
   son ratio natif. #reader reçoit une largeur fixe (définie par scanner.js) puis est mis à l'échelle (transform)
   pour remplir l'écran, ce qui ne modifie pas la mise en page vue par la bibliothèque. */
#reader { position: absolute !important; left: 50%; top: 50%; border: 0 !important; padding: 0 !important; transform-origin: center; transform: translate(-50%, -50%); }
#reader video { display: block !important; position: static !important; width: 100% !important; height: auto !important; }
#reader > div { display: none !important; } /* éléments d'interface de la bibliothèque (nous avons les nôtres) */

.reticle { position: absolute; left: 50%; top: 46%; width: min(78vw, 420px); aspect-ratio: 1.25; transform: translate(-50%, -50%); pointer-events: none; }
.reticle i { position: absolute; width: 34px; height: 34px; border: 3px solid var(--accent); opacity: .9; }
.reticle i:nth-child(1) { left: 0; top: 0; border-right: 0; border-bottom: 0; border-radius: 12px 0 0 0; }
.reticle i:nth-child(2) { right: 0; top: 0; border-left: 0; border-bottom: 0; border-radius: 0 12px 0 0; }
.reticle i:nth-child(3) { left: 0; bottom: 0; border-right: 0; border-top: 0; border-radius: 0 0 0 12px; }
.reticle i:nth-child(4) { right: 0; bottom: 0; border-left: 0; border-top: 0; border-radius: 0 0 12px 0; }

.scan-actions { position: absolute; right: 14px; bottom: 16px; display: flex; flex-direction: column; gap: 12px; z-index: 4; }
.fab { width: 52px; height: 52px; border-radius: 50%; border: 1px solid #fff3; background: #000a; color: var(--text);
  display: grid; place-items: center; backdrop-filter: blur(6px); }
.fab[aria-pressed="true"] { background: var(--accent); color: var(--accent-ink); border-color: var(--accent); }
.fab:disabled { opacity: .35; }

.scan-msg { position: absolute; left: 20px; right: 20px; top: 50%; transform: translateY(-50%); text-align: center; z-index: 3;
  background: #000c; border: 1px solid var(--border); padding: 18px; border-radius: var(--radius); }
.scan-msg button { margin-top: 12px; }

.scan-card { position: absolute; left: 14px; right: 80px; bottom: 16px; z-index: 4; text-align: left; display: flex; flex-direction: column; gap: 2px;
  padding: 10px 14px; border-radius: var(--radius); border: 1px solid var(--accent); background: #101a06f0; color: var(--text); }
.scan-card b { color: var(--accent-2); font-size: .78rem; letter-spacing: .04em; text-transform: uppercase; }
.scan-card span { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.scan-card.dup { border-color: #f0c05a; background: #241c06f0; }
.scan-card.dup b { color: #f0c05a; }
.scan-card.flash { animation: flash .35s ease-out; }
@keyframes flash { from { transform: scale(1.04); } to { transform: none; } }

/* ---------- Historique ---------- */
.search input { width: 100%; padding: 12px 14px; border-radius: var(--radius); border: 1px solid var(--border); background: var(--surface); }
.list { list-style: none; margin: 0; padding: 0; display: flex; flex-direction: column; gap: 8px; }
.item { display: flex; gap: 12px; align-items: center; padding: 12px; border-radius: var(--radius); background: var(--surface); border: 1px solid var(--border); width: 100%; text-align: left; }
.item-main { flex: 1; min-width: 0; display: flex; flex-direction: column; gap: 2px; }
.item-code { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; font-weight: 600; }
.item-meta { font-size: .8rem; color: var(--muted); display: flex; gap: 8px; flex-wrap: wrap; }
.item-note { font-size: .82rem; color: var(--accent-2); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.fmt { font-size: .68rem; font-weight: 700; letter-spacing: .04em; padding: 2px 7px; border-radius: 6px; background: var(--surface-2); color: var(--accent-2); white-space: nowrap; }
.thumb { width: 44px; height: 44px; border-radius: 8px; background: var(--surface-2); object-fit: cover; flex: none; display: grid; place-items: center; color: var(--muted); }
.empty { text-align: center; color: var(--muted); padding: 40px 0; }

/* ---------- Cartes / boutons / formulaires ---------- */
.card { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); padding: 14px; margin-bottom: 12px; }
.card h2 { margin: 0 0 8px; font-size: 1rem; }
.card p { margin: 6px 0; }
.btn { padding: 11px 16px; border-radius: 12px; border: 1px solid var(--border); background: var(--surface-2); cursor: pointer; }
.btn:active { filter: brightness(1.2); }
.btn:disabled { opacity: .45; }
.btn-primary { background: var(--accent); color: var(--accent-ink); border-color: var(--accent); font-weight: 600; }
.btn-danger { color: var(--danger); border-color: #5a2a20; background: transparent; }
.btn-block { display: block; width: 100%; margin-top: 10px; }
.btn-row { display: flex; gap: 8px; flex-wrap: wrap; }
.btn-row .btn { flex: 1; }
.switch { display: flex; align-items: center; gap: 12px; padding: 8px 0; }
.switch input { width: 20px; height: 20px; accent-color: var(--accent); }
input[type="password"], input[type="text"], textarea { width: 100%; padding: 12px; border-radius: 12px; border: 1px solid var(--border); background: var(--bg); }
textarea { min-height: 96px; resize: vertical; }
label.field { display: block; margin: 12px 0 4px; font-size: .85rem; color: var(--muted); }

/* ---------- Détail ---------- */
.sheet { position: fixed; inset: 0; z-index: 20; background: var(--bg); overflow-y: auto; padding: 0 14px calc(24px + var(--safe-b)); }
.sheet-bar { position: sticky; top: 0; z-index: 2; display: flex; align-items: center; gap: 8px; padding: calc(var(--safe-t) + 8px) 0 8px; background: var(--bg); }
.sheet-bar h2 { margin: 0; font-size: 1.05rem; flex: 1; }
.icon-btn { width: 42px; height: 42px; display: grid; place-items: center; border-radius: 50%; border: 0; background: var(--surface); }
.code-box { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); padding: 14px; margin: 8px 0; }
.code-text { font-size: 1.1rem; font-weight: 600; white-space: pre-wrap; word-break: break-all; }
.kv { display: grid; grid-template-columns: 110px 1fr; gap: 6px 10px; margin: 12px 0; font-size: .92rem; }
.kv dt { color: var(--muted); } .kv dd { margin: 0; word-break: break-all; }
.kv a { color: var(--accent-2); }
.photo { display: block; width: 100%; border-radius: var(--radius); background: var(--surface); }
.gen { background: #fff; border-radius: var(--radius); padding: 10px; display: grid; place-items: center; margin: 10px 0; }
.gen canvas { max-width: 100%; height: auto; }
.gen-note { color: #7a4a00; font-size: .8rem; text-align: center; padding: 4px; }
.section { margin: 18px 0 6px; font-size: .78rem; text-transform: uppercase; letter-spacing: .06em; color: var(--muted); }

/* ---------- Dialogue / toast ---------- */
dialog { border: 1px solid var(--border); background: var(--surface); color: var(--text); border-radius: 18px; padding: 18px; width: min(92vw, 420px); }
dialog::backdrop { background: #000b; }
dialog h3 { margin: 0 0 8px; }
dialog p { margin: 6px 0; }
.dlg-actions { display: flex; gap: 8px; justify-content: flex-end; margin-top: 16px; }
.dlg-err { color: var(--danger); font-size: .88rem; min-height: 1.2em; }
#toast { position: fixed; left: 50%; bottom: calc(var(--tab-h) + var(--safe-b) + 18px); transform: translateX(-50%); z-index: 40; max-width: 90vw;
  background: var(--surface-2); border: 1px solid var(--border); border-radius: 99px; padding: 10px 18px; font-size: .9rem; opacity: 0; pointer-events: none; transition: opacity .2s; }
#toast.show { opacity: 1; }

/* ---------- Page de connexion ---------- */
.login-body { display: grid; place-items: center; min-height: 100%; padding: 20px; }
.login { width: min(100%, 360px); text-align: center; }
.login h1 { margin: 8px 0 20px; font-size: 1.8rem; letter-spacing: .02em; }
.login form { text-align: left; }
.login label { display: block; margin-bottom: 6px; color: var(--muted); font-size: .9rem; }
.login input { margin-bottom: 14px; }
.login-error { color: var(--danger); margin: 0 0 12px; }
