/*
 * Torch architecture diagram for byrtus.dev.
 * Visual language from main.torch.cz/about (carved slabs, gold-ish hairline inlay, runes,
 * 14 s staggered pulses, crawling scaffold for work in progress), re-tinted to byrtus.dev
 * tokens (tokens.css: --ink-*, --graphite-*, --signal-*, --copper-*, --cream-50).
 *
 * Everything is scoped to .tarch; colours come only from tokens.css. The caption panel,
 * the text version and the feature block around the diagram are in torch-feature.css.
 */

.tarch {
  /* palette ------------------------------------------------------------- */
  --tarch-bg: var(--ink-900);
  --tarch-frame: var(--ink-700);
  --tarch-slab-client: color-mix(in srgb, var(--ink-900) 50%, var(--ink-800));
  --tarch-slab: var(--ink-800);
  --tarch-slab-gateway: var(--ink-700);
  --tarch-border: var(--ink-500);
  --tarch-border-strong: color-mix(in srgb, var(--ink-500) 70%, var(--copper-500));
  --tarch-text: var(--cream-50);
  --tarch-text-2: var(--graphite-200);
  --tarch-text-3: var(--graphite-300);
  --tarch-accent-strong: var(--signal-500);  /* HTTP pulse */
  --tarch-accent: var(--copper-300);         /* WebSocket pulse */
  --tarch-accent-muted: var(--copper-500);   /* runes, scaffold, planned */
  --tarch-engraving: color-mix(in srgb, var(--signal-500) 8%, transparent);
  --tarch-engraving-lit: color-mix(in srgb, var(--signal-500) 28%, transparent);
  --tarch-focus: var(--signal-500);
  /* type ---------------------------------------------------------------- */
  --tarch-font-display: "Fraunces", "IBM Plex Serif", Georgia, serif;
  --tarch-font-body: "IBM Plex Sans", system-ui, -apple-system, "Segoe UI", sans-serif;
  --tarch-font-mono: "IBM Plex Mono", ui-monospace, monospace;
  /* motion -------------------------------------------------------------- */
  --tarch-pulse-period: 14s;
  --tarch-crawl-period: 9s;
  /* below this width the SVG scrolls sideways instead of shrinking past legibility */
  --tarch-min-width: 46rem;

  margin: 0;
  display: grid;
  gap: 1rem;
  width: 100%;
  min-width: 0;
  color: var(--tarch-text-2);
  font-family: var(--tarch-font-body);
}

.tarch *,
.tarch *::before,
.tarch *::after {
  box-sizing: border-box;
}

/* frame + stage ----------------------------------------------------------- */

.tarch__frame {
  position: relative;
  min-width: 0;
  max-width: 100%;
  border: 1px solid var(--tarch-frame);
  border-radius: 2px;
  background:
    radial-gradient(120% 80% at 50% 0%, color-mix(in srgb, var(--signal-500) 5%, transparent), transparent 60%),
    var(--tarch-bg);
}

.tarch__stage {
  overflow-x: auto;
  overscroll-behavior-x: contain;
  -webkit-overflow-scrolling: touch;
  scrollbar-color: var(--tarch-border) transparent;
  padding: clamp(0.5rem, 2vw, 1.25rem);
}

.tarch__svg {
  display: block;
  width: 100%;
  min-width: var(--tarch-min-width);
  height: auto;
  overflow: visible;
}

.tarch__scroll-hint {
  display: none;
  margin: 0;
  padding: 0 1rem 0.75rem;
  font-family: var(--tarch-font-mono);
  font-size: 0.75rem;
  letter-spacing: 0.04em;
  color: var(--tarch-text-3);
}

@media (max-width: 52rem) {
  .tarch--ready .tarch__scroll-hint {
    display: block;
  }
}

/* connectors -------------------------------------------------------------- */

.tarch__line {
  stroke: var(--tarch-border-strong);
  stroke-width: 1.5;
  stroke-linejoin: round;
  transition: stroke 0.2s, opacity 0.2s;
}

.tarch__line--socket {
  stroke-dasharray: 7 6;
}

.tarch__line--trust {
  stroke-dasharray: 2 7;
  stroke-linecap: round;
  opacity: 0.8;
}

.tarch__line--planned {
  stroke: var(--tarch-accent-muted);
  stroke-dasharray: 8 8;
  opacity: 0.6;
  animation: tarch-crawl var(--tarch-crawl-period) linear infinite;
}

/* dash gap (194) > normalised length (100): the dash never wraps to the far end */
.tarch__pulse {
  stroke-width: 2.5;
  stroke-linecap: round;
  stroke-linejoin: round;
  stroke-dasharray: 6 194;
  opacity: 0;
  animation: tarch-pulse var(--tarch-pulse-period) linear infinite;
  animation-delay: var(--tarch-delay, 0s);
}

.tarch__pulse--http { stroke: var(--tarch-accent-strong); }
.tarch__pulse--socket { stroke: var(--tarch-accent); }
.tarch__pulse--trust { stroke: var(--tarch-text-2); }

@keyframes tarch-pulse {
  0% { stroke-dashoffset: 6; opacity: 0; }
  5% { opacity: 1; }
  24% { opacity: 1; }
  28% { stroke-dashoffset: -106; opacity: 0; }
  100% { stroke-dashoffset: -106; opacity: 0; }
}

@keyframes tarch-crawl {
  to { stroke-dashoffset: -32; }
}

/* nodes ------------------------------------------------------------------- */

.tarch__node {
  cursor: pointer;
  outline: none;
  transition: opacity 0.2s;
}

.tarch__hit {
  fill: transparent;
  stroke: none;
}

.tarch__slab {
  fill: var(--tarch-slab);
  stroke: var(--tarch-border);
  stroke-width: 1;
  filter: drop-shadow(0 1px 0 color-mix(in srgb, var(--ink-900) 45%, transparent)) drop-shadow(0 3px 8px color-mix(in srgb, var(--ink-900) 40%, transparent));
  transition: stroke 0.2s, fill 0.2s;
}

.tarch__node--client .tarch__slab { fill: var(--tarch-slab-client); }

.tarch__node--gateway .tarch__slab {
  fill: var(--tarch-slab-gateway);
  stroke: var(--tarch-border-strong);
}

.tarch__inlay {
  fill: none;
  stroke: var(--tarch-engraving);
  stroke-width: 1;
  transition: stroke 0.2s;
}

.tarch__scaffold {
  fill: none;
  stroke: var(--tarch-accent-muted);
  stroke-width: 1.5;
  stroke-dasharray: 8 8;
  opacity: 0.6;
  animation: tarch-crawl var(--tarch-crawl-period) linear infinite;
}

.tarch__ring {
  fill: none;
  stroke: var(--tarch-focus);
  stroke-width: 2;
  opacity: 0;
}

.tarch__rune {
  fill: none;
  stroke: var(--tarch-accent-muted);
  stroke-width: 1.5;
  stroke-linecap: round;
  stroke-linejoin: round;
  opacity: 0.85;
  filter: drop-shadow(0 1px 0 color-mix(in srgb, var(--ink-900) 75%, transparent));
  transition: stroke 0.2s, opacity 0.2s;
}

.tarch__title {
  fill: var(--tarch-text);
  font-family: var(--tarch-font-display);
  font-size: 19px;
  font-weight: 450;
  text-anchor: middle;
}

.tarch__sub {
  fill: var(--tarch-text-2);
  font-family: var(--tarch-font-body);
  font-size: 13px;
  text-anchor: middle;
}

.tarch__tech {
  fill: var(--tarch-text-3);
  font-family: var(--tarch-font-mono);
  font-size: 11px;
  letter-spacing: 0.04em;
  text-anchor: middle;
}

.tarch__title,
.tarch__sub,
.tarch__tech {
  pointer-events: none;
}

/* interaction states */

.tarch__node:hover .tarch__slab,
.tarch__node.is-active .tarch__slab {
  stroke: var(--tarch-accent);
}

.tarch__node:hover .tarch__inlay,
.tarch__node.is-active .tarch__inlay {
  stroke: var(--tarch-engraving-lit);
}

.tarch__node:hover .tarch__rune,
.tarch__node.is-active .tarch__rune {
  stroke: var(--tarch-accent-strong);
  opacity: 1;
}

.tarch__node.is-related .tarch__slab {
  stroke: var(--tarch-accent-muted);
}

.tarch__node[aria-pressed="true"] .tarch__slab {
  stroke: var(--tarch-accent-strong);
  stroke-width: 1.5;
}

.tarch__node:focus-visible .tarch__ring {
  opacity: 1;
}

.tarch--focus .tarch__node:not(.is-active):not(.is-related) {
  opacity: 0.4;
}

.tarch--focus .tarch__line:not(.is-lit),
.tarch--focus .tarch__pulse:not(.is-lit) {
  opacity: 0.25;
}

.tarch__line.is-lit {
  stroke: var(--tarch-accent);
  opacity: 1;
}

.tarch__line--planned.is-lit {
  stroke: var(--tarch-accent-muted);
}

/* a lit connector pulses three times as often */
.tarch__pulse.is-lit {
  animation-duration: calc(var(--tarch-pulse-period) / 3);
  animation-delay: 0s;
}

.tarch--offscreen .tarch__pulse,
.tarch--offscreen .tarch__scaffold,
.tarch--offscreen .tarch__line--planned {
  animation-play-state: paused;
}

/* legend ------------------------------------------------------------------ */

.tarch__legend {
  display: flex;
  flex-wrap: wrap;
  justify-content: center;
  gap: 0.5rem 1.5rem;
  margin: 0;
  padding: 0 1rem 1rem;
  list-style: none;
  font-size: 0.8rem;
  color: var(--tarch-text-3);
}

.tarch__legend li {
  display: flex;
  align-items: center;
  gap: 0.5rem;
}

.tarch__legend-line {
  flex: none;
  width: 2rem;
  height: 0;
  border-top: 2px solid var(--tarch-border-strong);
}

.tarch__legend-line--http { border-top-color: var(--tarch-accent-strong); }
.tarch__legend-line--socket { border-top-style: dashed; border-top-color: var(--tarch-accent); }
.tarch__legend-line--trust { border-top-style: dotted; border-top-color: var(--tarch-text-2); }
.tarch__legend-line--planned { border-top-style: dashed; border-top-color: var(--tarch-accent-muted); }

/* reduced motion: static lines, no pulse, no crawl ------------------------ */

@media (prefers-reduced-motion: reduce) {
  .tarch__pulse {
    display: none;
  }

  .tarch__scaffold,
  .tarch__line--planned {
    animation: none;
  }

  .tarch__node,
  .tarch__line,
  .tarch__slab,
  .tarch__inlay,
  .tarch__rune {
    transition: none;
  }
}

/* forced colours (Windows high contrast) */
@media (forced-colors: active) {
  .tarch__slab { stroke: CanvasText; }
  .tarch__title, .tarch__sub, .tarch__tech { fill: CanvasText; }
  .tarch__line { stroke: CanvasText; }
  .tarch__ring { stroke: Highlight; }
}
