/* Household Expenses — mobile first, theme aware.
   Colours come from the validated reference palette; the dark column is its own
   selected set of steps for the dark surface, not an automatic inversion. */

:root {
  color-scheme: light;
  --surface-0: #f4f3f0;
  --surface-1: #fcfcfb;
  --surface-2: #ffffff;
  --border: #e2e1dc;
  --border-strong: #cdccc5;
  --text-primary: #0b0b0b;
  --text-secondary: #52514e;
  --text-muted: #78776f;

  --series-1: #2a78d6;
  --series-2: #eb6834;
  --series-3: #1baf7a;
  --series-4: #eda100;

  --good: #0a7a35;
  --bad: #c02525;

  --radius: 14px;
  --radius-sm: 9px;
  --shadow: 0 1px 2px rgba(11, 11, 11, .06), 0 6px 20px rgba(11, 11, 11, .06);
}

@media (prefers-color-scheme: dark) {
  :root:where(:not([data-theme="light"])) {
    color-scheme: dark;
    --surface-0: #121211;
    --surface-1: #1a1a19;
    --surface-2: #232322;
    --border: #333331;
    --border-strong: #464643;
    --text-primary: #ffffff;
    --text-secondary: #c3c2b7;
    --text-muted: #96958c;

    --series-1: #3987e5;
    --series-2: #d95926;
    --series-3: #199e70;
    --series-4: #c98500;

    --good: #4bb96f;
    --bad: #e66767;
    --shadow: 0 1px 2px rgba(0, 0, 0, .5), 0 6px 20px rgba(0, 0, 0, .4);
  }
}

:root[data-theme="dark"] {
  color-scheme: dark;
  --surface-0: #121211;
  --surface-1: #1a1a19;
  --surface-2: #232322;
  --border: #333331;
  --border-strong: #464643;
  --text-primary: #ffffff;
  --text-secondary: #c3c2b7;
  --text-muted: #96958c;
  --series-1: #3987e5;
  --series-2: #d95926;
  --series-3: #199e70;
  --series-4: #c98500;
  --good: #4bb96f;
  --bad: #e66767;
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  padding: 0;
  background: var(--surface-0);
  color: var(--text-primary);
  font: 400 16px/1.45 -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, system-ui, sans-serif;
  -webkit-text-size-adjust: 100%;
  overscroll-behavior-y: contain;
}

body {
  min-height: 100vh;
  min-height: 100dvh;
  padding-bottom: calc(96px + env(safe-area-inset-bottom));
}

/* ---------- top bar ---------- */
.topbar {
  position: sticky;
  top: 0;
  z-index: 20;
  background: color-mix(in srgb, var(--surface-1) 92%, transparent);
  backdrop-filter: saturate(1.4) blur(12px);
  border-bottom: 1px solid var(--border);
  padding: calc(8px + env(safe-area-inset-top)) 12px 0;
}

.topbar-row {
  display: flex;
  align-items: center;
  gap: 6px;
  min-height: 44px;
}

.topbar h1 {
  flex: 1;
  margin: 0;
  font-size: 17px;
  font-weight: 650;
  letter-spacing: -.01em;
  text-align: center;
}

.icon-btn {
  min-width: 44px;
  min-height: 44px;
  border: 0;
  background: none;
  color: var(--text-secondary);
  font-size: 19px;
  border-radius: var(--radius-sm);
  cursor: pointer;
}
.icon-btn:active { background: var(--surface-2); }

.year-strip {
  display: flex;
  gap: 6px;
  overflow-x: auto;
  padding: 8px 0 10px;
  scrollbar-width: none;
}
.year-strip::-webkit-scrollbar { display: none; }

.year-chip {
  flex: 0 0 auto;
  padding: 7px 15px;
  border-radius: 999px;
  border: 1px solid var(--border);
  background: var(--surface-1);
  color: var(--text-secondary);
  font-size: 14px;
  font-weight: 550;
  cursor: pointer;
}
.year-chip.is-on {
  background: var(--series-1);
  border-color: var(--series-1);
  color: #fff;
}

/* ---------- layout ---------- */
main { padding: 14px 12px 0; max-width: 780px; margin: 0 auto; }

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

.card > h2 {
  margin: 0 0 2px;
  font-size: 15px;
  font-weight: 650;
}
.card > .sub {
  margin: 0 0 12px;
  color: var(--text-muted);
  font-size: 13px;
}

/* ---------- stat tiles ---------- */
.tiles {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(150px, 1fr));
  gap: 10px;
  margin-bottom: 12px;
}
.tile {
  background: var(--surface-1);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 12px 14px;
}
.tile-label {
  display: block;
  font-size: 12px;
  font-weight: 550;
  text-transform: uppercase;
  letter-spacing: .04em;
  color: var(--text-muted);
}
.tile-value {
  display: block;
  margin-top: 4px;
  /* Clamped so a long negative figure like -5429.931 doesn't wrap its unit. */
  font-size: clamp(17px, 5.2vw, 23px);
  font-weight: 680;
  letter-spacing: -.02em;
  font-variant-numeric: tabular-nums;
  white-space: nowrap;
}
.tile-value small { font-size: 13px; font-weight: 550; color: var(--text-muted); }
.tile-value.is-good { color: var(--good); }
.tile-value.is-bad { color: var(--bad); }

/* ---------- charts ---------- */
.chart-host { position: relative; }
.chart { display: block; width: 100%; height: auto; touch-action: pan-y; }
.chart.pie { max-width: 320px; margin: 0 auto; }
.grid { stroke: var(--border); stroke-width: 1; }
.axis-label { fill: var(--text-muted); font-size: 11px; font-family: inherit; }
.crosshair { stroke: var(--border-strong); stroke-width: 1; stroke-dasharray: 3 3; }
.hover-dot { transition: opacity .1s; pointer-events: none; }

.chart-tip {
  position: absolute;
  top: 0;
  transform: translateX(-50%);
  pointer-events: none;
  background: var(--surface-2);
  border: 1px solid var(--border-strong);
  border-radius: var(--radius-sm);
  box-shadow: var(--shadow);
  padding: 7px 9px;
  font-size: 12px;
  min-width: 132px;
  z-index: 5;
}
.tip-head { font-weight: 650; margin-bottom: 4px; }
.tip-row { display: flex; align-items: center; gap: 6px; white-space: nowrap; }
.tip-name { color: var(--text-secondary); }
.tip-val { margin-left: auto; font-variant-numeric: tabular-nums; font-weight: 550; }
.swatch {
  width: 9px; height: 9px; border-radius: 2px; flex: 0 0 auto;
  display: inline-block;
}

/* ---------- ranked bars ---------- */
.bars { display: flex; flex-direction: column; gap: 2px; }
.bar-row {
  display: grid;
  grid-template-columns: 104px 1fr auto;
  align-items: center;
  gap: 9px;
  width: 100%;
  padding: 5px 0;
  border: 0;
  background: none;
  color: inherit;
  font: inherit;
  text-align: left;
  cursor: pointer;
  border-radius: 6px;
}
.bar-row:active { background: var(--surface-2); }
.bar-label {
  font-size: 12.5px;
  color: var(--text-secondary);
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.bar-track {
  height: 16px;
  background: var(--surface-0);
  border-radius: 4px;
  overflow: hidden;
}
.bar-fill {
  display: block;
  height: 100%;
  background: var(--series-1);
  border-radius: 0 4px 4px 0;
  min-width: 2px;
}
.bar-value {
  font-size: 12.5px;
  font-variant-numeric: tabular-nums;
  font-weight: 550;
  white-space: nowrap;
}

/* ---------- pie legend ---------- */
.pie-legend {
  list-style: none;
  margin: 12px 0 0;
  padding: 0;
  display: flex;
  flex-direction: column;
  gap: 5px;
}
.pie-legend li {
  display: flex;
  align-items: center;
  gap: 8px;
  font-size: 13px;
}
.pie-name { color: var(--text-secondary); }
.pie-val {
  margin-left: auto;
  font-variant-numeric: tabular-nums;
  font-weight: 550;
  white-space: nowrap;
}
.pie-val small { color: var(--text-muted); font-weight: 400; }

/* ---------- small multiples ---------- */
.multiples {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(148px, 1fr));
  gap: 10px;
}
.multiple {
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  background: var(--surface-1);
  padding: 9px 10px 4px;
  text-align: left;
  cursor: pointer;
  font: inherit;
  color: inherit;
}
.multiple:active { background: var(--surface-2); }
.multiple-head { display: flex; align-items: baseline; gap: 6px; margin-bottom: 2px; }
.multiple-name {
  font-size: 11.5px;
  font-weight: 600;
  color: var(--text-secondary);
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.multiple-total {
  margin-left: auto;
  font-size: 11.5px;
  font-variant-numeric: tabular-nums;
  color: var(--text-muted);
}
.spark { display: block; width: 100%; height: auto; }

/* ---------- month list ---------- */
.months { display: flex; flex-direction: column; gap: 1px; }
.month-row {
  display: grid;
  grid-template-columns: 1fr auto auto;
  align-items: center;
  gap: 10px;
  width: 100%;
  padding: 11px 4px;
  border: 0;
  border-bottom: 1px solid var(--border);
  background: none;
  color: inherit;
  font: inherit;
  text-align: left;
  cursor: pointer;
}
.month-row:last-child { border-bottom: 0; }
.month-row:active { background: var(--surface-2); }
.month-row[disabled] { opacity: .45; cursor: default; }
.month-name { font-size: 14.5px; font-weight: 550; }
.month-dates { display: block; font-size: 11.5px; color: var(--text-muted); font-weight: 400; }
.month-total { font-variant-numeric: tabular-nums; font-size: 14px; font-weight: 600; }
.month-chev { color: var(--text-muted); font-size: 15px; }

/* ---------- entry list ---------- */
.entries { display: flex; flex-direction: column; }
.entry {
  display: grid;
  grid-template-columns: 46px 1fr auto;
  gap: 10px;
  align-items: baseline;
  padding: 9px 0;
  border-bottom: 1px solid var(--border);
}
.entry:last-child { border-bottom: 0; }
.entry-date {
  font-size: 11.5px;
  color: var(--text-muted);
  font-variant-numeric: tabular-nums;
}
.entry-cat { font-size: 13.5px; font-weight: 550; }
.entry-desc { display: block; font-size: 12.5px; color: var(--text-secondary); font-weight: 400; }
.entry-amt {
  font-size: 13.5px;
  font-weight: 600;
  font-variant-numeric: tabular-nums;
  white-space: nowrap;
}
.tag {
  display: inline-block;
  margin-left: 6px;
  padding: 1px 5px;
  border-radius: 4px;
  background: var(--surface-0);
  border: 1px solid var(--border);
  font-size: 10px;
  font-weight: 600;
  color: var(--text-muted);
  vertical-align: 1px;
}

/* ---------- toggles ---------- */
.seg {
  display: inline-flex;
  padding: 2px;
  gap: 2px;
  background: var(--surface-0);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
}
.seg-btn {
  border: 0;
  background: none;
  color: var(--text-secondary);
  font: inherit;
  font-size: 13px;
  font-weight: 550;
  padding: 7px 13px;
  border-radius: 7px;
  cursor: pointer;
  min-height: 36px;
}
.seg-btn.is-on {
  background: var(--surface-2);
  color: var(--text-primary);
  box-shadow: 0 1px 2px rgba(0, 0, 0, .08);
}
.seg-sm .seg-btn { padding: 6px 11px; min-height: 32px; font-size: 12.5px; }

.card-head {
  display: flex;
  align-items: flex-start;
  gap: 10px;
  margin-bottom: 10px;
}
.card-head h2 { margin: 0; font-size: 15px; font-weight: 650; }
.card-head .sub { margin: 1px 0 0; font-size: 12.5px; color: var(--text-muted); }
.card-head .seg { margin-left: auto; flex: 0 0 auto; }

.cat-toggles {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
  margin-top: 12px;
}
.cat-chip {
  display: inline-flex;
  align-items: center;
  gap: 5px;
  padding: 5px 10px;
  border-radius: 999px;
  border: 1px solid var(--border);
  background: var(--surface-1);
  color: var(--text-secondary);
  font-size: 12px;
  font-weight: 550;
  cursor: pointer;
  min-height: 32px;
}
.cat-chip[aria-pressed="true"] {
  border-color: var(--border-strong);
  background: var(--surface-2);
  color: var(--text-primary);
}
.cat-chip[disabled] { opacity: .4; cursor: default; }

/* ---------- fab ---------- */
.fab {
  position: fixed;
  right: 18px;
  bottom: calc(20px + env(safe-area-inset-bottom));
  width: 58px;
  height: 58px;
  border-radius: 50%;
  border: 0;
  background: var(--series-1);
  color: #fff;
  font-size: 27px;
  line-height: 1;
  box-shadow: 0 4px 14px rgba(42, 120, 214, .4);
  cursor: pointer;
  z-index: 30;
}
.fab:active { transform: scale(.95); }
/* Tuck out of the way while scrolling so it never sits on top of a figure. */
.fab {
  transition: transform .18s ease, opacity .18s ease;
}
.fab.is-tucked {
  transform: translateY(78px);
  opacity: 0;
  pointer-events: none;
}

/* ---------- dialogs ---------- */
.sheet {
  border: 0;
  padding: 0;
  width: 100%;
  max-width: 520px;
  background: var(--surface-1);
  color: var(--text-primary);
  border-radius: var(--radius) var(--radius) 0 0;
  margin: auto auto 0;
  box-shadow: var(--shadow);
}
.sheet::backdrop { background: rgba(0, 0, 0, .45); }
.sheet form, .sheet > * { padding: 0; }
.sheet form { padding: 0 16px calc(16px + env(safe-area-inset-bottom)); }
.sheet > .sheet-head, .sheet > .meta, .sheet > .menu-actions {
  padding-left: 16px;
  padding-right: 16px;
}
.sheet > .menu-actions { padding-bottom: calc(16px + env(safe-area-inset-bottom)); }

@media (min-width: 560px) {
  .sheet { border-radius: var(--radius); margin: auto; }
}

.sheet-head {
  display: flex;
  align-items: center;
  padding-top: 14px;
  padding-bottom: 6px;
}
.sheet-head h2 { margin: 0; font-size: 17px; font-weight: 650; flex: 1; }

.field { display: block; margin: 14px 0 0; }
.field > span {
  display: block;
  font-size: 12.5px;
  font-weight: 600;
  color: var(--text-secondary);
  margin-bottom: 5px;
}
.field > span small { font-weight: 400; color: var(--text-muted); }

input[type="text"], input[type="number"], input[type="date"], select {
  width: 100%;
  padding: 12px;
  font: inherit;
  font-size: 16px; /* 16px keeps iOS from zooming on focus */
  color: var(--text-primary);
  background: var(--surface-2);
  border: 1px solid var(--border-strong);
  border-radius: var(--radius-sm);
  min-height: 46px;
}
input:focus-visible, select:focus-visible, button:focus-visible {
  outline: 2px solid var(--series-1);
  outline-offset: 1px;
}

.amount-row { display: flex; gap: 8px; align-items: stretch; }
.amount-row input { flex: 1; }
.amount-row .seg { align-items: center; }

.hint { margin: 6px 0 0; font-size: 12.5px; color: var(--text-muted); }
.err {
  margin: 12px 0 0;
  padding: 10px 12px;
  font-size: 13px;
  color: var(--bad);
  background: color-mix(in srgb, var(--bad) 9%, transparent);
  border: 1px solid color-mix(in srgb, var(--bad) 28%, transparent);
  border-radius: var(--radius-sm);
}
.warn {
  margin: 6px 0 0;
  padding: 9px 11px;
  font-size: 12.5px;
  color: var(--text-secondary);
  background: color-mix(in srgb, var(--series-4) 12%, transparent);
  border: 1px solid color-mix(in srgb, var(--series-4) 34%, transparent);
  border-radius: var(--radius-sm);
}

.sheet-actions {
  display: flex;
  gap: 9px;
  margin-top: 20px;
}
.btn {
  flex: 1;
  padding: 13px 16px;
  font: inherit;
  font-size: 15px;
  font-weight: 600;
  border-radius: var(--radius-sm);
  border: 1px solid var(--border-strong);
  background: var(--surface-2);
  color: var(--text-primary);
  cursor: pointer;
  min-height: 48px;
}
.btn-primary {
  background: var(--series-1);
  border-color: var(--series-1);
  color: #fff;
}
.btn-primary[disabled] { opacity: .6; cursor: default; }
.btn-quiet { background: var(--surface-2); }
.btn-danger { color: var(--bad); }
.btn-danger-solid {
  background: var(--bad);
  border-color: var(--bad);
  color: #fff;
}

/* Entry rows become tappable once delete exists. */
button.entry {
  width: 100%;
  border: 0;
  border-bottom: 1px solid var(--border);
  background: none;
  color: inherit;
  font: inherit;
  text-align: left;
  cursor: pointer;
}
button.entry:active { background: var(--surface-2); }
.entry-chev { color: var(--text-muted); font-size: 15px; align-self: center; }

.menu-actions { display: flex; flex-direction: column; gap: 8px; margin-top: 14px; }
.menu-actions .btn { flex: none; }

.meta { margin: 8px 0 0; font-size: 13px; }
.meta div { display: flex; gap: 10px; padding: 6px 0; border-bottom: 1px solid var(--border); }
.meta dt { color: var(--text-muted); }
.meta dd { margin: 0 0 0 auto; text-align: right; word-break: break-all; }

/* ---------- misc ---------- */
.loading {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 10px;
  padding: 60px 20px;
  color: var(--text-secondary);
  font-size: 14px;
}
.spinner {
  width: 17px; height: 17px;
  border: 2px solid var(--border-strong);
  border-top-color: var(--series-1);
  border-radius: 50%;
  animation: spin .7s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }

.empty { color: var(--text-muted); font-size: 13.5px; text-align: center; padding: 18px 0; }

.splash { text-align: center; padding: 36px 18px; }
.splash h2 { font-size: 21px; margin: 0 0 8px; letter-spacing: -.02em; }
.splash p { color: var(--text-secondary); font-size: 14.5px; margin: 0 auto 20px; max-width: 42ch; }
.splash .btn { flex: none; display: inline-block; min-width: 220px; }
.splash ol { text-align: left; max-width: 46ch; margin: 0 auto 20px; color: var(--text-secondary); font-size: 14px; line-height: 1.6; }
.splash code {
  font-size: 12.5px;
  background: var(--surface-2);
  border: 1px solid var(--border);
  border-radius: 5px;
  padding: 1px 5px;
  word-break: break-all;
}

.toast {
  position: fixed;
  left: 50%;
  transform: translateX(-50%);
  bottom: calc(92px + env(safe-area-inset-bottom));
  background: var(--text-primary);
  color: var(--surface-1);
  padding: 11px 17px;
  border-radius: 999px;
  font-size: 13.5px;
  font-weight: 550;
  box-shadow: var(--shadow);
  z-index: 40;
  max-width: calc(100vw - 32px);
  text-align: center;
  /* Informational only — must never swallow a tap meant for the content. */
  pointer-events: none;
}

@media (prefers-reduced-motion: reduce) {
  * { animation-duration: .01ms !important; transition-duration: .01ms !important; }
}
