:root {
  color-scheme: light;
  --bg: #ffffff;
  --surface: #ffffff;
  --ink: #202629;
  --ink-2: #596166;
  --muted: #7c858b;
  --line: #e8ecee;
  --accent: #17775a;
  --accent-ink: #0f5c45;
  --accent-soft: #e8f4ee;
  --positive: #17775a;
  --danger: #b4312f;
  --key: #f5f6f7;
  --key-press: #eceff0;
  --shadow: 0 1px 2px rgba(21, 32, 27, 0.03);
  --font: "PingFang SC", "HarmonyOS Sans SC", "MiSans", "Microsoft YaHei UI", "Microsoft YaHei", system-ui, sans-serif;
  --num: "SF Pro Display", "Segoe UI Variable Display", "Segoe UI", "Roboto", var(--font);
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  height: 100%;
  background: var(--bg);
  color: var(--ink);
  font-family: var(--font);
  -webkit-font-smoothing: antialiased;
  -webkit-tap-highlight-color: transparent;
  /* 允许滚动和双指缩放，避免 iOS 连续点按时触发双击放大。 */
  touch-action: manipulation;
  -webkit-text-size-adjust: 100%;
  text-size-adjust: 100%;
}

button, input, select { font: inherit; color: inherit; }
button {
  cursor: pointer;
  touch-action: manipulation;
  -webkit-user-select: none;
  user-select: none;
}
:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }
.icon {
  width: 24px;
  height: 24px;
  flex: none;
  fill: none;
  stroke: currentColor;
  stroke-width: 1.7;
  stroke-linecap: round;
  stroke-linejoin: round;
}

.app {
  max-width: 480px;
  margin: 0 auto;
  min-height: 100%;
  display: flex;
  flex-direction: column;
  padding-bottom: calc(64px + env(safe-area-inset-bottom));
}

/* 顶栏 */
.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: calc(14px + env(safe-area-inset-top)) 20px 6px;
}
.topbar h1 { margin: 0; font-size: 20px; font-weight: 600; letter-spacing: 0.02em; }

.sync-status {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  border: 0;
  background: transparent;
  color: var(--muted);
  font-size: 13px;
  padding: 6px 4px;
}
.sync-status .dot { width: 7px; height: 7px; border-radius: 50%; background: var(--muted); }
.sync-status[data-state="synced"] .dot { background: var(--accent); }
.sync-status[data-state="pending"] .dot { background: #d79b1e; }
.sync-status[data-state="syncing"] .dot { background: var(--accent); animation: pulse 1s ease-in-out infinite; }
@keyframes pulse { 50% { opacity: 0.3; } }

main { flex: 1; padding: 0 16px; }
.view { display: none; }
.view.active { display: block; }

/* 支出 / 收入 切换 */
.type-toggle {
  display: inline-flex;
  background: var(--key-press);
  border-radius: 999px;
  padding: 3px;
}
.type-toggle button {
  border: 0;
  background: transparent;
  padding: 7px 20px;
  border-radius: 999px;
  font-size: 15px;
  color: var(--ink-2);
}
.type-toggle button[aria-pressed="true"] {
  background: var(--surface);
  color: var(--ink);
  font-weight: 600;
  box-shadow: 0 1px 2px rgba(0, 0, 0, 0.08);
}
.type-toggle.small button { padding: 5px 14px; font-size: 14px; }

/* 记账页 */
#view-add { display: none; }
#view-add.active {
  display: flex;
  flex-direction: column;
  align-items: center;
  min-height: calc(100dvh - 64px - 56px - env(safe-area-inset-top) - env(safe-area-inset-bottom));
}

.today-summary {
  margin: 2px 0 14px;
  font-size: 13px;
  color: var(--muted);
  min-height: 18px;
}

.amount {
  display: flex;
  align-items: baseline;
  justify-content: center;
  gap: 6px;
  margin: 18px 0 20px;
  font-family: var(--num);
  font-variant-numeric: tabular-nums;
  width: 100%;
}
.amount .currency { font-size: 26px; color: var(--muted); font-weight: 400; }
.amount-text {
  font-size: clamp(48px, 15vw, 64px);
  font-weight: 300;
  letter-spacing: -0.02em;
  line-height: 1;
  transition: color 0.15s;
}
.amount-text.is-empty { color: #b6bdc3; }
.amount.shake { animation: shake 0.3s; }
@keyframes shake { 25% { transform: translateX(-6px); } 75% { transform: translateX(6px); } }

/* 大类图标：支出两行、收入一行 */
.category-grid {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 14px 10px;
  width: 100%;
  margin-bottom: 20px;
}
.cat {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 5px;
  border: 0;
  background: transparent;
  padding: 2px 0;
  font-size: 12px;
  color: var(--ink-2);
}
.cat .category-icon {
  width: 52px;
  height: 52px;
  display: grid;
  place-items: center;
  border-radius: 17px;
  background: var(--key);
  box-shadow: none;
  font-size: 19px;
  font-weight: 600;
  color: var(--ink);
  transition: background 0.12s, color 0.12s, transform 0.12s;
}
.cat[aria-checked="true"] { color: var(--ink); font-weight: 600; }
.cat[aria-checked="true"] .category-icon {
  background: var(--accent);
  color: #fff;
  box-shadow: none;
  transform: scale(1.06);
}
.cat .icon { width: 26px; height: 26px; }
.edit-categories { gap: 8px; margin-bottom: 16px; }
.edit-categories .category-icon { width: 44px; height: 44px; border-radius: 14px; }
.edit-categories .icon { width: 23px; height: 23px; }
.date-chip:focus-within { outline: 2px solid var(--accent); outline-offset: 2px; }

.meta-row {
  display: flex;
  gap: 8px;
  width: 100%;
  margin-bottom: 12px;
}
.date-chip {
  position: relative;
  flex: none;
  display: inline-flex;
  align-items: center;
  padding: 0 14px;
  height: 42px;
  border-radius: 12px;
  background: var(--surface);
  box-shadow: inset 0 0 0 1px var(--line);
  font-size: 14px;
  color: var(--ink-2);
}
.date-chip input {
  position: absolute;
  inset: 0;
  opacity: 0;
  width: 100%;
  cursor: pointer;
}
.note-input {
  flex: 1;
  min-width: 0;
  height: 42px;
  border: 0;
  border-radius: 12px;
  padding: 0 14px;
  background: var(--surface);
  box-shadow: inset 0 0 0 1px var(--line);
  /* iOS 对小于 16px 的输入框会在聚焦时自动放大。 */
  font-size: 16px;
}
.note-input::placeholder { color: var(--muted); }
.note-input:focus { outline: none; box-shadow: inset 0 0 0 1.5px var(--accent); }

.keypad {
  margin-top: auto;
  width: 100%;
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  grid-auto-rows: 54px;
  gap: 6px;
  padding-bottom: 8px;
}
.keypad button {
  border: 0;
  border-radius: 12px;
  background: var(--key);
  box-shadow: none;
  font-family: var(--num);
  font-size: 22px;
  font-weight: 400;
  display: grid;
  place-items: center;
  touch-action: manipulation;
  user-select: none;
}
.keypad button:active { background: var(--key-press); }
.keypad .fn, .keypad [data-key="back"] { background: var(--key); font-family: var(--font); font-size: 15px; color: var(--ink-2); }
.keypad .save {
  grid-row: span 2;
  background: var(--accent);
  color: #fff;
  font-family: var(--font);
  font-size: 18px;
  font-weight: 600;
  letter-spacing: 0.1em;
}
.keypad .save:active { background: var(--accent-ink); }
.keypad .save .icon { width: 32px; height: 32px; stroke-width: 2; }
.keypad .save:disabled { background: var(--key-press); color: var(--muted); cursor: default; }

/* 月份切换 */
.month-bar {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  margin: 4px 0 10px;
}
.month-label { font-size: 17px; font-weight: 600; min-width: 120px; text-align: center; font-variant-numeric: tabular-nums; }
.month-nav {
  width: 40px;
  height: 40px;
  border: 0;
  border-radius: 50%;
  background: transparent;
  font-size: 24px;
  color: var(--ink-2);
}
.month-nav:active { background: var(--key-press); }
.month-nav:disabled { opacity: 0.25; cursor: default; }

/* 账单列表 */
.ledger-calendar { border: 1px solid var(--line); border-radius: 18px; padding: 14px 10px 10px; margin-bottom: 20px; }
.calendar-heading { display: flex; justify-content: space-between; padding: 0 4px 14px; font-size: 13px; color: var(--ink-2); }
.calendar-heading span:last-child { color: var(--muted); font-size: 12px; }
.calendar-weekdays, .calendar-grid { display: grid; grid-template-columns: repeat(7, minmax(0, 1fr)); gap: 4px; }
.calendar-weekdays { text-align: center; color: var(--muted); font-size: 11px; margin-bottom: 6px; }
.calendar-day { min-width: 0; min-height: 55px; padding: 6px 1px; border: 1px solid transparent; border-radius: 10px; background: transparent; display: flex; flex-direction: column; align-items: center; gap: 4px; font-variant-numeric: tabular-nums; }
.calendar-day:hover { background: var(--key); }
.calendar-day[aria-pressed="true"] { background: var(--accent-soft); border-color: var(--accent); }
.calendar-date { font-size: 14px; line-height: 20px; }
.calendar-day[aria-current="date"] .calendar-date { color: var(--accent); font-weight: 700; text-decoration: underline; text-underline-offset: 3px; }
.calendar-day:disabled { opacity: 0.35; cursor: default; }
.calendar-net { font-size: 11px; line-height: 14px; white-space: nowrap; }
.positive { color: var(--positive); }
.negative { color: var(--danger); }
.neutral { color: var(--muted); }
.calendar-hint { margin: 8px 0 0; text-align: center; color: var(--muted); font-size: 11px; }
.list-scope { display: flex; align-items: center; justify-content: space-between; gap: 10px; margin-bottom: 14px; min-height: 32px; }
.list-scope h2 { font-size: 14px; font-weight: 500; margin: 0; }
.list-scope button { background: transparent; border: 0; color: var(--accent); font-size: 13px; padding: 8px 0 8px 10px; flex: none; }
.list-totals {
  text-align: center;
  font-size: 13px;
  color: var(--muted);
  margin: 0 0 14px;
  font-variant-numeric: tabular-nums;
}
.day { margin-bottom: 14px; }
.day-head {
  display: flex;
  justify-content: space-between;
  gap: 10px;
  padding: 0 4px 10px;
  font-size: 13px;
  color: var(--muted);
  font-variant-numeric: tabular-nums;
}
.day-heading { min-width: 0; }
.day-date { display: flex; flex-wrap: wrap; align-items: baseline; gap: 4px 8px; color: var(--ink-2); font-size: 13px; }
.day-date span { color: var(--muted); font-size: 11px; }
.day-net-line { display: flex; align-items: baseline; gap: 7px; margin-top: 5px; }
.day-net { font-family: var(--num); font-size: 23px; font-weight: 500; line-height: 1.2; }
.day-net-line > span { font-size: 11px; }
.day-breakdown { display: flex; flex-direction: column; gap: 5px; align-self: center; text-align: right; flex: none; font-size: 11px; }
.day-items {
  background: var(--surface);
  border-radius: 16px;
  box-shadow: var(--shadow);
  border: 1px solid var(--line);
  overflow: hidden;
}
.rec {
  display: flex;
  align-items: center;
  gap: 12px;
  width: 100%;
  padding: 12px 14px;
  border: 0;
  background: transparent;
  text-align: left;
}
.rec + .rec { border-top: 1px solid var(--line); }
.rec:active { background: var(--key); }
.rec .category-icon {
  flex: none;
  width: 34px;
  height: 34px;
  display: grid;
  place-items: center;
  border-radius: 10px;
  background: var(--key-press);
  font-size: 15px;
  font-weight: 600;
}
.rec.income .category-icon { background: var(--accent-soft); color: var(--accent-ink); }
.rec .category-icon .icon { width: 21px; height: 21px; }
.rec-main { flex: 1; min-width: 0; }
.rec-cat { font-size: 15px; }
.rec-note { font-size: 12px; color: var(--muted); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; margin-top: 2px; }
.rec-amt { font-family: var(--num); font-size: 16px; font-variant-numeric: tabular-nums; }
.rec-pending { width: 6px; height: 6px; border-radius: 50%; background: #d79b1e; flex: none; }

.empty {
  text-align: center;
  color: var(--muted);
  font-size: 14px;
  padding: 56px 0;
  line-height: 1.8;
}
.empty button { margin-top: 10px; }

/* 统计 */
.balance {
  background: var(--surface);
  border-radius: 20px;
  padding: 20px 20px 16px;
  box-shadow: var(--shadow);
  border: 1px solid var(--line);
  margin-bottom: 20px;
}
.balance-main { display: flex; flex-direction: column; gap: 4px; }
.balance-label { font-size: 13px; color: var(--muted); }
.balance-value {
  font-family: var(--num);
  font-size: 40px;
  font-weight: 300;
  letter-spacing: -0.02em;
  font-variant-numeric: tabular-nums;
}
.balance-split {
  display: grid;
  grid-template-columns: 1fr 1fr;
  margin-top: 14px;
  padding-top: 14px;
  border-top: 1px solid var(--line);
}
.balance-split div { display: flex; flex-direction: column; gap: 2px; }
.balance-split span { font-size: 12px; color: var(--muted); }
.balance-split strong { font-family: var(--num); font-size: 18px; font-weight: 500; font-variant-numeric: tabular-nums; }

#stats-type { margin-bottom: 14px; }

.category-chart { padding-bottom: 20px; }
.pie-figure { margin: 0 0 16px; text-align: center; }
.pie-total { margin: 0 0 4px; color: var(--muted); font-size: 13px; }
.pie-total strong { margin-left: 6px; font-weight: 500; font-variant-numeric: tabular-nums; }
.pie-svg { display: block; width: min(100%, 240px); height: auto; margin: 0 auto; }
.pie-percentage { fill: var(--ink); font-family: var(--num); font-size: 12px; font-weight: 600; text-anchor: middle; dominant-baseline: central; }
.pie-percentage.on-accent { fill: #fff; }
.pie-percentage.single { font-size: 22px; }
.legend-row {
  display: grid;
  grid-template-columns: minmax(0, 1fr) auto 46px;
  align-items: center;
  gap: 10px;
  padding: 10px 4px;
  border-radius: 10px;
}
.legend-row + .legend-row { border-top: 1px solid var(--line); }
.legend-name { display: flex; align-items: center; gap: 10px; font-size: 14px; }
.legend-dot { flex: none; width: 9px; height: 9px; border-radius: 50%; }
.legend-amount { text-align: right; font-size: 14px; font-variant-numeric: tabular-nums; }
.legend-percent { text-align: right; color: var(--muted); font-size: 12px; font-variant-numeric: tabular-nums; }

/* 备份页 */
.panel {
  background: var(--surface);
  border-radius: 18px;
  padding: 18px;
  margin-bottom: 14px;
  box-shadow: var(--shadow);
  border: 1px solid var(--line);
}
.panel h2 { margin: 0 0 6px; font-size: 16px; }
.hint { margin: 0 0 14px; font-size: 13px; color: var(--ink-2); line-height: 1.6; }
.kv { display: grid; grid-template-columns: auto 1fr; gap: 8px 16px; margin: 0 0 16px; font-size: 14px; }
.kv dt { color: var(--muted); }
.kv dd { margin: 0; text-align: right; font-variant-numeric: tabular-nums; }

.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  height: 42px;
  padding: 0 18px;
  border: 0;
  border-radius: 12px;
  background: var(--accent);
  color: #fff;
  font-size: 15px;
  font-weight: 500;
}
.btn:active { background: var(--accent-ink); }
.btn.ghost { background: var(--key-press); color: var(--ink); }
.btn.danger { background: transparent; color: var(--danger); padding: 0 8px; }
.btn-row { display: flex; gap: 8px; margin-bottom: 8px; }
.btn-row .btn { flex: 1; }
.file-btn { width: 100%; position: relative; overflow: hidden; }
.file-btn input { position: absolute; inset: 0; opacity: 0; cursor: pointer; }

/* 底部导航 */
.tabbar {
  position: fixed;
  left: 0;
  right: 0;
  bottom: 0;
  display: flex;
  justify-content: center;
  background: color-mix(in srgb, var(--bg) 88%, transparent);
  backdrop-filter: blur(12px);
  -webkit-backdrop-filter: blur(12px);
  border-top: 1px solid var(--line);
  padding-bottom: env(safe-area-inset-bottom);
}
.tabbar button {
  flex: 1;
  max-width: 120px;
  height: 60px;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 3px;
  border: 0;
  background: transparent;
  font-size: 11px;
  color: var(--muted);
}
.tabbar svg {
  width: 25px;
  height: 25px;
  fill: none;
  stroke: currentColor;
  stroke-width: 1.8;
  stroke-linecap: round;
  stroke-linejoin: round;
}
.tabbar button[aria-current="page"] { color: var(--accent); font-weight: 600; }
.tabbar button[aria-current="page"] svg {
  background: var(--accent-soft);
  box-shadow: 0 0 0 9px var(--accent-soft);
  border-radius: 7px;
}

/* 编辑弹窗 */
dialog {
  border: 0;
  border-radius: 20px;
  padding: 20px;
  width: min(92vw, 400px);
  background: var(--surface);
  color: var(--ink);
}
dialog::backdrop { background: rgba(10, 16, 13, 0.45); }
dialog h2 { margin: 0 0 14px; font-size: 18px; }
dialog .type-toggle { margin-bottom: 14px; }
.field {
  display: grid;
  grid-template-columns: 44px 1fr;
  align-items: center;
  gap: 12px;
  font-size: 14px;
  color: var(--ink-2);
  margin-bottom: 10px;
}
.field input, .field select {
  height: 42px;
  border: 0;
  border-radius: 10px;
  padding: 0 12px;
  background: var(--key);
  box-shadow: inset 0 0 0 1px var(--line);
  color: var(--ink);
  font-size: 16px;
  width: 100%;
}
.field input:focus, .field select:focus { outline: none; box-shadow: inset 0 0 0 1.5px var(--accent); }
.field input.positive { color: var(--positive); }
.field input.negative { color: var(--danger); }
.field input.neutral { color: var(--muted); }
.form-error { margin: 0; min-height: 18px; font-size: 13px; color: var(--danger); }
.dialog-actions { display: flex; align-items: center; gap: 8px; margin-top: 18px; }
.dialog-actions .spacer { flex: 1; }

/* 提示条 */
.toast {
  position: fixed;
  left: 50%;
  bottom: calc(76px + env(safe-area-inset-bottom));
  transform: translate(-50%, 12px);
  display: flex;
  align-items: center;
  gap: 14px;
  padding: 10px 10px 10px 16px;
  border-radius: 12px;
  background: var(--ink);
  color: var(--bg);
  font-size: 14px;
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.18s, transform 0.18s;
  white-space: nowrap;
  z-index: 10;
}
.toast.show { opacity: 1; transform: translate(-50%, 0); pointer-events: auto; }
.toast:not(:has(button)) { padding-right: 16px; }
.toast button {
  border: 0;
  background: transparent;
  color: var(--accent-soft);
  font-weight: 600;
  padding: 4px 6px;
}

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { animation: none !important; transition: none !important; }
}

/* 小屏高度：压缩记账页 */
@media (max-height: 700px) {
  .today-summary { margin-bottom: 6px; }
  .amount { margin: 8px 0; }
  .amount-text { font-size: 48px; }
  .category-grid { gap: 6px; margin-bottom: 10px; }
  .cat .category-icon { width: 40px; height: 40px; border-radius: 13px; }
  .meta-row { margin-bottom: 8px; }
  .keypad { grid-auto-rows: 48px; }
}
