/* Layout and components. Colours come only from tokens.css. The shell and tab bar are
 * copied from Flights (phone-first: safe-area padding, 44px tap targets). */
/* Phone first. The base rules are the iPhone layout (390pt wide); `min-width: 768px`
 * adds the desktop one. Nothing depends on hover, every tap target is ≥44pt, and every
 * typed-into control is ≥16px so Safari never zooms on focus. */

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

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

body {
  margin: 0;
  min-height: 100dvh;
  background: var(--bg-sunken);
  color: var(--text);
  font: var(--t-body) / 1.45 var(--font);
  -webkit-tap-highlight-color: transparent;
  /* Room for the fixed tab bar plus the home indicator. */
  padding-bottom: calc(var(--tabbar-h) + env(safe-area-inset-bottom) + var(--s4));
}

a {
  color: var(--accent);
}

:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 2px;
}

[x-cloak] {
  display: none !important;
}

.visually-hidden {
  position: absolute;
  width: 1px;
  height: 1px;
  overflow: hidden;
  clip: rect(0 0 0 0);
  white-space: nowrap;
}

/* --- Top bar ------------------------------------------------------------ */
.topbar {
  position: sticky;
  top: 0;
  z-index: 10;
  background: var(--bg);
  border-bottom: 1px solid var(--border);
  padding: env(safe-area-inset-top) max(var(--s4), env(safe-area-inset-right)) 0
    max(var(--s4), env(safe-area-inset-left));
}

.topbar__inner {
  max-width: var(--shell-max);
  margin: 0 auto;
  min-height: 52px;
  display: flex;
  align-items: center;
  gap: var(--s3);
}

.topbar__title {
  flex: 1;
  margin: 0;
  font-size: 17px;
  font-weight: 600;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.topbar__back,
.topbar__action {
  min-height: var(--tap);
  min-width: var(--tap);
  display: inline-flex;
  align-items: center;
  justify-content: center;
  text-decoration: none;
  font-weight: 600;
}

/* --- Content column ----------------------------------------------------- */
.shell {
  max-width: var(--shell-max);
  margin: 0 auto;
  padding: var(--s4) max(var(--s4), env(safe-area-inset-right)) 0
    max(var(--s4), env(safe-area-inset-left));
}

.notice {
  margin: 0 0 var(--s3);
  padding: var(--s3) var(--s4);
  border-radius: var(--r);
  background: var(--bg-raised);
  border: 1px solid var(--border);
  overflow-wrap: anywhere;
}

.notice--success {
  background: var(--ok-bg);
  border-color: transparent;
}

.notice--warning,
.notice--info {
  background: var(--warn-bg);
  border-color: transparent;
}

.notice--error {
  background: var(--bad-bg);
  border-color: transparent;
}

.muted {
  color: var(--text-muted);
}

.small {
  font-size: var(--t-small);
}

/* --- Bottom tab bar (iOS style) ----------------------------------------- */
.tabbar {
  position: fixed;
  inset: auto 0 0;
  z-index: 20;
  display: flex;
  justify-content: space-around;
  background: color-mix(in srgb, var(--bg) 92%, transparent);
  backdrop-filter: saturate(180%) blur(16px);
  -webkit-backdrop-filter: saturate(180%) blur(16px);
  border-top: 1px solid var(--border);
  padding: 0 env(safe-area-inset-right) env(safe-area-inset-bottom) env(safe-area-inset-left);
}

.tabbar a {
  flex: 1;
  max-width: 160px;
  min-height: var(--tabbar-h);
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 2px;
  font-size: 11px;
  font-weight: 500;
  color: var(--text-muted);
  text-decoration: none;
}

.tabbar a[aria-current="page"] {
  color: var(--accent);
}

.tabbar svg {
  width: 24px;
  height: 24px;
}

/* --- Cards ---------------------------------------------------------------- */
.card {
  display: block;
  background: var(--bg-raised);
  border: 1px solid var(--border);
  border-radius: var(--r-lg);
  padding: var(--s3) var(--s4);
  margin-bottom: var(--s2);
  color: inherit;
  text-decoration: none;
}

a.card:active {
  background: var(--bg-sunken);
}

/* Tabs whose screens arrive in later chunks. */
.tabbar a[aria-disabled="true"] {
  opacity: 0.4;
  pointer-events: none;
}

/* --- Forms ----------------------------------------------------------------- */
.form {
  display: grid;
  gap: var(--s3);
  margin: var(--s3) 0;
}

.field {
  display: grid;
  gap: var(--s1);
  font-size: var(--t-small);
  color: var(--text-muted);
}

.field input,
.field select,
.row > select,
.row > input {
  box-sizing: border-box;
  width: 100%;
  min-width: 0;
  font: inherit;
  font-size: var(--t-input);
  color: var(--text);
  background: var(--bg);
  border: 1px solid var(--border-strong);
  border-radius: var(--r);
  min-height: var(--tap);
  padding: 0 var(--s3);
}

.button {
  font: inherit;
  min-height: var(--tap);
  padding: 0 var(--s4);
  border-radius: var(--r);
  border: 1px solid var(--border-strong);
  background: var(--bg-raised);
  color: var(--text);
  display: inline-flex;
  align-items: center;
  justify-content: center;
  text-decoration: none;
}

.button--primary {
  background: var(--accent);
  border-color: var(--accent);
  color: var(--accent-on);
}

.muted {
  color: var(--text-muted);
}

.mono {
  font-family: var(--font-mono);
}

.qr svg {
  width: 200px;
  height: 200px;
  background: var(--qr-bg);
  padding: var(--s2);
  border-radius: var(--r);
}

.secret,
.codes {
  word-break: break-all;
}

.codes {
  columns: 2;
  padding-left: var(--s4);
}

/* --- C7: the lock ------------------------------------------------------------- */
.button--quiet {
  background: transparent;
  border-color: transparent;
  color: var(--text-muted);
}

.auth form + form,
.auth .button + form,
.auth form + .button {
  margin-top: var(--s3);
}

.list {
  list-style: none;
  margin: 0;
  padding: 0;
}

.list__row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: var(--s3);
  min-height: var(--tap);
  border-top: 1px solid var(--border);
}

.list__row:first-child {
  border-top: 0;
}

/* Up the moment the app goes to the background, so the app-switcher snapshot shows
   nothing (plan 14.4, R12). Opaque as well as blurred: blur alone leaks big numbers. */
.privacy-shield {
  position: fixed;
  inset: 0;
  z-index: 1000;
  background: var(--bg);
  backdrop-filter: blur(24px);
  -webkit-backdrop-filter: blur(24px);
  display: grid;
  place-items: center;
}

.privacy-shield[hidden] {
  display: none;
}

.privacy-shield img {
  width: 72px;
  height: 72px;
  border-radius: var(--r-lg);
}

/* --- C10: Connections ---------------------------------------------------------- */
.slots--warn {
  border-color: var(--warn);
}

.connection__head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: var(--s3);
}

.badge {
  font-size: 0.8rem;
  padding: 2px var(--s2);
  border-radius: 999px;
  background: var(--quiet-bg);
  color: var(--quiet);
  white-space: nowrap;
}

.badge--ok {
  background: var(--ok-bg);
  color: var(--ok);
}

.badge--needs_reauth,
.badge--error {
  background: var(--bad-bg);
  color: var(--bad);
}

.coverage {
  display: flex;
  flex-wrap: wrap;
  gap: var(--s2);
  list-style: none;
  margin: var(--s2) 0;
  padding: 0;
  font-size: 0.85rem;
}

.coverage .yes::before {
  content: "✓ ";
  color: var(--ok);
}

.coverage .no::before {
  content: "✕ ";
  color: var(--bad);
}

.actions {
  display: flex;
  flex-wrap: wrap;
  gap: var(--s2);
  margin-top: var(--s3);
}

.result {
  display: grid;
  gap: var(--s2);
  padding: var(--s3) 0;
  border-top: 1px solid var(--border);
}

.choice {
  display: flex;
  gap: var(--s2);
  align-items: center;
  min-height: var(--tap);
}

/* --- C13: Data health ---------------------------------------------------------- */
.health-row + .health-row {
  border-top: 1px solid var(--border);
  padding-top: var(--s3);
}

.ok {
  color: var(--ok);
}

.bad {
  color: var(--bad);
}

.banner {
  display: block;
  text-decoration: none;
}

/* --- C15: Transactions --------------------------------------------------------- */
.day__date {
  font-size: 0.9rem;
  color: var(--text-muted);
  margin: 0 0 var(--s2);
}

.txn {
  display: grid;
  grid-template-columns: 1fr auto auto;
  align-items: center;
  gap: var(--s2);
  min-height: var(--tap);
  border-top: 1px solid var(--border);
  padding: var(--s1) 0;
}

.txn:first-child {
  border-top: 0;
}

.txn__main {
  display: grid;
  color: inherit;
  text-decoration: none;
  min-width: 0;
}

.txn__name {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.txn__meta {
  font-size: 0.85rem;
}

.txn__amount {
  font-variant-numeric: tabular-nums;
}

.txn__amount--in {
  color: var(--ok);
}

.chip {
  font-size: 0.8rem;
  padding: 2px var(--s2);
  border-radius: 999px;
  background: var(--accent-bg);
  color: var(--accent-strong);
}

.row {
  display: flex;
  gap: var(--s2);
}

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

.edit__amount {
  font-size: 1.6rem;
  font-weight: 600;
  margin: 0;
}

.choice-group {
  border: 0;
  padding: 0;
  margin: 0;
  display: flex;
  flex-wrap: wrap;
  gap: var(--s1) var(--s3);
}

.form:has(input[name="mode"][value="share"]:checked) .mode--split,
.form:has(input[name="mode"][value="split"]:checked) .mode--share {
  display: none;
}

.split-row + .split-row {
  border-top: 1px solid var(--border);
  padding-top: var(--s2);
}

/* --- C16: Review ----------------------------------------------------------------- */
.review-link {
  display: flex;
  justify-content: space-between;
  text-decoration: none;
  color: inherit;
}

/* Budgets (C24): cards with a spent bar; Home's total with a pace marker */
.month-switch { display: flex; align-items: center; justify-content: space-between; margin: 0 0 var(--s3); }
.month-switch a { padding: var(--s2) var(--s3); font-size: 1.25rem; text-decoration: none; }
.budget { display: block; color: inherit; text-decoration: none; padding: var(--s2) 0; }
.budget__head { display: flex; justify-content: space-between; gap: var(--s3); }
.bar { position: relative; height: 8px; border-radius: 4px; background: var(--bg-sunken); overflow: visible; margin: var(--s2) 0; }
.bar > span { display: block; height: 100%; width: var(--pct); max-width: 100%; border-radius: 4px; background: var(--accent); }
.budget--ahead .bar > span { background: var(--warn); }
.budget--over .bar > span { background: var(--bad); }
.bar--pace > i { position: absolute; top: -3px; bottom: -3px; left: min(var(--at), 100%); width: 2px; background: var(--text); }
.home-top { display: block; color: inherit; text-decoration: none; }
.home-top__spent { font-size: 1.6rem; }

/* Charts (C25): server-drawn SVG; colours only from tokens */
.chart { margin: 0; }
.chart__title { font-weight: 600; margin-bottom: var(--s2); }
.chart svg { width: 100%; height: auto; display: block; color: var(--text); }
.chart__grid { stroke: var(--chart-grid); stroke-width: 1; }
.chart__axis { stroke: var(--text-muted); stroke-width: 1; }
.chart__tick, .chart__label { fill: var(--text-muted); font-size: 9px; }
.chart__pending { opacity: 0.45; }
.chart__line { fill: none; stroke-width: 2; stroke-linejoin: round; stroke-linecap: round; }
.chart__legend { display: flex; flex-wrap: wrap; gap: var(--s2) var(--s3); list-style: none; padding: 0; margin: var(--s2) 0 0; font-size: 0.85rem; }
.chart__swatch { display: inline-block; width: 0.7em; height: 0.7em; border-radius: 2px; margin-right: 0.35em; vertical-align: middle; }
.chart__data summary { cursor: pointer; color: var(--text-muted); font-size: 0.85rem; margin-top: var(--s2); }
.inline-bar { display: inline-block; width: 5rem; height: 6px; border-radius: 3px; background: var(--bg-sunken); vertical-align: middle; }
.inline-bar > span { display: block; height: 100%; border-radius: 3px; background: var(--accent); }
.inline-bar--over > span { background: var(--bad); }

/* Trends (C26) */
.segmented { display: flex; gap: var(--s2); overflow-x: auto; margin: 0 0 var(--s3); padding-bottom: var(--s1); }
.segmented a { flex: none; padding: var(--s1) var(--s3); border-radius: 999px; border: 1px solid var(--border); color: var(--text); text-decoration: none; font-size: 0.9rem; }
.segmented a[aria-current="page"] { background: var(--accent); border-color: var(--accent); color: var(--accent-on); }
.controls summary { cursor: pointer; font-weight: 600; }
.controls select[multiple] { min-height: 5.5rem; }
.table__child th { padding-left: var(--s4); font-weight: 400; color: var(--text-muted); }
.neg { color: var(--bad); }
.pos { color: var(--ok); }
.table { width: 100%; border-collapse: collapse; font-size: 0.9rem; font-variant-numeric: tabular-nums; }
.table th, .table td { padding: var(--s2) var(--s1); border-bottom: 1px solid var(--border); text-align: right; vertical-align: middle; }
.table th[scope="row"], .table thead th:first-child { text-align: left; }
.table thead th { color: var(--text-muted); font-weight: 500; }

/* Bills calendar (C29): Flights' month grid, rewritten */
.cal { display: grid; gap: 2px; font-size: 0.8rem; }
.cal__head, .cal__week { display: grid; grid-template-columns: repeat(7, 1fr); gap: 2px; }
.cal__head div { text-align: center; color: var(--text-muted); padding: var(--s1) 0; }
.cal__day { min-height: 4.2rem; padding: 2px 3px; border-radius: 4px; background: var(--bg-raised); border: 1px solid var(--border); overflow: hidden; }
.cal__day--other { opacity: 0.45; }
.cal__day--today { border-color: var(--accent); }
.cal__num { display: block; font-weight: 600; }
.cal__chip { display: block; margin-top: 2px; padding: 0 3px; border-radius: 3px; background: var(--accent-bg); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; font-size: 0.7rem; }
.cal__chip--paid, .cal__chip--card_paid { background: var(--ok-bg); text-decoration: line-through; }
.cal__chip--late { background: var(--bad-bg); }
.cal__chip--card { background: var(--warn-bg); }
.cal__more { font-size: 0.7rem; color: var(--text-muted); }

/* "Searching…" while a bank search is in flight (HTMX adds .htmx-request) */
.htmx-indicator { display: none; }
.htmx-request .htmx-indicator, .htmx-request.htmx-indicator { display: block; }
