/**
 * tech_portal 主题性能优化样式。
 *
 * 职责：骨架屏、懒加载增强、加载状态、无限滚动等性能体验优化。
 * 设计动机：提升感知性能，减少用户等待焦虑。
 */

/* ============================================================================
   图片懒加载增强
   ============================================================================ */

/* 懒加载占位容器 - 保持宽高比防止布局抖动 */
.lazy-wrapper {
    position: relative;
    overflow: hidden;
    background: var(--tp-bg-alt);
}

/* 常见宽高比占位 */
.lazy-wrapper--16x9 { aspect-ratio: 16 / 9; }
.lazy-wrapper--4x3 { aspect-ratio: 4 / 3; }
.lazy-wrapper--1x1 { aspect-ratio: 1 / 1; }
.lazy-wrapper--3x2 { aspect-ratio: 3 / 2; }
.lazy-wrapper--2x1 { aspect-ratio: 2 / 1; }

/* 占位动画背景 */
.lazy-wrapper::before {
    content: '';
    position: absolute;
    inset: 0;
    background: linear-gradient(
        90deg,
        var(--tp-bg-alt) 0%,
        var(--tp-surface) 20%,
        var(--tp-surface) 40%,
        var(--tp-bg-alt) 60%,
        var(--tp-bg-alt) 100%
    );
    background-size: 200% 100%;
    animation: lazy-shimmer 1.8s ease-in-out infinite;
    opacity: 1;
    transition: opacity 0.3s ease;
}

/* 图标占位符 */
.lazy-wrapper::after {
    content: '';
    position: absolute;
    top: 50%;
    left: 50%;
    width: 48px;
    height: 48px;
    margin: -24px 0 0 -24px;
    background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24' fill='none' stroke='%239ca3af' stroke-width='1.5'%3E%3Crect x='3' y='3' width='18' height='18' rx='2'/%3E%3Ccircle cx='8.5' cy='8.5' r='1.5'/%3E%3Cpath d='M21 15l-5-5L5 21'/%3E%3C/svg%3E");
    background-size: contain;
    background-repeat: no-repeat;
    opacity: 0.4;
    transition: opacity 0.3s ease;
}

/* 加载完成后隐藏占位 */
.lazy-wrapper.is-loaded::before,
.lazy-wrapper.is-loaded::after {
    opacity: 0;
    pointer-events: none;
}

/* 懒加载图片淡入 */
.lazy-wrapper img,
.lazy-wrapper .lazy-bg {
    position: absolute;
    inset: 0;
    width: 100%;
    height: 100%;
    object-fit: cover;
    opacity: 0;
    transition: opacity 0.4s ease;
}

.lazy-wrapper.is-loaded img,
.lazy-wrapper.is-loaded .lazy-bg {
    opacity: 1;
}

/* 错误状态 */
.lazy-wrapper.is-error::before {
    background: var(--tp-bg-alt);
    animation: none;
}

.lazy-wrapper.is-error::after {
    background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24' fill='none' stroke='%23ef4444' stroke-width='1.5'%3E%3Crect x='3' y='3' width='18' height='18' rx='2'/%3E%3Cpath d='M9 9l6 6M15 9l-6 6'/%3E%3C/svg%3E");
    opacity: 0.6;
}

@keyframes lazy-shimmer {
    0% { background-position: 100% 0; }
    100% { background-position: -100% 0; }
}

/* ============================================================================
   骨架屏组件
   ============================================================================ */

/* 骨架屏基础样式 */
.skeleton {
    background: linear-gradient(
        90deg,
        var(--tp-bg-alt) 25%,
        var(--tp-surface) 37%,
        var(--tp-bg-alt) 63%
    );
    background-size: 400% 100%;
    animation: skeleton-loading 1.4s ease infinite;
    border-radius: var(--tp-radius-sm);
}

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

/* 文本骨架 */
.skeleton-text {
    height: 1em;
    margin-bottom: 0.5em;
}

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

/* 标题骨架 */
.skeleton-title {
    height: 1.5em;
    width: 60%;
    margin-bottom: 1em;
}

/* 段落骨架 */
.skeleton-paragraph {
    display: flex;
    flex-direction: column;
    gap: 0.5em;
}

/* 头像骨架 */
.skeleton-avatar {
    width: 40px;
    height: 40px;
    border-radius: 50%;
    flex-shrink: 0;
}

.skeleton-avatar--sm { width: 32px; height: 32px; }
.skeleton-avatar--lg { width: 56px; height: 56px; }

/* 图片骨架 */
.skeleton-image {
    width: 100%;
    aspect-ratio: 16 / 9;
    border-radius: var(--tp-radius-md);
}

.skeleton-image--square { aspect-ratio: 1 / 1; }
.skeleton-image--wide { aspect-ratio: 2 / 1; }

/* 按钮骨架 */
.skeleton-button {
    height: 36px;
    width: 100px;
    border-radius: var(--tp-radius-md);
}

/* ============================================================================
   骨架屏布局模板
   ============================================================================ */

/* 文章卡片骨架 */
.skeleton-card {
    background: var(--tp-surface);
    border-radius: var(--tp-radius-lg);
    overflow: hidden;
    box-shadow: var(--tp-shadow-sm);
}

.skeleton-card__image {
    width: 100%;
    aspect-ratio: 16 / 9;
}

.skeleton-card__body {
    padding: var(--tp-space-4);
}

.skeleton-card__title {
    height: 1.25em;
    width: 80%;
    margin-bottom: var(--tp-space-3);
}

.skeleton-card__text {
    height: 0.875em;
    margin-bottom: var(--tp-space-2);
}

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

.skeleton-card__meta {
    display: flex;
    gap: var(--tp-space-4);
    margin-top: var(--tp-space-4);
}

.skeleton-card__meta-item {
    height: 0.75em;
    width: 60px;
}

/* 列表项骨架 */
.skeleton-list-item {
    display: flex;
    gap: var(--tp-space-4);
    padding: var(--tp-space-4) 0;
    border-bottom: 1px solid var(--tp-border);
}

.skeleton-list-item:last-child {
    border-bottom: none;
}

.skeleton-list-item__image {
    width: 120px;
    height: 80px;
    border-radius: var(--tp-radius-md);
    flex-shrink: 0;
}

.skeleton-list-item__content {
    flex: 1;
    display: flex;
    flex-direction: column;
    justify-content: center;
}

.skeleton-list-item__title {
    height: 1.125em;
    width: 75%;
    margin-bottom: var(--tp-space-2);
}

.skeleton-list-item__text {
    height: 0.875em;
    width: 50%;
}

/* 评论骨架 */
.skeleton-comment {
    display: flex;
    gap: var(--tp-space-4);
    padding: var(--tp-space-4) 0;
}

.skeleton-comment__body {
    flex: 1;
}

.skeleton-comment__header {
    display: flex;
    align-items: center;
    gap: var(--tp-space-3);
    margin-bottom: var(--tp-space-3);
}

.skeleton-comment__name {
    height: 1em;
    width: 80px;
}

.skeleton-comment__date {
    height: 0.75em;
    width: 60px;
}

.skeleton-comment__text {
    height: 0.875em;
    margin-bottom: var(--tp-space-2);
}

/* 侧栏骨架 */
.skeleton-sidebar {
    background: var(--tp-surface);
    border-radius: var(--tp-radius-lg);
    padding: var(--tp-space-4);
    box-shadow: var(--tp-shadow-sm);
}

.skeleton-sidebar__title {
    height: 1.25em;
    width: 40%;
    margin-bottom: var(--tp-space-4);
}

.skeleton-sidebar__item {
    height: 0.875em;
    margin-bottom: var(--tp-space-3);
}

.skeleton-sidebar__item:nth-child(2) { width: 90%; }
.skeleton-sidebar__item:nth-child(3) { width: 75%; }
.skeleton-sidebar__item:nth-child(4) { width: 85%; }
.skeleton-sidebar__item:nth-child(5) { width: 70%; }

/* ============================================================================
   无限滚动 / 加载更多
   ============================================================================ */

/* 加载状态指示器 */
.infinite-scroll-status {
    text-align: center;
    padding: var(--tp-space-6);
    color: var(--tp-text-muted);
}

/* 加载中动画 */
.infinite-scroll-status--loading {
    display: flex;
    align-items: center;
    justify-content: center;
    gap: var(--tp-space-3);
}

.loading-dots {
    display: flex;
    gap: 4px;
}

.loading-dots span {
    width: 8px;
    height: 8px;
    border-radius: 50%;
    background: var(--tp-primary);
    animation: loading-bounce 1.4s ease-in-out infinite;
}

.loading-dots span:nth-child(1) { animation-delay: 0s; }
.loading-dots span:nth-child(2) { animation-delay: 0.16s; }
.loading-dots span:nth-child(3) { animation-delay: 0.32s; }

@keyframes loading-bounce {
    0%, 80%, 100% {
        transform: scale(0.6);
        opacity: 0.5;
    }
    40% {
        transform: scale(1);
        opacity: 1;
    }
}

/* 无更多内容状态 */
.infinite-scroll-status--end {
    position: relative;
    color: var(--tp-text-muted);
}

.infinite-scroll-status--end::before,
.infinite-scroll-status--end::after {
    content: '';
    position: absolute;
    top: 50%;
    width: 80px;
    height: 1px;
    background: var(--tp-border);
}

.infinite-scroll-status--end::before {
    right: calc(50% + 60px);
}

.infinite-scroll-status--end::after {
    left: calc(50% + 60px);
}

/* 滚动到顶部触发区域 */
.scroll-sentinel {
    height: 1px;
    width: 100%;
    pointer-events: none;
}

/* 加载触发区域（无限滚动） */
.load-trigger {
    height: 100px;
    margin-top: calc(-1 * 100px);
    pointer-events: none;
}

/* ============================================================================
   页面加载进度条
   ============================================================================ */

.page-progress {
    position: fixed;
    top: 0;
    left: 0;
    right: 0;
    height: 3px;
    background: transparent;
    z-index: var(--tp-z-sticky);
    pointer-events: none;
}

.page-progress__bar {
    height: 100%;
    width: 0;
    background: linear-gradient(90deg, var(--tp-primary), var(--tp-accent, #7c3aed));
    transition: width 0.2s ease;
    box-shadow: 0 0 10px var(--tp-primary);
}

.page-progress--complete .page-progress__bar {
    width: 100%;
    transition: width 0.1s ease, opacity 0.3s 0.1s ease;
    opacity: 0;
}

/* ============================================================================
   内容加载过渡
   ============================================================================ */

/* 淡入动画 */
.fade-in {
    animation: fadeIn 0.4s ease forwards;
}

@keyframes fadeIn {
    from {
        opacity: 0;
        transform: translateY(10px);
    }
    to {
        opacity: 1;
        transform: translateY(0);
    }
}

/* 交错淡入（列表项） */
.stagger-fade-in > * {
    opacity: 0;
    animation: fadeIn 0.4s ease forwards;
}

.stagger-fade-in > *:nth-child(1) { animation-delay: 0.05s; }
.stagger-fade-in > *:nth-child(2) { animation-delay: 0.1s; }
.stagger-fade-in > *:nth-child(3) { animation-delay: 0.15s; }
.stagger-fade-in > *:nth-child(4) { animation-delay: 0.2s; }
.stagger-fade-in > *:nth-child(5) { animation-delay: 0.25s; }
.stagger-fade-in > *:nth-child(6) { animation-delay: 0.3s; }
.stagger-fade-in > *:nth-child(n+7) { animation-delay: 0.35s; }

/* 骨架屏到内容的过渡 */
.content-reveal {
    position: relative;
}

.content-reveal .skeleton-placeholder {
    position: absolute;
    inset: 0;
    opacity: 1;
    transition: opacity 0.3s ease;
}

.content-reveal.is-loaded .skeleton-placeholder {
    opacity: 0;
    pointer-events: none;
}

.content-reveal .actual-content {
    opacity: 0;
    transition: opacity 0.3s ease 0.1s;
}

.content-reveal.is-loaded .actual-content {
    opacity: 1;
}

/* ============================================================================
   滚动位置指示
   ============================================================================ */

/* 阅读进度条 */
.reading-progress {
    position: fixed;
    top: var(--tp-reading-progress-top, 0);
    left: 0;
    right: 0;
    height: 4px;
    background: rgba(148, 163, 184, 0.18);
    box-shadow: 0 1px 0 rgba(15, 23, 42, 0.06);
    z-index: calc(var(--tp-z-sticky) + 1);
    transform: translateZ(0);
    pointer-events: none;
}

.reading-progress__bar {
    height: 100%;
    width: 0;
    background: linear-gradient(90deg, color-mix(in srgb, var(--tp-primary) 80%, #ffffff), var(--tp-primary));
    box-shadow: 0 0 14px color-mix(in srgb, var(--tp-primary) 30%, transparent);
    transition: width 0.1s linear;
}

/* ============================================================================
   预加载提示
   ============================================================================ */

/* 链接预取提示 */
a[data-prefetch]::after {
    content: '';
    display: none;
}

/* 正在预取状态 */
a[data-prefetch].is-prefetching::before {
    content: '';
    position: absolute;
    bottom: -2px;
    left: 0;
    right: 0;
    height: 2px;
    background: var(--tp-primary);
    animation: prefetch-indicator 1s ease infinite;
}

@keyframes prefetch-indicator {
    0%, 100% { opacity: 0.3; }
    50% { opacity: 1; }
}

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

@media (prefers-reduced-motion: reduce) {
    .skeleton,
    .lazy-wrapper::before,
    [data-src].is-loading,
    [data-lazy].is-loading,
    .lazy.is-loading {
        animation: none;
        background-size: 100% 100%;
    }
    
    .loading-dots span {
        animation: none;
        opacity: 0.5;
    }
    
    .loading-dots span:nth-child(2) {
        opacity: 0.75;
    }
    
    .loading-dots span:nth-child(3) {
        opacity: 1;
    }
    
    .fade-in,
    .stagger-fade-in > * {
        animation: none;
        opacity: 1;
        transform: none;
    }
    
    .lazy-wrapper img,
    .lazy-wrapper .lazy-bg {
        transition: none;
    }
}

/* ============================================================================
   响应式调整
   ============================================================================ */

@media (max-width: 768px) {
    .skeleton-list-item {
        flex-direction: column;
    }
    
    .skeleton-list-item__image {
        width: 100%;
        height: auto;
        aspect-ratio: 16 / 9;
    }
    
    .infinite-scroll-status--end::before,
    .infinite-scroll-status--end::after {
        width: 40px;
    }
    
    .infinite-scroll-status--end::before {
        right: calc(50% + 40px);
    }
    
    .infinite-scroll-status--end::after {
        left: calc(50% + 40px);
    }
}
