:root {
  --bg: #f6f4ef;
  --card: #ffffff;
  --text: #1d2521;
  --muted: #5d6963;
  --line: #e3e0d8;
  --brand: #1f5e4b;
  --brand-ink: #ffffff;
  --accent: #c8963e;
  --danger: #b3261e;
  --income: #1f5e4b;
  --donation: #8a5a12;
  --radius: 14px;
  --shadow: 0 1px 3px rgba(0, 0, 0, 0.08);
  color-scheme: light dark;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #121614;
    --card: #1b211e;
    --text: #e8ece9;
    --muted: #9fa9a3;
    --line: #2c3430;
    --brand: #4fae8f;
    --brand-ink: #0e1512;
    --accent: #e0b766;
    --danger: #f2827a;
    --income: #6cc9a8;
    --donation: #e0b766;
    --shadow: none;
  }
}

* { box-sizing: border-box; }
[hidden] { display: none !important; }
html, body { margin: 0; }
body {
  font-family: system-ui, -apple-system, "Segoe UI", Roboto, Arial, sans-serif;
  background: var(--bg);
  color: var(--text);
  line-height: 1.45;
  padding-bottom: calc(72px + env(safe-area-inset-bottom));
}
h1, h2 { margin: 0; }
h1 { font-size: 1.25rem; }
h2 { font-size: 1.05rem; margin-top: 4px; }

.topbar {
  display: flex; align-items: center; justify-content: space-between;
  padding: calc(12px + env(safe-area-inset-top)) 16px 8px;
}
main { max-width: 560px; margin: 0 auto; padding: 0 16px 16px; }
.view { display: flex; flex-direction: column; gap: 14px; }

.card { background: var(--card); border-radius: var(--radius); padding: 16px; box-shadow: var(--shadow); border: 1px solid var(--line); }
.hero { text-align: center; }
.hero-label { margin: 0; color: var(--muted); }
.hero-amount { margin: 4px 0 12px; font-size: 2.4rem; font-weight: 700; font-variant-numeric: tabular-nums; }
.progress { height: 10px; border-radius: 99px; background: var(--line); overflow: hidden; }
.progress-bar { height: 100%; width: 0; background: var(--brand); transition: width 0.4s ease; }
.stats { display: grid; grid-template-columns: repeat(3, 1fr); gap: 8px; margin: 14px 0 0; }
.stats dt { font-size: 0.78rem; color: var(--muted); }
.stats dd { margin: 0; font-weight: 600; font-variant-numeric: tabular-nums; }

.actions { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; }

button, .filebtn {
  font: inherit; border-radius: 12px; border: 1px solid transparent; padding: 12px 14px;
  cursor: pointer; min-height: 44px; text-align: center;
}
.primary { background: var(--brand); color: var(--brand-ink); font-weight: 600; }
.secondary { background: transparent; color: var(--brand); border-color: var(--brand); font-weight: 600; display: block; }
.ghost { background: transparent; color: var(--text); }
.danger { color: var(--danger); }
.small { font-size: 0.85rem; min-height: 36px; padding: 6px 10px; }
button:focus-visible, .filebtn:focus-within, input:focus-visible, select:focus-visible, a:focus-visible {
  outline: 3px solid var(--accent); outline-offset: 2px;
}

.entries { list-style: none; margin: 0; padding: 0; display: flex; flex-direction: column; gap: 8px; }
.entry {
  display: flex; align-items: center; gap: 10px; background: var(--card); border: 1px solid var(--line);
  border-radius: 12px; padding: 10px 12px; width: 100%; text-align: start; color: var(--text);
}
.entry .dot { width: 10px; height: 10px; border-radius: 50%; flex: none; }
.entry.income .dot { background: var(--income); }
.entry.donation .dot { background: var(--donation); }
.entry .main { flex: 1; min-width: 0; }
.entry .title { display: block; font-weight: 600; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.entry .sub { display: block; font-size: 0.8rem; color: var(--muted); }
.entry .amt { font-weight: 700; font-variant-numeric: tabular-nums; white-space: nowrap; }
.entry.income .amt { color: var(--income); }
.entry.donation .amt { color: var(--donation); }
.month-head { font-size: 0.85rem; color: var(--muted); margin: 8px 4px 0; }

.segmented { display: grid; grid-template-columns: repeat(3, 1fr); background: var(--card); border: 1px solid var(--line); border-radius: 12px; padding: 4px; }
.segmented button { min-height: 38px; padding: 6px; background: transparent; color: var(--muted); }
.segmented button.active { background: var(--brand); color: var(--brand-ink); }

.form { display: flex; flex-direction: column; gap: 12px; }
.form label { display: flex; flex-direction: column; gap: 4px; font-size: 0.9rem; }
.form label.check { flex-direction: row; align-items: center; gap: 8px; }
input, select {
  font: inherit; padding: 10px 12px; border-radius: 10px; border: 1px solid var(--line);
  background: var(--bg); color: var(--text); min-height: 44px; width: 100%;
}
input[type="checkbox"] { width: 22px; height: 22px; min-height: 0; accent-color: var(--brand); }
.row { display: flex; gap: 8px; flex-wrap: wrap; }
.row .primary { flex: 1; }
.error { color: var(--danger); margin: 0; min-height: 1.2em; font-size: 0.9rem; }
.muted { color: var(--muted); }
.center { text-align: center; }
p.small, a.small { margin: 0; }

dialog {
  border: none; border-radius: var(--radius); padding: 18px; width: min(520px, calc(100vw - 24px));
  background: var(--card); color: var(--text); max-height: calc(100vh - 24px); overflow: auto;
}
dialog::backdrop { background: rgba(0, 0, 0, 0.45); }
.receipt { display: flex; flex-direction: column; gap: 8px; }
.receipt img { max-width: 100%; max-height: 220px; object-fit: contain; border-radius: 10px; border: 1px solid var(--line); }
#receiptDialog img { display: block; max-width: 100%; max-height: 75vh; margin: 0 auto 12px; }
#receiptDialog .primary { width: 100%; }

.year { display: flex; flex-direction: column; gap: 8px; margin-bottom: 14px; }
.year h2 { font-size: 1.2rem; }
.year dl { display: grid; grid-template-columns: 1fr 1fr; gap: 6px 12px; margin: 0; }
.year dt { color: var(--muted); font-size: 0.85rem; }
.year dd { margin: 0; font-weight: 600; font-variant-numeric: tabular-nums; }

.tabbar {
  position: fixed; inset-inline: 0; bottom: 0; display: grid; grid-template-columns: repeat(4, 1fr);
  background: var(--card); border-top: 1px solid var(--line); padding-bottom: env(safe-area-inset-bottom);
}
.tabbar button { background: none; color: var(--muted); display: flex; flex-direction: column; align-items: center; gap: 2px; padding: 8px 4px; font-size: 0.75rem; border-radius: 0; }
.tabbar .ico { font-size: 1.2rem; line-height: 1; }
.tabbar button.active { color: var(--brand); font-weight: 600; }

.banner {
  max-width: 528px; margin: 0 auto 10px; width: calc(100% - 32px); display: flex; gap: 8px; align-items: center;
  background: #fff4dc; color: #4a3500; border-radius: 12px; padding: 10px 12px;
}
.banner span { flex: 1; }
.banner .ghost { color: inherit; }

.toast {
  position: fixed; bottom: calc(84px + env(safe-area-inset-bottom)); inset-inline: 16px; margin: 0 auto; max-width: 420px;
  background: var(--text); color: var(--bg); padding: 10px 14px; border-radius: 10px; text-align: center;
  opacity: 0; pointer-events: none; transition: opacity 0.2s;
}
.toast.show { opacity: 1; }

.print-only { display: none; }
@media print {
  body { background: #fff; color: #000; padding: 0; }
  .topbar, main, .tabbar, .banner, .toast, dialog { display: none !important; }
  .print-only { display: block; padding: 0 8mm; }
  .print-only table { width: 100%; border-collapse: collapse; margin: 8px 0 16px; font-size: 11pt; }
  .print-only th, .print-only td { border: 1px solid #999; padding: 4px 6px; text-align: start; }
  .print-only .receipt-img { max-width: 48%; max-height: 120mm; margin: 4px; border: 1px solid #ccc; }
}
@media (prefers-reduced-motion: reduce) { * { transition: none !important; } }
