:root {
  --bg: #f6f7f6;
  --surface: #ffffff;
  --text: #17201e;
  --muted: #5f6b68;
  --border: #e1e5e3;
  --accent: #0f766e;
  --accent-text: #ffffff;
  --accent-soft: #e3f2f0;
  --warn: #b45309;
  --warn-soft: #fdf0dc;
  --danger: #b42318;
  --friend: #6d28d9;
  --friend-soft: #efe9fb;
  --radius: 14px;
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0f1413;
    --surface: #182020;
    --text: #e8eeec;
    --muted: #93a19d;
    --border: #2a3533;
    --accent: #2dd4bf;
    --accent-text: #062a26;
    --accent-soft: #123532;
    --warn: #f5a524;
    --warn-soft: #3a2a10;
    --danger: #f97066;
    --friend: #b79cf6;
    --friend-soft: #2a2140;
    color-scheme: dark;
  }
}

* { box-sizing: border-box; }
html, body { margin: 0; }
body {
  font: 16px/1.45 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  background: var(--bg);
  color: var(--text);
  padding-bottom: env(safe-area-inset-bottom);
}
[hidden] { display: none !important; }

.topbar {
  display: flex; align-items: center; justify-content: space-between;
  padding: 14px 16px; padding-top: max(14px, env(safe-area-inset-top));
}
.topbar h1 { font-size: 1.15rem; margin: 0; letter-spacing: -0.01em; }

main, .screen { max-width: 720px; margin: 0 auto; padding: 0 16px 32px; }

.tabs {
  display: flex; gap: 4px; overflow-x: auto; scrollbar-width: none;
  position: sticky; top: 0; z-index: 2; background: var(--bg);
  padding: 4px 0 12px; margin-bottom: 4px;
}
.tabs button {
  flex: none; background: transparent; color: var(--muted); border: 1px solid transparent;
  padding: 8px 12px; border-radius: 999px; font-weight: 600;
}
.tabs button.active { background: var(--surface); color: var(--text); border-color: var(--border); }

.card {
  background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius);
  padding: 16px; margin-bottom: 12px;
}
.card.narrow { max-width: 420px; margin: 40px auto; }
.card h2, .card h3 { margin: 0 0 8px; }
.card h3 { font-size: 1rem; }

.grid { display: grid; gap: 12px; grid-template-columns: 1fr 1fr; margin-bottom: 12px; }
.kpi {
  background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius);
  padding: 14px 16px; display: flex; flex-direction: column; gap: 2px; min-width: 0;
}
.kpi span { color: var(--muted); font-size: .85rem; }
.kpi strong { font-size: 1.35rem; font-variant-numeric: tabular-nums; }
.kpi small { color: var(--muted); font-size: .75rem; }
.kpi.big { grid-column: 1 / -1; background: var(--accent-soft); border-color: transparent; }
.kpi.big strong { font-size: 2.1rem; color: var(--accent); }

.alert {
  background: var(--warn-soft); color: var(--warn); border-radius: var(--radius);
  padding: 12px 14px; margin-bottom: 12px; font-weight: 600; cursor: pointer;
}

table.totals { width: 100%; border-collapse: collapse; font-variant-numeric: tabular-nums; }
table.totals th, table.totals td { padding: 6px 4px; text-align: right; border-bottom: 1px solid var(--border); }
table.totals th:first-child, table.totals td:first-child { text-align: left; color: var(--muted); }
table.totals tr:last-child td { font-weight: 700; border-bottom: 0; }

.muted { color: var(--muted); }
.ok { color: var(--accent); font-weight: 600; }
.warn-text { color: var(--warn); }
.pos { color: var(--friend); }
.neg { color: var(--accent); }

button {
  font: inherit; font-weight: 600; border: 0; border-radius: 10px; cursor: pointer;
  padding: 10px 14px; background: var(--accent); color: var(--accent-text);
}
button.ghost { background: transparent; color: var(--text); border: 1px solid var(--border); }
button.danger { color: var(--danger); }
button.small { padding: 6px 10px; font-size: .85rem; }
button:active { transform: scale(.98); }

input, select {
  font: inherit; width: 100%; padding: 10px 12px; border-radius: 10px;
  border: 1px solid var(--border); background: var(--bg); color: var(--text);
}
input.inline { width: 7em; display: inline-block; padding: 4px 8px; }
input.search { margin-bottom: 12px; }

form.form label, #edit-form label { display: block; margin-bottom: 12px; font-size: .9rem; color: var(--muted); }
form.form label input, form.form label select,
#edit-form label input, #edit-form label select { margin-top: 4px; color: var(--text); }
label.radio { display: flex !important; align-items: center; gap: 8px; color: var(--text) !important; margin-bottom: 6px !important; }
label.radio input[type=radio], label.radio input[type=checkbox] { width: auto; margin: 0; }
fieldset { border: 1px solid var(--border); border-radius: 10px; margin: 0 0 12px; padding: 8px 12px 6px; }
legend { font-size: .85rem; color: var(--muted); padding: 0 4px; }
#login-form { display: grid; gap: 8px; }
.row { display: flex; gap: 8px; }
.row input { flex: 1; }

.item-head { display: flex; align-items: center; gap: 10px; }
.kind { font-size: 1.3rem; width: 1.6em; text-align: center; flex: none; }
.grow { flex: 1; min-width: 0; overflow-wrap: anywhere; }
.actions { display: flex; flex-wrap: wrap; gap: 8px; margin-top: 12px; }
.actions button { flex: 1 1 auto; }

.list { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); overflow: hidden; }
.line { display: flex; gap: 10px; align-items: center; padding: 12px 14px; border-bottom: 1px solid var(--border); cursor: pointer; }
.line:last-child { border-bottom: 0; }
.line.cancelled { opacity: .5; }
.line .right { text-align: right; font-variant-numeric: tabular-nums; flex: none; }

.tag { font-size: .72rem; padding: 2px 7px; border-radius: 999px; background: var(--bg); border: 1px solid var(--border); white-space: nowrap; font-weight: 600; }
.tag.me { background: var(--accent-soft); color: var(--accent); border-color: transparent; }
.tag.friend, .tag.split { background: var(--friend-soft); color: var(--friend); border-color: transparent; }
.tag.warn { background: var(--warn-soft); color: var(--warn); border-color: transparent; }

.badge { background: var(--accent); color: var(--accent-text); font-size: .72rem; padding: 1px 7px; border-radius: 999px; margin-left: 4px; }
.badge.warn { background: var(--warn); color: #fff; }

.empty { text-align: center; padding: 48px 16px; color: var(--muted); }
details { margin-top: 8px; }
pre { white-space: pre-wrap; font-size: .8rem; max-height: 240px; overflow: auto; background: var(--bg); padding: 8px; border-radius: 8px; }

dialog {
  border: 1px solid var(--border); border-radius: var(--radius); background: var(--surface); color: var(--text);
  width: min(480px, calc(100vw - 32px)); padding: 18px;
}
dialog::backdrop { background: rgba(0, 0, 0, .45); }

.toast {
  position: fixed; left: 50%; bottom: max(20px, env(safe-area-inset-bottom)); transform: translateX(-50%);
  background: var(--text); color: var(--bg); padding: 10px 16px; border-radius: 999px; font-weight: 600; z-index: 10;
  max-width: calc(100vw - 32px);
}
.toast.error { background: var(--danger); color: #fff; }

@media (max-width: 420px) {
  .grid { grid-template-columns: 1fr; }
  .kpi.big strong { font-size: 1.8rem; }
}

/* --- Cycle de vie des ventes --- */
.kpi.wide { grid-column: 1 / -1; }
.tag.st-sold { background: var(--warn-soft); color: var(--warn); border-color: transparent; }
.tag.st-shipped { background: var(--friend-soft); color: var(--friend); border-color: transparent; }
.tag.st-done { background: var(--accent-soft); color: var(--accent); border-color: transparent; }
.tag.st-cancel { text-decoration: line-through; }
.small { font-size: .82rem; }
ul.events { margin: 4px 0 12px; padding-left: 18px; font-size: .85rem; color: var(--muted); }

/* --- Synchro à la demande --- */
.top-actions { display: flex; gap: 8px; }
#sync.spinning { animation: spin 1s linear infinite; }
@keyframes spin { to { transform: rotate(360deg); } }
