:root {
  /* Light, minimal palette with a single cobalt accent */
  --accent: #3355ff;
  --accent-strong: #2440e0;
  --accent-soft: rgba(51, 85, 255, 0.1);
  --on-accent: #ffffff;

  --bg: #f4f5f7;
  --surface: #ffffff;
  --surface-2: #eef0f3;
  --track: #e8ebf0;
  --text: #0b0c10;
  --muted: #6b7079;
  --faint: #a3a8b1;
  --border: rgba(11, 12, 16, 0.07);
  --danger: #e5484d;
  --danger-soft: rgba(229, 72, 77, 0.1);
  --shadow: 0 1px 2px rgba(11, 12, 16, 0.04), 0 6px 20px rgba(11, 12, 16, 0.04);
  --backdrop: rgba(11, 12, 16, 0.32);

  --radius: 24px;
  --radius-sm: 14px;
  --gutter: 16px;

  --safe-top: env(safe-area-inset-top, 0px);
  --safe-bottom: env(safe-area-inset-bottom, 0px);
  --safe-left: env(safe-area-inset-left, 0px);
  --safe-right: env(safe-area-inset-right, 0px);

  color-scheme: light;
}

*,
*::before,
*::after {
  box-sizing: border-box;
}

html {
  -webkit-text-size-adjust: 100%;
  text-size-adjust: 100%;
  -webkit-tap-highlight-color: transparent;
  background: var(--bg);
}

body {
  margin: 0;
  min-height: 100vh;
  min-height: 100dvh;
  background: var(--bg);
  color: var(--text);
  font-family: -apple-system, BlinkMacSystemFont, "SF Pro Text", "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  font-size: 16px;
  line-height: 1.4;
  overflow-x: hidden;
  overscroll-behavior-y: none;
}

h1, h2, p { margin: 0; }

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

[hidden] { display: none !important; }

svg { width: 20px; height: 20px; fill: none; stroke: currentColor; stroke-width: 2; stroke-linecap: round; stroke-linejoin: round; flex: none; }

.muted { color: var(--muted); }
.small { font-size: 0.875rem; }

/* ---------- Layout ---------- */

.app {
  max-width: 560px;
  margin: 0 auto;
  padding:
    calc(var(--safe-top) + 8px)
    calc(var(--safe-right) + var(--gutter))
    calc(var(--safe-bottom) + 32px)
    calc(var(--safe-left) + var(--gutter));
}

.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  min-height: 56px;
  margin-bottom: 4px;
}

.brand {
  font-size: 1.0625rem;
  font-weight: 700;
  letter-spacing: -0.01em;
}

main {
  display: grid;
  gap: 24px;
}

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

/* ---------- Buttons ---------- */

.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  min-height: 52px;
  padding: 0 18px;
  border: 0;
  border-radius: 16px;
  font-weight: 650;
  font-size: 1rem;
  cursor: pointer;
  user-select: none;
  -webkit-user-select: none;
  transition: transform 0.12s ease, background-color 0.15s ease, opacity 0.15s ease;
}

.btn:active { transform: scale(0.97); }
.btn:focus-visible, .icon-btn:focus-visible, .settings-item:focus-visible, .tx-content:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 2px;
}

.btn-primary { background: var(--accent); color: var(--on-accent); }
.btn-primary:hover { background: var(--accent-strong); }
.btn-secondary { background: var(--surface-2); color: var(--text); }
.btn-secondary:hover { background: var(--track); }
.btn-ghost { background: transparent; color: var(--accent); }
.btn-ghost:hover { background: var(--accent-soft); }
.btn-danger { background: var(--danger); color: #fff; }
.btn-sm { min-height: 36px; padding: 0 12px; font-size: 0.9375rem; border-radius: 12px; }
.btn-block { width: 100%; }

.icon-btn {
  display: grid;
  place-items: center;
  width: 44px;
  height: 44px;
  border: 1px solid var(--border);
  border-radius: 50%;
  background: var(--surface);
  color: var(--text);
  cursor: pointer;
}

/* ---------- Empty goal ---------- */

.empty-goal {
  text-align: center;
  display: grid;
  gap: 12px;
  justify-items: center;
  padding: 36px 20px 20px;
}

.empty-goal h2 { font-size: 1.5rem; letter-spacing: -0.02em; }
.empty-goal p { max-width: 30ch; margin-bottom: 8px; }

.empty-mark {
  width: 72px;
  height: 72px;
  border-radius: 50%;
  background:
    radial-gradient(circle, var(--surface) 0 54%, transparent 55%),
    conic-gradient(var(--accent) 0 72%, var(--track) 72% 100%);
}

/* ---------- Goal card: number-first ---------- */

.goal-head {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 12px;
  margin-bottom: 18px;
}

#goalName {
  min-width: 0;
  font-size: 1.0625rem;
  font-weight: 650;
  overflow-wrap: anywhere;
}

.goal-head .btn { margin-right: -8px; flex: none; }

.figure + .figure { margin-top: 14px; }

.figure-label {
  font-size: 0.8125rem;
  font-weight: 650;
  text-transform: uppercase;
  letter-spacing: 0.08em;
  color: var(--muted);
  margin-bottom: 2px;
}

.figure-value {
  display: flex;
  align-items: baseline;
  gap: 0.2em;
  white-space: nowrap;
  font-variant-numeric: tabular-nums;
  line-height: 1.02;
  letter-spacing: -0.035em;
}

.figure-value .cur {
  font-size: 0.42em;
  font-weight: 650;
  letter-spacing: 0;
  color: var(--muted);
  align-self: flex-start;
  margin-top: 0.3em;
}

.figure-value .frac { color: var(--faint); }

.figure-saved {
  font-size: 3.5rem;
  font-weight: 800;
  color: var(--text);
}

.figure-target {
  font-size: 2.25rem;
  font-weight: 700;
  color: var(--muted);
}

.figure-target .cur { color: var(--faint); }

.progress-block { margin: 22px 0 20px; }

.progress-meta {
  display: flex;
  justify-content: space-between;
  align-items: baseline;
  gap: 12px;
  margin-bottom: 10px;
  font-variant-numeric: tabular-nums;
}

.percent {
  font-size: 1.25rem;
  font-weight: 800;
  letter-spacing: -0.02em;
  color: var(--accent);
}

.remaining {
  font-size: 0.9375rem;
  font-weight: 600;
  color: var(--muted);
  text-align: right;
}

.remaining strong { color: var(--text); font-weight: 700; }
.remaining.done { color: var(--accent); }

.progress {
  position: relative;
  height: 14px;
  border-radius: 999px;
  background: var(--track);
  overflow: hidden;
}

.progress-fill {
  position: absolute;
  inset: 0;
  border-radius: inherit;
  background: var(--accent);
  transform-origin: left center;
  transform: scaleX(0);
  transition: transform 0.9s cubic-bezier(0.22, 1, 0.36, 1);
}

.progress-fill::after {
  content: "";
  position: absolute;
  inset: 0;
  background: linear-gradient(90deg, transparent, rgba(255, 255, 255, 0.45), transparent);
  transform: translateX(-100%);
  animation: shimmer 2.6s ease-in-out 1s infinite;
}

@keyframes shimmer {
  0% { transform: translateX(-100%); }
  60%, 100% { transform: translateX(100%); }
}

.actions {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 10px;
}

/* ---------- History ---------- */

.section-head {
  display: flex;
  justify-content: space-between;
  align-items: baseline;
  padding: 0 4px;
}

.section-head h2 { font-size: 1.25rem; letter-spacing: -0.01em; }

.hint { padding: 2px 4px 10px; }

.tx-list {
  list-style: none;
  margin: 0;
  padding: 0;
  background: var(--surface);
  border-radius: var(--radius);
  border: 1px solid var(--border);
  box-shadow: var(--shadow);
  overflow: hidden;
}

.tx-list:empty { display: none; }

.tx {
  position: relative;
  overflow: hidden;
}

.tx + .tx .tx-content { border-top: 1px solid var(--border); }

.tx-delete {
  position: absolute;
  inset: 0 0 0 auto;
  width: 96px;
  border: 0;
  background: var(--danger);
  color: #fff;
  font-weight: 650;
  cursor: pointer;
}

.tx-content {
  position: relative;
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 14px 16px;
  background: var(--surface);
  touch-action: pan-y;
  transition: transform 0.25s ease;
  cursor: pointer;
  user-select: none;
  -webkit-user-select: none;
}

.tx.dragging .tx-content { transition: none; }
.tx.open .tx-content { transform: translateX(-96px); }

.tx-icon {
  display: grid;
  place-items: center;
  width: 38px;
  height: 38px;
  border-radius: 50%;
  flex: none;
}

.tx-icon svg { width: 18px; height: 18px; stroke-width: 2.5; }
.tx.deposit .tx-icon { background: var(--accent-soft); color: var(--accent); }
.tx.withdrawal .tx-icon { background: var(--danger-soft); color: var(--danger); }

.tx-main { flex: 1; min-width: 0; }

.tx-note {
  font-weight: 600;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.tx-date { font-size: 0.8125rem; color: var(--muted); }

.tx-amount {
  font-size: 1.0625rem;
  font-weight: 750;
  font-variant-numeric: tabular-nums;
  letter-spacing: -0.01em;
  white-space: nowrap;
}

.tx.deposit .tx-amount { color: var(--text); }
.tx.withdrawal .tx-amount { color: var(--danger); }

.tx-empty { text-align: center; }

.tx.removing {
  animation: collapse 0.28s ease forwards;
}

@keyframes collapse {
  to { opacity: 0; transform: translateX(-30%); }
}

/* ---------- Dialogs / sheets ---------- */

dialog {
  padding: 0;
  border: 0;
  color: var(--text);
  background: var(--surface);
}

dialog::backdrop {
  background: var(--backdrop);
  -webkit-backdrop-filter: blur(2px);
  backdrop-filter: blur(2px);
}

.sheet {
  width: 100%;
  max-width: 560px;
  max-height: calc(100dvh - var(--safe-top) - 24px);
  margin: auto auto 0;
  border-radius: 28px 28px 0 0;
  overflow-y: auto;
  overscroll-behavior: contain;
}

.sheet[open] { animation: sheet-up 0.28s cubic-bezier(0.22, 1, 0.36, 1); }

@keyframes sheet-up {
  from { transform: translateY(100%); }
  to { transform: translateY(0); }
}

.sheet > form,
.sheet > .sheet-body {
  display: grid;
  gap: 14px;
  padding:
    10px
    calc(var(--safe-right) + 20px)
    calc(var(--safe-bottom) + 20px)
    calc(var(--safe-left) + 20px);
}

.sheet h2 { font-size: 1.25rem; letter-spacing: -0.01em; margin-bottom: 2px; }

.sheet-grabber {
  width: 40px;
  height: 5px;
  border-radius: 3px;
  background: var(--track);
  margin: 0 auto 6px;
}

@media (min-width: 600px) {
  .sheet {
    margin: auto;
    border-radius: 28px;
  }
  .sheet[open] { animation: pop-in 0.2s ease; }
  .sheet-grabber { display: none; }
}

@keyframes pop-in {
  from { opacity: 0; transform: scale(0.96); }
  to { opacity: 1; transform: scale(1); }
}

.field {
  display: grid;
  gap: 6px;
  min-width: 0;
}

.field > span {
  font-size: 0.875rem;
  font-weight: 600;
  color: var(--muted);
}

.field em { font-style: normal; font-weight: 400; }

.field-row {
  display: flex;
  gap: 10px;
}

.grow { flex: 1; }

input[type="text"],
input[type="date"],
select {
  width: 100%;
  min-width: 0;
  min-height: 50px;
  padding: 0 14px;
  /* 16px+ prevents iOS Safari from zooming on focus */
  font-size: 1rem;
  border-radius: var(--radius-sm);
  border: 1px solid transparent;
  background: var(--surface-2);
  outline: none;
  -webkit-appearance: none;
  appearance: none;
}

input[type="date"] {
  display: flex;
  align-items: center;
}

input[type="date"]::-webkit-date-and-time-value { text-align: left; }

select {
  padding-right: 34px;
  background-image:
    linear-gradient(45deg, transparent 50%, var(--muted) 50%),
    linear-gradient(135deg, var(--muted) 50%, transparent 50%);
  background-position: calc(100% - 18px) 22px, calc(100% - 13px) 22px;
  background-size: 5px 5px;
  background-repeat: no-repeat;
}

input:focus,
select:focus {
  border-color: var(--accent);
  background-color: var(--surface);
  box-shadow: 0 0 0 3px var(--accent-soft);
}

.amount-input {
  display: flex;
  align-items: center;
  background: var(--surface-2);
  border-radius: var(--radius-sm);
  border: 1px solid transparent;
}

.amount-input:focus-within {
  border-color: var(--accent);
  background: var(--surface);
  box-shadow: 0 0 0 3px var(--accent-soft);
}

.amount-currency {
  padding-left: 16px;
  font-weight: 650;
  color: var(--muted);
}

.amount-input input,
.amount-input input:focus {
  border: 0;
  background: transparent;
  box-shadow: none;
  font-size: 2rem;
  font-weight: 750;
  letter-spacing: -0.02em;
  font-variant-numeric: tabular-nums;
  min-height: 68px;
}

.segmented {
  display: grid;
  grid-template-columns: 1fr 1fr;
  padding: 4px;
  border-radius: 14px;
  background: var(--surface-2);
}

.segmented label { position: relative; }

.segmented input {
  position: absolute;
  opacity: 0;
  inset: 0;
  margin: 0;
  cursor: pointer;
}

.segmented span {
  display: grid;
  place-items: center;
  min-height: 40px;
  border-radius: 10px;
  font-weight: 600;
  color: var(--muted);
  transition: background-color 0.15s ease, color 0.15s ease;
}

.segmented input:checked + span {
  background: var(--surface);
  color: var(--text);
  box-shadow: 0 1px 3px rgba(11, 12, 16, 0.1);
}

.segmented input:focus-visible + span { outline: 2px solid var(--accent); }

.form-error {
  min-height: 1.2em;
  margin: -4px 0 -6px;
  font-size: 0.875rem;
  color: var(--danger);
}

.form-error:empty { min-height: 0; margin: 0; }

.sheet-actions {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 10px;
  margin-top: 4px;
}

.sheet-actions.single { grid-template-columns: 1fr; }

.settings-list {
  display: grid;
  border-radius: 16px;
  overflow: hidden;
  background: var(--surface-2);
}

.settings-item {
  display: flex;
  align-items: center;
  width: 100%;
  min-height: 60px;
  padding: 12px 16px;
  border: 0;
  background: transparent;
  text-align: left;
  cursor: pointer;
}

.settings-item + .settings-item { border-top: 1px solid var(--border); }
.settings-item:active { background: var(--track); }
.settings-item span { display: grid; gap: 2px; }
.settings-item small { font-size: 0.8125rem; }
.settings-item.danger strong { color: var(--danger); }
.settings-foot { text-align: center; }

.settings-group {
  margin: 6px 4px -6px;
  font-size: 0.8125rem;
  font-weight: 650;
  text-transform: uppercase;
  letter-spacing: 0.08em;
  color: var(--muted);
}

.settings-item.toggle {
  justify-content: space-between;
  gap: 16px;
}

/* iOS-style switch */
.toggle input {
  position: relative;
  flex: none;
  width: 51px;
  height: 31px;
  margin: 0;
  border-radius: 999px;
  background: var(--track);
  box-shadow: inset 0 0 0 1px var(--border);
  -webkit-appearance: none;
  appearance: none;
  cursor: pointer;
  transition: background-color 0.2s ease;
}

.toggle input::after {
  content: "";
  position: absolute;
  top: 2px;
  left: 2px;
  width: 27px;
  height: 27px;
  border-radius: 50%;
  background: #fff;
  box-shadow: 0 2px 4px rgba(11, 12, 16, 0.2);
  transition: transform 0.2s ease;
}

.toggle input:checked { background: var(--accent); box-shadow: none; }
.toggle input:checked::after { transform: translateX(20px); }
.toggle input:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }

/* Card becomes the add button when the action buttons are hidden */
.goal.tappable { cursor: pointer; }
.goal.tappable:active { transform: scale(0.99); }
.goal { transition: transform 0.12s ease; }
.goal.no-remaining .progress-meta { justify-content: flex-start; }
.goal.tappable .progress-block { margin-bottom: 0; }

.confirm {
  width: calc(100% - 48px);
  max-width: 340px;
  border-radius: 22px;
  margin: auto;
}

.confirm[open] { animation: pop-in 0.18s ease; }

.confirm form {
  display: grid;
  gap: 10px;
  padding: 22px 20px 18px;
  text-align: center;
}

.confirm h2 { font-size: 1.125rem; }

.confirm-actions {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 10px;
  margin-top: 10px;
}

/* ---------- Toast & confetti ---------- */

.toast {
  position: fixed;
  left: 50%;
  bottom: calc(var(--safe-bottom) + 24px);
  max-width: calc(100% - 32px);
  padding: 12px 18px;
  border-radius: 999px;
  background: var(--text);
  color: #fff;
  font-weight: 600;
  font-size: 0.9375rem;
  transform: translate(-50%, 150%);
  opacity: 0;
  pointer-events: none;
  transition: transform 0.3s ease, opacity 0.3s ease;
  z-index: 20;
}

.toast.show {
  transform: translate(-50%, 0);
  opacity: 1;
}

.confetti {
  position: fixed;
  inset: 0;
  width: 100%;
  height: 100%;
  pointer-events: none;
  z-index: 30;
}

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after {
    animation-duration: 0.01ms !important;
    animation-iteration-count: 1 !important;
    transition-duration: 0.01ms !important;
  }
}
