:root {
  --primary: #3b82f6;
  --primary-dark: #2563eb;
  --primary-light: #eff6ff;
  --bg: #f5f6fa;
  --card: #ffffff;
  --text: #2c3e50;
  --text-sub: #7f8c8d;
  --border: #e8e8ee;
  --radius: 12px;
  --shadow: 0 2px 12px rgba(0, 0, 0, 0.06);
  --bar-empty: #ecf0f1;
  --record-hover: #e2e8f0;
}

/* ===== 暗黑主题 ===== */
[data-theme="dark"] {
  --primary: #60a5fa;
  --primary-dark: #3b82f6;
  --primary-light: #1e3a5f;
  --bg: #111827;
  --card: #1f2937;
  --text: #e5e7eb;
  --text-sub: #9ca3af;
  --border: #374151;
  --shadow: 0 2px 12px rgba(0, 0, 0, 0.4);
  --bar-empty: #374151;
  --record-hover: #2d3748;
}

.theme-toggle {
  width: 38px;
  height: 38px;
  border: 1px solid var(--border);
  border-radius: 10px;
  background: var(--card);
  font-size: 18px;
  cursor: pointer;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  transition: all .2s;
  color: var(--text);
}
.theme-toggle:hover { border-color: var(--primary); }
.theme-toggle-fixed {
  position: fixed;
  top: 14px;
  right: 16px;
  z-index: 400;
  box-shadow: var(--shadow);
}

/* 暗黑模式细节适配 */
[data-theme="dark"] #auth-view {
  background: linear-gradient(160deg, #0b1120 0%, #111827 60%, #0b1526 100%);
}
[data-theme="dark"] .type-badge { background: #334155; color: #cbd5e1; }
[data-theme="dark"] .rel-badge { background: #1e3a5f; color: #93c5fd; }
[data-theme="dark"] .bar { background: linear-gradient(180deg, #93c5fd, #3b82f6); }
[data-theme="dark"] .record-item:hover { box-shadow: 0 2px 12px rgba(0, 0, 0, 0.4); }
[data-theme="dark"] input, [data-theme="dark"] select, [data-theme="dark"] textarea {
  background: var(--card);
  color: var(--text);
  color-scheme: dark;
}

* { margin: 0; padding: 0; box-sizing: border-box; }

body {
  font-family: -apple-system, "PingFang SC", "Microsoft YaHei", "Segoe UI", sans-serif;
  background: var(--bg);
  color: var(--text);
  min-height: 100vh;
}

/* ===== 登录/注册页 ===== */
#auth-view {
  min-height: 100vh;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 24px 16px;
  background: linear-gradient(160deg, #eef4ff 0%, #f5f6fa 60%, #e8eefc 100%);
}
.auth-card {
  background: var(--card);
  border-radius: 20px;
  box-shadow: 0 10px 40px rgba(37, 99, 235, 0.12);
  padding: 40px 36px;
  width: 400px;
  max-width: 100%;
}
.auth-logo {
  font-size: 44px;
  text-align: center;
  margin-bottom: 8px;
}
.auth-title { text-align: center; font-size: 24px; color: var(--text); }
.auth-sub { text-align: center; color: var(--text-sub); font-size: 13px; margin: 8px 0 24px; }

.auth-tabs {
  display: flex;
  background: var(--bg);
  border-radius: 10px;
  padding: 4px;
  margin-bottom: 20px;
}
.auth-tab {
  flex: 1;
  padding: 9px 0;
  border: none;
  background: transparent;
  border-radius: 8px;
  font-size: 14px;
  color: var(--text-sub);
  cursor: pointer;
  transition: all .2s;
}
.auth-tab.active { background: var(--card); color: var(--primary); font-weight: 600; box-shadow: 0 1px 6px rgba(0,0,0,.08); }

.auth-error {
  color: #dc2626;
  font-size: 13px;
  min-height: 20px;
  margin-bottom: 8px;
  text-align: center;
}
.btn-lg { width: 100%; padding: 12px 0; font-size: 16px; border-radius: 10px; margin-top: 4px; }

/* ===== 用户菜单 ===== */
.user-menu { position: relative; }
.user-btn {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 5px 12px 5px 5px;
  border: 1px solid var(--border);
  border-radius: 22px;
  background: var(--card);
  cursor: pointer;
  transition: border-color .2s;
}
.user-btn:hover { border-color: var(--primary); }
.user-avatar {
  width: 28px;
  height: 28px;
  border-radius: 50%;
  background: var(--primary);
  color: #fff;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  font-size: 14px;
  font-weight: 600;
}
.user-avatar.lg { width: 34px; height: 34px; }
.user-name { font-size: 14px; color: var(--text); max-width: 80px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.user-caret { font-size: 11px; color: var(--text-sub); }

.user-dropdown {
  position: absolute;
  right: 0;
  top: calc(100% + 8px);
  background: var(--card);
  border-radius: 12px;
  box-shadow: 0 6px 24px rgba(0, 0, 0, 0.14);
  min-width: 150px;
  overflow: hidden;
  z-index: 300;
}
.user-dropdown-item {
  display: flex;
  align-items: center;
  gap: 10px;
  width: 100%;
  padding: 12px 16px;
  border: none;
  background: transparent;
  font-size: 14px;
  color: var(--text);
  cursor: pointer;
  text-align: left;
}
.user-dropdown-item:hover { background: var(--bg); }
.user-dropdown-item.user-info { border-bottom: 1px solid var(--border); cursor: default; }
.user-dropdown-item.user-info:hover { background: transparent; }

/* ===== 顶栏 ===== */
.app-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 14px 28px;
  background: var(--card);
  border-bottom: 1px solid var(--border);
  position: sticky;
  top: 0;
  z-index: 100;
}
.header-left { display: flex; align-items: center; gap: 24px; }
.logo { font-size: 22px; font-weight: 700; color: var(--primary); }

.view-tabs { display: flex; gap: 6px; }
.tab {
  padding: 8px 20px;
  border: none;
  background: transparent;
  border-radius: 8px;
  font-size: 14px;
  color: var(--text-sub);
  cursor: pointer;
  transition: all .2s;
}
.tab:hover { background: var(--bg); }
.tab.active { background: var(--primary-light); color: var(--primary); font-weight: 600; }

.header-right { display: flex; align-items: center; gap: 12px; }

select, input[type="text"], input[type="password"], input[type="number"], input[type="date"], textarea {
  padding: 8px 12px;
  border: 1px solid var(--border);
  border-radius: 8px;
  font-size: 14px;
  background: var(--card);
  color: var(--text);
  outline: none;
  transition: border-color .2s;
}
select:focus, input:focus, textarea:focus { border-color: var(--primary); }

/* ===== 按钮 ===== */
.btn {
  padding: 8px 18px;
  border: none;
  border-radius: 8px;
  font-size: 14px;
  cursor: pointer;
  transition: all .2s;
}
.btn-primary { background: var(--primary); color: #fff; }
.btn-primary:hover { background: var(--primary-dark); }
.btn-ghost { background: transparent; border: 1px solid var(--border); color: var(--text-sub); }
.btn-ghost:hover { border-color: var(--primary); color: var(--primary); }
.btn-block { width: 100%; margin-top: 8px; }
.btn-sm { padding: 4px 10px; font-size: 12px; }

/* 顶部「添加记录」按钮（桌面端显示，移动端隐藏用 FAB） */
.btn-add {
  margin-right: 8px;
  padding: 8px 16px;
  font-size: 14px;
  white-space: nowrap;
}

/* ===== 主布局 ===== */
main { max-width: 1280px; margin: 0 auto; padding: 24px 28px; }

.view { display: grid; grid-template-columns: 220px 1fr 220px; gap: 20px; align-items: start; }

/* ===== 浮动添加按钮（仅移动端） ===== */
.fab {
  display: none;
  position: fixed;
  right: 20px;
  bottom: calc(24px + env(safe-area-inset-bottom));
  width: 56px;
  height: 56px;
  border-radius: 50%;
  border: none;
  background: var(--primary);
  color: #fff;
  font-size: 30px;
  line-height: 1;
  box-shadow: 0 4px 16px rgba(59, 130, 246, 0.45);
  cursor: pointer;
  z-index: 150;
  align-items: center;
  justify-content: center;
  transition: transform .15s, background .2s;
  -webkit-tap-highlight-color: transparent;
}
.fab:active { transform: scale(.92); background: var(--primary-dark); }

/* ===== 筛选开关（仅移动端） ===== */
.filter-toggle {
  display: none;
  width: 100%;
  padding: 10px 16px;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: var(--card);
  color: var(--text);
  font-size: 14px;
  cursor: pointer;
  text-align: left;
  position: relative;
  box-shadow: var(--shadow);
}
.filter-toggle-icon { color: var(--primary); font-weight: 700; margin-right: 6px; }
.filter-toggle-badge {
  position: absolute;
  right: 14px;
  top: 50%;
  transform: translateY(-50%);
  background: var(--primary);
  color: #fff;
  font-size: 11px;
  min-width: 20px;
  height: 20px;
  border-radius: 10px;
  align-items: center;
  justify-content: center;
  padding: 0 6px;
}

/* ===== 筛选面板 ===== */
.filter-panel {
  background: var(--card);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 18px;
  position: sticky;
  top: 84px;
}
.filter-panel h3 { font-size: 15px; margin-bottom: 14px; color: var(--text); }
.filter-panel label { display: block; font-size: 12px; color: var(--text-sub); margin: 12px 0 6px; }
.filter-panel select, .filter-panel input { width: 100%; }

/* ===== 记录列表 ===== */
.records-list { display: flex; flex-direction: column; gap: 12px; }

.record-item {
  background: var(--card);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 16px 20px;
  display: flex;
  justify-content: space-between;
  align-items: center;
  transition: transform .15s;
}
.record-item:hover { transform: translateY(-2px); }

.record-main { display: flex; flex-direction: column; gap: 6px; }
.record-top { display: flex; align-items: center; gap: 12px; flex-wrap: wrap; }
.record-date { font-size: 13px; color: var(--text-sub); }
.record-lunar { font-size: 12px; color: var(--primary); background: var(--primary-light); padding: 2px 8px; border-radius: 6px; }
.record-name { font-size: 16px; font-weight: 600; }

.type-badge, .rel-badge {
  font-size: 12px;
  padding: 3px 10px;
  border-radius: 20px;
  font-weight: 500;
}
.type-badge { background: #e2e8f0; color: #475569; }
.rel-badge { background: #dbeafe; color: #1e40af; }

.record-remark { font-size: 13px; color: var(--text-sub); }

.record-side { display: flex; align-items: center; gap: 16px; }
.record-amount { font-size: 20px; font-weight: 700; color: var(--primary); white-space: nowrap; }
.record-actions { display: flex; gap: 6px; }

.empty-tip { text-align: center; color: var(--text-sub); padding: 60px 0; }

/* ===== 汇总面板 ===== */
.summary-panel { display: flex; flex-direction: column; gap: 12px; position: sticky; top: 84px; }
.summary-card {
  background: var(--card);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 18px;
  text-align: center;
}
.summary-label { font-size: 12px; color: var(--text-sub); }
.summary-value { font-size: 24px; font-weight: 700; color: var(--primary); margin: 8px 0 4px; }
.summary-sub { font-size: 12px; color: var(--text-sub); }

/* ===== 统计视图 ===== */
.stats-grid { display: grid; grid-template-columns: 2fr 1fr; gap: 20px; }
.card {
  background: var(--card);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 20px;
  margin-bottom: 20px;
}
.card h3 { font-size: 16px; margin-bottom: 16px; }
.card-year { font-weight: 400; font-size: 13px; color: var(--text-sub); }

.bar-chart { display: flex; align-items: flex-end; gap: 10px; height: 200px; padding-top: 10px; }
.bar-col { flex: 1; display: flex; flex-direction: column; align-items: center; gap: 6px; height: 100%; justify-content: flex-end; }
.bar {
  width: 70%;
  background: linear-gradient(180deg, #60a5fa, #3b82f6);
  border-radius: 6px 6px 0 0;
  min-height: 2px;
  position: relative;
  transition: height .4s ease;
}
.bar.empty { background: var(--bar-empty); }
.bar:hover { opacity: .85; }
.bar-label { font-size: 11px; color: var(--text-sub); }
.bar-value { font-size: 11px; color: var(--text); font-weight: 600; }

.types-row {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 10px 4px;
  border-bottom: 1px dashed var(--border);
  font-size: 14px;
}
.types-row:last-child { border-bottom: none; }
.types-name { color: var(--text); }
.types-num { color: var(--text-sub); font-size: 12px; }
.types-amount { color: var(--primary); font-weight: 600; }

.summary-cards { display: grid; grid-template-columns: repeat(3, 1fr); gap: 16px; }

/* ===== 弹窗 ===== */
.modal-mask {
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.4);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 200;
}
.modal {
  background: var(--card);
  border-radius: 16px;
  width: 440px;
  max-width: 92vw;
  max-height: 90vh;
  overflow-y: auto;
  padding: 26px;
  box-shadow: 0 10px 40px rgba(0, 0, 0, 0.2);
}
.modal h2 { font-size: 18px; margin-bottom: 18px; }
.form-row { margin-bottom: 14px; }
.form-row label { display: block; font-size: 13px; color: var(--text-sub); margin-bottom: 6px; }
.req { color: var(--primary); }
.form-row input, .form-row textarea { width: 100%; }
.lunar-preview { font-size: 12px; color: var(--primary); margin-top: 6px; }

.radio-group { display: flex; flex-wrap: wrap; gap: 8px; }
.radio-group label {
  display: inline-flex;
  align-items: center;
  gap: 4px;
  border: 1px solid var(--border);
  border-radius: 20px;
  padding: 5px 12px;
  font-size: 13px;
  cursor: pointer;
  transition: all .2s;
  color: var(--text);
  margin: 0;
}
.radio-group label:hover { border-color: var(--primary); }
.radio-group input { display: none; }
.radio-group label.checked { background: var(--primary-light); border-color: var(--primary); color: var(--primary); font-weight: 600; }

.modal-actions { display: flex; justify-content: flex-end; gap: 10px; margin-top: 20px; }

/* ===== 平板适配 ===== */
@media (max-width: 1100px) {
  .view { grid-template-columns: 190px 1fr 190px; gap: 14px; }
  main { padding: 18px 16px; }
}

/* ===== 移动端适配（≤ 768px） ===== */
@media (max-width: 768px) {
  /* ---- 顶栏双行布局 ---- */
  .app-header {
    flex-direction: column;
    align-items: stretch;
    gap: 8px;
    padding: 12px 16px calc(12px + env(safe-area-inset-top));
  }
  .header-left { justify-content: space-between; gap: 8px; }
  .header-right { display: flex; justify-content: space-between; gap: 8px; }
  .header-right select { flex: 1; }
  .header-add { display: none; }   /* 顶栏按钮隐藏，改用浮动按钮 */
  .view-tabs { display: flex; gap: 4px; }
  .tab { padding: 8px 16px; font-size: 15px; }
  .logo { font-size: 20px; }

  main { padding: 14px 12px calc(96px + env(safe-area-inset-bottom)); }

  /* ---- 单列布局 + 汇总置顶 ---- */
  .view { display: grid; grid-template-columns: 1fr; gap: 12px; }
  .filter-panel { order: 2; position: static; display: none; }
  .filter-panel.open { display: block; }
  .filter-toggle { display: block; order: 1; }
  .records-area { order: 3; }
  .summary-panel {
    order: 0;
    position: static;
    flex-direction: row;
    gap: 8px;
    overflow-x: auto;
    scrollbar-width: none;
  }
  .summary-panel::-webkit-scrollbar { display: none; }
  .summary-card { flex: 1 0 auto; min-width: 104px; padding: 12px 10px; }
  .summary-value { font-size: 20px; margin: 6px 0 2px; }

  /* ---- 记录卡片 ---- */
  .record-item {
    flex-direction: column;
    align-items: stretch;
    gap: 10px;
    padding: 14px 14px;
  }
  .record-top { gap: 8px; }
  .record-name { font-size: 17px; }
  .record-side {
    justify-content: space-between;
    align-items: center;
    border-top: 1px dashed var(--border);
    padding-top: 10px;
  }
  .record-amount { font-size: 22px; }
  .btn-sm { padding: 7px 14px; font-size: 13px; min-height: 36px; }

  /* ---- 筛选面板表单（移动端加大触控） ---- */
  .filter-panel select, .filter-panel input {
    min-height: 42px;
    font-size: 15px;
  }
  .filter-panel .btn { min-height: 42px; font-size: 15px; }

  /* ---- 浮动按钮显示 ---- */
  .fab { display: flex; }

  /* 移动端隐藏顶部添加按钮（用 FAB 代替） */
  .btn-add { display: none; }

  /* ---- 统计 ---- */
  .stats-grid { grid-template-columns: 1fr; gap: 12px; }
  .card { padding: 16px; margin-bottom: 12px; }
  .bar-chart { height: 160px; gap: 6px; }
  .bar-value { display: none; }   /* 柱顶数值隐藏，避免 12 根柱文字重叠 */
  .bar-label { font-size: 10px; }
  .summary-cards { grid-template-columns: repeat(3, 1fr); gap: 8px; }
  .summary-cards .summary-value { font-size: 18px; }
  .types-row { padding: 12px 2px; }

  /* ---- 弹窗：底部滑出全屏 ---- */
  .modal-mask { align-items: flex-end; }
  .modal {
    width: 100%;
    max-width: 100%;
    max-height: 90vh;
    border-radius: 18px 18px 0 0;
    padding: 22px 18px calc(22px + env(safe-area-inset-bottom));
    animation: slideUp .28s ease;
  }
  @keyframes slideUp {
    from { transform: translateY(40px); opacity: 0; }
    to { transform: translateY(0); opacity: 1; }
  }
  .form-row input, .form-row textarea {
    min-height: 44px;
    font-size: 16px;   /* 防止 iOS 聚焦自动放大 */
  }
  .radio-group label { padding: 8px 14px; font-size: 14px; }
  .modal-actions .btn { flex: 1; min-height: 46px; font-size: 15px; }

  /* ---- 空状态 ---- */
  .empty-tip { padding: 40px 0; }
}

/* ===== 小屏手机适配（≤ 380px） ===== */
@media (max-width: 380px) {
  .record-lunar, .rel-badge { display: none; }
  .bar-label { font-size: 9px; }
  .summary-cards { grid-template-columns: 1fr; }
}
