:root {
  --bg: #f5f6f8;
  --card: #ffffff;
  --line: #e7e9ee;
  --line-soft: #f0f1f4;
  --ink: #17181c;
  --ink-2: #4b5058;
  --muted: #8a9099;
  --accent: #3b6ef5;
  --accent-soft: rgba(59, 110, 245, 0.08);
  --income: #12a06a;
  --expense: #d94a3d;
  --radius: 16px;
  --safe-bottom: env(safe-area-inset-bottom, 0px);
  --safe-top: env(safe-area-inset-top, 0px);
}

* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }

html, body {
  margin: 0;
  background: var(--bg);
  color: var(--ink);
  font-family: -apple-system, BlinkMacSystemFont, "SF Pro Text", "PingFang SC",
    "Hiragino Sans GB", "Microsoft YaHei", system-ui, sans-serif;
  -webkit-font-smoothing: antialiased;
  font-size: 15px;
  line-height: 1.5;
}

.app {
  max-width: 560px;
  margin: 0 auto;
  padding: calc(var(--safe-top) + 8px) 16px calc(96px + var(--safe-bottom));
}

/* ---------- 顶栏 ---------- */
.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 8px 2px 16px;
}
.topbar h1 {
  margin: 0;
  font-size: 22px;
  font-weight: 650;
  letter-spacing: -0.01em;
}
.icon-btn {
  width: 36px;
  height: 36px;
  border: 0;
  border-radius: 10px;
  background: transparent;
  color: var(--ink-2);
  font-size: 18px;
  line-height: 1;
  cursor: pointer;
}
.icon-btn:active { background: var(--line-soft); }

/* ---------- 周期 ---------- */
.period { margin-bottom: 14px; }

.segment {
  display: inline-flex;
  padding: 3px;
  gap: 2px;
  background: #eceef2;
  border-radius: 12px;
}
.segment button {
  border: 0;
  background: transparent;
  padding: 7px 16px;
  font-size: 14px;
  font-family: inherit;
  color: var(--ink-2);
  border-radius: 9px;
  cursor: pointer;
  transition: background 0.15s, color 0.15s;
}
.segment button.active {
  background: #fff;
  color: var(--ink);
  font-weight: 600;
  box-shadow: 0 1px 2px rgba(16, 20, 30, 0.08);
}
.segment.small button { padding: 5px 12px; font-size: 13px; }

.period-nav {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  margin-top: 10px;
  min-height: 36px;
}
#period-label {
  font-size: 15px;
  font-weight: 600;
  color: var(--ink);
  font-variant-numeric: tabular-nums;
}

/* ---------- 卡片 ---------- */
.card {
  background: var(--card);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  padding: 16px;
  margin-bottom: 12px;
}

.summary {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 8px;
  padding: 18px 16px;
}
.metric { display: flex; flex-direction: column; gap: 6px; }
.metric-label { font-size: 12px; color: var(--muted); }
.metric-value {
  font-size: 20px;
  font-weight: 640;
  letter-spacing: -0.02em;
  font-variant-numeric: tabular-nums;
}
.metric-value.income { color: var(--income); }
.metric-value.expense { color: var(--expense); }

.card-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 12px;
}
.card-head h2 {
  margin: 0;
  font-size: 15px;
  font-weight: 600;
  color: var(--ink);
}

/* ---------- 日历 ---------- */
.calendar { padding: 14px 12px 10px; }
.cal-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0 4px 8px;
}
#cal-label {
  font-size: 15px;
  font-weight: 600;
  font-variant-numeric: tabular-nums;
}
.cal-weekdays {
  display: grid;
  grid-template-columns: repeat(7, 1fr);
  padding: 0 0 4px;
}
.cal-weekdays span {
  text-align: center;
  font-size: 11px;
  color: var(--muted);
}
.cal-grid {
  display: grid;
  grid-template-columns: repeat(7, 1fr);
  gap: 2px;
}
.cal-cell {
  position: relative;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 1px;
  height: 46px;
  border: 0;
  border-radius: 10px;
  background: transparent;
  font-family: inherit;
  color: var(--ink);
  cursor: pointer;
  padding: 0;
}
.cal-cell:active { background: var(--line-soft); }
.cal-cell .cal-day {
  font-size: 13px;
  line-height: 1.1;
  font-variant-numeric: tabular-nums;
}
.cal-cell .cal-amt {
  font-size: 9px;
  line-height: 1.1;
  color: var(--expense);
  font-variant-numeric: tabular-nums;
  max-width: 100%;
  overflow: hidden;
  white-space: nowrap;
}
.cal-cell.dim { color: var(--muted); opacity: 0.45; }
.cal-cell.today { box-shadow: inset 0 0 0 1.5px var(--accent); }
.cal-cell.has-income::after {
  content: '';
  position: absolute;
  top: 6px;
  right: 7px;
  width: 4px;
  height: 4px;
  border-radius: 50%;
  background: var(--income);
}
.cal-cell.selected {
  background: var(--accent);
  color: #fff;
}
.cal-cell.selected .cal-amt { color: rgba(255, 255, 255, 0.9); }
.cal-cell.selected.has-income::after { background: #fff; }
.cal-hint {
  margin: 8px 2px 0;
  font-size: 12px;
  color: var(--muted);
  min-height: 16px;
  font-variant-numeric: tabular-nums;
}

/* ---------- 图表 ---------- */
.chart { width: 100%; }
.chart svg { display: block; width: 100%; height: auto; }
.chart-caption {
  margin: 8px 0 0;
  font-size: 12px;
  color: var(--muted);
  min-height: 16px;
}

/* ---------- 分类 ---------- */
.cat-list { list-style: none; margin: 0; padding: 0; }
.cat-item { padding: 8px 0; }
.cat-item + .cat-item { border-top: 1px solid var(--line-soft); }
.cat-row {
  display: flex;
  align-items: baseline;
  justify-content: space-between;
  gap: 12px;
  margin-bottom: 6px;
}
.cat-name { font-size: 14px; color: var(--ink-2); }
.cat-amount {
  font-size: 14px;
  font-weight: 600;
  font-variant-numeric: tabular-nums;
}
.cat-bar {
  height: 6px;
  border-radius: 99px;
  background: var(--line-soft);
  overflow: hidden;
}
.cat-bar > i {
  display: block;
  height: 100%;
  border-radius: 99px;
  background: var(--accent);
  opacity: 0.85;
}

.empty {
  padding: 28px 8px;
  text-align: center;
  color: var(--muted);
  font-size: 13px;
}

/* ---------- 明细 ---------- */
.list-section { margin-top: 22px; }
.section-title {
  margin: 0 0 10px;
  font-size: 14px;
  font-weight: 600;
  color: var(--ink-2);
  display: flex;
  align-items: baseline;
  gap: 8px;
}
.section-title .count { font-size: 12px; color: var(--muted); font-weight: 400; }

.day-group {
  background: var(--card);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  margin-bottom: 10px;
  overflow: hidden;
}
.day-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 10px 16px;
  background: #fbfbfc;
  border-bottom: 1px solid var(--line-soft);
  font-size: 13px;
  color: var(--ink-2);
}
.day-head strong { font-weight: 600; font-variant-numeric: tabular-nums; }
.day-head .day-amount { font-variant-numeric: tabular-nums; }
.day-head .day-amount .expense { color: var(--expense); }

.record {
  display: flex;
  align-items: center;
  gap: 12px;
  width: 100%;
  padding: 12px 16px;
  border: 0;
  background: transparent;
  font-family: inherit;
  text-align: left;
  cursor: pointer;
}
.record + .record { border-top: 1px solid var(--line-soft); }
.record:active { background: #fafbfd; }
.record-main { flex: 1; min-width: 0; }
.record-title {
  font-size: 14px;
  color: var(--ink);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.record-sub {
  font-size: 12px;
  color: var(--muted);
  margin-top: 2px;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.record-amount {
  font-size: 15px;
  font-weight: 600;
  font-variant-numeric: tabular-nums;
  white-space: nowrap;
}
.record-amount.in { color: var(--income); }
.record-amount.out { color: var(--ink); }

/* ---------- 主按钮 ---------- */
.fab {
  position: fixed;
  left: 50%;
  transform: translateX(-50%);
  bottom: calc(20px + var(--safe-bottom));
  width: min(528px, calc(100% - 32px));
  height: 52px;
  border: 0;
  border-radius: 14px;
  background: var(--accent);
  color: #fff;
  font-size: 16px;
  font-weight: 600;
  font-family: inherit;
  cursor: pointer;
  box-shadow: 0 6px 20px rgba(59, 110, 245, 0.28);
}
.fab:active { transform: translateX(-50%) scale(0.99); }

/* ---------- 底部弹层 ---------- */
.sheet-backdrop {
  position: fixed;
  inset: 0;
  background: rgba(17, 20, 26, 0.28);
  z-index: 20;
}
.sheet {
  position: fixed;
  left: 50%;
  bottom: 0;
  transform: translateX(-50%);
  width: min(560px, 100%);
  background: #fff;
  border-radius: 20px 20px 0 0;
  z-index: 21;
  box-shadow: 0 -8px 30px rgba(16, 20, 30, 0.12);
  animation: sheet-in 0.22s ease-out;
  max-height: 92vh;
  overflow-y: auto;
}
@keyframes sheet-in {
  from { transform: translate(-50%, 24px); opacity: 0.6; }
  to { transform: translate(-50%, 0); opacity: 1; }
}
.sheet-handle {
  width: 38px;
  height: 4px;
  border-radius: 99px;
  background: #e2e4e9;
  margin: 10px auto 2px;
}
.sheet-body { padding: 8px 16px calc(16px + var(--safe-bottom)); }

.type-seg { display: flex; width: 100%; margin-bottom: 14px; }
.type-seg button { flex: 1; padding: 9px 0; }

.amount-field {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 12px 14px;
  border: 1px solid var(--line);
  border-radius: 12px;
  margin-bottom: 14px;
}
.amount-field span { font-size: 13px; color: var(--muted); }
.amount-field input {
  flex: 1;
  border: 0;
  outline: none;
  font-size: 26px;
  font-weight: 640;
  font-family: inherit;
  color: var(--ink);
  font-variant-numeric: tabular-nums;
  background: transparent;
  min-width: 0;
}

.chips { display: flex; flex-wrap: wrap; gap: 8px; margin-bottom: 16px; }
.chip {
  border: 1px solid var(--line);
  background: #fff;
  color: var(--ink-2);
  padding: 8px 14px;
  border-radius: 99px;
  font-size: 14px;
  font-family: inherit;
  cursor: pointer;
}
.chip.active {
  border-color: var(--accent);
  background: var(--accent-soft);
  color: var(--accent);
  font-weight: 600;
}

.field-row { display: flex; gap: 10px; margin-bottom: 12px; }
.field {
  display: flex;
  flex-direction: column;
  gap: 6px;
  flex: 1;
  margin-bottom: 12px;
}
.field-row .field { margin-bottom: 0; }
.field span { font-size: 12px; color: var(--muted); }
.field input, .field select {
  width: 100%;
  border: 1px solid var(--line);
  border-radius: 10px;
  padding: 10px 12px;
  font-size: 16px;
  font-family: inherit;
  color: var(--ink);
  background: #fff;
  outline: none;
  appearance: none;
}
.field input:focus, .field select:focus { border-color: var(--accent); }
.field select { background-image: none; }

.sheet-actions {
  display: flex;
  gap: 10px;
  margin-top: 18px;
}
.btn {
  flex: 1;
  height: 48px;
  border-radius: 12px;
  border: 1px solid transparent;
  font-size: 15px;
  font-weight: 600;
  font-family: inherit;
  cursor: pointer;
}
.btn.primary { background: var(--accent); color: #fff; }
.btn.ghost { background: #f3f4f7; color: var(--ink-2); }
.btn.danger { color: var(--expense); }
.btn.ghost.danger { background: #fdf1f0; }

/* ---------- 菜单 ---------- */
.menu {
  position: fixed;
  top: calc(var(--safe-top) + 52px);
  right: max(16px, calc(50% - 264px));
  z-index: 22;
  background: #fff;
  border: 1px solid var(--line);
  border-radius: 14px;
  box-shadow: 0 10px 30px rgba(16, 20, 30, 0.12);
  overflow: hidden;
  min-width: 190px;
}
.menu button {
  display: block;
  width: 100%;
  padding: 13px 16px;
  border: 0;
  background: transparent;
  text-align: left;
  font-size: 14px;
  font-family: inherit;
  color: var(--ink);
  cursor: pointer;
}
.menu button + button { border-top: 1px solid var(--line-soft); }
.menu button.danger { color: var(--expense); }
.menu button:active { background: #f7f8fa; }

/* ---------- 提示 ---------- */
.toast {
  position: fixed;
  left: 50%;
  bottom: calc(88px + var(--safe-bottom));
  transform: translateX(-50%);
  background: rgba(23, 24, 28, 0.9);
  color: #fff;
  font-size: 13px;
  padding: 9px 16px;
  border-radius: 99px;
  z-index: 30;
  white-space: nowrap;
  animation: toast-in 0.18s ease-out;
}
@keyframes toast-in {
  from { opacity: 0; transform: translate(-50%, 6px); }
  to { opacity: 1; transform: translate(-50%, 0); }
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0f1013;
    --card: #17181c;
    --line: #26282e;
    --line-soft: #1e2025;
    --ink: #f2f3f5;
    --ink-2: #b9bec7;
    --muted: #7d838d;
    --accent: #6f95ff;
    --accent-soft: rgba(111, 149, 255, 0.12);
    --income: #3ec98a;
    --expense: #ff6b5e;
  }
  .segment { background: #1f2127; }
  .segment button.active { background: #2a2d34; }
  .day-head { background: #1b1d22; }
  .btn.ghost { background: #23262b; }
  .btn.ghost.danger { background: #2a1c1b; }
  .chip { background: #1b1d22; }
  .field input, .field select { background: #1b1d22; }
  .sheet { background: #17181c; }
  .menu { background: #1b1d22; }
}
