/* ============================================================
   Hushållsboken — a two-person ledger.

   The organising idea: this app only ever answers "which of us,
   and which way?". Position carries identity — whose entry, whose
   side of the beam. Colour carries direction, and nothing else:
   credit and debit are the only two hues that mean anything, which
   is why no chart in here uses a categorical palette.
   ============================================================ */

:root {
  color-scheme: light;

  --ground:     #E4E9E8;
  --surface:    #F8FAFA;
  --surface-2:  #EEF2F2;
  --ink:        #10262A;
  --ink-soft:   #4A6064;
  --ink-faint:  #7A8E91;
  --rule:       #C9D3D2;
  --rule-firm:  #A6B4B4;

  /* validated pair — see README: credit/debit are the only data colours */
  --credit:     #00879B;
  --debit:      #AF4526;
  --flag:       #9A6A10;   /* reserved: needs a human. never a series */

  --credit-wash: #D7EBEF;
  --debit-wash:  #F3DED6;
  --flag-wash:   #F2E8D2;

  --focus:      #00879B;

  --ui: ui-sans-serif, system-ui, -apple-system, "Segoe UI", Roboto,
        "Helvetica Neue", Arial, sans-serif;
  --num: ui-monospace, "SF Mono", "Cascadia Mono", "Roboto Mono",
         Menlo, Consolas, monospace;

  --step--1: 0.8125rem;
  --step-0:  0.9375rem;
  --step-1:  1.0625rem;
  --step-2:  1.375rem;
  --step-3:  1.875rem;
  --step-4:  2.5rem;

  --measure: 62rem;
  --gap: 1rem;
  --radius: 3px;
}

@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    color-scheme: dark;
    --ground:     #0F1A1C;
    --surface:    #15201F;
    --surface-2:  #1C2827;
    --ink:        #E3EAE8;
    --ink-soft:   #9BAAAA;
    --ink-faint:  #6E7E7E;
    --rule:       #2B3837;
    --rule-firm:  #3E4D4C;
    --credit:     #179AAB;
    --debit:      #C8673F;
    --flag:       #C2913A;
    --credit-wash: #10353B;
    --debit-wash:  #3A2219;
    --flag-wash:   #332913;
    --focus:      #45BACB;
  }
}

:root[data-theme="dark"] {
  color-scheme: dark;
  --ground:     #0F1A1C;
  --surface:    #15201F;
  --surface-2:  #1C2827;
  --ink:        #E3EAE8;
  --ink-soft:   #9BAAAA;
  --ink-faint:  #6E7E7E;
  --rule:       #2B3837;
  --rule-firm:  #3E4D4C;
  --credit:     #179AAB;
  --debit:      #C8673F;
  --flag:       #C2913A;
  --credit-wash: #10353B;
  --debit-wash:  #3A2219;
  --flag-wash:   #332913;
  --focus:      #45BACB;
}

* { box-sizing: border-box; }

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

body {
  margin: 0;
  background: var(--ground);
  color: var(--ink);
  font-family: var(--ui);
  font-size: var(--step-0);
  line-height: 1.55;
  -webkit-font-smoothing: antialiased;
}

.boot { padding: 4rem 1.25rem; color: var(--ink-faint); }

/* every figure in the app is tabular, so columns of money line up */
.num, td.num, .amount, input[type="number"] {
  font-family: var(--num);
  font-variant-numeric: tabular-nums;
  font-feature-settings: "tnum" 1;
}

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

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

/* ---------- chrome ---------- */

.topbar {
  border-bottom: 1px solid var(--rule);
  background: var(--surface);
}
.topbar-inner {
  max-width: var(--measure);
  margin: 0 auto;
  padding: 0.6rem clamp(0.9rem, 3vw, 1.5rem);
  display: flex;
  align-items: center;
  gap: 0.5rem 1.25rem;
  flex-wrap: wrap;
}
.wordmark {
  font-size: var(--step-0);
  font-weight: 650;
  letter-spacing: -0.012em;
  margin: 0;
  display: flex;
  align-items: center;
  gap: 0.5rem;
}
.wordmark svg { display: block; }

nav.tabs { display: flex; gap: 0.15rem; margin-inline-start: auto; flex-wrap: wrap; }
nav.tabs a {
  color: var(--ink-soft);
  text-decoration: none;
  padding: 0.3rem 0.6rem;
  border-radius: var(--radius);
  font-size: var(--step--1);
}
nav.tabs a:hover { background: var(--surface-2); color: var(--ink); }
nav.tabs a[aria-current="page"] {
  color: var(--ink);
  font-weight: 600;
  box-shadow: inset 0 -2px 0 var(--credit);
}

.whoami {
  display: flex;
  align-items: center;
  gap: 0.55rem;
  font-size: var(--step--1);
  color: var(--ink-soft);
}

main {
  max-width: var(--measure);
  margin: 0 auto;
  padding: clamp(1.25rem, 4vw, 2.25rem) clamp(0.9rem, 3vw, 1.5rem) 6rem;
}
main.wide { max-width: 76rem; }

/* ---------- the beam: hero, diverging chart and answer in one ---------- */

.beam { margin: 0 0 1.75rem; }
.beam svg { display: block; width: 100%; height: auto; }
.beam-verdict {
  font-size: var(--step-2);
  font-weight: 600;
  letter-spacing: -0.018em;
  margin: 0.2rem 0 0.1rem;
}
.beam-verdict.credit { color: var(--credit); }
.beam-verdict.debit  { color: var(--debit); }
.beam-note { margin: 0; color: var(--ink-soft); font-size: var(--step--1); max-width: 48ch; }

/* ---------- what the app wants next ---------- */

.nextup {
  display: flex;
  gap: 0.85rem;
  align-items: flex-start;
  padding: 0.85rem 1rem;
  background: var(--surface);
  border: 1px solid var(--rule);
  border-left: 3px solid var(--credit);
  border-radius: var(--radius);
  margin-bottom: 1.5rem;
}
.nextup.waiting { border-left-color: var(--flag); }
.nextup.done    { border-left-color: var(--rule-firm); }
.nextup p { margin: 0; }
.nextup strong { font-weight: 600; }
.nextup .nextup-body { flex: 1; min-width: 0; }
.nextup-actions { display: flex; gap: 0.5rem; flex-wrap: wrap; margin-top: 0.6rem; }

/* ---------- the ledger ---------- */

section.panel { margin-bottom: 2.25rem; }
section.panel > h2 {
  font-size: var(--step-1);
  font-weight: 650;
  letter-spacing: -0.012em;
  margin: 0 0 0.15rem;
}
section.panel > .sub { margin: 0 0 0.9rem; color: var(--ink-soft); font-size: var(--step--1); }

.ledger { border-top: 1px solid var(--rule-firm); }

.row {
  display: grid;
  grid-template-columns: 3px 1fr auto;
  gap: 0 0.85rem;
  align-items: start;
  padding: 0.7rem 0;
  border-bottom: 1px solid var(--rule);
}
/* the gutter mark says whose entry this is — position, not colour */
.row .side { width: 3px; align-self: stretch; border-radius: 2px; background: var(--rule-firm); }
.row.mine .side { background: var(--ink); }

.row-main { min-width: 0; }
.row-desc { font-weight: 550; overflow-wrap: anywhere; }
.row-meta { font-size: var(--step--1); color: var(--ink-soft); display: flex; gap: 0.6rem; flex-wrap: wrap; }
.row-amounts { text-align: right; white-space: nowrap; }
.row-amount { font-family: var(--num); font-variant-numeric: tabular-nums; font-weight: 600; }
.row-share { font-size: var(--step--1); color: var(--ink-faint); font-family: var(--num); }

.row-actions { grid-column: 2 / -1; display: flex; gap: 0.4rem; flex-wrap: wrap; margin-top: 0.55rem; }

.state {
  display: inline-flex;
  align-items: center;
  gap: 0.3rem;
  font-size: var(--step--1);
}
.state::before { content: ""; width: 7px; height: 7px; border-radius: 50%; background: currentColor; }
.state.draft    { color: var(--ink-faint); }
.state.pending  { color: var(--ink-soft); }
.state.approved { color: var(--credit); }
.state.disputed { color: var(--flag); font-weight: 600; }

.dispute-note {
  grid-column: 2 / -1;
  margin: 0.5rem 0 0;
  padding: 0.5rem 0.7rem;
  background: var(--flag-wash);
  border-radius: var(--radius);
  font-size: var(--step--1);
}

.empty {
  padding: 2rem 0 2.5rem;
  color: var(--ink-soft);
  border-bottom: 1px solid var(--rule);
}
.empty strong { display: block; color: var(--ink); font-weight: 600; margin-bottom: 0.2rem; }

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

form.stack { display: grid; gap: 0.85rem; }
.field { display: grid; gap: 0.3rem; }
.field > label { font-size: var(--step--1); color: var(--ink-soft); }
.field-row { display: grid; grid-template-columns: repeat(auto-fit, minmax(9rem, 1fr)); gap: 0.85rem; }

input, select, textarea, button {
  font: inherit;
  color: inherit;
}
input, select, textarea {
  background: var(--surface);
  border: 1px solid var(--rule-firm);
  border-radius: var(--radius);
  padding: 0.5rem 0.6rem;
  width: 100%;
  min-width: 0;
}
input:hover, select:hover, textarea:hover { border-color: var(--ink-faint); }
input[aria-invalid="true"] { border-color: var(--debit); }

button {
  background: var(--surface);
  border: 1px solid var(--rule-firm);
  border-radius: var(--radius);
  padding: 0.45rem 0.8rem;
  cursor: pointer;
}
button:hover:not(:disabled) { border-color: var(--ink-soft); background: var(--surface-2); }
button:disabled { opacity: 0.5; cursor: not-allowed; }

button.primary {
  background: var(--ink);
  border-color: var(--ink);
  color: var(--surface);
  font-weight: 600;
}
button.primary:hover:not(:disabled) { background: var(--credit); border-color: var(--credit); }
button.quiet { border-color: transparent; background: transparent; color: var(--ink-soft); }
button.quiet:hover:not(:disabled) { color: var(--ink); background: var(--surface-2); }
button.danger { color: var(--debit); }
button.small { padding: 0.25rem 0.55rem; font-size: var(--step--1); }

.hint { font-size: var(--step--1); color: var(--ink-soft); margin: 0; }
.problem {
  font-size: var(--step--1);
  color: var(--debit);
  margin: 0;
  font-weight: 550;
}

/* category suggestions: offered, never applied */
.suggestions { display: flex; gap: 0.4rem; flex-wrap: wrap; align-items: center; }
.suggestions .lead { font-size: var(--step--1); color: var(--ink-soft); }

.warn {
  padding: 0.6rem 0.75rem;
  background: var(--flag-wash);
  border-radius: var(--radius);
  font-size: var(--step--1);
}
.warn p { margin: 0; }

/* ---------- readiness: the two of you, side by side ---------- */

.readiness { display: grid; grid-template-columns: 1fr 1fr; gap: 1px; background: var(--rule); border: 1px solid var(--rule); border-radius: var(--radius); overflow: hidden; }
.readiness > div { background: var(--surface); padding: 0.7rem 0.85rem; }
.readiness .who { font-weight: 600; }
.readiness .what { font-size: var(--step--1); color: var(--ink-soft); }
.readiness .what.yes { color: var(--credit); }

/* ---------- charts ---------- */

.charts { display: grid; gap: 2rem; }
.chart { min-width: 0; }
.chart h3 { font-size: var(--step-0); font-weight: 650; margin: 0 0 0.1rem; }
.chart .sub { margin: 0 0 0.75rem; color: var(--ink-soft); font-size: var(--step--1); }
.chart svg { display: block; width: 100%; height: auto; overflow: visible; }

.facets { display: grid; grid-template-columns: repeat(auto-fit, minmax(10.5rem, 1fr)); gap: 1.25rem 1.5rem; }
.facet h4 { margin: 0 0 0.1rem; font-size: var(--step--1); font-weight: 600; }
.facet .facet-total { margin: 0 0 0.4rem; font-size: var(--step--1); color: var(--ink-faint); font-family: var(--num); }

.tiles { display: grid; grid-template-columns: repeat(auto-fit, minmax(11rem, 1fr)); gap: 1px; background: var(--rule); border: 1px solid var(--rule); border-radius: var(--radius); overflow: hidden; }
.tile { background: var(--surface); padding: 0.85rem 1rem; }
.tile .tile-label { font-size: var(--step--1); color: var(--ink-soft); }
.tile .tile-value { font-family: var(--num); font-variant-numeric: tabular-nums; font-size: var(--step-2); font-weight: 600; letter-spacing: -0.02em; }
.tile .tile-value.credit { color: var(--credit); }
.tile .tile-value.debit { color: var(--debit); }

#tooltip {
  position: fixed;
  z-index: 60;
  pointer-events: none;
  background: var(--ink);
  color: var(--surface);
  padding: 0.35rem 0.55rem;
  border-radius: var(--radius);
  font-size: var(--step--1);
  line-height: 1.35;
  opacity: 0;
  transition: opacity 90ms ease;
  max-width: 16rem;
}
#tooltip[data-open="true"] { opacity: 1; }
#tooltip .tt-value { font-family: var(--num); font-variant-numeric: tabular-nums; font-weight: 600; }

/* ---------- tables (history, sessions) ---------- */

table.data { width: 100%; border-collapse: collapse; }
table.data th {
  text-align: left;
  font-size: var(--step--1);
  font-weight: 600;
  color: var(--ink-soft);
  padding: 0 0.75rem 0.4rem 0;
  border-bottom: 1px solid var(--rule-firm);
}
table.data td { padding: 0.55rem 0.75rem 0.55rem 0; border-bottom: 1px solid var(--rule); vertical-align: top; }
table.data td:last-child, table.data th:last-child { padding-right: 0; }
table.data td.num, table.data th.num { text-align: right; }
table.data tbody tr:hover { background: var(--surface); }
table.data a { color: var(--ink); text-decoration-color: var(--rule-firm); text-underline-offset: 2px; }

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

.signin-wrap { min-height: 100dvh; display: grid; place-items: center; padding: 1.5rem; }
.signin {
  width: min(24rem, 100%);
  background: var(--surface);
  border: 1px solid var(--rule);
  border-radius: var(--radius);
  padding: clamp(1.25rem, 5vw, 2rem);
}
.signin h1 { font-size: var(--step-2); margin: 0 0 0.15rem; letter-spacing: -0.018em; }
.signin .sub { margin: 0 0 1.25rem; color: var(--ink-soft); font-size: var(--step--1); }
.signin-beam { margin-bottom: 1.1rem; }

/* ---------- step-up dialog ---------- */

#overlay:empty { display: none; }
.scrim {
  position: fixed;
  inset: 0;
  z-index: 50;
  background: color-mix(in oklab, var(--ink) 55%, transparent);
  display: grid;
  place-items: center;
  padding: 1.25rem;
}
.sheet {
  width: min(23rem, 100%);
  background: var(--surface);
  border: 1px solid var(--rule-firm);
  border-radius: var(--radius);
  padding: 1.25rem;
}
.sheet h2 { font-size: var(--step-1); margin: 0 0 0.2rem; }
.sheet .sub { margin: 0 0 1rem; color: var(--ink-soft); font-size: var(--step--1); }
.sheet-actions { display: flex; gap: 0.5rem; justify-content: flex-end; margin-top: 1rem; flex-wrap: wrap; }
.sheet-or { display: flex; align-items: center; gap: 0.6rem; margin: 0.9rem 0; color: var(--ink-faint); font-size: var(--step--1); }
.sheet-or::before, .sheet-or::after { content: ""; height: 1px; background: var(--rule); flex: 1; }

/* ---------- locked state ---------- */

.locked {
  padding: 2.5rem 1.25rem;
  text-align: left;
  max-width: 42ch;
}
.locked h2 { font-size: var(--step-1); margin: 0 0 0.3rem; }
.locked p { margin: 0 0 1rem; color: var(--ink-soft); }

/* ---------- toast ---------- */

#toast {
  position: fixed;
  left: 50%;
  bottom: 1.25rem;
  transform: translateX(-50%);
  z-index: 70;
  background: var(--ink);
  color: var(--surface);
  padding: 0.55rem 0.9rem;
  border-radius: var(--radius);
  font-size: var(--step--1);
  max-width: calc(100vw - 2rem);
}
#toast.debit { background: var(--debit); }

/* ---------- phone ---------- */

@media (max-width: 34rem) {
  .readiness { grid-template-columns: 1fr; }
  .row { grid-template-columns: 3px 1fr; }
  .row-amounts { grid-column: 2; text-align: left; margin-top: 0.3rem; }
  nav.tabs { width: 100%; margin-inline-start: 0; order: 3; }
  .whoami { margin-inline-start: auto; }
}

/* ---------- beam internals (SVG text takes its colour from tokens) ---------- */

.beam-name   { font-family: var(--ui); font-size: 14px; font-weight: 600; fill: var(--ink); }
.beam-axis   { stroke: var(--rule-firm); stroke-width: 1.5; }
.beam-centre { stroke: var(--ink-faint); stroke-width: 1.5; }
.beam-tick   { font-family: var(--ui); font-size: 11px; fill: var(--ink-faint); }
.beam-figure { font-family: var(--num); font-size: 16px; font-weight: 700; font-variant-numeric: tabular-nums; }

/* chart text, likewise — never the series colour */
.ax-label  { font-family: var(--ui); font-size: 11px; fill: var(--ink-soft); }
.ax-value  { font-family: var(--num); font-size: 11px; fill: var(--ink-soft); font-variant-numeric: tabular-nums; }
.ax-strong { font-family: var(--num); font-size: 11px; fill: var(--ink); font-weight: 600; font-variant-numeric: tabular-nums; }
.grid-line { stroke: var(--rule); stroke-width: 1; }
.zero-line { stroke: var(--rule-firm); stroke-width: 1.5; }

/* ---------- bulk import ---------- */

details.bulk > summary {
  cursor: pointer;
  display: flex;
  align-items: baseline;
  gap: 0.6rem;
  font-family: var(--ui);
  font-size: var(--step-1);
  font-weight: 650;
  letter-spacing: -0.012em;
  padding: 0.1rem 0;
}
details.bulk > summary::marker { color: var(--ink-faint); }
details.bulk > summary .bulk-note {
  font-size: var(--step--1);
  font-weight: 400;
  color: var(--ink-soft);
  letter-spacing: 0;
}
details.bulk[open] > summary { margin-bottom: 0.9rem; }
details.bulk textarea {
  font-family: var(--num);
  font-size: var(--step--1);
  line-height: 1.6;
  resize: vertical;
}

.import-rows { border-top: 1px solid var(--rule-firm); }

/* the review grid: one row per entry, label row only where there is room */
.import-head,
.import-row {
  display: grid;
  grid-template-columns: minmax(0, 2.2fr) 6.5rem 9rem minmax(0, 1.7fr) 7.5rem 8.5rem auto;
  gap: 0.4rem 0.5rem;
  align-items: center;
}
.import-head {
  font-size: var(--step--1);
  color: var(--ink-soft);
  padding: 0.45rem 0;
  border-bottom: 1px solid var(--rule);
}
.import-entry {
  padding: 0.5rem 0;
  border-bottom: 1px solid var(--rule);
}
.import-entry.incomplete { background: var(--flag-wash); }
.import-note {
  margin: 0.35rem 0 0;
  font-size: var(--step--1);
  color: var(--flag);
  font-weight: 550;
}
.import-row input, .import-row select { padding: 0.35rem 0.45rem; font-size: var(--step--1); }
.import-cat { display: flex; gap: 0.3rem; align-items: center; min-width: 0; }
.import-cat select { min-width: 0; }

@media (max-width: 60rem) {
  .import-head { display: none; }
  .import-entry { padding: 0.85rem 0; }
  .import-row {
    grid-template-columns: 1fr 1fr;
    gap: 0.5rem;
  }
  .import-row > :first-child { grid-column: 1 / -1; }
  .import-row > :last-child { grid-column: 1 / -1; justify-self: start; }
}

/* ---------- how costs are split ---------- */

.split-current {
  border-top: 1px solid var(--rule-firm);
  border-bottom: 1px solid var(--rule);
  padding: 0.85rem 0;
  margin-bottom: 1.25rem;
}
.split-mode {
  font-family: var(--num);
  font-variant-numeric: tabular-nums;
  font-size: var(--step-1);
  font-weight: 600;
  letter-spacing: -0.01em;
}
.split-current .hint { margin-top: 0.2rem; }

.split-derived {
  display: flex;
  gap: 1.25rem;
  flex-wrap: wrap;
  margin-top: 0.5rem;
  font-family: var(--num);
  font-variant-numeric: tabular-nums;
  font-size: var(--step--1);
  color: var(--ink-soft);
}
