/*
  Reveal card.

  The image (front) and body text (back) share one grid cell, so opening the
  card swaps one for the other in place. The cell is as tall as whichever is
  taller, so long body text grows the card rather than overflowing it.

  Styles:
  - Flip (.reveal-card--flip): the whole card turns over. The title stays the
    one toggle button; it counter-rotates halfway through the turn, when the
    card is edge-on, so it always reads the right way round.
  - Accordion (.reveal-card--accordion): the body replaces the image.
  - Lightbox (.reveal-card--lightbox): the card is a fixed square, with the
    image as its background, and the body opens in a modal dialog.

  Themes restyle the card by setting the --reveal-card-* custom properties.
*/
.reveal-card {
  --reveal-card-background: var(--color-white, #fff);
  --reveal-card-border: var(--color-accent, #b1b4b6);
  --reveal-card-title-text: var(--color-text, #0b0c0c);
  --reveal-card-back-background: var(--color-grey-lightest, #f3f2f1);
  --reveal-card-back-text: var(--color-text, #0b0c0c);
  --reveal-card-link-text: var(--color-link, #1d70b8);
  --reveal-card-focus: var(--color-focus, #fd0);
  --reveal-card-flip-duration: 0.5s;
  --reveal-card-border-width: var(--border-width);

  margin-bottom: var(--spacing, 1rem);
  perspective: 60rem;
}

.reveal-card__inner {
  display: flex;
  flex-direction: column;
  height: 100%;
  background: var(--reveal-card-background);
  border: var(--reveal-card-border-width, 1px) solid var(--reveal-card-border);
  border-radius: var(--border-radius, 4px);
}

.reveal-card__title {
  margin: 0;
  padding: var(--spacing, 1rem);
  color: var(--reveal-card-title-text);
  font-size: 1.25rem;
}

.reveal-card--enhanced .reveal-card__title {
  padding: 0;
}

.reveal-card__toggle {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: var(--spacing-small, 0.5rem);
  width: 100%;
  min-height: 44px;
  margin: 0;
  padding: var(--spacing, 1rem);
  color: inherit;
  font: inherit;
  text-align: left;
  background: transparent;
  border: 0;
  border-radius: var(--border-radius, 4px) var(--border-radius, 4px) 0 0;
  cursor: pointer;
}

/* The card is its own surface, so it keeps its own colours inside a coloured
   page section. The base theme colours a section's headings and links, and
   every button on hover and focus, with selectors that would otherwise beat
   the card's own. The focus-visible styles below still win over these. */
.reveal-card .reveal-card__title,
.reveal-card .reveal-card__dialog-title {
  color: var(--reveal-card-title-text);
}

.reveal-card .reveal-card__back :is(h2, h3, h4, h5, h6) {
  color: inherit;
}

.reveal-card .reveal-card__back a:not(:focus) {
  color: var(--reveal-card-link-text);
}

.reveal-card .reveal-card__toggle:is(:hover, :focus),
.reveal-card .reveal-card__close:is(:hover, :focus) {
  color: inherit;
  background-color: transparent;
}

.reveal-card .reveal-card__toggle:is(:hover, :focus) {
  text-decoration: none;
}

/* The whole card works the toggle (see reveal-card.front.js). */
.reveal-card--enhanced .reveal-card__inner {
  cursor: pointer;
}

.reveal-card--enhanced .reveal-card__inner:hover .reveal-card__toggle-text {
  text-decoration: underline;
  text-underline-offset: 0.15em;
}

/* Matches the base theme's link focus: yellow fill, black text, plus a bar so
   it doesn't rely on colour alone. The transparent outline shows in forced
   colours mode. */
.reveal-card .reveal-card__toggle:focus-visible {
  color: var(--color-black, #0b0c0c);
  background-color: var(--reveal-card-focus);
  outline: 3px solid transparent;
  box-shadow: inset 0 -4px 0 var(--color-black, #0b0c0c);
}

.reveal-card__icon {
  flex-shrink: 0;
  font-size: 1.5rem;
  line-height: 1;
}

.reveal-card--accordion .reveal-card__icon::after {
  content: "+";
}

.reveal-card--accordion.reveal-card--open .reveal-card__icon::after {
  content: "\2013";
}

/* A turn-over arrow, flipped to point back once the card is turned over. */
.reveal-card--flip .reveal-card__icon::after {
  content: "\21BB";
}

.reveal-card--flip.reveal-card--open .reveal-card__icon::after {
  content: "\21BA";
}

.reveal-card__faces {
  display: grid;
  flex-grow: 1;
}

.reveal-card__front img {
  display: block;
  width: 100%;
  height: auto;
}

.reveal-card__back {
  padding: var(--spacing, 1rem);
  color: var(--reveal-card-back-text);
  background: var(--reveal-card-back-background);
  border-radius: 0 0 var(--border-radius, 4px) var(--border-radius, 4px);
  overflow-wrap: break-word;
}

.reveal-card__back[hidden] {
  display: none;
}

.reveal-card--enhanced .reveal-card__front,
.reveal-card--enhanced .reveal-card__back {
  grid-area: 1 / 1;
}

.reveal-card__front {
  overflow: hidden;
  border-radius: 0 0 var(--border-radius, 4px) var(--border-radius, 4px);
}

.reveal-card__back > *:last-child,
.reveal-card__back .field > *:last-child {
  margin-bottom: 0;
}

/* Accordion */

/* Hidden but still holding its space, so the card doesn't shrink. */
.reveal-card--accordion.reveal-card--open .reveal-card__front {
  visibility: hidden;
}

/* Flip */

.reveal-card--flip.reveal-card--enhanced .reveal-card__inner,
.reveal-card--flip.reveal-card--enhanced .reveal-card__faces {
  transform-style: preserve-3d;
}

.reveal-card--flip.reveal-card--open .reveal-card__inner {
  transform: rotateY(180deg);
}

.reveal-card--flip.reveal-card--open .reveal-card__title {
  transform: rotateY(180deg);
}

.reveal-card--flip.reveal-card--enhanced .reveal-card__front,
.reveal-card--flip.reveal-card--enhanced .reveal-card__back {
  -webkit-backface-visibility: hidden;
  backface-visibility: hidden;
}

/* Each face gets its own transform, lifting it just off the card. Without
   one, Safari puts the front face in the same plane as the card's background
   and can paint the background over the image. */
.reveal-card--flip.reveal-card--enhanced .reveal-card__front {
  transform: translateZ(1px);
}

.reveal-card--flip.reveal-card--enhanced .reveal-card__back {
  transform: rotateY(180deg) translateZ(1px);
}

.reveal-card--flip.reveal-card--enhanced .reveal-card__faces {
  position: relative;
}

/* Out of the layout while closed, so the closed card is as tall as the
   image, then grows to fit the back when opened. */
.reveal-card--flip.reveal-card--enhanced:not(.reveal-card--open) .reveal-card__back {
  position: absolute;
  inset: 0;
  overflow: hidden;
}

/* Faces turned away are hidden from screen readers and the tab order. */
.reveal-card--flip.reveal-card--enhanced .reveal-card__back,
.reveal-card--flip.reveal-card--open .reveal-card__front {
  visibility: hidden;
}

.reveal-card--flip.reveal-card--open .reveal-card__back {
  visibility: visible;
}

/* Lightbox */

.reveal-card--lightbox .reveal-card__icon::after {
  content: "+";
}

/* Exactly square, title included, whatever the title's length or the
   image's shape. The body text is in the dialog, so nothing else needs
   room. */
.reveal-card--lightbox.reveal-card--enhanced .reveal-card__inner {
  aspect-ratio: 1;
}

.reveal-card--lightbox.reveal-card--enhanced .reveal-card__faces {
  min-height: 0;
}

/* The image is decorative, so it just covers the front like a background. */
.reveal-card--lightbox.reveal-card--enhanced .reveal-card__front {
  position: relative;
}

/* Core makes contextual regions position: relative, which would size the
   image against the media wrapper (0 tall, as the image is out of flow)
   instead of the face, for logged-in users. */
.reveal-card--lightbox.reveal-card--enhanced .reveal-card__front .contextual-region {
  position: static;
}

.reveal-card--lightbox.reveal-card--enhanced .reveal-card__front img {
  position: absolute;
  inset: 0;
  height: 100%;
  object-fit: cover;
}

.reveal-card__dialog {
  width: 100%;
  max-width: min(40rem, calc(100vw - 2 * var(--spacing, 1rem)));
  max-height: calc(100vh - 2 * var(--spacing, 1rem));
  padding: 0;
  overflow-y: auto;
  overscroll-behavior: contain;
  color: var(--reveal-card-back-text);
  background: var(--reveal-card-back-background);
  border: var(--reveal-card-border-width, 1px) solid var(--reveal-card-border);
  border-radius: var(--border-radius, 4px);
}

.reveal-card__dialog::backdrop {
  background: rgb(11 12 12 / 0.7);
}

/* Stop the page scrolling behind an open dialog. */
html:has(.reveal-card__dialog[open]) {
  overflow: hidden;
}

/* Stays in view while long body text scrolls under it, so Close is always
   to hand. */
.reveal-card__dialog-header {
  position: sticky;
  top: 0;
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: var(--spacing, 1rem);
  padding: var(--spacing, 1rem);
  color: var(--reveal-card-title-text);
  background: var(--reveal-card-background);
  border-bottom: var(--reveal-card-border-width, 1px) solid var(--reveal-card-border);
}

.reveal-card__dialog-title {
  margin: 0;
  font-size: 1.5rem;
}

.reveal-card__close {
  flex-shrink: 0;
  min-width: 44px;
  min-height: 44px;
  margin: 0;
  padding: 0 var(--spacing, 1rem);
  color: inherit;
  font: inherit;
  background: transparent;
  border: var(--reveal-card-border-width, 1px) solid currentColor;
  border-radius: var(--border-radius, 4px);
  cursor: pointer;
}

.reveal-card__close:hover {
  text-decoration: underline;
  text-underline-offset: 0.15em;
}

/* Matches the toggle's focus style. */
.reveal-card .reveal-card__close:focus-visible {
  color: var(--color-black, #0b0c0c);
  background-color: var(--reveal-card-focus);
  outline: 3px solid transparent;
  box-shadow: inset 0 -4px 0 var(--color-black, #0b0c0c);
}

.reveal-card__dialog .reveal-card__back {
  border-radius: 0;
}

@media (prefers-reduced-motion: no-preference) {
  .reveal-card--accordion.reveal-card--open .reveal-card__back {
    animation: reveal-card-fade 0.25s ease-out;
  }

  /* reveal-card.front.js sets the height in pixels so it can animate. */
  .reveal-card--flip .reveal-card__inner {
    transition:
      transform var(--reveal-card-flip-duration) ease-in-out,
      height var(--reveal-card-flip-duration) ease-in-out;
  }

  .reveal-card__dialog[open] {
    animation: reveal-card-pop 0.2s ease-out;
  }

  .reveal-card__dialog[open]::backdrop {
    animation: reveal-card-fade 0.2s ease-out;
  }

  /* Swap the title and the faces' visibility at the halfway point, when the
     card is edge-on and neither side can be seen. */
  .reveal-card--flip .reveal-card__title,
  .reveal-card--flip .reveal-card__front,
  .reveal-card--flip .reveal-card__back {
    transition:
      transform 0s linear calc(var(--reveal-card-flip-duration) / 2),
      visibility 0s linear calc(var(--reveal-card-flip-duration) / 2);
  }
}

@keyframes reveal-card-pop {
  from {
    opacity: 0;
    transform: scale(0.96);
  }

  to {
    opacity: 1;
    transform: none;
  }
}

@keyframes reveal-card-fade {
  from {
    opacity: 0;
  }

  to {
    opacity: 1;
  }
}
