/**
 * BBC eBird Checklist Archive.
 *
 * Every selector is wrapped in :where() so it carries zero specificity - any
 * theme rule, however plain, overrides it without needing !important.
 *
 * Colours and spacing come from custom properties on the wrapper, so a theme
 * can retune the whole table by setting a few values. See readme.txt.
 */

:where( .bbcc ) {
	--bbcc-gap: 1rem;
	--bbcc-border: currentColor;
	--bbcc-muted: currentColor;
	--bbcc-stripe: transparent;

	/* The suggestion popup sits over the table, so it needs an opaque
	   background. Canvas/CanvasText are the browser's page colours; a dark
	   theme should set both. */
	--bbcc-surface: Canvas;
	--bbcc-surface-text: CanvasText;

	/* Opacity keeps the defaults legible on light and dark themes alike,
	   without hardcoding a palette the theme would have to fight. */
	--bbcc-border-opacity: 0.18;
	--bbcc-muted-opacity: 0.7;
	--bbcc-breakpoint: 640px;

	display: block;
}

/* Filters
   ------------------------------------------------------------------------ */

:where( .bbcc-filters ) {
	display: flex;
	flex-wrap: wrap;
	align-items: flex-end;
	gap: var( --bbcc-gap );
	margin-block-end: var( --bbcc-gap );
}

:where( .bbcc-field ) {
	display: flex;
	flex-direction: column;
	gap: 0.25em;
	flex: 1 1 12rem;
	min-width: 0;
}

:where( .bbcc-field ) > label {
	font-size: 0.875em;
	opacity: var( --bbcc-muted-opacity );
}

:where( .bbcc-field ) :where( select, input ) {
	width: 100%;
	min-height: 44px; /* Tap target floor (SPEC.md). */
	max-width: 100%;
	box-sizing: border-box;
}

:where( .bbcc-location ) {
	position: relative;
	flex: 1 1 10rem;
}

/* Dates and Reset keep their natural width so all four controls fit on one
   line; the bar only wraps when the container is narrower than that. */
:where( .bbcc-date-field, .bbcc-reset-field ) {
	flex: 0 0 auto;
}

/* Location chips
   ------------------------------------------------------------------------ */

:where( .bbcc-chips ) {
	display: flex;
	flex-wrap: wrap;
	gap: 0.35em;
	margin-block: calc( var( --bbcc-gap ) * -0.5 ) var( --bbcc-gap );
}

:where( .bbcc-chips[hidden] ) {
	display: none;
}

:where( .bbcc-chip ) {
	display: inline-flex;
	align-items: center;
	max-width: 100%;
	padding-inline-start: 0.65em;
	font-size: 0.875em;
	border: 1px solid;
	border-color: color-mix( in srgb, var( --bbcc-border ) calc( var( --bbcc-border-opacity ) * 100% ), transparent );
	border-radius: 999px;
	background-color: color-mix( in srgb, currentColor 6%, transparent );
}

:where( .bbcc-chip-label ) {
	overflow-wrap: anywhere;
}

:where( .bbcc-chip-remove ) {
	display: inline-flex;
	align-items: center;
	justify-content: center;
	flex: 0 0 auto;
	min-width: 2em;
	min-height: 2em;
	padding: 0;
	font: inherit;
	font-size: 1.15em;
	line-height: 1;
	color: inherit;
	background: none;
	border: 0;
	border-radius: 999px;
	cursor: pointer;
}

:where( .bbcc-chip-remove:hover ) {
	background-color: color-mix( in srgb, currentColor 12%, transparent );
}

/* Location suggestions
   ------------------------------------------------------------------------ */

:where( .bbcc-suggest ) {
	position: absolute;
	inset-inline: 0;
	top: 100%;
	z-index: 20;
	margin-block-start: 2px;
	max-height: min( 26rem, 60vh );
	overflow-y: auto;
	overscroll-behavior: contain;
	color: var( --bbcc-surface-text );
	background-color: var( --bbcc-surface );
	border: 1px solid;
	border-color: color-mix( in srgb, var( --bbcc-surface-text ) 25%, transparent );
	box-shadow: 0 6px 18px rgb( 0 0 0 / 0.15 );
}

:where( .bbcc-suggest-heading ) {
	padding: 0.6em 0.75em 0.2em;
	font-size: 0.8em;
	font-weight: 700;
	opacity: var( --bbcc-muted-opacity );
}

:where( .bbcc-suggest-option ) {
	display: flex;
	align-items: baseline;
	gap: 0.75em;
	min-height: 44px; /* Tap target floor (SPEC.md). */
	box-sizing: border-box;
	padding: 0.55em 0.75em;
	cursor: pointer;
}

:where( .bbcc-suggest-option:hover ) {
	background-color: color-mix( in srgb, currentColor 7%, transparent );
}

/* The active option. Focus stays in the input (aria-activedescendant), so
   this is the only visible focus cue - keep it strong. */
:where( .bbcc-suggest-option[aria-selected="true"] ) {
	background-color: color-mix( in srgb, currentColor 14%, transparent );
	outline: 2px solid currentColor;
	outline-offset: -2px;
}

:where( .bbcc-suggest-text ) {
	flex: 1 1 auto;
	min-width: 0;
	overflow-wrap: anywhere;
}

:where( .bbcc-suggest-label ) {
	margin-inline-end: 0.25em;
}

:where( .bbcc-suggest-secondary ) {
	font-size: 0.875em;
	opacity: var( --bbcc-muted-opacity );
}

:where( .bbcc-suggest-count ) {
	flex: 0 0 auto;
	font-size: 0.875em;
	font-variant-numeric: tabular-nums;
	opacity: var( --bbcc-muted-opacity );
}

:where( .bbcc-suggest-empty ) {
	padding: 0.75em;
	opacity: var( --bbcc-muted-opacity );
}

/* Touch screens get full-size remove buttons. */
@media ( pointer: coarse ) {

	:where( .bbcc-chip-remove ) {
		min-width: 44px;
		min-height: 44px;
	}
}

:where( .bbcc-reset ) {
	min-height: 44px;
	cursor: pointer;
}

/* Status, messages, empty state
   ------------------------------------------------------------------------ */

:where( .bbcc-status ) {
	margin-block: 0 0.5em;
	font-size: 0.9em;
	opacity: var( --bbcc-muted-opacity );
}

:where( .bbcc-message ) {
	margin-block: 0 var( --bbcc-gap );
	padding: 0.75em 1em;
	border-inline-start: 3px solid;
	border-color: color-mix( in srgb, var( --bbcc-border ) 40%, transparent );
}

:where( .bbcc-empty, .bbcc-noscript ) {
	margin-block: var( --bbcc-gap );
	opacity: var( --bbcc-muted-opacity );
}

/* Table
   ------------------------------------------------------------------------ */

:where( .bbcc-table ) {
	width: 100%;
	border-collapse: collapse;
}

:where( .bbcc-table ) :where( th, td ) {
	text-align: start;
	vertical-align: top;
	padding: 0.5em 0.75em;
	border-block-end: 1px solid;
	border-color: color-mix( in srgb, var( --bbcc-border ) calc( var( --bbcc-border-opacity ) * 100% ), transparent );
}

:where( .bbcc-table tbody tr:nth-child( even ) ) {
	background-color: var( --bbcc-stripe );
}

:where( .bbcc-cell-n ) {
	font-variant-numeric: tabular-nums;
	white-space: nowrap;
}

:where( .bbcc-cell-date ) {
	white-space: nowrap;
}

/* The caption is kept in the accessibility tree but out of the way; a theme
   can reveal it by overriding position and clip-path. */
:where( .bbcc-caption ) {
	position: absolute;
	width: 1px;
	height: 1px;
	padding: 0;
	margin: -1px;
	overflow: hidden;
	clip-path: inset( 50% );
	white-space: nowrap;
	border: 0;
}

:where( .bbcc-sr-only ) {
	position: absolute;
	width: 1px;
	height: 1px;
	padding: 0;
	margin: -1px;
	overflow: hidden;
	clip-path: inset( 50% );
	white-space: nowrap;
	border: 0;
}

/* Sortable headers
   ------------------------------------------------------------------------ */

:where( .bbcc-sort ) {
	display: inline-flex;
	align-items: center;
	gap: 0.35em;
	padding: 0;
	font: inherit;
	color: inherit;
	background: none;
	border: 0;
	cursor: pointer;
}

/* Never remove focus outlines (SPEC.md). */
:where( .bbcc-sort:focus-visible, .bbcc-page:focus-visible, .bbcc-reset:focus-visible, .bbcc-chip-remove:focus-visible ) {
	outline: 2px solid currentColor;
	outline-offset: 2px;
}

:where( [aria-sort="ascending"] ) .bbcc-arrow::after {
	content: "\2191";
}

:where( [aria-sort="descending"] ) .bbcc-arrow::after {
	content: "\2193";
}

:where( [aria-sort="none"] ) .bbcc-arrow::after {
	content: "\2195";
	opacity: 0.35;
}

/* Pagination
   ------------------------------------------------------------------------ */

:where( .bbcc-pagination ) {
	display: flex;
	flex-wrap: wrap;
	align-items: center;
	gap: 0.25em;
	margin-block-start: var( --bbcc-gap );
}

:where( .bbcc-page ) {
	min-width: 44px;
	min-height: 44px;
	padding: 0.25em 0.6em;
	font: inherit;
	color: inherit;
	background: none;
	border: 1px solid;
	border-color: color-mix( in srgb, var( --bbcc-border ) calc( var( --bbcc-border-opacity ) * 100% ), transparent );
	cursor: pointer;
}

:where( .bbcc-page[disabled] ) {
	opacity: 0.4;
	cursor: default;
}

:where( .bbcc-page.is-current ) {
	font-weight: 700;
	border-width: 2px;
}

:where( .bbcc-ellipsis ) {
	padding-inline: 0.25em;
	opacity: var( --bbcc-muted-opacity );
}

/* Mobile card layout
   ------------------------------------------------------------------------

   Six columns do not fit on a phone, so each row becomes a stacked card. This
   is done with CSS on the same semantic <table> markup - there is no second
   DOM (SPEC.md). Override this one media query to move or remove the
   breakpoint. */

@media ( max-width: 640px ) {

	:where( .bbcc-table thead ) {
		position: absolute;
		width: 1px;
		height: 1px;
		overflow: hidden;
		clip-path: inset( 50% );
	}

	:where( .bbcc-table ),
	:where( .bbcc-table tbody ),
	:where( .bbcc-table tr ),
	:where( .bbcc-table td ) {
		display: block;
		width: auto;
	}

	:where( .bbcc-table tr ) {
		padding-block: 0.75em;
		border-block-end: 1px solid;
		border-color: color-mix( in srgb, var( --bbcc-border ) calc( var( --bbcc-border-opacity ) * 100% ), transparent );
	}

	:where( .bbcc-table td ) {
		padding: 0.1em 0;
		border: 0;
	}

	/* Line 1: "#17432 - May 14, 2018 (6:15 AM)" with the date as the link. */
	:where( .bbcc-table .bbcc-cell-n ) {
		display: inline;
		font-weight: 700;
	}

	:where( .bbcc-table .bbcc-cell-n )::before {
		content: "#";
	}

	:where( .bbcc-table .bbcc-cell-date ) {
		display: inline;
	}

	:where( .bbcc-table .bbcc-cell-date )::before {
		content: " \00b7 ";
	}

	/* Line 3: "Essex, Massachusetts, United States" reads as one sentence. */
	:where( .bbcc-table .bbcc-cell-county ),
	:where( .bbcc-table .bbcc-cell-state ),
	:where( .bbcc-table .bbcc-cell-country ) {
		display: inline;
		opacity: var( --bbcc-muted-opacity );
		font-size: 0.9em;
	}

	:where( .bbcc-table .bbcc-cell-state )::before,
	:where( .bbcc-table .bbcc-cell-country )::before {
		content: ", ";
	}

	/* A row with no county would otherwise open with a stray comma. */
	:where( .bbcc-table .bbcc-cell-county:empty ) + :where( .bbcc-cell-state )::before {
		content: "";
	}

	:where( .bbcc-table .bbcc-cell-loc ) {
		display: block;
	}

	:where( .bbcc-filters ) {
		flex-direction: column;
		align-items: stretch;
	}

	:where( .bbcc-field ) {
		flex: 1 1 auto;
	}

	:where( .bbcc-chip-remove ) {
		min-width: 44px;
		min-height: 44px;
	}
}

/* Loading and error
   ------------------------------------------------------------------------ */

:where( .bbcc:not( .bbcc-ready ) .bbcc-filters ) {
	opacity: 0.5;
	pointer-events: none;
}

:where( .bbcc-has-error .bbcc-status ) {
	opacity: 1;
	font-weight: 700;
}
