am-gallery { display: block; margin-top: 1rem; }
.am-gallery-topbar { margin-bottom: 1rem; }
.am-gallery-controls { display: flex; flex-wrap: wrap; gap: 0.5rem; }
.am-gallery-controls[role="toolbar"] {
  align-items: center;
}
.am-sort-btn {
  border: 1px solid var(--line);
  background: var(--surface-soft);
  color: var(--muted);
  border-radius: 999px;
  padding: 0.45rem 0.82rem;
  font-size: 0.87rem;
  cursor: pointer;
}
.am-sort-btn.active,
.am-sort-btn:hover { border-color: var(--primary); color: var(--text); }

.am-gallery-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(240px, 1fr));
  gap: 1rem;
}
.am-gallery-card {
  border: 1px solid var(--line);
  border-radius: 16px;
  overflow: hidden;
  background: var(--surface);
  color: var(--text);
  cursor: pointer;
  transition: transform 180ms ease, box-shadow 180ms ease;
  box-shadow: 0 10px 24px var(--shadow);
}
.am-gallery-card:hover {
  transform: translateY(-3px) scale(1.02);
  box-shadow: 0 18px 34px var(--shadow);
}
.am-gallery-image-wrap { aspect-ratio: 1/1; background: var(--surface-soft); overflow: hidden; }
.am-gallery-image-wrap img {
  width: 100%;
  height: 100%;
  object-fit: cover;
  display: block;
  filter: blur(10px);
  opacity: 0.9;
  transition: filter 200ms ease, opacity 200ms ease;
}
.am-gallery-image-wrap > [hidden] {
  display: none !important;
}
.am-gallery-image-wrap img.is-visible { filter: blur(0); opacity: 1; }
.am-gallery-image-wrap img.is-fallback {
  filter: none;
  opacity: 0.45;
  object-fit: contain;
  padding: 1.25rem;
}
.am-gallery-card:focus-visible {
  outline: 2px solid var(--warm);
  outline-offset: 2px;
}

.am-gallery-card[aria-current="true"] {
  border-color: var(--primary);
  box-shadow: 0 0 0 2px color-mix(in srgb, var(--primary) 24%, transparent), 0 18px 34px var(--shadow);
}

.am-gallery-caption { padding: 0.72rem 0.74rem 0.8rem; display: grid; gap: 0.12rem; }
.am-gallery-caption strong {
  font-size: 0.95rem;
  font-weight: 700;
  line-height: 1.25;
  overflow: hidden;
  text-overflow: ellipsis;
  display: -webkit-box;
  -webkit-box-orient: vertical;
  -webkit-line-clamp: 2;
  overflow-wrap: anywhere;
}
.am-gallery-caption span { color: var(--muted); font-size: 0.82rem; }
.am-gallery-caption span,
.am-gallery-caption time {
  overflow-wrap: anywhere;
}
.am-gallery-caption time { color: var(--muted); font-size: 0.75rem; }

.gallery-more-wrap { margin-top: 1rem; }
.gallery-more-wrap .btn {
  min-width: min(100%, 16rem);
}
.gallery-summary {
  overflow-wrap: anywhere;
}
.gallery-summary-secondary {
  margin-top: -0.35rem;
  font-size: 0.94rem;
}
.gallery-summary-secondary.is-warning { color: var(--warm); }

.am-gallery-modal { position: fixed; inset: 0; display: none; z-index: 55; }
.am-gallery-modal.is-open { display: block; }
.am-gallery-modal-overlay { position: absolute; inset: 0; background: var(--modal-overlay); backdrop-filter: blur(2px); }
.am-gallery-modal-content {
  position: relative;
  z-index: 1;
  width: min(1240px, calc(100% - 1rem));
  margin: max(1.5vh, env(safe-area-inset-top, 0px)) auto max(1.5vh, env(safe-area-inset-bottom, 0px));
  border-radius: 16px;
  overflow: hidden;
  background: var(--surface);
  color: var(--text);
  display: grid;
  grid-template-columns: 1fr;
  min-height: min(92vh, 92svh);
  max-height: min(92vh, 92svh);
  border: 1px solid var(--line);
}
.am-modal-main {
  background: var(--surface-soft);
  min-height: 260px;
  position: relative;
  display: grid;
  place-items: center;
  padding: 1rem;
}
.am-modal-main > img { width: 100%; max-height: min(72vh, 720px); object-fit: contain; display: block; }
.am-modal-loading-indicator {
  position: absolute;
  left: 50%;
  bottom: 1.1rem;
  transform: translateX(-50%);
  z-index: 2;
  padding: 0.5rem 0.8rem;
  border-radius: 999px;
  background: var(--surface);
  border: 1px solid var(--line);
  color: var(--text);
  font-size: 0.82rem;
  font-weight: 600;
  backdrop-filter: blur(2px);
}
.am-modal-image-fallback {
  display: grid;
  place-items: center;
  width: min(100%, 640px);
  min-height: min(48vh, 420px);
  padding: 1.5rem;
  border: 1px dashed var(--line);
  border-radius: 14px;
  background: var(--panel-bg);
  color: var(--muted);
  text-align: center;
}
.am-modal-info {
  --am-modal-actions-space: 0px;
  padding: 1.1rem;
  display: grid;
  align-content: start;
  gap: 0.62rem;
  border-left: 1px solid var(--line);
  background: var(--surface);
  overflow-y: auto;
}
.am-modal-info p,
.am-modal-info span,
.am-modal-info a {
  overflow-wrap: anywhere;
}
.am-modal-tag-group {
  display: grid;
  gap: 0.38rem;
}
.am-modal-tags {
  display: flex;
  flex-wrap: wrap;
  gap: 0.4rem;
  min-height: 1.5rem;
}
.am-tag-pill {
  display: inline-flex;
  align-items: center;
  max-width: 100%;
  min-height: 1.9rem;
  padding: 0.28rem 0.62rem;
  border-radius: 999px;
  background: var(--accent-soft);
  border: 1px solid var(--line);
  color: var(--text);
  font-size: 0.82rem;
  line-height: 1.2;
  overflow-wrap: anywhere;
}
.am-tag-pill-muted {
  background: var(--surface-soft);
  color: var(--muted);
}
.am-modal-usage {
  color: var(--muted);
  font-size: 0.9rem;
  line-height: 1.45;
}
.am-modal-mode {
  display: inline-flex;
  align-items: center;
  width: fit-content;
  max-width: 100%;
  min-height: 2rem;
  padding: 0.28rem 0.7rem;
  border-radius: 999px;
  background: var(--accent-soft);
  border: 1px solid color-mix(in srgb, var(--primary) 24%, transparent);
  color: var(--text);
  font-size: 0.82rem;
  font-weight: 700;
  line-height: 1.25;
  overflow-wrap: anywhere;
}
.am-modal-status {
  min-height: 1.35rem;
  color: var(--text);
  font-size: 0.88rem;
  font-weight: 600;
  overflow-wrap: anywhere;
}
.am-modal-close {
  position: absolute;
  right: 0.6rem;
  top: 0.2rem;
  min-width: 44px;
  min-height: 44px;
  font-size: 2rem;
  color: var(--text);
  background: none;
  border: 0;
  cursor: pointer;
  z-index: 2;
}
.am-modal-close:focus-visible,
.am-modal-nav:focus-visible,
.am-modal-actions a:focus-visible,
.am-modal-actions button:focus-visible,
.am-sort-btn:focus-visible {
  outline: 2px solid var(--warm);
  outline-offset: 3px;
}
.am-modal-nav {
  position: absolute;
  top: 50%;
  transform: translateY(-50%);
  border: 1px solid var(--line);
  width: 40px;
  height: 56px;
  background: var(--surface);
  color: var(--text);
  cursor: pointer;
  z-index: 2;
}
.am-modal-nav[disabled],
.am-modal-nav[aria-disabled="true"] {
  opacity: 0.4;
  cursor: not-allowed;
}
.am-modal-nav.prev { left: 0; border-radius: 0 12px 12px 0; }
.am-modal-nav.next { right: 0; border-radius: 12px 0 0 12px; }
.am-modal-actions { display: flex; flex-wrap: wrap; gap: 0.55rem; }
.am-modal-actions a,
.am-modal-actions button {
  border: 1px solid var(--line);
  background: var(--surface-soft);
  color: var(--text);
  text-decoration: none;
  border-radius: 10px;
  min-height: 44px;
  padding: 0.6rem 0.8rem;
  cursor: pointer;
  overflow-wrap: anywhere;
}
.am-modal-actions a[aria-disabled="true"] {
  opacity: 0.56;
  cursor: not-allowed;
  pointer-events: none;
}
.am-modal-actions [hidden] {
  display: none !important;
}
.am-sort-btn[aria-pressed="true"] {
  border-color: var(--primary);
  color: var(--text);
}

.am-before-after {
  --split: 50%;
  position: absolute;
  inset: 1rem;
  overflow: hidden;
  background: var(--surface-soft);
  border-radius: 12px;
}
.am-before-after img {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  object-fit: contain;
}
.am-before-after-after {
  position: absolute;
  inset: 0;
  width: var(--split);
  overflow: hidden;
}
.am-before-after-range {
  position: absolute;
  left: 0;
  right: 0;
  bottom: 0.9rem;
  width: calc(100% - 2rem);
  margin: 0 auto;
  z-index: 2;
}
.am-before-after-divider {
  position: absolute;
  top: 0;
  bottom: 0;
  width: 2px;
  background: var(--primary);
  box-shadow: 0 0 0 2px var(--shadow);
  z-index: 2;
}

@media (min-width: 920px) {
  .am-gallery-modal-content { grid-template-columns: 1.3fr 0.7fr; }
}

@media (max-width: 919px) {
  .am-gallery-modal-content { width: min(100%, calc(100% - 0.5rem)); }
  .am-modal-info {
    --am-modal-actions-space: 5.8rem;
    border-left: 0;
    border-top: 1px solid var(--line);
    padding-bottom: calc(var(--am-modal-actions-space) + env(safe-area-inset-bottom, 0px));
  }
  .am-modal-main { min-height: 220px; padding: 0.75rem; }
  .am-modal-main > img { max-height: 52vh; }
  .am-modal-image-fallback {
    width: 100%;
    min-height: 240px;
  }
  .am-gallery-grid {
    grid-template-columns: repeat(auto-fit, minmax(180px, 1fr));
    gap: 0.75rem;
  }
  .am-modal-actions > * {
    flex: 1 1 100%;
    justify-content: center;
  }
  .am-tag-pill {
    width: 100%;
    justify-content: center;
  }
  .am-modal-actions {
    position: sticky;
    bottom: -1.1rem;
    margin: 0 -1.1rem -1.1rem;
    padding: 0.9rem 1.1rem calc(0.9rem + env(safe-area-inset-bottom, 0px));
    background: var(--panel-bg);
    border-top: 1px solid var(--line);
  }
  .am-modal-nav {
    top: auto;
    bottom: 0.75rem;
    transform: none;
    width: 52px;
    height: 52px;
  }
  .am-modal-nav.prev { left: 0.75rem; border-radius: 12px; }
  .am-modal-nav.next { right: 0.75rem; border-radius: 12px; }
}

@media (max-height: 760px) and (orientation: landscape) {
  .am-gallery-modal-content {
    width: min(100%, calc(100% - 0.35rem));
    min-height: min(96vh, 96svh);
    max-height: min(96vh, 96svh);
    margin: 0.5vh auto;
  }

  .am-modal-main {
    min-height: 180px;
    padding: 0.65rem;
  }

  .am-modal-main > img {
    max-height: 54vh;
  }

  .am-modal-info {
    --am-modal-actions-space: 5.3rem;
    padding: 0.9rem;
    padding-bottom: calc(var(--am-modal-actions-space) + env(safe-area-inset-bottom, 0px));
  }

  .am-modal-actions {
    bottom: -0.9rem;
    margin: 0 -0.9rem -0.9rem;
    padding: 0.75rem 0.9rem calc(0.75rem + env(safe-area-inset-bottom, 0px));
  }

  .am-modal-nav {
    bottom: 0.55rem;
    width: 48px;
    height: 48px;
  }

  .am-before-after {
    inset: 0.65rem;
  }
}

.am-gallery-image-placeholder {
  display: flex;
  align-items: center;
  justify-content: center;
  width: 100%;
  min-height: 100%;
  padding: 1rem;
  text-align: center;
  color: var(--muted);
  background: var(--panel-bg);
}

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

@media (prefers-reduced-motion: reduce) {
  .am-gallery-card,
  .am-gallery-image-wrap img,
  .am-sort-btn {
    transition: none;
  }

  .am-gallery-card:hover {
    transform: none;
  }
}

@media (hover: none), (pointer: coarse) {
  .am-gallery-card:hover {
    transform: none;
    box-shadow: 0 10px 24px rgba(0, 0, 0, 0.2);
  }
}

