/* ============ Couleurs et réglages ============ */
:root {
  color-scheme: light;
  --fond: #f7f3f1;
  --surface: #ffffff;
  --surface-2: #f1ebe8;
  --texte: #1f1a1b;
  --texte-2: #5d5557;
  --texte-3: #8a8183;
  --bordure: rgba(31, 26, 27, 0.09);
  --accent: #d9334a;
  --accent-fort: #bf2238;
  --accent-texte: #ffffff;
  --accent-doux: #fbe5e8;
  --ok: #1baf7a;
  --ok-texte: #0b7a52;
  --attention: #e39a00;
  --attention-texte: #93600a;
  --depasse: #d03b3b;
  --depasse-texte: #b42727;
  --positif: #2a78d6;
  --negatif: #e34948;
  --grille: #e8e1dd;
  --ombre: 0 1px 2px rgba(31, 26, 27, 0.05), 0 6px 20px rgba(31, 26, 27, 0.06);
  --rayon: 18px;
  --rayon-petit: 12px;
  --hauteur-onglets: 64px;
  --police: system-ui, -apple-system, "SF Pro Text", "Segoe UI", Roboto, sans-serif;
}

@media (prefers-color-scheme: dark) {
  :root {
    color-scheme: dark;
    --fond: #121011;
    --surface: #1d1a1b;
    --surface-2: #2a2526;
    --texte: #f4eff0;
    --texte-2: #c9c0c2;
    --texte-3: #93898b;
    --bordure: rgba(255, 255, 255, 0.09);
    --accent: #ff5468;
    --accent-fort: #ff7082;
    --accent-texte: #1d0b0e;
    --accent-doux: #3b1d22;
    --ok: #199e70;
    --ok-texte: #4fd6a4;
    --attention: #c98500;
    --attention-texte: #f2bd52;
    --depasse: #e66767;
    --depasse-texte: #ff8a8a;
    --positif: #3987e5;
    --negatif: #e66767;
    --grille: #2f2a2b;
    --ombre: 0 1px 2px rgba(0, 0, 0, 0.3), 0 6px 20px rgba(0, 0, 0, 0.25);
  }
}

/* ============ Base ============ */
*, *::before, *::after { box-sizing: border-box; }
html { -webkit-text-size-adjust: 100%; text-size-adjust: 100%; }
body {
  margin: 0;
  font-family: var(--police);
  font-size: 16px;
  line-height: 1.45;
  color: var(--texte);
  background: var(--fond);
  -webkit-font-smoothing: antialiased;
  -webkit-tap-highlight-color: transparent;
}
button, input, select, textarea { font: inherit; color: inherit; }
button { cursor: pointer; }
[hidden] { display: none !important; }
h1, h2, h3, p { margin: 0; }
svg { display: block; }
.nombres { font-variant-numeric: tabular-nums; }
.discret { color: var(--texte-3); }
.visuellement-cache {
  position: absolute; width: 1px; height: 1px; overflow: hidden;
  clip: rect(0 0 0 0); white-space: nowrap;
}
:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }

/* ============ Chargement & connexion ============ */
.chargement {
  position: fixed; inset: 0; display: grid; place-items: center; background: var(--fond);
}
.chargement img { animation: battement 1.2s ease-in-out infinite; }
@keyframes battement { 0%, 100% { transform: scale(1); } 50% { transform: scale(1.08); } }

.connexion {
  min-height: 100dvh; display: grid; place-items: center;
  padding: 24px 16px calc(24px + env(safe-area-inset-bottom));
}
.connexion-carte {
  width: 100%; max-width: 380px; background: var(--surface); border-radius: 24px;
  padding: 32px 24px 24px; box-shadow: var(--ombre); display: grid; gap: 16px;
}
.connexion-logo { margin: 0 auto; }
.connexion h1 { text-align: center; font-size: 26px; letter-spacing: -0.01em; margin-bottom: 4px; }

/* ============ Champs et boutons ============ */
.champ { display: grid; gap: 6px; }
.champ > span { font-size: 14px; font-weight: 600; color: var(--texte-2); }
.champ input, .champ select, .champ textarea, .saisie {
  width: 100%; min-height: 48px; padding: 12px 14px; font-size: 16px;
  background: var(--surface-2); border: 1.5px solid transparent; border-radius: var(--rayon-petit);
  outline: none; appearance: none; -webkit-appearance: none;
}
.champ textarea { min-height: 110px; resize: vertical; line-height: 1.4; }
.champ input[type="date"] { display: block; text-align: left; }
.champ input[type="date"]::-webkit-date-and-time-value { text-align: left; }
.champ input:focus, .champ select:focus, .champ textarea:focus { border-color: var(--accent); background: var(--surface); }
.champ select {
  background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24' fill='none' stroke='%238a8183' stroke-width='2' stroke-linecap='round'%3E%3Cpath d='M6 9l6 6 6-6'/%3E%3C/svg%3E");
  background-repeat: no-repeat; background-position: right 12px center; background-size: 18px;
  padding-right: 38px;
}
.champ-aide, .champ > .champ-aide { font-size: 13px; font-weight: 400; color: var(--texte-3); }
.champ-montant input { font-size: 30px; font-weight: 700; min-height: 64px; letter-spacing: -0.01em; }
.ligne-champs { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; }
.case { display: flex; align-items: center; gap: 10px; min-height: 44px; font-weight: 500; }
.case input { width: 22px; height: 22px; accent-color: var(--accent); }

.erreur { color: var(--depasse-texte); font-size: 14px; min-height: 1em; }
.erreur:empty { display: none; }

.bouton {
  display: inline-flex; align-items: center; justify-content: center; gap: 8px;
  min-height: 48px; padding: 0 20px; border-radius: 999px; border: 0;
  font-weight: 650; font-size: 16px; background: var(--surface-2); color: var(--texte);
  transition: transform 0.1s ease, background 0.15s ease;
}
.bouton:active { transform: scale(0.97); }
.bouton:disabled { opacity: 0.5; cursor: default; }
.bouton-principal { background: var(--accent); color: var(--accent-texte); }
.bouton-principal:hover { background: var(--accent-fort); }
.bouton-doux { background: var(--accent-doux); color: var(--accent-fort); }
.bouton-danger { background: transparent; color: var(--depasse-texte); }
.bouton-large { width: 100%; }
.bouton-petit { min-height: 36px; padding: 0 14px; font-size: 14px; }
.bouton svg { width: 20px; height: 20px; fill: none; stroke: currentColor; stroke-width: 2; stroke-linecap: round; stroke-linejoin: round; }

.lien {
  background: none; border: 0; padding: 6px 2px; color: var(--accent-fort);
  font-weight: 600; font-size: 15px;
}

/* ============ Navigation ============ */
.onglets {
  position: fixed; left: 0; right: 0; bottom: 0; z-index: 20;
  display: grid; grid-template-columns: repeat(5, 1fr);
  padding: 6px 6px calc(6px + env(safe-area-inset-bottom));
  background: color-mix(in srgb, var(--surface) 92%, transparent);
  -webkit-backdrop-filter: blur(16px); backdrop-filter: blur(16px);
  border-top: 1px solid var(--bordure);
}
.onglet {
  display: grid; justify-items: center; gap: 2px; padding: 6px 0 4px;
  background: none; border: 0; border-radius: 12px; color: var(--texte-3);
  font-size: 11.5px; font-weight: 600;
}
.onglet svg { width: 24px; height: 24px; fill: none; stroke: currentColor; stroke-width: 1.8; stroke-linecap: round; stroke-linejoin: round; }
.onglet[aria-current="page"] { color: var(--accent); }

main {
  max-width: 760px; margin: 0 auto;
  padding: calc(8px + env(safe-area-inset-top)) 16px calc(var(--hauteur-onglets) + 96px + env(safe-area-inset-bottom));
  outline: none;
}

.fab {
  position: fixed; z-index: 21;
  right: max(16px, calc((100vw - 760px) / 2 + 16px));
  bottom: calc(var(--hauteur-onglets) + 16px + env(safe-area-inset-bottom));
  width: 60px; height: 60px; border-radius: 50%; border: 0;
  background: var(--accent); color: var(--accent-texte);
  display: grid; place-items: center;
  box-shadow: 0 6px 18px color-mix(in srgb, var(--accent) 40%, transparent);
  transition: transform 0.12s ease;
}
.fab:active { transform: scale(0.94); }
.fab svg { width: 28px; height: 28px; fill: none; stroke: currentColor; stroke-width: 2.4; stroke-linecap: round; }

@media (min-width: 900px) {
  .onglets {
    position: sticky; top: 0; bottom: auto; border-top: 0; border-bottom: 1px solid var(--bordure);
    display: flex; justify-content: center; gap: 4px; padding: 8px 16px;
  }
  .onglet { display: flex; align-items: center; gap: 8px; padding: 8px 16px; font-size: 15px; }
  .onglet svg { width: 20px; height: 20px; }
  .onglet[aria-current="page"] { background: var(--accent-doux); color: var(--accent-fort); }
  .onglet:hover:not([aria-current="page"]) { background: var(--surface-2); }
  main { padding-top: 16px; padding-bottom: 120px; }
  .fab { bottom: 32px; }
}

/* ============ En-têtes de vue ============ */
.entete {
  display: flex; align-items: center; justify-content: space-between; gap: 8px;
  min-height: 56px; margin-bottom: 8px;
}
.entete h1 { font-size: 26px; letter-spacing: -0.015em; line-height: 1.2; }
.navigation-periode { display: flex; align-items: center; gap: 4px; width: 100%; }
.navigation-periode h1 { flex: 1; text-align: center; }
.bouton-rond {
  width: 44px; height: 44px; border-radius: 50%; border: 0; flex: none;
  display: grid; place-items: center; background: var(--surface); color: var(--texte);
  box-shadow: var(--ombre);
}
.bouton-rond:disabled { opacity: 0.35; cursor: default; }
.bouton-rond svg { width: 22px; height: 22px; fill: none; stroke: currentColor; stroke-width: 2.2; stroke-linecap: round; stroke-linejoin: round; }
.pastille-retour { display: flex; justify-content: center; margin: -4px 0 8px; }
.periode-petite { flex: 1; text-align: center; font-weight: 650; color: var(--texte-2); }
.entete + .entete { margin-top: -4px; }

.section { margin-top: 28px; }
.section-titre {
  display: flex; align-items: baseline; justify-content: space-between; gap: 12px;
  margin: 0 4px 10px;
}
.section-titre h2 { font-size: 19px; letter-spacing: -0.01em; }
.section-titre .discret { font-size: 14px; }

/* ============ Cartes ============ */
.carte {
  background: var(--surface); border-radius: var(--rayon); box-shadow: var(--ombre);
  padding: 18px;
}
.carte + .carte { margin-top: 12px; }

.resume-titre { font-size: 15px; font-weight: 600; color: var(--texte-2); }
.resume-montant {
  font-size: 42px; font-weight: 750; letter-spacing: -0.025em; line-height: 1.1; margin: 4px 0 2px;
}
.resume-montant.negatif { color: var(--depasse-texte); }
.resume-sous { font-size: 14px; color: var(--texte-3); }
.resume-lignes { margin: 14px 0 0; padding: 12px 0 0; border-top: 1px solid var(--bordure); display: grid; gap: 6px; }
.resume-ligne { display: flex; justify-content: space-between; font-size: 15px; color: var(--texte-2); }
.resume-ligne strong { color: var(--texte); font-weight: 600; }
.resume-epargne {
  margin-top: 14px; padding: 12px 14px; border-radius: var(--rayon-petit); background: var(--accent-doux);
  display: flex; align-items: center; justify-content: space-between; gap: 12px; font-size: 15px;
}
.resume-epargne strong { color: var(--accent-fort); }
.resume-actions { margin-top: 14px; }

/* Budgets */
.grille-budgets { display: grid; gap: 10px; }
@media (min-width: 600px) { .grille-budgets { grid-template-columns: 1fr 1fr; } }
.budget {
  width: 100%; text-align: left; border: 0; display: grid; gap: 10px;
  background: var(--surface); border-radius: var(--rayon); box-shadow: var(--ombre); padding: 16px;
}
.budget:active { transform: scale(0.99); }
.budget-haut { display: flex; align-items: center; gap: 12px; }
.budget-icone {
  width: 42px; height: 42px; flex: none; border-radius: 12px; background: var(--surface-2);
  display: grid; place-items: center; font-size: 22px;
}
.budget-nom { flex: 1; min-width: 0; font-weight: 650; font-size: 17px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.budget-reste { text-align: right; }
.budget-reste strong { display: block; font-size: 19px; font-weight: 700; letter-spacing: -0.01em; }
.budget-reste span { font-size: 12.5px; color: var(--texte-3); }
.budget-reste.depasse strong, .budget-reste.depasse span { color: var(--depasse-texte); }
.barre { display: block; height: 8px; border-radius: 999px; background: var(--surface-2); overflow: hidden; }
.barre > span { display: block; height: 100%; border-radius: 999px; background: var(--ok); min-width: 0; transition: width 0.3s ease; }
.barre.attention > span { background: var(--attention); }
.barre.depasse > span { background: var(--depasse); }
.budget-bas { display: flex; justify-content: space-between; font-size: 13.5px; color: var(--texte-3); }
.budget-bas .alerte { color: var(--depasse-texte); font-weight: 600; }
.budget-bas .presque { color: var(--attention-texte); font-weight: 600; }

/* Listes */
.liste { background: var(--surface); border-radius: var(--rayon); box-shadow: var(--ombre); overflow: hidden; }
.element {
  width: 100%; display: flex; align-items: center; gap: 12px; padding: 13px 16px;
  background: none; border: 0; text-align: left; min-height: 60px;
}
.element + .element { border-top: 1px solid var(--bordure); }
button.element:active { background: var(--surface-2); }
.element-icone {
  width: 38px; height: 38px; flex: none; border-radius: 11px; background: var(--surface-2);
  display: grid; place-items: center; font-size: 19px;
}
.element-texte { flex: 1; min-width: 0; }
.element-titre { display: block; font-weight: 600; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.element-sous { display: block; font-size: 13.5px; color: var(--texte-3); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.element-montant { font-weight: 650; white-space: nowrap; text-align: right; }
.element-montant small { display: block; font-size: 12.5px; font-weight: 500; color: var(--texte-3); }
.element-montant.plus { color: var(--ok-texte); }
.element-vide { padding: 18px 16px; color: var(--texte-3); font-size: 15px; text-align: center; }
.element-action { justify-content: center; color: var(--accent-fort); font-weight: 650; min-height: 52px; }
.element-action svg { width: 20px; height: 20px; fill: none; stroke: currentColor; stroke-width: 2.2; stroke-linecap: round; }

.etiquette {
  display: inline-block; padding: 2px 9px; border-radius: 999px; font-size: 12px; font-weight: 650;
  background: var(--surface-2); color: var(--texte-2); vertical-align: 1px;
}
.etiquette.estime { background: color-mix(in srgb, var(--attention) 18%, transparent); color: var(--attention-texte); }
.etiquette.ok { background: color-mix(in srgb, var(--ok) 18%, transparent); color: var(--ok-texte); }
.etiquette.accent { background: var(--accent-doux); color: var(--accent-fort); }

.groupe-boutons { display: flex; flex-wrap: wrap; gap: 8px; margin-top: 12px; }

/* Crédits */
.credit-progression { margin-top: 8px; display: grid; gap: 6px; }
.credit-progression .barre > div { background: var(--positif); }

/* Repli */
details.repli summary { list-style: none; cursor: pointer; }
details.repli summary::-webkit-details-marker { display: none; }
details.repli summary .chevron { transition: transform 0.2s ease; }
details.repli[open] summary .chevron { transform: rotate(180deg); }
.chevron { width: 20px; height: 20px; fill: none; stroke: var(--texte-3); stroke-width: 2; stroke-linecap: round; stroke-linejoin: round; flex: none; }
.resume-repli { display: inline-flex; align-items: center; gap: 6px; }
details.repli summary { padding: 4px 0; }
.defilement-horizontal { overflow-x: auto; }
.note-tableau { margin: 8px 4px 0; }

/* ============ Année ============ */
.chiffres-annee { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; }
.chiffre { background: var(--surface); border-radius: var(--rayon); box-shadow: var(--ombre); padding: 16px; }
.chiffre span { font-size: 14px; color: var(--texte-2); font-weight: 600; }
.chiffre strong { display: block; font-size: 26px; font-weight: 750; letter-spacing: -0.02em; margin-top: 2px; }
.chiffre.accent strong { color: var(--accent-fort); }

.graphique { position: relative; margin-top: 4px; }
.graphique svg { width: 100%; height: auto; overflow: visible; }
.graphique .grille-ligne { stroke: var(--grille); stroke-width: 1; }
.graphique .ligne-zero { stroke: var(--texte-3); stroke-width: 1; }
.graphique .axe-texte { fill: var(--texte-3); font-size: 11px; font-family: var(--police); }
.graphique .barre-pos { fill: var(--positif); }
.graphique .barre-neg { fill: var(--negatif); }
.graphique .barre-prevue { opacity: 0.4; }
.graphique .zone { fill: transparent; cursor: pointer; }
.graphique .zone.active { fill: color-mix(in srgb, var(--texte) 6%, transparent); }
.infobulle {
  position: absolute; pointer-events: none; z-index: 2; transform: translate(-50%, -100%);
  background: var(--texte); color: var(--fond); border-radius: 10px; padding: 8px 10px;
  font-size: 13px; line-height: 1.35; white-space: nowrap; box-shadow: var(--ombre);
}
.infobulle strong { font-weight: 700; }
.legende-graphique { display: flex; flex-wrap: wrap; gap: 6px 16px; margin-top: 10px; font-size: 13px; color: var(--texte-2); }
.legende-graphique span { display: inline-flex; align-items: center; gap: 6px; }
.pastille { display: inline-block; width: 10px; height: 10px; border-radius: 3px; background: var(--positif); }
.pastille.neg { background: var(--negatif); }
.pastille.prevue { background: var(--positif); opacity: 0.4; }

.tableau-annee { width: 100%; border-collapse: collapse; font-size: 14px; font-variant-numeric: tabular-nums; }
.tableau-annee th, .tableau-annee td { padding: 10px 6px; text-align: right; white-space: nowrap; }
.tableau-annee th:first-child, .tableau-annee td:first-child { text-align: left; padding-left: 16px; }
.tableau-annee th:last-child, .tableau-annee td:last-child { padding-right: 16px; }
.tableau-annee thead th { font-size: 12px; font-weight: 650; color: var(--texte-3); text-transform: uppercase; letter-spacing: 0.03em; }
.tableau-annee tbody tr { border-top: 1px solid var(--bordure); }
.tableau-annee tbody tr.prevu td { color: var(--texte-3); font-style: italic; }
.tableau-annee tbody tr.courant td:first-child { font-weight: 700; color: var(--accent-fort); }
.tableau-annee tfoot td { font-weight: 700; border-top: 2px solid var(--bordure); }
.tableau-annee .negatif { color: var(--depasse-texte); }
@media (max-width: 380px) { .tableau-annee { font-size: 13px; } .tableau-annee th, .tableau-annee td { padding: 9px 4px; } }

/* ============ Idées ============ */
.demande { padding: 14px 16px; }
.demande + .demande { border-top: 1px solid var(--bordure); }
.demande-haut { display: flex; justify-content: space-between; align-items: center; gap: 8px; margin-bottom: 6px; font-size: 13px; color: var(--texte-3); }
.demande-texte { white-space: pre-wrap; overflow-wrap: anywhere; }
.demande-reponse { margin-top: 8px; padding: 10px 12px; border-radius: var(--rayon-petit); background: var(--surface-2); font-size: 14.5px; }
.demande-reponse strong { display: block; font-size: 12.5px; color: var(--texte-3); margin-bottom: 2px; }

.pied { margin-top: 36px; display: grid; justify-items: center; gap: 8px; color: var(--texte-3); font-size: 13px; }

/* ============ Feuilles (fenêtres) ============ */
.voile {
  position: fixed; inset: 0; z-index: 50; background: rgba(15, 10, 12, 0.42);
  display: flex; align-items: flex-end; justify-content: center;
  animation: apparition 0.18s ease;
}
.feuille {
  width: 100%; max-width: 560px; max-height: 92dvh; overflow-y: auto; overscroll-behavior: contain;
  background: var(--surface); border-radius: 24px 24px 0 0;
  padding: 8px 20px calc(20px + env(safe-area-inset-bottom));
  animation: montee 0.24s cubic-bezier(0.2, 0.8, 0.2, 1);
}
.feuille:focus { outline: none; }
.feuille-poignee { width: 40px; height: 5px; border-radius: 3px; background: var(--grille); margin: 4px auto 10px; }
.feuille-entete { display: flex; align-items: center; justify-content: space-between; gap: 12px; margin-bottom: 16px; }
.feuille-entete h2 { font-size: 21px; letter-spacing: -0.01em; }
.feuille-fermer {
  width: 36px; height: 36px; border-radius: 50%; border: 0; background: var(--surface-2); color: var(--texte-2);
  display: grid; place-items: center; flex: none;
}
.feuille-fermer svg { width: 18px; height: 18px; fill: none; stroke: currentColor; stroke-width: 2.4; stroke-linecap: round; }
.feuille-corps { display: grid; gap: 16px; }
.feuille-actions { display: grid; gap: 8px; margin-top: 8px; }
.feuille-texte { color: var(--texte-2); }
@media (min-width: 640px) {
  .voile { align-items: center; padding: 24px; }
  .feuille { border-radius: 24px; padding-bottom: 24px; animation: apparition 0.18s ease; }
  .feuille-poignee { display: none; }
}
@keyframes apparition { from { opacity: 0; } to { opacity: 1; } }
@keyframes montee { from { transform: translateY(40px); opacity: 0.6; } to { transform: none; opacity: 1; } }

/* Choix de budget (puces) */
.puces { display: flex; flex-wrap: wrap; gap: 8px; }
.puce {
  display: inline-flex; align-items: center; gap: 6px; min-height: 42px; padding: 0 14px;
  border-radius: 999px; border: 1.5px solid var(--bordure); background: var(--surface);
  font-weight: 600; font-size: 15px;
}
.puce[aria-pressed="true"] { border-color: var(--accent); background: var(--accent-doux); color: var(--accent-fort); }

/* Choix d'icône */
.icones { display: grid; grid-template-columns: repeat(auto-fill, minmax(46px, 1fr)); gap: 6px; }
.icone-choix {
  height: 46px; border-radius: 12px; border: 1.5px solid transparent; background: var(--surface-2);
  font-size: 22px; display: grid; place-items: center;
}
.icone-choix[aria-pressed="true"] { border-color: var(--accent); background: var(--accent-doux); }

/* Message temporaire */
.toast {
  position: fixed; left: 50%; z-index: 60; transform: translate(-50%, 20px);
  bottom: calc(var(--hauteur-onglets) + 92px + env(safe-area-inset-bottom));
  background: var(--texte); color: var(--fond); padding: 12px 18px; border-radius: 999px;
  font-size: 15px; font-weight: 600; box-shadow: var(--ombre);
  opacity: 0; pointer-events: none; transition: opacity 0.2s ease, transform 0.2s ease;
  max-width: calc(100vw - 32px); text-align: center;
}
.toast.visible { opacity: 1; transform: translate(-50%, 0); }
@media (min-width: 900px) { .toast { bottom: 40px; } }

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