:root {
  --bg: #f4f5f2;
  --surface: #ffffff;
  --ink: #1d2320;
  --muted: #6b736e;
  --line: #e3e5df;
  --accent: #06c755;
  --income: #0f8a4b;
  --expense: #c2410c;
  --transfer: #2563eb;
  --danger: #c0262d;
  --s0: #f97316;
  --s1: #2563eb;
  --s2: #16a34a;
  --s3: #a855f7;
  --s4: #eab308;
  --s5: #0891b2;
  --s6: #94a3b8;
  --radius: 14px;
  color-scheme: light dark;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #111413;
    --surface: #1b1f1d;
    --ink: #e8ebe8;
    --muted: #9aa39e;
    --line: #2c3330;
    --income: #4ade80;
    --expense: #fb923c;
    --transfer: #60a5fa;
    --danger: #f87171;
  }
}

* {
  box-sizing: border-box;
}

body {
  margin: 0;
  background: var(--bg);
  color: var(--ink);
  font:
    16px/1.5 -apple-system,
    'PingFang TC',
    'Noto Sans TC',
    system-ui,
    sans-serif;
  padding-bottom: calc(72px + env(safe-area-inset-bottom));
}

.top {
  position: sticky;
  top: 0;
  z-index: 2;
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 12px;
  padding: 12px 16px;
  background: var(--surface);
  border-bottom: 1px solid var(--line);
}

h1 {
  margin: 0;
  font-size: 1.1rem;
}

h2 {
  margin: 0 0 10px;
  font-size: 1rem;
}

p {
  margin: 0;
}

main {
  padding: 14px 16px;
  max-width: 720px;
  margin: 0 auto;
}

.stack {
  display: grid;
  gap: 14px;
}

.card {
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  padding: 16px;
  min-width: 0;
}

.muted {
  color: var(--muted);
  font-size: 0.85rem;
}

.empty {
  text-align: center;
  color: var(--muted);
  padding: 24px 0;
  list-style: none;
}

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

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

.transfer {
  color: var(--transfer);
}

.num {
  font-variant-numeric: tabular-nums;
  white-space: nowrap;
}

input,
select,
button {
  font: inherit;
  color: var(--ink);
  background: var(--bg);
  border: 1px solid var(--line);
  border-radius: 10px;
  padding: 10px 12px;
  min-height: 44px;
  min-width: 0;
}

button {
  cursor: pointer;
  background: var(--accent);
  border-color: var(--accent);
  color: #fff;
  font-weight: 600;
}

button.ghost {
  background: transparent;
  color: var(--ink);
  border-color: var(--line);
  font-weight: 500;
}

button.danger {
  background: transparent;
  color: var(--danger);
  border-color: var(--danger);
}

button.wide {
  width: 100%;
}

.kpis {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(140px, 1fr));
  gap: 10px;
}

.kpi {
  display: grid;
  gap: 2px;
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  padding: 12px 14px;
}

.kpi strong {
  font-size: 1.1rem;
  font-variant-numeric: tabular-nums;
  white-space: nowrap;
}

.rows,
.legend,
.entries {
  list-style: none;
  margin: 0;
  padding: 0;
}

.rows li {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 10px;
  padding: 10px 0;
  border-bottom: 1px solid var(--line);
}

.rows li:last-child {
  border-bottom: 0;
}

.actions {
  display: flex;
  gap: 6px;
  flex-shrink: 0;
}

.actions button {
  min-height: 36px;
  padding: 6px 10px;
  font-size: 0.85rem;
}

.entries {
  display: grid;
  gap: 8px;
}

.entry {
  display: flex;
  justify-content: space-between;
  align-items: flex-start;
  gap: 12px;
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: 12px;
  padding: 12px 14px;
  cursor: pointer;
}

.entry-main {
  display: grid;
  gap: 2px;
  min-width: 0;
}

.entry-main strong {
  overflow-wrap: anywhere;
}

.note {
  font-style: italic;
}

.filters,
.inline {
  display: flex;
  gap: 8px;
}

.filters input,
.inline input {
  flex: 1;
}

.field {
  display: grid;
  gap: 4px;
  color: var(--muted);
  font-size: 0.85rem;
}

.field[hidden] {
  display: none;
}

.field input,
.field select {
  font-size: 1rem;
  color: var(--ink);
}

.segments {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(0, 1fr));
  gap: 6px;
}

.seg {
  background: transparent;
  color: var(--ink);
  border-color: var(--line);
}

.seg.active {
  background: var(--accent);
  border-color: var(--accent);
  color: #fff;
}

details summary {
  cursor: pointer;
  color: var(--accent);
  padding: 10px 0;
}

.tabs {
  position: fixed;
  inset: auto 0 0;
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  background: var(--surface);
  border-top: 1px solid var(--line);
  padding-bottom: env(safe-area-inset-bottom);
  z-index: 3;
}

.tabs button {
  display: grid;
  justify-items: center;
  gap: 0;
  border: 0;
  border-radius: 0;
  background: transparent;
  color: var(--muted);
  font-size: 1.1rem;
  padding: 8px 0;
}

.tabs button span {
  font-size: 0.72rem;
}

.tabs button.active {
  color: var(--accent);
}

.toast {
  position: fixed;
  left: 16px;
  right: 16px;
  bottom: calc(84px + env(safe-area-inset-bottom));
  z-index: 4;
  margin: 0 auto;
  max-width: 480px;
  padding: 12px 16px;
  border-radius: 12px;
  background: var(--ink);
  color: var(--surface);
  text-align: center;
}

.toast.error {
  background: var(--danger);
  color: #fff;
}

/* charts (shared module) */
.chart {
  width: 100%;
  height: auto;
  display: block;
}

.chart .grid {
  stroke: var(--line);
}

.chart .axis {
  fill: var(--muted);
  /* The SVG is 640 units wide and scaled down on phones, so labels need a larger unit size. */
  font-size: 22px;
}

.chart rect.income {
  fill: var(--income);
}

.chart rect.expense {
  fill: var(--expense);
}

.chart .stroke {
  fill: none;
  stroke-width: 2;
}

.chart .stroke.expense {
  stroke: var(--expense);
}

.chart .area.expense {
  fill: var(--expense);
  opacity: 0.12;
}

.donut {
  display: grid;
  grid-template-columns: minmax(120px, 180px) 1fr;
  gap: 14px;
  align-items: center;
}

@media (max-width: 380px) {
  .donut {
    grid-template-columns: 1fr;
    justify-items: center;
  }
}

.donut .chart {
  max-width: 180px;
  margin: 0 auto;
}

.slice.s0 {
  stroke: var(--s0);
}
.slice.s1 {
  stroke: var(--s1);
}
.slice.s2 {
  stroke: var(--s2);
}
.slice.s3 {
  stroke: var(--s3);
}
.slice.s4 {
  stroke: var(--s4);
}
.slice.s5 {
  stroke: var(--s5);
}
.slice.s6 {
  stroke: var(--s6);
}

.legend {
  display: grid;
  gap: 6px;
  font-size: 0.88rem;
}

.legend li {
  display: grid;
  grid-template-columns: 12px 1fr auto;
  gap: 8px;
  align-items: center;
}

.dot {
  display: inline-block;
  width: 10px;
  height: 10px;
  border-radius: 50%;
  margin-right: 4px;
}

.dot.s0 {
  background: var(--s0);
}
.dot.s1 {
  background: var(--s1);
}
.dot.s2 {
  background: var(--s2);
}
.dot.s3 {
  background: var(--s3);
}
.dot.s4 {
  background: var(--s4);
}
.dot.s5 {
  background: var(--s5);
}
.dot.s6 {
  background: var(--s6);
}
.dot.income {
  background: var(--income);
}
.dot.expense {
  background: var(--expense);
}

.legend-inline {
  margin-top: 6px;
}

.button-link {
  display: inline-flex;
  align-items: center;
  min-height: 36px;
  padding: 6px 12px;
  border: 1px solid var(--line);
  border-radius: 10px;
  color: var(--ink);
  text-decoration: none;
  font-size: 0.85rem;
}
