:root {
  --bg: #f4f5f2;
  --surface: #ffffff;
  --surface-2: #eceee9;
  --text: #1b1f1c;
  --muted: #636a65;
  --border: #e1e4de;
  --track: #e6e9e3;
  --accent: #2f7d5b;
  --accent-ink: #ffffff;
  --accent-soft: #e1efe7;
  --ok-bar: #3a9a6e;
  --warn: #a86400;
  --warn-bar: #e2a33a;
  --over: #c13b2a;
  --over-bar: #d9503e;
  --over-soft: #fbe8e4;
  --user-a: #2f6db5;
  --user-b: #b5487a;
  --shadow: 0 1px 2px rgb(0 0 0 / 0.05), 0 6px 20px rgb(0 0 0 / 0.06);
  --radius: 16px;
  --nav-h: 64px;
  color-scheme: light;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #121513;
    --surface: #1b1f1c;
    --surface-2: #252a26;
    --text: #e9ece9;
    --muted: #9aa29c;
    --border: #2c312d;
    --track: #2b302c;
    --accent: #5cbf8f;
    --accent-ink: #0d1a13;
    --accent-soft: #1f3529;
    --ok-bar: #4fb383;
    --warn: #e8ab4a;
    --warn-bar: #d99a35;
    --over: #f07c6a;
    --over-bar: #e0604d;
    --over-soft: #3a211d;
    --user-a: #7aa9e8;
    --user-b: #e48ab3;
    --shadow: 0 1px 2px rgb(0 0 0 / 0.4);
    color-scheme: dark;
  }
}

* { box-sizing: border-box; }
[hidden] { display: none !important; }

html { -webkit-text-size-adjust: 100%; }

body {
  margin: 0;
  min-height: 100dvh;
  background: var(--bg);
  color: var(--text);
  font: 16px/1.4 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  font-variant-numeric: tabular-nums;
  -webkit-tap-highlight-color: transparent;
}

button, input, select { font: inherit; color: inherit; }
button { cursor: pointer; }
:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }

.boot, .empty { color: var(--muted); text-align: center; padding: 48px 16px; margin: 0; }

/* ---------- Barre du haut ---------- */

.topbar {
  position: sticky;
  top: 0;
  z-index: 5;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  padding: calc(env(safe-area-inset-top) + 8px) 8px 8px;
  background: color-mix(in srgb, var(--bg) 92%, transparent);
  backdrop-filter: blur(12px);
}
.month-nav { flex: 1; display: flex; align-items: center; justify-content: center; gap: 4px; }
.topbar-spacer { width: 44px; flex: none; }
.account-btn { flex: none; }
.account { background: var(--bg); border-radius: 14px; margin-top: 8px; }
.topbar h1 { font-size: 18px; font-weight: 650; margin: 0; flex: 1; text-align: center; }
.month-btn { background: none; border: 0; padding: 8px 12px; border-radius: 10px; font-size: 18px; font-weight: 650; }
.month-btn small { display: block; font-size: 12px; font-weight: 500; color: var(--accent); }
.icon-btn {
  display: inline-grid;
  place-items: center;
  width: 44px;
  height: 44px;
  border: 0;
  border-radius: 12px;
  background: none;
}
.icon-btn:disabled { opacity: 0.25; cursor: default; }
.icon-btn:not(:disabled):active { background: var(--surface-2); }
.icon { width: 22px; height: 22px; display: inline-block; }
.icon svg { width: 100%; height: 100%; display: block; fill: none; stroke: currentColor; stroke-width: 2; stroke-linecap: round; stroke-linejoin: round; }

.offline {
  margin: 0 16px 8px;
  padding: 8px 12px;
  border-radius: 10px;
  background: var(--over-soft);
  color: var(--over);
  font-size: 14px;
}

/* ---------- Contenu ---------- */

.view {
  max-width: 560px;
  margin: 0 auto;
  padding: 4px 16px calc(var(--nav-h) + env(safe-area-inset-bottom) + 96px);
}

.card {
  background: var(--surface);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
}

.summary { padding: 18px 18px 16px; margin-bottom: 20px; }
.summary .label { color: var(--muted); font-size: 14px; margin: 0; }
.summary .big { font-size: 40px; font-weight: 700; letter-spacing: -0.02em; line-height: 1.1; margin: 2px 0 4px; }
.summary .big.over { color: var(--over); }
.summary .meta { display: flex; justify-content: space-between; gap: 12px; color: var(--muted); font-size: 14px; margin-top: 10px; }

.bar { height: 8px; border-radius: 99px; background: var(--track); overflow: hidden; }
.bar > span { display: block; height: 100%; width: var(--p, 0%); border-radius: inherit; background: var(--ok-bar); transition: width 0.3s ease; }
.bar.warn > span { background: var(--warn-bar); }
.bar.over > span { background: var(--over-bar); }
.summary .bar { height: 10px; }

.group-title {
  font-size: 13px;
  font-weight: 650;
  text-transform: uppercase;
  letter-spacing: 0.06em;
  color: var(--muted);
  margin: 22px 4px 8px;
}

.list { overflow: hidden; }
.list > * + * { border-top: 1px solid var(--border); }

.cat {
  display: grid;
  grid-template-columns: 1fr auto;
  gap: 6px 12px;
  width: 100%;
  padding: 14px 16px;
  border: 0;
  background: none;
  text-align: left;
}
.cat:active, .row:active { background: var(--surface-2); }
.cat .bar { grid-column: 1 / -1; height: 6px; }
.cat-name { font-weight: 560; }
.cat-left { font-weight: 700; text-align: right; }
.cat-sub, .cat-sub-right { font-size: 13px; color: var(--muted); }
.cat-sub-right { text-align: right; }
.warn-text { color: var(--warn); }
.over-text { color: var(--over); }

.banner {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 10px 8px 10px 16px;
  margin-bottom: 16px;
  background: var(--accent-soft);
  border-radius: 14px;
  font-size: 14px;
}
.banner span { flex: 1; }

/* ---------- Historique ---------- */

.day-title { font-size: 14px; font-weight: 650; color: var(--muted); margin: 20px 4px 8px; display: flex; justify-content: space-between; }
.row {
  display: flex;
  align-items: center;
  gap: 12px;
  width: 100%;
  padding: 12px 16px;
  border: 0;
  background: none;
  text-align: left;
}
.row-main { flex: 1; min-width: 0; }
.row-title { display: block; font-weight: 560; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.row-sub { display: block; font-size: 13px; color: var(--muted); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.row-amount { font-weight: 650; white-space: nowrap; }
.row-amount.refund { color: var(--accent); }
.who {
  flex: none;
  display: inline-grid;
  place-items: center;
  width: 30px;
  height: 30px;
  border-radius: 50%;
  font-size: 13px;
  font-weight: 700;
  color: #fff;
  background: var(--user-a);
}
.who.b { background: var(--user-b); }
@media (prefers-color-scheme: dark) { .who { color: #10141a; } }

.stats { color: var(--muted); font-size: 14px; margin: 4px 4px 0; }

/* ---------- Réglages ---------- */

.section-title { font-size: 13px; font-weight: 650; text-transform: uppercase; letter-spacing: 0.06em; color: var(--muted); margin: 24px 4px 8px; }
.note { color: var(--muted); font-size: 13px; margin: 8px 4px 0; }
.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  min-height: 48px;
  padding: 0 20px;
  border: 0;
  border-radius: 14px;
  font-weight: 650;
  background: var(--surface-2);
}
.btn.primary { background: var(--accent); color: var(--accent-ink); }
.btn.primary:disabled { opacity: 0.45; cursor: default; }
.btn.danger { background: none; color: var(--over); }
.btn.ghost { background: none; color: var(--accent); }
.btn.block { width: 100%; }
.btn.small { min-height: 36px; padding: 0 14px; font-size: 14px; }
.stack { display: grid; gap: 10px; margin-top: 12px; }

/* ---------- Navigation ---------- */

.nav {
  position: fixed;
  left: 0;
  right: 0;
  bottom: 0;
  z-index: 5;
  display: flex;
  justify-content: space-around;
  height: calc(var(--nav-h) + env(safe-area-inset-bottom));
  padding-bottom: env(safe-area-inset-bottom);
  background: var(--surface);
  border-top: 1px solid var(--border);
}
.nav button {
  flex: 1;
  display: grid;
  place-items: center;
  align-content: center;
  gap: 2px;
  border: 0;
  background: none;
  color: var(--muted);
  font-size: 12px;
  font-weight: 600;
}
.nav button[aria-current="page"] { color: var(--accent); }
.nav-icon { position: relative; display: inline-grid; }
.nav-badge {
  position: absolute;
  top: -6px;
  right: -12px;
  min-width: 18px;
  height: 18px;
  padding: 0 5px;
  border-radius: 9px;
  background: var(--over);
  color: #fff;
  font-size: 11px;
  font-weight: 700;
  line-height: 18px;
  text-align: center;
}

/* ---------- Courses ---------- */

.shop-add { display: flex; gap: 8px; margin: 4px 0 16px; }
.shop-add .field { flex: 1; min-width: 0; background: var(--surface); }
.shop-add .btn { width: 52px; padding: 0; flex: none; }
.row.shop-item { padding: 0; gap: 0; }
.shop-toggle {
  flex: 1;
  min-width: 0;
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 12px 4px 12px 16px;
  border: 0;
  background: none;
  text-align: left;
}
.shop-toggle:active { background: var(--surface-2); }
.shop-box {
  flex: none;
  display: grid;
  place-items: center;
  width: 26px;
  height: 26px;
  border: 2px solid var(--border);
  border-radius: 8px;
  color: var(--accent-ink);
}
.shop-item.checked .shop-box { background: var(--accent); border-color: var(--accent); }
.shop-item.checked .row-title { color: var(--muted); text-decoration: line-through; }
.shop-item .who { width: 24px; height: 24px; font-size: 11px; }
.shop-delete { flex: none; margin-right: 4px; color: var(--muted); }
.shop-done-title { display: flex; justify-content: space-between; align-items: center; }
.shop-done-title .link { text-transform: none; letter-spacing: 0; }

/* ---------- Factures ---------- */

.row.bill { padding: 0; gap: 0; }
.bill-open {
  flex: 1;
  min-width: 0;
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 12px 8px 12px 16px;
  border: 0;
  background: none;
  text-align: left;
}
.bill-open:active { background: var(--surface-2); }
.pay-btn {
  flex: none;
  display: grid;
  place-items: center;
  width: 44px;
  height: 44px;
  margin-right: 8px;
  border: 2px solid var(--border);
  border-radius: 50%;
  background: none;
  color: var(--muted);
}
.pay-btn:active { background: var(--accent); border-color: var(--accent); color: var(--accent-ink); }
.row.paid .row-title, .row.paid .row-amount { color: var(--muted); text-decoration: line-through; }
.btn.primary-soft { background: var(--accent-soft); color: var(--accent); }

.fab {
  position: fixed;
  right: max(16px, calc(50% - 264px));
  bottom: calc(var(--nav-h) + env(safe-area-inset-bottom) + 16px);
  z-index: 6;
  display: inline-flex;
  align-items: center;
  gap: 6px;
  height: 56px;
  padding: 0 22px 0 18px;
  border: 0;
  border-radius: 18px;
  background: var(--accent);
  color: var(--accent-ink);
  font-weight: 700;
  box-shadow: 0 6px 20px rgb(0 0 0 / 0.2);
}

/* ---------- Fenêtre de saisie ---------- */

.sheet {
  width: 100%;
  max-width: 560px;
  max-height: 92dvh;
  margin: auto auto 0;
  padding: 0;
  border: 0;
  border-radius: 22px 22px 0 0;
  background: var(--surface);
  color: var(--text);
}
.sheet::backdrop { background: rgb(0 0 0 / 0.4); }
body:has(.sheet[open]) { overflow: hidden; }
.sheet[open] { animation: rise 0.18s ease-out; }
@keyframes rise { from { transform: translateY(24px); opacity: 0.6; } }
@media (prefers-reduced-motion: reduce) { .sheet[open] { animation: none; } .bar > span { transition: none; } }

.sheet-body { padding: 8px 16px calc(16px + env(safe-area-inset-bottom)); }
.sheet-head { display: flex; align-items: center; gap: 8px; padding: 8px 0 4px; }
.sheet-head h2 { flex: 1; margin: 0; font-size: 18px; font-weight: 650; }
.sheet-head .icon-btn { margin-right: -10px; }
.hint { color: var(--muted); font-size: 13px; margin: 0 0 4px; }
.link { border: 0; background: none; padding: 0; color: var(--accent); font-weight: 600; font-size: 14px; }

.amount {
  display: flex;
  align-items: baseline;
  justify-content: center;
  gap: 8px;
  padding: 12px 0 4px;
}
.amount input {
  width: 6.5ch;
  min-width: 0;
  border: 0;
  border-bottom: 2px solid var(--border);
  background: none;
  font-size: 44px;
  font-weight: 700;
  text-align: center;
  padding: 0 0 2px;
}
.amount input:focus { outline: none; border-color: var(--accent); }
.amount span { font-size: 18px; color: var(--muted); font-weight: 600; }

.status { text-align: center; font-size: 15px; color: var(--muted); min-height: 22px; margin: 4px 0 12px; }
.status strong { color: var(--text); }
.status.over strong { color: var(--over); }
.status.warn strong { color: var(--warn); }

.chips { display: flex; flex-wrap: wrap; gap: 8px; margin: 8px 0 12px; }
.chip {
  padding: 8px 12px;
  border: 1px solid var(--border);
  border-radius: 999px;
  background: none;
  font-size: 14px;
}
.chip[aria-pressed="true"] { background: var(--accent); border-color: var(--accent); color: var(--accent-ink); font-weight: 600; }
.chip-group { width: 100%; font-size: 12px; font-weight: 650; text-transform: uppercase; letter-spacing: 0.06em; color: var(--muted); margin-top: 4px; }

.fields { display: grid; grid-template-columns: 1fr auto; gap: 8px; margin-bottom: 12px; }
.field {
  width: 100%;
  min-height: 48px;
  padding: 10px 12px;
  border: 1px solid var(--border);
  border-radius: 12px;
  background: var(--bg);
}
.field:focus { outline: 2px solid var(--accent); outline-offset: -1px; }
label.lbl { display: block; font-size: 13px; color: var(--muted); font-weight: 600; margin: 12px 2px 6px; }

.toggle { display: flex; align-items: center; gap: 10px; margin: 0 2px 14px; font-size: 15px; }
.later { margin: -6px 0 14px; }
.later .lbl { margin-top: 0; }
.toggle input { width: 20px; height: 20px; accent-color: var(--accent); }

.actions { display: flex; gap: 8px; }
.actions .primary { flex: 1; }

.error { color: var(--over); font-size: 14px; margin: 8px 2px 0; min-height: 0; }
.error:empty { display: none; }

.recent { margin-top: 18px; }
.recent .list { background: var(--bg); border-radius: 14px; }

/* ---------- Connexion ---------- */

.login {
  max-width: 380px;
  margin: 0 auto;
  padding: calc(env(safe-area-inset-top) + 15vh) 16px 32px;
}
.login img { width: 64px; height: 64px; border-radius: 16px; display: block; margin-bottom: 16px; }
.login h1 { margin: 0 0 4px; font-size: 26px; }
.login p { margin: 0 0 20px; color: var(--muted); }
.login .btn { margin-top: 20px; }

/* ---------- Notification ---------- */

.toast {
  position: fixed;
  left: 16px;
  right: 16px;
  bottom: calc(var(--nav-h) + env(safe-area-inset-bottom) + 84px);
  z-index: 20;
  max-width: 528px;
  margin: 0 auto;
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 12px 8px 12px 16px;
  border-radius: 14px;
  background: #1f2621;
  color: #f1f4f1;
  font-size: 15px;
  box-shadow: 0 8px 24px rgb(0 0 0 / 0.25);
}
.toast[hidden] { display: none; }
.toast span { flex: 1; }
.toast button { border: 0; background: none; color: #8fe0b6; font-weight: 700; padding: 8px 10px; border-radius: 8px; }
@media (prefers-color-scheme: dark) { .toast { background: #e6ebe7; color: #18201b; } .toast button { color: #1f7a50; } }
