:root {
  --bg: #F5F1EA;
  --card: #FFFDF9;
  --ink: #2B2B2B;
  --muted: #7A7369;
  --green: #2F5D50;
  --green-soft: #DCE7E2;
  --terra: #D98E73;
  --terra-soft: #F6E3DA;
  --sand: #E6D5B8;
  --line: #ECE5D8;
  --danger: #B5503B;
  --radius: 18px;
  --shadow: 0 6px 24px rgba(47, 93, 80, 0.08);
  --c1: #2F5D50; --c2: #D98E73; --c3: #8FA998; --c4: #C9A86A; --c5: #7E8CA3;
  --c6: #B86B5A; --c7: #5F8577; --c8: #D3B38C; --c9: #A3807A; --c10: #9CA9A0;
}
* { box-sizing: border-box; }
html, body { margin: 0; background: var(--bg); color: var(--ink); font-family: -apple-system, "PingFang TC", "Noto Sans TC", "Microsoft JhengHei", system-ui, sans-serif; -webkit-font-smoothing: antialiased; }
body { min-height: 100dvh; overflow-x: hidden; }
.app { max-width: 560px; margin: 0 auto; min-height: 100dvh; position: relative; }
.screen { min-height: 100dvh; padding-bottom: 32px; }
[hidden] { display: none !important; }
h1, h2 { margin: 0; font-weight: 600; }
button { font: inherit; }

/* 密碼頁 */
.screen-pin { display: flex; align-items: center; justify-content: center; padding: 24px 16px; }
.pin-card { width: 100%; max-width: 360px; text-align: center; }
.pin-logo { width: 88px; height: 88px; border-radius: 24px; box-shadow: var(--shadow); }
.pin-title { font-size: 24px; margin-top: 14px; color: var(--green); }
.pin-sub { color: var(--muted); margin: 6px 0 18px; }
.pin-dots { display: flex; justify-content: center; gap: 12px; height: 16px; margin-bottom: 18px; }
.pin-dots i { width: 14px; height: 14px; border-radius: 50%; border: 2px solid var(--green); display: block; }
.pin-dots i.on { background: var(--green); }
.pin-input { position: absolute; opacity: 0; pointer-events: none; width: 1px; height: 1px; }
.keypad { display: grid; grid-template-columns: repeat(3, 1fr); gap: 10px; }
.keypad button { height: 58px; border: 0; border-radius: 16px; background: var(--card); box-shadow: var(--shadow); font-size: 22px; color: var(--ink); }
.keypad button:active { transform: scale(0.96); }
.keypad button.ok { background: var(--green); color: #fff; }
.keypad button.del { color: var(--muted); }
.pin-msg { min-height: 22px; color: var(--danger); margin-top: 14px; font-size: 14px; }

/* 頂欄 */
.topbar { position: sticky; top: 0; z-index: 5; display: flex; align-items: center; justify-content: space-between; padding: 14px 16px 10px; background: color-mix(in srgb, var(--bg) 88%, transparent); backdrop-filter: blur(10px); }
.month-nav { display: flex; align-items: center; gap: 6px; }
.month-title { font-size: 18px; letter-spacing: 0.02em; }
.icon-btn { width: 40px; height: 40px; border-radius: 12px; border: 0; background: var(--card); box-shadow: var(--shadow); color: var(--green); font-size: 22px; line-height: 1; display: inline-flex; align-items: center; justify-content: center; }
.icon-btn:active { transform: scale(0.95); }

.payer-tabs { display: flex; gap: 8px; padding: 4px 16px 10px; }
.payer-tabs button { flex: 1; padding: 9px 0; border-radius: 999px; border: 1px solid var(--line); background: var(--card); color: var(--muted); }
.payer-tabs button[aria-selected="true"] { background: var(--green); border-color: var(--green); color: #fff; }

.content { padding: 0 16px; display: flex; flex-direction: column; gap: 14px; }
.card { background: var(--card); border-radius: var(--radius); box-shadow: var(--shadow); padding: 18px 16px; }
.card-head { display: flex; justify-content: space-between; align-items: baseline; margin-bottom: 12px; }
.card-head h2 { font-size: 15px; color: var(--green); }
.hint { font-size: 12px; color: var(--muted); }
.label { margin: 0; font-size: 13px; color: var(--muted); }
.muted { color: var(--muted); }

.hero { background: linear-gradient(145deg, var(--green) 0%, #3E7366 100%); color: #fff; }
.hero .label { color: rgba(255,255,255,0.75); }
.hero-amount { margin: 6px 0 4px; font-size: 40px; font-weight: 700; letter-spacing: -0.02em; font-variant-numeric: tabular-nums; }
.hero-amount .currency { font-size: 16px; font-weight: 500; opacity: 0.8; margin-right: 4px; }
.hero-delta { margin: 0; font-size: 14px; color: rgba(255,255,255,0.85); }
.hero-count { margin: 2px 0 0; font-size: 12px; color: rgba(255,255,255,0.65); }

/* 圓餅 */
.pie-wrap { position: relative; width: 190px; height: 190px; margin: 4px auto 14px; }
.pie { width: 100%; height: 100%; transform: rotate(-90deg); }
.pie circle { fill: none; stroke-width: 18; cursor: pointer; }
.pie-center { position: absolute; inset: 0; display: flex; align-items: center; justify-content: center; font-size: 15px; color: var(--muted); pointer-events: none; }
.cat-list { list-style: none; margin: 0; padding: 0; display: flex; flex-direction: column; gap: 8px; }
.cat-list li { display: grid; grid-template-columns: 12px 1fr auto; align-items: center; gap: 10px; padding: 10px 8px; border-radius: 12px; cursor: pointer; }
.cat-list li:active { background: var(--bg); }
.cat-list .dot { width: 12px; height: 12px; border-radius: 50%; }
.cat-list .name { font-size: 15px; }
.cat-list .bar { height: 5px; background: var(--line); border-radius: 3px; margin-top: 6px; overflow: hidden; }
.cat-list .bar i { display: block; height: 100%; border-radius: 3px; transform-origin: left; }
.cat-list .amt { font-variant-numeric: tabular-nums; font-weight: 600; }
.cat-list .pct { font-size: 12px; color: var(--muted); text-align: right; }

/* 預算環 */
.rings { display: grid; grid-template-columns: repeat(3, 1fr); gap: 10px; }
.ring { text-align: center; }
.ring svg { width: 84px; height: 84px; transform: rotate(-90deg); }
.ring circle { fill: none; stroke-width: 8; }
.ring .track { stroke: var(--line); }
.ring .name { font-size: 13px; margin-top: 2px; }
.ring .val { font-size: 12px; color: var(--muted); font-variant-numeric: tabular-nums; }

/* 趨勢 */
.trend { display: grid; grid-template-columns: repeat(6, 1fr); gap: 8px; align-items: end; height: 150px; }
.trend .col { display: flex; flex-direction: column; align-items: center; justify-content: flex-end; height: 100%; gap: 6px; }
.trend .bar { width: 100%; max-width: 34px; border-radius: 8px 8px 4px 4px; background: var(--green-soft); transform-origin: bottom; }
.trend .col.cur .bar { background: var(--green); }
.trend .m { font-size: 11px; color: var(--muted); }
.trend .v { font-size: 11px; color: var(--ink); font-variant-numeric: tabular-nums; }

/* 分人 */
.payer-bars { display: flex; flex-direction: column; gap: 10px; }
.payer-bars .row { display: grid; grid-template-columns: 64px 1fr auto; gap: 10px; align-items: center; }
.payer-bars .bar { height: 12px; border-radius: 6px; background: var(--line); overflow: hidden; }
.payer-bars .bar i { display: block; height: 100%; border-radius: 6px; background: var(--terra); transform-origin: left; }
.payer-bars .amt { font-variant-numeric: tabular-nums; font-weight: 600; }

/* 下鑽 */
.crumbs { display: flex; gap: 6px; padding: 0 16px 10px; font-size: 13px; color: var(--muted); flex-wrap: wrap; }
.crumbs button { border: 0; background: none; color: var(--green); padding: 0; font-size: 13px; }
.merchant-list, .entry-list { list-style: none; margin: 0; padding: 0; }
.merchant-list li { display: grid; grid-template-columns: 1fr auto; gap: 8px; padding: 12px 6px; border-bottom: 1px solid var(--line); cursor: pointer; }
.merchant-list li:last-child { border-bottom: 0; }
.merchant-list li[aria-selected="true"] { background: var(--bg); border-radius: 10px; }
.merchant-list .name { font-size: 15px; }
.merchant-list .sub { font-size: 12px; color: var(--muted); }
.merchant-list .bar { grid-column: 1 / -1; height: 4px; border-radius: 2px; background: var(--line); overflow: hidden; }
.merchant-list .bar i { display: block; height: 100%; background: var(--green); transform-origin: left; }
.merchant-list .amt { font-variant-numeric: tabular-nums; font-weight: 600; text-align: right; }
.entry-list li { display: grid; grid-template-columns: 1fr auto; gap: 2px 10px; padding: 12px 6px; border-bottom: 1px solid var(--line); cursor: pointer; }
.entry-list li:last-child { border-bottom: 0; }
.entry-list .top { font-size: 15px; }
.entry-list .meta { font-size: 12px; color: var(--muted); }
.entry-list .note { grid-column: 1 / -1; font-size: 13px; color: var(--ink); background: var(--bg); border-radius: 8px; padding: 6px 8px; margin-top: 4px; }
.entry-list .amt { font-variant-numeric: tabular-nums; font-weight: 600; text-align: right; }
.entry-list .amt.neg { color: var(--terra); }
.tag { font-size: 11px; padding: 1px 6px; border-radius: 6px; background: var(--sand); color: var(--ink); margin-left: 6px; }
.empty { text-align: center; color: var(--muted); padding: 24px 0; }

/* 說明 */
.help h2 { font-size: 15px; color: var(--green); margin: 14px 0 6px; }
.help h2:first-child { margin-top: 0; }
.help p { margin: 0; line-height: 1.6; font-size: 14px; }

/* 底部表單 */
.sheet { position: fixed; inset: 0; z-index: 20; }
.sheet-backdrop { position: absolute; inset: 0; background: rgba(43,43,43,0.35); }
.sheet-panel { position: absolute; left: 0; right: 0; bottom: 0; margin: 0 auto; max-width: 560px; background: var(--card); border-radius: 22px 22px 0 0; padding: 10px 18px calc(18px + env(safe-area-inset-bottom)); box-shadow: 0 -8px 30px rgba(0,0,0,0.12); display: flex; flex-direction: column; gap: 10px; max-height: 92dvh; overflow: auto; }
.sheet-panel.small { gap: 12px; }
.sheet-handle { width: 40px; height: 4px; border-radius: 2px; background: var(--line); margin: 2px auto 6px; }
.sheet-panel h2 { font-size: 17px; color: var(--green); }
.sheet-panel label { display: flex; flex-direction: column; gap: 4px; font-size: 13px; color: var(--muted); }
.sheet-panel input, .sheet-panel select { font: inherit; font-size: 16px; padding: 10px 12px; border-radius: 12px; border: 1px solid var(--line); background: var(--bg); color: var(--ink); }
.sheet-actions { display: flex; gap: 10px; margin-top: 6px; }
.btn { flex: 1; padding: 12px; border-radius: 14px; border: 0; font-size: 16px; }
.btn.primary { background: var(--green); color: #fff; }
.btn.ghost { background: var(--bg); color: var(--ink); }
.btn.danger { background: var(--terra-soft); color: var(--danger); }
.form-msg { margin: 0; min-height: 18px; color: var(--danger); font-size: 13px; }

.toast { position: fixed; left: 50%; bottom: 28px; transform: translateX(-50%); background: var(--ink); color: #fff; padding: 10px 16px; border-radius: 999px; font-size: 14px; z-index: 30; }
.loading { position: fixed; inset: 0; display: flex; align-items: center; justify-content: center; z-index: 25; pointer-events: none; }
.spinner { width: 34px; height: 34px; border-radius: 50%; border: 3px solid var(--line); border-top-color: var(--green); animation: spin 0.8s linear infinite; }
@keyframes spin { to { transform: rotate(360deg); } }
.skeleton { background: linear-gradient(90deg, var(--line), #f7f3ec, var(--line)); background-size: 200% 100%; animation: shimmer 1.2s infinite; border-radius: 8px; }
@keyframes shimmer { to { background-position: -200% 0; } }

@media (prefers-reduced-motion: reduce) {
  .spinner, .skeleton { animation-duration: 0.01s; }
}
@media (min-width: 561px) {
  .app { padding: 0 0 24px; }
}
