:root {
  --bg: #f4f6f5;
  --surface: #ffffff;
  --text: #17201d;
  --muted: #66736e;
  --line: #e2e7e5;
  --accent: #0f766e;
  --accent-text: #ffffff;
  --pos: #15803d;
  --neg: #c2410c;
  --danger: #b91c1c;
  --shadow: 0 1px 3px rgba(0, 0, 0, .08);
  color-scheme: light dark;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0e1412;
    --surface: #18211e;
    --text: #e8eeeb;
    --muted: #93a19b;
    --line: #26322e;
    --accent: #2dd4bf;
    --accent-text: #04201d;
    --pos: #4ade80;
    --neg: #fb923c;
    --danger: #f87171;
    --shadow: none;
  }
}

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

.pos { color: var(--pos); }
.neg { color: var(--neg); }
.hint { color: var(--muted); font-size: .85rem; margin: 0; }
.pad { padding: 0 16px 8px; }
.err { color: var(--danger); min-height: 1em; margin: 0; font-size: .9rem; }
.warn { color: var(--danger); }

/* ---------- Composants ---------- */
.card {
  background: var(--surface);
  border-radius: 16px;
  padding: 16px;
  margin: 12px 16px;
  box-shadow: var(--shadow);
}
.form { display: flex; flex-direction: column; gap: 12px; }
.form h2, .form h3 { margin: 4px 0 0; font-size: 1.05rem; }
label { display: flex; flex-direction: column; gap: 4px; font-size: .85rem; color: var(--muted); }
input {
  background: var(--bg);
  border: 1px solid var(--line);
  border-radius: 10px;
  padding: 12px;
  font-size: 1rem;
  width: 100%;
  min-height: 46px;
}
input:focus { outline: 2px solid var(--accent); outline-offset: -1px; }
input.big { font-size: 1.6rem; font-weight: 600; }

.btn {
  display: inline-flex; align-items: center; justify-content: center;
  border: 1px solid var(--line);
  background: var(--surface);
  border-radius: 12px;
  padding: 12px 16px;
  min-height: 46px;
  font-weight: 600;
  text-decoration: none;
  cursor: pointer;
}
.btn.primary { background: var(--accent); color: var(--accent-text); border-color: var(--accent); }
.btn.danger { color: var(--danger); }
.btn.wide { display: flex; margin: 16px; width: calc(100% - 32px); }
.row { display: flex; gap: 8px; }
.row .btn { flex: 1; padding: 12px 8px; }

/* ---------- Connexion / installation ---------- */
.screen { min-height: 100vh; min-height: 100dvh; }
#login { display: flex; align-items: center; justify-content: center; }
.login { width: 100%; max-width: 380px; text-align: center; }
.login img { margin: 0 auto; border-radius: 18px; }
.login h1 { margin: 0; }
.login label { text-align: left; }
.install { max-width: 520px; margin: 0 auto; padding: 16px 0 40px; }
.install h1 { padding: 0 16px; }

/* ---------- App ---------- */
#app { padding-bottom: calc(80px + env(safe-area-inset-bottom)); max-width: 640px; margin: 0 auto; }
.top {
  background: var(--accent);
  color: var(--accent-text);
  padding: calc(18px + env(safe-area-inset-top)) 16px 20px;
}
.balance span { font-size: .85rem; opacity: .85; display: block; }
.balance strong { font-size: 2.2rem; font-weight: 700; letter-spacing: -.02em; }
.top .neg, .top .pos { color: inherit; }
.top .neg::after { content: " ⚠"; font-size: 1.2rem; }

.monthnav { display: flex; align-items: center; justify-content: space-between; padding: 12px 8px 0; }
.monthnav h2 { margin: 0; font-size: 1.15rem; text-transform: capitalize; }
.icon { background: none; border: 0; font-size: 2rem; width: 48px; height: 48px; cursor: pointer; color: var(--muted); }

.stats { display: grid; grid-template-columns: repeat(3, 1fr); gap: 8px; padding: 12px 16px; }
.stat { background: var(--surface); border-radius: 14px; padding: 12px 10px; box-shadow: var(--shadow); min-width: 0; }
.stat span { display: block; font-size: .75rem; color: var(--muted); }
.stat strong { font-size: 1.02rem; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; display: block; }

.title { margin: 0; padding: 18px 16px 8px; font-size: 1.25rem; }
.list { padding: 0 16px; }
.day { margin: 16px 4px 6px; font-size: .8rem; color: var(--muted); font-weight: 600; text-transform: capitalize; }
.item {
  display: flex; align-items: center; justify-content: space-between; gap: 12px;
  width: 100%;
  background: var(--surface);
  border: 0;
  border-radius: 12px;
  padding: 14px;
  margin-bottom: 6px;
  text-align: left;
  box-shadow: var(--shadow);
  cursor: pointer;
}
.item .lbl { min-width: 0; overflow: hidden; text-overflow: ellipsis; }
.item .lbl small { display: block; color: var(--muted); font-size: .78rem; margin-top: 2px; }
.item .amt { font-weight: 700; white-space: nowrap; font-variant-numeric: tabular-nums; }
.hist .lbl { text-transform: capitalize; }
.empty { text-align: center; color: var(--muted); padding: 40px 0; }

.fab {
  position: fixed;
  right: max(20px, calc(50vw - 300px));
  bottom: calc(84px + env(safe-area-inset-bottom));
  width: 60px; height: 60px;
  border-radius: 50%;
  border: 0;
  background: var(--accent);
  color: var(--accent-text);
  font-size: 2rem;
  box-shadow: 0 4px 14px rgba(0, 0, 0, .25);
  cursor: pointer;
}

.tabs {
  position: fixed; left: 0; right: 0; bottom: 0;
  display: grid; grid-template-columns: repeat(4, 1fr);
  background: var(--surface);
  border-top: 1px solid var(--line);
  padding-bottom: env(safe-area-inset-bottom);
}
.tabs button {
  background: none; border: 0;
  padding: 8px 0 10px;
  font-size: .72rem;
  color: var(--muted);
  display: flex; flex-direction: column; align-items: center; gap: 2px;
  cursor: pointer;
}
.tabs button span { font-size: 1.25rem; filter: grayscale(1); opacity: .7; }
.tabs button.active { color: var(--accent); font-weight: 700; }
.tabs button.active span { filter: none; opacity: 1; }

/* ---------- Fenêtres de saisie ---------- */
dialog {
  border: 0;
  border-radius: 20px 20px 0 0;
  background: var(--surface);
  color: var(--text);
  width: 100%;
  max-width: 640px;
  margin: auto auto 0;
  padding: 20px 16px calc(20px + env(safe-area-inset-bottom));
}
dialog::backdrop { background: rgba(0, 0, 0, .45); }

.seg { display: grid; grid-template-columns: 1fr 1fr; background: var(--bg); border-radius: 12px; padding: 4px; }
.seg label { color: var(--text); font-size: .95rem; }
.seg input { position: absolute; opacity: 0; pointer-events: none; width: auto; min-height: 0; }
.seg span { text-align: center; padding: 10px; border-radius: 9px; cursor: pointer; font-weight: 600; }
.seg input:checked + span { background: var(--surface); box-shadow: var(--shadow); }
.seg input[value="depense"]:checked + span { color: var(--neg); }
.seg input[value="revenu"]:checked + span { color: var(--pos); }

.toast {
  position: fixed; left: 50%; transform: translateX(-50%);
  bottom: calc(150px + env(safe-area-inset-bottom));
  background: var(--text); color: var(--bg);
  padding: 10px 18px; border-radius: 999px;
  font-size: .9rem;
  z-index: 10;
}
