/* ==========================================================================
   Dialog & Drawer — built on the native <dialog> element (focus trap, inert
   background, focus return and Esc come from the browser).

   <button data-moey-open="#invite">Invite</button>
   <dialog class="dialog" id="invite" aria-labelledby="invite-title">
     <div class="dialog-header">
       <h2 class="dialog-title" id="invite-title">Invite people</h2>
       <button class="btn-close" aria-label="Close"></button>
     </div>
     <div class="dialog-body">…</div>           (.dialog-flush: edge to edge, for lists)
     <div class="dialog-footer">…buttons…</div>
   </dialog>

   moey.js: opens with data-moey-open, closes on .btn-close / data-moey-dismiss /
   outside click / Esc (data-moey-static keeps it open).
   On phones a .dialog becomes a bottom sheet (.dialog-center keeps it centered).
   ========================================================================== */

/* Lock page scrolling while a modal is open */
html:has(:is(.dialog, .drawer, .sheet)[open]:modal) { overflow: hidden; }

/* Shared ------------------------------------------------------------------------ */
:is(.dialog, .drawer, .sheet) {
  --mo-dialog-padding: 1.5rem;
  --mo-dialog-footer-padding: .875rem;          /* space above & below the footer buttons */
  --mo-dialog-bg: var(--mo-bg-raised);
  --mo-dialog-duration: .22s;
  max-width: none;
  max-height: none;
  padding: 0;
  color: var(--mo-fg-body);
  background: var(--mo-dialog-bg);
  border: 0;
  box-shadow: var(--mo-shadow-overlay);
}
:is(.dialog, .drawer, .sheet)[open] { display: flex; flex-direction: column; }
/* While a modal is open the page keeps the space of its scrollbar (no layout jump), but
   fixed boxes stop short of it — so anything edge-to-edge is sized to the whole window (100vw). */
:is(.dialog, .drawer, .sheet)::backdrop {
  inset-inline-end: auto;
  width: 100vw;
  background: oklch(14% .01 258 / .45);
  backdrop-filter: blur(2px);
  opacity: calc(1 - var(--mo-sheet-drag, 0));   /* fades while a sheet is dragged down */
}
:is(.dialog, .drawer, .sheet):focus-visible { outline: 0; }

.dialog-header {
  display: flex;
  flex-shrink: 0;
  align-items: flex-start;
  gap: .75rem;
  padding: var(--mo-dialog-padding) var(--mo-dialog-padding) 0;
}
.dialog-header > :not(.btn-close, .dialog-icon) { flex: 1; min-width: 0; }
.dialog-title { margin: 0; font-size: 1.125rem; font-weight: var(--mo-font-weight-semibold); line-height: 1.35; }
.dialog-description { margin: .25rem 0 0; font-size: .875rem; color: var(--mo-fg-3); }
.dialog-header > .btn-close { margin: -.375rem -.5rem 0 auto; }

.dialog-body {
  flex: 1 1 auto;
  min-height: 0;
  padding: 1rem var(--mo-dialog-padding);
  overflow-y: auto;
  overscroll-behavior: contain;
  font-size: .9375rem;
}
.dialog-body > :last-child { margin-bottom: 0; }
/* Edge-to-edge body (a list of actions, a table): <div class="dialog-body dialog-flush"> — list rows reach the edges,
   their text lines up with the dialog's padding */
.dialog-body.dialog-flush { padding-inline: 0; }
.dialog-body:has(+ .dialog-footer) { padding-bottom: var(--mo-dialog-footer-padding); }

.dialog-footer {
  display: flex;
  flex-shrink: 0;
  flex-wrap: wrap;
  align-items: center;
  justify-content: flex-end;
  gap: .5rem;
  padding: 0 var(--mo-dialog-padding) var(--mo-dialog-footer-padding);
}
/* Dividers between header / body / footer (for long, scrolling content) */
.dialog-divided .dialog-header { padding-bottom: 1rem; border-bottom: var(--mo-border-width) solid var(--mo-border-color); }
.dialog-divided .dialog-footer { padding-top: var(--mo-dialog-footer-padding); border-top: var(--mo-border-width) solid var(--mo-border-color); }

/* Icon for confirm dialogs: <span class="dialog-icon theme-red"><i class="mi mi-trash-2"></i></span> */
.dialog-icon {
  display: inline-grid;
  flex-shrink: 0;
  place-items: center;
  width: 2.5rem;
  height: 2.5rem;
  font-size: 1.25rem;
  color: var(--mo-theme-fg);
  background: var(--mo-theme-bg-subtle);
  border-radius: 50%;
}

/* Dialog ------------------------------------------------------------------------ */
.dialog {
  /* width takes any value (32rem, 600px, 80%, fit-content, auto = as wide as allowed);
     max-width keeps it on screen. Set --mo-dialog-width on the dialog or any parent. */
  width: var(--mo-dialog-width, 32rem);
  max-width: calc(100% - 2rem);
  max-height: calc(100dvh - 2rem);
  margin: auto;
  border-radius: var(--mo-radius-dialog);
}
.dialog-sm { --mo-dialog-width: 24rem; }
.dialog-lg { --mo-dialog-width: 48rem; }
.dialog-xl { --mo-dialog-width: 72rem; }
.dialog-fit { --mo-dialog-width: fit-content; }   /* as wide as its content */
/* Fullscreen: covers the whole screen (content clears the notch / home bar) */
.dialog-fullscreen { inset-inline: 0 auto; width: 100vw; max-width: none; max-height: 100dvh; height: 100dvh; margin: 0; border-radius: 0; padding: env(safe-area-inset-top) env(safe-area-inset-right) env(safe-area-inset-bottom) env(safe-area-inset-left); }

/* Phones: dialogs become bottom sheets (unless .dialog-center / .dialog-fullscreen) */
@media (width < 576px) {
  .dialog:not(.dialog-center, .dialog-fullscreen) {
    inset-inline: 0 auto;
    width: 100vw;
    max-width: none;
    max-height: calc(100dvh - 2.5rem);
    margin: auto 0 0;
    border-end-start-radius: 0;
    border-end-end-radius: 0;
    padding-bottom: env(safe-area-inset-bottom);
  }
}

/* Sheet -------------------------------------------------------------------------
   A bottom sheet at every size: full width on phones; docked at the bottom and centered
   on larger screens. Drag it down (or flick) to close.
   Height:  default fits the content · .sheet-sm 35% · .sheet-md 50% · .sheet-lg 75% ·
            .sheet-xl as tall as allowed (a gap stays at the top) · .sheet-full the whole screen
   Width (tablets & desktops; phones are always full width):
            .sheet-narrow 30rem · default 40rem · .sheet-wide 56rem · .sheet-stretch
   Or set --mo-sheet-max-width / --mo-sheet-height (on the sheet or any ancestor).
   .sheet-inset floats as a rounded card · data-moey-snap="50%, 100%" adds snap points ·
   data-moey-drag="false" turns dragging off. A .dialog becomes the same draggable sheet on phones. */
.sheet,
.dialog.is-sheet {
  --mo-dialog-duration: .4s;
  --mo-ease: cubic-bezier(.32, .72, 0, 1);      /* the native sheet curve: quick start, long soft landing */
}
.sheet {
  --_top-gap: var(--mo-sheet-top-gap, 2.5rem);   /* the page stays visible above the sheet */
  width: 100vw;
  max-width: var(--mo-sheet-max-width, 40rem);
  height: var(--mo-sheet-height, fit-content);   /* fit-content: a modal dialog would otherwise stretch */
  max-height: calc(100dvh - var(--_top-gap));
  margin: auto auto 0;
  padding-bottom: env(safe-area-inset-bottom);
  border-radius: var(--mo-radius-dialog) var(--mo-radius-dialog) 0 0;
}
/* Snap points need room to grow into: full height unless a size class says otherwise */
.sheet:where([data-moey-snap]) { --mo-sheet-height: 100dvh; }

/* Heights (max-height still keeps the gap at the top; content scrolls inside) */
.sheet-sm { --mo-sheet-height: 35dvh; }
.sheet-md { --mo-sheet-height: 50dvh; }
.sheet-lg { --mo-sheet-height: 75dvh; }
.sheet-xl { --mo-sheet-height: 100dvh; }
/* Fullscreen sheet: edge to edge, no gap, no corners — still drags down to close */
.sheet-full {
  --mo-sheet-height: 100dvh;
  --mo-sheet-max-width: 100vw;
  max-height: 100dvh;
  padding-top: env(safe-area-inset-top);
  border-radius: 0;
}

/* Widths on tablets & desktops */
.sheet-narrow { --mo-sheet-max-width: 30rem; }
.sheet-wide { --mo-sheet-max-width: 56rem; }
.sheet-stretch { --mo-sheet-max-width: 100vw; }

.sheet-inset {
  width: calc(100% - 1rem);
  max-width: var(--mo-sheet-max-width, 40rem);
  max-height: calc(100dvh - var(--_top-gap) - .5rem);
  margin-bottom: max(.5rem, env(safe-area-inset-bottom));
  padding-bottom: 0;
  border-radius: var(--mo-radius-dialog);
}

/* Grab handle: on sheets, and on dialogs while they are phone bottom sheets (moey.js
   adds .is-sheet then). The header and the handle area start a drag. */
:is(.sheet, .dialog.is-sheet):not([data-moey-drag="false"])::before {
  position: absolute;
  top: .5rem;
  left: 50%;
  z-index: 1;
  width: 2.25rem;
  height: .3125rem;
  content: "";
  background: color-mix(in oklab, var(--mo-fg-body) 22%, transparent);
  border-radius: 99px;
  translate: -50% 0;
}
:is(.sheet, .dialog.is-sheet):not([data-moey-drag="false"]) { touch-action: none; }   /* the .dialog-body keeps scrolling */
:is(.sheet, .dialog.is-sheet):not([data-moey-drag="false"]) .dialog-header { padding-top: calc(var(--mo-dialog-padding) + .25rem); }
:is(.sheet, .dialog).is-dragging { cursor: grabbing; user-select: none; }
:is(.sheet, .dialog).is-dragging,
:is(.sheet, .dialog).is-dragging::backdrop { transition: none !important; }

/* Drawer ------------------------------------------------------------------------ */
.drawer {
  position: fixed;
  inset-block: 0;
  inset-inline: auto calc(100% - 100vw);        /* end side by default, flush with the window edge */
  width: var(--mo-drawer-size, 24rem);
  max-width: calc(100% - 2.5rem);
  height: 100dvh;
  margin: 0;
  border-radius: 0;
}
.drawer-start { inset-inline: 0 auto; }
.drawer-top, .drawer-bottom {
  inset-inline: 0 auto;
  width: 100vw;
  max-width: none;
  height: var(--mo-drawer-size, auto);        /* top / bottom: the size is a height; default fits the content */
  max-height: calc(100dvh - 2.5rem);
}
.drawer-top { inset-block: 0 auto; }
.drawer-bottom { inset-block: auto 0; }
.drawer .dialog-body { flex: 1; }
.drawer-sm { --mo-drawer-size: 18rem; }
.drawer-lg { --mo-drawer-size: 36rem; }

/* Inset: a floating, rounded panel instead of one attached to the edge */
.drawer-inset {
  inset-block: .75rem;
  height: calc(100dvh - 1.5rem);
  border-radius: var(--mo-radius-dialog);
}
.drawer-inset:not(.drawer-start, .drawer-top, .drawer-bottom) { inset-inline: auto .75rem; }
.drawer-inset.drawer-start { inset-inline: .75rem auto; }

/* Animation ------------------------------------------------------------------------
   Enter with @starting-style; exit by animating display/overlay (allow-discrete). */
@media (prefers-reduced-motion: no-preference) {
  :is(.dialog, .drawer, .sheet),
  :is(.dialog, .drawer, .sheet)::backdrop {
    transition:
      opacity var(--mo-dialog-duration) var(--mo-ease),
      translate var(--mo-dialog-duration) var(--mo-ease),
      scale var(--mo-dialog-duration) var(--mo-ease),
      overlay var(--mo-dialog-duration) allow-discrete,
      display var(--mo-dialog-duration) allow-discrete;
  }
  /* closed state (also the starting point of the exit) */
  :is(.dialog, .drawer, .sheet):not([open]),
  :is(.dialog, .drawer, .sheet):not([open])::backdrop { opacity: 0; }
  .dialog:not([open]) { scale: .96; translate: 0 .5rem; }
  .drawer:not([open]) { translate: 100% 0; }
  .drawer-start:not([open]) { translate: -100% 0; }
  .drawer-top:not([open]) { translate: 0 -100%; }
  .drawer-bottom:not([open]) { translate: 0 100%; }
  .drawer:not([open]):dir(rtl) { translate: -100% 0; }
  .drawer-start:not([open]):dir(rtl) { translate: 100% 0; }

  @starting-style {
    :is(.dialog, .drawer, .sheet)[open],
    :is(.dialog, .drawer, .sheet)[open]::backdrop { opacity: 0; }
    .dialog[open] { scale: .96; translate: 0 .5rem; }
    .drawer[open] { translate: 100% 0; }
    .drawer-start[open] { translate: -100% 0; }
    .drawer-top[open] { translate: 0 -100%; }
    .drawer-bottom[open] { translate: 0 100%; }
  }

  /* Sheets slide up (and dialogs too, while they are phone bottom sheets) */
  .sheet:not([open]) { opacity: 1; translate: 0 100%; }              /* sheets slide, they don't fade */
  @starting-style { .sheet[open] { opacity: 1; translate: 0 100%; } }
  :is(.sheet, .dialog.is-sheet).dialog-bump { animation-name: mo-sheet-bump; }
  @keyframes mo-sheet-bump { 50% { translate: 0 -.5rem; } }

  @media (width < 576px) {
    .dialog:not(.dialog-center, .dialog-fullscreen):not([open]) { scale: 1; translate: 0 100%; }
    @starting-style {
      .dialog:not(.dialog-center, .dialog-fullscreen)[open] { scale: 1; translate: 0 100%; }
    }
  }

  /* "Can't close that" bump for data-moey-static */
  .dialog-bump { animation: mo-dialog-bump .25s var(--mo-ease); }
  @keyframes mo-dialog-bump { 50% { scale: 1.02; } }
  /* drawers nudge in from their edge */
  .drawer.dialog-bump { animation-name: mo-bump-end; }
  .drawer:dir(rtl).dialog-bump, .drawer-start.dialog-bump { animation-name: mo-bump-start; }
  .drawer-start:dir(rtl).dialog-bump { animation-name: mo-bump-end; }
  .drawer-top.dialog-bump { animation-name: mo-bump-top; }
  .drawer-bottom.dialog-bump { animation-name: mo-bump-bottom; }
  @keyframes mo-bump-end { 50% { translate: -.5rem 0; } }
  @keyframes mo-bump-start { 50% { translate: .5rem 0; } }
  @keyframes mo-bump-top { 50% { translate: 0 .5rem; } }
  @keyframes mo-bump-bottom { 50% { translate: 0 -.5rem; } }
}

/* Moey.confirm() / alert() / prompt() ------------------------------------------------ */
.moey-ask .dialog-body { display: grid; gap: .5rem; padding-bottom: 1.5rem; }   /* clear room between the message and the buttons */
.moey-ask .dialog-title { margin: 0; font-size: 1.125rem; }
.moey-ask-message { margin: 0; color: var(--mo-fg-2); }
.moey-ask-icon { --mo-avatar-size: 2.5rem; margin-bottom: .25rem; font-size: 1.25rem; }
.moey-ask .field { margin-top: .5rem; }
.moey-ask-form { display: contents; }
@media (width < 576px) { .moey-ask .dialog-footer > .btn { flex: 1; min-height: 2.75rem; } }
