:root {
  --bg: #f4f5f7;
  --surface: #ffffff;
  --text: #16181d;
  --muted: #6b7280;
  --border: #e3e6ea;
  --accent: #2f6fed;
  --accent-text: #ffffff;
  --ok: #16a34a;
  --warn: #d97706;
  --bad: #dc2626;
  --track: #e8ebef;
  --radius: 16px;
  --shadow: 0 1px 2px rgba(16, 24, 40, 0.05), 0 4px 16px rgba(16, 24, 40, 0.04);
}

/* Thème : automatique (suit l'appareil) par défaut ; data-theme="light|dark" force un choix (voir theme.js). */
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #0f1115;
    --surface: #1a1d23;
    --text: #f1f3f5;
    --muted: #9aa3af;
    --border: #2a2f38;
    --accent: #5b8def;
    --ok: #34d399;
    --warn: #fbbf24;
    --bad: #f87171;
    --track: #2a2f38;
    --shadow: none;
    color-scheme: dark;
  }
}

:root[data-theme="dark"] {
  --bg: #0f1115;
  --surface: #1a1d23;
  --text: #f1f3f5;
  --muted: #9aa3af;
  --border: #2a2f38;
  --accent: #5b8def;
  --ok: #34d399;
  --warn: #fbbf24;
  --bad: #f87171;
  --track: #2a2f38;
  --shadow: none;
  color-scheme: dark;
}

:root[data-theme="light"] { color-scheme: light; }

* { box-sizing: border-box; }

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

body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font: 16px/1.45 -apple-system, BlinkMacSystemFont, "SF Pro Text", "Segoe UI", Roboto, sans-serif;
  -webkit-font-smoothing: antialiased;
  padding-left: env(safe-area-inset-left);
  padding-right: env(safe-area-inset-right);
  padding-bottom: env(safe-area-inset-bottom);
}

/* ---------- En-tête ---------- */
.topbar {
  position: sticky;
  top: 0;
  z-index: 10;
  background: color-mix(in srgb, var(--bg) 85%, transparent);
  -webkit-backdrop-filter: saturate(180%) blur(14px);
  backdrop-filter: saturate(180%) blur(14px);
  border-bottom: 1px solid var(--border);
  padding-top: env(safe-area-inset-top);
}
.topbar-inner {
  max-width: 720px;
  margin: 0 auto;
  padding: 10px 16px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
}
.topbar h1 { font-size: 20px; margin: 0; letter-spacing: -0.01em; }

/* ---------- Mise en page ---------- */
main { max-width: 720px; margin: 0 auto; padding: 16px 16px 48px; display: grid; gap: 16px; }

.card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 18px;
}
.card h2 { font-size: 15px; margin: 0 0 12px; color: var(--muted); font-weight: 600; }

.row { display: flex; align-items: center; justify-content: space-between; gap: 12px; }
.muted { color: var(--muted); }
.small { font-size: 13px; }
.num { font-variant-numeric: tabular-nums; }

/* ---------- Solde ---------- */
.balance-label { font-size: 14px; color: var(--muted); font-weight: 600; }
.balance {
  font-size: clamp(38px, 11vw, 56px);
  font-weight: 750;
  letter-spacing: -0.03em;
  line-height: 1.1;
  margin: 2px 0 14px;
}
.balance.ok { color: var(--ok); }
.balance.warn { color: var(--warn); }
.balance.bad { color: var(--bad); }

.bar { height: 10px; border-radius: 999px; background: var(--track); overflow: hidden; }
.bar > span { display: block; height: 100%; border-radius: 999px; background: var(--ok); transition: width 0.3s ease; }
.bar.warn > span { background: var(--warn); }
.bar.bad > span { background: var(--bad); }

.stats { display: flex; justify-content: space-between; gap: 12px; margin-top: 12px; font-size: 14px; }
.stats strong { display: block; font-size: 16px; }
.stats div:last-child { text-align: right; }

.card-actions { display: flex; flex-wrap: wrap; gap: 8px; margin-top: 16px; }

/* ---------- Formulaires ---------- */
form { display: grid; gap: 12px; }
label { display: grid; gap: 6px; font-size: 13px; font-weight: 600; color: var(--muted); }

input, select {
  font: inherit;
  font-size: 16px; /* évite le zoom automatique d'iOS */
  color: var(--text);
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 12px 14px;
  min-height: 46px;
  width: 100%;
  -webkit-appearance: none;
  appearance: none;
}
input[type="date"] { min-width: 0; text-align: left; }
input:focus-visible { outline: 2px solid var(--accent); outline-offset: 1px; border-color: transparent; }

.form-grid { display: grid; gap: 12px; grid-template-columns: 1fr 1fr; }
.form-grid .span-2 { grid-column: 1 / -1; }

button {
  font: inherit;
  font-weight: 600;
  font-size: 16px;
  min-height: 46px;
  padding: 0 18px;
  border-radius: 12px;
  border: 1px solid var(--border);
  background: var(--surface);
  color: var(--text);
  cursor: pointer;
  -webkit-tap-highlight-color: transparent;
}
button:active { transform: scale(0.98); }
button.primary { background: var(--accent); color: var(--accent-text); border-color: transparent; }
button.danger { color: var(--bad); }
button.danger.solid { background: var(--bad); color: #fff; border-color: transparent; }
button.small { min-height: 38px; font-size: 14px; padding: 0 14px; }
button.ghost { background: transparent; }
button:disabled { opacity: 0.5; }
button:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }

/* ---------- Liste de dépenses ---------- */
.expenses { list-style: none; margin: 0; padding: 0; }
.expense {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 12px 0;
  border-top: 1px solid var(--border);
}
.expense:first-child { border-top: 0; padding-top: 0; }
.expense:last-child { padding-bottom: 0; }
.expense-main { flex: 1; min-width: 0; text-align: left; }
.expense-desc { font-weight: 600; overflow-wrap: anywhere; }
.expense-date { font-size: 13px; color: var(--muted); }
.expense-amount { font-weight: 650; white-space: nowrap; }
.expense-btn {
  display: flex;
  align-items: center;
  gap: 12px;
  flex: 1;
  min-width: 0;
  cursor: pointer;
  -webkit-tap-highlight-color: transparent;
}
button.expense-btn {
  all: unset;
  display: flex;
  align-items: center;
  gap: 12px;
  flex: 1;
  min-width: 0;
  cursor: pointer;
  -webkit-tap-highlight-color: transparent;
}
.readonly .expense-btn { cursor: default; }

.empty { text-align: center; color: var(--muted); padding: 12px 0; }
.group-title { font-size: 13px; font-weight: 700; color: var(--muted); margin: 14px 0 4px; text-transform: capitalize; }
.group-title:first-child { margin-top: 0; }

/* ---------- Archives ---------- */
.archive-link {
  display: block;
  color: inherit;
  text-decoration: none;
}
.archive-link .row + .row { margin-top: 4px; }
.badge {
  display: inline-block;
  font-size: 12px;
  font-weight: 700;
  padding: 3px 10px;
  border-radius: 999px;
  background: var(--track);
  color: var(--muted);
}
.back { color: var(--accent); text-decoration: none; font-weight: 600; }

/* ---------- Dialogue ---------- */
dialog {
  border: 0;
  border-radius: 20px;
  padding: 20px;
  width: min(440px, calc(100vw - 32px));
  background: var(--surface);
  color: var(--text);
  box-shadow: 0 20px 60px rgba(0, 0, 0, 0.35);
}
dialog::backdrop { background: rgba(0, 0, 0, 0.45); }
dialog h3 { margin: 0 0 6px; font-size: 18px; }
dialog p { margin: 0 0 14px; color: var(--muted); }
.dialog-actions { display: grid; gap: 8px; margin-top: 14px; }
.dialog-actions.inline { grid-auto-flow: column; grid-auto-columns: 1fr; }

/* ---------- Toast ---------- */
.toast {
  position: fixed;
  left: 50%;
  bottom: calc(20px + env(safe-area-inset-bottom));
  transform: translateX(-50%);
  background: var(--text);
  color: var(--bg);
  padding: 12px 18px;
  border-radius: 999px;
  font-size: 14px;
  font-weight: 600;
  z-index: 50;
  max-width: calc(100vw - 32px);
}
.toast.error { background: var(--bad); color: #fff; }

/* ---------- Bureau ---------- */
@media (min-width: 720px) {
  main { padding-top: 28px; }
}

@media (max-width: 380px) {
  .form-grid { grid-template-columns: 1fr; }
}

@media (prefers-reduced-motion: reduce) {
  * { transition: none !important; }
}

/* ---------- Connexion / compte ---------- */
body.logged-out .menu, body.logged-out .menu-btn#menu-btn, body.logged-out .menu-backdrop { display: none !important; }
.login { margin-top: 8vh; }
.greeting { margin: 0 4px -4px; font-size: 14px; color: var(--muted); font-weight: 600; }

/* ---------- Administration ---------- */
[hidden] { display: none !important; }
select {
  font: inherit;
  font-size: 16px;
  color: var(--text);
  background-color: var(--bg);
  background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='12' height='8' fill='none' stroke='%239aa3af' stroke-width='2' stroke-linecap='round' stroke-linejoin='round'%3E%3Cpath d='M1 1.5l5 5 5-5'/%3E%3C/svg%3E");
  background-repeat: no-repeat;
  background-position: right 14px center;
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 12px 38px 12px 14px;
  min-height: 46px;
  width: 100%;
  -webkit-appearance: none;
  appearance: none;
}
.badge-admin { background: color-mix(in srgb, var(--accent) 16%, transparent); color: var(--accent); }
.secret-box { background: var(--bg); border: 1px dashed var(--border); border-radius: 12px; padding: 14px; text-align: center; }
.secret {
  display: block;
  margin-top: 6px;
  font: 700 26px/1.2 ui-monospace, SFMono-Regular, Menlo, monospace;
  letter-spacing: 0.04em;
  user-select: all;
  -webkit-user-select: all;
  overflow-wrap: anywhere;
}
.warn-note { color: var(--warn) !important; font-size: 14px; font-weight: 600; margin: 12px 0 0 !important; }
dialog { max-height: calc(100dvh - 32px); overflow-y: auto; }

/* ---------- Menu hamburger ---------- */
.menu-btn {
  display: grid;
  place-items: center;
  width: 44px;
  height: 44px;
  min-height: 0;
  padding: 0;
  border-radius: 12px;
  background: transparent;
  border: 0;
  color: var(--text);
}
.menu-btn:active { background: var(--track); transform: none; }
.topbar-inner { padding-right: 8px; }

.menu-backdrop {
  position: fixed;
  inset: 0;
  z-index: 40;
  background: rgba(0, 0, 0, 0.4);
  -webkit-backdrop-filter: blur(2px);
  backdrop-filter: blur(2px);
}

.menu {
  position: fixed;
  top: 0;
  right: 0;
  bottom: 0;
  z-index: 41;
  width: min(300px, 86vw);
  height: 100vh;
  height: 100dvh;
  display: flex;
  flex-direction: column;
  background: var(--surface);
  border-left: 1px solid var(--border);
  box-shadow: -12px 0 40px rgba(0, 0, 0, 0.18);
  padding: env(safe-area-inset-top) calc(12px + env(safe-area-inset-right)) calc(12px + env(safe-area-inset-bottom)) 12px;
  transform: translateX(105%);
  visibility: hidden;
  transition: transform 0.22s ease, visibility 0s linear 0.22s;
}
.menu.open { transform: none; visibility: visible; transition: transform 0.22s ease; }

.menu-head { display: flex; align-items: center; justify-content: space-between; gap: 8px; padding: 10px 0 14px 8px; border-bottom: 1px solid var(--border); }
.menu-user { display: grid; min-width: 0; }
.menu-user strong { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; font-size: 17px; }

.menu-links { display: grid; gap: 4px; padding: 12px 0; flex: 1; align-content: start; }
.menu-links a {
  display: block;
  padding: 14px;
  border-radius: 12px;
  color: var(--text);
  text-decoration: none;
  font-weight: 600;
  font-size: 17px;
  -webkit-tap-highlight-color: transparent;
}
.menu-links a:active { background: var(--track); }
.menu-links a.active { background: color-mix(in srgb, var(--accent) 14%, transparent); color: var(--accent); }
.menu-logout { color: var(--bad); width: 100%; }

/* ---------- Choix du thème ---------- */
.segmented { display: grid; grid-template-columns: repeat(3, 1fr); gap: 4px; background: var(--track); padding: 4px; border-radius: 14px; border: 0; margin: 0; min-width: 0; }
.segmented label { position: relative; display: block; margin: 0; }
.segmented input { position: absolute; inset: 0; opacity: 0; width: 100%; height: 100%; margin: 0; cursor: pointer; }
.segmented span {
  display: grid;
  place-items: center;
  min-height: 42px;
  padding: 0 6px;
  border-radius: 10px;
  color: var(--muted);
  font-weight: 600;
  font-size: 15px;
  text-align: center;
}
.segmented input:checked + span { background: var(--surface); color: var(--text); box-shadow: var(--shadow), 0 0 0 1px var(--border); }
.segmented input:focus-visible + span { outline: 2px solid var(--accent); outline-offset: 1px; }
