@layer components {
  .image {
    position: relative;
    overflow: hidden;
    border-radius: var(--radius-lg);
    margin: 0;

    & > img {
      display: block;
      width: 100%;
      height: 100%;
      object-fit: cover;
      transition: filter 200ms ease;
    }

    /* -- Progressive placeholder --------------------------------
       Why: the low-res stand-in (data-src-low) shows blurred until the
       real source is preloaded and swapped in; 8px is a hardcoded filter
       value per the token boundary (no --* token for filters). */
    & > img[data-loading] {
      filter: blur(8px);
    }

    /* -- Fit variants ---------------------------------------- */
    &[data-fit="contain"] > img { object-fit: contain; }
    &[data-fit="fill"]    > img { object-fit: fill; }
    &[data-fit="none"]    > img { object-fit: none; }

    /* -- Aspect ratio ---------------------------------------- */
    &[data-ratio="1/1"]  { aspect-ratio: 1 / 1; }
    &[data-ratio="4/3"]  { aspect-ratio: 4 / 3; }
    &[data-ratio="3/2"]  { aspect-ratio: 3 / 2; }
    &[data-ratio="16/9"] { aspect-ratio: 16 / 9; }
    &[data-ratio="21/9"] { aspect-ratio: 21 / 9; }
    &[data-ratio="3/4"]  { aspect-ratio: 3 / 4; }

    /* -- Radius variants ------------------------------------- */
    &[data-radius="none"] { border-radius: 0; }
    &[data-radius="sm"]   { border-radius: var(--radius-sm); }
    &[data-radius="md"]   { border-radius: var(--radius-md); }
    &[data-radius="full"] { border-radius: 9999px; }

    /* -- Preview cursor -------------------------------------- */
    &[data-preview] {
      cursor: zoom-in;
    }

    /* -- Fallback -------------------------------------------- */
    & > .image-fallback {
      position: absolute;
      inset: 0;
      display: flex;
      align-items: center;
      justify-content: center;
      background-color: var(--muted);
      color: var(--muted-foreground);

      & > svg {
        width: 2.5rem;
        height: 2.5rem;
        opacity: 0.5;
      }
    }

    /* Hide fallback when image loads successfully */
    &:has(> img:not([data-error])) > .image-fallback {
      display: none;
    }
  }

  /* -- Caption ----------------------------------------------- */
  .image-caption {
    padding: 0.625rem 0 0.25rem;
    font-size: 0.8125rem;
    line-height: 1.4;
    color: var(--muted-foreground);
    text-wrap: pretty;
  }

  /* -- Lightbox dialog --------------------------------------- */
  dialog.image-lightbox {
    border: none;
    background: transparent;
    padding: 0;
    margin: auto;
    position: fixed;
    inset: 0;
    max-width: 100vw;
    max-height: 100vh;
    width: 100vw;
    height: 100vh;
    overflow: hidden;
    opacity: 0;
    transition: opacity 200ms ease, display 200ms allow-discrete;

    &[open] {
      opacity: 1;
    }

    &::backdrop {
      background: oklch(0 0 0 / 0);
      transition: all 200ms ease, display 200ms allow-discrete;
    }

    &[open]::backdrop {
      background: oklch(0 0 0 / 0.85);
    }
  }

  @starting-style {
    dialog.image-lightbox[open] {
      opacity: 0;
    }

    dialog.image-lightbox[open]::backdrop {
      background: oklch(0 0 0 / 0);
    }
  }

  /* -- Scroll lock ------------------------------------------- */
  /* Page behind stays put while the lightbox is modal (see dialog.css):
     `:modal` + overflow:hidden freezes the viewport at its current offset. */
  html:has(dialog.image-lightbox:modal) {
    overflow: hidden;
    scrollbar-gutter: stable;
  }

  .image-lightbox-content {
    display: flex;
    align-items: center;
    justify-content: center;
    width: 100%;
    height: 100%;
    position: relative;

    & > img {
      max-width: 90vw;
      max-height: 85vh;
      object-fit: contain;
      border-radius: var(--radius-md);
      transition: transform 200ms ease;
      user-select: none;
      -webkit-user-drag: none;
    }
  }

  .image-lightbox-toolbar {
    position: absolute;
    top: 1rem;
    right: 1rem;
    display: flex;
    gap: 0.25rem;
    z-index: 1;
  }

  .image-lightbox-btn {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    width: 2.25rem;
    height: 2.25rem;
    border: none;
    border-radius: var(--radius-md);
    background: oklch(0 0 0 / 0.5);
    color: white;
    cursor: pointer;
    transition: background 150ms ease;

    &:hover {
      background: oklch(0 0 0 / 0.7);
    }

    &:focus-visible {
      outline: 2px solid var(--ring);
      outline-offset: 2px;
    }

    & > svg {
      width: 1rem;
      height: 1rem;
    }
  }

  @media (prefers-reduced-motion: reduce) {
    .image > img,
    dialog.image-lightbox,
    dialog.image-lightbox::backdrop,
    .image-lightbox-content > img {
      transition: none;
    }
  }

  /* -- Hover gallery (daisyUI's hover-gallery, plus) -----------------------
     A .hover-gallery holds 2-10 images in one cell. The first shows; every
     other image is an invisible vertical strip of the frame (1/n of its
     width, in order) that the pointer can hit - hovering strip k shows
     image k full-size, and the strips of the others stay above it so moving
     on keeps working. CSS only. data-ratio sizes the frame (default 4/3),
     data-indicator draws a position bar, data-effect="zoom" eases the shown
     image in, data-direction="vertical" slices the frame into rows. On
     touch screens (no hover) it becomes a swipeable scroll-snap strip. */
  .hover-gallery {
    --_n: 1;
    --_cur: 1;
    position: relative;
    display: grid;
    overflow: hidden;
    aspect-ratio: 4 / 3;
    border-radius: var(--radius-lg);
    background-color: var(--muted);
    isolation: isolate;

    &[data-ratio="1/1"] { aspect-ratio: 1; }
    &[data-ratio="3/4"] { aspect-ratio: 3 / 4; }
    &[data-ratio="16/9"] { aspect-ratio: 16 / 9; }

    & > :is(img, picture, video) {
      grid-area: 1 / 1;
      display: block;
      width: 100%;
      height: 100%;
      min-width: 0;
      min-height: 0;
      object-fit: cover;
    }
    & > :not(:first-child) {
      z-index: 1;
      opacity: 0;
      /* strip k of n: the columns (k-1)/n .. k/n */
      clip-path: inset(0 calc(100% - var(--_k) * 100% / var(--_n)) 0 calc((var(--_k) - 1) * 100% / var(--_n)));
      /* the switch is instant: a crossfade would leave both images half
         transparent for a moment and the first would shine through */
    }
    & > :not(:first-child):hover {
      z-index: 0;
      opacity: 1;
      clip-path: none;
    }
    /* with image.js: no switching until every image is decoded (data-ready) -
       the first one stays instead of a half-loaded frame */
    &[data-init]:not([data-ready]) > :not(:first-child) { visibility: hidden; }
    &[data-direction="vertical"] > :not(:first-child) {
      clip-path: inset(calc((var(--_k) - 1) * 100% / var(--_n)) 0 calc(100% - var(--_k) * 100% / var(--_n)) 0);
    }
    &[data-direction="vertical"] > :not(:first-child):hover { clip-path: none; }
    /* strip 1 needs a hit area too: moving back from image k must leave it */
    &::before {
      content: '';
      position: absolute;
      z-index: 1;
      inset: 0 calc(100% - 100% / var(--_n)) 0 0;
    }
    &[data-direction="vertical"]::before { inset: 0 0 calc(100% - 100% / var(--_n)) 0; }

    /* k per image, n from the count, the shown one for the indicator */
    & > :nth-child(2) { --_k: 2; }
    & > :nth-child(3) { --_k: 3; }
    & > :nth-child(4) { --_k: 4; }
    & > :nth-child(5) { --_k: 5; }
    & > :nth-child(6) { --_k: 6; }
    & > :nth-child(7) { --_k: 7; }
    & > :nth-child(8) { --_k: 8; }
    & > :nth-child(9) { --_k: 9; }
    & > :nth-child(10) { --_k: 10; }
    &:has(> :nth-child(2)) { --_n: 2; }
    &:has(> :nth-child(3)) { --_n: 3; }
    &:has(> :nth-child(4)) { --_n: 4; }
    &:has(> :nth-child(5)) { --_n: 5; }
    &:has(> :nth-child(6)) { --_n: 6; }
    &:has(> :nth-child(7)) { --_n: 7; }
    &:has(> :nth-child(8)) { --_n: 8; }
    &:has(> :nth-child(9)) { --_n: 9; }
    &:has(> :nth-child(10)) { --_n: 10; }
    &:has(> :nth-child(2):hover) { --_cur: 2; }
    &:has(> :nth-child(3):hover) { --_cur: 3; }
    &:has(> :nth-child(4):hover) { --_cur: 4; }
    &:has(> :nth-child(5):hover) { --_cur: 5; }
    &:has(> :nth-child(6):hover) { --_cur: 6; }
    &:has(> :nth-child(7):hover) { --_cur: 7; }
    &:has(> :nth-child(8):hover) { --_cur: 8; }
    &:has(> :nth-child(9):hover) { --_cur: 9; }
    &:has(> :nth-child(10):hover) { --_cur: 10; }

    /* position bar: n segments, the shown one bright */
    &[data-indicator]::after {
      content: '';
      position: absolute;
      z-index: 2;
      inset: auto 0.75rem 0.625rem;
      height: 3px;
      pointer-events: none;
      --_seg: calc((100% + 4px) / var(--_n));
      /* stops along the full width: the shown segment bright, all segments dim */
      background:
        linear-gradient(90deg, transparent calc((var(--_cur) - 1) * var(--_seg)), oklch(1 0 0) 0 calc(var(--_cur) * var(--_seg) - 4px), transparent 0),
        repeating-linear-gradient(90deg, oklch(1 0 0 / 0.45) 0 calc(var(--_seg) - 4px), transparent 0 var(--_seg));
      border-radius: 9999px;
      filter: drop-shadow(0 1px 1px oklch(0 0 0 / 0.35));
      opacity: 0;
      transition: opacity 150ms ease;
    }
    &[data-indicator]:hover::after,
    &[data-indicator="always"]::after { opacity: 1; }

    /* zoom: the shown image eases in from a slight scale */
    &[data-effect="zoom"] > :is(img, picture, video) { transition: scale 400ms ease; }
    &[data-effect="zoom"]:hover > :first-child:not(:has(~ :hover)),
    &[data-effect="zoom"] > :not(:first-child):hover { scale: 1.04; }
  }
  /* no hover (touch): a swipeable strip, one image per view */
  @media (hover: none) {
    .hover-gallery {
      display: flex;
      overflow-x: auto;
      scroll-snap-type: x mandatory;
      overscroll-behavior-inline: contain;
      scrollbar-width: none;

      & > :is(img, picture, video),
      & > :not(:first-child) {
        flex: 0 0 100%;
        opacity: 1;
        clip-path: none;
        scroll-snap-align: start;
      }
      &[data-indicator]::after { display: none; }
    }
  }
  @media (prefers-reduced-motion: reduce) {
    .hover-gallery > * { transition: none; }
    .hover-gallery[data-effect="zoom"] > * { scale: none !important; }
  }
}
