:root {
  --primary: #1f6feb;
  --primary-dark: #1657b8;
  --bg: #f5f6f8;
  --card: #ffffff;
  --text: #1f2430;
  --text-2: #6b7280;
  --border: #e5e7eb;
  --expense: #e5484d;
  --expense-bg: #fdecec;
  --income: #30a46c;
  --income-bg: #e9f7ef;
  --radius: 14px;
  --shadow: 0 1px 3px rgba(16, 24, 40, 0.08);
}

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

html {
  -webkit-text-size-adjust: 100%;
}

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

button {
  font: inherit;
  border: none;
  background: none;
  cursor: pointer;
  color: inherit;
}

input,
select {
  font: inherit;
  color: inherit;
}

.hidden {
  display: none !important;
}

.primary {
  background: var(--primary);
  color: #fff;
  padding: 12px 18px;
  border-radius: 10px;
  font-weight: 600;
  transition: background 0.15s;
}

.primary:hover {
  background: var(--primary-dark);
}

.primary:disabled {
  opacity: 0.6;
  cursor: default;
}

.ghost {
  background: #fff;
  color: var(--text-2);
  padding: 12px 18px;
  border-radius: 10px;
  border: 1px solid var(--border);
  font-weight: 600;
}

.danger {
  background: var(--expense-bg);
  color: var(--expense);
  padding: 12px 18px;
  border-radius: 10px;
  font-weight: 600;
  width: 100%;
}

.error {
  color: var(--expense);
  font-size: 13px;
  min-height: 1.2em;
  margin-top: 8px;
}

.hint {
  color: var(--text-2);
  font-size: 13px;
  margin-top: 10px;
}

/* ---------- 登录页 ---------- */

#login-view {
  min-height: 100vh;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 24px;
  background: linear-gradient(160deg, #eaf2ff 0%, var(--bg) 55%);
}

.login-card {
  width: 100%;
  max-width: 380px;
  background: var(--card);
  border-radius: 18px;
  box-shadow: var(--shadow);
  padding: 32px 26px;
  text-align: center;
}

.login-logo {
  width: 64px;
  height: 64px;
  margin: 0 auto 14px;
  border-radius: 18px;
  background: var(--primary);
  color: #fff;
  font-size: 34px;
  font-weight: 700;
  display: flex;
  align-items: center;
  justify-content: center;
}

.login-card h1 {
  font-size: 24px;
  margin-bottom: 6px;
}

.login-card > p {
  color: var(--text-2);
  font-size: 14px;
  margin-bottom: 18px;
}

.login-card input {
  width: 100%;
  padding: 13px 14px;
  border: 1px solid var(--border);
  border-radius: 10px;
  margin-bottom: 10px;
  outline: none;
}

.login-card input:focus {
  border-color: var(--primary);
}

.login-card .primary {
  width: 100%;
  margin-top: 6px;
}

/* ---------- 应用外壳 ---------- */

#app {
  max-width: 960px;
  margin: 0 auto;
  padding: 0 14px 90px;
}

.topbar {
  position: sticky;
  top: 0;
  z-index: 20;
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 14px 4px 10px;
  background: var(--bg);
}

.brand {
  font-size: 19px;
  font-weight: 700;
  color: var(--primary);
  white-space: nowrap;
}

.tabs {
  display: flex;
  gap: 6px;
}

.tab {
  padding: 8px 13px;
  border-radius: 9px;
  font-size: 14px;
  font-weight: 600;
  color: var(--text-2);
}

.tab.active {
  background: #e4edfc;
  color: var(--primary);
}

main {
  padding-top: 6px;
}

.view {
  display: none;
}

.view.active {
  display: block;
}

.view > h1 {
  font-size: 22px;
  margin: 8px 0 12px;
}

.bottom-nav {
  position: fixed;
  left: 0;
  right: 0;
  bottom: 0;
  z-index: 30;
  display: flex;
  background: #fff;
  border-top: 1px solid var(--border);
  padding: 6px 4px calc(6px + env(safe-area-inset-bottom));
}

.nav-btn {
  flex: 1;
  padding: 8px 2px;
  font-size: 13px;
  color: var(--text-2);
  border-radius: 8px;
}

.nav-btn.active {
  color: var(--primary);
  font-weight: 700;
  background: #eef4fe;
}

/* ---------- 通用组件 ---------- */

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

.panel h2 {
  font-size: 16px;
  margin-bottom: 12px;
}

.month-nav {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 14px;
  margin: 6px 0 12px;
}

.month-nav button {
  width: 36px;
  height: 36px;
  border-radius: 50%;
  background: #fff;
  border: 1px solid var(--border);
  font-size: 18px;
  line-height: 1;
}

.month-nav span {
  font-size: 17px;
  font-weight: 700;
  min-width: 120px;
  text-align: center;
}

.cards {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 10px;
  margin-bottom: 14px;
}

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

.card-income {
  background: var(--income-bg);
}

.card-expense {
  background: var(--expense-bg);
}

.card-balance {
  background: #eef4fe;
}

.card-label {
  font-size: 12px;
  color: var(--text-2);
  margin-bottom: 4px;
}

.card-value {
  font-size: 18px;
  font-weight: 700;
  overflow-wrap: anywhere;
}

.chart-wrap {
  position: relative;
  height: 260px;
}

.chart-wrap.small {
  height: 180px;
  width: 180px;
  flex: 0 0 auto;
}

.chart-row {
  display: flex;
  gap: 16px;
  align-items: center;
  flex-wrap: wrap;
}

#cat-legend {
  list-style: none;
  flex: 1;
  min-width: 180px;
}

#cat-legend li {
  display: flex;
  align-items: center;
  gap: 8px;
  font-size: 13px;
  padding: 4px 0;
}

#cat-legend .dot {
  width: 10px;
  height: 10px;
  border-radius: 3px;
  flex: 0 0 auto;
}

#cat-legend .name {
  flex: 1;
  color: var(--text-2);
}

#cat-legend .amount {
  font-weight: 600;
}

/* ---------- 表单 ---------- */

.form {
  display: flex;
  flex-direction: column;
  gap: 14px;
}

.seg {
  display: flex;
  background: #eef0f3;
  border-radius: 11px;
  padding: 4px;
}

.seg-btn {
  flex: 1;
  padding: 10px;
  border-radius: 8px;
  font-weight: 600;
  color: var(--text-2);
}

.seg-btn.active {
  background: #fff;
  box-shadow: 0 1px 3px rgba(16, 24, 40, 0.12);
  color: var(--text);
}

.seg-expense.active {
  color: var(--expense);
}

.seg-income.active {
  color: var(--income);
}

.field {
  display: flex;
  flex-direction: column;
  gap: 6px;
}

.field label {
  font-size: 13px;
  color: var(--text-2);
}

.field input,
.field select,
.filters input,
.filters select,
.add-cat-row select,
.add-cat-row input {
  width: 100%;
  padding: 12px 13px;
  border: 1px solid var(--border);
  border-radius: 10px;
  background: #fff;
  outline: none;
}

.field input:focus,
.field select:focus,
.filters input:focus,
.filters select:focus,
.add-cat-row input:focus {
  border-color: var(--primary);
}

.amount-input {
  display: flex;
  align-items: center;
  border: 1px solid var(--border);
  border-radius: 10px;
  background: #fff;
  padding: 0 13px;
}

.amount-input span {
  font-size: 20px;
  font-weight: 700;
  color: var(--text-2);
  margin-right: 6px;
}

.amount-input input {
  border: none;
  outline: none;
  padding: 13px 0;
  font-size: 22px;
  font-weight: 700;
  width: 100%;
}

/* ---------- 明细 ---------- */

.filters {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 10px;
}

.filters select:last-child {
  grid-column: 1 / -1;
}

.summary-line {
  font-size: 13px;
  color: var(--text-2);
  margin: 0 2px 10px;
}

.entry-row {
  display: flex;
  align-items: center;
  gap: 12px;
  background: var(--card);
  border-radius: 12px;
  box-shadow: var(--shadow);
  padding: 12px 14px;
  margin-bottom: 9px;
}

.entry-date {
  width: 56px;
  flex: 0 0 auto;
  text-align: center;
}

.entry-date .d {
  font-size: 17px;
  font-weight: 700;
  line-height: 1.2;
}

.entry-date .w {
  font-size: 11px;
  color: var(--text-2);
}

.entry-main {
  flex: 1;
  min-width: 0;
}

.entry-main .top {
  display: flex;
  align-items: center;
  gap: 8px;
}

.cat-badge {
  font-size: 12px;
  padding: 2px 9px;
  border-radius: 999px;
  background: #eef0f3;
  color: var(--text-2);
  white-space: nowrap;
}

.entry-note {
  font-size: 12px;
  color: var(--text-2);
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.entry-amount {
  font-weight: 700;
  font-size: 16px;
  white-space: nowrap;
}

.entry-amount.expense {
  color: var(--expense);
}

.entry-amount.income {
  color: var(--income);
}

.entry-actions {
  display: flex;
  flex-direction: column;
  gap: 4px;
}

.entry-actions button {
  font-size: 12px;
  padding: 4px 9px;
  border-radius: 7px;
  background: #f1f3f5;
  color: var(--text-2);
}

.entry-actions button.del {
  color: var(--expense);
}

#list-more {
  width: 100%;
  margin-top: 6px;
}

.empty {
  text-align: center;
  color: var(--text-2);
  padding: 30px 0;
}

/* ---------- 统计 ---------- */

.table-wrap {
  overflow-x: auto;
}

table {
  width: 100%;
  border-collapse: collapse;
  font-size: 14px;
}

th,
td {
  text-align: right;
  padding: 10px 8px;
  border-bottom: 1px solid var(--border);
}

th:first-child,
td:first-child {
  text-align: left;
}

th {
  color: var(--text-2);
  font-weight: 600;
  font-size: 13px;
}

.pct-bar {
  height: 6px;
  border-radius: 3px;
  background: #eef0f3;
  margin-top: 5px;
  overflow: hidden;
}

.pct-bar > div {
  height: 100%;
  border-radius: 3px;
  background: var(--primary);
}

/* ---------- 设置 ---------- */

#cat-manage {
  margin-bottom: 14px;
}

.cat-group {
  margin-bottom: 12px;
}

.cat-group h3 {
  font-size: 14px;
  color: var(--text-2);
  margin-bottom: 6px;
}

.cat-item {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 8px 10px;
  border: 1px solid var(--border);
  border-radius: 9px;
  margin-bottom: 6px;
}

.cat-item .cat-name {
  flex: 1;
}

.cat-item button {
  font-size: 12px;
  color: var(--text-2);
  padding: 3px 8px;
  border-radius: 6px;
  background: #f1f3f5;
}

.cat-item button.del {
  color: var(--expense);
}

.add-cat-row {
  display: flex;
  gap: 8px;
}

.add-cat-row select {
  width: 90px;
  flex: 0 0 auto;
}

.add-cat-row input {
  flex: 1;
}

.add-cat-row .primary {
  padding: 12px 14px;
}

#export-link {
  display: inline-block;
  color: var(--primary);
  font-weight: 600;
  text-decoration: none;
  padding: 10px 0;
}

/* ---------- Toast ---------- */

#toast {
  position: fixed;
  left: 50%;
  bottom: 86px;
  transform: translateX(-50%);
  background: #242a38;
  color: #fff;
  padding: 10px 18px;
  border-radius: 999px;
  font-size: 14px;
  z-index: 100;
  box-shadow: 0 4px 14px rgba(0, 0, 0, 0.25);
  max-width: 86vw;
  text-align: center;
}

/* ---------- 桌面端 ---------- */

@media (min-width: 720px) {
  .bottom-nav {
    display: none;
  }

  #app {
    padding-bottom: 40px;
  }

  .cards {
    gap: 14px;
  }

  .card-value {
    font-size: 24px;
  }

  .filters {
    grid-template-columns: 1fr 1fr 1fr;
  }

  .filters select:last-child {
    grid-column: auto;
  }

  .entry-actions {
    flex-direction: row;
  }

  .view > h1 {
    margin-top: 16px;
  }
}
