/* @svelte-lean/styles — alert (Tier 0, a message block; role="status" or role="alert" only for
 * a message the application writes after the page loaded).
 * Markup: <div data-slean="alert" data-color="warning"> <span data-slean-part="icon"
 * aria-hidden="true"></span> <p data-slean-part="title">…</p> <p data-slean-part="description">…
 * </p> <div data-slean-part="actions">…</div> <button type="button" data-slean-part="close"
 * aria-label="Dismiss"></button> </div>. Every part is optional and a child of the root; an empty
 * root draws nothing, so it can wait in the page as a live region. Color (data-color): neutral
 * (default), primary, secondary, info, success, warning, error. Variant (data-variant): standard
 * (default, the soft tint), filled (the main color), outlined (a line, no fill). An empty icon part
 * shows the status icon of the color (--slean-icon-success|warning|error, the info icon for the
 * others), so the color does not rest on color alone; an icon part with content (an author's SVG)
 * is only sized. The close part sits at the end of the first line; empty, it draws the close icon
 * (--slean-icon-close). Columns: the icon, the content, the close button (an empty column when
 * there is none). Parts are matched as children of the root, so a nested primitive's parts are
 * never reached and a large page pays nothing for them. */
@layer slean.reset, slean.tokens, slean.base, slean.components, slean.compositions, slean.utilities;

@layer slean.components {
	:where([data-slean='alert']) {
		display: grid;
		grid-template-columns: auto minmax(0, 1fr) auto;
		row-gap: var(--slean-space-1);
		padding-block: var(--slean-space-3);
		padding-inline: var(--slean-space-4);
		border: 1px solid color-mix(in oklch, var(--slean-color-main) 30%, transparent);
		border-radius: var(--slean-radius-md);
		background: var(--slean-color-soft);
		color: var(--slean-fg);
		font-size: var(--slean-text-sm);
		line-height: var(--slean-leading);
	}

	/* An empty alert draws nothing: the root can stay in the page as the live region and show its
	 * box when the application writes a message into it. */
	:where([data-slean='alert']:empty) {
		padding: 0;
		border-width: 0;
	}

	/* Content goes in the second column; the icon, when present, takes the first and the close
	 * button the third. */
	:where([data-slean='alert'] > *) {
		grid-column: 2;
		margin: 0;
	}

	:where([data-slean='alert'] > [data-slean-part='title']) {
		font-weight: var(--slean-font-weight-semibold);
	}

	:where([data-slean='alert'] > [data-slean-part='actions']) {
		display: flex;
		flex-wrap: wrap;
		align-items: center;
		gap: var(--slean-space-2);
		margin-block-start: var(--slean-space-2);
	}

	:where([data-slean='alert'] a:not([data-slean])) {
		color: inherit;
		font-weight: var(--slean-font-weight-medium);
		text-decoration-line: underline;
		text-underline-offset: 0.2em;
	}

	/* Icon: an empty part shows the status icon of the color (info for neutral, primary, secondary
	 * and info), masked in the text color of the variant, so the color does not rest on color
	 * alone; a part with content (an author's SVG) is only sized. */
	:where([data-slean='alert'] > [data-slean-part='icon']) {
		grid-column: 1;
		grid-row: 1;
		display: block;
		inline-size: 1.125rem;
		block-size: 1.125rem;
		margin-block-start: 0.125rem;
		margin-inline-end: var(--slean-space-3);
	}

	:where([data-slean='alert'] > [data-slean-part='icon'] > svg) {
		inline-size: 100%;
		block-size: 100%;
	}

	:where([data-slean='alert'] > [data-slean-part='icon']:empty) {
		background: currentColor;
		mask: var(--slean-icon-info) center / contain no-repeat;
	}

	:where([data-slean='alert'][data-color='success'] > [data-slean-part='icon']:empty) {
		mask-image: var(--slean-icon-success);
	}

	:where([data-slean='alert'][data-color='warning'] > [data-slean-part='icon']:empty) {
		mask-image: var(--slean-icon-warning);
	}

	:where([data-slean='alert'][data-color='error'] > [data-slean-part='icon']:empty) {
		mask-image: var(--slean-icon-error);
	}

	/* Close: a button at the end of the first line, centred on it without making it taller, so
	 * an alert that is only a title stays one line high. It takes the text color of the variant;
	 * the application removes the alert when it is pressed. */
	:where([data-slean='alert'] > [data-slean-part='close']) {
		display: inline-grid;
		place-items: center;
		grid-column: 3;
		grid-row: 1;
		inline-size: var(--slean-control-height-sm);
		block-size: var(--slean-control-height-sm);
		margin-block: calc((var(--slean-leading) * 1em - var(--slean-control-height-sm)) / 2);
		margin-inline: var(--slean-space-2) calc(var(--slean-space-2) * -1);
		padding: 0;
		border: 0;
		border-radius: var(--slean-radius-sm);
		background: transparent;
		color: inherit;
		font: inherit;
		cursor: pointer;
	}

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

	:where([data-slean='alert'] > [data-slean-part='close'] > svg) {
		inline-size: 1rem;
		block-size: 1rem;
	}

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

	@media (hover: hover) {
		:where([data-slean='alert'] > [data-slean-part='close']:hover) {
			background: color-mix(in oklch, currentColor 10%, transparent);
		}
	}

	/* Variants, drawn from the current color: standard (above) puts the title and the icon in the
	 * soft text; filled is the main color with its contrast text; outlined is a line, no fill. */
	:where([data-slean='alert'] > [data-slean-part='title']),
	:where([data-slean='alert'] > [data-slean-part='icon']) {
		color: var(--slean-color-soft-text);
	}

	:where([data-slean='alert'][data-variant='filled']) {
		border-color: transparent;
		background: var(--slean-color-main);
		color: var(--slean-color-contrast-text);
	}

	:where([data-slean='alert'][data-variant='filled'] > [data-slean-part='title']),
	:where([data-slean='alert'][data-variant='filled'] > [data-slean-part='icon']) {
		color: inherit;
	}

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

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

	/* Forced colors: the surface becomes Canvas, so the border carries the box; the masked icons
	 * keep CanvasText and ButtonText. */
	@media (forced-colors: active) {
		:where([data-slean='alert']) {
			border-color: CanvasText;
		}

		:where([data-slean='alert'] > [data-slean-part='icon']:empty) {
			forced-color-adjust: none;
			background: CanvasText;
		}

		:where([data-slean='alert'] > [data-slean-part='close']:empty)::before {
			forced-color-adjust: none;
			background: ButtonText;
		}
	}
}
