:root {
  color-scheme: light;
  --bg: #ffffff;
  --surface: #ffffff;
  --ink: #191817;
  --muted: #6f6b68;
  --line: #e9e6e3;
  --accent: #fc4a3c;
  --accent-dark: #dc3329;
  --accent-soft: #fff0ed;
  --teal: #16898a;
  --teal-dark: #0d6e70;
  --teal-soft: #eaf7f6;
  --sand: #fff9ed;
  --sand-line: #eedfbe;
  --danger: #d52e28;
  --shadow: 0 18px 50px rgba(38, 30, 24, 0.14);
  --radius-lg: 20px;
  --radius-md: 13px;
  --page-x: 16px;
  --bottom-actions-height: 124px;
  font-family: -apple-system, BlinkMacSystemFont, "PingFang SC", "Microsoft YaHei", "Noto Sans CJK SC", sans-serif;
}

* { box-sizing: border-box; }

html {
  min-height: 100%;
  background: #f2f0ed;
  -webkit-text-size-adjust: 100%;
  text-size-adjust: 100%;
}

body {
  min-width: 320px;
  min-height: 100%;
  margin: 0;
  overflow-x: hidden;
  color: var(--ink);
  background: #f2f0ed;
  -webkit-font-smoothing: antialiased;
  -webkit-tap-highlight-color: transparent;
}

button, input { font: inherit; }
button { color: inherit; touch-action: manipulation; }

button:focus-visible,
input:focus-visible {
  outline: 3px solid rgba(252, 74, 60, 0.22);
  outline-offset: 2px;
}

.app-shell {
  position: relative;
  width: min(100%, 480px);
  min-height: 100dvh;
  margin: 0 auto;
  overflow: hidden;
  background: var(--bg);
  box-shadow: 0 0 38px rgba(39, 32, 27, 0.09);
}

.top-safe { height: max(8px, env(safe-area-inset-top)); }

.app-header {
  display: grid;
  grid-template-columns: 44px 1fr 44px;
  align-items: center;
  min-height: 62px;
  padding: 4px var(--page-x);
}

.brand-lockup {
  grid-column: 2;
  text-align: center;
}

.brand-lockup h1 {
  margin: 0;
  font-size: 21px;
  font-weight: 780;
  letter-spacing: 0.06em;
}

.brand-lockup p {
  margin: 2px 0 0;
  color: var(--muted);
  font-size: 13px;
  letter-spacing: 0.09em;
}

.icon-button {
  grid-column: 3;
  display: grid;
  width: 44px;
  height: 44px;
  padding: 0;
  place-items: center;
  border: 0;
  border-radius: 50%;
  background: transparent;
  cursor: pointer;
}

.icon-button:active { background: #f4f2f0; }

.icon {
  display: inline-block;
  width: 22px;
  height: 22px;
  vertical-align: middle;
}

.family-rail {
  display: grid;
  grid-template-columns: 1fr 38px 1fr;
  align-items: center;
  padding: 6px 20px 10px;
}

.family {
  display: flex;
  align-items: center;
  gap: 6px;
  min-width: 0;
}

.family strong {
  overflow: hidden;
  font-size: 15px;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.family-a { color: var(--accent); }
.family-b { color: var(--teal); }
.family-b { justify-content: flex-end; }

.family-mark {
  display: inline-grid;
  flex: 0 0 auto;
  place-items: center;
}

.family-mark .icon { width: 22px; height: 22px; }

.split-mark {
  display: grid;
  place-items: center;
  color: #85807b;
}

.content-view {
  display: none;
  padding: 0 var(--page-x);
}

.content-view.active { display: block; }

.settlement-panel {
  padding: 12px 14px 11px;
  text-align: center;
  border: 1px solid var(--sand-line);
  border-radius: var(--radius-lg);
  background: var(--sand);
}

.settlement-label {
  margin: 0;
  font-size: 13px;
  font-weight: 650;
}

.settlement-amount {
  margin: 1px 0 0;
  color: var(--accent-dark);
  font-size: clamp(34px, 9.5vw, 42px);
  font-variant-numeric: tabular-nums;
  font-weight: 780;
  line-height: 1.14;
  letter-spacing: -0.035em;
}

.settlement-panel[data-receiver="B"] .settlement-amount { color: var(--teal-dark); }
.settlement-panel[data-receiver="none"] .settlement-amount { color: var(--ink); }

.settlement-note {
  margin: 0;
  color: var(--muted);
  font-size: 13px;
}

.summary-grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  margin-top: 9px;
  padding-top: 9px;
  border-top: 1px dashed #e7d9b8;
}

.summary-grid > div {
  display: grid;
  gap: 2px;
}

.summary-grid > div + div { border-left: 1px solid #eadfc8; }

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

.summary-grid strong {
  font-size: clamp(18px, 5.4vw, 23px);
  font-variant-numeric: tabular-nums;
  line-height: 1.25;
}

.filter-tabs {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  height: 42px;
  margin: 10px 0 12px;
  overflow: hidden;
  border: 1px solid #ddd9d6;
  border-radius: 11px;
}

.filter {
  padding: 0 8px;
  overflow: hidden;
  border: 0;
  background: white;
  font-size: 13px;
  text-overflow: ellipsis;
  white-space: nowrap;
  cursor: pointer;
}

.filter + .filter { border-left: 1px solid #e5e2df; }
.filter.active { color: white; background: var(--accent); font-weight: 700; }
.filter[data-filter="A"]:not(.active) { color: var(--accent-dark); }
.filter[data-filter="B"]:not(.active) { color: var(--teal-dark); }
.filter[data-filter="B"].active { background: var(--teal); }

.expense-list { min-height: 200px; }

.date-group + .date-group { margin-top: 13px; }

.date-heading {
  display: flex;
  align-items: center;
  gap: 7px;
  margin: 0 3px 7px;
  color: #312f2d;
  font-size: 13px;
  font-weight: 720;
}

.date-heading .icon { width: 17px; height: 17px; }

.expense-group {
  overflow: hidden;
  border: 1px solid var(--line);
  border-radius: var(--radius-md);
  background: var(--surface);
}

.expense-row {
  position: relative;
  display: grid;
  grid-template-columns: 41px minmax(0, 1fr) auto 30px;
  gap: 9px;
  align-items: center;
  min-height: 64px;
  padding: 7px 9px;
}

.expense-row + .expense-row { border-top: 1px solid var(--line); }

.category-icon {
  display: grid;
  width: 40px;
  height: 40px;
  place-items: center;
  color: white;
  border-radius: 50%;
  background: var(--accent);
}

.expense-row[data-payer="B"] .category-icon { background: var(--teal); }
.category-icon .icon { width: 21px; height: 21px; }

.expense-main { min-width: 0; }
.expense-main strong,
.expense-main span { display: block; }

.expense-main strong {
  overflow: hidden;
  font-size: 15px;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.expense-main span {
  margin-top: 2px;
  color: var(--muted);
  font-size: 12px;
}

.expense-amount { text-align: right; }
.expense-amount strong,
.expense-amount span { display: block; }

.expense-amount strong {
  color: var(--accent-dark);
  font-size: 17px;
  font-variant-numeric: tabular-nums;
}

.expense-row[data-payer="B"] .expense-amount strong { color: var(--teal-dark); }

.expense-amount span {
  max-width: 110px;
  margin-top: 2px;
  overflow: hidden;
  color: var(--muted);
  font-size: 11px;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.row-menu {
  display: grid;
  width: 34px;
  height: 44px;
  margin-right: -5px;
  padding: 0;
  place-items: center;
  border: 0;
  border-radius: 9px;
  color: #8b8682;
  background: transparent;
  cursor: pointer;
}

.row-menu:active { background: #f4f2f0; }
.row-menu .icon { width: 20px; height: 20px; }

.empty-state {
  display: grid;
  min-height: 205px;
  padding: 30px 18px;
  place-items: center;
  align-content: center;
  text-align: center;
  border: 1px dashed #dcd7d2;
  border-radius: var(--radius-lg);
}

.empty-state .empty-icon {
  display: grid;
  width: 56px;
  height: 56px;
  margin-bottom: 11px;
  place-items: center;
  color: var(--accent);
  border-radius: 50%;
  background: var(--accent-soft);
}

.empty-state .icon { width: 28px; height: 28px; }
.empty-state h2 { margin: 0; font-size: 17px; }
.empty-state p { margin: 7px 0 0; color: var(--muted); font-size: 13px; line-height: 1.6; }

.bottom-spacer { height: calc(var(--bottom-actions-height) + env(safe-area-inset-bottom)); }

.bottom-actions {
  position: fixed;
  z-index: 20;
  right: max(0px, calc((100vw - 480px) / 2));
  bottom: 0;
  left: max(0px, calc((100vw - 480px) / 2));
  width: auto;
  border-top: 1px solid var(--line);
  background: rgba(255, 255, 255, 0.96);
  backdrop-filter: blur(18px);
}

.add-expense-button {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  width: calc(100% - 32px);
  height: 48px;
  margin: 8px 16px 5px;
  border: 0;
  border-radius: 13px;
  color: white;
  background: var(--accent);
  box-shadow: 0 8px 20px rgba(252, 74, 60, 0.22);
  font-size: 16px;
  font-weight: 750;
  cursor: pointer;
}

.add-expense-button:active { transform: translateY(1px); background: var(--accent-dark); }
.add-expense-button .icon { width: 21px; height: 21px; }

.bottom-nav {
  display: grid;
  grid-template-columns: 1fr 1fr;
  height: 54px;
}

.nav-item {
  display: grid;
  gap: 2px;
  padding: 4px 0 3px;
  place-items: center;
  border: 0;
  color: #5d5956;
  background: transparent;
  font-size: 11px;
  cursor: pointer;
}

.nav-item .icon { width: 22px; height: 22px; }
.nav-item.active { color: var(--accent); font-weight: 700; }
.bottom-safe { height: env(safe-area-inset-bottom); }

.stats-view { padding-top: 4px; }

.stats-heading {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  margin-bottom: 14px;
}

.stats-heading h2 { margin: 0; font-size: 20px; }
.stats-heading p { margin: 3px 0 0; color: var(--muted); font-size: 12px; }
.stats-heading > span { color: var(--muted); font-size: 12px; }

.payment-comparison {
  padding: 13px 15px;
  border-radius: 18px;
  background: #f7f5f2;
}

.payment-comparison > p { margin: 0 0 7px; color: var(--muted); font-size: 12px; }
.payment-row {
  display: grid;
  grid-template-columns: minmax(0, 1fr) auto;
  gap: 12px;
  align-items: baseline;
  min-height: 36px;
  padding: 7px 0;
}
.payment-row + .payment-row { border-top: 1px solid rgba(111, 107, 104, 0.12); }
.payment-row span {
  min-width: 0;
  overflow: hidden;
  font-size: 15px;
  font-weight: 680;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.payment-row strong {
  text-align: right;
  font-size: 18px;
  font-variant-numeric: tabular-nums;
  white-space: nowrap;
}

.category-stats { margin-top: 18px; }
.category-stats h3 { margin: 0 0 10px; font-size: 16px; }

.category-stat {
  display: grid;
  grid-template-columns: 36px minmax(0, 1fr) auto;
  gap: 9px;
  align-items: center;
  padding: 8px 0;
}

.category-stat-icon {
  display: grid;
  width: 36px;
  height: 36px;
  place-items: center;
  color: #4a4744;
  border-radius: 11px;
  background: #f2f0ed;
}

.category-stat-icon .icon { width: 19px; height: 19px; }
.category-stat-main { min-width: 0; }
.category-stat-title { display: flex; gap: 8px; justify-content: space-between; margin-bottom: 6px; font-size: 13px; }
.category-stat-title strong { min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.category-stat-title span:last-child { flex: 0 0 auto; color: var(--muted); font-size: 11px; white-space: nowrap; }
.stat-bar { height: 5px; overflow: hidden; border-radius: 99px; background: #eeeae7; }
.stat-bar > span { display: block; height: 100%; border-radius: inherit; background: var(--accent); }
.category-stat > strong { min-width: 84px; text-align: right; font-size: 14px; font-variant-numeric: tabular-nums; white-space: nowrap; }

.sheet-backdrop {
  position: fixed;
  z-index: 50;
  inset: 0;
  background: rgba(25, 24, 23, 0.48);
  opacity: 0;
  transition: opacity 180ms ease;
}

.sheet-backdrop.visible { opacity: 1; }

.bottom-sheet {
  position: fixed;
  z-index: 51;
  right: max(0px, calc((100vw - 480px) / 2));
  bottom: 0;
  left: max(0px, calc((100vw - 480px) / 2));
  max-height: 94dvh;
  padding: 6px 16px calc(16px + env(safe-area-inset-bottom));
  overflow-x: hidden;
  overflow-y: auto;
  overscroll-behavior-y: contain;
  border-radius: 24px 24px 0 0;
  background: white;
  box-shadow: var(--shadow);
  transform: translateY(102%);
  transition: transform 240ms cubic-bezier(.2,.75,.25,1);
}

.bottom-sheet.visible { transform: translateY(0); }
.sheet-handle { width: 40px; height: 4px; margin: 0 auto 5px; border-radius: 99px; background: #d6d2cf; }

.sheet-heading {
  display: grid;
  grid-template-columns: 62px 1fr 62px;
  align-items: center;
  height: 44px;
}

.sheet-heading h2 { margin: 0; text-align: center; font-size: 19px; }
.text-button { justify-self: start; padding: 10px 3px; border: 0; color: var(--muted); background: none; font-size: 14px; cursor: pointer; }
.heading-spacer { width: 62px; }

.field-label, .form-section legend {
  display: block;
  margin-bottom: 7px;
  color: #373432;
  font-size: 13px;
  font-weight: 700;
}

.amount-input-wrap {
  display: flex;
  align-items: baseline;
  justify-content: center;
  gap: 10px;
  min-height: 58px;
}

.amount-input-wrap span { font-size: 28px; font-weight: 700; }
.amount-input-wrap input {
  width: min(72%, 260px);
  padding: 0;
  border: 0;
  outline: none;
  color: var(--ink);
  background: transparent;
  font-size: 40px;
  font-variant-numeric: tabular-nums;
  font-weight: 760;
  letter-spacing: -0.03em;
}
.amount-input-wrap input::placeholder { color: #c3c0bd; }

.form-section {
  margin: 0;
  padding: 11px 0;
  border: 0;
  border-bottom: 1px solid var(--line);
}

.category-picker { display: grid; grid-template-columns: repeat(5, 1fr); gap: 7px; }

.category-option {
  display: grid;
  min-width: 0;
  min-height: 60px;
  gap: 3px;
  padding: 6px 3px 5px;
  place-items: center;
  border: 1px solid var(--line);
  border-radius: 11px;
  color: var(--muted);
  background: white;
  font-size: 12px;
  cursor: pointer;
}

.category-option .icon { width: 22px; height: 22px; }
.category-option.selected { border-color: var(--accent); color: var(--accent-dark); background: var(--accent-soft); font-weight: 700; }

.payer-picker { display: grid; grid-template-columns: 1fr 1fr; gap: 9px; }
.payer-option {
  justify-content: center;
  min-width: 0;
  min-height: 46px;
  padding: 8px 10px;
  border: 1px solid var(--line);
  border-radius: 11px;
  cursor: pointer;
}
.payer-option input { position: absolute; width: 1px; height: 1px; opacity: 0; pointer-events: none; }
.payer-option.family-a.selected { border-color: var(--accent); background: var(--accent-soft); }
.payer-option.family-b.selected { border-color: var(--teal); background: var(--teal-soft); }
.payer-option .family-mark .icon { width: 22px; height: 22px; }
.payer-option strong { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }

.form-field {
  display: grid;
  grid-template-columns: minmax(0, 1fr);
  min-width: 0;
}

.form-field input,
.settings-dialog input {
  display: block;
  min-width: 0;
  max-width: 100%;
  width: 100%;
  height: 45px;
  padding: 0 12px;
  border: 1px solid var(--line);
  border-radius: 10px;
  outline: none;
  color: var(--ink);
  background: #fbfaf9;
  font-size: 16px;
}

.form-field input:focus,
.settings-dialog input:focus { border-color: var(--accent); box-shadow: 0 0 0 3px var(--accent-soft); }

.date-control {
  position: relative;
  display: grid;
  min-width: 0;
  width: 100%;
  height: 45px;
  place-items: center;
  overflow: hidden;
  border: 1px solid var(--line);
  border-radius: 10px;
  color: var(--ink);
  background: #fbfaf9;
}

.date-control:focus-within {
  border-color: var(--accent);
  box-shadow: 0 0 0 3px var(--accent-soft);
}

.date-control-value {
  padding-inline: 42px;
  text-align: center;
  font-size: 16px;
  font-variant-numeric: tabular-nums;
  line-height: 1;
  pointer-events: none;
}

.date-control-icon {
  position: absolute;
  right: 13px;
  display: grid;
  width: 18px;
  height: 18px;
  place-items: center;
  color: var(--muted);
  pointer-events: none;
}

.date-control-icon .icon { width: 18px; height: 18px; }

.date-control #expense-date {
  position: absolute;
  z-index: 2;
  inset: 0;
  width: 100%;
  height: 100%;
  margin: 0;
  padding: 0;
  border: 0;
  border-radius: 0;
  opacity: 0;
  cursor: pointer;
  -webkit-appearance: none;
  appearance: none;
}

.form-error { min-height: 18px; margin: 1px 0 5px; color: var(--danger); font-size: 12px; }

.primary-button, .secondary-button, .danger-button {
  min-height: 46px;
  padding: 0 18px;
  border-radius: 11px;
  font-weight: 720;
  cursor: pointer;
}

.primary-button { border: 0; color: white; background: var(--accent); }
.primary-button:disabled { opacity: .55; cursor: wait; }
.secondary-button { border: 1px solid var(--line); color: #47433f; background: #f8f6f4; }
.danger-button { border: 0; color: white; background: var(--danger); }
#expense-form > .primary-button { width: 100%; }

dialog {
  width: min(calc(100% - 36px), 390px);
  padding: 0;
  border: 0;
  border-radius: 21px;
  color: var(--ink);
  background: white;
  box-shadow: var(--shadow);
}

dialog::backdrop { background: rgba(25, 24, 23, 0.48); backdrop-filter: blur(2px); }
dialog form { padding: 24px; text-align: center; }
.dialog-icon { display: grid; width: 54px; height: 54px; margin: 0 auto 12px; place-items: center; border-radius: 50%; color: var(--accent); background: var(--accent-soft); }
.dialog-icon.danger { color: var(--danger); }
.dialog-icon .icon { width: 27px; height: 27px; }
dialog h2 { margin: 0; font-size: 21px; }
dialog form > p:not(.form-error) { margin: 7px auto 19px; color: var(--muted); font-size: 13px; line-height: 1.55; }
.settings-dialog label { display: block; margin: 13px 0; text-align: left; }
.settings-dialog label > span { display: block; margin-bottom: 6px; font-size: 12px; font-weight: 700; }
.settings-dialog .form-error { text-align: left; }
.dialog-actions { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; margin-top: 16px; }

.toast {
  position: fixed;
  z-index: 90;
  top: calc(18px + env(safe-area-inset-top));
  left: 50%;
  max-width: calc(100% - 40px);
  padding: 10px 16px;
  border-radius: 99px;
  color: white;
  background: #292725;
  box-shadow: 0 8px 24px rgba(0,0,0,.2);
  font-size: 13px;
  opacity: 0;
  pointer-events: none;
  transform: translate(-50%, -12px);
  transition: opacity 160ms ease, transform 160ms ease;
}
.toast.visible { opacity: 1; transform: translate(-50%, 0); }

[hidden] { display: none !important; }

@media (max-width: 360px) {
  :root { --page-x: 13px; }
  .family-rail { padding-inline: 17px; }
  .expense-row { grid-template-columns: 42px minmax(0, 1fr) auto 22px; gap: 8px; padding-inline: 8px; }
  .category-icon { width: 40px; height: 40px; }
  .expense-main strong { font-size: 15px; }
  .expense-amount strong { font-size: 16px; }
  .category-picker { gap: 4px; }
  .category-option { font-size: 11px; }
}

@media (min-width: 700px) {
  body { padding: 24px 0; }
  .app-shell { min-height: calc(100dvh - 48px); border-radius: 24px; }
  .bottom-actions { bottom: 24px; border-radius: 0 0 24px 24px; }
  .bottom-sheet { bottom: 24px; border-radius: 24px; }
}

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { scroll-behavior: auto !important; transition-duration: 0.01ms !important; }
}
