/* Charts and maps — HTML/CSS rebuilds of the Figma "native editable" report
   figures (Design System › 11 Charts, 12 Parish maps). Every bar reads its
   size from --value (and --min / --max on the chart), so data edits are a
   one-attribute change and charts reflow at any width. GSAP grows bars
   ([data-bar]) in once they scroll into view (see motion.js). */

@layer charts {
  /* Bars start collapsed from the first paint so they never flash full-size
     before charts.js grows them (GSAP inline styles take over from here).
     Reduced-motion users always get the finished chart. */
  @media (prefers-reduced-motion: no-preference) {
    .charts-pending :is(.colchart__bar, .colchart__stack)[data-bar] {
      transform: scaleY(0.01);
    }

    .charts-pending :is(.hbar__bar, .divchart__bar)[data-bar] {
      transform: scaleX(0.01);
    }

    .charts-pending :is(.colchart__value, .hbar__value, .divchart__value) {
      opacity: 0;
      visibility: hidden;
    }
  }

  [data-chart] {
    display: flex;
    flex-direction: column;
    gap: var(--v2-space-16);
    margin: 0;
    font-size: var(--v2-body-text-small-size);
    line-height: var(--lh-small);
  }

  [data-chart] > h4,
  [data-chart] > h5 {
    font-size: var(--v2-h4-size);
  }

  /* Source / sample note under every chart. */
  [data-chart] > figcaption {
    text-align: center;
  }

  /* Column chart ----------------------------------------------------------- */
  .colchart {
    --plot-h: 18.75rem; /* 300 — Figma plot area */
    --axis-w: 2.75rem;
    --bar-w: clamp(1.5rem, 7cqi, 4.5rem);
    container-type: inline-size;
  }

  /* Many paired groups: slimmer bars so each pair reads as its own group
     with clear space between groupings. */
  .colchart--narrow {
    --bar-w: clamp(1.25rem, 5cqi, 3rem);
  }

  .colchart__plot {
    --min: 0;
    --range: calc(var(--max) - var(--min));
    position: relative;
    padding-inline-start: var(--axis-w);
  }

  .colchart__grid {
    position: absolute;
    inset-block-start: 0;
    inset-inline: 0;
    block-size: var(--plot-h);
    display: flex;
    flex-direction: column;
    justify-content: space-between;
    pointer-events: none;

    & span {
      position: relative;
      block-size: 0;
      margin-inline-start: var(--axis-w);
      border-block-start: 1px solid color-mix(in srgb, currentColor 12%, transparent);
    }

    & span {
      font-size: var(--v2-body-text-small-size);
      line-height: 0;
      text-indent: calc(-1 * var(--axis-w));
      white-space: nowrap;
    }
  }

  /* Unevenly spaced ticks: each line sits at its own value (--at), on the
     same scale as the bars (--max / --range from .colchart__plot). */
  .colchart__grid--scaled span {
    position: absolute;
    inset-inline: 0;
    inset-block-start: calc((var(--max) - var(--at)) / var(--range) * 100%);
  }

  .colchart__groups {
    position: relative;
    display: flex;
    margin: 0;
  }

  .colchart__group {
    display: flex;
    flex: 1;
    flex-direction: column;
    align-items: center;
    gap: var(--v2-space-8);
    min-inline-size: 0;

    & dt {
      order: 2;
      margin-block-start: var(--v2-space-8); /* 16 from the plot, with the gap */
      text-align: center;
      text-wrap: balance;
    }
  }

  .colchart__bars {
    position: relative;
    display: grid;
    grid-auto-flow: column;
    grid-auto-columns: var(--bar-w);
    justify-content: center;
    column-gap: 0.25rem;
    inline-size: 100%;
    block-size: var(--plot-h);
    margin: 0;
  }

  .colchart__bar {
    position: relative;
    align-self: end;
    block-size: calc(var(--value) / var(--range) * 100%);
    /* lift the baseline to zero when the axis goes negative */
    inset-block-end: calc(-1 * var(--min) / var(--range) * 100%);
    background: var(--bar-color, var(--chart-primary));
    transform-origin: 50% 100%;
  }

  .colchart__bar--neg {
    align-self: start;
    inset-block-end: auto;
    inset-block-start: calc(var(--max) / var(--range) * 100%);
    transform-origin: 50% 0;
  }

  .colchart__value {
    position: absolute;
    inset-block-end: calc(100% + 0.25rem);
    inset-inline-start: 50%;
    translate: -50% 0;
    white-space: nowrap;

    .colchart__bar--neg > & {
      inset-block-end: auto;
      inset-block-start: calc(100% + 0.25rem);
    }
  }

  /* Increase/decrease bars of one category share a column. */
  .colchart--stacked .colchart__bars {
    grid-auto-flow: row;
    grid-template-columns: var(--bar-w);
    grid-template-rows: 1fr;

    & > * {
      grid-area: 1 / 1;
    }
  }

  /* Increase/decrease pairs side by side, staggered either side of zero
     (Figma "Change in Annual Budget"). */
  .colchart--paired .colchart__bars {
    column-gap: var(--v2-space-12);
  }

  .colchart__bar--primary,
  .colchart__bar--increase,
  .colchart__bar--y2021 {
    --bar-color: var(--chart-primary);
  }

  .colchart__bar--comparison {
    --bar-color: var(--chart-comparison);
  }

  .colchart__bar--decrease,
  .colchart__bar--y2020 {
    --bar-color: var(--v2-white);
  }

  .colchart__bar--y2026 {
    --bar-color: var(--v2-yellow);
  }

  /* 100% stacked columns (Figure 16): segments share one column. */
  .colchart__stack {
    display: flex;
    flex-direction: column-reverse;
    align-self: end;
    inline-size: var(--bar-w);
    block-size: 100%;
    transform-origin: 50% 100%;
  }

  .colchart__seg {
    display: grid;
    place-items: center;
    flex: 0 0 calc(var(--value) * 1%);
    background: var(--bar-color);
    color: var(--v2-navy);
    font-size: var(--v2-body-text-small-size);
  }

  .colchart__with-aside {
    display: flex;
    flex-wrap: wrap;
    gap: var(--v2-space-24);

    & > :first-child {
      flex: 1 1 16rem;
    }

    & > .colchart__aside {
      flex: 1 1 12rem;
    }
  }

  /* Horizontal bars ------------------------------------------------------ */
  .hbar {
    --label-w: 12.5rem; /* 200 */
    --gap: 0.75rem;
    container-type: inline-size;
  }

  .hbar--percent {
    --label-w: 13.75rem; /* 220 */
    --gap: 1rem;
  }

  .hbar__rows {
    display: flex;
    flex-direction: column;
    gap: var(--v2-space-12, 0.75rem);
    margin: 0;
  }

  .hbar--dense .hbar__rows {
    gap: 0.25rem;
  }

  .hbar__row {
    display: grid;
    grid-template-columns: var(--label-w) 1fr;
    align-items: center;
    gap: var(--gap);

    & dd {
      position: relative;
      display: flex;
      align-items: center;
      gap: 0.25rem;
      block-size: 1.75rem;
      margin: 0;
    }
  }

  .hbar__bar {
    flex: 0 0 auto;
    inline-size: max(2px, calc(var(--value) / var(--max) * (100% - 3rem)));
    block-size: 1.25rem; /* 20 */
    background: var(--chart-primary);
    transform-origin: 0 50%;
  }

  .hbar__axis {
    display: flex;
    justify-content: space-between;
    margin-inline-start: calc(var(--label-w) + var(--gap));
    margin-inline-end: 3rem;
    opacity: 0.8;

    /* Zero-width ticks so each label centres on its gridline. */
    .hbar:has(&) & > span {
      display: flex;
      justify-content: center;
      inline-size: 0;
      white-space: nowrap;
    }
  }

  /* Diverging bars (decrease left / increase right of zero) ------------------ */
  .divchart {
    --label-w: 9rem;
    container-type: inline-size;
  }

  .divchart__rows {
    display: flex;
    flex-direction: column;
    gap: 0.5rem;
    margin: 0;
  }

  .divchart__row {
    display: grid;
    grid-template-columns: var(--label-w) 1fr;
    align-items: center;
    gap: var(--v2-space-16);

    & dd {
      display: grid;
      grid-template-columns: 1fr 1fr;
      align-items: center;
      block-size: 1.75rem;
      margin: 0;
      background: linear-gradient(currentColor, currentColor) 50% 0 / 1px 100% no-repeat;
    }
  }

  .divchart__bar {
    position: relative;
    inline-size: calc(var(--value) / var(--max) * 100%);
    block-size: 1.25rem;
  }

  .divchart__bar--decrease {
    justify-self: end;
    background: var(--v2-white);
    transform-origin: 100% 50%;
  }

  .divchart__bar--increase {
    justify-self: start;
    background: var(--chart-primary);
    transform-origin: 0 50%;
  }

  .divchart__value {
    position: absolute;
    inset-block-start: 50%;
    translate: 0 -50%;
    white-space: nowrap;

    .divchart__bar--decrease > & {
      inset-inline-end: calc(100% + 0.375rem);
    }

    .divchart__bar--increase > & {
      inset-inline-start: calc(100% + 0.375rem);
    }
  }

  .divchart__axis {
    display: flex;
    justify-content: space-between;
    margin-inline-start: calc(var(--label-w) + var(--v2-space-16));
    opacity: 0.8;
  }

  /* Region map (13 stacked vector layers + labels) ----------------------- */
  .region-map-figure {
    display: flex;
    flex-direction: column;
    gap: var(--v2-space-24);
    margin: 0;
    padding: var(--v2-space-24);
    padding-top: 0px;
  }

  .region-map {
    position: relative;
    aspect-ratio: 498 / 473.198;
    container-type: inline-size;

    & img {
      position: absolute;
      inset: 0;
      inline-size: 100%;
      block-size: 100%;
      max-inline-size: none;
    }

    & span {
      position: absolute;
      display: grid;
      place-items: center;
      font-size: 2.884cqi; /* 14.363 / 498 */
      font-weight: var(--v2-weight-medium);
      line-height: 1.125;
      text-align: center;
      white-space: nowrap;
      color: var(--v2-navy);
      -webkit-text-stroke: 0.25em var(--v2-white);
      paint-order: stroke fill;
    }
  }

  /* Parish coverage map ------------------------------------------------- */
  .parish-map {
    display: flex;
    flex-direction: column;
    gap: var(--v2-space-16);
    margin: 0;
  }

  .parish-map__stage {
    position: relative;
    aspect-ratio: 752 / 714.548;
    container-type: inline-size;

    & > img,
    & > svg {
      position: absolute;
      inset: 0;
      inline-size: 100%;
      block-size: 100%;
      max-inline-size: none;
    }
  }

  .parish-map__labels {
    position: absolute;
    inset: 0;
    pointer-events: none;

    & span {
      position: absolute;
      display: grid;
      place-items: center;
      font-size: 2.618cqi; /* 19.688 / 752 */
      font-weight: var(--v2-weight-medium);
      line-height: 1.117;
      text-align: center;
      white-space: nowrap;
      color: var(--v2-navy);
      -webkit-text-stroke: 0.2em var(--v2-white);
      paint-order: stroke fill;
    }

    & .on-dark {
      color: var(--v2-white);
      -webkit-text-stroke-color: var(--v2-navy);
    }
  }

  .map-legend {
    display: flex;
    flex-wrap: wrap;
    gap: var(--v2-space-8) var(--v2-space-20);
    font-size: var(--v2-body-text-small-size);

    & li {
      display: inline-flex;
      align-items: center;
      gap: var(--v2-space-8);
    }

    & .swatch {
      inline-size: 0.875rem;
      block-size: 0.875rem;
    }
  }

  /* Narrow containers: stack the label above its bar. */
  @container (width < 30rem) {
    .hbar__row,
    .divchart__row {
      grid-template-columns: 1fr;
      gap: 0.25rem;
    }

    .hbar__axis,
    .divchart__axis {
      margin-inline-start: 0;
    }
  }
  /* Centre charts in the chapter panels. Lives in this layer because the
     [data-chart] margin reset above would beat a rule in @layer components. */
  .chapters__panel [data-chart] {
    margin-inline: auto;
  }
}
