@layer cinder.tokens, cinder.foundation, cinder.components, cinder.utilities;
@layer cinder.components {
  /* ========================================
 * MODAL
 * ======================================== */

  .cinder-modal {
    /* Reset browser's default <dialog> styles */
    margin: auto;
    padding: 0;
    border: none;
    border-radius: var(--cinder-radius-lg);
    background: transparent;
    color: var(--cinder-text-default);
    max-width: min(90vw, 32rem);
    width: 100%;

    /* Keep dialog-owned anchored surfaces in the native top layer visible.
     * The panel below remains the clipping boundary for ordinary content. */
    max-height: calc(100dvh - var(--cinder-space-8));
    overflow: visible;
  }

  /* Chromeless / full-bleed mode (`chrome="none"`): suppresses max-width so the
   * dialog fills the viewport. The header, border, and body padding are
   * suppressed further down, scoped to `.cinder-modal__panel[data-cinder-chrome='none']`
   * and `.cinder-modal__body[data-cinder-chrome='none']`. All coordination logic
   * (focus trap, scroll lock, escape stack, exit-transition lifecycle) is
   * untouched — this block is presentation only. */
  .cinder-modal[data-cinder-chrome='none'] {
    max-width: none;
    width: 100%;
    height: 100%;
    max-height: 100dvh;
    inset: 0;
    margin: 0;
  }

  /* Backdrop — native <dialog>::backdrop is controlled by the UA.
   * `::backdrop` does NOT reliably inherit custom properties from its
   * originating element across engines (some engines inherit from the
   * dialog, some effectively from the initial containing block/`:root`,
   * some not at all).
   *
   * The fallback lives on the CONSUMING property (`background-color`
   * below), NOT on a redeclaration of `--cinder-modal-backdrop` itself.
   * `--cinder-modal-backdrop: var(--cinder-modal-backdrop, fallback)` looks
   * like a reasonable "use the inherited value, or fall back" pattern, but
   * it is a CSS custom-property dependency CYCLE — a property referencing
   * itself, even guarded by a fallback — which the spec resolves by making
   * the property invalid at computed-value time (cycle detection happens
   * BEFORE fallback substitution). That breaks the backdrop for every Modal
   * with no override at all, which is strictly worse than the reliability
   * problem this rule exists to solve.
   *
   * Declaring NOTHING here for `--cinder-modal-backdrop` and only consuming
   * it via `var(--cinder-modal-backdrop, var(--cinder-overlay-backdrop))`
   * is what's actually correct: whatever value the property resolves to for
   * `::backdrop`'s own scope (inherited from `:root`/an ancestor in
   * whichever engines route inheritance that way, or from a same-selector
   * `::backdrop` override, or simply unset) is used as-is, falling back to
   * `--cinder-overlay-backdrop` only when the property is truly unset/
   * invalid here. A consumer override must still target `::backdrop`
   * directly for cross-engine reliability — see `ImageLightbox`'s
   * `:global(.lightbox-modal::backdrop)` rule for the pattern; a
   * class-scoped override on the element alone is not reliably visible
   * here. */
  .cinder-modal::backdrop {
    background-color: var(--cinder-modal-backdrop, var(--cinder-overlay-backdrop));
    backdrop-filter: blur(var(--cinder-overlay-blur));
    transition:
      background-color var(--cinder-duration-normal) var(--cinder-ease-standard),
      backdrop-filter var(--cinder-duration-normal) var(--cinder-ease-standard);
    /* `::backdrop` is added to/removed from the top layer as a discrete step
     (like `display`), so `@starting-style` below needs `allow-discrete` to
     actually engage on insertion instead of silently no-op'ing. */
    transition-behavior: allow-discrete;
  }

  .cinder-modal[data-cinder-closing]::backdrop {
    background-color: transparent;
    backdrop-filter: blur(0);
  }

  @starting-style {
    .cinder-modal::backdrop {
      background-color: transparent;
      backdrop-filter: blur(0);
    }
  }

  /* ----------------------------------------
 * Panel (the visible card inside the dialog)
 * ---------------------------------------- */

  .cinder-modal__panel {
    position: relative;
    display: flex;
    flex-direction: column;
    background: var(--cinder-surface-raised);
    /* A structural hairline, at most 1.5:1 against the surface drawn outside it, which is the page
       under the dimmed backdrop (COR-2985). */
    border: var(--cinder-border-width) solid var(--cinder-modal-edge);
    border-radius: var(--cinder-radius-lg);
    box-shadow: var(--cinder-shadow-lg);
    max-height: inherit;
    overflow: hidden;
    opacity: 1;
    /* No resting `translate` here (unlike the `[data-cinder-closing]`/
     `@starting-style` states below) — ANY non-`none` transform/translate
     value, including a no-op `0 0`, establishes a CSS containing block,
     which would silently break `position: fixed` for every descendant
     (e.g. a portal-owned popover surface) for the entire time the modal is
     open, not just mid-transition. `none` (the default) transitions to/from
     a real value just fine — matching Drawer/Sheet, which never set a
     resting translate either. */
    transition:
      opacity var(--cinder-duration-normal) var(--cinder-ease-standard),
      translate var(--cinder-duration-normal) var(--cinder-ease-spring);
  }

  /* Someone who asks for more contrast keeps the full structural tier in both themes. */
  @media (prefers-contrast: more) {
    .cinder-modal__panel {
      border-color: var(--cinder-border);
    }
  }

  /* Exit transition: the component keeps the panel mounted for the duration
   of this transition (via the shared `SlidingDialogState`/`data-cinder-closing`
   lifecycle Drawer and Sheet also use) instead of removing it the instant
   `open` goes false, so this can actually play. */
  .cinder-modal__panel[data-cinder-closing] {
    opacity: 0;
    translate: 0 -0.5rem;
    pointer-events: none;
  }

  @starting-style {
    .cinder-modal__panel {
      opacity: 0;
      translate: 0 -0.5rem;
    }
  }

  /* Chromeless / full-bleed mode: the panel fills the chromeless dialog
   * and drops the card chrome (border, radius, surface, shadow) so the
   * consumer's own content (e.g. an image lightbox) owns the full surface. */
  .cinder-modal__panel[data-cinder-chrome='none'] {
    height: 100%;
    background: transparent;
    border: none;
    border-radius: 0;
    box-shadow: none;
  }

  /* ----------------------------------------
 * Header
 * ---------------------------------------- */

  .cinder-modal__header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: var(--cinder-space-4);
    /* Reserve trailing inline space so the title doesn't run under the
     absolute-positioned close button. */
    /* The title sits close to what it introduces: 12px under it plus the body's 12px above, so the
       gap to the first line reads at about the body's own paragraph rhythm with no rule, and the
       title has the same room above and below it when the rule is drawn. Unconditional: the rule's
       presence follows the body's overflow, and padding that followed it could change whether the
       body scrolls. */
    padding-block: var(--cinder-space-3);
    padding-inline-start: var(--cinder-space-5);
    padding-inline-end: calc(var(--cinder-space-5) + 2.25rem);
    /* The rule is drawn only while the body scrolls (below); a short confirmation has nothing to
       separate from. The border keeps its width so the header does not move when it appears. */
    border-block-end: 1px solid;
    border-block-end-color: transparent;
    background: var(--cinder-surface-raised);
    flex-shrink: 0;
  }

  .cinder-modal__panel:has(> .cinder-modal__body[data-cinder-scrollable]) .cinder-modal__header {
    border-block-end-color: var(--cinder-border-muted);
  }

  .cinder-modal__panel:has(> .cinder-modal__body[data-cinder-scrollable]) .cinder-modal__footer {
    border-block-start-color: var(--cinder-border-muted);
  }

  /* With no close button to keep clear of, a header that carries trailing content uses the same
     padding at both edges. */
  .cinder-modal__header[data-cinder-trailing]:not([data-cinder-closable]) {
    padding-inline-end: var(--cinder-space-5);
  }

  /* Content opposite the title (a count) reads on the title's line, muted and out of its way. */
  .cinder-modal__header-trailing {
    flex: none;
    font-size: var(--cinder-text-sm);
    color: var(--cinder-text-muted);
  }

  /* The title with the line that says what the dialog is for under it. */
  .cinder-modal__heading {
    display: flex;
    flex-direction: column;
    gap: var(--cinder-space-1);
    min-inline-size: 0;
  }

  .cinder-modal__description {
    margin: 0;
    font-size: var(--cinder-text-sm);
    line-height: var(--cinder-leading-snug);
    color: var(--cinder-text-muted);
  }

  .cinder-modal__title {
    margin: 0;
    font-size: var(--cinder-text-lg);
    font-weight: var(--cinder-font-semibold);
    line-height: var(--cinder-leading-snug);
    color: var(--cinder-text-default);
  }

  /* ----------------------------------------
 * Close button
 * ---------------------------------------- */

  .cinder-modal__close {
    /* Rendered last in DOM order so the close button isn't the first focusable
     element on dialog open — but visually positioned in the header corner. */
    position: absolute;
    /* 8px from the corner on both edges: the 36px button's centre then sits on the title's line
       (12px of header padding plus half the title's line box). */
    inset-block-start: var(--cinder-space-2);
    inset-inline: 0;
    margin-inline: auto var(--cinder-space-2);

    display: inline-flex;
    align-items: center;
    justify-content: center;
    flex-shrink: 0;
    /* The close control is a 36px icon button (COR-2322 AC5). */
    width: 2.25rem;
    height: 2.25rem;
    padding: var(--cinder-space-1);

    border-radius: var(--cinder-radius-md);
    border: none;
    background: transparent;
    color: var(--cinder-text-muted);
    cursor: pointer;

    transition:
      background-color var(--cinder-duration-fast) var(--cinder-ease-standard),
      color var(--cinder-duration-fast) var(--cinder-ease-standard),
      box-shadow var(--cinder-duration-fast) var(--cinder-ease-standard);
  }

  /* The close face stays 36px. On a touch device, its 44px target expands four pixels into the
     panel's existing eight-pixel corner gutter, so the clipped panel contains it and nearby header
     controls keep their own hit areas. */
  @media (any-pointer: coarse) {
    .cinder-modal__close::before {
      content: '';
      position: absolute;
      inset-block: min(0px, calc((100% - var(--cinder-touch-target-min)) / 2));
      inset-inline: min(0px, calc((100% - var(--cinder-touch-target-min)) / 2));
    }
  }

  @media (hover: hover) {
    .cinder-modal__close:hover {
      background: color-mix(in oklch, currentColor, transparent 85%);
      color: var(--cinder-text-default);
    }
  }

  .cinder-modal__close:focus-visible {
    /* The close button sits in the panel corner, and the panel has
     `overflow: hidden`. An outset box-shadow ring would be clipped there, so
     paint an INSET ring (Strategy B-inset) entirely within the button's own
     border box. The transparent outline placeholder reserves the channel the
     forced-colors fallback repaints (WCAG 2.4.7). */
    outline: var(--cinder-ring-width) solid transparent;
    box-shadow: inset 0 0 0 var(--cinder-ring-width)
      var(--_cinder-modal-close-ring, var(--cinder-ring-color));
  }

  @media (forced-colors: active) {
    .cinder-modal__close:focus-visible {
      outline: var(--cinder-ring-width) solid ButtonText;
      outline-offset: calc(var(--cinder-ring-width) * -1);
    }
  }

  .cinder-modal__close-icon {
    width: 1.25rem;
    height: 1.25rem;
  }

  /* ----------------------------------------
 * Body
 * ---------------------------------------- */

  .cinder-modal__body {
    --_cinder-scroll-fade-color: var(--cinder-surface-raised);

    flex: 1;
    overflow-y: auto;
    /* The header, body and footer share one inline inset (space-5), so the title, the body text
       and the footer actions start on one edge (s4#8). No gutter is reserved: a reserved gutter
       made the body's inline-end inset wider than its start whenever the scrollbar is classic,
       even when nothing scrolls. */
    padding: var(--cinder-space-3) var(--cinder-space-5) var(--cinder-space-4);
    background: var(--cinder-surface-raised);
    overscroll-behavior: contain;
    scrollbar-gutter: auto;
    /* The body container is focused on open (tabindex=-1) when the dialog has
     no autofocus target. The ring is hidden because focus arrived
     programmatically — pressing Tab forward will move into real content. */
    outline: none;
  }

  /* A body with nothing in it takes no room. A confirmation whose only content is its failure (the
   * callout lives in the footer) would otherwise leave a padded band between the title and the
   * footer. `:empty` ignores the comment nodes a template leaves, so only a body that renders
   * nothing collapses. */
  .cinder-modal__body:empty {
    display: none;
  }

  .cinder-modal__body[data-cinder-chrome='none'] {
    padding: 0;
    background: transparent;
  }

  /* Programmatic initial focus stays visually quiet, while a body made keyboard-reachable by
   * `overflowFade({ keyboardScrollable })` keeps a clipping-safe inset ring. */
  .cinder-modal__body:focus:not(:focus-visible) {
    outline: none;
  }

  .cinder-modal__body:focus-visible {
    outline: var(--cinder-ring-width) solid transparent;
    box-shadow: inset 0 0 0 var(--cinder-ring-width) var(--cinder-ring-color);
  }

  @media (forced-colors: active) {
    .cinder-modal__body:focus-visible {
      outline: var(--cinder-ring-width) solid Highlight;
      outline-offset: calc(var(--cinder-ring-width) * -1);
    }
  }

  /* The shared `.cinder-_scroll-fade` recipe (see `_scroll-fade.css`) fades
   * with an OPAQUE overlay painted in `--_cinder-scroll-fade-color`
   * (`--cinder-surface` here) by design — masking a container that paints
   * its own background would make the fade light up instead of fading out
   * over a brighter parent surface (see that file's design-rule #1). A
   * chromeless body is transparent/full-bleed on purpose (an image
   * lightbox's photo, for instance) with no surface color of its own to
   * fade INTO, so that same opaque band is simply wrong here — it would
   * paint a solid `--cinder-surface` stripe across arbitrary full-bleed
   * content. `content: none` fully suppresses the generated `::after` box
   * (rather than merely zeroing `opacity`, which a running
   * `animation-timeline: scroll()` keyframe would still override — see this
   * file's own forced-colors comments for why `opacity` alone can't win
   * against that animation), so no fade box exists at all in the chromeless
   * chrome. */
  .cinder-modal__body[data-cinder-chrome='none']::after {
    content: none;
  }

  /* ----------------------------------------
 * Footer
 * ---------------------------------------- */

  .cinder-modal__footer {
    display: flex;
    align-items: center;
    justify-content: flex-end;
    gap: var(--cinder-space-3);
    padding: var(--cinder-space-3) var(--cinder-space-5);
    border-block-start: 1px solid;
    border-block-start-color: transparent;
    background: var(--cinder-surface-raised);
    flex-shrink: 0;
  }

  /* The centered dialog's footer wraps, so three buttons that do not fit one line start another
     line at the end edge instead of overflowing and clipping the first. The bottom sheet is left
     alone: its footer is a stacked column with its own rules below. */
  .cinder-modal:not([data-cinder-sheet='docked']) .cinder-modal__footer {
    flex-wrap: wrap;
  }

  /* Chromeless mode fills the dialog's entire content box with the panel/
   * body/footer as one full-bleed surface — the base rule's card-style
   * background, top border, and padding would paint a visible seam between
   * the footer and a consumer's own full-bleed content, breaking the
   * "genuinely full-bleed" contract chrome="none" promises. Reset all three,
   * matching the identical pattern already applied to
   * `.cinder-modal__body[data-cinder-chrome='none']` above. */
  .cinder-modal__footer[data-cinder-chrome='none'] {
    padding: 0;
    background: transparent;
    border-block-start: none;
  }

  /* ----------------------------------------
 * Reduced motion
 * ---------------------------------------- */

  @media (prefers-reduced-motion: reduce) {
    :root:not([data-cinder-reduced-motion='false']):not([data-reduced-motion='off'])
      .cinder-modal__panel {
      transition: none;
    }

    :root:not([data-cinder-reduced-motion='false']):not([data-reduced-motion='off'])
      .cinder-modal::backdrop {
      transition: none;
    }
  }

  /* ----------------------------------------
 * Bottom sheet (row 05d)
 *
 * Below `sheetBelow` the modal docks to the bottom edge, as a drawer placed at the bottom does:
 * full width, rounded top corners, a grabber, a height capped so the body scrolls inside the sheet
 * instead of the sheet outgrowing the screen, and a lift above the on-screen keyboard
 * (`--_cinder-keyboard-inset`, published while it is open). The footer's actions stack at full
 * actions share one row at the button's painted height (the hit area reaches 44px), the last one,
 * the confirmation, filling the rest; DOM order is unchanged, so the
 * first action keeps initial focus. Physical properties, as in drawer.css, because the sheet is
 * physically bottom anchored in every writing mode.
 * ---------------------------------------- */

  .cinder-modal[data-cinder-sheet='docked'] {
    inset-block-start: auto;
    inset-block-end: var(--_cinder-keyboard-inset, 0px);
    inset-inline: 0;
    margin: 0;
    max-width: none;
    max-height: calc(90dvh - var(--_cinder-keyboard-inset, 0px));
    border-radius: 0;
  }

  /* The sheet's shape (COR-2322 AC4): a 20px top radius, a square bottom edge that meets the
   * screen, an upward shadow, and 32px of padding under the last row (more where the device
   * reports a bottom safe area). The panel paints the sheet surface so the padding band reads as
   * part of the sheet rather than a raised strip under the footer. Black, not a theme colour,
   * because a shadow is darkness, not ink: it has to darken the page in both themes. */
  .cinder-modal[data-cinder-sheet='docked'] .cinder-modal__panel {
    border-width: 1px 0 0;
    border-radius: 1.25rem 1.25rem 0 0;
    background: var(--cinder-surface-raised);
    box-shadow: 0 -0.5rem 1.5rem -0.25rem color-mix(in oklch, black, transparent 80%);
    padding-block-end: max(var(--cinder-space-8), env(safe-area-inset-bottom));
  }

  .cinder-modal__grabber {
    flex-shrink: 0;
    inline-size: 2.25rem;
    block-size: 0.25rem;
    margin: var(--cinder-space-2) auto var(--cinder-space-1);
    border-radius: var(--cinder-radius-full);
    background: var(--cinder-border-contrast);
  }

  /* One footer rule for every docked dialog (COR-2448 AC5): the actions share one row, the
   * secondary actions at their own width and the last, the confirmation, filling the rest. When
   * they do not fit one line the confirmation wraps above the others. ConfirmDialog's row of
   * actions (inside its footer, under the failure region) takes the same rule. A lone action
   * fills the row. */
  .cinder-modal[data-cinder-sheet='docked']
    :is(.cinder-modal__footer, .cinder-confirm-dialog__actions) {
    flex-wrap: wrap-reverse;
    align-items: center;
    justify-content: stretch;
  }

  .cinder-modal[data-cinder-sheet='docked']
    :is(.cinder-modal__footer, .cinder-confirm-dialog__actions)
    > * {
    flex: 1 1 auto;
  }

  .cinder-modal[data-cinder-sheet='docked']
    :is(.cinder-modal__footer, .cinder-confirm-dialog__actions)
    > :not(:last-child, :has(+ [data-cinder-button-status]:last-child)) {
    flex: 0 0 auto;
  }

  /* A ConfirmDialog measures whether its two labels actually fit. When they do not, it marks the
   * row and becomes one deliberate vertical group: confirmation above Cancel, both full width.
   * DOM order stays Cancel then confirmation, preserving Cancel's initial focus and tab order. */
  .cinder-modal[data-cinder-sheet='docked']
    .cinder-confirm-dialog__actions[data-cinder-stacked]:not([data-cinder-fault-only]) {
    flex-direction: column-reverse;
    flex-wrap: nowrap;
    align-items: stretch;
  }

  .cinder-modal[data-cinder-sheet='docked']
    .cinder-confirm-dialog__actions[data-cinder-stacked]:not([data-cinder-fault-only])
    > button {
    flex: 0 0 auto;
    inline-size: 100%;
  }

  /* A button that is loading mounts a status region (`data-cinder-button-status`, a visually hidden
   * live region) after itself, so the confirmation is not the last child while the region is
   * there. The region is not an action: the button before it is still the confirmation, and
   * still fills, so it keeps its width when it starts to load (196px, not 151px). */

  /* The one exception to "a lone action fills the row" (owner decision, 2026-11-21): a ConfirmDialog
   * whose failure carries its own action (Retry, Refresh preview) leaves Cancel, labelled Close,
   * the only button of the row. The callout above it holds the action that leads, so Close is
   * compact at the inline end, not full width where it would outweigh that action. A row that is
   * lone for another reason (nothing to confirm) still fills. ConfirmDialog marks the row. */
  .cinder-modal[data-cinder-sheet='docked']
    .cinder-confirm-dialog__actions[data-cinder-fault-only] {
    justify-content: flex-end;
  }

  .cinder-modal[data-cinder-sheet='docked']
    .cinder-confirm-dialog__actions[data-cinder-fault-only]
    > * {
    flex: 0 0 auto;
  }

  /* The body's fields are full 44px targets in the sheet. The selector repeats the input's own class
   * so it outranks the compact height without a cross-component custom property. The footer's
   * buttons are not: a phone button is drawn at its painted height and reaches 44px through the
   * Button's own hit area (`data-cinder-hit-target`), so two of them never take a quarter of the
   * screen. */
  .cinder-modal[data-cinder-sheet='docked']
    :is(.cinder-modal__body, .cinder-confirm-dialog__footer)
    .cinder-input.cinder-input {
    min-height: var(--cinder-touch-target-min);
  }

  .cinder-modal[data-cinder-sheet='docked'] .cinder-modal__panel[data-cinder-closing] {
    translate: 0 100%;
  }

  @starting-style {
    .cinder-modal[data-cinder-sheet='docked'] .cinder-modal__panel {
      translate: 0 100%;
    }
  }

  /* Reduced motion: no slide, a cross-fade instead (design row 05d). foundation.css forces every
   * CSS transition to 0.01ms under reduced motion, so the fade is played from script
   * (reduced-motion-fade.ts); CSS only keeps the panel where it is. */
  @media (prefers-reduced-motion: reduce) {
    :root:not([data-cinder-reduced-motion='false']):not([data-reduced-motion='off'])
      .cinder-modal[data-cinder-sheet='docked']
      .cinder-modal__panel[data-cinder-closing] {
      translate: none;
    }
  }

  .cinder-modal[data-cinder-sheet='docked']:is(:root[data-reduced-motion='on'] *)
    .cinder-modal__panel[data-cinder-closing] {
    translate: none;
  }
}
