/**
 * @file
 * Styling for the Modal Popup dialog.
 *
 * Written against nothing: no CSS framework, no grid system, no utility
 * classes, no external assets. Drop the module on any theme and the dialog
 * looks finished. Everything is laid out with flexbox and logical
 * properties, so it follows the text direction without a separate RTL
 * stylesheet.
 *
 * Every value a theme is likely to want to change is a custom property on
 * `.modal-popup`, so restyling never needs a single overridden rule:
 *
 * @code
 * .modal-popup {
 *   --modal-popup-surface: #fff;
 *   --modal-popup-radius: 1rem;
 *   --modal-popup-max-inline-size: 60rem;
 *   --modal-popup-accent: rebeccapurple;
 * }
 * @endcode
 */

.modal-popup {
  /* Surface. */
  --modal-popup-backdrop: rgb(26, 28, 30, 0.7);
  --modal-popup-surface: #f7f7f7;
  --modal-popup-text: #1a1c1e;
  --modal-popup-accent: #4a4d50;
  --modal-popup-shadow: 0 1.5rem 4rem rgb(0, 0, 0, 0.25);

  /* Size. Wide by default - a popup usually holds a whole view mode, not a
     paragraph of text - but capped so it does not sprawl on a wide screen. */
  --modal-popup-inline-size: 88%;
  --modal-popup-max-inline-size: 84rem;
  --modal-popup-max-block-size: 90vh;
  --modal-popup-min-block-size: 18.75rem;
  --modal-popup-gutter: 1.25rem;
  --modal-popup-padding: 50px;

  /* Shape. */
  --modal-popup-radius: 2.5rem;
  --modal-popup-radius-sheet: 1.25rem;
  --modal-popup-control-inset: 2rem;
  --modal-popup-control-inset-sheet: 1.25rem;
  --modal-popup-control-size: 2rem;

  /* Motion. */
  --modal-popup-duration: 260ms;
  --modal-popup-easing: cubic-bezier(0.2, 0.7, 0.3, 1);

  overflow: hidden;
  margin: 0;
  padding: 0;
  color: var(--modal-popup-text);
  border: 0;
  background: transparent;

  /* A native <dialog> paints in the top layer, so no z-index on the page can
     cover it and no ancestor's overflow can clip it. */
  max-inline-size: none;
  max-block-size: none;
  inline-size: 100%;
  block-size: 100%;
}

.modal-popup:not([open]) {
  display: none;
}

/* The backdrop is the browser's own pseudo-element. */
.modal-popup::backdrop {
  animation: modal-popup-backdrop-in var(--modal-popup-duration) ease both;
  background: var(--modal-popup-backdrop);
}

.modal-popup:focus,
.modal-popup:focus-visible {
  outline: none;
}

/*
 * Centring layer. Flexbox rather than absolute positioning, so the dialog
 * can size itself to its content and still sit in the middle.
 */
.modal-popup__viewport {
  display: flex;
  align-items: center;
  justify-content: center;
  block-size: 100%;
  padding: var(--modal-popup-gutter);
}

/*
 * The dialog. It does not scroll itself - the content region below does -
 * so the close button stays reachable however long the content is.
 */
.modal-popup__panel {
  position: relative;
  display: flex;

  /* Hidden until the content lands, so an empty box never flashes up.
     `translate` and `scale` are used instead of `transform` so that a theme
     is free to use `transform` for its own purposes without breaking the
     entrance. */
  visibility: hidden;
  overflow: hidden;
  flex-direction: column;
  transition:
    visibility 0s linear var(--modal-popup-duration),
    opacity var(--modal-popup-duration) ease,
    translate var(--modal-popup-duration) var(--modal-popup-easing),
    scale var(--modal-popup-duration) var(--modal-popup-easing);
  opacity: 0;
  color: var(--modal-popup-text);
  border-radius: var(--modal-popup-radius);
  outline: none;
  background: var(--modal-popup-surface);
  box-shadow: var(--modal-popup-shadow);
  inline-size: var(--modal-popup-inline-size);
  max-inline-size: var(--modal-popup-max-inline-size);
  max-block-size: var(--modal-popup-max-block-size);
  translate: 0 1.5rem;
  scale: 0.98;
}

/* Held back until the content has landed, so an empty frame never flashes. */
.modal-popup.is-ready .modal-popup__panel {
  visibility: visible;
  transition-delay: 0s;
  opacity: 1;
  translate: 0;
  scale: 1;
}

.modal-popup__panel:focus-visible {
  outline: 2px solid var(--modal-popup-accent);
  outline-offset: -4px;
}

/* Content region: the only part that scrolls. */
.modal-popup__content {
  overflow: auto;
  flex: 1 1 auto;
  padding: var(--modal-popup-padding);
  min-block-size: 0;
  overscroll-behavior: contain;

  /* The dialog has its own generous padding around the content; hiding the
     scrollbar keeps that padding looking even. */
  scrollbar-width: none;
  -ms-overflow-style: none;
}

.modal-popup__content::-webkit-scrollbar {
  display: none;
}

.modal-popup__content:not(.is-loading) {
  animation: modal-popup-fade-in 240ms ease both;
}

/* Close button. */
.modal-popup__controls {
  position: absolute;
  z-index: 2;
  inset-block-start: var(--modal-popup-control-inset);
  inset-inline-end: var(--modal-popup-control-inset);
}

.modal-popup__close {
  display: block;
  padding: 0;
  cursor: pointer;
  transition:
    opacity 200ms ease,
    scale 200ms ease;
  color: inherit;
  border: 0;
  border-radius: 50%;
  background: transparent;
  font-size: 0;
  inline-size: var(--modal-popup-control-size);
  block-size: var(--modal-popup-control-size);
}

/*
 * The icon is a mask rather than a background image, so it takes its colour
 * from `currentColor` and stays visible on a dark surface too.
 */
.modal-popup__close::before {
  display: block;
  content: "";
  opacity: 0.65;
  background: currentColor;
  inline-size: 100%;
  block-size: 100%;
  -webkit-mask: var(--modal-popup-close-icon) no-repeat center / 100% 100%;
  mask: var(--modal-popup-close-icon) no-repeat center / 100% 100%;
}

.modal-popup {
  --modal-popup-close-icon: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 32 32'%3E%3Cpath d='M16 3C13.4288 3 10.9154 3.76244 8.77759 5.1909C6.63975 6.61935 4.97351 8.64968 3.98957 11.0251C3.00563 13.4006 2.74819 16.0144 3.2498 18.5362C3.75141 21.0579 4.98953 23.3743 6.80762 25.1924C8.6257 27.0105 10.9421 28.2486 13.4638 28.7502C15.9856 29.2518 18.5995 28.9944 20.9749 28.0104C23.3503 27.0265 25.3807 25.3603 26.8091 23.2224C28.2376 21.0846 29 18.5712 29 16C28.9964 12.5533 27.6256 9.24882 25.1884 6.81163C22.7512 4.37445 19.4467 3.00364 16 3ZM20.7075 19.2925C20.8004 19.3854 20.8741 19.4957 20.9244 19.6171C20.9747 19.7385 21.0006 19.8686 21.0006 20C21.0006 20.1314 20.9747 20.2615 20.9244 20.3829C20.8741 20.5043 20.8004 20.6146 20.7075 20.7075C20.6146 20.8004 20.5043 20.8741 20.3829 20.9244C20.2615 20.9747 20.1314 21.0006 20 21.0006C19.8686 21.0006 19.7385 20.9747 19.6171 20.9244C19.4957 20.8741 19.3854 20.8004 19.2925 20.7075L16 17.4137L12.7075 20.7075C12.6146 20.8004 12.5043 20.8741 12.3829 20.9244C12.2615 20.9747 12.1314 21.0006 12 21.0006C11.8686 21.0006 11.7385 20.9747 11.6171 20.9244C11.4957 20.8741 11.3854 20.8004 11.2925 20.7075C11.1996 20.6146 11.1259 20.5043 11.0756 20.3829C11.0253 20.2615 10.9994 20.1314 10.9994 20C10.9994 19.8686 11.0253 19.7385 11.0756 19.6171C11.1259 19.4957 11.1996 19.3854 11.2925 19.2925L14.5863 16L11.2925 12.7075C11.1049 12.5199 10.9994 12.2654 10.9994 12C10.9994 11.7346 11.1049 11.4801 11.2925 11.2925C11.4801 11.1049 11.7346 10.9994 12 10.9994C12.2654 10.9994 12.5199 11.1049 12.7075 11.2925L16 14.5863L19.2925 11.2925C19.3854 11.1996 19.4957 11.1259 19.6171 11.0756C19.7385 11.0253 19.8686 10.9994 20 10.9994C20.1314 10.9994 20.2615 11.0253 20.3829 11.0756C20.5043 11.1259 20.6146 11.1996 20.7075 11.2925C20.8004 11.3854 20.8741 11.4957 20.9244 11.6171C20.9747 11.7385 21.0006 11.8686 21.0006 12C21.0006 12.1314 20.9747 12.2615 20.9244 12.3829C20.8741 12.5043 20.8004 12.6146 20.7075 12.7075L17.4138 16L20.7075 19.2925Z'/%3E%3C/svg%3E");
}

.modal-popup__close:hover::before {
  opacity: 1;
}

.modal-popup__close:active {
  scale: 0.92;
}

.modal-popup__close:focus-visible {
  outline: 2px solid var(--modal-popup-accent);
  outline-offset: 3px;
}

/*
 * Loading state. The spinner sits over the backdrop rather than inside the
 * dialog, because the dialog is still invisible at that point - so the
 * popup never shows an empty frame while it waits.
 */
.modal-popup__status-overlay {
  position: absolute;
  z-index: 1;
  display: none;
  align-items: center;
  justify-content: center;
  pointer-events: none;
  outline: none;
  inset: 0;
}

.modal-popup.is-loading .modal-popup__status-overlay {
  display: flex;
}

.modal-popup__spinner {
  display: block;
  animation: modal-popup-spin 800ms linear infinite;
  border: 3px solid rgb(255, 255, 255, 0.25);
  border-block-start-color: #fff;
  border-radius: 50%;
  inline-size: 2.5rem;
  block-size: 2.5rem;
}

/* Error state, shown inside the dialog once it is visible. */
.modal-popup__status {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 1.25rem;
  min-block-size: var(--modal-popup-min-block-size);
  padding: 0;
  text-align: center;
}

.modal-popup__status .modal-popup__spinner {
  border-color: rgb(26, 28, 30, 0.12);
  border-block-start-color: var(--modal-popup-accent);
}

.modal-popup__error-message {
  margin: 0;
}

.modal-popup__retry {
  padding: 0.75rem 1.5rem;
  cursor: pointer;
  transition: opacity 200ms ease;
  color: inherit;
  border: 1px solid currentColor;
  border-radius: 999px;
  background: transparent;
  font: inherit;
}

.modal-popup__retry:hover {
  opacity: 0.7;
}

.modal-popup__retry:focus-visible {
  outline: 2px solid var(--modal-popup-accent);
  outline-offset: 2px;
}

/*
 * The trigger. A restrained default so the link reads as a control on a
 * theme that has no button styling of its own; add your own class in the
 * formatter settings to replace it entirely.
 */
.modal-popup-link {
  --modal-popup-link-accent: #1a1c1e;

  display: inline-flex;
  align-items: center;
  justify-content: center;
  padding: 0.625rem 1.5rem;
  cursor: pointer;
  transition:
    background-color 200ms ease,
    color 200ms ease;
  text-align: center;
  text-decoration: none;
  color: var(--modal-popup-link-accent);
  border: 1px solid var(--modal-popup-link-accent);
  border-radius: 999px;
  background: transparent;
  font: inherit;
  gap: 0.5rem;
}

.modal-popup-link:hover,
.modal-popup-link:focus-visible {
  text-decoration: none;
  color: #fff;
  background: var(--modal-popup-link-accent);
}

.modal-popup-link:focus-visible {
  outline: 2px solid var(--modal-popup-link-accent);
  outline-offset: 2px;
}

/*
 * Scroll lock. The measured scrollbar width is published by the script as
 * --modal-popup-scrollbar-width so that fixed page furniture can
 * compensate for it and stay put while the dialog is open.
 */
body.modal-popup-open {
  overflow: hidden;
}

/*
 * Small screens: a bottom sheet. The dialog leaves the centre, sits on the
 * bottom edge, rounds only its top corners and slides up into place.
 */
@media (max-width: 62rem) {
  .modal-popup__viewport {
    align-items: flex-end;
    padding: 0;
  }

  .modal-popup__panel {
    inline-size: 100%;
    max-inline-size: none;
    max-block-size: 92vh;
    border-radius: var(--modal-popup-radius-sheet) var(--modal-popup-radius-sheet) 0 0;
    translate: 0 100%;
    scale: 1;
  }

  .modal-popup.is-ready .modal-popup__panel {
    translate: 0;
  }

  .modal-popup__controls {
    inset-block-start: var(--modal-popup-control-inset-sheet);
    inset-inline-end: var(--modal-popup-control-inset-sheet);
  }
}

@keyframes modal-popup-backdrop-in {
  from {
    opacity: 0;
  }
  to {
    opacity: 1;
  }
}

@keyframes modal-popup-spin {
  to {
    rotate: 360deg;
  }
}

@keyframes modal-popup-fade-in {
  from {
    opacity: 0;
  }
  to {
    opacity: 1;
  }
}

@media (prefers-reduced-motion: reduce) {
  .modal-popup::backdrop {
    animation-duration: 1ms;
  }

  .modal-popup,
  .modal-popup__panel,
  .modal-popup__close,
  .modal-popup__retry,
  .modal-popup-link {
    transition-duration: 1ms;
  }

  .modal-popup__panel {
    translate: 0;
    scale: 1;
  }

  .modal-popup__content:not(.is-loading) {
    animation: none;
  }

  .modal-popup__spinner {
    animation-duration: 2.4s;
  }
}

@media (forced-colors: active) {
  .modal-popup__panel {
    border: 1px solid CanvasText;
  }

  .modal-popup__close::before {
    background: CanvasText;
  }
}
