/*
 * Image presentation layer: semantic figures, responsive evidence sets,
 * decode-aware arrival, complete attribution, and a native-dialog detail view.
 */

.pc-image,
.pc-image-set,
.pc-image-tile,
.image-lightbox-figure {
  margin: 0;
}

.pc-image[data-state="resolved"],
.pc-image-set {
  isolation: isolate;
  border-radius: 20px;
}

/* `[data-state="placeholder"]` (the server is still searching/downloading --
   typically the longer wait of the two, several seconds and sometimes much
   more) gets the same sweep as the post-resolve decode wait below. Without
   this, the placeholder was a static dashed box with only faint title text
   in it -- nothing on screen changed for however long the search took, which
   read as "the image never loads" even though it eventually did. */
.pc-image[data-state="resolved"]::before,
.pc-image[data-state="placeholder"]::before,
.pc-image-tile::before {
  content: "";
  position: absolute;
  inset: 0;
  z-index: 1;
  pointer-events: none;
  background:
    linear-gradient(
      105deg,
      color-mix(in srgb, var(--wa-stage-sunken) 92%, transparent) 18%,
      color-mix(in srgb, var(--wa-stage-raised) 86%, transparent) 40%,
      color-mix(in srgb, var(--wa-stage-sunken) 92%, transparent) 62%
    );
  background-size: 220% 100%;
  animation: imageLoadingSweep 1.45s linear infinite;
  transition: opacity 180ms var(--ease-out);
}

.pc-image[data-state="resolved"][data-ready]::before,
.pc-image-tile[data-ready]::before {
  opacity: 0;
}

/* The resolved-state sweep sits (deliberately) above its own `<img>`, which
   starts at `opacity: 0` during decode, so the stacking order there never
   mattered. The placeholder's only content is a bare title/query text node,
   with nothing else to hide it behind -- an absolutely-positioned `z-index:
   1` `::before` paints above normal in-flow content regardless of the
   number, so without `isolation: isolate` + a negative `z-index` here the
   sweep would sit on top of that text and make it unreadable for however
   long the search takes. */
.pc-image[data-state="placeholder"] {
  isolation: isolate;
}
.pc-image[data-state="placeholder"]::before {
  z-index: -1;
}

.pc-image-media,
.pc-image-tile-media {
  opacity: 0;
  transform: scale(1.008);
  transition:
    opacity 220ms var(--ease-out),
    transform 360ms var(--ease-out);
}

.pc-image[data-ready] > .pc-image-media,
.pc-image-tile[data-ready] > .pc-image-tile-media {
  opacity: 1;
  transform: none;
}

.pc-image-media[data-fit="contain"],
.pc-image-tile-media[data-fit="contain"] {
  object-fit: contain;
  background: var(--wa-stage-sunken);
}

.pc-image[data-error]::after,
.pc-image-tile[data-error]::after {
  content: "Image unavailable";
  position: absolute;
  inset: 0;
  z-index: 2;
  display: grid;
  place-items: center;
  padding: 1rem;
  color: var(--wa-ink-faint);
  background: var(--wa-stage-sunken);
  font-size: 0.86rem;
}

/* Deliberately no forced `aspect-ratio` on the set itself (see the single-
   image frame's own comment on the same decision): a fixed-ratio box split
   into 2-4 cells routinely squeezed each tile shorter than its own 2-line
   attribution caption needed, so the caption -- clipped by the tile's own
   `overflow: hidden` -- lost its top line or vanished entirely. Sizing off
   content instead (`min-height` per row below, `max-height` here as the only
   cap) means a tile is only ever as short as its own photo, never shorter
   than what its caption needs. */
.pc-image-set {
  position: relative;
  width: min(100%, 54rem);
  min-height: 14rem;
  max-height: min(60vh, 34rem);
  margin-inline: auto;
  padding: 0.55rem;
  display: grid;
  gap: 0.55rem;
  align-self: center;
  justify-self: center;
  overflow: hidden;
  background: var(--wa-stage-sunken);
  border: 1px solid var(--wa-line);
  box-shadow: 0 24px 54px -24px color-mix(in srgb, var(--wa-ink) 48%, transparent);
}

.app[data-mode="session"] .pc-image-set {
  min-height: 0;
  max-height: 100%;
}

/* Width hints survive per preset (a portrait pairing shouldn't sprawl as
   wide as a landscape one) but no longer force an exact total height. */
.pc-image-set[data-aspect-ratio="4:3"] { max-width: 46rem; }
.pc-image-set[data-aspect-ratio="1:1"] { max-width: 34rem; }
.pc-image-set[data-aspect-ratio="9:16"] { max-width: 23rem; }

/* `minmax(9rem, 1fr)`, not `minmax(0, 1fr)`: 9rem is enough room for a
   two-line caption (~3.3rem of padding + clamped text) plus a legible sliver
   of the photo above it, so no tile can be squeezed shorter than its own
   caption needs regardless of how many tiles share the row. */
.pc-image-set[data-layout="split"] {
  grid-template-columns: repeat(2, minmax(0, 1fr));
  grid-template-rows: minmax(9rem, 1fr);
}

.pc-image-set[data-layout="mosaic"] {
  grid-template-columns: minmax(0, 1.65fr) minmax(0, 1fr);
  grid-template-rows: repeat(2, minmax(9rem, 1fr));
}

.pc-image-set[data-layout="mosaic"] > .pc-image-tile:first-child {
  grid-row: 1 / span 2;
}

.pc-image-set[data-layout="grid"] {
  grid-template-columns: repeat(2, minmax(0, 1fr));
  grid-template-rows: repeat(2, minmax(9rem, 1fr));
}

.pc-image-tile {
  position: relative;
  min-width: 0;
  min-height: 0;
  overflow: hidden;
  border-radius: 14px;
  background: var(--wa-stage-raised);
  border: 1px solid color-mix(in srgb, var(--wa-line) 82%, transparent);
}

.pc-image-tile-media {
  display: block;
  width: 100%;
  height: 100%;
  object-fit: cover;
}

.pc-image-caption,
.pc-tile-caption {
  margin: 0;
}

.pc-image[data-state="resolved"] .pc-image-caption {
  position: absolute;
  inset: auto 0 0;
  z-index: 3;
  pointer-events: none;
}

.pc-image[data-state="resolved"] .pc-image-caption .pc-attribution,
.pc-tile-caption .pc-tile-attribution {
  position: static;
  display: -webkit-box;
  overflow: hidden;
  padding: 0.5rem 0.75rem;
  color: var(--wa-ink-faint);
  background:
    linear-gradient(
      to top,
      color-mix(in srgb, var(--wa-stage) 94%, transparent),
      color-mix(in srgb, var(--wa-stage) 72%, transparent)
    );
  font-size: 0.7rem;
  line-height: 1.28;
  text-align: left;
  text-decoration: none;
  white-space: normal;
  -webkit-box-orient: vertical;
  -webkit-line-clamp: 2;
  line-clamp: 2;
  pointer-events: auto;
  transition:
    color 140ms var(--ease-out),
    background-color 140ms var(--ease-out);
}

.pc-tile-caption {
  position: absolute;
  inset: auto 0 0;
  z-index: 3;
  pointer-events: none;
}

.pc-tile-caption .pc-tile-attribution {
  pointer-events: auto;
}

.pc-image-caption .pc-attribution:focus-visible,
.pc-tile-caption .pc-tile-attribution:focus-visible {
  outline: 3px solid var(--wa-ink-muted);
  outline-offset: -2px;
}

.pc-image-caption .pc-attribution:hover,
.pc-tile-caption .pc-tile-attribution:hover {
  color: var(--wa-ink);
  text-decoration: underline;
}

.pc-image-expand {
  appearance: none;
  position: absolute;
  top: 0.7rem;
  right: 0.7rem;
  z-index: 5;
  display: grid;
  place-items: center;
  width: 2.35rem;
  height: 2.35rem;
  padding: 0;
  color: var(--wa-ink);
  background: color-mix(in srgb, var(--wa-stage) 82%, transparent);
  border: 1px solid color-mix(in srgb, var(--wa-line) 90%, transparent);
  border-radius: 999px;
  box-shadow: 0 8px 24px color-mix(in srgb, var(--wa-stage) 48%, transparent);
  cursor: zoom-in;
  opacity: 0;
  transform: translateY(-0.2rem);
  transition:
    opacity 150ms var(--ease-out),
    transform 180ms var(--ease-out),
    background-color 150ms var(--ease-out);
}

.pc-image-expand svg {
  width: 1.05rem;
  height: 1.05rem;
}

.pc-image:hover > .pc-image-expand,
.pc-image:focus-within > .pc-image-expand,
.pc-image-tile:hover > .pc-image-expand,
.pc-image-tile:focus-within > .pc-image-expand {
  opacity: 1;
  transform: none;
}

.pc-image-expand:hover {
  background: var(--wa-stage-raised);
}

.pc-image-expand:focus-visible {
  opacity: 1;
  transform: none;
  outline: 3px solid var(--wa-ink-muted);
  outline-offset: 2px;
}

.image-lightbox {
  width: min(94vw, 86rem);
  max-width: none;
  max-height: 92dvh;
  padding: 0;
  overflow: hidden;
  color: var(--wa-ink);
  background: var(--wa-stage-raised);
  border: 1px solid var(--wa-line);
  border-radius: 22px;
  box-shadow: 0 32px 90px color-mix(in srgb, var(--wa-stage) 62%, transparent);
}

.image-lightbox::backdrop {
  background: color-mix(in srgb, var(--wa-stage) 84%, transparent);
  backdrop-filter: blur(12px);
}

.image-lightbox[open] {
  display: grid;
  grid-template-rows: auto minmax(0, 1fr);
}

.image-lightbox-chrome {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 1rem;
  padding: 0.85rem 1rem;
  border-bottom: 1px solid var(--wa-line);
}

.image-lightbox-heading {
  min-width: 0;
}

.image-lightbox-title {
  margin: 0;
  overflow: hidden;
  font-family: var(--wa-font-heading);
  font-size: clamp(1rem, 2vw, 1.25rem);
  line-height: 1.25;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.image-lightbox-close {
  appearance: none;
  flex: none;
  display: grid;
  place-items: center;
  width: 2.45rem;
  height: 2.45rem;
  padding: 0;
  color: var(--wa-ink);
  background: transparent;
  border: 1px solid var(--wa-line);
  border-radius: 999px;
  cursor: pointer;
}

.image-lightbox-close svg {
  width: 1.05rem;
  height: 1.05rem;
}

.image-lightbox-close:focus-visible {
  outline: 3px solid var(--wa-ink-muted);
  outline-offset: 2px;
}

.image-lightbox-figure {
  min-height: 0;
  display: grid;
  grid-template-rows: minmax(0, 1fr) auto;
  background: var(--wa-stage-sunken);
}

.image-lightbox-media {
  align-self: center;
  justify-self: center;
  display: block;
  max-width: 100%;
  max-height: calc(92dvh - 8.5rem);
  object-fit: contain;
  opacity: 0;
  transition: opacity 180ms var(--ease-out);
}

.image-lightbox[data-ready] .image-lightbox-media {
  opacity: 1;
}

.image-lightbox-caption {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: 1rem;
  padding: 0.75rem 1rem;
  color: var(--wa-ink-faint);
  background: var(--wa-stage-raised);
  border-top: 1px solid var(--wa-line);
  font-size: 0.78rem;
  line-height: 1.4;
}

.image-lightbox-description {
  min-width: 0;
}

.image-lightbox-source {
  flex: none;
  color: var(--wa-ink);
  text-decoration: none;
}

.image-lightbox-source:hover {
  text-decoration: underline;
}

html[data-image-dialog-open] {
  overflow: hidden;
}

@media (max-width: 40rem) {
  .pc-image-set {
    display: flex;
    gap: 0.65rem;
    aspect-ratio: auto !important;
    max-width: 100%;
    min-height: 16rem;
    overflow-x: auto;
    overflow-y: hidden;
    scroll-padding-inline: 0.45rem;
    scroll-snap-type: inline mandatory;
    overscroll-behavior-inline: contain;
    scrollbar-width: thin;
  }

  .pc-image-set > .pc-image-tile {
    flex: 0 0 86%;
    min-height: 0;
    aspect-ratio: 4 / 3;
    scroll-snap-align: center;
  }

  .image-lightbox {
    width: 100vw;
    max-height: 100dvh;
    border-radius: 0;
  }

  .image-lightbox-media {
    max-height: calc(100dvh - 9rem);
  }

  .image-lightbox-caption {
    display: grid;
  }
}

@media (hover: none), (pointer: coarse) {
  .pc-image-expand {
    opacity: 1;
    transform: none;
  }
}

@media (prefers-reduced-motion: reduce) {
  .pc-image[data-state="resolved"]::before,
  .pc-image-tile::before {
    animation: none;
  }

  .pc-image-media,
  .pc-image-tile-media,
  .pc-image-expand,
  .image-lightbox-media {
    transition-duration: 0s;
  }
}

@keyframes imageLoadingSweep {
  from { background-position: 110% 0; }
  to { background-position: -110% 0; }
}
