:root {
  color-scheme: light dark;
  --bg: #EEF1F4;
  --surface: #FFFFFF;
  --ink: #121A24;
  --muted: #5B6775;
  --line: #D9DFE6;
  --accent: #1F4FD8;
  --on-accent: #FFFFFF;
  --danger: #B42318;
  --font: system-ui, -apple-system, "Segoe UI", Roboto, "Helvetica Neue", sans-serif;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0E131A;
    --surface: #17202B;
    --ink: #EAF0F6;
    --muted: #93A1B1;
    --line: #263241;
    --accent: #7B97FF;
    --on-accent: #0B1020;
    --danger: #FF8A7A;
  }
}

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

html { -webkit-text-size-adjust: 100%; }
body {
  margin: 0;
  background: var(--bg);
  color: var(--ink);
  font: 16px/1.45 var(--font);
  font-variant-numeric: tabular-nums;
  -webkit-tap-highlight-color: transparent;
}

h1, h2, p { margin: 0; }
button, input { font: inherit; color: inherit; }
.muted { color: var(--muted); }
.small { font-size: .875rem; }
.sr { position: absolute; width: 1px; height: 1px; overflow: hidden; clip: rect(0 0 0 0); white-space: nowrap; }

:focus-visible { outline: 3px solid var(--accent); outline-offset: 2px; }

.wrap {
  max-width: 560px;
  margin: 0 auto;
  padding: calc(12px + env(safe-area-inset-top)) 16px calc(120px + env(safe-area-inset-bottom));
}

/* ---------- En-tête ---------- */
.top { display: flex; align-items: center; justify-content: space-between; gap: 8px; }
.monthnav { display: flex; align-items: center; gap: 4px; }
.monthnav h1 { font-size: 1.05rem; font-weight: 600; min-width: 9.5em; text-align: center; text-transform: none; }
.icon-btn {
  width: 44px; height: 44px; border: 0; border-radius: 50%;
  background: transparent; font-size: 1.6rem; line-height: 1; cursor: pointer;
}
.icon-btn:active { background: var(--line); }

/* ---------- Total ---------- */
.hero { padding: 20px 4px 12px; }
.hero-label { color: var(--muted); font-size: .95rem; }
.hero-amount {
  font-size: clamp(2.8rem, 14vw, 4rem);
  font-weight: 700;
  letter-spacing: -.03em;
  line-height: 1.05;
  margin-top: 2px;
}
.budget { margin-top: 14px; }
.meter { height: 8px; border-radius: 4px; background: var(--line); overflow: hidden; }
#meterFill { height: 100%; width: 0; background: var(--accent); border-radius: 4px; transition: width .3s ease; }
#meterFill.over { background: var(--danger); }
.budget p { margin-top: 6px; }

/* ---------- Onglets ---------- */
.tabs { display: flex; gap: 4px; border-bottom: 1px solid var(--line); margin: 8px 0 16px; }
.tabs button {
  flex: 1; padding: 12px 0; border: 0; background: none; cursor: pointer;
  color: var(--muted); font-weight: 600; border-bottom: 3px solid transparent; margin-bottom: -1px;
}
.tabs button[aria-selected="true"] { color: var(--ink); border-bottom-color: var(--accent); }

.banner {
  background: var(--surface); border: 1px solid var(--line); border-radius: 10px;
  padding: 10px 12px; margin-bottom: 14px; font-size: .9rem; color: var(--muted);
}

/* ---------- Journal ---------- */
.day { margin-bottom: 20px; }
.day-head { display: flex; justify-content: space-between; align-items: baseline; padding: 0 4px 6px; color: var(--muted); font-size: .9rem; }
.day-head strong { font-weight: 600; color: var(--ink); }
.rows { background: var(--surface); border-radius: 16px; overflow: hidden; }
.row {
  display: flex; align-items: center; gap: 12px; width: 100%;
  padding: 14px 16px; border: 0; background: none; text-align: left; cursor: pointer;
}
.row + .row { border-top: 1px solid var(--line); }
.row:active { background: var(--bg); }
.dot { flex: none; width: 12px; height: 12px; border-radius: 50%; }
.row-main { flex: 1; min-width: 0; }
.row-cat { font-weight: 600; }
.row-note { color: var(--muted); font-size: .9rem; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.row-pending { color: var(--muted); font-size: .8rem; }
.row-amount { font-weight: 600; white-space: nowrap; }

.empty { text-align: center; color: var(--muted); padding: 40px 16px; }

/* ---------- Bilan ---------- */
.kpis { display: grid; grid-template-columns: repeat(3, 1fr); gap: 8px; margin-bottom: 24px; }
.kpi { background: var(--surface); border-radius: 12px; padding: 12px; }
.kpi span { display: block; color: var(--muted); font-size: .8rem; }
.kpi strong { display: block; font-size: 1.05rem; margin-top: 2px; }
.section-title { font-size: 1rem; font-weight: 600; margin: 0 4px 10px; }
.cat-row { margin: 0 4px 14px; }
.cat-top { display: flex; justify-content: space-between; gap: 8px; margin-bottom: 5px; font-size: .95rem; }
.cat-top small { color: var(--muted); font-weight: 400; }
.hint { margin: 0 4px 24px; }
.cat-bar { height: 8px; border-radius: 4px; background: var(--line); overflow: hidden; }
.cat-bar div { height: 100%; border-radius: 4px; }
.chart { display: flex; align-items: flex-end; gap: 2px; height: 120px; margin: 0 4px; }
.chart .bar { flex: 1; min-height: 2px; background: var(--accent); border-radius: 2px 2px 0 0; opacity: .85; }
.chart .bar.zero { background: var(--line); opacity: 1; }
.chart .bar.today { opacity: 1; outline: 2px solid var(--ink); outline-offset: 1px; }
.chart-axis { display: flex; justify-content: space-between; margin: 6px 4px 24px; color: var(--muted); font-size: .8rem; }

/* ---------- Bouton + ---------- */
.fab {
  position: fixed;
  bottom: calc(20px + env(safe-area-inset-bottom));
  right: max(20px, calc((100vw - 560px) / 2 + 20px));
  width: 64px; height: 64px; border: 0; border-radius: 50%;
  background: var(--accent); color: var(--on-accent);
  font-size: 2.2rem; line-height: 1; cursor: pointer;
  box-shadow: 0 6px 18px rgba(18, 26, 36, .28);
}
.fab:active { transform: scale(.95); }

/* ---------- Feuilles (dialogues) ---------- */
dialog { border: 0; padding: 0; color: var(--ink); background: var(--surface); }
dialog::backdrop { background: rgba(10, 16, 26, .5); }
dialog.sheet {
  width: calc(100% - 24px); max-width: 520px; margin: auto;
  border-radius: 22px; max-height: min(92dvh, 900px);
}
dialog.sheet[open] { animation: slide-up .22s ease-out; }
@keyframes slide-up { from { transform: translateY(40px); opacity: 0; } to { transform: none; opacity: 1; } }
dialog form { padding: 20px 20px calc(20px + env(safe-area-inset-bottom)); }
dialog h2 { font-size: 1.1rem; font-weight: 600; margin-bottom: 8px; }

.amount { display: flex; align-items: baseline; justify-content: center; gap: 6px; margin: 8px 0 16px; }
.amount input {
  width: 100%; max-width: 280px; border: 0; border-bottom: 2px solid var(--line);
  background: transparent; text-align: center; font-size: 3rem; font-weight: 700; padding: 4px 0;
  letter-spacing: -.02em; border-radius: 0;
}
.amount input:focus { outline: none; border-bottom-color: var(--accent); }
.amount span { font-size: 2rem; font-weight: 600; color: var(--muted); }

.chips {
  border: 0; padding: 0; margin: 0 0 16px; min-width: 0;
  display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 8px;
}
@media (min-width: 480px) { .chips { grid-template-columns: repeat(3, minmax(0, 1fr)); } }
.chip { position: relative; min-width: 0; }
.chip input { position: absolute; opacity: 0; inset: 0; margin: 0; width: 100%; height: 100%; cursor: pointer; }
.chip span {
  display: flex; align-items: center; gap: 10px; min-height: 48px; padding: 0 14px;
  border-radius: 12px; border: 1.5px solid var(--line); background: var(--surface);
  font-size: .95rem; cursor: pointer; overflow: hidden;
}
.chip i { flex: none; width: 11px; height: 11px; border-radius: 50%; background: var(--c); }
.chip input:checked + span {
  border-color: var(--c);
  background: color-mix(in srgb, var(--c) 16%, var(--surface));
  box-shadow: 0 0 0 1px var(--c) inset;
  font-weight: 600;
}
.chip-new span { justify-content: center; border-style: dashed; color: var(--accent); font-weight: 600; background: transparent; }
.chip-new input:checked + span { border-style: solid; border-color: var(--accent); background: color-mix(in srgb, var(--accent) 12%, var(--surface)); box-shadow: 0 0 0 1px var(--accent) inset; }
.chip input:focus-visible + span { outline: 3px solid var(--accent); outline-offset: 2px; }
.catnew { margin: -4px 0 16px; }

.colorpick {
  flex: none; width: 44px; height: 44px; padding: 3px; border-radius: 10px;
  border: 1.5px solid var(--line); background: var(--surface); cursor: pointer;
}
.items .colorpick { width: 34px; height: 34px; padding: 2px; }

.field { display: block; }
.field span, .field > label { display: block; font-size: .85rem; color: var(--muted); margin-bottom: 4px; }
.field input {
  width: 100%; padding: 12px; border: 1.5px solid var(--line); border-radius: 10px;
  background: var(--bg); min-height: 48px;
}
.field input:focus { outline: none; border-color: var(--accent); }
.cols2 { display: grid; grid-template-columns: 1fr 1fr; gap: 12px; margin-bottom: 12px; }
.field select {
  width: 100%; padding: 12px; border: 1.5px solid var(--line); border-radius: 10px;
  background: var(--bg); min-height: 48px; text-overflow: ellipsis;
}
.field select:focus { outline: none; border-color: var(--accent); }
.field select + input { margin-top: 8px; }
.section-label { margin-top: 20px; font-size: .85rem; color: var(--muted); }
.row2 { display: grid; grid-template-columns: 1fr 1.4fr; gap: 12px; }

.error { color: var(--danger); min-height: 1.4em; margin-top: 10px; font-size: .9rem; }

.actions { display: flex; gap: 8px; margin-top: 12px; }
.actions .btn { flex: 1; }
.btn {
  display: inline-flex; align-items: center; justify-content: center;
  min-height: 48px; padding: 0 16px; border-radius: 12px; border: 1.5px solid var(--line);
  background: transparent; font-weight: 600; cursor: pointer; text-decoration: none; color: var(--ink);
}
.btn.primary { background: var(--accent); border-color: var(--accent); color: var(--on-accent); }
.btn.danger { color: var(--danger); border-color: var(--danger); }
.btn:active { filter: brightness(.92); }

/* ---------- Justificatifs ---------- */
.attach { margin-top: 4px; }
.attach .actions { margin-top: 8px; }
.attach .error { margin-top: 6px; min-height: 0; }
.att-list { list-style: none; margin: 8px 0 0; padding: 0; }
.att { display: flex; align-items: center; gap: 10px; padding: 8px 0; border-top: 1px solid var(--line); }
.att-thumb {
  flex: none; width: 48px; height: 48px; border-radius: 8px; object-fit: cover; background: var(--bg);
  display: inline-flex; align-items: center; justify-content: center; font-size: .75rem; font-weight: 700; color: var(--muted);
}
.att-thumb.pdf { border: 1.5px solid var(--line); }
.att-main { flex: 1; min-width: 0; }
.att-name { white-space: nowrap; overflow: hidden; text-overflow: ellipsis; font-weight: 600; }
.att-meta { color: var(--muted); font-size: .8rem; }
.att-link { color: var(--accent); font-weight: 600; text-decoration: none; padding: 10px 6px; }
.clip { display: inline-flex; vertical-align: -2px; margin-right: 8px; color: var(--muted); }

/* ---------- Gestion des listes ---------- */
.listbox { padding: 20px 20px calc(20px + env(safe-area-inset-bottom)); }
dialog .addrow { display: flex; gap: 8px; margin-top: 8px; padding: 0; }
.addrow input {
  flex: 1; min-width: 0; padding: 12px; border: 1.5px solid var(--line); border-radius: 10px;
  background: var(--bg); min-height: 48px;
}
.addrow input:focus { outline: none; border-color: var(--accent); }
.items { list-style: none; margin: 8px 0 0; padding: 0; max-height: 42dvh; overflow: auto; }
.items li { display: flex; align-items: center; gap: 2px; padding: 2px 0; border-top: 1px solid var(--line); }
.items li > span { flex: 1; min-width: 0; overflow-wrap: anywhere; padding: 8px 0; }
.items li.muted { padding: 12px 0; }
.icon-btn.small { width: 40px; height: 40px; font-size: 1.1rem; }

/* ---------- Connexion ---------- */
.login { padding-top: calc(15vh + env(safe-area-inset-top)); }
.login h1 { font-size: 2rem; margin-bottom: 6px; }
.login form { margin-top: 24px; }
.login .btn { width: 100%; margin-top: 8px; }

@media (prefers-reduced-motion: reduce) {
  dialog.sheet[open] { animation: none; }
  #meterFill { transition: none; }
}

/* ---------- Utilisateurs ---------- */
.adduser { display: grid; gap: 8px; margin-top: 8px; }
.adduser input, .adduser select, .users select {
  width: 100%; padding: 12px; border: 1.5px solid var(--line); border-radius: 10px;
  background: var(--bg); min-height: 48px; color: inherit;
}
.items.users li { flex-wrap: wrap; padding: 8px 0; }
.items.users li > span { flex: 1 1 100%; padding: 0; }
.items.users small { color: var(--muted); }
.items.users select { flex: 1 1 auto; width: auto; min-height: 40px; padding: 6px 10px; }
.items.users .btn { min-height: 40px; padding: 0 12px; font-size: .9rem; }
.badge { display: inline-block; padding: 1px 8px; border-radius: 999px; background: var(--line); font-size: .75rem; margin-left: 6px; }
.adduser { padding: 0; }
.items.users select { flex: 1 1 100px; width: 0; min-width: 0; }
.items.users .btn { flex: none; }

/* ---------- Pied de page et page Nouveautés ---------- */
.appfoot { margin-top: 32px; padding: 16px 0 96px; text-align: center; font-size: .8rem; color: var(--muted); line-height: 1.6; }
.appfoot a, .changelog .back { color: var(--accent); font-weight: 600; text-decoration: none; }
.login .appfoot { padding-bottom: 24px; }
.changelog { padding-top: calc(24px + env(safe-area-inset-top)); padding-bottom: calc(32px + env(safe-area-inset-bottom)); }
.changelog h1 { font-size: 1.6rem; margin: 8px 0 4px; }
.release { background: var(--surface); border-radius: 16px; padding: 16px 18px; margin-top: 14px; }
.release h2 { font-size: 1.05rem; margin: 0 0 8px; }
.release h2 small { color: var(--muted); font-weight: 400; font-size: .85rem; }
.release ul { margin: 0; padding-left: 20px; }
.release li { margin: 4px 0; }
.credit { margin-top: 24px; text-align: center; }

/* ---------- Logo ---------- */
.logo { display: block; width: 72px; height: 72px; border-radius: 22%; margin: 0 0 16px; box-shadow: 0 6px 18px rgba(11, 21, 65, .3); }
.logo.small { display: inline-block; width: 36px; height: 36px; border-radius: 22%; margin: 0 10px 0 0; vertical-align: -9px; box-shadow: none; }

/* ---------- Afficher / masquer le mot de passe ---------- */
.pwwrap { position: relative; display: block; width: 100%; }
.pwwrap input { width: 100%; padding-right: 52px; }
.pwtoggle {
  position: absolute; right: 4px; top: 50%; transform: translateY(-50%);
  width: 44px; height: 44px; display: grid; place-items: center;
  border: 0; background: transparent; color: var(--muted); border-radius: 10px; cursor: pointer;
}
.pwtoggle:hover, .pwtoggle:focus-visible { color: var(--accent); }
.pwtoggle[aria-pressed="true"] { color: var(--accent); }

.payfield { margin-bottom: 14px; }

/* ---------- Bilan : synthèse et sections dépliables ---------- */
.synth { background: var(--surface); border-radius: 16px; padding: 14px 14px 6px; margin-bottom: 8px; }
.synth .section-title { margin: 0 0 8px; }
.synth-row {
  display: flex; justify-content: space-between; align-items: baseline; gap: 12px; width: 100%;
  padding: 11px 0; border: 0; border-top: 1px solid var(--line); background: transparent; color: inherit;
  font: inherit; text-align: left; cursor: pointer;
}
.synth-row:first-of-type { border-top: 0; }
.synth-label { color: var(--muted); font-size: .85rem; flex: none; }
.synth-value { text-align: right; min-width: 0; }
.synth-value strong { font-weight: 600; }
.synth-value small { color: var(--muted); font-size: .85rem; white-space: nowrap; }
.synth-row:focus-visible { outline: 3px solid var(--accent); outline-offset: 2px; border-radius: 8px; }

details.src { background: var(--surface); border-radius: 14px; margin: 0 0 8px; }
details.src > summary {
  list-style: none; display: flex; align-items: center; gap: 10px; padding: 14px; cursor: pointer; min-height: 52px;
}
details.src > summary::-webkit-details-marker { display: none; }
details.src > summary::after {
  content: ''; flex: none; width: 9px; height: 9px; margin-left: 4px;
  border-right: 2px solid var(--muted); border-bottom: 2px solid var(--muted); transform: rotate(45deg); transition: transform .15s;
}
details.src[open] > summary::after { transform: rotate(-135deg); }
.src-title { font-weight: 600; flex: none; }
.src-top { flex: 1; min-width: 0; text-align: right; color: var(--muted); font-size: .88rem; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.src-body { padding: 4px 14px 12px; }
.src-body .cat-row { margin: 0 0 14px; }
.src-body .chart { margin: 8px 0 0; }
.src-body .chart-axis { margin: 6px 0 4px; }
details.src > summary:focus-visible { outline: 3px solid var(--accent); outline-offset: 2px; border-radius: 12px; }

/* ---------- Divers ---------- */
.login-info { color: var(--muted); margin: 8px 0 0; min-height: 1.2em; }
.success { color: #1c7a4d; font-size: .95rem; margin: 8px 0; }
@media (prefers-color-scheme: dark) { .success { color: #5ad19a; } }
h2[tabindex="-1"]:focus { outline: none; }
textarea {
  width: 100%; padding: 12px; border: 1.5px solid var(--line); border-radius: 10px;
  background: var(--bg); color: inherit; font: inherit; resize: vertical; min-height: 120px;
}
textarea:focus { outline: none; border-color: var(--accent); }
a.btn { display: grid; place-items: center; text-decoration: none; margin: 8px 0; }

/* ---------- « À retenir » : libellé à gauche, nom en gras centré, chiffres à droite ---------- */
.synth-row { display: grid; grid-template-columns: minmax(0, 1fr) minmax(0, 1fr) auto; align-items: center; column-gap: 10px; }
.synth-name { text-align: center; font-weight: 700; overflow-wrap: anywhere; }
.synth-figs { text-align: right; color: var(--muted); font-size: .85rem; white-space: nowrap; }
