/*
 * Turbo Modal Dialog
 *
 * The custom element is a configuration marker. Its <dialog> child (when
 * a modal is presented) needs to render normally, so we use
 * display:contents — the custom element itself produces no box, but its
 * children participate in the body's layout as if they were direct
 * children of <body>. (display:none on the ancestor would suppress the
 * child dialog even when it's in the top layer.)
 *
 * The <script type="application/json"> child is non-rendering, so it is
 * unaffected.
 *
 * All class names are prefixed with the library name (BEM block
 * `turbo-modal-dialog`, elements flat under it). The prefix is what
 * isolates us from host-application CSS: an unscoped `.modal-dialog`
 * rule (Bootstrap's, notably) would otherwise apply properties we
 * don't declare — Bootstrap sets `pointer-events: none` and
 * `position: relative` on that class, which specificity alone cannot
 * defend against. Rules are additionally nested under the marker,
 * which raises specificity to (0,1,1); direct selector overrides need
 * matching specificity, but in practice most customization goes
 * through the documented CSS custom properties below.
 */

turbo-modal-dialog {
  display: contents;

  /*
   * Default styles for modal dialogs. Override via CSS custom properties
   * or with selectors of equal-or-higher specificity to match your
   * application's design system.
   *
   * Custom properties:
   *   --turbo-modal-dialog-background
   *   --turbo-modal-dialog-color
   *   --turbo-modal-dialog-backdrop
   *   --turbo-modal-dialog-close-background
   *   --turbo-modal-dialog-close-background-hover
   *   --turbo-modal-dialog-border-radius
   *   --turbo-modal-dialog-max-inline-size
   *   --turbo-modal-dialog-max-block-size
   *   --turbo-modal-dialog-animation-duration
   */

  & .turbo-modal-dialog__dialog {
    padding: unset;
    border: unset;
    background: var(--turbo-modal-dialog-background, #fff);
    color: var(--turbo-modal-dialog-color, #000);
    box-shadow:
      0 0.2rem 0.4rem 0 rgba(0, 0, 0, 0.1),
      0 1rem 4rem 0 rgba(0, 0, 0, 0.4);
    overflow: hidden;
    display: flex;
    flex-direction: column;

    /* Mobile (compact): nearly full-screen, anchored to bottom
       like iOS pageSheet on iPhone */
    inline-size: 100%;
    max-inline-size: 100%;
    block-size: var(--turbo-modal-dialog-max-block-size, 95vh);
    max-block-size: var(--turbo-modal-dialog-max-block-size, 95vh);
    border-radius: var(--turbo-modal-dialog-border-radius, 0.75rem 0.75rem 0 0);
    margin-block-start: auto;
    margin-block-end: 0;

    &::backdrop {
      background: var(--turbo-modal-dialog-backdrop, rgba(0, 0, 0, 0.5));
    }

    /* Tablet/Desktop (regular): centered card
       like iOS pageSheet on iPad */
    @media (min-width: 45rem) {
      inline-size: calc(100% - 4rem);
      max-inline-size: var(--turbo-modal-dialog-max-inline-size, 40rem);
      block-size: auto;
      max-block-size: var(--turbo-modal-dialog-max-block-size, 85vh);
      border-radius: var(--turbo-modal-dialog-border-radius, 1rem);
      margin: auto;
    }
  }

  & .turbo-modal-dialog__header {
    display: flex;
    align-items: center;
    gap: 0.5rem;
    padding: 0.5rem;
    flex-shrink: 0;
  }

  & .turbo-modal-dialog__title {
    flex: 1;
    font-weight: 600;
    font-size: 1rem;
    line-height: 1.3;
    text-align: center;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
    min-inline-size: 0;
  }

  & .turbo-modal-dialog__iframe {
    border: unset;
    inline-size: 100%;
    block-size: 100%;
    flex: 1 1 auto;
    opacity: 0;
    transition: opacity 0.15s ease-in;
  }

  & .turbo-modal-dialog__iframe--loaded {
    opacity: 1;
  }

  & .turbo-modal-dialog__back-button,
  & .turbo-modal-dialog__close-button {
    cursor: pointer;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    inline-size: 2rem;
    block-size: 2rem;
    padding: 0;
    border: unset;
    border-radius: 0.5rem;
    background: var(--turbo-modal-dialog-close-background, #f6f7f8);
    color: inherit;
    font-size: 1rem;
    line-height: 1;
  }

  /* Reserve layout space when no back step is available so the title
     stays centered between back and close. visibility instead of
     display:none keeps the flex column widths symmetric. */
  & .turbo-modal-dialog__back-button--invisible {
    visibility: hidden;
    pointer-events: none;
  }

  @media (hover: hover) and (pointer: fine) {
    & .turbo-modal-dialog__back-button:hover,
    & .turbo-modal-dialog__close-button:hover {
      background: var(--turbo-modal-dialog-close-background-hover, #dee2e6);
    }
  }

  /*
   * Modal Style Variants
   * Maps to Hotwire Native's modal_style property
   */

  /* large (default) — defined in .turbo-modal-dialog__dialog base
     Mobile: nearly full screen. Desktop: centered card 40rem wide. */

  /* medium — iOS half-sheet detent */
  & .turbo-modal-dialog__dialog--medium {
    block-size: 50vh;
    max-block-size: 50vh;

    @media (min-width: 45rem) {
      max-inline-size: 30rem;
      max-block-size: 60vh;
    }
  }

  /* full — covers entire screen */
  & .turbo-modal-dialog__dialog--full {
    inline-size: 100%;
    max-inline-size: 100%;
    block-size: 100%;
    max-block-size: 100%;
    border-radius: 0;
    margin: 0;
  }

  /* page_sheet — wider card on desktop, same as large on mobile */
  & .turbo-modal-dialog__dialog--page_sheet {
    @media (min-width: 45rem) {
      max-inline-size: 45rem;
      max-block-size: 90vh;
    }
  }

  /* form_sheet — same as large on mobile, narrower card on desktop
     (matches iOS where formSheet == pageSheet on iPhone) */
  & .turbo-modal-dialog__dialog--form_sheet {
    @media (min-width: 45rem) {
      max-inline-size: 25rem;
    }
  }

  /*
   * Animation hookup. The pseudo-element rules and @keyframes below
   * are inherently global; the unique `turbo-modal-dialog`
   * view-transition-name provides their isolation.
   */
  & .turbo-modal-dialog__dialog:not(.turbo-modal-dialog__dialog--no-animation) {
    view-transition-name: turbo-modal-dialog;
  }
}

/*
 * Direct-access mode: hide every other top-level body child so the
 * modal appears over an empty background instead of the modal page's
 * full-page rendering. The dialog is a descendant of turbo-modal-dialog
 * so excluding turbo-modal-dialog is enough.
 */

body.turbo-modal-dialog-direct-access > *:not(turbo-modal-dialog) {
  display: none !important;
}

/*
 * Lock the underlying page's scroll while a modal is open. The HTML
 * spec for <dialog> is silent about scroll behavior, so the default
 * lets wheel and arrow keys scroll the body underneath — jarring and
 * inconsistent with native iOS / Android modal semantics where the
 * background is frozen. Lock via :has() so this stays declarative and
 * tracks the dialog's [open] state automatically.
 */

html:has(turbo-modal-dialog dialog.turbo-modal-dialog__dialog[open]) {
  overflow: hidden;
}

/*
 * View Transition pseudo-elements and @keyframes — necessarily global,
 * but uniquely named after this library's view-transition-name.
 *
 * Slide-up entry and slide-down exit, mirroring the native iOS/Android
 * modal presentation. Driven by the View Transitions API: the JS side
 * wraps dialog mutations in document.startViewTransition(), and these
 * pseudo-element rules animate the captured snapshots. Same animations
 * play regardless of trigger (link click, popstate back/forward, form
 * submit), so present/dismiss feel symmetrical.
 *
 * Browsers without View Transitions support fall back to no animation.
 * Add the .turbo-modal-dialog__dialog--no-animation class to opt out per dialog.
 */

::view-transition-old(turbo-modal-dialog) {
  animation: turbo-modal-dialog-slide-out var(--turbo-modal-dialog-animation-duration, 0.2s) ease-in forwards;
}

::view-transition-new(turbo-modal-dialog) {
  animation: turbo-modal-dialog-slide-in var(--turbo-modal-dialog-animation-duration, 0.3s) ease-out;
}

@keyframes turbo-modal-dialog-slide-in {
  from {
    transform: translateY(100%);
    opacity: 0;
  }
  to {
    transform: translateY(0);
    opacity: 1;
  }
}

@keyframes turbo-modal-dialog-slide-out {
  from {
    transform: translateY(0);
    opacity: 1;
  }
  to {
    transform: translateY(100%);
    opacity: 0;
  }
}

@media (prefers-reduced-motion: reduce) {
  ::view-transition-old(turbo-modal-dialog),
  ::view-transition-new(turbo-modal-dialog) {
    animation-duration: 0s;
  }
}
