/* app.css — MathFlow "engine waterfall" tokens & components.
   Blueprint aesthetic adapted from the Stitch reference (Flowchart website/Stitch code.txt):
   light surface, faint 8px grid, JetBrains Mono, step cards with ports + flow connectors,
   plus the NEW dataflow-DAG pieces (spine + tributary bus) for this site. */

:root {
  /* palette (from Stitch tailwind config) */
  --bg: #f6fafe;
  --card: #ffffff;
  --surface: #eaeef2;
  --surface-low: #f0f4f8;
  --border: #c6c6cd;
  --text: #171c1f;
  --muted: #3a3b41;
  --accent: #0058be;        /* secondary blue — live / flow / internal curve */
  --accent-strong: #2170e4;
  --accent2: #d95f00;       /* tertiary orange — superficial curve */
  --live-bg: #e1f5ee;
  --live-fg: #0f6e56;
  --base-bg: #eef1f5;
  --base-fg: #45464d;
  --grid-line: #e2e8f0;

  --font-mono: 'JetBrains Mono', ui-monospace, 'Cascadia Code', monospace;
  --font-head: 'Hanken Grotesk', system-ui, sans-serif;

  --rail-w: 220px;
  --radius: 6px;
  --connector: var(--border);
}

* { box-sizing: border-box; }

html, body { margin: 0; }
html { scroll-behavior: smooth; }

/* return-to-top button (appears after scrolling) */
.to-top {
  position: fixed; right: 20px; bottom: 20px; z-index: 60;
  width: 42px; height: 42px; border-radius: 50%;
  display: flex; align-items: center; justify-content: center;
  background: var(--accent); color: #fff; text-decoration: none;
  font-size: 20px; line-height: 1; border: 1px solid var(--accent);
  box-shadow: 0 2px 10px rgba(0, 0, 0, 0.18);
  opacity: 0; pointer-events: none; transition: opacity 0.2s ease;
}
.to-top.show { opacity: 1; pointer-events: auto; }
.to-top:hover { background: var(--accent-strong); }

body {
  background: var(--bg);
  color: var(--text);
  font-family: var(--font-mono);
  font-size: 14px;
  line-height: 1.5;
  -webkit-font-smoothing: antialiased;
}

/* faint blueprint grid */
.grid-bg {
  background-color: var(--bg);
  background-image:
    linear-gradient(to right, var(--grid-line) 1px, transparent 1px),
    linear-gradient(to bottom, var(--grid-line) 1px, transparent 1px);
  background-size: 8px 8px;
}

/* ---- top nav ---- */
.nav {
  position: sticky;
  top: 0;
  z-index: 50;
  display: flex;
  align-items: center;
  justify-content: space-between;
  height: 60px;
  padding: 0 28px;
  background: var(--bg);
  border-bottom: 1px solid var(--border);
}
.nav__brand { font-family: var(--font-head); font-weight: 700; font-size: 18px; letter-spacing: 0.02em; }
.nav__brand span { color: var(--accent); }
.nav__right { display: flex; align-items: center; gap: 14px; }
.nav__badge {
  font-size: 11px; letter-spacing: 0.08em; text-transform: uppercase;
  background: var(--surface); color: var(--muted);
  padding: 5px 10px; border-radius: var(--radius); border: 1px solid var(--border);
}
.nav__link { color: var(--muted); text-decoration: none; font-family: var(--font-head); font-weight: 600; font-size: 14px; }
.nav__link:hover { color: var(--accent); }

/* ---- intro ---- */
.intro { max-width: 980px; margin: 28px auto 8px; padding: 0 24px; }
.intro h1 { font-family: var(--font-head); font-size: 26px; margin: 0 0 6px; }
.intro p { color: var(--muted); margin: 0; max-width: 70ch; }
.intro .legend { display: flex; gap: 18px; flex-wrap: wrap; margin-top: 14px; font-size: 12px; }
.intro .legend b { font-weight: 700; }
.legend__chip { display: inline-flex; align-items: center; gap: 6px; }
.legend__dot { width: 10px; height: 10px; border-radius: 50%; }

/* ---- the waterfall lane ---- */
.lane { max-width: 980px; margin: 8px auto 120px; padding: 0 24px; }

.step {
  display: grid;
  grid-template-columns: var(--rail-w) 1fr;
  align-items: stretch;
}

/* tributary rail (inputs branching in) + the local "bus" line */
.step__rail {
  position: relative;
  display: flex;
  flex-direction: column;
  justify-content: center;
  gap: 8px;
  padding: 16px 20px 16px 0;
}
.step__rail.has-trib::after {     /* vertical bus collecting the tributaries */
  content: '';
  position: absolute;
  right: 0; top: 26px; bottom: 26px;
  width: 1px; background: var(--connector);
}
.trib { position: relative; }
.step__rail.has-trib .trib::after {  /* horizontal stub from node to bus */
  content: '';
  position: absolute;
  right: -20px; top: 50%;
  width: 20px; height: 1px; background: var(--connector);
}
.trib__label { display: block; font-size: 10px; letter-spacing: 0.07em; text-transform: uppercase; color: var(--muted); }
.trib__sym { color: var(--text); font-weight: 500; }
.trib__unit { color: var(--muted); font-size: 10px; }
.trib__val { font-size: 13px; color: var(--text); }
.trib--edit input {
  font-family: var(--font-mono); font-size: 13px;
  width: 100%; padding: 5px 8px; margin-top: 2px;
  background: var(--card); color: var(--text);
  border: 1px solid var(--border); border-radius: var(--radius);
}
.trib--edit input:focus { outline: none; border-color: var(--accent); box-shadow: 0 0 0 2px rgba(0,88,190,0.15); }
.trib--ro, .trib--data {
  background: var(--surface-low); border: 1px solid var(--border);
  border-radius: var(--radius); padding: 5px 9px;
}
.trib--data { background: var(--surface); }
.trib__head { display: flex; align-items: center; justify-content: space-between; gap: 6px; }
.trib__right { display: flex; align-items: center; gap: 6px; min-width: 0; }

/* info button + popover */
.trib__info {
  flex: 0 0 auto; width: 15px; height: 15px; padding: 0;
  border: 1px solid var(--border); border-radius: 50%;
  background: var(--surface); color: var(--muted);
  font-family: var(--font-head); font-weight: 700; font-size: 10px; line-height: 13px;
  text-align: center; cursor: pointer;
}
.trib__info:hover, .trib__info:focus-visible { border-color: var(--accent); color: var(--accent); outline: none; }
.trib__pop {
  display: none; position: absolute; top: 100%; left: 0; margin-top: 5px; z-index: 40;
  width: 250px; max-width: 78vw;
  background: var(--card); border: 1px solid var(--border); border-radius: var(--radius);
  padding: 9px 11px; font-size: 11px; line-height: 1.5; color: var(--text);
  box-shadow: 0 6px 18px rgba(0, 0, 0, 0.10);
}
.trib__pop.show { display: block; }

/* the card column */
.step__main { position: relative; min-width: 0; }

/* spine connector between cards */
.connector { position: relative; height: 44px; display: flex; justify-content: center; }
.connector__line { width: 1px; height: 100%; background: var(--connector); }
.connector__flow {
  position: absolute; top: 0; left: 50%; transform: translateX(-50%);
  width: 1px; height: 100%;
  background: linear-gradient(180deg, transparent, var(--accent), transparent);
  background-size: 100% 220%;
  animation: flowDown 2.2s linear infinite;
}
@keyframes flowDown { 0% { background-position: 0 -100%; } 100% { background-position: 0 200%; } }
.connector__badge {
  position: absolute; top: 50%; left: 50%; transform: translate(-50%, -50%);
  width: 26px; height: 26px; border-radius: 50%;
  display: flex; align-items: center; justify-content: center;
  background: var(--card); border: 1px solid var(--border); color: var(--accent);
  font-size: 13px;
}

/* the card */
.card {
  position: relative;
  background: var(--card);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 18px 20px 16px;
}
.card--live { border: 2px solid var(--accent); }
.card--root { border-style: dashed; }
.card.has-trib::before {  /* junction dot where the bus merges into the card */
  content: '';
  position: absolute; left: -1px; top: 50%;
  width: 9px; height: 9px; border-radius: 50%;
  background: var(--card); border: 1.5px solid var(--accent);
  transform: translate(-50%, -50%);
}
.port { position: absolute; left: 50%; transform: translateX(-50%); width: 9px; height: 9px; border-radius: 50%; background: var(--card); border: 1.5px solid var(--text); }
.port--top { top: -5px; }
.port--bottom { bottom: -5px; }

.card__head { display: flex; align-items: flex-start; justify-content: space-between; gap: 12px; margin-bottom: 10px; }
.card__badges { display: flex; align-items: center; gap: 8px; flex-wrap: wrap; }
.card__badge { font-family: var(--font-head); font-weight: 700; font-size: 11px; letter-spacing: 0.1em; text-transform: uppercase; color: var(--accent); }
.card__cat { font-family: var(--font-head); font-size: 10px; letter-spacing: 0.06em; text-transform: uppercase; color: var(--muted); background: var(--surface); border: 1px solid var(--border); padding: 2px 8px; border-radius: var(--radius); }
.card__tags { display: flex; gap: 6px; align-items: center; flex-wrap: wrap; }
.tag { font-size: 10px; letter-spacing: 0.06em; padding: 3px 8px; border-radius: var(--radius); }
.tag--live { background: var(--live-bg); color: var(--live-fg); }
.tag--base { background: var(--base-bg); color: var(--base-fg); }
.card__title { font-family: var(--font-head); font-weight: 600; font-size: 17px; margin: 2px 0 2px; }
.card__sub { color: var(--muted); font-size: 13px; margin: 0 0 12px; }

.eqns { display: flex; flex-direction: column; gap: 8px; margin: 6px 0 12px; }
.eqn { background: var(--surface-low); border: 1px solid var(--border); border-radius: var(--radius); padding: 8px 12px; overflow-x: auto; }
.eqn .katex { font-size: 1.02em; }

.values { font-size: 13px; color: var(--text); background: var(--surface); border-radius: var(--radius); padding: 8px 12px; margin: 0 0 12px; }
.values b { color: var(--accent); font-weight: 500; }

.graph { width: 100%; height: 300px; margin: 6px 0 12px; }
.graph--3d { height: 380px; }
.graph--short { height: 150px; }
.graph-row { display: grid; grid-template-columns: 1fr 1fr; gap: 14px; }
@media (max-width: 760px) { .graph-row { grid-template-columns: 1fr; } }

.produces { display: flex; gap: 6px; flex-wrap: wrap; align-items: center; margin-top: 4px; }
.produces__label { font-size: 10px; letter-spacing: 0.07em; text-transform: uppercase; color: var(--muted); }
.chip { font-size: 12px; padding: 3px 9px; border-radius: var(--radius); background: var(--surface-low); border: 1px solid var(--border); color: var(--text); }
.chip--out { background: #e6f1fb; border-color: #b5d4f4; color: #0c447c; }

.kpis { display: grid; grid-template-columns: repeat(auto-fit, minmax(120px, 1fr)); gap: 10px; margin: 4px 0 14px; }
.kpi { background: var(--surface-low); border: 1px solid var(--border); border-radius: var(--radius); padding: 10px 12px; }
.kpi__v { font-size: 20px; font-weight: 500; }
.kpi__l { font-size: 11px; color: var(--muted); letter-spacing: 0.04em; }

.foot { display: flex; justify-content: space-between; gap: 12px; flex-wrap: wrap; border-top: 1px solid var(--border); margin-top: 10px; padding-top: 8px; }
.foot__src { font-size: 11px; color: var(--accent); }
.foot details { font-size: 11px; color: var(--muted); max-width: 100%; }
.foot summary { cursor: pointer; color: var(--muted); }
.foot details p { margin: 6px 0 0; line-height: 1.55; }

.disclaimer { max-width: 980px; margin: 0 auto; padding: 12px 24px; }
.disclaimer div { background: #fff6e9; border: 1px solid #ffb690; color: #783200; border-radius: var(--radius); padding: 10px 14px; font-size: 12px; }

/* responsive: collapse the rail into a chips strip above the card */
@media (max-width: 900px) {
  .step { grid-template-columns: 1fr; }
  .step__rail { flex-direction: row; flex-wrap: wrap; padding: 0 0 10px; justify-content: flex-start; }
  .step__rail.has-trib::after, .step__rail.has-trib .trib::after { display: none; }
  .card.has-trib::before { display: none; }
  .trib--edit { min-width: 130px; }
}

@media (prefers-reduced-motion: reduce) {
  .connector__flow { animation: none; opacity: 0; }
  html { scroll-behavior: auto; }
}

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