/* @svelte-lean/styles — rating (Tier 0, native <input type="radio"> sharing a name).
 * Markup: <fieldset data-slean="rating"><legend>…</legend>
 *   <label><input type="radio" name="…" value="1"> <svg aria-hidden="true">…</svg>
 *   <span>1 star</span></label> …</fieldset>.
 * Each radio covers its star, transparent and focusable; the text span is visually hidden and
 * names the radio. The checked star and every star before it are filled
 * (label:has(:checked), label:has(~ label :checked)); under (hover: hover) the hovered star and
 * the ones before it preview the value. A label without an svg is the "no rating" option, drawn
 * as the close icon. The browser owns selection, the arrow keys and the form value.
 * Color: data-color on the root or an ancestor (primary by default). Sizes (data-size): small,
 * medium (default), large. */
@layer slean.reset, slean.tokens, slean.base, slean.components, slean.compositions, slean.utilities;

@layer slean.components {
	:where([data-slean='rating']) {
		display: inline-flex;
		align-items: center;
		min-inline-size: 0;
		margin: 0;
		padding: 0;
		border: 0;
	}

	:where([data-slean='rating'] legend) {
		padding: 0;
		margin-block-end: var(--slean-space-1);
		color: var(--slean-fg);
		font-size: var(--slean-text-sm);
		font-weight: var(--slean-font-weight-medium);
	}

	:where([data-slean='rating'] label) {
		position: relative;
		display: inline-grid;
		place-items: center;
		flex: none;
		inline-size: var(--slean-control-height-sm);
		block-size: var(--slean-control-height-sm);
		border-radius: var(--slean-radius-sm);
		color: var(--slean-fg-muted);
		cursor: pointer;
		-webkit-tap-highlight-color: transparent;
	}

	:where([data-slean='rating'] input[type='radio']) {
		appearance: none;
		position: absolute;
		inset: 0;
		margin: 0;
		opacity: 0;
		cursor: inherit;
	}

	:where([data-slean='rating'] label > span) {
		position: absolute;
		inline-size: 1px;
		block-size: 1px;
		overflow: hidden;
		clip-path: inset(50%);
		white-space: nowrap;
	}

	:where([data-slean='rating'] svg) {
		inline-size: 1.375rem;
		block-size: 1.375rem;
		overflow: visible;
		fill: var(--slean-muted);
		stroke: var(--slean-border-strong);
		stroke-width: 1.5;
		stroke-linejoin: round;
		pointer-events: none;
		transition:
			fill var(--slean-duration-fast) var(--slean-ease),
			stroke var(--slean-duration-fast) var(--slean-ease);
	}

	/* The value: the checked star and every star before it. */
	:where([data-slean='rating'] label:has(input:checked) svg),
	:where([data-slean='rating'] label:has(~ label input:checked) svg) {
		fill: var(--slean-color-main);
		stroke: var(--slean-color-main);
	}

	/* Sizes: the star and its target (MUI's 18px, 22px and 30px stars). */
	:where([data-slean='rating'][data-size='small'] label) {
		inline-size: var(--slean-control-height-xs);
		block-size: var(--slean-control-height-xs);
	}

	:where([data-slean='rating'][data-size='small'] svg) {
		inline-size: 1.125rem;
		block-size: 1.125rem;
	}

	:where([data-slean='rating'][data-size='large'] label) {
		inline-size: var(--slean-control-height-md);
		block-size: var(--slean-control-height-md);
	}

	:where([data-slean='rating'][data-size='large'] svg) {
		inline-size: 1.875rem;
		block-size: 1.875rem;
	}

	/* The "no rating" option: a label without a star, drawn as the close icon. */
	:where([data-slean='rating'] label:not(:has(svg)))::before {
		content: '';
		inline-size: 1rem;
		block-size: 1rem;
		background: currentColor;
		mask: var(--slean-icon-close) center / contain no-repeat;
	}

	/* Hover preview: while a star is hovered, it and the stars before it show the value it
	 * would choose, and the others empty. */
	@media (hover: hover) {
		:where([data-slean='rating']:not(:disabled):has(label:hover) svg) {
			fill: var(--slean-muted);
			stroke: var(--slean-border-strong);
		}

		:where([data-slean='rating']:not(:disabled) label:hover svg),
		:where([data-slean='rating']:not(:disabled) label:has(~ label:hover) svg) {
			fill: var(--slean-color-dark);
			stroke: var(--slean-color-dark);
		}

		:where([data-slean='rating']:not(:disabled) label:not(:has(svg)):hover) {
			color: var(--slean-fg);
		}
	}

	:where([data-slean='rating'] label:has(input:focus-visible)) {
		outline: var(--slean-focus-ring);
		outline-offset: calc(-1 * var(--slean-focus-ring-width));
	}

	:where([data-slean='rating']:disabled label) {
		opacity: 0.5;
		cursor: not-allowed;
	}

	@media (pointer: coarse) {
		:where([data-slean='rating'] label) {
			inline-size: var(--slean-control-height-md);
			block-size: var(--slean-control-height-md);
		}
	}

	@media (forced-colors: active) {
		:where([data-slean='rating'] svg) {
			forced-color-adjust: none;
			fill: Canvas;
			stroke: CanvasText;
		}

		:where([data-slean='rating'] label:has(input:checked) svg),
		:where([data-slean='rating'] label:has(~ label input:checked) svg) {
			fill: Highlight;
			stroke: Highlight;
		}

		:where([data-slean='rating'] label:not(:has(svg)))::before {
			forced-color-adjust: none;
			background: CanvasText;
		}

		:where([data-slean='rating']:disabled label) {
			color: GrayText;
			opacity: 1;
		}

		:where([data-slean='rating']:disabled label:not(:has(svg)))::before {
			background: GrayText;
		}

		:where([data-slean='rating']:disabled svg) {
			stroke: GrayText;
		}

		:where([data-slean='rating']:disabled label:has(input:checked) svg),
		:where([data-slean='rating']:disabled label:has(~ label input:checked) svg) {
			fill: GrayText;
			stroke: GrayText;
		}
	}
}
