:root {
  --bg: #f4f6fb;
  --card: #ffffff;
  --text: #1d2433;
  --text-2: #5a6478;
  --muted: #98a1b3;
  --border: #e9ecf3;
  --brand: #4c6fff;
  --brand-soft: rgba(76, 111, 255, .12);
  --income: #12b886;
  --expense: #fa5252;
  --shadow: 0 6px 22px rgba(29, 36, 51, .07);
  --radius: 16px;
  --sidebar-w: 232px;
}

[data-theme="dark"] {
  --bg: #12151c;
  --card: #1a1f29;
  --text: #e8ecf4;
  --text-2: #a5afc2;
  --muted: #6f7b91;
  --border: #262d3a;
  --brand: #6a8aff;
  --brand-soft: rgba(106, 138, 255, .18);
  --income: #2ed3a7;
  --expense: #ff7676;
  --shadow: 0 6px 22px rgba(0, 0, 0, .35);
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  padding: 0;
  height: 100%;
}

body {
  background: var(--bg);
  color: var(--text);
  font: 15px/1.5 -apple-system, BlinkMacSystemFont, "Segoe UI", "PingFang SC", "Microsoft YaHei", "Helvetica Neue", Arial, sans-serif;
  -webkit-font-smoothing: antialiased;
  -webkit-tap-highlight-color: transparent;
}

svg {
  width: 20px;
  height: 20px;
  fill: none;
  stroke: currentColor;
  stroke-width: 1.8;
  stroke-linecap: round;
  stroke-linejoin: round;
}

button, input, select { font: inherit; color: inherit; }
button { cursor: pointer; border: 0; background: none; }
h1, h2 { margin: 0; }
ul { margin: 0; padding: 0; list-style: none; }

.app {
  display: flex;
  min-height: 100%;
}

/* ---------- 侧边栏 ---------- */
.sidebar {
  width: var(--sidebar-w);
  flex: 0 0 var(--sidebar-w);
  background: var(--card);
  border-right: 1px solid var(--border);
  padding: 22px 14px;
  display: flex;
  flex-direction: column;
  gap: 18px;
  position: sticky;
  top: 0;
  height: 100vh;
}

.brand {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 0 8px;
}

.brand-logo {
  width: 34px;
  height: 34px;
  border-radius: 10px;
  background: var(--brand);
  color: #fff;
  display: grid;
  place-items: center;
  font-weight: 700;
  font-size: 18px;
}

.brand-name { font-size: 17px; font-weight: 700; letter-spacing: .5px; }

.side-nav { display: flex; flex-direction: column; gap: 4px; }

.nav-item {
  display: flex;
  align-items: center;
  gap: 11px;
  padding: 11px 12px;
  border-radius: 12px;
  color: var(--text-2);
  font-weight: 500;
  transition: background .18s, color .18s;
}

.nav-item:hover { background: var(--brand-soft); color: var(--brand); }
.nav-item.is-active { background: var(--brand-soft); color: var(--brand); font-weight: 600; }

.side-foot {
  margin-top: auto;
  font-size: 12px;
  color: var(--muted);
  line-height: 1.7;
  padding: 0 10px;
}

/* ---------- 主区域 ---------- */
.main {
  flex: 1;
  min-width: 0;
  display: flex;
  flex-direction: column;
}

.topbar {
  position: sticky;
  top: 0;
  z-index: 20;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  padding: 18px 28px;
  background: var(--bg);
  background: color-mix(in srgb, var(--bg) 88%, transparent);
  backdrop-filter: blur(10px);
  border-bottom: 1px solid var(--border);
}

.topbar h1 { font-size: 20px; font-weight: 700; }
.topbar-actions { display: flex; align-items: center; gap: 10px; }

.month-select {
  border: 1px solid var(--border);
  background: var(--card);
  border-radius: 10px;
  padding: 8px 10px;
  font-size: 14px;
  max-width: 140px;
}

.views { padding: 22px 28px 40px; max-width: 1080px; width: 100%; }
.view.is-hidden { display: none; }

.banner {
  margin: 16px 28px 0;
  padding: 12px 16px;
  border-radius: 12px;
  background: color-mix(in srgb, var(--expense) 12%, var(--card));
  background: rgba(250, 82, 82, .12);
  border: 1px solid var(--expense);
  color: var(--expense);
  font-size: 14px;
  line-height: 1.6;
}
.banner code {
  background: rgba(0, 0, 0, .06);
  border-radius: 6px;
  padding: 1px 6px;
}
.banner-detail {
  margin-top: 6px;
  color: var(--text-2);
  font-size: 13px;
  line-height: 1.9;
}
.banner-detail code { color: var(--expense); }

/* ---------- 通用控件 ---------- */
.btn {
  border: 1px solid var(--border);
  background: var(--card);
  border-radius: 10px;
  padding: 9px 16px;
  font-weight: 500;
  transition: filter .18s, transform .1s;
}
.btn:hover { filter: brightness(.97); }
.btn:active { transform: scale(.98); }

.btn-primary {
  background: var(--brand);
  border-color: var(--brand);
  color: #fff;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
}
.btn-primary svg { width: 17px; height: 17px; }

.btn-danger { background: var(--expense); border-color: var(--expense); color: #fff; }

.btn-add { display: inline-flex; }

.link-btn { color: var(--brand); font-size: 14px; font-weight: 500; }

.icon-btn {
  width: 34px;
  height: 34px;
  border-radius: 10px;
  display: grid;
  place-items: center;
  color: var(--text-2);
}
.icon-btn:hover { background: var(--brand-soft); color: var(--brand); }

.input {
  width: 100%;
  border: 1px solid var(--border);
  background: var(--card);
  border-radius: 10px;
  padding: 10px 12px;
  outline: none;
  transition: border-color .18s;
}
.input:focus { border-color: var(--brand); }

.seg {
  display: inline-flex;
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 3px;
  gap: 2px;
}
.seg-btn {
  padding: 6px 14px;
  border-radius: 8px;
  font-size: 13px;
  color: var(--text-2);
  font-weight: 500;
}
.seg-btn.is-active { background: var(--card); color: var(--brand); font-weight: 600; box-shadow: var(--shadow); }
.seg-block { width: 100%; }
.seg-block .seg-btn { flex: 1; padding: 10px; font-size: 15px; }

.chips { display: flex; gap: 8px; flex-wrap: wrap; }
.chip {
  padding: 7px 16px;
  border-radius: 999px;
  border: 1px solid var(--border);
  background: var(--card);
  font-size: 14px;
  color: var(--text-2);
}
.chip.is-active { background: var(--brand); border-color: var(--brand); color: #fff; font-weight: 500; }

.danger { color: var(--expense); }
.muted { color: var(--muted); font-size: 13px; margin: 4px 0 0; }

/* ---------- 统计卡片 ---------- */
.stat-grid {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 14px;
  margin-bottom: 18px;
}

.stat-card {
  background: var(--card);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 16px 18px;
  box-shadow: var(--shadow);
  display: flex;
  flex-direction: column;
  gap: 4px;
}
.stat-label { font-size: 13px; color: var(--text-2); }
.stat-value { font-size: 24px; font-weight: 700; letter-spacing: -.3px; word-break: break-all; }
.stat-sub { font-size: 12px; color: var(--muted); }
.stat-card.income .stat-value { color: var(--income); }
.stat-card.expense .stat-value { color: var(--expense); }
.stat-card.balance .stat-value { color: var(--brand); }

/* ---------- 面板 ---------- */
.panel {
  background: var(--card);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 18px;
  box-shadow: var(--shadow);
  margin-bottom: 18px;
}

.panel-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  margin-bottom: 14px;
}
.panel-head h2 { font-size: 16px; font-weight: 600; }

/* ---------- 环形图 ---------- */
.donut-wrap {
  display: flex;
  align-items: center;
  gap: 24px;
  flex-wrap: wrap;
}
.donut { position: relative; width: 150px; height: 150px; flex: 0 0 150px; }
.donut svg { width: 100%; height: 100%; stroke: none; }
.donut circle { transition: stroke-dasharray .4s ease, stroke-dashoffset .4s ease; }
.donut-center {
  position: absolute;
  inset: 0;
  display: grid;
  place-content: center;
  text-align: center;
  gap: 3px;
  /* 限制在圆环内圈范围内，避免金额过长时溢出圆外 */
  padding: 0 17%;
}
.donut-center span { font-size: 12px; color: var(--muted); }
.donut-center strong {
  font-size: 18px;
  font-weight: 700;
  line-height: 1.15;
  word-break: break-all;
}

.legend { flex: 1; min-width: 200px; display: flex; flex-direction: column; gap: 9px; }
.legend li { display: flex; align-items: center; gap: 9px; font-size: 14px; }
.legend .dot { width: 9px; height: 9px; border-radius: 50%; flex: 0 0 9px; }
.legend .name { flex: 1; color: var(--text-2); }
.legend .val { font-variant-numeric: tabular-nums; }
.legend .pct { color: var(--muted); width: 46px; text-align: right; font-size: 13px; }

/* ---------- 账单列表 ---------- */
.filters { display: flex; flex-direction: column; gap: 12px; margin-bottom: 16px; }
.filter-row { display: flex; gap: 10px; }
.filter-row .input { flex: 1; min-width: 0; }
#categoryFilter { flex: 0 0 150px; }

.list { display: flex; flex-direction: column; gap: 10px; }

.day-group {
  background: var(--card);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  overflow: hidden;
}

.day-head {
  display: flex;
  align-items: baseline;
  justify-content: space-between;
  gap: 10px;
  padding: 11px 16px;
  background: var(--card);
  background: color-mix(in srgb, var(--brand) 5%, var(--card));
  border-bottom: 1px solid var(--border);
  font-size: 13px;
  color: var(--text-2);
}
.day-head .day-title { font-weight: 600; color: var(--text); font-size: 14px; }
.day-head .day-sum { display: flex; gap: 12px; font-variant-numeric: tabular-nums; }

.row {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 12px 16px;
  border-bottom: 1px solid var(--border);
}
.row:last-child { border-bottom: 0; }

.row-emoji {
  width: 38px;
  height: 38px;
  flex: 0 0 38px;
  border-radius: 12px;
  display: grid;
  place-items: center;
  font-size: 19px;
  background: var(--brand-soft);
}

.row-main { flex: 1; min-width: 0; }
.row-title { font-weight: 500; }
.row-note {
  font-size: 12.5px;
  color: var(--muted);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.row-amount {
  font-weight: 700;
  font-variant-numeric: tabular-nums;
  white-space: nowrap;
}
.row-amount.income { color: var(--income); }
.row-amount.expense { color: var(--expense); }

.row-actions { display: flex; gap: 4px; transition: opacity .18s; }
@media (hover: hover) and (min-width: 769px) {
  .row-actions { opacity: 0; }
  .row:hover .row-actions { opacity: 1; }
}
.row-actions button {
  width: 30px;
  height: 30px;
  border-radius: 8px;
  display: grid;
  place-items: center;
  color: var(--muted);
}
.row-actions button:hover { background: var(--brand-soft); color: var(--brand); }
.row-actions svg { width: 16px; height: 16px; }

.more-btn {
  width: 100%;
  padding: 12px;
  border-radius: 12px;
  border: 1px dashed var(--border);
  background: var(--card);
  color: var(--brand);
  font-size: 14px;
}
.more-btn:hover { background: var(--brand-soft); }

.empty {
  text-align: center;
  padding: 50px 20px;
  color: var(--muted);
  background: var(--card);
  border: 1px dashed var(--border);
  border-radius: var(--radius);
}
.empty p { margin: 6px 0 0; font-size: 13px; }

/* ---------- 柱状图 ---------- */
.bars {
  display: flex;
  align-items: flex-end;
  gap: 10px;
  height: 190px;
  padding-top: 8px;
}
.bar-col { flex: 1; display: flex; flex-direction: column; align-items: center; gap: 6px; height: 100%; min-width: 0; }
.bar-pair { flex: 1; width: 100%; display: flex; align-items: flex-end; justify-content: center; gap: 5px; }

/* 柱顶金额：紧贴柱子顶端，两柱高度接近时自动错开一行 */
.bar-val {
  position: absolute;
  bottom: 100%;
  left: 50%;
  transform: translateX(-50%);
  margin-bottom: 4px;
  font-size: 11px;
  line-height: 1;
  font-style: normal;
  white-space: nowrap;
  font-variant-numeric: tabular-nums;
  letter-spacing: -.2px;
}
.bar-val.lift { margin-bottom: 17px; }
.bar.income .bar-val { color: var(--income); }
.bar.expense .bar-val { color: var(--expense); }

.bars.is-12 { gap: 4px; }
.bars.is-12 .bar { width: 10px; }
.bars.is-12 .bar-val { font-size: 9.5px; margin-bottom: 3px; }
.bars.is-12 .bar-val.lift { margin-bottom: 14px; }
.bars.is-12 .bar-label { font-size: 10.5px; }
.bar {
  position: relative;
  width: 13px;
  max-width: 26px;
  flex: 1;
  border-radius: 5px 5px 0 0;
  min-height: 0;
  transition: height .45s ease;
}
.bar.income { background: var(--income); }
.bar.expense { background: var(--expense); }
.bar-label { font-size: 12px; color: var(--muted); white-space: nowrap; }

.bars-legend { display: flex; gap: 18px; justify-content: center; margin-top: 12px; font-size: 13px; color: var(--text-2); }
.bars-legend span { display: inline-flex; align-items: center; gap: 6px; }
.dot { width: 9px; height: 9px; border-radius: 50%; display: inline-block; }
.dot.income { background: var(--income); }
.dot.expense { background: var(--expense); }

/* ---------- 分类排行 ---------- */
.rank-list { display: flex; flex-direction: column; gap: 14px; }
.rank-item { display: flex; flex-direction: column; gap: 6px; }
.rank-top { display: flex; align-items: center; gap: 8px; font-size: 14px; }
.rank-top .rank-name { flex: 1; }
.rank-top .rank-val { font-variant-numeric: tabular-nums; font-weight: 600; }
.rank-top .rank-pct { color: var(--muted); font-size: 13px; width: 46px; text-align: right; }
.rank-bar { height: 8px; border-radius: 999px; background: var(--bg); overflow: hidden; }
.rank-bar i { display: block; height: 100%; border-radius: 999px; transition: width .45s ease; }

/* ---------- 设置 ---------- */
.setting-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 14px;
  padding: 14px 0;
  border-bottom: 1px solid var(--border);
}
.setting-row:last-child { border-bottom: 0; }
.setting-row strong { font-weight: 600; font-size: 15px; }

.switch { position: relative; width: 46px; height: 26px; flex: 0 0 46px; }
.switch input { opacity: 0; width: 0; height: 0; position: absolute; }
.slider {
  position: absolute;
  inset: 0;
  background: var(--border);
  border-radius: 999px;
  transition: background .2s;
}
.slider::before {
  content: "";
  position: absolute;
  width: 20px;
  height: 20px;
  left: 3px;
  top: 3px;
  background: #fff;
  border-radius: 50%;
  transition: transform .2s;
  box-shadow: 0 1px 3px rgba(0, 0, 0, .2);
}
.switch input:checked + .slider { background: var(--brand); }
.switch input:checked + .slider::before { transform: translateX(20px); }

.about p { margin: 0 0 10px; font-size: 14px; color: var(--text-2); line-height: 1.8; }
.about code {
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: 6px;
  padding: 1px 6px;
  font-size: 13px;
}
.storage-info {
  border-left: 3px solid var(--income);
  background: var(--bg);
  border-radius: 0 8px 8px 0;
  padding: 8px 12px !important;
  font-size: 13px !important;
}
.storage-info.warn { border-left-color: var(--expense); }

/* ---------- 移动端底部导航 ---------- */
.tabbar {
  display: none;
  position: fixed;
  left: 0;
  right: 0;
  bottom: 0;
  z-index: 40;
  background: var(--card);
  background: color-mix(in srgb, var(--card) 92%, transparent);
  backdrop-filter: blur(14px);
  border-top: 1px solid var(--border);
  padding: 6px 4px calc(6px + env(safe-area-inset-bottom));
}
.tab {
  flex: 1;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 3px;
  padding: 6px 0;
  color: var(--muted);
  font-size: 11px;
}
.tab.is-active { color: var(--brand); }
.tab-add { justify-content: center; }
.tab-add-circle {
  width: 46px;
  height: 46px;
  border-radius: 50%;
  background: var(--brand);
  color: #fff;
  font-size: 26px;
  line-height: 44px;
  text-align: center;
  box-shadow: 0 6px 16px rgba(76, 111, 255, .4);
}
.tab-add:active .tab-add-circle { transform: scale(.94); }

/* ---------- 弹窗 ---------- */
.modal {
  position: fixed;
  inset: 0;
  z-index: 60;
  display: grid;
  place-items: center;
  padding: 16px;
}
.modal[hidden] { display: none; }
/* 键盘弹起：遮罩换成弹窗底色，保证键盘上方看到的仍是弹窗而不是灰条 */
.modal.keyboard-up .modal-mask {
  background: var(--card);
  backdrop-filter: none;
  -webkit-backdrop-filter: none;
}
body.modal-open { overflow: hidden; }
/* 弹窗打开时彻底隐藏底层页面，避免任何情况下透出背景内容 */
body.modal-open .app { visibility: hidden; }
.modal-mask {
  position: absolute;
  inset: 0;
  background: rgba(16, 21, 32, .68);
  backdrop-filter: blur(4px);
  -webkit-backdrop-filter: blur(4px);
  animation: fade .2s ease;
}

.modal-card {
  position: relative;
  width: 100%;
  max-width: 430px;
  max-height: 90vh;
  max-height: 90dvh;
  overflow-y: auto;
  background: var(--card);
  border-radius: 20px;
  box-shadow: 0 20px 50px rgba(0, 0, 0, .25);
  animation: pop .22s ease;
  display: flex;
  flex-direction: column;
}

.modal-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 18px 20px 10px;
  /* 标题吸顶：内容滚动或键盘顶起时，标题始终可见 */
  position: sticky;
  top: 0;
  z-index: 3;
  background: var(--card);
}
.modal-head h2 { font-size: 18px; font-weight: 700; }

.modal-body {
  padding: 6px 20px 20px;
  display: flex;
  flex-direction: column;
  gap: 16px;
  flex: 1;
}

.field { display: flex; flex-direction: column; gap: 7px; flex: 1; min-width: 0; }
.field-label { font-size: 13px; color: var(--text-2); }
.field-row { display: flex; gap: 12px; }

.amount-input {
  display: flex;
  align-items: center;
  gap: 8px;
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 4px 14px;
  background: var(--card);
}
.amount-input:focus-within { border-color: var(--brand); }
.amount-input .currency { font-size: 22px; color: var(--muted); }
.amount-input input {
  flex: 1;
  border: 0;
  outline: none;
  background: none;
  font-size: 28px;
  font-weight: 700;
  padding: 8px 0;
  min-width: 0;
}
.amount-input input::-webkit-outer-spin-button,
.amount-input input::-webkit-inner-spin-button { -webkit-appearance: none; margin: 0; }
.amount-input input { -moz-appearance: textfield; }

.cat-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(78px, 1fr)); gap: 8px; }
.cat-btn {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 4px;
  padding: 9px 4px;
  border-radius: 12px;
  border: 1px solid var(--border);
  background: var(--card);
  font-size: 12px;
  color: var(--text-2);
  transition: border-color .18s, background .18s;
}
.cat-btn .emoji { font-size: 19px; }
.cat-btn.is-active { border-color: var(--brand); background: var(--brand-soft); color: var(--text); font-weight: 600; }

/* 弹窗被拉高（键盘弹起）时，按钮始终贴在弹窗底部 */
.modal-foot { display: flex; gap: 10px; margin-top: auto; }
.modal-foot .btn {
  flex: 1;
  padding: 12px;
  display: flex;
  align-items: center;
  justify-content: center;
  text-align: center;
}

@keyframes fade { from { opacity: 0; } }
@keyframes pop { from { opacity: 0; transform: translateY(14px) scale(.97); } }

/* ---------- Toast ---------- */
.toast {
  position: fixed;
  left: 50%;
  bottom: 34px;
  transform: translate(-50%, 20px);
  background: rgba(24, 30, 43, .94);
  color: #fff;
  padding: 10px 20px;
  border-radius: 999px;
  font-size: 14px;
  opacity: 0;
  pointer-events: none;
  transition: opacity .22s, transform .22s;
  z-index: 80;
  max-width: 80vw;
  text-align: center;
}
.toast.is-show { opacity: 1; transform: translate(-50%, 0); }

/* ---------- 响应式 ---------- */
@media (max-width: 1024px) {
  .stat-value { font-size: 21px; }
}

@media (max-width: 768px) {
  .sidebar { display: none; }

  .topbar {
    padding: 14px 16px;
    padding-top: calc(14px + env(safe-area-inset-top));
  }
  .topbar h1 { font-size: 18px; }
  .btn-add { display: none; }

  .views { padding: 16px 16px calc(96px + env(safe-area-inset-bottom)); }

  .tabbar { display: flex; }

  .stat-grid { grid-template-columns: repeat(3, 1fr); gap: 10px; }
  .stat-card { padding: 12px 12px; }
  .stat-value { font-size: 17px; }
  .stat-label { font-size: 12px; }
  .stat-sub { font-size: 11px; }

  .panel { padding: 15px; }
  .donut-wrap { gap: 18px; }
  .donut { width: 178px; height: 178px; flex: 0 0 178px; margin: 0 auto; }
  .donut-center { padding: 0 19%; }
  .donut-center span { font-size: 11.5px; }

  .filter-row { flex-direction: column; }
  #categoryFilter { flex: 1; }

  /* 移动端操作按钮常驻显示 */
  .row-actions { opacity: 1; }

  .bars { height: 160px; gap: 6px; }
  .bar { width: 10px; }
  .bar-label { font-size: 11px; }
  .bar-val { font-size: 10px; margin-bottom: 3px; }
  .bar-val.lift { margin-bottom: 15px; }
  .bars.is-12 { gap: 3px; }
  .bars.is-12 .bar { width: 7px; }
  .bars.is-12 .bar-val { font-size: 8.5px; margin-bottom: 2px; }
  .bars.is-12 .bar-val.lift { margin-bottom: 12px; }
  .bars.is-12 .bar-label { font-size: 9.5px; }

  .setting-row { flex-wrap: wrap; }

  .modal { place-items: end center; padding: 0; }
  .modal-card {
    max-width: none;
    border-radius: 20px 20px 0 0;
    max-height: 92vh;
    max-height: 92dvh;
    padding-bottom: env(safe-area-inset-bottom);
    animation: sheet .24s ease;
  }
  /* 键盘弹起时优先保证弹窗顶部可见 */
  .modal { padding-top: env(safe-area-inset-top); }
  @keyframes sheet { from { transform: translateY(100%); } }
  .toast { bottom: calc(84px + env(safe-area-inset-bottom)); }
}

@media (max-width: 400px) {
  .stat-grid { grid-template-columns: 1fr 1fr; }
  .stat-card.balance { grid-column: span 2; }
  .bar-label { font-size: 10px; }
}

@media print {
  .sidebar, .tabbar, .topbar-actions { display: none; }
}
