:root {
  color-scheme: light;
  --bg: #f5f5fb;
  --card: rgba(255, 255, 255, 0.94);
  --text: #171629;
  --muted: #77758a;
  --primary: #5b4ff0;
  --primary-2: #8b5cf6;
  --line: #e9e8f1;
  --danger: #e5484d;
  --safe-bottom: env(safe-area-inset-bottom, 0px);
  --safe-left: env(safe-area-inset-left, 0px);
  --safe-right: env(safe-area-inset-right, 0px);
  font-family: -apple-system, BlinkMacSystemFont, "SF Pro Display", "PingFang SC", sans-serif;
}

* { box-sizing: border-box; }

body {
  margin: 0;
  min-height: 100dvh;
  color: var(--text);
  background:
    radial-gradient(circle at 90% -10%, rgba(139, 92, 246, 0.18), transparent 34%),
    var(--bg);
  -webkit-tap-highlight-color: transparent;
}

button, input { font: inherit; }
button { cursor: pointer; }

#app {
  width: min(100%, 680px);
  margin: 0 auto;
  padding-bottom: calc(128px + var(--safe-bottom));
}

.page {
  display: none;
  padding: max(22px, env(safe-area-inset-top)) max(16px, var(--safe-right)) 24px max(16px, var(--safe-left));
  animation: page-in 180ms ease-out;
}

.page.active { display: block; }

@keyframes page-in {
  from { opacity: 0; transform: translateY(4px); }
  to { opacity: 1; transform: translateY(0); }
}

.page-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin: 8px 4px 22px;
}

.eyebrow {
  margin: 0 0 4px;
  color: var(--primary);
  font-size: 13px;
  font-weight: 700;
  letter-spacing: 0.04em;
}

h1 { margin: 0; font-size: 32px; letter-spacing: -0.04em; }
h2 { margin: 0 0 13px; font-size: 17px; }

.brand-mark, .icon-button {
  display: grid;
  place-items: center;
  width: 48px;
  height: 48px;
  border: 0;
  border-radius: 16px;
  color: white;
  font-size: 22px;
  font-weight: 800;
  background: linear-gradient(135deg, var(--primary), var(--primary-2));
  box-shadow: 0 10px 24px rgba(91, 79, 240, 0.25);
}
.record-actions { display: flex; gap: 8px; }
.icon-button.secondary { color: var(--primary); background: rgba(91,79,240,.11); box-shadow: none; cursor: pointer; }
.backup-hint { margin: -14px 4px 14px; color: var(--muted); font-size: 12px; text-align: right; }

.card {
  margin-bottom: 15px;
  padding: 18px;
  border: 1px solid rgba(255, 255, 255, 0.8);
  border-radius: 22px;
  background: var(--card);
  box-shadow: 0 8px 26px rgba(38, 35, 70, 0.055);
  backdrop-filter: blur(20px);
}

.amount-card label, .field-label {
  display: block;
  margin-bottom: 7px;
  color: var(--muted);
  font-size: 14px;
  font-weight: 700;
}

.amount-input {
  display: flex;
  align-items: baseline;
  gap: 8px;
}

.amount-input span { font-size: 30px; font-weight: 750; }

.amount-input input {
  width: 100%;
  min-width: 0;
  border: 0;
  outline: 0;
  color: var(--text);
  background: transparent;
  font-size: clamp(40px, 13vw, 58px);
  font-weight: 800;
  letter-spacing: -0.055em;
}

.amount-input input::placeholder { color: #d0cfda; }
.amount-input.compact input { font-size: clamp(34px, 10vw, 48px); }
.form-hint { margin: 10px 0 0; color: var(--muted); font-size: 12px; line-height: 1.5; }

.category {
  border: 0;
  border-radius: 14px;
  color: var(--text);
  background: #f2f1f7;
  font-weight: 700;
}
.category.active { color: white; background: var(--primary); box-shadow: 0 8px 16px rgba(91, 79, 240, 0.2); }

.category-grid { display: grid; grid-template-columns: repeat(3, 1fr); gap: 10px; }
.income-category-grid { grid-template-columns: repeat(2, 1fr); }
.category { display: flex; flex-direction: column; align-items: center; gap: 7px; padding: 12px 6px; font-size: 13px; }
.category span { font-size: 22px; line-height: 1; }

#note, #income-note, #edit-note, #edit-income-note {
  width: 100%;
  padding: 13px;
  border: 0;
  border-radius: 13px;
  outline: none;
  background: #f2f1f7;
}

.primary-button {
  width: 100%;
  padding: 17px;
  border: 0;
  border-radius: 17px;
  color: white;
  background: linear-gradient(135deg, var(--primary), var(--primary-2));
  box-shadow: 0 12px 25px rgba(91, 79, 240, 0.28);
  font-size: 17px;
  font-weight: 800;
}
.primary-button.secondary-action {
  margin-bottom: 15px;
  color: var(--primary);
  background: rgba(91,79,240,.11);
  box-shadow: none;
}
#income-form { margin-bottom: 18px; }

.tab-bar {
  position: fixed;
  z-index: 20;
  right: max(12px, var(--safe-right));
  bottom: calc(10px + var(--safe-bottom));
  left: max(12px, var(--safe-left));
  display: grid;
  grid-template-columns: repeat(5, 1fr);
  width: min(calc(100% - 24px), 640px);
  margin: auto;
  padding: 8px;
  border: 1px solid rgba(255,255,255,.85);
  border-radius: 22px;
  background: rgba(255,255,255,.9);
  box-shadow: 0 15px 38px rgba(26, 23, 51, .16);
  backdrop-filter: blur(24px);
}

.tab {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 3px;
  padding: 7px;
  border: 0;
  border-radius: 15px;
  color: var(--muted);
  background: transparent;
  font-size: 11px;
  font-weight: 700;
  min-width: 0;
  min-height: 56px;
}
.tab span { font-size: 21px; line-height: 1; }
.tab.active { color: var(--primary); background: rgba(91, 79, 240, .09); }

.summary-strip {
  display: grid;
  gap: 6px;
  margin-bottom: 14px;
  padding: 14px 16px;
  border-radius: 16px;
  color: #554f6d;
  background: rgba(255,255,255,.7);
  font-size: 13px;
}
.summary-strip span { display: block; }

.records-list { display: grid; gap: 10px; }
.record-row {
  display: grid;
  grid-template-columns: 44px minmax(0, 1fr) max-content 26px;
  align-items: center;
  gap: 13px;
  width: 100%;
  min-width: 0;
  overflow: hidden;
  padding: 15px;
  border-radius: 18px;
  background: white;
  box-shadow: 0 7px 20px rgba(38,35,70,.045);
  cursor: pointer;
  transition: transform .15s ease, box-shadow .15s ease;
}
.record-row:active { transform: scale(.99); }
.record-row:focus-visible { outline: 3px solid rgba(91,79,240,.28); outline-offset: 2px; }
.record-icon { display: grid; place-items: center; width: 44px; height: 44px; min-width: 0; border-radius: 14px; background: #eeecff; font-size: 21px; }
.income-icon { background: #e8f8ef; }
.record-copy { min-width: 0; flex: 1; }
.record-copy strong, .record-copy small { display: block; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.record-copy small { margin-top: 5px; color: var(--muted); }
.record-amount { min-width: 0; overflow: hidden; font-weight: 800; font-variant-numeric: tabular-nums; text-align: right; white-space: nowrap; }
.income-amount { color: #0f9f6e; }
.delete-button { width: 26px; min-width: 0; padding: 7px 0; border: 0; color: #aaa6b5; background: transparent; font-size: 17px; }
.empty-state { padding: 70px 24px; text-align: center; color: var(--muted); }
.compact-empty { padding: 32px 18px; }
.empty-state span { display: block; margin-bottom: 12px; font-size: 48px; }

.month-nav { display: flex; align-items: center; gap: 5px; }
.month-nav button { display: grid; place-items: center; width: 34px; height: 34px; border: 0; border-radius: 11px; color: var(--primary); background: rgba(91,79,240,.1); font-size: 24px; line-height: 1; }
.month-nav button:disabled { opacity: .3; }
.month-chip { min-width: 92px; padding: 9px 10px; border-radius: 12px; color: var(--primary); background: rgba(91,79,240,.1); font-size: 13px; font-weight: 800; text-align: center; }
.total-card { display: flex; flex-direction: column; gap: 8px; margin-bottom: 15px; padding: 23px; border-radius: 24px; color: white; background: linear-gradient(135deg, var(--primary), #995ff2); box-shadow: 0 14px 28px rgba(91,79,240,.25); }
.balance-card { background: linear-gradient(135deg, #0f766e, #22c55e); box-shadow: 0 14px 28px rgba(16,185,129,.22); }
.total-card p { margin: 0; opacity: .8; font-size: 14px; }
.total-card strong { font-size: 42px; letter-spacing: -.04em; font-variant-numeric: tabular-nums; }
.total-card span { opacity: .75; font-size: 13px; }
.stats-list { display: grid; gap: 17px; }
.stat-row {
  width: 100%;
  padding: 0;
  border: 0;
  color: inherit;
  background: transparent;
  text-align: left;
  cursor: pointer;
}
.stat-row.active {
  padding: 10px;
  border-radius: 16px;
  background: rgba(91,79,240,.08);
}
.stat-row:disabled {
  cursor: default;
  opacity: .55;
}
.stat-row:focus-visible { outline: 3px solid rgba(91,79,240,.28); outline-offset: 4px; border-radius: 14px; }
.stat-row-head { display: flex; align-items: center; gap: 7px; margin-bottom: 8px; font-size: 14px; }
.stat-row-head strong { margin-left: auto; font-variant-numeric: tabular-nums; }
.stat-row-head small { width: 36px; color: var(--muted); text-align: right; }
.progress { height: 8px; overflow: hidden; border-radius: 999px; background: #ebeaf1; }
.progress > div { height: 100%; min-width: 0; border-radius: inherit; background: linear-gradient(90deg, var(--primary), var(--primary-2)); transition: width .35s ease; }
.section-title-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  margin-bottom: 16px;
}
.section-title-row h2 {
  min-width: 0;
  margin: 0;
  line-height: 1.25;
}
.section-title-row button {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  flex: 0 0 auto;
  min-height: 38px;
  border: 0;
  border-radius: 999px;
  padding: 0 14px;
  color: var(--primary);
  background: rgba(91,79,240,.1);
  font-weight: 800;
  line-height: 1;
}
.detail-list { display: grid; gap: 10px; }
.detail-row {
  display: grid;
  grid-template-columns: 38px minmax(0, 1fr) max-content;
  align-items: center;
  gap: 12px;
  width: 100%;
  min-width: 0;
  max-width: 100%;
  overflow: hidden;
  padding: 12px;
  border-radius: 16px;
  background: #f7f6fb;
}
.detail-icon {
  display: grid;
  place-items: center;
  width: 38px;
  height: 38px;
  flex: 0 0 38px;
  border-radius: 13px;
  background: white;
  font-size: 18px;
  font-weight: 900;
  color: var(--primary);
  box-shadow: 0 6px 14px rgba(38,35,70,.045);
}
.detail-copy { min-width: 0; flex: 1; }
.detail-copy strong, .detail-copy small { display: block; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.detail-copy small { margin-top: 4px; color: var(--muted); font-size: 12px; }
.detail-amount { min-width: 0; overflow: hidden; font-weight: 900; font-variant-numeric: tabular-nums; text-align: right; white-space: nowrap; }
.detail-empty { margin: 0; padding: 14px; border-radius: 16px; color: var(--muted); background: #f7f6fb; text-align: center; }
.swipe-hint { margin: 18px 0 4px; color: var(--muted); font-size: 12px; text-align: center; }

.pie-layout {
  display: grid;
  grid-template-columns: 128px minmax(0, 1fr);
  align-items: center;
  gap: 18px;
}
.balance-pie {
  width: 128px;
  height: 128px;
  border-radius: 50%;
  background: #ebeaf1;
  box-shadow: inset 0 0 0 16px rgba(255,255,255,.65), 0 10px 24px rgba(38,35,70,.08);
}
.balance-legend { display: grid; gap: 10px; }
.legend-row {
  display: flex;
  align-items: center;
  gap: 10px;
  min-width: 0;
}
.legend-dot {
  width: 11px;
  height: 11px;
  flex: 0 0 11px;
  border-radius: 50%;
}
.legend-row strong, .legend-row small { display: block; }
.legend-row small { margin-top: 3px; color: var(--muted); font-size: 12px; }
.balance-field {
  display: grid;
  grid-template-columns: minmax(0, 1fr) 150px;
  align-items: center;
  gap: 10px;
  padding: 12px 0;
  border-bottom: 1px solid var(--line);
  color: var(--muted);
  font-size: 14px;
  font-weight: 750;
}
.balance-field:last-of-type { border-bottom: 0; }
.balance-field input {
  width: 100%;
  padding: 11px 12px;
  border: 0;
  border-radius: 12px;
  outline: none;
  color: var(--text);
  background: #f2f1f7;
  font-weight: 800;
  text-align: right;
}
.dialog-account-grid {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 8px;
}
.account-option {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 6px;
  min-height: 62px;
  padding: 10px 4px;
  border: 0;
  border-radius: 14px;
  color: var(--text);
  background: #f2f1f7;
  font-size: 12px;
  font-weight: 750;
}
.account-option span { font-size: 18px; line-height: 1; }
.account-option.active { color: white; background: var(--primary); box-shadow: 0 8px 16px rgba(91,79,240,.2); }

.toast {
  position: fixed;
  z-index: 40;
  right: 20px;
  bottom: calc(100px + var(--safe-bottom));
  left: 20px;
  max-width: 440px;
  margin: auto;
  padding: 13px 17px;
  border-radius: 15px;
  color: white;
  background: rgba(28, 26, 44, .92);
  opacity: 0;
  transform: translateY(12px);
  pointer-events: none;
  transition: .2s ease;
  text-align: center;
  font-size: 14px;
}
.toast.show { opacity: 1; transform: translateY(0); }

dialog { width: min(calc(100% - 40px), 360px); padding: 22px; border: 0; border-radius: 22px; box-shadow: 0 20px 60px rgba(0,0,0,.22); }
dialog::backdrop { background: rgba(15,13,25,.35); backdrop-filter: blur(3px); }
dialog h2 { margin-bottom: 7px; }
dialog p { color: var(--muted); }
.dialog-field {
  display: grid;
  gap: 7px;
  margin-top: 14px;
  color: var(--muted);
  font-size: 13px;
  font-weight: 750;
}
.dialog-field input {
  width: 100%;
  padding: 13px;
  border: 0;
  border-radius: 13px;
  outline: none;
  color: var(--text);
  background: #f2f1f7;
  font-size: 16px;
  font-weight: 650;
}
.dialog-field input:focus { box-shadow: 0 0 0 3px rgba(91,79,240,.14); }
.dialog-category-grid {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 8px;
}
.dialog-category-grid .category {
  min-height: 64px;
  padding: 10px 4px;
  font-size: 12px;
}
.dialog-category-grid .category span { font-size: 19px; }
.dialog-help { margin: 12px 0 0; font-size: 12px; line-height: 1.5; }
.dialog-actions { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; margin-top: 20px; }
.dialog-actions button { padding: 12px; border: 0; border-radius: 12px; background: #efeff5; font-weight: 750; }
.dialog-actions .danger { color: white; background: var(--danger); }
.dialog-actions .save { color: white; background: var(--primary); }

@media (min-width: 700px) {
  .page { padding-top: 34px; }
}

@media (max-width: 420px) {
  .page { padding-top: max(18px, env(safe-area-inset-top)); }
  .page-header { margin-bottom: 16px; }
  .card { margin-bottom: 12px; padding: 16px; }
  .category-grid { gap: 8px; }
  .category { min-height: 64px; padding: 9px 5px; }
  .tab-bar { padding: 6px; }
  .tab { min-height: 52px; padding: 6px 2px; }
  .pie-layout { grid-template-columns: 1fr; justify-items: center; }
  .balance-legend { width: 100%; }
  .balance-field { grid-template-columns: 1fr; }
}

/* Duo 的窄侧屏：保留三列分类，同时减少首屏空隙。 */
@media (min-width: 421px) and (max-width: 699px) {
  .page { padding-top: max(18px, env(safe-area-inset-top)); }
  .page-header { margin-bottom: 16px; }
  .card { margin-bottom: 12px; padding: 16px; }
  .category-grid { gap: 8px; }
  .category { min-height: 64px; padding: 9px 5px; }
  .tab-bar { padding: 6px; }
  .tab { min-height: 52px; }
}

/* Duo 展开后的宽屏：表单利用横向空间，导航跟随内容宽度。 */
@media (min-width: 760px) {
  #app { width: min(100%, 980px); }
  .page { padding: max(34px, env(safe-area-inset-top)) 30px 30px; }
  .tab-bar { width: min(calc(100% - 60px), 920px); }
  #expense-form, #income-form {
    display: grid;
    grid-template-columns: minmax(0, .9fr) minmax(0, 1.1fr);
    grid-template-areas: "amount categories" "note categories" "save categories";
    align-content: start;
    align-items: start;
    gap: 0 16px;
  }
  #expense-form > .amount-card, #income-form > .amount-card { grid-area: amount; }
  #expense-form > .card:nth-child(2), #income-form > .card:nth-child(2) { grid-area: categories; }
  #expense-form > .card:nth-child(3), #income-form > .card:nth-child(3) { grid-area: note; }
  #expense-form > .primary-button, #income-form > .primary-button { grid-area: save; }
  .category { min-height: 74px; }
}
