/* ==========================================================================
   Ma cagnotte — feuille de style
   Cible : iOS Safari 16+ / standalone (ecran d'accueil), puis desktop.
   ========================================================================== */

:root {
  --bg:        #0f1018;
  --surface:   #161826;
  --card:      #232532;
  --card-2:    #1c1e2b;
  --line:      rgba(233, 233, 237, .07);
  --line-2:    rgba(233, 233, 237, .14);

  --text:      #e9e9ed;
  --muted:     rgba(233, 233, 237, .55);
  --faint:     rgba(233, 233, 237, .40);
  --fainter:   rgba(233, 233, 237, .30);

  --accent:    #b5abfc;
  --accent-2:  #9184d9;
  --accent-bg: #2b2741;
  --danger:    #e2536b;

  --gutter: 20px;
  --tap: 44px;            /* Apple HIG : cible tactile minimale */
  --radius: 8px;

  --safe-t: env(safe-area-inset-top, 0px);
  --safe-b: env(safe-area-inset-bottom, 0px);
  --safe-l: env(safe-area-inset-left, 0px);
  --safe-r: env(safe-area-inset-right, 0px);

  --kb: 0px;              /* hauteur du clavier logiciel, pilotee en JS */
  --ease: cubic-bezier(.22, .9, .3, 1);
}

* { box-sizing: border-box; }

/* display: grid/flex ci-dessous ecraserait le display:none de [hidden]. */
[hidden] { display: none !important; }

html {
  height: 100%;
  -webkit-text-size-adjust: 100%;   /* iOS : pas de reflow en rotation */
  text-size-adjust: 100%;
}

body {
  height: 100%;
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font-family: "Inter", -apple-system, BlinkMacSystemFont, system-ui, sans-serif;
  display: flex;
  align-items: center;
  justify-content: center;
  overflow: hidden;                  /* la page ne scrolle jamais : .scroll le fait */
  overscroll-behavior: none;         /* coupe le rubber-band global iOS */
  -webkit-tap-highlight-color: transparent;
}

button, input {
  font-family: inherit;
  color: inherit;
  -webkit-tap-highlight-color: transparent;
  touch-action: manipulation;         /* supprime le delai 300 ms + double-tap zoom */
}

button {
  -webkit-user-select: none;
  user-select: none;
  -webkit-touch-callout: none;        /* pas de menu contextuel au long press */
}

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

.sr-only {
  position: absolute;
  width: 1px; height: 1px;
  padding: 0; margin: -1px;
  overflow: hidden;
  clip-path: inset(50%);
  white-space: nowrap;
}

/* --------------------------------------------------------------------------
   Coquille
   -------------------------------------------------------------------------- */

.app {
  position: relative;
  width: 100%;
  max-width: 480px;
  height: 100vh;
  height: 100dvh;                     /* suit la barre d'outils Safari */
  overflow: hidden;
  background: var(--surface);
}

.scroll {
  position: absolute;
  inset: 0;
  overflow-y: auto;
  overscroll-behavior-y: contain;     /* pas de propagation au document */
  -webkit-overflow-scrolling: touch;
  scrollbar-width: none;
  padding:
    calc(16px + var(--safe-t))
    var(--safe-r)
    calc(132px + var(--safe-b))
    var(--safe-l);
}
.scroll::-webkit-scrollbar { width: 0; height: 0; }

/* sheet ouverte : on gele le fond */
.app.is-locked .scroll { overflow: hidden; }

/* --------------------------------------------------------------------------
   Barre superieure
   -------------------------------------------------------------------------- */

.topbar {
  padding: 0 calc(var(--gutter) - 10px) 0 var(--gutter);
  display: flex;
  align-items: center;
  justify-content: space-between;
  min-height: var(--tap);
}
.app-name {
  font-size: 13px;
  font-weight: 500;
  letter-spacing: .02em;
  color: var(--muted);
}
.reset-btn {
  border: 0;
  background: transparent;
  color: var(--faint);
  font-size: 12px;
  min-height: var(--tap);
  padding: 0 10px;
  cursor: pointer;
  border-radius: var(--radius);
}
.reset-btn:active { color: var(--danger); background: rgba(226, 83, 107, .10); }
.reset-btn.is-armed {
  color: var(--danger);
  background: rgba(226, 83, 107, .12);
  font-weight: 500;
}

/* --------------------------------------------------------------------------
   Total
   -------------------------------------------------------------------------- */

.total-block {
  padding: 6px var(--gutter) 18px;
  display: flex;
  flex-direction: column;
  gap: 6px;
}
.total-row {
  display: flex;
  align-items: baseline;
  gap: 6px;
}
.total-amount {
  font-size: 52px;
  line-height: 1;
  font-weight: 500;
  letter-spacing: -.03em;
  font-variant-numeric: tabular-nums;  /* pas de saut de largeur a l'update */
}
.total-currency {
  font-size: 24px;
  font-weight: 400;
  color: rgba(233, 233, 237, .45);
}
.total-meta {
  display: flex;
  align-items: center;
  flex-wrap: wrap;
  gap: 8px;
  font-size: 12.5px;
  color: var(--muted);
}
.accent { color: var(--accent); }
.dot {
  width: 3px; height: 3px;
  flex: none;
  border-radius: 50%;
  background: var(--fainter);
}

/* --------------------------------------------------------------------------
   Graphique
   -------------------------------------------------------------------------- */

.chart-card {
  margin: 0 var(--gutter) 20px;
  padding: 16px 0 0;
  background: linear-gradient(180deg, #232532 0%, #1d1f2c 100%);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  overflow: hidden;
}
.chart-head {
  padding: 0 16px 10px;
  display: flex;
  align-items: baseline;
  justify-content: space-between;
}
.chart-label,
.section-head > span:first-child {
  font-size: 11px;
  font-weight: 500;
  letter-spacing: .06em;
  text-transform: uppercase;
  color: rgba(233, 233, 237, .42);
}
.chart-range { font-size: 11px; color: rgba(233, 233, 237, .42); }
.chart-svg { display: block; }
.chart-foot {
  display: flex;
  justify-content: space-between;
  padding: 8px 16px 12px;
  font-size: 10.5px;
  color: var(--fainter);
}

/* --------------------------------------------------------------------------
   KPI
   -------------------------------------------------------------------------- */

.kpi-grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 10px;
  padding: 0 var(--gutter) 22px;
}
.kpi-card {
  padding: 12px 13px;
  background: var(--card);
  border: 1px solid rgba(233, 233, 237, .06);
  border-radius: var(--radius);
  display: flex;
  flex-direction: column;
  gap: 5px;
}
.kpi-label {
  font-size: 10.5px;
  font-weight: 500;
  letter-spacing: .05em;
  text-transform: uppercase;
  color: var(--faint);
}
.kpi-value {
  font-size: 19px;
  font-weight: 500;
  letter-spacing: -.01em;
  font-variant-numeric: tabular-nums;
}
.kpi-hint { font-size: 10.5px; color: rgba(233, 233, 237, .38); }

/* --------------------------------------------------------------------------
   Intertitre
   -------------------------------------------------------------------------- */

.section-head {
  padding: 0 var(--gutter) 8px;
  display: flex;
  align-items: center;
  gap: 10px;
}
.rule {
  flex: 1;
  height: 1px;
  background: linear-gradient(90deg, rgba(233, 233, 237, .12), transparent);
}

/* --------------------------------------------------------------------------
   Historique + swipe-to-delete (idiome iOS)
   -------------------------------------------------------------------------- */

.history-group { padding: 12px var(--gutter) 0; }
.history-group-head {
  display: flex;
  align-items: baseline;
  justify-content: space-between;
  padding: 0 0 8px;
}
.history-group-label {
  font-size: 12.5px;
  font-weight: 500;
  color: rgba(233, 233, 237, .62);
  text-transform: capitalize;
}
.history-group-total {
  font-size: 12px;
  color: var(--accent);
  font-variant-numeric: tabular-nums;
}
.history-items {
  display: flex;
  flex-direction: column;
  gap: 1px;
  background: rgba(233, 233, 237, .06);
  border-radius: var(--radius);
  overflow: hidden;
}

.history-item {
  position: relative;
  overflow: hidden;
  /* Fond neutre : en rouge, il ressortait en filet aux jointures subpixel.
     Le rouge vit sur le bouton, qui n'apparait que pendant le swipe. */
  background: var(--card-2);
}
.history-del {
  position: absolute;
  top: 0; right: 0; bottom: 0;
  width: 96px;
  border: 0;
  background: var(--danger);
  color: #fff;
  font-size: 13px;
  font-weight: 500;
  cursor: pointer;
}
.history-row {
  position: relative;
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 11px 13px;
  background: var(--card-2);
  min-height: 52px;
  touch-action: pan-y;                 /* le vertical reste au scroll, l'horizontal est a nous */
  transition: transform .22s var(--ease);
  will-change: transform;
}
.history-item.is-dragging .history-row { transition: none; }

.history-chip {
  width: 28px; height: 28px;
  flex: none;
  border-radius: var(--radius);
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 14px;
  line-height: 1;
}
.history-body {
  flex: 1;
  display: flex;
  flex-direction: column;
  gap: 2px;
  min-width: 0;
}
.history-note {
  font-size: 13.5px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.history-date { font-size: 11px; color: rgba(233, 233, 237, .38); }
.history-amount {
  font-size: 14px;
  font-weight: 500;
  font-variant-numeric: tabular-nums;
}

/* La croix ne sert qu'aux pointeurs fins : au doigt on swipe. */
.history-remove { display: none; }
@media (hover: hover) and (pointer: fine) {
  .history-remove {
    display: flex;
    align-items: center;
    justify-content: center;
    width: 30px; height: 30px;
    flex: none;
    margin-right: -6px;
    border: 0;
    border-radius: var(--radius);
    background: transparent;
    color: rgba(233, 233, 237, .25);
    font-size: 17px;
    line-height: 1;
    cursor: pointer;
  }
  .history-remove:hover { color: var(--danger); background: rgba(226, 83, 107, .12); }
}

/* --------------------------------------------------------------------------
   Etat vide
   -------------------------------------------------------------------------- */

.empty-state {
  margin: 4px var(--gutter);
  padding: 34px 20px;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 8px;
  text-align: center;
  border: 1px dashed var(--line-2);
  border-radius: var(--radius);
}
.empty-glyph {
  width: 46px; height: 46px;
  display: flex;
  align-items: center;
  justify-content: center;
  margin-bottom: 4px;
  border-radius: 50%;
  background: var(--accent-bg);
  color: var(--accent);
  font-size: 20px;
}
.empty-title { font-size: 15px; font-weight: 500; }
.empty-text { font-size: 13px; color: var(--faint); max-width: 24ch; line-height: 1.45; }

/* --------------------------------------------------------------------------
   Degrade bas + bouton d'ajout
   -------------------------------------------------------------------------- */

.fade {
  position: absolute;
  left: 0; right: 0; bottom: 0;
  height: calc(110px + var(--safe-b));
  pointer-events: none;
  background: linear-gradient(180deg,
    rgba(22, 24, 38, 0) 0%,
    rgba(22, 24, 38, .92) 55%,
    var(--surface) 100%);
}
.add-btn {
  position: absolute;
  bottom: calc(22px + var(--safe-b));
  left: 50%;
  transform: translateX(-50%);
  display: flex;
  align-items: center;
  gap: 8px;
  height: 52px;
  padding: 0 26px;
  white-space: nowrap;
  border-radius: var(--radius);
  border: 1px solid var(--accent-2);
  background: rgba(43, 39, 65, .72);
  -webkit-backdrop-filter: blur(12px);
  backdrop-filter: blur(12px);
  font-size: 15px;
  font-weight: 500;
  cursor: pointer;
  box-shadow: 0 8px 28px rgba(0, 0, 0, .45);
  transition: transform .12s var(--ease), background .16s;
}
.add-btn:active { transform: translateX(-50%) scale(.965); background: var(--accent-bg); }
.add-btn .plus { font-size: 20px; line-height: 1; color: var(--accent); }

/* --------------------------------------------------------------------------
   Bottom sheet
   -------------------------------------------------------------------------- */

.overlay {
  position: absolute;
  inset: 0;
  background: rgba(11, 12, 20, .62);
  -webkit-backdrop-filter: blur(2px);
  backdrop-filter: blur(2px);
  opacity: 0;
  transition: opacity .22s var(--ease);
}
.overlay.is-open { opacity: 1; }

.sheet {
  position: absolute;
  left: 0; right: 0;
  bottom: var(--kb);                   /* remonte au-dessus du clavier iOS */
  max-height: calc(100% - var(--kb) - 24px);
  overflow-y: auto;
  overscroll-behavior: contain;
  background: var(--card);
  border-top: 1px solid rgba(233, 233, 237, .1);
  border-radius: 16px 16px 0 0;
  padding:
    6px
    calc(var(--gutter) + var(--safe-r))
    calc(20px + var(--safe-b))
    calc(var(--gutter) + var(--safe-l));
  box-shadow: 0 -18px 44px rgba(0, 0, 0, .5);
  transform: translateY(100%);
  transition: transform .28s var(--ease);
  will-change: transform;
}
.sheet.is-open { transform: translateY(0); }
.sheet.is-dragging { transition: none; }
/* clavier ouvert : la safe-area est deja couverte par le clavier */
.sheet.kb-up { padding-bottom: 14px; }

.sheet-drag {
  padding: 8px 0 14px;
  margin: 0 calc(-1 * var(--gutter));
  cursor: grab;
  touch-action: none;                  /* on gere nous-memes le geste */
}
.grabber {
  width: 38px; height: 5px;
  border-radius: 9999px;
  background: rgba(233, 233, 237, .18);
  margin: 0 auto;
}

.toggle-row {
  display: flex;
  gap: 2px;
  padding: 2px;
  background: rgba(233, 233, 237, .07);
  border-radius: var(--radius);
  margin-bottom: 18px;
}
.toggle-btn {
  flex: 1;
  min-height: var(--tap);
  border: 0;
  border-radius: 7px;
  cursor: pointer;
  font-size: 14px;
  font-weight: 500;
  background: transparent;
  color: var(--muted);
  transition: background .16s, color .16s;
}
.toggle-btn.is-active { background: var(--accent-bg); color: var(--text); }

.amount-row {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  padding: 0 0 16px;
}
.amount-currency {
  font-size: 34px;
  font-weight: 400;
  color: rgba(233, 233, 237, .35);
}
.amount-input {
  width: 180px;
  background: transparent;
  border: 0;
  border-bottom: 1px solid rgba(233, 233, 237, .16);
  border-radius: 0;                    /* iOS arrondit les inputs par defaut */
  -webkit-appearance: none;
  appearance: none;
  font-size: 44px;                     /* > 16px : pas de zoom au focus */
  font-weight: 500;
  letter-spacing: -.02em;
  text-align: center;
  padding: 0 0 6px;
  font-variant-numeric: tabular-nums;
}
.amount-input::placeholder { color: rgba(233, 233, 237, .22); }
.amount-input:focus { outline: none; border-bottom-color: var(--accent-2); }

.quick-row {
  display: flex;
  gap: 8px;
  padding: 0 0 16px;
}
.quick-btn {
  flex: 1;
  min-height: var(--tap);
  border: 1px solid var(--line-2);
  border-radius: var(--radius);
  background: transparent;
  color: rgba(233, 233, 237, .7);
  font-size: 14px;
  cursor: pointer;
  transition: transform .12s var(--ease), border-color .16s, color .16s;
}
.quick-btn:active { transform: scale(.94); border-color: var(--accent-2); color: var(--text); }

.field-input {
  width: 100%;
  min-height: var(--tap);
  padding: 0 13px;
  margin: 0 0 10px;
  background: var(--card-2);
  border: 1px solid rgba(233, 233, 237, .1);
  border-radius: var(--radius);
  -webkit-appearance: none;
  appearance: none;
  font-size: 16px;                     /* 16px minimum, sinon Safari zoome */
  transition: border-color .16s;
}
.field-input::placeholder { color: rgba(233, 233, 237, .3); }
.field-input:focus { outline: none; border-color: var(--accent-2); }
.field-input[type="date"] {
  margin: 0 0 16px;
  color-scheme: dark;
  min-width: 0;                        /* iOS : evite le debordement du picker */
}

.sheet-actions { display: flex; gap: 10px; }
.btn-secondary,
.btn-primary {
  min-height: 50px;
  border-radius: var(--radius);
  font-size: 16px;
  cursor: pointer;
  transition: transform .12s var(--ease), background .16s, opacity .16s;
}
.btn-secondary {
  flex: 1;
  border: 1px solid var(--line-2);
  background: transparent;
  color: rgba(233, 233, 237, .7);
}
.btn-primary {
  flex: 2;
  border: 1px solid var(--accent-2);
  background: rgba(43, 39, 65, .6);
  font-weight: 500;
}
.btn-secondary:active,
.btn-primary:active:not(:disabled) { transform: scale(.98); background: var(--accent-bg); }
.btn-primary:disabled {
  opacity: .38;
  cursor: default;
  transform: none;
  background: rgba(43, 39, 65, .3);
}

/* --------------------------------------------------------------------------
   Toast
   -------------------------------------------------------------------------- */

.toast {
  position: absolute;
  left: 50%;
  bottom: calc(88px + var(--safe-b));
  transform: translate(-50%, 8px);
  max-width: calc(100% - 40px);
  padding: 10px 16px;
  border-radius: 9999px;
  background: rgba(35, 37, 50, .95);
  -webkit-backdrop-filter: blur(10px);
  backdrop-filter: blur(10px);
  border: 1px solid var(--line-2);
  font-size: 13px;
  white-space: nowrap;
  opacity: 0;
  pointer-events: none;
  transition: opacity .2s, transform .2s var(--ease);
}
.toast.is-open { opacity: 1; transform: translate(-50%, 0); }

/* --------------------------------------------------------------------------
   Survol : uniquement pour les vrais pointeurs (sinon etats collants iOS)
   -------------------------------------------------------------------------- */

@media (hover: hover) and (pointer: fine) {
  .add-btn:hover      { background: var(--accent-bg); border-color: var(--accent); }
  .quick-btn:hover    { border-color: var(--accent-2); color: var(--text); }
  .btn-primary:hover:not(:disabled) { background: var(--accent-bg); }
  .btn-secondary:hover { border-color: var(--accent-2); color: var(--text); }
  .reset-btn:hover    { color: var(--danger); }
}

@media (prefers-reduced-motion: reduce) {
  * { animation-duration: .01ms !important; transition-duration: .01ms !important; }
}
