/*
 * CN-03 Modal/dialog, CN-04 Confirmation dialog, CN-05 Bottom sheet/drawer
 * — app/components/ui/modal_component.rb,
 * app/components/ui/confirmation_dialog_component.rb.
 *
 * One component, two presentations (spec §3.4 CN-03/05): `.ui-modal` is a
 * centred panel on desktop and, below --bp-md, becomes a bottom sheet via
 * the media query at the end of this file — pure CSS, no separate Ruby
 * class. `variant: :drawer` (.ui-modal--drawer) opts a dialog into the
 * desktop right-slide-in presentation instead.
 *
 * Panel is always --color-surface-solid — never glass on glass (spec
 * line 166): a modal always sits over the page, so translucency is
 * dropped regardless of viewport.
 *
 * `.is-static` classes below apply the open-state visuals without native
 * <dialog> semantics, for forced-open styleguide statics (a real
 * `<dialog open>` traps focus and breaks the page it's embedded in).
 *
 * Token vars only — enforced by test/lint/css_token_lint_test.rb.
 */

/* ---------------------------------------------------------------------- */
/* Scrim / native <dialog> chrome                                         */
/* ---------------------------------------------------------------------- */

/*
 * Placement (pre-existing bug, fixed here because it affected every modal
 * and confirmation dialog in the product): a native <dialog>:modal is
 * `position: fixed; inset: 0; width: fit-content; margin: auto` in the UA
 * stylesheet — `margin: auto` is what centres it. Tailwind's preflight
 * resets `margin: 0` on every element, which beats the UA rule, so every
 * open dialog sat pinned to the viewport's top-left corner. Rather than
 * restoring `margin: auto` (whose centring depends on the panel's
 * fit-content width, which the drawer and sheet variants both override),
 * the dialog fills the viewport and places its panel with flexbox — which
 * is what the drawer and bottom-sheet rules further down were already
 * written against.
 */
.ui-modal {
  padding: 0;
  border: none;
  background: transparent;
  width: 100%;
  height: 100%;
  max-width: none;
  max-height: none;
  overflow: visible;
  z-index: var(--z-modal);
}

.ui-modal[open] {
  display: flex;
  align-items: center;
  justify-content: center;
}

.ui-modal::backdrop {
  background: var(--color-overlay);
}

/*
 * Styleguide forced-open state: renders the open-panel visuals IN FLOW so
 * multiple static examples can sit side by side inside demo cards. A
 * viewport-fixed static with a scrim would stack every example over the
 * page — the real overlay behaviour belongs to the native <dialog> only.
 */
.ui-modal.is-static {
  position: static;
  display: flex;
  justify-content: center;
  width: 100%;
  /* Overrides the viewport-filling height above — a static example sits in
     flow inside a demo card and must be as tall as its panel. */
  height: auto;
}

/* ---------------------------------------------------------------------- */
/* Panel                                                                   */
/* ---------------------------------------------------------------------- */

.ui-modal__panel {
  position: relative;
  display: flex;
  flex-direction: column;
  width: 100%;
  max-height: 85vh;
  background: var(--color-surface-solid);
  border-radius: var(--radius-lg);
  box-shadow: var(--shadow-lg);
  padding: var(--space-6);
  opacity: 0;
  transform: translateY(var(--space-2)) scale(0.98);
  transition: opacity var(--duration-base) var(--ease-out),
              transform var(--duration-base) var(--ease-out);
}

.ui-modal[open] .ui-modal__panel,
.ui-modal.is-static .ui-modal__panel {
  opacity: 1;
  transform: translateY(0) scale(1);
}

.ui-modal--sm .ui-modal__panel {
  max-width: 480px;
}

.ui-modal--md .ui-modal__panel {
  max-width: 560px;
}

.ui-modal--lg .ui-modal__panel {
  max-width: 720px;
}

.ui-modal__handle {
  display: none;
}

/* ---------------------------------------------------------------------- */
/* Header / body / footer                                                 */
/* ---------------------------------------------------------------------- */

.ui-modal__header {
  display: flex;
  align-items: flex-start;
  gap: var(--space-4);
  margin-bottom: var(--space-4);
}

.ui-modal__title {
  flex: 1;
  min-width: 0;
  font-size: var(--text-xl);
  line-height: var(--text-xl--line-height);
  font-weight: 600;
  color: var(--color-ink);
}

.ui-modal__header-action {
  flex-shrink: 0;
}

.ui-modal__close {
  flex-shrink: 0;
}

.ui-modal__body {
  display: block;
  overflow-y: auto;
  min-height: 0;
}

.ui-modal__footer {
  display: flex;
  justify-content: flex-end;
  align-items: center;
  gap: var(--space-2);
  margin-top: var(--space-6);
}

/* Submitting state (spec: footer primary -> loading, body inputs
   disabled) — toggled by dialog_controller.js's `submitting` value from
   turbo:submit-start/-end, or set manually. */
.ui-modal[data-dialog-submitting-value="true"] .ui-modal__body :is(input, textarea, select, button) {
  pointer-events: none;
  opacity: 0.6;
}

/* ---------------------------------------------------------------------- */
/* CN-04 Confirmation dialog                                              */
/* ---------------------------------------------------------------------- */

/* The modal's own header carries the aria-labelledby title but is
   visually redundant with CN-04's icon/title/consequence body — kept in
   the accessibility tree via a standard sr-only clip rather than
   display:none (which would drop it from the tree in most browsers). */
.ui-modal--confirmation .ui-modal__header {
  position: absolute;
  width: 1px;
  height: 1px;
  overflow: hidden;
  clip: rect(0, 0, 0, 0);
  white-space: nowrap;
  margin: 0;
}

.ui-confirmation-dialog__body {
  display: flex;
  flex-direction: column;
  align-items: flex-start;
  gap: var(--space-2);
}

.ui-confirmation-dialog__icon-frame {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 40px;
  height: 40px;
  border-radius: var(--radius-round);
  margin-bottom: var(--space-2);
}

.ui-confirmation-dialog__icon-frame--warning {
  background: var(--color-warning-subtle);
  color: var(--color-warning-ink);
}

.ui-confirmation-dialog__icon-frame--danger {
  background: var(--color-danger-subtle);
  color: var(--color-danger-ink);
}

.ui-confirmation-dialog__icon-frame--accent {
  background: var(--color-accent-100);
  color: var(--color-accent-pressed);
}

.ui-confirmation-dialog__question {
  font-size: var(--text-lg);
  line-height: var(--text-lg--line-height);
  font-weight: 600;
  color: var(--color-ink);
}

.ui-confirmation-dialog__consequence {
  font-size: var(--text-sm);
  line-height: var(--text-sm--line-height);
  color: var(--color-ink-muted);
}

.ui-confirmation-dialog__detail-panel,
.ui-confirmation-dialog__note-field {
  width: 100%;
  margin-top: var(--space-2);
}

/* B6 approve-milestone summary block reward amount (spec: --text-xl
   tabular). */
.ui-confirmation-dialog__summary-amount {
  font-size: var(--text-xl);
  line-height: var(--text-xl--line-height);
  font-variant-numeric: var(--numeric-tabular);
  font-weight: 600;
  color: var(--color-ink);
}

/* ---------------------------------------------------------------------- */
/* CN-05 Drawer variant (desktop) — right slide-in, 420px                 */
/* ---------------------------------------------------------------------- */

/* Qualified to [open]/.is-static — an unqualified `display: flex` here
   would beat the UA default `dialog:not([open]) { display: none }`,
   making a closed drawer render (and take up layout / capture clicks)
   anyway. */
.ui-modal--drawer[open],
.ui-modal--drawer.is-static {
  display: flex;
  justify-content: flex-end;
  align-items: stretch;
}

.ui-modal--drawer .ui-modal__panel {
  max-width: 420px;
  max-height: 100vh;
  height: 100vh;
  border-radius: 0;
  transform: translateX(var(--space-8));
}

.ui-modal--drawer[open] .ui-modal__panel,
.ui-modal--drawer.is-static .ui-modal__panel {
  transform: translateX(0);
}

.ui-modal--drawer.is-static {
  justify-content: flex-end;
  align-items: stretch;
}

/* ---------------------------------------------------------------------- */
/* CN-05 Bottom sheet — every non-drawer modal below --bp-md              */
/* ---------------------------------------------------------------------- */

@media (max-width: 767px) {
  /* Qualified to [open] — same reasoning as .ui-modal--drawer above: an
     open native <dialog> is UA display:block, so align-items/justify-
     content on the unqualified selector was inert (nothing to align/
     justify without display:flex), and the sheet never pinned to the
     bottom. Scoping `display: flex` to [open] keeps the UA's
     dialog:not([open]) { display: none } undefeated on close. */
  .ui-modal:not(.ui-modal--drawer)[open] {
    display: flex;
    align-items: flex-end;
    justify-content: center;
  }

  .ui-modal:not(.ui-modal--drawer).is-static {
    align-items: flex-end;
  }
}

/* Sheet layout rules — shared between the real below-768px switch above
   and .is-sheet below. A forced-open styleguide static (CN-05) needs the
   sheet presentation regardless of the actual viewport width, since a
   media query only ever reacts to the real viewport, not a 375px demo
   frame drawn inside a wider page, so .is-sheet applies the same rules
   unconditionally. */
.ui-modal:not(.ui-modal--drawer).is-sheet .ui-modal__panel,
.ui-modal:not(.ui-modal--drawer)[open] .ui-modal__panel,
.ui-modal:not(.ui-modal--drawer).is-static .ui-modal__panel {
  transform: translateY(0);
}

@media (max-width: 767px) {
  .ui-modal:not(.ui-modal--drawer) .ui-modal__panel {
    max-width: none;
    max-height: 85vh;
    border-radius: var(--radius-lg) var(--radius-lg) 0 0;
    padding-bottom: calc(var(--space-6) + env(safe-area-inset-bottom));
    transform: translateY(100%);
    transition: transform var(--duration-slow) var(--ease-spring);
  }

  .ui-modal:not(.ui-modal--drawer) .ui-modal__handle {
    display: block;
    width: var(--space-8);
    height: 4px;
    border-radius: var(--radius-pill);
    background: var(--color-border-strong);
    margin: 0 auto var(--space-4);
    flex-shrink: 0;
  }
}

.ui-modal:not(.ui-modal--drawer).is-sheet .ui-modal__panel {
  max-width: none;
  max-height: 85vh;
  border-radius: var(--radius-lg) var(--radius-lg) 0 0;
  padding-bottom: calc(var(--space-6) + env(safe-area-inset-bottom));
}

.ui-modal:not(.ui-modal--drawer).is-sheet {
  align-items: flex-end;
}

.ui-modal:not(.ui-modal--drawer).is-sheet .ui-modal__handle {
  display: block;
  width: var(--space-8);
  height: 4px;
  border-radius: var(--radius-pill);
  background: var(--color-border-strong);
  margin: 0 auto var(--space-4);
  flex-shrink: 0;
}
