/* Gallery-specific styles: loaded only on gallery page */

/* Grid of thumbnails */
#gallery-grid-view.gallery-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(160px, 1fr));
  gap: 2rem;
  justify-items: center;
  align-items: start;
  margin: 2em 0;
}

/* Controls bar */
#gallery-controls.gallery-controls-bar {
  display: none; /* hidden by default, toggled via button */
  align-items: center;
  justify-content: flex-end; /* right-align under the icon */
  gap: 0.5rem;
  margin: 0.5rem 0 0;
}
#gallery-controls .controls-row {
  display: flex;
  align-items: center;
  gap: 0.5rem;
}
#gallery-controls .controls-row select {
  padding: 0.35rem 0.5rem;
  border-radius: 4px;
  border: 1px solid #ccc;
}

/* Pagination */
#gallery-pagination {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 0.75rem;
  margin: 0.75rem 0 0.5rem;
}
#gallery .gallery-toolbar {
  display: flex;
  justify-content: flex-end;
}
#gallery .gallery-toolbar .icon-button {
  background: rgba(255, 255, 255, 0.9);
  color: #111;
  border: 1px solid rgba(0, 0, 0, 0.15);
  border-radius: 999px;
  width: 36px;
  height: 36px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.15);
}
#gallery .gallery-toolbar .icon-button:hover {
  background: #fff;
}
#gallery .gallery-toolbar .icon-button:focus-visible {
  outline: 2px solid rgba(0, 0, 0, 0.25);
  outline-offset: 2px;
}

/* When controls are toggled open */
.gallery-controls-open #gallery-controls.gallery-controls-bar {
  display: flex;
}
#gallery-pagination .page-status {
  min-width: 8ch;
  text-align: center;
}

#gallery-grid-view .small-thumb {
  max-width: 150px;
  max-height: 225px;
  width: 100%;
  height: auto;
  margin: 0 auto;
  display: block;
  border-radius: 6px;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.08);
}

/* Detail view presentation */
#gallery-detail-view .content {
  display: block;
}

#gallery-detail-view .detail-media {
  position: static;
  display: inline-block;
  /* Control dimensions/spacing */
  --gc-size: 40px;
  --gc-gap: 12px;
}

/* Two-column layout for detail view when space allows */
.detail-flex {
  display: grid;
  grid-template-columns: 320px 1fr;
  gap: 2rem;
  align-items: start;
}
.detail-meta {
  max-width: 480px;
}
.detail-meta h2 {
  margin-bottom: 0.25em;
}
.detail-meta p {
  margin-top: 0.25em;
}
.detail-order {
  margin-top: 0.75em;
}
/* Description and actions in meta column */
.detail-description {
  margin-top: 0.5rem;
  color: #444;
}
.detail-actions {
  margin-top: 0.75rem;
  display: flex;
  gap: 0.5rem;
  flex-wrap: wrap;
}
.button.primary {
  background: #111;
  color: #fff;
  border-color: #111;
}

/* Center image area on smaller screens by stacking */
@media (max-width: 900px) {
  .detail-flex {
    display: flex;
    flex-direction: column;
    align-items: flex-start;
    gap: 1rem;
  }
  #detail-media {
    order: 1;
  }
  .detail-meta {
    width: 100%;
    max-width: 640px;
    order: 2;
  }
}

#gallery-detail-view img#detail-image {
  width: auto;
  max-width: 100%;
  height: auto;
  max-height: 80vh; /* Stage 1: bounded by viewport height */
  border-radius: 6px;
  box-shadow: 0 2px 12px rgba(0, 0, 0, 0.12);
}

/* Show zoom-in cursor only when a larger image is available */
body.gallery-can-zoom #gallery-detail-view img#detail-image {
  cursor: zoom-in;
}
body.gallery-no-zoom #gallery-detail-view img#detail-image {
  cursor: default;
}

/* When detail is open, hide sidebar and the Gallery heading */
body.gallery-detail-open #sidebar {
  display: none;
}
body.gallery-detail-open #gallery > header {
  display: none;
}

/* Lock background scroll when detail overlay is open */
body.gallery-lock-scroll {
  overflow: hidden;
}

/* Present the detail as a fixed overlay so there's no page scroll flash */
body.gallery-detail-open #gallery-detail-view {
  position: fixed;
  /* Expose control sizing at overlay scope so other descendants (e.g., content padding) can reference them */
  --gc-size: 40px;
  --gc-gap: 12px;
  left: 8px; /* inset so border is visible */
  right: 8px;
  /* Sit just under the fixed header; remove extra gap to reduce perceived jump */
  top: calc(var(--detail-top, 3.5em));
  bottom: 8px;
  /* Avoid horizontal scrollbars; reserve gutter so vertical scrollbar doesn't overlay controls */
  overflow-x: hidden;
  overflow-y: auto;
  scrollbar-gutter: stable both-edges;
  z-index: 10001; /* under the menu (10002), above page content */
  background: #fff;
  border: 1px solid #ddd; /* subtle border to distinguish detail page */
  box-shadow: 0 2px 12px rgba(0, 0, 0, 0.06);
  box-sizing: border-box;
}

/* Ensure the overlay isn't constrained by base `.post` width rules at narrow breakpoints */
body.gallery-detail-open #gallery-detail-view.post {
  width: auto !important;
  max-width: none !important;
  /* Reduce internal top padding so content sits closer to the top */
  padding-top: 0.75em;
}

@media (max-width: 900px) {
  body.gallery-detail-open #gallery-detail-view.post {
    padding-top: 0.5em;
  }
}

/* Hide only overlay content (keep white background visible) during initial alignment */
#gallery-detail-view.detail-content-hidden > * {
  opacity: 0;
}

/* Zoomed state: allow viewing at larger size (may require scrolling) */
body.gallery-zoomed #gallery-detail-view img#detail-image {
  max-width: 100%; /* width will be driven by sizes/srcset (up to 1600px) */
  max-height: none; /* allow full height -> page may scroll */
  cursor: zoom-out;
}

/* Full Size button is always visible */
#detail-fullsize {
  display: inline-block;
}

/* Bottom bar with Back and Full size aligned */
.detail-bottom-bar {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  margin-top: 1rem;
}
.detail-bottom-bar {
  /* Align with the left edge of the image column (320px meta + 2rem gap) */
  margin-left: calc(320px + 2rem);
}
.detail-bottom-bar {
  /* Align with image column when two-column layout is active */
  margin-left: calc(320px + 2rem);
}
.detail-bottom-bar #back-to-grid {
  order: 0;
}
/* Place Full Size directly next to Back button */

/* Overlay controls (Close, Prev, Next) anchored to overlay edges */
.detail-media .gallery-controls {
  position: absolute;
  left: 0;
  right: 0;
  top: 0;
  bottom: 0;
  pointer-events: none; /* only buttons capture */
}
.detail-media .gallery-control {
  pointer-events: auto;
  position: absolute;
  background: rgba(255, 255, 255, 0.9);
  color: #111;
  border: 1px solid rgba(0, 0, 0, 0.15);
  border-radius: 999px;
  width: var(--gc-size);
  height: var(--gc-size);
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.15);
  transition: background 0.15s ease, box-shadow 0.15s ease, transform 0.1s ease;
}
.detail-media .gallery-control:hover {
  background: #fff;
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.2);
  transform: scale(1.03);
}
/* Only show the ring when keyboard-focused */
.detail-media .gallery-control:focus {
  outline: none;
}
.detail-media .gallery-control:focus-visible {
  outline: 2px solid rgba(0, 0, 0, 0.25);
  outline-offset: 2px;
}

/* Positioning at overlay edges */
#detail-close {
  top: calc(var(--gc-gap) + 16px);
  right: calc(var(--gc-gap) + 16px);
  left: auto;
}
#detail-prev {
  left: var(--gc-gap);
  top: 50%;
  transform: translateY(-50%);
}
#detail-next {
  right: var(--gc-gap);
  top: 50%;
  transform: translateY(-50%);
}

/* Ensure controls are visible only in detail mode */
.detail-media .gallery-controls {
  display: none;
}
body.gallery-detail-open .detail-media .gallery-controls {
  display: block;
}

/* Small screens: slightly smaller controls and tighter gap (still outside) */
@media (max-width: 520px) {
  #gallery-detail-view .detail-media {
    --gc-size: 36px;
    --gc-gap: 8px;
  }
}

/* On stacked layout, keep art left-aligned but reserve a left gutter so the Prev arrow doesn’t cover it
   (only when there’s room; allow overlap on very small screens <=520px). */
@media (min-width: 521px) and (max-width: 900px) {
  #gallery-detail-view .content {
    padding-left: calc(var(--gc-gap) + var(--gc-size) + 8px);
  }
}

@media (max-width: 900px) {
  .detail-bottom-bar {
    margin-left: 0;
  }
}

@media (max-width: 900px) {
  .detail-bottom-bar {
    margin-left: 0;
  }
}
