/* ── Dark mode ───────────────────────────────────────────────────────────────
 *
 * An accessibility setting, not a theme choice. It applies only when the
 * visitor's device asks for dark AND for more contrast (prefers-contrast:
 * more — "Increase contrast" on macOS and iOS, a contrast theme on Windows).
 * Someone who runs their device dark for looks gets the designed light site;
 * someone who has asked their device for help reading gets the dark one.
 *
 * Front end only: inc/dark-mode.php enqueues this outside the editor, so
 * authors always pick colours against the light page they are designing.
 *
 * The site is white "paper" carrying brand-coloured parts — navy bands, teal
 * and burgundy panels, photo banners. Dark mode turns the paper dark and leaves
 * every brand-coloured part exactly as it is in light mode. That works on the
 * palette variables rather than on individual rules:
 *
 *   1. PAPER. The light palette slugs are pointed at dark values: base (white)
 *      becomes the dark page, sky/cream/light become dark tints of themselves,
 *      and primary/contrast (navy text) become white. Everything that reads
 *      those variables — global styles, editor colour classes, the theme's own
 *      CSS — follows without being rewritten. This is set on <body>, and again
 *      on every light surface (a white group, a cream panel, an input), so a
 *      light surface turns dark wherever it sits, even inside an island.
 *
 *   2. ISLANDS. Anything with a brand-coloured or image background puts the
 *      light palette back for itself and everything inside it, so a navy band
 *      with white text stays a navy band with white text. Colours chosen in
 *      the editor are islands automatically (.has-background), unless they are
 *      one of the light paper colours.
 *
 * Remapping one palette slug on its own would break the colour pairs: base is
 * the page colour AND the white text on every navy panel. Islands are what let
 * both meanings survive.
 *
 * The light palette is captured on :root, where WordPress defines it. Custom
 * properties inherit as resolved values, so --aa-lit-* hold the light colours
 * everywhere below even after the presets are repointed.
 *
 * Palette: the page is the one colour not in it — navy taken 35% toward black,
 * so navy bands and panels still read as navy against it. Every other dark
 * value is the palette itself or a palette colour mixed into that page.
 *
 * Browsers without color-mix() (pre-2023) keep the light site; the whole
 * scheme is inside the @supports below.
 * -------------------------------------------------------------------------- */

:root {
	--aa-lit-base: var(--wp--preset--color--base);
	--aa-lit-background: var(--wp--preset--color--background);
	--aa-lit-contrast: var(--wp--preset--color--contrast);
	--aa-lit-primary: var(--wp--preset--color--primary);
	--aa-lit-neutral: var(--wp--preset--color--neutral);
	--aa-lit-light: var(--wp--preset--color--light);
	--aa-lit-sky: var(--wp--preset--color--sky);
	--aa-lit-cream: var(--wp--preset--color--cream);
	--aa-lit-gradient-white-sky: var(--wp--preset--gradient--white-sky);
	--aa-lit-border-card: var(--border-card);
	--aa-lit-shadow-card: var(--shadow-card);
	--aa-lit-colour-warning: var(--colour-warning);
}

@supports (color: color-mix(in srgb, red 50%, blue)) {
@media (prefers-color-scheme: dark) and (prefers-contrast: more) {

	/* The page's own scrollbar and canvas follow the root element. */
	:root {
		color-scheme: dark;
	}

	/* ── 1. Paper ──────────────────────────────────────────────────────────── */

	/* The light surfaces. Repeated where an island has to leave them out, as
	   CSS has no way to name a selector list once. aa_angle_sky.svg is the sky
	   wedge some sections use as a background image; it is handled at the end.
	   The component variants are the light pairs in aa_accordion_colours() and
	   aa_card_colours() — keep in step with those lists. */
	body,
	:is(
		.has-base-background-color, .has-background-background-color, .has-sky-background-color,
		.has-light-background-color, .has-cream-background-color,
		.has-white-sky-gradient-background, [style*="--wp--preset--gradient--white-sky"],
		[style*="aa_angle_sky.svg"],
		.aa-accordion__panel--white, .aa-accordion__panel--sky, .aa-accordion__panel--cream, .aa-accordion__panel--light,
		.aa-announcement-bar--white, .aa-announcement-bar--sky, .aa-announcement-bar--cream, .aa-announcement-bar--light,
		.aa-popup--white, .aa-popup--sky, .aa-popup--cream, .aa-popup--light,
		.aa-cards--colour-white, .aa-cards--colour-sky, .aa-cards--colour-cream, .aa-cards--colour-light, .aa-cards--colour-frosted,
		.page-label--light,
		.aa-country-search,
		input, select, textarea
	) {
		color-scheme: dark;

		--aa-dark-page: color-mix(in srgb, var(--aa-lit-primary) 65%, #000);

		--wp--preset--color--base: var(--aa-dark-page);
		--wp--preset--color--background: color-mix(in srgb, var(--aa-lit-primary) 82%, #000);
		--wp--preset--color--contrast: var(--aa-lit-base);
		--wp--preset--color--primary: var(--aa-lit-base);
		--wp--preset--color--neutral: color-mix(in srgb, var(--aa-lit-base) 72%, var(--aa-dark-page));
		/* Card edges, dividers and rules: 3.1:1 on the page, the WCAG minimum
		   for an edge you need to see, since this mode is for people who asked
		   for more contrast. */
		--wp--preset--color--light: color-mix(in srgb, var(--aa-lit-light) 40%, var(--aa-dark-page));
		--wp--preset--color--sky: var(--aa-lit-primary);
		--wp--preset--color--cream: color-mix(in srgb, var(--aa-lit-cream) 16%, var(--aa-dark-page));

		/* Derived on :root from the light palette, so they must be derived again
		   here. white-sky mirrors theme.json; the two navy/teal gradients only
		   ever paint islands, which keep the light versions. */
		--wp--preset--gradient--white-sky: linear-gradient(0deg, var(--wp--preset--color--sky) 0%, var(--wp--preset--color--base) 100%);
		--border-card: 1px solid var(--wp--preset--color--light);
		--shadow-card: 0 2px 12px rgb(0 0 0 / 0.3);

		/* Orange text is 3.3:1 on the dark page; yellow is the dark-mode warning. */
		--colour-warning: var(--wp--preset--color--yellow);

		/* Paper-only values, put back by islands (below). */
		--aa-dark-tint: rgb(255 255 255 / 0.05);
		--aa-dark-edge: rgb(255 255 255 / 0.2);
		--aa-dark-tick: var(--wp--preset--color--secondary);
	}

	/* A light surface that sets no text colour of its own inherits whatever is
	   around it, which inside an island can be navy. Default text on a light
	   surface is "contrast", so it gets that — white here. Zero specificity, so
	   a colour the surface does set wins. */
	:where(
		.has-base-background-color, .has-background-background-color, .has-sky-background-color,
		.has-light-background-color, .has-cream-background-color,
		.has-white-sky-gradient-background, [style*="--wp--preset--gradient--white-sky"],
		.aa-country-search
	) {
		color: var(--wp--preset--color--contrast);
	}

	/* ── 2. Islands ────────────────────────────────────────────────────────── */

	:is(
		/* Colours chosen in the editor, custom ones included */
		.has-background:not(.has-base-background-color, .has-background-background-color, .has-sky-background-color, .has-light-background-color, .has-cream-background-color, .has-white-sky-gradient-background, [style*="--wp--preset--gradient--white-sky"], [style*="aa_angle_sky.svg"]),
		.wp-block-cover,
		/* Table cells coloured in the Flexible Table block carry literal inline
		   colours, which no variable can darken. */
		:is(th, td)[style*="background-color"],

		/* Core blocks the theme colours navy */
		.wp-block-code,
		.wp-block-preformatted,
		.wp-block-quote:not(.has-base-background-color, .has-background-background-color, .has-sky-background-color, .has-light-background-color, .has-cream-background-color),
		:is(.wp-block-button__link, .wp-element-button):not(.has-base-background-color, .has-background-background-color, .has-sky-background-color, .has-light-background-color, .has-cream-background-color, .is-style-outline > *),
		:is(.wp-block-table, .wp-block-flexible-table-block-table) thead,
		.is-style-filled-first-column td:first-of-type,

		/* Theme components */
		.kh-header,
		:is(.header-banner, .header-icon) .text,
		.quick-links h4,
		.page-anchor-nav,
		.page-label--type,
		.card.page-link,
		.card[class*="card-style-"],
		:is(.is-style-card, .is-style-card-link) > .wp-block-image,
		.wp-block-frost-post-banner,
		.aa-card__badge,
		.aa-news-pagination__link.is-current,
		.aa-cal-day--today .aa-cal-day__num,
		.aa-cal-day__events a,
		:is(.aa-cal-prev, .aa-cal-next):hover,
		:is(.frost-search__type-toggle, .frost-search__tag-toggle, .fsr-toggle).is-active,
		.frost-search__result-type,
		.aa-marquee__toggle,
		.aa-marquee__nav,
		body.section-theme--first-nations,

		/* Fluent Forms' submit button, coloured navy in the form's custom CSS */
		.fluentform .ff-btn-submit,

		/* Colour pairs with a brand-coloured ground: every pair in
		   aa_accordion_colours() except the four light ones in Paper above.
		   Page labels have their own list in aa_label_colours(). */
		.aa-accordion__panel--navy, .aa-accordion__panel--teal, .aa-accordion__panel--aqua,
		.aa-accordion__panel--neutral, .aa-accordion__panel--yellow, .aa-accordion__panel--orange,
		.aa-accordion__panel--red, .aa-accordion__panel--burgundy, .aa-accordion__panel--red-shade,
		.aa-announcement-bar--navy, .aa-announcement-bar--teal, .aa-announcement-bar--aqua,
		.aa-announcement-bar--neutral, .aa-announcement-bar--yellow, .aa-announcement-bar--orange,
		.aa-announcement-bar--red, .aa-announcement-bar--burgundy, .aa-announcement-bar--red-shade,
		.aa-popup--navy, .aa-popup--teal, .aa-popup--aqua,
		.aa-popup--neutral, .aa-popup--yellow, .aa-popup--orange,
		.aa-popup--red, .aa-popup--burgundy, .aa-popup--red-shade,
		.aa-cards--colour-navy, .aa-cards--colour-teal, .aa-cards--colour-aqua,
		.aa-cards--colour-neutral, .aa-cards--colour-yellow, .aa-cards--colour-orange,
		.aa-cards--colour-red, .aa-cards--colour-burgundy, .aa-cards--colour-red-shade,
		.page-label--navy, .page-label--red, .page-label--orange, .page-label--yellow
	) {
		color-scheme: light;

		--wp--preset--color--base: var(--aa-lit-base);
		--wp--preset--color--background: var(--aa-lit-background);
		--wp--preset--color--contrast: var(--aa-lit-contrast);
		--wp--preset--color--primary: var(--aa-lit-primary);
		--wp--preset--color--neutral: var(--aa-lit-neutral);
		--wp--preset--color--light: var(--aa-lit-light);
		--wp--preset--color--sky: var(--aa-lit-sky);
		--wp--preset--color--cream: var(--aa-lit-cream);
		--wp--preset--gradient--white-sky: var(--aa-lit-gradient-white-sky);
		--border-card: var(--aa-lit-border-card);
		--shadow-card: var(--aa-lit-shadow-card);
		--colour-warning: var(--aa-lit-colour-warning);

		--aa-dark-tint: rgb(0 0 0 / 0.04);
		--aa-dark-edge: rgb(0 0 0 / 0.2);
		--aa-dark-tick: var(--aa-lit-primary);
	}

	/* An island that sets no text colour of its own inherits the page's, which
	   is now white. In light mode it would have inherited navy, so it gets navy
	   back — at zero specificity, so any colour the island does set wins, and
	   only on outermost islands, so one inside another keeps its parent's. */
	:where(
		.has-background:not(.has-base-background-color, .has-background-background-color, .has-sky-background-color, .has-light-background-color, .has-cream-background-color, .has-white-sky-gradient-background, [style*="--wp--preset--gradient--white-sky"], [style*="aa_angle_sky.svg"]),
		:is(th, td)[style*="background-color"]
	):not(:where(.has-background, .wp-block-cover) *) {
		color: var(--aa-lit-contrast);
	}

	/* ── 3. Component fixes ────────────────────────────────────────────────── */

	/* Yellow is light in both schemes, so text on it is navy in both. These
	   rules asked for "primary", which is white on the dark page. */
	.wp-block-button__link:is(.has-base-background-color, .has-light-background-color, .has-sky-background-color, .has-cream-background-color, .has-aqua-background-color):is(:hover, :focus-visible) {
		color: var(--aa-lit-primary) !important;
	}

	:is(.aa-popup--white, .aa-popup--sky, .aa-popup--cream, .aa-popup--light) {
		--aa-popup-button-hover-fg: var(--aa-lit-primary);
	}

	/* A white tick on a white box: the checked fill takes teal instead (4.9:1). */
	input[type="checkbox"]:checked {
		background-color: var(--aa-dark-tick);
		border-color: var(--aa-dark-tick);
	}

	/* Shadows tinted navy vanish on a navy page; black ones still lift a card. */
	.card:hover {
		box-shadow: 0 2px 12px rgb(0 0 0 / 0.6);
	}

	.card:has(> .wp-block-template-part > .aa-sub-accordion) {
		filter: drop-shadow(0 2px 12px rgb(0 0 0 / 0.3));
	}

	.card:has(> .wp-block-template-part > .aa-sub-accordion):hover {
		filter: drop-shadow(0 2px 12px rgb(0 0 0 / 0.6));
	}

	:is(.subpages-circular, .subpages-clockwise) {
		--card-shadow: drop-shadow(0 2px 8px rgb(0 0 0 / 0.4));
		--card-shadow-hover: drop-shadow(0 4px 13px rgb(0 0 0 / 0.7));
	}

	.aa-card:hover {
		box-shadow: 0 3px 12px rgb(0 0 0 / 0.5);
	}

	/* Black-on-white tints written as literals become invisible; these read
	   --aa-dark-tint / --aa-dark-edge, which islands set back to the originals. */
	.event-meta {
		background: var(--aa-dark-tint);
	}

	.aa-cal-day {
		border-color: var(--aa-dark-edge);
	}

	.aa-cal-day--empty,
	.aa-card__image-placeholder {
		background: var(--aa-dark-tint);
	}

	:is(.wp-block-table, .wp-block-flexible-table-block-table).striped tr:nth-child(even) {
		background-color: var(--aa-dark-tint);
	}

	.aa-news-pagination__link {
		border-color: var(--aa-dark-edge);
		background-color: var(--aa-dark-tint);
	}

	.aa-event-form :is(input, textarea) {
		background: var(--wp--preset--color--base);
		border-color: var(--aa-dark-edge);
	}

	/* Orange and red text fall below 4.5:1 on the dark page. */
	.frost-search__result-breadcrumb {
		color: var(--wp--preset--color--yellow);
	}

	.frost-search__error {
		color: color-mix(in srgb, var(--wp--preset--color--red) 55%, var(--aa-lit-base));
	}

	/* The Knowledge Hub header art is navy line work at 20%, which disappears
	   on navy. Drawn white instead, at the same strength. */
	.header-icon :is(img, svg) {
		filter: brightness(0) invert(1);
	}

	/* The sky wedge background (aa_angle_sky.svg) would be a bright sky
	   triangle. Soft-light over the page colour turns it into a slightly lighter
	   navy wedge, so the shape stays. The inline style sets only the image; a
	   background colour chosen in the editor (a class, !important) still wins. */
	[style*="aa_angle_sky.svg"] {
		background-color: var(--wp--preset--color--background);
		background-blend-mode: soft-light;
	}

	/* A section's background image (inc/sections.php) gets the same treatment
	   — the same sky wedge is one of them — over the page's own colour, which
	   <main> and its wrapper take from <body> so section themes keep theirs.
	   First Nations is an island and keeps its image as it is. */
	body.has-section-bg:not(.section-theme--first-nations) :is(.wp-site-blocks, .wp-site-blocks > main) {
		background-color: inherit;
	}

	body.has-section-bg:not(.section-theme--first-nations) .wp-site-blocks > main {
		background-blend-mode: soft-light;
	}

}
}
