[hidden] { display: none !important; }

:root {
  --bg: #f5f6fa;
  --card: #ffffff;
  --text: #1c1d22;
  --muted: #6b6f7b;
  --line: #e5e7ee;
  --accent: #4f46e5;
  --accent-soft: #eef0ff;
  --warn: #d97706;
  --warn-soft: #fff4e0;
  --danger: #dc2626;
  --ok: #059669;
  --shadow: 0 1px 3px rgba(0, 0, 0, .06), 0 4px 14px rgba(0, 0, 0, .04);
  --radius: 14px;
  color-scheme: light;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #111318;
    --card: #1b1e25;
    --text: #eceef3;
    --muted: #9aa0ad;
    --line: #2b2f39;
    --accent: #8b85ff;
    --accent-soft: #24234a;
    --warn: #f5a524;
    --warn-soft: #3a2c12;
    --danger: #f87171;
    --ok: #34d399;
    --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-family: -apple-system, BlinkMacSystemFont, "PingFang TC", "Noto Sans TC", "Microsoft JhengHei", sans-serif;
  font-size: 16px;
  line-height: 1.45;
  padding-bottom: calc(72px + env(safe-area-inset-bottom));
  -webkit-tap-highlight-color: transparent;
}
button, input, select { font: inherit; color: inherit; }
button { cursor: pointer; }

.topbar {
  position: sticky; top: 0; z-index: 5;
  display: flex; align-items: center; justify-content: space-between;
  padding: calc(10px + env(safe-area-inset-top)) 16px 10px;
  background: color-mix(in srgb, var(--bg) 88%, transparent);
  backdrop-filter: blur(10px);
  -webkit-backdrop-filter: blur(10px);
}
.topbar h1 { font-size: 20px; margin: 0; }
.icon-btn {
  width: 38px; height: 38px; border-radius: 50%;
  border: none; background: var(--accent-soft); color: var(--accent);
  font-size: 22px; line-height: 1; display: grid; place-items: center;
}

main { max-width: 640px; margin: 0 auto; padding: 0 16px; }
h2 { font-size: 17px; margin: 24px 0 10px; }
h2 small { color: var(--muted); font-weight: normal; font-size: 13px; margin-left: 4px; }
h3 { margin: 0 0 10px; font-size: 16px; }
.hint { color: var(--muted); font-size: 13px; margin: 8px 0; }
.center { text-align: center; }

.card {
  background: var(--card); border-radius: var(--radius);
  box-shadow: var(--shadow); padding: 14px; margin-bottom: 12px;
}

/* 總額 */
.totals { display: grid; gap: 10px; grid-template-columns: 1fr 1fr; margin-top: 4px; }
.total-card {
  background: var(--card); border-radius: var(--radius); box-shadow: var(--shadow);
  padding: 16px 14px;
}
.total-card.primary { background: var(--accent); color: #fff; }
.total-card .label { font-size: 13px; opacity: .85; }
.total-card .big {
  font-size: clamp(24px, 7.5vw, 36px); font-weight: 700; letter-spacing: -.5px;
  font-variant-numeric: tabular-nums; margin-top: 2px; white-space: nowrap;
}
.total-card .big .cur { font-size: .5em; font-weight: 600; margin-right: 2px; opacity: .85; }

/* 清單 */
.list { display: flex; flex-direction: column; gap: 8px; }
.item {
  display: flex; align-items: center; gap: 12px;
  background: var(--card); border-radius: var(--radius); box-shadow: var(--shadow);
  padding: 12px 14px; border: none; width: 100%; text-align: left;
}
.item .dot { width: 10px; height: 10px; border-radius: 50%; flex: none; }
.item .main { flex: 1; min-width: 0; }
.item .name, .item .sub, .item .amt { display: block; }
.item .name { font-weight: 600; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.item .sub { font-size: 13px; color: var(--muted); }
.item .right { text-align: right; flex: none; font-variant-numeric: tabular-nums; }
.item .amt { font-weight: 600; }
.item.cancelled { opacity: .5; }
.item.cancelled .name { text-decoration: line-through; }
.badge {
  display: inline-block; font-size: 11px; padding: 1px 7px; border-radius: 99px;
  background: var(--accent-soft); color: var(--accent); margin-left: 6px; vertical-align: 2px;
}
.badge.warn { background: var(--warn-soft); color: var(--warn); }
.badge.muted { background: var(--line); color: var(--muted); }
.badge.todo { background: var(--warn-soft); color: var(--warn); }
.empty { color: var(--muted); text-align: center; padding: 18px; background: var(--card); border-radius: var(--radius); }

.seg { display: flex; gap: 6px; overflow-x: auto; margin: 4px 0 12px; scrollbar-width: none; }
.seg button {
  flex: none; border: 1px solid var(--line); background: var(--card);
  padding: 6px 14px; border-radius: 99px; font-size: 14px;
}
.seg button.on { background: var(--accent); border-color: var(--accent); color: #fff; }

/* 圓餅圖 */
.chart-card { display: flex; align-items: center; gap: 16px; }
#pie { width: 130px; height: 130px; flex: none; }
.legend { list-style: none; padding: 0; margin: 0; flex: 1; font-size: 14px; }
.legend li { display: flex; align-items: center; gap: 8px; padding: 3px 0; }
.legend .sw { width: 10px; height: 10px; border-radius: 3px; flex: none; }
.legend .v { margin-left: auto; color: var(--muted); font-variant-numeric: tabular-nums; }

/* 月曆 */
.cal-head { display: flex; align-items: center; justify-content: space-between; margin: 4px 0 10px; }
#calTitle { font-weight: 700; font-size: 18px; }
.cal-grid { display: grid; grid-template-columns: repeat(7, 1fr); gap: 4px; }
.cal-week div { text-align: center; font-size: 12px; color: var(--muted); padding-bottom: 4px; }
.cal-cell {
  aspect-ratio: 1 / 1.1; background: var(--card); border-radius: 10px; border: 2px solid transparent;
  padding: 4px 2px; display: flex; flex-direction: column; align-items: center; gap: 3px;
  font-size: 13px; min-width: 0;
}
.cal-cell.blank { background: transparent; }
.cal-cell.today { border-color: var(--accent); }
.cal-cell.sel { background: var(--accent-soft); }
.cal-cell .dots { display: flex; flex-wrap: wrap; justify-content: center; gap: 2px; }
.cal-cell .dots i { width: 6px; height: 6px; border-radius: 50%; }
.cal-cell .amt { font-size: 10px; color: var(--muted); font-variant-numeric: tabular-nums; }
.cal-sum { text-align: right; color: var(--muted); font-size: 13px; margin-top: 8px; }

/* 審視 */
.review-card { text-align: center; padding: 24px 18px; }
.review-card .progress { color: var(--muted); font-size: 13px; }
.review-card .rname { font-size: 26px; font-weight: 700; margin: 12px 0 4px; }
.review-card .q { font-size: 20px; margin: 22px 0 16px; }
.review-card .save { color: var(--ok); font-weight: 600; }
.review-actions { display: grid; gap: 10px; }
.bar { height: 6px; background: var(--line); border-radius: 99px; overflow: hidden; margin-top: 8px; }
.bar > div { height: 100%; background: var(--accent); transition: width .2s; }
.savings { text-align: center; }
.savings .big { font-size: 34px; font-weight: 700; color: var(--ok); font-variant-numeric: tabular-nums; }

/* 按鈕 & 表單 */
.btn {
  display: block; width: 100%; text-align: center;
  border: none; background: var(--accent); color: #fff;
  padding: 12px 16px; border-radius: 12px; font-weight: 600; margin-top: 8px;
}
.row .btn { width: auto; margin-top: 0; }
.btn.ghost { background: var(--accent-soft); color: var(--accent); }
.btn.danger { background: var(--danger); color: #fff; }
.btn.danger.ghost { background: transparent; color: var(--danger); padding-left: 4px; }
.btn.warn { background: var(--warn); color: #fff; }
.btn.ok { background: var(--ok); color: #fff; }
.form label.btn { display: block; color: var(--accent); font-size: 16px; margin-bottom: 0; cursor: pointer; }
.cal-cell { color: var(--text); }

.form label { display: flex; flex-direction: column; gap: 4px; font-size: 13px; color: var(--muted); margin-bottom: 12px; }
.form input, .form select {
  width: 100%; padding: 10px 12px; border-radius: 10px;
  border: 1px solid var(--line); background: var(--bg); color: var(--text); font-size: 16px;
  min-height: 44px;
}
.row { display: flex; gap: 10px; align-items: flex-end; }
.row > label { flex: none; min-width: 0; }
.row > .grow { flex: 1; }
.actions { margin-top: 6px; align-items: center; }

dialog {
  border: none; border-radius: 18px; padding: 18px; width: min(560px, calc(100vw - 24px));
  background: var(--card); color: var(--text); max-height: calc(100dvh - 40px);
}
dialog::backdrop { background: rgba(0, 0, 0, .45); }

/* 底部頁籤 */
.tabbar {
  position: fixed; left: 0; right: 0; bottom: 0; z-index: 10;
  display: grid; grid-template-columns: repeat(5, 1fr);
  background: var(--card); border-top: 1px solid var(--line);
  padding-bottom: env(safe-area-inset-bottom);
}
.tabbar button {
  border: none; background: none; padding: 7px 0 8px; font-size: 11px; color: var(--muted);
  display: flex; flex-direction: column; align-items: center; gap: 1px;
}
.tabbar button span { font-size: 20px; filter: grayscale(1); opacity: .7; }
.tabbar button.on { color: var(--accent); font-weight: 600; }
.tabbar button.on span { filter: none; opacity: 1; }

#toast {
  position: fixed; left: 50%; bottom: calc(84px + env(safe-area-inset-bottom));
  transform: translateX(-50%) translateY(20px); opacity: 0; pointer-events: none;
  background: var(--text); color: var(--bg); padding: 10px 16px; border-radius: 99px;
  font-size: 14px; transition: .2s; max-width: calc(100vw - 32px); z-index: 20;
}
#toast.show { opacity: 1; transform: translateX(-50%) translateY(0); }

/* ===== 記帳 / 統計 ===== */
.seg-full { display: grid; grid-auto-flow: column; grid-auto-columns: 1fr; }
.seg-full button { text-align: center; }
.ledger-card { display: flex; flex-direction: column; gap: 4px; }
.ledger-nums { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; }
.label { font-size: 13px; color: var(--muted); }
.total-card .label { color: inherit; }
.mid { font-size: 22px; font-weight: 700; font-variant-numeric: tabular-nums; }
.item .emoji {
  width: 36px; height: 36px; border-radius: 10px; flex: none;
  display: grid; place-items: center; font-size: 18px;
}
.period-title { font-weight: 700; font-size: 18px; }
.ledger-total {
  display: flex; justify-content: space-between; align-items: baseline;
  color: var(--muted); font-size: 14px; margin-bottom: 8px;
}
.ledger-total strong { color: var(--text); font-size: 24px; font-variant-numeric: tabular-nums; }
.day-head {
  display: flex; justify-content: space-between; color: var(--muted);
  font-size: 13px; margin: 14px 4px 6px; font-variant-numeric: tabular-nums;
}
.empty .btn { width: auto; display: inline-block; margin-top: 12px; }

.stats-controls { display: flex; justify-content: space-between; align-items: center; gap: 10px; }
.stats-controls .seg { margin: 4px 0; }
.toggle { display: flex; align-items: center; gap: 6px; font-size: 14px; color: var(--muted); }
.toggle input { width: 18px; height: 18px; accent-color: var(--accent); }
.hero-num { font-size: 34px; font-weight: 700; font-variant-numeric: tabular-nums; letter-spacing: -.5px; }
.bars { width: 100%; height: auto; display: block; }
.bars .grid { stroke: var(--line); stroke-width: 1; stroke-dasharray: 2 3; }
.bars .baseline { stroke: var(--muted); stroke-width: 1; }
.bars .axis { font-size: 9px; fill: var(--muted); font-variant-numeric: tabular-nums; }
.bars [data-bar], .pie-seg, .legend.tappable li { cursor: pointer; }
.bar-info { font-size: 14px; min-height: 22px; margin-bottom: 4px; color: var(--muted); }
.bar-info strong { color: var(--text); }
#statsPie { width: 130px; height: 130px; flex: none; }
.legend li.sel { font-weight: 600; }
.split-bar { display: flex; gap: 2px; height: 14px; border-radius: 4px; overflow: hidden; margin: 4px 0 12px; }
.split-legend { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; }
.split-legend .sw { display: inline-block; width: 10px; height: 10px; border-radius: 3px; margin-right: 6px; }
.top-title { margin: 16px 0 6px; font-size: 14px; color: var(--muted); font-weight: 600; }
.top-list { margin: 0; padding-left: 22px; font-size: 14px; }
.top-list li { padding: 4px 0; }
.top-list li > span:first-child { display: inline-block; }
.top-list li { display: list-item; }
.top-list .v { float: right; color: var(--muted); font-variant-numeric: tabular-nums; }

/* 記帳表單 */
.amount-input { font-size: 28px !important; font-weight: 700; text-align: right; font-variant-numeric: tabular-nums; }
.field-label { font-size: 13px; color: var(--muted); margin-bottom: 6px; }
.cat-grid { display: grid; grid-template-columns: repeat(4, 1fr); gap: 6px; margin-bottom: 12px; }
.cat-grid button {
  border: 2px solid var(--line); background: var(--bg); border-radius: 12px;
  padding: 6px 2px; font-size: 12px; display: flex; flex-direction: column; align-items: center; gap: 2px;
  min-height: 56px;
}
.cat-grid button span { font-size: 20px; }
.cat-grid button.on { border-color: var(--c); background: color-mix(in srgb, var(--c) 14%, var(--card)); font-weight: 600; }
.meal-row { margin-bottom: 12px; }
.meal-row[hidden] { display: none; }
.chips-head { display: flex; justify-content: space-between; align-items: center; font-size: 13px; color: var(--muted); margin: -4px 0 6px; }
.chips-head[hidden] { display: none; }
.link-btn { border: none; background: none; color: var(--accent); font-size: 13px; padding: 4px; }
.chips { display: flex; flex-wrap: wrap; gap: 6px; margin-bottom: 12px; }
.chip {
  border: 1px solid var(--line); background: var(--bg); border-radius: 99px;
  padding: 6px 12px; font-size: 14px;
}
.chip small { color: var(--muted); }
.chip.manage { border-color: var(--danger); }
.chip b { color: var(--danger); font-weight: normal; margin-left: 2px; }

/* 分類管理 */
.cat-row { display: flex; gap: 6px; align-items: center; margin-bottom: 8px; }
.form .cat-row input { min-height: 40px; padding: 6px 8px; }
.form .cat-row .cat-emoji { width: 48px; flex: none; text-align: center; }
.form .cat-row .cat-name { flex: 1; min-width: 0; }
.form .cat-row .cat-color { width: 44px; flex: none; padding: 2px; }
.cat-del { width: 36px; flex: none; border: none; background: none; font-size: 18px; text-align: center; }
