:root {
  --bg: #f6f7f4;
  --panel: #ffffff;
  --ink: #1f2526;
  --muted: #647071;
  --line: #dfe5e1;
  --teal: #1f6f78;
  --teal-dark: #185760;
  --green: #248255;
  --red: #bd4739;
  --amber: #b2741d;
  --blue: #326ebd;
  --shadow: 0 14px 34px rgba(31, 37, 38, 0.09);
  font-family: "Segoe UI", "Noto Sans TC", system-ui, sans-serif;
}

* {
  box-sizing: border-box;
}

body {
  margin: 0;
  background: var(--bg);
  color: var(--ink);
}

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

button {
  cursor: pointer;
}

button:disabled {
  cursor: not-allowed;
}

.app-shell {
  min-height: 100vh;
  padding: calc(18px + env(safe-area-inset-top)) 16px calc(96px + env(safe-area-inset-bottom));
  max-width: 1040px;
  margin: 0 auto;
}

.topbar,
.section-head,
.water-row,
.record-main,
.record-meta,
.backup-item,
.topup-head,
.topup-person,
.compact-actions,
.quick-actions {
  display: flex;
  align-items: center;
}

.topbar {
  justify-content: space-between;
  gap: 16px;
  margin-bottom: 16px;
}

.eyebrow {
  margin: 0 0 4px;
  color: var(--muted);
  font-size: 0.78rem;
  font-weight: 700;
}

h1,
h2,
p {
  margin-top: 0;
}

h1 {
  margin-bottom: 0;
  font-size: 1.75rem;
  letter-spacing: 0;
}

h2 {
  margin-bottom: 0;
  font-size: 1.12rem;
  letter-spacing: 0;
}

.view {
  display: none;
}

.view.active {
  display: block;
}

.balance-panel,
.section,
.metric-card,
.alert-card,
.settings-panel,
.chart-panel,
.restore-panel,
.entry-form {
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 8px;
  box-shadow: var(--shadow);
}

.balance-panel {
  padding: 20px;
}

.balance-panel h2 {
  font-size: clamp(2.25rem, 8vw, 4rem);
  line-height: 1;
  margin: 0 0 18px;
}

.water-status {
  display: inline-flex;
  align-items: center;
  min-height: 30px;
  padding: 4px 10px;
  border-radius: 999px;
  background: #e6f2f0;
  color: var(--teal-dark);
  font-weight: 800;
  margin-bottom: 12px;
}

.water-status.low {
  background: #fff1d8;
  color: var(--amber);
}

.water-status.danger {
  background: #fde7e2;
  color: var(--red);
}

.water-bar {
  height: 12px;
  border-radius: 999px;
  background: #dfe8e5;
  overflow: hidden;
}

.water-bar span {
  display: block;
  height: 100%;
  width: 0%;
  background: var(--teal);
  transition: width 180ms ease;
}

.water-row {
  justify-content: space-between;
  gap: 12px;
  margin-top: 8px;
  color: var(--muted);
  font-size: 0.86rem;
}

.quick-actions {
  gap: 10px;
  margin: 14px 0;
  overflow-x: auto;
}

.primary-button,
.secondary-button,
.ghost-button,
.small-button,
.text-button,
.icon-button {
  border: 0;
  border-radius: 8px;
  min-height: 42px;
  font-weight: 800;
}

.primary-button {
  background: var(--teal);
  color: #fff;
  padding: 0 16px;
}

.primary-button:hover {
  background: var(--teal-dark);
}

.primary-button.locked,
.secondary-button.locked,
.small-button.locked {
  opacity: 0.45;
}

.secondary-button,
.small-button {
  background: #e9efeb;
  color: var(--ink);
  padding: 0 14px;
}

.ghost-button,
.text-button,
.icon-button {
  background: transparent;
  color: var(--teal-dark);
  padding: 0 10px;
}

.small-button {
  min-height: 36px;
  font-size: 0.88rem;
}

.icon-button {
  min-width: 40px;
  color: var(--muted);
  font-size: 1.4rem;
}

.alert-card {
  padding: 14px;
  display: flex;
  justify-content: space-between;
  gap: 12px;
  margin: 14px 0;
  border-color: #f2c46e;
  background: #fff9ed;
}

.alert-title {
  font-weight: 900;
  margin-bottom: 4px;
}

.alert-card p:last-child {
  margin-bottom: 0;
  color: var(--muted);
}

.section,
.chart-panel {
  margin-top: 14px;
  padding: 16px;
}

.section-head {
  justify-content: space-between;
  gap: 12px;
  margin-bottom: 12px;
}

.page-head {
  margin: 4px 0 14px;
}

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

.metric-card {
  padding: 14px;
  box-shadow: none;
}

.metric-card span {
  display: block;
  color: var(--muted);
  font-size: 0.84rem;
  margin-bottom: 6px;
}

.metric-card strong {
  font-size: 1.25rem;
}

.record-list,
.backup-list,
.topup-list,
.bar-chart {
  display: grid;
  gap: 10px;
}

.record-item,
.backup-item,
.topup-item,
.member-card,
.bar-row {
  border: 1px solid var(--line);
  border-radius: 8px;
  background: #fff;
}

.record-item {
  padding: 12px;
}

.record-main {
  justify-content: space-between;
  gap: 12px;
}

.record-title {
  font-weight: 900;
}

.record-meta {
  flex-wrap: wrap;
  gap: 8px;
  margin-top: 8px;
  color: var(--muted);
  font-size: 0.84rem;
}

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

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

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

.record-actions {
  display: flex;
  gap: 8px;
  margin-top: 10px;
}

.filter-row {
  display: flex;
  gap: 8px;
  overflow-x: auto;
  padding-bottom: 8px;
}

.stats-controls {
  display: grid;
  gap: 10px;
  margin-bottom: 14px;
}

.segment-control {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 4px;
  padding: 4px;
  background: #e7ede9;
  border: 1px solid var(--line);
  border-radius: 8px;
}

.segment-control button {
  border: 0;
  border-radius: 7px;
  min-height: 40px;
  background: transparent;
  color: var(--muted);
  font-weight: 900;
}

.segment-control button.active {
  background: var(--panel);
  color: var(--teal-dark);
  box-shadow: 0 4px 12px rgba(31, 37, 38, 0.08);
}

.month-switcher {
  display: grid;
  grid-template-columns: auto 1fr auto;
  align-items: center;
  gap: 10px;
}

.month-switcher strong {
  text-align: center;
  font-size: 1.05rem;
}

.range-note {
  color: var(--muted);
  font-size: 0.82rem;
  font-weight: 800;
  white-space: nowrap;
}

.filter-chip {
  border: 1px solid var(--line);
  background: #fff;
  border-radius: 999px;
  min-height: 36px;
  padding: 0 12px;
  color: var(--muted);
}

.filter-chip.active {
  background: var(--teal);
  color: #fff;
  border-color: var(--teal);
}

.group-label {
  color: var(--muted);
  font-weight: 900;
  margin-top: 8px;
}

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

label,
fieldset {
  display: grid;
  gap: 6px;
  color: var(--muted);
  font-weight: 800;
}

input,
select,
textarea {
  width: 100%;
  max-width: 100%;
  min-width: 0;
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 11px 12px;
  color: var(--ink);
  background: #fff;
}

textarea {
  resize: vertical;
}

fieldset {
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 10px;
}

legend {
  padding: 0 6px;
}

.check-row {
  display: flex;
  grid-template-columns: none;
  align-items: center;
  gap: 8px;
  color: var(--ink);
  font-weight: 700;
}

.check-row input {
  width: auto;
}

.topup-item {
  padding: 12px;
}

.topup-head {
  justify-content: space-between;
  gap: 12px;
  margin-bottom: 10px;
}

.topup-person {
  justify-content: space-between;
  gap: 10px;
  padding: 9px 0;
  border-top: 1px solid var(--line);
}

.topup-person:first-of-type {
  border-top: 0;
}

.status-pill {
  border-radius: 999px;
  min-height: 28px;
  border: 0;
  padding: 0 10px;
  background: #edf2f5;
  color: var(--blue);
  font-size: 0.8rem;
  font-weight: 900;
}

.status-pill.done {
  background: #e3f2e9;
  color: var(--green);
}

.member-card {
  padding: 14px;
}

.member-card span {
  display: block;
  color: var(--muted);
  font-size: 0.82rem;
}

.member-card strong {
  display: block;
  font-size: 1.25rem;
  margin-top: 4px;
}

.bar-row,
.stack-row {
  padding: 10px;
}

.bar-top {
  display: flex;
  justify-content: space-between;
  gap: 12px;
  margin-bottom: 8px;
  font-weight: 800;
}

.bar-track {
  height: 10px;
  border-radius: 999px;
  background: #e6ece8;
  overflow: hidden;
}

.bar-track span {
  display: block;
  height: 100%;
  background: var(--teal);
}

.stack-legend {
  display: flex;
  flex-wrap: wrap;
  gap: 8px 12px;
  margin-bottom: 4px;
  color: var(--muted);
  font-size: 0.82rem;
  font-weight: 800;
}

.stack-legend span {
  display: inline-flex;
  align-items: center;
  gap: 6px;
}

.stack-legend i {
  width: 10px;
  height: 10px;
  border-radius: 999px;
}

.stack-track {
  height: 16px;
  border-radius: 999px;
  background: #e6ece8;
  overflow: hidden;
}

.stack-fill {
  display: flex;
  height: 100%;
  min-width: 4px;
  border-radius: 999px;
  overflow: hidden;
}

.stack-segment {
  display: block;
  height: 100%;
  min-width: 5px;
}

.restore-panel {
  padding: 16px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 16px;
}

.backup-item,
.audit-item {
  justify-content: space-between;
  gap: 12px;
  padding: 12px;
}

.audit-item {
  display: grid;
  gap: 6px;
}

.audit-item span {
  color: var(--muted);
  font-size: 0.88rem;
  font-weight: 800;
}

.entry-dialog {
  border: 0;
  padding: 0;
  background: transparent;
  width: min(94vw, 520px);
  max-width: calc(100vw - 24px);
}

.entry-dialog::backdrop {
  background: rgba(20, 28, 30, 0.38);
}

.entry-form {
  padding: 16px;
  display: grid;
  gap: 12px;
  width: 100%;
  min-width: 0;
  overflow: hidden;
}

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

.entry-form label,
.entry-form fieldset {
  min-width: 0;
}

.entry-form input[type="date"] {
  appearance: none;
  -webkit-appearance: none;
  min-inline-size: 0;
}

.tabbar {
  position: static;
  width: min(680px, calc(100vw - 24px));
  margin: 18px auto 0;
  display: grid;
  grid-template-columns: repeat(5, 1fr);
  gap: 4px;
  padding: 8px;
  background: rgba(255, 255, 255, 0.94);
  border: 1px solid var(--line);
  border-radius: 8px;
  box-shadow: var(--shadow);
  z-index: 5;
}

.view > :last-child {
  margin-bottom: 18px;
}

.tab-button {
  min-height: 46px;
  border: 0;
  border-radius: 8px;
  background: transparent;
  color: var(--muted);
  font-weight: 900;
}

.tab-button.active {
  background: var(--teal);
  color: #fff;
}

.toast {
  position: fixed;
  left: 50%;
  bottom: 84px;
  transform: translateX(-50%);
  min-width: 220px;
  max-width: min(92vw, 480px);
  border-radius: 8px;
  background: #1f2526;
  color: #fff;
  padding: 12px 14px;
  text-align: center;
  opacity: 0;
  pointer-events: none;
  transition: opacity 160ms ease;
  z-index: 10;
}

.toast.show {
  opacity: 1;
}

.hidden {
  display: none !important;
}

@media (min-width: 780px) {
  .app-shell {
    padding-top: 28px;
  }

  main {
    display: block;
  }

  .metric-grid,
  .member-grid {
    grid-template-columns: repeat(4, minmax(0, 1fr));
  }

  .settings-panel {
    grid-template-columns: 1fr 1fr auto;
    align-items: end;
  }

  .quick-actions {
    overflow: visible;
  }

  .primary-button,
  .secondary-button {
    min-width: 132px;
  }
}
