:root {
  color-scheme: light;
  --gold: #f5c451;
  --gold-ink: #0b1f3a;
  --page: #f3f5f9;
  --surface: #ffffff;
  --surface-2: #eceff5;
  --ink: #0b1426;
  --ink-2: #4a5568;
  --muted: #7d8aa0;
  --line: #e0e5ee;
  --border: rgba(11, 20, 38, 0.08);
  --accent: #17406f;
  --accent-ink: #ffffff;
  --accent-soft: #e3ebf5;
  --bar: #2f6fbf;
  --bar-track: #e6eaf2;
  --good: #0a7d0a;
  --good-bar: #0ca30c;
  --warn: #9a6400;
  --warn-bar: #fab219;
  --warn-soft: #fff4dc;
  --crit: #c22f2f;
  --crit-bar: #d03b3b;
  --crit-soft: #fde8e8;
  --shadow: 0 1px 2px rgba(0, 0, 0, 0.04), 0 4px 16px rgba(0, 0, 0, 0.04);
  --radius: 18px;
  --safe-top: env(safe-area-inset-top, 0px);
  --safe-bottom: env(safe-area-inset-bottom, 0px);
}

@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    color-scheme: dark;
    --page: #08121f;
    --surface: #0f1b2d;
    --surface-2: #17263b;
    --ink: #eaf0f8;
    --ink-2: #b9c5d6;
    --muted: #8393aa;
    --line: #1c2b42;
    --border: rgba(255, 255, 255, 0.08);
    --accent: #f5c451;
    --accent-ink: #0b1f3a;
    --accent-soft: #1a3150;
    --bar: #6ba6f0;
    --bar-track: #1c2b42;
    --good: #3cc23c;
    --good-bar: #0ca30c;
    --warn: #fab219;
    --warn-bar: #fab219;
    --warn-soft: #3a2c0c;
    --crit: #ff7a7a;
    --crit-bar: #d03b3b;
    --crit-soft: #3d1717;
    --shadow: none;
  }
}
:root[data-theme="dark"] {
  color-scheme: dark;
  --page: #08121f;
  --surface: #0f1b2d;
  --surface-2: #17263b;
  --ink: #eaf0f8;
  --ink-2: #b9c5d6;
  --muted: #8393aa;
  --line: #1c2b42;
  --border: rgba(255, 255, 255, 0.08);
  --accent: #f5c451;
  --accent-ink: #0b1f3a;
  --accent-soft: #1a3150;
  --bar: #6ba6f0;
  --bar-track: #1c2b42;
  --good: #3cc23c;
  --good-bar: #0ca30c;
  --warn: #fab219;
  --warn-bar: #fab219;
  --warn-soft: #3a2c0c;
  --crit: #ff7a7a;
  --crit-bar: #d03b3b;
  --crit-soft: #3d1717;
  --shadow: none;
}

* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
html, body { margin: 0; padding: 0; }
body {
  background: var(--page);
  color: var(--ink);
  font: 16px/1.4 system-ui, -apple-system, "Segoe UI", sans-serif;
  -webkit-text-size-adjust: 100%;
  min-height: 100dvh;
  overscroll-behavior-y: none;
}
button, input, select, textarea { font: inherit; color: inherit; }
button { cursor: pointer; background: none; border: 0; padding: 0; }
[hidden] { display: none !important; }

/* ---------- Top bar ---------- */
.topbar {
  position: sticky; top: 0; z-index: 10;
  display: flex; align-items: center; justify-content: space-between;
  padding: calc(var(--safe-top) + 8px) 12px 8px;
  background: color-mix(in srgb, var(--page) 88%, transparent);
  -webkit-backdrop-filter: blur(14px); backdrop-filter: blur(14px);
}
.icon-btn {
  width: 40px; height: 40px; border-radius: 50%;
  font-size: 26px; line-height: 1; color: var(--ink-2);
  display: grid; place-items: center;
}
.icon-btn:active { background: var(--surface-2); }
.period { text-align: center; }
.period-label { font-weight: 650; font-size: 17px; text-transform: capitalize; }
.period-today { font-size: 12px; color: var(--accent); font-weight: 600; margin-top: 2px; }

/* ---------- Layout ---------- */
.view {
  max-width: 560px; margin: 0 auto;
  padding: 4px 16px calc(var(--safe-bottom) + 150px);
}
.card {
  background: var(--surface); border-radius: var(--radius);
  box-shadow: var(--shadow); border: 1px solid var(--border);
  padding: 16px; margin-bottom: 12px;
}
.section-title {
  display: flex; justify-content: space-between; align-items: baseline;
  font-size: 13px; font-weight: 650; text-transform: uppercase; letter-spacing: .04em;
  color: var(--muted); margin: 20px 4px 8px;
}
.section-title { gap: 10px; }
.section-title button { white-space: nowrap; color: var(--accent); text-transform: none; letter-spacing: 0; font-size: 14px; font-weight: 600; }
.muted { color: var(--muted); }
.small { font-size: 13px; }
.num { font-variant-numeric: tabular-nums; white-space: nowrap; }

/* ---------- Hero ---------- */
.hero { padding: 20px; }
.hero-label { font-size: 14px; color: var(--ink-2); font-weight: 550; }
.hero-amount { font-size: 44px; font-weight: 750; letter-spacing: -0.02em; line-height: 1.1; margin: 4px 0 2px; }
.hero-amount.neg { color: var(--crit); }
.hero-sub { font-size: 15px; color: var(--ink-2); }
.hero-sub strong { color: var(--ink); }
.perday {
  display: flex; align-items: center; gap: 12px;
  margin-top: 14px; padding: 12px 14px; border-radius: 14px;
  background: var(--accent-soft);
}
.perday.warn { background: var(--warn-soft); }
.perday.crit { background: var(--crit-soft); }
.perday-amount { font-size: 26px; font-weight: 750; }
.perday-text { font-size: 13px; color: var(--ink-2); line-height: 1.3; }

.progress { height: 8px; background: var(--bar-track); border-radius: 99px; overflow: hidden; margin-top: 14px; }
.progress > span { display: block; height: 100%; border-radius: 99px; background: var(--good-bar); }
.progress.warn > span { background: var(--warn-bar); }
.progress.crit > span { background: var(--crit-bar); }
.progress-legend { display: flex; justify-content: space-between; font-size: 12px; color: var(--muted); margin-top: 6px; }

.kpis { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; margin-bottom: 12px; }
.kpi { background: var(--surface); border: 1px solid var(--border); box-shadow: var(--shadow); border-radius: 14px; padding: 12px; }
.kpi-label { font-size: 12px; color: var(--muted); font-weight: 550; }
.kpi-value { font-size: 18px; font-weight: 700; margin-top: 2px; }

/* ---------- Alerts ---------- */
.alert {
  display: flex; gap: 10px; align-items: flex-start;
  padding: 12px 14px; border-radius: 14px; margin-bottom: 8px; font-size: 14px;
}
.alert.warn { background: var(--warn-soft); }
.alert.crit { background: var(--crit-soft); }
.alert.info { background: var(--surface-2); }
.alert.good { background: var(--accent-soft); }
.alert b { font-weight: 650; }
.alert .ai { font-size: 18px; line-height: 1.2; }

/* ---------- Lists ---------- */
.list { background: var(--surface); border-radius: var(--radius); border: 1px solid var(--border); box-shadow: var(--shadow); overflow: hidden; }
.row {
  display: flex; align-items: center; gap: 12px;
  padding: 12px 14px; width: 100%; text-align: left;
  border-bottom: 1px solid var(--line);
}
.row:last-child { border-bottom: 0; }
button.row:active { background: var(--surface-2); }
.row-icon {
  width: 38px; height: 38px; border-radius: 12px; flex: none;
  display: grid; place-items: center; font-size: 19px; background: var(--surface-2);
}
.row-main { flex: 1; min-width: 0; }
.row-title { font-weight: 560; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.row-sub { font-size: 13px; color: var(--muted); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.row-amount { font-weight: 650; font-variant-numeric: tabular-nums; white-space: nowrap; }
.row-amount.in { color: var(--good); }
.empty { padding: 28px 16px; text-align: center; color: var(--muted); font-size: 14px; }
.day-head { font-size: 13px; font-weight: 600; color: var(--muted); margin: 16px 4px 6px; text-transform: capitalize; }
.tag { display: inline-block; font-size: 11px; font-weight: 650; padding: 2px 7px; border-radius: 99px; background: var(--surface-2); color: var(--ink-2); vertical-align: 1px; }
.tag.done { background: var(--accent-soft); color: var(--accent); }

/* ---------- Envelopes ---------- */
.env { padding: 14px; border-bottom: 1px solid var(--line); width: 100%; text-align: left; display: block; }
.env:last-child { border-bottom: 0; }
.env:active { background: var(--surface-2); }
.env-top { display: flex; align-items: center; gap: 10px; }
.env-name { flex: 1; font-weight: 600; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.env-left { font-weight: 700; font-variant-numeric: tabular-nums; }
.env-left.warn { color: var(--warn); }
.env-left.crit { color: var(--crit); }
.env .progress { margin-top: 10px; height: 7px; }
.env-meta { display: flex; justify-content: space-between; font-size: 12px; color: var(--muted); margin-top: 6px; }

/* ---------- Segmented / chips ---------- */
.seg { display: flex; background: var(--surface-2); border-radius: 12px; padding: 3px; margin: 4px 0 12px; }
.seg button { flex: 1; padding: 8px 6px; border-radius: 9px; font-size: 14px; font-weight: 600; color: var(--ink-2); }
.seg button.on { background: var(--surface); color: var(--ink); box-shadow: 0 1px 3px rgba(0,0,0,.12); }
.chips { display: flex; gap: 8px; overflow-x: auto; padding: 2px 0 10px; margin: 0 -16px; padding-left: 16px; padding-right: 16px; scrollbar-width: none; }
.chips::-webkit-scrollbar { display: none; }
.chip { flex: none; padding: 7px 12px; border-radius: 99px; background: var(--surface); border: 1px solid var(--border); font-size: 14px; white-space: nowrap; }
.chip.on { background: var(--ink); color: var(--page); border-color: var(--ink); }

/* ---------- Charts ---------- */
.hbar { margin-bottom: 12px; }
.hbar:last-child { margin-bottom: 0; }
.hbar-top { display: flex; justify-content: space-between; gap: 8px; font-size: 14px; margin-bottom: 5px; }
.hbar-track { height: 10px; border-radius: 4px; background: var(--bar-track); overflow: hidden; }
.hbar-fill { height: 100%; background: var(--bar); border-radius: 0 4px 4px 0; }
.vchart { display: flex; align-items: flex-end; gap: 10px; height: 160px; padding-top: 18px; border-bottom: 1px solid var(--line); }
.vcol { flex: 1; height: 100%; display: flex; flex-direction: column; justify-content: flex-end; align-items: center; position: relative; }
.vbar { width: 100%; max-width: 34px; background: var(--bar); border-radius: 4px 4px 0 0; min-height: 2px; }
.vcol.current .vbar { background: var(--accent); }
.vcol.over .vbar { background: var(--crit-bar); }
.vval { font-size: 11px; color: var(--ink-2); margin-bottom: 4px; font-variant-numeric: tabular-nums; white-space: nowrap; }
.vlabels { display: flex; gap: 10px; margin-top: 6px; }
.vlabels span { flex: 1; text-align: center; font-size: 11px; color: var(--muted); text-transform: capitalize; }
.legend { display: flex; gap: 14px; flex-wrap: wrap; font-size: 12px; color: var(--ink-2); margin-top: 10px; }
.legend i { display: inline-block; width: 10px; height: 10px; border-radius: 3px; margin-right: 5px; vertical-align: -1px; }
.stat-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; }
.tooltip {
  position: fixed; z-index: 60; pointer-events: none;
  background: var(--ink); color: var(--page); font-size: 13px;
  padding: 6px 10px; border-radius: 8px; max-width: 220px;
  transform: translate(-50%, -110%);
}

/* ---------- Goals ---------- */
.goal-pct { font-size: 12px; color: var(--muted); }

/* ---------- FAB & tab bar ---------- */
.fab {
  position: fixed; z-index: 20; right: 18px;
  bottom: calc(var(--safe-bottom) + 76px);
  width: 60px; height: 60px; border-radius: 50%;
  background: var(--gold); color: var(--gold-ink);
  font-size: 34px; font-weight: 400; line-height: 1;
  box-shadow: 0 6px 20px rgba(0,0,0,.25);
  display: grid; place-items: center;
}
.fab:active { transform: scale(.94); }
.tabbar {
  position: fixed; z-index: 15; left: 0; right: 0; bottom: 0;
  display: flex; justify-content: space-around;
  padding: 6px 4px calc(var(--safe-bottom) + 6px);
  background: color-mix(in srgb, var(--surface) 90%, transparent);
  -webkit-backdrop-filter: blur(14px); backdrop-filter: blur(14px);
  border-top: 1px solid var(--line);
}
.tabbar button {
  flex: 1; display: flex; flex-direction: column; align-items: center; gap: 1px;
  font-size: 10.5px; font-weight: 600; color: var(--muted); padding: 4px 0;
}
.tabbar .ti { font-size: 21px; filter: grayscale(1); opacity: .65; }
.tabbar button.active { color: var(--ink); }
.tabbar button.active .ti { filter: none; opacity: 1; }

/* ---------- Sheet ---------- */
.sheet-backdrop { position: fixed; inset: 0; z-index: 40; background: rgba(0,0,0,.4); animation: fade .2s; }
.sheet {
  position: fixed; z-index: 50; left: 0; right: 0; bottom: 0;
  max-width: 560px; margin: 0 auto;
  max-height: calc(100dvh - var(--safe-top) - 20px); overflow-y: auto;
  background: var(--surface); border-radius: 22px 22px 0 0;
  padding: 8px 16px calc(var(--safe-bottom) + 20px);
  animation: up .25s cubic-bezier(.2,.8,.2,1);
}
@keyframes up { from { transform: translateY(100%); } }
@keyframes fade { from { opacity: 0; } }
.sheet-grip { width: 38px; height: 5px; border-radius: 9px; background: var(--line); margin: 0 auto 10px; }
.sheet-head { display: flex; justify-content: space-between; align-items: center; margin-bottom: 12px; }
.sheet-head h2 { font-size: 19px; margin: 0; }
.sheet-head button { color: var(--muted); font-size: 15px; font-weight: 600; }

.field { margin-bottom: 14px; }
.field > label, .field-label { display: block; font-size: 13px; font-weight: 600; color: var(--ink-2); margin-bottom: 6px; }
.input {
  width: 100%; padding: 12px 14px; border-radius: 12px;
  background: var(--surface-2); border: 1px solid transparent; font-size: 16px; outline: none;
  -webkit-appearance: none; appearance: none;
}
.input:focus { border-color: var(--accent); }
.amount-input { font-size: 34px; font-weight: 700; text-align: center; padding: 14px; }
.field-row { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; }
.field-row.emoji { grid-template-columns: 72px 1fr; }
.emoji-input { text-align: center; font-size: 22px; padding: 9px; }
.hint { font-size: 12px; color: var(--muted); margin-top: 5px; }
.cat-grid { display: grid; grid-template-columns: repeat(4, 1fr); gap: 8px; }
.cat-opt {
  display: flex; flex-direction: column; align-items: center; gap: 2px;
  padding: 9px 4px; border-radius: 12px; background: var(--surface-2);
  font-size: 11.5px; border: 2px solid transparent; min-width: 0;
}
.cat-opt span:first-child { font-size: 22px; }
.cat-opt span:last-child { max-width: 100%; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.cat-opt.on { border-color: var(--accent); background: var(--accent-soft); }
.btn {
  display: block; width: 100%; padding: 15px; border-radius: 14px;
  background: var(--accent); color: var(--accent-ink); font-weight: 700; font-size: 16px; text-align: center;
}
.btn:active { opacity: .85; }
.btn.secondary { background: var(--surface-2); color: var(--ink); }
.btn.danger { background: transparent; color: var(--crit); margin-top: 6px; }
.btn + .btn { margin-top: 8px; }
.toast {
  position: fixed; z-index: 70; left: 50%; top: calc(var(--safe-top) + 12px);
  transform: translate(-50%, -150%); transition: transform .3s cubic-bezier(.2,.8,.2,1);
  background: var(--ink); color: var(--page); padding: 11px 16px; border-radius: 14px;
  font-size: 14px; font-weight: 550; max-width: calc(100% - 32px); width: max-content;
  box-shadow: 0 8px 24px rgba(0,0,0,.25);
}
.toast.show { transform: translate(-50%, 0); }

.setting-row { display: flex; justify-content: space-between; align-items: center; gap: 12px; padding: 12px 14px; border-bottom: 1px solid var(--line); }
.setting-row:last-child { border-bottom: 0; }
.setting-row select { background: var(--surface-2); border: 0; border-radius: 10px; padding: 8px 10px; font-size: 15px; }
.help { font-size: 14px; color: var(--ink-2); line-height: 1.5; }
.help ol { padding-left: 20px; margin: 8px 0 0; }

/* ---------- Blocs repliables & liens ---------- */
details.fold { padding: 0; }
details.fold > summary { list-style: none; cursor: pointer; padding: 16px; font-weight: 650; display: flex; justify-content: space-between; align-items: center; }
details.fold > summary::-webkit-details-marker { display: none; }
details.fold > summary::after { content: '›'; font-size: 22px; color: var(--muted); transition: transform .2s; }
details.fold[open] > summary::after { transform: rotate(90deg); }
.fold-body { padding: 0 16px 16px; }
.link-btn { display: block; margin: 2px auto 8px; padding: 6px 12px; color: var(--accent); font-weight: 600; font-size: 14px; }
