/*
 * Year bars: a bar a fiscal year, oldest first, each as tall as its share of
 * the tallest (`--share`), its figure over it and its year — and a change,
 * where the chart has one — under it. Plain HTML, so the figures are in
 * view-source and on paper with no JavaScript; the bars are `aria-hidden`.
 * The Dividends card (companies/keystats/_dividends) draws what was paid in
 * the series colour, a forecast outlined on the forecast tint and filled
 * from the foot as it is paid (`--paid`); the Cash conversion and returns
 * card (companies/keystats/_cash_returns) a share a year.
 *
 *   .year-bars--hatched    its forecasts hatched rather than outlined.
 *                          Its figures stand in its bars
 *                          (YearBarsHelper#bar_figure_tag);
 *                          `.year-bars--alternating` raises every other one
 *                          in a chart under 22rem, for a dozen of "10.04".
 *   .year-bars--reference  a dashed line across the bars at `--reference`,
 *                          its share of the tallest: 100% of net income. The
 *                          figures sit on the paper so the line passes
 *                          behind them.
 *   .year-bars--sparse     under 640px only the years marked
 *                          `.year-bars__year--keep` (first, last, lowest,
 *                          highest) keep their figure, so ten of them fit a
 *                          phone without running into each other.
 *   .year-bars--marked     a bar and a line across it (`.year-bars__mark`) a
 *                          year, in a `.year-bars__plot` with a zero line at
 *                          `--zero`; each starts at `--start` from the top,
 *                          so a loss hangs below the line. What sits under
 *                          the year is the line over the bar.
 *                          `.year-bars__year--trailing`, the last twelve
 *                          months, is edged in ink, its figures semibold.
 *                          Or a bar either side of the line
 *                          (`.year-bars__bar--mark` hanging from it, filled or
 *                          `--outlined`), each half on its own scale.
 *   .year-bars--figured    marked, with each figure written by its bar or its
 *                          line (`.year-bars__figure`, `--start`;
 *                          Companies::YearPlot).
 *                          `--over` and `--under` are the room they need
 *                          past the plot, as shares of its height.
 *   .year-bars--staggered  in a chart under 22rem every other year's figure
 *                          drops to a second line, so figures as wide as
 *                          "−889" all fit the smallest phone.
 */
@layer components {
  .year-bars {
    --year-bars-height: 7rem;

    display: grid;
    grid-auto-columns: minmax(0, 1fr);
    grid-auto-flow: column;
    align-items: end;
    gap: var(--space-2xs);
    margin: var(--space-sm) 0 0;
    padding: 0;
    list-style: none;
    font-family: var(--font-data);
    font-variant-numeric: tabular-nums lining-nums;
  }

  /* One column, held to the year's width: an auto column grew to a label's,
     and took the bar with it. */
  .year-bars__year {
    display: grid;
    grid-template-columns: minmax(0, 1fr);
    justify-items: center;
    gap: var(--space-3xs);
    min-inline-size: 0;
  }

  .year-bars__total,
  .year-bars__change {
    color: var(--color-ink-subtle);
    font-size: var(--text-meta-xs);
    white-space: nowrap;
  }

  /* A line even with no change to show, or that year's foot drops below the
     others': the first year, and the year after one of no dividend. */
  .year-bars__change {
    min-block-size: 1lh;
  }

  .year-bars__bar {
    inline-size: 100%;
    block-size: calc(var(--share) * var(--year-bars-height));
    background: linear-gradient(to top, var(--color-series-1) calc(var(--paid) * 100%), transparent 0);
  }

  /* Its figures stand in its bars, so what is paid is a tint they read on. */
  .year-bars--hatched {
    --year-bars-paid: color-mix(in srgb, var(--color-series-1) 35%, var(--color-surface));

    & .year-bars__bar {
      position: relative;
      background: linear-gradient(to top, var(--year-bars-paid) calc(var(--paid) * 100%), transparent 0);
    }

    & .year-bars__figure {
      inset-block: auto 0;
    }

    & .year-bars__figure--over {
      inset-block: auto 100%;
    }

    & .year-bars__figure--backed > span {
      padding-inline: 1px;
      background: var(--color-surface);
    }
  }

  .is-forecast .year-bars__bar {
    border: var(--rule-width) dashed var(--color-rule-control);
    background-color: var(--color-forecast);
  }

  /* Hatched as the growth bars' estimates are (comparison-bars.css); what is
     already paid still fills it from the foot. */
  .year-bars--hatched .is-forecast .year-bars__bar {
    border: var(--rule-width) solid var(--color-series-1);
    background:
      linear-gradient(to top, var(--year-bars-paid) calc(var(--paid) * 100%), transparent 0),
      repeating-linear-gradient(135deg, var(--color-series-1) 0 1.5px, transparent 1.5px 5px)
      var(--color-forecast);
  }

  .year-bars__label {
    padding-block-start: var(--space-3xs);
    border-block-start: var(--rule-hairline);
    inline-size: 100%;
    color: var(--color-ink-muted);
    font-size: var(--text-meta-xs);
    text-align: center;
  }

  .year-bars--reference .year-bars__bar {
    position: relative;

    &::after {
      position: absolute;
      inset-inline: 0;
      inset-block-end: calc(var(--reference) * var(--year-bars-height));
      border-block-start: var(--rule-width) dashed var(--color-ink);
      content: "";
    }
  }

  /* The line runs on across the gap to the next year. */
  .year-bars--reference .year-bars__year:not(:last-child) .year-bars__bar::after {
    inset-inline-end: calc(-1 * var(--space-2xs));
  }

  .year-bars--reference .year-bars__total {
    position: relative;
    z-index: 1;
    padding-inline: var(--space-3xs);
    background: var(--color-surface);
  }

  .year-bars__plot {
    position: relative;
    inline-size: 100%;
    block-size: var(--year-bars-height);

    /* The zero line, on across the gap to the next year. */
    &::after {
      position: absolute;
      inset-inline: 0 calc(-1 * var(--space-2xs));
      inset-block-start: calc(var(--zero) * 100%);
      border-block-start: var(--rule-width) solid var(--color-rule-strong);
      content: "";
    }

    & .year-bars__bar {
      position: absolute;
      inset-block-start: calc(var(--start) * 100%);
      background: color-mix(in srgb, var(--color-ink) 18%, transparent);
    }
  }

  .year-bars__year:last-child .year-bars__plot::after {
    inset-inline-end: 0;
  }

  /* Edged in paper, so it stands off the bar it crosses. */
  .year-bars__mark {
    position: absolute;
    z-index: 1;
    inset-inline: 0;
    inset-block-start: calc(var(--start) * 100% - 2px);
    block-size: 4px;
    outline: 1px solid var(--color-surface);
    background: var(--color-series-1);
  }

  .year-bars__year--trailing {
    & .year-bars__plot .year-bars__bar:not(.year-bars__bar--mark) {
      box-sizing: border-box;
      /* None on a 0, where the edge alone would draw a bar. */
      border-block-start: min(var(--rule-width-strong), calc(var(--share, 0) * 1000px)) solid var(--color-ink);
    }

    & .year-bars__plot .year-bars__bar--mark:not(.year-bars__bar--outlined) {
      box-sizing: border-box;
      border-block-end: min(var(--rule-width-strong), calc(var(--share, 0) * 1000px)) solid var(--color-ink);
    }

    & .year-bars__plot .year-bars__bar--loss {
      border-block-start: 0;
      border-block-end: min(var(--rule-width-strong), calc(var(--share, 0) * 1000px)) solid var(--color-ink);
    }

    & .year-bars__figure,
    & .year-bars__total,
    & .year-bars__label,
    & .year-bars__change {
      color: var(--color-ink);
      font-weight: var(--weight-semibold);
    }
  }

  /* The figure hanging from the line, where the plot is mirrored. */
  .year-bars__year .year-bars__plot .year-bars__bar--mark {
    background: color-mix(in srgb, var(--color-series-1) 35%, transparent);
  }

  .year-bars__year .year-bars__plot .year-bars__bar--outlined {
    box-sizing: border-box;
    border: solid var(--color-series-1);
    /* None on a 0, where the border alone would draw a bar. */
    border-width: min(var(--rule-width), calc(var(--share, 0) * 1000px));
    background: var(--color-surface);
  }

  .year-bars--marked .year-bars__label {
    border-block-start: 0;
  }

  .year-bars--figured {
    --year-bars-height: 9.5rem;

    padding-block-start: calc(var(--over, 0) * var(--year-bars-height));

    & .year-bars__plot {
      margin-block-end: calc(var(--under, 0) * var(--year-bars-height));
    }
  }

  /* Wider than a phone's bar, over the gaps either side of it. */
  .year-bars__figure {
    position: absolute;
    z-index: 2;
    inset-inline: calc(-1 * var(--space-2xs));
    inset-block-start: calc(var(--start) * 100%);
    color: var(--color-ink);
    font-size: var(--text-meta-xs);
    line-height: 0.875rem;
    text-align: center;
    white-space: nowrap;
  }

  .year-bars__year .year-bars__figure--mark {
    color: var(--color-series-1);
  }

  .year-bars--figured,
  .year-bars--staggered {
    container: year-bars / inline-size;
  }

  /* A line of room over or under each, so the bars keep one foot. */
  @container year-bars (width < 22rem) {
    .year-bars__figure {
      letter-spacing: -0.03em;
    }

    .year-bars--staggered .year-bars__year:nth-child(even) .year-bars__figure--hanging {
      translate: 0 1lh;
    }

    .year-bars--alternating .year-bars__year:nth-child(even) .year-bars__figure {
      translate: 0 -1lh;
    }

    .year-bars--staggered .year-bars__year:nth-child(odd) .year-bars__change {
      padding-block-end: 1lh;
    }

    .year-bars--staggered .year-bars__year:nth-child(even) .year-bars__change {
      padding-block-start: 1lh;
    }
  }

  @media (width < 640px) {
    .year-bars--sparse .year-bars__year:not(.year-bars__year--keep) .year-bars__total {
      visibility: hidden;
    }
  }
}
