/* ML Meanders - Figures & Images */

/* ============================================
   Base Figure Styling
   ============================================ */
figure {
  margin: var(--space-xl) 0;
  padding: 0;
}

figure img {
  width: 100%;
  height: auto;
  border-radius: var(--radius-md);
}

figcaption {
  margin-top: var(--space-sm);
  font-size: var(--font-size-sm);
  color: var(--color-text-muted);
  line-height: var(--line-height-normal);
  text-align: center;
}

figcaption strong {
  color: var(--color-text);
}

/* Figure number */
figcaption::before {
  content: 'Figure ' counter(figure) '. ';
  font-weight: var(--font-weight-semibold);
  color: var(--color-accent);
}

.post-body {
  counter-reset: figure sidenote-counter;
}

figure {
  counter-increment: figure;
}

/* ============================================
   Figure Width Classes
   ============================================ */

/* Standard width (body) */
figure.l-body {
  max-width: var(--width-body);
  margin-left: auto;
  margin-right: auto;
}

/* Medium width */
figure.l-middle {
  max-width: var(--width-middle);
  margin-left: auto;
  margin-right: auto;
}

/* Full page width */
figure.l-page {
  max-width: var(--width-page);
  margin-left: auto;
  margin-right: auto;
}

/* Full screen width */
figure.l-screen {
  width: 100vw;
  max-width: none;
  margin-left: calc(-50vw + 50%);
  margin-right: calc(-50vw + 50%);
  border-radius: 0;
}

figure.l-screen img {
  border-radius: 0;
}

figure.l-screen figcaption {
  max-width: var(--width-body);
  margin-left: auto;
  margin-right: auto;
  padding: 0 var(--gutter);
}

/* ============================================
   Multi-Panel Figures
   ============================================ */
.figure-row {
  display: grid;
  gap: var(--space-md);
}

.figure-row.cols-2 {
  grid-template-columns: repeat(2, 1fr);
}

.figure-row.cols-3 {
  grid-template-columns: repeat(3, 1fr);
}

.figure-row.cols-4 {
  grid-template-columns: repeat(4, 1fr);
}

@media (max-width: 768px) {
  .figure-row.cols-3,
  .figure-row.cols-4 {
    grid-template-columns: repeat(2, 1fr);
  }
}

@media (max-width: 480px) {
  .figure-row {
    grid-template-columns: 1fr !important;
  }
}

/* Sub-figure labels */
.figure-row figure {
  counter-increment: none;
  margin: 0;
}

.figure-row figure figcaption::before {
  content: '(' counter(subfigure, lower-alpha) ') ';
  counter-increment: subfigure;
}

.figure-row {
  counter-reset: subfigure;
}

/* ============================================
   Image Lightbox (optional expansion)
   ============================================ */
figure.expandable img {
  cursor: zoom-in;
  transition: transform var(--transition-base);
}

figure.expandable img:hover {
  transform: scale(1.02);
}

/* Lightbox overlay */
.lightbox {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background-color: rgba(0, 0, 0, 0.9);
  z-index: var(--z-modal);
  display: flex;
  align-items: center;
  justify-content: center;
  padding: var(--space-xl);
  cursor: zoom-out;
  opacity: 0;
  pointer-events: none;
  transition: opacity var(--transition-base);
}

.lightbox.active {
  opacity: 1;
  pointer-events: auto;
}

.lightbox img {
  max-width: 100%;
  max-height: 100%;
  object-fit: contain;
  border-radius: var(--radius-md);
}

.lightbox-close {
  position: absolute;
  top: var(--space-lg);
  right: var(--space-lg);
  width: 40px;
  height: 40px;
  border: none;
  background: rgba(255, 255, 255, 0.1);
  color: white;
  border-radius: 50%;
  cursor: pointer;
  font-size: var(--font-size-xl);
  display: flex;
  align-items: center;
  justify-content: center;
  transition: background-color var(--transition-fast);
}

.lightbox-close:hover {
  background: rgba(255, 255, 255, 0.2);
}

/* ============================================
   Image Attribution
   ============================================ */
figure .attribution {
  display: block;
  font-size: var(--font-size-xs);
  color: var(--color-text-faint);
  margin-top: var(--space-xs);
}

figure .attribution a {
  color: var(--color-text-muted);
}

/* ============================================
   Diagrams and SVGs
   ============================================ */
figure svg {
  width: 100%;
  height: auto;
}

/* Dark mode SVG adjustment */
[data-theme="dark"] figure svg {
  filter: invert(0.9) hue-rotate(180deg);
}

/* Don't invert already-dark SVGs */
[data-theme="dark"] figure.no-invert svg {
  filter: none;
}

/* ============================================
   Video Embeds
   ============================================ */
figure.video {
  position: relative;
  padding-bottom: 56.25%; /* 16:9 aspect ratio */
  height: 0;
  overflow: hidden;
}

figure.video iframe,
figure.video video {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  border: none;
  border-radius: var(--radius-md);
}

/* ============================================
   Image Loading States
   ============================================ */
figure img {
  background-color: var(--color-bg-subtle);
}

figure img[loading="lazy"] {
  opacity: 0;
  transition: opacity var(--transition-slow);
}

figure img[loading="lazy"].loaded {
  opacity: 1;
}
