:root {
  --bg: #f5f2ea; --card: #fffdf8; --ink: #17211f; --muted: #66716d; --line: #e4dfd2;
  --brand: #123c3a; --on-brand: #f5f2ea; --accent: #1f7a6b; --accent-ink: #fff;
  --pos: #1c7a52; --neg: #c2412d; --warn: #c98a12; --field: #fff;
  --radius: 16px; --shadow: 0 1px 2px rgb(20 30 28 / 6%), 0 6px 20px rgb(20 30 28 / 5%);
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #0e1615; --card: #17211f; --ink: #ecefe9; --muted: #9aa6a1; --line: #29353a;
    --brand: #0b2a29; --accent: #4fc3a8; --accent-ink: #06201c; --pos: #5fd39b; --neg: #ff8a73; --warn: #f0b94a; --field: #101a19;
    --shadow: none; color-scheme: dark;
  }
}
:root[data-theme="dark"] {
  --bg: #0e1615; --card: #17211f; --ink: #ecefe9; --muted: #9aa6a1; --line: #29353a;
  --brand: #0b2a29; --accent: #4fc3a8; --accent-ink: #06201c; --pos: #5fd39b; --neg: #ff8a73; --warn: #f0b94a; --field: #101a19;
  --shadow: none; color-scheme: dark;
}

* { box-sizing: border-box; }
html { -webkit-text-size-adjust: 100%; }
body {
  margin: 0; background: var(--bg); color: var(--ink);
  font: 16px/1.45 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  -webkit-font-smoothing: antialiased; overflow-x: hidden;
}
h1, h2, h3, p { margin: 0; }
h2 { font-size: .8rem; text-transform: uppercase; letter-spacing: .08em; color: var(--muted); font-weight: 650; margin-bottom: 12px; }
h3 { font-size: .9rem; margin: 16px 0 8px; }
button, input, select { font: inherit; color: inherit; }
button { cursor: pointer; }
[hidden] { display: none !important; }
.sr { position: absolute; width: 1px; height: 1px; overflow: hidden; clip-path: inset(50%); }
.muted { color: var(--muted); }
.small, small { font-size: .82rem; }
.pos { color: var(--pos); }
.neg { color: var(--neg); }
.boot { padding: 30vh 16px; text-align: center; color: var(--muted); }
:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }

/* ----- Structure ----- */
#app { min-height: 100dvh; }
.side { display: none; }
.main { max-width: 760px; margin: 0 auto; padding: 0 16px calc(96px + env(safe-area-inset-bottom)); }
.top { display: flex; align-items: center; gap: 12px; padding: calc(12px + env(safe-area-inset-top)) 0 12px; }
.space-pick { flex: 1; min-width: 0; }
.space-pick select { width: 100%; border: 0; background: transparent; font-size: 1.25rem; font-weight: 700; padding: 6px 0; text-overflow: ellipsis; }
.top .add-top { display: none; }
main > * + * { margin-top: 14px; }
.grid { display: grid; gap: 14px; }

.tabs {
  position: fixed; inset: auto 0 0 0; z-index: 5; display: flex; background: var(--card);
  border-top: 1px solid var(--line); padding-bottom: env(safe-area-inset-bottom);
}
.tabs button { flex: 1; border: 0; background: none; padding: 9px 2px 8px; display: grid; justify-items: center; gap: 2px; font-size: .68rem; color: var(--muted); }
.tabs button.on { color: var(--accent); font-weight: 650; }
svg { width: 22px; height: 22px; fill: none; stroke: currentColor; stroke-width: 1.8; stroke-linecap: round; stroke-linejoin: round; }
.fab {
  position: fixed; right: 16px; bottom: calc(72px + env(safe-area-inset-bottom)); z-index: 6; width: 56px; height: 56px; border-radius: 50%;
  border: 0; background: var(--accent); color: var(--accent-ink); font-size: 1.7rem; line-height: 1; box-shadow: 0 6px 18px rgb(0 0 0 / 25%);
}

@media (min-width: 900px) {
  #app { display: grid; grid-template-columns: 232px 1fr; }
  .side { display: block; position: sticky; top: 0; height: 100dvh; padding: 22px 14px; border-right: 1px solid var(--line); background: var(--card); }
  .brand { display: flex; align-items: center; gap: 10px; font-weight: 750; font-size: 1.15rem; padding: 0 8px 18px; }
  .brand img { border-radius: 9px; }
  .side nav { display: grid; gap: 2px; }
  .side nav button { display: flex; align-items: center; gap: 12px; border: 0; background: none; padding: 10px; border-radius: 10px; text-align: left; color: var(--muted); }
  .side nav button:hover { background: var(--bg); }
  .side nav button.on { background: var(--bg); color: var(--ink); font-weight: 650; }
  .main { max-width: 980px; width: 100%; padding: 0 28px 48px; }
  .tabs, .fab { display: none; }
  .top .add-top { display: inline-flex; }
  .grid { grid-template-columns: 1fr 1fr; }
  .grid > :last-child:nth-child(odd) { grid-column: 1 / -1; }
}

/* ----- Composants ----- */
.card { background: var(--card); border: 1px solid var(--line); border-radius: var(--radius); padding: 16px; box-shadow: var(--shadow); min-width: 0; }
.hero { background: var(--brand); color: var(--on-brand); border-radius: var(--radius); padding: 20px 18px; }
.hero small { opacity: .75; }
.big { font-size: clamp(1.9rem, 8vw, 2.6rem); font-weight: 750; letter-spacing: -.02em; font-variant-numeric: tabular-nums; }
.trio { display: grid; grid-template-columns: repeat(3, 1fr); gap: 8px; margin-top: 14px; padding-top: 14px; border-top: 1px solid rgb(255 255 255 / 16%); }
.trio b { display: block; font-variant-numeric: tabular-nums; }
.insight { font-size: .9rem; padding: 10px 14px; border-radius: 12px; background: color-mix(in srgb, var(--accent) 12%, transparent); }
.lead { font-size: 1.35rem; font-weight: 700; margin-bottom: 8px; font-variant-numeric: tabular-nums; }
.lead .muted { font-size: .95rem; font-weight: 500; }

.month-nav { display: flex; align-items: center; justify-content: space-between; }
.month-nav strong { text-transform: capitalize; }
.month-nav button { width: 44px; height: 44px; border-radius: 50%; border: 1px solid var(--line); background: var(--card); font-size: 1.3rem; line-height: 1; }

.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 6px; min-height: 44px; padding: 0 16px; border-radius: 12px;
  border: 1px solid var(--line); background: var(--card); font-weight: 600; text-decoration: none; color: var(--ink); white-space: nowrap;
}
.btn.primary { background: var(--accent); border-color: var(--accent); color: var(--accent-ink); }
.btn.ghost { background: transparent; }
.btn.danger { color: var(--neg); }
.btn.small { min-height: 36px; padding: 0 12px; font-size: .88rem; }
.btn.block { width: 100%; }
.link { border: 0; background: none; padding: 8px 0 0; color: var(--accent); font-weight: 600; }
.actions { display: flex; flex-wrap: wrap; gap: 8px; align-items: center; margin-top: 12px; }
.grow { flex: 1; }

.bar { height: 8px; border-radius: 99px; background: var(--line); overflow: hidden; margin: 6px 0; }
.bar i { display: block; height: 100%; border-radius: 99px; background: var(--accent); }
.bar.warn i { background: var(--warn); }
.bar.over i { background: var(--neg); }

.tx, .account, .budget, .menu button {
  display: flex; align-items: center; gap: 12px; width: 100%; border: 0; background: none; text-align: left;
  padding: 10px 0; border-top: 1px solid var(--line);
}
h2 + .tx, h2 + .account, h2 + .budget, .day + .tx, .menu button:first-child, table + .tx { border-top: 0; }
.dot { flex: none; width: 40px; height: 40px; border-radius: 12px; display: grid; place-items: center; background: var(--bg); font-size: 1.15rem; }
.tx-main { flex: 1; min-width: 0; display: grid; }
.tx-main b, .tx-main small { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.tx-main b { font-weight: 600; }
.tx-main small { color: var(--muted); }
.amount { font-weight: 650; font-variant-numeric: tabular-nums; white-space: nowrap; }
.amount.income { color: var(--pos); }
.amount.expense { color: var(--ink); }
.account .amount.expense { color: var(--neg); }
.account.archived { opacity: .55; }
.day { display: flex; justify-content: space-between; padding: 14px 0 4px; font-size: .8rem; color: var(--muted); font-weight: 600; }
.day span:first-child::first-letter { text-transform: uppercase; }
.day:first-child { padding-top: 0; }
.budget { display: block; }
.budget-head { display: flex; justify-content: space-between; gap: 12px; font-variant-numeric: tabular-nums; }
.goal { padding: 12px 0; border-top: 1px solid var(--line); }
h2 + .goal { border-top: 0; padding-top: 0; }
.menu button { padding: 14px 0; font-weight: 600; }
.menu button span:nth-child(2) { flex: 1; }
.empty { text-align: center; padding: 22px 8px; color: var(--muted); display: grid; gap: 12px; justify-items: center; }

.filters { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; }
.filters input { grid-column: 1 / -1; }
input, select {
  width: 100%; min-height: 44px; padding: 8px 12px; border-radius: 12px; border: 1px solid var(--line); background: var(--field); min-width: 0;
}
input[type="checkbox"], input[type="radio"] { width: 20px; height: 20px; min-height: 0; padding: 0; accent-color: var(--accent); flex: none; }
input[type="color"] { padding: 4px; }
label { display: grid; gap: 4px; font-size: .85rem; font-weight: 600; color: var(--muted); }
label input, label select { font-size: 1rem; font-weight: 400; color: var(--ink); }
.check { display: flex; align-items: center; gap: 10px; color: var(--ink); font-size: .95rem; }
.hint { font-size: .82rem; color: var(--muted); }
.form-error { color: var(--neg); font-weight: 600; font-size: .9rem; }
.two { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; }
.two.narrow { grid-template-columns: 76px 1fr; }
.stack, .inline { display: grid; gap: 10px; }
.inline { grid-template-columns: 1fr auto; align-items: end; margin-bottom: 8px; }
.stack .btn { justify-self: start; }
.seg-label { display: flex; align-items: center; gap: 8px; margin-right: auto; }
.seg-label select { width: auto; }

/* Graphiques */
.donut-wrap { display: grid; grid-template-columns: minmax(110px, 150px) 1fr; gap: 16px; align-items: center; }
.donut { width: 100%; height: auto; stroke: none; }
.donut-total { font-size: 4.6px; font-weight: 700; text-anchor: middle; fill: var(--ink); stroke: none; }
.donut-cap { font-size: 2.6px; text-anchor: middle; fill: var(--muted); stroke: none; }
.legend, .people { list-style: none; margin: 0; padding: 0; display: grid; gap: 6px; }
.legend li { display: grid; grid-template-columns: 10px 1fr auto; gap: 8px; align-items: center; font-size: .88rem; }
.legend li span { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.legend i, .chip i, .keys i { width: 10px; height: 10px; border-radius: 3px; display: inline-block; }
.legend b { font-variant-numeric: tabular-nums; }
.trend { display: grid; grid-template-columns: repeat(6, 1fr); gap: 8px; text-align: center; }
.cols { height: 120px; display: flex; align-items: flex-end; justify-content: center; gap: 4px; border-bottom: 1px solid var(--line); }
.cols i { width: 38%; max-width: 18px; min-height: 2px; border-radius: 4px 4px 0 0; }
.in { background: var(--accent); }
.out { background: #e0795f; }
.trend small { color: var(--muted); text-transform: capitalize; }
.keys { display: flex; gap: 16px; margin-top: 10px; font-size: .8rem; color: var(--muted); }
.keys span { display: inline-flex; align-items: center; gap: 6px; }

/* Partage */
.owe { display: flex; align-items: center; justify-content: space-between; gap: 10px; padding: 10px 0; border-top: 1px solid var(--line); }
h2 + .owe { border-top: 0; padding-top: 0; }
.owe .actions { margin: 0; flex-wrap: nowrap; }
.people { margin-top: 12px; padding-top: 12px; border-top: 1px solid var(--line); }
h3 + .people { border-top: 0; margin-top: 0; padding-top: 0; }
.people li { display: flex; justify-content: space-between; align-items: center; gap: 10px; }
.people b { font-variant-numeric: tabular-nums; }
.code { font: 700 1.7rem/1 ui-monospace, SFMono-Regular, Menlo, monospace; letter-spacing: .18em; text-align: center; padding: 16px; margin-top: 12px; border-radius: 12px; background: var(--bg); border: 1px dashed var(--line); user-select: all; }
table { width: 100%; border-collapse: collapse; font-size: .9rem; margin-bottom: 8px; }
th { text-align: left; color: var(--muted); font-weight: 600; font-size: .78rem; }
th, td { padding: 6px 0; border-bottom: 1px solid var(--line); }
th:not(:first-child), td:not(:first-child) { text-align: right; font-variant-numeric: tabular-nums; }
.chips { display: flex; flex-wrap: wrap; gap: 8px; }
.chip { display: inline-flex; align-items: center; gap: 6px; min-height: 36px; padding: 0 12px; border-radius: 99px; border: 1px solid var(--line); background: var(--bg); font-size: .9rem; }
.chip.add { border-style: dashed; color: var(--muted); background: none; }

/* Connexion */
.auth { max-width: 380px; margin: 0 auto; padding: 12vh 16px 40px; display: grid; gap: 12px; justify-items: center; text-align: center; }
.auth img { border-radius: 16px; }
.auth form { width: 100%; display: grid; gap: 12px; text-align: left; margin-top: 8px; }

/* Fenêtres */
.modal { border: 0; padding: 0; background: var(--card); color: var(--ink); border-radius: 20px; width: min(520px, calc(100vw - 16px)); max-height: calc(100dvh - 16px); box-shadow: 0 20px 60px rgb(0 0 0 / 35%); }
.modal::backdrop { background: rgb(8 16 15 / 55%); }
.modal form { display: flex; flex-direction: column; max-height: calc(100dvh - 16px); }
.modal header { display: flex; justify-content: space-between; align-items: center; padding: 16px 18px 6px; }
.modal header h2 { margin: 0; font-size: 1.05rem; text-transform: none; letter-spacing: 0; color: var(--ink); }
.modal .x { border: 0; background: none; width: 40px; height: 40px; font-size: 1rem; color: var(--muted); }
.modal-body { padding: 8px 18px 14px; display: grid; gap: 12px; overflow-y: auto; }
.modal footer { display: flex; gap: 8px; padding: 12px 18px calc(14px + env(safe-area-inset-bottom)); border-top: 1px solid var(--line); }
.seg { display: grid; grid-template-columns: repeat(3, 1fr); background: var(--bg); border-radius: 12px; padding: 4px; }
.seg label { display: block; }
.seg input { position: absolute; opacity: 0; pointer-events: none; }
.seg span { display: block; text-align: center; padding: 9px 4px; border-radius: 9px; color: var(--muted); font-size: .92rem; }
.seg input:checked + span { background: var(--card); color: var(--ink); box-shadow: var(--shadow); }
.seg input:focus-visible + span { outline: 2px solid var(--accent); }
.shared-box { display: grid; gap: 10px; padding: 12px; border-radius: 12px; background: var(--bg); }
.shared-box [data-split] { display: grid; gap: 8px; }
.split { display: grid; grid-template-columns: 1fr 110px; gap: 10px; align-items: center; }
@media (max-width: 520px) {
  .modal { margin: auto auto 0; width: 100vw; max-width: 100vw; border-radius: 20px 20px 0 0; }
}

#toast {
  position: fixed; left: 50%; bottom: calc(86px + env(safe-area-inset-bottom)); transform: translate(-50%, 20px); z-index: 99; opacity: 0; pointer-events: none;
  background: var(--ink); color: var(--bg); padding: 10px 16px; border-radius: 12px; font-size: .92rem; font-weight: 600; transition: .2s; max-width: calc(100vw - 32px);
}
#toast.show { opacity: 1; transform: translate(-50%, 0); }
#toast.error { background: var(--neg); color: #fff; }
@media (prefers-reduced-motion: reduce) { #toast { transition: none; } }

/* Rappel du soir */
.reminder-form .status { font-weight: 600; font-size: .92rem; }
.reminder-form input[type="time"] { max-width: 160px; }
.notice { margin-top: 12px; padding: 12px 14px; border-radius: 12px; background: color-mix(in srgb, var(--warn) 14%, transparent); font-size: .92rem; }
