:root {
  --primary: #9C2748;
  --primary-dark: #7A1D39;
  --primary-light: #FBEAEE;
  --accent: #E8776D;
  --bg: #F4F5F7;
  --card: #FFFFFF;
  --text: #1C1E21;
  --text-muted: #767B85;
  --border: #E7E9EC;
  --danger: #E5484D;
  --success: #1DAA61;
  --radius: 16px;
  --radius-sm: 10px;
  --safe-top: env(safe-area-inset-top, 0px);
  --safe-bottom: env(safe-area-inset-bottom, 0px);
}

* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }

html, body {
  height: 100%;
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  overscroll-behavior-y: none;
}

button, input { font-family: inherit; }

.screen {
  display: none;
  flex-direction: column;
  position: fixed;
  inset: 0;
  background: var(--bg);
}
.screen.active { display: flex; }

.content {
  flex: 1;
  overflow-y: auto;
  padding: 16px 16px calc(24px + var(--safe-bottom));
  -webkit-overflow-scrolling: touch;
}

/* ---------- Topbar ---------- */
.topbar {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: calc(14px + var(--safe-top)) 12px 14px;
  background: var(--primary);
  color: #fff;
  flex-shrink: 0;
}
.topbar-title { flex: 1; min-width: 0; text-align: center; }
.shop-name { font-size: 17px; font-weight: 700; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.shop-sub { font-size: 13px; opacity: .85; margin-top: 1px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.icon-btn {
  width: 40px; height: 40px; border-radius: 50%;
  border: none; background: rgba(255,255,255,.15); color: #fff;
  font-size: 18px; display: flex; align-items: center; justify-content: center;
  flex-shrink: 0;
}
.icon-btn:active { background: rgba(255,255,255,.3); }

/* ---------- Buttons ---------- */
.btn {
  border: none; border-radius: var(--radius-sm);
  padding: 15px 18px; font-size: 16px; font-weight: 600;
  display: inline-flex; align-items: center; justify-content: center; gap: 8px;
  cursor: pointer; transition: transform .08s ease, opacity .15s ease;
  min-height: 50px;
}
.btn:active { transform: scale(0.97); }
.btn-block { width: 100%; }
.btn-primary { background: var(--primary); color: #fff; }
.btn-primary:disabled { opacity: .6; }
.btn-secondary { background: var(--border); color: var(--text); }
.btn-danger { background: var(--danger); color: #fff; }
.btn-new { background: var(--primary); margin-bottom: 20px; font-size: 17px; }
.btn-new .plus { font-size: 20px; font-weight: 800; }

/* ---------- Forms ---------- */
.auth-wrap { max-width: 420px; margin: 0 auto; width: 100%; padding: 60px 24px 24px; }
.auth-logo { width: 84px; height: 84px; border-radius: 20px; display: block; margin: 0 auto 16px; object-fit: cover; box-shadow: 0 6px 20px rgba(0,0,0,.12); }
.auth-title { text-align: center; font-size: 24px; font-weight: 800; margin: 0 0 4px; }
.auth-subtitle { text-align: center; color: var(--text-muted); margin: 0 0 28px; font-size: 15px; }
.auth-form { display: flex; flex-direction: column; gap: 14px; }
.field { display: flex; flex-direction: column; gap: 6px; }
.field-label { font-size: 13px; font-weight: 600; color: var(--text-muted); padding-left: 2px; }
.field input {
  border: 1.5px solid var(--border); border-radius: var(--radius-sm);
  padding: 14px 14px; font-size: 16px; background: var(--card); color: var(--text);
  outline: none;
}
.field input:focus { border-color: var(--primary); }
.field-error { color: var(--danger); font-size: 14px; text-align: center; margin: 2px 0 0; }
.hidden { display: none !important; }

.spinner {
  display: inline-block;
  width: 18px; height: 18px; border-radius: 50%;
  border: 2.5px solid rgba(255,255,255,.4); border-top-color: #fff;
  animation: spin .7s linear infinite;
}
.loading-block .spinner { border-color: var(--border); border-top-color: var(--primary); width: 26px; height: 26px; margin: 40px auto; }
.loading-block { display: none; text-align: center; }
.loading-block.show { display: flex; flex: 1; align-items: center; justify-content: center; }
.loading-block.show .spinner { margin: 0; }
@keyframes spin { to { transform: rotate(360deg); } }

/* ---------- Caissiers ---------- */
.caissier-grid { display: grid; grid-template-columns: repeat(2, 1fr); gap: 12px; padding-top: 8px; }
.caissier-card {
  background: var(--card); border-radius: var(--radius); padding: 20px 10px;
  display: flex; flex-direction: column; align-items: center; gap: 10px;
  box-shadow: 0 2px 8px rgba(0,0,0,.05); border: 1.5px solid transparent;
}
.caissier-card:active { border-color: var(--primary); transform: scale(0.98); }
.caissier-avatar {
  width: 56px; height: 56px; border-radius: 50%; background: var(--primary-light);
  color: var(--primary-dark); font-weight: 800; font-size: 20px;
  display: flex; align-items: center; justify-content: center;
}
.caissier-nom { font-size: 14px; font-weight: 600; text-align: center; line-height: 1.25; }

/* ---------- PIN ---------- */
.pin-content { display: flex; flex-direction: column; align-items: center; padding-top: 40px; }
.pin-dots { display: flex; gap: 16px; margin-bottom: 14px; }
.pin-error { min-height: 20px; visibility: hidden; }
.pin-error.hidden { display: block !important; visibility: hidden; }
.pin-error:not(.hidden) { visibility: visible; }
.pin-dots .dot {
  width: 16px; height: 16px; border-radius: 50%; border: 2px solid var(--primary);
  background: transparent; transition: background .1s ease;
}
.pin-dots .dot.filled { background: var(--primary); }
.pin-content.shake { animation: shake .35s; }
@keyframes shake {
  10%, 90% { transform: translateX(-6px); }
  20%, 80% { transform: translateX(8px); }
  30%, 50%, 70% { transform: translateX(-10px); }
  40%, 60% { transform: translateX(10px); }
}
.keypad { display: grid; grid-template-columns: repeat(3, 76px); gap: 18px; margin-top: 28px; }
.keypad button {
  width: 76px; height: 76px; border-radius: 50%; border: none;
  background: var(--card); font-size: 26px; font-weight: 600; color: var(--text);
  box-shadow: 0 2px 6px rgba(0,0,0,.06);
}
.keypad button:active { background: var(--primary-light); }
.keypad .key-empty { background: transparent; box-shadow: none; }
.keypad .key-del { font-size: 20px; background: transparent; box-shadow: none; color: var(--text-muted); }

/* ---------- Home / inventory list ---------- */
.section-block { margin-bottom: 22px; }
.section-title { font-size: 14px; font-weight: 700; color: var(--text-muted); text-transform: uppercase; letter-spacing: .04em; margin: 0 0 10px 2px; }
.inv-list { display: flex; flex-direction: column; gap: 10px; }
.inv-card {
  background: var(--card); border-radius: var(--radius); padding: 14px 16px;
  display: flex; align-items: center; gap: 12px;
  box-shadow: 0 2px 8px rgba(0,0,0,.05);
}
.inv-card-main { flex: 1; min-width: 0; }
.inv-card-name { font-weight: 700; font-size: 15.5px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.inv-card-date { font-size: 13px; color: var(--text-muted); margin-top: 2px; }
.inv-card-status { font-size: 12px; font-weight: 700; margin-top: 3px; }
.status-dot { width: 9px; height: 9px; border-radius: 50%; flex-shrink: 0; }
.status-encours { background: var(--accent); }
.status-termine { background: var(--success); }
.status-autre { background: var(--text-muted); }
.status-dot.status-attente { background: var(--accent); }
.status-dot.status-annule { background: var(--danger); }
.status-dot.status-alimente { background: var(--success); }
.status-dot.status-inconnu { background: var(--text-muted); }
.inv-card-status.status-attente { color: var(--accent); }
.inv-card-status.status-annule { color: var(--danger); }
.inv-card-status.status-alimente { color: var(--success); }
.inv-card-status.status-inconnu { color: var(--text-muted); }
.inv-card-type { font-size: 11px; font-weight: 600; margin-top: 2px; text-transform: uppercase; letter-spacing: .3px; }
.inv-card-type.type-livraison { color: var(--primary); }
.inv-card-type.type-prelevement { color: #b8860b; }
.inv-card-del { background: transparent; border: none; color: var(--text-muted); font-size: 18px; padding: 6px; flex-shrink: 0; }
.empty-state { text-align: center; color: var(--text-muted); padding: 40px 20px; }

/* ---------- Count screen ---------- */
.topbar-count .shop-sub { opacity: .9; }
.progress-wrap { padding: 10px 16px 6px; background: var(--bg); flex-shrink: 0; }
.progress-bar { height: 8px; background: var(--border); border-radius: 6px; overflow: hidden; }
.progress-fill { height: 100%; background: linear-gradient(90deg, var(--primary), var(--success)); width: 0%; transition: width .25s ease; border-radius: 6px; }
.progress-text { font-size: 12.5px; color: var(--text-muted); margin-top: 6px; font-weight: 600; }

.search-wrap { padding: 6px 16px; flex-shrink: 0; }
.search-wrap input {
  width: 100%; border: 1.5px solid var(--border); border-radius: 12px;
  padding: 11px 14px; font-size: 15.5px; background: var(--card); outline: none;
}
.search-wrap input:focus { border-color: var(--primary); }

.content-count { padding-top: 0; padding-bottom: 110px; }

.cat-section {
  margin-bottom: 10px; background: var(--card); border-radius: var(--radius);
  box-shadow: 0 2px 8px rgba(0,0,0,.05); overflow: hidden;
}
.cat-header {
  padding: 15px 14px; display: flex; align-items: center; gap: 10px;
  background: var(--card); user-select: none;
}
.cat-header-name { flex: 1; font-size: 14.5px; font-weight: 800; text-transform: uppercase; letter-spacing: .03em; color: var(--primary-dark); }
.cat-header-count { font-size: 12.5px; color: var(--text-muted); font-weight: 700; background: var(--primary-light); padding: 3px 9px; border-radius: 10px; }
.cat-chevron { color: var(--text-muted); font-size: 13px; transition: transform .2s ease; flex-shrink: 0; }
.cat-section.open .cat-chevron { transform: rotate(180deg); }
.cat-body { padding: 0 10px 10px; }
.cat-body.collapsed { display: none; }

.prod-row {
  background: var(--card); border-radius: var(--radius); padding: 10px 12px;
  display: flex; align-items: center; gap: 12px; margin-bottom: 10px;
  border: 1.5px solid transparent; transition: border-color .2s ease, background .2s ease;
}
.prod-row.counted { border-color: var(--primary); background: var(--primary-light); }
.prod-row.hidden-row { display: none; }
.prod-img {
  width: 50px; height: 50px; border-radius: 12px; object-fit: cover; background: var(--border); flex-shrink: 0;
}
.prod-info { flex: 1; min-width: 0; }
.prod-name { font-size: 14.5px; font-weight: 700; line-height: 1.25; }
.prod-label { font-size: 12.5px; color: var(--text-muted); margin-top: 1px; }
.prod-unite { font-size: 11.5px; color: var(--text-muted); margin-top: 1px; }
.prod-save-state { font-size: 11px; margin-top: 3px; height: 14px; }
.prod-save-state.saving { color: var(--accent); }
.prod-save-state.saved { color: var(--success); }
.prod-save-state.error { color: var(--danger); }

.prod-row.dual-unit { display: block; }
.prod-row-top { display: flex; align-items: center; gap: 12px; }
.dual-qty-total { font-size: 12.5px; font-weight: 700; color: var(--primary); flex-shrink: 0; text-align: right; white-space: nowrap; }
.dual-qty-row {
  display: flex; gap: 10px; margin-top: 10px; padding-top: 10px; border-top: 1px solid var(--border);
}
.dual-qty-field { flex: 1; min-width: 0; display: flex; flex-direction: column; gap: 4px; }
.dual-qty-field span { font-size: 11.5px; font-weight: 700; color: var(--text-muted); }
.dual-qty-field input {
  width: 100%; box-sizing: border-box; min-width: 0;
  border: 1.5px solid var(--border); border-radius: 8px; padding: 8px 10px;
  font-size: 14.5px; font-weight: 700; background: var(--bg); color: var(--text); outline: none;
}
.dual-qty-field input:focus { border-color: var(--primary); }

.qty-control { display: flex; align-items: center; gap: 0; flex-shrink: 0; }
.qty-btn {
  width: 36px; height: 36px; border-radius: 50%; border: none; background: var(--primary-light);
  color: var(--primary-dark); font-size: 19px; font-weight: 700; display: flex; align-items: center; justify-content: center;
}
.qty-btn:active { background: var(--primary); color: #fff; }
.qty-input {
  width: 46px; text-align: center; border: none; background: transparent;
  font-size: 18px; font-weight: 800; color: var(--text); outline: none; margin: 0 2px;
}

.bottom-bar {
  position: fixed; left: 0; right: 0; bottom: 0; padding: 12px 16px calc(12px + var(--safe-bottom));
  background: linear-gradient(to top, var(--bg) 60%, transparent);
}
.bottom-bar .btn + .btn { margin-top: 8px; }
#arr-detail-content { padding-bottom: 140px; }
#arrivages-content { padding-bottom: 140px; }

/* ---------- Modal ---------- */
.modal-overlay {
  position: fixed; inset: 0; background: rgba(0,0,0,.45); z-index: 50;
  display: flex; align-items: center; justify-content: center; padding: 24px;
}
.modal { background: var(--card); border-radius: var(--radius); padding: 22px 20px; width: 100%; max-width: 340px; text-align: center; }
.modal p { font-size: 15.5px; font-weight: 600; margin: 0 0 18px; }
.modal-actions { display: flex; gap: 10px; }
.modal-actions .btn { flex: 1; }

/* ---------- Loader global (toutes les requêtes API) ---------- */
.global-loader {
  position: fixed; top: 0; left: 0; right: 0; height: 3px; z-index: 200;
  opacity: 0; pointer-events: none; transition: opacity .2s ease;
  overflow: hidden; background: rgba(156, 39, 72, .12);
}
.global-loader.show { opacity: 1; }
.global-loader-bar {
  position: absolute; top: 0; left: 0; height: 100%; width: 40%;
  background: var(--primary); border-radius: 0 3px 3px 0;
  animation: global-loader-slide 1.1s ease-in-out infinite;
}
@keyframes global-loader-slide {
  0%   { left: -40%; }
  60%  { left: 100%; }
  100% { left: 100%; }
}

/* ---------- Toast ---------- */
.toast {
  position: fixed; left: 50%; bottom: 24px; transform: translateX(-50%) translateY(100px);
  background: #24262B; color: #fff; padding: 12px 18px; border-radius: 12px;
  font-size: 14px; font-weight: 600; z-index: 100; opacity: 0; transition: transform .25s ease, opacity .25s ease;
  max-width: 90vw; text-align: center; box-shadow: 0 6px 20px rgba(0,0,0,.2);
}
.toast.show { transform: translateX(-50%) translateY(0); opacity: 1; }
.toast.toast-error { background: var(--danger); }
.toast.toast-success { background: var(--success); }

/* ---------- Ecarts ---------- */
.ecart-summary-box {
  margin: 14px 16px 0; background: var(--card); border-radius: var(--radius);
  padding: 16px; display: flex; align-items: center; gap: 14px; box-shadow: 0 2px 8px rgba(0,0,0,.05);
}
.ecart-summary-num { font-size: 30px; font-weight: 800; color: var(--primary-dark); line-height: 1; }
.ecart-summary-label { font-size: 13.5px; color: var(--text-muted); font-weight: 600; }
.ecart-link-btn {
  display: block; width: calc(100% - 32px); margin: 10px 16px 0; background: transparent; border: none;
  color: var(--primary); font-weight: 700; font-size: 13.5px; padding: 6px 0; text-align: center;
}
.ecart-row {
  background: var(--card); border-radius: var(--radius); padding: 10px 12px;
  display: flex; align-items: center; gap: 12px; margin-bottom: 10px;
  box-shadow: 0 1px 4px rgba(0,0,0,.04);
}
.ecart-meta { font-size: 12.5px; color: var(--text-muted); margin-top: 2px; }
.ecart-badge {
  flex-shrink: 0; font-size: 15px; font-weight: 800; padding: 8px 12px; border-radius: 12px; min-width: 54px; text-align: center;
}
.ecart-pos { background: #E4F5EF; color: #0E7C61; }
.ecart-neg { background: #FDEBEC; color: #C6262B; }
.ecart-zero { background: var(--border); color: var(--text-muted); }
.ecart-row { cursor: pointer; }

.info-banner {
  margin: 12px 16px 0; background: #FFF6E5; border: 1px solid #F5D98B; color: #8A6100;
  border-radius: var(--radius-sm); padding: 10px 12px; font-size: 12.5px; font-weight: 600; line-height: 1.4;
}

.modal-detail { max-width: 380px; text-align: left; position: relative; padding-top: 44px; max-height: 80vh; display: flex; flex-direction: column; }
.modal-close {
  position: absolute; top: 12px; right: 12px; width: 30px; height: 30px; border-radius: 50%;
  border: none; background: var(--bg); color: var(--text-muted); font-size: 14px; line-height: 1;
}
#ecart-detail-title { font-size: 16px; font-weight: 800; margin: 0 0 14px; text-align: center; color: var(--text); flex-shrink: 0; }
.ecart-detail-body { overflow-y: auto; }
.ecart-detail-explain { font-size: 13.5px; color: var(--text); background: var(--bg); border-radius: var(--radius-sm); padding: 12px; margin: 0 0 14px; line-height: 1.45; }
.ecart-detail-row {
  display: flex; justify-content: space-between; align-items: center; gap: 10px;
  padding: 9px 0; border-bottom: 1px solid var(--border); font-size: 13.5px;
}
.ecart-detail-row:last-child { border-bottom: none; }
.ecart-detail-row.ecart-detail-total .ecart-detail-label,
.ecart-detail-row.ecart-detail-total .ecart-detail-value { font-weight: 800; color: var(--primary-dark); }
.ecart-detail-label { color: var(--text-muted); }
.ecart-detail-value { font-weight: 700; color: var(--text); text-align: right; }

/* ---------- Bons de livraison / arrivage ---------- */
.btn-secondary.btn-new { background: var(--border); color: var(--text); }

.arr-row {
  background: var(--card); border-radius: var(--radius); padding: 10px 12px;
  margin-bottom: 10px; border: 1.5px solid transparent; transition: border-color .2s ease, background .2s ease;
}
.arr-row.counted { border-color: var(--primary); background: var(--primary-light); }
.arr-row.hidden-row { display: none; }
.arr-row-top { display: flex; align-items: center; gap: 12px; }
.arr-row-edit {
  display: flex; gap: 10px; margin-top: 10px; padding-top: 10px; border-top: 1px solid var(--border);
}
.arr-row-edit.hidden { display: none; }
.arr-field { flex: 1; min-width: 0; display: flex; flex-direction: column; gap: 4px; }
.arr-field span { font-size: 11.5px; font-weight: 700; color: var(--text-muted); }
.arr-field input {
  width: 100%; box-sizing: border-box; min-width: 0;
  border: 1.5px solid var(--border); border-radius: 8px; padding: 8px 10px;
  font-size: 14.5px; font-weight: 700; background: var(--bg); color: var(--text); outline: none;
}
.arr-field input:focus { border-color: var(--primary); }

.arr-row-ttc {
  margin-top: 8px; font-size: 12.5px; font-weight: 700; color: var(--primary); text-align: right;
}
.arr-row-ttc.hidden { display: none; }

.arr-summary { text-align: center; font-size: 13px; font-weight: 700; color: var(--text-muted); margin-bottom: 8px; }

.arr-detail-supplier {
  background: var(--card); border-radius: var(--radius); padding: 16px; margin-bottom: 14px;
  box-shadow: 0 2px 8px rgba(0,0,0,.05);
}
.arr-detail-supplier-name { font-size: 16px; font-weight: 800; }
.arr-detail-supplier-meta { font-size: 13px; color: var(--text-muted); margin-top: 4px; }
.arr-detail-product {
  background: var(--card); border-radius: var(--radius); padding: 12px 14px; margin-bottom: 10px;
  display: flex; align-items: center; gap: 12px; box-shadow: 0 1px 4px rgba(0,0,0,.04);
}
.arr-detail-product-info { flex: 1; min-width: 0; }
.arr-detail-product-name { font-size: 14px; font-weight: 700; }
.arr-detail-product-meta { font-size: 12.5px; color: var(--text-muted); margin-top: 2px; }
.arr-detail-product-count { font-weight: 800; color: var(--primary-dark); font-size: 15px; flex-shrink: 0; }
.arr-detail-total {
  background: var(--primary-light); border-radius: var(--radius); padding: 14px 16px; margin-top: 4px;
  display: flex; justify-content: space-between; align-items: center; font-weight: 800; color: var(--primary-dark);
}

@media (min-width: 560px) {
  .screen { max-width: 480px; left: 50%; transform: translateX(-50%); box-shadow: 0 0 40px rgba(0,0,0,.08); }
}
