/* --------------------------------------------------------------------------
   The turn, drawn — question → Nura → module → rounds → answer

   Left to right, as a pipeline (product owner, 2026-10-05). Lookups asked for
   together stack in one column; the next round follows to the right after
   "then". A long turn scrolls sideways rather than squeezing its boxes. The
   edge into a node that is still running carries a moving dash.
   -------------------------------------------------------------------------- */

.flow {
  --flow-line: color-mix(in srgb, var(--accent) 35%, var(--border-strong));
  display: flex;
  flex-direction: row;
  align-items: center;
  /* A long turn wraps onto the next line rather than scrolling sideways
     (product owner, 2026-10-06); each arrow stays with its node (.flow-link).
     Sideways scroll is left only for a node wider than a phone's column. */
  flex-wrap: wrap;
  gap: 14px 0;
  margin-top: 14px;
  margin-bottom: 4px;
  padding: 16px 12px;
  overflow-x: auto;
  justify-content: safe center;
  border: 1px solid var(--border);
  border-radius: var(--r-lg);
  background:
    radial-gradient(circle at 1px 1px, color-mix(in srgb, var(--text-faint) 22%, transparent) 1px, transparent 0) 0 0 / 14px 14px,
    var(--surface-raised);
}

.flow-node {
  display: inline-flex;
  align-items: center;
  gap: 8px;
  flex: none;
  max-width: 172px;
  padding: 6px 10px;
  border: 1px solid var(--border-strong);
  border-radius: var(--r-md);
  background: var(--surface);
  font-size: 12.5px;
  line-height: 1.3;
  color: var(--text-muted);
  box-shadow: var(--shadow-sm);
  animation: flow-in .3s ease-out both;
}

.flow-node small { color: var(--text-faint); font-size: 11.5px; }
.flow-question { color: var(--text-faint); border-style: dashed; }
.flow-router, .flow-module { font-weight: 650; color: var(--accent); }
.flow-router small, .flow-module small { display: none; }

.flow-icon { display: inline-flex; flex: none; color: var(--accent); }

/* Each module in its own colour; its lookups carry the colour on their edge,
   so a turn that crosses modules shows the handover. */
.flow-module {
  /* 50% of the hue: 75% left Loyalty's label at 4.07:1 (audit — WCAG 1.4.3).
     Mixed with the theme's strongest text, so it holds in the dark too. */
  color: color-mix(in srgb, var(--hue) 50%, var(--text-strong));
  border-color: color-mix(in srgb, var(--hue) 45%, var(--border-strong));
  background: color-mix(in srgb, var(--hue) 7%, var(--surface));
}
.flow-module .flow-icon { color: var(--hue); }
.flow-lookup { border-left: 3px solid color-mix(in srgb, var(--hue, var(--accent)) 70%, transparent); }
.flow-writing { color: var(--text-muted); }
/* The label and its time, stacked. Not :last-child — the hidden state word
   (flow.js `said`) is last now, and "Writing the answer2.3s" ran together. */
.flow-writing > span:not(.flow-badge):not(.flow-icon):not(.sr-state) { display: flex; flex-direction: column; }

/* A round: the lookups the model asked for together, one above the other. */
.flow-round {
  display: flex;
  flex-direction: column;
  align-items: stretch;
  gap: 6px;
  flex: none;
}

.flow-lookup { text-align: left; }
.flow-lookup > span:not(.flow-mark) { display: flex; flex-direction: column; }
.flow-lookup .flow-text { color: var(--text); }

/* The query behind an Insights lookup — debugging only (insights.show_sql). */
.flow-sql { margin-top: 4px; font-size: 11.5px; }
.flow-sql summary { cursor: pointer; color: var(--accent); }
/* Opened, the node makes room for it — the diagram wraps around the wider
   node — instead of the query spilling over its neighbours. */
.flow-node:has(.flow-sql[open]) { max-width: min(460px, 100%); }
.flow-sql pre {
  margin: 4px 0 0;
  overflow-wrap: anywhere;
  padding: 6px 8px;
  overflow-x: auto;
  border-radius: var(--r-sm);
  background: var(--surface-sunken);
  font-family: var(--font-mono);
  font-size: 11px;
  white-space: pre-wrap;
  color: var(--text-strong);
}
.flow-meta:empty { display: none; }

/* An arrow and its node, never split across lines. */
.flow-link { display: flex; align-items: center; flex: none; }

/* Edges: a short line with an arrow head; it moves while the next node runs. */
.flow-edge {
  position: relative;
  flex: none;
  width: 18px;
  height: 2px;
  background: var(--flow-line);
}

.flow-edge::after {
  content: "";
  position: absolute;
  right: -1px;
  top: -3px;
  border: 4px solid transparent;
  border-left-color: var(--flow-line);
  border-right: 0;
}

/* Room for its word: "then" on an 18px arrow ran into the nodes either side. */
.flow-edge:has(> .flow-edge-label) { width: 36px; }

.flow-edge-label {
  position: absolute;
  left: 50%;
  bottom: 6px;
  transform: translateX(-50%);
  font-size: 10.5px;
  color: var(--text-faint);
  white-space: nowrap;
}

.flow:not(.flow-still) .flow-edge:has(+ .flow-round .is-running),
.flow:not(.flow-still) .flow-edge:last-child {
  background: repeating-linear-gradient(to right, var(--accent) 0 4px, transparent 4px 8px);
  background-size: 8px 2px;
  animation: flow-move .5s linear infinite;
}

/* Marks: a turning ring while running, then a tick or a cross. The tick and
   the cross are drawn in the surface's colour, so they cut through in both
   themes. */
.flow-mark {
  position: relative;
  flex: none;
  width: 13px;
  height: 13px;
  border-radius: 50%;
}

.is-running .flow-mark {
  border: 2px solid color-mix(in srgb, var(--accent) 22%, transparent);
  border-top-color: var(--accent);
  animation: flow-turn .8s linear infinite;
}

.is-done .flow-mark, .flow-answer .flow-mark { background: var(--pos); animation: flow-pop .22s ease-out; }
.is-done .flow-mark::after, .flow-answer .flow-mark::after {
  content: "";
  position: absolute;
  left: 4px;
  top: 2px;
  width: 3px;
  height: 6px;
  border: solid var(--surface);
  border-width: 0 2px 2px 0;
  transform: rotate(45deg);
}

.is-failed { border-color: color-mix(in srgb, var(--neg) 40%, var(--border-strong)); }
.is-failed .flow-mark { background: var(--neg); }
.is-failed .flow-mark::before, .is-failed .flow-mark::after {
  content: "";
  position: absolute;
  left: 5.5px;
  top: 2.5px;
  width: 2px;
  height: 8px;
  background: var(--surface);
  transform: rotate(45deg);
}
.is-failed .flow-mark::after { transform: rotate(-45deg); }

.flow-answer { font-weight: 650; color: var(--pos); border-color: color-mix(in srgb, var(--pos) 35%, var(--border-strong)); }

/* --- every node's state ------------------------------------------------------
   RUNNING pulses — a ring breathing out in the node's colour, and the spinner;
   DONE carries a tick; FAILED a cross and a red edge. Lookups, the answer and
   the stop show it at their left; Nura, a module and the writing in a
   badge on their corner, so their symbol stays. */

.flow-node { position: relative; }

.flow-badge {
  position: absolute;
  top: -6px;
  right: -6px;
  box-shadow: 0 0 0 2px var(--surface);
  background: var(--surface);
}

.flow:not(.flow-still) .flow-node.is-running {
  border-color: color-mix(in srgb, var(--hue, var(--accent)) 55%, var(--border-strong));
  animation: flow-in .3s ease-out both, flow-pulse 1.4s ease-in-out .3s infinite;
}

.flow-router.is-failed, .flow-module.is-failed, .flow-writing.is-failed,
.flow-lookup.is-failed {
  border-color: color-mix(in srgb, var(--neg) 55%, var(--border-strong));
  background: var(--danger-soft);
}

.flow-lookup.is-failed .flow-text { color: var(--neg); }

.flow-stopped {
  font-weight: 650;
  color: var(--neg);
  border-color: color-mix(in srgb, var(--neg) 45%, var(--border-strong));
  background: var(--danger-soft);
}

.flow-taken.is-stopped summary { color: var(--neg); }
.turn-failed { margin: 0; }

/* Where the seconds went: one bar, in shares, with the totals beneath. */
.timebar { margin-top: 10px; margin-bottom: 2px; padding: 0 4px; }
.timebar-track {
  display: flex;
  height: 8px;
  overflow: hidden;
  border-radius: var(--r-pill);
  background: var(--border);
}
.timebar-part { flex-basis: 0; min-width: 3px; }
.timebar-part + .timebar-part { border-left: 2px solid var(--surface); }
.timebar-part.is-ready,    .timebar-key.is-ready i    { background: var(--border-strong); }
.timebar-part.is-choosing, .timebar-key.is-choosing i { background: color-mix(in srgb, var(--text-faint) 75%, var(--surface)); }
.timebar-key.is-database { margin-left: auto; font-style: italic; }
.timebar-part.is-thinking, .timebar-key.is-thinking i { background: color-mix(in srgb, var(--accent) 45%, var(--surface)); }
.timebar-part.is-lookups,  .timebar-key.is-lookups i  { background: var(--hue, var(--accent)); }
.timebar-part.is-writing,  .timebar-key.is-writing i  { background: var(--pos); }
.timebar-legend {
  display: flex;
  flex-wrap: wrap;
  gap: 4px 14px;
  margin-top: 6px;
  font-size: 11.5px;
  color: var(--text-faint);
}
.timebar-key { display: inline-flex; align-items: center; gap: 5px; }
.timebar-key i { width: 8px; height: 8px; border-radius: 2px; }

/* Folded under a reply: one quiet line that opens to the diagram. */
.flow-taken { margin-top: 6px; font-size: 12.5px; color: var(--text-faint); }
.flow-taken summary {
  display: inline-flex;
  align-items: center;
  gap: 7px;
  min-height: 28px;
  padding: 2px 8px;
  margin-left: -8px;
  border-radius: var(--r-sm);
  cursor: pointer;
  list-style: none;
  transition: background .15s, color .15s;
}
.flow-taken summary::-webkit-details-marker { display: none; }
.flow-taken summary:hover { color: var(--accent); background: var(--surface-sunken); }
.flow-chevron {
  width: 6px;
  height: 6px;
  border: solid currentColor;
  border-width: 0 1.5px 1.5px 0;
  transform: rotate(-45deg);
  transition: transform .15s;
}
/* Its own chevron only: a fold inside an open fold ("Technical details")
   turned with its parent. */
.flow-taken[open] > summary .flow-chevron { transform: rotate(45deg); }
.flow-still .flow-node { animation: none; }


@keyframes flow-in   { from { opacity: 0; transform: translateY(-4px) scale(.98); } to { opacity: 1; transform: none; } }
@keyframes flow-turn { to { transform: rotate(360deg); } }
@keyframes flow-pop  { 0% { transform: scale(.6); } 70% { transform: scale(1.12); } 100% { transform: scale(1); } }
@keyframes flow-move { to { background-position: 8px 0; } }
@keyframes flow-pulse {
  0%   { box-shadow: 0 0 0 0 color-mix(in srgb, var(--hue, var(--accent)) 40%, transparent); }
  70%  { box-shadow: 0 0 0 7px color-mix(in srgb, var(--hue, var(--accent)) 0%, transparent); }
  100% { box-shadow: 0 0 0 0 transparent; }
}

@media (prefers-reduced-motion: reduce) {
  .dots i { animation: none; opacity: .6; }
  .transcript { scroll-behavior: auto; }
  .flow-node, .is-done .flow-mark, .flow-edge { animation: none !important; }
  .is-running .flow-mark { animation: none; background: var(--accent); }
  .flow:not(.flow-still) .flow-node.is-running { animation: none; }
  .is-working .nura-light, .msg.is-pending .msg-avatar .nura-light,
  .is-working .nura-glow, .msg.is-pending .msg-avatar .nura-glow,
  .msg.is-pending .msg-avatar { animation: none; }
}
