/*
 * Photos: the file input that adds them and the grid of thumbnails (shared/_photos).
 * The native file input keeps the browser's camera/gallery picker on phones;
 * only its button is styled, like .btn--secondary.
 */
.file-input {
  max-width: 100%;
  color: var(--color-text-muted);
  font-size: var(--text-sm);

  &::file-selector-button {
    height: var(--control-height-sm);
    margin-right: var(--space-3);
    padding: 0 var(--space-3);
    background: var(--color-surface);
    border: var(--border-strong);
    border-radius: var(--radius-md);
    box-shadow: var(--shadow-xs);
    color: var(--color-text);
    font: inherit;
    font-weight: var(--weight-medium);
    cursor: pointer;
  }

  &::file-selector-button:hover {
    background: var(--color-surface-hover);
  }

  &:focus-visible {
    outline: none;
  }

  &:focus-visible::file-selector-button {
    border-color: var(--color-focus);
    box-shadow: var(--focus-ring);
  }
}

.photo-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(6.5rem, 1fr));
  gap: var(--space-2);
  padding: 0;
  list-style: none;
}

.photo-grid__item {
  position: relative;

  img {
    width: 100%;
    aspect-ratio: 1;
    object-fit: cover;
    border-radius: var(--radius-md);
    background: var(--color-surface-sunken);
  }

  /* The ✕ that deletes a photo, over its corner */
  form {
    position: absolute;
    top: var(--space-1);
    right: var(--space-1);
  }

  .btn {
    background: var(--color-surface);
    box-shadow: var(--shadow-sm);
  }
}

/* "Añadir fotos" in a card's header: a label styled as a button around a hidden file input,
   so tapping it opens the camera or gallery and the form sends as soon as photos are picked */
.photo-upload__button {
  cursor: pointer;

  &:focus-within {
    box-shadow: var(--focus-ring);
  }
}

/* Photos waiting for a Gestor's approval: dimmed until they're the cat's */
.photo-grid--pending img {
  opacity: 0.6;
  outline: 2px dashed var(--color-warning-border);
}

/* A record's photos at the top of its page, each opening the full-screen carousel (shared/_photo_tiles) */
.photo-tiles__list {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(6.5rem, 1fr));
  gap: var(--space-2);
  margin: 0;
  padding: 0;
  list-style: none;
}

.photo-tiles__tile {
  aspect-ratio: 1;
  overflow: hidden;
  border: var(--border);
  border-radius: var(--radius-lg);
  background: var(--color-surface);

  img {
    display: block;
    width: 100%;
    height: 100%;
    object-fit: cover;
  }
}

.photo-tiles__open {
  display: block;
  height: 100%;
  cursor: zoom-in;
}

/* The photos' own card on an edit page, under its form: where they're deleted */
.edit-photos {
  margin-top: var(--space-4);
}

/* In a column of cards (an aviso's, beside its form) the column's gap already spaces it */
.stack > .edit-photos {
  margin-top: 0;
}

/* A record's photos on its page (shared/_photo_gallery): the large one, when opened, as wide as a cat's on its public
   page (its 3 of 5 columns of 68rem), and the row of small ones, sideways when they don't fit */
.photo-gallery {
  display: grid;
  gap: var(--space-2);
  margin-bottom: var(--space-4);
}

.photo-gallery__viewer {
  max-width: 40rem;

  .public-gallery__stage {
    background: var(--color-surface-sunken);
  }

  /* Always there (no hover on a phone to bring them) */
  .public-gallery__arrow {
    opacity: 1;
  }
}

.photo-gallery__close {
  position: absolute;
  top: var(--space-3);
  right: var(--space-3);
  display: grid;
  place-items: center;
  width: 2.5rem;
  height: 2.5rem;
  padding: 0;
  border: 0;
  border-radius: 50%;
  background: var(--color-surface);
  box-shadow: var(--shadow-md);
  color: var(--color-text);
  cursor: pointer;

  svg {
    width: 1.1rem;
    height: 1.1rem;
    fill: none;
    stroke: currentColor;
    stroke-width: 2;
    stroke-linecap: round;
  }

  &:focus-visible {
    box-shadow: var(--focus-ring);
  }
}

.photo-gallery__row {
  display: flex;
  gap: var(--space-2);
  margin: 0;
  /* Room for the ring of the one shown large, which the scrolling row would cut off */
  padding: 3px;
  overflow-x: auto;
  list-style: none;
  scrollbar-width: thin;

  > li {
    flex: none;
  }
}

.photo-gallery__thumb,
.photo-gallery__pending,
.photo-gallery__add {
  display: block;
  width: 5rem;
  height: 5rem;
  overflow: hidden;
  border: var(--border);
  border-radius: var(--radius-md);
  background: var(--color-surface-sunken);

  img {
    display: block;
    width: 100%;
    height: 100%;
    object-fit: cover;
  }
}

.photo-gallery__thumb {
  cursor: zoom-in;

  /* The one shown large, only while it's open */
  .photo-gallery:has(.photo-gallery__viewer:not([hidden])) &[aria-current="true"] {
    box-shadow: 0 0 0 2px var(--color-primary);
  }

  &:focus-visible {
    box-shadow: var(--focus-ring);
  }
}

/* Waiting for a Gestor's approval: dimmed until they're the cat's */
.photo-gallery__pending img {
  opacity: 0.6;
}

.photo-gallery__pending {
  border: 2px dashed var(--color-warning-border);
}

/* Add more (a cat's, for whoever can't edit it): a dashed square that opens the camera or the gallery */
.photo-gallery__add {
  display: grid;
  place-items: center;
  border-style: dashed;
  color: var(--color-text-subtle);
  cursor: pointer;

  svg {
    width: 1.75rem;
    height: 1.75rem;
  }

  &:hover {
    color: var(--color-text-muted);
    background: var(--color-surface-hover);
  }

  &:focus-within {
    box-shadow: var(--focus-ring);
  }
}

.photo-gallery__note {
  margin-top: calc(-1 * var(--space-2));
  margin-bottom: var(--space-4);
}

/* A photo picked by clicking it (an entrada's bag, photo_tile_controller.js; a record's photos, shared/_photo_editor):
   the saved or just-picked photo, or a placeholder, with a bin in its corner. The file field sits hidden behind it, still reachable with the keyboard */
.photo-tile {
  position: relative;
  width: 4.5rem;
  height: 4.5rem;
  flex: none;
}

.photo-tile__input {
  position: absolute;
  width: 1px;
  height: 1px;
  opacity: 0;
  pointer-events: none;
}

.photo-tile__pick {
  display: grid;
  place-items: center;
  width: 100%;
  height: 100%;
  overflow: hidden;
  border: 1px dashed var(--color-border-strong, var(--color-border));
  border-radius: var(--radius-md);
  background: var(--color-surface-sunken);
  color: var(--color-text-subtle);
  cursor: pointer;

  &:hover {
    color: var(--color-text-muted);
    background: var(--color-surface-hover);
  }

  .photo-tile--filled & {
    border-style: solid;
  }

  svg {
    width: 1.75rem;
    height: 1.75rem;
  }
}

.photo-tile__input:focus-visible + .photo-tile__pick {
  outline: 2px solid var(--color-focus, var(--color-primary));
  outline-offset: 2px;
}

.photo-tile__image {
  width: 100%;
  height: 100%;
  object-fit: cover;
}

.photo-tile__clear {
  position: absolute;
  top: calc(-1 * var(--space-2));
  right: calc(-1 * var(--space-2));
  display: grid;
  place-items: center;
  width: 1.75rem;
  height: 1.75rem;
  padding: 0;
  border: 1px solid var(--color-border);
  border-radius: 999px;
  background: var(--color-surface);
  color: var(--color-text-muted);
  box-shadow: var(--shadow-sm);
  cursor: pointer;

  &:hover {
    color: var(--color-danger-text);
  }

  svg {
    width: 0.95rem;
    height: 0.95rem;
  }
}

/* A record's photos in its form (shared/_photo_editor): its photo tiles in a row that wraps, the square that adds
   more last. One marked to go is dimmed, saying so, until it's saved (or its bin clicked again) */
.photo-editor {
  min-width: 0;
  margin: 0;
  padding: 0;
  border: 0;
}

.photo-editor__list {
  display: flex;
  flex-wrap: wrap;
  gap: var(--space-3);
  margin: 0;
  /* Room for the bins, which stick out of their tiles' corners */
  padding: var(--space-2) var(--space-2) 0 0;
  list-style: none;
}

.photo-editor__removed {
  display: none;
}

/* Dragged into their order: a finger on one moves it, not the page (touch-action) */
.photo-editor__tile {
  cursor: grab;
  touch-action: none;
  user-select: none;

  &:focus-visible {
    outline: 2px solid var(--color-focus, var(--color-primary));
    outline-offset: 2px;
    border-radius: var(--radius-md);
  }

  .photo-tile__pick {
    cursor: inherit;
  }
}

.photo-editor--dragging {
  z-index: 1;
  cursor: grabbing;
  opacity: 0.85;
  box-shadow: var(--shadow-md);
  transform: scale(1.06);
}

/* The first one is the record's main photo */
.photo-editor__main {
  display: none;
}

/* At its foot: the bin has the top corner */
.photo-editor__list > .photo-editor__tile:first-child .photo-editor__main {
  position: absolute;
  bottom: var(--space-1);
  left: var(--space-1);
  display: block;
  padding: 0 var(--space-1);
  border-radius: var(--radius-sm, 4px);
  background: var(--color-primary);
  color: var(--color-on-primary, #fff);
  font-size: var(--text-xs);
  line-height: 1.5;
}

.photo-editor--removed {
  .photo-tile__image {
    opacity: 0.35;
  }

  .photo-tile__clear {
    color: var(--color-danger-text);
    border-color: var(--color-danger-text);
  }

  .photo-editor__removed {
    position: absolute;
    inset: auto 0 0;
    display: block;
    padding: 2px var(--space-1);
    border-radius: 0 0 var(--radius-md) var(--radius-md);
    background: var(--color-surface);
    color: var(--color-danger-text);
    font-size: var(--text-xs);
    line-height: 1.2;
    text-align: center;
  }
}

/* "(o usa las flechas ← →)": not on a phone, which has no arrow keys */
@media (pointer: coarse) {
  .photo-editor__keys {
    display: none;
  }
}
