/* Charte Cheops : bleu ciel (bandeau) + vert (menu latéral), fond blanc */
:root {
  color-scheme: light;
  --brand-blue: #4cc9f0;
  --brand-green: #bfdb7f;
  --header-blue: #0b80b3;     /* bleu Cheops foncé : texte blanc net */
  --bg: #ffffff;
  --surface: #ffffff;
  --subtle: #f5f7f9;          /* fonds secondaires très légers */
  --line: #e4e8ec;            /* filets */
  --text: #1d2733;
  --muted: #6b7684;
  --primary: #1ba3d6;         /* mode « Infos lot » */
  --primary-dark: #0b7fae;
  --accent: #8fbf3f;          /* mode « Ajout vente » */
  --accent-dark: #4f7a12;
  --accent-text: #1f3a0a;
  --ok: #3f8f1c;
  --info: #0b7fae;
  --danger: #c8322e;
  --radius: 10px;
  --font: "Segoe UI", system-ui, -apple-system, Roboto, "Helvetica Neue", Arial, sans-serif;
}

* { box-sizing: border-box; }

html, body {
  height: 100%;
  margin: 0;
  overflow: hidden;              /* l'application tient dans l'écran : pas de défilement */
  background: var(--bg);
  color: var(--text);
  font: 15px/1.4 var(--font);
  -webkit-tap-highlight-color: transparent;
  overscroll-behavior: none;
}

.hidden { display: none !important; }
.muted { color: var(--muted); }
.mono { font-family: ui-monospace, "Cascadia Mono", Consolas, monospace; }

.ico {
  width: 20px; height: 20px; flex: none;
  fill: none; stroke: currentColor; stroke-width: 2; stroke-linecap: round; stroke-linejoin: round;
}
.ico-xl { width: 40px; height: 40px; stroke-width: 1.6; }

/* --- Structure : colonne plein écran ------------------------------------ */
.app {
  height: 100vh;
  height: 100dvh;
  max-width: 640px;
  margin: 0 auto;
  display: flex;
  flex-direction: column;
  background: var(--bg);
}
.app > * { flex: none; }
.app > .camera { flex: 1 1 auto; min-height: 160px; }

/* --- En-tête (bandeau bleu Cheops) -------------------------------------- */
/* Bleu Cheops foncé pour un texte blanc net (contraste AA), liseré bleu ciel + vert de la charte */
.topbar {
  display: flex; align-items: center; gap: 8px;
  padding: calc(10px + env(safe-area-inset-top)) 12px 10px 16px;
  background: var(--header-blue); color: #fff;
  border-bottom: 3px solid var(--brand-blue);
  box-shadow: 0 3px 0 0 var(--brand-green);
  margin-bottom: 3px;
  -webkit-font-smoothing: antialiased;
}
.brand { display: grid; min-width: 0; flex: 1; }
.brand strong { font-size: 1.35rem; font-weight: 700; letter-spacing: .01em; line-height: 1.15; }
.status {
  display: flex; align-items: center; gap: 6px;
  font-size: .82rem; font-weight: 500; white-space: nowrap; overflow: hidden;
}
.status #status { overflow: hidden; text-overflow: ellipsis; }
.dot { width: 8px; height: 8px; border-radius: 50%; background: #fff; flex: none; }
.dot.offline { background: var(--danger); box-shadow: 0 0 0 2px #fff; }

.btn-sync {
  position: relative; display: inline-flex; align-items: center; gap: 6px;
  font: 600 .9rem var(--font); min-height: 40px; padding: 0 14px;
  border: 0; border-radius: 8px; background: #fff; color: var(--primary-dark); cursor: pointer;
}
.btn-sync .ico { width: 18px; height: 18px; }
.btn-sync:disabled { opacity: .75; }
.btn-sync.busy .ico { animation: spin 1s linear infinite; }
.sync-badge {
  position: absolute; top: -7px; right: -7px; min-width: 20px; height: 20px; padding: 0 5px;
  border-radius: 10px; background: var(--danger); color: #fff;
  font-size: .72rem; font-weight: 700; line-height: 20px; text-align: center;
}
@keyframes spin { to { transform: rotate(360deg); } }

.icon-btn {
  display: inline-grid; place-items: center; width: 40px; height: 40px; flex: none;
  border: 0; border-radius: 8px; background: transparent; color: var(--text); cursor: pointer;
}
.icon-btn.light { color: #fff; }

/* --- Onglets (contrôle segmenté) ---------------------------------------- */
.tabs {
  display: grid; grid-template-columns: 1fr 1fr; gap: 4px;
  margin: 10px 12px; padding: 4px; border-radius: var(--radius); background: var(--subtle);
  border: 1px solid var(--line);
}
.tab {
  display: inline-flex; align-items: center; justify-content: center; gap: 8px;
  font: 600 .95rem var(--font); min-height: 42px; cursor: pointer;
  border: 0; border-radius: 7px; background: transparent; color: var(--muted);
}
.tab .ico { width: 18px; height: 18px; }
#modeConsult.active { background: var(--primary); color: #fff; }
#modeAttrib.active  { background: var(--accent); color: var(--accent-text); }

/* --- Barre vente (mode ajout) ------------------------------------------- */
.vente-bar {
  display: none;
  grid-template-columns: minmax(0, 1fr) auto auto; gap: 8px;
  padding: 0 12px 10px;
}
body.attrib-mode .vente-bar { display: grid; }
.vente-select-wrap {
  display: grid; align-content: center; min-width: 0; cursor: pointer;
  border: 1px solid var(--line); border-left: 4px solid var(--accent);
  border-radius: var(--radius); padding: 4px 10px 6px; background: var(--surface);
}
.vente-label { font-size: .7rem; font-weight: 600; text-transform: uppercase; letter-spacing: .05em; color: var(--muted); }
.vente-select-wrap select {
  font: 600 1rem var(--font); width: 100%; border: 0; padding: 0; min-height: 26px;
  background: transparent; color: var(--text); outline: none;
}
.counter {
  position: relative; display: grid; place-items: center; align-content: center; min-width: 88px; padding: 4px 12px;
  font-family: var(--font); cursor: pointer; border: 0; border-radius: var(--radius);
  background: var(--accent); color: var(--accent-text);
}
.counter b { font-size: 1.35rem; line-height: 1.1; }
.counter small { font-size: .72rem; font-weight: 600; }
.vente-btn {
  display: grid; place-items: center; align-content: center; gap: 2px; min-width: 64px; padding: 4px 8px;
  font-family: var(--font); cursor: pointer; border-radius: var(--radius);
  border: 1px solid var(--accent); background: var(--surface); color: var(--accent-dark);
}
.vente-btn .ico { width: 20px; height: 20px; }
.vente-btn small { font-size: .68rem; font-weight: 600; line-height: 1.1; text-align: center; }
.lieu-box { display: grid; gap: 6px; margin: 12px 0 8px; padding: 10px 12px; border: 1px solid var(--line); border-radius: 8px; background: var(--subtle); }
.lieu-box label { font-size: .85rem; font-weight: 600; }
.lieu-row { display: flex; gap: 8px; }
.lieu-row input {
  flex: 1; min-width: 0; font: 1rem var(--font); padding: 8px 10px; min-height: 42px;
  border: 1px solid #cdd5dc; border-radius: 8px; background: #fff; color: var(--text);
}
.lieu-row .btn { min-height: 42px; white-space: nowrap; }
.sheet-summary { margin: 12px 0 4px; color: var(--muted); line-height: 1.5; }
.sheet-summary b { color: var(--text); }
.list .num { min-width: 32px; width: auto; }
/* Choix du tri (Ordre de passage / N° de lot) */
.seg {
  display: grid; grid-template-columns: 1fr 1fr; gap: 4px; margin: 8px 0 4px; padding: 3px;
  border: 1px solid var(--line); border-radius: 8px; background: var(--subtle);
}
.seg-btn {
  font: 600 .85rem var(--font); min-height: 36px; cursor: pointer;
  border: 0; border-radius: 6px; background: transparent; color: var(--muted);
}
.seg-btn.active { background: #fff; color: var(--primary-dark); box-shadow: 0 1px 3px rgba(0, 0, 0, .12); }
.rejected-dot {
  position: absolute; top: 5px; right: 5px; width: 10px; height: 10px; border-radius: 50%;
  background: var(--danger); box-shadow: 0 0 0 2px #fff;
}

/* --- Caméra -------------------------------------------------------------- */
.camera {
  position: relative; overflow: hidden; background: #0f1418;
  margin: 0 12px; border-radius: var(--radius);
  outline: 3px solid transparent; outline-offset: -3px; transition: outline-color .15s;
}
body.attrib-mode .camera { outline-color: var(--accent); }
#reader { position: absolute; inset: 0; }
#reader video { width: 100% !important; height: 100% !important; object-fit: cover; display: block; }

.viewfinder { position: absolute; inset: 0; display: grid; place-items: center; pointer-events: none; }
/* Seule cette zone est analysée : un seul code-barres doit y tenir */
.viewfinder span {
  position: relative; width: 80%; height: 28%; max-height: 140px; min-height: 70px;
  border: 2px solid rgba(255, 255, 255, .9); border-radius: 8px;
  box-shadow: 0 0 0 9999px rgba(0, 0, 0, .22);
}
.viewfinder span::after {
  content: ''; position: absolute; left: 6%; right: 6%; top: 50%; height: 2px;
  background: rgba(76, 201, 240, .95);
}
body:not(.cam-on) .viewfinder { display: none; }
/* Pause après un scan validé : cadre grisé, ligne de visée masquée */
.camera.cooldown .viewfinder span { border-color: rgba(255, 255, 255, .35); }
.camera.cooldown .viewfinder span::after { opacity: 0; }

/* Caméra éteinte : panneau clair */
.camera-start {
  position: absolute; inset: 0; display: grid; place-content: center; justify-items: center; gap: 6px;
  font-family: var(--font); text-align: center; cursor: pointer;
  background: var(--subtle); color: var(--text);
  border: 1px dashed #c9d2da; border-radius: var(--radius);
}
.camera-start .ico { color: var(--primary); margin-bottom: 4px; }
.cs-title { font-weight: 600; font-size: 1.05rem; }
.cs-sub { font-size: .85rem; color: var(--muted); }
body.attrib-mode .camera-start .ico { color: var(--accent-dark); }
body.cam-on .camera-start { display: none; }

/* Message en surimpression après chaque scan */
.flash {
  position: absolute; left: 10px; right: 10px; bottom: 10px;
  display: flex; align-items: center; gap: 10px;
  padding: 10px 14px; border-radius: 8px; color: #fff;
  opacity: 0; transform: translateY(8px); transition: opacity .15s, transform .15s; pointer-events: none;
  box-shadow: 0 4px 14px rgba(0, 0, 0, .18);
}
.flash.show { opacity: 1; transform: none; }
.flash .ico { width: 26px; height: 26px; stroke-width: 2.4; }
.flash-text { display: grid; min-width: 0; }
.flash-text b { font-size: 1.05rem; font-weight: 700; }
.flash-text span { font-size: .85rem; opacity: .92; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.flash.ok { background: var(--ok); }
.flash.info { background: var(--info); }
.flash.danger { background: var(--danger); }

.camera.pulse-ok { outline-color: var(--ok) !important; }
.camera.pulse-danger { outline-color: var(--danger) !important; }

.cam-tools { position: absolute; top: 10px; right: 10px; display: grid; gap: 8px; }
.cam-btn {
  display: grid; place-items: center; width: 42px; height: 42px; border-radius: 8px; cursor: pointer;
  border: 1px solid var(--line); background: rgba(255, 255, 255, .95); color: var(--text);
}

/* --- Résultat (hauteur fixe) -------------------------------------------- */
.result {
  height: clamp(170px, 38dvh, 360px);
  overflow-y: auto; overscroll-behavior: contain;
  padding: 12px 16px calc(12px + env(safe-area-inset-bottom));
}
.result-empty {
  height: 100%; display: grid; place-content: center; justify-items: center; gap: 8px;
  text-align: center; color: var(--muted);
}
.result-empty .ico { width: 34px; height: 34px; stroke-width: 1.6; color: #a9b4bf; }

.res-status {
  display: flex; align-items: center; gap: 8px; margin: 0 0 10px; padding: 8px 10px;
  border-radius: 8px; font-weight: 600; font-size: .92rem;
}
.res-status .ico { width: 18px; height: 18px; }
.res-status.ok { background: #eef6e8; color: var(--ok); }
.res-status.info { background: #e8f4fa; color: var(--info); }
.res-status.danger { background: #fbeceb; color: var(--danger); }
.res-status.warn { align-items: flex-start; background: #fdf3e3; color: #a35c00; font-weight: 500; }
.res-status.warn .ico { margin-top: 1px; }
.res-status.warn ul { margin: 4px 0 0; padding-left: 18px; }
.res-status.warn li { margin: 1px 0; }

.res-head { display: flex; align-items: center; justify-content: space-between; gap: 8px; flex-wrap: wrap; }
.res-code { font-family: ui-monospace, "Cascadia Mono", Consolas, monospace; font-weight: 700; font-size: 1.05rem; }
.res-title { margin: 6px 0 8px; font-size: 1rem; font-weight: 600; white-space: pre-line; }
body.attrib-mode .res-title { display: -webkit-box; -webkit-line-clamp: 2; -webkit-box-orient: vertical; overflow: hidden; }
.res-title.missing { color: var(--danger); }

/* Fiche : lignes libellé / valeur */
.facts { margin: 0; border-top: 1px solid var(--line); }
.fact {
  display: grid; grid-template-columns: 40% 1fr; gap: 12px;
  padding: 8px 0; border-bottom: 1px solid var(--line);
}
.fact dt { color: var(--muted); font-size: .88rem; }
.fact dd { margin: 0; font-weight: 600; text-align: right; word-break: break-word; white-space: pre-line; }

.tag {
  display: inline-block; padding: 3px 8px; border-radius: 6px;
  font-size: .8rem; font-weight: 600; border: 1px solid;
}
/* États du lot */
.tag.stock { color: var(--ok); border-color: #cfe5c2; background: #f3f9ef; }
.tag.prevu { color: var(--primary-dark); border-color: #c9e6f3; background: #eef8fc; }
.tag.invendu { color: #a35c00; border-color: #f1d9b0; background: #fdf3e3; }
.tag.vendu { color: #5b6b78; border-color: #d5dee5; background: #eef2f5; }
.tag.sorti { color: #ffffff; border-color: #5b6b78; background: #5b6b78; }
.tag.pending { color: var(--accent-dark); border-color: #d8e9b8; background: #f6fbec; }

.link-btn {
  font: 600 .88rem var(--font); border: 0; background: none; color: var(--primary-dark);
  padding: 4px 0; cursor: pointer; text-decoration: underline; text-underline-offset: 3px;
}

/* --- Panneau du bas (sheet) -------------------------------------------- */
dialog { color: var(--text); font-family: var(--font); }
dialog::backdrop { background: rgba(15, 23, 32, .4); }

.sheet {
  margin: auto auto 0; width: 100%; max-width: 640px; max-height: 82dvh;
  padding: 0; border: 0; border-radius: 14px 14px 0 0; background: var(--surface);
  display: none; flex-direction: column;
}
.sheet[open] { display: flex; animation: up .18s ease-out; }
@keyframes up { from { transform: translateY(30px); opacity: .6; } to { transform: none; opacity: 1; } }
.sheet-handle { width: 40px; height: 4px; border-radius: 2px; background: var(--line); margin: 8px auto 0; }
.sheet-head { display: flex; align-items: center; justify-content: space-between; padding: 4px 8px 6px 16px; border-bottom: 1px solid var(--line); }
.sheet-head h2 { margin: 0; font-size: 1.05rem; font-weight: 600; }
.sheet-body { overflow-y: auto; padding: 0 16px 8px; }
.sheet-foot { padding: 10px 16px calc(12px + env(safe-area-inset-bottom)); display: grid; gap: 8px; border-top: 1px solid var(--line); }
.sheet-foot:empty { display: none; }
.sheet h3 { font-size: .75rem; font-weight: 600; text-transform: uppercase; letter-spacing: .05em; color: var(--muted); margin: 16px 0 2px; }
.sheet h3.danger { color: var(--danger); }

.list { list-style: none; margin: 0; padding: 0; }
.list li { display: flex; align-items: center; gap: 12px; padding: 10px 0; border-bottom: 1px solid var(--line); }
.list .grow { flex: 1; min-width: 0; }
.list .line1 { font-weight: 600; }
.list .line2 { font-size: .84rem; color: var(--muted); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.list .num { width: 24px; text-align: right; color: var(--muted); font-size: .85rem; flex: none; font-variant-numeric: tabular-nums; }
.list button.row { all: unset; display: flex; align-items: center; gap: 12px; width: 100%; cursor: pointer; }
.list .rm {
  display: grid; place-items: center; width: 36px; height: 36px; border-radius: 8px; flex: none;
  border: 1px solid var(--line); background: var(--surface); color: var(--muted); cursor: pointer;
}
.list .rm .ico { width: 16px; height: 16px; }
.empty-msg { color: var(--muted); padding: 16px 0; text-align: center; }

/* --- Boutons & modales -------------------------------------------------- */
.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 8px;
  font: 600 .95rem var(--font); min-height: 44px; padding: 0 16px; cursor: pointer;
  border: 1px solid var(--line); border-radius: 8px; background: var(--surface); color: var(--text);
}
.btn .ico { width: 18px; height: 18px; }
.btn.primary { background: var(--primary); border-color: var(--primary); color: #fff; }
.btn.danger { color: var(--danger); }
.btn.wide { width: 100%; }
.btn-left { justify-content: flex-start; }

.modal {
  width: min(420px, calc(100vw - 32px)); padding: 0;
  border: 0; border-radius: 12px; background: var(--surface);
  box-shadow: 0 12px 40px rgba(0, 0, 0, .2);
}
.modal form { display: grid; gap: 12px; padding: 18px; }
.modal h2 { margin: 0 0 2px; font-size: 1.1rem; font-weight: 600; }
.modal input, .modal select {
  font: 1.05rem var(--font); padding: 10px 12px; min-height: 46px; width: 100%;
  border: 1px solid #cdd5dc; border-radius: 8px; background: #fff; color: var(--text);
}
.modal input:focus, .vente-select-wrap:focus-within { outline: 2px solid var(--brand-blue); outline-offset: 1px; }
.field { display: grid; gap: 6px; font-size: .88rem; color: var(--muted); }
.modal-actions { display: flex; justify-content: flex-end; gap: 8px; margin-top: 4px; }
.ask-message { margin: 0; line-height: 1.45; color: var(--text); white-space: pre-line; }
.ask-warn { margin: 0; padding: 8px 10px; border-radius: 8px; background: #fbeceb; color: var(--danger); font-weight: 600; font-size: .9rem; }
.btn.primary.btn-danger { background: var(--danger); border-color: var(--danger); }

/* --- Toast -------------------------------------------------------------- */
.toast {
  position: fixed; left: 50%; top: calc(70px + env(safe-area-inset-top));
  transform: translate(-50%, -16px); opacity: 0; pointer-events: none;
  max-width: calc(100vw - 32px); width: max-content;
  background: #1d2733; color: #fff; padding: 10px 16px; border-radius: 8px;
  font-size: .9rem; font-weight: 500; transition: .2s ease; z-index: 50;
  box-shadow: 0 6px 20px rgba(0, 0, 0, .2);
}
.toast.show { opacity: 1; transform: translate(-50%, 0); }

/* --- Désignation longue ------------------------------------------------- */
.res-title.clamp {
  display: -webkit-box; -webkit-box-orient: vertical; -webkit-line-clamp: 3;
  overflow: hidden; cursor: pointer;
}
body.attrib-mode .res-title.clamp { -webkit-line-clamp: 2; }
.more-btn {
  display: inline-flex; align-items: center; gap: 6px; margin: -2px 0 10px; padding: 6px 10px;
  font: 600 .85rem var(--font); color: var(--primary-dark); cursor: pointer;
  background: #e8f4fa; border: 1px solid #cfe6f2; border-radius: 6px;
}
.more-btn .ico { width: 16px; height: 16px; }
.detail-title { margin: 12px 0; font-size: 1.05rem; font-weight: 600; line-height: 1.45; white-space: pre-line; }

/* --- Écran de connexion -------------------------------------------------- */
.login {
  position: fixed; inset: 0; z-index: 100; display: grid; place-items: center; padding: 16px;
  background: linear-gradient(var(--header-blue) 0 180px, var(--subtle) 180px);
}
body.locked .app { visibility: hidden; }
.login-card {
  width: min(400px, 100%); display: grid; gap: 12px; padding: 24px 22px;
  background: var(--surface); border-radius: 12px; box-shadow: 0 10px 30px rgba(15, 23, 32, .18);
}
.login-brand { font-size: 1.5rem; font-weight: 700; color: var(--header-blue); }
.login-card h2 { margin: 0; font-size: 1.15rem; font-weight: 600; }
.login-card p { margin: 0; }
.login-card input {
  font: 600 1.15rem ui-monospace, "Cascadia Mono", Consolas, monospace; letter-spacing: .08em;
  padding: 10px 12px; min-height: 48px; width: 100%; text-transform: uppercase;
  border: 1px solid #cdd5dc; border-radius: 8px; background: #fff; color: var(--text);
}
.login-card input:focus { outline: 2px solid var(--brand-blue); outline-offset: 1px; }
.login-error { padding: 8px 10px; border-radius: 8px; background: #fbeceb; color: var(--danger); font-weight: 600; font-size: .9rem; }
.small-note { font-size: .8rem; }
.user-row { display: flex; align-items: center; gap: 8px; padding: 8px 10px; border-radius: 8px; background: var(--subtle); }
.user-row .ico { width: 18px; height: 18px; color: var(--primary-dark); }

/* Petits écrans en hauteur */
@media (max-height: 640px) {
  .result { height: clamp(140px, 32dvh, 240px); }
  .tabs { margin: 6px 12px; }
  .tab { min-height: 38px; }
}
