@font-face {
  font-family: 'Atkinson Next';
  src: url('/fonts/atkinson-next.woff2') format('woff2');
  font-weight: 200 800;
  font-display: swap;
}

/* Banknote palette: pale note paper, intaglio blue-grey ink, £5-note teal. */
:root {
  --paper: #edf1ee;
  --surface: #f8faf8;
  --ink: #1d2b30;
  --muted: #5d6b70;
  --line: #d3dcd8;
  --teal: #0e6b66;
  --teal-soft: #d4e8e5;
  --amber: #a45f00;
  --amber-soft: #f6e6cc;
  --red: #a8261d;
  --red-soft: #f6dcd9;
  --on-teal: #ffffff;
  --hatch: rgba(14, 107, 102, 0.22);
  --focus: #0e6b66;

  --s-1: 0.8125rem;   /* 13 */
  --s0: 0.9375rem;    /* 15 */
  --s1: 1.171875rem;  /* 18.75 */
  --s2: 1.46875rem;   /* 23.5 */
  --s3: 1.83125rem;   /* 29.3 */
  --s4: 2.2875rem;    /* 36.6 */
  --s5: 2.8625rem;    /* 45.8 */

  --gutter: 20px;
  --radius-s: 6px;
  --radius-m: 12px;
  color-scheme: light;
}

@media (prefers-color-scheme: dark) {
  :root {
    --paper: #101719;
    --surface: #172124;
    --ink: #e4ecea;
    --muted: #93a3a6;
    --line: #2a3a3e;
    --teal: #5db8b0;
    --teal-soft: #173a38;
    --amber: #e3a24a;
    --amber-soft: #3a2a12;
    --red: #f07368;
    --red-soft: #3d1c19;
    --on-teal: #08201e;
    --hatch: rgba(93, 184, 176, 0.25);
    --focus: #5db8b0;
    color-scheme: dark;
  }
}

*, *::before, *::after { box-sizing: border-box; }
[hidden] { display: none !important; }

html { -webkit-text-size-adjust: 100%; }
body {
  margin: 0;
  background: var(--paper);
  color: var(--ink);
  font-family: 'Atkinson Next', system-ui, sans-serif;
  font-size: var(--s0);
  line-height: 1.5;
  font-variant-numeric: tabular-nums;
  -webkit-font-smoothing: antialiased;
}

h1, h2, h3, p { margin: 0; }
a { color: var(--teal); }
button, input, select, textarea { font: inherit; color: inherit; }
:focus-visible { outline: 2px solid var(--focus); outline-offset: 2px; border-radius: 4px; }

/* ---------- Login ---------- */
.login {
  min-height: 100dvh;
  display: grid;
  place-items: center;
  padding: var(--gutter);
}
.login-card {
  width: min(360px, 100%);
  display: grid;
  gap: 16px;
}
.login-mark { border-radius: 14px; }
.login-title { font-size: var(--s4); font-weight: 750; letter-spacing: -0.02em; line-height: 1.1; }
.login-sub { color: var(--muted); margin-top: -8px; }

/* ---------- Shell ---------- */
.app {
  max-width: 600px;
  margin: 0 auto;
  min-height: 100dvh;
  padding-bottom: calc(76px + env(safe-area-inset-bottom));
}
.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: calc(14px + env(safe-area-inset-top)) var(--gutter) 6px;
}
.brand {
  display: inline-flex;
  align-items: center;
  gap: 8px;
  color: var(--ink);
  text-decoration: none;
  font-weight: 750;
  font-size: var(--s1);
  letter-spacing: -0.01em;
}
.brand img { border-radius: 8px; }
.sync-chip {
  display: inline-flex;
  align-items: center;
  gap: 8px;
  border: 1px solid var(--line);
  background: var(--surface);
  border-radius: 999px;
  padding: 5px 12px;
  font-size: var(--s-1);
  color: var(--muted);
  cursor: pointer;
  min-height: 34px;
}
.sync-dot { width: 8px; height: 8px; border-radius: 50%; background: var(--teal); }
.sync-dot.is-error { background: var(--red); }
.sync-dot.is-busy { background: var(--amber); animation: pulse 1s ease-in-out infinite; }
@keyframes pulse { 50% { opacity: 0.3; } }

.view { padding: 8px var(--gutter) 24px; outline: none; }

.tabbar {
  position: fixed;
  left: 0; right: 0; bottom: 0;
  display: flex;
  justify-content: center;
  background: var(--surface);
  border-top: 1px solid var(--line);
  padding-bottom: env(safe-area-inset-bottom);
  z-index: 10;
}
.tabbar a {
  flex: 1;
  max-width: 120px;
  display: grid;
  justify-items: center;
  gap: 2px;
  padding: 9px 4px 10px;
  color: var(--muted);
  text-decoration: none;
  font-size: 0.75rem;
}
.tabbar svg {
  width: 24px; height: 24px;
  fill: none; stroke: currentColor; stroke-width: 1.8; stroke-linecap: round; stroke-linejoin: round;
}
.tabbar a[aria-current='page'] { color: var(--teal); font-weight: 700; }

/* ---------- Type ---------- */
.headline {
  font-size: var(--s5);
  font-weight: 780;
  line-height: 1.04;
  letter-spacing: -0.03em;
  margin-top: 18px;
}
.headline .soft { color: var(--muted); font-weight: 500; }
.headline.is-over { color: var(--red); }
.subline { font-size: var(--s1); color: var(--muted); margin-top: 10px; line-height: 1.35; }
.section { margin-top: 36px; }
.section-title { font-size: var(--s1); font-weight: 720; letter-spacing: -0.01em; margin-bottom: 12px; }
.section-head { display: flex; align-items: baseline; justify-content: space-between; gap: 12px; margin-bottom: 12px; }
.section-head .section-title { margin-bottom: 0; }
.muted { color: var(--muted); }
.small { font-size: var(--s-1); }
.pos { color: var(--teal); }
.neg { color: var(--red); }
.warn { color: var(--amber); }

/* ---------- Pace ribbon (the signature element) ---------- */
.ribbon { margin-top: 26px; }
.ribbon-track {
  position: relative;
  height: 46px;
  border-radius: var(--radius-s);
  background:
    repeating-linear-gradient(135deg, var(--hatch) 0 1px, transparent 1px 7px),
    var(--surface);
  border: 1px solid var(--line);
  overflow: hidden;
}
.ribbon-fill {
  position: absolute;
  inset: 0 auto 0 0;
  width: 0;
  background: var(--teal);
  transition: width 600ms cubic-bezier(.2,.7,.2,1);
}
.ribbon-fill.is-amber { background: var(--amber); }
.ribbon-fill.is-red { background: var(--red); }
.ribbon-today {
  position: absolute;
  top: -6px; bottom: -6px;
  width: 2px;
  margin-left: -1px;
  background: var(--ink);
}
.ribbon-legend {
  display: flex;
  justify-content: space-between;
  gap: 12px;
  margin-top: 8px;
  font-size: var(--s-1);
  color: var(--muted);
}
@media (prefers-reduced-motion: reduce) { .ribbon-fill { transition: none; } .sync-dot.is-busy { animation: none; } }

/* ---------- Facts row ---------- */
.facts {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 0;
  margin-top: 22px;
  border-top: 1px solid var(--line);
  border-bottom: 1px solid var(--line);
}
.fact { padding: 12px 0; }
.fact + .fact { padding-left: 14px; border-left: 1px solid var(--line); }
.fact-value { font-size: var(--s1); font-weight: 720; display: block; }
.fact-label { font-size: var(--s-1); color: var(--muted); }
.facts-note { margin-top: 8px; }

/* ---------- Lists ---------- */
.rows { list-style: none; margin: 0; padding: 0; }
.row {
  display: grid;
  grid-template-columns: auto 1fr auto;
  align-items: center;
  gap: 14px;
  padding: 12px 0;
  border-bottom: 1px solid var(--line);
}
.row:first-child { border-top: 1px solid var(--line); }
.row-main { min-width: 0; }
.row-title { font-weight: 650; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.row-sub { font-size: var(--s-1); color: var(--muted); }
.row-amount { font-weight: 650; text-align: right; white-space: nowrap; }
.row.is-done .row-title { color: var(--muted); }

.datebox {
  width: 44px;
  text-align: center;
  border: 1px solid var(--line);
  border-radius: var(--radius-s);
  background: var(--surface);
  padding: 3px 0 4px;
  line-height: 1.1;
}
.datebox b { display: block; font-size: var(--s1); }
.datebox span { font-size: 0.6875rem; color: var(--muted); }
.datebox.is-soon { border-color: var(--amber); }
.datebox.is-today { border-color: var(--red); }

.tick {
  display: inline-block;
  font-size: var(--s-1);
  padding: 1px 8px;
  border-radius: 999px;
  background: var(--teal-soft);
  color: var(--teal);
  font-weight: 650;
}
.tick.is-warn { background: var(--amber-soft); color: var(--amber); }
.tick.is-red { background: var(--red-soft); color: var(--red); }

.swatch { width: 10px; height: 10px; border-radius: 3px; background: var(--muted); }

/* Category bars */
.cat { padding: 12px 0; border-bottom: 1px solid var(--line); }
.cat:first-child { border-top: 1px solid var(--line); }
.cat-top { display: flex; justify-content: space-between; gap: 12px; align-items: baseline; }
.cat-name { font-weight: 650; display: inline-flex; align-items: center; gap: 8px; }
.cat-bar { height: 6px; border-radius: 3px; background: var(--line); margin-top: 8px; overflow: hidden; }
.cat-bar i { display: block; height: 100%; width: 0; background: var(--teal); border-radius: 3px; }
.cat-bar i.is-amber { background: var(--amber); }
.cat-bar i.is-red { background: var(--red); }
button.cat { width: 100%; text-align: left; background: none; border-left: 0; border-right: 0; border-top: 0; cursor: pointer; }
button.cat:first-child { border-top: 1px solid var(--line); }
button.cat[aria-pressed='true'] .cat-name { color: var(--teal); }

.daygroup { font-size: var(--s-1); color: var(--muted); font-weight: 650; padding: 18px 0 6px; }

/* ---------- Notices ---------- */
.notice {
  margin-top: 18px;
  padding: 14px 16px;
  border-radius: var(--radius-m);
  background: var(--surface);
  border: 1px solid var(--line);
  display: grid;
  gap: 6px;
}
.notice.is-warn { background: var(--amber-soft); border-color: transparent; }
.notice.is-red { background: var(--red-soft); border-color: transparent; }
.notice.is-ok { background: var(--teal-soft); border-color: transparent; }
.notice-title { font-weight: 720; }
.notice .btn { justify-self: start; margin-top: 4px; }

.empty { padding: 28px 0; color: var(--muted); }
.empty .btn { margin-top: 12px; }

/* ---------- Charts ---------- */
.chart { margin-top: 8px; }
.chart svg { width: 100%; height: auto; display: block; overflow: visible; }
.chart .grid { stroke: var(--line); stroke-width: 1; }
.chart .axis { fill: var(--muted); font-size: 11px; font-family: inherit; }
.chart .line-now { fill: none; stroke: var(--teal); stroke-width: 2.5; stroke-linejoin: round; stroke-linecap: round; }
.chart .line-prev { fill: none; stroke: var(--muted); stroke-width: 1.5; stroke-dasharray: 4 4; }
.chart .line-budget { stroke: var(--amber); stroke-width: 1.5; stroke-dasharray: 2 3; }
.chart .dot { fill: var(--teal); }
.legend { display: flex; flex-wrap: wrap; gap: 16px; font-size: var(--s-1); color: var(--muted); margin-top: 6px; }
.legend i { display: inline-block; width: 16px; height: 0; border-top: 2.5px solid var(--teal); vertical-align: middle; margin-right: 6px; }
.legend i.prev { border-top: 1.5px dashed var(--muted); }
.legend i.budget { border-top: 1.5px dotted var(--amber); }

/* ---------- Segmented control ---------- */
.seg { display: inline-flex; border: 1px solid var(--line); border-radius: 999px; padding: 3px; background: var(--surface); }
.seg button {
  border: 0; background: none; padding: 6px 14px; border-radius: 999px; cursor: pointer;
  font-size: var(--s-1); color: var(--muted); min-height: 32px;
}
.seg button[aria-pressed='true'] { background: var(--ink); color: var(--paper); font-weight: 650; }

/* ---------- Forms ---------- */
.form { display: grid; gap: 18px; }
.fieldset { border: 0; padding: 0; margin: 0; display: grid; gap: 14px; }
.fieldset legend { font-size: var(--s1); font-weight: 720; padding: 0; margin-bottom: 4px; float: left; width: 100%; }
.fieldset legend + * { clear: both; }
.fieldset + .fieldset { padding-top: 26px; border-top: 1px solid var(--line); }
.field { display: grid; gap: 6px; }
.field > span { font-weight: 650; font-size: var(--s-1); }
.field small { color: var(--muted); font-size: var(--s-1); }
.field input, .field select, .field textarea, .budget-line input {
  width: 100%;
  min-height: 46px;
  padding: 10px 12px;
  border: 1px solid var(--line);
  border-radius: var(--radius-s);
  background: var(--surface);
}
.field textarea { min-height: 96px; resize: vertical; }
.field input:focus, .field select:focus, .field textarea:focus, .budget-line input:focus { border-color: var(--teal); outline: none; box-shadow: 0 0 0 3px var(--teal-soft); }
.field-row { display: grid; grid-template-columns: 1fr 1fr; gap: 12px; }
.money-input { position: relative; }
.money-input::before { content: '£'; position: absolute; left: 12px; top: 50%; transform: translateY(-50%); color: var(--muted); pointer-events: none; }
.money-input input { padding-left: 26px; }
.check { display: flex; gap: 12px; align-items: flex-start; cursor: pointer; }
.check input { width: 22px; height: 22px; margin: 1px 0 0; accent-color: var(--teal); flex: none; }
.check small { display: block; color: var(--muted); font-size: var(--s-1); }
.budget-grid { display: grid; gap: 10px; }
.budget-line { display: grid; grid-template-columns: 1fr 130px; gap: 12px; align-items: center; }
.budget-line label { display: inline-flex; align-items: center; gap: 8px; font-weight: 600; }
.budget-line .money-input input { min-height: 42px; }
.form-error { color: var(--red); font-size: var(--s-1); }

.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 8px;
  min-height: 46px; padding: 10px 18px;
  border-radius: var(--radius-s);
  border: 1px solid var(--line);
  background: var(--surface);
  font-weight: 700;
  cursor: pointer;
  text-decoration: none;
  color: var(--ink);
}
.btn-primary { background: var(--teal); border-color: var(--teal); color: var(--on-teal); }
.btn:disabled { opacity: 0.6; cursor: wait; }
.btn-row { display: flex; flex-wrap: wrap; gap: 10px; }
.sticky-save {
  position: sticky;
  bottom: calc(72px + env(safe-area-inset-bottom));
  padding: 12px 0;
  background: linear-gradient(to top, var(--paper) 70%, transparent);
}
.sticky-save .btn { width: 100%; }

/* Slider for quick mortgage what-ifs */
.range { display: grid; gap: 4px; }
.range input[type='range'] { width: 100%; accent-color: var(--teal); min-height: 32px; }
.range-head { display: flex; justify-content: space-between; font-weight: 650; }

.log { list-style: none; padding: 0; margin: 0; font-size: var(--s-1); }
.log li { padding: 8px 0; border-bottom: 1px solid var(--line); }
.log b { font-weight: 650; }

/* ---------- Transaction sheet ---------- */
button.row {
  width: 100%;
  background: none;
  border-left: 0; border-right: 0; border-top: 0;
  text-align: left;
  cursor: pointer;
}
button.row:hover .row-title { color: var(--teal); }
li > button.row { border-top: 0; }
.rows > li { list-style: none; }
.sheet {
  border: 0;
  padding: 22px var(--gutter) calc(22px + env(safe-area-inset-bottom));
  width: min(600px, 100%);
  max-width: 100%;
  margin: auto auto 0;
  border-radius: 18px 18px 0 0;
  background: var(--paper);
  color: var(--ink);
  max-height: 90dvh;
}
.sheet::backdrop { background: rgba(10, 20, 22, 0.5); }
.sheet-title { font-size: var(--s2); font-weight: 750; letter-spacing: -0.01em; line-height: 1.2; overflow-wrap: anywhere; }
.small-legend { font-size: var(--s-1) !important; font-weight: 650 !important; }
.choice { display: grid; grid-template-columns: repeat(3, 1fr); border: 1px solid var(--line); border-radius: var(--radius-s); overflow: hidden; }
.choice label { position: relative; cursor: pointer; }
.choice input { position: absolute; opacity: 0; inset: 0; cursor: pointer; }
.choice span { display: flex; align-items: center; justify-content: center; min-height: 44px; padding: 6px; text-align: center; font-size: var(--s-1); font-weight: 650; background: var(--surface); }
.choice label + label span { border-left: 1px solid var(--line); }
.choice input:checked + span { background: var(--ink); color: var(--paper); }
.choice input:focus-visible + span { outline: 2px solid var(--focus); outline-offset: -2px; }

.toast {
  position: fixed;
  left: 50%;
  bottom: calc(86px + env(safe-area-inset-bottom));
  transform: translateX(-50%);
  background: var(--ink);
  color: var(--paper);
  padding: 10px 16px;
  border-radius: 999px;
  font-size: var(--s-1);
  font-weight: 650;
  z-index: 20;
  max-width: calc(100% - 40px);
}

@media (min-width: 640px) {
  :root { --gutter: 28px; }
  .headline { font-size: 3.4rem; }
}
