/* Drawer. Base rules are the quiet look: a hairline panel pinned to its edge
   (default: the inline-end, a right-hand drawer in English), full height,
   sliding in with a plain slide. A page without JavaScript still gets a
   working native <dialog> (its own-box default position), since the edge
   layout below only applies once the element has upgraded.

   In quiet, the <dialog> itself IS the panel (simplest structure; no Carepa
   to host). In warm/playful the <dialog> becomes full-bleed instead, so the
   Carepa scrim covers the whole viewport like Dialog's does — not just the
   panel's own box — and .sg-drawer-card is placed and animated at the edge
   inside it. Rasika OB1: an edge-sized dialog only dimmed the rest of the
   page instead of hiding it; the page's own body text stayed legible next
   to the panel. */

sg-drawer { display: contents; }
sg-drawer dialog {
  box-sizing: border-box;
  position: fixed;
  inset: 0 0 0 auto;
  margin: 0;
  max-inline-size: min(26rem, 92vw);
  inline-size: min(26rem, 92vw);
  block-size: 100%;
  max-block-size: none;
  padding: 0;
  border: 0;
  border-inline-start: var(--sg-hairline) solid var(--sg-rule-strong);
  background: var(--sg-surface-raised);
  color: var(--sg-text);
  box-shadow: -24px 0 60px -30px var(--sg-shadow);
}
sg-drawer dialog::backdrop { background: var(--sg-scrim, color-mix(in oklab, Canvas 55%, transparent)); }
sg-drawer dialog:not([open]) { display: none; }
sg-drawer .sg-drawer-scrim { display: none; }
/* Rasika S4: .sg-drawer-card stays overflow: visible (its default) so the
   deckled ::before, drawn 1px outside its own border box, is never clipped.
   Scrolling, padding and block-size live on .sg-drawer-content instead — a
   plain inner wrapper with no decoration of its own. */
sg-drawer .sg-drawer-card { position: relative; block-size: 100%; box-sizing: border-box; }
sg-drawer .sg-drawer-content { padding: var(--sg-space-4); display: flow-root; block-size: 100%; box-sizing: border-box; overflow: auto; }
sg-drawer .sg-drawer-card h2, sg-drawer .sg-drawer-card h3 { font: 400 1.375rem/1.2 var(--sg-font-display); margin: 0 0 var(--sg-space-2); }
sg-drawer :focus-visible { outline: var(--sg-focus-width) solid var(--sg-focus); outline-offset: var(--sg-focus-offset); }
/* actions: the same styling as Dialog's card buttons (Rasika OP1) */
sg-drawer .sg-drawer-card menu, sg-drawer .sg-drawer-card form menu {
  display: flex; gap: var(--sg-space-2); justify-content: flex-end; margin: var(--sg-space-4) 0 0; padding: 0; list-style: none;
}
sg-drawer .sg-drawer-card button {
  font: 500 0.9375rem/1 var(--sg-font-body); color: var(--sg-text); background: var(--sg-surface);
  border: var(--sg-hairline) solid var(--sg-rule-strong); border-radius: var(--sg-radius-pill); padding: 0.6em 1.1em; cursor: pointer;
}
sg-drawer .sg-drawer-card button[value="ok"] {
  background: var(--sg-accent); color: var(--sg-on-accent); border-color: transparent;
}

/* the four edges (logical: start/end flip in a right-to-left page), quiet's own-box panel */
sg-drawer[data-edge="start"] dialog { inset: 0 auto 0 0; border-inline: 0; border-inline-end: var(--sg-hairline) solid var(--sg-rule-strong); box-shadow: 24px 0 60px -30px var(--sg-shadow); }
sg-drawer[data-edge="top"] dialog { inset: 0 0 auto 0; inline-size: 100%; max-inline-size: none; block-size: min(70vh, 30rem); border-inline: 0; border-block-end: var(--sg-hairline) solid var(--sg-rule-strong); box-shadow: 0 24px 60px -30px var(--sg-shadow); }
sg-drawer[data-edge="bottom"] dialog { inset: auto 0 0 0; inline-size: 100%; max-inline-size: none; block-size: min(70vh, 30rem); border-inline: 0; border-block-start: var(--sg-hairline) solid var(--sg-rule-strong); box-shadow: 0 -24px 60px -30px var(--sg-shadow); }

/* ── warm and playful: the dialog is full-bleed (Carepa fills the whole
   viewport, exactly like Dialog's does), and .sg-drawer-card is the panel,
   placed and animated at the edge inside it. [open] gates every rule that
   sets `display` or the full-bleed box, the same lesson as Dialog's own
   cascade bug: a closed dialog must stay display:none, never win the
   full-bleed layout by source order. ── */
sg-drawer[data-skin="warm"] dialog[open],
sg-drawer[data-skin="playful"] dialog[open] {
  inset: 0; margin: 0; max-inline-size: none; max-block-size: none;
  inline-size: 100%; block-size: 100%;
  background: transparent; box-shadow: none; border: 0;
}
sg-drawer[data-skin="warm"] .sg-drawer-scrim,
sg-drawer[data-skin="playful"] .sg-drawer-scrim {
  display: block; position: absolute; inset: 0; z-index: 0;
}
sg-drawer[data-skin="warm"] .sg-drawer-card,
sg-drawer[data-skin="playful"] .sg-drawer-card {
  position: absolute; z-index: 1; inset-block: 0;
  inset-inline-start: auto; inset-inline-end: 0; /* default edge: end */
  inline-size: min(26rem, 92vw); block-size: 100%;
  background: color-mix(in oklab, var(--sg-surface-raised) 97%, transparent);
  box-shadow: -18px 0 46px -26px var(--sg-shadow);
}
sg-drawer[data-edge="start"][data-skin="warm"] .sg-drawer-card,
sg-drawer[data-edge="start"][data-skin="playful"] .sg-drawer-card {
  inset-inline: 0 auto; box-shadow: 18px 0 46px -26px var(--sg-shadow);
}
sg-drawer[data-edge="top"][data-skin="warm"] .sg-drawer-card,
sg-drawer[data-edge="top"][data-skin="playful"] .sg-drawer-card {
  inset: 0 0 auto 0; inline-size: 100%; block-size: min(70vh, 30rem); box-shadow: 0 18px 46px -26px var(--sg-shadow);
}
sg-drawer[data-edge="bottom"][data-skin="warm"] .sg-drawer-card,
sg-drawer[data-edge="bottom"][data-skin="playful"] .sg-drawer-card {
  inset: auto 0 0 0; inline-size: 100%; block-size: min(70vh, 30rem); box-shadow: 0 -18px 46px -26px var(--sg-shadow);
}
/* the deckled (torn-paper) edge, drawn with a mask so it works at any panel height */
sg-drawer[data-skin="warm"] .sg-drawer-card::before,
sg-drawer[data-skin="playful"] .sg-drawer-card::before {
  content: ""; position: absolute; inset-block: 0; inline-size: 10px; inset-inline-start: -1px;
  background: color-mix(in oklab, var(--sg-surface-raised) 97%, transparent);
  -webkit-mask: repeating-radial-gradient(circle at 0 3px, transparent 0 1.2px, #000 1.6px 3px) 0 0 / 6px 6px;
  mask: repeating-radial-gradient(circle at 0 3px, transparent 0 1.2px, #000 1.6px 3px) 0 0 / 6px 6px;
}
sg-drawer[data-edge="start"][data-skin="warm"] .sg-drawer-card::before,
sg-drawer[data-edge="start"][data-skin="playful"] .sg-drawer-card::before { inset-inline-start: auto; inset-inline-end: -1px; }

/* ── entering and leaving: a transition from the first frame ────────────────
   Quiet: the panel is the dialog, and it changes opacity for 120 ms, nothing
   else. Warm and playful: the dialog fades, and the panel slides in from its
   edge (a short slide in warm, a longer one on the spring in playful, timed to
   the Teental cycle's first beat). Where the browser can hold the drawer in
   the top layer while it goes (the overlay property) it leaves the same way.
   Reduced motion: it is simply there. */
sg-drawer dialog {
  --_dur: 120ms; --_ease: var(--sg-ease-out, ease-out); --_slide: 0rem;
  opacity: 0;
}
sg-drawer dialog[open] { opacity: 1; transition: opacity var(--_dur) var(--_ease); }
sg-drawer dialog::backdrop { opacity: 0; }
sg-drawer dialog[open]::backdrop { opacity: 1; transition: opacity 120ms ease-out; }
@starting-style {
  sg-drawer dialog[open] { opacity: 0; }
  sg-drawer dialog[open]::backdrop { opacity: 0; }
}
@supports (overlay: auto) {
  sg-drawer dialog:not([open]) { transition: opacity var(--_dur) var(--_ease), overlay var(--_dur) allow-discrete, display var(--_dur) allow-discrete; }
  sg-drawer dialog:not([open])::backdrop { transition: opacity 120ms ease-out; }
}
sg-drawer[data-skin="warm"] dialog { --_dur: 200ms; --_slide: 2rem; }
sg-drawer[data-skin="playful"] dialog { --_dur: 280ms; --_ease: var(--sg-ease-spring, ease-out); --_slide: 3rem; }
sg-drawer:dir(rtl) dialog { --_flip: -1; }
sg-drawer[data-edge="end"] dialog { --_in-x: calc(var(--_flip, 1) * var(--_slide)); --_in-y: 0px; }
sg-drawer[data-edge="start"] dialog { --_in-x: calc(var(--_flip, 1) * -1 * var(--_slide)); --_in-y: 0px; }
sg-drawer[data-edge="top"] dialog { --_in-x: 0px; --_in-y: calc(-1 * var(--_slide)); }
sg-drawer[data-edge="bottom"] dialog { --_in-x: 0px; --_in-y: var(--_slide); }
sg-drawer:is([data-skin="warm"], [data-skin="playful"]) .sg-drawer-card { translate: var(--_in-x, 0px) var(--_in-y, 0px); opacity: 0; }
sg-drawer:is([data-skin="warm"], [data-skin="playful"]) dialog[open] .sg-drawer-card {
  translate: 0 0; opacity: 1;
  transition: translate var(--_dur) var(--_ease) var(--sg-drawer-delay, 0ms), opacity var(--_dur) var(--_ease) var(--sg-drawer-delay, 0ms);
}
@starting-style {
  sg-drawer:is([data-skin="warm"], [data-skin="playful"]) dialog[open] .sg-drawer-card { translate: var(--_in-x, 0px) var(--_in-y, 0px); opacity: 0; }
}
@supports (overlay: auto) {
  sg-drawer:is([data-skin="warm"], [data-skin="playful"]) dialog:not([open]) .sg-drawer-card { transition: translate var(--_dur) var(--_ease), opacity var(--_dur) var(--_ease); }
}

/* ── the panel's buttons, pressed (the touch contract): see dialog.css for the rule of it ── */
sg-drawer .sg-drawer-card button {
  transition: translate 120ms ease-out, scale 120ms var(--sg-ease-out, ease-out), box-shadow 100ms ease-out;
}
sg-drawer .sg-drawer-card button:active { transition: none; translate: 0 1px; box-shadow: inset 0 0 0 100vmax color-mix(in oklab, var(--sg-text, CanvasText) 8%, transparent); }
sg-drawer[data-skin="warm"] .sg-drawer-card button:active { translate: none; box-shadow: inset 0 0 0 100vmax color-mix(in oklab, var(--sg-text) 16%, transparent); }
sg-drawer[data-skin="playful"] .sg-drawer-card button { transition-timing-function: ease-out, var(--sg-ease-spring, ease-out), ease-out; }
sg-drawer[data-skin="playful"] .sg-drawer-card button:active { translate: none; scale: 0.97; box-shadow: inset 0 0 0 100vmax color-mix(in oklab, var(--sg-accent, Highlight) 20%, transparent); }

@media (prefers-reduced-motion: reduce) {
  sg-drawer dialog, sg-drawer dialog::backdrop, sg-drawer .sg-drawer-card { animation: none !important; transition: none !important; }
  sg-drawer .sg-drawer-card button:active { translate: none !important; scale: none !important; box-shadow: inset 0 0 0 100vmax color-mix(in oklab, var(--sg-text) 16%, transparent); }
}
:root[data-sg-input="key"] sg-drawer .sg-drawer-card button:active { translate: none !important; scale: none !important; }
