/**
 * WCAG 2.1 AA Accessibility Improvements
 *
 * This file contains accessibility fixes for:
 * 1. Text Contrast Compliance (4.5:1 for normal text)
 * 2. Large Text Contrast (3:1 for 18pt+ or 14pt+ bold)
 * 3. Placeholder Text Contrast (4.5:1)
 * 4. Focus States for Keyboard Navigation
 * 5. Custom Control Accessibility
 *
 * @package DemocraticServices
 * @since 3.2.0
 */

/* ==========================================================================
   1. TEXT CONTRAST FIXES - WCAG AA Compliant (4.5:1 minimum)
   ========================================================================== */

/* Body text - ensure dark enough color on light backgrounds */
body:not(.democratic-custom-content-layout),
.text-gray-600,
.text-gray-500 {
  color: var(
    --color-gray-700
  ) !important; /* gray-700 - 7.5:1 contrast on white */
}

/* Links inside long-form prose / editor-rendered content. Scoped to actual
   text-content contexts so it does NOT override deliberate button colors
   (e.g. hero CTAs, callouts) that already set text-white / text-gov-primary
   via utility classes. */
.entry-content:not(.democratic-custom-html-content):not(.democratic-visual-page-shell) a:not([class*="bg-"]):not([class*="text-"]):not(.wp-block-button__link),
.prose:not(.democratic-custom-html-content):not(.democratic-visual-page-shell) a:not([class*="bg-"]):not([class*="text-"]),
.democratic-block-content a:not([class*="bg-"]):not([class*="text-"]):not(.wp-block-button__link) {
  color: var(
    --color-blue-800
  ) !important; /* blue-800 - 7.21:1 contrast on white */
}

/* main a:hover,
main a:focus,
article a:hover,
article a:focus,
.content a:hover,
.content a:focus {
  color: var(
    --color-blue-900
  ) !important; 
} */

/* Gray text on light backgrounds - improve contrast */
.text-gray-400 {
  color: var(
    --color-gray-600
  ) !important; /* gray-600 - 5.74:1 contrast on white */
}

/* Navigation links in mobile menu */
#mobile-menu ul.mobile-menu-list a,
#mobile-menu ul.mobile-menu-list button,
#mobile-menu ul.mobile-menu-list .menu-link {
  color: var(
    --color-gray-900
  ) !important; /* gray-900 - 16:1 contrast on white */
}

/* Desktop header navigation - dark text on white background */
#masthead #primary-menu li a {
  color: var(
    --color-gray-700
  ) !important; /* gray-700 for white header background */
}

#masthead #primary-menu li a:hover,
#masthead #primary-menu li a:focus {
  color: var(
    --color-gray-900
  ) !important; /* gray-900 on hover - 16:1 contrast */
  background-color: color-mix(
    in srgb,
    var(--color-gov-primary) 15%,
    transparent
  ) !important;
}

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

#masthead #primary-menu li.current-menu-item a,
#masthead #primary-menu li.current_page_item a,
#masthead #primary-menu li.current-page-ancestor a {
  background-color: var(--democratic-color-primary) !important;
  color: var(--color-white) !important;
  border-radius: 0.5rem;
  padding: 0.35rem 0.6rem;
}

/* Footer text on dark background */
#colophon .text-gray-300 {
  color: var(
    --color-gray-200
  ) !important; /* gray-200 - 11.66:1 contrast on gray-900 */
}

#colophon .text-gray-400 {
  color: var(
    --color-gray-300
  ) !important; /* gray-300 - 9.42:1 contrast on gray-900 */
}

/* ==========================================================================
   2. LARGE TEXT CONTRAST FIXES - WCAG AA (3:1 minimum for 18pt+/14pt bold)
   ========================================================================== */

/* 
 * NOTE: We do NOT globally set heading colors here because:
 * 1. It overrides intentional text-white classes on dark backgrounds
 * 2. Tailwind classes should handle colors contextually
 * 3. Only specific problematic areas should be targeted
 */

/* Large headings on colored backgrounds maintain visibility */
.bg-purple-900 h1,
.bg-purple-900 h2,
.bg-purple-800 h1,
.bg-purple-800 h2,
.bg-gradient-to-r h1,
.bg-gradient-to-r h2,
.bg-gradient-to-br h1,
.bg-gradient-to-br h2 {
  color: var(--color-white) !important; /* White on purple - 12.63:1 contrast */
}

/* ==========================================================================
   3. PLACEHOLDER TEXT CONTRAST - WCAG AA (4.5:1 minimum)
   ========================================================================== */

/* Form input placeholders - improved contrast */
::placeholder {
  color: var(
    --color-gray-600
  ) !important; /* gray-600 - 5.74:1 contrast on white */
  opacity: 1;
}

:-ms-input-placeholder {
  color: var(--color-gray-600) !important;
}

::-ms-input-placeholder {
  color: var(--color-gray-600) !important;
}

/* Search input placeholders */
input[type="search"]::placeholder,
input[type="text"]::placeholder,
input[type="email"]::placeholder,
textarea::placeholder {
  color: var(--color-gray-600) !important; /* gray-600 - 5.74:1 contrast */
}

/* Placeholder for dark backgrounds */
.bg-gray-800 input::placeholder,
.bg-gray-900 input::placeholder,
.bg-purple-900 input::placeholder {
  color: var(
    --color-gray-400
  ) !important; /* gray-400 - 4.64:1 contrast on dark */
}

/* Override Tailwind placeholder classes for WCAG AA compliance (4.5:1 minimum) */
.placeholder-slate-400::placeholder,
.placeholder-gray-500::placeholder,
.placeholder-gray-400::placeholder {
  color: var(
    --color-gray-600
  ) !important; /* gray-600 - 5.74:1 contrast on white */
  opacity: 1 !important;
}

/* ==========================================================================
   4. FOCUS STATES - WCAG 2.4.7 Focus Visible
   ========================================================================== */

/* Global focus outline for all interactive elements */
*:focus {
  outline: none;
}

*:focus-visible {
  outline: 3px solid #ffdd00 !important; /* GOV.UK focus yellow - highly visible */
  outline-offset: 2px !important;
  border-radius: 4px;
}

/* Links focus state */
a:focus-visible {
  outline: 3px solid #ffdd00 !important;
  outline-offset: 2px !important;
  text-decoration: underline;
}

/* Button focus states */
button:focus-visible,
[type="button"]:focus-visible,
[type="submit"]:focus-visible,
[type="reset"]:focus-visible,
[role="button"]:focus-visible {
  outline: 3px solid #ffdd00 !important;
  outline-offset: 2px !important;
  box-shadow: 0 0 0 4px
    color-mix(in srgb, var(--color-gov-primary) 30%, transparent) !important;
}

/* Input focus states */
input:focus-visible,
textarea:focus-visible,
select:focus-visible {
  outline: 3px solid #ffdd00 !important;
  outline-offset: 0 !important;
  border-color: var(--color-gov-primary) !important;
  box-shadow: 0 0 0 3px
    color-mix(in srgb, var(--color-gov-primary) 20%, transparent) !important;
}

/* Menu items focus */
#primary-menu li a:focus-visible,
#mobile-menu li a:focus-visible,
nav a:focus-visible {
  outline: 3px solid #ffdd00 !important;
  outline-offset: 2px !important;
  background-color: color-mix(
    in srgb,
    var(--color-gov-primary) 10%,
    transparent
  ) !important;
}

/* Focus on white/light backgrounds */
.bg-white button:focus-visible,
.bg-gray-50 button:focus-visible,
.bg-gray-100 button:focus-visible {
  outline-color: var(--color-gov-primary) !important;
}

/* Focus on dark backgrounds */
.bg-purple-900 a:focus-visible,
.bg-purple-900 button:focus-visible,
.bg-gray-900 a:focus-visible,
.bg-gray-900 button:focus-visible,
#colophon a:focus-visible,
#colophon button:focus-visible {
  outline: 3px solid var(--color-focus-dark-bg) !important; /* purple-100 - visible on dark */
  outline-offset: 2px !important;
}

/* Header buttons focus states */
#mobile-menu-toggle:focus-visible,
#mobile-menu-close:focus-visible,
#search-toggle:focus-visible,
#font-size-increase:focus-visible,
#font-size-decrease:focus-visible {
  outline: 3px solid #ffdd00 !important;
  outline-offset: 2px !important;
  background-color: color-mix(
    in srgb,
    var(--color-gov-primary) 15%,
    transparent
  ) !important;
  transform: scale(1.05);
}

/* Back to top button focus */
#back-to-top:focus-visible {
  outline: 3px solid var(--color-white) !important;
  outline-offset: 3px !important;
  transform: scale(1.1);
}

/* Cookie consent banner – above all overlays (mobile menu, modals) and always clickable */
#cookie-consent-banner {
  pointer-events: auto !important;
  z-index: 2147483647 !important;
  position: fixed !important;
}
#cookie-consent-banner button,
#cookie-consent-banner .cookie-consent-btn {
  pointer-events: auto !important;
  cursor: pointer !important;
}

/* Cookie consent buttons focus */
#cookie-consent-banner button:focus-visible {
  outline: 3px solid #ffdd00 !important;
  outline-offset: 2px !important;
  transform: scale(1.02);
}

#reject-optional-cookies {
  color: #ffffff !important;
  background-color: #505a5f !important;
}

/* Front-page hero: opaque dark surfaces for WCAG (overlays must not use Customizer text colour). */
section.democratic-front-page-hero,
section.democratic-front-page-hero[role="banner"] {
  background-color: #0b0c0c !important;
}

section.democratic-front-page-hero .hero-image-overlay {
  background-image: linear-gradient(
    to bottom right,
    rgba(11, 12, 12, 0.92),
    rgba(11, 12, 12, 0.82),
    rgba(29, 112, 184, 0.35)
  ) !important;
}

section.democratic-front-page-hero h1 .hero-title-line1 {
  color: var(--hero-title-line1-color, #ffffff) !important;
}

section.democratic-front-page-hero h1 .hero-title-line2,
section.democratic-front-page-hero h1 .hero-title-accent,
section.democratic-front-page-hero h1 .text-gov-focus {
  color: var(--hero-title-line2-color, #ffdd00) !important;
}

section.democratic-front-page-hero h1 .hero-title-line3 {
  color: var(--hero-title-line3-color, #ffffff) !important;
}

section.democratic-front-page-hero .hero-stats-panel .text-white,
section.democratic-front-page-hero .hero-stats-panel [class*="text-white"],
section.democratic-front-page-hero .hero-stats-panel a.hero-stat-link {
  color: #ffffff !important;
}

section.democratic-front-page-hero .hero-stats-panel--glass {
  background-color: rgba(255, 255, 255, 0.12) !important;
  border-color: rgba(255, 255, 255, 0.22) !important;
  -webkit-backdrop-filter: blur(12px);
  backdrop-filter: blur(12px);
}

section.democratic-front-page-hero .hero-stats-panel--glass .hero-stat-link,
section.democratic-front-page-hero .hero-stats-panel--glass > div {
  background-color: rgba(255, 255, 255, 0.08) !important;
  border-color: rgba(255, 255, 255, 0.18) !important;
}

section.democratic-front-page-hero .hero-stats-panel--glass a.hero-stat-link:hover,
section.democratic-front-page-hero .hero-stats-panel--glass a.hero-stat-link:focus-visible {
  border-color: #8ecae6 !important;
}

section.democratic-front-page-hero .hero-stats-panel--solid {
  background-color: #383f43 !important;
  border-color: #505a5f !important;
}

section.democratic-front-page-hero .hero-stats-panel--solid > div,
section.democratic-front-page-hero .hero-stats-panel--solid .grid > div,
section.democratic-front-page-hero .hero-stats-panel--solid .grid > a.hero-stat-link,
section.democratic-front-page-hero .hero-stats-panel--solid > a.hero-stat-link {
  background-color: #1a252d !important;
  border-color: #505a5f !important;
}

section.democratic-front-page-hero .hero-stats-panel--solid a.hero-stat-link:hover,
section.democratic-front-page-hero .hero-stats-panel--solid a.hero-stat-link:focus-visible {
  border-color: #8ecae6 !important;
}

/* Brand blue on dark stat tiles fails WCAG 3:1 for large text; use light accent for icons only. */
section.democratic-front-page-hero .hero-stats-panel .text-gov-primary {
  color: #8ecae6 !important;
}

section.democratic-front-page-hero .hero-stats-panel .text-2xl.text-gov-primary,
section.democratic-front-page-hero .hero-stats-panel .text-3xl.text-gov-primary,
section.democratic-front-page-hero .hero-stats-panel [class*="text-2xl"][class*="text-gov-primary"],
section.democratic-front-page-hero .hero-stats-panel [class*="text-3xl"][class*="text-gov-primary"],
section.democratic-front-page-hero .hero-stats-panel [class*="text-4xl"][class*="text-gov-primary"] {
  color: #ffffff !important;
}

#footer-newsletter-success .success-text {
  color: #005a30;
}

#footer-newsletter-error .error-text {
  color: #942514;
}

/* ==========================================================================
   5. KEYBOARD NAVIGATION INDICATORS
   ========================================================================== */

/* Show focus indicator for keyboard users only */
.js-focus-visible :focus:not(.focus-visible) {
  outline: none;
}

.js-focus-visible .focus-visible {
  outline: 3px solid #ffdd00 !important;
  outline-offset: 2px !important;
}

/* Keyboard navigation for dropdown/submenu items */
.menu-item-has-children > a:focus-visible + .sub-menu,
.menu-item-has-children:focus-within .sub-menu {
  display: block !important;
  visibility: visible !important;
  opacity: 1 !important;
}

/* ==========================================================================
   6. SKIP LINK ENHANCEMENTS
   ========================================================================== */

/* Enhanced skip link - already in header but ensure high visibility */
.skip-to-content-link:focus,
.skip-to-content-link:focus-visible {
  position: fixed !important;
  top: 1rem !important;
  left: 1rem !important;
  z-index: 999999 !important;
  padding: 1rem 1.5rem !important;
  background-color: var(--color-gov-primary) !important;
  color: var(--color-white) !important;
  font-weight: 700 !important;
  font-size: 1rem !important;
  border-radius: 0.5rem !important;
  box-shadow: 0 25px 50px -12px rgba(0, 0, 0, 0.5) !important;
  outline: 4px solid #ffdd00 !important;
  outline-offset: 2px !important;
  text-decoration: none !important;
}

/* ==========================================================================
   7. CUSTOM CONTROL ACCESSIBILITY
   ========================================================================== */

/* Mobile menu - ensure keyboard accessible */
#mobile-menu.is-open {
  /* Trap focus within mobile menu when open */
}

#mobile-menu[aria-hidden="true"] {
  visibility: hidden;
}

#mobile-menu[aria-hidden="false"] {
  visibility: visible;
}

/* Interactive elements must have visible focus */
[role="button"],
[role="menuitem"],
[role="tab"],
[role="option"],
[tabindex="0"] {
  cursor: pointer;
}

[role="button"]:focus-visible,
[role="menuitem"]:focus-visible,
[role="tab"]:focus-visible,
[role="option"]:focus-visible,
[tabindex="0"]:focus-visible {
  outline: 3px solid #ffdd00 !important;
  outline-offset: 2px !important;
}

/* Google Translate select: keep selected text black even when parent
   inherits link colors from the header. */
.democratic-header-gtranslate-wrap select,
.democratic-google-translate-widget select,
.goog-te-combo {
  color: #0b0c0c !important;
  background-color: #ffffff !important;
}
.democratic-google-translate-widget select option,
.goog-te-combo option {
  color: #0b0c0c;
  background-color: #ffffff;
}

/* Disabled states - ensure they look disabled */
[disabled],
[aria-disabled="true"],
.disabled {
  opacity: 0.5;
  cursor: not-allowed;
  pointer-events: none;
}

/* ==========================================================================
   8. HIGH CONTRAST MODE SUPPORT
   ========================================================================== */

@media (prefers-contrast: high) {
  /* Increase all borders and outlines */
  *:focus-visible {
    outline-width: 4px !important;
  }

  /* Ensure text is fully opaque */
  body,
  p,
  span,
  a,
  li {
    opacity: 1 !important;
  }

  /* Enhance button visibility */
  button,
  .btn,
  [role="button"] {
    border: 2px solid currentColor !important;
  }
}

/* ==========================================================================
   9. REDUCED MOTION SUPPORT
   ========================================================================== */

@media (prefers-reduced-motion: reduce) {
  *,
  *::before,
  *::after {
    animation-duration: 0.01ms !important;
    animation-iteration-count: 1 !important;
    transition-duration: 0.01ms !important;
    scroll-behavior: auto !important;
  }
}

/* ==========================================================================
   10. SPECIFIC COMPONENT FIXES
   ========================================================================== */

/* Top bar text on gradient background */
.bg-gradient-to-r.from-purple-900 .text-xs,
.bg-gradient-to-r.from-purple-900 a {
  color: var(
    --color-white
  ) !important; /* Ensure white text on purple gradient */
}

/* Form labels */
label {
  color: var(--color-gray-900) !important; /* gray-900 */
  font-weight: 500;
}

/* Error messages - ensure visible */
.error-message,
.form-error,
[role="alert"] {
  color: var(--color-error-dark) !important; /* red-700 - 5.92:1 contrast */
  font-weight: 500;
}

/* Success messages */
.success-message {
  color: var(--color-success-dark) !important; /* green-700 - 4.69:1 contrast */
  font-weight: 500;
}

/* Table headers and cells */
th {
  color: var(--color-gray-900) !important;
  font-weight: 600;
}

td {
  color: var(--color-gray-700) !important; /* gray-700 */
}

/* Breadcrumbs */
.breadcrumb a {
  color: var(--color-blue-800) !important; /* blue-800 */
}

.breadcrumb .current {
  color: var(--color-gray-700) !important; /* gray-700 */
}

/* Card text */
.card p,
.card-body {
  color: var(--color-gray-700) !important; /* gray-700 */
}

/* Badge/Tag text */
.badge,
.tag {
  font-weight: 600;
}

/* Footer links - white background footer */
#colophon a {
  color: var(--color-blue-800) !important; /* blue-800 - 7.21:1 on white */
}

#colophon a:hover,
#colophon a:focus {
  color: var(--color-blue-900) !important; /* blue-900 - 8.59:1 on white */
}

/* Ensure icons next to text don't reduce contrast */
svg + span,
span + svg {
  color: inherit;
}

/* ==========================================================================
   WordPress Block Elements - WCAG AA Contrast Fixes
   ========================================================================== */

/* WordPress Search Block Button */
.wp-block-search__button,
.wp-block-search__button.wp-element-button {
  background-color: var(--gov-primary, #0b0c0c) !important;
  color: #ffffff !important;
  border: 2px solid var(--gov-primary, #0b0c0c) !important;
  font-weight: 500;
}

.wp-block-search__button:hover,
.wp-block-search__button:focus,
.wp-block-search__button.wp-element-button:hover,
.wp-block-search__button.wp-element-button:focus {
  background-color: var(--gov-primary-dark, #1d1d1d) !important;
  border-color: var(--gov-primary-dark, #1d1d1d) !important;
}

/* ==========================================================================
   MOBILE RESPONSIVE OVERFLOW PREVENTION
   ========================================================================== */

/* Prevent horizontal overflow on main content areas */
.site-main,
.member-profile,
article,
main {
  overflow-x: hidden;
  max-width: 100%;
}

/* Ensure content sections don't overflow */
.container {
  max-width: 100%;
}

/* Force word breaks on long text */
.prose,
.content,
article p,
article dd,
article td {
  word-wrap: break-word;
  overflow-wrap: break-word;
}

/* Responsive table handling */
@media screen and (max-width: 640px) {
  table {
    font-size: 0.875rem;
  }

  table th,
  table td {
    padding: 0.5rem 0.75rem;
  }
}

/* ==========================================================================
   10. MOBILE NAVIGATION FIX
   ========================================================================== */

/* Ensure mobile menu toggle button is visible and clickable on mobile screens */
#mobile-menu-toggle {
  display: flex !important;
  position: relative;
  z-index: 10020;
  pointer-events: auto !important;
  cursor: pointer !important;
  touch-action: manipulation;
  -webkit-tap-highlight-color: transparent;
}

/* Hide mobile menu toggle on desktop (lg and above: 1024px) */
@media screen and (min-width: 1024px) {
  #mobile-menu-toggle {
    display: none !important;
  }
}

/* Ensure search toggle is also clickable */
#search-toggle {
  position: relative;
  z-index: 10020;
  pointer-events: auto !important;
  cursor: pointer !important;
  touch-action: manipulation;
  -webkit-tap-highlight-color: transparent;
}

/* Ensure mobile menu overlay is properly hidden by default */
#mobile-menu.mobile-overlay {
  display: none;
  visibility: hidden;
  opacity: 0;
  pointer-events: none;
  position: fixed;
  inset: 0;
  z-index: 10010;
}

/* Show mobile menu when open */
#mobile-menu.mobile-overlay.is-open {
  display: block !important;
  visibility: visible !important;
  opacity: 1 !important;
  pointer-events: auto !important;
}

/* Hide mobile menu on desktop screens */
@media screen and (min-width: 1024px) {
  #mobile-menu.mobile-overlay,
  #mobile-menu.mobile-overlay.is-open {
    display: none !important;
  }
}
