:root {
  color-scheme: light;
  --bg: #f7f6f3;
  --ink: #1c1c19;
  --muted: #5e5c56;
  --teal: #1f6f66;
  --on-teal: #ffffff;
  --teal-container: #d3ebe6;
  --on-teal-container: #00201c;
  --surface: #f7f6f3;
  --surface-variant: #e7e4dc;
  --outline: #c8c4bb;
  --error: #9c3b32;
  --on-error: #ffffff;
  --scrim: rgba(20, 20, 16, 0.38);
  --shadow: 0 10px 28px rgba(28, 28, 25, 0.14);
  --fab-bg: #d3ebe6;
  --fab-ink: #00201c;
  --bar-height: 56px;
}

@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    color-scheme: dark;
    --bg: #121412;
    --ink: #e6e2da;
    --muted: #c4c0b6;
    --teal: #9ed4cb;
    --on-teal: #003731;
    --teal-container: #1f4e48;
    --on-teal-container: #d3ebe6;
    --surface: #121412;
    --surface-variant: #2a2c2a;
    --outline: #3e423f;
    --error: #ffb4ab;
    --on-error: #690005;
    --scrim: rgba(0, 0, 0, 0.56);
    --shadow: 0 12px 32px rgba(0, 0, 0, 0.4);
    --fab-bg: #1f4e48;
    --fab-ink: #d3ebe6;
  }
}

:root[data-theme="dark"] {
  color-scheme: dark;
  --bg: #121412;
  --ink: #e6e2da;
  --muted: #c4c0b6;
  --teal: #9ed4cb;
  --on-teal: #003731;
  --teal-container: #1f4e48;
  --on-teal-container: #d3ebe6;
  --surface: #121412;
  --surface-variant: #2a2c2a;
  --outline: #3e423f;
  --error: #ffb4ab;
  --on-error: #690005;
  --scrim: rgba(0, 0, 0, 0.56);
  --shadow: 0 12px 32px rgba(0, 0, 0, 0.4);
  --fab-bg: #1f4e48;
  --fab-ink: #d3ebe6;
}

:root[data-theme="light"] {
  color-scheme: light;
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  min-height: 100%;
  background: var(--bg);
  color: var(--ink);
}

body {
  font-family: system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  font-size: 16px;
  line-height: 1.4;
  min-height: 100dvh;
}

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

button { cursor: pointer; }

button:focus-visible,
a:focus-visible,
input:focus-visible,
textarea:focus-visible {
  outline: 2px solid var(--teal);
  outline-offset: 2px;
}

.shell {
  min-height: 100dvh;
  display: flex;
  flex-direction: column;
}

.bar {
  position: sticky;
  top: 0;
  z-index: 5;
  display: flex;
  align-items: center;
  gap: 4px;
  min-height: var(--bar-height);
  padding: calc(8px + env(safe-area-inset-top)) calc(8px + env(safe-area-inset-right)) 8px calc(8px + env(safe-area-inset-left));
  background: var(--bg);
}

.bar h1 {
  margin: 0;
  flex: 1;
  font-size: 1.375rem;
  font-weight: 600;
  letter-spacing: -0.01em;
}

.bar h1.quiet {
  font-size: 1rem;
  font-weight: 500;
  color: var(--muted);
  letter-spacing: 0;
}

.icon-button {
  width: 44px;
  height: 44px;
  border: 0;
  border-radius: 12px;
  background: transparent;
  color: var(--ink);
  display: grid;
  place-items: center;
  flex: none;
}

.icon-button svg { width: 24px; height: 24px; }

.view {
  flex: 1;
  padding: 0 calc(16px + env(safe-area-inset-right)) calc(28px + env(safe-area-inset-bottom)) calc(16px + env(safe-area-inset-left));
}

.view-inner { width: min(560px, 100%); margin: 0 auto; }

.home {
  min-height: calc(100dvh - 72px - env(safe-area-inset-top));
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 12px 12px 32px;
}

.periods {
  width: min(420px, 100%);
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 28px;
}

.period {
  width: 100%;
  border: 0;
  background: transparent;
  border-radius: 18px;
  padding: 4px 8px 8px;
  text-align: center;
}

@media (hover: hover) {
  .period:hover { background: var(--surface-variant); }
}

.period-label {
  display: block;
  font-size: 0.8125rem;
  font-weight: 500;
  letter-spacing: 0.08em;
  color: var(--muted);
}

.period-dates {
  display: block;
  margin-top: 2px;
  font-size: 0.875rem;
  color: var(--muted);
}

.amount {
  display: block;
  margin-top: 8px;
  font-weight: 700;
  letter-spacing: -0.03em;
  font-variant-numeric: tabular-nums;
  line-height: 1.05;
}

.amount.primary.lg { font-size: 4rem; }
.amount.primary.md { font-size: 3.25rem; }
.amount.primary.sm { font-size: 2.75rem; }
.amount.primary.xs { font-size: 2.25rem; }
.amount.secondary { font-weight: 600; margin-top: 4px; }
.amount.secondary.lg { font-size: 2.5rem; }
.amount.secondary.md { font-size: 2.25rem; }
.amount.secondary.sm { font-size: 2rem; }
.amount.secondary.xs { font-size: 1.75rem; }

@media (max-width: 380px) {
  .amount.primary.lg { font-size: 3.1rem; }
  .amount.secondary.lg { font-size: 2rem; }
}

.hint {
  margin: 28px 0 0;
  text-align: center;
  color: var(--muted);
  font-size: 0.95rem;
}

.stack { display: flex; flex-direction: column; gap: 12px; }

.total-block { padding: 8px 4px 4px; }

.total-dates { margin: 0; color: var(--muted); font-size: 1rem; }

.total-amount {
  margin: 2px 0 0;
  font-size: 2rem;
  font-weight: 700;
  letter-spacing: -0.03em;
  font-variant-numeric: tabular-nums;
}

.row {
  width: 100%;
  text-align: left;
  border: 1px solid var(--outline);
  background: transparent;
  border-radius: 14px;
  padding: 14px 16px;
  display: flex;
  align-items: center;
  gap: 12px;
}

.row-text { flex: 1; min-width: 0; }

.row-title, .card-title {
  display: block;
  font-size: 1rem;
  font-weight: 500;
}

.row-meta, .card-sub {
  display: block;
  margin-top: 2px;
  color: var(--muted);
  font-size: 0.875rem;
}

.row-meta.overdue, .card-sub.overdue, .overdue {
  color: var(--error);
  font-weight: 650;
}

.row-amount, .card-amount {
  font-weight: 600;
  font-variant-numeric: tabular-nums;
  white-space: nowrap;
}

.card {
  border: 1px solid var(--outline);
  border-radius: 14px;
  padding: 8px 16px 10px;
}

.card-main {
  width: calc(100% + 8px);
  margin: 0 -4px;
  border: 0;
  background: transparent;
  text-align: left;
  border-radius: 10px;
  padding: 8px 4px;
  display: flex;
  align-items: center;
  gap: 12px;
}

.card-actions, .inline-actions {
  display: flex;
  flex-wrap: wrap;
  gap: 4px 8px;
  align-items: center;
}

.empty { padding: 36px 12px 12px; }

.empty h2 { margin: 0; font-size: 1.75rem; font-weight: 600; letter-spacing: -0.02em; }

.empty p { margin: 12px 0 0; color: var(--muted); font-size: 1rem; }

.fab {
  position: fixed;
  z-index: 6;
  right: max(16px, env(safe-area-inset-right));
  bottom: calc(16px + env(safe-area-inset-bottom));
  border: 0;
  border-radius: 16px;
  background: var(--fab-bg);
  color: var(--fab-ink);
  font-weight: 650;
  padding: 14px 20px;
  box-shadow: var(--shadow);
}

.form { display: flex; flex-direction: column; gap: 14px; padding-bottom: 12px; }

.help, .status-copy { margin: 0; color: var(--muted); font-size: 0.95rem; }

.field { display: flex; flex-direction: column; gap: 6px; }

.field > span, .switch-copy strong, .group-label {
  font-size: 0.95rem;
  font-weight: 600;
}

.field input, .field textarea {
  width: 100%;
  border: 1px solid var(--outline);
  background: transparent;
  border-radius: 12px;
  padding: 12px 14px;
  min-height: 48px;
}

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

.field input.invalid, .money-input.invalid { border-color: var(--error); }

.money-input {
  display: flex;
  align-items: center;
  gap: 2px;
  border: 1px solid var(--outline);
  border-radius: 12px;
  padding: 0 14px;
  min-height: 48px;
  background: transparent;
}

.money-input span { color: var(--muted); }

.money-input input {
  border: 0;
  min-height: 46px;
  padding: 12px 0;
  width: 100%;
  background: transparent;
}

.money-input input:focus { outline: none; }

.money-input:focus-within {
  outline: 2px solid var(--teal);
  outline-offset: 2px;
}

.field-error { min-height: 0; color: var(--error); font-size: 0.875rem; }

.field-error:empty { display: none; }

.chips { display: flex; flex-wrap: wrap; gap: 8px; }

.chip {
  border: 1px solid var(--outline);
  background: transparent;
  border-radius: 999px;
  padding: 8px 14px;
  min-height: 40px;
}

.chip[aria-pressed="true"], .chip.selected {
  background: var(--teal-container);
  border-color: transparent;
  color: var(--on-teal-container);
  font-weight: 600;
}

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

.switch-copy { display: flex; flex-direction: column; gap: 2px; }

.switch-copy span, .fine { color: var(--muted); font-size: 0.875rem; font-weight: 400; }

.switch-control { position: relative; width: 52px; height: 32px; flex: none; }

.switch-control input {
  position: absolute;
  inset: 0;
  margin: 0;
  opacity: 0;
}

.track {
  display: block;
  width: 52px;
  height: 32px;
  border-radius: 16px;
  background: var(--outline);
  position: relative;
}

.track::after {
  content: "";
  position: absolute;
  width: 24px;
  height: 24px;
  border-radius: 50%;
  background: #fff;
  top: 4px;
  left: 4px;
}

.switch-control input:checked + .track { background: var(--teal); }

.switch-control input:checked + .track::after { transform: translateX(20px); }

.switch-control input:focus-visible + .track { outline: 2px solid var(--teal); outline-offset: 2px; }

.btn, .btn-secondary, .btn-text, .btn-danger {
  border-radius: 12px;
  min-height: 48px;
  padding: 12px 16px;
  font-weight: 650;
}

.btn {
  border: 0;
  background: var(--teal);
  color: var(--on-teal);
}

.btn-secondary {
  background: transparent;
  border: 1px solid var(--outline);
  color: var(--teal);
}

.btn-text, .btn-danger {
  border: 0;
  background: transparent;
  color: var(--teal);
  min-height: 44px;
}

.btn-danger { color: var(--error); }

.btn-wide { width: 100%; }

.section-title {
  margin: 8px 0 0;
  font-size: 1.35rem;
  font-weight: 650;
}

.settings { display: flex; flex-direction: column; gap: 12px; padding-bottom: 24px; }

.settings p { margin: 0; color: var(--muted); }

.scrim {
  position: fixed;
  inset: 0;
  background: var(--scrim);
  border: 0;
  padding: 0;
  z-index: 20;
}

.drawer {
  position: fixed;
  z-index: 21;
  top: 0;
  right: 0;
  bottom: 0;
  width: min(312px, 88vw);
  background: var(--surface);
  box-shadow: var(--shadow);
  padding: calc(28px + env(safe-area-inset-top)) 12px calc(16px + env(safe-area-inset-bottom));
  display: flex;
  flex-direction: column;
}

.drawer h2 { margin: 0 16px 4px; font-size: 1.35rem; }

.drawer .fine { margin: 0 16px 16px; }

.drawer hr { border: 0; border-top: 1px solid var(--outline); margin: 0 0 8px; }

.nav-item {
  display: flex;
  align-items: center;
  gap: 12px;
  width: 100%;
  border: 0;
  background: transparent;
  border-radius: 12px;
  min-height: 48px;
  padding: 10px 16px;
  text-align: left;
  font-weight: 500;
}

.nav-item svg { width: 22px; height: 22px; flex: none; }

.nav-item[aria-current="page"] {
  background: var(--teal-container);
  color: var(--on-teal-container);
}

.drawer-foot { margin-top: auto; padding: 16px; color: var(--muted); }

.sheet-scrim, .modal-scrim { z-index: 30; }

.sheet {
  position: fixed;
  z-index: 31;
  left: 0;
  right: 0;
  bottom: 0;
  background: var(--bg);
  border-radius: 20px 20px 0 0;
  box-shadow: var(--shadow);
  padding: 10px 24px calc(28px + env(safe-area-inset-bottom));
  max-height: min(88dvh, 720px);
  overflow: auto;
}

.grab {
  width: 36px;
  height: 4px;
  border-radius: 2px;
  background: var(--outline);
  margin: 4px auto 12px;
}

.sheet h2, .dialog h2 { margin: 0; font-size: 1.35rem; }

.sheet .amount-line {
  margin: 8px 0;
  font-size: 1.5rem;
  font-weight: 650;
  font-variant-numeric: tabular-nums;
}

.modal-scrim { z-index: 40; }

.dialog {
  position: fixed;
  z-index: 41;
  left: 50%;
  top: 50%;
  transform: translate(-50%, -50%);
  width: min(440px, calc(100vw - 32px));
  max-height: min(86dvh, 720px);
  overflow: auto;
  background: var(--bg);
  border-radius: 24px;
  padding: 24px;
  box-shadow: var(--shadow);
}

.dialog h3 { margin: 12px 0 0; font-size: 1.25rem; }

.dialog-actions { display: flex; justify-content: flex-end; gap: 8px; margin-top: 8px; }

.toast {
  position: fixed;
  z-index: 50;
  left: 50%;
  bottom: calc(24px + env(safe-area-inset-bottom));
  transform: translateX(-50%);
  width: min(420px, calc(100% - 32px));
  background: var(--ink);
  color: var(--bg);
  border-radius: 12px;
  padding: 12px 16px;
  box-shadow: var(--shadow);
}

.toast[hidden] { display: none; }

.noscript {
  padding: calc(24px + env(safe-area-inset-top)) 20px;
}

[hidden] { display: none !important; }

@media (prefers-reduced-motion: reduce) {
  * { transition: none !important; scroll-behavior: auto !important; }
}
