:root {
  color-scheme: light;
  --page: #f4f5f1;
  --surface: #fff;
  --surface-soft: #f0f3ee;
  --ink: #182720;
  --muted: #55675e;
  --line: #dce5dc;
  --accent: #135441;
  --accent-wash: #e1efe5;
  --expense: #a14130;
  --warning: #805719;
  --warning-bg: #f5ead4;
  --hero: #163c34;
  --hero-text: #f7fff6;
  --hero-muted: #c3dbce;
  --chart-income: #13745d;
  --chart-expense: #be6242;
  --shadow: 0 12px 32px #173f2920;
  --frame: 980px;
  font-family: -apple-system, BlinkMacSystemFont, "PingFang SC", "Microsoft YaHei", "Noto Sans CJK SC", sans-serif;
  font-synthesis: none;
  -webkit-tap-highlight-color: transparent;
}

:root[data-theme="dark"] {
  color-scheme: dark;
  --page: #131c19;
  --surface: #1d2723;
  --surface-soft: #25332d;
  --ink: #f0f6ef;
  --muted: #b1c3b9;
  --line: #38483f;
  --accent: #a5dec1;
  --accent-wash: #2a4537;
  --expense: #ffa693;
  --warning: #f4d59f;
  --warning-bg: #493b27;
  --hero: #23483c;
  --chart-income: #78d9b0;
  --chart-expense: #ff9d7d;
  --shadow: 0 12px 32px #0005;
}

@media (prefers-color-scheme: dark) {
  :root[data-theme="system"] {
    color-scheme: dark;
    --page: #131c19;
    --surface: #1d2723;
    --surface-soft: #25332d;
    --ink: #f0f6ef;
    --muted: #b1c3b9;
    --line: #38483f;
    --accent: #a5dec1;
    --accent-wash: #2a4537;
    --expense: #ffa693;
    --warning: #f4d59f;
    --warning-bg: #493b27;
    --hero: #23483c;
    --chart-income: #78d9b0;
    --chart-expense: #ff9d7d;
    --shadow: 0 12px 32px #0005;
  }
}

* { box-sizing: border-box; }
[hidden] { display: none !important; }
html { background: var(--page); }
body { margin: 0; min-width: 280px; color: var(--ink); font-size: 15px; }
button, select, input, textarea { color: inherit; font: inherit; }
button, summary { cursor: pointer; }
button:disabled { opacity: .55; cursor: wait; }
button:focus-visible, a:focus-visible, summary:focus-visible, input:focus-visible, select:focus-visible, textarea:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 2px;
}
h1, h2, h3, p { margin: 0; }
h1 { font-size: clamp(1.65rem, 5.2vw, 2.25rem); line-height: 1.25; letter-spacing: -.035em; }
h2 { font-size: 1.08rem; line-height: 1.4; }
h3 { font-size: .96rem; }
small, .muted { color: var(--muted); }
small { font-size: .79rem; }
strong, td, .numeric { font-variant-numeric: tabular-nums; }

.app-header {
  background: var(--surface);
  border-bottom: 1px solid var(--line);
  padding-top: env(safe-area-inset-top);
}
.header-inner {
  max-width: var(--frame);
  padding: 10px 24px;
  margin: 0 auto;
  min-height: 66px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
}
.brand, .header-actions { display: flex; align-items: center; gap: 10px; }
.brand { padding: 0; min-height: 44px; background: none; border: 0; font-weight: 750; white-space: nowrap; }
.brand img { border-radius: 10px; }
.offline-status { color: var(--muted); font-size: .8rem; }
.offline-status::before {
  content: "";
  display: inline-block;
  width: 6px;
  height: 6px;
  background: var(--accent);
  margin: 0 6px 2px 0;
  border-radius: 50%;
}
.page { max-width: var(--frame); min-height: 55vh; margin: 0 auto; padding: 25px 24px calc(108px + env(safe-area-inset-bottom)); }
.page:focus { outline: none; }
.page-head, .card-head, .sheet-head {
  display: flex;
  align-items: center;
  gap: 14px;
  justify-content: space-between;
}
.page-head { margin-bottom: 20px; align-items: end; }
.page-head p { margin-top: 6px; }
.stack { display: grid; gap: 14px; }
.cards-grid { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 14px; }
.wide { grid-column: 1 / -1; }
.card { background: var(--surface); border: 1px solid var(--line); border-radius: 21px; padding: 18px; min-width: 0; }
.card-head { margin-bottom: 12px; }
.card-head :only-child { margin: 0; }
.empty { color: var(--muted); line-height: 1.6; padding: 15px 0; }
.empty-panel { padding: 24px 4px 12px; }
.error { color: var(--expense); line-height: 1.45; }

.balance-card { background: var(--hero); color: var(--hero-text); padding: 23px; border: none; border-radius: 24px; }
.balance-card h2, .balance-card small { color: var(--hero-muted); font-size: .88rem; font-weight: 550; }
.big-number { display: block; font-size: clamp(1.9rem, 8vw, 3.1rem); font-weight: 750; overflow-wrap: anywhere; letter-spacing: -.07em; line-height: 1.15; margin: 5px 0 18px; }
.balance-grid { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 12px; }
.balance-grid > div { border-top: 1px solid #ffffff36; padding-top: 11px; display: grid; gap: 4px; min-width: 0; }
.balance-grid strong { font-size: clamp(.88rem, 3.5vw, 1.18rem); overflow-wrap: anywhere; }
.balance-grid.three { grid-template-columns: repeat(3, minmax(0, 1fr)); }
.totals-table { margin-top: 7px; }
.totals-table td:last-child, .totals-table th:not(:first-child) { text-align: right; }

.button {
  border: 1px solid transparent;
  background: var(--surface-soft);
  color: var(--accent);
  font-weight: 680;
  border-radius: 11px;
  min-height: 44px;
  padding: 8px 13px;
  white-space: nowrap;
}
.button.primary { background: var(--accent); color: var(--surface); }
.button.secondary { border-color: var(--line); background: var(--surface); }
.button.text-button { background: none; }
.button.danger, .negative { color: var(--expense); }
.positive { color: var(--accent); }
.action-row { display: flex; gap: 10px; justify-content: flex-end; flex-wrap: wrap; }
.action-row .primary { flex: 1; }
.app-nav {
  position: fixed;
  z-index: 2;
  left: 50%;
  bottom: 0;
  transform: translateX(-50%);
  width: min(100%, 600px);
  padding: 6px max(10px, env(safe-area-inset-right)) calc(6px + env(safe-area-inset-bottom)) max(10px, env(safe-area-inset-left));
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: 20px 20px 0 0;
  display: grid;
  grid-template-columns: repeat(5, minmax(0, 1fr));
  gap: 4px;
  box-shadow: var(--shadow);
}
.app-nav button {
  display: grid;
  justify-items: center;
  align-items: center;
  align-content: center;
  gap: 6px;
  min-height: 54px;
  padding: 5px 2px;
  border: 0;
  border-radius: 12px;
  background: transparent;
  color: var(--muted);
  font-size: .76rem;
  font-weight: 600;
}
.app-nav button[aria-current="page"] { background: var(--accent-wash); color: var(--accent); }
.app-nav .nav-add {
  background: var(--accent);
  color: var(--surface);
  gap: 0;
  margin-inline: 3px;
}
.nav-add span:first-child { font-size: 1.4rem; line-height: 1.05; }
.nav-icon { position: relative; display: block; width: 20px; height: 18px; }
.home-icon::before { content: ""; position: absolute; width: 11px; height: 11px; left: 4px; top: 3px; border-top: 2px solid; border-left: 2px solid; transform: rotate(45deg); }
.home-icon::after { content: ""; position: absolute; width: 12px; height: 9px; left: 3px; bottom: 0; border: 2px solid; border-top: 0; border-radius: 0 0 3px 3px; }
.bills-icon { background: linear-gradient(currentColor, currentColor) 4px 4px / 8px 2px no-repeat, linear-gradient(currentColor, currentColor) 4px 8px / 8px 2px no-repeat; border: 2px solid; border-radius: 3px; }
.stats-icon { border-bottom: 2px solid; background: linear-gradient(currentColor, currentColor) 2px bottom / 3px 8px no-repeat, linear-gradient(currentColor, currentColor) 8px bottom / 3px 15px no-repeat, linear-gradient(currentColor, currentColor) 14px bottom / 3px 12px no-repeat; }
.assets-icon { border: 2px solid; border-radius: 4px; }
.assets-icon::after { content: ""; position: absolute; width: 6px; height: 4px; top: 4px; right: -2px; border: 2px solid; border-radius: 3px 0 0 3px; background: var(--surface); }
.app-nav [aria-current="page"] .assets-icon::after { background: var(--accent-wash); }

.graph { width: 100%; height: 174px; display: block; }
.chart-legend { display: flex; gap: 16px; color: var(--muted); font-size: .8rem; padding-top: 6px; }
.chart-legend span::before { content: ""; display: inline-block; width: 8px; height: 8px; border-radius: 50%; margin: 0 5px 1px 0; background: var(--chart-income); }
.chart-legend span:nth-child(2)::before { background: var(--chart-expense); }
.quiet-details { border-top: 1px solid var(--line); margin-top: 12px; padding-top: 9px; }
.quiet-details summary { color: var(--accent); min-height: 32px; display: list-item; }
table { width: 100%; border-collapse: collapse; font-size: .87rem; }
th, td { padding: 7px 5px; text-align: left; border-bottom: 1px solid var(--line); }
thead th { color: var(--muted); font-weight: 600; }
tr:last-child td { border-bottom: 0; }
.rank-list, .bill-list, .setting-list { margin: 0; padding: 0; list-style: none; }
.rank-list { display: grid; gap: 12px; }
.rank-list li > div { display: flex; justify-content: space-between; align-items: baseline; gap: 12px; }
.rank-list strong { white-space: nowrap; }
meter { display: block; width: 100%; height: 8px; margin-top: 5px; background: var(--surface-soft); border: 0; border-radius: 8px; }
meter::-webkit-meter-bar { border: 0; background: var(--surface-soft); border-radius: 8px; }
meter::-webkit-meter-optimum-value { background: var(--chart-expense); border-radius: 8px; }
meter::-moz-meter-bar { background: var(--chart-expense); border-radius: 8px; }

.bill-list li + li, .setting-list li + li { border-top: 1px solid var(--line); }
.bill-row {
  display: grid;
  grid-template-columns: minmax(0, 1fr) auto;
  width: 100%;
  align-items: start;
  gap: 5px 12px;
  padding: 11px 0;
  min-height: 54px;
  border: 0;
  background: none;
  text-align: left;
}
.bill-title { display: flex; flex-wrap: wrap; align-items: center; gap: 5px 7px; font-weight: 700; overflow-wrap: anywhere; }
.bill-meta { display: flex; flex-wrap: wrap; gap: 2px 7px; overflow-wrap: anywhere; }
.bill-value { display: grid; justify-items: end; gap: 4px; white-space: nowrap; }
.pill { display: inline-block; font-size: .75rem; line-height: 1.3; padding: 2px 7px; border-radius: 7px; color: var(--accent); background: var(--accent-wash); }
.pill.warn { color: var(--warning); background: var(--warning-bg); }
.bill-count { margin-bottom: 5px; }

.form, .fields { display: grid; gap: 12px; }
.pair { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 9px; }
.inline { display: flex; align-items: end; gap: 8px; }
.inline label { flex: 1; }
label.field { display: grid; min-width: 0; gap: 5px; color: var(--muted); font-size: .82rem; font-weight: 620; }
input:not([type="checkbox"]), select, textarea {
  width: 100%;
  min-width: 0;
  min-height: 44px;
  padding: 8px 10px;
  font-size: 16px;
  font-weight: 470;
  color: var(--ink);
  border: 1px solid var(--line);
  border-radius: 10px;
  background: var(--surface);
  text-align: left;
}
input[type="date"], input[type="datetime-local"] { display: block; padding: 8px; }
input[type="date"]::-webkit-date-and-time-value, input[type="datetime-local"]::-webkit-date-and-time-value { text-align: left; }
input[type="checkbox"] { width: 20px; height: 20px; flex: 0 0 20px; margin: 0; accent-color: var(--chart-income); }
textarea { resize: vertical; min-height: 70px; }
.segmented { display: flex; flex-wrap: wrap; gap: 6px; }
.segmented .button { min-width: 44px; }
.segmented .button[aria-pressed="true"] { background: var(--accent-wash); color: var(--accent); border-color: var(--accent); }
.period-range { overflow-wrap: anywhere; }
.filters > summary { min-height: 37px; color: var(--accent); padding: 7px 0; }
.filters[open] .fields { margin-top: 6px; }
.difference { margin-top: 3px; font-size: .78rem; display: block; }
.insights { list-style: none; margin: 0; padding: 0; }
.insights li { padding: 9px 0; overflow-wrap: anywhere; }
.insights li + li { border-top: 1px solid var(--line); }
.insights small { display: block; margin-top: 3px; }
.section-note { line-height: 1.55; margin-top: 6px; }

.sheet {
  max-width: min(calc(100% - 28px), 500px);
  width: 100%;
  max-height: min(92vh, 850px);
  max-height: min(92dvh, 850px);
  padding: 0;
  border: 1px solid var(--line);
  border-radius: 20px;
  background: var(--surface);
  color: var(--ink);
  box-shadow: var(--shadow);
  overscroll-behavior: contain;
}
.sheet::backdrop { background: #0f1d18a6; }
.sheet-head { position: sticky; top: 0; padding: 11px 17px 8px; z-index: 1; border-bottom: 1px solid var(--line); background: var(--surface); }
.sheet-head h2 { font-size: 1.2rem; }
.sheet-head button { padding-inline: 11px; }
.sheet .form { padding: 14px 17px 19px; }
.sheet .quick-entry { background: var(--surface-soft); border-radius: 12px; padding: 11px; display: grid; gap: 6px; }
.quick-entry input { min-height: 42px; }
.quick-entry .button { border-color: var(--line); background: var(--surface); }
.amount-entry { grid-template-columns: minmax(75px, 1fr) minmax(80px, .6fr); }
.amount-entry input[name="amount"] { font-size: 1.22rem; font-weight: 700; font-variant-numeric: tabular-nums; }
.category-entry { align-items: end; }
.category-entry .button { height: 44px; }
.sheet details summary { color: var(--accent); min-height: 39px; padding: 9px 0; }
.sheet details .fields { padding-top: 3px; }
.setting-row { display: flex; min-height: 44px; align-items: center; justify-content: space-between; gap: 6px; padding: 3px 0; }
.setting-row > button:first-child { flex: 1; text-align: left; overflow-wrap: anywhere; white-space: normal; }
.setting-check { display: flex; align-items: center; gap: 7px; flex: 1; min-height: 44px; }
.setting-actions { display: flex; gap: 5px; }
.setting-actions .button { min-width: 44px; }
.category-child { padding-left: 15px; }
.toast {
  max-width: min(calc(100% - 24px), 420px);
  position: fixed;
  z-index: 3;
  bottom: calc(79px + env(safe-area-inset-bottom));
  left: 50%;
  transform: translateX(-50%);
  padding: 10px 16px;
  border-radius: 12px;
  background: var(--hero);
  color: var(--hero-text);
  box-shadow: var(--shadow);
}

@media (max-width: 640px) {
  .header-inner { padding: 6px 15px; min-height: 60px; }
  .page { padding: 19px 13px calc(102px + env(safe-area-inset-bottom)); }
  .card { padding: 15px; border-radius: 18px; }
  .balance-card { padding: 19px; }
  .cards-grid { grid-template-columns: 1fr; }
  .page-head { margin-bottom: 14px; }
  .graph { height: 148px; }
  .sheet {
    margin: auto 0 0;
    max-width: 100%;
    border-radius: 20px 20px 0 0;
    border-bottom: 0;
    padding-bottom: env(safe-area-inset-bottom);
  }
}
