:root {
  --bg: #f3f5ef;
  --surface: #ffffff;
  --surface-2: #eef2ea;
  --text: #1d2a1f;
  --muted: #627064;
  --border: #dbe2d6;
  --brand: #2f6b3a;
  --brand-strong: #24552d;
  --brand-ink: #ffffff;
  --header-bg: #2f6b3a;
  --header-ink: #f2f8ef;
  --header-muted: #b9d7b6;
  --pay: #1f7a4d;
  --material: #9a5b13;
  --danger: #b3261e;
  --focus: #6fb26b;
  --shadow: 0 1px 2px rgba(20, 40, 20, .06), 0 4px 16px rgba(20, 40, 20, .06);
  color-scheme: light;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #121612;
    --surface: #1b211c;
    --surface-2: #232b24;
    --text: #e6ece4;
    --muted: #96a497;
    --border: #2f3a31;
    --brand: #5fae69;
    --brand-strong: #74c07d;
    --brand-ink: #0d1a0f;
    --header-bg: #1a2e1d;
    --header-ink: #eaf5e7;
    --header-muted: #9cbf98;
    --pay: #6fcf97;
    --material: #e0a458;
    --danger: #ff8a80;
    --shadow: none;
    color-scheme: dark;
  }
}

* { box-sizing: border-box; }

html { -webkit-text-size-adjust: 100%; }

body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font: 16px/1.4 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  -webkit-tap-highlight-color: transparent;
}

button, input { font: inherit; color: inherit; }
button { cursor: pointer; touch-action: manipulation; }

/* ---------- Kopf mit Saldo ---------- */
.top {
  background: var(--header-bg);
  color: var(--header-ink);
  padding: calc(env(safe-area-inset-top) + 14px) 16px 22px;
  border-radius: 0 0 22px 22px;
}
.brand {
  display: flex;
  align-items: center;
  gap: 10px;
  font-weight: 650;
  font-size: 18px;
  max-width: 560px;
  margin: 0 auto;
}
.brand img { border-radius: 7px; box-shadow: 0 0 0 1px rgba(255,255,255,.15); }
.brand .logout {
  margin-left: auto;
  border: 0;
  background: transparent;
  padding: 6px 0;
  font-size: 13px;
  font-weight: 500;
  color: var(--header-muted);
}

/* ---------- Anmeldung ---------- */
.login { display: none; }
.login h2 { margin: 0; font-size: 18px; }
.login-error { margin: 12px 0 0; color: var(--danger); font-size: 14px; font-weight: 600; }
body.locked .login { display: block; }
body.locked .saldo,
body.locked .logout,
body.locked #form,
body.locked .history { display: none; }
.saldo { max-width: 560px; margin: 18px auto 0; }
.saldo-label { color: var(--header-muted); font-size: 14px; }
.saldo-value {
  font-size: 42px;
  font-weight: 700;
  letter-spacing: -.02em;
  font-variant-numeric: tabular-nums;
  line-height: 1.15;
}
.saldo-parts {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 8px;
  margin-top: 14px;
}
.saldo-parts div {
  background: rgba(255, 255, 255, .09);
  border-radius: 12px;
  padding: 8px 10px;
  min-width: 0;
}
.saldo-parts span { display: block; font-size: 12px; color: var(--header-muted); }
.saldo-parts b {
  display: block;
  font-size: 15px;
  font-variant-numeric: tabular-nums;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.saldo-parts small { display: block; font-size: 12px; color: var(--header-muted); }

/* ---------- Formular ---------- */
main {
  max-width: 560px;
  margin: 0 auto;
  padding: 16px 16px calc(env(safe-area-inset-bottom) + 24px);
}
.card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 18px;
  padding: 14px;
  box-shadow: var(--shadow);
}

.seg {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  background: var(--surface-2);
  border-radius: 12px;
  padding: 4px;
  gap: 4px;
}
.seg button {
  border: 0;
  background: transparent;
  padding: 10px 4px;
  border-radius: 9px;
  font-weight: 600;
  color: var(--muted);
}
.seg button[aria-checked="true"] {
  background: var(--surface);
  color: var(--text);
  box-shadow: 0 1px 3px rgba(0, 0, 0, .12);
}

.edit-banner {
  margin-top: 12px;
  padding: 8px 12px;
  border-radius: 10px;
  background: color-mix(in srgb, var(--material) 14%, transparent);
  color: var(--material);
  font-weight: 600;
  font-size: 14px;
}

.field { display: block; margin-top: 14px; }
.field[hidden] { display: none; }
.field > span {
  display: block;
  font-size: 13px;
  font-weight: 600;
  color: var(--muted);
  margin-bottom: 6px;
}
.field small { font-weight: 400; }

input[type="text"], input[type="date"], input[type="password"] {
  width: 100%;
  min-height: 48px;
  padding: 10px 12px;
  border: 1px solid var(--border);
  border-radius: 12px;
  background: var(--surface);
  font-size: 17px; /* >= 16px verhindert Auto-Zoom auf iOS */
  appearance: none;
  -webkit-appearance: none;
}
input:focus-visible, button:focus-visible {
  outline: 2px solid var(--focus);
  outline-offset: 2px;
}
input[type="date"] { display: block; }

.stepper {
  display: grid;
  grid-template-columns: 56px 1fr 56px;
  gap: 8px;
}
.stepper button {
  border: 1px solid var(--border);
  background: var(--surface-2);
  border-radius: 12px;
  font-size: 26px;
  line-height: 1;
}
.stepper button:active { background: var(--border); }
.stepper input {
  text-align: center;
  font-size: 28px !important;
  font-weight: 700;
  font-variant-numeric: tabular-nums;
}

.chips {
  display: grid;
  grid-template-columns: repeat(7, 1fr);
  gap: 6px;
  margin-top: 8px;
}
.chips button {
  border: 1px solid var(--border);
  background: var(--surface);
  border-radius: 999px;
  padding: 7px 0;
  font-weight: 600;
  font-size: 15px;
}
.chips button.on {
  background: var(--brand);
  border-color: var(--brand);
  color: var(--brand-ink);
}

.money { position: relative; }
.money input { padding-right: 36px; font-size: 22px; font-weight: 650; }
.money em {
  position: absolute;
  right: 14px;
  top: 50%;
  transform: translateY(-50%);
  font-style: normal;
  color: var(--muted);
  font-size: 18px;
}

.preview {
  margin-top: 14px;
  min-height: 20px;
  font-size: 14px;
  color: var(--muted);
  font-variant-numeric: tabular-nums;
}
.preview b { color: var(--text); }

.actions { margin-top: 12px; }
.actions button {
  width: 100%;
  min-height: 50px;
  border-radius: 14px;
  font-weight: 650;
  font-size: 17px;
  border: 1px solid var(--border);
  background: var(--surface);
}
.actions .primary {
  background: var(--brand);
  border-color: var(--brand);
  color: var(--brand-ink);
}
.actions .primary:active { background: var(--brand-strong); }
.actions .primary:disabled { opacity: .6; }
.edit-actions {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 8px;
  margin-top: 8px;
}
.edit-actions[hidden] { display: none; }
.actions .danger { color: var(--danger); }

/* ---------- Verlauf ---------- */
.history { margin-top: 24px; }
.history-head {
  display: flex;
  align-items: baseline;
  justify-content: space-between;
  padding: 0 4px;
}
.history-head h2 { font-size: 18px; margin: 0; }
.history-head a {
  color: var(--brand);
  font-size: 14px;
  font-weight: 600;
  text-decoration: none;
  padding: 6px 0;
}

.month { margin-top: 14px; }
.month-head {
  display: flex;
  flex-wrap: wrap;
  align-items: baseline;
  justify-content: space-between;
  column-gap: 12px;
  padding: 0 4px 6px;
  font-size: 13px;
  color: var(--muted);
}
.month-head strong { color: var(--text); font-size: 14px; white-space: nowrap; }
.month-head span { font-variant-numeric: tabular-nums; }
.month-head i { font-style: normal; white-space: nowrap; }

.month ul {
  list-style: none;
  margin: 0;
  padding: 0;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 16px;
  overflow: hidden;
  box-shadow: var(--shadow);
}
.month li + li { border-top: 1px solid var(--border); }
.entry {
  display: grid;
  grid-template-columns: 44px 1fr auto;
  align-items: center;
  gap: 12px;
  width: 100%;
  padding: 10px 12px;
  border: 0;
  background: transparent;
  text-align: left;
}
.entry:active { background: var(--surface-2); }
.entry.editing { background: color-mix(in srgb, var(--material) 10%, transparent); }
.day {
  text-align: center;
  line-height: 1.1;
  border-radius: 10px;
  background: var(--surface-2);
  padding: 5px 0;
}
.day b { display: block; font-size: 17px; }
.day small { font-size: 11px; color: var(--muted); text-transform: uppercase; }
.what { min-width: 0; }
.what b { display: block; font-weight: 600; }
.what small {
  display: block;
  color: var(--muted);
  font-size: 13px;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.amount {
  font-weight: 650;
  font-variant-numeric: tabular-nums;
  white-space: nowrap;
}
.entry.zahlung .amount { color: var(--pay); }
.entry.material .day { background: color-mix(in srgb, var(--material) 15%, transparent); }
.entry.material .day small { color: var(--material); }
.entry.zahlung .day { background: color-mix(in srgb, var(--pay) 15%, transparent); }
.entry.zahlung .day small, .entry.zahlung .what b { color: var(--pay); }

.empty {
  text-align: center;
  color: var(--muted);
  padding: 28px 12px;
}

footer {
  text-align: center;
  font-size: 12px;
  color: var(--muted);
  padding: 0 16px calc(env(safe-area-inset-bottom) + 20px);
}

#toast {
  position: fixed;
  left: 50%;
  bottom: calc(env(safe-area-inset-bottom) + 20px);
  transform: translate(-50%, 20px);
  max-width: calc(100% - 32px);
  background: var(--text);
  color: var(--bg);
  padding: 10px 16px;
  border-radius: 12px;
  font-size: 15px;
  opacity: 0;
  pointer-events: none;
  transition: opacity .2s, transform .2s;
}
#toast.show { opacity: 1; transform: translate(-50%, 0); }
#toast.error { background: var(--danger); color: #fff; }
