/* Shared component vocabulary. Modules may add layout; they never redefine
 * these, and they never name a colour — only var(--accent), which a row state
 * rebinds locally. That is what lets one row component serve a subscription, an
 * envelope and a spending entry without a single branch. */

/* ── The ledger ──────────────────────────────────────────────────────────
   A money app is a column of figures that must align. Every amount is
   right-aligned on a shared axis in tabular figures, so scanning down any list
   the digits stack. Rows are separated by hairlines, not boxes. */
.ledger { border-top: 1px solid var(--rule); }

.row {
  display: grid;
  grid-template-columns: 1fr auto;
  grid-template-areas: "title amount" "meta amount";
  column-gap: var(--s-4);
  align-items: baseline;
  padding: var(--s-3) var(--s-2);
  border-bottom: 1px solid var(--rule);
  position: relative;
  isolation: isolate;
}

.row-title {
  grid-area: title;
  font-size: var(--t-lead);
  font-weight: 500;
  color: var(--ink);
}
.row-meta {
  grid-area: meta;
  font-size: var(--t-small);
  color: var(--ink-3);
}
.row-amount {
  grid-area: amount;
  font-size: var(--t-lead);
  font-weight: 500;
  text-align: right;
  white-space: nowrap;
  color: var(--ink);
}
.row-amount.is-signed { color: var(--accent); }

.row-actions {
  grid-area: amount;
  grid-row: 2;
  display: flex;
  gap: var(--s-1);
  justify-content: flex-end;
  margin-top: var(--s-1);
}

/* State rebinds --accent locally; everything inside follows. */
.row.is-overdue { --accent: var(--out); --accent-dim: var(--out-dim); }
.row.is-due     { --accent: var(--due); --accent-dim: var(--due-dim); }
.row.is-funded  { --accent: var(--in);  --accent-dim: var(--in-dim); }

/* Wide screens: same <ul>, same DOM, one render pass — it just becomes a table. */
.ledger-head { display: none; }
@media (min-width: 720px) {
  .ledger-head {
    display: grid;
    grid-template-columns: var(--cols, 2fr 1fr 1fr auto);
    gap: var(--s-4);
    padding: var(--s-2);
    font-size: var(--t-label);
    font-weight: 500;
    color: var(--ink-3);
    border-bottom: 1px solid var(--rule-firm);
  }
  .ledger-head > :last-child { text-align: right; }
}

/* ── Tiles ───────────────────────────────────────────────────────────────── */
.tiles {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(140px, 1fr));
  gap: var(--s-3);
  margin-bottom: var(--s-5);
}
.tile { padding: var(--s-3) 0; border-top: 1px solid var(--rule); }
.tile-label { font-size: var(--t-label); color: var(--ink-3); }
.tile-value {
  font-size: var(--t-h2);
  font-weight: 500;
  margin-top: 2px;
}

/* ── Icons ───────────────────────────────────────────────────────────────
   Sizing lives here, never in the markup, so an icon tracks the type size of
   whatever it sits in. flex-shrink:0 because a long label must not squash the
   glyph into an ellipse. */
.ico, .btn-icon, .nav-icon {
  display: block;
  flex-shrink: 0;
  width: 1em;
  height: 1em;
}
.ico { width: 18px; height: 18px; }
.nav-icon { width: 22px; height: 22px; }
/* 1.15em rather than a fixed px: .btn and .btn--sm differ in font-size, and the
   icon should differ with them. */
.btn-icon { width: 1.15em; height: 1.15em; }

/* ── Buttons ─────────────────────────────────────────────────────────────── */
.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: var(--s-2);
  min-height: 40px;
  padding: 0 var(--s-4);
  border-radius: var(--r-2);
  border: 1px solid transparent;
  background: none;
  font-size: var(--t-small);
  font-weight: 500;
  white-space: nowrap;     /* "Mark paid" must not wrap to two lines */
  text-decoration: none;   /* .btn is used on <a> as well as <button> */
  transition: background-color 120ms var(--ease), border-color 120ms var(--ease);
}
@media (hover: none) { .btn { min-height: 44px; } }

.btn--solid { background: var(--accent); color: var(--on-accent); }
.btn--solid:hover { filter: brightness(1.08); }

.btn--quiet { background: var(--surface-2); color: var(--ink); }
.btn--quiet:hover { background: var(--rule); }

.btn--ghost { border-color: var(--rule-firm); color: var(--ink-2); }
.btn--ghost:hover { color: var(--ink); border-color: var(--ink-3); }

.btn--danger { background: var(--out-dim); color: var(--out); }

.btn--sm { min-height: 32px; padding: 0 var(--s-3); font-size: var(--t-label); }

/* Square, for a button whose icon IS the label. Keeps a 40px (44px touch)
   target rather than collapsing to the glyph's width. */
.btn--icon { padding: 0; min-width: 40px; }
.btn--sm.btn--icon { min-width: 34px; }
@media (hover: none) { .btn--sm.btn--icon { min-width: 40px; } }
.btn--icon .btn-icon { width: 17px; height: 17px; }
@media (hover: none) { .btn--sm { min-height: 36px; } }

.btn[disabled] { opacity: 0.45; pointer-events: none; }

/* ── Fields ──────────────────────────────────────────────────────────────── */
.field { margin-bottom: var(--s-4); }
.field-label {
  display: block;
  font-size: var(--t-label);
  font-weight: 500;
  color: var(--ink-3);
  margin-bottom: var(--s-1);
}
.field input, .field select, .field textarea {
  width: 100%;
  min-height: 44px;
  padding: var(--s-2) var(--s-3);
  font-size: var(--t-input);   /* 16px minimum, or iOS Safari zooms on focus */
  background: var(--surface);
  color: var(--ink);
  border: 1px solid var(--rule-firm);
  border-radius: var(--r-1);
}
.field input:focus, .field select:focus { border-color: var(--accent); }
.field-hint { font-size: var(--t-label); color: var(--ink-3); margin-top: var(--s-1); }
.field-error { font-size: var(--t-label); color: var(--out); margin-top: var(--s-1); }
.field-row { display: flex; gap: var(--s-3); }
.field-row > * { flex: 1; }

/* ── Sheet / dialog ──────────────────────────────────────────────────────
   One implementation, one visibility convention: [hidden]. The two apps this
   replaces used opposite conventions (.open to show vs .hidden to hide). */
.sheet {
  position: fixed;
  inset: 0;
  z-index: 60;
  background: var(--overlay);
  display: flex;
  align-items: flex-end;
  justify-content: center;
}
.sheet-panel {
  background: var(--surface);
  width: 100%;
  max-width: 480px;
  max-height: 92vh;
  overflow-y: auto;
  border-radius: var(--r-3) var(--r-3) 0 0;
  box-shadow: var(--shadow-sheet);
  padding: var(--s-5) var(--s-4);
  padding-bottom: calc(var(--s-5) + var(--safe-bottom));
  animation: sheet-up 180ms var(--ease);
}
@keyframes sheet-up { from { transform: translateY(14px); opacity: 0; } to { transform: none; opacity: 1; } }

@media (min-width: 641px) {
  .sheet { align-items: center; }
  .sheet-panel {
    border-radius: var(--r-3);
    padding-bottom: var(--s-5);
    animation: none;
  }
}

.sheet-title { font-size: var(--t-h2); font-weight: 600; margin-bottom: var(--s-4); }
.sheet-actions {
  display: flex;
  gap: var(--s-2);
  justify-content: flex-end;
  margin-top: var(--s-5);
}

/* ── Tags ────────────────────────────────────────────────────────────────── */
.tag {
  display: inline-block;
  padding: 2px var(--s-2);
  border-radius: var(--r-1);
  font-size: var(--t-micro);
  font-weight: 500;
  white-space: nowrap;
  background: var(--accent-dim);
  color: var(--accent);
}

/* State tags name the semantic token directly rather than rebinding --accent:
   a tag is the one place the colour IS the content, so there is nothing
   downstream of it to inherit. */
.tag--ok      { background: var(--in-dim);  color: var(--in); }
.tag--due     { background: var(--due-dim); color: var(--due); }
.tag--overdue { background: var(--out-dim); color: var(--out); }

/* ── Empty state: an invitation, not a statement ─────────────────────────── */
.empty {
  padding: var(--s-7) var(--s-4);
  text-align: left;
  max-width: 420px;
}
.empty-title { font-size: var(--t-lead); font-weight: 500; margin-bottom: var(--s-2); }
.empty-body { color: var(--ink-2); font-size: var(--t-small); margin-bottom: var(--s-4); }

/* ── Menu ────────────────────────────────────────────────────────────────── */
.menu {
  position: absolute;
  min-width: 190px;
  background: var(--surface);
  border: 1px solid var(--rule);
  border-radius: var(--r-2);
  box-shadow: var(--shadow-menu);
  padding: var(--s-1);
  z-index: 70;
}
.menu button {
  display: block;
  width: 100%;
  text-align: left;
  padding: var(--s-2) var(--s-3);
  background: none;
  border: 0;
  border-radius: var(--r-1);
  font-size: var(--t-small);
}
.menu button:hover { background: var(--surface-2); }

/* ── Bars (category breakdowns) ──────────────────────────────────────────── */
.bars { display: flex; flex-direction: column; gap: var(--s-2); }
.bar { display: grid; grid-template-columns: 1fr 2fr auto; gap: var(--s-3); align-items: center; }
.bar-label { font-size: var(--t-small); color: var(--ink-2); }
.bar-track { height: 12px; background: var(--surface-2); border-radius: var(--r-full); overflow: hidden; }
/* display:block is load-bearing — .bar-fill is an <i>, and width/height are
   ignored on an inline box, which rendered every bar invisible. */
.bar-fill {
  display: block;
  height: 100%;
  width: 0;
  background: var(--accent);
  border-radius: var(--r-full);
  transition: width 320ms var(--ease);
}
.bar-value { font-size: var(--t-small); text-align: right; white-space: nowrap; }

/* On a phone the label and figure share a line and the track spans beneath,
   rather than squeezing three columns into 320px. */
@media (max-width: 559px) {
  .bar { grid-template-columns: 1fr auto; }
  .bar-label { order: 1; }
  .bar-value { order: 2; }
  .bar-track { order: 3; grid-column: 1 / -1; }
}

/* ── Sections and panels ─────────────────────────────────────────────────
   Used by every module, so they live here rather than in a module stylesheet.
   They were in subscriptions.css, which is loaded lazily — landing straight on
   #/spending (the PWA's start_url) rendered every section heading unstyled. */
.section { margin-top: var(--s-6); }
.section-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: var(--s-3);
  margin-bottom: var(--s-3);
}
.section-title {
  font-size: var(--t-lead);
  font-weight: 500;
  margin-bottom: var(--s-3);
}
.section-head .section-title { margin-bottom: 0; }

/* Collapsible: the whole heading is the control, so the hit target is the
   width of the words rather than a lone chevron. */
.section-toggle {
  display: flex;
  align-items: center;
  gap: var(--s-2);
  background: none;
  border: 0;
  padding: var(--s-1) 0;
  color: var(--ink);
  text-align: left;
}
/* The icon set's own chevron, rotated rather than swapped, so the two states
   are one drawing and the transition has something to animate. */
.section-chevron {
  display: flex;
  color: var(--ink-2);
  transition: transform 160ms var(--ease);
}
.section-toggle[aria-expanded="false"] .section-chevron { transform: rotate(-90deg); }
.section-count {
  font-size: var(--t-label);
  color: var(--ink-3);
  background: var(--surface-2);
  border-radius: var(--r-full);
  padding: 1px var(--s-2);
}

.panel {
  background: var(--surface);
  border: 1px solid var(--rule);
  border-radius: var(--r-3);
  padding: var(--s-4);
}
.panel--flush { padding: 0; overflow: hidden; }

/* ── Toolbar controls ────────────────────────────────────────────────────── */
.inline-select {
  display: inline-flex;
  align-items: center;
  gap: var(--s-2);
}
.inline-select select {
  min-height: 40px;
  padding: 0 var(--s-2);
  font-size: var(--t-small);
  background: var(--surface);
  color: var(--ink);
  border: 1px solid var(--rule-firm);
  border-radius: var(--r-1);
}

.fx-status {
  font-size: var(--t-label);
  color: var(--ink-3);
  align-self: center;
}

/* ── Toast ───────────────────────────────────────────────────────────────── */
.toast-host {
  position: fixed;
  left: 50%;
  bottom: calc(var(--bar-h) + var(--safe-bottom) + var(--s-4));
  transform: translateX(-50%);
  /* Above the login and boot overlays (100): a toast is transient feedback and
     belongs on top of whatever triggered it. */
  z-index: 120;
  display: flex;
  flex-direction: column;
  gap: var(--s-2);
  pointer-events: none;
}
@media (min-width: 900px) {
  .toast-host { bottom: var(--s-5); left: calc(var(--rail-w) + var(--s-5)); transform: none; }
}
.toast {
  background: var(--ink);
  color: var(--canvas);
  padding: var(--s-2) var(--s-4);
  border-radius: var(--r-2);
  font-size: var(--t-small);
  box-shadow: var(--shadow-menu);
}
.toast.is-error { background: var(--out); color: #fff; }
