/* ==========================================================================
   Declaration flow diagram

   How CAT360 reads a declaration, in one picture: CDS report lines on the
   left, the reasoning layer in the middle, findings with citations on the
   right. Used on the homepage (#How, dark) and /for-brokers (light).

   The final state is the default. Nothing is hidden unless decl-flow.js has
   run and set data-animate, so with JS off, under reduced motion, or for a
   crawler, the whole diagram is simply there. The wires between columns are
   drawn by the script from measured positions; the CSS chevrons between
   stages are the fallback and the stacked mobile connector.
   ========================================================================== */

.decl-flow {
  --df-surface: var(--white);
  --df-border: var(--purple-tint-3);
  --df-text: var(--purple-primary);
  --df-muted: var(--text-muted);
  --df-heading: var(--purple-heading);
  --df-row-alt: var(--off-white);
  --df-skeleton: var(--purple-tint-2);
  --df-chip: var(--purple-tint-1);
  --df-chip-text: var(--purple-heading);
  --df-chip-lit: var(--purple-accent);
  --df-wire: var(--purple-tint-2);
  --df-shadow: var(--shadow-sm);
  --df-reclaim: var(--gold);
  --df-reclaim-text: var(--gold-text);
  --df-error: #c8364a;
  --df-evidence: var(--purple-accent);
  --df-ease: cubic-bezier(0.22, 1, 0.36, 1);
  --df-mono: ui-monospace, 'SF Mono', SFMono-Regular, Menlo, Consolas, monospace;

  position: relative;
  margin: var(--space-xl) 0 0;
  color: var(--df-text);
}

.decl-flow--dark {
  --df-surface: rgba(255, 255, 255, 0.06);
  --df-border: rgba(255, 255, 255, 0.14);
  --df-text: var(--white);
  --df-muted: rgba(255, 255, 255, 0.72);
  --df-heading: var(--white);
  --df-row-alt: rgba(255, 255, 255, 0.04);
  --df-skeleton: rgba(255, 255, 255, 0.14);
  --df-chip: rgba(255, 255, 255, 0.08);
  --df-chip-text: rgba(255, 255, 255, 0.85);
  --df-chip-lit: var(--gold);
  --df-wire: rgba(255, 255, 255, 0.16);
  --df-shadow: 0 20px 50px rgba(20, 5, 28, 0.35);
  --df-evidence: #b28cff;
}

/* ── Stages ─────────────────────────────────────────────────────────────── */

.decl-flow__stages {
  list-style: none;
  margin: 0;
  padding: 0;
  display: grid;
  grid-template-columns: 1.1fr 0.95fr 1.15fr;
  grid-template-rows: auto auto 1fr;
  column-gap: clamp(3rem, 6vw, 5.5rem);
  row-gap: var(--space-xs);
}

.decl-flow__stage {
  position: relative;
  display: grid;
  grid-row: span 3;
  grid-template-rows: subgrid;
  min-width: 0;
}

.decl-flow__label {
  display: flex;
  align-items: center;
  gap: 0.625rem;
}

.decl-flow__label h3 {
  margin: 0;
  font-size: 1.125rem;
  line-height: 1.3;
  color: var(--df-heading);
}

.decl-flow__num {
  flex: none;
  display: inline-grid;
  place-items: center;
  width: 1.75rem;
  height: 1.75rem;
  border-radius: 50%;
  background: var(--purple-accent);
  color: var(--white);
  font-size: 0.8125rem;
  font-weight: 700;
}

.decl-flow--dark .decl-flow__num {
  background: var(--gold);
  color: var(--purple-dark);
}

.decl-flow__desc {
  margin: 0 0 var(--space-sm);
  font-size: 0.9375rem;
  line-height: 1.55;
  color: var(--df-muted);
}

.decl-flow__panel {
  position: relative;
  background: var(--df-surface);
  border: 1px solid var(--df-border);
  border-radius: var(--radius-lg);
  box-shadow: var(--df-shadow);
}

.decl-flow--dark .decl-flow__panel {
  -webkit-backdrop-filter: blur(6px);
  backdrop-filter: blur(6px);
}

/* Fallback chevrons between stages. Hidden once the script draws wires. */
.decl-flow__stage:not(:last-child)::after {
  content: '';
  position: absolute;
  top: 62%;
  right: calc(clamp(3rem, 6vw, 5.5rem) / -2 - 0.5rem);
  width: 1rem;
  height: 1rem;
  border-top: 2px solid var(--df-chip-lit);
  border-right: 2px solid var(--df-chip-lit);
  transform: rotate(45deg);
  opacity: 0.7;
}

.decl-flow.has-wires .decl-flow__stage::after {
  display: none;
}

/* The findings set the height of the panel row; the other two sit centred
   against it so the wires fan out and back in symmetrically. */
.decl-flow__report,
.decl-flow__engine {
  align-self: center;
}

/* ── Stage 1: the report ────────────────────────────────────────────────── */

.decl-flow__report {
  padding: var(--space-sm);
  overflow: hidden;
}

.decl-flow__files {
  display: flex;
  flex-wrap: wrap;
  gap: 0.375rem;
  margin-bottom: 0.75rem;
}

.decl-flow__file {
  display: inline-flex;
  align-items: center;
  gap: 0.375rem;
  padding: 0.25rem 0.5rem;
  border-radius: var(--radius-sm);
  background: var(--df-chip);
  color: var(--df-chip-text);
  font-size: 0.6875rem;
  font-weight: 600;
  letter-spacing: 0.01em;
}

.decl-flow__file::before {
  content: '';
  width: 0.5rem;
  height: 0.625rem;
  border: 1.5px solid currentColor;
  border-radius: 1px;
  opacity: 0.7;
}

.decl-flow__table {
  display: grid;
  gap: 2px;
  font-size: 0.75rem;
}

.decl-flow__row {
  position: relative;
  display: grid;
  grid-template-columns: 2.25rem 1fr 2.25rem 4.5rem;
  align-items: center;
  gap: 0.5rem;
  min-height: 2.125rem;
  padding: 0 0.625rem;
  border-radius: 6px;
  background: var(--df-row-alt);
  font-variant-numeric: tabular-nums;
}

.decl-flow__row > :last-child {
  text-align: right;
}

.decl-flow__row--head {
  min-height: 1.75rem;
  background: none;
  color: var(--df-muted);
  font-size: 0.625rem;
  font-weight: 700;
  letter-spacing: 0.06em;
  text-transform: uppercase;
}

.decl-flow__code {
  font-family: var(--df-mono);
  font-size: 0.75rem;
  white-space: nowrap;
}

.decl-flow__bar {
  display: block;
  height: 0.4375rem;
  border-radius: 99px;
  background: var(--df-skeleton);
}

.decl-flow__bar--s { width: 55%; }
.decl-flow__bar--m { width: 75%; }
.decl-flow__bar--l { width: 90%; }
.decl-flow__row > :last-child .decl-flow__bar { margin-left: auto; }

/* Flag marker on the rows a finding came from. */
.decl-flow__row--flag::before {
  content: '';
  position: absolute;
  left: 0;
  top: 0.3125rem;
  bottom: 0.3125rem;
  width: 3px;
  border-radius: 3px;
  background: var(--df-flag);
}

.decl-flow__row--flag {
  background: color-mix(in srgb, var(--df-flag) 12%, transparent);
  box-shadow: inset 0 0 0 1px color-mix(in srgb, var(--df-flag) 35%, transparent);
}

[data-flag='reclaim'] { --df-flag: var(--df-reclaim); }
[data-flag='error'] { --df-flag: var(--df-error); }
[data-flag='evidence'] { --df-flag: var(--df-evidence); }

.decl-flow__more {
  margin-top: 0.625rem;
  font-size: 0.6875rem;
  color: var(--df-muted);
  text-align: center;
}

/* ── Stage 2: the reasoning layer ───────────────────────────────────────── */

.decl-flow__engine {
  display: flex;
  flex-direction: column;
  justify-content: center;
  gap: var(--space-sm);
  padding: var(--space-md) var(--space-sm);
}

.decl-flow__core {
  position: relative;
  display: grid;
  place-items: center;
  align-self: center;
  width: 7.5rem;
  height: 7.5rem;
  border-radius: 50%;
  background:
    radial-gradient(circle at 30% 25%, rgba(255, 255, 255, 0.18), transparent 60%),
    linear-gradient(145deg, var(--purple-accent), var(--purple-dark));
  color: var(--white);
  text-align: center;
  box-shadow: 0 10px 30px rgba(133, 71, 250, 0.35);
}

.decl-flow__core::before,
.decl-flow__core::after {
  content: '';
  position: absolute;
  inset: -0.5rem;
  border-radius: 50%;
  border: 1px solid color-mix(in srgb, var(--purple-accent) 45%, transparent);
}

.decl-flow__core::after {
  inset: -1.125rem;
  border-style: dashed;
  border-color: color-mix(in srgb, var(--gold) 45%, transparent);
}

.decl-flow__mark {
  display: block;
  font-size: 1.0625rem;
  font-weight: 700;
  letter-spacing: 0.14em;
}

.decl-flow__mark small {
  display: block;
  margin-top: 0.125rem;
  font-size: 0.5625rem;
  font-weight: 600;
  letter-spacing: 0.08em;
  opacity: 0.8;
  text-transform: uppercase;
}

.decl-flow__sources {
  list-style: none;
  margin: var(--space-sm) 0 0;
  padding: 0;
  display: grid;
  gap: 0.375rem;
}

.decl-flow__source {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  padding: 0.4375rem 0.625rem;
  border-radius: 99px;
  background: var(--df-chip);
  color: var(--df-chip-text);
  font-size: 0.75rem;
  font-weight: 600;
  line-height: 1.2;
}

.decl-flow__source::before {
  content: '';
  flex: none;
  width: 0.4375rem;
  height: 0.4375rem;
  border-radius: 50%;
  background: var(--df-chip-lit);
}

/* ── Stage 3: findings ──────────────────────────────────────────────────── */

.decl-flow__findings {
  list-style: none;
  margin: 0;
  padding: 0;
  display: grid;
  align-content: space-between;
  gap: 0.75rem;
}

.decl-flow__finding {
  position: relative;
  padding: 0.875rem 1rem 0.875rem 1.125rem;
  border-radius: var(--radius-lg);
  background: var(--white);
  border: 1px solid var(--purple-tint-3);
  box-shadow: var(--shadow-sm);
  color: var(--purple-primary);
  overflow: hidden;
}

.decl-flow--dark .decl-flow__finding {
  border-color: transparent;
  box-shadow: var(--df-shadow);
}

.decl-flow__finding::before {
  content: '';
  position: absolute;
  left: 0;
  top: 0;
  bottom: 0;
  width: 4px;
  background: var(--df-flag);
}

.decl-flow__tag {
  display: inline-block;
  margin-bottom: 0.375rem;
  padding: 0.125rem 0.5rem;
  border-radius: 99px;
  background: color-mix(in srgb, var(--df-flag) 14%, white);
  color: var(--df-flag-text, var(--df-flag));
  font-size: 0.625rem;
  font-weight: 700;
  letter-spacing: 0.06em;
  text-transform: uppercase;
}

.decl-flow__finding[data-flag='reclaim'] { --df-flag-text: var(--gold-text); }
.decl-flow__finding[data-flag='evidence'] { --df-flag: var(--purple-accent); }

.decl-flow__finding-title {
  margin: 0 0 0.25rem;
  font-size: 0.9375rem;
  font-weight: 700;
  line-height: 1.3;
  color: var(--purple-heading);
}

.decl-flow__finding-body {
  margin: 0 0 0.5rem;
  font-size: 0.8125rem;
  line-height: 1.5;
  color: var(--purple-primary);
}

.decl-flow__cite {
  display: flex;
  gap: 0.375rem;
  margin: 0;
  padding-top: 0.5rem;
  border-top: 1px dashed var(--purple-tint-2);
  font-size: 0.6875rem;
  line-height: 1.45;
  color: var(--text-muted);
}

.decl-flow__cite::before {
  content: '';
  flex: none;
  width: 0.75rem;
  height: 0.75rem;
  margin-top: 0.0625rem;
  background: currentColor;
  -webkit-mask: var(--df-cite-icon) center / contain no-repeat;
  mask: var(--df-cite-icon) center / contain no-repeat;
  --df-cite-icon: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 16 16' fill='none' stroke='black' stroke-width='1.6' stroke-linecap='round' stroke-linejoin='round'%3E%3Cpath d='M7 9a3 3 0 0 0 4.2.3l2-2a3 3 0 0 0-4.2-4.2l-1 1'/%3E%3Cpath d='M9 7a3 3 0 0 0-4.2-.3l-2 2A3 3 0 0 0 7 12.9l1-1'/%3E%3C/svg%3E");
}

.decl-flow__cite strong {
  font-weight: 600;
  color: var(--purple-heading);
}

/* ── Caption and replay ─────────────────────────────────────────────────── */

.decl-flow__caption {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  justify-content: center;
  gap: 0.5rem 1rem;
  margin-top: var(--space-md);
  font-size: 0.8125rem;
  color: var(--df-muted);
  text-align: center;
}

.decl-flow__replay {
  display: none;
  align-items: center;
  gap: 0.375rem;
  min-height: 2.25rem;
  padding: 0.375rem 0.875rem;
  border: 1px solid var(--df-border);
  border-radius: 99px;
  background: transparent;
  color: inherit;
  font: inherit;
  font-weight: 600;
  cursor: pointer;
}

.decl-flow__replay:hover {
  border-color: var(--df-chip-lit);
  color: var(--df-heading);
}

.decl-flow[data-animate] .decl-flow__replay {
  display: inline-flex;
}

/* ── Wires ──────────────────────────────────────────────────────────────── */

.decl-flow__wires {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  overflow: visible;
  pointer-events: none;
}

.decl-flow__wire {
  fill: none;
  stroke: var(--df-wire);
  stroke-width: 1.5;
}

.decl-flow__wire--lit {
  stroke: var(--df-flag);
  stroke-width: 2;
  stroke-linecap: round;
}

.decl-flow__packet {
  fill: none;
  stroke: var(--df-flag);
  stroke-width: 6;
  stroke-linecap: round;
  opacity: 0;
}

/* ── Animation ──────────────────────────────────────────────────────────────
   Only when the script opts in with data-animate. .is-playing is the end
   state; every element transitions to it on its own delay, so one class
   change runs the whole sequence and removing it resets for a replay.
   Timings (ms): rows 300-1000, flags 1100, left wires 1350, sources 2200,
   right wires 3050, findings 3700.
   -------------------------------------------------------------------------- */

.decl-flow[data-animate] .decl-flow__row,
.decl-flow[data-animate] .decl-flow__file,
.decl-flow[data-animate] .decl-flow__core,
.decl-flow[data-animate] .decl-flow__source,
.decl-flow[data-animate] .decl-flow__finding {
  transition:
    opacity 0.7s var(--df-ease),
    transform 0.8s var(--df-ease),
    background-color 0.6s ease,
    box-shadow 0.6s ease;
}

.decl-flow[data-animate]:not(.is-playing) .decl-flow__file,
.decl-flow[data-animate]:not(.is-playing) .decl-flow__row {
  opacity: 0;
  transform: translateX(-1rem);
}

.decl-flow[data-animate]:not(.is-playing) .decl-flow__row--flag {
  background: var(--df-row-alt);
  box-shadow: none;
}

.decl-flow[data-animate]:not(.is-playing) .decl-flow__row--flag::before {
  transform: scaleY(0);
}

.decl-flow[data-animate] .decl-flow__row--flag::before {
  transition: transform 0.6s var(--df-ease);
}

.decl-flow[data-animate]:not(.is-playing) .decl-flow__core {
  opacity: 0.5;
  transform: scale(0.9);
}

.decl-flow[data-animate]:not(.is-playing) .decl-flow__source {
  opacity: 0.35;
}

.decl-flow[data-animate]:not(.is-playing) .decl-flow__source::before {
  background: var(--df-skeleton);
}

/* Waiting findings show as faint slots, so the wires have somewhere to go. */
.decl-flow[data-animate]:not(.is-playing) .decl-flow__finding {
  opacity: 0.1;
  transform: translateX(0.75rem) scale(0.98);
}

.decl-flow[data-animate] .decl-flow__finding > * {
  transition: opacity 0.45s ease;
}

.decl-flow[data-animate]:not(.is-playing) .decl-flow__finding > * {
  opacity: 0;
}

.decl-flow.is-playing .decl-flow__finding > * {
  transition-delay: calc(3900ms + var(--i, 0) * 260ms);
}

.decl-flow.is-playing .decl-flow__file { transition-delay: calc(var(--i, 0) * 120ms); }
.decl-flow.is-playing .decl-flow__row { transition-delay: calc(300ms + var(--i, 0) * 110ms); }
.decl-flow.is-playing .decl-flow__row--flag {
  transition-delay: calc(300ms + var(--i, 0) * 110ms), calc(300ms + var(--i, 0) * 110ms), calc(1100ms + var(--f, 0) * 220ms), calc(1100ms + var(--f, 0) * 220ms);
}
.decl-flow.is-playing .decl-flow__row--flag::before { transition-delay: calc(1100ms + var(--f, 0) * 220ms); }
.decl-flow.is-playing .decl-flow__core { transition-delay: 2000ms; }
.decl-flow.is-playing .decl-flow__source { transition-delay: calc(2200ms + var(--i, 0) * 160ms); }
.decl-flow.is-playing .decl-flow__finding { transition-delay: calc(3700ms + var(--i, 0) * 260ms); }

.decl-flow.is-playing .decl-flow__core {
  animation: decl-flow-pulse 1.8s 2.2s var(--df-ease) 1;
}

@keyframes decl-flow-pulse {
  0% { box-shadow: 0 10px 30px rgba(133, 71, 250, 0.35), 0 0 0 0 rgba(223, 164, 55, 0.5); }
  70% { box-shadow: 0 10px 30px rgba(133, 71, 250, 0.35), 0 0 0 1.75rem rgba(223, 164, 55, 0); }
  100% { box-shadow: 0 10px 30px rgba(133, 71, 250, 0.35), 0 0 0 0 rgba(223, 164, 55, 0); }
}

/* ── Stacked layout ─────────────────────────────────────────────────────── */

@media (max-width: 959px) {
  .decl-flow__stages {
    grid-template-columns: minmax(0, 34rem);
    grid-template-rows: none;
    justify-content: center;
    row-gap: 3.5rem;
  }

  .decl-flow__stage {
    grid-row: auto;
    grid-template-rows: none;
  }

  .decl-flow__stage:not(:last-child)::after {
    top: auto;
    right: auto;
    bottom: -2.25rem;
    left: calc(50% - 0.5rem);
    transform: rotate(135deg);
  }

  .decl-flow__stage:not(:last-child)::before {
    content: '';
    position: absolute;
    left: 50%;
    bottom: -3rem;
    height: 2.25rem;
    border-left: 2px dashed var(--df-wire);
  }

  .decl-flow.has-wires .decl-flow__stage::after {
    display: block;
  }

  .decl-flow__engine {
    flex-direction: row;
    align-items: center;
    gap: var(--space-md);
  }

  .decl-flow__core {
    width: 6rem;
    height: 6rem;
    margin: 1.125rem;
  }

  .decl-flow__sources {
    flex: 1;
    margin-top: 0;
  }
}

@media (max-width: 479px) {
  .decl-flow__row {
    grid-template-columns: 1.75rem 1fr 1.75rem 3.75rem;
    gap: 0.375rem;
    padding: 0 0.5rem;
  }

  .decl-flow__code { font-size: 0.6875rem; }

  .decl-flow__engine {
    flex-direction: column;
  }

  .decl-flow__sources {
    width: 100%;
    grid-template-columns: 1fr 1fr;
  }
}
