/*
 * Dialogs — native <dialog> styled as sheets. Full-screen on Mobile; on larger
 * screens a corner panel (--panel), a centered modal (--centered) or a drawer
 * from the right (--drawer). Opened/closed by the `dialog` Stimulus controller.
 */

:root {
  --drawer-width: 320px;
}

dialog.sheet {
  padding: 0;
  margin: 0;
  border: 0;
  color: var(--color-foreground);
  background-color: var(--color-card);
  box-shadow: var(--shadow-dialog);
  border-radius: 0;
  max-width: none;
  max-height: none;
}

dialog.sheet::backdrop {
  background-color: rgba(27, 26, 35, 0.8);
}

dialog.sheet:not([open]) {
  display: none;
}

/* Mobile: every sheet fills the screen */
dialog.sheet[open] {
  position: fixed;
  inset: 0;
  width: 100vw;
  height: 100dvh;
  display: flex;
  flex-direction: column;
}

/* A sheet's parts usually sit inside a turbo-frame and a form. Those wrappers pass the
   column down, so the header and footer stay put and only the body scrolls: a short
   sheet shrinks to its content, a long one stops at its maximum height and scrolls. */
dialog.sheet[open] {
  overflow: hidden;
}

dialog.sheet[open] :has(.sheet__body) {
  display: flex;
  flex: 1 1 auto;
  flex-direction: column;
  min-height: 0;
}

.sheet__header {
  flex: 0 0 auto;
  display: flex;
  align-items: center;
  gap: var(--space-3);
  padding: var(--space-3) var(--space-4);
  border-bottom: 1px solid var(--color-border-subtle);
}

.sheet__title {
  flex: 1;
  min-width: 0;
  font-size: var(--text-xl);
  font-weight: var(--weight-semibold);
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.sheet__header .btn {
  width: auto;
}

.sheet__body {
  flex: 1;
  min-height: 0;
  overflow-y: auto;
  padding: var(--space-4);
}

.sheet__footer {
  flex: 0 0 auto;
  padding: var(--space-3) var(--space-4);
  border-top: 1px solid var(--color-border-subtle);
}

/* Tablet and up: panels */
@media (min-width: 640px) {
  dialog.sheet--panel[open] {
    inset: auto;
    right: var(--space-6);
    bottom: var(--space-6);
    width: 380px;
    height: 560px;
    max-height: calc(100dvh - var(--space-12));
    border-radius: var(--radius-comfortable);
  }

  /* As tall as its content, up to 80% of the window; past that the body scrolls between
     the fixed header and footer. (`fit-content`: with `inset: 0`, `auto` would stretch.) */
  dialog.sheet--centered[open] {
    inset: 0;
    margin: auto;
    width: min(560px, calc(100vw - var(--space-8)));
    height: fit-content;
    max-height: 80dvh;
    border-radius: var(--radius-comfortable);
  }

  dialog.sheet--panel::backdrop,
  dialog.sheet--centered::backdrop {
    background-color: rgba(27, 26, 35, 0.6);
  }

  /* Drawer: full height on the right, like the friends rail on Home. */
  dialog.sheet--drawer[open] {
    inset: 0 0 0 auto;
    width: min(var(--drawer-width), 100vw);
    height: 100dvh;
    background-color: var(--color-deep-night);
    border-left: 1px solid var(--color-border);
    animation: sheet-drawer-in 180ms ease-out;
  }

  dialog.sheet--drawer::backdrop {
    background-color: rgba(27, 26, 35, 0.45);
  }

  dialog.sheet--drawer .sheet__header {
    padding: var(--space-3) var(--space-4);
  }
}

/* Room for both: a quick chat opened from the drawer sits beside it, so the
   friends list stays in view. */
@media (min-width: 1024px) {
  body:has(dialog.sheet--drawer[open]) dialog.sheet--panel[open] {
    right: calc(var(--drawer-width) + var(--space-6));
  }
}

@keyframes sheet-drawer-in {
  from { transform: translateX(100%); }
  to { transform: none; }
}

@media (prefers-reduced-motion: reduce) {
  dialog.sheet--drawer[open] {
    animation: none;
  }
}
