/**
 * Mobile Layout Stabilizer v1.3.6.2
 * แก้ปัญหา Layout Shift, Jittering, และ Instability บนมือถือ
 * 
 * @package PlantPedia
 * @version 1.3.6.2
 */

/* ========================================
   CRITICAL MOBILE FIXES
   ======================================== */

/* 1. Prevent Layout Shifts */
* {
    -webkit-tap-highlight-color: transparent;
}

html {
    -webkit-text-size-adjust: 100%;
    -ms-text-size-adjust: 100%;
    text-size-adjust: 100%;
}

body {
    -webkit-font-smoothing: antialiased;
    -moz-osx-font-smoothing: grayscale;
    text-rendering: optimizeLegibility;
}

/* 2. Fix iOS Safari Bounce/Overscroll */
html, body {
    overflow-x: hidden;
    overscroll-behavior-x: none;
    overscroll-behavior-y: contain;
}

body {
    position: relative;
    width: 100%;
    min-height: 100vh;
}

/* 3. Prevent Content Shifts During Scroll */
.site-wrapper,
.review-container {
    width: 100%;
    max-width: 100vw;
    overflow-x: hidden;
}

/* ========================================
   FIX ANIMATIONS ON MOBILE
   ======================================== */

@media (max-width: 991px) {
    /* Disable hover animations on touch devices */
    .taxonomy-chip:hover,
    .taxonomy-tag:hover,
    .gallery-thumb:hover,
    .spec-card:hover,
    .cta-btn:hover {
        transform: none !important;
    }
    
    /* Reduce motion for better stability */
    * {
        animation-duration: 0.3s !important;
        transition-duration: 0.2s !important;
    }
    
    /* Disable parallax/heavy animations */
    .hero-section,
    .review-hero {
        transform: none !important;
        will-change: auto !important;
    }
}

/* ========================================
   FIX VIEWPORT HEIGHT ISSUES (iOS Safari)
   ======================================== */

/* Use dvh (dynamic viewport height) when available */
@supports (height: 100dvh) {
    body {
        min-height: 100dvh;
    }
}

/* Fallback for older browsers */
@supports not (height: 100dvh) {
    body {
        min-height: 100vh;
        min-height: -webkit-fill-available;
    }
}

/* ========================================
   FIX FIXED/STICKY ELEMENTS
   ======================================== */

@media (max-width: 991px) {
    /* Ensure fixed elements don't cause layout shifts */
    .site-header {
        position: sticky;
        top: 0;
        z-index: 1000;
        width: 100%;
        transform: translateZ(0);
        -webkit-transform: translateZ(0);
        backface-visibility: hidden;
    }
    
    /* Fix sticky TOC on mobile */
    .table-of-contents {
        position: relative !important;
        top: auto !important;
        transform: none !important;
    }
    
    /* Fix floating elements */
    .floating-cta,
    .scroll-to-top {
        transform: translateZ(0);
        -webkit-transform: translateZ(0);
        will-change: transform;
    }
}

/* ========================================
   FIX IMAGES CAUSING LAYOUT SHIFTS
   ======================================== */

/* All images should have dimensions */
img {
    max-width: 100%;
    height: auto;
    display: block;
}

/* Product images with aspect ratio */
.product-image img,
.gallery-main img,
.featured-image img {
    width: 100%;
    height: 100%;
    object-fit: cover;
    display: block;
}

/* Gallery and product image containers */
.product-image,
.gallery-main,
.featured-image {
    position: relative;
    overflow: hidden;
    aspect-ratio: 1 / 1;
    background: #f8f9fa;
}

/* ========================================
   FIX FONT LOADING SHIFTS (FOUT/FOIT)
   ======================================== */

/* Ensure fonts load smoothly */
body {
    font-display: swap;
}

/* Prevent layout shift during font load */
.product-title,
.section-title,
h1, h2, h3 {
    font-display: optional;
}

/* ========================================
   FIX SCROLLING ISSUES
   ======================================== */

@media (max-width: 991px) {
    /* Smooth scrolling */
    html {
        scroll-behavior: smooth;
        scroll-padding-top: 80px;
    }
    
    /* Prevent momentum scrolling issues */
    body {
        -webkit-overflow-scrolling: touch;
    }
    
    /* Fix horizontal scroll */
    .hero-grid,
    .specs-cards-grid,
    .gallery-thumbnails {
        overflow-x: auto;
        overflow-y: hidden;
        -webkit-overflow-scrolling: touch;
        scrollbar-width: thin;
    }
}

/* ========================================
   FIX Z-INDEX STACKING ISSUES
   ======================================== */

@media (max-width: 991px) {
    /* Clear z-index hierarchy */
    .site-header { z-index: 1000; }
    .mobile-menu { z-index: 999; }
    .gallery-lightbox { z-index: 10000; }
    .modal { z-index: 9999; }
    .floating-cta { z-index: 998; }
    .scroll-to-top { z-index: 997; }
    
    /* Ensure proper stacking */
    .review-content,
    .product-gallery,
    .technical-specs-cards {
        position: relative;
        z-index: 1;
    }
}

/* ========================================
   FIX TOUCH/TAP TARGETS
   ======================================== */

@media (max-width: 991px) {
    /* Minimum touch target size: 44x44px */
    button,
    a,
    .cta-btn,
    .gallery-nav,
    .gallery-zoom-btn,
    .gallery-thumb {
        min-height: 44px;
        min-width: 44px;
        touch-action: manipulation;
    }
    
    /* Prevent double-tap zoom */
    button,
    .cta-btn,
    .gallery-add-button {
        touch-action: manipulation;
        user-select: none;
    }
}

/* ========================================
   FIX GRID/FLEX LAYOUT SHIFTS
   ======================================== */

@media (max-width: 991px) {
    /* Stable grid layouts */
    .specs-cards-grid {
        display: grid;
        grid-template-columns: repeat(auto-fit, minmax(140px, 1fr));
        gap: 12px;
    }
    
    /* Prevent flex wrap shifts */
    .cta-buttons {
        display: flex;
        flex-direction: column;
        gap: 10px;
    }
    
    /* Stable container widths */
    .review-container {
        width: 100%;
        max-width: 100%;
        padding-left: 16px;
        padding-right: 16px;
        margin-left: auto;
        margin-right: auto;
        box-sizing: border-box;
    }
}

/* ========================================
   FIX VIEWPORT UNITS ISSUES
   ======================================== */

@media (max-width: 991px) {
    /* Avoid 100vw causing horizontal scroll */
    .full-width-section {
        width: 100%;
        max-width: 100%;
        margin-left: 0;
        margin-right: 0;
    }
    
    /* Use calc() for safer viewport widths */
    .hero-section,
    .review-hero {
        width: 100%;
        max-width: 100vw;
        overflow-x: hidden;
    }
}

/* ========================================
   FIX CONTENT JUMPING (CLS)
   ======================================== */

@media (max-width: 991px) {
    /* Reserve space for images */
    .product-image,
    .gallery-main,
    .spec-card {
        min-height: 200px;
    }
    
    /* Prevent content reflow */
    .price-box,
    .product-meta,
    .rating-display {
        min-height: 32px;
        display: flex;
        align-items: center;
    }
    
    /* Stable button heights */
    .cta-btn {
        min-height: 48px;
        display: flex;
        align-items: center;
        justify-content: center;
    }
}

/* ========================================
   FIX INPUT/FORM ZOOM (iOS)
   ======================================== */

@media (max-width: 991px) {
    /* Prevent auto-zoom on input focus */
    input,
    select,
    textarea {
        font-size: 16px !important;
    }
}

/* ========================================
   FIX GALLERY SPECIFIC ISSUES
   ======================================== */

@media (max-width: 991px) {
    /* Stable gallery layout */
    .product-gallery {
        width: 100%;
        max-width: 100%;
        overflow: hidden;
    }
    
    .gallery-main {
        position: relative;
        width: 100%;
        aspect-ratio: 1 / 1;
        border-radius: 12px;
    }
    
    /* Fix thumbnail scrolling */
    .gallery-thumbnails {
        display: flex;
        overflow-x: auto;
        overflow-y: hidden;
        scroll-snap-type: x mandatory;
        -webkit-overflow-scrolling: touch;
        gap: 8px;
        padding: 8px 0;
    }
    
    .gallery-thumb {
        scroll-snap-align: start;
        flex-shrink: 0;
        width: 64px;
        height: 64px;
    }
    
    /* Prevent lightbox body scroll */
    body.lightbox-open {
        overflow: hidden;
        position: fixed;
        width: 100%;
        height: 100%;
    }
}

/* ========================================
   FIX CARDS LAYOUT SHIFTS
   ======================================== */

@media (max-width: 991px) {
    .spec-card {
        min-height: 140px;
        padding: 16px;
    }
    
    .spec-icon {
        width: 48px;
        height: 48px;
        flex-shrink: 0;
    }
    
    .spec-value {
        word-break: break-word;
        overflow-wrap: break-word;
    }
}

/* ========================================
   PERFORMANCE OPTIMIZATIONS
   ======================================== */

@media (max-width: 991px) {
    /* GPU acceleration for smooth rendering */
    .product-gallery,
    .specs-cards-grid,
    .gallery-lightbox,
    .spec-card,
    .cta-btn {
        transform: translateZ(0);
        -webkit-transform: translateZ(0);
        will-change: transform;
    }
    
    /* Reduce paint areas */
    .gallery-thumb,
    .spec-card {
        contain: layout style paint;
    }
    
    /* Optimize animations */
    @media (prefers-reduced-motion: reduce) {
        * {
            animation-duration: 0.01ms !important;
            animation-iteration-count: 1 !important;
            transition-duration: 0.01ms !important;
        }
    }
}

/* ========================================
   FIX SAFE AREAS (iPhone X+)
   ======================================== */

@media (max-width: 991px) {
    /* Support for iPhone notch/home indicator */
    body {
        padding-left: env(safe-area-inset-left);
        padding-right: env(safe-area-inset-right);
    }
    
    .site-header {
        padding-top: env(safe-area-inset-top);
    }
    
    .site-footer {
        padding-bottom: env(safe-area-inset-bottom);
    }
}

/* ========================================
   DEBUG MODE (Remove in production)
   ======================================== */

/* Uncomment to visualize layout shifts
@media (max-width: 991px) {
    * {
        outline: 1px solid rgba(255, 0, 0, 0.1);
    }
}
*/

/* ========================================
   SPECIFIC FIXES FOR PRODUCT REVIEW PAGE
   ======================================== */

@media (max-width: 991px) {
    /* Fix Breadcrumb */
    .breadcrumb,
    .breadcrumb-wrapper {
        overflow-x: auto;
        white-space: nowrap;
        -webkit-overflow-scrolling: touch;
        scrollbar-width: none;
    }
    
    .breadcrumb::-webkit-scrollbar {
        display: none;
    }
    
    /* Fix Trust Indicators (11 คน กำลังดูตอนนี้) */
    .trust-indicators {
        display: flex;
        flex-wrap: wrap;
        gap: 8px;
        padding: 12px;
        overflow: hidden;
    }
    
    .trust-item {
        flex-shrink: 0;
        animation: none !important;
        transform: none !important;
    }
    
    .trust-item.live {
        animation: none !important;
    }
    
    /* ปิด animation ของ dot กระพริบ */
    .live-dot {
        animation: none !important;
        opacity: 1;
    }
    
    /* Fix Hero Section */
    .review-hero {
        padding: 20px 0;
        overflow: hidden;
    }
    
    .hero-grid {
        display: flex;
        flex-direction: column;
        gap: 20px;
    }
    
    /* Fix Product Image/Gallery Container */
    .product-gallery {
        width: 100%;
        margin: 0 auto;
        overflow: visible;
        position: relative;
    }
    
    /* Main Gallery - Simple & Stable */
    .gallery-main {
        position: relative;
        width: 100%;
        aspect-ratio: 1 / 1;
        background: #f8f9fa;
        border-radius: 12px;
        overflow: hidden;
        display: flex;
        align-items: center;
        justify-content: center;
    }
    
    .gallery-main img {
        width: 100%;
        height: 100%;
        object-fit: cover;
        display: block;
        background: #f8f9fa;
    }
    
    /* Fix Gallery Thumbnails Scrolling */
    .gallery-thumbnails {
        display: flex;
        overflow-x: auto;
        overflow-y: hidden;
        scroll-snap-type: x mandatory;
        -webkit-overflow-scrolling: touch;
        gap: 8px;
        padding: 12px 0;
        margin-top: 12px;
        scrollbar-width: thin;
        scrollbar-color: #cbd5e0 transparent;
    }
    
    .gallery-thumbnails::-webkit-scrollbar {
        height: 4px;
    }
    
    .gallery-thumbnails::-webkit-scrollbar-track {
        background: transparent;
    }
    
    .gallery-thumbnails::-webkit-scrollbar-thumb {
        background: #cbd5e0;
        border-radius: 2px;
    }
    
    .gallery-thumb {
        scroll-snap-align: start;
        flex-shrink: 0;
        width: 64px;
        height: 64px;
        border-radius: 8px;
        border: 2px solid transparent;
        transition: border-color 0.2s ease;
    }
    
    .gallery-thumb:hover {
        transform: none !important;
        box-shadow: none !important;
    }
    
    .gallery-thumb.active {
        border-color: #27AE60;
    }
    
    /* Fix Navigation Arrows */
    .gallery-nav {
        position: absolute;
        top: 50%;
        transform: translateY(-50%);
        width: 40px;
        height: 40px;
        background: rgba(255, 255, 255, 0.95);
        border: 2px solid rgba(0, 0, 0, 0.1);
        border-radius: 50%;
        display: flex;
        align-items: center;
        justify-content: center;
        touch-action: manipulation;
        z-index: 10;
        box-shadow: 0 2px 8px rgba(0, 0, 0, 0.2);
    }
    
    .gallery-prev {
        left: 8px;
    }
    
    .gallery-next {
        right: 8px;
    }
    
    .gallery-nav svg {
        width: 24px;
        height: 24px;
        color: #2c3e50;
    }
    
    /* Fix Zoom Button */
    .gallery-zoom-btn {
        position: absolute;
        width: 40px;
        height: 40px;
        top: 12px;
        right: 12px;
        background: rgba(255, 255, 255, 0.95);
        border: 2px solid rgba(0, 0, 0, 0.1);
        border-radius: 50%;
        display: flex;
        align-items: center;
        justify-content: center;
        touch-action: manipulation;
        z-index: 10;
        box-shadow: 0 2px 8px rgba(0, 0, 0, 0.2);
    }
    
    .gallery-zoom-btn svg {
        width: 20px;
        height: 20px;
        color: #2c3e50;
    }
    
    /* Fix Counter */
    .gallery-counter {
        bottom: 12px;
        right: 12px;
        padding: 6px 12px;
        font-size: 12px;
    }
    
    /* Fix Product Summary */
    .product-summary {
        padding: 16px;
    }
    
    .product-title {
        font-size: 22px;
        line-height: 1.3;
        margin-bottom: 12px;
        word-break: break-word;
    }
    
    /* Fix Rating Display */
    .rating-display {
        display: flex;
        align-items: center;
        gap: 8px;
        min-height: 24px;
    }
    
    /* Fix Price Box */
    .price-box {
        display: flex;
        flex-direction: column;
        gap: 8px;
        padding: 16px;
        min-height: auto;
    }
    
    /* Fix CTA Buttons */
    .cta-buttons {
        display: flex;
        flex-direction: column;
        gap: 10px;
        padding: 0 16px;
    }
    
    .cta-btn {
        min-height: 48px;
        padding: 12px 20px;
        font-size: 15px;
        touch-action: manipulation;
        user-select: none;
    }
    
    .cta-btn:hover {
        transform: none !important;
    }
    
    /* Fix Spec Cards */
    .specs-cards-grid {
        display: grid;
        grid-template-columns: repeat(2, 1fr);
        gap: 12px;
        padding: 0 16px;
    }
    
    .spec-card {
        min-height: 120px;
        padding: 16px;
    }
    
    .spec-card:hover {
        transform: none !important;
    }
    
    .spec-icon {
        width: 48px;
        height: 48px;
        margin-bottom: 12px;
    }
    
    .spec-icon:hover {
        transform: none !important;
    }
    
    .spec-value {
        font-size: 14px;
        line-height: 1.4;
        word-break: break-word;
    }
    
    /* Fix Section Titles */
    .section-title {
        font-size: 20px;
        padding: 0 16px;
        margin-bottom: 20px;
    }
    
    /* Fix Review Container */
    .review-container {
        width: 100%;
        max-width: 100%;
        padding-left: 16px;
        padding-right: 16px;
        margin-left: auto;
        margin-right: auto;
        box-sizing: border-box;
    }
    
    /* Fix Hamburger Menu */
    .mobile-menu-toggle {
        width: 44px;
        height: 44px;
        display: flex;
        align-items: center;
        justify-content: center;
        touch-action: manipulation;
    }
    
    /* Prevent Menu Shifts */
    .site-header {
        position: sticky;
        top: 0;
        width: 100%;
        z-index: 1000;
        background: white;
        transform: translateZ(0);
        backface-visibility: hidden;
    }
}

/* ========================================
   EXTRA SMALL DEVICES (<400px)
   ======================================== */

@media (max-width: 400px) {
    /* Even smaller adjustments */
    .gallery-thumb {
        width: 56px;
        height: 56px;
    }
    
    .spec-card {
        min-height: 100px;
        padding: 12px;
    }
    
    .spec-icon {
        width: 40px;
        height: 40px;
    }
    
    .product-title {
        font-size: 20px;
    }
}

/* ========================================
   LANDSCAPE MODE
   ======================================== */

@media (max-width: 991px) and (orientation: landscape) {
    /* Adjust for landscape */
    .gallery-main {
        padding-top: 75%; /* 4:3 aspect ratio in landscape */
    }
    
    .specs-cards-grid {
        grid-template-columns: repeat(3, 1fr);
    }
}
