/* --------------------------------------------------------------- tokens */
:root {
  --ink: #16202c;
  --ink-soft: #4a5a6b;
  --ink-muted: #64748b;
  --paper: #f6f7f9;
  --surface: #ffffff;
  --line: #dde3ea;
  --line-strong: #c3ccd8;
  --brand: #1f3a5f;
  --brand-soft: #eaf0f7;
  --accent: #0b6e4f;
  --warn: #8a5a00;
  --warn-soft: #fdf3e0;
  --bad: #a32020;
  --bad-soft: #fdecec;
  --ok: #14663f;
  --ok-soft: #e8f5ee;
  --radius: 8px;
  --radius-sm: 5px;
  --shadow: 0 1px 2px rgb(22 32 44 / 8%), 0 6px 18px rgb(22 32 44 / 6%);
  --mono: ui-monospace, "SF Mono", "Cascadia Mono", Menlo, Consolas, monospace;
  --sans: system-ui, -apple-system, "Segoe UI", Roboto, "Helvetica Neue", sans-serif;
}

@media (prefers-color-scheme: dark) {
  :root {
    --ink: #e8edf3;
    --ink-soft: #b9c4d0;
    --ink-muted: #93a1b1;
    --paper: #131922;
    --surface: #1a212b;
    --line: #2c3745;
    --line-strong: #3c4a5b;
    --brand: #9fc0e6;
    --brand-soft: #1e2a38;
    --accent: #6fd3aa;
    --warn: #e6b95c;
    --warn-soft: #2e2413;
    --bad: #f19a9a;
    --bad-soft: #33191b;
    --ok: #7fd6a8;
    --ok-soft: #14291f;
    --shadow: 0 1px 2px rgb(0 0 0 / 40%);
  }
}

* {
  box-sizing: border-box;
}

html {
  color-scheme: light dark;
}

body {
  margin: 0;
  font-family: var(--sans);
  font-size: 15px;
  line-height: 1.5;
  color: var(--ink);
  background: var(--paper);
}

h1,
h2,
h3 {
  margin: 0 0 0.4em;
  line-height: 1.25;
}

h1 {
  font-size: 1.4rem;
}

h2 {
  font-size: 1.1rem;
}

h3 {
  font-size: 0.98rem;
}

p {
  margin: 0 0 0.6em;
}

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

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

.skip-link {
  position: absolute;
  left: -9999px;
}

.skip-link:focus {
  left: 0.5rem;
  top: 0.5rem;
  z-index: 10;
  padding: 0.5rem 0.75rem;
  background: var(--surface);
  border: 2px solid var(--brand);
  border-radius: var(--radius-sm);
}

/* ---------------------------------------------------------------- layout */
.app {
  max-width: 1180px;
  margin: 0 auto;
  padding: 0 1rem 3rem;
}

.app-header {
  display: flex;
  flex-wrap: wrap;
  gap: 0.75rem 1.5rem;
  align-items: flex-end;
  justify-content: space-between;
  padding: 1.25rem 0 0.9rem;
  border-bottom: 1px solid var(--line);
}

.brand-title {
  display: block;
  font-size: 1.35rem;
  font-weight: 650;
  text-decoration: none;
  color: var(--ink);
}

.brand-tagline {
  color: var(--ink-muted);
  font-size: 0.85rem;
}

.header-tools {
  display: flex;
  flex-wrap: wrap;
  gap: 0.75rem;
}

.body {
  display: grid;
  grid-template-columns: minmax(150px, 190px) 1fr;
  gap: 1.25rem;
  align-items: start;
  padding-top: 1.1rem;
}

@media (max-width: 760px) {
  .body {
    grid-template-columns: 1fr;
  }
}

.view {
  min-width: 0;
}

.app-footer {
  margin-top: 2rem;
  padding-top: 0.9rem;
  border-top: 1px solid var(--line);
  color: var(--ink-muted);
}

/* ------------------------------------------------------------------- nav */
.nav {
  display: flex;
  flex-direction: column;
  gap: 0.15rem;
  position: sticky;
  top: 0.75rem;
}

@media (max-width: 760px) {
  .nav {
    flex-direction: row;
    flex-wrap: wrap;
    position: static;
  }
}

.nav-link {
  display: block;
  padding: 0.4rem 0.6rem;
  border-radius: var(--radius-sm);
  text-decoration: none;
  color: var(--ink-soft);
}

.nav-link:hover {
  background: var(--brand-soft);
}

.nav-link.is-active {
  background: var(--brand-soft);
  color: var(--brand);
  font-weight: 600;
}

/* ----------------------------------------------------------------- cards */
.card,
.entry-card {
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 0.9rem 1rem;
  margin-bottom: 1rem;
}

.card-title {
  font-size: 1rem;
  font-weight: 650;
  margin-bottom: 0.6rem;
}

.page-header {
  display: flex;
  flex-wrap: wrap;
  gap: 0.5rem 1rem;
  align-items: baseline;
  justify-content: space-between;
  margin-bottom: 0.9rem;
}

.page-actions,
.form-actions,
.empty-actions {
  display: flex;
  flex-wrap: wrap;
  gap: 0.5rem;
  align-items: center;
}

.grid-2 {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(320px, 1fr));
  gap: 1rem;
  align-items: start;
}

/* --------------------------------------------------------------- controls */
button,
.btn {
  font: inherit;
  padding: 0.38rem 0.7rem;
  border: 1px solid var(--line-strong);
  border-radius: var(--radius-sm);
  background: var(--surface);
  color: var(--ink);
  cursor: pointer;
}

button:hover,
.btn:hover {
  border-color: var(--brand);
}

button:disabled {
  opacity: 0.55;
  cursor: not-allowed;
}

.btn-primary {
  background: var(--brand);
  border-color: var(--brand);
  color: #fff;
}

@media (prefers-color-scheme: dark) {
  .btn-primary {
    color: #10161d;
  }
}

.btn-secondary {
  background: var(--brand-soft);
  border-color: var(--line-strong);
}

.btn-danger {
  color: var(--bad);
  border-color: var(--bad);
  background: transparent;
}

.btn-ghost {
  background: transparent;
  border-color: transparent;
  color: var(--ink-soft);
  text-decoration: underline;
}

input,
select {
  font: inherit;
  padding: 0.35rem 0.45rem;
  border: 1px solid var(--line-strong);
  border-radius: var(--radius-sm);
  background: var(--surface);
  color: var(--ink);
  max-width: 100%;
}

input[type="checkbox"] {
  width: auto;
}

.field {
  display: flex;
  flex-direction: column;
  gap: 0.2rem;
  min-width: 0;
}

.field-label {
  font-size: 0.8rem;
  color: var(--ink-muted);
}

.hint {
  font-size: 0.8rem;
  color: var(--ink-muted);
}

.form-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(190px, 1fr));
  gap: 0.7rem;
  margin-bottom: 0.7rem;
}

.inline-field {
  display: inline-flex;
  gap: 0.35rem;
  align-items: center;
  white-space: nowrap;
}

.grow {
  flex: 1;
  min-width: 0;
}

.range-bar {
  display: flex;
  flex-wrap: wrap;
  gap: 0.6rem;
  align-items: flex-end;
  padding: 0.6rem 0.8rem;
  margin-bottom: 0.9rem;
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: var(--radius);
}

.range-label {
  font-size: 0.8rem;
  color: var(--ink-muted);
}

/* ---------------------------------------------------------------- tables */
.table {
  width: 100%;
  border-collapse: collapse;
  font-variant-numeric: tabular-nums;
}

.table th,
.table td {
  padding: 0.35rem 0.5rem;
  border-bottom: 1px solid var(--line);
  text-align: left;
  vertical-align: top;
}

.table th {
  font-size: 0.8rem;
  text-transform: uppercase;
  letter-spacing: 0.03em;
  color: var(--ink-muted);
  border-bottom-width: 2px;
}

.table .num,
.table td.num,
.num {
  text-align: right;
  font-family: var(--mono);
  font-size: 0.92rem;
}

.table tbody tr:hover {
  background: var(--brand-soft);
}

.table tr.is-closed td {
  color: var(--ink-muted);
  font-style: italic;
}

.indentable .lvl-1 {
  padding-left: 0;
  font-weight: 650;
}

.indentable .lvl-2 {
  padding-left: 0.9rem;
  font-weight: 600;
}

.indentable .lvl-3 {
  padding-left: 1.8rem;
}

.indentable .lvl-4 {
  padding-left: 2.7rem;
}

.indentable .lvl-5 {
  padding-left: 3.6rem;
}

.lines-table input,
.lines-table select {
  width: 100%;
}

/* The handle column is a fixed narrow strip; the rest of the row absorbs the
   remaining width. Scoped to .col-handle rather than :first-child, because the
   saved-entry tables reuse .lines-table without a handle column. */
.lines-table .col-handle {
  width: 1%;
  white-space: nowrap;
}

/* Amounts are typed, not read, so they line up on the decimal point too.
   Every number input in the app is an amount, so this needs no scoping. */
input[type='number'] {
  text-align: right;
}

/* An amount input sits in a right-aligned column, so the control has to fill
   its cell. Right-aligned text inside a default-width input does not line up
   with the right-aligned column header beside it. */
.lines-table input[type='number'] {
  width: 100%;
}

/* Reordering an entry's lines: a drag handle, not the whole row, because a
   draggable ancestor fights the <select> and <input> inside each cell. */
.line-handle {
  padding: 0.1rem 0.3rem;
  border: 1px solid transparent;
  background: none;
  color: var(--ink-muted);
  line-height: 1;
  cursor: grab;
  user-select: none;
}

.line-handle:hover {
  border-color: var(--line-strong);
  color: var(--ink);
}

.line-handle:active {
  cursor: grabbing;
}

.lines-table tr.is-dragging {
  opacity: 0.4;
}

/* The drop indicator is a border, not a box-shadow: a box-shadow on <tr> is
   unreliable under the border-collapse: collapse that .table sets. */
.lines-table tr.drop-before > td {
  border-top: 2px solid var(--brand);
}

.lines-table tr.drop-after > td {
  border-bottom: 2px solid var(--brand);
}

.row-actions {
  display: flex;
  flex-wrap: wrap;
  gap: 0.3rem;
}

.entry-card header {
  display: flex;
  flex-wrap: wrap;
  gap: 0.6rem;
  align-items: baseline;
  margin-bottom: 0.3rem;
}

.entry-card footer {
  display: flex;
  gap: 0.4rem;
  margin-top: 0.6rem;
}

.entry-no {
  font-weight: 650;
}

.entry-narration {
  margin-bottom: 0.5rem;
}

.tag {
  display: inline-block;
  padding: 0.05rem 0.4rem;
  margin-left: 0.35rem;
  font-size: 0.75rem;
  border: 1px solid var(--line-strong);
  border-radius: 999px;
  color: var(--ink-muted);
}

/* A "label ....... amount" row. The label column takes the slack so every
   amount's right edge lands on the container's right edge, which is what makes
   the figures read as a column rather than as loose inline text. */
.total-row {
  display: grid;
  grid-template-columns: minmax(0, 1fr) max-content;
  column-gap: 1rem;
  align-items: baseline;
}

.total-row .amount {
  text-align: right;
  font-family: var(--mono);
  font-variant-numeric: tabular-nums;
}

.totals,
.subtotal,
.result {
  display: flex;
  flex-direction: column;
  gap: 0.25rem;
}

.totals {
  margin-top: 0.7rem;
  padding-top: 0.5rem;
  border-top: 1px solid var(--line);
}

/* The pills and counts that share a strip but are not amounts. */
.totals-notes {
  display: flex;
  flex-wrap: wrap;
  gap: 0.4rem 1.2rem;
  align-items: baseline;
  margin-top: 0.35rem;
}

.subtotal {
  margin: 0.5rem 0 0;
}

.group-title {
  margin: 0.7rem 0 0.3rem;
  font-size: 0.95rem;
  color: var(--ink-soft);
}

.result {
  margin-top: 0.7rem;
  padding: 0.5rem 0.7rem;
  border-radius: var(--radius-sm);
  background: var(--ok-soft);
  color: var(--ok);
}

.result.loss {
  background: var(--bad-soft);
  color: var(--bad);
}

/* ---------------------------------------------------------------- picker */
/* The account combobox. `.combo` is the positioning context and the input
   fills it, so the list can be exactly as wide as the control it drops from
   whether that control sits in a narrow table cell or a full-width form row. */
.combo {
  position: relative;
  width: 100%;
}

.combo-input {
  width: 100%;
}

.combo-list {
  position: absolute;
  z-index: 20;
  top: calc(100% + 2px);
  left: 0;
  right: 0;
  max-height: 15rem;
  overflow-y: auto;
  margin: 0;
  padding: 0.2rem;
  list-style: none;
  background: var(--surface);
  border: 1px solid var(--line-strong);
  border-radius: var(--radius-sm);
  box-shadow: var(--shadow);
}

.combo-option {
  padding: 0.32rem 0.45rem;
  border-radius: var(--radius-sm);
  cursor: pointer;
  /* Long account names must wrap rather than force the dropdown wider than
     the column, which would push the rest of the table around. */
  overflow-wrap: anywhere;
}

.combo-option:hover {
  background: var(--brand-soft);
}

.combo-option[aria-selected='true'] {
  background: var(--brand-soft);
  color: var(--brand);
  font-weight: 600;
}

.combo-none {
  padding: 0.32rem 0.45rem;
  color: var(--ink-muted);
  cursor: default;
}

/* The matched run, so the eye lands on why an account was offered. */
.combo-hit {
  background: var(--brand-soft);
  color: var(--brand);
  font-weight: 700;
  border-radius: 2px;
}

/* Announced to assistive tech, never shown. */
.sr-only {
  position: absolute;
  width: 1px;
  height: 1px;
  padding: 0;
  margin: -1px;
  overflow: hidden;
  clip: rect(0 0 0 0);
  white-space: nowrap;
  border: 0;
}

/* -------------------------------------------------------------- messages */
.banner {
  padding: 0.55rem 0.8rem;
  margin: 0 0 0.9rem;
  border: 1px solid var(--line-strong);
  border-left-width: 4px;
  border-radius: var(--radius-sm);
  background: var(--surface);
}

.banner-info {
  border-left-color: var(--brand);
  background: var(--brand-soft);
}

.banner-ok {
  border-left-color: var(--ok);
  background: var(--ok-soft);
  color: var(--ok);
}

.banner-warn {
  border-left-color: var(--warn);
  background: var(--warn-soft);
  color: var(--warn);
}

.banner-error {
  border-left-color: var(--bad);
  background: var(--bad-soft);
  color: var(--bad);
}

.issues {
  margin: 0.4rem 0 0.7rem;
  padding-left: 1.1rem;
  font-size: 0.9rem;
}

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

/* An amount the currency cannot express: flagged in place, never rounded away. */
.table td.num.is-invalid input {
  outline: 2px solid var(--bad);
  outline-offset: 1px;
}

.amount-error {
  display: block;
  color: var(--bad);
  font-size: 0.78rem;
  line-height: 1.25;
  margin-top: 0.15rem;
  text-align: right;
}

.issue-warn {
  color: var(--warn);
}

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

.small {
  font-size: 0.85rem;
}

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

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

.empty {
  padding: 1.5rem 1rem;
  text-align: center;
  background: var(--surface);
  border: 1px dashed var(--line-strong);
  border-radius: var(--radius);
}

.empty-actions {
  justify-content: center;
}

.list {
  display: flex;
  flex-direction: column;
  gap: 0.6rem;
}

.sample {
  padding: 0.6rem 0.7rem;
  overflow-x: auto;
  font-family: var(--mono);
  font-size: 0.85rem;
  background: var(--paper);
  border: 1px solid var(--line);
  border-radius: var(--radius-sm);
}

.diff-block {
  margin-bottom: 0.9rem;
}

.stack {
  max-height: 16rem;
  overflow: auto;
  padding: 0.6rem;
  font-size: 0.8rem;
  background: var(--paper);
  border: 1px solid var(--line);
  border-radius: var(--radius-sm);
}

@media print {
  .app-header,
  .nav,
  .app-footer,
  .form-actions,
  .page-actions,
  .skip-link {
    display: none;
  }

  .body {
    grid-template-columns: 1fr;
  }

  .card {
    box-shadow: none;
    break-inside: avoid;
  }
}
