/**
 * Core Web Vitals 優化樣式
 * 針對 17po.net 主網站的效能優化
 */

/* ========== LCP (Largest Contentful Paint) 優化 ========== */

/* 預載入關鍵資源 */
.hero-image,
.main-content img {
    /* 使用現代圖片格式 */
    image-rendering: -webkit-optimize-contrast;
    image-rendering: optimize-contrast;
}

/* 關鍵內容優先載入 */
.above-fold {
    /* 確保首屏內容快速載入 */
    will-change: auto;
}

/* 字體載入優化 */
@font-face {
    font-family: 'system-ui';
    src: local('system-ui'), local('-apple-system'), local('BlinkMacSystemFont');
    font-display: swap;
}

/* ========== CLS (Cumulative Layout Shift) 優化 ========== */

/* 圖片容器固定尺寸 */
.img-container {
    position: relative;
    width: 100%;
    height: 0;
    overflow: hidden;
}

.img-container.ratio-16-9 {
    padding-bottom: 56.25%; /* 16:9 比例 */
}

.img-container.ratio-4-3 {
    padding-bottom: 75%; /* 4:3 比例 */
}

.img-container.ratio-1-1 {
    padding-bottom: 100%; /* 1:1 比例 */
}

.img-container img {
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    object-fit: cover;
}

/* 廣告容器固定尺寸 */
.ad-container {
    min-height: 200px;
    background: #f8f9fa;
    display: flex;
    align-items: center;
    justify-content: center;
    border: 1px solid #dee2e6;
    border-radius: 4px;
}

.ad-container:empty::before {
    content: "廣告載入中...";
    color: #6c757d;
    font-size: 14px;
}

/* 避免動態內容造成佈局偏移 */
.dynamic-content {
    min-height: 100px;
    transition: height 0.3s ease;
}

/* 表單元素穩定化 */
.form-group {
    margin-bottom: 1rem;
    min-height: 58px; /* 確保一致高度 */
}

/* 導航選單穩定化 */
.navbar-nav {
    height: auto;
    min-height: 40px;
}

/* ========== FID (First Input Delay) 優化 ========== */

/* 減少不必要的重繪 */
.optimized-element {
    transform: translateZ(0);
    backface-visibility: hidden;
    perspective: 1000;
}

/* 輸入元素優化 */
input, button, select, textarea {
    touch-action: manipulation;
}

/* 點擊熱區優化 */
.btn, .link, a {
    min-height: 44px;
    min-width: 44px;
    display: inline-flex;
    align-items: center;
    justify-content: center;
}

/* 滾動優化 */
.scroll-container {
    -webkit-overflow-scrolling: touch;
    overscroll-behavior: contain;
}

/* ========== 懶載入樣式 ========== */
.lazy {
    opacity: 0;
    transition: opacity 0.3s ease;
    background: #f8f9fa;
    min-height: 200px;
    display: flex;
    align-items: center;
    justify-content: center;
}

.lazy::before {
    content: "";
    width: 40px;
    height: 40px;
    border: 3px solid #f3f3f3;
    border-top: 3px solid #007bff;
    border-radius: 50%;
    animation: spin 1s linear infinite;
}

.lazy.loaded {
    opacity: 1;
}

.lazy.loaded::before {
    display: none;
}

@keyframes spin {
    0% { transform: rotate(0deg); }
    100% { transform: rotate(360deg); }
}

/* ========== 響應式圖片優化 ========== */
picture {
    display: block;
    width: 100%;
}

picture img {
    width: 100%;
    height: auto;
    max-width: 100%;
    display: block;
}

/* WebP 支援回退 */
.webp .img-webp {
    display: block;
}

.no-webp .img-webp {
    display: none;
}

.webp .img-fallback {
    display: none;
}

.no-webp .img-fallback {
    display: block;
}

/* ========== 載入狀態優化 ========== */
.loading-skeleton {
    background: linear-gradient(90deg, #f0f0f0 25%, #e0e0e0 50%, #f0f0f0 75%);
    background-size: 200% 100%;
    animation: loading 1.5s infinite;
}

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

/* ========== 移動裝置優化 ========== */
@media (max-width: 768px) {
    /* 減少移動端的動畫 */
    .reduce-motion {
        animation: none !important;
        transition: none !important;
    }
    
    /* 優化觸控目標 */
    .btn-mobile {
        min-height: 48px;
        padding: 12px 16px;
    }
    
    /* 改善滾動效能 */
    .mobile-scroll {
        -webkit-overflow-scrolling: touch;
        scroll-behavior: smooth;
    }
}

/* ========== 列表項目優化 ========== */
.ad-list-item {
    /* 固定高度避免佈局偏移 */
    min-height: 120px;
    padding: 15px;
    border-bottom: 1px solid #dee2e6;
    display: flex;
    align-items: flex-start;
}

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

.ad-item-image {
    width: 80px;
    height: 80px;
    object-fit: cover;
    border-radius: 4px;
    margin-right: 15px;
    flex-shrink: 0;
}

.ad-item-content {
    flex: 1;
    min-width: 0; /* 確保文字不會撐開容器 */
}

.ad-item-title {
    font-size: 16px;
    font-weight: 600;
    margin-bottom: 8px;
    line-height: 1.4;
    /* 限制標題行數 */
    display: -webkit-box;
    -webkit-line-clamp: 2;
    -webkit-box-orient: vertical;
    overflow: hidden;
}

.ad-item-meta {
    font-size: 14px;
    color: #6c757d;
    margin-bottom: 4px;
}

/* ========== 搜尋列優化 ========== */
.search-bar {
    display: flex;
    gap: 10px;
    margin-bottom: 20px;
    padding: 20px;
    background: #f8f9fa;
    border-radius: 8px;
    align-items: center;
}

.search-bar .form-control {
    flex: 1;
    min-width: 200px;
}

.search-bar select {
    min-width: 120px;
}

.search-bar .btn {
    min-width: 80px;
    white-space: nowrap;
}

/* ========== 分類區塊優化 ========== */
.category-subsection {
    margin-bottom: 30px;
}

.subcategory-title {
    font-size: 18px;
    font-weight: 600;
    margin-bottom: 15px;
    color: #333;
    border-bottom: 2px solid #007bff;
    padding-bottom: 8px;
}

.category-subsection ul {
    list-style: none;
    padding: 0;
    margin: 0;
}

.category-subsection li {
    padding: 5px 0;
    border-bottom: 1px solid #eee;
}

.category-subsection li:last-child {
    border-bottom: none;
}

.category-subsection a {
    text-decoration: none;
    color: #495057;
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 8px 0;
    transition: color 0.2s ease;
}

.category-subsection a:hover {
    color: #007bff;
    text-decoration: none;
}

/* ========== 效能監控輔助 ========== */
.perf-marker {
    /* 用於效能測量的隱藏元素 */
    position: absolute;
    width: 1px;
    height: 1px;
    opacity: 0;
    pointer-events: none;
}

/* ========== 無障礙優化 ========== */
.sr-only {
    position: absolute;
    width: 1px;
    height: 1px;
    padding: 0;
    margin: -1px;
    overflow: hidden;
    clip: rect(0, 0, 0, 0);
    white-space: nowrap;
    border: 0;
}

/* 跳過連結 */
.skip-link {
    position: absolute;
    top: -40px;
    left: 6px;
    background: #000;
    color: #fff;
    padding: 8px;
    text-decoration: none;
    z-index: 1000;
}

.skip-link:focus {
    top: 6px;
}

/* ========== 列印樣式優化 ========== */
@media print {
    .no-print {
        display: none !important;
    }
    
    .ad-list-item {
        break-inside: avoid;
        page-break-inside: avoid;
    }
}