/* Monatskasse — Designsystem nach Apple-Prinzipien:
   Systemschrift mit größenabhängigem Tracking, gruppierte Listen,
   durchscheinende Leisten, Feedback beim Drücken, ruhige Farben. */

:root {
  --bg: #f2f2f7;
  --surface: #ffffff;
  --surface-press: #e9e9ee;
  --sheet: #f2f2f7;
  --label: #1c1c1e;
  --label-2: rgba(60, 60, 67, 0.64);
  --label-3: rgba(60, 60, 67, 0.32);
  --sep: rgba(60, 60, 67, 0.16);
  --fill: rgba(120, 120, 128, 0.14);
  --fill-strong: rgba(120, 120, 128, 0.22);

  --accent: #0a7c70;          /* Text/Buttons, 4.9:1 auf Weiß */
  --accent-soft: rgba(10, 124, 112, 0.12);
  --on-accent: #ffffff;
  --good: #12a594;
  --warn: #f08c00;
  --bad: #e5352b;
  --bad-text: #c4281f;

  --bar: rgba(242, 242, 247, 0.82);
  --bar-line: rgba(60, 60, 67, 0.14);
  --scrim: rgba(0, 0, 0, 0.38);
  --shadow-card: 0 1px 2px rgba(0, 0, 0, 0.04), 0 8px 24px rgba(0, 0, 0, 0.04);
  --shadow-float: 0 10px 28px rgba(10, 124, 112, 0.32), 0 2px 6px rgba(0, 0, 0, 0.12);

  --font: system-ui, -apple-system, "Roboto", "Segoe UI", sans-serif;
  --tabbar-h: 58px;
  --r-card: 22px;
  --r-group: 16px;
  --ease-out: cubic-bezier(0.22, 1, 0.36, 1);
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #000000;
    --surface: #1c1c1e;
    --surface-press: #2c2c2e;
    --sheet: #1c1c1e;
    --label: #ffffff;
    --label-2: rgba(235, 235, 245, 0.62);
    --label-3: rgba(235, 235, 245, 0.3);
    --sep: rgba(84, 84, 88, 0.62);
    --fill: rgba(120, 120, 128, 0.26);
    --fill-strong: rgba(120, 120, 128, 0.36);

    --accent: #34d3bf;
    --accent-soft: rgba(52, 211, 191, 0.16);
    --on-accent: #00201c;
    --good: #30d5c0;
    --warn: #ffa31a;
    --bad: #ff453a;
    --bad-text: #ff6961;

    --bar: rgba(0, 0, 0, 0.76);   /* in Ruhe unsichtbar auf Schwarz, Trennlinie erst beim Scrollen */
    --bar-line: rgba(84, 84, 88, 0.5);
    --scrim: rgba(0, 0, 0, 0.55);
    --shadow-card: none;
    --shadow-float: 0 10px 28px rgba(0, 0, 0, 0.5);
  }
  /* Gruppen in Sheets brauchen im Dunkeln eine hellere Ebene als das Sheet selbst */
  .sheet .group-box { background: #2c2c2e; }
  .sheet .group-box .press:active { background: #3a3a3c; }
}

* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
html { font-size: 100%; }
html, body { margin: 0; }
body {
  font: 400 1.0625rem/1.4 var(--font);   /* 17px Body wie iOS */
  letter-spacing: -0.01em;
  color: var(--label);
  background: var(--bg);
  -webkit-font-smoothing: antialiased;
  font-optical-sizing: auto;
  -webkit-user-select: none; user-select: none;
  overscroll-behavior-y: none;
  padding-top: env(safe-area-inset-top);
}
input, textarea { -webkit-user-select: text; user-select: text; font: inherit; color: inherit; }
button { font: inherit; color: inherit; background: none; border: 0; padding: 0; cursor: pointer; }
svg { display: block; }
.hidden { display: none !important; }
html.locked, html.locked body { overflow: hidden; } /* Hintergrund scrollt nicht unter offenem Sheet */
.sr-only { position: absolute; width: 1px; height: 1px; overflow: hidden; clip: rect(0 0 0 0); }

/* ---------- Seitenrahmen ---------- */
.view {
  max-width: 640px; margin: 0 auto;
  padding: 0 16px calc(var(--tabbar-h) + env(safe-area-inset-bottom) + 96px);
}

/* Durchscheinende Kopfleiste; Trennlinie erst, wenn Inhalt darunter liegt */
.topbar {
  position: sticky; top: 0; z-index: 10;
  margin: 0 -16px; padding: 10px 12px 10px;
  display: grid; grid-template-columns: 44px 1fr 44px; align-items: center;
  background: var(--bar);
  -webkit-backdrop-filter: blur(20px) saturate(180%);
  backdrop-filter: blur(20px) saturate(180%);
  box-shadow: 0 0.5px 0 transparent;
  transition: box-shadow 0.2s;
}
.topbar.scrolled { box-shadow: 0 0.5px 0 var(--bar-line); }
.topbar.titled { grid-template-columns: 1fr auto; padding: 14px 16px 10px; }
.topbar-title { text-align: center; line-height: 1.1; }
.topbar-title h1 { margin: 0; font-size: 1.25rem; font-weight: 700; letter-spacing: -0.02em; }
.topbar-title .year { font-size: 0.8125rem; color: var(--label-2); letter-spacing: 0; font-variant-numeric: tabular-nums; }
.large-title { margin: 0; font-size: 2rem; font-weight: 750; letter-spacing: -0.03em; line-height: 1.1; }

.nav-btn {
  width: 44px; height: 44px; border-radius: 22px;
  display: grid; place-items: center; color: var(--accent);
  transition: background-color 0.25s, transform 0.25s var(--ease-out);
}
.nav-btn:active { background: var(--fill); transform: scale(0.92); transition: none; }
.nav-btn:disabled { color: var(--label-3); background: none; transform: none; }
.nav-btn svg { width: 22px; height: 22px; }
.text-btn { color: var(--accent); font-size: 1.0625rem; padding: 8px 4px; border-radius: 10px; transition: opacity 0.2s; }
.text-btn:active { opacity: 0.45; transition: none; }
.text-btn.strong { font-weight: 650; }
.text-btn:disabled { color: var(--label-3); }
.text-btn .ico { display: inline-flex; vertical-align: -4px; margin-right: 4px; }
.text-btn .ico svg { width: 20px; height: 20px; }

.chip-back {
  display: block; margin: 6px auto 0; padding: 7px 14px; border-radius: 999px;
  background: var(--accent-soft); color: var(--accent);
  font-size: 0.9375rem; font-weight: 600;
  transition: transform 0.25s var(--ease-out);
}
.chip-back:active { transform: scale(0.95); transition: none; }

/* ---------- Saldo ---------- */
.hero {
  margin-top: 12px; padding: 20px 22px 18px;
  background: var(--surface); border-radius: var(--r-card); box-shadow: var(--shadow-card);
}
.hero-label { font-size: 0.9375rem; font-weight: 500; color: var(--label-2); letter-spacing: -0.005em; }
.hero-amount {
  margin: 2px 0 14px;
  font-size: clamp(2.75rem, 13.5vw, 3.75rem); font-weight: 800;
  letter-spacing: -0.04em; line-height: 1.02;      /* große Zahl: enges Tracking, enge Zeilenhöhe */
  font-variant-numeric: tabular-nums;
  transition: color 0.3s;
}
.hero-amount .cur { font-weight: 650; letter-spacing: -0.01em; color: var(--label-2); margin-left: 0.12em; font-size: 0.62em; }
.hero-amount.over { color: var(--bad); }
.meter { height: 8px; border-radius: 99px; background: var(--fill); overflow: hidden; }
.meter-fill {
  height: 100%; width: 100%; border-radius: 99px; background: var(--good);
  /* width statt scaleX: scaleX würde die runden Enden stauchen */
  transition: width 0.6s var(--ease-out), background-color 0.3s;
}
.meter-fill.warn { background: var(--warn); }
.meter-fill.over { background: var(--bad); }
.hero-meta { margin-top: 10px; font-size: 0.9375rem; color: var(--label-2); font-variant-numeric: tabular-nums; letter-spacing: -0.005em; }
.hero-meta b { color: var(--label); font-weight: 600; }
.pace {
  display: inline-flex; align-items: center; gap: 6px; margin-top: 12px;
  padding: 6px 11px; border-radius: 999px; background: var(--fill);
  font-size: 0.8125rem; font-weight: 550; letter-spacing: 0.005em; color: var(--label-2);
  font-variant-numeric: tabular-nums;
}
.pace.good { background: var(--accent-soft); color: var(--accent); }
.pace.bad { background: color-mix(in srgb, var(--bad) 14%, transparent); color: var(--bad-text); }

/* ---------- Gruppen / Listen ---------- */
.section { margin-top: 28px; }
.section-head { display: flex; align-items: baseline; justify-content: space-between; padding: 0 4px 8px; }
.section-head h2 { margin: 0; font-size: 1.25rem; font-weight: 700; letter-spacing: -0.02em; }
.section-head .meta { font-size: 0.9375rem; color: var(--label-2); font-variant-numeric: tabular-nums; }
.day-head {
  display: flex; justify-content: space-between; padding: 14px 4px 6px;
  font-size: 0.8125rem; font-weight: 600; letter-spacing: 0.01em; color: var(--label-2);
  font-variant-numeric: tabular-nums;
}
.group-box { background: var(--surface); border-radius: var(--r-group); overflow: hidden; box-shadow: var(--shadow-card); }
.group-foot { padding: 7px 16px 0; font-size: 0.8125rem; line-height: 1.35; letter-spacing: 0.005em; color: var(--label-2); }
.list { list-style: none; margin: 0; padding: 0; }

.row {
  position: relative; display: flex; align-items: center; gap: 12px;
  min-height: 56px; padding: 9px 16px;
  transition: background-color 0.3s;
}
.row + .row::before {   /* eingerückte Trennlinie, beginnt beim Text */
  content: ""; position: absolute; top: 0; right: 0; left: var(--inset, 64px);
  height: 0.5px; background: var(--sep);
}
.press:active { background: var(--surface-press); transition: none; }
.row-main { flex: 1; min-width: 0; }
.row-title { font-size: 1.0625rem; font-weight: 500; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.row-sub { font-size: 0.8125rem; color: var(--label-2); letter-spacing: 0.005em; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.row-amount { font-size: 1.0625rem; font-weight: 600; font-variant-numeric: tabular-nums; white-space: nowrap; }
.row-chev { color: var(--label-3); width: 8px; flex: none; }

.avatar {
  flex: none; width: 36px; height: 36px; border-radius: 18px;
  display: grid; place-items: center;
  font-size: 0.9375rem; font-weight: 700; letter-spacing: 0;
  background: var(--fill); color: var(--label-2);
}
.thumb { flex: none; width: 36px; height: 36px; border-radius: 9px; object-fit: cover; background: var(--fill); }

/* Kategorien: gestapelter Balken + Legende */
.stack { display: flex; height: 10px; border-radius: 99px; overflow: hidden; gap: 2px; margin: 16px 16px 6px; }
.stack span { display: block; height: 100%; min-width: 3px; }
.legend { display: grid; grid-template-columns: 1fr 1fr; column-gap: 8px; padding: 4px 10px 12px; }
.legend li { display: flex; align-items: center; gap: 8px; min-width: 0; padding: 6px 6px; font-size: 0.9375rem; }
.legend .name { flex: 1; min-width: 0; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.legend .amt { color: var(--label-2); font-variant-numeric: tabular-nums; white-space: nowrap; }
.dot { flex: none; width: 10px; height: 10px; border-radius: 5px; }

/* Verlauf-Zeilen mit Mini-Balken */
.mini-meter { height: 4px; border-radius: 99px; background: var(--fill); margin-top: 7px; overflow: hidden; }
.mini-meter > span { display: block; height: 100%; background: var(--good); border-radius: 99px; }
.mini-meter > span.over { background: var(--bad); }

/* Leerer Zustand */
.empty { padding: 36px 24px 12px; text-align: center; }
.empty-icon { width: 64px; height: 64px; margin: 0 auto 14px; color: var(--accent); opacity: 0.9; }
.empty h3 { margin: 0 0 4px; font-size: 1.0625rem; font-weight: 650; letter-spacing: -0.01em; }
.empty p { margin: 0; font-size: 0.9375rem; color: var(--label-2); }
.welcome { display: grid; gap: 12px; margin-top: 20px; text-align: left; }

/* ---------- Einstellungs-Zeilen ---------- */
.field-row { --inset: 16px; min-height: 50px; }
.field-row label, .field-row .lbl { flex: none; font-size: 1.0625rem; }
.field-row input {
  flex: 1; min-width: 0; border: 0; outline: 0; background: transparent;
  text-align: right; font-size: 1.0625rem; color: var(--label); padding: 8px 0;
}
.field-row input::placeholder { color: var(--label-3); }
.field-row input.invalid { color: var(--bad-text); }
/* Datum: sichtbare Pille, darüber das unsichtbare native Feld (öffnet den System-Kalender) */
.date-row { position: relative; }
.date-pill {
  margin-left: auto; padding: 6px 12px; border-radius: 9px;
  background: var(--fill); font-size: 1.0625rem; font-variant-numeric: tabular-nums;
}
.date-row:active .date-pill { background: var(--fill-strong); }
.date-row input[type="date"] { position: absolute; inset: 0; width: 100%; height: 100%; opacity: 0; padding: 0; }
.action-row { --inset: 16px; min-height: 50px; color: var(--accent); font-size: 1.0625rem; width: 100%; text-align: left; }
.action-row .ico svg { width: 22px; height: 22px; }
.action-row.destructive { color: var(--bad-text); justify-content: center; }
.app-foot { margin-top: 28px; text-align: center; font-size: 0.8125rem; color: var(--label-2); line-height: 1.5; }

/* ---------- FAB ---------- */
.fab {
  position: fixed; z-index: 20;
  right: max(18px, calc(50% - 320px + 18px));
  bottom: calc(var(--tabbar-h) + env(safe-area-inset-bottom) + 16px);
  width: 58px; height: 58px; border-radius: 29px;
  display: grid; place-items: center;
  background: var(--accent); color: var(--on-accent); box-shadow: var(--shadow-float);
  transition: transform 0.35s var(--ease-out), opacity 0.25s;
}
.fab svg { width: 26px; height: 26px; }
.fab:active { transform: scale(0.9); transition: transform 0.08s; }
.fab.away { transform: scale(0.6); opacity: 0; pointer-events: none; }

/* ---------- Tableiste (Material) ---------- */
.tabbar {
  position: fixed; left: 0; right: 0; bottom: 0; z-index: 15;
  height: calc(var(--tabbar-h) + env(safe-area-inset-bottom));
  padding-bottom: env(safe-area-inset-bottom);
  display: flex; justify-content: center;
  background: var(--bar);
  -webkit-backdrop-filter: blur(20px) saturate(180%);
  backdrop-filter: blur(20px) saturate(180%);
  box-shadow: 0 -0.5px 0 var(--bar-line);
}
.tab {
  flex: 1; max-width: 200px;
  display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 3px;
  color: var(--label-2); font-size: 0.6875rem; font-weight: 600; letter-spacing: 0.01em;
}
.tab svg { width: 25px; height: 25px; transition: transform 0.3s var(--ease-out); }
.tab:active svg { transform: scale(0.86); transition: transform 0.08s; }
.tab[aria-selected="true"] { color: var(--accent); }

/* ---------- Sheets ---------- */
.scrim {
  position: fixed; inset: 0; z-index: 40; background: var(--scrim);
  opacity: 0; visibility: hidden; pointer-events: none; touch-action: none;
}
.scrim.is-open { visibility: visible; pointer-events: auto; }
.sheet {
  position: fixed; left: 0; right: 0; bottom: 0; z-index: 41;
  max-width: 640px; margin: 0 auto;
  max-height: calc(100vh - 28px);
  max-height: calc(100dvh - 28px);
  display: flex; flex-direction: column;
  background: var(--sheet); border-radius: 20px 20px 0 0;
  box-shadow: 0 -8px 40px rgba(0, 0, 0, 0.18);
  transform: translate3d(0, 110%, 0); visibility: hidden;
  will-change: transform;
}
.sheet.is-open { visibility: visible; }
.sheet.top { z-index: 51; }
.scrim.top { z-index: 50; }
.grabber-zone { flex: none; height: 20px; display: grid; place-items: center; touch-action: none; }
.grabber { width: 38px; height: 5px; border-radius: 3px; background: var(--label-3); }
.sheet-head {
  flex: none; display: grid; grid-template-columns: 1fr auto 1fr; align-items: center;
  padding: 0 12px 6px; touch-action: none;
}
.sheet-head h2 { margin: 0; font-size: 1.0625rem; font-weight: 650; letter-spacing: -0.015em; text-align: center; }
.sheet-head > :first-child { justify-self: start; }
.sheet-head > :last-child { justify-self: end; }
.sheet-body {
  flex: 1; overflow-y: auto; overscroll-behavior: contain; -webkit-overflow-scrolling: touch;
  padding: 4px 16px calc(24px + env(safe-area-inset-bottom));
}
.sheet .group-box { box-shadow: none; }
.sheet .section { margin-top: 18px; }

/* Großes Betragsfeld */
.amount {
  display: flex; justify-content: center; align-items: baseline; gap: 6px;
  padding: 14px 0 4px;
}
.amount input {
  width: 4ch; min-width: 3ch; max-width: 100%;
  border: 0; outline: 0; background: transparent; text-align: right;
  font-size: 3.25rem; font-weight: 750; letter-spacing: -0.04em; line-height: 1.1;
  font-variant-numeric: tabular-nums; caret-color: var(--accent); padding: 0;
}
.amount input::placeholder { color: var(--label-3); }
.amount .cur { font-size: 2rem; font-weight: 650; color: var(--label-2); letter-spacing: -0.02em; }
.amount-hint { text-align: center; font-size: 0.8125rem; color: var(--label-2); min-height: 1.2em; }
.shake { animation: shake 0.42s var(--ease-out); }
@keyframes shake {
  0%, 100% { transform: translateX(0); }
  20% { transform: translateX(-9px); } 40% { transform: translateX(8px); }
  60% { transform: translateX(-5px); } 80% { transform: translateX(3px); }
}

/* Kategorie-Chips */
.chips { display: flex; gap: 8px; overflow-x: auto; padding: 10px 2px 2px; scrollbar-width: none; }
.chips::-webkit-scrollbar { display: none; }
.chip {
  flex: none; padding: 7px 13px; border-radius: 999px;
  background: var(--fill); font-size: 0.9375rem; font-weight: 500;
  transition: transform 0.25s var(--ease-out), background-color 0.2s, color 0.2s;
}
.chip:active { transform: scale(0.94); transition: none; }
.chip[aria-pressed="true"] { background: var(--accent); color: var(--on-accent); }

/* Beleg */
.receipt { padding: 12px; }
.receipt img { display: block; width: 100%; max-height: 240px; object-fit: cover; object-position: top; border-radius: 12px; background: var(--fill); }
.receipt-actions { display: flex; gap: 8px; margin-top: 10px; }
.btn {
  flex: 1; display: inline-flex; align-items: center; justify-content: center; gap: 6px;
  min-height: 44px; padding: 10px 14px; border-radius: 12px;
  font-size: 0.9375rem; font-weight: 600;
  transition: transform 0.25s var(--ease-out), opacity 0.2s;
}
.btn:active { transform: scale(0.97); transition: none; }
.btn svg { width: 19px; height: 19px; }
.btn.tinted { background: var(--accent-soft); color: var(--accent); }
.btn.plain { background: var(--fill); color: var(--label); }
.btn.filled { background: var(--accent); color: var(--on-accent); }
.btn.danger { background: color-mix(in srgb, var(--bad) 12%, transparent); color: var(--bad-text); }
.btn:disabled { opacity: 0.4; }

/* OCR-Liste */
.ocr-status { padding: 6px 4px 10px; font-size: 0.9375rem; color: var(--label-2); }
.progress { height: 4px; border-radius: 99px; background: var(--fill); overflow: hidden; margin: 0 4px 12px; }
.progress > span { display: block; height: 100%; width: 0; background: var(--accent); border-radius: 99px; transition: width 0.25s linear; }
.ocr-row { --inset: 52px; gap: 10px; min-height: 52px; padding: 6px 12px 6px 14px; }
.ocr-row input[type="text"] { min-width: 0; border: 0; outline: 0; background: transparent; padding: 8px 0; font-size: 1.0625rem; }
.ocr-row .oi-name { flex: 1; }
.ocr-row .oi-price { width: 5.2em; text-align: right; font-variant-numeric: tabular-nums; font-weight: 600; }
.check {
  appearance: none; -webkit-appearance: none; flex: none; margin: 0;
  width: 24px; height: 24px; border-radius: 12px; border: 2px solid var(--label-3);
  display: grid; place-items: center; transition: background-color 0.15s, border-color 0.15s;
}
.check:checked {
  border-color: var(--accent); background: var(--accent)
    url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24'%3E%3Cpath d='M6.5 12.5l3.5 3.5 7.5-8' fill='none' stroke='white' stroke-width='2.6' stroke-linecap='round' stroke-linejoin='round'/%3E%3C/svg%3E") center/18px no-repeat;
}
.ocr-total { display: flex; justify-content: space-between; padding: 12px 4px 0; font-size: 0.9375rem; color: var(--label-2); font-variant-numeric: tabular-nums; }
.ocr-total b { color: var(--label); }

/* ---------- Bildansicht ---------- */
.lightbox {
  position: fixed; inset: 0; z-index: 70; background: #000;
  display: grid; place-items: center;
  opacity: 0; visibility: hidden; transition: opacity 0.22s, visibility 0s 0.22s;
}
.lightbox.is-open { opacity: 1; visibility: visible; transition: opacity 0.22s; }
.lightbox img { max-width: 100%; max-height: 100%; object-fit: contain; }
.lightbox .close {
  position: absolute; top: calc(env(safe-area-inset-top) + 12px); right: 12px;
  padding: 9px 16px; border-radius: 999px; color: #fff; font-weight: 600;
  background: rgba(60, 60, 60, 0.6);
  -webkit-backdrop-filter: blur(16px); backdrop-filter: blur(16px);
}

/* ---------- Toast ---------- */
.toast {
  position: fixed; z-index: 90; left: 50%;
  bottom: calc(var(--tabbar-h) + env(safe-area-inset-bottom) + 88px); /* über dem +-Knopf */
  display: flex; align-items: center; gap: 14px;
  width: max-content; max-width: calc(100% - 32px); /* left:50% würde sonst die Breite halbieren */
  padding: 11px 12px 11px 18px; border-radius: 999px;
  background: rgba(28, 28, 30, 0.92); color: #fff;
  -webkit-backdrop-filter: blur(16px); backdrop-filter: blur(16px);
  font-size: 0.9375rem; font-weight: 500; letter-spacing: -0.005em;
  box-shadow: 0 8px 28px rgba(0, 0, 0, 0.22);
  transform: translate(-50%, 14px); opacity: 0; pointer-events: none;
  transition: transform 0.35s var(--ease-out), opacity 0.25s;
}
.toast.show { transform: translate(-50%, 0); opacity: 1; pointer-events: auto; }
.toast.no-action { padding-right: 18px; }
#toast-text { white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.toast button { color: #5eead4; font-weight: 650; padding: 4px 8px; border-radius: 8px; }
.toast button:active { opacity: 0.5; }
@media (prefers-color-scheme: dark) { .toast { background: rgba(58, 58, 60, 0.94); } }

/* ---------- Barrierefreiheit ---------- */
@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { animation: none !important; }
  .meter-fill, .toast, .fab, .tab svg, .chip, .btn, .nav-btn { transition-duration: 0.01s !important; }
  .toast { transform: translate(-50%, 0); }
}
@media (prefers-reduced-transparency: reduce) {
  :root { --bar: var(--bg); }
  .topbar, .tabbar, .toast, .lightbox .close { -webkit-backdrop-filter: none; backdrop-filter: none; }
}
@media (prefers-contrast: more) {
  :root { --label-2: var(--label); --sep: var(--label-3); }
  .group-box, .hero { outline: 1px solid var(--label-3); }
}
:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }
input:focus-visible { outline: none; }
