:root {
  --bg: #0a0a0f;
  --text-primary: #e8e8e8;
  --text-secondary: #8a8a9a;
  --grid-line: #1a1a2e;
  --hover-glow: rgba(255, 255, 255, 0.15);

  --c-military: #c0392b;
  --c-veterans: #8e44ad;
  --c-homeland: #e67e22;
  --c-civilian: #2980b9;
  --c-science: #16a085;
  --c-international: #f39c12;
  --c-mandatory: #7f8c8d;
  --c-debt: #e74c3c;

  --font-head: "Space Grotesk", "Inter", sans-serif;
  --font-mono: "JetBrains Mono", "Roboto Mono", monospace;
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  padding: 0;
  background: var(--bg);
  color: var(--text-primary);
  font-family: var(--font-head);
  height: 100%;
  overflow: hidden;
}

/* ---------- Left rail (spec Section 14) ---------- */

#left-rail {
  position: fixed;
  left: 0;
  top: 0;
  width: 286px;
  height: 100vh;
  background: #0d0d14;
  border-right: 1px solid var(--grid-line);
  display: flex;
  flex-direction: column;
  padding: 22px 18px;
  gap: 14px;
  overflow-y: auto;
  overflow-x: hidden;
  z-index: 100;
}

.rail-sep {
  border: none;
  border-top: 1px solid var(--grid-line);
  margin: 12px 0 0;
}

.rail-label {
  font-family: var(--font-mono);
  font-size: 9px;
  letter-spacing: 0.1em;
  color: var(--text-secondary);
}

/* National debt label: centered above the counter */
#debt-clock .rail-label {
  text-align: center;
}

/* 1. Title block */

.rail-title-main {
  font-family: var(--font-head);
  font-weight: 700;
  font-size: 22px;
  color: var(--text-primary);
  line-height: 1.1;
}

.rail-title-year {
  font-family: var(--font-head);
  font-weight: 700;
  font-size: 40px;
  color: var(--c-debt);
  line-height: 1.1;
}

.rail-subtitle {
  margin: 8px 0 0;
  font-size: 11px;
  color: var(--text-secondary);
  line-height: 1.4;
  text-wrap: pretty;
}

/* 2. Debt counter block */

.rail-debt-value {
  font-family: var(--font-mono);
  font-size: 19.5px;
  font-weight: 700;
  color: var(--c-debt);
  margin-top: 4px;
  word-break: keep-all;
  overflow-wrap: normal;
  white-space: normal;
  text-align: justify;
  text-align-last: center;
}

.rail-debt-rate,
.rail-debt-crossed {
  font-family: var(--font-mono);
  font-size: 10px;
  color: var(--text-secondary);
  margin-top: 3px;
}

.est-tag { opacity: 0.7; }

/* 3. The Full Picture panel */

.rail-reality-sub {
  font-family: var(--font-mono);
  font-size: 13px;
  color: var(--text-secondary);
  margin-top: 2px;
}

.rail-reality-body {
  display: flex;
  gap: 10px;
  margin-top: 10px;
}

.rail-bar-vertical {
  flex: 0 0 12px;
  width: 12px;
  height: 160px;
  border-radius: 3px;
  overflow: hidden;
  display: flex;
  flex-direction: column;
  background: var(--grid-line);
}

.bar-seg.discretionary {
  background: linear-gradient(180deg, var(--c-military), var(--c-civilian));
}
.bar-seg.debt-service { background: var(--c-debt); }
.bar-seg.mandatory { background: var(--c-mandatory); }

.rail-reality-list {
  display: flex;
  flex-direction: column;
  gap: 10px;
  min-width: 0;
}

.rail-col-label {
  font-family: var(--font-head);
  font-weight: 700;
  font-size: 13px;
  color: var(--text-primary);
  padding-left: 12px;
  position: relative;
}

.rail-col-label::before {
  content: "";
  position: absolute;
  left: 0;
  top: 4px;
  width: 8px;
  height: 8px;
  border-radius: 2px;
}

.mandatory-swatch::before { background: var(--c-mandatory); }
.debt-swatch::before { background: var(--c-debt); }
.discretionary-swatch::before { background: linear-gradient(135deg, var(--c-military), var(--c-civilian)); }

.rail-col-amount {
  display: block;
  font-family: var(--font-mono);
  font-weight: 400;
  font-size: 13px;
  color: var(--text-secondary);
  margin-top: 1px;
}

.rail-badge {
  font-family: var(--font-mono);
  font-size: 13px;
  font-weight: 700;
  color: var(--c-debt);
  margin: 2px 0 0 12px;
}

.rail-col-list {
  margin: 4px 0 0;
  padding-left: 20px;
  font-family: var(--font-mono);
  font-size: 13px;
  color: var(--text-secondary);
  line-height: 1.55;
  text-wrap: pretty;
}

#reality-panel .rail-label {
  font-size: 13px;
}

.rail-reality .rail-footnote {
  font-size: 13px;
}

.rail-footnote {
  margin-top: 8px;
  font-size: 9px;
  color: var(--text-secondary);
  opacity: 0.8;
  line-height: 1.4;
  text-wrap: pretty;
}

/* 4. Editorial callouts */

.rail-callouts .callout {
  font-size: 10px;
  color: var(--text-secondary);
  line-height: 1.4;
  margin-bottom: 8px;
  text-wrap: pretty;
}

.callout-warn { color: var(--c-debt); }

/* 5. Search box */

#search-input {
  width: 100%;
  height: 30px;
  border-radius: 4px;
  background: var(--grid-line);
  border: 1px solid #2a2a3e;
  color: var(--text-primary);
  padding: 0 10px;
  font-family: var(--font-head);
  font-size: 12px;
}

#search-input::placeholder { color: var(--text-secondary); }

/* 6. Category filter pills (stacked vertically) */

.rail-filters {
  display: flex;
  flex-direction: column;
  gap: 6px;
}

.rail-pill {
  display: flex;
  align-items: center;
  gap: 8px;
  width: 100%;
  height: 26px;
  padding: 0 8px;
  border-radius: 4px;
  background: transparent;
  border: 1px solid var(--pill-color, var(--grid-line));
  color: var(--pill-color, var(--text-secondary));
  font-family: var(--font-head);
  font-size: 11px;
  cursor: pointer;
  opacity: 0.65;
}

.rail-pill .dot {
  width: 6px;
  height: 6px;
  border-radius: 50%;
  background: var(--pill-color, var(--text-secondary));
  flex-shrink: 0;
}

.rail-pill-military { --pill-color: var(--c-military); }
.rail-pill-veterans { --pill-color: var(--c-veterans); }
.rail-pill-homeland { --pill-color: var(--c-homeland); }
.rail-pill-civilian { --pill-color: var(--c-civilian); }
.rail-pill-science { --pill-color: var(--c-science); }
.rail-pill-international { --pill-color: var(--c-international); }
.rail-pill-all { --pill-color: var(--text-primary); }

.rail-pill.active {
  opacity: 1;
  background: color-mix(in srgb, var(--pill-color, var(--text-secondary)) 20%, transparent);
  border-color: var(--pill-color, var(--text-primary));
}

/* 7. View toggle */

.rail-view-toggle {
  display: flex;
  gap: 4px;
}

.view-btn {
  flex: 1;
  font-family: var(--font-head);
  font-size: 11px;
  border: 1px solid #2a2a3e;
  background: transparent;
  color: var(--text-secondary);
  border-radius: 4px;
  padding: 7px 0;
  cursor: pointer;
}

.view-btn.active {
  background: var(--grid-line);
  border-color: #2a2a3e;
  color: var(--text-primary);
}

/* Full-picture accounting toggle */
.reality-toggle {
  display: flex;
  gap: 4px;
  margin: 6px 0 8px;
}

.reality-mode {
  flex: 1;
  font-family: var(--font-mono);
  font-size: 9px;
  letter-spacing: 0.08em;
  border: 1px solid #2a2a3e;
  background: transparent;
  color: var(--text-secondary);
  border-radius: 4px;
  padding: 5px 0;
  cursor: pointer;
  transition: background .15s, color .15s, border-color .15s;
}

.reality-mode.active {
  background: color-mix(in srgb, var(--c-mandatory, #e8c85e) 18%, transparent);
  border-color: var(--c-mandatory, #e8c85e);
  color: var(--text-primary);
}

.rail-info-btn {
  background: transparent;
  border: none;
  color: var(--text-secondary);
  font-size: 9px;
  text-align: left;
  cursor: pointer;
  padding: 0;
}

/* 8. Footer */

.rail-footer {
  margin-top: auto;
  display: flex;
  flex-direction: column;
  gap: 0;
}

.rail-export-btn {
  width: 100%;
  height: 30px;
  border-radius: 4px;
  background: var(--c-civilian);
  border: 1px solid var(--c-civilian);
  color: #fff;
  font-family: var(--font-head);
  font-size: 12px;
  font-weight: 700;
  cursor: pointer;
}

/* Export modal */
#export-overlay {
  position: fixed;
  inset: 0;
  background: rgba(5, 5, 10, 0.72);
  display: none;
  align-items: center;
  justify-content: center;
  z-index: 200;
}
.export-modal {
  background: #12121a;
  border: 1px solid #2a2a3e;
  border-radius: 8px;
  padding: 18px;
  width: 280px;
  display: flex;
  flex-direction: column;
  gap: 8px;
}
.export-title {
  font-family: var(--font-head);
  font-size: 12px;
  font-weight: 700;
  letter-spacing: 0.14em;
  color: var(--text-primary);
  margin-bottom: 4px;
}
.export-opt {
  text-align: left;
  padding: 10px 12px;
  background: #1a1a26;
  border: 1px solid #2a2a3e;
  border-radius: 5px;
  color: var(--text-primary);
  font-family: var(--font-head);
  font-size: 13px;
  font-weight: 700;
  cursor: pointer;
}
.export-opt span {
  font-weight: 400;
  font-size: 10px;
  color: var(--text-secondary);
}
.export-opt:hover:not(:disabled) { border-color: #3d3d58; background: #20202e; }
.export-opt:disabled { opacity: 0.5; cursor: wait; }
.export-cancel {
  margin-top: 4px;
  background: none;
  border: none;
  color: var(--text-secondary);
  font-size: 11px;
  cursor: pointer;
  text-decoration: underline;
}

.rail-reset-link {
  margin-top: 10px;
  background: none;
  border: none;
  color: var(--text-secondary);
  font-size: 9px;
  text-align: left;
  cursor: pointer;
  padding: 0;
  text-decoration: underline;
}

#reset-view-btn, #wedge-scaffold-btn { margin-top: 4px; }

/* Reserved-wedge scaffolding: hidden by default, revealed by the "Show Wedges"
   toggle. Faint sector outlines — a layout-verification aid, not part of the
   data presentation. */
.wedges-layer { display: none; }
svg.show-scaffolding .wedges-layer { display: block; }
.wedge-boundary {
  fill: rgba(255, 255, 255, 0.02);
  stroke: rgba(255, 255, 255, 0.14);
  stroke-width: 1;
  stroke-dasharray: 3 4;
  pointer-events: none;
}

.rail-attribution {
  margin: 10px 0 0;
  font-size: 9px;
  color: #4a4a5a;
  line-height: 1.5;
  text-wrap: pretty;
}

/* ---------- Canvas ---------- */

#canvas-wrap {
  position: fixed;
  left: 286px;
  top: 0;
  width: calc(100vw - 286px);
  height: 100vh;
  overflow: hidden;
}

#main-canvas {
  width: 100%;
  height: 100%;
  display: block;
  background: var(--bg);
}

/* ---------- Bubble nodes: connector lines + external labels (spec Section 12) ---------- */

.bubble-node {
  cursor: pointer;
  outline: none;
}

.bubble-node:focus-visible .bubble-fill {
  filter: drop-shadow(0 0 6px var(--text-primary));
}

/* Phase A: bubble pop-in, all together, slight overshoot */
@keyframes bubblePop {
  from { transform: scale(0); }
  to { transform: scale(1); }
}

.bubble-visual {
  transform-origin: center;
  transform-box: fill-box;
}

.bubble-visual.cascade-in {
  animation: bubblePop 400ms cubic-bezier(0.34, 1.56, 0.64, 1) both;
}

.bubble-node.bubble-dim .bubble-visual {
  transform: scale(0.95);
}

.bubble-fill,
.bubble-ring {
  transition: transform 200ms ease-out, filter 120ms ease-out;
}

.bubble-node.bubble-dim {
  opacity: 0.12;
  transition: opacity 200ms;
}

.bubble-node:not(.bubble-dim) {
  transition: opacity 200ms;
}

.bubble-node:hover .bubble-fill,
.bubble-node.pinned .bubble-fill {
  filter: drop-shadow(0 0 8px var(--cat-color));
}

.bubble-ring {
  fill: none;
  stroke: var(--cat-color);
  stroke-width: 1px;
  opacity: 0;
  transition: opacity 120ms ease-out;
}

.bubble-node:hover .bubble-ring,
.bubble-node.pinned .bubble-ring {
  opacity: 0.4;
}

/* Phase B: connector line draws in via stroke-dashoffset, largest bubble first */
@keyframes drawConnector {
  from { stroke-dashoffset: var(--dash-len); }
  to { stroke-dashoffset: 0; }
}

.connector-line {
  fill: none;
  stroke: var(--cat-color);
  stroke-width: 0.75px;
  stroke-linecap: round;
  opacity: 0.3;
  stroke-dasharray: var(--dash-len);
  stroke-dashoffset: var(--dash-len);
  transition: opacity 120ms ease-out, stroke-width 120ms ease-out;
}

.connector-line.cascade-in {
  animation: drawConnector 180ms ease-out both;
}

.connector-line.settled {
  stroke-dashoffset: 0;
}

.bubble-node[data-tier='2'] .connector-line {
  stroke-width: 0.5px;
  /* Tier 2 hairlines never "draw in" via stroke-dashoffset (spec Section 18 — plain
     opacity fade only), so the dash stays fully drawn instead of inheriting the Tier 1
     load-cascade's hidden-until-drawn default. */
  stroke-dashoffset: 0;
}

.bubble-node:hover .connector-line,
.bubble-node.pinned .connector-line {
  opacity: 1;
  stroke-width: 1.5px;
}

.connector-tick {
  stroke: var(--cat-color);
  stroke-width: 0.75px;
  stroke-linecap: round;
  opacity: 0;
  transition: opacity 120ms ease-out;
}

@keyframes fadeInTick {
  from { opacity: 0; }
  to { opacity: 0.3; }
}

.connector-tick.cascade-in {
  animation: fadeInTick 60ms ease-out both;
}

.connector-tick.settled {
  opacity: 0.3;
}

.bubble-node:hover .connector-tick,
.bubble-node.pinned .connector-tick {
  opacity: 1;
}

/* Phase C: label fade-in, largest bubble first */
@keyframes labelFadeIn {
  from { opacity: 0; }
  to { opacity: 0.75; }
}

.label-group {
  opacity: 0.75;
  pointer-events: none;
}

.label-group.cascade-in {
  animation: labelFadeIn 200ms ease-in-out both;
}

.bubble-node:hover .label-group,
.bubble-node.pinned .label-group {
  opacity: 1;
}

.label-name {
  font-family: var(--font-head);
  font-weight: 700;
  font-size: 13px;
  fill: var(--text-primary);
}

.bubble-node[data-tier='2'] .label-name {
  font-size: 11px;
}

.label-amount {
  font-family: var(--font-mono);
  font-size: 11px;
  fill: var(--text-secondary);
}

.bubble-node[data-tier='2'] .label-amount {
  font-size: 10px;
}

.pct-up { fill: #2ecc71; }
.pct-down { fill: #e74c3c; }
.pct-flat { fill: #7f8c8d; }


.label-dismiss {
  font-family: var(--font-mono);
  font-size: 11px;
  fill: var(--text-secondary);
  opacity: 0;
  cursor: pointer;
  pointer-events: none;
  transition: opacity 120ms ease-out;
}

.bubble-node.pinned .label-dismiss {
  opacity: 0.8;
  pointer-events: auto;
}

/* ---------- Tier 2: hover reveal in place (spec Section 18) ---------- */
/* Children never move. Hidden at rest; BubbleView.activateParent()/deactivateParent()
   toggle .tier2-revealed on a child when its parent is hovered/pinned, and each fades
   in/out in place with a per-element transition-delay set in JS for the stagger.
   Tier 2 nodes stay pointer-events:none permanently — Chromium recomputes :hover
   hit-testing on layout/opacity changes under a stationary cursor, so an appearing
   child under the parent's cursor could otherwise steal hover away from the parent. */

.bubble-node[data-tier='2'] {
  pointer-events: none;
}

.bubble-node[data-tier='2'] .bubble-visual {
  opacity: 0;
  transition: opacity 150ms ease-out;
}

.bubble-node[data-tier='2'].tier2-revealed .bubble-visual {
  opacity: 1;
}

.bubble-node[data-tier='2'] .connector-line,
.bubble-node[data-tier='2'] .connector-tick {
  opacity: 0;
  transition: opacity 150ms ease-out;
}

.bubble-node[data-tier='2'].tier2-revealed .connector-line,
.bubble-node[data-tier='2'].tier2-revealed .connector-tick {
  opacity: 0.6;
}

.bubble-node[data-tier='2'] .label-group {
  opacity: 0;
  transition: opacity 150ms ease-out;
}

.bubble-node[data-tier='2'].tier2-revealed .label-group {
  opacity: 1;
}

/* Non-hovered Tier 1 departments dim while another's children are revealed */
.bubble-node[data-tier='1'].tier1-sibling-dim {
  opacity: 0.3;
  transition: opacity 200ms;
}

/* ---------- Dense-fan label columns ---------- */
/* Parents with ≥4 children get a compact screen-vertical label column just
   outside their fan (rows match fan order, hairline to each child bubble).
   Hidden at rest; activateParent() adds .column-revealed. Pointer-events off —
   same hover-steal rationale as Tier 2. */
.label-column {
  pointer-events: none;
}

.label-column .column-row,
.label-column .column-hairline {
  opacity: 0;
  transition: opacity 160ms ease-out;
}

.label-column.column-revealed .column-row {
  opacity: 1;
}

.label-column.column-revealed .column-hairline {
  opacity: 0.4;
}

.label-column .column-hairline {
  stroke: var(--cat-color, #888);
  stroke-width: 1;
  fill: none;
}

.label-column .column-name {
  font-size: 10.5px;
  font-weight: 600;
  fill: var(--text-primary);
}

.label-column .column-amount {
  font-size: 9.5px;
  fill: var(--text-secondary);
}

/* ---------- Tooltip panel ---------- */

.tooltip-panel {
  position: absolute;
  max-width: 300px;
  background: #12121a;
  border: 1px solid var(--grid-line);
  border-radius: 6px;
  padding: 12px 14px;
  font-size: 12px;
  pointer-events: none;
  z-index: 10;
  box-shadow: 0 8px 24px rgba(0, 0, 0, 0.5);
}

.tooltip-panel.pinned { pointer-events: auto; }

.tooltip-title {
  font-weight: 700;
  font-size: 13px;
  margin-bottom: 4px;
}

.tooltip-row {
  font-family: var(--font-mono);
  color: var(--text-secondary);
  font-size: 11px;
  margin: 2px 0;
}

.tooltip-badge {
  display: inline-block;
  font-family: var(--font-mono);
  font-size: 10px;
  padding: 1px 6px;
  border-radius: 8px;
  margin-top: 4px;
}

.tooltip-badge.up { background: rgba(22, 160, 133, 0.2); color: #16a085; }
.tooltip-badge.down { background: rgba(192, 57, 43, 0.2); color: #e57373; }
.tooltip-badge.flat { background: rgba(138, 138, 154, 0.2); color: var(--text-secondary); }

.tooltip-warning {
  color: #f1c40f;
  font-size: 10px;
  margin-top: 6px;
}

.tooltip-source {
  font-size: 10px;
  color: var(--text-secondary);
  margin-top: 6px;
  border-top: 1px solid var(--grid-line);
  padding-top: 4px;
}

.tooltip-notables {
  margin: 6px 0 0;
  padding-left: 14px;
  font-size: 10px;
  color: var(--text-secondary);
}

/* ---------- Table view ---------- */

.table-view-wrap {
  height: 100%;
  overflow: auto;
  padding: 16px 24px;
}

#budget-table {
  width: 100%;
  border-collapse: collapse;
  font-size: 12px;
}

#budget-table th, #budget-table td {
  text-align: left;
  padding: 6px 10px;
  border-bottom: 1px solid var(--grid-line);
}

#budget-table th {
  font-family: var(--font-head);
  cursor: pointer;
  color: var(--text-secondary);
  position: sticky;
  top: 0;
  background: var(--bg);
}

#budget-table td.amount { font-family: var(--font-mono); text-align: right; }

/* ---------- Modal ---------- */

.modal {
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.6);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 100;
}

.modal[hidden] {
  display: none;
}

.modal-content {
  background: #12121a;
  border: 1px solid var(--grid-line);
  border-radius: 8px;
  padding: 24px;
  max-width: 480px;
  position: relative;
}

.modal-close {
  position: absolute;
  top: 8px;
  right: 8px;
  background: transparent;
  border: none;
  color: var(--text-secondary);
  font-size: 16px;
  cursor: pointer;
}

@media (max-width: 900px) {
  .reality-columns { grid-template-columns: 1fr; }
  .debt-clock { text-align: left; min-width: unset; }
}
