/* ═══════════════════════════════════════════════════════════════════════════
   Market Analysis — Visual Design
   UX: Von Restorff (accent colors), System-1 (stat hero), Serial Position
       (top-3 badge), Skeleton (spatial memory), RWD-first layout
   ═══════════════════════════════════════════════════════════════════════════ */

/* ── Shared FAB base (all 3 floating action buttons) ──────────────────── */
.gacha-fab,
.market-analysis-btn {
    position: fixed;
    right: 24px;
    width: 56px;
    height: 56px;
    border-radius: 50%;
    border: none;
    cursor: pointer;
    z-index: 1000;
    display: flex;
    align-items: center;
    justify-content: center;
    transition: transform 0.2s ease, box-shadow 0.2s ease;
    touch-action: manipulation;
    -webkit-tap-highlight-color: transparent;
}
.gacha-fab:hover,
.market-analysis-btn:hover { transform: scale(1.1); }
.gacha-fab:active,
.market-analysis-btn:active { transform: scale(0.95); }
.gacha-fab:focus-visible,
.market-analysis-btn:focus-visible {
    outline: 3px solid rgba(255,255,255,0.85);
    outline-offset: 2px;
}

/* ── Gacha FAB (抽卡模擬器) ────────────────────────────────────────────── */
.gacha-fab {
    bottom: calc(160px + env(safe-area-inset-bottom, 0px));
    background: linear-gradient(135deg, #14213d 0%, #6c3eb8 100%);
    box-shadow: 0 4px 14px rgba(108, 62, 184, 0.45);
    text-decoration: none;
}
.gacha-fab:hover { box-shadow: 0 6px 20px rgba(108, 62, 184, 0.65); }
.gacha-fab img {
    width: 30px;
    height: 30px;
    object-fit: contain;
    pointer-events: none;
}

/* ── Market Analysis FAB ────────────────────────────────────────────────── */
.market-analysis-btn {
    bottom: calc(92px + env(safe-area-inset-bottom, 0px));
    background: linear-gradient(135deg, #ff6b35 0%, #f7931e 100%);
    color: white;
    box-shadow: 0 4px 14px rgba(255, 107, 53, 0.5);
}
.market-analysis-btn:hover { box-shadow: 0 6px 20px rgba(255, 107, 53, 0.65); }

@media (max-width: 480px) {
    .gacha-fab,
    .market-analysis-btn {
        right: 16px;
        width: 48px;
        height: 48px;
    }
    .gacha-fab {
        bottom: calc(148px + env(safe-area-inset-bottom, 0px));
    }
    .gacha-fab img { width: 26px; height: 26px; }
    .market-analysis-btn {
        bottom: calc(84px + env(safe-area-inset-bottom, 0px));
    }
}

@media (prefers-reduced-motion: reduce) {
    .gacha-fab,
    .market-analysis-btn { transition: none; }
}

/* ── Per-Section Theme Accents (Von Restorff differentiation) ──────────── */
/* 鞋店 (snkrdunk) sections */
[data-theme="sold"]             { --ma-accent: #f59e0b; --ma-accent-bg: rgba(245,158,11,0.08); }
[data-theme="gain"]             { --ma-accent: #16a34a; --ma-accent-bg: rgba(22,163,74,0.08); }
[data-theme="change"]           { --ma-accent: #3b82f6; --ma-accent-bg: rgba(59,130,246,0.08); }
[data-theme="gain-pct-snkr"]   { --ma-accent: #15803d; --ma-accent-bg: rgba(21,128,61,0.08); }
[data-theme="change-pct-snkr"] { --ma-accent: #6366f1; --ma-accent-bg: rgba(99,102,241,0.08); }
/* PSA商店 (psa_price_timeseries / psa_population_data) sections */
[data-theme="psa-sold"]        { --ma-accent: #ea580c; --ma-accent-bg: rgba(234,88,12,0.08); }
[data-theme="gain-pct"]        { --ma-accent: #0891b2; --ma-accent-bg: rgba(8,145,178,0.08); }
[data-theme="change-pct"]      { --ma-accent: #dc2626; --ma-accent-bg: rgba(220,38,38,0.08); }
[data-theme="gain-jpy-psa"]    { --ma-accent: #0369a1; --ma-accent-bg: rgba(3,105,161,0.08); }
[data-theme="change-jpy-psa"]  { --ma-accent: #b91c1c; --ma-accent-bg: rgba(185,28,28,0.08); }
[data-theme="population"]      { --ma-accent: #7c3aed; --ma-accent-bg: rgba(124,58,237,0.08); }
/* User data sections */
[data-theme="favorites"]   { --ma-accent: #e11d48; --ma-accent-bg: rgba(225,29,72,0.08); }
[data-theme="portfolio"]   { --ma-accent: #0d9488; --ma-accent-bg: rgba(13,148,136,0.08); }

/* ── Source Badges (鞋店 / PSA商店) ────────────────────────────────────── */
.ma-source-badge {
    display: inline-block;
    font-size: 0.75rem;
    font-weight: 700;
    letter-spacing: 0.04em;
    padding: 0.1rem 0.4rem;
    border-radius: 3px;
    vertical-align: middle;
    line-height: 1.6;
    white-space: nowrap;
    flex-shrink: 0;
}
.ma-source-badge--snkr {
    background: #29b6f6;
    color: #fff;
}
.ma-source-badge--psa {
    background: #e53935;
    color: #fff;
}

/* Dark-mode soften accents slightly */
[data-theme] { --ma-accent-border: var(--ma-accent); }

/* ── Testing Notice Banner ─────────────────────────────────────────────── */
.ma-notice {
    display: flex;
    align-items: flex-start;
    gap: var(--space-sm);
    padding: 10px var(--space-md);
    margin-bottom: var(--space-md);
    background: rgba(245, 158, 11, 0.12);
    border: 1px solid rgba(245, 158, 11, 0.45);
    border-radius: var(--radius-md);
    font-size: var(--font-size-xs);
    color: var(--color-text-secondary);
    line-height: 1.6;
}
.ma-notice-icon { font-size: 14px; flex-shrink: 0; margin-top: 1px; }
.ma-notice strong { color: var(--color-text-primary); font-weight: 600; }

/* ── Page Shell ────────────────────────────────────────────────────────── */
.ma-page {
    min-height: 100dvh;
    background: var(--color-bg-body);
    padding-bottom: var(--space-xxl);
    font-family: var(--font-family-base);
}

/* ── Page container — constrains max-width (matches card_details pattern) ── */
.ma-container {
    max-width: 1100px;
    margin: 0 auto;
    padding: 0 var(--space-lg);
}

/* ── Header — 3-col flex, same pattern as card_details.php .page-header ─── */
/* Override global `header` rule from styles.css */
.ma-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: var(--space-md);
    height: 64px;
    padding: 0 var(--space-md);
    background: var(--color-primary);
    border-radius: var(--radius-lg);
    box-shadow: var(--shadow-md);
    /* Override sticky/border from global header rule */
    position: static;
    border-bottom: none;
    margin-bottom: var(--space-md);
}

/* Left col: back link + brand */
.ma-header-left {
    display: flex;
    align-items: center;
    gap: var(--space-md);
    flex-shrink: 0;
}

.ma-header-back {
    display: flex;
    align-items: center;
    gap: 5px;
    color: var(--color-primary-light);
    text-decoration: none;
    font-size: var(--font-size-sm);
    font-weight: 600;
    white-space: nowrap;
    min-height: 44px; /* Fitts's Law */
    transition: color 0.15s;
}
.ma-header-back:hover { text-decoration: underline; color: #e5940f; }

.ma-header-brand {
    display: flex;
    align-items: center;
    gap: var(--space-sm);
    white-space: nowrap;
}
.ma-header-brand h1 {
    margin: 0;
    font-size: var(--font-size-base);
    font-weight: 700;
    color: #fff;
}
.ma-flame { font-size: 20px; }

/* Center col: dates — fade in after data loads */
.ma-header-center {
    flex: 1 1 auto;
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 6px;
    min-width: 0;
    overflow: hidden;
    font-size: var(--font-size-xs);
    color: rgba(255, 255, 255, 0.45);
    opacity: 0;
    transition: opacity 0.4s ease;
}
.ma-header-center.visible { opacity: 1; }
.ma-meta-label { color: rgba(255, 255, 255, 0.45); white-space: nowrap; }
.ma-meta-value { color: rgba(255, 255, 255, 0.9); font-weight: 600; white-space: nowrap; }
.ma-meta-arrow { color: rgba(255, 255, 255, 0.3); font-size: 11px; }

/* Right col: currency toggle + refresh */
.ma-header-right {
    flex-shrink: 0;
    display: flex;
    align-items: center;
    gap: var(--space-sm);
}
/* Cancel the global margin-left on these buttons so only the flex gap applies */
.ma-header-right .theme-toggle-btn,
.ma-header-right .currency-toggle-btn {
    margin-left: 0;
}

.ma-refresh-btn {
    display: flex;
    align-items: center;
    gap: 6px;
    padding: 7px 14px;
    background: rgba(255, 255, 255, 0.12);
    border: 1px solid rgba(255, 255, 255, 0.22);
    border-radius: 20px;
    color: rgba(255, 255, 255, 0.85);
    font-size: var(--font-size-xs);
    font-family: inherit;
    white-space: nowrap;
    cursor: pointer;
    min-height: 44px;
    transition: background 0.15s, color 0.15s;
}
.ma-refresh-btn:hover { background: rgba(255, 255, 255, 0.22); color: #fff; }
.ma-refresh-icon { font-size: 15px; display: inline-block; }
.ma-refresh-btn.spinning .ma-refresh-icon { animation: ma-spin 0.7s linear infinite; }

/* ── Mobile ─────────────────────────────────────────────────────────────── */
@media (max-width: 640px) {
    .ma-container { padding: 0 var(--space-sm); }
    .ma-header {
        height: 52px;
        padding: 0 var(--space-sm);
        border-radius: var(--radius-md);
        margin-bottom: var(--space-sm);
    }
    .ma-header-center { display: none; }
    .ma-refresh-label { display: none; }
    .ma-refresh-btn { padding: 7px 10px; }
    .ma-header-brand h1 { font-size: var(--font-size-sm); }
    .ma-back-label { display: none; }
}

/* ── Grouped Layout ─────────────────────────────────────────────────────── */
/* Three category groups (熱銷 / 價格 / 收藏), each with its own 2-col grid */
.ma-groups {
    display: flex;
    flex-direction: column;
    gap: var(--space-xl);
}

/* Group separator row */
.ma-group-header {
    display: flex;
    align-items: center;
    gap: var(--space-sm);
    margin-bottom: var(--space-md);
}

.ma-group-icon {
    font-size: 15px;
    flex-shrink: 0;
}

.ma-group-label {
    font-size: var(--font-size-xs);
    font-weight: 700;
    color: var(--color-text-secondary);
    letter-spacing: 0.08em;
    text-transform: uppercase;
    white-space: nowrap;
}

/* Horizontal rule that fills remaining width */
.ma-group-line {
    flex: 1;
    height: 1px;
    background: var(--color-border);
}

/* 2-col grid inside each group */
.ma-group-grid {
    display: grid;
    grid-template-columns: repeat(2, 1fr);
    gap: var(--space-lg);
}

@media (max-width: 860px) { .ma-group-grid { grid-template-columns: 1fr; } }
@media (max-width: 480px) {
    .ma-group-grid  { gap: var(--space-md); }
    .ma-groups      { gap: var(--space-lg); }
    .ma-group-label { font-size: 10px; }
}

/* ── Store sub-groups (Gestalt Common Region — groups sections by store) ─── */
.ma-store-group {
    margin-bottom: var(--space-xl);
}
.ma-store-group:last-child { margin-bottom: 0; }

.ma-store-header {
    display: flex;
    align-items: center;
    gap: var(--space-sm);
    margin-bottom: var(--space-md);
}

.ma-store-badge {
    display: inline-block;
    font-size: var(--font-size-xs);
    font-weight: 700;
    letter-spacing: 0.04em;
    padding: 3px 10px;
    border-radius: 20px;
    white-space: nowrap;
    flex-shrink: 0;
}
.ma-store-badge--snkr { background: #29b6f6; color: #fff; }
.ma-store-badge--psa  { background: #e53935; color: #fff; }

.ma-store-name {
    font-size: var(--font-size-xs);
    color: var(--color-text-tertiary);
    letter-spacing: 0.06em;
    white-space: nowrap;
}

/* ── Section Card ──────────────────────────────────────────────────────── */
.ma-section {
    background: var(--color-bg-surface);
    border: 1px solid var(--color-border);
    border-radius: var(--radius-lg);
    overflow: hidden;
    box-shadow: var(--shadow-sm);
    transition: box-shadow 0.2s;
}
.ma-section:hover { box-shadow: var(--shadow-md); }

.ma-section-header {
    display: flex;
    align-items: center;
    flex-wrap: wrap;
    row-gap: 4px;
    gap: var(--space-sm);
    padding: 14px var(--space-md);
    border-bottom: 1px solid var(--color-border);
    border-left: 4px solid var(--ma-accent, var(--color-primary)); /* Von Restorff accent */
    background: var(--ma-accent-bg, transparent);
}

.ma-section-icon { font-size: 18px; flex-shrink: 0; }

.ma-section-title {
    font-size: var(--font-size-base);
    font-weight: 700;
    color: var(--color-text-main);
    margin: 0;
    flex: 1;
}

.ma-section-sub {
    font-size: var(--font-size-xs);
    color: var(--color-text-tertiary);
    white-space: nowrap;
}

/* ── Ranking List ──────────────────────────────────────────────────────── */
.ma-list {
    list-style: none;
    margin: 0;
    padding: 0;
}

/* Row */
.ma-item {
    display: flex;
    align-items: center;
    gap: var(--space-sm);
    padding: 12px var(--space-md);
    border-bottom: 1px solid var(--color-border);
    cursor: pointer;
    transition: background 0.15s;
    text-decoration: none;
    color: inherit;
    position: relative;
}
.ma-item:last-child { border-bottom: none; }
.ma-item:hover { background: var(--color-bg-elevated); }

/* Top-1 row: gradient accent (Serial Position + Peak-End) */
.ma-item--top1 {
    background: linear-gradient(90deg, var(--ma-accent-bg, rgba(245,158,11,0.09)) 0%, transparent 75%);
}
.ma-item--top1:hover {
    background: linear-gradient(90deg, color-mix(in srgb, var(--ma-accent, #f59e0b) 16%, transparent) 0%, color-mix(in srgb, var(--ma-accent, #f59e0b) 4%, transparent) 75%);
}

/* Rank badge — circle (replaces plain text) */
.ma-rank {
    width: 28px;
    height: 28px;
    border-radius: 50%;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 12px;
    font-weight: 700;
    flex-shrink: 0;
    background: var(--color-bg-elevated);
    color: var(--color-text-tertiary);
    border: 1px solid var(--color-border);
}
.ma-rank-1 {
    background: linear-gradient(135deg, #fbbf24 0%, #f59e0b 55%, #d97706 100%);
    color: #fff;
    border-color: transparent;
    width: 32px;
    height: 32px;
    font-size: 13px;
    box-shadow: 0 0 0 3px rgba(245,158,11,0.28), 0 3px 14px rgba(245,158,11,0.55);
}
.ma-rank-2 { background: #6b7280; color: #fff; border-color: #6b7280; }
.ma-rank-3 { background: #92580a; color: #fff; border-color: #92580a; }

/* Card thumbnail */
.ma-thumb {
    width: 46px;
    height: 46px;
    object-fit: contain;
    border-radius: var(--radius-sm);
    flex-shrink: 0;
    background: var(--color-bg-elevated);
    border: 1px solid var(--color-border);
}

/* Text info */
.ma-info {
    flex: 1;
    min-width: 0;
}

.ma-name {
    font-size: var(--font-size-sm);
    font-weight: 600;
    color: var(--color-text-main);
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
    line-height: 1.4;
}

.ma-meta {
    font-size: var(--font-size-xs);
    color: var(--color-text-tertiary);
    margin-top: 3px;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

/* Stat column — hero number (System 1: big & scannable) */
.ma-stat {
    text-align: right;
    flex-shrink: 0;
    min-width: 56px;
}

.ma-stat-main {
    font-size: var(--font-size-lg);  /* 18px — data hero */
    font-weight: 700;
    color: var(--color-text-main);
    line-height: 1.2;
    font-variant-numeric: tabular-nums;
}

.ma-stat-sub {
    font-size: var(--font-size-xs);
    color: var(--color-text-tertiary);
    margin-top: 2px;
    font-variant-numeric: tabular-nums;
}

/* Price change colours — accessible contrast */
.ma-up   { color: #16a34a; }
.ma-down { color: #dc2626; }
.ma-flat { color: var(--color-text-tertiary); }

/* Dark mode: manual toggle (data-theme) + OS preference */
[data-theme="dark"] .ma-up   { color: #4ade80; }
[data-theme="dark"] .ma-down { color: #f87171; }
@media (prefers-color-scheme: dark) {
    .ma-up   { color: #4ade80; }
    .ma-down { color: #f87171; }
}

/* ── Skeleton Loader (spatial memory — preserves layout shape) ─────────── */
.ma-skeleton {
    display: flex;
    align-items: center;
    gap: var(--space-sm);
    padding: 12px var(--space-md);
    border-bottom: 1px solid var(--color-border);
}
.ma-skeleton:last-child { border-bottom: none; }

.ma-sk-rank,
.ma-sk-thumb,
.ma-sk-line,
.ma-sk-stat {
    border-radius: 4px;
    background: linear-gradient(
        90deg,
        var(--color-border) 25%,
        var(--color-bg-elevated) 50%,
        var(--color-border) 75%
    );
    background-size: 200% 100%;
    animation: ma-shimmer 1.4s ease-in-out infinite;
}

.ma-sk-rank  { width: 28px; height: 28px; border-radius: 50%; flex-shrink: 0; }
.ma-sk-thumb { width: 46px; height: 46px; flex-shrink: 0; }
.ma-sk-info  { flex: 1; }
.ma-sk-line-name { display: block; width: 72%; height: 13px; margin-bottom: 6px; }
.ma-sk-line-meta { display: block; width: 48%; height: 11px; }
.ma-sk-stat  { width: 48px; height: 36px; flex-shrink: 0; }

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

@media (prefers-reduced-motion: reduce) {
    .ma-sk-rank, .ma-sk-thumb, .ma-sk-line, .ma-sk-stat {
        animation: none;
        background: var(--color-border);
    }
    .ma-refresh-btn.spinning .ma-refresh-icon { animation: none; }
}

/* ── Empty / Error states ──────────────────────────────────────────────── */
.ma-empty {
    padding: var(--space-xl) var(--space-md);
    text-align: center;
    color: var(--color-text-tertiary);
    font-size: var(--font-size-sm);
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: var(--space-sm);
}

.ma-retry-btn {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    min-height: 44px;
    padding: 0 var(--space-md);
    background: var(--color-primary);
    color: #fff;
    border: none;
    border-radius: var(--radius-md);
    font-size: var(--font-size-sm);
    font-family: inherit;
    cursor: pointer;
    transition: opacity 0.15s;
}
.ma-retry-btn:hover { opacity: 0.82; }

/* ── Updated bar ───────────────────────────────────────────────────────── */
.ma-footer {
    display: flex;
    align-items: center;
    justify-content: center;
    gap: var(--space-sm);
    padding: var(--space-md) var(--space-lg);
    margin-top: var(--space-md);
    font-size: var(--font-size-xs);
    color: var(--color-text-tertiary);
}

@keyframes ma-spin {
    to { transform: rotate(360deg); }
}

/* ═══════════════════════════════════════════════════════════════════════════
   漲跌 Pill Badge — 亮眼的漲跌數據視覺化
   ─────────────────────────────────────────────────────────────────────── */

/* 主統計數字內的漲跌 span：pill 背景 + 箭頭間距 */
.ma-stat-main .ma-up,
.ma-stat-main .ma-down {
    display: inline-flex;
    align-items: center;
    gap: 3px;
    padding: 3px 10px;
    border-radius: 999px;
    font-weight: 800;
    font-size: 1.0rem;
    letter-spacing: -0.01em;
    line-height: 1.3;
}

/* 光模式漲跌 pill 顏色 */
.ma-stat-main .ma-up {
    color: #15803d;
    background: rgba(21, 128, 61, 0.10);
    border: 1px solid rgba(21, 128, 61, 0.18);
}
.ma-stat-main .ma-down {
    color: #be123c;
    background: rgba(190, 18, 60, 0.09);
    border: 1px solid rgba(190, 18, 60, 0.18);
}

/* 次要統計 (百分比 / 絕對值) — 稍微加強顏色對比 */
.ma-stat-sub .ma-up   { color: #16a34a; font-weight: 600; }
.ma-stat-sub .ma-down { color: #dc2626; font-weight: 600; }

/* 暗色模式 — data-theme 手動切換 */
[data-theme="dark"] .ma-stat-main .ma-up {
    color: #4ade80;
    background: rgba(74, 222, 128, 0.13);
    border-color: rgba(74, 222, 128, 0.22);
}
[data-theme="dark"] .ma-stat-main .ma-down {
    color: #fb7185;
    background: rgba(251, 113, 133, 0.12);
    border-color: rgba(251, 113, 133, 0.22);
}
[data-theme="dark"] .ma-stat-sub .ma-up   { color: #4ade80; }
[data-theme="dark"] .ma-stat-sub .ma-down { color: #f87171; }

/* 暗色模式 — OS prefers-color-scheme */
@media (prefers-color-scheme: dark) {
    .ma-stat-main .ma-up {
        color: #4ade80;
        background: rgba(74, 222, 128, 0.13);
        border-color: rgba(74, 222, 128, 0.22);
    }
    .ma-stat-main .ma-down {
        color: #fb7185;
        background: rgba(251, 113, 133, 0.12);
        border-color: rgba(251, 113, 133, 0.22);
    }
    .ma-stat-sub .ma-up   { color: #4ade80; }
    .ma-stat-sub .ma-down { color: #f87171; }
}

/* 排行前三名列 — 整行有特色的顏色方向感知 */
.ma-item--top2 { background: linear-gradient(90deg, rgba(107,114,128,0.06) 0%, transparent 70%); }
.ma-item--top3 { background: linear-gradient(90deg, rgba(146,88,10,0.06) 0%, transparent 70%); }
