/* ==========================================================================
   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: drop the markup (see cornerstone.bar-compare.spec.md) into a text CE
   inside any light band (`band bg-cream section-80` / `bg-white`).
   ========================================================================== */

:root {
    /* Grey ramp for the "lesser" bars/segments (per Penpot). */
    --bc-g1: #C4CCD6;   /* lightest */
    --bc-g2: #9AA5B3;   /* mid grey */
    /* Highlight fill: prime -> ink, light on the left, dark on the right.
       Verified against Case Study A's board bar, whose gradient is
       #0a417c -> #04203f running start(0.007, 0.467) to end(1.001, 0.488). */
    --bc-hi: linear-gradient(90deg, var(--prime) 0%, var(--ink) 100%);
    --bc-radius: 0;     /* square ends, like the column charts (Bob, 13 Oct) */
}

/* ---- On a DARK band the whole scale re-keys ------------------------------
   A navy-on-navy bar is invisible, so the board uses different paint on the
   ink bands. Case Study D (#04203f) draws the lesser bar as a solid mid blue
   rather than the light grey, and runs the highlight gradient the OTHER WAY —
   Penpot: #9cb0ec at start(1, 0.5) to #0a417c at end(0, 0.5), i.e. deep navy at
   the root brightening to periwinkle at the tip. That inversion is what John
   reported as the wrong colour/gradient on the "Safe harbor match only" /
   "With cash balance combo" spans. The light-band values above are correct and
   unchanged. */
.bg-ink .bar-compare,
.bg-dark .bar-compare {
    --bc-g1: #4D7FB0;
    --bc-g2: #4D7FB0;
    --bc-hi: linear-gradient(90deg, var(--prime) 0%, #9CB0EC 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(--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(--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 (per Penpot). */
.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(--body);
}
.bs-key { display: inline-flex; align-items: center; gap: 0.55rem; white-space: nowrap;
    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(--body); line-height: 1.45;
}

/* ==========================================================================
   SHARED — chart title (sits above a .bar-stack / .bar-col)
   ========================================================================== */
/* Literata navy caption, ~26px (per Penpot: hero "How a combo design stacks up"
   and §"Maximum tax-deductible… by age"). */
.chart-title {
    font-family: var(--type-display);
    font-weight: 700;
    font-size: clamp(1.25rem, 1.6vw, 1.625rem);   /* the boards: 26px / 700, five pages */
    line-height: 1.2;
    color: #083C6F;
    margin: 0 0 clamp(1rem, 1.6vw, 1.5rem);
}
/* In a hero the chart title sits on a navy rule across the chart's column,
   19px under the text (Complex Plan Design, Consulting: board x1352 y409 w748;
   John, 8 Oct). The age chart further down Complex Plan Design has none. */
#hero .chart-title {
    padding-bottom: clamp(0.75rem, 0.8vw, 1.1875rem);
    border-bottom: 1px solid var(--prime);
}

/* ==========================================================================
   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 (Penpot: bar px / 266). 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-start;     /* columns top-aligned: the bar areas are one fixed height, so the bars
                                    share a baseline even when a label wraps to two lines (Bob, 13 Oct) */
    gap: 0;                                     /* bars touch, as on the boards and the printed case studies */
    border-bottom: 1px solid var(--ink);        /* axis baseline — board #04203f */
}
.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(--body);
    margin-bottom: 0.4rem;
    line-height: 1;
}
/* The board ramps these columns by VALUE rather than painting them all one
   grey — Complex Plan Design's age chart runs #EEF1F5, #DFE4EA, #9AA5B3,
   var(--text-muted), then back DOWN to #9AA5B3 where the $500k column dips, and only the
   highlight is navy. So the fill is per-column data, supplied inline next to
   `--h` in the spec, exactly the way the height already is. `--bc-g1` stays the
   fallback for a chart that does not ramp.
   Bars are square-topped on the board (radius 0), not rounded. */
.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: 600;
    font-size: clamp(0.9rem, 1.05vw, 1.125rem);   /* 18px / 600 */
    color: #1f262d;
    padding: 0.7rem 0.2rem 0;           /* the bars touch; their labels don't */
    line-height: 1;
}
/* The highlight column is a VERTICAL gradient, dark at the top:
   Penpot #04203f -> #0a417c, s(0.5, 0) to e(0.5, 1). */
.bcx--hi {
    .bcx-bar { background: linear-gradient(180deg, var(--ink) 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 / 500 */
    font-weight: 500;
    color: #333B44;
    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; }
}

/* ==========================================================================
   SCROLL-IN REVEAL — bars fill the first time the chart enters view
   ==========================================================================
   Driven by `is-in`, added once by fe_page.html.twig's IntersectionObserver.
   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); }
}
