/**
 * Card Details Page Stylesheet
 * 
 * Defines the visual styling for the product detail view, including:
 * - Responsive Grid Layout (Mobile/Desktop)
 * - Typography and Color Variables
 * - Component Styling (Cards, Gauges, Tables)
 * - Interactive Elements (Buttons, Toggles)
 * - Accessibility (Touch Targets, Visibility)
 */

:root {
    /* -------------------------------------------------------------------------- */
    /* Design Tokens: Colors */
    /* -------------------------------------------------------------------------- */
    --primary-color: #14213d;       /* Dark Blue */
    --primary-hover: #0a1120;       /* Darker Blue */
    --text-color: #000000;          /* Black */
    --text-muted: #666666;          /* Gray */
    --bg-color: #e5e5e5;            /* Light Grey */
    --card-bg: #ffffff;             /* White */
    --border-color: #e5e5e5;        /* Light Gray */
    --accent-color: #fca311;        /* Orange */
    --shadow: 0 4px 6px rgba(0, 0, 0, 0.1); /* Subtle shadow */
    
    /* -------------------------------------------------------------------------- */
    /* Design Tokens: Layout & Spacing */
    /* -------------------------------------------------------------------------- */
    --radius: 8px;                  /* Default border radius */
    --container-max-width: 1200px;  /* Maximum content width */
    --spacing-xs: 0.5rem;           /* Extra small spacing (8px) */
    --spacing-sm: 1rem;             /* Small spacing (16px) */
    --spacing-md: 1.5rem;           /* Medium spacing (24px) */
    --spacing-lg: 2rem;             /* Large spacing (32px) */
    
    /* -------------------------------------------------------------------------- */
    /* Design Tokens: Typography */
    /* -------------------------------------------------------------------------- */
    --font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
    --font-size-base: 1rem;         /* 16px */
    --font-size-sm: 0.875rem;       /* 14px */
    --font-size-xs: 0.75rem;        /* 12px */
    --line-height-base: 1.5;        /* Standard line height */
    
    /* -------------------------------------------------------------------------- */
    /* Accessibility: Touch Targets */
    /* -------------------------------------------------------------------------- */
    --min-touch-target: 44px;       /* Minimum size for clickable elements */
}

/* Dark Mode Theme */
[data-theme="dark"] {
    /* Colors Override */
    --primary-color: #101A31;
    --primary-hover: #0B1222;
    --text-color: #CECAC4;
    --text-muted: #A59F97;
    --bg-color: #272A2C;
    --card-bg: #181A1B;
    --border-color: #34373A;
    --shadow: 0 4px 6px rgba(0, 0, 0, 0.5);
    
    /* Ensure accent color is visible */
    --accent-color: #C17902;
}

/* -------------------------------------------------------------------------- */
/* Global Reset & Base Styles */
/* -------------------------------------------------------------------------- */

/* Box-sizing reset for consistent sizing */
*, *::before, *::after {
    box-sizing: border-box;
}

html {
    font-size: 16px; /* Base size for rem calculations */
    -webkit-text-size-adjust: 100%; /* Prevent font scaling in landscape on iOS */
}

body {
    font-family: var(--font-family);
    background-color: var(--bg-color);
    color: var(--text-color);
    margin: 0;
    padding: 0;
    line-height: var(--line-height-base);
}

/* -------------------------------------------------------------------------- */
/* Layout Containers */
/* -------------------------------------------------------------------------- */

/* Main Content Container */
.container {
    width: 100%;
    max-width: var(--container-max-width);
    margin: 0 auto;                 /* Center horizontally */
    padding: 0 var(--spacing-xs);   /* Horizontal padding */
    container-type: inline-size;    /* Enable container queries */
    container-name: main-container;
}

/* -------------------------------------------------------------------------- */
/* Header Section */
/* -------------------------------------------------------------------------- */

.page-header {
    margin-bottom: 8px;
    display: flex;
    align-items: center;
    justify-content: space-between;
    background-color: var(--primary-color);
    height: 64px;
    padding: 0 var(--spacing-sm);
    border-radius: var(--radius);
    color: #ffffff;
    box-shadow: var(--shadow);
    gap: var(--spacing-xs);
}

.header-left, .header-right {
    display: flex;
    align-items: center;
    flex-shrink: 0;
}

/* 移除預設的 gap，因為內部元素已經有寫死 style margin 進行精確控制 (同 index.php) */
.header-right {
    gap: 0;
}

.header-center {
    flex: 1 1 auto;
    display: flex;
    align-items: center;
    justify-content: center;
    min-width: 0; /* Allow text truncation */
    overflow: hidden;
}

/* Back Navigation Link */
.back-link {
    text-decoration: none;
    color: var(--accent-color);
    font-weight: 600;
    display: inline-flex;
    align-items: center;
    min-height: var(--min-touch-target);
    padding: 0;
    border-radius: 4px;
    white-space: nowrap;
}

.back-link:hover {
    text-decoration: underline;
    color: #e5940f;
}

.back-link .back-short { display: none; }

/* Page Title & Brand */
.header-title-text {
    display: none;
    font-size: 1.25rem;
    font-weight: 700;
    letter-spacing: 0.2px;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
    line-height: 1.2;
}

.header-title-mark {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    gap: 8px;
    font-size: 1.1rem;
    font-weight: 700;
    letter-spacing: 0.2px;
    text-decoration: none;
    color: inherit;
    min-width: 0;
}

.header-title-mark img.logo {
    height: 40px;
    width: auto;
    object-fit: contain;
    flex-shrink: 0;
    margin: 0;
}

.brand-name {
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
    display: inline-flex;
    align-items: center;
    line-height: 1.2;
}

/* Responsive Header Rules */
@media (min-width: 768px) {
    .page-header {
        display: grid;
        grid-template-columns: 1fr auto 1fr;
        justify-content: stretch;
    }

    .header-left { justify-content: flex-start; }
    .header-center { justify-content: center; }
    .header-right { justify-content: flex-end; }

    .header-title-text {
        display: inline;
    }

    .header-title-mark {
        display: none;
    }
}

@media (max-width: 767px) {
    .page-header {
        height: 56px;
        padding: 0 8px;
    }

    .back-link {
        font-size: 0.95rem;
    }
    
    .header-title-mark {
        gap: 6px;
        font-size: 1.05rem;
    }
    
    .header-title-mark img.logo {
        height: 32px;
    }
}

@media (max-width: 420px) {
    .back-link .back-long {
        display: none;
    }

    .back-link .back-short {
        display: inline;
    }
}

@media (max-width: 320px) {
    /* Extreme small screen fallback */
    .header-center {
        display: none;
    }
}

/* -------------------------------------------------------------------------- */
/* Main Grid Layout */
/* -------------------------------------------------------------------------- */

/* Responsive Grid for Card Details */
.card-details-grid {
    display: grid;
    grid-template-columns: 1fr;     /* Default: Single column (Mobile) */
    gap: var(--spacing-md);
    background: var(--card-bg);
    padding: var(--spacing-md);
    border-radius: var(--radius);
    box-shadow: var(--shadow);
    align-items: start;
    width: 100%;
    min-height: 500px; /* Reserve space so hot-cards below doesn't CLS during JS render */
}

@media (min-width: 1024px) {
    .card-details-grid {
        min-height: 700px;
    }
}

.promo-footer {
    display: none;
}

/* Tablet Layout */
@media (min-width: 768px) {
    .card-details-grid {
        padding: var(--spacing-lg); /* Increased padding */
    }
}

/* Desktop Layout */
@media (min-width: 1024px) {
    .card-details-grid {
        grid-template-columns: 320px 1fr; /* Sidebar (Image) + Content */
        gap: var(--spacing-lg);
    }

    /* Desktop: sidebar is sticky, so render as a full-width action button (no floating) */
    .floating-fav-btn {
        position: static !important;
        top: auto !important;
        bottom: auto !important;
        right: auto !important;
        width: 100%;
        min-height: var(--min-touch-target);   /* 44px — matches action-btn */
        height: auto;
        margin-top: 12px;                       /* consistent 12px gap from buy btn */
        border-radius: var(--radius);
        background: var(--bg-secondary, #f5f5f5);
        border: 1.5px solid var(--border-color);
        color: var(--text-color);
        box-shadow: none;
        display: flex;
        align-items: center;
        justify-content: center;
        gap: 8px;
        padding: 0 12px;                        /* vertical centering via min-height + align-items */
        font-size: var(--font-size-base, 0.95rem);
        font-weight: 600;
        z-index: auto !important;
        opacity: 1;
        transform: none;
        pointer-events: auto;
        transition: background 0.2s, border-color 0.2s, color 0.2s;
    }

    /* Equalise gap below fav btn — override mt-24 utility class */
    #nicknameQuickBtn {
        margin-top: 12px;
    }

    .floating-fav-btn:hover {
        background: var(--bg-color, #e8eaed);
        box-shadow: none;
    }

    .floating-fav-btn.is-fav {
        background: rgba(239, 68, 68, 0.08);
        border-color: rgba(239, 68, 68, 0.35);
        color: #c53030;
    }

    .floating-fav-btn.is-fav:hover {
        background: rgba(239, 68, 68, 0.14);
    }

    .floating-fav-btn .icon-ball {
        width: 20px;
        height: 20px;
        flex-shrink: 0;
    }

    .fav-btn-text {
        display: inline;
    }

    [data-theme="dark"] .floating-fav-btn {
        background: var(--color-bg-elevated, #2a2d2e);
        border-color: var(--border-color);
        box-shadow: none;
    }

    [data-theme="dark"] .floating-fav-btn:hover {
        background: var(--bg-color, #1e2122);
    }

    [data-theme="dark"] .floating-fav-btn.is-fav {
        background: rgba(239, 68, 68, 0.15);
        border-color: rgba(239, 68, 68, 0.4);
        color: #fc8181;
    }
}

/* -------------------------------------------------------------------------- */
/* Sticky Sidebar (Card Image) */
/* -------------------------------------------------------------------------- */

.sticky-sidebar {
    position: relative;             /* Default for non-sticky contexts */
    width: 100%;
}

/* Desktop: Stick to top while scrolling */
@media (min-width: 1024px) {
    .sticky-sidebar {
        position: sticky;
        top: calc(var(--spacing-md) + var(--page-header-sticky-offset, 0px));     /* Gap from top of viewport */
        align-self: start;          /* Ensure it sticks to the top of the grid cell */
    }
}

/* Tablet & Mobile: Sticky Title Logic (Single Column Layout) */
@media (max-width: 1023px) {
    /* sticky-sidebar 維持原本排版，不做 sticky 行為 */
    .sticky-sidebar {
        position: relative;
        z-index: auto;
        background: transparent;
        padding-bottom: var(--spacing-sm);
    }

    /* sticky-title 永遠使用 fixed overlay，不佔版面高度 */
    .sticky-title {
        position: fixed !important;
        top: 0 !important;
        left: 0 !important;
        right: 0 !important;
        z-index: 9999 !important;
        margin-top: 0 !important;
        padding: 0 12px !important; /* 移除 padding-bottom: 8px 讓上下對稱 */
        height: 72px; /* 固定總高度 */
        border-top: none;
        background: var(--card-bg);
        box-shadow: 0 2px 6px rgba(0, 0, 0, 0.18);
        max-height: none;
        transform: none;
        opacity: 0;
        pointer-events: none;
        display: flex;
        align-items: center;
        gap: 12px; /* 增加一點圖文間距 */
    }

    /* 只透過 visible 切換顯示／隱藏，不改變任何布局 */
    .sticky-title.visible {
        opacity: 1;
        pointer-events: auto;
    }

    /* Equalise gap on mobile — fav btn is position:fixed (no flow height), so buy→我是誰? gap = mt-24 = 24px, reduce to 12px */
    #nicknameQuickBtn {
        margin-top: 12px;
    }

    /* Floating Favorite Button (Mobile/Tablet: fixed floating circle) */
    .fav-btn-text {
        display: none;
    }

    .floating-fav-btn {
        position: fixed !important;
        top: 92px !important;
        bottom: auto !important;
        right: 16px !important;
        width: 48px;
        height: 48px;
        border-radius: 50%;
        background: var(--card-bg);
        border: none;
        box-shadow: 0 4px 12px rgba(0, 0, 0, 0.2);
        display: flex;
        align-items: center;
        justify-content: center;
        z-index: 9999 !important;
        opacity: 0;
        transform: translateY(-20px);
        pointer-events: none;
        transition: all 0.3s cubic-bezier(0.4, 0, 0.2, 1);
    }

    .floating-fav-btn.visible {
        opacity: 1;
        transform: translateY(0);
        pointer-events: auto;
    }

    [data-theme="dark"] .floating-fav-btn {
        background: var(--color-bg-elevated);
        box-shadow: 0 4px 12px rgba(0, 0, 0, 0.5);
    }

    .floating-fav-btn .icon-ball {
        width: 24px;
        height: 24px;
    }

    /* sticky-title 出現時隱藏 header */
    body.sticky-active .page-header {
        visibility: hidden !important; /* 改用 visibility 避免 DOM reflow 導致網頁抖動 */
        pointer-events: none;
    }

    /* 顯示 h3 左側縮圖（提高選擇器優先權以覆蓋桌機規則） */
    .sticky-title .sticky-title-image {
        display: block;
        height: 52px; /* 稍微縮小高度，避免貼齊上下邊緣 */
        aspect-ratio: 1 / 1;
        flex-shrink: 0;
        border-radius: 4px;
        border: 1px solid var(--border-color);
        object-fit: contain;
    }

    .sticky-title-text-group {
        display: flex;
        flex-direction: column;
        justify-content: center; /* 讓文字群組在自己區域內垂直置中 */
        gap: 2px;
        min-width: 0;
        height: 100%;
    }

    /* 只顯示 h3 文本，隱藏副標 */
    .sticky-title .sticky-subtitle-text {
        display: none;
    }

    /* h3 文字可換行，最多顯示約兩行填滿上方空間 */
    .sticky-title .sticky-title-text {
        font-size: 0.875rem;
        white-space: normal;
        overflow: hidden;
        display: -webkit-box;
        -webkit-line-clamp: 2;
        -webkit-box-orient: vertical;
    }

    /* sticky-title 出現只做淡入淡出，不做任何位移動畫 */
    .sticky-sidebar .sticky-title {
        transform: none;
        max-height: none;
        margin-top: 0;
        padding-top: 0;
        border-top: none;
        transition: opacity 0.25s ease;
    }

    .sticky-sidebar .sticky-title.visible {
        transform: none;
    }

    .sticky-theme-toggle {
        display: flex;
        align-items: center;
        justify-content: center;
    }
}

/* Image Loading Animation for Details Page */
.card-image-wrapper {
    position: relative;
    overflow: hidden;
    /* Shimmer Effect for Loading State */
    background: linear-gradient(90deg, #f0f0f0 25%, #e0e0e0 50%, #f0f0f0 75%);
    background-size: 200% 100%;
    animation: imageShimmer 1.5s infinite;
}

.card-image-wrapper.loaded {
    background: transparent;
    animation: none;
}

[data-theme="dark"] .card-image-wrapper.loaded {
    background: transparent;
    animation: none;
}

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

.card-image {
    /* Initial Loading State */
    opacity: 0;
    transform: scale(0.9);
    transition: opacity 0.5s ease-out, transform 0.5s ease-out;
}

.card-image.loaded {
    opacity: 1;
    transform: scale(1);
}

.card-image.error {
    opacity: 1;
    transform: scale(1);
    padding: 30%;
    background-color: #f5f5f5;
}

[data-theme="dark"] .card-image.error {
    background-color: #181A1B;
}

/* Mobile: Other Layout Adjustments */
@media (max-width: 767px) {
    body {
        padding: 0;
    }

    /* 手機版：減少 Header 高度，滿版顯示，移除圓角 */
    .page-header {
        padding: var(--spacing-sm);
        margin-left: calc(-1 * var(--spacing-xs));
        margin-right: calc(-1 * var(--spacing-xs));
        border-radius: 0;
    }

    .container {
        width: 100%;
        max-width: 100%;
        margin: 0;
        padding: 0 var(--spacing-xs);
    }

    .card-details-grid {
        padding: var(--spacing-xs);
        border-radius: 0;
        box-shadow: none;
    }

    /* Sticky Logic moved to max-width: 1023px */

    .promo-footer {
        position: fixed;
        /* 懸浮置中 */
        left: 50%;
        transform: translateX(-50%);
        bottom: calc(env(safe-area-inset-bottom, 0px) + 16px);
        width: auto;
        min-width: 300px;
        max-width: 90%;
        height: 48px;
        
        /* Glassmorphism 毛玻璃效果 */
        background: rgba(20, 33, 61, 0.85); /* 半透明的 primary-color */
        backdrop-filter: blur(12px);
        -webkit-backdrop-filter: blur(12px); /* Safari 支援 */
        
        color: #fff;
        display: flex;
        align-items: center;
        justify-content: center;
        font-size: 0.8rem;
        z-index: 9998;
        padding: 0 20px;
        
        /* 藥丸造型 */
        border-radius: 999px;
        border: 1px solid rgba(255, 255, 255, 0.15);
        box-shadow: 0 10px 25px -5px rgba(0, 0, 0, 0.3);
        gap: 10px;
        
        /* 滾動顯示/隱藏的動畫 */
        transition: transform 0.4s cubic-bezier(0.4, 0, 0.2, 1), opacity 0.4s ease;
    }

    [data-theme="dark"] .promo-footer {
        background: rgba(30, 30, 30, 0.85);
        border-color: rgba(255, 255, 255, 0.1);
    }

    .promo-footer.is-hidden {
        transform: translate(-50%, 150%);
        opacity: 0;
        pointer-events: none;
    }

    .promo-footer-logo {
        width: 20px;
        height: 20px;
        object-fit: contain;
        flex-shrink: 0;
        margin: 0;
    }

    .promo-footer-text {
        white-space: nowrap;
        overflow: hidden;
        text-overflow: ellipsis;
        font-weight: 500;
        letter-spacing: 0.3px;
        margin: 0;
        min-width: 0;
    }

}

/* 桌機預設：不顯示 sticky 標題縮圖，避免影響原本版面 */
.sticky-title-image {
    display: none;
}

/* Images */
.card-image-section {
    display: flex;
    flex-direction: column;
    gap: var(--spacing-sm);
}

.card-image-wrapper {
    flex-shrink: 0;
    position: static;
    aspect-ratio: auto;
    padding-top: 0;
    background: transparent;
    overflow: visible;
}

.card-image-box {
    width: 100%;
    aspect-ratio: 1 / 1;
    background: url('../img/BG.svg') center / cover no-repeat, #ffffff;
    border: 1px solid var(--border-color);
    border-radius: var(--radius);
    overflow: hidden;
    padding: var(--spacing-sm);
    box-sizing: border-box;
}

[data-theme="dark"] .card-image-box {
    background: url('../img/BG-dark.svg') center / cover no-repeat, #181A1B;
}

.card-image {
    width: 100%;
    height: 100%;
    max-width: 100%;
    object-fit: contain;
    display: block;
    border-radius: 0;
}

@media (max-width: 767px) {
    .card-image-box {
        padding: var(--spacing-xs);
    }
}

/* Buttons */
.action-btn {
    display: flex;
    align-items: center;
    justify-content: center;
    width: 100%;
    min-height: var(--min-touch-target);
    padding: 12px;
    background-color: var(--accent-color);
    color: #000;
    text-decoration: none;
    border-radius: var(--radius);
    font-weight: 600;
    transition: opacity 0.2s;
    font-size: var(--font-size-base);
}

.action-btn:hover {
    opacity: 0.9;
}

.btn-nickname {
    background-color: var(--primary-color);
    color: #fff;
}

.btn-nickname:hover {
    opacity: 1;
    background-color: var(--primary-hover);
}

.mt-16 {
    margin-top: 16px;
}

.mt-24 {
    margin-top: 24px;
}

/* Sticky Title */
.sticky-theme-toggle {
    display: none;
    color: var(--text-color);
    border: 1px solid var(--border-color);
    margin-left: auto;
    width: 36px;
    height: 36px;
    padding: 6px;
    font-size: 1rem;
}

.sticky-theme-toggle:hover {
    background-color: var(--bg-color);
    border-color: var(--primary-color);
}

[data-theme="dark"] .sticky-theme-toggle {
    color: #ffffff;
}

.sticky-title {
    opacity: 0;
    transform: translateY(-10px);
    max-height: 0;
    margin-top: 0;
    padding-top: 0;
    border-top: 1px solid transparent;
    overflow: hidden;
    transition: all 0.4s cubic-bezier(0.4, 0, 0.2, 1);
}

.sticky-title.visible {
    opacity: 1;
    transform: translateY(0);
    max-height: 100px;
    margin-top: 16px;
    padding-top: 10px;
    border-top: 1px solid var(--border-color);
}

.sticky-title-text {
    font-size: 1rem;
    margin: 0 0 4px 0;
    font-weight: bold;
}

.sticky-subtitle-text {
    font-size: 0.75rem;
    color: var(--text-muted);
}

/* Info Section */
.card-info-section {
    display: flex;
    flex-direction: column;
    gap: var(--spacing-md);
    width: 100%;
    min-width: 0; /* Important for flex/grid children overflow */
}

.card-header {
    border-bottom: 1px solid var(--border-color);
    padding-bottom: var(--spacing-sm);
}

.card-title-jp {
    font-size: clamp(1.25rem, 3vw, 1.75rem);
    font-weight: 700;
    margin: 0 0 0.5rem 0;
    color: var(--text-color);
    line-height: 1.3;
}

.card-subtitle {
    display: flex;
    flex-wrap: wrap;
    align-items: center;
    gap: 6px;
    font-size: var(--font-size-sm);
    color: var(--text-muted);
    margin: 0;
    line-height: 1.4;
}

.card-subtitle__nickname {
    flex-shrink: 0;
}

.card-subtitle__chips {
    display: flex;
    flex-wrap: wrap;
    gap: 4px;
}

.card-chip {
    display: inline-flex;
    align-items: center;
    padding: 1px 7px;
    border-radius: 99px;
    font-size: 0.72rem;
    font-weight: 600;
    letter-spacing: 0.02em;
    line-height: 1.6;
    white-space: nowrap;
}

.card-chip--year {
    background: #fca31122;
    color: #b36a00;
    border: 1px solid #fca31155;
}

[data-theme="dark"] .card-chip--year {
    background: #fca31120;
    color: #fca311;
    border-color: #fca31140;
}

.card-chip--code {
    background: var(--bg-color, #f1f3f4);
    color: var(--text-muted);
    border: 1px solid var(--border-color);
}

/* Data Sections */
.data-section {
    background: var(--card-bg);
    border-radius: var(--radius);
    padding: var(--spacing-sm);
    border: 1px solid var(--border-color);
    width: 100%;
    overflow: hidden;
}

/* 目前正在販售區：下方留白再縮小一點，避免空間浪費 */
#active-listings-section {
    padding-bottom: 8px;
}

/* 目前正在販售列表容器：高度固定至少 261px，避免切頁時高度變小造成抖動 */
#active-listings-container {
    min-height: 261px;
}

/* 目前正在販售區內部 carousel：高度至少 261px，但可隨內容自動增高，不遮蔽文字 */
#active-listings-container .listings-carousel {
    min-height: 261px;
}


.section-title {
    font-size: 1rem;
    font-weight: 600;
    margin: 0 0 var(--spacing-sm) 0;
    display: flex;
    align-items: center;
    justify-content: space-between;
    flex-wrap: wrap;
    gap: 8px;
}

.source-title {
    display: inline-flex;
    align-items: center;
    gap: 6px;
}

.source-badge {
    display: inline-block;
    font-size: 0.75rem;
    font-weight: 700;
    letter-spacing: 0.04em;
    padding: 0.1rem 0.45rem;
    border-radius: 3px;
    vertical-align: middle;
    line-height: 1.6;
    text-transform: uppercase;
}

.source-badge--snkr {
    background: #29B6F6;
    color: #fff;
}

.source-badge--psa {
    background: #FF5252;
    color: #fff;
}

.btn-toggle-analysis {
    padding: 8px 12px;
    font-size: 12px;
    color: #fff;
    background-color: var(--primary-color);
    border: none;
    border-radius: 4px;
    cursor: pointer;
    transition: background-color 0.2s;
    min-height: 44px;
}

.btn-toggle-analysis:hover {
    background-color: var(--accent-color);
    color: #000;
}

/* Analysis Grid (Gauges) */
.analysis-grid {
    display: grid;
    grid-template-columns: repeat(2, 1fr);
    gap: 15px;
}

/* Removed media queries to enforce 2-column layout across all screen sizes as requested */

.analysis-card {
    background: var(--card-bg);
    padding: 15px;
    border: 1px solid var(--border-color);
    border-radius: 12px;
    display: flex;
    flex-direction: column;
    align-items: center;
    box-shadow: 0 2px 6px rgba(0, 0, 0, 0.05);
    height: 220px; /* Initial fixed height */
    overflow: hidden; /* Hide excess content */
    justify-content: flex-start; /* Align content to top */
    transition: height 0.4s ease; /* Smooth transition */
}

/* Mobile: 分析區每排 2 個指標，每個指標為 1:1 正方形 */
@media (max-width: 767px) {
    .analysis-grid {
        grid-template-columns: repeat(2, 1fr);
    }

    /* 手機：預設狀態 1:1 卡片，只顯示指標標題 + 儀表 + 標籤 */
    .analysis-card {
        height: auto !important;
        aspect-ratio: 1 / 1;
        padding: 10px;
        justify-content: center;
        overflow: hidden;
    }

    .analysis-card .gauge-container {
        height: 70px;
        width: 100%;
        display: flex;
        justify-content: center;
        align-items: center;
    }

    .analysis-card .gauge-value {
        bottom: -2px;
        font-size: 1rem;
    }

    .analysis-card .analysis-text {
        margin-top: 6px;
        font-size: 0.75rem;
    }

    /* 預設收合：不顯示長敘述，避免在小正方形裡爆版 */
    .analysis-card .analysis-desc {
        display: none;
    }

    /* 點擊「分析」後：改為單欄直列，自適應高度，展開完整說明 */
    body:has(.analysis-card.expanded) .analysis-grid {
        grid-template-columns: 1fr;
    }

    .analysis-card.expanded {
        aspect-ratio: auto;
        height: auto !important;
        padding: 12px;
        overflow: visible;
    }

    .analysis-card.expanded .gauge-container {
        height: 90px;
    }

    .analysis-card.expanded .analysis-desc {
        display: block;
        opacity: 1;
    }

    /* 手機：PSA 鑑定分佈三個 stat-item 同一排 */
    .stats-grid {
        display: grid;
        grid-template-columns: repeat(3, minmax(0, 1fr));
        gap: var(--spacing-xs);
    }

    /* 手機：PSA 分佈三個卡片內文字對齊，高度一致、排版舒服 */
    .stat-item {
        display: flex;
        flex-direction: column;
        align-items: center;
        justify-content: center;
        padding: var(--spacing-xs);
    }

    .stat-label {
        display: flex;
        align-items: flex-end;
        justify-content: center;
        min-height: 20px;
        max-width: 100%;
        white-space: nowrap;                          /* 一律一行，不換行 */
        font-size: clamp(0.6rem, 2.8vw, 0.75rem);     /* 視寬度自適應變小 */
    }

    .stat-value {
        display: flex;
        align-items: center;
        justify-content: center;
        min-height: 24px; /* 三個數字區塊高度一致 */
        font-size: 0.95rem;
    }

    .stat-pct {
        margin-left: 4px;
    }
}

.analysis-title {
    font-size: 0.875rem;
    font-weight: bold;
    margin-bottom: 5px;
    color: var(--text-color);
    text-align: center;
    flex-shrink: 0; /* Prevent title from shrinking */
}

.gauge-container {
    position: relative;
    height: 120px;
    width: 200px;
    max-width: 100%; /* Responsive canvas */
    flex-shrink: 0; /* Prevent gauge from shrinking */
}

.gauge-value {
    position: absolute;
    bottom: -5px;
    left: 0;
    right: 0;
    text-align: center;
    font-weight: bold;
    font-size: 1.25rem;
    color: var(--text-color);
}

.analysis-text {
    text-align: center;
    margin-top: 10px;
    font-size: 0.8125rem;
    line-height: 1.5;
}

.analysis-desc {
    display: block; /* Always visible in DOM, hidden by parent overflow */
    margin-top: 8px;
    font-size: 0.75rem;
    color: var(--text-muted);
    opacity: 0;
    transition: opacity 0.3s ease;
}

.analysis-card.expanded .analysis-desc {
    opacity: 1;
}

/* Stats Grid */
.stats-grid {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(110px, 1fr));
    gap: var(--spacing-sm);
}

.stat-item {
    background: var(--card-bg);
    padding: var(--spacing-sm);
    border-radius: 6px;
    border: 1px solid var(--border-color);
    text-align: center;
}

.stat-label {
    font-size: var(--font-size-xs);
    color: var(--text-muted);
    margin-bottom: 4px;
    display: block;
}

.stat-value {
    font-size: 1.125rem;
    font-weight: 700;
    color: var(--text-color);
    display: block;
}

.stat-pct {
    font-size: 0.75rem;
    color: var(--text-muted);
    font-weight: 400;
}

/* Transaction Grid */
.transaction-grid {
    display: grid;
    grid-template-columns: 1fr;
    gap: var(--spacing-sm);
}

@media (min-width: 600px) {
    .transaction-grid {
        grid-template-columns: repeat(2, 1fr);
    }
}

@media (min-width: 1000px) {
    .transaction-grid {
        grid-template-columns: repeat(3, 1fr);
    }
}

/* Each column is a card */
.transaction-column {
    display: flex;
    flex-direction: column;
    gap: 8px;
    background: var(--card-bg);
    border: 1px solid var(--border-color);
    border-radius: var(--radius);
    padding: 10px 10px 8px;
}

.tx-column-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding-bottom: 6px;
    border-bottom: 1px solid var(--border-color);
}

.tx-record-count {
    font-size: 0.7rem;
    color: var(--text-muted);
}

/* Extended grade colours */
.grade-tag.b    { background: #e8f0fe; color: #1967d2; border: 1px solid #c5d8fb; }
.grade-tag.c    { background: #fce8e6; color: #c5221f; border: 1px solid #f5c6c3; }
.grade-tag.d    { background: #fef7e0; color: #b06000; border: 1px solid #fde38a; }
.grade-tag.bgs10bl,
.grade-tag.bgs10gl,
.grade-tag.bgs95,
.grade-tag.bgs9以下 { background: #e6f4ea; color: #137333; border: 1px solid #b7dfbf; }
.grade-tag.ars10\+,
.grade-tag.ars10,
.grade-tag.ars9,
.grade-tag.ars8以下 { background: #f3e8fd; color: #6a0dad; border: 1px solid #d4b0f5; }

[data-theme="dark"] .grade-tag.b    { background: #1967d220; color: #8ab4f8; border-color: #1967d240; }
[data-theme="dark"] .grade-tag.c    { background: #c5221f20; color: #f28b82; border-color: #c5221f40; }
[data-theme="dark"] .grade-tag.d    { background: #b0600020; color: #fdd663; border-color: #b0600040; }
[data-theme="dark"] .grade-tag.bgs10bl,
[data-theme="dark"] .grade-tag.bgs10gl,
[data-theme="dark"] .grade-tag.bgs95,
[data-theme="dark"] .grade-tag.bgs9以下 { background: #13733320; color: #81c995; border-color: #13733340; }
[data-theme="dark"] .grade-tag.ars10\+,
[data-theme="dark"] .grade-tag.ars10,
[data-theme="dark"] .grade-tag.ars9,
[data-theme="dark"] .grade-tag.ars8以下 { background: #6a0dad20; color: #d7aefb; border-color: #6a0dad40; }

/* Limit Visibility Logic */
.transaction-grid:not(.expanded) .transaction-column:nth-child(n+5) {
    display: none;
}

@media (min-width: 1000px) {
    .transaction-grid:not(.expanded) .transaction-column:nth-child(n+5) {
        display: flex;
    }
    .transaction-grid:not(.expanded) .transaction-column:nth-child(n+7) {
        display: none;
    }
    .hide-lg-up {
        display: none !important;
    }
}

.more-btn-container {
    text-align: center;
    margin-top: var(--spacing-md);
    width: 100%;
}

.transaction-table-container {
    max-height: 250px;
    overflow-y: auto;
    overflow-x: auto;
    border: 1px solid var(--border-color);
    border-radius: 6px;
    background: var(--card-bg);
    box-shadow: 0 2px 6px rgba(0, 0, 0, 0.1);
    -webkit-overflow-scrolling: touch;
}

.transaction-pager {
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 8px;
    padding: 5px 0 2px;
}

.transaction-pager .listing-nav-btn {
    position: static;
    transform: none;
    width: 28px;
    height: 28px;
    flex-shrink: 0;
}

.tx-page-label {
    font-size: 0.75rem;
    color: var(--text-muted);
    min-width: 44px;
    text-align: center;
}

.transaction-table {
    width: 100%;
    border-collapse: collapse;
    background: var(--card-bg);
}

.transaction-table th, 
.transaction-table td {
    padding: 8px 12px;
    text-align: left;
    border-bottom: 1px solid var(--border-color);
    font-size: 0.8125rem;
    white-space: nowrap;
}

.transaction-table th {
    background: var(--bg-color);
    font-weight: 600;
    color: var(--text-muted);
    position: sticky;
    top: 0;
    z-index: 1;
}

.transaction-table tr[onclick] {
    cursor: pointer;
}

.transaction-table tr[onclick]:hover {
    background-color: var(--bg-color);
}

/* Price cell links — inherit text colour, reveal clickability on hover only */
.transaction-table td a {
    color: inherit;
    text-decoration: none;
    border-bottom: 1px dashed transparent;
    transition: color 0.15s, border-color 0.15s;
}

.transaction-table td a:hover {
    color: var(--primary-color, #14213d);
    border-bottom-color: var(--primary-color, #14213d);
}

[data-theme="dark"] .transaction-table td a:hover {
    color: var(--accent-color, #fca311);
    border-bottom-color: var(--accent-color, #fca311);
}

.grade-tag {
    display: inline-block;
    padding: 2px 6px;
    border-radius: 4px;
    font-size: 0.7rem;
    font-weight: 600;
    background: var(--border-color);
    color: var(--text-color);
}

.grade-tag.psa10 { background: var(--primary-color); color: #ffffff; }
.grade-tag.psa9 { background: var(--accent-color); color: #000000; }

/* Price Table */
.price-table {
    width: 100%;
    border-collapse: collapse;
    /* display: block; Removed to allow table to fill width */
    /* overflow-x: auto; Moved to .table-responsive */
    white-space: nowrap;
}

.table-responsive {
    width: 100%;
    overflow-x: auto;
    -webkit-overflow-scrolling: touch;
    display: block;
}

.price-table th, .price-table td {
    padding: 10px;
    border-bottom: 1px solid var(--border-color);
    text-align: left;
    font-size: 0.875rem;
}

.price-table th {
    color: var(--text-muted);
    font-weight: 500;
    background-color: var(--card-bg);
}

.price-table td.text-right {
    text-align: right;
}

.price-table th:last-child, 
.price-table td:last-child {
    text-align: right; /* Often numbers are on the right */
}

/* Override for specific columns if needed */
.price-table th:first-child,
.price-table td:first-child {
    text-align: left;
}

/* Calculator */
.market-summary {
    display: grid;
    grid-template-columns: repeat(3, 1fr);
    gap: 10px;
    margin-bottom: 15px;
}

@media (max-width: 480px) {
    .market-summary {
        grid-template-columns: repeat(3, 1fr);
    }
}

.summary-card {
    background: var(--card-bg);
    padding: 10px;
    border-radius: 6px;
    border: 1px solid var(--border-color);
    border-left: 4px solid var(--accent-color);
    text-align: center;
}

.summary-card-title { font-size: 0.75rem; font-weight: bold; margin-bottom: 4px; }
.summary-price { font-size: 1rem; font-weight: bold; color: var(--text-color); }
.summary-vol { font-size: 0.75rem; color: var(--text-muted); margin-top: 2px; }

@media (max-width: 767px) {
    /* 手機：套利 summary 僅顯示一行，去掉 NT 前綴視覺干擾 */
    .summary-price {
        white-space: nowrap;
        overflow: hidden;
        text-overflow: ellipsis;
    }
}

/* 手機：PSA 套利試算區排版與文字大小微調，提升閱讀性 */
@media (max-width: 767px) {
    /* 下拉選單字體稍微縮小，避免過寬 */
    #psaServiceSelect.form-control {
        font-size: 0.875rem;
    }

    /* 表頭與表格內容在手機上略縮小 */
    .arbitrage-calculator .price-table th {
        font-size: 0.75rem;
    }

    .arbitrage-calculator .price-table td {
        font-size: 0.75rem;
    }

    /* 獲利與 ROI 顏色文字保持可讀，不需再放大 */
    .arbitrage-calculator .text-danger,
    .arbitrage-calculator .text-success {
        font-size: 0.8rem;
    }

    /* 底部說明文字再縮小一階，當備註閱讀 */
    .arbitrage-calculator .calc-note {
        font-size: 0.7rem;
        line-height: 1.4;
    }
}

.text-psa10 { color: var(--accent-color); }
.text-psa9 { color: var(--primary-color); }
.text-other { color: var(--text-muted); }

.calculator-box {
    background: var(--card-bg);
    padding: 15px;
    border-radius: 8px;
    border: 1px solid var(--border-color);
    border-top: 3px solid var(--accent-color);
    box-shadow: var(--shadow);
}

.calculator-form-row {
    display: grid;
    grid-template-columns: 1fr;
    gap: 15px;
}

@media (min-width: 600px) {
    .calculator-form-row {
        grid-template-columns: 1fr 1fr;
    }
}

.form-group label {
    display: block;
    font-size: 0.75rem;
    color: var(--text-muted);
    margin-bottom: 4px;
    display: flex;
    align-items: flex-end;
    height: 20px;
}

.cost-value-display {
    font-size: 1.125rem;
    font-weight: bold;
    color: var(--text-color);
}

.cost-sub-display {
    font-size: 0.75rem;
    color: var(--text-muted);
}

.form-control {
    width: 100%;
    padding: 10px;
    border: 1px solid var(--border-color);
    border-radius: 4px;
    font-size: 1rem;
    min-height: 44px;
    background-color: var(--card-bg);
    color: var(--text-color);
}

/* Chart Container */
.chart-container {
    position: relative;
    height: 300px;
    width: 100%;
    max-width: 100%;
}

.chart-controls {
    display: flex;
    flex-wrap: wrap;
    gap: 4px;
}

.monthly-avg-controls {
    display: flex;
    flex-wrap: wrap;
    gap: 4px;
}

.btn-monthly-avg {
    cursor: pointer;
    padding: 6px 12px;
    border: 1px solid var(--border-color);
    background: var(--card-bg);
    color: var(--text-color);
    border-radius: 4px;
    font-size: 0.75rem;
    min-height: 32px;
    transition: all 0.2s;
}

.btn-monthly-avg:hover {
    border-color: var(--primary-color);
    color: var(--primary-color);
}

.btn-monthly-avg.active {
    background: var(--accent-color);
    color: #000000;
    border-color: var(--accent-color);
    font-weight: bold;
}

.btn-range {
    cursor: pointer;
    padding: 8px 12px;
    border: 1px solid var(--border-color);
    background: var(--card-bg);
    color: var(--text-color);
    border-radius: 4px;
    font-size: 0.75rem;
    min-height: 44px;
    transition: all 0.2s;
}

.btn-range:hover {
    border-color: var(--primary-color);
    color: var(--primary-color);
}

.btn-range.active {
    background: var(--accent-color);
    color: #000000;
    border-color: var(--accent-color);
    font-weight: bold;
}

/* Container Queries */
@container main-container (max-width: 400px) {
    .stats-grid {
        grid-template-columns: 1fr 1fr; /* Desktop 窄容器時改為 2 欄 */
    }
    .transaction-grid {
        grid-template-columns: 1fr;
    }
}

/* Hide specific elements on very small mobile screens */
@media (max-width: 400px) {
    .hide-on-mobile-small {
        display: none;
    }
}

/* 手機優先：PSA 分佈三個 stat-item 一律同一排（3 欄），避免落到第 2 排 */
@media (max-width: 767px) {
    .stats-grid {
        grid-template-columns: repeat(3, minmax(0, 1fr));
    }
}

/* Loading State */
.loading-state {
    text-align: center;
    padding: 40px;
    font-size: 1.2rem;
    color: var(--text-muted);
    grid-column: 1 / -1;
}

.loading-state--skeleton {
    padding: 0;
    text-align: left;
}

/* Skeleton loading layout */
.loading-skeleton {
    display: grid;
    grid-template-columns: 1fr;
    gap: 20px;
    width: 100%;
}

@media (min-width: 1024px) {
    .loading-skeleton {
        grid-template-columns: 320px 1fr;
        gap: 24px;
    }
}

.loading-skeleton__card {
    display: flex;
    flex-direction: column;
    gap: 12px;
    max-width: 260px;
    margin: 0 auto;
    width: 100%;
}

@media (min-width: 1024px) {
    .loading-skeleton__card { max-width: none; margin: 0; }
}

.loading-skeleton__img {
    width: 100%;
    aspect-ratio: 63 / 88;
    border-radius: 10px;
    background: linear-gradient(90deg, var(--bg-secondary, #f0f0f0) 25%, var(--border-color, #e0e0e0) 50%, var(--bg-secondary, #f0f0f0) 75%);
    background-size: 200% 100%;
    animation: imageShimmer 1.5s infinite;
}

.loading-skeleton__btn {
    height: 44px;
    border-radius: 8px;
    background: linear-gradient(90deg, var(--bg-secondary, #f0f0f0) 25%, var(--border-color, #e0e0e0) 50%, var(--bg-secondary, #f0f0f0) 75%);
    background-size: 200% 100%;
    animation: imageShimmer 1.5s 0.1s infinite;
}

.loading-skeleton__btn--sm { height: 36px; }

.loading-skeleton__right {
    display: flex;
    flex-direction: column;
    gap: 16px;
    padding-top: 4px;
}

.loading-skeleton__title {
    height: 26px;
    width: 72%;
    border-radius: 6px;
    background: linear-gradient(90deg, var(--bg-secondary, #f0f0f0) 25%, var(--border-color, #e0e0e0) 50%, var(--bg-secondary, #f0f0f0) 75%);
    background-size: 200% 100%;
    animation: imageShimmer 1.5s infinite;
}

.loading-skeleton__price {
    height: 56px;
    border-radius: 10px;
    background: linear-gradient(90deg, var(--bg-secondary, #f0f0f0) 25%, var(--border-color, #e0e0e0) 50%, var(--bg-secondary, #f0f0f0) 75%);
    background-size: 200% 100%;
    animation: imageShimmer 1.5s 0.05s infinite;
}

.loading-skeleton__section {
    display: flex;
    flex-direction: column;
    gap: 8px;
}

.loading-skeleton__line {
    height: 14px;
    border-radius: 4px;
    background: linear-gradient(90deg, var(--bg-secondary, #f0f0f0) 25%, var(--border-color, #e0e0e0) 50%, var(--bg-secondary, #f0f0f0) 75%);
    background-size: 200% 100%;
    animation: imageShimmer 1.5s 0.2s infinite;
}

.loading-skeleton__line--med  { width: 70%; }
.loading-skeleton__line--short { width: 45%; }

[data-theme="dark"] .loading-skeleton__img,
[data-theme="dark"] .loading-skeleton__btn,
[data-theme="dark"] .loading-skeleton__title,
[data-theme="dark"] .loading-skeleton__price,
[data-theme="dark"] .loading-skeleton__line {
    background: linear-gradient(90deg, #2a2d32 25%, #3a3d42 50%, #2a2d32 75%);
    background-size: 200% 100%;
    animation: imageShimmer 1.5s infinite;
}

/* Fade-in when JS renders the card */
.card-details--entering {
    animation: cardDetailsEnter 0.25s ease-out both;
}

@keyframes cardDetailsEnter {
    from { opacity: 0; transform: translateY(5px); }
    to   { opacity: 1; transform: none; }
}

/* Helpers */
.text-right { text-align: right; }

/* Print Styles */
@media print {
    body { background: #fff; padding: 0; }
    .container { max-width: 100%; }
    .card-details-grid { display: block; box-shadow: none; border: none; padding: 0; }
    .sticky-sidebar { position: static; }
    .action-btn, .chart-controls, .btn-toggle-analysis { display: none; }
    .transaction-table-container { max-height: none; overflow: visible; }
}

/* Additional Utilities */
.price-ref-note {
    font-size: 0.75rem;
    color: var(--text-muted);
    margin-top: 8px;
    text-align: right;
}

.mt-4 {
    margin-top: 4px;
}

.calc-summary-row {
    font-weight: bold;
    border-top: 2px solid var(--border-color);
}

/* JS Refactoring Utility Classes */
.section-header-flex {
    display: flex;
    justify-content: space-between;
    align-items: center;
}

.listings-controls-flex {
    display: flex;
    gap: 8px;
    align-items: center;
}

.listing-checkbox {
    display: flex;
    align-items: center;
    font-size: 13px;
    color: var(--text-color);
    white-space: nowrap;
    cursor: pointer;
}

.listing-checkbox input {
    margin-right: 4px;
    cursor: pointer;
}

.form-control-auto {
    width: auto;
    height: 30px;
    font-size: 13px;
    line-height: 30px;
    padding: 0 10px;
}

select.form-control.form-control-auto {
    padding-top: 0;
    padding-bottom: 0;
    line-height: 30px;
}

.text-center-padded {
    text-align: center;
    padding: 20px;
    width: 100%;
}

.text-muted-padded {
    padding: 20px;
    text-align: center;
    color: var(--text-muted);
}

.text-error-padded {
    text-align: center;
    padding: 20px;
    width: 100%;
    color: var(--color-error, #d32f2f);
}

.listing-image-wrapper {
    width: 130px; 
    aspect-ratio: 9 / 16;
    height: auto;
    background-size: cover; 
    background-repeat: no-repeat; 
    background-position: center; 
    background-color: var(--card-bg);
    background-image: var(--bg-img);
    border-radius: 4px;
    margin: 0 auto;
    position: relative;
}

.sold-overlay {
    position: absolute;
    bottom: 0;
    left: 0;
    width: 100%;
    background-color: rgba(0, 0, 0, 0.7); /* Slightly darker for better contrast */
    display: flex;
    justify-content: center;
    align-items: center;
    border-bottom-left-radius: 4px;
    border-bottom-right-radius: 4px;
    z-index: 5;
    padding: 4px 0;
}

.sold-overlay span {
    color: white;
    font-weight: bold;
    font-size: 12px;
    line-height: 1;
}

.listing-info-padded {
    padding: 8px;
    text-align: center;
}

.listing-price-text {
    font-weight: bold; 
    color: var(--accent-color); 
    font-size: 14px;
}

.price-value {
    font-size: var(--font-size-lg);
    font-weight: 700;
    color: var(--accent-color);
    display: inline-block;
    margin-right: 4px;
}

.listing-grade-text {
    font-size: 12px;
    color: var(--text-muted);
    margin-top: 4px;
}

.grade-badge {
    background: var(--border-color); 
    padding: 2px 6px; 
    border-radius: 4px;
}

.scroll-container-flex {
    display: flex; 
    overflow-x: auto; 
    gap: 16px; 
    padding: 4px; 
    scrollbar-width: none;
    scroll-behavior: smooth;
}

.btn-expand-trans {
    width: auto;
    padding: 8px 24px;
    background: var(--card-bg);
    color: var(--text-color);
    border: 1px solid var(--border-color);
    border-radius: var(--radius);
    cursor: pointer;
    font-size: var(--font-size-sm);
    font-weight: 600;
    transition: background 0.15s, border-color 0.15s;
}

.btn-expand-trans:hover {
    background: var(--bg-color);
    border-color: var(--primary-color);
    color: var(--primary-color);
}

.more-grades-container {
    margin-top: 20px;
}

.text-bold {
    font-weight: bold;
}

.gauge-label {
    font-weight: bold; 
    font-size: 16px;
    /* Color handled by specific classes */
}

/* Gauge Colors */
.text-gauge-green { color: #4CAF50; }
.text-gauge-light-green { color: #8BC34A; }
.text-gauge-yellow { color: #FFC107; }
.text-gauge-orange { color: var(--accent-color); }
.text-gauge-red { color: #F44336; }

/* Removed duplicate .btn-range.active */

.gauge-desc {
    color: var(--text-muted);
    font-size: 12px;
    display: block;
    text-align: left;
    margin-top: 4px;
}

.text-success {
    color: #28a745;
}

.text-danger {
    color: #dc3545;
}

.text-neutral {
    color: var(--text-color);
}

.monthly-avg-change {
    font-variant-numeric: tabular-nums;
    font-weight: 700;
}

.monthly-avg-change-up {
    color: #dc3545;
}

.monthly-avg-change-down {
    color: #28a745;
}

.monthly-avg-change-neutral {
    color: var(--text-muted);
    font-weight: 600;
}

/* Listings Container & Navigation */
.listings-wrapper {
    display: block;
}

.listings-carousel {
    position: relative;
    padding: 0 48px;
}

/* 手機預設：橫向捲動，每張卡片固定寬度 */
.listing-card {
    display: block;
    text-decoration: none;
    color: inherit;
    flex: 0 0 auto;
    width: 130px;
    transition: transform 0.2s;
}

.listing-card:hover {
    transform: translateY(-2px) scale(1.03);
}

.listing-nav-btn {
    position: absolute;
    top: 50%;
    transform: translateY(-50%);
    width: 32px;
    height: 32px;
    border-radius: 50%;
    background: var(--card-bg);
    border: 1px solid var(--border-color);
    box-shadow: 0 2px 6px rgba(0, 0, 0, 0.14);
    display: flex;
    align-items: center;
    justify-content: center;
    cursor: pointer;
    z-index: 2;
    font-size: 14px;
    color: var(--text-color);
    transition: all 0.2s;
}

.listing-nav-btn:hover {
    background: var(--accent-color);
    color: #000;
    border-color: var(--accent-color);
    box-shadow: 0 4px 8px rgba(252, 163, 17, 0.3);
}

.listing-nav-btn.prev {
    left: 0;
}

.listing-nav-btn.next {
    right: 0;
}

.page-indicator {
    text-align: center;
    font-size: 0.75rem;
    color: var(--text-muted);
    margin: 4px auto 0;
}

/* 手機版（寬度小於 1000px）：目前正在販售改為三欄平均 */
@media (max-width: 999px) {
    .listings-carousel {
        padding: 0;
    }

    .listings-carousel .scroll-container-flex {
        overflow-x: visible;
        flex-wrap: wrap;
        gap: 20px;
        padding: 4px 0;
        max-width: 480px;
        margin: 0 auto;
    }

    .listings-carousel .listing-image-wrapper {
        width: 100%;
    }

    .listings-carousel .listing-card {
        box-sizing: border-box;
        /* 3 張卡片平均分配整個列寬度（扣除 40px 間距） */
        flex: 0 0 calc((100% - 40px) / 3);
        width: calc((100% - 40px) / 3);
    }
}

@media (min-width: 1000px) {
    .listings-carousel .listing-card {
        box-sizing: border-box;
        /* 5 張卡片 + 4 個間距（10px），寬度平均分成 5 份 */
        width: calc((100% - 4 * 10px) / 5);
    }

    .listings-carousel .scroll-container-flex {
        padding-left: 0;
        padding-right: 0;
        gap: 10px;
    }

    /* 桌機：圖片容器吃滿卡片寬度（保持 9:16 比例），5 張平均分配在一排 */
    .listings-carousel .listing-image-wrapper {
        width: 100%;
    }
}

/* =========================================
   Monthly Average Section (RWD Optimization)
   ========================================= */

#monthly-avg-section {
    background-color: var(--card-bg);
    border: 1px solid var(--border-color);
    border-radius: var(--radius);
    padding: var(--spacing-sm);
    box-shadow: 0 2px 4px rgba(0,0,0,0.02);
}

#monthly-avg-section .section-title {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-bottom: var(--spacing-sm);
    font-weight: bold;
    color: var(--primary-color);
}

#monthly-avg-section .monthly-avg-controls {
    display: flex;
    gap: 4px;
}

#monthly-avg-section .btn-monthly-avg {
    padding: 4px 12px;
    font-size: 0.85rem;
    border: 1px solid var(--border-color);
    background-color: var(--bg-color);
    color: var(--text-color);
    border-radius: 4px;
    cursor: pointer;
    transition: all 0.2s;
}

#monthly-avg-section .btn-monthly-avg:hover {
    background-color: var(--border-color);
}

#monthly-avg-section .btn-monthly-avg.active {
    background-color: var(--primary-color);
    color: #fff;
    border-color: var(--primary-color);
}

/* Table Styling */
#monthly-avg-section .monthly-avg-table {
    width: 100%;
    border-collapse: separate;
    border-spacing: 0;
    table-layout: fixed;
    margin-bottom: 0;
}

#monthly-avg-section .monthly-avg-table th {
    background-color: var(--bg-color);
    color: var(--text-muted);
    font-weight: 600;
    padding: 8px 10px;
    border-bottom: 2px solid var(--border-color);
    font-size: 0.9rem;
    vertical-align: middle;
}

#monthly-avg-section .monthly-avg-table td {
    padding: 8px 10px;
    border-bottom: 1px solid var(--border-color);
    vertical-align: middle;
    font-size: 1rem;
}

#monthly-avg-section .monthly-avg-table th:nth-child(1),
#monthly-avg-section .monthly-avg-table td:nth-child(1) {
    text-align: center;
    font-variant-numeric: tabular-nums;
}

#monthly-avg-section .monthly-avg-table th:nth-child(2),
#monthly-avg-section .monthly-avg-table td:nth-child(2) {
    text-align: right;
    font-variant-numeric: tabular-nums;
}

#monthly-avg-section .monthly-avg-table th:nth-child(3),
#monthly-avg-section .monthly-avg-table td:nth-child(3) {
    text-align: right;
    font-variant-numeric: tabular-nums;
}

#monthly-avg-section .monthly-avg-table th:nth-child(4),
#monthly-avg-section .monthly-avg-table td:nth-child(4) {
    text-align: center;
    font-variant-numeric: tabular-nums;
}

#monthly-avg-section .monthly-avg-table tbody tr:nth-child(even) td {
    background-color: rgba(0, 0, 0, 0.015);
}

#monthly-avg-section .monthly-avg-table tbody tr:hover td {
    background-color: rgba(20, 33, 61, 0.04);
}

#monthly-avg-section .table-responsive {
    overflow-x: visible;
}

#monthly-avg-section .monthly-avg-table tr:last-child td {
    border-bottom: none;
}

/* Change Colors & Styling */
#monthly-avg-section .monthly-avg-change {
    font-weight: 700;
    padding: 2px 6px;
    border-radius: 4px;
    display: inline-block;
    text-align: center;
}

#monthly-avg-section .monthly-avg-change-up {
    color: #dc3545; /* Red for Up */
    background-color: rgba(220, 53, 69, 0.08);
}

#monthly-avg-section .monthly-avg-change-down {
    color: #28a745; /* Green for Down */
    background-color: rgba(40, 167, 69, 0.08);
}

#monthly-avg-section .monthly-avg-change-neutral {
    color: var(--text-muted);
    background-color: rgba(0, 0, 0, 0.03);
}

/* Mobile Optimization (< 768px) */
@media (max-width: 767px) {
    #monthly-avg-section {
        padding: 12px;
        margin-left: -4px;
        margin-right: -4px;
        border-radius: 4px;
    }

    #monthly-avg-section .section-title {
        flex-direction: column;
        align-items: flex-start;
        gap: 8px;
    }

    #monthly-avg-section .monthly-avg-controls {
        width: 100%;
    }

    #monthly-avg-section .btn-monthly-avg {
        flex: 1;
        text-align: center;
        padding: 8px 0;
    }

    #monthly-avg-section .monthly-avg-table th,
    #monthly-avg-section .monthly-avg-table td {
        padding: 8px 4px;
        font-size: 0.9rem;
    }
    
    #monthly-avg-section .monthly-avg-change {
        min-width: auto;
        padding: 2px 4px;
        font-size: 0.85rem;
    }

    #monthly-avg-section .table-responsive {
        overflow-x: auto;
    }
}

/* Dark Mode Overrides */
[data-theme="dark"] .stat-item,
[data-theme="dark"] .transaction-table-container,
[data-theme="dark"] .transaction-table,
[data-theme="dark"] .summary-card,
[data-theme="dark"] .calculator-box,
[data-theme="dark"] #monthly-avg-section,
[data-theme="dark"] .listing-nav-btn {
    background-color: var(--card-bg);
    border-color: var(--border-color);
}

[data-theme="dark"] .transaction-table th {
    background-color: var(--bg-color); /* Slightly darker than card-bg */
    color: var(--text-color);
    border-bottom-color: var(--border-color);
}

[data-theme="dark"] .grade-tag {
    background-color: var(--border-color);
    color: var(--text-color);
}

[data-theme="dark"] .grade-tag.psa10 {
    background-color: var(--primary-color);
    color: #ffffff;
}

[data-theme="dark"] .grade-tag.psa9 {
    background-color: var(--accent-color);
    color: #000000;
}

[data-theme="dark"] #monthly-avg-section .monthly-avg-table th {
    background-color: var(--bg-color);
    color: var(--text-muted);
    border-bottom-color: var(--border-color);
}

[data-theme="dark"] #monthly-avg-section .btn-monthly-avg {
    background-color: var(--bg-color);
    border-color: var(--border-color);
    color: var(--text-color);
}

[data-theme="dark"] #monthly-avg-section .btn-monthly-avg:hover {
    background-color: var(--border-color);
}

[data-theme="dark"] #monthly-avg-section .btn-monthly-avg.active {
    background-color: var(--primary-color);
    color: #fff;
    border-color: var(--primary-color);
}

[data-theme="dark"] #monthly-avg-section .monthly-avg-table tbody tr:nth-child(even) td {
    background-color: rgba(255, 255, 255, 0.05);
}

[data-theme="dark"] #monthly-avg-section .monthly-avg-table tbody tr:hover td {
    background-color: rgba(255, 255, 255, 0.1);
}

[data-theme="dark"] .section-title,
[data-theme="dark"] .analysis-title {
    color: #ffffff;
}

/* Fix: #monthly-avg-section .section-title has specificity 0-1-1-0 which beats
   the generic [data-theme="dark"] .section-title rule (0-1-0). Adding the ID
   raises specificity to 0-2-1-0 so the white override wins in dark mode. */
[data-theme="dark"] #monthly-avg-section .section-title {
    color: #ffffff;
}

/* Fix: .text-psa9 uses var(--primary-color) which is #101A31 in dark mode —
   nearly invisible on the #181A1B card background. Override to readable text. */
[data-theme="dark"] .text-psa9 {
    color: var(--text-color);
}

/* Link, Button, and Header Colors */
[data-theme="dark"] a:not(.btn),
[data-theme="dark"] button:not(.btn):not(.theme-toggle-btn),
[data-theme="dark"] h1,
[data-theme="dark"] h4 {
    color: #ffffff;
}
/* Gauge and semantic color spans must NOT be overridden — they use specific color classes */

/* Scrollbars */
[data-theme="dark"] *::-webkit-scrollbar {
    width: 12px;
    height: 12px;
    background-color: var(--bg-color);
}

[data-theme="dark"] *::-webkit-scrollbar-track {
    background-color: var(--bg-color);
}

[data-theme="dark"] *::-webkit-scrollbar-thumb {
    background-color: var(--border-color);
    border-radius: 6px;
    border: 3px solid var(--bg-color);
}

/* Form Controls Dark Mode */
[data-theme="dark"] .form-control,
[data-theme="dark"] input,
[data-theme="dark"] select,
[data-theme="dark"] textarea {
    background-color: var(--card-bg);
    color: var(--text-color);
    border-color: var(--border-color);
}

[data-theme="dark"] .form-control:focus,
[data-theme="dark"] input:focus,
[data-theme="dark"] select:focus,
[data-theme="dark"] textarea:focus {
    background-color: var(--bg-color);
    border-color: var(--accent-color);
    color: var(--text-color);
}

/* Ensure table headers in dark mode */
[data-theme="dark"] .price-table th {
    background-color: var(--bg-color);
    color: var(--text-muted);
}

/* Ensure monthly avg table rows in dark mode */
[data-theme="dark"] #monthly-avg-section .monthly-avg-table tbody tr:nth-child(even) td {
    background-color: rgba(255, 255, 255, 0.05);
}

[data-theme="dark"] #monthly-avg-section .monthly-avg-table tbody tr:hover td {
    background-color: rgba(255, 255, 255, 0.1);
}

/* Fix any remaining white backgrounds in nested divs */
[data-theme="dark"] .card-details-grid div[style*="background: #fff"],
[data-theme="dark"] .card-details-grid div[style*="background-color: #fff"],
[data-theme="dark"] .card-details-grid div[style*="background: white"],
[data-theme="dark"] .card-details-grid div[style*="background-color: white"] {
    background-color: var(--card-bg) !important;
    color: var(--text-color) !important;
}

/* Breadcrumb Navigation */
.breadcrumb-nav {
    padding: 6px var(--spacing-sm);
    margin-bottom: 4px;
}

.breadcrumb {
    display: flex;
    flex-wrap: wrap;
    align-items: center;
    list-style: none;
    margin: 0;
    padding: 0;
    gap: 0;
    font-size: 0.8rem;
    color: var(--text-muted);
}

.breadcrumb__item + .breadcrumb__item::before {
    content: "›";
    margin: 0 5px;
    color: var(--text-muted);
    font-size: 1rem;
    line-height: 1;
}

.breadcrumb__item a {
    color: var(--primary-color);
    text-decoration: none;
    transition: color 0.15s, opacity 0.15s;
    opacity: 0.85;
}

.breadcrumb__item a:hover {
    opacity: 1;
    text-decoration: underline;
}

.breadcrumb__item--current span {
    color: var(--text-color);
    font-weight: 500;
    max-width: 200px;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
    display: inline-block;
    vertical-align: bottom;
}

@media (min-width: 600px) {
    .breadcrumb__item--current span {
        max-width: 360px;
    }
}

[data-theme="dark"] .breadcrumb__item a:hover {
    color: var(--accent-color);
    opacity: 1;
}

/* ── Hot Cards Scroll Strip ─────────────────────────────── */
.hot-cards-section {
    margin-top: 20px;
    padding: 0 var(--spacing-sm) var(--spacing-md);
}

/* Header row: title left, arrows right */
.hot-cards-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    margin-bottom: 10px;
}

.hot-cards-section__title {
    font-size: 0.95rem;
    font-weight: 700;
    color: var(--text-color);
    margin: 0;
}

/* Right-side action group: "查看完整系列" link + arrow buttons */
.hot-cards-actions {
    display: flex;
    align-items: center;
    gap: 10px;
    flex-shrink: 0;
}

.hot-cards-section__icon {
    display: inline-flex;
    align-items: center;
    margin-right: 5px;
    vertical-align: middle;
    opacity: 0.7;
}

.hot-cards-set-link {
    display: inline-flex;
    align-items: center;
    gap: 3px;
    font-size: 0.76rem;
    font-weight: 600;
    color: var(--primary-color, #1a73e8);
    text-decoration: none;
    white-space: nowrap;
    padding: 4px 8px;
    border-radius: 20px;
    border: 1px solid var(--primary-color, #1a73e8);
    transition: background 0.15s, color 0.15s;
    line-height: 1;
}

.hot-cards-set-link:hover {
    background: var(--primary-color, #1a73e8);
    color: #fff;
}

[data-theme="dark"] .hot-cards-set-link {
    color: var(--accent-color, #7baaf7);
    border-color: var(--accent-color, #7baaf7);
}

[data-theme="dark"] .hot-cards-set-link:hover {
    background: var(--accent-color, #7baaf7);
    color: #fff;
}

/* Arrow buttons */
.hot-cards-nav {
    display: flex;
    gap: 6px;
}

.hot-cards-btn {
    width: 32px;
    height: 32px;
    border-radius: 50%;
    border: 1px solid var(--border-color, #e8eaed);
    background: var(--card-bg, #fff);
    color: var(--text-color);
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    transition: background 0.15s, box-shadow 0.15s;
    flex-shrink: 0;
    /* Ensure 44px touch target without changing visual size */
    padding: 0;
    position: relative;
}

.hot-cards-btn::before {
    content: '';
    position: absolute;
    inset: -6px;
}

.hot-cards-btn:hover:not(:disabled) {
    background: var(--primary-color, #14213d);
    color: #fff;
    border-color: var(--primary-color, #14213d);
    box-shadow: 0 2px 8px rgba(20, 33, 61, 0.3);
}

.hot-cards-btn:disabled {
    opacity: 0.35;
    cursor: default;
}

/* Scroll track */
.hot-cards-track {
    display: flex;
    gap: 10px;
    overflow-x: auto;
    scroll-snap-type: x mandatory;
    -webkit-overflow-scrolling: touch;
    scrollbar-width: none;        /* Firefox */
    padding-bottom: 4px;          /* room for box-shadow */
    list-style: none;
    margin: 0;
    padding-left: 0;
    padding-right: 16px;          /* peek gap for last card */
}

.hot-cards-track::-webkit-scrollbar {
    display: none;                /* Chrome / Safari */
}

/* Each card slot */
.hot-cards-track__item {
    flex-shrink: 0;
    scroll-snap-align: start;
    width: 108px;
}

.hot-card-link {
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 6px;
    text-decoration: none;
    color: var(--text-color);
    background: var(--card-bg, #fff);
    border: 1px solid var(--border-color, #e8eaed);
    border-radius: var(--radius, 8px);
    padding: 10px 8px 8px;
    height: 100%;
    transition: box-shadow 0.15s, transform 0.15s;
}

.hot-card-link:hover {
    box-shadow: 0 4px 14px rgba(0,0,0,.12);
    transform: translateY(-2px);
    color: var(--primary-color);
}

.hot-card-link:focus-visible {
    outline: 2px solid var(--primary-color, #14213d);
    outline-offset: 2px;
}

.hot-card-link__img {
    width: 100%;
    aspect-ratio: 63 / 88;   /* standard PTCG card ratio */
    object-fit: contain;
    border-radius: 4px;
    display: block;
}

.hot-card-link__name {
    font-size: 0.75rem;
    text-align: center;
    line-height: 1.3;
    width: 100%;
    overflow: hidden;
    display: -webkit-box;
    -webkit-line-clamp: 2;
    -webkit-box-orient: vertical;
}

.hot-card-link__num {
    font-size: 0.72rem;
    color: var(--text-muted, #888);
    text-align: center;
    width: 100%;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
    line-height: 1.2;
}

/* Mobile: slightly smaller cards, still 4+ visible */
@media (max-width: 480px) {
    .hot-cards-track__item {
        width: 82px;
    }
    /* hot-card-link__img uses width:100% + aspect-ratio, no override needed */
    .hot-cards-btn {
        width: 28px;
        height: 28px;
    }

    .hot-cards-btn svg {
        width: 12px;
        height: 12px;
    }
    /* Expand touch target to 44px minimum on mobile (WCAG 2.5.5) */
    .hot-cards-btn::before {
        inset: -8px;
    }
}

/* Respect user's reduced-motion preference — covers all animations on this page */
@media (prefers-reduced-motion: reduce) {
    *,
    *::before,
    *::after {
        animation-duration: 0.01ms !important;
        animation-iteration-count: 1 !important;
        transition-duration: 0.01ms !important;
    }
}

/* Dark mode */
[data-theme="dark"] .hot-card-link {
    background: var(--card-bg);
    border-color: var(--border-color);
}

[data-theme="dark"] .hot-card-link:hover {
    color: var(--accent-color);
    box-shadow: 0 4px 14px rgba(0,0,0,.4);
}

[data-theme="dark"] .hot-cards-btn {
    background: var(--card-bg);
    border-color: var(--border-color);
}

[data-theme="dark"] .hot-cards-btn:hover:not(:disabled) {
    background: var(--primary-color, #101A31);
    color: #fff;
}

/* ── PSA 官方成交走勢 ──────────────────────────────────────────────── */

.psa-trend-loading,
.psa-trend-empty,
.psa-trend-error {
    position: absolute;
    inset: 0;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 0.85rem;
    color: var(--text-muted, #888);
}

.psa-trend-retry {
    margin-left: 0.5rem;
    padding: 0.2rem 0.5rem;
    font-size: 0.8rem;
    border: 1px solid var(--primary-color, #14213d);
    border-radius: 4px;
    background: transparent;
    color: var(--primary-color, #14213d);
    cursor: pointer;
}

.psa-trend-retry:hover {
    background: var(--primary-color, #14213d);
    color: #fff;
}

/* =========================================
   UX Enhancement Block
   - Focus visibility (WCAG 2.1 §2.4.7)
   - Interaction press feedback
   - Breadcrumb dark-mode contrast fix
   - Data section visual hierarchy
   - Transaction table readability
   ========================================= */

/* ── Focus Visible — keyboard navigation ─────────────────────────── */
.action-btn:focus-visible,
.back-link:focus-visible,
.btn-toggle-analysis:focus-visible,
.btn-expand-trans:focus-visible,
.btn-monthly-avg:focus-visible,
.btn-limit-plan:focus-visible,
.btn-limit-secondary:focus-visible,
.btn-limit-primary:focus-visible,
.floating-fav-btn:focus-visible,
.hot-cards-set-link:focus-visible,
.psa-trend-retry:focus-visible {
    outline: none;
    box-shadow: var(--focus-ring, 0 0 0 3px rgba(252, 163, 17, 0.4));
}

/* ── Action button press feedback ────────────────────────────────── */
.action-btn:active {
    transform: translateY(1px);
}

.btn-expand-trans:active,
.btn-monthly-avg:active {
    transform: translateY(1px);
}

/* ── Breadcrumb — dark-mode contrast fix ─────────────────────────── */
/* var(--primary-color) = #101A31 in dark mode; contrast ~1.2:1 on #272A2C → WCAG fail */
[data-theme="dark"] .breadcrumb__item a {
    color: var(--accent-color);
    opacity: 0.9;
}

[data-theme="dark"] .breadcrumb__item a:hover {
    color: var(--accent-color);
    opacity: 1;
}

/* ── Data section — subtle elevation for visual hierarchy ─────────── */
.data-section {
    box-shadow: 0 1px 4px rgba(0, 0, 0, 0.06);
}

[data-theme="dark"] .data-section {
    box-shadow: 0 1px 4px rgba(0, 0, 0, 0.35);
}

/* ── Transaction table — row striping for readability ─────────────── */
.transaction-table tbody tr:nth-child(even) td {
    background-color: rgba(0, 0, 0, 0.018);
}

[data-theme="dark"] .transaction-table tbody tr:nth-child(even) td {
    background-color: rgba(255, 255, 255, 0.04);
}

/* ── Error color token in dark mode ──────────────────────────────── */
[data-theme="dark"] .text-error-padded {
    color: #ef5350;
}

/* ── touch-action: manipulation — removes 300ms tap delay on Android ─ */
button,
a,
.action-btn,
.back-link,
.btn-range,
.btn-toggle-analysis,
.btn-expand-trans,
.btn-monthly-avg,
.btn-limit-plan,
.listing-nav-btn,
.hot-cards-btn,
.floating-fav-btn,
[role="button"] {
    touch-action: manipulation;
}

/* ── Safe area inset — PWA / notch / Dynamic Island devices ──────── */
/* When running as home-screen PWA, position:fixed top:0 is at the physical
   top of screen (behind the notch). padding-top override restores visual clearance. */
@supports (padding: env(safe-area-inset-top)) {
    @media (max-width: 1023px) {
        .sticky-title {
            padding-top: env(safe-area-inset-top, 0px) !important;
            height: calc(72px + env(safe-area-inset-top, 0px));
        }

        .floating-fav-btn {
            top: calc(92px + env(safe-area-inset-top, 0px)) !important;
        }
    }
}

/* ── Landscape compact mode — mobile portrait → landscape ────────── */
/* At <500px viewport height (typical phone landscape), reduce fixed element
   heights so content area doesn't get crushed. */
@media (orientation: landscape) and (max-height: 500px) {
    .page-header {
        height: 48px;
    }

    .card-details-grid {
        padding: var(--spacing-xs);
    }

    @media (max-width: 1023px) {
        .sticky-title {
            height: 52px;
        }
    }

    .promo-footer {
        height: 40px;
        font-size: 0.72rem;
        padding: 0 14px;
    }
}
