/* @svelte-lean/styles — toast (Tier 2; a popover="manual" region with a live list).
 * Markup: <section data-slean="toast" popover="manual" aria-label="Notifications"><ol
 * data-slean-part="list" aria-live="polite"></ol></section>; items are added by showToast():
 * <li data-slean-part="item" data-color="success" data-variant="filled"> with the alert's parts: title,
 * description, actions (a row with the action button) and close. Placement: data-slean-side=
 * "top|bottom" (default bottom) and data-slean-align="start|center|end" (default end), logical.
 * Color (data-color on the item): neutral (default) and the palette colors; a colored toast shows
 * the color's status icon (info for primary, secondary and info). Variant (data-variant):
 * standard (default, the soft tint), filled (the main color), outlined (a line, the surface). A
 * new item enters with @starting-style. */
@layer slean.reset, slean.tokens, slean.base, slean.components, slean.compositions, slean.utilities;

@layer slean.components {
	:where([data-slean='toast']) {
		inset: auto;
		inset-block-end: var(--slean-space-4);
		inset-inline-end: var(--slean-space-4);
		inline-size: min(24rem, calc(100vw - 2 * var(--slean-space-4)));
		max-block-size: calc(100dvh - 2 * var(--slean-space-4));
		margin: 0;
		padding: 0;
		border: 0;
		background: transparent;
		color: var(--slean-fg);
		color-scheme: var(--slean-color-scheme);
		overflow: visible;
	}

	:where([data-slean='toast'][data-slean-side='top']) {
		inset-block-start: var(--slean-space-4);
		inset-block-end: auto;
	}

	:where([data-slean='toast'][data-slean-align='start']) {
		inset-inline-start: var(--slean-space-4);
		inset-inline-end: auto;
	}

	:where([data-slean='toast'][data-slean-align='center']) {
		inset-inline: 0;
		margin-inline: auto;
	}

	:where([data-slean='toast'] [data-slean-part='list']) {
		display: grid;
		gap: var(--slean-space-2);
		margin: 0;
		padding: 0;
		list-style: none;
	}

	/*
	 * An item is an overlay in the current color's soft tint (standard), with the status icon of a
	 * colored toast before the title. Columns: the icon (a colored toast only), the text, the close
	 * button. The close button sits on the first line and is centred on it without making it
	 * taller, so a toast that is only a title is one line high.
	 */
	:where([data-slean='toast'] [data-slean-part='item']) {
		display: grid;
		grid-template-columns: minmax(0, 1fr) auto;
		align-items: start;
		column-gap: var(--slean-space-3);
		row-gap: var(--slean-space-1);
		padding-block: var(--slean-space-3);
		padding-inline: var(--slean-space-4) var(--slean-space-3);
		border: 1px solid color-mix(in oklch, var(--slean-color-main) 25%, transparent);
		border-radius: var(--slean-radius-md);
		background: var(--slean-color-soft);
		box-shadow: var(--slean-elevation-5);
		color: var(--slean-fg);
		font-size: var(--slean-text-sm);
		line-height: var(--slean-leading);
		transition:
			opacity var(--slean-duration-normal) var(--slean-ease),
			translate var(--slean-duration-normal) var(--slean-ease);
	}

	@starting-style {
		:where([data-slean='toast'] [data-slean-part='item']) {
			opacity: 0;
			translate: 0 var(--slean-space-2);
		}
	}

	:where([data-slean='toast'] [data-slean-part='item'][data-color]:not([data-color='neutral'])) {
		grid-template-columns: auto minmax(0, 1fr) auto;
		padding-inline-start: var(--slean-space-3);
	}

	:where(
		[data-slean='toast'] [data-slean-part='item'][data-color]:not([data-color='neutral'])
	)::before {
		content: '';
		grid-column: 1;
		grid-row: 1;
		inline-size: 1.125rem;
		block-size: 1.125rem;
		/* Centred on the title's first line. */
		margin-block: calc((var(--slean-leading) * 1em - 1.125rem) / 2);
		background: currentColor;
		color: var(--slean-color-soft-text);
		mask: var(--slean-icon-info) center / contain no-repeat;
	}

	:where([data-slean='toast'] [data-slean-part='item'][data-color='success'])::before {
		mask-image: var(--slean-icon-success);
	}

	:where([data-slean='toast'] [data-slean-part='item'][data-color='warning'])::before {
		mask-image: var(--slean-icon-warning);
	}

	:where([data-slean='toast'] [data-slean-part='item'][data-color='error'])::before {
		mask-image: var(--slean-icon-error);
	}

	/* Variants: filled is the main color with its contrast text; outlined is the overlay surface
	 * with a line in the color. */
	:where([data-slean='toast'] [data-slean-part='item'][data-variant='filled']) {
		border-color: transparent;
		background: var(--slean-color-main);
		color: var(--slean-color-contrast-text);
	}

	:where([data-slean='toast'] [data-slean-part='item'][data-variant='outlined']) {
		border-color: color-mix(in oklch, var(--slean-color-main) 60%, transparent);
		background: var(--slean-surface);
	}

	:where([data-slean='toast'] [data-slean-part='item'][data-variant='filled'])::before {
		color: inherit;
	}

	/* The text column: the first in a plain toast, the second beside the icon of a colored toast. */
	:where([data-slean='toast'] [data-slean-part='title']),
	:where([data-slean='toast'] [data-slean-part='description']),
	:where([data-slean='toast'] [data-slean-part='actions']) {
		grid-column: -3;
	}

	:where([data-slean='toast'] [data-slean-part='title']) {
		margin: 0;
		color: var(--slean-color-soft-text);
		font-weight: var(--slean-font-weight-semibold);
	}

	:where([data-slean='toast'] [data-slean-part='description']) {
		margin: 0;
		color: var(--slean-fg-muted);
	}

	:where([data-slean='toast'] [data-slean-part='actions']) {
		display: flex;
		flex-wrap: wrap;
		gap: var(--slean-space-2);
		margin-block-start: var(--slean-space-1);
	}

	:where([data-slean='toast'] [data-slean-part='actions'] > button) {
		padding-block: var(--slean-space-1);
		padding-inline: var(--slean-space-2);
		border: 1px solid var(--slean-border);
		border-radius: var(--slean-radius-sm);
		background: transparent;
		color: inherit;
		font-weight: var(--slean-font-weight-medium);
		cursor: pointer;
	}

	:where([data-slean='toast'] [data-slean-part='close']) {
		display: inline-grid;
		place-items: center;
		grid-column: -2;
		grid-row: 1;
		inline-size: var(--slean-control-height-sm);
		block-size: var(--slean-control-height-sm);
		/* Centred on the first line, and no taller than it. */
		margin-block: calc((var(--slean-leading) * 1em - var(--slean-control-height-sm)) / 2);
		margin-inline-end: calc(var(--slean-space-1) * -1);
		padding: 0;
		border: 0;
		border-radius: var(--slean-radius-sm);
		background: transparent;
		color: var(--slean-fg-muted);
		cursor: pointer;
	}

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

	/* A filled toast: the text, the close button and the action take the contrast text. */
	:where(
		[data-slean='toast'] [data-slean-part='item'][data-variant='filled'] [data-slean-part='title']
	),
	:where(
		[data-slean='toast']
			[data-slean-part='item'][data-variant='filled']
			[data-slean-part='description']
	),
	:where(
		[data-slean='toast'] [data-slean-part='item'][data-variant='filled'] [data-slean-part='close']
	),
	:where(
		[data-slean='toast']
			[data-slean-part='item'][data-variant='filled']
			[data-slean-part='actions']
			> button
	) {
		color: inherit;
	}

	:where([data-slean='toast'] [data-slean-part='close']:active) {
		background: color-mix(in oklch, currentColor 18%, transparent);
	}

	/* On a filled toast the theme's focus ring may vanish into the main color: the close button's
	 * ring takes the contrast text. */
	:where(
		[data-slean='toast']
			[data-slean-part='item'][data-variant='filled']
			[data-slean-part='close']:focus-visible
	) {
		outline-color: currentColor;
	}

	@media (hover: hover) {
		/* A tint of the button's own color, so the hover reads on every variant (the soft tint, the
		 * main color, the surface). */
		:where([data-slean='toast'] [data-slean-part='close']:hover) {
			background: color-mix(in oklch, currentColor 10%, transparent);
			color: var(--slean-fg);
		}

		:where(
			[data-slean='toast']
				[data-slean-part='item'][data-variant='filled']
				[data-slean-part='close']:hover
		) {
			color: inherit;
		}

		:where([data-slean='toast'] [data-slean-part='actions'] > button:hover) {
			background: var(--slean-muted);
		}
	}

	@media (forced-colors: active) {
		:where([data-slean='toast'] [data-slean-part='item']) {
			border-color: CanvasText;
		}

		:where([data-slean='toast'] [data-slean-part='close'])::before,
		:where(
			[data-slean='toast'] [data-slean-part='item'][data-color]:not([data-color='neutral'])
		)::before {
			forced-color-adjust: none;
			background: ButtonText;
		}
	}
}
