/* Family Spending Review — a household ledger, not a dashboard.
   Palette, type and layout follow docs/design-direction.md exactly. */

:root {
  --paper: #EFF2EE;
  --ink: #16211C;
  --rule: #C9D2CA;
  --ledger: #1F4E5F;
  --debit: #A8331F;
  --credit: #3F6B4A;

  --ledger-10: rgba(31, 78, 95, 0.10);
  --ledger-20: rgba(31, 78, 95, 0.20);
  --ledger-35: rgba(31, 78, 95, 0.35);
  --ledger-55: rgba(31, 78, 95, 0.55);
  --focus-ring: #2E6E86;

  --font-display: "Bricolage Grotesque", system-ui, sans-serif;
  --font-body: "Source Serif 4", Georgia, serif;
  --font-mono: "IBM Plex Mono", ui-monospace, monospace;

  color-scheme: light;
}

@media (prefers-color-scheme: dark) {
  :root {
    --paper: #111714;
    --ink: #E6EBE5;
    --rule: #2B362F;
    --ledger: #4E93A6;
    --debit: #C24A34;
    --credit: #4F8B5C;

    --ledger-10: rgba(78, 147, 166, 0.14);
    --ledger-20: rgba(78, 147, 166, 0.26);
    --ledger-35: rgba(78, 147, 166, 0.42);
    --ledger-55: rgba(78, 147, 166, 0.62);
    --focus-ring: #7EC0D4;

    color-scheme: dark;
  }
}

* {
  box-sizing: border-box;
}

html {
  background: var(--paper);
}

body {
  margin: 0;
  background: var(--paper);
  color: var(--ink);
  font-family: var(--font-body);
  font-size: 15px;
  line-height: 1.6;
  -webkit-font-smoothing: antialiased;
}

h1, h2, h3, h4 {
  font-family: var(--font-display);
  font-weight: 700;
  letter-spacing: -0.01em;
  line-height: 1.15;
  margin: 0 0 0.5em;
  color: var(--ink);
}

h1 { font-weight: 800; }

p {
  margin: 0 0 1em;
  max-width: 68ch;
}

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

/* Money and every figure, without exception, is monospaced with tabular
   figures so columns align the way a statement's do. */
.num,
table td.num,
.figure,
input[type="text"].num {
  font-family: var(--font-mono);
  font-variant-numeric: tabular-nums;
}

button, input, select, textarea {
  font-family: inherit;
  color: inherit;
}

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

/* The `hidden` attribute must win over any element's own `display` rule
   (flex, grid, etc.) — an author stylesheet rule of equal specificity to
   the UA's `[hidden]{display:none}` otherwise silently overrides it. */
[hidden] {
  display: none !important;
}

hr {
  border: none;
  border-top: 1px solid var(--rule);
  margin: 1.5em 0;
}

/* ---------- Reduced motion ---------- */
@media (prefers-reduced-motion: reduce) {
  * {
    animation-duration: 0.001ms !important;
    animation-iteration-count: 1 !important;
    transition-duration: 0.001ms !important;
  }
}

/* ---------- Password gate ---------- */

#gate {
  min-height: 100vh;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 24px;
}

#gate[hidden] {
  display: none;
}

.gate-card {
  width: 100%;
  max-width: 360px;
  border: 1px solid var(--rule);
  padding: 32px 28px;
  background: var(--paper);
}

.gate-card h1 {
  font-size: 28px;
  margin-bottom: 0.3em;
}

.gate-card p {
  font-size: 15px;
  color: var(--ink);
  opacity: 0.75;
}

.field {
  margin: 20px 0 8px;
}

.field label {
  display: block;
  font-size: 12px;
  letter-spacing: 0.06em;
  margin-bottom: 6px;
  opacity: 0.7;
}

.field input {
  width: 100%;
  padding: 10px 12px;
  border: 1px solid var(--rule);
  background: transparent;
  color: var(--ink);
  font-size: 15px;
  font-family: var(--font-mono);
}

.btn {
  display: inline-block;
  border: 1px solid var(--ledger);
  background: var(--ledger);
  color: var(--paper);
  padding: 10px 18px;
  font-family: var(--font-display);
  font-weight: 700;
  font-size: 14px;
  cursor: pointer;
  text-align: center;
}

.btn:hover {
  opacity: 0.9;
}

.btn-quiet {
  background: transparent;
  color: var(--ink);
  border: 1px solid var(--rule);
  font-weight: 700;
}

.gate-error {
  color: var(--debit);
  font-size: 13px;
  margin-top: 10px;
  min-height: 1.2em;
}

/* ---------- App shell ---------- */

#app {
  min-height: 100vh;
}

.app-header {
  position: sticky;
  top: 0;
  z-index: 20;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 16px;
  padding: 14px 20px;
  background: var(--paper);
  border-bottom: 1px solid var(--rule);
}

.app-header .brand {
  font-family: var(--font-display);
  font-weight: 800;
  font-size: 17px;
  letter-spacing: -0.01em;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.header-actions {
  display: flex;
  align-items: center;
  gap: 14px;
}

.tabs {
  display: flex;
  gap: 4px;
}

.tab-btn {
  background: transparent;
  border: 1px solid var(--rule);
  color: var(--ink);
  padding: 7px 14px;
  font-family: var(--font-display);
  font-weight: 700;
  font-size: 13px;
  cursor: pointer;
}

.tab-btn[aria-selected="true"] {
  background: var(--ledger);
  border-color: var(--ledger);
  color: var(--paper);
}

/* ---------- Layout: rail + column ---------- */

.page-layout {
  display: flex;
  align-items: flex-start;
}

.report-root {
  display: flex;
  width: 100%;
  align-items: flex-start;
}

/* The spine: the signature element. A vertical bar of the whole year,
   segmented to true scale, fixed while the reader scrolls. */
.spine {
  flex: 0 0 84px;
  position: sticky;
  top: 61px;
  height: calc(100vh - 61px);
  display: flex;
  flex-direction: column;
  padding: 20px 10px;
  border-right: 1px solid var(--rule);
}

.spine-total {
  font-family: var(--font-mono);
  font-size: 11px;
  text-align: center;
  opacity: 0.65;
  margin-bottom: 10px;
}

.spine-bar {
  flex: 1;
  display: flex;
  flex-direction: column;
  border: 1px solid var(--rule);
  overflow: hidden;
}

.spine-segment {
  width: 100%;
  min-width: 0;
  min-height: 0;
  overflow: hidden;
  background: var(--ledger-20);
  border-bottom: 1px solid var(--paper);
  cursor: pointer;
  transform-origin: top;
  transition: background 0.15s ease;
  padding: 0;
  display: block;
  text-align: left;
}

.spine-segment:last-child {
  border-bottom: none;
}

.spine-segment:hover,
.spine-segment:focus-visible {
  background: var(--ledger-35);
}

.spine-segment[data-active="true"] {
  background: var(--ledger);
}

.spine-segment .seg-label {
  display: block;
  writing-mode: vertical-rl;
  transform: rotate(180deg);
  font-family: var(--font-mono);
  font-size: 10px;
  padding: 6px 3px;
  color: var(--ink);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  max-height: 100%;
}

.spine-segment[data-active="true"] .seg-label {
  color: var(--paper);
}

@keyframes spine-draw {
  from { transform: scaleY(0); }
  to { transform: scaleY(1); }
}

.spine-bar.animate .spine-segment {
  animation: spine-draw 700ms ease-out both;
}

@media (prefers-reduced-motion: reduce) {
  .spine-bar.animate .spine-segment {
    animation: none;
  }
}

.report-column {
  flex: 1;
  min-width: 0;
  padding: 28px 24px 96px;
  max-width: 880px;
}

@media (max-width: 899px) {
  .report-root {
    flex-direction: column;
    /* Stacked, the cross axis is horizontal: align-items:flex-start (set for
       the desktop side-by-side layout) would let report-column shrink-to-fit
       its widest content instead of the viewport, which is exactly what
       causes the page body to scroll horizontally. Stretch it back to full
       width so only the chart/table containers scroll, in their own box. */
    align-items: stretch;
  }
  .spine {
    position: sticky;
    top: 53px;
    z-index: 10;
    width: 100%;
    height: 56px;
    flex-direction: row;
    padding: 8px 12px;
    border-right: none;
    border-bottom: 1px solid var(--rule);
    background: var(--paper);
    align-items: center;
  }
  .spine-total {
    display: none;
  }
  .spine-bar {
    flex-direction: row;
    height: 100%;
    width: 100%;
  }
  .spine-segment {
    border-bottom: none;
    border-right: 1px solid var(--paper);
  }
  .spine-segment:last-child {
    border-right: none;
  }
  .spine-segment .seg-label {
    writing-mode: horizontal-tb;
    transform: none;
    font-size: 9px;
    padding: 2px;
    text-align: center;
  }
  @keyframes spine-draw-h {
    from { transform: scaleX(0); }
    to { transform: scaleX(1); }
  }
  .spine-bar.animate .spine-segment {
    animation-name: spine-draw-h;
    transform-origin: left;
  }
  .report-column {
    padding: 20px 16px 72px;
  }
}

@media (max-width: 374px) {
  .report-column { padding: 16px 12px 64px; }
}

/* ---------- Prose blocks ---------- */

.lede {
  font-size: 19px;
  line-height: 1.5;
}

section.report-section {
  margin: 56px 0;
  scroll-margin-top: 80px;
}

section.report-section:first-of-type {
  margin-top: 0;
}

.section-head {
  font-size: 28px;
  margin-bottom: 0.6em;
}

@media (max-width: 599px) {
  .section-head { font-size: 22px; }
}

.section-intro {
  opacity: 0.9;
}

.caption {
  font-family: var(--font-mono);
  font-size: 12px;
  opacity: 0.7;
  line-height: 1.5;
}

.evidence {
  font-family: var(--font-body);
  font-size: 15px;
}

/* ---------- Summary box ---------- */

.summary-box {
  border: 1px solid var(--rule);
  padding: 24px 22px;
  margin-bottom: 8px;
}

.summary-grid {
  display: grid;
  grid-template-columns: repeat(2, 1fr);
  gap: 20px 24px;
}

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

.summary-item .summary-value {
  font-family: var(--font-mono);
  font-variant-numeric: tabular-nums;
  font-size: 36px;
  font-weight: 500;
  color: var(--ledger);
  display: block;
  line-height: 1.1;
}

@media (max-width: 599px) {
  .summary-item .summary-value { font-size: 28px; }
}

.summary-item .summary-label {
  font-family: var(--font-display);
  font-weight: 700;
  font-size: 13px;
  letter-spacing: 0.04em;
  margin-top: 6px;
  display: block;
}

.summary-item .summary-gloss {
  font-size: 13px;
  opacity: 0.75;
  margin-top: 4px;
  line-height: 1.45;
}

.summary-item .summary-value.over { color: var(--debit); }
.summary-item .summary-value.under { color: var(--credit); }

/* ---------- Tables ---------- */

.table-scroll {
  overflow-x: auto;
  border: 1px solid var(--rule);
}

table.ledger {
  width: 100%;
  border-collapse: collapse;
  font-size: 14px;
  min-width: 480px;
}

table.ledger caption {
  text-align: left;
  padding: 10px 12px;
  font-family: var(--font-display);
  font-weight: 700;
  font-size: 13px;
  border-bottom: 1px solid var(--rule);
}

table.ledger th,
table.ledger td {
  padding: 9px 12px;
  border-bottom: 1px solid var(--rule);
  text-align: left;
  vertical-align: top;
}

table.ledger thead th {
  font-family: var(--font-display);
  font-weight: 700;
  font-size: 11px;
  letter-spacing: 0.04em;
  opacity: 0.7;
  border-bottom: 1px solid var(--ink);
}

table.ledger td.num,
table.ledger th.num {
  text-align: right;
  font-family: var(--font-mono);
  font-variant-numeric: tabular-nums;
  white-space: nowrap;
}

table.ledger tbody tr:last-child td {
  border-bottom: none;
}

table.ledger tbody tr:hover {
  background: var(--ledger-10);
}

.value-over { color: var(--debit); }
.value-under { color: var(--credit); }

.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;
}

/* ---------- Charts ---------- */

.chart-wrap {
  position: relative;
  border: 1px solid var(--rule);
  padding: 16px 12px 8px;
  overflow-x: auto;
}

.chart-wrap svg {
  display: block;
  min-width: 420px;
  width: 100%;
  height: auto;
  overflow: visible;
}

.chart-legend {
  display: flex;
  flex-wrap: wrap;
  gap: 14px;
  font-family: var(--font-mono);
  font-size: 11px;
  padding: 8px 4px 0;
  opacity: 0.85;
}

.chart-legend .key {
  display: inline-flex;
  align-items: center;
  gap: 6px;
}

.chart-legend .swatch {
  width: 14px;
  height: 2px;
  display: inline-block;
}

.tooltip {
  position: fixed;
  pointer-events: none;
  background: var(--ink);
  color: var(--paper);
  font-family: var(--font-mono);
  font-size: 12px;
  padding: 6px 9px;
  max-width: 240px;
  line-height: 1.4;
  z-index: 100;
  opacity: 0;
  transition: opacity 0.1s ease;
}

.tooltip.visible {
  opacity: 1;
}

.bar-mark {
  fill: var(--ledger);
  cursor: pointer;
}

.bar-mark.partial {
  fill: var(--ledger-35);
}

.bar-mark:hover,
.bar-mark:focus-visible {
  fill: var(--ledger-55);
}

.bar-mark.housing {
  fill: var(--ink);
  opacity: 0.85;
}

.line-mark {
  fill: none;
  stroke: var(--ledger);
  stroke-width: 2;
  stroke-linejoin: round;
  stroke-linecap: round;
}

.mean-line {
  stroke: var(--ink);
  stroke-width: 1;
  stroke-dasharray: 3 3;
  opacity: 0.55;
}

.dot-mark {
  fill: var(--ledger);
  stroke: var(--paper);
  stroke-width: 2;
  cursor: pointer;
}

.dot-mark.partial {
  fill: var(--paper);
  stroke: var(--ledger);
  stroke-width: 2;
}

.axis-text {
  font-family: var(--font-mono);
  font-size: 10px;
  fill: var(--ink);
  opacity: 0.65;
}

.chart-value-label {
  font-family: var(--font-mono);
  font-size: 11px;
  fill: var(--ink);
}

/* ---------- Benchmark comparisons ---------- */

.bench-row {
  border: 1px solid var(--rule);
  padding: 14px 16px;
  margin-bottom: 10px;
}

.bench-head {
  display: flex;
  flex-wrap: wrap;
  align-items: baseline;
  justify-content: space-between;
  gap: 8px 16px;
}

.bench-name {
  font-family: var(--font-display);
  font-weight: 700;
  font-size: 15px;
}

.bench-figures {
  font-family: var(--font-mono);
  font-size: 13px;
  display: flex;
  gap: 14px;
  flex-wrap: wrap;
}

.bench-diff.over { color: var(--debit); }
.bench-diff.under { color: var(--credit); }

.bench-source {
  font-family: var(--font-mono);
  font-size: 11.5px;
  opacity: 0.72;
  margin-top: 8px;
  line-height: 1.5;
}

.bench-tag {
  display: inline-block;
  font-family: var(--font-mono);
  font-size: 10px;
  letter-spacing: 0.04em;
  border: 1px solid var(--rule);
  padding: 1px 6px;
  margin-left: 8px;
  opacity: 0.8;
}

.bench-refpoints {
  list-style: none;
  margin: 8px 0 0;
  padding: 0;
  font-family: var(--font-mono);
  font-size: 12.5px;
}

.bench-refpoints li {
  padding: 3px 0;
  border-top: 1px solid var(--rule);
}

.bench-refpoints li:first-child {
  border-top: none;
}

.bench-none {
  font-family: var(--font-mono);
  font-size: 13px;
  opacity: 0.75;
}

details.bench-detail summary {
  cursor: pointer;
  font-size: 12px;
  opacity: 0.75;
  margin-top: 6px;
}

/* ---------- Recommendations ---------- */

.rec-card {
  border: 1px solid var(--rule);
  border-left: 3px solid var(--ledger);
  padding: 14px 16px;
  margin-bottom: 12px;
}

.rec-head {
  display: flex;
  justify-content: space-between;
  align-items: baseline;
  gap: 12px;
  flex-wrap: wrap;
}

.rec-title {
  font-family: var(--font-display);
  font-weight: 700;
  font-size: 16px;
}

.rec-amount {
  font-family: var(--font-mono);
  color: var(--credit);
  font-size: 16px;
  white-space: nowrap;
}

.rec-meta {
  font-family: var(--font-mono);
  font-size: 11px;
  letter-spacing: 0.04em;
  opacity: 0.7;
  margin: 4px 0 8px;
}

.disruption-low { color: var(--credit); }
.disruption-medium { color: var(--ink); }
.disruption-high { color: var(--debit); }

/* ---------- Budget tab ---------- */

.budget-root {
  padding: 28px 24px 96px;
  max-width: 960px;
}

@media (max-width: 899px) {
  .budget-root { padding: 20px 16px 220px; }
}

@media (max-width: 374px) {
  .budget-root { padding: 16px 12px 220px; }
}

table.budget-table {
  min-width: 720px;
}

table.budget-table td:first-child {
  min-width: 180px;
}

.budget-row-note {
  margin: 3px 0 0;
  max-width: 32ch;
}

.budget-input {
  width: 100px;
  padding: 6px 8px;
  border: 1px solid var(--rule);
  background: transparent;
  color: var(--ink);
  font-size: 13px;
  text-align: right;
}

.budget-remove {
  background: transparent;
  border: none;
  color: var(--debit);
  font-family: var(--font-display);
  font-weight: 700;
  font-size: 12px;
  cursor: pointer;
  padding: 4px 6px;
  white-space: nowrap;
}

.budget-add-row {
  display: flex;
  gap: 10px;
  margin: 16px 0 32px;
  flex-wrap: wrap;
}

.budget-add-input {
  flex: 1 1 220px;
  min-width: 0;
  padding: 9px 12px;
  border: 1px solid var(--rule);
  background: transparent;
  color: var(--ink);
  font-size: 14px;
  font-family: var(--font-body);
}

.budget-footer {
  position: sticky;
  bottom: 0;
  border-top: 1px solid var(--rule);
  background: var(--paper);
  padding: 16px 4px;
  margin-top: 8px;
  display: flex;
  flex-direction: column;
  gap: 14px;
}

.budget-footer-stats {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 16px 20px;
}

@media (max-width: 699px) {
  .budget-footer-stats { grid-template-columns: 1fr; gap: 10px; }
}

.budget-stat {
  display: flex;
  flex-direction: column;
  gap: 2px;
}

.budget-stat-label {
  font-family: var(--font-display);
  font-weight: 700;
  font-size: 11px;
  letter-spacing: 0.04em;
  opacity: 0.65;
}

.budget-stat-value {
  font-size: 20px;
}

.budget-footer-controls {
  display: flex;
  align-items: center;
  gap: 14px;
  flex-wrap: wrap;
}

.budget-status {
  font-family: var(--font-mono);
  font-size: 12.5px;
  opacity: 0.8;
}

.budget-status-error {
  color: var(--debit);
  opacity: 1;
}

.budget-status-ok {
  color: var(--credit);
  opacity: 1;
}

.budget-retry {
  font-size: 12px;
  padding: 4px 10px;
}

/* ---------- Chat panel ---------- */

#chat-panel {
  z-index: 30;
  font-size: 13.5px;
}

.chat-toggle-btn {
  display: none;
}

.chat-frame {
  display: flex;
  flex-direction: column;
  background: var(--paper);
}

.chat-close {
  display: none;
}

.chat-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 12px 14px;
  border-bottom: 1px solid var(--rule);
  flex: 0 0 auto;
}

.chat-title {
  font-family: var(--font-display);
  font-weight: 700;
  font-size: 14px;
}

.chat-messages {
  flex: 1 1 auto;
  overflow-y: auto;
  padding: 12px 14px;
  display: flex;
  flex-direction: column;
  gap: 10px;
  min-height: 0;
}

.chat-bubble {
  padding: 8px 11px;
  border: 1px solid var(--rule);
  font-size: 13.5px;
  line-height: 1.5;
  max-width: 92%;
  white-space: pre-wrap;
}

.chat-user {
  align-self: flex-end;
  background: var(--ledger-10);
  border-color: var(--ledger-20);
}

.chat-assistant {
  align-self: flex-start;
  background: transparent;
}

.chat-chips {
  display: flex;
  flex-direction: column;
  gap: 6px;
  padding: 0 14px 12px;
  flex: 0 0 auto;
}

.chat-chip {
  text-align: left;
  border: 1px solid var(--rule);
  background: transparent;
  color: var(--ink);
  padding: 8px 10px;
  font-family: var(--font-body);
  font-size: 13px;
  cursor: pointer;
}

.chat-chip:hover,
.chat-chip:focus-visible {
  background: var(--ledger-10);
  border-color: var(--ledger-35);
}

.chat-error {
  margin: 0 14px 10px;
  padding: 8px 10px;
  border: 1px solid var(--debit);
  color: var(--debit);
  font-size: 12.5px;
  line-height: 1.5;
  flex: 0 0 auto;
}

.chat-input-row {
  display: flex;
  gap: 8px;
  padding: 10px 12px;
  border-top: 1px solid var(--rule);
  flex: 0 0 auto;
}

.chat-input {
  flex: 1;
  resize: none;
  min-height: 36px;
  max-height: 120px;
  padding: 8px 10px;
  border: 1px solid var(--rule);
  background: transparent;
  color: var(--ink);
  font-family: var(--font-body);
  font-size: 13.5px;
  line-height: 1.4;
}

.chat-send {
  align-self: flex-end;
  padding: 8px 14px;
  font-size: 13px;
}

@media (min-width: 900px) {
  #chat-panel {
    position: fixed;
    top: 61px;
    right: 0;
    bottom: 0;
    width: 340px;
    border-left: 1px solid var(--rule);
    background: var(--paper);
  }
  .chat-frame {
    height: 100%;
  }
  .page-layout {
    margin-right: 340px;
  }
}

@media (max-width: 899px) {
  #chat-panel {
    position: fixed;
    right: 16px;
    bottom: 16px;
    left: auto;
    top: auto;
    width: auto;
  }
  .chat-toggle-btn {
    display: inline-block;
    border: 1px solid var(--ledger);
    background: var(--ledger);
    color: var(--paper);
    padding: 12px 18px;
    font-family: var(--font-display);
    font-weight: 700;
    font-size: 14px;
    cursor: pointer;
  }
  .chat-frame {
    display: none;
  }
  .chat-close {
    display: inline-block;
    font-size: 20px;
    line-height: 1;
    padding: 2px 8px;
  }
  #chat-panel.chat-open {
    left: 16px;
    right: 16px;
    top: 64px;
    bottom: 16px;
    width: auto;
    border: 1px solid var(--rule);
  }
  #chat-panel.chat-open .chat-toggle-btn {
    display: none;
  }
  #chat-panel.chat-open .chat-frame {
    display: flex;
    height: 100%;
  }
}

.spending-bands {
  display: flex;
  gap: 22px;
  flex-wrap: wrap;
  margin: 4px 0 20px;
  font-family: var(--font-mono);
  font-size: 13px;
}

.spending-bands > div {
  flex: 1 1 200px;
  min-width: 180px;
}

.spending-bands strong {
  display: block;
  font-family: var(--font-display);
  font-size: 11px;
  letter-spacing: 0.04em;
  opacity: 0.65;
  margin-bottom: 2px;
}

.spending-bands .num {
  font-size: 18px;
  color: var(--ledger);
}

.band-gloss {
  font-family: var(--font-body);
  font-size: 12.5px;
  opacity: 0.75;
  margin: 4px 0 0;
  line-height: 1.45;
  max-width: none;
}
