/**
 * Threads Sync WordPress Plugin - Frontend Threads-Style Media Display
 * Version: 1.6.1
 * Description: 模仿Threads应用的真实媒体展示风格 - 横向滚动多图+灯箱预览
 */

/* ===== Threads风格媒体容器 ===== */
.threads-media-container {
    width: 100%;
    max-width: 100%;
    margin: 20px 0;
    border-radius: 12px;
    overflow: hidden;
    position: relative;
}

/* ===== 单张图片样式 ===== */
.threads-media-container img {
    width: 100%;
    height: auto;
    display: block;
    object-fit: cover;
    max-height: 600px;
}

/* ===== 单个视频样式 ===== */
.threads-media-container video {
    width: 100%;
    height: auto;
    display: block;
    max-height: 600px;
    outline: none;
}

/* ===== 多图Gallery样式 - Threads真实横向滚动布局 ===== */
.threads-gallery {
    display: flex;
    gap: 10px;
    background: transparent;
    margin: 20px 0;
    width: 100%;
    max-width: 100%;
    overflow-x: auto;
    overflow-y: hidden;
    -webkit-overflow-scrolling: touch;
    scrollbar-width: thin;
    scrollbar-color: rgba(0, 0, 0, 0.3) transparent;
    border-radius: 12px;
    position: relative;
}

/* 滚动条样式优化 */
.threads-gallery::-webkit-scrollbar {
    height: 6px;
}

.threads-gallery::-webkit-scrollbar-track {
    background: transparent;
}

.threads-gallery::-webkit-scrollbar-thumb {
    background: rgba(0, 0, 0, 0.3);
    border-radius: 3px;
}

.threads-gallery::-webkit-scrollbar-thumb:hover {
    background: rgba(0, 0, 0, 0.5);
}

/* Gallery Item 基础样式 - 固定高度200px，宽度自适应 */
.threads-gallery-item {
    position: relative;
    flex-shrink: 0;
    width: auto !important;
    height: 200px !important;
    overflow: hidden;
    border-radius: 8px;
    cursor: pointer;
    transition: transform 0.2s ease, opacity 0.2s ease;
}

.threads-gallery-item:hover {
    transform: scale(1.02);
    opacity: 0.95;
}

/* 单张图片特殊处理 - 移除overflow-x限制，但保持200px高度 */
.threads-gallery.gallery-count-1 {
    overflow-x: visible;
}

.threads-gallery.gallery-count-1 .threads-gallery-item {
    /* 保持200px高度，不覆盖 */
    width: auto !important;
    height: 200px !important;
    flex-shrink: 1;
}

/* 图片和视频内部定位 - 高度200px，宽度自动缩放 */
.threads-gallery-item img,
.threads-gallery-item video {
    width: auto !important;
    height: 200px !important;
    max-width: none !important;
    max-height: 200px !important;
    min-height: 200px !important;
    object-fit: contain !important;
    object-position: center !important;
    display: block !important;
}

/* 单张图片也保持200px高度 */
.threads-gallery.gallery-count-1 .threads-gallery-item img,
.threads-gallery.gallery-count-1 .threads-gallery-item video {
    width: auto !important;
    height: 200px !important;
    max-width: none !important;
    max-height: 200px !important;
    min-height: 200px !important;
    object-fit: contain !important;
    object-position: center !important;
}

/* 视频控件样式优化 */
.threads-gallery-item video {
    outline: none;
}

.threads-gallery-item video::-webkit-media-controls {
    opacity: 0;
    transition: opacity 0.3s;
}

.threads-gallery-item:hover video::-webkit-media-controls {
    opacity: 1;
}

/* 视频播放图标覆层 */
.threads-gallery-item.video-item::before {
    content: '';
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
    width: 40px;
    height: 40px;
    background: rgba(255, 255, 255, 0.9);
    border-radius: 50%;
    pointer-events: none;
    z-index: 1;
    opacity: 0.8;
    transition: opacity 0.3s;
}

.threads-gallery-item.video-item::after {
    content: '';
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-35%, -50%);
    width: 0;
    height: 0;
    border-style: solid;
    border-width: 8px 0 8px 14px;
    border-color: transparent transparent transparent #000;
    pointer-events: none;
    z-index: 2;
    opacity: 0.8;
    transition: opacity 0.3s;
}

.threads-gallery-item.video-item:hover::before,
.threads-gallery-item.video-item:hover::after {
    opacity: 1;
}

/* 视频正在播放时隐藏播放图标 */
.threads-gallery-item.video-item.playing::before,
.threads-gallery-item.video-item.playing::after {
    opacity: 0;
}

/* 单张大图的视频播放按钮 */
.threads-gallery.gallery-count-1 .threads-gallery-item.video-item::before {
    width: 60px;
    height: 60px;
}

.threads-gallery.gallery-count-1 .threads-gallery-item.video-item::after {
    border-width: 12px 0 12px 20px;
}

/* ===== 加载动画 ===== */
.threads-media-container.loading::before {
    content: '';
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
    width: 40px;
    height: 40px;
    border: 3px solid rgba(255, 255, 255, 0.3);
    border-top-color: #fff;
    border-radius: 50%;
    animation: threads-spin 0.8s linear infinite;
}

@keyframes threads-spin {
    to { transform: translate(-50%, -50%) rotate(360deg); }
}

/* ===== 灯箱/Lightbox 样式 ===== */
.threads-lightbox {
    display: none;
    position: fixed;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    background: rgba(0, 0, 0, 0.95);
    z-index: 999999;
    justify-content: center;
    align-items: center;
    animation: threads-lightbox-fadein 0.3s ease;
}

.threads-lightbox.active {
    display: flex;
}

@keyframes threads-lightbox-fadein {
    from {
        opacity: 0;
    }
    to {
        opacity: 1;
    }
}

.threads-lightbox-content {
    position: relative;
    max-width: 90vw;
    max-height: 90vh;
    animation: threads-lightbox-zoomin 0.3s ease;
}

@keyframes threads-lightbox-zoomin {
    from {
        transform: scale(0.8);
        opacity: 0;
    }
    to {
        transform: scale(1);
        opacity: 1;
    }
}

.threads-lightbox-content img,
.threads-lightbox-content video {
    max-width: 100%;
    max-height: 90vh;
    width: auto;
    height: auto;
    display: block;
    object-fit: contain;
    border-radius: 8px;
}

.threads-lightbox-close {
    position: absolute;
    top: 20px;
    right: 20px;
    width: 40px;
    height: 40px;
    background: rgba(255, 255, 255, 0.2);
    border: none;
    border-radius: 50%;
    color: #fff;
    font-size: 24px;
    line-height: 1;
    cursor: pointer;
    transition: background 0.2s ease;
    z-index: 1000000;
}

.threads-lightbox-close:hover {
    background: rgba(255, 255, 255, 0.3);
}

.threads-lightbox-close::before {
    content: '×';
}

/* 导航按钮 */
.threads-lightbox-nav {
    position: absolute;
    top: 50%;
    transform: translateY(-50%);
    width: 50px;
    height: 50px;
    background: rgba(255, 255, 255, 0.2);
    border: none;
    border-radius: 50%;
    color: #fff;
    font-size: 24px;
    cursor: pointer;
    transition: background 0.2s ease;
    z-index: 1000000;
}

.threads-lightbox-nav:hover {
    background: rgba(255, 255, 255, 0.3);
}

.threads-lightbox-prev {
    left: 20px;
}

.threads-lightbox-prev::before {
    content: '‹';
}

.threads-lightbox-next {
    right: 20px;
}

.threads-lightbox-next::before {
    content: '›';
}

/* 计数器 */
.threads-lightbox-counter {
    position: absolute;
    bottom: 20px;
    left: 50%;
    transform: translateX(-50%);
    background: rgba(0, 0, 0, 0.7);
    color: #fff;
    padding: 8px 16px;
    border-radius: 20px;
    font-size: 14px;
    z-index: 1000000;
}

/* ===== 响应式设计 ===== */
@media screen and (max-width: 768px) {
    .threads-gallery {
        border-radius: 8px;
        gap: 4px;
    }
    
    .threads-media-container {
        border-radius: 8px;
    }
    
    .threads-media-container img,
    .threads-media-container video {
        max-height: 450px;
    }
    
    .threads-gallery-item {
        width: auto !important;
        height: 200px !important;
    }
    
    .threads-gallery-item img,
    .threads-gallery-item video {
        width: auto !important;
        height: 200px !important;
        max-height: 200px !important;
        min-height: 200px !important;
    }
    
    .threads-gallery-item.video-item::before {
        width: 35px;
        height: 35px;
    }
    
    .threads-gallery-item.video-item::after {
        border-width: 7px 0 7px 12px;
    }
    
    .threads-lightbox-nav {
        width: 40px;
        height: 40px;
    }
    
    .threads-lightbox-close {
        width: 35px;
        height: 35px;
        top: 15px;
        right: 15px;
    }
}

@media screen and (max-width: 480px) {
    .threads-media-container img,
    .threads-media-container video {
        max-height: 350px;
    }
    
    .threads-gallery {
        border-radius: 6px;
        gap: 4px;
    }
    
    .threads-media-container {
        border-radius: 6px;
    }
    
    .threads-gallery-item {
        width: auto !important;
        height: 180px !important;
    }
    
    .threads-gallery-item img,
    .threads-gallery-item video {
        width: auto !important;
        height: 180px !important;
        max-height: 180px !important;
        min-height: 180px !important;
    }
    
    .threads-gallery-item.video-item::before {
        width: 30px;
        height: 30px;
    }
    
    .threads-gallery-item.video-item::after {
        border-width: 6px 0 6px 10px;
    }
    
    .threads-lightbox-content {
        max-width: 95vw;
        max-height: 85vh;
    }
    
    .threads-lightbox-nav {
        width: 35px;
        height: 35px;
        font-size: 20px;
    }
    
    .threads-lightbox-prev {
        left: 10px;
    }
    
    .threads-lightbox-next {
        right: 10px;
    }
}

/* ===== 图片懒加载支持 ===== */
.threads-gallery-item img[loading="lazy"] {
    background: linear-gradient(90deg, #1a1a1a 25%, #2a2a2a 50%, #1a1a1a 75%);
    background-size: 200% 100%;
    animation: threads-loading 1.5s ease-in-out infinite;
}

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

/* ===== 可访问性增强 ===== */
.threads-gallery-item:focus-within {
    outline: 2px solid #0095f6;
    outline-offset: 2px;
}

/* ===== 打印样式 ===== */
@media print {
    .threads-gallery-item video {
        display: none;
    }
    
    .threads-gallery-item.video-item::before,
    .threads-gallery-item.video-item::after {
        display: none;
    }
    
    .threads-media-container {
        break-inside: avoid;
    }
}

/* ===== 深色模式优化 ===== */
@media (prefers-color-scheme: dark) {
    /* 深色模式下不添加背景色 */
}

/* ===== 更多图片数量的优化布局 ===== */
.threads-gallery[data-count] {
    position: relative;
}



/* ===== 禁止body滚动（当灯箱打开时） ===== */
body.threads-lightbox-open {
    overflow: hidden;
}
