:root {
  color-scheme: light;
  --bg: #f3f6f4;
  --panel: #ffffff;
  --panel-soft: #edf5f2;
  --text: #15231f;
  --muted: #65736f;
  --line: rgba(21, 35, 31, 0.12);
  --shadow: 0 16px 38px rgba(20, 40, 34, 0.08);
  --accent: #0f766e;
  --danger: #dc2626;
  --income: #16a34a;
  --tab: rgba(255, 255, 255, 0.92);
  --field: #f7faf8;
}

:root[data-theme="dark"] {
  color-scheme: dark;
  --bg: #0f1513;
  --panel: #18211e;
  --panel-soft: #202d29;
  --text: #edf5f2;
  --muted: #9aa9a4;
  --line: rgba(237, 245, 242, 0.12);
  --shadow: 0 18px 42px rgba(0, 0, 0, 0.34);
  --accent: #2dd4bf;
  --danger: #f87171;
  --income: #4ade80;
  --tab: rgba(24, 33, 30, 0.94);
  --field: #111a17;
}

* {
  box-sizing: border-box;
}

body {
  margin: 0;
  min-height: 100vh;
  background: var(--bg);
  color: var(--text);
  font-family: -apple-system, BlinkMacSystemFont, "SF Pro Text", "PingFang SC", "Hiragino Sans GB", sans-serif;
}

button,
input,
select,
textarea {
  font: inherit;
}

button {
  color: inherit;
}

.app-shell {
  min-height: 100vh;
  padding: max(18px, env(safe-area-inset-top)) 16px calc(86px + env(safe-area-inset-bottom));
}

.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  margin: 4px auto 18px;
  max-width: 760px;
}

.page-title {
  margin: 0;
  font-size: 30px;
  letter-spacing: 0;
}

.page-subtitle {
  margin: 4px 0 0;
  color: var(--muted);
  font-size: 14px;
}

.page {
  max-width: 760px;
  margin: 0 auto;
}

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

.card {
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 20px;
  box-shadow: var(--shadow);
  padding: 16px;
}

.hero-card h2 {
  margin: 8px 0 8px;
  font-size: 26px;
  line-height: 1.15;
}

.hero-card p {
  margin: 0;
  color: var(--muted);
  line-height: 1.6;
}

.import-card {
  display: grid;
  gap: 8px;
  width: 100%;
  border: 1px dashed color-mix(in srgb, var(--accent) 52%, var(--line));
  background: color-mix(in srgb, var(--accent) 8%, var(--panel));
  border-radius: 20px;
  box-shadow: var(--shadow);
  padding: 18px;
}

.import-card strong {
  font-size: 19px;
}

.import-card span {
  color: var(--muted);
  font-size: 13px;
  line-height: 1.5;
}

.import-card input {
  width: 100%;
  color: var(--muted);
}

.soft-card {
  background: var(--panel-soft);
  border: 1px solid var(--line);
  border-radius: 18px;
  padding: 14px;
}

.summary-grid,
.metric-grid {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 12px;
}

.metric {
  border-radius: 18px;
  background: var(--panel-soft);
  padding: 14px;
}

.metric span,
.eyebrow {
  display: block;
  color: var(--muted);
  font-size: 12px;
}

.metric strong {
  display: block;
  margin-top: 7px;
  font-size: 21px;
  line-height: 1.1;
}

.section-head {
  display: flex;
  align-items: baseline;
  justify-content: space-between;
  gap: 10px;
  margin: 10px 2px 8px;
}

.section-head h2 {
  margin: 0;
  font-size: 18px;
}

.section-head span {
  color: var(--muted);
  font-size: 12px;
}

.entry-card,
.category-card,
.setting-row,
.cycle-row {
  width: 100%;
  border: 1px solid var(--line);
  background: var(--panel);
  border-radius: 18px;
  padding: 15px;
  text-align: left;
  box-shadow: var(--shadow);
}

.entry-card {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
}

.entry-card.income {
  background: color-mix(in srgb, var(--income) 10%, var(--panel));
}

.category-card {
  position: relative;
  overflow: hidden;
}

.category-card::before {
  content: "";
  position: absolute;
  inset: 0 auto 0 0;
  width: var(--progress, 0%);
  background: color-mix(in srgb, var(--category-color, var(--accent)) 22%, transparent);
  pointer-events: none;
}

.category-card.warning {
  border-color: var(--danger);
  border-width: 2px;
}

.category-card > * {
  position: relative;
  z-index: 1;
}

.category-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 18px;
}

.category-title {
  font-size: 20px;
  font-weight: 750;
}

.category-stats {
  display: grid;
  gap: 6px;
  min-width: 132px;
  text-align: right;
}

.category-stats small {
  color: var(--muted);
  margin-right: 6px;
}

.category-stats strong {
  font-size: 18px;
}

.negative {
  color: var(--danger);
}

.tabbar {
  position: fixed;
  left: 0;
  right: 0;
  bottom: 0;
  z-index: 20;
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 4px;
  padding: 8px 12px calc(8px + env(safe-area-inset-bottom));
  background: var(--tab);
  border-top: 1px solid var(--line);
  backdrop-filter: blur(16px);
}

.tabbar button {
  border: 0;
  background: transparent;
  border-radius: 14px;
  padding: 9px 4px;
  color: var(--muted);
  font-size: 12px;
}

.tabbar button.active {
  color: var(--accent);
  background: color-mix(in srgb, var(--accent) 12%, transparent);
  font-weight: 700;
}

.icon-button,
.primary-button,
.secondary-button,
.danger-button {
  border: 0;
  border-radius: 14px;
  padding: 11px 14px;
  font-weight: 700;
}

.icon-button {
  width: 42px;
  height: 42px;
  padding: 0;
  border-radius: 21px;
  background: var(--panel);
  border: 1px solid var(--line);
}

.primary-button {
  color: white;
  background: var(--accent);
}

.secondary-button {
  background: var(--panel-soft);
}

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

.compact {
  padding: 8px 10px;
  font-size: 12px;
}

.toolbar-row {
  display: flex;
  gap: 10px;
  align-items: center;
  justify-content: space-between;
}

.segmented {
  display: grid;
  grid-auto-flow: column;
  grid-auto-columns: 1fr;
  gap: 4px;
  padding: 4px;
  border-radius: 16px;
  background: var(--panel-soft);
}

.segmented button {
  border: 0;
  border-radius: 12px;
  padding: 9px 8px;
  background: transparent;
  color: var(--muted);
}

.segmented button.active {
  color: var(--text);
  background: var(--panel);
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.08);
  font-weight: 700;
}

.list {
  display: grid;
  gap: 10px;
}

.ledger-day {
  margin-top: 8px;
  color: var(--muted);
  font-size: 13px;
  font-weight: 700;
}

.ledger-row {
  display: grid;
  grid-template-columns: 4px minmax(0, 1fr) auto;
  gap: 12px;
  align-items: center;
  padding: 14px 0;
  border-bottom: 1px solid var(--line);
}

.ledger-row:last-child {
  border-bottom: 0;
}

.ledger-accent {
  width: 4px;
  height: 42px;
  border-radius: 3px;
  background: var(--entry-color, var(--accent));
}

.ledger-title {
  font-weight: 750;
}

.ledger-meta,
.small {
  color: var(--muted);
  font-size: 12px;
}

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

.ledger-right {
  display: grid;
  justify-items: end;
  gap: 8px;
}

.mini-button {
  border: 0;
  border-radius: 999px;
  background: color-mix(in srgb, var(--income) 16%, transparent);
  color: var(--income);
  font-size: 12px;
  font-weight: 800;
  padding: 6px 10px;
}

.amount {
  font-weight: 800;
  white-space: nowrap;
}

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

.modal-backdrop {
  position: fixed;
  inset: 0;
  z-index: 40;
  display: flex;
  align-items: flex-end;
  justify-content: center;
  background: rgba(0, 0, 0, 0.28);
  padding: 18px 12px max(18px, env(safe-area-inset-bottom));
}

.modal {
  width: min(760px, 100%);
  max-height: min(88vh, 760px);
  overflow: auto;
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 24px;
  padding: 16px;
  box-shadow: 0 24px 80px rgba(0, 0, 0, 0.28);
}

.modal h2 {
  margin: 0 0 14px;
}

.form-grid {
  display: grid;
  gap: 12px;
}

.field {
  display: grid;
  gap: 6px;
}

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

.field input,
.field select,
.field textarea {
  width: 100%;
  border: 1px solid var(--line);
  border-radius: 14px;
  background: var(--field);
  color: var(--text);
  padding: 12px;
  outline: none;
}

.field textarea {
  min-height: 74px;
  resize: vertical;
}

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

.checkbox-row {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 12px;
  border-radius: 14px;
  background: var(--panel-soft);
}

.chart-card {
  overflow: hidden;
}

.chart-scroll {
  overflow-x: auto;
  -webkit-overflow-scrolling: touch;
  padding: 4px 0 0;
}

.chart {
  display: block;
  width: 100%;
  height: auto;
  min-height: 210px;
  color: var(--muted);
}

.chart.wide {
  min-width: var(--chart-width, 900px);
}

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

.chart-grid-line.strong {
  stroke: color-mix(in srgb, var(--text) 18%, var(--line));
}

.chart-axis-label,
.chart-bar-label {
  fill: var(--muted);
  font-size: 11px;
}

.chart-zero-marker {
  stroke: color-mix(in srgb, var(--muted) 42%, transparent);
  stroke-width: 2;
  stroke-linecap: round;
}

.chart-area {
  fill: color-mix(in srgb, var(--accent) 12%, transparent);
}

.chart-line {
  fill: none;
  stroke: var(--accent);
  stroke-width: 3.5;
  stroke-linecap: round;
  stroke-linejoin: round;
}

.chart-point {
  fill: var(--panel);
  stroke: var(--accent);
  stroke-width: 2.5;
}

.donut-wrap {
  display: grid;
  grid-template-columns: 176px 1fr;
  gap: 18px;
  align-items: center;
}

.donut-figure {
  position: relative;
  width: 176px;
  height: 176px;
  margin: 0 auto;
}

.donut-figure svg {
  width: 100%;
  height: 100%;
  display: block;
}

.donut-track {
  fill: transparent;
  stroke: var(--panel-soft);
  stroke-width: 16;
}

.donut-segment {
  stroke-linecap: butt;
}

.donut-center {
  position: absolute;
  inset: 42px;
  display: grid;
  place-content: center;
  text-align: center;
  border-radius: 999px;
  background: color-mix(in srgb, var(--panel) 78%, transparent);
}

.donut-center span {
  color: var(--muted);
  font-size: 11px;
}

.donut-center strong {
  margin-top: 4px;
  font-size: 17px;
  line-height: 1.05;
}

.legend {
  display: grid;
  gap: 9px;
}

.legend-item {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  color: var(--muted);
  font-size: 13px;
  padding: 9px 10px;
  border-radius: 14px;
  background: var(--panel-soft);
}

.legend-name {
  display: flex;
  align-items: center;
  min-width: 0;
}

.legend-value {
  display: grid;
  justify-items: end;
  gap: 2px;
  white-space: nowrap;
}

.legend-value strong {
  color: var(--text);
}

.legend-value small {
  color: var(--muted);
  font-size: 11px;
}

.dot {
  width: 10px;
  height: 10px;
  border-radius: 50%;
  display: inline-block;
  margin-right: 7px;
  background: var(--dot-color);
}

.empty {
  color: var(--muted);
  text-align: center;
  padding: 32px 16px;
}

.settings-grid {
  display: grid;
  gap: 12px;
}

.setting-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
}

.color-swatch {
  width: 22px;
  height: 22px;
  border-radius: 50%;
  border: 2px solid var(--panel);
  box-shadow: 0 0 0 1px var(--line);
}

.category-manage-row {
  display: grid;
  grid-template-columns: auto 1fr auto;
  gap: 12px;
  align-items: center;
  padding: 12px 0;
  border-bottom: 1px solid var(--line);
}

.category-manage-row:last-child {
  border-bottom: 0;
}

@media (min-width: 760px) {
  .app-shell {
    padding-bottom: 104px;
  }

  .tabbar {
    left: 50%;
    right: auto;
    bottom: 18px;
    width: min(720px, calc(100% - 32px));
    transform: translateX(-50%);
    border: 1px solid var(--line);
    border-radius: 24px;
  }
}

@media (max-width: 520px) {
  .summary-grid,
  .metric-grid,
  .donut-wrap {
    grid-template-columns: 1fr;
  }

  .category-row {
    align-items: flex-start;
    flex-direction: column;
  }

  .category-stats {
    width: 100%;
    text-align: left;
  }
}
