:root {
  --bg: #f6f7f9;
  --surface: #ffffff;
  --surface-2: #f0f2f5;
  --border: #dfe3e8;
  --text: #17202a;
  --muted: #67737f;
  --accent: #1d4ed8;
  --accent-soft: #e8eefc;
  --ok: #12795a;
  --ok-soft: #e2f5ee;
  --warn: #9a6200;
  --warn-soft: #fdf1dc;
  --danger: #b42318;
  --danger-soft: #fdeceb;
  --radius: 10px;
  --shadow: 0 1px 2px rgba(16, 24, 40, .06), 0 1px 3px rgba(16, 24, 40, .08);
  --mono: ui-monospace, SFMono-Regular, "SF Mono", Menlo, monospace;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #10151b; --surface: #171d25; --surface-2: #1d242e; --border: #2b3542;
    --text: #e8edf3; --muted: #93a1b1; --accent: #6b9bff; --accent-soft: #1b2941;
    --ok: #5fd1aa; --ok-soft: #14312a; --warn: #e0aa5a; --warn-soft: #372b17;
    --danger: #f08b82; --danger-soft: #3a1f1d;
    --shadow: 0 1px 2px rgba(0, 0, 0, .3);
  }
}

* { box-sizing: border-box; }

body {
  margin: 0;
  font: 15px/1.5 -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  background: var(--bg);
  color: var(--text);
  -webkit-font-smoothing: antialiased;
}

a { color: var(--accent); text-decoration: none; }
a:hover { text-decoration: underline; }

/* ------------------------------------------------------------ layout --- */
.shell { display: flex; min-height: 100vh; }

.sidebar {
  width: 216px; flex: 0 0 216px; background: var(--surface);
  border-right: 1px solid var(--border); padding: 18px 12px; position: sticky;
  top: 0; height: 100vh; overflow-y: auto;
}
.brand { display: flex; align-items: center; gap: 9px; padding: 0 8px 16px; font-weight: 700; letter-spacing: -.01em; }
.brand .logo {
  width: 28px; height: 28px; border-radius: 8px; background: var(--accent);
  color: #fff; display: grid; place-items: center; font-size: 15px; flex: 0 0 28px;
}
.nav { display: flex; flex-direction: column; gap: 1px; }
.nav a {
  display: flex; align-items: center; gap: 9px; padding: 8px 10px; border-radius: 8px;
  color: var(--text); font-size: 14px; text-decoration: none;
}
.nav a:hover { background: var(--surface-2); }
.nav a.active { background: var(--accent-soft); color: var(--accent); font-weight: 600; }
.nav .sep { margin: 12px 10px 5px; font-size: 11px; text-transform: uppercase;
  letter-spacing: .07em; color: var(--muted); font-weight: 600; }

.main { flex: 1; min-width: 0; display: flex; flex-direction: column; }
.topbar {
  display: flex; align-items: center; justify-content: space-between; gap: 16px;
  padding: 14px 24px; border-bottom: 1px solid var(--border); background: var(--surface);
  position: sticky; top: 0; z-index: 20;
}
.topbar h1 { margin: 0; font-size: 18px; letter-spacing: -.015em; }
.content { padding: 22px 24px 64px; max-width: 1400px; width: 100%; }

.menu-toggle { display: none; background: none; border: 1px solid var(--border);
  border-radius: 8px; padding: 6px 10px; color: var(--text); cursor: pointer; }

@media (max-width: 880px) {
  .sidebar { position: fixed; z-index: 40; transform: translateX(-100%); transition: transform .18s; }
  .sidebar.open { transform: none; box-shadow: 0 0 0 100vmax rgba(0,0,0,.35); }
  .menu-toggle { display: block; }
  .content { padding: 16px; }
  .topbar { padding: 12px 16px; }
}

/* ------------------------------------------------------------- cards --- */
.card {
  background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius);
  box-shadow: var(--shadow); margin-bottom: 18px; overflow: hidden;
}
.card > header {
  display: flex; align-items: center; justify-content: space-between; gap: 12px;
  padding: 13px 16px; border-bottom: 1px solid var(--border); background: var(--surface);
}
.card > header h2 { margin: 0; font-size: 14px; font-weight: 650; letter-spacing: -.01em; }
.card .body { padding: 16px; }
.card .body.tight { padding: 0; }

.grid { display: grid; gap: 14px; }
.grid.cols-2 { grid-template-columns: repeat(auto-fit, minmax(320px, 1fr)); }
.grid.cols-3 { grid-template-columns: repeat(auto-fit, minmax(240px, 1fr)); }
.grid.cols-4 { grid-template-columns: repeat(auto-fit, minmax(190px, 1fr)); }

/* -------------------------------------------------------------- stat --- */
.stat { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius);
  padding: 14px 16px; box-shadow: var(--shadow); }
.stat .label { font-size: 12px; color: var(--muted); text-transform: uppercase;
  letter-spacing: .05em; font-weight: 600; }
.stat .value { font-size: 25px; font-weight: 680; letter-spacing: -.025em; margin-top: 4px;
  font-variant-numeric: tabular-nums; }
.stat .sub { font-size: 12.5px; color: var(--muted); margin-top: 3px; }
.stat.accent .value { color: var(--accent); }
.stat.ok .value { color: var(--ok); }
.stat.warn .value { color: var(--warn); }
.stat.danger .value { color: var(--danger); }

/* ------------------------------------------------------------ tables --- */
.table-wrap { overflow-x: auto; }
table { width: 100%; border-collapse: collapse; font-size: 13.5px; }
th, td { padding: 9px 12px; text-align: left; border-bottom: 1px solid var(--border);
  vertical-align: middle; }
th { font-size: 11.5px; text-transform: uppercase; letter-spacing: .05em; color: var(--muted);
  font-weight: 650; background: var(--surface-2); position: sticky; top: 0; white-space: nowrap; }
tbody tr:hover { background: var(--surface-2); }
tbody tr:last-child td { border-bottom: 0; }
td.num, th.num { text-align: right; font-variant-numeric: tabular-nums; white-space: nowrap; }
tfoot td { font-weight: 650; background: var(--surface-2); border-top: 2px solid var(--border); }
.nowrap { white-space: nowrap; }
.muted { color: var(--muted); }
.small { font-size: 12.5px; }

/* ------------------------------------------------------------ badges --- */
.badge {
  display: inline-block; padding: 2px 8px; border-radius: 999px; font-size: 11.5px;
  font-weight: 600; white-space: nowrap; background: var(--surface-2); color: var(--muted);
  border: 1px solid var(--border);
}
.badge-paid, .badge-ok { background: var(--ok-soft); color: var(--ok); border-color: transparent; }
.badge-pending, .badge-warn { background: var(--warn-soft); color: var(--warn); border-color: transparent; }
.badge-late, .badge-danger { background: var(--danger-soft); color: var(--danger); border-color: transparent; }
.badge-info { background: var(--accent-soft); color: var(--accent); border-color: transparent; }

/* ------------------------------------------------------------ forms --- */
label { display: block; font-size: 12.5px; font-weight: 600; color: var(--muted); margin-bottom: 5px; }
input, select, textarea {
  width: 100%; padding: 8px 10px; border: 1px solid var(--border); border-radius: 8px;
  background: var(--surface); color: var(--text); font: inherit; font-size: 14px;
}
input:focus, select:focus, textarea:focus {
  outline: 2px solid var(--accent-soft); border-color: var(--accent); outline-offset: 0;
}
input[type=checkbox] { width: auto; }
.field { margin-bottom: 13px; }
.field-row { display: grid; gap: 12px; grid-template-columns: repeat(auto-fit, minmax(170px, 1fr)); }
.check { display: flex; align-items: center; gap: 8px; font-size: 14px; font-weight: 500; color: var(--text); }
.hint { font-size: 12px; color: var(--muted); margin-top: 4px; }

.btn {
  display: inline-flex; align-items: center; gap: 6px; padding: 8px 13px; border-radius: 8px;
  border: 1px solid var(--border); background: var(--surface); color: var(--text);
  font: inherit; font-size: 13.5px; font-weight: 600; cursor: pointer; text-decoration: none;
  white-space: nowrap;
}
.btn:hover { background: var(--surface-2); text-decoration: none; }
.btn-primary { background: var(--accent); border-color: var(--accent); color: #fff; }
.btn-primary:hover { background: var(--accent); filter: brightness(1.08); }
.btn-ok { background: var(--ok); border-color: var(--ok); color: #fff; }
.btn-ok:hover { filter: brightness(1.08); background: var(--ok); }
.btn-danger { color: var(--danger); border-color: var(--danger); background: transparent; }
.btn-danger:hover { background: var(--danger-soft); }
.btn-sm { padding: 4px 9px; font-size: 12.5px; }
.btn-group { display: flex; gap: 8px; flex-wrap: wrap; align-items: center; }

.filters { display: flex; gap: 10px; flex-wrap: wrap; align-items: flex-end; }
.filters .field { margin: 0; min-width: 150px; }

/* ------------------------------------------------------------ alerts --- */
.alert { padding: 11px 14px; border-radius: 8px; margin-bottom: 16px; font-size: 14px;
  border: 1px solid transparent; }
.alert-success { background: var(--ok-soft); color: var(--ok); }
.alert-error { background: var(--danger-soft); color: var(--danger); }
.alert-warning { background: var(--warn-soft); color: var(--warn); }
.alert-info { background: var(--accent-soft); color: var(--accent); }

/* ------------------------------------------------------------- misc --- */
.bar { height: 7px; border-radius: 4px; background: var(--surface-2); overflow: hidden;
  display: flex; min-width: 90px; }
.bar span { display: block; height: 100%; }
.bar .sold { background: var(--ok); }
.bar .depot { background: var(--accent); }
.bar .stock { background: var(--border); }
.legend { display: flex; gap: 12px; flex-wrap: wrap; font-size: 12px; color: var(--muted); margin-top: 7px; }
.legend i { width: 9px; height: 9px; border-radius: 2px; display: inline-block; margin-right: 4px; }

.sparkline { display: flex; align-items: flex-end; gap: 3px; height: 42px; }
.sparkline div { flex: 1; background: var(--accent); border-radius: 2px 2px 0 0; min-height: 2px; opacity: .85; }

.empty { text-align: center; padding: 34px 16px; color: var(--muted); font-size: 14px; }
.dot { display: inline-block; width: 9px; height: 9px; border-radius: 50%; margin-right: 7px;
  vertical-align: baseline; }

/* ---------------------------------------------------------- graphiques --- */
/* Palette catégorielle en ordre fixe : un canal garde toujours sa couleur.
   Validée pour la vision des couleurs en mode clair comme en mode sombre. */
:root {
  --s1: #2a78d6;  /* librairie  */
  --s2: #eb6834;  /* direct     */
  --s3: #1baf7a;  /* évènement  */
  --s4: #eda100;  /* boutique   */
  --s5: #e87ba4;  /* marketplace*/
  --s6: #8a8a85;  /* autre      */
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --s1: #3987e5; --s2: #d95926; --s3: #199e70;
    --s4: #c98500; --s5: #d55181; --s6: #9a9a95;
  }
}

.chart { margin-top: 4px; }

/* Colonnes empilées */
.chart-columns { display: flex; align-items: flex-end; gap: 3px; height: 200px; }
.chart-columns .col { flex: 1; display: flex; flex-direction: column-reverse;
  justify-content: flex-start; height: 100%; min-width: 6px; position: relative; cursor: default; }
.chart-columns .col .seg { width: 100%; border-radius: 0; }
.chart-columns .col .seg + .seg { margin-bottom: 2px; }       /* respiration entre segments */
.chart-columns .col .seg:last-child { border-radius: 4px 4px 0 0; }
.chart-columns .col:hover .seg { filter: brightness(1.15); }
.chart-columns .col .pad { flex: 1 1 auto; }
.chart-axis { display: flex; gap: 3px; margin-top: 7px; font-size: 10.5px; color: var(--muted); }
.chart-axis span { flex: 1; text-align: center; min-width: 6px; overflow: hidden;
  white-space: nowrap; }

/* Barres horizontales */
.chart-bars { display: grid; grid-template-columns: minmax(90px, 1.2fr) 3fr auto;
  gap: 7px 10px; align-items: center; font-size: 13px; }
.chart-bars .name { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.chart-bars .track { background: var(--surface-2); border-radius: 4px; height: 16px;
  position: relative; }
.chart-bars .fill { height: 100%; border-radius: 4px; background: var(--s1); min-width: 3px; }
.chart-bars .val { font-variant-numeric: tabular-nums; font-weight: 600; white-space: nowrap; }

.chart-legend { display: flex; gap: 14px; flex-wrap: wrap; margin-top: 12px;
  font-size: 12.5px; color: var(--muted); }
.chart-legend i { width: 10px; height: 10px; border-radius: 3px; display: inline-block;
  margin-right: 5px; vertical-align: -1px; }

#viz-tip { position: fixed; z-index: 100; pointer-events: none; opacity: 0;
  transition: opacity .08s; background: var(--surface); color: var(--text);
  border: 1px solid var(--border); border-radius: 8px; padding: 8px 11px;
  box-shadow: 0 4px 14px rgba(16,24,40,.16); font-size: 12.5px; max-width: 240px; }
#viz-tip .tip-title { font-weight: 650; margin-bottom: 5px; }
#viz-tip .tip-row { display: flex; justify-content: space-between; gap: 14px; }
#viz-tip .tip-row i { width: 9px; height: 9px; border-radius: 2px; display: inline-block;
  margin-right: 5px; }
#viz-tip .tip-total { border-top: 1px solid var(--border); margin-top: 5px; padding-top: 5px;
  font-weight: 650; }
.mono { font-family: var(--mono); font-size: 12.5px; }
.inline-form { display: inline-flex; gap: 5px; align-items: center; }
.inline-form input[type=number] { width: 70px; padding: 4px 7px; font-size: 13px; }
.page-actions { display: flex; gap: 8px; flex-wrap: wrap; }
details.inline > summary { cursor: pointer; font-size: 13px; color: var(--accent); list-style: none; }
details.inline > summary::-webkit-details-marker { display: none; }
h3 { font-size: 14px; margin: 0 0 10px; }
.kv { display: grid; grid-template-columns: auto 1fr; gap: 5px 16px; font-size: 13.5px; }
.kv dt { color: var(--muted); }
.kv dd { margin: 0; font-weight: 550; }
.tabs { display: flex; gap: 4px; border-bottom: 1px solid var(--border); margin-bottom: 16px; }
.tabs a { padding: 8px 13px; font-size: 13.5px; font-weight: 600; color: var(--muted);
  border-bottom: 2px solid transparent; }
.tabs a.active { color: var(--accent); border-bottom-color: var(--accent); }

.login-wrap { min-height: 100vh; display: grid; place-items: center; padding: 20px; }
.login-card { width: 100%; max-width: 370px; background: var(--surface);
  border: 1px solid var(--border); border-radius: 14px; padding: 28px; box-shadow: var(--shadow); }
.htmx-request { opacity: .55; transition: opacity .1s; }
