:root {
  --bg: #0f172a;
  --surface: #1e293b;
  --surface-2: #334155;
  --text: #f1f5f9;
  --muted: #94a3b8;
  --primary: #0066cc;
  --accent: #38bdf8;
  --up: #22c55e;
  --down: #ef4444;
  --radius: 14px;
  --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 {
  margin: 0; padding: 0;
  background: var(--bg); color: var(--text);
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
  overscroll-behavior-y: contain;
}
body { padding-bottom: calc(64px + var(--safe-bottom)); }
h1 { font-size: 20px; margin: 0; }
h3 { font-size: 15px; margin: 0 0 10px; color: var(--text); }

/* En-tête */
.app-header {
  position: sticky; top: 0; z-index: 20;
  background: linear-gradient(180deg, #0b1220, var(--bg));
  padding: calc(var(--safe-top) + 10px) 14px 8px;
  border-bottom: 1px solid var(--surface-2);
}
.header-row { display: flex; align-items: center; justify-content: space-between; }
.sync-btn {
  background: var(--primary); color: #fff; border: 0; width: 40px; height: 40px;
  border-radius: 50%; font-size: 20px; line-height: 1;
}
.sync-btn.spinning { animation: spin 1s linear infinite; }
@keyframes spin { to { transform: rotate(360deg); } }

.period-bar { display: flex; gap: 6px; margin-top: 10px; overflow-x: auto; }
.period-bar button {
  flex: 0 0 auto; background: var(--surface); color: var(--muted); border: 0;
  padding: 7px 14px; border-radius: 20px; font-size: 13px; font-weight: 600;
}
.period-bar button.active { background: var(--primary); color: #fff; }

.sync-banner {
  padding: 8px 14px; font-size: 13px; background: var(--surface-2);
  text-align: center;
}
.sync-banner.ok { background: #14532d; }
.sync-banner.err { background: #7f1d1d; }

/* Vues */
.view { display: none; padding: 12px 14px; }
.view.active { display: block; }

/* Filtres */
.filters { display: flex; gap: 8px; margin-bottom: 12px; }
.filters select {
  flex: 1; background: var(--surface); color: var(--text);
  border: 1px solid var(--surface-2); border-radius: 10px; padding: 9px; font-size: 13px;
}

/* Cartes de vente */
.orders-list { display: flex; flex-direction: column; gap: 8px; }
.order-card {
  display: flex; gap: 12px; background: var(--surface);
  border-radius: var(--radius); padding: 10px; align-items: center;
}
.order-card img, .order-card .noimg {
  width: 56px; height: 56px; border-radius: 10px; object-fit: cover;
  background: var(--surface-2); flex: 0 0 56px;
  display: flex; align-items: center; justify-content: center; font-size: 22px;
}
.order-card .info { flex: 1; min-width: 0; }
.order-card .title {
  font-size: 14px; font-weight: 600; white-space: nowrap;
  overflow: hidden; text-overflow: ellipsis;
}
.order-card .meta { font-size: 12px; color: var(--muted); margin-top: 3px; }
.order-card .right { text-align: right; }
.order-card .price { font-size: 15px; font-weight: 700; }
.badge {
  display: inline-block; font-size: 10px; padding: 2px 7px; border-radius: 8px;
  background: var(--surface-2); color: var(--text); margin-top: 4px;
}
.badge.shipped { background: #0e7490; } .badge.delivered { background: #15803d; }
.badge.pending { background: #b45309; } .badge.returned, .badge.cancelled { background: #b91c1c; }
.chan { font-weight: 600; color: var(--accent); }

/* KPI */
.kpi-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; margin-bottom: 12px; }
.kpi {
  background: var(--surface); border-radius: var(--radius); padding: 12px;
}
.kpi .label { font-size: 11px; color: var(--muted); text-transform: uppercase; }
.kpi .value { font-size: 20px; font-weight: 700; margin-top: 2px; }
.kpi .delta { font-size: 12px; font-weight: 600; margin-top: 2px; }
.delta.up { color: var(--up); } .delta.down { color: var(--down); }

/* Cartes graph */
.card { background: var(--surface); border-radius: var(--radius); padding: 14px; margin-bottom: 12px; }
/* Hauteur de canvas bornée + conteneur relatif : évite la boucle de resize Chart.js. */
.card canvas { max-width: 100%; height: 240px !important; }

/* Tables */
.table-wrap { overflow-x: auto; }
table { width: 100%; border-collapse: collapse; font-size: 13px; }
th, td { text-align: right; padding: 7px 6px; border-bottom: 1px solid var(--surface-2); white-space: nowrap; }
th:first-child, td:first-child { text-align: left; }
th { color: var(--muted); font-weight: 600; font-size: 11px; text-transform: uppercase; }
.pos { color: var(--up); } .neg { color: var(--down); }

/* Boutons */
.secondary-btn, .primary-btn {
  width: 100%; padding: 12px; border: 0; border-radius: 12px; font-size: 15px;
  font-weight: 600; margin-top: 4px;
}
.secondary-btn { background: var(--surface-2); color: var(--text); }
.primary-btn { background: var(--primary); color: #fff; }

/* Onglets bas */
.tabbar {
  position: fixed; bottom: 0; left: 0; right: 0; z-index: 30;
  display: flex; background: #0b1220; border-top: 1px solid var(--surface-2);
  padding-bottom: var(--safe-bottom);
}
.tab {
  flex: 1; background: none; border: 0; color: var(--muted);
  padding: 8px 0 6px; font-size: 11px; font-weight: 600;
  display: flex; flex-direction: column; align-items: center; gap: 2px;
}
.tab span { font-size: 20px; }
.tab.active { color: var(--accent); }

/* Modale */
.modal {
  position: fixed; inset: 0; z-index: 50; background: rgba(0,0,0,.6);
  display: flex; align-items: flex-end;
}
.modal-content {
  background: var(--surface); width: 100%; max-height: 85vh; overflow-y: auto;
  border-radius: 18px 18px 0 0; padding: 16px;
  padding-bottom: calc(16px + var(--safe-bottom));
}
.modal-head { display: flex; justify-content: space-between; align-items: center; margin-bottom: 12px; }
.modal-head button { background: none; border: 0; color: var(--muted); font-size: 20px; }
.param-row { display: flex; align-items: center; justify-content: space-between; gap: 10px; margin-bottom: 10px; }
.param-row label { font-size: 13px; flex: 1; }
.param-row input {
  width: 90px; background: var(--bg); color: var(--text);
  border: 1px solid var(--surface-2); border-radius: 8px; padding: 8px; text-align: right;
}

/* Skeleton */
.skeleton { background: linear-gradient(90deg, var(--surface) 25%, var(--surface-2) 50%, var(--surface) 75%);
  background-size: 200% 100%; animation: shimmer 1.3s infinite; height: 76px; border-radius: var(--radius); margin-bottom: 8px; }
@keyframes shimmer { to { background-position: -200% 0; } }

/* Tableaux interactifs : tri, recherche, voir plus */
.tbl-search {
  width: 100%; background: var(--bg); color: var(--text);
  border: 1px solid var(--surface-2); border-radius: 10px;
  padding: 9px 12px; font-size: 14px; margin-bottom: 8px;
}
th { cursor: pointer; user-select: none; white-space: nowrap; }
th.sorted { color: var(--accent); }
.tbl-count { font-size: 11px; color: var(--muted); margin-top: 6px; }
.more-btn {
  width: 100%; margin-top: 8px; background: var(--surface-2); color: var(--text);
  border: 0; border-radius: 10px; padding: 10px; font-size: 13px; font-weight: 600;
}
.tag {
  display: inline-block; font-size: 10px; padding: 1px 6px; border-radius: 6px;
  background: var(--surface-2); color: var(--muted); margin: 1px 1px;
}
.reason { font-size: 11px; color: var(--muted); white-space: normal; display: block; max-width: 230px; }
.warn { color: #f59e0b; }

/* Chips de filtre */
.chips { display: flex; gap: 6px; overflow-x: auto; margin-bottom: 10px; }
.chip {
  flex: 0 0 auto; background: var(--surface-2); color: var(--muted); border: 0;
  padding: 6px 12px; border-radius: 16px; font-size: 12px; font-weight: 600;
}
.chip.active { background: var(--primary); color: #fff; }

/* Sous-navigation (2e niveau) : même langage visuel que la barre de période */
.subnav { display: flex; gap: 4px; margin-top: 8px; overflow-x: auto; }
.subnav .sub {
  flex: 0 0 auto; background: none; border: 0; color: var(--muted);
  padding: 8px 12px 7px; font-size: 13px; font-weight: 600;
  border-bottom: 2px solid transparent; white-space: nowrap;
}
.subnav .sub.active { color: var(--accent); border-bottom-color: var(--accent); }

.header-actions { display: flex; align-items: center; gap: 8px; }
.icon-btn {
  background: var(--surface-2); color: var(--text); border: 0;
  width: 40px; height: 40px; border-radius: 50%; font-size: 18px;
}
.hint { font-size: 12px; color: var(--muted); margin: -4px 0 10px; line-height: 1.4; }

/* Écran Réglages plein écran */
.settings-screen {
  position: fixed; inset: 0; z-index: 60; background: var(--bg);
  overflow-y: auto; display: flex; flex-direction: column;
}
.settings-body { padding: 12px 14px calc(24px + var(--safe-bottom)); }

/* Ligne de tarif transporteur */
.tarif-row {
  display: flex; align-items: center; gap: 10px;
  padding: 9px 0; border-bottom: 1px solid var(--surface-2);
}
.tarif-info { flex: 1; min-width: 0; }
.tarif-nom { font-size: 13px; font-weight: 600; }
.tarif-meta { font-size: 11px; color: var(--muted); margin-top: 2px; }
.tarif-row input {
  width: 84px; background: var(--bg); color: var(--text);
  border: 1px solid var(--surface-2); border-radius: 8px;
  padding: 9px; text-align: right; font-size: 15px;
}

/* Barre de proportion intégrée dans une cellule (remplace les camemberts) */
td.barcell { position: relative; text-align: right; }
.barfill {
  position: absolute; left: 0; top: 3px; bottom: 3px;
  background: linear-gradient(90deg, rgba(0,102,204,.55), rgba(56,189,248,.35));
  border-radius: 4px; z-index: 0;
}
.barval { position: relative; z-index: 1; font-weight: 600; }

/* Ligne marge cible */
.target-row { display: flex; align-items: center; gap: 8px; flex-wrap: wrap; }
.target-row input {
  width: 80px; background: var(--bg); color: var(--text);
  border: 1px solid var(--surface-2); border-radius: 10px; padding: 10px; font-size: 16px;
}
.target-row span { font-size: 13px; color: var(--muted); flex: 1; }
.primary-btn.small, .secondary-btn.small {
  width: auto; padding: 10px 16px; margin-top: 0;
}

/* Écran de connexion */
.login-overlay {
  position: fixed; inset: 0; z-index: 100;
  background: linear-gradient(160deg, #0b1220, var(--bg));
  display: flex; align-items: center; justify-content: center; padding: 24px;
}
.login-box {
  width: 100%; max-width: 340px; background: var(--surface);
  border-radius: 18px; padding: 28px 22px;
  display: flex; flex-direction: column; gap: 12px;
  box-shadow: 0 20px 60px rgba(0,0,0,.4);
}
.login-logo { font-size: 40px; text-align: center; }
.login-box h2 { text-align: center; margin: 0 0 8px; font-size: 20px; }
.login-box input {
  background: var(--bg); color: var(--text); border: 1px solid var(--surface-2);
  border-radius: 12px; padding: 14px; font-size: 16px;
}
.login-box button {
  background: var(--primary); color: #fff; border: 0; border-radius: 12px;
  padding: 14px; font-size: 16px; font-weight: 700; margin-top: 4px;
}
.login-box button:disabled { opacity: .6; }
.login-err { color: var(--down); font-size: 13px; text-align: center; min-height: 16px; }

.hidden { display: none !important; }
.sentinel { height: 40px; }
.empty { text-align: center; color: var(--muted); padding: 40px 0; }
.pull-hint { text-align: center; color: var(--muted); font-size: 12px; padding: 6px; }

/* Bouton d'import de fichier (input file masqué) */
.file-btn {
  display: block; width: 100%; text-align: center; margin-top: 8px;
  background: var(--surface-2); color: var(--text);
  border-radius: 12px; padding: 12px; font-size: 15px; font-weight: 600;
}

/* Sélecteur de plage personnalisée */
.custom-range {
  display: flex; align-items: center; gap: 6px; margin-top: 8px;
}
.custom-range input[type="date"] {
  flex: 1; background: var(--surface); color: var(--text);
  border: 1px solid var(--surface-2); border-radius: 10px;
  padding: 8px; font-size: 13px;
}
.custom-range span { color: var(--muted); font-size: 13px; }

/* ---------- Écrans IA ---------- */
.ia-card { border-left: 3px solid var(--accent); }
.ia-synthese {
  background: var(--surface-2); border-radius: 12px; padding: 12px 14px;
  font-size: 14px; line-height: 1.55; margin: 10px 0 14px; white-space: pre-line;
}
.ia-thinking { color: var(--muted); font-size: 14px; padding: 14px 0; }
.ia-thinking::after { content: ' ⏳'; }
.ia-err {
  color: var(--neg); font-size: 14px; padding: 12px;
  background: color-mix(in srgb, var(--neg) 12%, transparent); border-radius: 10px;
}

/* Recommandations produit */
.reco-item {
  border-left: 3px solid var(--surface-2); background: var(--surface);
  border-radius: 10px; padding: 10px 12px; margin-bottom: 10px;
}
.reco-item.act-up { border-left-color: #e8a13a; }
.reco-item.act-down { border-left-color: #4a9fd8; }
.reco-item.act-stop { border-left-color: var(--neg); }
.reco-item.act-fix { border-left-color: #9b7fd4; }
.reco-item.act-watch { border-left-color: var(--muted); }
.reco-item.act-ok { border-left-color: var(--pos); }
.reco-head { display: flex; flex-wrap: wrap; align-items: center; gap: 8px; }
.reco-act { font-weight: 600; font-size: 14px; }
.reco-sku { font-family: ui-monospace, monospace; font-size: 12px; color: var(--muted); }
.reco-hausse {
  background: color-mix(in srgb, #e8a13a 20%, transparent); color: #e8a13a;
  border-radius: 6px; padding: 2px 7px; font-size: 12px; font-weight: 600;
}
.reco-titre { font-size: 12px; color: var(--muted); margin-top: 3px; }
.reco-msg { font-size: 14px; line-height: 1.5; margin: 7px 0 0; }
.reco-stats { display: flex; flex-wrap: wrap; gap: 10px; margin-top: 8px;
  font-size: 12px; color: var(--muted); }

/* Revue des charges */
.revue-item {
  border-left: 3px solid var(--surface-2); background: var(--surface);
  border-radius: 10px; padding: 10px 12px; margin-bottom: 10px;
}
.revue-item.v-ok { border-left-color: var(--pos); }
.revue-item.v-warn { border-left-color: #e8a13a; }
.revue-item.v-bad { border-left-color: var(--neg); }
.revue-head { display: flex; justify-content: space-between; gap: 10px;
  align-items: baseline; font-size: 14px; }
.revue-mt { color: var(--muted); font-size: 12px; white-space: nowrap; }
.revue-ex { font-size: 11px; color: var(--muted); margin-top: 3px;
  overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.revue-move { font-size: 13px; margin-top: 6px; }
.revue-move .arrow { color: var(--muted); margin: 0 4px; }
.revue-q { font-size: 13px; margin: 6px 0 0; color: #e8a13a; line-height: 1.45; }
.revue-form { display: flex; flex-wrap: wrap; gap: 6px; margin-top: 9px; }
.revue-form select {
  background: var(--surface-2); color: var(--text); border: 1px solid var(--surface-2);
  border-radius: 8px; padding: 6px 8px; font-size: 12px; max-width: 100%;
}
.r-chk { display: flex; align-items: center; gap: 5px; font-size: 12px; color: var(--muted); }
.revue-chat { display: flex; gap: 6px; margin-top: 8px; align-items: stretch; }
.revue-chat input {
  flex: 1 1 auto; min-width: 0; width: 100%;
  background: var(--surface-2); color: var(--text);
  border: 1px solid var(--surface-2); border-radius: 8px;
  padding: 9px 11px; font-size: 14px;
}
.revue-chat input:focus {
  outline: none; border-color: var(--primary);
}
.revue-chat input::placeholder { color: var(--muted); }
/* Le bouton ne doit ni s'étirer ni écraser le champ de saisie. */
.revue-chat .secondary-btn { flex: 0 0 auto; white-space: nowrap; }
.ia-reply { background: var(--surface-2); border-radius: 8px; padding: 8px 10px; margin-top: 8px; }
.link-btn { background: none; border: none; color: var(--muted); cursor: pointer; font-size: 14px; }

/* ---------- Prévisionnel annuel ---------- */
.src { opacity: .5; font-size: 10px; }
.tot-row {
  display: flex; flex-wrap: wrap; gap: 10px; align-items: baseline;
  margin-top: 10px; padding-top: 10px; border-top: 1px solid var(--surface-2);
  font-size: 13px;
}
.tot-row span { color: var(--muted); }

.bloc-head { display: flex; gap: 8px; }
.bloc {
  flex: 1; background: var(--surface-2); border-radius: 12px; padding: 12px 10px;
  text-align: center; border-top: 3px solid var(--muted);
}
.bloc.b-rec { border-top-color: #4a9fd8; }
.bloc.b-ponc { border-top-color: #e8a13a; }
.bloc.b-dot { border-top-color: #9b7fd4; }
.bloc span { display: block; font-size: 11px; color: var(--muted); }
.bloc b { display: block; font-size: 16px; margin: 3px 0; }
.bloc em { font-size: 11px; color: var(--muted); font-style: normal; }

.rec-item {
  border-left: 2px solid var(--surface-2); padding: 8px 0 8px 10px;
  margin-bottom: 6px;
}
.rec-item.borne { border-left-color: #e8a13a; }
.rec-l1 { display: flex; justify-content: space-between; gap: 10px; font-size: 14px; }
.rec-mt { white-space: nowrap; }
.rec-l2 {
  display: flex; flex-wrap: wrap; gap: 8px; align-items: center;
  margin-top: 5px; font-size: 12px; color: var(--muted);
}
.rec-l2 input[type="month"] {
  background: var(--surface-2); color: var(--text); border: 1px solid var(--surface-2);
  border-radius: 8px; padding: 4px 7px; font-size: 12px; margin-left: 4px;
}

.remu-dispo {
  display: flex; justify-content: space-between; align-items: baseline;
  gap: 10px; padding: 12px; background: var(--surface-2); border-radius: 12px;
  margin-bottom: 12px;
}
.remu-dispo span { font-size: 13px; color: var(--muted); }
.remu-dispo b { font-size: 20px; }
.remu-grid { display: flex; flex-direction: column; gap: 10px; }
@media (min-width: 640px) { .remu-grid { flex-direction: row; } }
.remu-opt {
  flex: 1; background: var(--surface-2); border-radius: 12px; padding: 12px;
}
.remu-opt h4 { margin: 0 0 8px; font-size: 14px; }
.remu-l {
  display: flex; justify-content: space-between; align-items: baseline;
  gap: 8px; font-size: 13px; padding: 3px 0;
}
.remu-l span { color: var(--muted); }
.remu-scenario {
  margin-top: 12px; padding: 12px; border-radius: 12px;
  border: 1px solid var(--primary);
}
.remu-scenario h4 { margin: 0 0 8px; font-size: 14px; }

.rec-l2 select, .rec-l2 input[type="number"] {
  background: var(--surface-2); color: var(--text); border: 1px solid var(--surface-2);
  border-radius: 8px; padding: 4px 7px; font-size: 12px;
}
.rec-l2 input[type="number"] { width: 62px; margin-left: 4px; }

.bloc.b-sal { border-top-color: #e05a6a; }
.sal-form { display: flex; flex-wrap: wrap; gap: 6px; align-items: center; margin-bottom: 10px; }
.sal-form input[type="text"], .sal-form input:not([type]) { flex: 1 1 140px; min-width: 0; }
.sal-form input, .sal-form label {
  background: var(--surface-2); color: var(--text); border: 1px solid var(--surface-2);
  border-radius: 8px; padding: 8px 10px; font-size: 13px;
}
.sal-form label { display: flex; align-items: center; gap: 5px; color: var(--muted); font-size: 12px; }
.sal-form label input { border: 0; padding: 2px 4px; background: transparent; }
