/* @svelte-lean/styles — file drop (Tier 1; the drag mark, the checks and the file list come from
 * @svelte-lean/primitives/file-drop). Markup: <div data-slean="file-drop"> around an
 * <input type="file" data-slean-part="input">, the instruction text, an optional
 * <output data-slean-part="output"> and an optional <ul data-slean-part="list">. The drop area is
 * the root, or the area part around the input when the root also holds a list: the input is
 * stretched over it, transparent, so a dropped file lands on it and a click opens the picker. An
 * empty icon part in the area draws the upload icon. The behavior writes one item per file into
 * the list: media (a thumbnail) or icon (the type mark), title, description (the size) and remove.
 * Without scripting the input is shown as it is, with the platform's own file name text.
 * States: data-slean-dragging during a drag, data-slean-full at the file limit,
 * data-slean-rejected on the root and on a refused item, :focus-visible, :disabled and
 * :user-invalid on the input, aria-busy on an item the application is uploading.
 * Variants: none (a dashed area, the list below it as rows); "picture-card" and
 * "picture-circle" (square or round tiles, the area being the last tile of the list).
 * Color: data-color on the root or an ancestor colors the drag state (primary by default). */
@layer slean.reset, slean.tokens, slean.base, slean.components, slean.compositions, slean.utilities;

@layer slean.components {
	:where([data-slean='file-drop']) {
		position: relative;
		display: grid;
		gap: var(--slean-space-3);
		color: var(--slean-fg-muted);
		font-size: var(--slean-text-sm);
	}

	/* The drop area: the root itself, or its area part. */
	:where([data-slean='file-drop']:not(:has([data-slean-part='area']))),
	:where([data-slean='file-drop'] [data-slean-part='area']) {
		position: relative;
		display: grid;
		place-items: center;
		align-content: center;
		gap: var(--slean-space-2);
		min-block-size: 8rem;
		padding: var(--slean-space-5);
		border: 1.5px dashed var(--slean-border-strong);
		border-radius: var(--slean-radius-lg);
		background: var(--slean-surface);
		text-align: center;
		transition:
			border-color var(--slean-duration-fast) var(--slean-ease),
			background-color var(--slean-duration-fast) var(--slean-ease);
	}

	:where([data-slean='file-drop'] p) {
		margin: 0;
	}

	/* The upload icon: an empty icon part in the drop area. */
	:where([data-slean='file-drop'] [data-slean-part='area'] > [data-slean-part='icon']),
	:where([data-slean='file-drop'] > [data-slean-part='icon']) {
		inline-size: 2rem;
		block-size: 2rem;
		background: currentColor;
		mask: var(--slean-icon-upload) center / contain no-repeat;
	}

	:where([data-slean='file-drop'] [data-slean-part='output']) {
		color: var(--slean-fg);
		font-weight: var(--slean-font-weight-medium);
		overflow-wrap: anywhere;
		white-space: pre-line;
	}

	:where([data-slean='file-drop'] [data-slean-part='output']:empty) {
		display: none;
	}

	/* With a list the list shows the files: the output stays for assistive technology, which it
	 * tells about each choice and refusal. */
	:where([data-slean='file-drop']:has([data-slean-part='list']) [data-slean-part='output']) {
		position: absolute;
		inline-size: 1px;
		block-size: 1px;
		overflow: hidden;
		clip-path: inset(50%);
		white-space: nowrap;
	}

	@media (scripting: enabled) {
		:where([data-slean='file-drop'] [data-slean-part='input']) {
			position: absolute;
			inset: 0;
			inline-size: 100%;
			block-size: 100%;
			margin: 0;
			opacity: 0;
			cursor: pointer;
		}

		:where([data-slean='file-drop'] [data-slean-part='input']:disabled) {
			cursor: not-allowed;
		}
	}

	:where([data-slean='file-drop'] [data-slean-part='input']:focus-visible) {
		outline-style: none;
	}

	:where(
		[data-slean='file-drop']:not(:has([data-slean-part='area'])):has(
				[data-slean-part='input']:focus-visible
			)
	),
	:where(
		[data-slean='file-drop'] [data-slean-part='area']:has([data-slean-part='input']:focus-visible)
	) {
		outline: var(--slean-focus-ring);
		outline-offset: var(--slean-focus-ring-offset);
	}

	@media (hover: hover) {
		:where(
			[data-slean='file-drop']:not(:has([data-slean-part='area'])):has(
					[data-slean-part='input']:enabled:hover
				)
		),
		:where(
			[data-slean='file-drop'] [data-slean-part='area']:has([data-slean-part='input']:enabled:hover)
		) {
			border-color: var(--slean-fg-muted);
		}
	}

	:where([data-slean='file-drop'][data-slean-dragging]:not(:has([data-slean-part='area']))),
	:where([data-slean='file-drop'][data-slean-dragging] [data-slean-part='area']) {
		border-color: var(--slean-color-main);
		border-style: solid;
		background: var(--slean-color-soft);
		color: var(--slean-color-soft-text);
	}

	:where(
		[data-slean='file-drop']:not(:has([data-slean-part='area'])):has(
				[data-slean-part='input']:user-invalid
			)
	),
	:where(
		[data-slean='file-drop'] [data-slean-part='area']:has([data-slean-part='input']:user-invalid)
	) {
		border-color: var(--slean-error-main);
	}

	:where([data-slean='file-drop']:has([data-slean-part='input']:disabled)) {
		opacity: 0.5;
	}

	/* The list: one row per file. */
	:where([data-slean='file-drop'] [data-slean-part='list']) {
		display: grid;
		gap: var(--slean-space-2);
		margin: 0;
		padding: 0;
		list-style: none;
	}

	:where([data-slean='file-drop'] [data-slean-part='list']:empty) {
		display: none;
	}

	:where([data-slean='file-drop'] [data-slean-part='item']) {
		display: grid;
		grid-template-columns: auto minmax(0, 1fr) auto;
		grid-template-areas:
			'media title remove'
			'media description remove';
		column-gap: var(--slean-space-3);
		align-items: center;
		padding: var(--slean-space-2);
		border: 1px solid var(--slean-border);
		border-radius: var(--slean-radius-md);
		background: var(--slean-surface);
		color: var(--slean-fg);
		text-align: start;
	}

	:where([data-slean='file-drop'] [data-slean-part='item'] > [data-slean-part='media']),
	:where([data-slean='file-drop'] [data-slean-part='item'] > [data-slean-part='icon']) {
		grid-area: media;
		inline-size: 2.5rem;
		block-size: 2.5rem;
		border-radius: var(--slean-radius-sm);
	}

	:where([data-slean='file-drop'] [data-slean-part='item'] > [data-slean-part='media']) {
		object-fit: cover;
		background: var(--slean-muted);
	}

	/* The type mark: the extension on a muted badge, or the file icon when there is none. */
	:where([data-slean='file-drop'] [data-slean-part='item'] > [data-slean-part='icon']) {
		display: grid;
		place-items: center;
		background: var(--slean-muted);
		color: var(--slean-fg-muted);
		font-size: 0.625rem;
		font-weight: var(--slean-font-weight-semibold);
		letter-spacing: 0.02em;
		line-height: 1;
	}

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

	:where([data-slean='file-drop'] [data-slean-part='item'] > [data-slean-part='title']) {
		grid-area: title;
		overflow: hidden;
		font-weight: var(--slean-font-weight-medium);
		text-overflow: ellipsis;
		white-space: nowrap;
	}

	:where([data-slean='file-drop'] [data-slean-part='item'] > [data-slean-part='description']) {
		grid-area: description;
		color: var(--slean-fg-muted);
		font-size: var(--slean-text-xs);
	}

	/* What the application appends while it uploads the file: a native progress bar. */
	:where([data-slean='file-drop'] [data-slean-part='item'] > progress) {
		grid-column: 2 / 4;
		inline-size: 100%;
		margin-block-start: var(--slean-space-1);
	}

	:where(
		[data-slean='file-drop'] [data-slean-part='item'][aria-busy='true'] > [data-slean-part='media']
	) {
		opacity: 0.5;
	}

	:where([data-slean='file-drop'] [data-slean-part='remove']) {
		appearance: none;
		grid-area: remove;
		display: inline-grid;
		place-items: center;
		inline-size: 1.75rem;
		block-size: 1.75rem;
		padding: 0;
		border: 0;
		border-radius: var(--slean-radius-sm);
		background: transparent;
		color: var(--slean-fg-muted);
		cursor: pointer;
	}

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

	@media (hover: hover) {
		:where([data-slean='file-drop'] [data-slean-part='remove']:enabled:hover) {
			background: var(--slean-muted-hover);
			color: var(--slean-fg);
		}
	}

	:where([data-slean='file-drop'] [data-slean-part='remove']:focus-visible) {
		outline: var(--slean-focus-ring);
		outline-offset: 0;
	}

	:where([data-slean='file-drop'] [data-slean-part='remove']:disabled) {
		cursor: not-allowed;
	}

	/* A refused file: its message in the error color. */
	:where([data-slean='file-drop'] [data-slean-part='item'][data-slean-rejected]) {
		border-color: var(--slean-error-main);
	}

	:where(
		[data-slean='file-drop']
			[data-slean-part='item'][data-slean-rejected]
			> [data-slean-part='title']
	) {
		color: var(--slean-error-main);
		white-space: normal;
	}

	:where(
		[data-slean='file-drop']
			[data-slean-part='item'][data-slean-rejected]
			> [data-slean-part='icon']
	) {
		background: color-mix(in oklch, currentColor 14%, transparent);
		color: var(--slean-error-main);
	}

	/* Tiles: data-variant="picture-card" (square) and "picture-circle" (round). The list wraps the
	 * tiles, the area being the last one: a centered add icon over the author's text. */
	:where([data-slean='file-drop'][data-variant='picture-card'] [data-slean-part='list']),
	:where([data-slean='file-drop'][data-variant='picture-circle'] [data-slean-part='list']) {
		display: flex;
		flex-wrap: wrap;
	}

	:where([data-slean='file-drop'][data-variant='picture-card'] [data-slean-part='item']),
	:where([data-slean='file-drop'][data-variant='picture-circle'] [data-slean-part='item']),
	:where([data-slean='file-drop'][data-variant='picture-card'] [data-slean-part='area']),
	:where([data-slean='file-drop'][data-variant='picture-circle'] [data-slean-part='area']) {
		position: relative;
		inline-size: var(--slean-file-drop-tile-size);
		block-size: var(--slean-file-drop-tile-size);
		min-block-size: 0;
		padding: var(--slean-space-2);
		border-radius: var(--slean-radius-md);
	}

	:where([data-slean='file-drop'][data-variant='picture-circle'] [data-slean-part='item']),
	:where([data-slean='file-drop'][data-variant='picture-circle'] [data-slean-part='area']) {
		border-radius: var(--slean-radius-full);
	}

	:where([data-slean='file-drop'][data-variant='picture-card'] [data-slean-part='area']),
	:where([data-slean='file-drop'][data-variant='picture-circle'] [data-slean-part='area']) {
		gap: var(--slean-space-1);
		background: var(--slean-muted);
		color: var(--slean-fg);
		font-size: var(--slean-text-sm);
	}

	:where([data-slean='file-drop'][data-variant='picture-card'] [data-slean-part='area'])::before,
	:where([data-slean='file-drop'][data-variant='picture-circle'] [data-slean-part='area'])::before {
		content: '';
		inline-size: 1.25rem;
		block-size: 1.25rem;
		background: currentColor;
		mask: var(--slean-icon-plus) center / contain no-repeat;
	}

	/* At the file limit the add tile goes, unless it holds keyboard focus (a choice made from the
	 * keyboard leaves focus on it; it goes once focus moves on). */
	:where(
		[data-slean='file-drop'][data-variant='picture-card'][data-slean-full]
			[data-slean-part='area']:not(:has([data-slean-part='input']:focus-visible))
	),
	:where(
		[data-slean='file-drop'][data-variant='picture-circle'][data-slean-full]
			[data-slean-part='area']:not(:has([data-slean-part='input']:focus-visible))
	) {
		display: none;
	}

	:where([data-slean='file-drop'][data-variant='picture-card'] [data-slean-part='item']),
	:where([data-slean='file-drop'][data-variant='picture-circle'] [data-slean-part='item']) {
		grid-template-columns: minmax(0, 1fr);
		grid-template-areas:
			'media'
			'title';
		place-items: center;
		align-content: center;
		row-gap: var(--slean-space-1);
		text-align: center;
	}

	:where(
		[data-slean='file-drop'][data-variant='picture-card']
			[data-slean-part='item']
			> [data-slean-part='media']
	),
	:where(
		[data-slean='file-drop'][data-variant='picture-circle']
			[data-slean-part='item']
			> [data-slean-part='media']
	) {
		position: absolute;
		/* An absolute grid item with a grid area would be placed in that area. */
		grid-area: auto;
		inset: 0;
		inline-size: 100%;
		block-size: 100%;
		border-radius: inherit;
	}

	:where(
		[data-slean='file-drop'][data-variant='picture-card']
			[data-slean-part='item']
			> [data-slean-part='icon']
	),
	:where(
		[data-slean='file-drop'][data-variant='picture-circle']
			[data-slean-part='item']
			> [data-slean-part='icon']
	) {
		inline-size: 2rem;
		block-size: 2rem;
	}

	:where(
		[data-slean='file-drop'][data-variant='picture-card']
			[data-slean-part='item']
			> [data-slean-part='title']
	),
	:where(
		[data-slean='file-drop'][data-variant='picture-circle']
			[data-slean-part='item']
			> [data-slean-part='title']
	) {
		display: -webkit-box;
		max-inline-size: 100%;
		-webkit-box-orient: vertical;
		-webkit-line-clamp: 3;
		line-clamp: 3;
		font-size: var(--slean-text-xs);
		line-height: 1.3;
		white-space: normal;
		overflow-wrap: anywhere;
	}

	/* A thumbnail is the tile: its name and every size stay for assistive technology only. */
	:where(
		[data-slean='file-drop'][data-variant='picture-card']
			[data-slean-part='item']:has(> [data-slean-part='media'])
			> [data-slean-part='title']
	),
	:where(
		[data-slean='file-drop'][data-variant='picture-circle']
			[data-slean-part='item']:has(> [data-slean-part='media'])
			> [data-slean-part='title']
	),
	:where(
		[data-slean='file-drop'][data-variant='picture-card']
			[data-slean-part='item']
			> [data-slean-part='description']
	),
	:where(
		[data-slean='file-drop'][data-variant='picture-circle']
			[data-slean-part='item']
			> [data-slean-part='description']
	) {
		position: absolute;
		inline-size: 1px;
		block-size: 1px;
		overflow: hidden;
		clip-path: inset(50%);
		white-space: nowrap;
	}

	:where(
		[data-slean='file-drop'][data-variant='picture-card']
			[data-slean-part='item']
			> [data-slean-part='remove']
	),
	:where(
		[data-slean='file-drop'][data-variant='picture-circle']
			[data-slean-part='item']
			> [data-slean-part='remove']
	) {
		position: absolute;
		grid-area: auto;
		inset-block-start: var(--slean-space-1);
		inset-inline-end: var(--slean-space-1);
		inline-size: 1.5rem;
		block-size: 1.5rem;
		border-radius: var(--slean-radius-full);
		background: var(--slean-surface);
		box-shadow: var(--slean-elevation-1);
		color: var(--slean-fg);
	}

	:where(
		[data-slean='file-drop'][data-variant='picture-circle']
			[data-slean-part='item']
			> [data-slean-part='remove']
	) {
		inset-block-start: 0;
		inset-inline-end: 0;
	}

	:where(
		[data-slean='file-drop'][data-variant='picture-card']
			[data-slean-part='item']
			> [data-slean-part='remove']
	)::before,
	:where(
		[data-slean='file-drop'][data-variant='picture-circle']
			[data-slean-part='item']
			> [data-slean-part='remove']
	)::before {
		inline-size: 0.75rem;
		block-size: 0.75rem;
	}

	:where([data-slean='file-drop'][data-variant='picture-card'] [data-slean-part='item'] > progress),
	:where(
		[data-slean='file-drop'][data-variant='picture-circle'] [data-slean-part='item'] > progress
	) {
		position: absolute;
		grid-area: auto;
		inset-inline: var(--slean-space-2);
		inset-block-end: var(--slean-space-2);
		inline-size: auto;
		margin: 0;
	}

	@media (scripting: none) {
		:where([data-slean='file-drop'][data-variant='picture-card'] [data-slean-part='area']),
		:where([data-slean='file-drop'][data-variant='picture-circle'] [data-slean-part='area']) {
			inline-size: auto;
			block-size: auto;
			border-radius: var(--slean-radius-md);
		}
	}

	@media (forced-colors: active) {
		:where([data-slean='file-drop']:not(:has([data-slean-part='area']))),
		:where([data-slean='file-drop'] [data-slean-part='area']),
		:where([data-slean='file-drop'] [data-slean-part='item']) {
			border-color: CanvasText;
		}

		:where([data-slean='file-drop'][data-slean-dragging]:not(:has([data-slean-part='area']))),
		:where([data-slean='file-drop'][data-slean-dragging] [data-slean-part='area']) {
			border-color: Highlight;
		}

		:where([data-slean='file-drop'] [data-slean-part='item'][data-slean-rejected]) {
			border-style: dashed;
		}

		:where([data-slean='file-drop'] [data-slean-part='remove'])::before,
		:where([data-slean='file-drop'] [data-slean-part='area'])::before,
		:where([data-slean='file-drop'] [data-slean-part='icon'])::before,
		:where([data-slean='file-drop'] [data-slean-part='area'] > [data-slean-part='icon']) {
			forced-color-adjust: none;
			background: CanvasText;
		}

		:where([data-slean='file-drop']:has([data-slean-part='input']:disabled)) {
			opacity: 1;
			color: GrayText;
			border-color: GrayText;
		}
	}
}
