:root {
  color-scheme: light dark;
  --bg: #f2f2f7;
  --card: #ffffff;
  --fill: #f2f2f7;
  --label: #111827;
  --secondary: #5f6470;
  --separator: #e5e7eb;
  --blue: #2563eb;
  --blue-press: #1d4ed8;
  --blue-fill: #2563eb;
  --red-fill: #dc2626;
  --green-fill: #16834f;
  --green: #16834f;
  --cell: #ebedf0;
  --cell-on: #1a8a5a;
  --red: #dc2626;
  --radius: 14px;
  --font: -apple-system, BlinkMacSystemFont, "SF Pro Text", "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  --c: 17px; /* heatmap cell */
  --g: 3px;  /* heatmap gap */
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #000000;
    --card: #1c1c1e;
    --fill: #2c2c2e;
    --label: #f2f2f7;
    --secondary: #98989f;
    --separator: #38383a;
    --blue: #3b82f6;
    --blue-press: #1d4ed8;
    --blue-fill: #2563eb;
    --red-fill: #d92d20;
    --green: #34c77b;
    --cell: #2c2c2e;
    --cell-on: #2ea86a;
    --red: #ff5a52;
  }
}

* { box-sizing: border-box; }
html { -webkit-text-size-adjust: 100%; }
body {
  margin: 0;
  background: var(--bg);
  color: var(--label);
  font: 17px/1.35 var(--font);
  -webkit-font-smoothing: antialiased;
  -webkit-tap-highlight-color: transparent;
  padding: env(safe-area-inset-top) env(safe-area-inset-right) calc(env(safe-area-inset-bottom) + 32px) env(safe-area-inset-left);
}
::selection { background: color-mix(in srgb, var(--blue) 28%, transparent); }
input { caret-color: var(--blue); }
:focus-visible { outline: 3px solid color-mix(in srgb, var(--blue) 55%, transparent); outline-offset: 2px; }
a { color: var(--blue); text-decoration: none; }
h1, h2, h3, p { margin: 0; }
main { max-width: 560px; margin: 0 auto; padding: 0 16px; }
main.narrow { padding-top: 56px; }

/* ---------- navigation bar ---------- */

.nav {
  max-width: 560px;
  margin: 0 auto;
  min-height: 52px;
  padding: 8px 16px 0;
  display: flex;
  align-items: center;
}
.nav-home { justify-content: space-between; padding-top: 14px; padding-bottom: 14px; }
.nav-home .large-title { margin: 0; }
.nav-back { display: inline-flex; align-items: center; gap: 2px; margin-left: -8px; padding: 8px; font-size: 17px; }

.large-title { font-size: 34px; line-height: 1.15; font-weight: 700; letter-spacing: -0.02em; margin: 4px 0 14px; }
.lede { color: var(--secondary); margin: -6px 0 18px; }
.lede strong { color: var(--label); font-weight: 600; }

/* ---------- settings button ---------- */

.icon-btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 44px;
  height: 44px;
  margin-right: -10px;
  border-radius: 50%;
  color: var(--blue);
  transition: transform 0.12s ease-out, opacity 0.2s;
}
.icon-btn:active { transform: scale(0.92); opacity: 0.6; }

/* ---------- cards ---------- */

.card {
  background: var(--card);
  border-radius: var(--radius);
  padding: 16px;
  margin-bottom: 16px;
}
.card-title { font-size: 15px; font-weight: 600; color: var(--secondary); margin-bottom: 4px; }
.card-list { padding-bottom: 4px; }
.card-list .card-title { margin-bottom: 0; }
.count { font-size: 20px; font-weight: 600; line-height: 1.25; letter-spacing: -0.01em; text-wrap: balance; }
.count strong { color: var(--green); font-variant-numeric: tabular-nums; }
.hint { color: var(--secondary); font-size: 13px; margin-top: 10px; }
.hint.center { text-align: center; }
.muted { color: var(--secondary); font-weight: 400; }
.pos { color: var(--green); }
.neg { color: var(--red); }

/* ---------- heatmap ---------- */

.heatmap { display: flex; gap: 6px; margin-top: 14px; }
.hm-days {
  display: grid;
  grid-template-rows: repeat(7, var(--c));
  row-gap: var(--g);
  padding-top: 20px;
  font-size: 11px;
  line-height: var(--c);
  color: var(--secondary);
  flex: none;
}
.hm-scroll {
  overflow-x: auto;
  scroll-snap-type: x mandatory;
  scrollbar-width: none;
  overscroll-behavior-x: contain;
  margin-right: -16px;
}
.hm-inner { width: max-content; padding: 0 16px 6px 0; }
.hm-scroll::-webkit-scrollbar { display: none; }
.hm-months {
  display: grid;
  grid-template-columns: repeat(var(--weeks), var(--c));
  column-gap: var(--g);
  height: 20px;
  font-size: 11px;
  color: var(--secondary);
  white-space: nowrap;
}
.hm-grid {
  display: grid;
  grid-template-rows: repeat(7, var(--c));
  grid-auto-columns: var(--c);
  grid-auto-flow: column;
  gap: var(--g);
}
.hm-cell {
  width: var(--c);
  height: var(--c);
  border: 0;
  padding: 0;
  border-radius: 4px;
  background: var(--cell);
  cursor: pointer;
}
.hm-cell { scroll-snap-align: start; }
.hm-cell.on { background: var(--cell-on); }
.hm-cell.today { box-shadow: inset 0 0 0 2px var(--label); }
.hm-cell.on.today { box-shadow: inset 0 0 0 2px var(--label), inset 0 0 0 3.5px var(--card); }
.hm-future { background: transparent; cursor: default; }
.hm-cell { position: relative; transition: transform 0.18s cubic-bezier(0.16, 1, 0.3, 1); }
.hm-cell:active { transform: scale(0.85); }
/* The tapped day stays ringed in blue and enlarged while its sheet is open. */
.hm-grid .hm-cell.selected {
  z-index: 1;
  transform: scale(1.35);
  background: var(--blue-fill);
  box-shadow: 0 0 0 2px var(--card), 0 0 0 4px var(--blue-fill), 0 2px 8px rgb(0 0 0 / 0.35);
}
.hm-cell.just-logged { animation: logged 0.6s cubic-bezier(0.16, 1, 0.3, 1); }
.hm-cell.just-removed { animation: removed 0.6s cubic-bezier(0.16, 1, 0.3, 1); }
@keyframes logged {
  0% { transform: scale(0.4); }
  60% { transform: scale(1.3); }
  100% { transform: scale(1); }
}
@keyframes removed {
  0% { transform: scale(1.3); background: var(--cell-on); }
  100% { transform: scale(1); }
}

/* ---------- fund ---------- */

.balance {
  font-size: 44px;
  line-height: 1.1;
  font-weight: 700;
  letter-spacing: -0.025em;
  font-variant-numeric: tabular-nums;
}
.balance.neg { color: var(--red); }
.progress { height: 8px; border-radius: 4px; background: var(--cell); overflow: hidden; margin-top: 14px; }
.progress > div { height: 100%; border-radius: 4px; background: var(--blue); }
.progress.done > div { background: var(--cell-on); }
.goal-line { font-size: 15px; color: var(--secondary); margin-top: 8px; font-variant-numeric: tabular-nums; }
.goal-line strong { font-weight: 600; }

.fund-actions { display: grid; gap: 10px; margin-top: 18px; }

/* ---------- forms ---------- */

.form { display: grid; gap: 14px; }
.field { display: grid; gap: 6px; min-width: 0; }
.field-label { font-size: 13px; font-weight: 600; color: var(--secondary); }
.field-help { font-size: 13px; color: var(--secondary); }
.field-row { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; margin-bottom: 12px; }
.field input,
.invite-url {
  width: 100%;
  min-height: 44px;
  padding: 0 12px;
  border: 0;
  border-radius: 10px;
  background: var(--fill);
  color: var(--label);
  font: 17px var(--font);
  -webkit-appearance: none;
  appearance: none;
}
.field input[type="date"] { display: flex; align-items: center; }
.field input::placeholder { color: var(--secondary); opacity: 1; }
.money-input { position: relative; display: block; }
.money-input > span { position: absolute; left: 12px; top: 50%; transform: translateY(-50%); color: var(--secondary); pointer-events: none; }
.money-input input { padding-left: 26px; font-variant-numeric: tabular-nums; }
.money-input.invalid input { box-shadow: inset 0 0 0 2px var(--red); }
.form-error { color: var(--red); font-size: 15px; margin-bottom: 10px; }
.form .form-error, .form .form-notice, .form .field-row { margin: 0; }
.form .hint { margin-top: -4px; }
.form-notice { color: var(--green); font-size: 15px; margin-bottom: 10px; }

/* ---------- buttons ---------- */

.btn {
  display: flex;
  align-items: center;
  justify-content: center;
  width: 100%;
  min-height: 50px;
  padding: 0 16px;
  border: 0;
  border-radius: 12px;
  font: 600 17px var(--font);
  cursor: pointer;
  text-align: center;
  transition: transform 0.12s ease-out, background-color 0.2s, opacity 0.2s;
}
.btn:active { transform: scale(0.98); }
.btn:disabled { opacity: 0.5; cursor: default; }
.btn-primary { background: var(--blue-fill); color: #fff; }
.btn-primary:active { background: var(--blue-press); }
.btn-secondary { background: color-mix(in srgb, var(--blue) 12%, transparent); color: var(--blue); }
.btn-plain { background: transparent; color: var(--blue); }
.btn-danger { background: var(--red-fill); color: #fff; }
.btn-treat { background: var(--green-fill); color: #fff; }
.btn-locked, .btn-locked:disabled { background: var(--fill); color: var(--secondary); opacity: 1; }
.btn-danger-plain { background: transparent; color: var(--red); }
.btn-small { width: auto; min-height: 36px; font-size: 15px; border-radius: 10px; padding: 0 14px; }
.narrow > .btn { margin-top: 8px; }
.card > form + form, .card > .invites + form { margin-top: 0; }

/* ---------- lists ---------- */

.entries { list-style: none; margin: 0; padding: 0; }
.entries li + li .entry { border-top: 1px solid var(--separator); }
.entry {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  width: 100%;
  min-height: 56px;
  padding: 10px 0;
  border: 0;
  background: none;
  color: inherit;
  font: inherit;
  text-align: left;
}
button.entry { cursor: pointer; }
button.entry:active { opacity: 0.55; }
.entry-main { display: grid; gap: 2px; min-width: 0; }
.entry-title { font-size: 17px; overflow-wrap: anywhere; }
.entry-sub { font-size: 13px; color: var(--secondary); }
.entry-amt { font-weight: 600; font-variant-numeric: tabular-nums; white-space: nowrap; }
.empty { color: var(--secondary); font-size: 15px; padding: 12px 0; }

.row-link {
  display: flex;
  align-items: center;
  gap: 8px;
  min-height: 50px;
  color: var(--label);
  border-top: 1px solid var(--separator);
}
.row-link svg { color: var(--secondary); opacity: 0.7; margin-left: auto; flex: none; }
.row-link .muted { margin-left: auto; }
.row-link .muted + svg { margin-left: 0; }
.row-link-accent { color: var(--blue); }
.menu { padding-top: 0; padding-bottom: 0; }
.menu .row-link:first-child { border-top: 0; }

.invites { list-style: none; margin: 12px 0; padding: 0; display: grid; gap: 14px; }
.invite-url { font-size: 15px; color: var(--secondary); text-overflow: ellipsis; }
.invite-actions { display: flex; gap: 4px; margin-top: 6px; }
#invites > form { margin-top: 12px; }
#members .entry form { flex: none; margin-right: -14px; }

/* ---------- bottom sheets ---------- */

.sheet {
  width: 100%;
  max-width: 560px;
  margin: auto auto 0;
  padding: 0;
  border: 0;
  border-radius: 16px 16px 0 0;
  background: var(--card);
  color: var(--label);
}
.sheet::backdrop { background: rgb(0 0 0 / 0.25); }
.sheet[open] { animation: sheet-up 0.32s cubic-bezier(0.16, 1, 0.3, 1); }
.sheet[open]::backdrop { animation: fade 0.25s ease-out; }
@keyframes sheet-up { from { transform: translateY(100%); } to { transform: none; } }
@keyframes fade { from { opacity: 0; } to { opacity: 1; } }
.sheet form {
  display: grid;
  gap: 10px;
  padding: 22px 16px calc(env(safe-area-inset-bottom) + 12px);
}
.sheet h3 { font-size: 20px; font-weight: 600; text-align: center; }
.sheet-text { color: var(--secondary); font-size: 15px; text-align: center; margin-bottom: 8px; }
.sheet .field { text-align: left; }
.sheet .btn-plain { min-height: 44px; }

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { animation: none !important; transition: none !important; }
}

.hm-tip {
  position: fixed;
  z-index: 10;
  padding: 5px 9px;
  border-radius: 10px;
  background: var(--label);
  color: var(--card);
  font-size: 13px;
  font-weight: 500;
  white-space: nowrap;
  pointer-events: none;
  opacity: 0;
  transition: opacity 0.12s ease-out;
}
.hm-tip.show { opacity: 1; }
