:root {
  --bg: #f4f6f5;
  --card: #ffffff;
  --text: #1d2624;
  --muted: #64726f;
  --border: #e1e6e4;
  --primary: #2f7d6d;
  --primary-soft: #e3f1ed;
  --on-primary: #ffffff;
  --income: #23875a;
  --expense: #c2453d;
  --warn: #c98a12;
  --warn-soft: #fbf1dc;
  --danger-soft: #fbe5e3;
  --shadow: 0 1px 3px rgba(0, 0, 0, .08);
  --radius: 16px;
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #111615;
    --card: #1b2220;
    --text: #e8eeec;
    --muted: #9aa8a4;
    --border: #2c3532;
    --primary: #4fb39c;
    --primary-soft: #1d3530;
    --on-primary: #0d1513;
    --income: #4cc38a;
    --expense: #ef7a72;
    --warn: #e8b04a;
    --warn-soft: #3a3020;
    --danger-soft: #3d2322;
    --shadow: none;
    color-scheme: dark;
  }
}

* { box-sizing: border-box; }
html, body { margin: 0; }
body {
  background: var(--bg);
  color: var(--text);
  font: 17px/1.45 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  -webkit-tap-highlight-color: transparent;
}
[hidden] { display: none !important; }
button, input, select { font: inherit; color: inherit; }
h1, h2, h3 { margin: 0; }

/* ---------- Connexion ---------- */
.login { min-height: 100vh; display: grid; place-items: center; padding: 16px; }
.login-card {
  width: 100%; max-width: 360px; background: var(--card); border-radius: var(--radius);
  padding: 32px 24px; box-shadow: var(--shadow); text-align: center;
}
.login-logo { width: 72px; height: 72px; }
.login-card h1 { margin: 12px 0 24px; }
.login-card form { text-align: left; }

/* ---------- Barre du haut ---------- */
.topbar {
  position: sticky; top: 0; z-index: 5;
  display: flex; align-items: center; justify-content: space-between;
  padding: calc(10px + env(safe-area-inset-top)) 12px 10px;
  background: var(--primary); color: var(--on-primary);
}
.topbar h1 { font-size: 20px; text-transform: capitalize; }
.icon-btn {
  width: 48px; height: 48px; border: 0; border-radius: 50%;
  background: rgba(255, 255, 255, .15); color: inherit; font-size: 28px; line-height: 1; cursor: pointer;
}

main { max-width: 640px; margin: 0 auto; padding: 16px 16px calc(150px + env(safe-area-inset-bottom)); }

/* ---------- Onglets du bas ---------- */
.tabbar {
  position: fixed; bottom: 0; left: 0; right: 0; z-index: 5;
  display: grid; grid-template-columns: repeat(4, 1fr);
  background: var(--card); border-top: 1px solid var(--border);
  padding-bottom: env(safe-area-inset-bottom);
}
.tabbar button {
  border: 0; background: none; padding: 8px 0 10px; font-size: 13px; color: var(--muted);
  display: flex; flex-direction: column; align-items: center; gap: 2px; cursor: pointer;
}
.tabbar button span { font-size: 24px; filter: grayscale(1); opacity: .7; }
.tabbar button.active { color: var(--primary); font-weight: 600; }
.tabbar button.active span { filter: none; opacity: 1; }

.fab {
  position: fixed; right: 20px; bottom: calc(84px + env(safe-area-inset-bottom)); z-index: 6;
  width: 64px; height: 64px; border-radius: 50%; border: 0;
  background: var(--primary); color: var(--on-primary); font-size: 36px; line-height: 1;
  box-shadow: 0 4px 12px rgba(0, 0, 0, .25); cursor: pointer;
}

/* ---------- Cartes ---------- */
.card {
  background: var(--card); border-radius: var(--radius); padding: 16px;
  box-shadow: var(--shadow); margin-bottom: 16px;
}
.card h2 { font-size: 18px; margin-bottom: 12px; }
.card h2 small { font-weight: 400; color: var(--muted); font-size: 14px; }

.hero { text-align: center; }
.hero .label { color: var(--muted); font-size: 15px; }
.hero .big { font-size: 40px; font-weight: 700; letter-spacing: -.5px; }
.hero .split { display: grid; grid-template-columns: 1fr 1fr; gap: 12px; margin-top: 16px; }
.hero .split div { background: var(--bg); border-radius: 12px; padding: 10px; }
.hero .split strong { display: block; font-size: 20px; }

.pos { color: var(--income); }
.neg { color: var(--expense); }
.muted { color: var(--muted); }

.alert {
  border-radius: 12px; padding: 12px 14px; margin-bottom: 12px; font-size: 15px;
  background: var(--warn-soft); border-left: 4px solid var(--warn);
}
.alert.danger { background: var(--danger-soft); border-left-color: var(--expense); }

/* Barres de progression (plafonds) */
.budget-row { padding: 10px 0; border-bottom: 1px solid var(--border); }
.budget-row:last-child { border-bottom: 0; }
.budget-row .head { display: flex; justify-content: space-between; gap: 8px; }
.budget-row .head span:last-child { white-space: nowrap; }
.bar { height: 10px; border-radius: 5px; background: var(--border); margin-top: 6px; overflow: hidden; }
.bar > i { display: block; height: 100%; border-radius: 5px; background: var(--primary); }
.bar > i.warn { background: var(--warn); }
.bar > i.over { background: var(--expense); }

/* Listes */
.list { list-style: none; margin: 0; padding: 0; }
.list li {
  display: flex; align-items: center; gap: 12px; padding: 12px 0;
  border-bottom: 1px solid var(--border); cursor: pointer;
}
.list li:last-child { border-bottom: 0; }
.list .ico {
  width: 42px; height: 42px; flex: none; display: grid; place-items: center;
  font-size: 22px; background: var(--bg); border-radius: 12px;
}
.list .main { flex: 1; min-width: 0; }
.list .main strong { display: block; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.list .main small { color: var(--muted); }
.list .amount { font-weight: 600; white-space: nowrap; }
.day-title { font-size: 14px; color: var(--muted); margin: 8px 0 0; text-transform: capitalize; }
.badge {
  display: inline-block; font-size: 12px; padding: 1px 8px; border-radius: 10px;
  background: var(--primary-soft); color: var(--primary); margin-left: 6px; vertical-align: middle;
}
.empty { text-align: center; color: var(--muted); padding: 24px 8px; }

/* Statistiques courses */
.stats { display: grid; grid-template-columns: repeat(3, 1fr); gap: 8px; text-align: center; }
.stats div { background: var(--bg); border-radius: 12px; padding: 10px 4px; }
.stats strong { display: block; font-size: 18px; }
.stats small { color: var(--muted); font-size: 13px; }
.compare { text-align: center; font-size: 16px; margin-top: 12px; }
.chart-box { position: relative; height: 220px; }

/* ---------- Boutons ---------- */
.btn {
  border: 0; border-radius: 12px; padding: 14px 18px; font-weight: 600; cursor: pointer;
  background: var(--bg); color: var(--text); min-height: 50px;
}
.btn-primary { background: var(--primary); color: var(--on-primary); }
.btn-danger { background: var(--danger-soft); color: var(--expense); }
.btn-block { display: block; width: 100%; }
.btn-row { display: flex; gap: 10px; margin-top: 20px; }
.btn-row .btn { flex: 1; }

/* ---------- Formulaires ---------- */
label { display: block; font-size: 15px; color: var(--muted); margin: 14px 0 6px; }
input, select {
  width: 100%; padding: 13px 14px; border: 1px solid var(--border); border-radius: 12px;
  background: var(--card); min-height: 50px;
}
input:focus, select:focus { outline: 2px solid var(--primary); outline-offset: 0; border-color: transparent; }
input.amount-input { font-size: 30px; font-weight: 700; text-align: center; }
.error { color: var(--expense); min-height: 1.2em; margin: 8px 0; font-size: 15px; }

.toggle { display: grid; grid-template-columns: 1fr 1fr; gap: 6px; background: var(--bg); padding: 4px; border-radius: 14px; }
.toggle button { border: 0; background: none; padding: 12px; border-radius: 10px; font-weight: 600; cursor: pointer; color: var(--muted); }
.toggle button.on { background: var(--card); color: var(--text); box-shadow: var(--shadow); }

.cat-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(92px, 1fr)); gap: 8px; }
.cat-grid button {
  border: 2px solid transparent; background: var(--bg); border-radius: 12px; padding: 10px 4px;
  font-size: 13px; cursor: pointer; display: flex; flex-direction: column; align-items: center; gap: 4px;
}
.cat-grid button span { font-size: 24px; }
.cat-grid button.on { border-color: var(--primary); background: var(--primary-soft); }

.chips { display: flex; flex-wrap: wrap; gap: 8px; margin-bottom: 8px; }
.chips button {
  border: 1px solid var(--border); background: var(--card); padding: 8px 14px; border-radius: 20px; cursor: pointer;
}
.chips button.on { background: var(--primary); color: var(--on-primary); border-color: var(--primary); }

.check { display: flex; align-items: center; gap: 10px; margin-top: 16px; color: var(--text); }
.check input { width: 24px; min-height: 24px; height: 24px; }

/* ---------- Fenêtre du bas ---------- */
.sheet-backdrop { position: fixed; inset: 0; background: rgba(0, 0, 0, .4); z-index: 10; }
.sheet {
  position: fixed; left: 0; right: 0; bottom: 0; z-index: 11;
  max-height: 92vh; overflow-y: auto; background: var(--card);
  border-radius: 20px 20px 0 0; padding: 20px 16px calc(20px + env(safe-area-inset-bottom));
  max-width: 640px; margin: 0 auto;
  animation: up .2s ease-out;
}
.sheet h2 { font-size: 20px; margin-bottom: 4px; }
@keyframes up { from { transform: translateY(40px); opacity: 0; } }

.toast {
  position: fixed; left: 50%; transform: translateX(-50%); bottom: calc(160px + env(safe-area-inset-bottom));
  background: var(--text); color: var(--bg); padding: 12px 20px; border-radius: 24px; z-index: 20;
  max-width: calc(100% - 32px); text-align: center;
}

.settings-link { display: block; text-decoration: none; color: inherit; }
.help { font-size: 14px; color: var(--muted); margin: 0 0 12px; }
