/**
 * Mobile Article Optimization
 * Requirements: 7.1, 7.2, 7.4, 7.5, 7.7, 7.8
 * 
 * Comprehensive mobile optimizations for article display:
 * - Collapsible TOC on mobile
 * - Touch-friendly FloatingActionBar
 * - Optimized font sizes
 * - Scrollable tables
 * - Header hide on scroll
 */

/* ============================================
   Mobile-Specific Variables
   ============================================ */

:root {
    --mobile-breakpoint: 768px;
    --mobile-font-size-base: 17px;
    --mobile-line-height: 1.65;
    --mobile-touch-target: 44px;
    --mobile-spacing: 16px;
}

/* ============================================
   15.1: Mobile Layout Adaptations
   Requirements: 7.1, 7.7
   ============================================ */

@media (max-width: 768px) {
    /* Article page layout adjustments */
    .article-page {
        padding: 0;
    }
    
    .article-page__header {
        padding: var(--mobile-spacing);
    }
    
    .article-page__body {
        display: block;
        padding: 0;
    }
    
    /* Make TOC collapsible on mobile */
    .article-toc {
        position: relative;
        top: 0;
        max-height: none;
        margin: var(--mobile-spacing);
        margin-bottom: 24px;
        padding: var(--mobile-spacing);
        background-color: var(--color-bg-secondary, #f7fafc);
        border-radius: 12px;
        border: 1px solid var(--color-border, #e2e8f0);
        box-shadow: 0 2px 4px rgba(0, 0, 0, 0.05);
    }
    
    .article-toc__header {
        display: flex;
        align-items: center;
        justify-content: space-between;
        margin-bottom: 12px;
        padding-bottom: 12px;
        border-bottom: 1px solid var(--color-border, #e2e8f0);
        cursor: pointer;
        user-select: none;
    }
    
    .article-toc__title {
        margin: 0;
        font-size: 14px;
        font-weight: 600;
        color: var(--color-text, #1a202c);
        text-transform: uppercase;
        letter-spacing: 0.05em;
    }
    
    .article-toc__toggle {
        display: flex;
        align-items: center;
        justify-content: center;
        width: 32px;
        height: 32px;
        padding: 0;
        background: var(--color-bg, #ffffff);
        border: 1px solid var(--color-border, #e2e8f0);
        border-radius: 8px;
        cursor: pointer;
        color: var(--color-text-secondary, #64748b);
        transition: all 0.2s ease;
        font-size: 18px;
    }
    
    .article-toc__toggle:active {
        transform: scale(0.95);
        background: var(--color-bg-secondary, #f1f5f9);
    }
    
    .article-toc__toggle--active {
        transform: rotate(180deg);
    }
    
    .article-toc__list--collapsed {
        display: none;
    }
    
    .article-toc__list {
        animation: slideDown 0.3s ease;
    }
    
    @keyframes slideDown {
        from {
            opacity: 0;
            transform: translateY(-10px);
        }
        to {
            opacity: 1;
            transform: translateY(0);
        }
    }
    
    /* Content area */
    .article-page__content {
        padding: 0 var(--mobile-spacing);
        margin-bottom: 32px;
    }
    
    /* Spacer hidden on mobile */
    .article-page__spacer {
        display: none;
    }
    
    /* Footer adjustments */
    .article-page__footer {
        padding: 0 var(--mobile-spacing);
        margin-bottom: 32px;
    }
    
    /* ============================================
       Touch-Friendly FloatingActionBar
       Requirements: 7.2
       ============================================ */
    
    .floating-action-bar {
        bottom: 16px;
        right: 16px;
        left: auto;
        padding: 8px;
        gap: 8px;
        box-shadow: 0 4px 16px rgba(0, 0, 0, 0.2);
    }
    
    /* Ensure minimum touch target size of 44x44px */
    .floating-action-bar__btn {
        min-width: 44px;
        min-height: 44px;
        width: 44px;
        height: 44px;
        padding: 10px;
        border-radius: 50%;
    }
    
    /* Larger tap area for better touch interaction */
    .floating-action-bar__btn::before {
        content: '';
        position: absolute;
        top: -8px;
        left: -8px;
        right: -8px;
        bottom: -8px;
        border-radius: 50%;
    }
    
    /* Icon sizing for touch */
    .floating-action-bar__btn svg,
    .floating-action-bar__btn .icon {
        width: 24px;
        height: 24px;
    }
    
    /* Toast positioning */
    .floating-action-bar__toast {
        bottom: 80px;
        right: 16px;
        left: 16px;
        text-align: center;
        font-size: 15px;
        padding: 14px 20px;
    }
    
    /* ============================================
       Optimized Font Sizes for Mobile
       Requirements: 7.7
       ============================================ */
    
    /* Base article content */
    .article-page__content.prose {
        font-size: var(--mobile-font-size-base);
        line-height: var(--mobile-line-height);
    }
    
    /* Headings */
    .prose h1 {
        font-size: 1.875rem; /* 30px */
        line-height: 1.2;
        margin-top: 1.5em;
    }
    
    .prose h2 {
        font-size: 1.5rem; /* 24px */
        line-height: 1.3;
        margin-top: 1.75em;
    }
    
    .prose h3 {
        font-size: 1.25rem; /* 20px */
        line-height: 1.4;
        margin-top: 1.5em;
    }
    
    .prose h4 {
        font-size: 1.125rem; /* 18px */
        line-height: 1.4;
    }
    
    .prose h5,
    .prose h6 {
        font-size: 1rem; /* 16px */
        line-height: 1.5;
    }
    
    /* Article title */
    .article-page__title {
        font-size: 1.75rem; /* 28px */
        line-height: 1.25;
        margin-bottom: 16px;
    }
    
    /* Meta information */
    .article-page__meta {
        font-size: 14px;
    }
    
    /* Code blocks */
    .prose pre {
        font-size: 14px;
        padding: 12px;
        margin: 20px -16px; /* Extend to edges */
        border-radius: 0;
    }
    
    .prose code {
        font-size: 0.9em;
    }
    
    /* Blockquotes */
    .prose blockquote {
        font-size: 16px;
        padding: 12px 16px;
        margin: 20px 0;
    }
    
    /* Lists */
    .prose ul,
    .prose ol {
        padding-left: 24px;
    }
    
    .prose li {
        margin-bottom: 8px;
    }
    
    /* ============================================
       Touch-Friendly Interactive Elements
       Requirements: 7.2
       ============================================ */
    
    /* Back to top button */
    .back-to-top {
        width: 48px;
        height: 48px;
        bottom: 80px; /* Above floating action bar */
        right: 16px;
    }
    
    /* Text highlighter menu */
    .text-highlighter__menu {
        padding: 8px;
        gap: 8px;
    }
    
    .text-highlighter__btn {
        min-width: 44px;
        min-height: 44px;
        padding: 10px;
    }
    
    /* Share buttons */
    .share-modal__button {
        min-height: 44px;
        padding: 12px 20px;
        font-size: 16px;
    }
    
    /* Image lightbox controls */
    .image-lightbox__close,
    .image-lightbox__prev,
    .image-lightbox__next {
        min-width: 44px;
        min-height: 44px;
        padding: 12px;
    }
    
    /* Paragraph anchors */
    .paragraph-anchor {
        width: 40px;
        height: 40px;
    }
    
    /* Inline comments */
    .inline-comment-indicator {
        min-width: 44px;
        min-height: 44px;
    }
    
    /* ============================================
       Breadcrumbs on Mobile
       ============================================ */
    
    /* Breadcrumbs styles moved to breadcrumbs.css */
    
    /* ============================================
       Categories and Tags
       ============================================ */
    
    .article-page__categories {
        margin-bottom: 12px !important;
        gap: 6px !important;
    }
    
    .category-badge {
        font-size: 12px !important;
        padding: 4px 10px !important;
    }
    
    .article-page__tags {
        font-size: 13px;
        gap: 6px;
    }
    
    /* ============================================
       Author Box
       ============================================ */
    
    .author-box {
        flex-direction: column;
        text-align: center;
        padding: 20px;
    }
    
    .author-box__actions {
        flex-direction: column;
        width: 100%;
        gap: 8px;
    }
    
    .author-box__actions .btn {
        width: 100%;
        justify-content: center;
    }
    
    /* ============================================
       Related Articles Grid
       ============================================ */
    
    .grid--4 {
        grid-template-columns: 1fr;
        gap: 16px;
    }
}

/* ============================================
   Small Mobile Devices (< 480px)
   ============================================ */

@media (max-width: 480px) {
    .article-page__content.prose {
        font-size: 16px;
    }
    
    .article-page__title {
        font-size: 1.5rem; /* 24px */
    }
    
    .prose h1 {
        font-size: 1.625rem; /* 26px */
    }
    
    .prose h2 {
        font-size: 1.375rem; /* 22px */
    }
    
    .prose h3 {
        font-size: 1.125rem; /* 18px */
    }
    
    .floating-action-bar {
        bottom: 12px;
        right: 12px;
        padding: 6px;
        gap: 6px;
    }
    
    .floating-action-bar__btn {
        width: 40px;
        height: 40px;
        min-width: 40px;
        min-height: 40px;
    }
}

/* ============================================
   Landscape Mobile Orientation
   ============================================ */

@media (max-width: 768px) and (orientation: landscape) {
    .article-toc {
        margin: 12px;
        padding: 12px;
    }
    
    .article-page__content {
        padding: 0 12px;
    }
    
    .floating-action-bar {
        bottom: 12px;
        right: 12px;
    }
}

/* ============================================
   Accessibility: Reduce Motion
   ============================================ */

@media (prefers-reduced-motion: reduce) {
    .article-toc__list {
        animation: none;
    }
    
    .article-toc__toggle {
        transition: none;
    }
    
    .floating-action-bar__btn::before {
        display: none;
    }
}

/* ============================================
   Dark Mode Support
   ============================================ */

[data-theme="dark"] {
    @media (max-width: 768px) {
        .article-toc {
            background-color: var(--color-bg-secondary-dark, #1e293b);
            border-color: var(--color-border-dark, #334155);
        }
        
        .article-toc__toggle {
            background: var(--color-bg-dark, #0f172a);
            border-color: var(--color-border-dark, #334155);
            color: var(--color-text-secondary-dark, #94a3b8);
        }
        
        .article-toc__title {
            color: var(--color-text-dark, #f1f5f9);
        }
    }
}


/* ============================================
   15.3: Header Hide on Scroll (Mobile)
   Requirements: 7.5
   ============================================ */

.header--mobile-hideable {
    transition: transform 0.3s ease-in-out;
}

@media (max-width: 768px) {
    .header--mobile-hideable.header--hidden {
        transform: translateY(-100%);
    }
    
    /* Ensure smooth transition */
    .header {
        position: fixed;
        top: 0;
        left: 0;
        right: 0;
        z-index: 1000;
        background: var(--color-bg, #ffffff);
        box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
    }
    
    /* Add padding to body to compensate for fixed header */
    body {
        padding-top: 64px; /* Adjust based on actual header height */
    }
}

/* ============================================
   15.4: Scrollable Tables on Mobile
   Requirements: 7.8
   ============================================ */

@media (max-width: 768px) {
    /* Wrap tables in scrollable container */
    .prose table {
        display: block;
        width: 100%;
        overflow-x: auto;
        -webkit-overflow-scrolling: touch;
        margin: 20px 0;
        border-radius: 8px;
        box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
    }
    
    /* Table wrapper for scroll indicators */
    .table-wrapper {
        position: relative;
        overflow-x: auto;
        -webkit-overflow-scrolling: touch;
        margin: 20px -16px; /* Extend to edges on mobile */
        padding: 0 16px;
    }
    
    .table-wrapper::before,
    .table-wrapper::after {
        content: '';
        position: absolute;
        top: 0;
        bottom: 0;
        width: 20px;
        pointer-events: none;
        z-index: 1;
    }
    
    /* Left scroll indicator (shadow) */
    .table-wrapper::before {
        left: 0;
        background: linear-gradient(to right, 
            rgba(0, 0, 0, 0.1) 0%, 
            transparent 100%);
        opacity: 0;
        transition: opacity 0.3s ease;
    }
    
    /* Right scroll indicator (shadow) */
    .table-wrapper::after {
        right: 0;
        background: linear-gradient(to left, 
            rgba(0, 0, 0, 0.1) 0%, 
            transparent 100%);
    }
    
    /* Show left shadow when scrolled */
    .table-wrapper.scrolled::before {
        opacity: 1;
    }
    
    /* Hide right shadow when at end */
    .table-wrapper.scrolled-end::after {
        opacity: 0;
    }
    
    /* Ensure table doesn't shrink */
    .prose table {
        min-width: 600px; /* Minimum width to ensure readability */
    }
    
    /* Adjust table cell padding for mobile */
    .prose th,
    .prose td {
        padding: 10px 12px;
        font-size: 14px;
        white-space: nowrap;
    }
    
    /* Allow text wrapping in specific columns if needed */
    .prose td.wrap-text {
        white-space: normal;
        min-width: 150px;
    }
    
    /* Scroll hint for tables */
    .table-scroll-hint {
        display: flex;
        align-items: center;
        justify-content: center;
        gap: 8px;
        padding: 8px;
        margin-top: -16px;
        margin-bottom: 16px;
        font-size: 13px;
        color: var(--color-text-muted, #718096);
        background: var(--color-bg-secondary, #f7fafc);
        border-radius: 0 0 8px 8px;
        text-align: center;
    }
    
    .table-scroll-hint svg {
        width: 16px;
        height: 16px;
        animation: swipeHint 2s ease-in-out infinite;
    }
    
    @keyframes swipeHint {
        0%, 100% {
            transform: translateX(0);
        }
        50% {
            transform: translateX(10px);
        }
    }
    
    /* Hide scroll hint after user interacts */
    .table-wrapper.interacted + .table-scroll-hint {
        display: none;
    }
}

/* ============================================
   15.5: Viewport Meta Tag Support
   Requirements: 7.4
   ============================================ */

/* 
 * Note: Viewport meta tag should be added in the HTML head:
 * <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=5, user-scalable=yes">
 * 
 * This CSS ensures proper scaling behavior
 */

@media (max-width: 768px) {
    /* Prevent horizontal overflow */
    html {
        overflow-x: hidden;
    }
    
    body {
        overflow-x: hidden;
        width: 100%;
        position: relative;
    }
    
    /* Ensure images don't cause overflow */
    img {
        max-width: 100%;
        height: auto;
    }
    
    /* Prevent pre/code blocks from causing overflow */
    .prose pre {
        max-width: 100%;
        overflow-x: auto;
    }
    
    /* Ensure iframes are responsive */
    .prose iframe {
        max-width: 100%;
    }
}

/* ============================================
   Additional Mobile Optimizations
   ============================================ */

@media (max-width: 768px) {
    /* Improve tap targets for links in content */
    .prose a {
        padding: 2px 0;
        margin: -2px 0;
    }
    
    /* Better spacing for lists on mobile */
    .prose ul,
    .prose ol {
        margin: 16px 0;
    }
    
    /* Optimize images for mobile */
    .prose img {
        margin: 16px 0;
        border-radius: 8px;
    }
    
    /* Better blockquote styling on mobile */
    .prose blockquote {
        margin-left: 0;
        margin-right: 0;
        border-left-width: 3px;
    }
    
    /* Adjust figure captions */
    .prose figcaption {
        font-size: 13px;
        padding: 0 8px;
    }
    
    /* Optimize horizontal rules */
    .prose hr {
        margin: 24px 0;
    }
    
    /* Better code block scrolling */
    .prose pre {
        scrollbar-width: thin;
        scrollbar-color: rgba(255, 255, 255, 0.3) transparent;
    }
    
    .prose pre::-webkit-scrollbar {
        height: 6px;
    }
    
    .prose pre::-webkit-scrollbar-track {
        background: transparent;
    }
    
    .prose pre::-webkit-scrollbar-thumb {
        background: rgba(255, 255, 255, 0.3);
        border-radius: 3px;
    }
}

/* ============================================
   Performance Optimizations
   ============================================ */

@media (max-width: 768px) {
    /* Use GPU acceleration for animations */
    .header--mobile-hideable,
    .floating-action-bar,
    .back-to-top {
        will-change: transform;
    }
    
    /* Optimize repaints */
    .article-toc__toggle,
    .floating-action-bar__btn {
        will-change: transform;
    }
}

/* Remove will-change after animation completes */
.header--mobile-hideable:not(.header--hidden),
.floating-action-bar.visible,
.back-to-top.visible {
    will-change: auto;
}


/* ============================================
   Inline Comments Mobile Fix
   ============================================ */

@media (max-width: 768px) {
    /* Ensure inline comment wrappers use flexbox for proper alignment */
    .prose .inline-comment-wrapper {
        position: relative !important;
        display: flex !important;
        align-items: flex-start !important;
        gap: 8px !important;
        margin-bottom: 1.5rem;
        margin-right: 0;
        padding-right: 0 !important; /* Remove desktop padding */
        min-height: 44px !important; /* Minimum height for touch target */
        height: auto !important; /* Allow wrapper to grow with content */
    }
    
    /* Paragraph takes remaining space */
    .prose .inline-comment-wrapper > p {
        flex: 1 !important;
        margin: 0 !important;
        display: block !important;
        min-height: 0 !important; /* Allow paragraph to be any height */
        height: auto !important;
    }
    
    /* Position indicator inline with paragraph */
    .prose .inline-comment-indicator {
        position: relative !important;
        display: inline-flex !important;
        flex-shrink: 0 !important;
        right: auto !important;
        top: 2px !important;
        margin-left: 0 !important;
        opacity: 1 !important;
        min-width: 44px;
        min-height: 44px;
        width: 44px;
        height: 44px;
    }
    
    /* Ensure panel is full-screen modal on mobile */
    .inline-comment-panel {
        position: fixed !important;
        right: 0 !important;
        top: auto !important;
        bottom: 0 !important;
        left: 0 !important;
        width: 100% !important;
        max-height: 85vh !important;
        border-radius: 16px 16px 0 0 !important;
        z-index: 1001 !important;
    }
    
    /* Add backdrop for mobile panel */
    .inline-comment-panel::before {
        content: '';
        position: fixed;
        top: 0;
        left: 0;
        right: 0;
        bottom: 0;
        background: rgba(0, 0, 0, 0.5);
        z-index: -1;
    }
    
    /* Better panel body height */
    .inline-comment-panel .comment-panel-body {
        max-height: calc(85vh - 70px) !important;
    }
}

/* Small mobile devices */
@media (max-width: 480px) {
    .inline-comment-panel {
        max-height: 90vh !important;
    }
    
    .inline-comment-panel .comment-panel-body {
        max-height: calc(90vh - 70px) !important;
    }
}
