/* ═══════════════════════════════════════════════════════════════
   DRAWER-N — Slide-out panel using native <dialog>.

   Layout aligns with card-ui: header uses a 3-column grid
   (icon | heading+description | action+close), footer uses a
   flex row with [slot="description"] + [slot="action"] semantics.

   Safari 17.x bug (Flavor C — top-layer): every panel rule is a
   `:scope[attr] > [slot="dialog"][dyn-attr] > [slot="panel"]` chain
   that fails to match in Safari's top-layer (the panel renders
   invisible). Plus Safari throttles requestAnimationFrame while a
   top-layer dialog is open. Workaround: dissolve @scope for everything
   except the token block (kept on `:where(drawer-ui)` so component-API
   tokens still resolve via inheritance). Plain `drawer-ui[…]` selectors
   match correctly in the top layer.
   ═══════════════════════════════════════════════════════════════ */

:where(drawer-ui) {
  /* ── Geometry ── */
  --drawer-width:       24rem;
  --drawer-height:      24rem;
  --drawer-inset:       var(--a-inset);
  --drawer-header-pad:  var(--drawer-inset);
  --drawer-footer-pad:  var(--drawer-inset);
  --drawer-header-gap:  var(--a-space-2);
  --drawer-footer-gap:  var(--a-space-2);

  /* ── Motion ── */
  --drawer-duration:    var(--a-duration);
  --drawer-easing:      var(--a-easing-out);

  /* ── Colors ── */
  --drawer-bg:          var(--a-canvas-1);
  --drawer-border:      var(--a-border-subtle);
  --drawer-divider:     var(--a-border-subtle);
  --drawer-shadow:      var(--a-shadow-lg);
  --drawer-backdrop:    var(--a-scrim-dialog);
  --drawer-fg:          var(--a-fg);
  --drawer-heading-fg:  var(--a-fg-strong);
  --drawer-description-fg: var(--a-fg-subtle);

  /* ── Typography ── */
  --drawer-font-family:      var(--a-font-family);
  --drawer-heading-size:     var(--a-ui-lg);
  --drawer-heading-weight:   var(--a-weight-semibold);
  --drawer-description-size: var(--a-caption-size);
}

drawer-ui {
  box-sizing: border-box;
  display: contents;
}

/* ── Size variants ── */
drawer-ui[size="sm"] { --drawer-width: min(18rem, 90vw); }
drawer-ui[size="md"] { --drawer-width: min(24rem, 90vw); }
drawer-ui[size="lg"] { --drawer-width: min(36rem, 90vw); }

/* ═══════ Dialog shell ═══════ */

drawer-ui > [slot="dialog"]:not([open]) { display: none; }

drawer-ui > [slot="dialog"][open] {
  box-sizing: border-box;
  position: fixed;
  inset: 0;
  width: 100%;
  height: 100%;
  max-width: none;
  max-height: none;
  margin: 0;
  padding: 0;
  border: none;
  background: transparent;
  overflow: clip;
}

drawer-ui > [slot="dialog"]::backdrop {
  background: var(--drawer-backdrop);
  opacity: 0;
  transition: opacity var(--drawer-duration) var(--drawer-easing);
}
drawer-ui > [slot="dialog"][data-open]::backdrop    { opacity: 1; }
drawer-ui > [slot="dialog"][data-closing]::backdrop { opacity: 0; }

/* ═══════ Panel ═══════ */

drawer-ui > [slot="dialog"] > [slot="panel"] {
  box-sizing: border-box;
  position: absolute;
  display: flex;
  flex-direction: column;
  background: var(--drawer-bg);
  color: var(--drawer-fg);
  font-family: var(--drawer-font-family);
  box-shadow: var(--drawer-shadow);
  overflow-y: auto;
  opacity: 0;
}

/* ── Side: right (default) ── */
drawer-ui:not([side]) > [slot="dialog"] > [slot="panel"],
drawer-ui[side="right"] > [slot="dialog"] > [slot="panel"] {
  top: 0; right: 0; bottom: 0;
  width: var(--drawer-width);
  border-left: 1px solid var(--drawer-border);
  transform: translateX(100%);
}
drawer-ui:not([side]) > [slot="dialog"][data-open] > [slot="panel"],
drawer-ui[side="right"] > [slot="dialog"][data-open] > [slot="panel"] {
  transition: transform var(--drawer-duration) var(--drawer-easing),
              opacity   var(--drawer-duration) var(--drawer-easing);
  transform: translateX(0);
  opacity: 1;
}
drawer-ui:not([side]) > [slot="dialog"][data-closing] > [slot="panel"],
drawer-ui[side="right"] > [slot="dialog"][data-closing] > [slot="panel"] {
  transition: transform var(--drawer-duration) var(--drawer-easing),
              opacity   var(--drawer-duration) var(--drawer-easing);
  transform: translateX(100%);
  opacity: 0;
}

/* ── Side: left ── */
drawer-ui[side="left"] > [slot="dialog"] > [slot="panel"] {
  top: 0; left: 0; bottom: 0;
  width: var(--drawer-width);
  border-right: 1px solid var(--drawer-border);
  transform: translateX(-100%);
}
drawer-ui[side="left"] > [slot="dialog"][data-open] > [slot="panel"] {
  transition: transform var(--drawer-duration) var(--drawer-easing),
              opacity   var(--drawer-duration) var(--drawer-easing);
  transform: translateX(0); opacity: 1;
}
drawer-ui[side="left"] > [slot="dialog"][data-closing] > [slot="panel"] {
  transition: transform var(--drawer-duration) var(--drawer-easing),
              opacity   var(--drawer-duration) var(--drawer-easing);
  transform: translateX(-100%); opacity: 0;
}

/* ── Side: bottom ── */
drawer-ui[side="bottom"] > [slot="dialog"] > [slot="panel"] {
  bottom: 0; left: 0; right: 0;
  height: var(--drawer-height);
  border-top: 1px solid var(--drawer-border);
  transform: translateY(100%);
}
drawer-ui[side="bottom"] > [slot="dialog"][data-open] > [slot="panel"] {
  transition: transform var(--drawer-duration) var(--drawer-easing),
              opacity   var(--drawer-duration) var(--drawer-easing);
  transform: translateY(0); opacity: 1;
}
drawer-ui[side="bottom"] > [slot="dialog"][data-closing] > [slot="panel"] {
  transition: transform var(--drawer-duration) var(--drawer-easing),
              opacity   var(--drawer-duration) var(--drawer-easing);
  transform: translateY(100%); opacity: 0;
}

/* ── Side: top ── */
drawer-ui[side="top"] > [slot="dialog"] > [slot="panel"] {
  top: 0; left: 0; right: 0;
  height: var(--drawer-height);
  border-bottom: 1px solid var(--drawer-border);
  transform: translateY(-100%);
}
drawer-ui[side="top"] > [slot="dialog"][data-open] > [slot="panel"] {
  transition: transform var(--drawer-duration) var(--drawer-easing),
              opacity   var(--drawer-duration) var(--drawer-easing);
  transform: translateY(0); opacity: 1;
}
drawer-ui[side="top"] > [slot="dialog"][data-closing] > [slot="panel"] {
  transition: transform var(--drawer-duration) var(--drawer-easing),
              opacity   var(--drawer-duration) var(--drawer-easing);
  transform: translateY(-100%); opacity: 0;
}

/* ═══════ Header — card-ui-aligned grid ═══════
   Block by default (fallback for empty/free-form content).
   Grid activates when any slotted child is present. Column template
   adapts to the presence of [slot="icon"] and [slot="action"|"close"]. */

drawer-ui [slot="panel"] > [slot="header"] {
  display: block;
  padding: var(--drawer-header-pad);
  border-bottom: 1px solid var(--drawer-divider);
  color: var(--drawer-heading-fg);
  flex-shrink: 0;
  /* Sticky so that with multiple <section> siblings scrolling below,
     the header stays pinned to the top of the panel. */
  position: sticky;
  top: 0;
  background: var(--drawer-bg);
  z-index: 1;
}

drawer-ui [slot="panel"] > [slot="header"]:has(> [slot]) {
  display: grid;
  gap: var(--drawer-header-gap);
  align-items: center;
}

drawer-ui [slot="panel"] > [slot="header"][padding] {
  background: var(--a-canvas-0-scrim);
}

drawer-ui [slot="panel"] > [slot="header"][center] {
  text-align: center;
  justify-items: center;
}

/* Column templates — mirror card-ui. `:has(> …)` restricts to direct
   children so that nested slot="icon" (e.g. inside an <avatar-ui>) doesn't
   accidentally activate the icon column. Treat [slot="close"] as an
   action-column occupant. */
drawer-ui [slot="panel"] > [slot="header"]:has(> [slot="icon"]):has(> :is([slot="action"], [slot="close"])) {
  grid-template-columns: max-content 1fr max-content;
}
drawer-ui [slot="panel"] > [slot="header"]:has(> [slot="icon"]):not(:has(> :is([slot="action"], [slot="close"]))) {
  grid-template-columns: max-content 1fr;
}
drawer-ui [slot="panel"] > [slot="header"]:not(:has(> [slot="icon"])):has(> :is([slot="action"], [slot="close"])) {
  grid-template-columns: 1fr max-content;
}
drawer-ui [slot="panel"] > [slot="header"]:not(:has(> [slot="icon"])):not(:has(> :is([slot="action"], [slot="close"]))) {
  grid-template-columns: 1fr;
}

/* Icon — first column */
drawer-ui [slot="panel"] > [slot="header"] > [slot="icon"] {
  grid-column: 1;
  grid-row: 1;
  align-self: center;
  display: flex;
  align-items: center;
  justify-content: center;
}
drawer-ui [slot="panel"] > [slot="header"]:has(> :is([slot="description"], p, small)) > [slot="icon"] {
  grid-row: 1 / span 2;
  align-self: start;
}

/* Heading — row 1 */
drawer-ui [slot="panel"] > [slot="header"] > :is([slot="heading"], h1, h2, h3, h4, h5, h6),
drawer-ui [slot="panel"] > [slot="header"] > [slot="heading"] :is(h1, h2, h3, h4, h5, h6) {
  grid-row: 1;
  line-height: 1.3;
  margin: 0;
  font-size: var(--drawer-heading-size);
  font-weight: var(--drawer-heading-weight);
  color: var(--drawer-heading-fg);
}
drawer-ui [slot="panel"] > [slot="header"] > [slot="heading"] {
  display: flex;
  align-items: center;
  gap: var(--drawer-header-gap);
}
drawer-ui [slot="panel"] > [slot="header"]:has(> [slot="icon"]) > :is([slot="heading"], h1, h2, h3, h4, h5, h6) {
  grid-column: 2;
}
drawer-ui [slot="panel"] > [slot="header"]:not(:has(> [slot="icon"])) > :is([slot="heading"], h1, h2, h3, h4, h5, h6) {
  grid-column: 1;
}

/* Description — row 2 */
drawer-ui [slot="panel"] > [slot="header"] > :is([slot="description"], p, small) {
  grid-row: 2;
  grid-column: 1 / -1;
  line-height: 1.4;
  margin: 0;
  font-size: var(--drawer-description-size);
  color: var(--drawer-description-fg);
}
drawer-ui [slot="panel"] > [slot="header"]:has(> [slot="icon"]) > :is([slot="description"], p, small) {
  grid-column: 2 / -1;
}

/* Action + close — row 1, last column */
drawer-ui [slot="panel"] > [slot="header"] > :is([slot="action"], [slot="close"]) {
  justify-self: end;
  align-self: center;
  grid-row: 1;
  grid-column: -2 / -1;
  display: flex;
  align-items: center;
  gap: var(--drawer-header-gap);
}

/* ═══════ Body (section) ═══════
   Padded by default. Multiple <section> siblings are allowed — they stack
   inside the panel's own scroll region (see panel's overflow-y + sticky
   header/footer above). [bleed] removes padding; [padding] adds a canvas
   scrim background (card-ui parity). */

drawer-ui [slot="panel"] > [slot="body"] {
  padding: var(--drawer-inset);
  /* Last body takes any remaining vertical slack so the footer hugs the
     bottom even when content is shorter than the viewport. */
  flex: 0 0 auto;
}
drawer-ui [slot="panel"] > [slot="body"]:last-of-type { flex: 1 0 auto; }
drawer-ui [slot="panel"] > [slot="body"] + [slot="body"] { padding-top: 0; }
drawer-ui [slot="panel"] > [slot="body"][bleed]   { padding: 0; }
drawer-ui [slot="panel"] > [slot="body"][padding] { background: var(--a-canvas-0-scrim); }

/* ═══════ Footer ═══════
   Block default — single non-slotted child (e.g. <grid-ui>, <col-ui>, raw
   paragraph) stretches full-width naturally. Activate flex-row only when a
   direct slotted child is present, or there are 2+ children — that's the
   action-row case. Mirrors card-ui footer semantics. */

drawer-ui [slot="panel"] > [slot="footer"] {
  display: block;
  padding: var(--drawer-footer-pad);
  border-top: 1px solid var(--drawer-divider);
  flex-shrink: 0;
  /* Sticky to the bottom so action buttons stay visible during scroll. */
  position: sticky;
  bottom: 0;
  background: var(--drawer-bg);
  z-index: 1;
}

drawer-ui [slot="panel"] > [slot="footer"]:has(> [slot]),
drawer-ui [slot="panel"] > [slot="footer"]:has(> :nth-child(2)) {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: var(--drawer-footer-gap);
}

drawer-ui [slot="panel"] > [slot="footer"][justify="end"] {
  justify-content: flex-end;
}

drawer-ui [slot="panel"] > [slot="footer"]:has(> :is([slot="description"], p, small)):has(> [slot="action"]) {
  justify-content: space-between;
}

drawer-ui [slot="panel"] > [slot="footer"] :is([slot="description"], p, small) {
  font-size: var(--drawer-description-size);
  color: var(--drawer-description-fg);
  flex: 1;
  min-width: 0;
  margin: 0;
}

drawer-ui [slot="panel"] > [slot="footer"] [slot="action"] {
  margin-inline-start: auto;
  display: flex;
  gap: var(--drawer-footer-gap);
}

drawer-ui [slot="panel"] > [slot="footer"] > [slot="action"] ~ [slot="action"] {
  margin-inline-start: 0;
}

/* Dual-cluster footer: leading action (e.g. Delete) on the inline-start
   edge, trailing action cluster on the inline-end. The margin-inline-end:
   auto on the leading slot fills the gap between the two groups. */
drawer-ui [slot="panel"] > [slot="footer"] > [slot="action-leading"] {
  margin-inline-end: auto;
  display: flex;
  gap: var(--drawer-footer-gap);
}
