/* =============================================================================
   explainer.css - shared rig for the Learn explainers (EXPLAINERS-PLAN
   phase 0). Carries the animation primitives proven on the homepage hero
   (same ha-* names so choreography knowledge transfers), the game-style
   tooltip card, and the stepper UI. The homepage keeps its own copies in
   hero.css; only guide pages load this file, so there is no collision.
   ============================================================================= */

/* --- the figure and stepper ------------------------------------------------ */
.explainer {
  margin: var(--space-md) 0 var(--space-lg);
  border: 1px solid var(--border);
  border-radius: var(--radius-lg);
  background: var(--surface-2);
  overflow: hidden;
  --ha-alert: #C0392B;
  --ex-copper: #C46A2B;
}
[data-theme="dark"] .explainer { --ha-alert: #F2777A; --ex-copper: #D98A4C; }
.explainer svg { width: 100%; height: auto; display: block; }
.ex-controls {
  display: flex; gap: 8px; align-items: center;
  padding: var(--space-2xs) var(--space-sm) var(--space-sm);
}
.ex-step {
  font: inherit; font-family: var(--font-mono); font-size: var(--step--2);
  padding: 4px 12px; border-radius: var(--radius-pill);
  border: 1px solid var(--border); background: var(--surface);
  color: var(--text-muted); cursor: pointer;
}
.ex-step:hover { border-color: var(--primary); }
.ex-step.is-on { background: var(--primary); color: var(--primary-ink); border-color: var(--primary); }
.ex-replay { margin-left: auto; }

/* --- step gating: elements tagged ex-s1..ex-s6 exist only in their step.
   Exact tokens only: a substring match here would swallow unrelated ex-s*
   paint classes (ex-star, ex-silk, ex-spider) and hide them in every step. */
.explainer svg .ex-s1, .explainer svg .ex-s2, .explainer svg .ex-s3,
.explainer svg .ex-s4, .explainer svg .ex-s5, .explainer svg .ex-s6 { visibility: hidden; }
.explainer[data-step="1"] svg .ex-s1,
.explainer[data-step="2"] svg .ex-s2,
.explainer[data-step="3"] svg .ex-s3,
.explainer[data-step="4"] svg .ex-s4,
.explainer[data-step="5"] svg .ex-s5,
.explainer[data-step="6"] svg .ex-s6 { visibility: visible; }

/* --- animation primitives (hero lineage) ----------------------------------- */
.ha-ln      { fill: none; stroke: var(--primary); stroke-width: 2.5; stroke-linecap: round; stroke-linejoin: round; }
.ha-ln--soft{ fill: none; stroke: color-mix(in srgb, var(--primary) 55%, transparent); stroke-width: 2; stroke-linecap: round; stroke-linejoin: round; }
.ha-ln--hair{ fill: none; stroke: color-mix(in srgb, var(--primary) 32%, transparent); stroke-width: 1.4; }
.ha-wire    { stroke-width: 3.5; }
.ha-fill    { fill: color-mix(in srgb, var(--primary) 10%, transparent); }
.ha-tag     { font-family: var(--font-mono); font-size: 21px; letter-spacing: 0.1em; fill: var(--text-muted); }
.ha-tag--sm { font-size: 16px; }
.ha-tag--on { fill: var(--primary); }
.ha-lbl     { font-family: var(--font-mono); font-size: 14px; letter-spacing: 0.09em; fill: var(--text-muted); opacity: 0.55; }
.ha-lbl--alert { fill: var(--ha-alert); opacity: 0.85; }
.ha-chipbg  { fill: color-mix(in srgb, var(--surface) 93%, transparent); stroke: color-mix(in srgb, var(--primary) 40%, var(--border)); }
.ha-keystone{ fill: color-mix(in srgb, var(--primary) 18%, transparent); stroke: color-mix(in srgb, var(--primary) 45%, transparent); stroke-width: 1; }
.ha-dot     { fill: var(--primary); }
.ha-led     { fill: var(--secondary); }
.ha-pulse   { fill: var(--accent); }
.ha-alertw  { fill: none; stroke: var(--ha-alert); stroke-width: 2.5; }

.ha-draw {
  stroke-dasharray: 1; stroke-dashoffset: 1;
  animation: haDraw 1s cubic-bezier(0.4, 0, 0.2, 1) forwards;
  animation-delay: var(--d, 0s);
}
.ha-draw--slow { animation-duration: 1.6s; }
.ha-pop {
  opacity: 0;
  animation: haPop 0.5s ease-out forwards;
  animation-delay: var(--d, 0s);
}
.ha-pop--spring {
  opacity: 0;
  transform-box: fill-box; transform-origin: center;
  animation: haSpring 0.55s cubic-bezier(0.34, 1.56, 0.64, 1) forwards;
  animation-delay: var(--d, 0s);
}
.ha-ring {
  fill: none; stroke: var(--secondary); stroke-width: 2; opacity: 0;
  transform-box: fill-box; transform-origin: center;
  animation: haRing 0.9s ease-out forwards;
  animation-delay: var(--d, 0s);
}
.ha-flash {
  fill: none; stroke: var(--ha-alert); stroke-width: 3; opacity: 0;
  transform-box: fill-box; transform-origin: center;
  animation: haRing 0.7s ease-out forwards;
  animation-delay: var(--d, 0s);
}
@keyframes haDraw   { to { stroke-dashoffset: 0; } }
@keyframes haPop    { from { opacity: 0; } to { opacity: 1; } }
@keyframes haSpring {
  0%   { opacity: 0; transform: scale(0.5); }
  70%  { opacity: 1; transform: scale(1.08); }
  100% { opacity: 1; transform: scale(1); }
}
@keyframes haRing {
  0%   { opacity: 0.9; transform: scale(0.4); }
  100% { opacity: 0;   transform: scale(2.6); }
}

/* coverage visuals (hero lineage) */
.ha-fov       { fill: color-mix(in srgb, var(--primary) 18%, transparent); stroke: color-mix(in srgb, var(--primary) 28%, transparent); stroke-width: 1; }
.ha-blindwash { fill: color-mix(in srgb, var(--ha-alert) 15%, transparent); }
.ha-lawn      { fill: color-mix(in srgb, var(--secondary) 8%, transparent); }

/* --- explainer-specific paints --------------------------------------------- */
.ex-copper   { fill: var(--ex-copper); }
.ex-copper-ln{ fill: none; stroke: var(--ex-copper); stroke-linecap: round; }
.ex-alu      { fill: color-mix(in srgb, var(--text-muted) 40%, transparent); }
.ex-ok-text  { fill: var(--secondary); }
.ex-bad-text { fill: var(--ha-alert); }
.ex-heat {
  fill: var(--ha-alert);
  animation: exHeat 1.5s ease-in-out infinite alternate;
}
@keyframes exHeat { from { fill-opacity: 0.05; } to { fill-opacity: 0.3; } }
.ex-bolt { fill: var(--ha-alert); stroke: none; }
.ex-grey { fill: color-mix(in srgb, var(--text-muted) 45%, transparent); }

/* event tag (pointer chip that announces, then hides via its own SMIL) */
.ha-evtag rect { fill: color-mix(in srgb, var(--surface) 92%, transparent); stroke: var(--ha-alert); }
.ha-evtag text { font-family: var(--font-mono); font-size: 14px; letter-spacing: 0.06em; fill: var(--ha-alert); }
.ha-evtag path { fill: var(--ha-alert); }
.ha-evtag--ok rect { stroke: var(--secondary); }
.ha-evtag--ok text { fill: var(--secondary); }
.ha-evtag--ok path { fill: var(--secondary); }

/* night scene (spiders explainer). The panel is night in both themes, so
   everything painted on it uses fixed inks instead of theme vars. */
.ex-night    { fill: #0D1524; stroke: #26344C; }
.ex-star     { fill: #C9D6EC; }
.ex-moon     { fill: #D9E2F0; }
.ex-nightln  { fill: none; stroke: #7E92B3; stroke-width: 2; stroke-linecap: round; stroke-linejoin: round; }
.ex-nightfill{ fill: rgba(126, 146, 179, 0.10); }
.ex-nightlbl { font-family: var(--font-mono); font-size: 14px; letter-spacing: 0.09em; fill: #93A7C4; }
.ex-nightfov { fill: rgba(122, 162, 247, 0.10); stroke: rgba(122, 162, 247, 0.22); stroke-width: 1; }
.ex-ir       { fill: none; stroke: #B03A2E; stroke-width: 2.5; }
.ex-irhalo   { fill: #B03A2E; animation: exIrPulse 1.6s ease-in-out infinite alternate; }
.ex-bug      { fill: #D8C878; }
.ex-web      { fill: none; stroke: #C8D4E6; stroke-width: 1; opacity: 0.85; }
.ex-bloom    { fill: #E8EEF7; }
.ex-spider   { fill: #8FA1B8; }
.ex-silk     { fill: none; stroke: #C8D4E6; stroke-width: 1; opacity: 0.6; }
.ex-beam     { fill: rgba(216, 200, 120, 0.13); stroke: rgba(216, 200, 120, 0.25); stroke-width: 1; }
.ex-lamp     { fill: rgba(216, 200, 120, 0.35); }
@keyframes exIrPulse { from { opacity: 0.25; } to { opacity: 0.6; } }

/* --- interaction ------------------------------------------------------------
   The descendant rule matters: a bare g.ha-click has no geometry, so hit
   testing lands on its children; without re-enabling THEM, every g-wrapped
   tooltip is dead to a real mouse (synthetic dispatch hides this). */
:where(.explainer) svg * { pointer-events: none; }
.explainer .ha-click, .explainer .ha-click * { pointer-events: auto; }
.explainer .ha-click { cursor: pointer; }
.explainer .ha-click:hover { filter: brightness(1.3); }

/* --- the tooltip card (shared with the hero look) --------------------------- */
.twin-tip {
  position: fixed; z-index: 60; max-width: 340px; pointer-events: none;
  background: color-mix(in srgb, var(--surface) 96%, transparent);
  border: 1px solid color-mix(in srgb, var(--primary) 35%, var(--border));
  border-top: 2px solid var(--accent);
  border-radius: var(--radius-md);
  box-shadow: 0 14px 36px rgba(0, 0, 0, 0.35);
  padding: var(--space-sm) var(--space-md);
}
.twin-tip .tt-kind {
  font-family: var(--font-mono); font-size: 0.66rem; letter-spacing: 0.14em;
  text-transform: uppercase; color: var(--text-muted);
}
.twin-tip h3 { font-size: var(--step-1); margin: 2px 0 2px; }
.twin-tip .tm-status { margin: 0 0 2px; }
.twin-tip ul {
  list-style: none; padding: 0; margin: 6px 0 0;
  display: flex; flex-direction: column; gap: 4px;
  font-size: var(--step--1); color: var(--text-muted);
}
.twin-tip li { padding-left: 14px; position: relative; }
.twin-tip li::before { content: "-"; position: absolute; left: 0; color: var(--primary); }
.tm-status { font-family: var(--font-mono); font-size: var(--step--1); letter-spacing: 0.08em; }
.tm-ok   { color: var(--secondary); }
.tm-warn { color: var(--accent-ink); }
.tm-info { color: var(--primary); }
.tm-bad  { color: #C0392B; }
[data-theme="dark"] .tm-bad { color: #F2777A; }

/* --- accessibility ----------------------------------------------------------- */
@media (prefers-reduced-motion: reduce) {
  .explainer svg .ha-draw, .explainer svg .ha-pop, .explainer svg .ha-pop--spring {
    animation: none; opacity: 1; stroke-dashoffset: 0;
  }
  .explainer svg .ha-ring, .explainer svg .ha-flash { display: none; }
  .explainer svg .ex-heat { animation: none; fill-opacity: 0.2; }
  .explainer svg .ex-irhalo { animation: none; opacity: 0.4; }
  .explainer svg .ex-smil { display: none; }
}
