@layer components {
  /* ChartsHelper#stimulus_chart sets the size as custom properties. */
  .stimulus-chart {
    block-size: var(--chart-height);
    inline-size: var(--chart-width);
  }

  .statistics-summary { margin-block-end: 2rem; }
  .statistics-grid {
    display: grid;
    gap: 1rem;
    grid-template-columns: repeat(auto-fit, minmax(10rem, 1fr));
  }
  .stat-out-of {
    color: var(--color-text-muted);
    font-size: 0.9rem;
    font-weight: 400;
  }
  .statistics-table td[data-z-score] { font-variant-numeric: tabular-nums; }
  .analysis-note {
    color: var(--color-text-muted);
    font-size: 0.85rem;
    margin-block-start: 0.75rem;
    max-inline-size: 60rem;
  }
  .distractor-widget-group {
    margin-block-start: 1rem;
    margin-inline-start: 1.5rem;
  }
  .distractor-widget-label {
    font-size: 0.9rem;
    font-weight: 600;
    margin-block-end: 0.25rem;
  }
  .expandable-row {
    cursor: pointer;

    &:hover { background: var(--color-surface-sunken); }
    &:focus-visible {
      outline: 2px solid var(--color-primary);
      outline-offset: -2px;
    }
  }
  .distractor-detail-row td { background: var(--color-surface-sunken); }

  /* Info tooltip: hover/focus popover, e.g. explaining a stat or column header.
     Placements only set --info-tooltip-*. */
  .info-tooltip {
    display: inline-flex;
    position: relative;
    vertical-align: middle;
  }
  .info-tooltip-trigger {
    align-items: center;
    all: unset;
    block-size: 1.1rem;
    border-radius: var(--radius-full);
    color: var(--color-text-muted);
    cursor: help;
    display: inline-flex;
    font-size: 0.85em;
    inline-size: 1.1rem;
    justify-content: center;
    line-height: 1;

    &:hover,
    &:focus-visible { color: var(--color-primary); }
    &:focus-visible {
      outline: 2px solid var(--color-focus);
      outline-offset: 2px;
    }
  }
  .info-tooltip-bubble {
    --info-tooltip-arrow-inset-end: auto;
    --info-tooltip-arrow-inset-start: 50%;
    --info-tooltip-arrow-transform: translateX(-50%);
    --info-tooltip-inset-end: auto;
    --info-tooltip-inset-start: 50%;
    --info-tooltip-shift: -50%;

    background: var(--color-text);
    border-radius: var(--radius-md);
    box-shadow: var(--shadow-md);
    color: var(--color-bg);
    font-size: 0.8rem;
    font-weight: 400;
    inline-size: max-content;
    inset-block-end: calc(100% + 0.5rem);
    inset-inline-end: var(--info-tooltip-inset-end);
    inset-inline-start: var(--info-tooltip-inset-start);
    line-height: 1.4;
    max-inline-size: 16rem;
    opacity: 0;
    padding: 0.625rem 0.75rem;
    pointer-events: none;
    position: absolute;
    text-align: start;
    transform: translateX(var(--info-tooltip-shift)) translateY(4px);
    transition: opacity 0.15s ease, transform 0.15s ease;
    visibility: hidden;
    white-space: normal;
    z-index: 20;

    &::after {
      border: 5px solid transparent;
      border-block-start-color: var(--color-text);
      content: "";
      inset-block-start: 100%;
      inset-inline-end: var(--info-tooltip-arrow-inset-end);
      inset-inline-start: var(--info-tooltip-arrow-inset-start);
      position: absolute;
      transform: var(--info-tooltip-arrow-transform);
    }
    .info-tooltip-trigger:hover + &,
    .info-tooltip-trigger:focus-visible + & {
      opacity: 1;
      transform: translateX(var(--info-tooltip-shift));
      visibility: visible;
    }

    /* Table headers are narrow and left-aligned; anchor the bubble to the start
       edge instead of centering so it doesn't get clipped against neighbours. */
    th & {
      --info-tooltip-arrow-inset-start: 0.75rem;
      --info-tooltip-arrow-transform: none;
      --info-tooltip-inset-start: 0;
      --info-tooltip-shift: 0;
    }
    /* The last header cells sit close to the page edge, where a start-anchored
       bubble runs off-screen and is half unreadable. Anchor those to the end. */
    th:nth-last-child(-n+2) & {
      --info-tooltip-arrow-inset-end: 0.75rem;
      --info-tooltip-arrow-inset-start: auto;
      --info-tooltip-inset-end: 0;
      --info-tooltip-inset-start: auto;
    }
  }
  /* Start-anchored variant: opens toward the end from the trigger. For tooltips near
     the start of a column (e.g. at the end of a form label) where the centred bubble
     would run off the page. */
  .info-tooltip-bubble--start {
    --info-tooltip-arrow-inset-start: 0.75rem;
    --info-tooltip-arrow-transform: none;
    --info-tooltip-inset-start: 0;
    --info-tooltip-shift: 0;
  }
}
