/* ==========================================================================
   FIG. 04  "flowdown"  -  styles for the contract-tier network diagram
   --------------------------------------------------------------------------
   The drawing itself is built by js/figures/flowdown.js. This file only
   decides how it LOOKS: colours, fonts, line weights and the animation.
   Every colour comes from the design tokens defined in css/site.css.

   Class names used on states (added and removed by the script):
     .is-route ..... the part lies on the route that is currently lit
     .is-selected .. the node the visitor is pointing at or has focused
     .is-tracing ... (on the root) a node is selected right now
     .is-paused .... (on the root) the figure is off screen
     .is-still ..... (on the root) the visitor pressed "Pause animations"
   ========================================================================== */


/* --------------------------------------------------------------------------
   1. FRAME
   -------------------------------------------------------------------------- */
.fg-flowdown {
  box-sizing: border-box;
  display: block;
  width: 100%;
  max-width: 880px;          /* never stretch wider than the layout was drawn for */
  margin: 0 auto;
}

.fg-flowdown__svg {
  display: block;
  width: 100%;
  height: auto;              /* the script sets the viewBox; height follows from it */
  overflow: visible;         /* lets the focus ring of an edge node show fully */
  font-family: var(--font-mono);
  font-size: 11px;
}


/* --------------------------------------------------------------------------
   2. TIER RULES AND LABELS
   -------------------------------------------------------------------------- */
.fg-flowdown__rule {
  stroke: var(--trace-3);
  stroke-width: 1;
  stroke-dasharray: 1 4;     /* dotted: a construction line, fainter than a wire */
  shape-rendering: crispEdges;
}

/* The solid rule above the key matches the rule above the readout. */
.fg-flowdown__rule--solid {
  stroke: var(--line-2);
  stroke-dasharray: none;
}

.fg-flowdown__tier {
  font-size: 10px;
  letter-spacing: 0.12em;
  fill: var(--ink-3);
}

.fg-flowdown__tier.is-route {
  fill: var(--ink);
}


/* --------------------------------------------------------------------------
   3. WIRES
   Every wire is drawn twice: a dim copy that is always visible, and a
   brighter copy on top that only shows while the wire is on the lit route.
   -------------------------------------------------------------------------- */
.fg-flowdown__wire {
  fill: none;
  stroke: var(--trace-2);
  stroke-width: 1;
  shape-rendering: crispEdges;
}

.fg-flowdown__wire--route {
  stroke: var(--trace);
  stroke-width: 1.5;
  shape-rendering: auto;
  opacity: 0;
}

.fg-flowdown__wire--route.is-route {
  opacity: 1;
}

.fg-flowdown.is-tracing .fg-flowdown__wire--route {
  stroke: var(--ink);
}

/* The amber delivery lane that runs beside the wires of the SGCS route. */
.fg-flowdown__lane {
  fill: none;
  stroke: var(--amber-2);
  stroke-width: 1;
  shape-rendering: crispEdges;
}

/* Junction dots and the square ports where wires meet the nodes. */
.fg-flowdown__port,
.fg-flowdown__junction {
  fill: var(--trace-2);
}

.fg-flowdown__port.is-route,
.fg-flowdown__junction.is-route {
  fill: var(--trace);
}

.fg-flowdown.is-tracing .fg-flowdown__port.is-route,
.fg-flowdown.is-tracing .fg-flowdown__junction.is-route {
  fill: var(--ink);
}

.fg-flowdown__port.fg-flowdown__port--up {
  fill: var(--amber);
}


/* --------------------------------------------------------------------------
   4. PACKETS  (the moving marks on the wires)
   A packet is one dash of a dashed line. Sliding the dash pattern along the
   line (stroke-dashoffset) makes the dashes travel.
     dash + gap = 72px, and one cycle moves the pattern by exactly 72px,
     so the loop is seamless. Change the duration to change the speed.
   -------------------------------------------------------------------------- */
.fg-flowdown__packets {
  fill: none;
  stroke: var(--trace);
  stroke-width: 2;
  stroke-dasharray: 6 66;
  stroke-dashoffset: -14;
  animation: fg-flowdown-down 4.2s linear infinite;
}

.fg-flowdown__packets.is-route {
  stroke: var(--ink);
}

/* Delivery packets: round amber dots that travel the other way (the lane
   paths are drawn from the bottom up, so the same kind of slide moves them
   upward). A different shape, not just a different colour. */
.fg-flowdown__packets--up {
  stroke: var(--amber);
  stroke-width: 3.5;
  stroke-linecap: round;
  stroke-dasharray: 0.01 71.99;
  stroke-dashoffset: -30;
  animation-name: fg-flowdown-up;
}

@keyframes fg-flowdown-down {
  from { stroke-dashoffset: -14; }
  to   { stroke-dashoffset: -86; }
}

@keyframes fg-flowdown-up {
  from { stroke-dashoffset: -30; }
  to   { stroke-dashoffset: -102; }
}

/* Off screen or in a background tab: hold still. */
.fg-flowdown.is-paused .fg-flowdown__packets {
  animation-play-state: paused;
}

/* Static arrowheads. Hidden normally; they replace the packets when the
   visitor has asked for reduced motion (see section 9) or has pressed
   "Pause animations" (just below). */
.fg-flowdown__arrow {
  display: none;
  fill: var(--trace);
}

/* Animations paused by the visitor: the same still picture as reduced
   motion. The packets are removed and the arrowheads take their place. */
.fg-flowdown.is-still .fg-flowdown__packets {
  display: none;
  animation: none;
}

.fg-flowdown.is-still .fg-flowdown__arrow {
  display: inline;
}

.fg-flowdown__arrow.is-route {
  fill: var(--ink);
}

.fg-flowdown__arrow.fg-flowdown__arrow--up {
  fill: var(--amber);
}


/* --------------------------------------------------------------------------
   5. NODES
   -------------------------------------------------------------------------- */
.fg-flowdown__node {
  cursor: default;
  outline: none;             /* replaced by .fg-flowdown__ring below */
  -webkit-user-select: none;
  user-select: none;
}

.fg-flowdown__box {
  fill: var(--bg);
  stroke: var(--trace-2);
  stroke-width: 1;
  shape-rendering: crispEdges;
}

.fg-flowdown__label {
  font-weight: 500;
  letter-spacing: 0.08em;
  fill: var(--ink-2);
}

.fg-flowdown__sub {
  font-size: 10px;
  letter-spacing: 0.08em;
  fill: var(--ink-3);
}

/* Slightly tighter lettering when the boxes are at their narrowest. */
.fg-flowdown--tight .fg-flowdown__label {
  letter-spacing: 0.05em;
}

/* Four small lamps: the lit one shows which tier the node is in. */
.fg-flowdown__lamp {
  fill: var(--trace-3);
}

.fg-flowdown__lamp.is-lit {
  fill: var(--trace);
}

/* On the lit route. */
.fg-flowdown__node.is-route .fg-flowdown__box {
  stroke: var(--trace);
}

.fg-flowdown__node.is-route .fg-flowdown__label,
.fg-flowdown__node.is-route .fg-flowdown__lamp.is-lit {
  fill: var(--ink);
}

/* Pointed at, or focused. */
.fg-flowdown__node.is-selected .fg-flowdown__box {
  fill: var(--bg-3);
  stroke: var(--ink);
}

/* SGCS: the one amber node. */
.fg-flowdown__node--sgcs .fg-flowdown__box,
.fg-flowdown__node--sgcs.is-route .fg-flowdown__box,
.fg-flowdown__node--sgcs.is-selected .fg-flowdown__box {
  fill: var(--amber-glow);
  stroke: var(--amber);
}

.fg-flowdown__node--sgcs.is-selected .fg-flowdown__box {
  stroke-width: 2;
}

.fg-flowdown__label.fg-flowdown__label--sgcs,
.fg-flowdown__node.is-route .fg-flowdown__label.fg-flowdown__label--sgcs {
  font-family: var(--font-display);
  font-size: 15px;
  font-weight: 800;
  font-stretch: 125%;
  letter-spacing: 0.06em;
  fill: var(--amber);
}

.fg-flowdown__node--sgcs .fg-flowdown__lamp.is-lit,
.fg-flowdown__node--sgcs.is-route .fg-flowdown__lamp.is-lit {
  fill: var(--amber);
}

/* Keyboard focus ring. Shown on focus, then hidden again when the browser
   knows the focus came from a mouse or a finger rather than the keyboard. */
.fg-flowdown__ring {
  fill: none;
  stroke: var(--ink);        /* white, so that amber keeps meaning "SGCS" */
  stroke-width: 2;
  opacity: 0;
  pointer-events: none;
}

.fg-flowdown__node:focus .fg-flowdown__ring {
  opacity: 1;
}

.fg-flowdown__node:focus:not(:focus-visible) .fg-flowdown__ring {
  opacity: 0;
}

/* The note under the SGCS node and its leader line. */
.fg-flowdown__note {
  font-size: 10px;
  letter-spacing: 0.08em;
  fill: var(--ink-2);
}

.fg-flowdown__leader {
  fill: none;
  stroke: var(--amber-2);
  stroke-width: 1;
  shape-rendering: crispEdges;
}


/* --------------------------------------------------------------------------
   6. KEY  (the two flow legends)
   -------------------------------------------------------------------------- */
.fg-flowdown__key-title {
  font-weight: 600;
  letter-spacing: 0.14em;
  fill: var(--ink);
}

.fg-flowdown__key-item {
  letter-spacing: 0.02em;
  fill: var(--ink-2);
}

.fg-flowdown__key-line {
  fill: none;
  stroke: var(--trace-2);
  stroke-width: 1;
  shape-rendering: crispEdges;
}

.fg-flowdown__key-head {
  fill: var(--trace);
}

.fg-flowdown__key--up .fg-flowdown__key-line {
  stroke: var(--amber-2);
}

.fg-flowdown__key--up .fg-flowdown__key-head {
  fill: var(--amber);
}


/* --------------------------------------------------------------------------
   7. READOUT  (the line of plain English under the diagram)
   -------------------------------------------------------------------------- */
.fg-flowdown__readout {
  box-sizing: border-box;
  display: grid;
  grid-template-columns: minmax(0, 1fr);
  margin-top: 12px;
  padding-top: 11px;
  border-top: 1px solid var(--line-2);
}

/* Wide layouts: a label in the left margin, lined up with the tier labels.
   74px is SIDE_GUTTER in the script; 22px below is STRIP_GUTTER. */
.fg-flowdown--side .fg-flowdown__readout {
  grid-template-columns: 74px minmax(0, 1fr);
}

.fg-flowdown--strip .fg-flowdown__readout {
  padding-left: 22px;
}

.fg-flowdown__readout-name {
  font: 400 10px/16px var(--font-mono);
  letter-spacing: 0.12em;
  color: var(--ink-3);
}

.fg-flowdown--strip .fg-flowdown__readout-name {
  display: none;
}

/* All the sentences sit in the same grid cell, one on top of the other;
   only the one marked .is-on is visible. */
.fg-flowdown__readout-stack {
  display: grid;
  grid-template-columns: minmax(0, 1fr);
}

.fg-flowdown__say {
  grid-area: 1 / 1;
  visibility: hidden;
}

.fg-flowdown__say.is-on {
  visibility: visible;
}

.fg-flowdown__say-tag {
  display: block;
  font: 500 11px/16px var(--font-mono);
  letter-spacing: 0.1em;
  color: var(--amber);
}

.fg-flowdown__say--idle .fg-flowdown__say-tag {
  color: var(--ink-3);
}

.fg-flowdown__say-text {
  display: block;
  margin-top: 3px;
  font: 400 14px/1.45 var(--font-body);
  color: var(--ink-2);
}


/* --------------------------------------------------------------------------
   8. SOFT COLOUR CHANGES when a node is selected
   -------------------------------------------------------------------------- */
.fg-flowdown__box,
.fg-flowdown__label,
.fg-flowdown__lamp,
.fg-flowdown__port,
.fg-flowdown__junction,
.fg-flowdown__tier,
.fg-flowdown__wire--route {
  transition: fill 0.16s ease, stroke 0.16s ease, opacity 0.16s ease;
}


/* --------------------------------------------------------------------------
   9. REDUCED MOTION
   Nothing moves: the packets are removed and fixed arrowheads show the
   direction of each flow instead.
   -------------------------------------------------------------------------- */
@media (prefers-reduced-motion: reduce) {
  .fg-flowdown__packets {
    display: none;
    animation: none;
  }

  .fg-flowdown__arrow {
    display: inline;
  }

  .fg-flowdown__box,
  .fg-flowdown__label,
  .fg-flowdown__lamp,
  .fg-flowdown__port,
  .fg-flowdown__junction,
  .fg-flowdown__tier,
  .fg-flowdown__wire--route {
    transition: none;
  }
}
