:root {
  color-scheme: light dark;
  --bg: #f3f5f4;
  --surface: #ffffff;
  --text: #17201d;
  --muted: #5d6b66;
  --border: #dde3e0;
  --accent: #0f766e;
  --on-accent: #ffffff;
  --credit: #15803d;
  --debit: #b91c1c;
  --tabbar-height: 56px;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #101514;
    --surface: #1a2220;
    --text: #e8eeec;
    --muted: #9aa8a3;
    --border: #2c3835;
    --accent: #2dd4bf;
    --on-accent: #06201c;
    --credit: #4ade80;
    --debit: #f87171;
  }
}

* { box-sizing: border-box; }

[hidden] { display: none !important; }

html { -webkit-text-size-adjust: 100%; }

body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font: 16px/1.4 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  -webkit-tap-highlight-color: transparent;
}

h1, h2, p, ul, fieldset { margin: 0; padding: 0; }
ul { list-style: none; }
h1 { font-size: 1.25rem; }
h2 { font-size: 1.15rem; }

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

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

label, legend {
  display: flex;
  flex-direction: column;
  gap: 6px;
  font-size: 0.85rem;
  font-weight: 600;
  color: var(--muted);
}

/* 16px minimum keeps iOS from zooming in on focus */
input, select {
  width: 100%;
  min-height: 46px;
  padding: 10px 12px;
  border: 1px solid var(--border);
  border-radius: 10px;
  background: var(--surface);
  color: var(--text);
  font: inherit;
  font-size: 1rem;
}

input:focus-visible, select:focus-visible, .btn:focus-visible, .tab:focus-visible, .fab:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 1px;
}

.hint { font-size: 0.8rem; color: var(--muted); margin-top: -6px; }
/* pre-line: an import can report several problem lines */
.form-error { color: var(--debit); font-size: 0.9rem; font-weight: 600; white-space: pre-line; }

.btn {
  min-height: 46px;
  padding: 10px 18px;
  border: 1px solid var(--border);
  border-radius: 10px;
  background: var(--surface);
  color: var(--text);
  font: inherit;
  font-weight: 600;
  cursor: pointer;
}
.btn.primary { background: var(--accent); border-color: var(--accent); color: var(--on-accent); }
.btn.danger { color: var(--debit); }
.btn.destructive { background: var(--debit); border-color: var(--debit); color: var(--surface); }
.btn.link { min-height: 0; padding: 6px 4px; border: 0; background: none; color: var(--accent); text-decoration: none; }
.btn.block { display: block; width: 100%; margin-top: 14px; }
.btn:disabled { opacity: 0.55; cursor: default; }

.actions { display: flex; gap: 10px; }
.actions .btn { flex: 1; }

fieldset.segmented { border: 0; display: flex; gap: 8px; flex-wrap: wrap; }
.segmented legend { margin-bottom: 6px; }
.segmented label { flex: 1; position: relative; }
.segmented input { position: absolute; opacity: 0; inset: 0; min-height: 0; }
.segmented span {
  display: block;
  padding: 11px 0;
  border: 1px solid var(--border);
  border-radius: 10px;
  background: var(--surface);
  color: var(--text);
  font-size: 1rem;
  text-align: center;
}
.segmented input:checked + span { background: var(--accent); border-color: var(--accent); color: var(--on-accent); }
.segmented input:focus-visible + span { outline: 2px solid var(--accent); outline-offset: 1px; }

.chips { display: flex; flex-wrap: wrap; gap: 8px; margin-top: -6px; }
.chip {
  padding: 7px 12px;
  border: 1px solid var(--border);
  border-radius: 999px;
  background: var(--bg);
  color: var(--text);
  font: inherit;
  font-size: 0.9rem;
  cursor: pointer;
}

.balance-preview {
  display: flex;
  justify-content: space-between;
  align-items: baseline;
  gap: 12px;
  padding: 12px;
  border-radius: 10px;
  background: var(--bg);
  font-size: 0.9rem;
  color: var(--muted);
}
.balance-preview output { font-size: 1.1rem; font-weight: 700; color: var(--text); font-variant-numeric: tabular-nums; }

/* ---------- sign-in ---------- */

.auth {
  min-height: 100dvh;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 16px;
  padding: 24px;
}
.auth img { border-radius: 16px; }
.auth form, #passkey-signin { width: 100%; max-width: 360px; }

.passkey { margin-top: 28px; }

.card {
  padding: 18px;
  border: 1px solid var(--border);
  border-radius: 14px;
  background: var(--surface);
}

/* ---------- app shell ---------- */

.topbar {
  position: sticky;
  top: 0;
  z-index: 1;
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 12px;
  padding: calc(10px + env(safe-area-inset-top)) 16px 10px;
  background: var(--bg);
  border-bottom: 1px solid var(--border);
}
.topbar-user { display: flex; align-items: center; gap: 10px; min-width: 0; font-size: 0.9rem; color: var(--muted); }
.topbar-user span { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.topbar-user .btn { flex: none; }

.content {
  max-width: 640px;
  margin: 0 auto;
  padding: 16px 16px calc(var(--tabbar-height) + 96px + env(safe-area-inset-bottom));
}

.balance-card { margin-bottom: 14px; padding: 12px 18px; }
.balance-label { display: block; font-size: 0.85rem; font-weight: 600; color: var(--muted); }
.balance-figure {
  display: block;
  font-size: 1.35rem;
  font-weight: 700;
  line-height: 1.3;
  font-variant-numeric: tabular-nums;
  overflow-wrap: anywhere;
}
.balance-figure.negative { color: var(--debit); }

.filter { margin-bottom: 8px; }
.tools { display: flex; justify-content: flex-end; align-items: center; gap: 18px; margin-bottom: 8px; }
.tools .hint { margin: 0 auto 0 0; }
.list-hint { margin: 0 0 8px; }

.list li[role=button] { cursor: pointer; }
.list li[role=button]:focus-visible { outline: 2px solid var(--accent); outline-offset: 1px; }

.list { display: flex; flex-direction: column; gap: 8px; }
.list li {
  padding: 12px 14px;
  border: 1px solid var(--border);
  border-radius: 12px;
  background: var(--surface);
}

.row { display: flex; justify-content: space-between; align-items: baseline; gap: 12px; }
.row + .row { margin-top: 2px; }
.row > :first-child { min-width: 0; overflow-wrap: anywhere; }
.row > :last-child { flex: none; text-align: right; font-variant-numeric: tabular-nums; }
.row.sub { font-size: 0.85rem; color: var(--muted); }

.title { font-weight: 600; }
.amount { font-weight: 700; }
.amount.credit { color: var(--credit); }
.amount.debit { color: var(--debit); }

.empty { padding: 40px 16px; text-align: center; color: var(--muted); }

.fab {
  position: fixed;
  right: 18px;
  bottom: calc(var(--tabbar-height) + 18px + env(safe-area-inset-bottom));
  width: 58px;
  height: 58px;
  border: 0;
  border-radius: 50%;
  background: var(--accent);
  color: var(--on-accent);
  font-size: 2rem;
  line-height: 1;
  box-shadow: 0 4px 14px rgb(0 0 0 / 0.28);
  cursor: pointer;
}

.tabbar {
  position: fixed;
  inset: auto 0 0 0;
  display: flex;
  padding-bottom: env(safe-area-inset-bottom);
  background: var(--surface);
  border-top: 1px solid var(--border);
}
.tab {
  flex: 1;
  height: var(--tabbar-height);
  border: 0;
  background: none;
  color: var(--muted);
  font: inherit;
  font-weight: 600;
  cursor: pointer;
}
.tab.active { color: var(--accent); box-shadow: inset 0 2px 0 var(--accent); }

/* ---------- dialogs: bottom sheet on phones, centred card on wider screens ---------- */

dialog {
  width: 100%;
  max-width: 480px;
  max-height: 92dvh;
  margin: auto auto 0;
  padding: 20px 18px calc(20px + env(safe-area-inset-bottom));
  border: 0;
  border-radius: 18px 18px 0 0;
  background: var(--surface);
  color: var(--text);
  overflow-y: auto;
}
dialog::backdrop { background: rgb(0 0 0 / 0.5); }

@media (min-width: 600px) {
  dialog { margin: auto; border-radius: 18px; }
}

/* ---------- confirmation popup: a small centred card on every screen size ---------- */

dialog.popup {
  width: calc(100% - 48px);
  max-width: 360px;
  margin: auto;
  /* The padding lives on the form, so a tap on the dialog element itself is a tap outside. */
  padding: 0;
  border-radius: 18px;
  box-shadow: 0 16px 48px rgb(0 0 0 / 0.35);
}
.popup form { padding: 22px 20px 20px; }
.popup p { color: var(--muted); }

@media (prefers-reduced-motion: no-preference) {
  dialog.popup[open] { animation: popup-in 0.16s ease-out; }
  dialog.popup[open]::backdrop { animation: fade-in 0.16s ease-out; }
}
@keyframes popup-in { from { opacity: 0; transform: scale(0.94); } }
@keyframes fade-in { from { opacity: 0; } }

.toast {
  position: fixed;
  left: 50%;
  bottom: calc(var(--tabbar-height) + 24px + env(safe-area-inset-bottom));
  transform: translateX(-50%);
  max-width: calc(100% - 32px);
  padding: 10px 16px;
  border-radius: 999px;
  background: var(--text);
  color: var(--bg);
  font-size: 0.9rem;
  z-index: 10;
}
