/* ==========================================================================
   A11y Widget — Self-contained styles
   ========================================================================== */

/* ─── CSS custom properties ─────────────────────────────────────────────── */
#a11y-widget {
	--a11y-accent:       #004ca0;
	--a11y-accent-hover: #003580;
	--a11y-bg:           #ffffff;
	--a11y-text:         #111827;
	--a11y-muted:        #6b7280;
	--a11y-border:       #e5e7eb;
	--a11y-row-hover:    #f9fafb;
	--a11y-radius:       0.375rem;
	--a11y-font:         system-ui, -apple-system, sans-serif;
	--a11y-transition:   150ms ease;
	font-family: var(--a11y-font);
}

/* ─── Screen-reader only ────────────────────────────────────────────────── */
.a11y-sr-only {
	position: absolute;
	width: 1px;
	height: 1px;
	padding: 0;
	margin: -1px;
	overflow: hidden;
	clip: rect(0,0,0,0);
	white-space: nowrap;
	border: 0;
}

/* ─── Widget wrapper — fixed at top ─────────────────────────────────────── */
#a11y-widget {
	position: fixed;
	top: 0;
	left: 0;
	right: 0;
	z-index: 99999;
	pointer-events: none;
}

#a11y-widget > * {
	pointer-events: auto;
}

/* Admin bar offset */
.admin-bar #a11y-widget {
	top: var(--wp-admin--admin-bar--height, 32px);
}

@media screen and (max-width: 782px) {
	.admin-bar #a11y-widget {
		top: 46px;
	}
}

/* ─── Built-in trigger button (shown when no external trigger) ───────────── */
.a11y-btn-open {
	position: absolute;
	top: 0;
	right: 1.5rem;
	display: flex;
	align-items: center;
	gap: 0.375rem;
	padding: 0.4375rem 1rem;
	background: var(--a11y-accent);
	color: #ffffff;
	border: none;
	border-radius: 0 0 var(--a11y-radius) var(--a11y-radius);
	font-family: var(--a11y-font);
	font-size: 0.8125rem;
	font-weight: 600;
	cursor: pointer;
	box-shadow: 0 2px 10px rgba(0,0,0,.18);
	transition: background-color var(--a11y-transition);
	white-space: nowrap;
}

.a11y-btn-open:hover,
.a11y-btn-open:focus-visible {
	background: var(--a11y-accent-hover);
}

.a11y-btn-open:focus-visible {
	outline: 3px solid var(--a11y-accent);
	outline-offset: 2px;
}

/* Hide built-in button when external trigger is configured */
.a11y-has-trigger .a11y-btn-open {
	display: none;
}

/* ─── Panel ──────────────────────────────────────────────────────────────── */
.a11y-panel {
	width: 100%;
	background: var(--a11y-bg);
	border-bottom: 1px solid var(--a11y-border);
	box-shadow: 0 4px 20px rgba(0,0,0,.08), 0 1px 4px rgba(0,0,0,.05);
	animation: a11y-slide-down 180ms ease both;
}

/* Keep every option reachable when the panel is taller than the viewport
   (small screens, 200 % zoom — RGAA 10.11). */
.a11y-panel {
	max-height: 100vh;
	max-height: 100dvh;
	overflow-y: auto;
	overscroll-behavior: contain;
}

.admin-bar .a11y-panel {
	max-height: calc(100vh - var(--wp-admin--admin-bar--height, 32px));
	max-height: calc(100dvh - var(--wp-admin--admin-bar--height, 32px));
}

@media screen and (max-width: 782px) {
	.admin-bar .a11y-panel {
		max-height: calc(100vh - 46px);
		max-height: calc(100dvh - 46px);
	}
}

@keyframes a11y-slide-down {
	from { opacity: 0; transform: translateY(-6px); }
	to   { opacity: 1; transform: translateY(0); }
}

/* ─── Panel header ───────────────────────────────────────────────────────── */
.a11y-panel-header {
	display: flex;
	align-items: center;
	gap: 0.5rem;
	padding: 0.5rem 1.25rem;
	border-bottom: 1px solid var(--a11y-border);
}

.a11y-panel-header-icon {
	display: flex;
	align-items: center;
	color: var(--a11y-accent);
	flex-shrink: 0;
}

.a11y-panel-title {
	flex: 1;
	font-size: 0.8125rem;
	font-weight: 600;
	color: var(--a11y-text);
	margin: 0;
	line-height: 1.3;
	font-family: var(--a11y-font);
}

.a11y-btn-close {
	display: flex;
	align-items: center;
	justify-content: center;
	width: 1.75rem;
	height: 1.75rem;
	border-radius: var(--a11y-radius);
	background: none;
	border: none;
	cursor: pointer;
	color: var(--a11y-muted);
	transition: background-color var(--a11y-transition), color var(--a11y-transition);
	flex-shrink: 0;
}

.a11y-btn-close:hover,
.a11y-btn-close:focus-visible {
	background: var(--a11y-row-hover);
	color: var(--a11y-text);
}

.a11y-btn-close:focus-visible {
	outline: 2px solid var(--a11y-accent);
	outline-offset: 1px;
}

/* ─── Feature rows — horizontal toolbar ─────────────────────────────────── */
.a11y-rows-wrap {
	display: flex;
	overflow-x: auto;
	-webkit-overflow-scrolling: touch;
}

.a11y-row {
	flex: 1 1 0;
	min-width: 120px;
	display: flex;
	flex-direction: column;
	align-items: center;
	justify-content: center;
	gap: 0.5rem;
	padding: 0.75rem 0.875rem;
	text-align: center;
	border-right: 1px solid var(--a11y-border);
	transition: background-color var(--a11y-transition);
}

.a11y-row:last-child {
	border-right: none;
}

.a11y-row:hover {
	background: var(--a11y-row-hover);
}

.a11y-row-label {
	display: flex;
	flex-direction: column;
	align-items: center;
	gap: 0.2rem;
	margin: 0;
	padding: 0;
	max-width: none;
	font-size: 0.6875rem;
	font-weight: 500;
	color: var(--a11y-muted);
	line-height: 1.3;
	font-family: var(--a11y-font);
	text-transform: uppercase;
	letter-spacing: 0.04em;
}

.a11y-row-label svg {
	color: var(--a11y-accent);
}

/* Narrow viewports / high zoom: stack options instead of scrolling sideways */
@media (max-width: 47.9375em) {
	.a11y-rows-wrap {
		flex-direction: column;
		overflow-x: visible;
	}

	.a11y-row {
		flex: 0 0 auto;
		min-width: 0;
		flex-direction: row;
		flex-wrap: wrap;
		justify-content: space-between;
		gap: 0.5rem 0.75rem;
		padding: 0.625rem 1.25rem;
		text-align: left;
		border-right: none;
		border-bottom: 1px solid var(--a11y-border);
	}

	.a11y-row:last-child {
		border-bottom: none;
	}

	.a11y-row-label {
		flex-direction: row;
		gap: 0.5rem;
		min-width: 0;
	}

	.a11y-row-label svg {
		flex-shrink: 0;
	}
}

/* ─── Font size stepper ──────────────────────────────────────────────────── */
.a11y-stepper {
	display: flex;
	border: 1px solid var(--a11y-border);
	border-radius: 9999px;
	overflow: hidden;
	flex-shrink: 0;
}

.a11y-step-btn {
	display: flex;
	align-items: center;
	justify-content: center;
	border: none;
	background: none;
	cursor: pointer;
	font-family: var(--a11y-font);
	font-weight: 600;
	color: var(--a11y-text);
	line-height: 1;
	transition: background-color var(--a11y-transition), color var(--a11y-transition);
	padding: 0 0.75rem;
	height: 1.875rem;
	white-space: nowrap;
}

.a11y-step-normal { font-size: 0.6875rem; }
.a11y-step-md     { font-size: 0.8125rem; }
.a11y-step-lg     { font-size: 0.9375rem; }

.a11y-step-btn + .a11y-step-btn {
	border-left: 1px solid var(--a11y-border);
}

.a11y-step-btn:hover,
.a11y-step-btn:focus-visible {
	background: var(--a11y-row-hover);
}

.a11y-step-btn[aria-pressed="true"] {
	background: var(--a11y-accent);
	color: #ffffff;
}

.a11y-step-btn:focus-visible {
	outline: 2px solid var(--a11y-accent);
	outline-offset: -2px;
}

/* ─── Toggle button ──────────────────────────────────────────────────────── */
.a11y-toggle-btn {
	display: flex;
	align-items: center;
	gap: 0.375rem;
	background: none;
	border: 1px solid var(--a11y-border);
	border-radius: 9999px;
	cursor: pointer;
	padding: 0.1875rem 0.625rem 0.1875rem 0.3125rem;
	height: 1.875rem;
	transition: border-color var(--a11y-transition), background-color var(--a11y-transition);
	flex-shrink: 0;
	font-family: var(--a11y-font);
}

.a11y-toggle-btn:hover {
	border-color: var(--a11y-accent);
}

.a11y-toggle-btn:focus-visible {
	outline: 2px solid var(--a11y-accent);
	outline-offset: 2px;
}

/* Track */
.a11y-toggle-track {
	position: relative;
	display: inline-flex;
	width: 1.75rem;
	height: 1rem;
	background: var(--a11y-border);
	border-radius: 9999px;
	transition: background-color var(--a11y-transition);
	flex-shrink: 0;
}

.a11y-toggle-thumb {
	position: absolute;
	top: 0.1875rem;
	left: 0.1875rem;
	width: 0.625rem;
	height: 0.625rem;
	background: #ffffff;
	border-radius: 50%;
	transition: transform var(--a11y-transition);
	box-shadow: 0 1px 3px rgba(0,0,0,.2);
}

/* Label text */
.a11y-toggle-label-on,
.a11y-toggle-label-off {
	font-size: 0.6875rem;
	font-weight: 500;
	line-height: 1;
}

.a11y-toggle-label-on  { display: none;  color: var(--a11y-accent); }
.a11y-toggle-label-off { display: block; color: var(--a11y-muted); }

/* Active state */
.a11y-toggle-btn[aria-pressed="true"] {
	border-color: var(--a11y-accent);
	background: color-mix(in srgb, var(--a11y-accent) 8%, transparent);
}

.a11y-toggle-btn[aria-pressed="true"] .a11y-toggle-track {
	background: var(--a11y-accent);
}

.a11y-toggle-btn[aria-pressed="true"] .a11y-toggle-thumb {
	transform: translateX(0.75rem);
}

.a11y-toggle-btn[aria-pressed="true"] .a11y-toggle-label-on  { display: block; }
.a11y-toggle-btn[aria-pressed="true"] .a11y-toggle-label-off { display: none; }

/* ─── Panel footer / reset ───────────────────────────────────────────────── */
.a11y-panel-footer {
	padding: 0.5rem 1.25rem;
	border-top: 1px solid var(--a11y-border);
	display: flex;
	justify-content: center;
}

.a11y-reset-btn {
	display: inline-flex;
	align-items: center;
	justify-content: center;
	font-family: var(--a11y-font);
	font-size: 0.75rem;
	font-weight: 500;
	color: var(--a11y-muted);
	background: none;
	border: 1px solid var(--a11y-border);
	border-radius: 9999px;
	padding: 0.3125rem 1.25rem;
	cursor: pointer;
	transition: color var(--a11y-transition), border-color var(--a11y-transition), background-color var(--a11y-transition);
}

.a11y-reset-btn:hover,
.a11y-reset-btn:focus-visible {
	color: var(--a11y-text);
	border-color: var(--a11y-text);
	background: var(--a11y-row-hover);
}

.a11y-reset-btn:focus-visible {
	outline: 2px solid var(--a11y-accent);
	outline-offset: 2px;
}

/* ─── Reduced motion ─────────────────────────────────────────────────────── */
@media (prefers-reduced-motion: reduce) {
	.a11y-panel,
	.a11y-btn-open,
	.a11y-toggle-track,
	.a11y-toggle-thumb,
	.a11y-step-btn,
	.a11y-toggle-btn {
		transition: none;
		animation: none;
	}
}

/* ─── Page-level accessibility overrides ─────────────────────────────────── */

html.a11y-font-md { font-size: 115% !important; }
html.a11y-font-lg { font-size: 130% !important; }

html.a11y-contrast,
html.a11y-contrast body {
	background-color: #000000 !important;
	color: #ffffff !important;
}

html.a11y-contrast *:not(#a11y-widget):not(#a11y-widget *) {
	background-color: #000000 !important;
	color: #ffffff !important;
	border-color: #ffffff !important;
}

html.a11y-contrast img:not(#a11y-widget img) {
	filter: brightness(0.9) contrast(1.1);
}

html.a11y-dyslexia *:not(#a11y-widget):not(#a11y-widget *) {
	font-family: 'OpenDyslexic', sans-serif !important;
}

html.a11y-spacing *:not(#a11y-widget):not(#a11y-widget *) {
	line-height: 1.8 !important;
}

html.a11y-links a:not(#a11y-widget a) {
	text-decoration: underline !important;
	text-underline-offset: 3px !important;
	font-weight: 700 !important;
}

html.a11y-grayscale {
	filter: grayscale(100%);
}

html.a11y-grayscale #a11y-widget {
	filter: none;
}
