:root {
  --bg: #f4f7f6;
  --surface: #ffffff;
  --surface-soft: #eef6f3;
  --text: #17221f;
  --muted: #66736f;
  --line: #dce6e2;
  --primary: #147d64;
  --primary-dark: #0e5f4c;
  --primary-light: #dff3ec;
  --danger: #b73333;
  --danger-soft: #fff1f1;
  --shadow: 0 18px 48px rgba(25, 62, 52, 0.12);
  --radius-lg: 24px;
  --radius-md: 18px;
  --radius-sm: 12px;
  color-scheme: light;
  font-family: Inter, ui-sans-serif, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
}

* { box-sizing: border-box; }

html { min-height: 100%; background: var(--bg); }
body {
  margin: 0;
  min-height: 100%;
  background:
    radial-gradient(circle at top right, rgba(20, 125, 100, 0.08), transparent 34%),
    var(--bg);
  color: var(--text);
  -webkit-tap-highlight-color: transparent;
  overscroll-behavior-y: none;
}

button, input { font: inherit; }
button { -webkit-touch-callout: none; }
button:focus-visible, input:focus-visible {
  outline: 3px solid rgba(20, 125, 100, 0.25);
  outline-offset: 2px;
}

.app-shell {
  width: min(100%, 760px);
  margin: 0 auto;
  min-height: 100vh;
  padding: calc(18px + env(safe-area-inset-top)) 18px calc(34px + env(safe-area-inset-bottom));
}

.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 16px;
  margin-bottom: 22px;
}

.eyebrow {
  margin: 0 0 5px;
  color: var(--primary);
  font-size: 0.72rem;
  font-weight: 800;
  letter-spacing: 0.12em;
}

h1, h2, h3, p { margin-top: 0; }
h1 { margin-bottom: 0; font-size: clamp(1.75rem, 7vw, 2.35rem); letter-spacing: -0.04em; }
h2 { margin-bottom: 0; font-size: 1.35rem; letter-spacing: -0.025em; }
h3 { margin-bottom: 8px; font-size: 1.05rem; }

.icon-button, .close-button, .calendar-nav {
  border: 0;
  background: var(--surface);
  color: var(--text);
  box-shadow: 0 8px 24px rgba(25, 62, 52, 0.09);
  cursor: pointer;
}

.icon-button {
  width: 48px;
  height: 48px;
  border-radius: 16px;
  display: grid;
  place-items: center;
  flex: 0 0 auto;
}
.icon-button svg { width: 23px; height: 23px; fill: currentColor; }

.summary-grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 12px;
  margin-bottom: 14px;
}

.summary-card {
  background: var(--surface);
  border: 1px solid rgba(220, 230, 226, 0.9);
  border-radius: var(--radius-md);
  padding: 17px;
  min-width: 0;
}
.summary-card span { display: block; color: var(--muted); font-size: 0.8rem; margin-bottom: 7px; }
.summary-card strong { display: block; font-size: clamp(1.1rem, 5vw, 1.5rem); letter-spacing: -0.035em; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.summary-card.accent { background: linear-gradient(145deg, var(--primary), var(--primary-dark)); color: white; border-color: transparent; }
.summary-card.accent span { color: rgba(255,255,255,0.72); }

.action-panel {
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: var(--radius-lg);
  padding: 14px;
  box-shadow: var(--shadow);
  display: grid;
  gap: 10px;
  margin-bottom: 28px;
}

.primary-button, .secondary-button, .danger-button, .text-button, .back-button {
  border: 0;
  border-radius: 15px;
  min-height: 50px;
  padding: 0 18px;
  font-weight: 800;
  cursor: pointer;
  transition: transform .16s ease, opacity .16s ease, background-color .16s ease;
}
.primary-button:active, .secondary-button:active, .danger-button:active, .icon-button:active { transform: scale(0.98); }
.primary-button {
  background: var(--primary);
  color: white;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 9px;
  box-shadow: 0 10px 24px rgba(20, 125, 100, 0.23);
}
.primary-button .plus { font-size: 1.6rem; line-height: 1; font-weight: 400; margin-top: -2px; }
.primary-button.small { min-height: 46px; width: 100%; }
.secondary-button {
  background: var(--surface-soft);
  color: var(--primary-dark);
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
}
.secondary-button svg { width: 21px; height: 21px; fill: currentColor; }
.secondary-button.full { width: 100%; }
.danger-button { background: var(--danger); color: white; }
.text-button, .back-button { background: transparent; color: var(--primary); min-height: 42px; padding: 0 4px; }
.back-button { justify-self: start; }

.records-section { padding-bottom: 20px; }
.section-heading {
  display: flex;
  justify-content: space-between;
  align-items: end;
  gap: 12px;
  margin-bottom: 14px;
}
.record-count {
  color: var(--muted);
  background: rgba(255,255,255,0.7);
  border: 1px solid var(--line);
  border-radius: 999px;
  padding: 7px 10px;
  font-size: .78rem;
  white-space: nowrap;
}

.empty-state {
  background: rgba(255,255,255,0.72);
  border: 1px dashed #c8d6d1;
  border-radius: var(--radius-lg);
  padding: 34px 22px;
  text-align: center;
  color: var(--muted);
}
.empty-state h3 { color: var(--text); margin-bottom: 7px; }
.empty-state p { margin: 0 auto; max-width: 320px; line-height: 1.5; }
.empty-icon {
  width: 58px;
  height: 58px;
  border-radius: 20px;
  background: var(--primary-light);
  color: var(--primary-dark);
  display: grid;
  place-items: center;
  margin: 0 auto 16px;
  font-weight: 900;
  font-size: 1.15rem;
}

.month-list { display: grid; gap: 12px; }
.month-card {
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: var(--radius-md);
  overflow: hidden;
  box-shadow: 0 8px 28px rgba(25, 62, 52, 0.05);
}
.month-toggle {
  width: 100%;
  border: 0;
  background: transparent;
  padding: 17px;
  display: grid;
  grid-template-columns: 1fr auto auto;
  align-items: center;
  gap: 11px;
  text-align: left;
  cursor: pointer;
  color: var(--text);
}
.month-title { font-weight: 850; font-size: 1rem; }
.month-meta { display: block; color: var(--muted); font-size: .78rem; margin-top: 4px; font-weight: 500; }
.month-total { text-align: right; }
.month-total strong { display: block; color: var(--primary-dark); }
.month-total span { color: var(--muted); font-size: .74rem; }
.chevron { font-size: 1.45rem; color: var(--muted); transition: transform .2s ease; }
.month-card.open .chevron { transform: rotate(180deg); }
.month-content { border-top: 1px solid var(--line); }
.month-content[hidden] { display: none; }
.record-row {
  display: grid;
  grid-template-columns: minmax(0, 1fr) auto auto;
  align-items: center;
  gap: 10px;
  padding: 14px 16px;
  border-bottom: 1px solid #edf2f0;
}
.record-row:last-child { border-bottom: 0; }
.record-date { font-weight: 800; }
.record-rate { display: block; color: var(--muted); font-size: .74rem; margin-top: 4px; }
.record-values { text-align: right; }
.record-values strong { display: block; }
.record-values span { color: var(--muted); font-size: .78rem; }
.delete-record {
  width: 38px;
  height: 38px;
  border: 0;
  border-radius: 12px;
  background: var(--danger-soft);
  color: var(--danger);
  display: grid;
  place-items: center;
  cursor: pointer;
}
.delete-record svg { width: 18px; height: 18px; fill: currentColor; }
.month-subtotal {
  background: var(--surface-soft);
  padding: 13px 16px;
  display: flex;
  justify-content: space-between;
  gap: 12px;
  font-size: .85rem;
}
.month-subtotal strong { color: var(--primary-dark); }

.modal[hidden] { display: none; }
.modal { position: fixed; inset: 0; z-index: 100; display: grid; align-items: end; }
.modal-backdrop { position: absolute; inset: 0; background: rgba(11, 26, 21, 0.48); backdrop-filter: blur(5px); }
.modal-sheet {
  position: relative;
  width: min(100%, 680px);
  margin: 0 auto;
  background: var(--surface);
  border-radius: 28px 28px 0 0;
  padding: 10px 18px calc(22px + env(safe-area-inset-bottom));
  box-shadow: 0 -24px 60px rgba(8, 28, 21, 0.2);
  max-height: min(92dvh, 820px);
  overflow-y: auto;
  animation: sheetIn .22s ease-out;
}
@keyframes sheetIn { from { transform: translateY(18px); opacity: .65; } to { transform: translateY(0); opacity: 1; } }
.sheet-handle { width: 46px; height: 5px; border-radius: 99px; background: #d9e1de; margin: 2px auto 14px; }
.modal-header { display: flex; align-items: center; justify-content: space-between; gap: 12px; margin-bottom: 18px; }
.close-button { width: 42px; height: 42px; border-radius: 14px; font-size: 1.7rem; line-height: 1; }
.step-panel { display: grid; gap: 14px; }

.calendar-toolbar { display: grid; grid-template-columns: 42px 1fr 42px; align-items: center; gap: 8px; text-align: center; }
.calendar-nav { width: 42px; height: 42px; border-radius: 14px; font-size: 1.75rem; box-shadow: none; background: var(--surface-soft); }
.weekday-row, .calendar-grid { display: grid; grid-template-columns: repeat(7, 1fr); gap: 5px; }
.weekday-row { color: var(--muted); font-size: .7rem; text-align: center; font-weight: 800; }
.calendar-day {
  aspect-ratio: 1;
  min-width: 0;
  border: 0;
  border-radius: 13px;
  background: transparent;
  color: var(--text);
  font-weight: 750;
  cursor: pointer;
}
.calendar-day:hover { background: var(--surface-soft); }
.calendar-day.today { box-shadow: inset 0 0 0 2px var(--primary); color: var(--primary-dark); }
.calendar-day.selected { background: var(--primary); color: white; box-shadow: none; }
.calendar-day.empty { pointer-events: none; }

.selected-date-card, .amount-preview {
  border-radius: var(--radius-md);
  background: var(--surface-soft);
  padding: 15px 16px;
}
.selected-date-card span, .amount-preview span { color: var(--muted); font-size: .78rem; display: block; margin-bottom: 4px; }
.selected-date-card strong { font-size: 1.1rem; }
.field-label { font-weight: 800; font-size: .88rem; margin-top: 2px; }
.input-with-suffix {
  min-height: 62px;
  display: grid;
  grid-template-columns: minmax(0, 1fr) auto;
  align-items: center;
  background: #f8faf9;
  border: 2px solid var(--line);
  border-radius: 17px;
  overflow: hidden;
}
.input-with-suffix:focus-within { border-color: var(--primary); box-shadow: 0 0 0 4px rgba(20,125,100,.1); }
.input-with-suffix input { width: 100%; border: 0; outline: 0; background: transparent; padding: 0 16px; font-size: 1.4rem; font-weight: 850; color: var(--text); min-width: 0; }
.input-with-suffix > span { color: var(--muted); font-weight: 800; padding: 0 16px 0 8px; }
.input-with-suffix.compact { min-height: 54px; }
.input-with-suffix.compact input { font-size: 1.1rem; }
.amount-preview { display: grid; grid-template-columns: 1fr auto; align-items: center; }
.amount-preview span { margin: 0; }
.amount-preview strong { font-size: 1.22rem; color: var(--primary-dark); }
.amount-preview small { grid-column: 1 / -1; color: var(--muted); margin-top: 5px; }
.field-error { min-height: 1.2em; margin: -4px 0 0; color: var(--danger); font-size: .82rem; }

.settings-sheet { padding-bottom: calc(30px + env(safe-area-inset-bottom)); }
.settings-content { display: grid; gap: 13px; }
.settings-card { border: 1px solid var(--line); border-radius: var(--radius-md); padding: 17px; }
.settings-card > p { color: var(--muted); line-height: 1.48; font-size: .85rem; }
.settings-actions { display: grid; gap: 9px; }
.status-text { margin: 11px 0 0 !important; font-size: .78rem !important; }
.danger-card { background: var(--danger-soft); border-color: #f0d0d0; }
.danger-card .danger-button { width: 100%; }

.confirm-modal { align-items: center; padding: 18px; }
.confirm-dialog {
  position: relative;
  width: min(100%, 390px);
  background: var(--surface);
  border-radius: 22px;
  padding: 22px;
  box-shadow: var(--shadow);
  animation: sheetIn .18s ease-out;
}
.confirm-dialog p { color: var(--muted); line-height: 1.5; }
.confirm-actions { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; }

.toast {
  position: fixed;
  left: 50%;
  bottom: calc(20px + env(safe-area-inset-bottom));
  transform: translate(-50%, 20px);
  background: #18231f;
  color: white;
  border-radius: 14px;
  padding: 12px 16px;
  max-width: calc(100% - 36px);
  text-align: center;
  font-size: .87rem;
  opacity: 0;
  pointer-events: none;
  z-index: 300;
  transition: opacity .18s ease, transform .18s ease;
  box-shadow: 0 14px 34px rgba(0,0,0,.22);
}
.toast.show { opacity: 1; transform: translate(-50%, 0); }

body.modal-open { overflow: hidden; }

@media (min-width: 620px) {
  .app-shell { padding-left: 26px; padding-right: 26px; }
  .action-panel { grid-template-columns: 1.45fr 1fr; }
  .modal { align-items: center; padding: 24px; }
  .modal-sheet { border-radius: 28px; padding-bottom: 26px; max-height: min(90dvh, 820px); }
  .sheet-handle { display: none; }
}

@media (max-width: 390px) {
  .app-shell { padding-left: 13px; padding-right: 13px; }
  .summary-card { padding: 14px; }
  .record-row { grid-template-columns: minmax(0,1fr) auto; }
  .delete-record { grid-column: 2; }
  .record-values { grid-row: 1 / span 2; grid-column: 2; padding-right: 42px; }
  .delete-record { grid-column: 2; grid-row: 2; justify-self: end; margin-top: 8px; }
}

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