/* ================================================================
   terminal.css - 分析終端頁面專用樣式
   ================================================================
   適用於：index.html（主分析頁面）
   
   區塊索引：
     1. 卡片元件         — .section-card, .preview-box
     2. 更新按鈕與快取標記 — .update-btn, .cache-badge
     3. 彈出報告視窗      — .draggable-window 及其子元素
     4. AI 報告內容樣式   — .window-body 內的 Markdown 轉 HTML
   
   修改指引：
     - 卡片預覽高度 → 搜尋 .preview-box { height: 80px }
     - 視窗最大寬度 → 搜尋 .draggable-window { max-width: 1200px }
     - 報告表格配色 → 搜尋 .window-body thead
   ================================================================ */


/* ==========================================================
   1. 卡片元件
   ========================================================== */

.section-card {
    background: white;
    border: 1px solid #eee;
    border-top: 3px solid var(--aurum-gold);
    transition: all 0.3s ease;
    display: flex;
    flex-direction: column;
}

/* 固定預覽文字高度（確保所有卡片等高） */
.preview-box {
    height: 80px;
    overflow: hidden;
}
.preview-box p {
    font-size: 13px !important;
    line-height: 1.7 !important;
}


/* ==========================================================
   2. 更新按鈕與快取標記
   ========================================================== */

/* 「重新分析」按鈕（永久隱藏） */
.update-btn {
    display: none !important;
}
.update-btn:hover:not(:disabled) {
    background: #fff;
    border-color: #28a745;
    color: #28a745;
}
.update-btn:disabled {
    background: #f0f0f0;
    cursor: not-allowed;
    opacity: 0.5;
}

/* 快取標記（CACHE / FRESH） */
.cache-badge {
    display: inline-block;
    padding: 2px 8px;
    font-size: 10px;
    font-weight: bold;
    letter-spacing: 0.12em;
    border-radius: 2px;
    font-family: 'JetBrains Mono', monospace;
    text-transform: uppercase;
    margin-left: 8px;
}
.cache-badge.cached { background: #e0f2fe; color: #0284c7; border: 1px solid #bae6fd; }
.cache-badge.fresh  { background: #dcfce7; color: #16a34a; border: 1px solid #bbf7d0; }


/* ==========================================================
   3. 彈出報告視窗
   ========================================================== */

.draggable-window {
    position: fixed;
    width: 85vw;
    max-width: 1200px;
    height: 85vh;
    background: white;
    z-index: 1000;
    display: flex;
    flex-direction: column;
    border: 1px solid #e5e5e5;
    box-shadow: 0 25px 50px -12px rgba(0, 0, 0, 0.25);
    border-radius: 6px;
    overflow: hidden;
}

/* 最大化（真正全螢幕，覆蓋 max-width） */
.draggable-window.maximized {
    width: 100vw !important;
    height: 100vh !important;
    max-width: 100vw !important;
    top: 0 !important;
    left: 0 !important;
    right: 0 !important;
    bottom: 0 !important;
    border-radius: 0 !important;
    box-shadow: none !important;
}

/* 最小化（右下角小條） */
.draggable-window.minimized {
    height: auto !important;
    width: 360px !important;
    bottom: 20px !important;
    top: auto !important;
    right: 20px !important;
    left: auto !important;
    box-shadow: 0 8px 30px rgba(0, 0, 0, 0.15);
}
.draggable-window.minimized .window-body { display: none; }

/* --- 視窗標題列 --- */
.window-header {
    background: linear-gradient(135deg, #1a1a1a 0%, #2a2a2a 100%);
    padding: 0;
    cursor: move;
    display: flex;
    justify-content: space-between;
    align-items: center;
    border-bottom: 2px solid var(--aurum-gold);
    flex-shrink: 0;
    user-select: none;
}
.window-header-left {
    display: flex; align-items: center; gap: 14px;
    padding: 14px 20px; flex: 1; min-width: 0;
}
.window-header-icon {
    width: 10px; height: 10px;
    background: linear-gradient(135deg, #c5a66d, #b18e4d);
    transform: rotate(45deg); flex-shrink: 0;
    box-shadow: 0 0 8px rgba(177, 142, 77, 0.4);
}
.window-header-title {
    color: #d4c5a0; font-size: 12px; font-weight: 700;
    letter-spacing: 0.18em; text-transform: uppercase;
    font-family: 'JetBrains Mono', monospace;
    white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
}
.window-header-tag {
    color: #b18e4d; font-size: 11px;
    font-family: 'JetBrains Mono', monospace;
    letter-spacing: 0.12em; opacity: 0.6; white-space: nowrap;
}

/* --- 視窗控制按鈕 --- */
.window-controls { display: flex; align-items: stretch; height: 100%; flex-shrink: 0; }
.window-ctrl-btn {
    display: flex; align-items: center; justify-content: center;
    width: 46px; height: 100%; padding: 14px 0;
    border: none; background: transparent; cursor: pointer;
    transition: background 0.2s ease; position: relative;
}
.window-ctrl-btn svg {
    width: 14px; height: 14px; stroke: #888;
    stroke-width: 1.5; fill: none; transition: stroke 0.2s ease;
}
.window-ctrl-btn:hover svg           { stroke: #d4c5a0; }
.window-ctrl-btn.btn-minimize:hover  { background: rgba(177, 142, 77, 0.15); }
.window-ctrl-btn.btn-maximize:hover  { background: rgba(177, 142, 77, 0.15); }
.window-ctrl-btn.btn-close:hover     { background: #c53030; }
.window-ctrl-btn.btn-close:hover svg { stroke: #fff; }
.window-ctrl-divider {
    width: 1px; align-self: center; height: 16px;
    background: rgba(255, 255, 255, 0.08);
}

/* --- 視窗內容區 --- */
.window-body { padding: 40px; overflow-y: auto; flex-grow: 1; line-height: 1.8; }

/* --- 載入中脈衝動畫 --- */
.loading-pulse {
    width: 8px; height: 8px; border-radius: 50%;
    background-color: var(--aurum-gold);
    animation: pulse 1.5s infinite;
}
@keyframes pulse {
    0%   { opacity: 0.3; transform: scale(1) rotate(45deg); }
    50%  { opacity: 1;   transform: scale(1.2) rotate(45deg); }
    100% { opacity: 0.3; transform: scale(1) rotate(45deg); }
}


/* ==========================================================
   4. AI 報告內容樣式（.window-body 內的 Markdown → HTML）
   ========================================================== */

/* 標題 */
.window-body h1, .window-body h2, .window-body h3, .window-body h4 {
    color: #1a1a1a; margin-top: 1.8em; margin-bottom: 0.6em; font-weight: 900;
}
.window-body h1 { font-size: 1.4em; border-bottom: 2px solid var(--aurum-gold); padding-bottom: 8px; }
.window-body h2 { font-size: 1.2em; border-bottom: 1px solid #eee; padding-bottom: 6px; }
.window-body h3 { font-size: 1.05em; color: var(--aurum-gold); }
.window-body h4 { font-size: 0.95em; color: #555; }

/* 段落 */
.window-body p      { margin-bottom: 0.8em; line-height: 1.9; }
.window-body strong  { color: #1a1a1a; font-weight: 700; }
.window-body em      { color: #666; }

/* 列表 */
.window-body ul, .window-body ol { margin: 0.6em 0 1em 1.5em; line-height: 1.9; }
.window-body li    { margin-bottom: 0.3em; }
.window-body ul li { list-style-type: disc; }
.window-body ol li { list-style-type: decimal; }

/* 表格 */
/* 表格：用 wrapper 包住做橫向捲動，避免文字被逼成一字一行 */
.window-body table {
    /* 不設 width:100%，讓表格自然撐開 */
    min-width: 480px;            /* 確保表格有最小寬度 */
    border-collapse: collapse;
    margin: 1.2em 0 1.8em 0;
    font-size: 14px; background: #fff; border: 1px solid #e5e5e5;
    border-radius: 4px; overflow: hidden; box-shadow: 0 1px 3px rgba(0,0,0,0.04);
}

/* wrapper：自動套用在所有 table 外層（由 JS 動態加） */
.table-wrapper {
    width: 100%;
    overflow-x: auto;
    -webkit-overflow-scrolling: touch;  /* iOS 慣性捲動 */
    margin: 1.2em 0 1.8em 0;
    border-radius: 4px;
    /* 橫向可捲動時顯示淡出提示 */
    background:
        linear-gradient(to right, white 30%, rgba(255,255,255,0)),
        linear-gradient(to right, rgba(255,255,255,0), white 70%) 100% 0,
        linear-gradient(to right, rgba(177,142,77,0.15), rgba(177,142,77,0)),
        linear-gradient(to left,  rgba(177,142,77,0.15), rgba(177,142,77,0)) 100% 0;
    background-repeat: no-repeat;
    background-size: 40px 100%, 40px 100%, 12px 100%, 12px 100%;
    background-attachment: local, local, scroll, scroll;
}

.window-body thead { background: linear-gradient(135deg, #1a1a1a 0%, #2a2a2a 100%); }
.window-body thead th {
    color: #d4c5a0; font-size: 12px; font-weight: 700;
    letter-spacing: 0.08em; text-transform: uppercase;
    padding: 12px 16px; text-align: left;
    border-bottom: 2px solid var(--aurum-gold);
    white-space: nowrap;        /* 表頭不換行 */
    min-width: 80px;            /* 每欄最小寬度 */
}
.window-body thead th:first-child { min-width: 120px; }  /* 第一欄（項目名）寬一點 */
.window-body thead th:last-child  { min-width: 140px; }  /* 最後欄（備註）寬一點 */

.window-body tbody td {
    padding: 10px 16px; border-bottom: 1px solid #f0f0f0;
    color: #444; vertical-align: top;
    white-space: normal;        /* 內容欄允許換行，但有 min-width 保底 */
    min-width: 80px;
    word-break: break-word;
}
.window-body tbody td:first-child { min-width: 120px; font-weight: 600; }
.window-body tbody td:last-child  { min-width: 140px; }  /* 備註欄不會太窄 */
.window-body tbody tr:hover { background-color: #fdfbf7; }

/* 程式碼 */
.window-body code {
    background: #f5f5f5; padding: 2px 6px; border-radius: 3px;
    font-family: 'JetBrains Mono', monospace; font-size: 12px; color: var(--aurum-gold);
}
.window-body pre {
    background: #1a1a1a; color: #d4c5a0; padding: 16px 20px;
    border-radius: 4px; overflow-x: auto; margin: 1em 0; font-size: 12px;
}
.window-body pre code { background: none; color: inherit; padding: 0; }

/* 分隔線、引用 */
.window-body hr {
    border: none; border-top: 1px solid #eee; margin: 2em 0;
}
.window-body blockquote {
    border-left: 3px solid var(--aurum-gold); padding: 8px 16px;
    margin: 1em 0; background: #fdfbf7; color: #555; font-style: italic;
}

/* Footer */
footer p { line-height: 1.6; }


/* ==========================================================
   5. 響應式 — 手機全屏彈出視窗
   ========================================================== */
@media (max-width: 640px) {
    .draggable-window {
        width: 100vw !important;
        height: 100dvh !important;
        max-width: 100vw !important;
        top: 0 !important;
        left: 0 !important;
        right: 0 !important;
        bottom: 0 !important;
        border-radius: 0 !important;
        box-shadow: none !important;
    }
    .window-body { padding: 20px 16px; }
    .window-header-left { padding: 12px 14px; gap: 10px; }
    .window-header-tag { display: none; }
}


/* ==========================================================
   6. Skeleton Loading — Optimistic UI 用
   ========================================================== */
.skeleton-line {
    height: 12px;
    border-radius: 3px;
    background: linear-gradient(90deg, #f0f0f0 25%, #e0e0e0 50%, #f0f0f0 75%);
    background-size: 200% 100%;
    animation: skeleton-shimmer 1.4s infinite;
}
@keyframes skeleton-shimmer {
    0%   { background-position: 200% 0; }
    100% { background-position: -200% 0; }
}

/* Header 名稱淡入淡出過渡 */
#header-chinese-name,
#header-en-name {
    transition: opacity 0.15s ease;
}


/* ==========================================================
   7. Mobile-First 優化
   ========================================================== */

/* ── 手機安全區域（iPhone 瀏海 / Home Bar）── */
body {
    padding-bottom: env(safe-area-inset-bottom);
    padding-left: env(safe-area-inset-left);
    padding-right: env(safe-area-inset-right);
}

/* ── 手機卡片觸控目標優化 ── */
@media (max-width: 640px) {

    /* 卡片內距：手機用較小 padding */
    .section-card {
        padding: 16px !important;
    }

    /* 「開啟報告」按鈕：手機加高 touch target（最少 44px）*/
    .section-card button {
        min-height: 44px;
        font-size: 13px !important;
    }

    /* 預覽文字區：手機稍微加高一點，避免文字被截斷 */
    .preview-box {
        height: 90px;
    }

    /* 彈出視窗內容區：手機 padding 縮小 */
    .window-body {
        padding: 20px 16px 40px 16px;
    }

    /* 彈出視窗表格：手機橫向可捲動 */
    .window-body table {
        display: block;
        overflow-x: auto;
        -webkit-overflow-scrolling: touch;
        white-space: nowrap;
    }

    /* 彈出視窗標題列：手機縮小 */
    .window-header-left {
        padding: 10px 12px;
        gap: 8px;
    }
    .window-header-title {
        font-size: 11px;
    }
    .window-ctrl-btn {
        width: 40px;
    }

    /* 快取標記：手機稍微縮小 */
    .cache-badge {
        font-size: 9px;
        padding: 1px 6px;
    }

    /* Skeleton */
    .skeleton-line {
        height: 12px;
    }

    /* ── 彈出視窗報告內文字體縮小 ── */
    .window-body {
        font-size: 13px;
        line-height: 1.75;
    }
    .window-body p {
        font-size: 13px;
        line-height: 1.75;
        margin-bottom: 0.6em;
    }
    .window-body h1 { font-size: 1.15em; }
    .window-body h2 { font-size: 1.05em; }
    .window-body h3 { font-size: 0.95em; }
    .window-body h4 { font-size: 0.88em; }

    .window-body ul,
    .window-body ol {
        font-size: 13px;
        line-height: 1.75;
        margin-left: 1.2em;
    }
    .window-body li {
        margin-bottom: 0.2em;
    }

    /* 表格文字縮小 */
    .window-body table {
        font-size: 12px;
    }
    .window-body thead th {
        font-size: 11px;
        padding: 9px 12px;
    }
    .window-body tbody td {
        font-size: 12px;
        padding: 8px 12px;
    }

    /* blockquote 縮小 */
    .window-body blockquote {
        font-size: 12px;
        padding: 6px 12px;
    }

    /* 卡片預覽文字 */
    .preview-box p {
        font-size: 11px !important;
        line-height: 1.6 !important;
    }

    /* 分析區塊標題 */
    .section-card h5 {
        font-size: 15px;
    }
}

/* ── 平板（sm）微調 ── */
@media (min-width: 641px) and (max-width: 1023px) {
    .section-card {
        padding: 20px;
    }
    .preview-box {
        height: 85px;
    }
}

/* ── 下拉選單：手機上限制高度避免超出螢幕 ── */
@media (max-width: 640px) {
    #stock-dropdown {
        max-height: 50vh !important;
        /* 讓下拉選單在手機上更好 touch */
    }
    #stock-dropdown .autocomplete-item {
        padding: 14px 16px !important;  /* 加大 touch target */
        min-height: 48px;
    }
}

/* ── 防止 iOS 自動縮放輸入框（字體小於 16px 會觸發）── */
input[type="text"] {
    font-size: 16px !important;
}
/* 但視覺上保持小字，用 transform 縮放 */
@media (max-width: 640px) {
    #stock-search-input {
        font-size: 16px !important;   /* 防 iOS zoom */
        letter-spacing: 0.1em;
    }
}
