/*
 * Images attached to a record: the grid that lists them and the viewer that
 * opens one.
 *
 * Every feature that carried photos drew both itself, and each drew them
 * differently — one had a bordered card per photo with two text buttons under
 * it, the next had a bare thumbnail that opened a fixed-size box with a "×" in
 * the corner. A photo attached to a record is one thing in the product, so it
 * gets one shape: a square tile that opens, an action that appears on it, and a
 * viewer that can actually be read — zoom, pan, rotate and the next photo
 * without going back to the page.
 */
.fs-image-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(132px, 1fr));
  gap: var(--fs-space-3);
  min-width: 0;
}

.fs-image-grid__empty {
  display: flex;
  align-items: center;
  gap: var(--fs-space-2);
  grid-column: 1 / -1;
  color: var(--fs-color-text-secondary);
  font-size: var(--fs-text-sm);
}

/* The empty state names what is missing with the same glyph the section uses. */
.fs-image-grid__empty::before {
  display: block;
  width: 16px;
  height: 16px;
  flex: 0 0 16px;
  background: var(--fs-color-text-muted);
  content: "";
  mask: url("/stoneware/icons/task/image.svg") center / 16px 16px no-repeat;
  -webkit-mask: url("/stoneware/icons/task/image.svg") center / 16px 16px no-repeat;
}

.fs-image-tile {
  position: relative;
  display: grid;
  gap: 6px;
  min-width: 0;
  margin: 0;
}

/*
 * The tile is the button: a photo is opened by clicking the photo, not by a
 * word under it. Its own surface shows through while the image loads, so the
 * grid keeps its shape instead of collapsing and jumping.
 */
.fs-image-tile__open {
  position: relative;
  display: block;
  width: 100%;
  aspect-ratio: 1 / 1;
  overflow: hidden;
  padding: 0;
  background: var(--fs-color-surface-subtle);
  border: 1px solid var(--fs-color-border-subtle);
  border-radius: var(--fs-radius-md);
  cursor: zoom-in;
  transition: border-color 0.16s ease, box-shadow 0.16s ease;
}

.fs-image-tile__open img {
  width: 100%;
  height: 100%;
  object-fit: cover;
}

@media (hover: hover) {
  .fs-image-tile__open:hover {
    border-color: var(--fs-color-primary-line);
    box-shadow: var(--fs-shadow-button-hover);
  }
}

.fs-image-tile__open:focus-visible {
  outline: none;
  border-color: var(--fs-color-primary);
  box-shadow: var(--fs-focus-ring);
}

/*
 * The magnifier says the tile opens. It stays out of the way until the pointer
 * is on the tile; on touch, where there is no hover, it is always visible.
 */
.fs-image-tile__zoom {
  position: absolute;
  right: 6px;
  bottom: 6px;
  width: 26px;
  height: 26px;
  background: color-mix(in srgb, var(--fs-palette-44) 52%, transparent);
  border-radius: var(--fs-radius-sm);
  opacity: 1;
  transition: opacity 0.16s ease;
}

.fs-image-tile__zoom::after {
  position: absolute;
  inset: 0;
  background: #fff;
  content: "";
  mask: url("/stoneware/icons/task-actions/zoom-in.svg") center / 16px 16px no-repeat;
  -webkit-mask: url("/stoneware/icons/task-actions/zoom-in.svg") center / 16px 16px no-repeat;
}

@media (hover: hover) {
  .fs-image-tile__zoom {
    opacity: 0;
  }

  .fs-image-tile:hover .fs-image-tile__zoom,
  .fs-image-tile__open:focus-visible .fs-image-tile__zoom {
    opacity: 1;
  }
}

/* Removal sits on the photo it removes, at the far corner from the magnifier. */
.fs-image-tile__delete {
  position: absolute;
  top: 6px;
  right: 6px;
}

.fs-image-tile__caption {
  display: flex;
  align-items: center;
  gap: 6px;
  min-width: 0;
}

.fs-image-tile__name {
  overflow: hidden;
  color: var(--fs-color-text-secondary);
  font-size: var(--fs-text-xs);
  text-overflow: ellipsis;
  white-space: nowrap;
}

.fs-image-tile--pending .fs-image-tile__open {
  border-style: dashed;
}

/*
 * Viewer. Full-bleed on purpose: a photo is looked at, and a framed dialog with
 * a page showing around it spends the screen on the page instead of the photo.
 */
.fs-image-viewer {
  position: fixed;
  inset: 0;
  width: 100vw;
  max-width: none;
  height: 100dvh;
  max-height: none;
  margin: 0;
  padding: 0;
  overflow: hidden;
  color: var(--fs-color-text);
  background: transparent;
  border: 0;
}

.fs-image-viewer::backdrop {
  background: color-mix(in srgb, var(--fs-palette-44) 88%, transparent);
}

.fs-image-viewer__frame {
  display: grid;
  grid-template-rows: auto minmax(0, 1fr);
  height: 100%;
}

/*
 * One bar: what is open on the left, what can be done to it on the right. The
 * bar keeps the product's surface rather than going dark with the stage — the
 * controls are the same controls as anywhere else and are read the same way.
 */
.fs-image-viewer__bar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: var(--fs-space-3);
  min-height: 54px;
  padding: var(--fs-space-2) var(--fs-space-3);
  background: var(--fs-color-surface);
  border-bottom: 1px solid var(--fs-color-border-subtle);
}

.fs-image-viewer__title {
  display: flex;
  align-items: baseline;
  gap: var(--fs-space-2);
  min-width: 0;
}

.fs-image-viewer__name {
  overflow: hidden;
  color: var(--fs-color-text);
  font-size: var(--fs-text-md);
  font-weight: 600;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.fs-image-viewer__counter {
  flex: 0 0 auto;
  color: var(--fs-color-text-muted);
  font-size: var(--fs-text-xs);
  font-variant-numeric: tabular-nums;
}

.fs-image-viewer__tools {
  display: flex;
  align-items: center;
  gap: var(--fs-space-1);
  flex: 0 0 auto;
}

/* The zoom figure sits between its two controls and reserves its own width, so
   the row does not shuffle sideways between 100% and 25%. */
.fs-image-viewer__scale {
  min-width: 52px;
  color: var(--fs-color-text-secondary);
  font-size: var(--fs-text-xs);
  font-variant-numeric: tabular-nums;
  text-align: center;
}

.fs-image-viewer__separator {
  width: 1px;
  height: 22px;
  margin-inline: var(--fs-space-1);
  background: var(--fs-color-border-subtle);
}

/*
 * The single cell is `minmax(0, 1fr)`, not an implicit auto row: against an auto
 * row the image's `height: 100%` and `max-height: 100%` resolve to nothing, the
 * box grows to the file's natural size, and a large original is cropped by the
 * stage instead of fitting inside it.
 */
.fs-image-viewer__stage {
  position: relative;
  display: grid;
  grid-template: minmax(0, 1fr) / minmax(0, 1fr);
  place-items: center;
  overflow: hidden;
  background: color-mix(in srgb, var(--fs-palette-44) 92%, transparent);
  touch-action: none;
  min-width: 0;
  min-height: 0;
}

/*
 * Intrinsic size capped by the stage rather than a full-bleed box with
 * `object-fit`: the element box is then the painted photo itself, so panning
 * bounds and rotation measure the picture instead of the empty strips beside it.
 */
.fs-image-viewer__image {
  width: auto;
  height: auto;
  max-width: 100%;
  max-height: 100%;
  object-fit: contain;
  transform-origin: center center;
  user-select: none;
  -webkit-user-drag: none;
}

.fs-image-viewer--zoomed .fs-image-viewer__image {
  cursor: grab;
}

.fs-image-viewer--panning .fs-image-viewer__image {
  cursor: grabbing;
}

/*
 * Stepping between photos belongs on the stage, at thumb height, not in the
 * bar: with the bar it would be two travels of the pointer to look through six
 * photos. A single photo has no steps, so the controls are absent, not disabled.
 */
.fs-image-viewer__step {
  position: absolute;
  top: 50%;
  width: 44px;
  height: 44px;
  margin-top: -22px;
  color: #fff;
  background: color-mix(in srgb, var(--fs-palette-44) 55%, transparent);
  border: 1px solid color-mix(in srgb, #fff 22%, transparent);
  border-radius: 999px;
}

.fs-image-viewer__step--prev {
  left: var(--fs-space-3);
}

.fs-image-viewer__step--next {
  right: var(--fs-space-3);
}

.fs-image-viewer__step::after {
  display: block;
  width: 100%;
  height: 100%;
  background: currentcolor;
  content: "";
  mask-position: center;
  mask-repeat: no-repeat;
  mask-size: 22px 22px;
  -webkit-mask-position: center;
  -webkit-mask-repeat: no-repeat;
  -webkit-mask-size: 22px 22px;
}

.fs-image-viewer__step--prev::after {
  mask-image: url("/stoneware/icons/task-actions/arrow-left.svg");
  -webkit-mask-image: url("/stoneware/icons/task-actions/arrow-left.svg");
}

.fs-image-viewer__step--next::after {
  mask-image: url("/stoneware/icons/task-actions/arrow-right.svg");
  -webkit-mask-image: url("/stoneware/icons/task-actions/arrow-right.svg");
}

@media (hover: hover) {
  .fs-image-viewer__step:hover {
    background: color-mix(in srgb, var(--fs-palette-44) 75%, transparent);
  }
}

.fs-image-viewer__step:focus-visible {
  outline: none;
  box-shadow: var(--fs-focus-ring);
}

.fs-image-viewer__hint {
  position: absolute;
  bottom: var(--fs-space-3);
  left: 50%;
  padding: 4px 10px;
  color: #fff;
  background: color-mix(in srgb, var(--fs-palette-44) 55%, transparent);
  border-radius: 999px;
  font-size: var(--fs-text-xs);
  transform: translateX(-50%);
}

@media (max-width: 760px) {
  .fs-image-grid {
    grid-template-columns: repeat(auto-fill, minmax(104px, 1fr));
  }

  /* On a phone the bar keeps the name and the two acts that are used there;
     the rest of the toolbar is what pinch and drag already do better. */
  .fs-image-viewer__scale,
  .fs-image-viewer__separator,
  .fs-image-viewer__tools [data-image-viewer-zoom-in],
  .fs-image-viewer__tools [data-image-viewer-zoom-out],
  .fs-image-viewer__hint {
    display: none;
  }
}
