/* =============================================
   移动端优化样式
   - 触摸交互增强
   - 响应式布局改进
   - 安全区域适配
   - 滚动与动画优化
   ============================================= */

/* ===== 1. 全局移动端基础 ===== */

/* 视口元标签配合 */
@media (max-width: 768px) {
    html {
        -webkit-text-size-adjust: 100%;
        -webkit-tap-highlight-color: transparent;
        scroll-behavior: smooth;
    }

    body {
        -webkit-overflow-scrolling: touch;
        overscroll-behavior-y: contain;
    }

    /* 安全区域适配（刘海屏/底部横条） */
    .news-flash-page {
        padding-left: env(safe-area-inset-left);
        padding-right: env(safe-area-inset-right);
        padding-bottom: env(safe-area-inset-bottom);
    }
}

/* ===== 2. 触摸目标优化 ===== */

/* Apple HIG 建议最小触摸目标 44x44px */
@media (max-width: 768px) {
    /* 所有可点击元素的最小触摸区域 */
    .btn:not(.action-btn),
    .like-button,
    .favorite-button,
    .comment-toggle,
    .feedback-toggle,
    .dark-mode-toggle:not(.action-btn),
    .views-button,
    .edit-news-flash,
    .search-btn,
    .close-modal,
    .mobile-category-toggle,
    .mobile-sidebar-close,
    .toggle-all-categories,
    .news-flash-pagination a,
    .news-flash-pagination span,
    .category-item a,
    .parent-header a,
    .modern-meta-tag {
        min-height: 44px;
        min-width: 44px;
        display: inline-flex;
        align-items: center;
        justify-content: center;
    }

    /* 分页按钮间距加大 */
    .news-flash-pagination a,
    .news-flash-pagination span {
        padding: 10px 14px;
        margin: 0 2px;
    }

    /* 操作按钮组间距 */
    .news-flash-action-buttons {
        gap: 16px;
    }

    .news-flash-action-buttons .action-btn {
        min-height: 48px;
        min-width: 48px;
        padding: 12px 20px;
        font-size: 14px;
    }

    /* 分类列表项触摸区域 */
    .parent-header {
        min-height: 48px;
    }

    .category-child a {
        min-height: 40px;
        display: flex;
        align-items: center;
    }

    /* 搜索按钮 */
    .search-btn {
        min-width: 48px;
        min-height: 48px;
    }

    /* 搜索输入框 */
    .search-input-wrapper input[type="text"] {
        min-height: 48px;
        font-size: 16px; /* 防止iOS自动缩放 */
        padding: 12px 16px;
    }

    /* 表单输入框 */
    input[type="text"],
    input[type="email"],
    input[type="url"],
    input[type="password"],
    input[type="search"],
    input[type="number"],
    textarea,
    select {
        font-size: 16px; /* 防止iOS自动缩放 */
        min-height: 44px;
    }

    /* 评论区输入框 */
    .comment-form textarea {
        min-height: 80px;
        font-size: 16px;
    }
}

/* ===== 3. 触摸反馈效果 ===== */

@media (max-width: 768px) {
    /* 触摸按下反馈 - 缩放效果 */
    .btn:active,
    .like-button:active,
    .favorite-button:active,
    .action-btn:active,
    .comment-toggle:active,
    .feedback-toggle:active {
        transform: scale(0.95);
        transition: transform 0.1s ease;
    }

    /* 卡片触摸反馈 */
    .news-flash-item:active {
        transform: scale(0.98);
        transition: transform 0.15s ease;
    }

    /* 分类项触摸反馈 */
    .parent-header:active,
    .category-child a:active {
        background-color: rgba(0, 0, 0, 0.05);
        transition: background-color 0.1s ease;
    }

    body.dark-mode .parent-header:active,
    body.dark-mode .category-child a:active {
        background-color: rgba(255, 255, 255, 0.05);
    }

    /* 移除桌面端hover效果，避免移动端粘滞 */
    .btn:hover,
    .like-button:hover,
    .favorite-button:hover,
    .news-flash-item:hover,
    .views-button:hover,
    .edit-news-flash:hover {
        transform: none;
        box-shadow: none;
    }

    /* 波纹效果容器 */
    .touch-ripple {
        position: absolute;
        border-radius: 50%;
        background: rgba(0, 0, 0, 0.1);
        transform: scale(0);
        animation: ripple-effect 0.5s ease-out;
        pointer-events: none;
    }

    body.dark-mode .touch-ripple {
        background: rgba(255, 255, 255, 0.15);
    }

    @keyframes ripple-effect {
        to {
            transform: scale(4);
            opacity: 0;
        }
    }
}

/* ===== 4. 移动端侧边栏优化 ===== */

@media (max-width: 768px) {
    /* 侧边栏滑出面板 */
    .news-flash-sidebar {
        position: fixed;
        top: 0;
        left: -100%;
        width: 85%;
        max-width: 320px;
        height: 100vh;
        height: 100dvh; /* 动态视口高度 */
        z-index: 1002;
        background: #fff;
        overflow-y: auto;
        -webkit-overflow-scrolling: touch;
        overscroll-behavior-y: contain;
        transition: left 0.3s cubic-bezier(0.4, 0, 0.2, 1);
        box-shadow: none;
        margin: 0;
        padding: 0;
        border-radius: 0;
    }

    body.dark-mode .news-flash-sidebar {
        background: #1e293b;
    }

    .news-flash-sidebar.mobile-open {
        left: 0;
        box-shadow: 4px 0 24px rgba(0, 0, 0, 0.2);
    }

    /* 侧边栏遮罩层 */
    .mobile-sidebar-overlay {
        position: fixed;
        top: 0;
        left: 0;
        width: 100%;
        height: 100%;
        background: rgba(0, 0, 0, 0);
        z-index: 1001;
        pointer-events: none;
        transition: background 0.3s ease;
    }

    .mobile-sidebar-overlay.active {
        background: rgba(0, 0, 0, 0.5);
        pointer-events: auto;
        backdrop-filter: blur(2px);
    }

    /* 侧边栏头部 */
    .mobile-sidebar-header {
        display: flex;
        align-items: center;
        justify-content: space-between;
        padding: 16px 20px;
        border-bottom: 1px solid #e2e8f0;
        position: sticky;
        top: 0;
        background: #fff;
        z-index: 1;
    }

    body.dark-mode .mobile-sidebar-header {
        background: #1e293b;
        border-bottom-color: #334155;
    }

    .mobile-sidebar-header h3 {
        margin: 0;
        font-size: 16px;
        font-weight: 600;
    }

    .mobile-sidebar-close {
        width: 40px;
        height: 40px;
        border: none;
        background: #f1f5f9;
        border-radius: 10px;
        font-size: 16px;
        color: #64748b;
        cursor: pointer;
        display: flex;
        align-items: center;
        justify-content: center;
    }

    body.dark-mode .mobile-sidebar-close {
        background: #334155;
        color: #94a3b8;
    }

    /* 侧边栏内容区域 */
    .news-flash-sidebar .sidebar-module {
        padding: 16px 20px;
    }

    /* 浮动分类按钮 - 右下角，避开底部导航栏 */
    .mobile-category-toggle {
        position: fixed;
        bottom: 80px;
        right: 16px;
        left: auto;
        width: 44px;
        height: 44px;
        border-radius: 12px;
        background: linear-gradient(135deg, #3b82f6, #2563eb);
        color: #fff;
        border: none;
        font-size: 16px;
        cursor: pointer;
        z-index: 999;
        display: flex;
        align-items: center;
        justify-content: center;
        box-shadow: 0 2px 12px rgba(59, 130, 246, 0.35);
        transition: all 0.3s ease;
        /* 安全区域适配 */
        bottom: calc(80px + env(safe-area-inset-bottom));
    }

    .mobile-category-toggle:active {
        transform: scale(0.9);
    }

    body.dark-mode .mobile-category-toggle {
        background: linear-gradient(135deg, #2563eb, #1d4ed8);
        box-shadow: 0 4px 16px rgba(37, 99, 235, 0.4);
    }
}

/* ===== 5. 移动端模态框优化 ===== */

@media (max-width: 768px) {
    /* 模态框全屏化 */
    .modal-content,
    .comments-modal .modal-content,
    .my-knowledge-modal .modal-content {
        width: 100% !important;
        max-width: 100% !important;
        height: 100vh;
        height: 100dvh;
        margin: 0 !important;
        border-radius: 0 !important;
        max-height: 100vh;
        max-height: 100dvh;
        display: flex;
        flex-direction: column;
    }

    /* 模态框内容可滚动 */
    .comments-list,
    .modal-body {
        flex: 1;
        overflow-y: auto;
        -webkit-overflow-scrolling: touch;
        overscroll-behavior-y: contain;
    }

    /* 模态框头部固定 */
    .comments-modal .modal-content > h3 {
        position: sticky;
        top: 0;
        z-index: 1;
        padding: 16px 20px;
        /* 安全区域适配 */
        padding-top: calc(16px + env(safe-area-inset-top));
    }

    /* 评论输入区域固定在底部 */
    .comment-form-wrapper {
        position: sticky;
        bottom: 0;
        background: #fff;
        border-top: 1px solid #e2e8f0;
        padding: 12px 16px;
        padding-bottom: calc(12px + env(safe-area-inset-bottom));
    }

    body.dark-mode .comment-form-wrapper {
        background: #1e293b;
        border-top-color: #334155;
    }

    /* 关闭按钮加大 */
    .comments-modal .close-modal {
        width: 40px;
        height: 40px;
        font-size: 22px;
        top: 14px;
        right: 16px;
    }
}

/* ===== 6. 移动端内容阅读优化 ===== */

@media (max-width: 768px) {
    /* 文章内容区域 */
    .news-flash-single {
        padding: 16px;
    }

    .news-flash-single-content {
        font-size: 16px;
        line-height: 1.8;
        word-break: break-word;
        overflow-wrap: break-word;
    }

    /* 文章内图片自适应 */
    .news-flash-single-content img {
        max-width: 100%;
        height: auto;
        border-radius: 8px;
    }

    /* 文章内表格横向滚动 */
    .news-flash-single-content table {
        display: block;
        overflow-x: auto;
        -webkit-overflow-scrolling: touch;
        white-space: nowrap;
        max-width: 100%;
    }

    /* 文章内代码块横向滚动 */
    .news-flash-single-content pre {
        overflow-x: auto;
        -webkit-overflow-scrolling: touch;
        padding: 16px;
        font-size: 13px;
        border-radius: 8px;
        max-width: 100%;
    }

    .news-flash-single-content code {
        word-break: break-all;
    }

    /* 标题层级缩放 */
    .news-flash-single-content h1 { font-size: 22px; }
    .news-flash-single-content h2 { font-size: 20px; }
    .news-flash-single-content h3 { font-size: 18px; }
    .news-flash-single-content h4 { font-size: 16px; }

    /* 引用块 */
    .news-flash-single-content blockquote {
        padding: 12px 16px;
        margin: 12px 0;
        font-size: 15px;
    }

    /* Meta信息区域 */
    .modern-meta-info {
        flex-wrap: wrap;
        gap: 8px;
        font-size: 12px;
    }

    .modern-meta-item {
        font-size: 12px;
    }

    .modern-meta-tags {
        flex-wrap: wrap;
        gap: 6px;
    }

    .modern-meta-tag {
        font-size: 12px;
        padding: 4px 10px;
    }
}

/* ===== 7. 移动端列表优化 ===== */

@media (max-width: 768px) {
    /* 文章列表卡片 */
    .news-flash-item {
        padding: 14px;
        margin-bottom: 12px;
        border-radius: 12px;
    }

    /* 列表缩略图 */
    .news-flash-item-thumbnail img {
        border-radius: 8px;
    }

    /* 列表标题 */
    .news-flash-item-title {
        font-size: 15px;
        max-width: none;
        white-space: normal;
        -webkit-line-clamp: 2;
        line-clamp: 2;
        display: -webkit-box;
        -webkit-box-orient: vertical;
        overflow: hidden;
    }

    /* 列表摘要 */
    .news-flash-item-excerpt {
        font-size: 13px;
        line-height: 1.6;
        -webkit-line-clamp: 2;
        line-clamp: 2;
        display: -webkit-box;
        -webkit-box-orient: vertical;
        overflow: hidden;
    }

    /* 列表Meta信息 */
    .news-flash-item-meta {
        font-size: 12px;
        flex-wrap: wrap;
        gap: 8px;
    }
}

/* ===== 8. 移动端Banner优化 ===== */

@media (max-width: 768px) {
    .news-flash-banner {
        border-radius: 12px;
        overflow: hidden;
        margin-bottom: 16px;
    }

    .news-flash-banner img {
        height: auto;
        max-height: 180px;
        object-fit: cover;
    }

    .news-flash-banner-text {
        font-size: 16px;
        padding: 12px 16px;
    }
}

/* ===== 9. 移动端目录（TOC）优化 ===== */

@media (max-width: 768px) {
    /* TOC浮动面板 - 底部弹出 */
    .news-flash-toc-floating {
        position: fixed;
        top: auto;
        bottom: 0;
        left: 0;
        right: 0;
        width: 100%;
        max-width: 100%;
        max-height: 60vh;
        border-radius: 16px 16px 0 0;
        z-index: 1000;
        transform: translateY(100%);
        transition: transform 0.3s cubic-bezier(0.4, 0, 0.2, 1);
        padding-bottom: env(safe-area-inset-bottom);
    }

    .news-flash-toc-floating.toc-visible {
        transform: translateY(0);
    }

    .news-flash-toc-floating .toc-content {
        max-height: 50vh;
        overflow-y: auto;
        -webkit-overflow-scrolling: touch;
    }

    /* TOC拖拽指示器 */
    .toc-drag-handle {
        width: 40px;
        height: 4px;
        background: #cbd5e1;
        border-radius: 2px;
        margin: 8px auto;
        display: block;
    }

    body.dark-mode .toc-drag-handle {
        background: #475569;
    }
}

/* ===== 10. 下拉刷新指示器 ===== */

.pull-to-refresh-indicator {
    position: fixed;
    top: -60px;
    left: 50%;
    transform: translateX(-50%);
    width: 40px;
    height: 40px;
    background: #fff;
    border-radius: 50%;
    box-shadow: 0 2px 12px rgba(0, 0, 0, 0.15);
    display: flex;
    align-items: center;
    justify-content: center;
    z-index: 9998;
    transition: top 0.3s ease;
}

.pull-to-refresh-indicator.pulling {
    top: 16px;
}

.pull-to-refresh-indicator.refreshing {
    top: 16px;
}

.pull-to-refresh-indicator .refresh-icon {
    width: 24px;
    height: 24px;
    border: 2px solid #e2e8f0;
    border-top-color: #3b82f6;
    border-radius: 50%;
    transition: transform 0.2s ease;
}

.pull-to-refresh-indicator.refreshing .refresh-icon {
    animation: spin 0.8s linear infinite;
}

.pull-to-refresh-indicator .refresh-arrow {
    font-size: 18px;
    color: #3b82f6;
    transition: transform 0.2s ease;
}

.pull-to-refresh-indicator.threshold .refresh-arrow {
    transform: rotate(180deg);
}

body.dark-mode .pull-to-refresh-indicator {
    background: #1e293b;
    box-shadow: 0 2px 12px rgba(0, 0, 0, 0.3);
}

body.dark-mode .pull-to-refresh-indicator .refresh-icon {
    border-color: #334155;
    border-top-color: #60a5fa;
}

/* ===== 11. 返回顶部按钮 ===== */

@media (max-width: 768px) {
    .back-to-top {
        position: fixed;
        bottom: 24px;
        right: 16px;
        width: 44px;
        height: 44px;
        border-radius: 12px;
        background: rgba(255, 255, 255, 0.9);
        border: 1px solid #e2e8f0;
        color: #64748b;
        font-size: 16px;
        cursor: pointer;
        z-index: 998;
        display: flex;
        align-items: center;
        justify-content: center;
        box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
        opacity: 0;
        transform: translateY(20px);
        transition: all 0.3s ease;
        pointer-events: none;
        /* 安全区域适配 */
        bottom: calc(24px + env(safe-area-inset-bottom));
    }

    .back-to-top.visible {
        opacity: 1;
        transform: translateY(0);
        pointer-events: auto;
    }

    .back-to-top:active {
        transform: scale(0.9);
    }

    body.dark-mode .back-to-top {
        background: rgba(30, 41, 59, 0.9);
        border-color: #334155;
        color: #94a3b8;
    }
}

/* ===== 12. 骨架屏加载效果 ===== */

.skeleton {
    background: linear-gradient(90deg, #f1f5f9 25%, #e2e8f0 50%, #f1f5f9 75%);
    background-size: 200% 100%;
    animation: skeleton-loading 1.5s infinite;
    border-radius: 8px;
}

body.dark-mode .skeleton {
    background: linear-gradient(90deg, #1e293b 25%, #334155 50%, #1e293b 75%);
    background-size: 200% 100%;
}

@keyframes skeleton-loading {
    0% { background-position: 200% 0; }
    100% { background-position: -200% 0; }
}

.skeleton-title {
    height: 20px;
    width: 70%;
    margin-bottom: 12px;
}

.skeleton-text {
    height: 14px;
    width: 100%;
    margin-bottom: 8px;
}

.skeleton-text:last-child {
    width: 60%;
}

.skeleton-thumbnail {
    height: 120px;
    width: 100%;
    margin-bottom: 12px;
}

.skeleton-avatar {
    width: 32px;
    height: 32px;
    border-radius: 50%;
}

.skeleton-card {
    padding: 16px;
    margin-bottom: 12px;
    border-radius: 12px;
    background: #fff;
    border: 1px solid #e2e8f0;
}

body.dark-mode .skeleton-card {
    background: #1e293b;
    border-color: #334155;
}

/* ===== 13. 超小屏幕特殊处理 ===== */

@media (max-width: 375px) {
    .news-flash-page .container {
        padding: 0 8px;
    }

    .news-flash-item {
        padding: 12px;
    }

    .news-flash-single {
        padding: 12px;
    }

    .news-flash-action-buttons .action-btn {
        padding: 10px 14px;
        font-size: 13px;
    }

    .news-flash-header h1 {
        font-size: 18px;
    }

    .news-flash-single-title {
        font-size: 20px;
    }
}

/* ===== 14. 横屏适配 ===== */

@media (max-width: 768px) and (orientation: landscape) {
    /* 横屏时减少垂直间距 */
    .news-flash-banner {
        max-height: 120px;
    }

    .news-flash-banner img {
        max-height: 120px;
    }

    /* 横屏时模态框调整 */
    .modal-content,
    .comments-modal .modal-content {
        height: auto;
        max-height: 100vh;
        max-height: 100dvh;
    }

    /* 横屏时侧边栏宽度 */
    .news-flash-sidebar {
        max-width: 280px;
    }
}

/* ===== 15. 平板设备优化 (768px - 1024px) ===== */

@media (min-width: 769px) and (max-width: 1024px) {
    /* 平板两列布局 */
    .news-flash-wrapper {
        gap: 20px;
    }

    .news-flash-sidebar {
        min-width: 240px;
        max-width: 260px;
    }

    /* 平板触摸目标 */
    .btn,
    .like-button,
    .favorite-button {
        min-height: 40px;
    }

    /* 平板卡片间距 */
    .news-flash-item {
        padding: 16px;
    }
}

/* ===== 16. 打印样式优化 ===== */

@media print {
    .mobile-category-toggle,
    .mobile-sidebar-overlay,
    .mobile-sidebar-header,
    .back-to-top,
    .pull-to-refresh-indicator,
    .news-flash-toc-floating {
        display: none !important;
    }
}

/* ===== 17. 减少动画偏好 ===== */

@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;
    }

    .skeleton {
        animation: none;
        background: #f1f5f9;
    }

    body.dark-mode .skeleton {
        background: #1e293b;
    }
}

/* ===== 18. 高对比度模式 ===== */

@media (prefers-contrast: high) {
    @media (max-width: 768px) {
        .news-flash-item {
            border: 2px solid #000;
        }

        .btn {
            border: 2px solid currentColor;
        }

        body.dark-mode .news-flash-item {
            border-color: #fff;
        }
    }
}
