:root {
  --bg: #111418;
  --surface: #1a1f26;
  --surface-2: #232a33;
  --border: #2c343f;
  --text: #e6e9ee;
  --muted: #8a94a3;
  --accent: #4fa3ff;
  --accent-ink: #0b1a2b;
  --pos: #3ecf8e;
  --neg: #ff7a7a;
  --radius: 10px;
  color-scheme: dark;
}

* { box-sizing: border-box; }
html, body { margin: 0; background: var(--bg); color: var(--text); }
body {
  font: 15px/1.35 -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
  -webkit-tap-highlight-color: transparent;
  padding-bottom: env(safe-area-inset-bottom, 0px);
}
a { color: inherit; text-decoration: none; }
button, input, select { font: inherit; color: inherit; }
[hidden] { display: none !important; }

.topbar {
  position: sticky;
  top: 0;
  z-index: 5;
  background: var(--bg);
  border-bottom: 1px solid var(--border);
  padding-top: env(safe-area-inset-top, 0px);
}
.topbar nav { display: flex; max-width: 560px; margin: 0 auto; }
.topbar a {
  flex: 1;
  text-align: center;
  padding: 12px 4px;
  color: var(--muted);
  font-weight: 600;
  border-bottom: 2px solid transparent;
}
.topbar a.active { color: var(--text); border-bottom-color: var(--accent); }

main { max-width: 560px; margin: 0 auto; padding: 12px 16px 24px; }
h2 { font-size: 13px; text-transform: uppercase; letter-spacing: .04em; color: var(--muted); margin: 20px 0 8px; }
.muted { color: var(--muted); }
.center { text-align: center; }
.small { font-size: 13px; }

/* Boutons */
.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  min-height: 44px;
  padding: 0 16px;
  border-radius: var(--radius);
  border: 1px solid var(--border);
  background: var(--surface-2);
  font-weight: 600;
  cursor: pointer;
}
.btn.primary { background: var(--accent); border-color: var(--accent); color: var(--accent-ink); }
.btn.danger { color: var(--neg); }
.btn.block { width: 100%; }
.btn:disabled { opacity: .45; cursor: default; }
.row-btns { display: flex; gap: 8px; }
.row-btns > * { flex: 1; }

/* Solde */
.balance {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 18px 16px;
  text-align: center;
}
.balance .who { color: var(--muted); }
.balance .amt { font-size: 38px; font-weight: 700; margin: 4px 0 12px; font-variant-numeric: tabular-nums; }
.balance.pos .amt { color: var(--pos); }
.balance.neg .amt { color: var(--neg); }
.home-actions { display: flex; gap: 8px; margin-top: 12px; }
.home-actions .btn.primary { flex: 2; font-size: 17px; min-height: 52px; }
.home-actions .btn:not(.primary) { flex: 1; min-height: 52px; }

/* Listes */
.list { list-style: none; margin: 0; padding: 0; }
.list li + li { border-top: 1px solid var(--border); }
.entry { display: flex; align-items: center; gap: 10px; padding: 10px 2px; }
.entry .emoji { width: 28px; text-align: center; font-size: 20px; flex: none; }
.entry .main { flex: 1; min-width: 0; }
.entry .label, .entry .sub { display: block; }
.entry .label { white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.entry .sub { font-size: 12px; color: var(--muted); }
.entry .amt { text-align: right; font-variant-numeric: tabular-nums; flex: none; }
.entry.transfer .label { color: var(--accent); }

/* Saisie */
.amount-display {
  font-size: 40px;
  font-weight: 700;
  text-align: center;
  padding: 0 0 4px;
  font-variant-numeric: tabular-nums;
}
.amount-display.empty { color: var(--muted); }
.chips { display: flex; flex-wrap: wrap; gap: 6px; }
.chip {
  border: 1px solid var(--border);
  background: var(--surface);
  border-radius: 999px;
  padding: 6px 11px;
  font-size: 14px;
  cursor: pointer;
  min-height: 34px;
}
.chip.on { background: var(--accent); border-color: var(--accent); color: var(--accent-ink); font-weight: 600; }
.section-label { font-size: 12px; color: var(--muted); margin: 12px 0 6px; }
.preview {
  margin-top: 8px;
  padding: 8px 10px;
  border-radius: var(--radius);
  background: var(--surface);
  font-size: 14px;
  font-variant-numeric: tabular-nums;
}
.preview b { font-weight: 600; }
.fields { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; margin-top: 12px; }
.fields .full { grid-column: 1 / -1; }
.field { display: flex; flex-direction: column; gap: 4px; font-size: 12px; color: var(--muted); }
input, select {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 9px 10px;
  min-height: 40px;
  width: 100%;
  font-size: 16px; /* évite le zoom iOS */
}
.seg { display: flex; border: 1px solid var(--border); border-radius: 8px; overflow: hidden; min-height: 40px; }
.seg button { flex: 1; border: 0; background: var(--surface); cursor: pointer; padding: 0 8px; }
.seg button.on { background: var(--surface-2); color: var(--accent); font-weight: 600; }

.keypad { display: grid; grid-template-columns: repeat(4, 1fr); gap: 6px; margin: 4px 0 6px; }
.keypad button {
  height: 46px;
  border-radius: var(--radius);
  border: 0;
  background: var(--surface);
  font-size: 22px;
  cursor: pointer;
}
.keypad button:active { background: var(--surface-2); }

/* Toujours visible : la saisie tient en montant → catégorie → Enregistrer, sans défiler. */
.savebar {
  margin-top: 12px;
  position: sticky;
  bottom: 0;
  z-index: 4;
  background: var(--bg);
  padding: 8px 0 calc(8px + env(safe-area-inset-bottom, 0px));
  margin-inline: -16px;
  padding-inline: 16px;
  border-top: 1px solid var(--border);
}
.savebar .error:empty { display: none; }
.savebar .error { margin: 0 0 6px; }
.savebar .btn { min-height: 50px; font-size: 17px; }

/* Historique */
.month-nav { display: flex; align-items: center; gap: 8px; }
.month-nav .title { flex: 1; text-align: center; font-weight: 600; text-transform: capitalize; }
.month-nav button { width: 44px; }
.totals { display: grid; grid-template-columns: auto 1fr 1fr; gap: 4px 12px; margin-top: 10px; font-size: 13px; font-variant-numeric: tabular-nums; }
.totals .h { color: var(--muted); }
.totals span:not(.h) { text-align: right; }

/* Réglages */
.cat-row { display: grid; grid-template-columns: 52px 1fr; gap: 6px; padding: 8px 0; }
.cat-row + .cat-row { border-top: 1px solid var(--border); }
.cat-row .emoji-in { text-align: center; }
.cat-row .tools { grid-column: 1 / -1; display: flex; gap: 6px; }
.cat-row .tools select { flex: 1; }
.cat-row .tools button { width: 40px; flex: none; }

#toast {
  position: fixed;
  left: 50%;
  transform: translateX(-50%);
  bottom: calc(16px + env(safe-area-inset-bottom, 0px));
  background: var(--surface-2);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 10px 14px;
  display: flex;
  gap: 14px;
  align-items: center;
  box-shadow: 0 6px 24px rgba(0, 0, 0, .4);
  max-width: calc(100% - 32px);
  z-index: 10;
}
#toast button { background: none; border: 0; color: var(--accent); font-weight: 700; cursor: pointer; padding: 4px; }
.error { color: var(--neg); }
