/* ==========================================================================
   BAR COMPARE — true-scale bar charts   (visual only)
   ==========================================================================
   Two related chart shapes, one file:

   A) .bar-compare — multi-row comparison. Each .bc-row = a head (label left /
      value right) over a proportional bar. Hairline rules between rows; the
      "winning" row (.bc-row--hi) is heading-navy with a prime->ink gradient bar.
      Optional 12px grey .bc-note caption.

   B) .bar-stack — one rail split into segments (grey -> grey -> navy gradient),
      pill ends, a swatch legend below, and a 19px body .bs-note caption.

   Both are a single `text` content element (all <div>/<span>/<p> — no inline
   <svg>, so Contao output keeps it). Bar widths are true-scale, set per-bar as
   inline `style="width:N%"`. The component takes the width of its column.

   Build: the markup (engine/cornerstone.bar-compare.spec.md has worked
   examples) in a text element, usually on a light band. The bars fill on
   scroll-in via the j_reveal template; the value labels count up with them.
   ========================================================================== */

:root {
    /* Neutral ramp for the "lesser" bars/segments; a site may re-key it. */
    --bc-g1: #C4CCD6;   /* lightest */
    --bc-g2: #9AA5B3;   /* mid grey */
    /* Highlight fill: prime -> prime-deep, light on the left. */
    --bc-hi: linear-gradient(90deg, var(--prime) 0%, var(--prime-deep) 100%);
    --bc-radius: 10px;  /* pill ends */
}

/* On a DARK band a navy bar is invisible: the lesser bars go mid-brand and the
   highlight runs the other way, deep at the root brightening to the tip. */
.bg-ink .bar-compare,
.bg-dark .bar-compare {
    --bc-g1: color-mix(in srgb, var(--prime) 65%, white);
    --bc-g2: color-mix(in srgb, var(--prime) 65%, white);
    --bc-hi: linear-gradient(90deg, var(--prime) 0%, color-mix(in srgb, var(--prime) 40%, white) 100%);
}

/* ==========================================================================
   A) MULTI-ROW COMPARISON
   ========================================================================== */
.bar-compare { display: flex; flex-direction: column; }

.bc-row { padding: 0.75rem 0 1rem; }
.bc-row + .bc-row,
.bar-compare .bc-note { border-top: 1px solid var(--hairline); }
.bc-row:first-child { padding-top: 0.25rem; }

/* Head: label left, value right, aligned on their baseline. */
.bc-head {
    display: flex; align-items: baseline; justify-content: space-between;
    gap: 1rem; margin-bottom: 0.7rem;
}
.bc-label {
    font-family: var(--type-body); font-weight: 500;
    font-size: clamp(1rem, 1.15vw, 1.1875rem);   /* 19px */
    color: var(--text-body); line-height: 1.2;
}
.bc-val {
    font-family: var(--type-body); font-weight: 700;
    font-size: clamp(1.35rem, 1.7vw, 1.6875rem);  /* 27px */
    color: var(--text-body); white-space: nowrap; line-height: 1;
}

/* Track + fill. Track is the full available width; fill is true-scale. */
.bc-track { width: 100%; }
.bc-fill {
    display: block; height: 30px; width: 0;
    border-radius: var(--bc-radius);
    background: var(--bc-g2);
}
.bc-fill--g1 { background: var(--bc-g1); }
.bc-fill--g2 { background: var(--bc-g2); }
.bc-fill--hi { background: var(--bc-hi); height: 35px; }

/* Highlight row: heading-navy text; value a touch smaller . */
.bc-row--hi {
    .bc-label { color: var(--heading); }
    .bc-val { color: var(--heading); font-size: clamp(1.25rem, 1.5vw, 1.5rem); }
} /* 24px */

/* Caption. */
.bc-note {
    margin: 0; padding-top: 0.9rem;
    font-family: var(--type-body);
    font-size: 0.75rem; color: var(--text-muted); line-height: 1.4;
}

/* ==========================================================================
   B) STACKED BAR + LEGEND
   ========================================================================== */
.bar-stack { display: flex; flex-direction: column; }

.bs-track {
    display: flex; align-items: stretch; gap: 3px;
    width: 100%; height: 61px;
}
.bs-seg {
    display: block; height: 100%; min-width: 2px;
    background: var(--bc-g2);
}
.bs-seg--g1 { background: var(--bc-g1); }
.bs-seg--g2 { background: var(--bc-g2); }
.bs-seg--hi { background: var(--bc-hi); }
.bs-seg:first-child { border-radius: var(--bc-radius) 0 0 var(--bc-radius); }
.bs-seg:last-child { border-radius: 0 var(--bc-radius) var(--bc-radius) 0; }

/* Legend. */
.bs-legend {
    display: flex; flex-wrap: wrap; align-items: center;
    gap: 0.5rem 2rem; margin-top: 1.25rem;
    font-family: var(--type-body);
    font-size: clamp(0.95rem, 1.05vw, 1.0625rem);
    color: var(--text-body);
}
.bs-key { display: inline-flex; align-items: center; gap: 0.55rem; white-space: nowrap; }
.bs-key b { color: var(--heading); font-weight: 600; }
.bs-dot {
    flex: none; width: 14px; height: 14px; border-radius: 4px;
    background: var(--bc-g2);
}
.bs-dot--g1 { background: var(--bc-g1); }
.bs-dot--g2 { background: var(--bc-g2); }
.bs-dot--hi { background: var(--bc-hi); }

/* Stack caption is body-size (19px), not the small scenario note. */
.bs-note {
    margin: 1.25rem 0 0;
    font-family: var(--type-body);
    font-size: clamp(1rem, 1.15vw, 1.1875rem);
    color: var(--text-body); line-height: 1.45;
}

/* ==========================================================================
   SHARED — chart title (sits above a .bar-stack / .bar-col)
   ========================================================================== */
/* Display-face caption above a chart. */
.chart-title {
    font-family: var(--type-display);
    font-weight: 600;
    font-size: clamp(1.25rem, 1.6vw, 1.625rem);   /* ~26px */
    line-height: 1.2;
    color: var(--heading);
    margin: 0 0 clamp(1rem, 1.6vw, 1.5rem);
}

/* ==========================================================================
   C) COLUMN CHART — vertical bars, value over bar, category under
   ==========================================================================
   Single `text` element (all <div>/<span>/<b> — survives Contao output).
   Bars are true-scale: each .bcx carries `--h` = bar height as a % of the plot
   track Lesser bars grey (--bc-g1); the highlight column
   (.bcx--hi) is solid --prime with navy labels. Markup (see the page spec):
     <div class="bar-col"><div class="bc-plot">
        <div class="bcx" style="--h:30%"><span class="bcx-col">
            <b class="bcx-val">$220k</b><span class="bcx-bar"></span>
        </span><span class="bcx-lab">Age 45</span></div> …
     </div></div>
     <p class="bar-col-note">…</p>
   ========================================================================== */
.bar-col { width: 100%; }
.bc-plot {
    display: flex;
    align-items: flex-end;
    gap: clamp(0.5rem, 1.6vw, 1.1rem);
    border-bottom: 1px solid var(--prime-deep);        /* axis baseline */
}
.bcx { flex: 1 1 0; min-width: 0; }
/* fixed bar area; value + bar bottom-aligned so bars meet the axis and each
   value label hugs its bar top (headroom above the tallest bar = 100% − 90%). */
.bcx-col {
    height: clamp(220px, 26vw, 280px);
    display: flex;
    flex-direction: column;
    justify-content: flex-end;
}
.bcx-val {
    display: block;
    text-align: center;
    font-family: var(--type-body);
    font-weight: 600;
    font-size: clamp(0.9rem, 1.05vw, 1.125rem);   /* ~18px */
    color: var(--text-body);
    margin-bottom: 0.4rem;
    line-height: 1;
}
/* A chart may ramp its columns by VALUE rather than one grey, so the fill is
   per-column data, set inline next to `--h` (`style="--h:30%;--bcx-fill:#DFE4EA"`).
   `--bc-g1` is the fallback. Bars are square-topped. */
.bcx-bar {
    display: block;
    height: var(--h, 0%);
    background: var(--bcx-fill, var(--bc-g1));
    transform-origin: bottom center;
}
.bcx-lab {
    display: block;
    text-align: center;
    font-family: var(--type-body);
    font-weight: 500;
    font-size: clamp(0.9rem, 1.05vw, 1.125rem);   /* ~18px */
    color: var(--text-strong);
    padding-top: 0.7rem;
    line-height: 1;
}
/* The highlight column is a vertical gradient, dark at the top. */
.bcx--hi {
    .bcx-bar { background: linear-gradient(180deg, var(--prime-deep) 0%, var(--prime) 100%); }
    .bcx-val, .bcx-lab { color: var(--prime); }
}

.bar-col-note {
    margin: 1.1rem 0 0;
    font-family: var(--type-body);
    font-size: clamp(0.78rem, 0.95vw, 0.8125rem);   /* ~13px */
    color: var(--text-strong);
    line-height: 1.45;
}

/* ==========================================================================
   MOBILE
   ========================================================================== */
@media only screen and (max-width: 760px) {
    .bc-head { flex-direction: column; gap: 0.15rem; }
    .bc-val, .bc-row--hi .bc-val { font-size: 1.375rem; }
    .bs-track { height: 48px; }
    .bs-legend { gap: 0.5rem 1.25rem; }
    .bcx-col { height: clamp(160px, 42vw, 220px); }
    .bcx-val, .bcx-lab { font-size: 0.8125rem; }
    .bc-plot { gap: 0.4rem; }
}

/* ==========================================================================
   SCROLL-IN REVEAL — bars fill the first time the chart enters view
   ==========================================================================
   Driven by `is-in`, added once by the j_reveal template.
   Gated on `.js-reveal`, which only exists while that observer is running, so
   with JS off or reduced motion requested every bar renders at its authored
   width immediately. One pass; it does not reverse on scroll-up.

   The reveal is a `clip-path` wipe, NOT `transform: scaleX()`. A scaled fill
   squashes its own 10px pill ends for the length of the animation and they
   only settle at the last frame; a clip wipe uncovers the bar at its true
   geometry, so the rounded cap looks right the whole way across. It also means
   the bar's inline `style="width:N%"` stays the single source of truth for
   length — nothing here has to know the value.

   The highlight bar lands a beat after the comparison bar so the pair reads as
   "this much... versus THIS much" rather than both arriving together.
   ========================================================================== */
.js-reveal {
    .bar-compare .bc-fill, .bar-stack .bs-seg { clip-path: inset(0 100% 0 0); }
    .bar-compare.is-in .bc-fill { animation: bcWipe 0.85s cubic-bezier(0.22, 0.61, 0.36, 1) both; }
    .bar-compare.is-in .bc-row:nth-child(2) .bc-fill { animation-delay: 0.18s; }
    .bar-compare.is-in .bc-row:nth-child(3) .bc-fill { animation-delay: 0.36s; }
    .bar-compare.is-in .bc-row--hi .bc-fill { animation-delay: 0.42s; }

    /* ---- The cumulative rail fills STRICTLY one segment at a time -----------
       `.bar-stack` is a single rail split into segments that add up to one total,
       so overlapping them read as one blurry sweep. Each segment now waits for the
       one before it to finish — light grey fills and stops, then the mid grey, then
       the navy — which is what makes the rail read as an accumulation rather than a
       bar. `--bs-step` is both the per-segment duration and the beat, so they stay
       butted together by construction. */
    .bar-stack { --bs-step: 0.5s; }
    .bar-stack.is-in .bs-seg { animation: bcWipe var(--bs-step) ease-out both; }
    .bar-stack.is-in .bs-seg:nth-child(1) { animation-delay: calc(0 * var(--bs-step)); }
    .bar-stack.is-in .bs-seg:nth-child(2) { animation-delay: calc(1 * var(--bs-step)); }
    .bar-stack.is-in .bs-seg:nth-child(3) { animation-delay: calc(2 * var(--bs-step)); }
    .bar-stack.is-in .bs-seg:nth-child(4) { animation-delay: calc(3 * var(--bs-step)); }
    .bar-stack.is-in .bs-seg:nth-child(5) { animation-delay: calc(4 * var(--bs-step)); }
}

@keyframes bcWipe {
    from { clip-path: inset(0 100% 0 0); }
    to   { clip-path: inset(0 0 0 0); }
}

/* Column charts grow UP from the axis; a vertical clip keeps the bar's own
   top radius intact the same way. */
.js-reveal {
    .bc-plot .bcx-bar { clip-path: inset(100% 0 0 0); }
    .bc-plot.is-in .bcx-bar { animation: bcxGrow 0.8s cubic-bezier(0.22, 0.61, 0.36, 1) both; }
    .bc-plot.is-in .bcx:nth-child(2) .bcx-bar { animation-delay: 0.12s; }
    .bc-plot.is-in .bcx:nth-child(3) .bcx-bar { animation-delay: 0.24s; }
    .bc-plot.is-in .bcx:nth-child(4) .bcx-bar { animation-delay: 0.36s; }
    .bc-plot.is-in .bcx:nth-child(5) .bcx-bar { animation-delay: 0.48s; }
    .bc-plot.is-in .bcx:nth-child(6) .bcx-bar { animation-delay: 0.60s; }
    .bc-plot.is-in .bcx--hi .bcx-bar { animation-delay: 0.72s; }
}

@keyframes bcxGrow {
    from { clip-path: inset(100% 0 0 0); }
    to   { clip-path: inset(0 0 0 0); }
}
