/* Budget : feuille de style unique, mobile first, thème clair / sombre automatique. */

:root {
  --bg: #f4f5f7;
  --surface: #ffffff;
  --surface-2: #eef0f3;
  --text: #16181d;
  --muted: #646b78;
  --border: #e1e4e9;
  --primary: #2563eb;
  --primary-text: #ffffff;
  --income: #15803d;
  --expense: #c2410c;
  --danger: #dc2626;
  --success-bg: #dcfce7;
  --success-text: #14532d;
  --error-bg: #fee2e2;
  --error-text: #7f1d1d;
  --radius: 14px;
  --shadow: 0 1px 2px rgb(0 0 0 / 0.05), 0 1px 8px rgb(0 0 0 / 0.04);
  --tabbar-height: 64px;
  --safe-bottom: env(safe-area-inset-bottom, 0px);
  color-scheme: light dark;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0f1115;
    --surface: #181b21;
    --surface-2: #22262e;
    --text: #e8eaee;
    --muted: #9aa1ad;
    --border: #2a2f38;
    --primary: #4f8cff;
    --income: #4ade80;
    --expense: #fb923c;
    --danger: #f87171;
    --success-bg: #14532d;
    --success-text: #dcfce7;
    --error-bg: #7f1d1d;
    --error-text: #fee2e2;
    --shadow: none;
  }
}

*, *::before, *::after { box-sizing: border-box; }

body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font: 16px/1.45 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  -webkit-font-smoothing: antialiased;
  -webkit-tap-highlight-color: transparent;
}
body.has-tabbar { padding-bottom: calc(var(--tabbar-height) + var(--safe-bottom) + 16px); }

a { color: inherit; text-decoration: none; }
h1, h2, h3 { margin: 0; line-height: 1.2; }
small { font-size: 0.82rem; }

.container { max-width: 640px; margin: 0 auto; padding: 16px; display: grid; gap: 16px; }
.muted { color: var(--muted); }
.center { text-align: center; }
.hint { color: var(--muted); font-size: 0.85rem; margin: 0; }
.link { color: var(--primary); font-weight: 500; font-size: 0.9rem; }

/* --- Barre du haut --- */
.topbar {
  position: sticky; top: 0; z-index: 10;
  display: flex; align-items: center; gap: 12px;
  padding: 12px 16px;
  background: color-mix(in srgb, var(--bg) 85%, transparent);
  backdrop-filter: blur(10px);
}
.brand { font-weight: 700; font-size: 1.15rem; }
.back { color: var(--primary); font-weight: 500; }
.topbar-title { font-weight: 600; margin-left: auto; margin-right: auto; padding-right: 60px; }

/* --- Barre d'onglets (mobile) --- */
.tabbar {
  position: fixed; inset: auto 0 0 0; z-index: 20;
  display: grid; grid-template-columns: 1fr 1fr 72px 1fr; align-items: center;
  height: calc(var(--tabbar-height) + var(--safe-bottom));
  padding-bottom: var(--safe-bottom);
  background: var(--surface);
  border-top: 1px solid var(--border);
}
.tab {
  display: grid; justify-items: center; gap: 2px;
  font-size: 0.72rem; color: var(--muted);
}
.tab.active { color: var(--primary); }
.tabbar svg { width: 24px; height: 24px; fill: none; stroke: currentColor; stroke-width: 2; stroke-linecap: round; stroke-linejoin: round; }
.fab {
  justify-self: center; display: grid; place-items: center;
  width: 56px; height: 56px; margin-top: -28px;
  border-radius: 50%; background: var(--primary); color: var(--primary-text);
  box-shadow: 0 4px 14px rgb(37 99 235 / 0.4);
}
.fab svg { width: 28px; height: 28px; stroke-width: 2.5; }

/* --- Cartes et listes --- */
.card {
  background: var(--surface); border-radius: var(--radius);
  box-shadow: var(--shadow); padding: 16px;
}
.card-header, .section-header { display: flex; justify-content: space-between; align-items: baseline; margin-bottom: 8px; }
.card-title { font-size: 1rem; }
.page-title { font-size: 1.6rem; }
.section-title { font-size: 0.85rem; text-transform: uppercase; letter-spacing: 0.04em; color: var(--muted); margin: 8px 4px; }
.empty { text-align: center; padding: 24px 16px; }

.list { list-style: none; margin: 0; padding: 0; }
.list.card { padding: 4px 16px; }
.list > li + li { border-top: 1px solid var(--border); }
.row { display: flex; align-items: center; gap: 12px; padding: 12px 0; min-height: 56px; }
.row-main { flex: 1; min-width: 0; display: grid; }
.row-main strong, .row-main small { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.row-main small { color: var(--muted); }
.row.is-archived { opacity: 0.55; }
.chevron { color: var(--muted); font-size: 1.3rem; }

.icon-badge {
  flex: none; display: grid; place-items: center;
  width: 38px; height: 38px; border-radius: 12px; font-size: 1.1rem;
  background: color-mix(in srgb, var(--badge) 18%, transparent);
  color: var(--badge);
}
.tag {
  display: inline-block; margin-left: 6px; padding: 1px 8px; border-radius: 99px;
  font-size: 0.72rem; font-weight: 500; vertical-align: middle;
  background: color-mix(in srgb, var(--primary) 15%, transparent); color: var(--primary);
}
.tag-muted { background: var(--surface-2); color: var(--muted); }

.amount { font-variant-numeric: tabular-nums; font-weight: 600; white-space: nowrap; }
.amount-income { color: var(--income); }
.amount-expense { color: var(--expense); }
.amount-transfer { color: var(--muted); }

/* --- Accueil --- */
.summary { display: grid; gap: 14px; }
.summary-grid { display: grid; grid-template-columns: repeat(3, 1fr); gap: 8px; }
.summary-grid > div { display: grid; }
.summary-grid strong { font-size: 1.05rem; font-variant-numeric: tabular-nums; }

/* --- Liste des opérations --- */
.filters { display: flex; gap: 8px; overflow-x: auto; padding-bottom: 2px; }
.filters select { flex: 1 0 auto; width: auto; min-width: 0; padding: 8px 10px; font-size: 0.9rem; }
.month-nav { display: flex; align-items: center; justify-content: space-between; }
.month-nav a {
  display: grid; place-items: center; width: 40px; height: 40px;
  border-radius: 50%; background: var(--surface); font-size: 1.5rem; box-shadow: var(--shadow);
}
.totals { display: flex; justify-content: space-around; color: var(--muted); font-size: 0.9rem; }
.day { display: grid; gap: 6px; margin-top: 4px; }
.day-title { font-size: 0.85rem; color: var(--muted); font-weight: 600; margin-left: 4px; }

/* --- Formulaires --- */
.stack { display: grid; gap: 16px; }
.field { display: grid; gap: 6px; font-size: 0.9rem; font-weight: 500; }
.field-row { display: grid; grid-template-columns: 1fr 1fr; gap: 12px; }
.field-error { color: var(--danger); font-size: 0.82rem; font-weight: 400; }

input, select, button { font: inherit; color: inherit; }
input:not([type=radio], [type=checkbox], [type=color]), select {
  width: 100%; padding: 12px; min-height: 46px;
  background: var(--surface); border: 1px solid var(--border); border-radius: 10px;
}
input:focus-visible, select:focus-visible, button:focus-visible, a:focus-visible {
  outline: 2px solid var(--primary); outline-offset: 2px;
}
input[type=color] { width: 100%; height: 46px; padding: 4px; border: 1px solid var(--border); border-radius: 10px; background: var(--surface); }
input[aria-invalid=true] { border-color: var(--danger); }

.amount-input { position: relative; display: block; }
.amount-input > span { position: absolute; right: 14px; top: 50%; translate: 0 -50%; color: var(--muted); }
.amount-field input { font-size: 2rem; font-weight: 700; text-align: center; padding: 14px 40px; }

.checkbox { display: flex; gap: 10px; align-items: flex-start; font-size: 0.9rem; }
.checkbox input { width: 20px; height: 20px; margin: 1px 0 0; accent-color: var(--primary); }

/* Contrôle segmenté (radios déguisés en onglets). */
.segmented {
  display: grid; grid-auto-flow: column; grid-auto-columns: 1fr;
  margin: 0; padding: 4px; border: 0; border-radius: 12px; background: var(--surface-2);
}
.segmented label, .chip { position: relative; }
.segmented input { position: absolute; opacity: 0; pointer-events: none; }
.segmented span { display: block; padding: 9px 4px; border-radius: 9px; text-align: center; font-weight: 500; font-size: 0.92rem; cursor: pointer; }
.segmented input:checked + span { background: var(--surface); box-shadow: var(--shadow); color: var(--primary); }
.segmented input:focus-visible + span { outline: 2px solid var(--primary); }

/* Catégories sous forme de pastilles cliquables. */
.chips { display: flex; flex-wrap: wrap; gap: 8px; }
.chip input { position: absolute; opacity: 0; pointer-events: none; }
.chip span {
  display: inline-block; padding: 8px 12px; border-radius: 99px; cursor: pointer;
  background: var(--surface); border: 1px solid var(--border); font-weight: 400; font-size: 0.9rem;
}
.chip input:checked + span {
  border-color: var(--badge); color: var(--text);
  background: color-mix(in srgb, var(--badge) 20%, var(--surface));
  font-weight: 600;
}
.chip input:focus-visible + span { outline: 2px solid var(--primary); }

/* Champs affichés selon le type d'opération choisi. */
.tx-form:not(:has([name=kind][value=expense]:checked)) .only-expense,
.tx-form:not(:has([name=kind][value=income]:checked)) .only-income,
.tx-form:not(:has([name=kind][value=transfer]:checked)) .only-transfer,
.tx-form:has([name=kind][value=transfer]:checked) .not-transfer { display: none; }

/* --- Boutons --- */
.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 6px;
  min-height: 46px; padding: 10px 18px; border-radius: 12px; cursor: pointer;
  border: 1px solid var(--border); background: var(--surface); font-weight: 600;
}
.btn-primary { background: var(--primary); border-color: var(--primary); color: var(--primary-text); }
.btn-danger { color: var(--danger); }
.btn-block { width: 100%; }
.actions { display: flex; gap: 12px; flex-wrap: wrap; }
.actions > * { flex: 1; }
.actions form .btn { width: 100%; }
.danger-zone { margin-top: 8px; }

/* --- Messages --- */
.flash { padding: 12px 14px; border-radius: 12px; font-size: 0.92rem; }
.flash-success { background: var(--success-bg); color: var(--success-text); }
.flash-error { background: var(--error-bg); color: var(--error-text); }

/* --- Connexion --- */
.login { display: grid; gap: 12px; padding-top: 12vh; text-align: center; }
.login .card { text-align: left; }
.login-title { font-size: 2.2rem; }

/* --- Écrans larges --- */
@media (min-width: 760px) {
  .container { padding-top: 24px; }
  .tabbar {
    inset: 0 auto 0 0; width: 96px; height: auto;
    grid-template-columns: 1fr; grid-auto-rows: 72px; align-content: start; padding-top: 16px;
    border-top: 0; border-right: 1px solid var(--border);
  }
  .fab { order: -1; margin: 0 0 12px; }
  body.has-tabbar { padding-bottom: 0; padding-left: 96px; }
}
