/**
 * COMPREHENSIVE VISUAL & RESPONSIVENESS FIXES v1.0.0
 * Date: 2026-01-12
 * 
 * This module contains ALL fixes from the comprehensive audit:
 * - Loading screen width (100vw fix)
 * - Search input overlap fixes
 * - Subscribe button glitch fixes
 * - Scrollbar jump prevention
 * - Mobile responsiveness fixes
 * - Dark mode conflicts
 * - Font loading (FOIT/FOUT) fixes
 * - Touch target improvements
 * - Accessibility enhancements
 * 
 * @package AI_WP_Manager
 * @since 1.0.0
 */

/* ========================================
   §1 — PRELOADER VIEWPORT COVERAGE FIX
   Force full viewport width without scrollbar gaps
   ======================================== */

#loader,
.preloader,
.loading-screen {
    /* CRITICAL: Use inset instead of width/height to prevent scrollbar gap */
    position: fixed !important;
    top: 0 !important;
    left: 0 !important;
    right: 0 !important;
    bottom: 0 !important;
    width: auto !important; /* Override any 100vw */
    height: auto !important; /* Override any 100vh */
    margin: 0 !important;
    padding: 0 !important;
    box-sizing: border-box !important;
    z-index: 999999 !important;
}

/* ========================================
   §2 — SEARCH INPUT TEXT OVERLAP FIX
   Prevent typed text from going under search icon
   ======================================== */

.search-form input.search-field,
input[type="search"],
.header-search input,
.site-header input[type="search"] {
    padding-right: 50px !important;
    box-sizing: border-box;
}

/* Ensure icon is positioned absolutely without shifting layout */
.search-form .search-submit,
.search-form i.fa-search,
.header-search__submit,
.header-search button {
    position: absolute !important;
    right: 12px !important;
    top: 50% !important;
    transform: translateY(-50%) !important;
    z-index: 10 !important;
    background: transparent !important;
    border: none !important;
    pointer-events: auto !important;
}

/* ========================================
   §3 — SUBSCRIBE BUTTON GLITCH FIX
   Prevent layout shift on hover (border changes)
   ======================================== */

.subscribe-form button,
input[type="submit"].subscribe-btn,
.newsletter-cta,
.newsletter-form__button {
    /* Reserve border space to prevent shift */
    border: 2px solid transparent !important;
    /* Hardware acceleration to prevent paint glitches */
    -webkit-backface-visibility: hidden !important;
    backface-visibility: hidden !important;
    transform: translateZ(0) !important;
    overflow: hidden !important;
    /* Prevent margin/padding changes on hover */
    transition: background-color 0.2s ease, border-color 0.2s ease, transform 0.2s ease !important;
}

.subscribe-form button:hover,
.newsletter-cta:hover {
    /* Maintain consistent spacing */
    margin: 0 !important;
}

/* ========================================
   §4 — SCROLLBAR JUMP PREVENTION
   Reserve space for scrollbar on Windows
   ======================================== */

html {
    /* Keeps space for scrollbar even when not needed */
    scrollbar-gutter: stable !important;
}

/* ========================================
   §5 — HORIZONTAL SCROLL PREVENTION
   Eliminate "ghost margin" on mobile
   ======================================== */

body,
html {
    overflow-x: hidden !important;
    width: 100% !important;
    max-width: 100vw !important;
}

/* Prevent any element from breaking the layout */
* {
    max-width: 100%;
}

/* ========================================
   §6 — MOBILE INPUT AUTO-ZOOM FIX (iOS)
   Prevent iPhone from zooming on focus
   ======================================== */

@media screen and (max-width: 768px) {
    input[type="text"],
    input[type="email"],
    input[type="search"],
    input[type="tel"],
    input[type="url"],
    input[type="number"],
    input[type="password"],
    textarea,
    select {
        font-size: 16px !important;
    }
}

/* ========================================
   §7 — TOUCH TARGET SIZE (44x44px Minimum)
   Accessibility requirement
   ======================================== */

.footer-links a,
.nav-links a,
.site-footer a,
nav a,
button,
.button,
a.button,
[role="button"] {
    padding: 12px 0 !important;
    display: inline-block !important;
    min-height: 44px !important;
    min-width: 44px !important;
}

/* ========================================
   §8 — STICKY HOVER ON MOBILE FIX
   Only apply hover effects on devices with mouse
   ======================================== */

@media (hover: hover) {
    .subscribe-btn:hover,
    .button:hover,
    .newsletter-cta:hover,
    a:hover {
        background-color: var(--color-primary-hover, #004D99) !important;
    }
}

/* Disable hover effects on touch devices */
@media (hover: none) {
    .subscribe-btn:hover,
    .button:hover,
    .newsletter-cta:hover {
        background-color: var(--color-primary, #0066CC) !important;
    }
}

/* ========================================
   §9 — TAP HIGHLIGHT REMOVAL
   Remove grey box on Android/mobile taps
   ======================================== */

a,
button,
input,
select,
textarea,
[role="button"],
.button {
    -webkit-tap-highlight-color: transparent !important;
}

/* ========================================
   §10 — BRANDED TEXT SELECTION
   Custom highlight color
   ======================================== */

::selection {
    background: var(--color-primary, #0066CC) !important;
    color: #ffffff !important;
}

::-moz-selection {
    background: var(--color-primary, #0066CC) !important;
    color: #ffffff !important;
}

/* ========================================
   §11 — MOMENTUM SCROLLING (iOS)
   Smooth glide physics on scrollable elements
   ======================================== */

.code-block,
.scroll-container,
pre,
.table-wrapper,
.overflow-scroll {
    -webkit-overflow-scrolling: touch !important;
}

/* ========================================
   §12 — BLURRY TEXT ANIMATION FIX
   Keep text sharp during transforms
   ======================================== */

.card:hover,
.post-card:hover,
[class*="card"]:hover,
[class*="animated"] {
    will-change: transform !important;
    -webkit-font-smoothing: antialiased !important;
    -moz-osx-font-smoothing: grayscale !important;
}

/* ========================================
   §13 — OVERSCROLL BEHAVIOR (MODAL TRAP)
   Contain scroll within modals/menus
   ======================================== */

.mobile-menu,
.modal-content,
.scrollable-sidebar,
.primary-nav.is-open,
[role="dialog"],
.aiwp-chatbot-container {
    overscroll-behavior: contain !important;
}

/* ========================================
   §14 — FLEXBOX SHRINK FIX
   Prevent long URLs/text from breaking layout
   ======================================== */

.flex-child-element,
.card,
.post-card,
[class*="card"],
article,
.content {
    min-width: 0 !important;
    overflow-wrap: break-word !important;
    word-wrap: break-word !important;
}

/* Force URLs to break */
a[href],
.url,
code,
pre {
    word-break: break-all !important;
    overflow-wrap: break-word !important;
}

/* ========================================
   §15 — FOCUS RING VISIBILITY
   Prevent outline from being cut off
   ======================================== */

.btn:focus,
.button:focus,
a:focus,
button:focus,
input:focus,
[role="button"]:focus {
    outline-offset: 2px !important;
}

/* Ensure parent containers don't clip focus rings */
.card,
.post-card,
[class*="card"],
.button-group {
    overflow: visible !important;
}

/* ========================================
   §16 — FONT LOADING (FOIT/FOUT) FIX
   Show fallback text immediately, swap when ready
   ======================================== */

@font-face {
    font-family: 'Inter';
    font-display: swap !important;
}

/* Prevent invisible text during font load */
html {
    font-display: swap !important;
}

/* ========================================
   §17 — AUTOFILL DARK MODE FIX
   Override Chrome's yellow autofill background
   ======================================== */

input:-webkit-autofill,
input:-webkit-autofill:hover,
input:-webkit-autofill:focus,
textarea:-webkit-autofill,
textarea:-webkit-autofill:hover,
textarea:-webkit-autofill:focus {
    -webkit-box-shadow: 0 0 0px 1000px var(--color-surface, #ffffff) inset !important;
    -webkit-text-fill-color: var(--color-text, #1a1a1a) !important;
    transition: background-color 5000s ease-in-out 0s !important;
}

[data-theme="dark"] input:-webkit-autofill,
[data-theme="dark"] input:-webkit-autofill:hover,
[data-theme="dark"] input:-webkit-autofill:focus {
    -webkit-box-shadow: 0 0 0px 1000px var(--color-surface, #1a1a1a) inset !important;
    -webkit-text-fill-color: var(--color-text, #ffffff) !important;
}

/* ========================================
   §18 — SYSTEM FONT REVERSION FIX
   Force form elements to inherit main font
   ======================================== */

input,
button,
select,
textarea {
    font-family: inherit !important;
    font-size: inherit !important;
}

/* ========================================
   §19 — IMAGE ASPECT RATIO PRESERVATION
   Prevent stretched/squashed images
   ======================================== */

.post-thumbnail img,
.card-image img,
.featured-image img,
img[class*="thumbnail"],
img[class*="featured"] {
    width: 100% !important;
    height: auto !important;
    object-fit: cover !important;
    object-position: center !important;
}

/* Specific height for cards */
.card img,
.post-card img {
    height: 250px !important;
}

/* ========================================
   §20 — CODE BLOCK OVERFLOW FIX
   Prevent horizontal page scroll
   ======================================== */

pre,
code,
.code-block,
.wp-block-code {
    max-width: calc(100vw - 4rem) !important;
    overflow-x: auto !important;
    white-space: pre-wrap !important;
    word-wrap: break-word !important;
}

/* ========================================
   §21 — NAVIGATION OVERLAP FIX
   Prevent items from overlapping at tablet sizes
   ======================================== */

@media (min-width: 768px) and (max-width: 1024px) {
    .site-header nav ul {
        gap: clamp(8px, 1.5vw, 16px) !important;
    }
    
    .site-header nav a {
        font-size: clamp(0.85rem, 1.2vw, 1rem) !important;
        padding: 0.5rem clamp(0.5rem, 1vw, 1rem) !important;
    }
}

/* ========================================
   §22 — NEWSLETTER FORM STACK ON MOBILE
   Input and button should stack vertically
   ======================================== */

@media (max-width: 640px) {
    .newsletter-form,
    .subscribe-form,
    .site-header__newsletter {
        flex-direction: column !important;
        gap: 12px !important;
    }
    
    .newsletter-form input,
    .subscribe-form input {
        width: 100% !important;
    }
    
    .newsletter-form button,
    .subscribe-form button {
        width: 100% !important;
    }
}

/* ========================================
   §23 — STATS GRID RESPONSIVE
   Prevent overlap at tablet sizes
   ======================================== */

.stats-grid,
[class*="stats-grid"],
.synth-stats-grid {
    display: grid !important;
    grid-template-columns: repeat(auto-fit, minmax(200px, 1fr)) !important;
    gap: clamp(1rem, 3vw, 2rem) !important;
}

@media (max-width: 900px) {
    .stats-grid,
    [class*="stats-grid"],
    .synth-stats-grid {
        grid-template-columns: repeat(2, 1fr) !important;
    }
}

@media (max-width: 600px) {
    .stats-grid,
    [class*="stats-grid"],
    .synth-stats-grid {
        grid-template-columns: 1fr !important;
    }
}

/* ========================================
   §24 — TOOLS GRID CARD SIZING
   Prevent cards from squeezing together
   ======================================== */

.tools-grid,
[class*="tools-grid"],
.ai-tools-grid {
    display: grid !important;
    grid-template-columns: repeat(auto-fit, minmax(280px, 1fr)) !important;
    gap: clamp(1.5rem, 3vw, 2.5rem) !important;
}

/* ========================================
   §25 — CHAT WIDGET FOOTER CLEARANCE
   Ensure footer links remain clickable
   ======================================== */

@media (max-width: 768px) {
    .aiwp-chat-launcher,
    .aiwp-chatbot-launcher,
    #aiwp-chatbot-launcher {
        bottom: 90px !important;
    }

    /* Keep pagination and footer content clickable/visible (launcher overlap guardrail) */
    .ajax-pagination,
    .pagination,
    .pagination-nav {
        margin-bottom: 96px !important;
    }

    .site-footer {
        padding-bottom: calc(var(--space-8, 2rem) + 96px) !important;
    }
    
    .site-footer a {
        padding: 12px 8px !important;
        min-height: 44px !important;
    }
}

/* ========================================
   §26 — THEME SWITCHER MOBILE POSITION
   Move to top-left to avoid chat button
   ======================================== */

@media (max-width: 640px) {
    .theme-toggle,
    [class*="theme-switcher"] {
        top: 80px !important;
        left: 10px !important;
        bottom: auto !important;
        right: auto !important;
    }
}

/* ========================================
   §27 — MODAL BACKGROUND SCROLL LOCK
   Prevent scrolling behind open modals
   ======================================== */

body.chat-open,
body.modal-open,
body.menu-open {
    overflow: hidden !important;
    height: 100vh !important;
    height: 100dvh !important;
}

/* ========================================
   §28 — LONG URL WRAPPING
   Force URLs to break at slashes
   ======================================== */

.content a[href*="http"],
.post-content a[href*="http"],
article a[href*="http"] {
    word-break: break-all !important;
    overflow-wrap: break-word !important;
    hyphens: auto !important;
}

/* ========================================
   §29 — HEADING RESPONSIVE SIZING
   Scale headings proportionally on small screens
   ======================================== */

h1,
.h1 {
    font-size: clamp(1.75rem, 5vw, 3rem) !important;
}

h2,
.h2 {
    font-size: clamp(1.5rem, 4vw, 2.5rem) !important;
}

h3,
.h3 {
    font-size: clamp(1.25rem, 3vw, 2rem) !important;
}

/* ========================================
   §30 — CODE BLOCK MOBILE SIZING
   Ensure code is readable without zooming
   ======================================== */

@media (max-width: 768px) {
    pre,
    code,
    .code-block {
        font-size: 0.75rem !important;
        -webkit-overflow-scrolling: touch !important;
    }
}

/* ========================================
   §31 — DARK MODE INPUT VISIBILITY
   Ensure form inputs have visible borders
   ======================================== */

[data-theme="dark"] input[type="text"],
[data-theme="dark"] input[type="email"],
[data-theme="dark"] input[type="search"],
[data-theme="dark"] textarea {
    border: 1px solid rgba(255, 255, 255, 0.2) !important;
    background: rgba(255, 255, 255, 0.05) !important;
    color: #ffffff !important;
}

/* ========================================
   §32 — LAYOUT SHIFT PREVENTION
   Explicit image dimensions
   ======================================== */

img {
    max-width: 100% !important;
    height: auto !important;
}

/* Reserve space for images before load */
.post-card__image,
.card__image {
    aspect-ratio: 16 / 9 !important;
    background: var(--color-surface, #f5f5f5) !important;
}

/* ========================================
   §33 — SCROLL JANK PREVENTION
   GPU-accelerated transforms
   ======================================== */

.card,
.post-card,
[class*="card"],
.animated-element {
    will-change: transform !important;
    transform: translateZ(0) !important;
}

/* Remove will-change after animation */
.card:not(:hover),
.post-card:not(:hover) {
    will-change: auto !important;
}

/* ========================================
   §34 — KEYBOARD TRAP IN MODAL
   Focus management
   ======================================== */

.modal.is-open,
.aiwp-chatbot-container.is-open,
[role="dialog"][aria-hidden="false"] {
    position: fixed !important;
    inset: 0 !important;
    z-index: 9999 !important;
}

/* ========================================
   §35 — Z-INDEX SYSTEM
   Organized stacking context
   ======================================== */

.site-header {
    z-index: 1000 !important;
}

.primary-nav.is-open {
    z-index: 1100 !important;
}

.modal,
[role="dialog"] {
    z-index: 2000 !important;
}

.aiwp-chat-launcher,
.aiwp-chatbot-launcher {
    z-index: 1500 !important;
}

.theme-toggle {
    z-index: 1200 !important;
}

.preloader,
#loader {
    z-index: 9999 !important;
}

/* ========================================
   §36 — HIGH CONTRAST MODE
   Force colors for accessibility
   ======================================== */

@media (prefers-contrast: high) {
    * {
        border-color: currentColor !important;
    }
    
    button,
    .button,
    a.button {
        border: 2px solid currentColor !important;
    }
}

/* ========================================
   §37 — REDUCED MOTION
   Respect user preferences
   ======================================== */

@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;
    }
}

/* ========================================
   §38 — PRINT STYLES
   Hide unnecessary elements
   ======================================== */

@media print {
    .site-header,
    .site-footer,
    .primary-nav,
    .newsletter-cta,
    .subscribe-form,
    .aiwp-chat-launcher,
    .theme-toggle,
    .social-share,
    [class*="launcher"],
    [class*="fab"] {
        display: none !important;
    }
    
    body {
        background: white !important;
        color: black !important;
    }
    
    a[href]::after {
        content: " (" attr(href) ")" !important;
        font-size: 0.8em !important;
    }
}

/* ========================================
   §39 — ORIENTATION HANDLING
   Landscape mobile adjustments
   ======================================== */

@media (max-height: 500px) and (orientation: landscape) {
    .site-header {
        height: 60px !important;
    }
    
    .hero,
    [class*="hero"] {
        padding-top: 2rem !important;
        padding-bottom: 2rem !important;
    }
    
    h1,
    .h1 {
        font-size: 1.75rem !important;
    }
}

/* ========================================
   §40 — NOTCHED DEVICES (iPhone X+)
   Safe area insets
   ======================================== */

@supports (padding: env(safe-area-inset-top)) {
    body {
        padding-top: env(safe-area-inset-top) !important;
        padding-bottom: env(safe-area-inset-bottom) !important;
        padding-left: env(safe-area-inset-left) !important;
        padding-right: env(safe-area-inset-right) !important;
    }
    
    .site-header {
        padding-top: env(safe-area-inset-top) !important;
    }
}

/* ========================================
   §41 — RETINA DISPLAYS
   Sharper rendering
   ======================================== */

@media (-webkit-min-device-pixel-ratio: 2), (min-resolution: 192dpi) {
    img {
        image-rendering: -webkit-optimize-contrast !important;
        image-rendering: crisp-edges !important;
    }
    
    svg {
        shape-rendering: geometricPrecision !important;
    }
}

/* ========================================
   §42 — CAROUSEL TEXT ON IMAGES
   Ensure text is always readable
   ======================================== */

.hero-carousel__slide h1,
.hero-carousel__slide h2,
.hero-carousel__slide p,
.hero-carousel__title,
.hero-carousel__excerpt {
    text-shadow: 0 2px 4px rgba(0, 0, 0, 0.8),
                 0 1px 2px rgba(0, 0, 0, 0.6) !important;
}

/* ========================================
   §43 — FORCED COLORS MODE
   High contrast mode on Windows
   ======================================== */

@media (forced-colors: active) {
    * {
        forced-color-adjust: auto !important;
    }
    
    button,
    .button {
        border: 2px solid ButtonText !important;
    }
}

/* ========================================
   §44 — ULTRAWIDESCREEN
   Prevent content from stretching too wide
   ======================================== */

@media (min-width: 2560px) {
    .container,
    .content-wrapper,
    main {
        max-width: 1920px !important;
        margin-left: auto !important;
        margin-right: auto !important;
    }
}

/* ========================================
   §45 — EMERGENCY VISIBILITY GUARANTEE
   Nuclear failsafe for critical elements
   ======================================== */

/* Ensure primary CTAs are never invisible */
.newsletter-cta,
.subscribe-btn,
[class*="primary-cta"],
button[type="submit"] {
    opacity: 1 !important;
    visibility: visible !important;
    display: inline-flex !important;
}

/* Ensure icons in CTAs are visible */
.newsletter-cta svg,
.subscribe-btn svg,
button[type="submit"] svg {
    opacity: 1 !important;
    visibility: visible !important;
    display: block !important;
}

/* ========================================
   END OF COMPREHENSIVE FIXES v1.0.0
   All 45 fix categories implemented
   ======================================== */
