/*
 * The Latest quarter card (companies/keystats/_latest_quarter,
 * LatestQuarterHelper): first in the sidebar on a wide screen, straight after
 * the price chart on a narrow one. Laid out by its own width: where it is
 * wide (the one-column page), the lines and the growth chart sit side by
 * side and the two notes under them do too; in the sidebar they stack; and
 * under 30rem each line takes two rows, its label and figure over its
 * change and its result against the consensus.
 */
@layer components {
  .latest-quarter {
    container: latest-quarter / inline-size;
  }

  .latest-quarter__lede {
    display: grid;
    gap: var(--space-2xs);
    margin-block-end: var(--space-md);
  }

  .latest-quarter__headline {
    margin: 0;
    max-inline-size: var(--measure);
    font-family: var(--font-prose);
    font-size: var(--text-lead);
    font-weight: var(--weight-semibold);
    line-height: var(--leading-lead);
    text-wrap: balance;

    & a {
      color: var(--color-ink);
      text-decoration: none;

      &:hover {
        color: var(--color-link-hover);
        text-decoration: underline;
        text-decoration-thickness: 1px;
        text-underline-offset: 0.2em;
      }
    }
  }

  .latest-quarter__dateline {
    margin: 0;
    color: var(--color-ink-muted);
    font-size: var(--text-ui-sm);
  }

  .latest-quarter__body {
    display: grid;
    gap: var(--space-lg);

    @container latest-quarter (width >= 44rem) {
      grid-template-columns: minmax(0, 1.2fr) minmax(0, 1fr);
      gap: var(--space-2xl);
      align-items: start;
    }
  }

  /* The three lines: a table's columns from rows of a subgrid. */
  .latest-quarter__lines {
    display: grid;
    grid-template-columns: minmax(0, 1fr) auto auto auto;
    font-family: var(--font-data);
    font-size: var(--text-data);
    line-height: var(--leading-data);
    font-variant-numeric: tabular-nums lining-nums;
  }

  .latest-quarter__row {
    display: grid;
    grid-column: 1 / -1;
    grid-template-columns: subgrid;
    align-items: baseline;
    border-block-end: var(--rule-hairline);

    & > * {
      padding: 0.4375rem 0 0.4375rem var(--space-md);
      text-align: end;
    }

    & > :first-child {
      padding-inline-start: 0;
      text-align: start;
    }
  }

  .latest-quarter__row--head {
    border-block-end-color: var(--color-rule-strong);

    & > * {
      padding-block: var(--space-2xs);
      color: var(--color-ink-muted);
      font-size: var(--text-table-head);
      line-height: var(--leading-table-head);
      white-space: nowrap;
    }
  }

  .latest-quarter__label {
    font-family: var(--font-ui);
    font-size: var(--text-ui-sm);
  }

  .latest-quarter__value {
    font-family: var(--font-prose);
    font-size: var(--text-prose);
    font-weight: var(--weight-semibold);
  }

  .latest-quarter__inline-label {
    display: none;
    color: var(--color-ink-subtle);
    font-size: var(--text-meta);
  }

  .latest-quarter__against {
    display: grid;
    justify-items: end;
  }

  .latest-quarter__result {
    font-weight: var(--weight-semibold);
    white-space: nowrap;
  }

  .latest-quarter__result--beat { color: var(--color-gain); }

  .latest-quarter__result--missed { color: var(--color-loss); }

  .latest-quarter__result--met { color: var(--color-ink); }

  .latest-quarter__consensus {
    color: var(--color-ink-subtle);
    font-size: var(--text-meta);
    white-space: nowrap;
  }

  /* A line's last quarters (LatestQuarterHelper#latest_quarter_bars_tag):
     a strip of bars across the row, under its figures, from a zero line,
     the latest darker, a loss below the line in brick; the first and last
     quarters named at its ends. Each bar's foot and height arrive as shares
     of the plot. */
  .latest-quarter__row > .latest-quarter__bars {
    display: grid;
    grid-column: 1 / -1;
    grid-template-columns: 1fr 1fr;
    padding: 0 0 var(--space-3xs);
    text-align: start;
  }

  .latest-quarter__bars-plot {
    position: relative;
    display: grid;
    grid-column: 1 / -1;
    grid-template-columns: repeat(var(--quarters), minmax(0, 1fr));
    column-gap: var(--space-xs);
    block-size: 2.5rem;

    &::after {
      content: "";
      position: absolute;
      inset-inline: 0;
      inset-block-end: var(--zero);
      border-block-start: var(--rule-width) solid var(--color-rule-strong);
    }
  }

  .latest-quarter__bar {
    position: relative;

    &::before {
      content: "";
      position: absolute;
      inset-inline: 0;
      inset-block-end: var(--foot);
      block-size: var(--height);
      background: color-mix(in srgb, var(--color-bar-gain) 50%, var(--color-ground));
    }
  }

  .latest-quarter__bar--loss::before {
    background: color-mix(in srgb, var(--color-bar-loss) 50%, var(--color-ground));
  }

  .latest-quarter__bar--latest::before {
    background: var(--color-bar-gain);
  }

  .latest-quarter__bar--latest.latest-quarter__bar--loss::before {
    background: var(--color-bar-loss);
  }

  .latest-quarter__bars-end {
    padding-block-start: var(--space-3xs);
    color: var(--color-ink-subtle);
    font-size: var(--text-meta-xs);
    line-height: var(--leading-meta-xs);

    &:last-child {
      text-align: end;
    }
  }

  .latest-quarter__footnote {
    margin: var(--space-xs) 0 0;
    color: var(--color-ink-subtle);
    font-size: var(--text-meta);
    line-height: var(--leading-meta);
  }

  /* Narrow: a line's label and figure, then its change and its result. */
  @container latest-quarter (width < 30rem) {
    .latest-quarter__lines {
      grid-template-columns: minmax(0, 1fr) auto;
    }

    .latest-quarter__row {
      row-gap: var(--space-3xs);
      padding-block: var(--space-xs);

      & > * {
        padding: 0;
      }

      & > :nth-child(3) {
        text-align: start;
      }
    }

    .latest-quarter__row--head {
      display: none;
    }

    .latest-quarter__inline-label {
      display: inline;
    }
  }

  .latest-quarter__growth {
    display: grid;
    gap: var(--space-xs);
    margin: 0;
  }

  .latest-quarter__sentence {
    margin: 0;
    color: var(--color-ink-soft);
    font-family: var(--font-prose);
    font-size: var(--text-prose-sm);
    line-height: var(--leading-prose-sm);
    text-wrap: pretty;
  }

  /* What drove it, and the outlook: a caps label over a line each. */
  .latest-quarter__notes {
    display: grid;
    gap: var(--space-md);
    margin-block-start: var(--space-lg);
    padding-block-start: var(--space-md);
    border-block-start: var(--rule-hairline);

    @container latest-quarter (width >= 44rem) {
      grid-template-columns: repeat(2, minmax(0, 1fr));
      gap: var(--space-2xl);
    }
  }

  .latest-quarter__note {
    display: grid;
    gap: var(--space-2xs);
    align-content: start;

    & p {
      margin: 0;
      color: var(--color-ink-soft);
      font-size: var(--text-ui);
      line-height: var(--leading-ui);
    }
  }

  .latest-quarter__note .latest-quarter__guide {
    color: var(--color-ink);
    font-family: var(--font-prose);
    font-size: var(--text-prose);
    line-height: var(--leading-prose);
  }

  .latest-quarter__note .latest-quarter__source {
    color: var(--color-ink-muted);
    font-size: var(--text-meta);
    line-height: var(--leading-meta);
  }

  .latest-quarter__next {
    display: flex;
    flex-wrap: wrap;
    align-items: baseline;
    justify-content: space-between;
    gap: var(--space-xs) var(--space-lg);
    margin-block-start: var(--space-lg);
    padding-block-start: var(--space-sm);
    border-block-start: var(--rule-hairline);
    font-size: var(--text-ui-sm);
  }

  .latest-quarter__next-date {
    margin: 0;
    color: var(--color-ink-soft);
  }
}
