@layer cinder.tokens, cinder.foundation, cinder.components, cinder.utilities;
@layer cinder.components {
  .cinder-carousel {
    display: grid;
    gap: var(--cinder-carousel-gap, var(--cinder-space-3));
  }

  .cinder-carousel__viewport {
    position: relative;
    display: flex;
    overflow-x: auto;
    overflow-y: hidden;
    touch-action: pan-x pan-y pinch-zoom;
    scroll-snap-type: x mandatory;
    overscroll-behavior-x: contain;
    scrollbar-width: none;
    border: 1px solid var(--cinder-border);
    border-radius: var(--cinder-radius-lg);
    background: var(--cinder-surface-raised);
  }

  .cinder-carousel__viewport::-webkit-scrollbar {
    display: none;
  }

  .cinder-carousel__viewport[data-cinder-dragging],
  .cinder-carousel__viewport[data-cinder-dragging] * {
    user-select: none;
    -webkit-user-drag: none;
  }

  /* The drag-to-scroll engine disables itself under prefers-reduced-motion
     (matches useReducedMotion() in the Svelte module) — the grab/grabbing
     affordance must not promise drag behavior it won't deliver. */
  @media (hover: hover) and (pointer: fine) and (prefers-reduced-motion: no-preference) {
    .cinder-carousel__viewport {
      cursor: grab;
    }

    .cinder-carousel__viewport[data-cinder-dragging] {
      cursor: grabbing;
    }
  }

  @media (hover: hover) and (pointer: fine) {
    :root:is([data-cinder-reduced-motion='false'], [data-reduced-motion='off'])
      .cinder-carousel__viewport {
      cursor: grab;
    }

    :root:is([data-cinder-reduced-motion='false'], [data-reduced-motion='off'])
      .cinder-carousel__viewport[data-cinder-dragging] {
      cursor: grabbing;
    }
  }

  .cinder-carousel__slide {
    flex: 0 0 var(--cinder-carousel-slide-size, 100%);
    min-inline-size: var(--cinder-carousel-slide-size, 100%);
    scroll-snap-align: start;
    display: grid;
    gap: var(--cinder-space-3);
    padding: var(--cinder-space-4);
  }

  .cinder-carousel[data-cinder-align='center'] .cinder-carousel__slide {
    scroll-snap-align: center;
  }

  .cinder-carousel__slide[data-cinder-collapsed] {
    block-size: 0;
    min-block-size: 0;
    padding-block: 0;
    overflow: hidden;
  }

  .cinder-carousel__link {
    display: grid;
    gap: var(--cinder-space-3);
    color: inherit;
    text-decoration: none;
  }

  .cinder-carousel__image {
    width: 100%;
    border-radius: var(--cinder-radius-md);
    object-fit: cover;
    aspect-ratio: var(--cinder-carousel-aspect-ratio, 16 / 9);
    background: var(--cinder-surface-inset);
  }

  .cinder-carousel__title {
    margin: 0;
    font: var(--cinder-font-semibold) var(--cinder-text-lg) / 1.3 var(--cinder-font-sans);
  }

  .cinder-carousel__description {
    margin: 0;
    color: var(--cinder-text-muted);
    line-height: 1.5;
  }

  /* The slides with Previous in a gutter at their inline start and Next at their inline end,
   * centred on the slides. Gutters, not an overlay, so a chevron never covers a slide's words or
   * image, at a phone's width too; the grid's inline axis mirrors them in right to left. */
  .cinder-carousel__stage {
    display: grid;
    grid-template-columns: auto minmax(0, 1fr) auto;
    grid-template-areas: 'previous slides next';
    align-items: center;
    column-gap: var(--cinder-space-2);
  }

  .cinder-carousel__stage > .cinder-carousel__viewport {
    grid-area: slides;
  }

  /* The navigation group adds no box: its two buttons (in their tooltip wrappers) are the grid's
   * gutter items. */
  .cinder-carousel__nav {
    display: contents;
  }

  .cinder-carousel__nav > :first-child {
    grid-area: previous;
  }

  .cinder-carousel__nav > :last-child {
    grid-area: next;
  }

  /* Previous and Next are round chevron buttons; each keeps a 44 by 44 hit area on a coarse
   * pointer through a pseudo-element. */
  .cinder-carousel__control {
    appearance: none;
    position: relative;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    inline-size: 2rem;
    block-size: 2rem;
    padding: 0;
    border: 1px solid var(--cinder-border-control);
    border-radius: var(--cinder-radius-full);
    background: var(--cinder-surface-raised);
    color: inherit;
    font: inherit;
    cursor: pointer;
  }

  /* The pause control above the slides keeps its words. */
  .cinder-carousel__control--pause {
    justify-self: start;
    inline-size: auto;
    padding-inline: var(--cinder-space-3);
  }

  /* At the first or last slide without looping, the way that goes nowhere is disabled. */
  .cinder-carousel__control:disabled {
    cursor: not-allowed;
    border-color: var(--cinder-border-muted);
    color: var(--cinder-text-disabled);
  }

  /* The chevrons point the way the slides move, so they turn around in right to left. */
  .cinder-carousel__control:where([dir='rtl'], [dir='rtl'] *) > :where(svg) {
    transform: scaleX(-1);
  }

  .cinder-carousel__control:where([dir='rtl'] [dir='ltr'], [dir='rtl'] [dir='ltr'] *)
    > :where(svg) {
    transform: none;
  }

  /* The page buttons: small dots centred under the slides, the current one a wider accent pill. */
  .cinder-carousel__controls {
    display: flex;
    justify-content: center;
    align-items: center;
  }

  /* Wide enough apart that each dot's 24px hit area (44px on a coarse pointer) never overlaps its
   * neighbour's: the gap is the hit area less the dot. */
  .cinder-carousel__dots {
    --_cinder-carousel-dot-size: var(--cinder-carousel-dot-size, 0.5rem);
    --_cinder-carousel-dot-target: 1.5rem;
    display: inline-flex;
    align-items: center;
    gap: calc(var(--_cinder-carousel-dot-target) - var(--_cinder-carousel-dot-size));
    flex-wrap: wrap;
    justify-content: center;
    min-inline-size: 0;
    max-inline-size: 100%;
    padding-block: calc(
      (var(--_cinder-carousel-dot-target) - var(--_cinder-carousel-dot-size)) / 2
    );
  }

  @media (any-pointer: coarse) {
    .cinder-carousel__dots {
      --_cinder-carousel-dot-target: var(--cinder-touch-target-min);
    }
  }

  .cinder-carousel__dot {
    appearance: none;
    position: relative;
    inline-size: var(--_cinder-carousel-dot-size);
    block-size: var(--_cinder-carousel-dot-size);
    padding: 0;
    border: 0;
    border-radius: var(--cinder-radius-full);
    background: color-mix(in oklch, currentColor 30%, transparent);
    color: inherit;
    cursor: pointer;
  }

  .cinder-carousel__dot[aria-current='true'] {
    inline-size: calc(var(--_cinder-carousel-dot-size) * 2.5);
    background: var(--cinder-accent-solid);
  }

  /* The hit area is a pseudo-element centred on the dot, as tall and as wide as the target, so the
   * painted dot stays small. */
  .cinder-carousel__dot::after,
  .cinder-carousel__control::after {
    content: '';
    position: absolute;
    inset-block: min(0px, calc((100% - var(--_cinder-carousel-dot-target, 1.5rem)) / 2));
    inset-inline: min(0px, calc((100% - var(--_cinder-carousel-dot-target, 1.5rem)) / 2));
  }

  @media (any-pointer: coarse) {
    .cinder-carousel__control::after {
      inset-block: min(0px, calc((100% - var(--cinder-touch-target-min)) / 2));
      inset-inline: min(0px, calc((100% - var(--cinder-touch-target-min)) / 2));
    }
  }

  .cinder-carousel__counter {
    color: var(--cinder-text-muted);
    font: var(--cinder-font-medium) var(--cinder-text-sm) / 1.4 var(--cinder-font-sans);
    white-space: nowrap;
  }

  @media (prefers-reduced-motion: no-preference) {
    .cinder-carousel__dot {
      transition:
        inline-size var(--cinder-duration-fast) var(--cinder-ease-standard),
        background-color var(--cinder-duration-fast) var(--cinder-ease-standard);
    }
  }

  @media (prefers-reduced-motion: reduce) {
    :root:not([data-cinder-reduced-motion='false']):not([data-reduced-motion='off'])
      .cinder-carousel__viewport {
      scroll-behavior: auto;
    }
  }

  @media (hover: hover) {
    .cinder-carousel__control:hover:not(:disabled) {
      background: var(--cinder-surface-hover);
    }

    .cinder-carousel__dot:hover:not([aria-current='true']) {
      background: color-mix(in oklch, currentColor 55%, transparent);
    }
  }

  /* A visible ring a gap out from the dot or the button, so an 8px dot's focus still reads. */
  .cinder-carousel__control:focus-visible,
  .cinder-carousel__dot:focus-visible {
    /* cinder-focus-ring-owner: parent */
    outline: none;
    box-shadow:
      0 0 0 var(--cinder-ring-offset) var(--cinder-ring-offset-color),
      0 0 0 calc(var(--cinder-ring-offset) + var(--cinder-ring-width)) var(--cinder-ring-color);
  }

  @media (forced-colors: active) {
    .cinder-carousel__control:focus-visible,
    .cinder-carousel__dot:focus-visible {
      outline: var(--cinder-ring-width) solid ButtonText;
      outline-offset: var(--cinder-ring-width);
      box-shadow: none;
    }
  }

  .cinder-carousel__sr-only {
    position: absolute;
    width: 1px;
    height: 1px;
    margin: -1px;
    padding: 0;
    border: 0;
    overflow: hidden;
    clip: rect(0, 0, 0, 0);
    white-space: nowrap;
  }
}
