/*
 * Slider: scroll-snap track that bleeds to the right viewport edge so the next slide peeks, plus slider-controls
 * (progress line and arrows). Works without JavaScript (scrollable, focusable); the module reveals the controls.
 * Slide width: 312 px on phones; from 1024 px 1 / --columns slides per container width (container 1036 or 1248 px).
 * Hooks: --slider-gap, --slider-item-width (set on .slider to override).
 */
@layer components {
  .slider {
    --slider-container: var(--layout-container-wide);
    --slider-gap: var(--layout-grid-gap);
    --slider-gutter: max(var(--layout-margin), calc((100vw - var(--slider-container)) / 2));
    --slider-visible: calc(100cqw - 2 * var(--slider-gutter));
    --slider-item-width: 312px;
    /* Space between the slides (their bottom edge, not the hover shadow) and the controls: 24 → 32 on phones, 24 → 40 on desktop. */
    --slider-controls-gap: var(--space-32);
    display: flex;
    flex-direction: column;
    gap: var(--slider-controls-gap);
    /* Full viewport width, centred on the container. */
    inline-size: 100vw;
    margin-inline: calc(50% - 50vw);
  }

  .slider--full {
    --slider-container: var(--layout-container-full);
  }

  /* Page must not scroll sideways because of the bleed (100vw includes a classic scrollbar). */
  .section:has(.slider) {
    overflow-x: clip;
  }

  .slider__track {
    container-type: inline-size;
    overflow-x: auto;
    overscroll-behavior-x: contain;
    scroll-snap-type: x mandatory;
    scroll-padding-inline: var(--slider-gutter);
    scrollbar-width: none;
    /* overflow-x: auto clips vertically too: room for the hover lift (4 px) and shadow (8 px), taken back by the negative margin. */
    padding-block: var(--space-16);
    margin-block: calc(-1 * var(--space-16));
  }

  .slider__track::-webkit-scrollbar {
    display: none;
  }

  .slider__list {
    display: flex;
    gap: var(--slider-gap);
    inline-size: max-content;
    margin: 0;
    padding: 0 var(--slider-gutter);
    list-style: none;
  }

  .slider__item {
    flex: none;
    inline-size: var(--slider-item-width);
    min-inline-size: 0;
    scroll-snap-align: start;
  }

  @media (min-width: 1024px) {
    .slider {
      --slider-controls-gap: var(--space-40);
    }

    .slider[data-columns='1'] {
      --slider-item-width: var(--slider-visible);
    }

    .slider[data-columns='2'] {
      --slider-item-width: calc((var(--slider-visible) - var(--slider-gap)) / 2);
    }

    .slider[data-columns='3'] {
      --slider-item-width: calc((var(--slider-visible) - 2 * var(--slider-gap)) / 3);
    }

    .slider[data-columns='4'] {
      --slider-item-width: calc((var(--slider-visible) - 3 * var(--slider-gap)) / 4);
    }
  }

  /* Keyboard users scroll a focused track with the arrow keys; show where the focus is. */
  .slider__track:focus-visible {
    outline-offset: -3px;
  }

  .slider-controls {
    display: flex;
    align-items: center;
    gap: var(--space-24);
    padding-inline: var(--slider-gutter);
  }

  .slider-controls[hidden] {
    display: none;
  }

  .slider-controls__progress {
    position: relative;
    flex: 1 1 auto;
    block-size: 2px;
    border-radius: var(--radius-pill);
    background-color: var(--color-border-default);
  }

  .slider-controls__thumb {
    position: absolute;
    inset-block: -1px;
    inset-inline-start: 0;
    inline-size: 100%;
    border-radius: var(--radius-pill);
    background-color: var(--color-text-default);
    block-size: 4px;
    transform-origin: left center;
  }

  .slider-controls__buttons {
    display: flex;
    flex: none;
    gap: var(--space-8);
  }
}
