@layer atoms {
  :root,
  :host,
  ::backdrop {
    /* Backdrop fallbacks — ::backdrop doesn't inherit from :root */
    --backdrop-color: #18181d;
    --backdrop-blur: 4px;
    --modal-transition-duration: 300ms;
  }

  /* Disable transitions if reduce motion flag is on */
  @media (prefers-reduced-motion: reduce) {
    :root,
    :host,
    ::backdrop {
      --modal-transition-duration: 0;
    }
  }

  .mobius-modal {
    position: fixed;
    display: flex;
    flex-direction: column;
    width: min(90%, 600px);
    max-height: 90%;
    padding: 0;
    margin: auto;
    background-color: var(--color-background);
    border: none;
    border-radius: var(--radius-1);
    overflow: hidden;

    @media (min-width: 768px) {
      max-height: 80%;
    }

    &:not([open]) {
      display: none;
    }

    &:focus-visible {
      outline: none;
    }

    &::backdrop {
      background-color: transparent;
      transition: background-color var(--modal-transition-duration) ease-in-out;
      backdrop-filter: blur(var(--backdrop-blur));
    }

    /* Injected by "dialog-polyfill" */
    /* Required for iOS <= 15.2 */
    & + .backdrop {
      position: fixed;
      top: 0;
      right: 0;
      bottom: 0;
      left: 0;
      background-color: var(--backdrop-color);
      opacity: 0.3;
      backdrop-filter: blur(var(--backdrop-blur));

      @supports (background-color: color-mix(in srgb, red 50%, blue)) {
        background-color: color-mix(
          in srgb,
          var(--backdrop-color) 30%,
          transparent
        );
        opacity: 1;
      }

      & + ._dialog_overlay {
        position: fixed;
        top: 0;
        right: 0;
        bottom: 0;
        left: 0;
      }
    }

    &.--is-fullscreen {
      top: 0;
      border-radius: 0;
      height: 100%;
      max-height: 100%;
      width: 100%;
      max-width: 100vw;
    }

    &.--no-dialog-support {
      /* Correctly position modal in x/y middle on iOS <= 15.2 */
      top: 50%;
      left: 50%;
      transform: translate(-50%, -50%);
    }

    &:not(.--should-transition) {
      display: none;

      &[open] {
        display: flex;
      }

      &::backdrop {
        background-color: var(--backdrop-color);
        opacity: 0.3;

        @supports (background-color: color-mix(in srgb, red 50%, blue)) {
          background-color: color-mix(
            in srgb,
            var(--backdrop-color) 30%,
            transparent
          );
          opacity: 1;
        }
      }
    }

    &.--fade {
      transition: opacity var(--modal-transition-duration) ease-in-out;
    }

    &.--slide-up {
      transition: transform var(--modal-transition-duration) ease-in-out;
    }

    &.--should-transition {
      &.--transition {
        &::backdrop {
          background-color: var(--backdrop-color);
          opacity: 0.3;

          @supports (background-color: color-mix(in srgb, red 50%, blue)) {
            background-color: color-mix(
              in srgb,
              var(--backdrop-color) 30%,
              transparent
            );
            opacity: 1;
          }
        }
      }

      &.--slide-up {
        transform: translateY(100vh);

        &.--transition {
          transform: translateY(0vh);
        }
      }

      &.--fade {
        opacity: 0;

        &.--transition {
          opacity: 1;
        }
      }
    }
  }

  .mobius-modal__header {
    display: grid;
    grid-template-columns: 1fr auto;
    justify-content: space-between;
    gap: var(--size-md);
    align-items: start;
    padding: var(--size-xs);
    padding-inline: var(--size-sm);
    font-family: var(--font-family);
    font-size: var(--font-size-4);
    margin: 0;

    @media (min-width: 768px) {
      padding: var(--size-sm);
      padding-inline: var(--size-md);
      font-size: var(--font-size-5);
    }
  }

  .mobius-modal__close.mobius-button {
    /* negative padding to match the padding of the header, so it aligns
       horizontally */
    padding: calc(var(--size-sm) * -1);
    font-size: var(--font-size-4);
    flex-shrink: 0;
  }

  .mobius-modal__content {
    flex-grow: 1;
    padding: 0 var(--size-sm);
    margin: var(--size-sm) 0;
    font-family: var(--font-family);
    overflow-y: auto;

    @media (min-width: 768px) {
      padding: 0 var(--size-md);
      margin: var(--size-md) 0;
    }
  }
}
