/* ============================================================
   HiVi Labs — Calculator data-visualisation primitives
   Shared, lightweight SVG viz used by every calculator readout
   (home Velocity + Agent ROI, and the /calculators page's
   Tech-Debt + Automation). Driven by static/js/calculators.js.

   CONTRACT (so markup + JS stay in lockstep):
   - A viz element carries  data-viz="<key>"  matching a key returned
     by that calculator's CALCS[type].viz() in calculators.js.
   - The JS sets the custom property  --pct  (0..1) on that element.
     CSS animates the bar/ring from --pct via transition (no JS rAF
     for the shape — only for the count-up numbers).
   - An optional descendant  [data-viz-num]  is count-up animated by
     the JS to the metric's value (format chosen by the JS viz map).
   - Colour is themed by a modifier class (--warn / --teal / --accent);
     default is neutral. Modifiers set --cviz-color / --cviz-track.

   Everything degrades gracefully: missing key -> stays at 0; no JS ->
   bars/rings sit at their --pct default (0) and the numeric result
   cards still show the real figures.
   ============================================================ */

.cviz {
  --cviz-color: var(--text-faint);
  --cviz-track: var(--bg-inset);
  --pct: 0;
}

/* colour themes ------------------------------------------------ */
.cviz--warn   { --cviz-color: var(--warn);     --cviz-track: rgba(220, 38, 38, 0.12); }
.cviz--teal   { --cviz-color: var(--accent-2); --cviz-track: rgba(13, 148, 136, 0.12); }
.cviz--accent { --cviz-color: var(--accent);   --cviz-track: rgba(59, 130, 246, 0.12); }

/* ============================================================
   Panel: the framed viz block that sits at the top of a readout.
   A ring on the left, a stack of comparison bars on the right.
   Collapses to a single column on narrow widgets.
   ============================================================ */
.cviz-panel {
  display: grid;
  grid-template-columns: auto minmax(0, 1fr);
  gap: clamp(1rem, 2.6vw, 1.75rem);
  align-items: center;
  margin-bottom: clamp(0.9rem, 2vw, 1.25rem);
  padding: clamp(0.9rem, 2vw, 1.25rem);
  border-radius: var(--r-md);
  border: 1px solid var(--glass-hairline);
  background:
    radial-gradient(120% 140% at 0% 0%, rgba(255, 255, 255, 0.7), transparent 60%),
    var(--bg-raised);
  box-shadow: inset 0 1px 0 rgba(255, 255, 255, 0.6);
}
.cviz-panel--bars-only { grid-template-columns: 1fr; }
.cviz-panel__bars { display: grid; gap: clamp(0.7rem, 1.6vw, 1rem); min-width: 0; }

@media (max-width: 520px) {
  .cviz-panel { grid-template-columns: 1fr; justify-items: center; }
  .cviz-panel__bars { width: 100%; }
}

/* ============================================================
   Comparison bar — a labelled, left-anchored SVG fill on a track.
   The fill is an SVG <rect> scaled on X by --pct (left origin), so
   it animates smoothly via a single CSS transition.
   ============================================================ */
.cviz-bar { display: grid; gap: 0.4rem; min-width: 0; }
.cviz-bar__head {
  display: flex; align-items: baseline; justify-content: space-between; gap: 0.75rem;
}
.cviz-bar__name {
  font-family: var(--font-mono); font-size: var(--fs-12);
  letter-spacing: 0.04em; text-transform: uppercase; color: var(--text-lo);
  white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
}
.cviz-bar__val {
  font-family: var(--font-display); font-weight: 600;
  font-size: var(--fs-16); color: var(--cviz-color);
  font-variant-numeric: tabular-nums; letter-spacing: var(--tracking-tight);
  white-space: nowrap;
}
.cviz-bar__track {
  position: relative;
  height: 10px;
  border-radius: var(--r-pill);
  background: var(--cviz-track);
  overflow: hidden;
  box-shadow: inset 0 1px 2px rgba(16, 24, 60, 0.10);
}
.cviz-bar__svg {
  position: absolute; inset: 0;
  width: 100%; height: 100%;
  display: block;
}
.cviz-bar__fill {
  fill: var(--cviz-color);
  transform: scaleX(var(--pct, 0));
  transform-box: view-box;          /* anchor the scale to the viewBox... */
  transform-origin: 0 0;            /* ...left edge */
  transition: transform 0.85s var(--ease-out);
}
/* soft sheen riding on top of the fill for a touch of depth */
.cviz-bar__track::after {
  content: ""; position: absolute; inset: 0; pointer-events: none;
  background: linear-gradient(180deg, rgba(255, 255, 255, 0.45), transparent 45%);
  mix-blend-mode: overlay;
}

/* ============================================================
   Ring gauge — a percentage dial. Two concentric SVG circles;
   the foreground arc is revealed by animating stroke-dashoffset
   from the circumference (326.726 for r=52) down to the --pct arc.
   ============================================================ */
.cviz-ring {
  position: relative;
  width: clamp(116px, 30vw, 136px);
  aspect-ratio: 1;
  display: grid;
  place-items: center;
  flex: none;
}
.cviz-ring__svg { width: 100%; height: 100%; display: block; overflow: visible; }
.cviz-ring__track {
  fill: none; stroke: var(--cviz-track); stroke-width: 10;
}
.cviz-ring__bar {
  fill: none;
  stroke: var(--cviz-color);
  stroke-width: 10;
  stroke-linecap: round;
  stroke-dasharray: 326.726;
  stroke-dashoffset: calc(326.726 * (1 - var(--pct, 0)));
  transition: stroke-dashoffset 0.95s var(--ease-out);
  filter: drop-shadow(0 2px 6px color-mix(in srgb, var(--cviz-color) 35%, transparent));
}
.cviz-ring__center {
  position: absolute; inset: 0;
  display: grid; place-content: center; text-align: center; gap: 0.1rem;
}
.cviz-ring__num {
  font-family: var(--font-display); font-weight: 700;
  font-size: var(--fs-28); line-height: 1; color: var(--cviz-color);
  font-variant-numeric: tabular-nums; letter-spacing: var(--tracking-tight);
}
.cviz-ring__cap {
  font-family: var(--font-mono); font-size: 0.6875rem;
  letter-spacing: 0.06em; text-transform: uppercase; color: var(--text-lo);
  max-width: 9ch; margin-inline: auto; line-height: 1.2;
}

/* ============================================================
   Standalone bars group (no ring) — e.g. a pure comparison block.
   ============================================================ */
.cviz-bars { display: grid; gap: clamp(0.7rem, 1.6vw, 1rem); }

/* ============================================================
   Legend chip row (optional) — tiny colour key under a panel.
   ============================================================ */
.cviz-legend {
  display: flex; flex-wrap: wrap; gap: 0.4rem 1rem;
  margin-top: 0.75rem;
  font-family: var(--font-mono); font-size: var(--fs-12); color: var(--text-lo);
}
.cviz-legend__item { display: inline-flex; align-items: center; gap: 0.45em; }
.cviz-legend__swatch {
  width: 10px; height: 10px; border-radius: 3px; background: var(--cviz-color);
}

/* ============================================================
   Lead-flow correctness: the `hidden` attribute must win over the
   calculators' own display rules. `.calc-success { display:flex }` and
   `.calc-capture { display:grid }` would otherwise override [hidden],
   leaving the success card visible before submit and the capture form
   visible after it. This keeps the unlock toggle behaving as intended.
   ============================================================ */
[data-calc-capture][hidden],
[data-calc-success][hidden] { display: none !important; }

/* ============================================================
   Motion: respect reduced-motion — snap shapes + numbers, no transition.
   ============================================================ */
@media (prefers-reduced-motion: reduce) {
  .cviz-bar__fill { transition: none; }
  .cviz-ring__bar { transition: none; }
}
