:root {
  color-scheme: light;
  --page: #f9f9f7;
  --surface: #fcfcfb;
  --surface-2: #f3f2ee;
  --border: #e1e0d9;
  --border-strong: #c3c2b7;
  --text-primary: #0b0b0b;
  --text-secondary: #52514e;
  --text-muted: #898781;

  --series-1: #2a78d6;   /* bleu — chiffre d'affaires */
  --series-1-wash: rgba(42, 120, 214, 0.12);
  --series-2: #eb6834;   /* orange — marge */
  --series-2-wash: rgba(235, 104, 52, 0.14);

  --good: #0ca30c;
  --good-bg: #e8f6e8;
  --warning: #fab219;
  --warning-bg: #fef3de;
  --warning-text: #8a5c00;
  --critical: #d03b3b;
  --critical-bg: #fbeaea;
  --success-text: #006300;

  --radius: 12px;
  --radius-sm: 8px;
  --shadow: 0 1px 2px rgba(11,11,11,0.05), 0 1px 8px rgba(11,11,11,0.04);
  --shadow-modal: 0 20px 60px rgba(11,11,11,0.25);
}

@media (prefers-color-scheme: dark) {
  :root:where(:not([data-theme="light"])) {
    color-scheme: dark;
    --page: #0d0d0d;
    --surface: #1a1a19;
    --surface-2: #232322;
    --border: #2c2c2a;
    --border-strong: #383835;
    --text-primary: #ffffff;
    --text-secondary: #c3c2b7;
    --text-muted: #8f8d86;

    --series-1: #3987e5;
    --series-1-wash: rgba(57, 135, 229, 0.16);
    --series-2: #d95926;
    --series-2-wash: rgba(217, 89, 38, 0.18);

    --good: #0ca30c;
    --good-bg: rgba(12, 163, 12, 0.16);
    --warning: #fab219;
    --warning-bg: rgba(250, 178, 25, 0.16);
    --warning-text: #fab219;
    --critical: #e66767;
    --critical-bg: rgba(230, 103, 103, 0.16);
    --success-text: #0ca30c;

    --shadow: 0 1px 2px rgba(0,0,0,0.3), 0 1px 8px rgba(0,0,0,0.25);
    --shadow-modal: 0 20px 60px rgba(0,0,0,0.5);
  }
}

:root[data-theme="dark"] {
  color-scheme: dark;
  --page: #0d0d0d;
  --surface: #1a1a19;
  --surface-2: #232322;
  --border: #2c2c2a;
  --border-strong: #383835;
  --text-primary: #ffffff;
  --text-secondary: #c3c2b7;
  --text-muted: #8f8d86;

  --series-1: #3987e5;
  --series-1-wash: rgba(57, 135, 229, 0.16);
  --series-2: #d95926;
  --series-2-wash: rgba(217, 89, 38, 0.18);

  --good: #0ca30c;
  --good-bg: rgba(12, 163, 12, 0.16);
  --warning: #fab219;
  --warning-bg: rgba(250, 178, 25, 0.16);
  --warning-text: #fab219;
  --critical: #e66767;
  --critical-bg: rgba(230, 103, 103, 0.16);
  --success-text: #0ca30c;

  --shadow: 0 1px 2px rgba(0,0,0,0.3), 0 1px 8px rgba(0,0,0,0.25);
  --shadow-modal: 0 20px 60px rgba(0,0,0,0.5);
}

* { box-sizing: border-box; }

body {
  margin: 0;
  font-family: system-ui, -apple-system, "Segoe UI", sans-serif;
  background: var(--page);
  color: var(--text-primary);
  font-size: 15px;
}

/* ---------------------------------------------------------------------- */
/* Topbar                                                                  */
/* ---------------------------------------------------------------------- */

.topbar {
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  padding: 14px 24px 0;
  position: sticky;
  top: 0;
  z-index: 20;
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: 16px;
}

.topbar-title { display: flex; align-items: center; gap: 10px; }
.topbar h1 { margin: 0 0 12px; font-size: 1.3rem; }

.topbar-actions { display: flex; gap: 8px; align-items: center; }

.icon-btn {
  border: 1px solid var(--border);
  background: var(--surface);
  color: var(--text-secondary);
  border-radius: var(--radius-sm);
  width: 36px; height: 36px;
  display: flex; align-items: center; justify-content: center;
  cursor: pointer;
  font-size: 1rem;
}
.icon-btn:hover { background: var(--surface-2); color: var(--text-primary); }

.tabs { display: flex; gap: 4px; flex-wrap: wrap; }

.tab-btn {
  background: none;
  border: none;
  padding: 10px 16px;
  font-size: 0.92rem;
  cursor: pointer;
  color: var(--text-muted);
  border-bottom: 3px solid transparent;
  border-radius: 6px 6px 0 0;
  white-space: nowrap;
}

.tab-btn:hover { background: var(--series-1-wash); color: var(--text-primary); }

.tab-btn.active {
  color: var(--series-1);
  border-bottom-color: var(--series-1);
  font-weight: 600;
}

main { padding: 24px; max-width: 1240px; margin: 0 auto; }

.tab-panel { display: none; }
.tab-panel.active { display: block; }

/* ---------------------------------------------------------------------- */
/* KPI cards                                                               */
/* ---------------------------------------------------------------------- */

.kpi-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(170px, 1fr));
  gap: 14px;
  margin-bottom: 24px;
}

.kpi-card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 18px;
  box-shadow: var(--shadow);
}

.kpi-value { font-size: 1.7rem; font-weight: 700; }
.kpi-label { color: var(--text-muted); font-size: 0.85rem; margin-top: 4px; }
.kpi-sub { color: var(--text-muted); font-size: 0.75rem; margin-top: 2px; }

.kpi-alert .kpi-value { color: var(--critical); }
.kpi-alert.has-alerts { background: var(--critical-bg); border-color: var(--critical); }
.kpi-warning.has-alerts { background: var(--warning-bg); border-color: var(--warning); }
.kpi-warning.has-alerts .kpi-value { color: var(--warning-text); }

.dash-columns {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 20px;
  margin-bottom: 20px;
}

@media (max-width: 800px) {
  .dash-columns { grid-template-columns: 1fr; }
}

.dash-block {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 18px;
  box-shadow: var(--shadow);
}

.dash-block h2 { margin-top: 0; font-size: 1.05rem; }

.bar-list { display: flex; flex-direction: column; gap: 8px; }

.bar-row { display: flex; align-items: center; gap: 8px; font-size: 0.85rem; }
.bar-row .bar-label { width: 76px; flex-shrink: 0; color: var(--text-muted); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.bar-row .bar-track { flex: 1; background: var(--surface-2); border-radius: 4px; height: 16px; overflow: hidden; }
.bar-row .bar-fill { height: 100%; background: var(--series-1); border-radius: 4px; min-width: 3px; }
.bar-row .bar-value { min-width: 38px; text-align: right; font-weight: 600; font-variant-numeric: tabular-nums; white-space: nowrap; }

/* Alertes stock bas */
.low-stock-list { display: flex; flex-direction: column; gap: 8px; max-height: 260px; overflow-y: auto; }
.low-stock-row {
  display: flex; align-items: center; justify-content: space-between;
  padding: 8px 10px; border-radius: var(--radius-sm);
  background: var(--warning-bg); font-size: 0.85rem;
}
.low-stock-row.out { background: var(--critical-bg); }
.low-stock-row .lsr-name { font-weight: 600; }
.low-stock-row .lsr-meta { color: var(--text-secondary); font-size: 0.8rem; }
.low-stock-row .lsr-qty { font-weight: 700; font-variant-numeric: tabular-nums; }

/* ---------------------------------------------------------------------- */
/* Toolbar & inputs                                                        */
/* ---------------------------------------------------------------------- */

.toolbar {
  display: flex;
  flex-wrap: wrap;
  gap: 10px;
  margin-bottom: 16px;
  align-items: center;
}

.toolbar input[type="search"] { flex: 1; min-width: 200px; }

.checkbox-label { display: flex; align-items: center; gap: 6px; font-size: 0.9rem; color: var(--text-muted); }

input[type="text"], input[type="number"], input[type="date"], input[type="search"],
input[type="password"], select, textarea {
  padding: 8px 10px;
  border: 1px solid var(--border-strong);
  border-radius: var(--radius-sm);
  font-size: 0.9rem;
  font-family: inherit;
  background: var(--surface);
  color: var(--text-primary);
}

input:focus, select:focus { outline: 2px solid var(--series-1); outline-offset: -1px; }

/* ---------------------------------------------------------------------- */
/* Buttons                                                                 */
/* ---------------------------------------------------------------------- */

.btn {
  padding: 9px 16px;
  border-radius: var(--radius-sm);
  border: 1px solid var(--border-strong);
  background: var(--surface);
  cursor: pointer;
  font-size: 0.9rem;
  font-weight: 500;
  color: var(--text-primary);
}

.btn:hover { filter: brightness(0.97); }
:root[data-theme="dark"] .btn:hover { filter: brightness(1.15); }
@media (prefers-color-scheme: dark) { :root:not([data-theme="light"]) .btn:hover { filter: brightness(1.15); } }

.btn-primary { background: var(--series-1); color: white; border-color: var(--series-1); }
.btn-primary:hover { filter: brightness(0.92); }

.btn-secondary { background: var(--series-1-wash); color: var(--series-1); border-color: transparent; }

.btn-danger { background: var(--critical-bg); color: var(--critical); border-color: transparent; }

.btn-row { margin-bottom: 12px; display: flex; gap: 10px; flex-wrap: wrap; }

.file-btn { display: inline-flex; align-items: center; }

.btn-sm { padding: 4px 10px; font-size: 0.8rem; }

.btn-block { width: 100%; }

/* ---------------------------------------------------------------------- */
/* Table (historique des ventes)                                          */
/* ---------------------------------------------------------------------- */

.table-wrap { overflow-x: auto; background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); box-shadow: var(--shadow); }

table { width: 100%; border-collapse: collapse; font-size: 0.87rem; }

th, td { padding: 10px 12px; text-align: left; border-bottom: 1px solid var(--border); white-space: nowrap; }

th { background: var(--surface-2); color: var(--text-muted); font-weight: 600; font-size: 0.8rem; text-transform: uppercase; letter-spacing: 0.02em; }

tbody tr:hover { background: var(--series-1-wash); }

td.numeric { text-align: right; font-variant-numeric: tabular-nums; }

.row-alert { background: var(--critical-bg) !important; }

.badge { display: inline-flex; align-items: center; gap: 4px; padding: 2px 9px; border-radius: 20px; font-size: 0.75rem; font-weight: 600; }
.badge-good { background: var(--good-bg); color: var(--success-text); }
.badge-warning { background: var(--warning-bg); color: var(--warning-text); }
.badge-critical { background: var(--critical-bg); color: var(--critical); }

/* ---------------------------------------------------------------------- */
/* Cartes produit — Inventaire                                            */
/* ---------------------------------------------------------------------- */

.product-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(230px, 1fr));
  gap: 16px;
}

.product-card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  overflow: hidden;
  display: flex;
  flex-direction: column;
}

.product-photo {
  aspect-ratio: 1 / 1;
  background: var(--surface-2);
  display: flex; align-items: center; justify-content: center;
  color: var(--text-muted);
  font-size: 2.4rem;
  overflow: hidden;
  position: relative;
}

.product-photo img { width: 100%; height: 100%; object-fit: cover; display: block; }

.product-body { padding: 12px 14px 14px; display: flex; flex-direction: column; gap: 6px; flex: 1; }

.product-sku { font-size: 0.72rem; color: var(--text-muted); font-weight: 600; letter-spacing: 0.03em; }
.product-title { font-weight: 600; font-size: 0.95rem; line-height: 1.25; }
.product-meta { font-size: 0.8rem; color: var(--text-secondary); }

.product-badges { display: flex; gap: 6px; flex-wrap: wrap; margin-top: 2px; }

.product-numbers { display: flex; justify-content: space-between; align-items: baseline; margin-top: auto; padding-top: 8px; }
.product-numbers .stock-total { font-weight: 700; font-variant-numeric: tabular-nums; }
.product-numbers .prix { color: var(--text-secondary); font-variant-numeric: tabular-nums; font-size: 0.85rem; }
.product-numbers .marge { color: var(--good); font-size: 0.75rem; }

.product-actions { display: flex; gap: 6px; padding: 0 14px 14px; }
.product-actions .btn { flex: 1; }

/* ---------------------------------------------------------------------- */
/* Forms                                                                   */
/* ---------------------------------------------------------------------- */

.card-form {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 20px 22px;
  box-shadow: var(--shadow);
  margin-bottom: 20px;
}

.card-form h2 { margin-top: 0; }

.form-grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 12px 16px;
  margin-bottom: 14px;
}

.form-grid label { display: flex; flex-direction: column; gap: 4px; font-size: 0.85rem; color: var(--text-muted); }

.form-grid .span-2 { grid-column: 1 / -1; }

.stock-hint { font-size: 0.85rem; color: var(--text-muted); margin-bottom: 12px; min-height: 1.2em; }
.stock-hint.warn { color: var(--critical); font-weight: 600; }

.section-title { margin-top: 30px; }

.save-status { color: var(--success-text); font-weight: 600; min-height: 1.2em; }

.photo-upload { display: flex; align-items: center; gap: 12px; margin-bottom: 16px; }
.photo-preview {
  width: 84px; height: 84px; border-radius: var(--radius-sm);
  background: var(--surface-2); display: flex; align-items: center; justify-content: center;
  overflow: hidden; color: var(--text-muted); font-size: 1.6rem; flex-shrink: 0;
  border: 1px solid var(--border);
}
.photo-preview img { width: 100%; height: 100%; object-fit: cover; }

/* ---------------------------------------------------------------------- */
/* Modal                                                                   */
/* ---------------------------------------------------------------------- */

.modal-overlay {
  position: fixed; inset: 0; background: rgba(11,11,11,0.5);
  display: flex; align-items: flex-start; justify-content: center;
  padding: 40px 16px; overflow-y: auto; z-index: 100;
}

.modal-overlay.hidden { display: none; }

.modal {
  background: var(--surface);
  border-radius: var(--radius);
  padding: 24px 28px;
  width: 100%;
  max-width: 640px;
  box-shadow: var(--shadow-modal);
}

.modal h2 { margin-top: 0; }
.modal h3 { font-size: 0.95rem; color: var(--text-muted); margin-bottom: 10px; }

.tailles-grid {
  display: grid;
  grid-template-columns: repeat(5, 1fr);
  gap: 8px;
  margin-bottom: 18px;
}

.taille-cell { display: flex; flex-direction: column; gap: 3px; align-items: center; }
.taille-cell label { font-size: 0.75rem; color: var(--text-muted); }
.taille-cell input { width: 100%; text-align: center; padding: 6px 4px; }

.add-size-row { display: flex; gap: 8px; margin-bottom: 20px; }
.add-size-row input { flex: 1; }

.modal-actions { display: flex; justify-content: flex-end; gap: 10px; }

/* Gestion des tailles (Réglages) */
.size-manage-list { display: flex; flex-wrap: wrap; gap: 8px; }
.size-manage-chip {
  display: inline-flex; align-items: center; gap: 6px;
  padding: 5px 6px 5px 12px; border: 1px solid var(--border); border-radius: 20px;
  background: var(--surface-2); font-size: 0.85rem; font-variant-numeric: tabular-nums;
}
.size-manage-chip button {
  border: none; background: none; cursor: pointer; color: var(--text-muted);
  font-size: 1rem; line-height: 1; width: 20px; height: 20px; border-radius: 50%;
}
.size-manage-chip button:hover { background: var(--critical-bg); color: var(--critical); }

/* ---------------------------------------------------------------------- */
/* Vente rapide                                                           */
/* ---------------------------------------------------------------------- */

.quick-sale { display: grid; grid-template-columns: 1.1fr 1fr; gap: 20px; align-items: start; }
@media (max-width: 900px) { .quick-sale { grid-template-columns: 1fr; } }

.qs-picker { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); box-shadow: var(--shadow); padding: 16px; }
.qs-picker h2 { margin-top: 0; }

.qs-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(150px, 1fr));
  gap: 10px;
  max-height: 460px;
  overflow-y: auto;
  padding: 4px 2px;
}

.qs-card {
  border: 2px solid var(--border);
  border-radius: var(--radius-sm);
  background: var(--surface);
  cursor: pointer;
  overflow: hidden;
  text-align: left;
  padding: 0;
  font: inherit;
  color: inherit;
}

.qs-card:hover { border-color: var(--series-1); }
.qs-card.selected { border-color: var(--series-1); box-shadow: 0 0 0 2px var(--series-1-wash); }

.qs-card .product-photo { aspect-ratio: 4 / 3; font-size: 1.6rem; }
.qs-card .qs-card-body { padding: 8px 10px; }
.qs-card .qs-card-title { font-weight: 600; font-size: 0.85rem; line-height: 1.2; }
.qs-card .qs-card-meta { font-size: 0.75rem; color: var(--text-muted); }

.qs-panel { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); box-shadow: var(--shadow); padding: 20px; position: sticky; top: 90px; }

.qs-selected-header { display: flex; gap: 12px; align-items: center; margin-bottom: 16px; }
.qs-selected-header .product-photo { width: 64px; height: 64px; border-radius: var(--radius-sm); flex-shrink: 0; font-size: 1.3rem; }
.qs-selected-title { font-weight: 700; }
.qs-selected-meta { font-size: 0.82rem; color: var(--text-muted); }

.qs-empty { color: var(--text-muted); font-size: 0.9rem; text-align: center; padding: 40px 10px; }

.size-chip-grid { display: flex; flex-wrap: wrap; gap: 8px; margin-bottom: 18px; }

.size-chip {
  min-width: 46px; padding: 8px 10px;
  border: 2px solid var(--border); border-radius: var(--radius-sm);
  background: var(--surface); cursor: pointer; text-align: center;
  font-weight: 600; font-size: 0.85rem; color: var(--text-primary);
}
.size-chip .chip-qty { display: block; font-size: 0.68rem; font-weight: 500; color: var(--text-muted); }
.size-chip.selected { border-color: var(--series-1); background: var(--series-1-wash); color: var(--series-1); }
.size-chip.low .chip-qty { color: var(--warning-text); }
.size-chip.empty { opacity: 0.35; cursor: not-allowed; }

.qty-stepper { display: flex; align-items: center; gap: 14px; margin-bottom: 18px; }
.qty-stepper button {
  width: 44px; height: 44px; border-radius: 50%; border: 1px solid var(--border-strong);
  background: var(--surface); font-size: 1.3rem; cursor: pointer; color: var(--text-primary);
}
.qty-stepper button:hover { background: var(--surface-2); }
.qty-stepper .qty-value { font-size: 1.6rem; font-weight: 700; min-width: 40px; text-align: center; font-variant-numeric: tabular-nums; }

.qs-price-row { display: flex; align-items: center; gap: 10px; margin-bottom: 18px; }
.qs-price-row label { font-size: 0.85rem; color: var(--text-muted); }
.qs-price-row input { width: 110px; }

.qs-total { font-size: 1.1rem; font-weight: 700; margin-bottom: 16px; }
.qs-total .amount { color: var(--series-1); }

/* ---------------------------------------------------------------------- */
/* Statistiques                                                            */
/* ---------------------------------------------------------------------- */

.stats-grid { display: grid; grid-template-columns: 1fr; gap: 20px; }

.legend { display: flex; gap: 16px; margin-bottom: 12px; font-size: 0.82rem; color: var(--text-secondary); }
.legend-item { display: flex; align-items: center; gap: 6px; }
.legend-swatch { width: 10px; height: 10px; border-radius: 3px; display: inline-block; }

.month-row { display: flex; align-items: center; gap: 10px; font-size: 0.85rem; margin-bottom: 8px; }
.month-row .month-label { width: 60px; flex-shrink: 0; color: var(--text-muted); }
.month-bars { flex: 1; display: flex; flex-direction: column; gap: 3px; }
.month-bar-track { background: var(--surface-2); border-radius: 4px; height: 12px; overflow: hidden; }
.month-bar-fill { height: 100%; border-radius: 4px; min-width: 2px; }
.month-bar-fill.ca { background: var(--series-1); }
.month-bar-fill.marge { background: var(--series-2); }
.month-values { width: 90px; text-align: right; font-variant-numeric: tabular-nums; font-size: 0.78rem; color: var(--text-secondary); }

/* ---------------------------------------------------------------------- */
/* Toasts                                                                  */
/* ---------------------------------------------------------------------- */

#toast-container {
  position: fixed; bottom: 20px; right: 20px; z-index: 200;
  display: flex; flex-direction: column; gap: 8px; max-width: 320px;
}

.toast {
  background: var(--surface); border: 1px solid var(--border); border-left: 4px solid var(--series-1);
  border-radius: var(--radius-sm); padding: 12px 14px; box-shadow: var(--shadow-modal);
  font-size: 0.87rem; color: var(--text-primary);
  animation: toast-in 0.18s ease-out;
}
.toast.error { border-left-color: var(--critical); }
.toast.success { border-left-color: var(--good); }

@keyframes toast-in { from { opacity: 0; transform: translateY(6px); } to { opacity: 1; transform: translateY(0); } }

/* ---------------------------------------------------------------------- */
/* Responsive                                                              */
/* ---------------------------------------------------------------------- */

@media (max-width: 600px) {
  main { padding: 14px; }
  .form-grid { grid-template-columns: 1fr; }
  .tailles-grid { grid-template-columns: repeat(3, 1fr); }
  .topbar { padding: 10px 14px 0; flex-direction: column; }
  .tab-btn { padding: 8px 10px; font-size: 0.85rem; }
  .product-grid { grid-template-columns: repeat(auto-fill, minmax(150px, 1fr)); }
}
