/* @svelte-lean/styles — dialog (Tier 0, native <dialog>), in three shapes (ADR 0011 §1.4):
 * centered by default; a drawer with data-slean-side="right|left|top|bottom" (physical edges);
 * an alert dialog with role="alertdialog" (narrower, the question and its answers).
 * Structural parts are matched by element first (ADR 0011 §2.4): a child <header> (or one inside
 * a child <form method="dialog">) is the header, its first heading the title, a <p> right after
 * it the description, a child <footer> the actions; data-slean-part names an element the
 * structure does not tell. The dialog scrolls when its content is taller than the viewport
 * allows, with header and footer sticky; data-slean-part="close" is an icon-only button drawn
 * at the top end corner, sticky too. Sizes: data-size="small|medium|large" (the width).
 * Open state is the native [open] attribute; the entrance uses @starting-style, the exit keeps
 * the element displayed and in the top layer through transition-behavior: allow-discrete on
 * display and overlay. Durations are tokens, zero under prefers-reduced-motion: reduce. */
@layer slean.reset, slean.tokens, slean.base, slean.components, slean.compositions, slean.utilities;

@layer slean.components {
	:where([data-slean='dialog']) {
		/* The layout holds while it is open and while it animates closed: display stays flex
		   until the exit transition ends (allow-discrete), so the direction is set closed too. */
		flex-direction: column;
		box-sizing: border-box;
		inset: 0;
		margin: auto;
		inline-size: min(100% - var(--slean-space-8), 32rem);
		max-block-size: calc(100% - var(--slean-space-8));
		padding: var(--slean-dialog-padding);
		gap: var(--slean-dialog-gap);
		overflow: auto;
		overscroll-behavior: contain;
		border: 1px solid color-mix(in oklch, var(--slean-border) 55%, transparent);
		border-radius: var(--slean-dialog-radius);
		background: var(--slean-surface);
		color: var(--slean-fg);
		box-shadow: var(--slean-dialog-elevation);
		font-family: var(--slean-font-sans);
		font-size: var(--slean-typography-body1-size);
		font-weight: var(--slean-typography-body1-weight);
		line-height: var(--slean-typography-body1-line-height);
		opacity: 0;
		scale: 0.96;
		transition:
			opacity var(--slean-duration-normal) var(--slean-ease),
			scale var(--slean-duration-normal) var(--slean-ease),
			display var(--slean-duration-normal) allow-discrete,
			overlay var(--slean-duration-normal) allow-discrete;
	}

	/* At rest the scale is none, not 1: any other value makes the open dialog the containing
	 * block of its position: fixed descendants, and a drag preview or a fixed layer placed with
	 * viewport coordinates would land offset by the dialog's own position. The entrance still
	 * animates from 0.96 (none interpolates as 1). */
	:where([data-slean='dialog'][open]) {
		display: flex;
		opacity: 1;
		scale: none;
	}

	@starting-style {
		:where([data-slean='dialog'][open]) {
			opacity: 0;
			scale: 0.96;
		}
	}

	:where([data-slean='dialog'])::backdrop {
		background: var(--slean-overlay);
		opacity: 0;
		transition:
			opacity var(--slean-duration-normal) var(--slean-ease),
			display var(--slean-duration-normal) allow-discrete,
			overlay var(--slean-duration-normal) allow-discrete;
	}

	:where([data-slean='dialog'][open])::backdrop {
		opacity: 1;
	}

	@starting-style {
		:where([data-slean='dialog'][open])::backdrop {
			opacity: 0;
		}
	}

	/* A <form method="dialog"> around the content keeps header and footer as the dialog's own. */
	:where([data-slean='dialog'] > form) {
		display: contents;
	}

	/* Header: sticky at the top, over the scrolling content, bleeding into the dialog's padding. */
	:where([data-slean='dialog'] > header),
	:where([data-slean='dialog'] > form > header),
	:where([data-slean='dialog'] > [data-slean-part='header']),
	:where([data-slean='dialog'] > form > [data-slean-part='header']) {
		/* Sticky insets count from the scroll container's content box: minus its padding is its
		 * padding edge. */
		position: sticky;
		inset-block-start: calc(-1 * var(--slean-dialog-padding));
		z-index: 1;
		display: flex;
		flex: none;
		flex-direction: column;
		gap: var(--slean-space-1);
		margin: calc(-1 * var(--slean-dialog-padding)) calc(-1 * var(--slean-dialog-padding)) 0;
		padding: var(--slean-dialog-padding) var(--slean-dialog-padding) var(--slean-space-4);
		background: inherit;
		border-start-start-radius: inherit;
		border-start-end-radius: inherit;
	}

	:where([data-slean='dialog'] > header > h1:first-child),
	:where([data-slean='dialog'] > header > h2:first-child),
	:where([data-slean='dialog'] > header > h3:first-child),
	:where([data-slean='dialog'] > header > h4:first-child),
	:where([data-slean='dialog'] > header > h5:first-child),
	:where([data-slean='dialog'] > header > h6:first-child),
	:where([data-slean='dialog'] > form > header > h1:first-child),
	:where([data-slean='dialog'] > form > header > h2:first-child),
	:where([data-slean='dialog'] > form > header > h3:first-child),
	:where([data-slean='dialog'] > form > header > h4:first-child),
	:where([data-slean='dialog'] > form > header > h5:first-child),
	:where([data-slean='dialog'] > form > header > h6:first-child),
	:where(
		[data-slean='dialog']
			[data-slean-part='title']:not(
				[data-slean='dialog'] [data-slean]:not([data-slean='dialog']) [data-slean-part]
			)
	) {
		margin: 0;
		font-family: var(--slean-font-sans);
		font-size: var(--slean-typography-h6-size);
		font-weight: var(--slean-typography-h6-weight);
		line-height: var(--slean-typography-h6-line-height);
	}

	:where([data-slean='dialog'] > header > :is(h1, h2, h3, h4, h5, h6):first-child + p),
	:where([data-slean='dialog'] > form > header > :is(h1, h2, h3, h4, h5, h6):first-child + p),
	:where(
		[data-slean='dialog']
			[data-slean-part='description']:not(
				[data-slean='dialog'] [data-slean]:not([data-slean='dialog']) [data-slean-part]
			)
	) {
		margin: 0;
		color: var(--slean-fg-muted);
		font-family: var(--slean-font-sans);
		font-size: var(--slean-typography-body2-size);
		font-weight: var(--slean-typography-body2-weight);
		line-height: var(--slean-typography-body2-line-height);
	}

	/* Footer: the actions at the inline end; sticky at the bottom, and at the bottom of a drawer
	 * whose content is short (margin-block-start: auto). */
	:where([data-slean='dialog'] > footer),
	:where([data-slean='dialog'] > form > footer),
	:where([data-slean='dialog'] > [data-slean-part='footer']),
	:where([data-slean='dialog'] > form > [data-slean-part='footer']) {
		position: sticky;
		inset-block-end: calc(-1 * var(--slean-dialog-padding));
		z-index: 1;
		display: flex;
		flex: none;
		flex-wrap: wrap;
		align-items: center;
		justify-content: flex-end;
		gap: var(--slean-space-2);
		margin: auto calc(-1 * var(--slean-dialog-padding)) calc(-1 * var(--slean-dialog-padding));
		padding: var(--slean-dialog-padding);
		background: inherit;
		border-end-start-radius: inherit;
		border-end-end-radius: inherit;
	}

	/* The close affordance: an icon-only text button at the top end corner, for example
	 * <button type="button" data-slean-part="close" command="close" commandfor="…"
	 * aria-label="Close"></button>, written last so it is not the first tab stop. It is drawn
	 * first (order) and sticky, so it stays in the corner while the dialog scrolls; it takes no
	 * room (a negative block-end margin that also cancels the dialog's gap) and the header leaves
	 * room for it. Its distance from the corner is space-4, or the dialog's padding when that is
	 * smaller; neither depends on the gap. */
	:where([data-slean='dialog'] > [data-slean-part='close']),
	:where([data-slean='dialog'] > form > [data-slean-part='close']) {
		appearance: none;
		position: sticky;
		inset-block-start: calc(
			min(var(--slean-space-4), var(--slean-dialog-padding)) - var(--slean-dialog-padding)
		);
		z-index: 2;
		order: -1;
		display: inline-grid;
		flex: none;
		place-items: center;
		align-self: flex-end;
		inline-size: var(--slean-control-height-sm);
		block-size: var(--slean-control-height-sm);
		margin: calc(
				min(var(--slean-space-4), var(--slean-dialog-padding)) - var(--slean-dialog-padding)
			)
			calc(min(var(--slean-space-4), var(--slean-dialog-padding)) - var(--slean-dialog-padding))
			calc(
				-1 * var(--slean-control-height-sm) -
					min(var(--slean-space-4), var(--slean-dialog-padding)) + var(--slean-dialog-padding) -
					var(--slean-dialog-gap)
			)
			0;
		padding: 0;
		border: 0;
		border-radius: var(--slean-radius-full);
		background: transparent;
		color: var(--slean-fg-muted);
		cursor: pointer;
		-webkit-tap-highlight-color: transparent;
		transition:
			background-color var(--slean-duration-fast) var(--slean-ease),
			color var(--slean-duration-fast) var(--slean-ease);
	}

	:where([data-slean='dialog'] > [data-slean-part='close'])::before,
	:where([data-slean='dialog'] > form > [data-slean-part='close'])::before {
		content: '';
		inline-size: 1.125rem;
		block-size: 1.125rem;
		background: currentColor;
		mask: var(--slean-icon-close) center / contain no-repeat;
	}

	@media (hover: hover) {
		:where([data-slean='dialog'] > [data-slean-part='close']:hover),
		:where([data-slean='dialog'] > form > [data-slean-part='close']:hover) {
			background: var(--slean-muted);
			color: var(--slean-fg);
		}
	}

	:where([data-slean='dialog'] > [data-slean-part='close']:focus-visible),
	:where([data-slean='dialog'] > form > [data-slean-part='close']:focus-visible) {
		outline: var(--slean-focus-ring);
		outline-offset: var(--slean-focus-ring-offset);
	}

	:where(
		[data-slean='dialog']:has(> [data-slean-part='close'], > form > [data-slean-part='close'])
			> header
	),
	:where(
		[data-slean='dialog']:has(> [data-slean-part='close'], > form > [data-slean-part='close'])
			> [data-slean-part='header']
	),
	:where(
		[data-slean='dialog']:has(> [data-slean-part='close'], > form > [data-slean-part='close'])
			> form
			> header
	),
	:where(
		[data-slean='dialog']:has(> [data-slean-part='close'], > form > [data-slean-part='close'])
			> form
			> [data-slean-part='header']
	) {
		padding-inline-end: calc(
			min(var(--slean-space-4), var(--slean-dialog-padding)) + var(--slean-control-height-sm)
		);
	}

	/* Sizes of a centered dialog. */
	:where([data-slean='dialog'][data-size='small']) {
		inline-size: min(100% - var(--slean-space-8), 24rem);
	}

	:where([data-slean='dialog'][data-size='large']) {
		inline-size: min(100% - var(--slean-space-8), 48rem);
	}

	/* Alert dialog: narrower; the question reads as body text, not as a muted caption. */
	:where([data-slean='dialog'][role='alertdialog']:not([data-size])) {
		inline-size: min(100% - var(--slean-space-8), 28rem);
	}

	:where([data-slean='dialog'][role='alertdialog'] > header),
	:where([data-slean='dialog'][role='alertdialog'] > form > header),
	:where([data-slean='dialog'][role='alertdialog'] > [data-slean-part='header']) {
		gap: var(--slean-space-2);
	}

	:where(
		[data-slean='dialog'][role='alertdialog'] > header > :is(h1, h2, h3, h4, h5, h6):first-child + p
	),
	:where(
		[data-slean='dialog'][role='alertdialog']
			> form
			> header
			> :is(h1, h2, h3, h4, h5, h6):first-child
			+ p
	) {
		font-family: var(--slean-font-sans);
		font-size: var(--slean-typography-body1-size);
		font-weight: var(--slean-typography-body1-weight);
		line-height: var(--slean-typography-body1-line-height);
	}

	/* Drawer: a modal dialog at an edge. Side panels take the full block size, top and bottom
	 * sheets the full inline size; it slides in from its edge. The side is physical, so the edge
	 * and the slide use physical properties; everything inside uses logical ones. */
	:where([data-slean='dialog'][data-slean-side]) {
		position: fixed;
		inset: 0 0 0 auto;
		inline-size: min(100% - var(--slean-space-8), 24rem);
		block-size: auto;
		max-inline-size: none;
		max-block-size: none;
		margin: 0;
		border: 0;
		border-radius: 0;
		opacity: 1;
		scale: none;
		translate: 100% 0;
		transition:
			translate var(--slean-duration-slow) var(--slean-ease),
			display var(--slean-duration-slow) allow-discrete,
			overlay var(--slean-duration-slow) allow-discrete;
	}

	:where([data-slean='dialog'][data-slean-side])::backdrop {
		transition:
			opacity var(--slean-duration-slow) var(--slean-ease),
			display var(--slean-duration-slow) allow-discrete,
			overlay var(--slean-duration-slow) allow-discrete;
	}

	:where([data-slean='dialog'][data-slean-side='right']) {
		padding-block: max(var(--slean-dialog-padding), env(safe-area-inset-top))
			max(var(--slean-dialog-padding), env(safe-area-inset-bottom));
		padding-right: max(var(--slean-dialog-padding), env(safe-area-inset-right));
		border-left: 1px solid var(--slean-border);
	}

	:where([data-slean='dialog'][data-slean-side='left']) {
		inset: 0 auto 0 0;
		padding-block: max(var(--slean-dialog-padding), env(safe-area-inset-top))
			max(var(--slean-dialog-padding), env(safe-area-inset-bottom));
		padding-left: max(var(--slean-dialog-padding), env(safe-area-inset-left));
		border-right: 1px solid var(--slean-border);
		translate: -100% 0;
	}

	:where([data-slean='dialog'][data-slean-side='left'][data-size='small']),
	:where([data-slean='dialog'][data-slean-side='right'][data-size='small']) {
		inline-size: min(100% - var(--slean-space-8), 20rem);
	}

	:where([data-slean='dialog'][data-slean-side='left'][data-size='large']),
	:where([data-slean='dialog'][data-slean-side='right'][data-size='large']) {
		inline-size: min(100% - var(--slean-space-8), 36rem);
	}

	:where([data-slean='dialog'][data-slean-side='top']),
	:where([data-slean='dialog'][data-slean-side='bottom']) {
		inline-size: auto;
		max-block-size: min(100% - var(--slean-space-8), 32rem);
	}

	:where([data-slean='dialog'][data-slean-side='top']) {
		inset: 0 0 auto;
		padding-top: max(var(--slean-dialog-padding), env(safe-area-inset-top));
		padding-inline: max(var(--slean-dialog-padding), env(safe-area-inset-left))
			max(var(--slean-dialog-padding), env(safe-area-inset-right));
		border-bottom: 1px solid var(--slean-border);
		border-end-start-radius: var(--slean-dialog-radius);
		border-end-end-radius: var(--slean-dialog-radius);
		translate: 0 -100%;
	}

	:where([data-slean='dialog'][data-slean-side='bottom']) {
		inset: auto 0 0;
		padding-bottom: max(var(--slean-dialog-padding), env(safe-area-inset-bottom));
		padding-inline: max(var(--slean-dialog-padding), env(safe-area-inset-left))
			max(var(--slean-dialog-padding), env(safe-area-inset-right));
		border-top: 1px solid var(--slean-border);
		border-start-start-radius: var(--slean-dialog-radius);
		border-start-end-radius: var(--slean-dialog-radius);
		translate: 0 100%;
	}

	/* translate: none at rest, not 0 0: see the centred dialog's scale. */
	:where([data-slean='dialog'][data-slean-side][open]) {
		opacity: 1;
		scale: none;
		translate: none;
	}

	@starting-style {
		:where([data-slean='dialog'][data-slean-side='right'][open]) {
			opacity: 1;
			translate: 100% 0;
		}

		:where([data-slean='dialog'][data-slean-side='left'][open]) {
			opacity: 1;
			translate: -100% 0;
		}

		:where([data-slean='dialog'][data-slean-side='top'][open]) {
			opacity: 1;
			translate: 0 -100%;
		}

		:where([data-slean='dialog'][data-slean-side='bottom'][open]) {
			opacity: 1;
			translate: 0 100%;
		}
	}

	/* A drawer's footer is separated by a line, as a toolbar at its edge. */
	:where([data-slean='dialog'][data-slean-side] > footer),
	:where([data-slean='dialog'][data-slean-side] > form > footer),
	:where([data-slean='dialog'][data-slean-side] > [data-slean-part='footer']) {
		padding-block: var(--slean-space-4);
		border-block-start: 1px solid var(--slean-border);
	}

	@media (forced-colors: active) {
		:where([data-slean='dialog'] > [data-slean-part='close'])::before,
		:where([data-slean='dialog'] > form > [data-slean-part='close'])::before {
			forced-color-adjust: none;
			background: ButtonText;
		}
	}
}
