/**
 * StockTablePanel 專用樣式
 * 統一與報價面板風格，支援主題切換
 * Updated: 2026-02-11 — Phase 1 專業化重設計
 * Design Tokens from brand-system.css
 */

/* ============================================================================
   表格捲動容器
   ============================================================================ */

.stp-table-wrapper {
    overflow-x: auto;
    overflow-y: auto;
    flex: 1;
    min-height: 0;
    scroll-snap-type: y mandatory;
    scroll-padding-top: 28px;
}

.stp-table-wrapper::-webkit-scrollbar {
    width: 6px;
    height: 6px;
}

.stp-table-wrapper::-webkit-scrollbar-track {
    background: var(--quote-panel-bg, var(--color-bg-page));
}

.stp-table-wrapper::-webkit-scrollbar-thumb {
    background: var(--quote-splitter-handle, var(--color-border-strong));
    border-radius: 3px;
}

.stp-table-wrapper::-webkit-scrollbar-thumb:hover {
    background: var(--color-text-muted);
}

/* ============================================================================
   執行健康度警告 Banner
   ============================================================================ */

.stp-health-banner {
    display: flex;
    align-items: center;
    gap: 6px;
    padding: 6px 12px;
    font-size: 12px;
    line-height: 1.4;
    flex-shrink: 0;
}

.stp-health-yellow {
    background: #fef3c7;
    color: #92400e;
    border-bottom: 1px solid #fbbf24;
}

.stp-health-red {
    background: #fee2e2;
    color: #991b1b;
    border-bottom: 1px solid #f87171;
}

.stp-health-icon {
    flex-shrink: 0;
    font-size: 13px;
}

.stp-health-text {
    flex: 1;
}

/* Dark theme */
[data-theme="dark"] .stp-health-yellow {
    background: rgba(251, 191, 36, 0.15);
    color: #fbbf24;
    border-bottom-color: rgba(251, 191, 36, 0.3);
}

[data-theme="dark"] .stp-health-red {
    background: rgba(248, 113, 113, 0.15);
    color: #f87171;
    border-bottom-color: rgba(248, 113, 113, 0.3);
}

/* ============================================================================
   工具列
   ============================================================================ */

.stp-toolbar {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 6px 10px;
    background: var(--quote-panel-surface, var(--color-bg-tertiary));
    border-bottom: 1px solid var(--quote-table-border, var(--color-border-default));
    min-height: 32px;
    flex-shrink: 0;
    gap: 8px;
}

.stp-toolbar:empty {
    display: none;
}

.stp-toolbar-left {
    display: flex;
    align-items: center;
    gap: 8px;
    font-size: 12px;
    color: var(--quote-table-header-text, var(--color-text-secondary));
}

.stp-toolbar-right {
    display: flex;
    align-items: center;
    gap: 4px;
}

.stp-toolbar-count {
    font-weight: 600;
    color: var(--quote-table-text, var(--color-text-primary));
}

.stp-toolbar-count strong {
    color: var(--color-accent);
}

/* 排序徽章 */
.stp-toolbar-sort-badge {
    display: inline-flex;
    align-items: center;
    gap: 3px;
    padding: 2px 8px;
    border-radius: 10px;
    background: var(--color-secondary-subtle);
    color: var(--color-secondary);
    font-size: 11px;
    font-weight: 500;
    white-space: nowrap;
}

.stp-toolbar-sort-badge svg {
    width: 10px;
    height: 10px;
}

/* 工具列按鈕 */
.stp-toolbar-btn {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    width: 28px;
    height: 28px;
    border: none;
    background: transparent;
    color: var(--quote-table-header-text, var(--color-text-tertiary));
    border-radius: 6px;
    cursor: pointer;
    transition: all 0.15s ease;
    position: relative;
}

.stp-toolbar-btn:hover {
    background: var(--quote-table-header-bg, var(--color-bg-hover));
    color: var(--color-accent);
}

.stp-toolbar-btn:active {
    transform: scale(0.95);
}

.stp-toolbar-btn svg {
    width: 15px;
    height: 15px;
}

/* ============================================================================
   欄位管理 Popover
   ============================================================================ */

.stp-column-popover {
    position: absolute;
    top: 100%;
    right: 0;
    margin-top: 4px;
    width: 200px;
    background: var(--quote-panel-surface, var(--color-bg-tertiary));
    border: 1px solid var(--quote-table-border, var(--color-border-default));
    border-radius: 8px;
    box-shadow: var(--shadow-md);
    z-index: 100;
    padding: 8px 0;
    font-size: 12px;
}

.stp-column-popover-title {
    padding: 4px 12px 8px;
    font-weight: 600;
    color: var(--quote-table-header-text, var(--color-text-secondary));
    border-bottom: 1px solid var(--quote-table-border, var(--color-border-default));
    margin-bottom: 4px;
}

.stp-column-popover-item {
    display: flex;
    align-items: center;
    gap: 8px;
    padding: 5px 12px;
    cursor: pointer;
    transition: background 0.1s;
}

.stp-column-popover-item:hover {
    background: var(--color-accent-muted);
}

.stp-column-popover-item input[type="checkbox"] {
    accent-color: var(--color-accent);
    width: 14px;
    height: 14px;
    cursor: pointer;
}

.stp-column-popover-item label {
    cursor: pointer;
    color: var(--quote-table-text, var(--color-text-primary));
    user-select: none;
}

/* ============================================================================
   股票表格 - 主題感知樣式（使用 CSS 變數）
   ============================================================================ */

#stockResultTable {
    background-color: var(--quote-panel-surface, var(--color-bg-tertiary));
    color: var(--quote-table-text, var(--color-text-primary));
    border-collapse: separate;
    border-spacing: 0;
    table-layout: auto;
    min-width: 100%;
    font-size: 12px;
    line-height: 1.4;
}

/* 表頭層級修正 — 確保 thead 在 tbody sticky cells 之上 */
#stockResultTable thead {
    z-index: 4;
}

/* 表頭 - 使用主題變數 */
#stockResultTable thead th {
    position: sticky;
    top: 0;
    background: var(--quote-table-header-bg, var(--color-bg-hover));
    color: var(--quote-table-header-text, var(--color-text-secondary));
    font-weight: 600;
    padding: 5px 10px;
    border-bottom: 1px solid var(--quote-table-border, var(--color-border-default));
    font-size: 12px;
    z-index: 6;
    text-align: center;
    white-space: nowrap;
}

/* Sticky 凍結欄位 — 表頭 */
#stockResultTable thead th.code-col {
    position: sticky;
    left: 0;
    z-index: 8;
    box-shadow: 2px 0 4px rgba(0, 0, 0, 0.08);
}

#stockResultTable thead th.name-col {
    position: sticky;
    left: 58px;
    z-index: 8;
    box-shadow: 2px 0 4px rgba(0, 0, 0, 0.08);
}

/* 表格行 */
#stockResultTable tbody tr {
    cursor: pointer;
    border-bottom: 1px solid var(--quote-table-border, var(--color-border-default));
    background-color: var(--quote-panel-surface, var(--color-bg-tertiary));
    transition: background-color 0.15s ease;
    scroll-snap-align: start;
}

/* 偶數行 - 輕微區隔 */
#stockResultTable tbody tr:nth-child(even) {
    background-color: var(--quote-row-alt-bg, rgba(0, 0, 0, 0.02));
}

/* Hover 效果 */
#stockResultTable tbody tr:hover {
    background-color: var(--color-accent-muted);
}

/* Selected 選中行 */
#stockResultTable tbody tr.selected {
    background-color: var(--color-accent-subtle) !important;
    box-shadow: inset 3px 0 0 var(--color-accent);
}

/* Sticky 凍結欄位 — 表體 */
#stockResultTable tbody td.code-col {
    position: sticky;
    left: 0;
    z-index: 2;
    box-shadow: 2px 0 4px rgba(0, 0, 0, 0.08);
    background-color: var(--quote-panel-surface, var(--color-bg-tertiary));
}

#stockResultTable tbody td.name-col {
    position: sticky;
    left: 58px;
    z-index: 2;
    box-shadow: 2px 0 4px rgba(0, 0, 0, 0.08);
    background-color: var(--quote-panel-surface, var(--color-bg-tertiary));
}

/* 凍結欄位 — 偶數行背景覆蓋 */
#stockResultTable tbody tr:nth-child(even) td.code-col,
#stockResultTable tbody tr:nth-child(even) td.name-col {
    background-color: var(--quote-row-alt-bg, rgba(0, 0, 0, 0.02));
}

/* 凍結欄位 — Hover 背景覆蓋 */
#stockResultTable tbody tr:hover td.code-col,
#stockResultTable tbody tr:hover td.name-col {
    background-color: var(--color-accent-muted);
}

/* 凍結欄位 — Selected 背景覆蓋 */
#stockResultTable tbody tr.selected td.code-col,
#stockResultTable tbody tr.selected td.name-col {
    background-color: var(--color-accent-subtle) !important;
}

/* ============================================================================
   漲跌顏色 - Cell-level 漲跌色（延伸至開高低）
   ============================================================================ */

/* Cell-level 上漲 */
#stockResultTable .price-col.price-up,
#stockResultTable .change-col.price-up,
#stockResultTable .change-percent-col.price-up,
#stockResultTable .open-col.price-up,
#stockResultTable .high-col.price-up,
#stockResultTable .low-col.price-up {
    color: var(--quote-price-up, var(--color-market-up));
}

/* Cell-level 下跌 */
#stockResultTable .price-col.price-down,
#stockResultTable .change-col.price-down,
#stockResultTable .change-percent-col.price-down,
#stockResultTable .open-col.price-down,
#stockResultTable .high-col.price-down,
#stockResultTable .low-col.price-down {
    color: var(--quote-price-down, var(--color-market-down));
}

/* Row-level 向後相容（降低優先級） */
#stockResultTable tbody tr.up .price-col,
#stockResultTable tbody tr.up .change-col,
#stockResultTable tbody tr.up .change-percent-col {
    color: var(--quote-price-up, var(--color-market-up));
}

#stockResultTable tbody tr.down .price-col,
#stockResultTable tbody tr.down .change-col,
#stockResultTable tbody tr.down .change-percent-col {
    color: var(--quote-price-down, var(--color-market-down));
}

#stockResultTable tbody tr.flat .price-col,
#stockResultTable tbody tr.flat .change-col,
#stockResultTable tbody tr.flat .change-percent-col {
    color: var(--quote-table-text, var(--color-text-tertiary));
}

/* ============================================================================
   等寬數字字型 — 統一宣告
   ============================================================================ */

#stockResultTable .price-col,
#stockResultTable .change-col,
#stockResultTable .change-percent-col,
#stockResultTable .open-col,
#stockResultTable .high-col,
#stockResultTable .low-col,
#stockResultTable .volume-col {
    font-family: 'Monaco', 'Menlo', 'Ubuntu Mono', monospace;
    font-variant-numeric: tabular-nums;
    font-weight: 600;
}

#stockResultTable .dynamic-col {
    font-family: 'Monaco', 'Menlo', 'Ubuntu Mono', monospace;
}

/* ============================================================================
   欄位寬度與對齊 — 專業排版
   ============================================================================ */

#stockResultTable .code-col {
    min-width: 58px;
    text-align: left;
    font-weight: 600;
    color: var(--color-accent);
    white-space: nowrap;
    padding-left: 20px;
}

#stockResultTable .name-col {
    min-width: 72px;
    max-width: 120px;
    text-align: left;
    font-weight: 600;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

#stockResultTable .price-col {
    min-width: 60px;
    text-align: right;
}

#stockResultTable .change-col {
    min-width: 54px;
    text-align: right;
}

#stockResultTable .change-percent-col {
    min-width: 60px;
    text-align: right;
}

#stockResultTable .open-col {
    min-width: 56px;
    text-align: right;
}

#stockResultTable .high-col {
    min-width: 56px;
    text-align: right;
}

#stockResultTable .low-col {
    min-width: 56px;
    text-align: right;
}

#stockResultTable .volume-col {
    min-width: 66px;
    text-align: right;
}

#stockResultTable .source-col {
    min-width: 70px;
    text-align: center;
}

#stockResultTable .dynamic-col {
    min-width: 78px;
    text-align: right;
    color: var(--color-accent);
    font-weight: 500;
}

/* 隱藏欄位 */
#stockResultTable th.stp-col-hidden,
#stockResultTable td.stp-col-hidden {
    display: none;
}

/* 表格儲存格基本樣式 */
#stockResultTable tbody td {
    padding: 5px 8px;
    border-bottom: 1px solid var(--quote-table-border, var(--color-border-default));
    color: var(--quote-table-text, var(--color-text-primary));
    vertical-align: middle;
    font-variant-numeric: tabular-nums;
}

/* ============================================================================
   排序表頭樣式 — 增強視覺
   ============================================================================ */

#stockResultTable thead th.stp-sortable {
    cursor: pointer;
    user-select: none;
    transition: background-color 0.15s ease, filter 0.15s ease;
}

#stockResultTable thead th.stp-sortable:hover {
    filter: brightness(1.05);
}

#stockResultTable thead th.stp-sortable:active {
    transform: scale(0.98);
}

#stockResultTable thead th.stp-sortable.active {
    color: var(--color-accent);
    background: #F0E7DB;
    border-bottom: 2px solid var(--color-accent);
}

/* 排序中的整欄高亮 */
#stockResultTable tbody td.stp-sort-highlight {
    background-color: rgba(249, 115, 22, 0.02);
}

/* 表頭內容容器 */
#stockResultTable thead th .stp-header-content {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    gap: 4px;
    white-space: nowrap;
}

/* 排序圖示 */
#stockResultTable .stp-sort-icon {
    opacity: 0.4;
    margin-left: 2px;
    display: inline-flex;
    align-items: center;
    transition: opacity 0.15s ease;
}

#stockResultTable thead th.stp-sortable:hover .stp-sort-icon {
    opacity: 0.7;
}

#stockResultTable .stp-sort-icon.active {
    opacity: 1;
    color: var(--color-accent);
}

/* ============================================================================
   智慧選股動態欄位樣式
   ============================================================================ */

/* 動態欄位表頭 */
#stockResultTable thead th.dynamic-col {
    background: linear-gradient(180deg, #F0E2D3 0%, #F0E7DB 100%);
    color: var(--color-accent);
    border-left: 2px solid rgba(249, 115, 22, 0.3);
}

#stockResultTable thead th.dynamic-col.stp-sortable.active {
    color: var(--color-accent-hover);
}

/* 動態欄位儲存格 */
#stockResultTable tbody td.dynamic-col {
    border-left: 1px solid var(--color-accent-subtle);
}

/* ============================================================================
   即時報價閃爍動畫
   ============================================================================ */

/* 價格上漲動畫 */
@keyframes priceFlashUp {
    0% { background-color: transparent; }
    50% { background-color: rgba(239, 68, 68, 0.3); }
    100% { background-color: transparent; }
}

/* 價格下跌動畫 */
@keyframes priceFlashDown {
    0% { background-color: transparent; }
    50% { background-color: rgba(16, 185, 129, 0.3); }
    100% { background-color: transparent; }
}

/* 增強版上漲閃爍 */
@keyframes priceFlashUpEnhanced {
    0% { background-color: transparent; }
    15% { background-color: rgba(239, 68, 68, 0.4); }
    50% { background-color: rgba(239, 68, 68, 0.25); }
    100% { background-color: transparent; }
}

/* 增強版下跌閃爍 */
@keyframes priceFlashDownEnhanced {
    0% { background-color: transparent; }
    15% { background-color: rgba(16, 185, 129, 0.4); }
    50% { background-color: rgba(16, 185, 129, 0.25); }
    100% { background-color: transparent; }
}

/* 單元格閃爍 class */
#stockResultTable tbody tr td.price-flash-up {
    animation: priceFlashUp 0.5s ease;
}

#stockResultTable tbody tr td.price-flash-down {
    animation: priceFlashDown 0.5s ease;
}

#stockResultTable tbody tr td.price-flash-up-enhanced {
    animation: priceFlashUpEnhanced 0.6s ease-out;
}

#stockResultTable tbody tr td.price-flash-down-enhanced {
    animation: priceFlashDownEnhanced 0.6s ease-out;
}

/* 價格脈衝效果 */
@keyframes pricePulse {
    0% { transform: scale(1); }
    50% { transform: scale(1.05); }
    100% { transform: scale(1); }
}

#stockResultTable tbody tr td.price-pulse {
    animation: pricePulse 0.3s ease-in-out;
}

/* 即時狀態指示器動畫 */
@keyframes liveIndicator {
    0%, 100% { opacity: 1; }
    50% { opacity: 0.5; }
}

#stockResultTable tbody tr td.source-col .live-indicator {
    animation: liveIndicator 2s ease-in-out infinite;
}

/* 整行閃爍動畫 */
@keyframes rowFlashUp {
    0% { background-color: transparent; }
    20% { background-color: rgba(239, 68, 68, 0.15); }
    100% { background-color: transparent; }
}

@keyframes rowFlashDown {
    0% { background-color: transparent; }
    20% { background-color: rgba(16, 185, 129, 0.15); }
    100% { background-color: transparent; }
}

#stockResultTable tbody tr.row-flash-up {
    animation: rowFlashUp 0.8s ease-out;
}

#stockResultTable tbody tr.row-flash-down {
    animation: rowFlashDown 0.8s ease-out;
}

/* ============================================================================
   排序動畫效果
   ============================================================================ */

@keyframes sortSlideIn {
    from {
        opacity: 0.7;
        transform: translateY(-2px);
    }
    to {
        opacity: 1;
        transform: translateY(0);
    }
}

#stockResultTable tbody.stp-sort-animation tr {
    animation: sortSlideIn 0.25s ease-out;
}

/* ============================================================================
   選中狀態指示器
   ============================================================================ */

#stockResultTable tbody tr[data-stock-code] td.code-col::before {
    content: "";
    position: absolute;
    left: 8px;
    top: 50%;
    transform: translateY(-50%);
    width: 6px;
    height: 6px;
    border-radius: 50%;
    background-color: transparent;
    transition: background-color 0.2s ease;
}

#stockResultTable tbody tr.selected td.code-col::before {
    background-color: var(--color-accent);
}

/* ============================================================================
   結果摘要列
   ============================================================================ */

.stp-result-summary-bar {
    display: flex;
    align-items: center;
    gap: 12px;
    padding: 4px 10px;
    font-size: 11px;
    color: var(--quote-table-header-text, var(--color-text-tertiary));
    background: var(--quote-panel-surface, var(--color-bg-tertiary));
    border-bottom: 1px solid var(--quote-table-border, var(--color-border-default));
    white-space: nowrap;
    overflow-x: auto;
}

.stp-summary-count strong {
    color: var(--color-accent);
}

.stp-summary-stat .positive { color: var(--quote-price-up, var(--color-market-up)); }
.stp-summary-stat .negative { color: var(--quote-price-down, var(--color-market-down)); }

/* ============================================================================
   無障礙增強
   ============================================================================ */

/* 高對比模式支援 */
@media (prefers-contrast: high) {
    #stockResultTable tbody tr.selected {
        box-shadow: inset 5px 0 0 var(--color-accent);
    }

    #stockResultTable thead th.stp-sortable.active {
        border-bottom: 2px solid var(--color-accent);
    }
}

/* 減少動畫偏好 */
@media (prefers-reduced-motion: reduce) {
    #stockResultTable tbody tr td.price-flash-up,
    #stockResultTable tbody tr td.price-flash-down,
    #stockResultTable tbody tr td.price-flash-up-enhanced,
    #stockResultTable tbody tr td.price-flash-down-enhanced,
    #stockResultTable tbody tr.row-flash-up,
    #stockResultTable tbody tr.row-flash-down,
    #stockResultTable tbody.stp-sort-animation tr {
        animation: none;
    }

    #stockResultTable thead th.stp-sortable,
    #stockResultTable .stp-sort-icon {
        transition: none;
    }
}
