@layer components {
  .lightbox {
    --lightbox-duration: 100ms;
    --lightbox-padding: 3vmin;

    background-color: transparent;
    block-size: 100dvh;
    border: 0;
    inline-size: 100dvw;
    inset: 0;
    margin: auto;
    align-items: center;
    justify-content: center;
    max-height: unset;
    max-width: unset;
    overflow: hidden;
    /* To make this work, the application.html.erb layout must include viewport-fit=cover */
    padding: calc(var(--lightbox-padding) + env(safe-area-inset-top, 0px))
      calc(var(--lightbox-padding) + env(safe-area-inset-right, 0px))
      calc(var(--lightbox-padding) + env(safe-area-inset-bottom, 0px))
      calc(var(--lightbox-padding) + env(safe-area-inset-left, 0px));
    text-align: center;

    &::backdrop {
      -webkit-backdrop-filter: blur(16px);
      backdrop-filter: blur(16px);
      /* Uses standard rgb fallback or color-mix based on your variables */
      background-color: rgb(0 0 0 / 0.5); /* dark in both themes, as is a backdrop: no color of the palette */
    }

    /* Closed state */
    &,
    &::backdrop {
      opacity: 0;
      transition: var(--lightbox-duration) allow-discrete;
      transition-property: display, opacity, overlay;
    }

    /* Open state */
    &:popover-open,
    &:popover-open::backdrop,
    &[open],
    &[open]::backdrop {
      display: flex;
      opacity: 1;

      @starting-style {
        opacity: 0;
      }
    }
  }

  /* Card */
  .lightbox__panel {
    /* Size */
    width: min(90vw, 1200px);
    max-height: calc(100dvh - var(--space-4) * 2);
    transform: scale(0.95);
    transition: transform var(--lightbox-duration) cubic-bezier(0.16, 1, 0.3, 1);
    overflow: hidden;
  }

  /* Animation */
  .lightbox:popover-open .lightbox__panel {
    transform: scale(1);

    @starting-style {
      transform: scale(0.95);
    }
  }

  .lightbox__image {
    flex: 1;
    min-block-size: 0;
    /* Prevents flexbox auto-sizing from blowing out of bounds */
  }

  /* Drawer (drawer: true): a panel at the right edge, the whole height, that slides in. Only as wide as the panel, with
     no backdrop, so the page next to it stays usable. --drawer-width is on :root, so the page can make room for it.
     The only one is a citation's source on Write (write.css), beside the text, not over it (Niklas, 30.11.): with the
     sidebar collapsed meanwhile (sidebar controller, about 4rem then), the text keeps about 35 %, at least 26rem, the
     PDF and the form the rest; in a window too narrow for the three, the whole width. */
  :root {
    --drawer-width: min(62vw, calc(100vw - 30rem));

    @media (max-width: 80rem) {
      --drawer-width: 100vw;
    }
  }

  .lightbox--drawer {
    --lightbox-duration: 250ms;

    inset: 0 0 0 auto;
    inline-size: var(--drawer-width);
    padding: 0;
    align-items: stretch;
    justify-content: stretch;

    /* The page next to it stays as it is */
    &::backdrop,
    &:popover-open::backdrop {
      background: none;
      -webkit-backdrop-filter: none;
      backdrop-filter: none;
    }

    .lightbox__panel {
      width: 100%;
      max-height: none;
      border-radius: 0;
      border-inline-start: var(--border);
      transform: translateX(100%);
    }

    &:popover-open .lightbox__panel {
      transform: none;

      @starting-style {
        transform: translateX(100%);
      }
    }

    /* Its content fills it, e.g. a PDF viewer in a Turbo Frame */
    .lightbox__body {
      display: flex;
      flex-direction: column;
      min-block-size: 0;

      > * {
        flex: 1;
        min-block-size: 0;
      }
    }
  }

  /* Prevent body from scrolling when lightbox is open */
  html:has(.lightbox:popover-open),
  html:has(.lightbox[open]) {
    overflow: clip;
  }
}
