﻿:root {
    --primary: #0d9488;
    --primary-dark: #0f766e;
    --bg-color: #e5e7eb;
    --chat-bg: #f3f4f6;
    --user-msg-bg: #0d9488;
    --bot-msg-bg: #ffffff;
    --text-primary: #111827;
    --text-secondary: #4b5563;
    --error-color: #dc2626;
    --error-bg: #fef2f2;
    --error-border: #fca5a5;
    --focus-ring: #2dd4bf;
}

* { box-sizing: border-box; }

body {
    font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
    background-color: var(--bg-color);
    margin: 0;
    display: flex;
    justify-content: center;
    height: 100dvh;
    overflow: hidden;
    -webkit-font-smoothing: antialiased;
    -moz-osx-font-smoothing: grayscale;
}

:focus-visible {
    outline: 3px solid var(--focus-ring);
    outline-offset: 2px;
}

@media (prefers-reduced-motion: reduce) {
    * {
        animation-duration: 0.01ms !important;
        animation-iteration-count: 1 !important;
        transition-duration: 0.01ms !important;
        scroll-behavior: auto !important;
    }
}

#app-container {
    width: 100%;
    max-width: 480px;
    background-color: #fff;
    display: flex;
    flex-direction: column;
    box-shadow: 0 10px 25px rgba(0,0,0,0.1);
    height: 100%;
    position: relative;
    transition: max-width 0.3s ease;
}

#app-container.expanded {
    max-width: 100%;
}

/* 展開模式下放大內容 */
#app-container.expanded .message {
    font-size: 1.05rem;
    padding: 14px 18px;
}

#app-container.expanded .option-btn {
    padding: 12px 16px;
    font-size: 1rem;
}

#app-container.expanded .option-title {
    font-size: 1rem;
}

#app-container.expanded .option-cat {
    font-size: 0.82rem;
}

#app-container.expanded .option-funname {
    font-size: 0.75rem;
}

#app-container.expanded .drilldown-btn {
    padding: 8px 18px;
    font-size: 0.95rem;
}

#app-container.expanded .result-card {
    font-size: 1.05rem;
}

#app-container.expanded .card-header h2 {
    font-size: 1rem;
}

#app-container.expanded .batch-table {
    font-size: 1rem;
}

#app-container.expanded .batch-table th,
#app-container.expanded .batch-table td {
    padding: 8px 14px;
}

#app-container.expanded .single-val {
    font-size: 2.5rem;
}

#app-container.expanded input[type="text"] {
    font-size: 18px;
}

header {
    background-color: var(--primary-dark);
    color: white;
    padding: 16px;
    display: flex;
    justify-content: space-between;
    align-items: center;
    z-index: 10;
    box-shadow: 0 2px 4px rgba(0,0,0,0.1);
}

header h1 {
    margin: 0;
    font-size: 1.1rem;
    font-weight: 600;
}

.beta-badge {
    font-size: 0.6rem;
    font-weight: 400;
    padding: 2px 6px;
    border-radius: 8px;
    background: rgba(255,255,255,0.2);
    border: 1px solid rgba(255,255,255,0.45);
    vertical-align: middle;
    margin-left: 8px;
    letter-spacing: 0.5px;
}

header p {
    margin: 2px 0 0;
    font-size: 0.75rem;
    opacity: 0.9;
}

.header-icon {
    height: 22px;
    width: auto;
    vertical-align: top;
    margin-right: 0.35em;
}

.header-link {
    color: inherit;
    text-decoration: none;
}
.header-link:hover {
    text-decoration: underline;
    text-underline-offset: 2px;
}

.header-content {
    display: flex;
    flex-direction: column;
}

.header-buttons {
    display: flex;
    gap: 8px;
    align-items: center;
}
#chat-area {
    flex: 1;
    overflow-y: auto;
    padding: 16px;
    background-color: var(--chat-bg);
    display: flex;
    flex-direction: column;
    gap: 16px;
    scroll-behavior: smooth;
}

.message {
    max-width: 85%;
    padding: 12px 16px;
    border-radius: 16px;
    font-size: 0.95rem;
    line-height: 1.5;
    position: relative;
    word-wrap: break-word;
    white-space: pre-wrap; 
    animation: fadeIn 0.3s ease;
}

.message.bot {
    background-color: var(--bot-msg-bg);
    color: var(--text-primary);
    border: 1px solid #e5e7eb;
    border-top-left-radius: 2px;
    align-self: flex-start;
}

/* 說明訊息工具按鈕圖示 */
.help-icon-svg {
    vertical-align: middle;
}
.help-icon-q {
    vertical-align: -1px;
}
.help-li-flex {
    display: flex !important;
    white-space: normal;
    padding-left: 0 !important;
    text-indent: 0 !important;
}
.help-li-label {
    white-space: nowrap;
    flex-shrink: 0;
}
.help-li-body {
    min-width: 0;
    white-space: normal;
    word-break: break-word;
}

/* 說明訊息內的列表縮排 */
.message.bot ul {
    margin: 6px 0 2px 0;
    padding-left: 0;
    list-style: none;
}
.message.bot li {
    padding-left: 1.2em;
    text-indent: -1.2em;
    line-height: 1.6;
}

.definition-title {
    color: var(--primary-dark);
    font-weight: 600;
    font-size: 1rem;
    display: block;
    margin-bottom: 4px;
}

.message.bot.definition {
    background-color: var(--bot-msg-bg);
    color: var(--text-primary);
    border: 1px solid #e5e7eb;
}

.definition-source {
    text-align: right;
    margin-top: 8px;
    font-size: 0.75rem;
}
.definition-source-label {
    color: #9ca3af;
}
.definition-source-link {
    color: #0d9488;
    text-decoration: none;
}
.definition-source-link:hover {
    text-decoration: underline;
}

.message.user {
    background-color: var(--user-msg-bg);
    color: white;
    border-bottom-right-radius: 2px;
    align-self: flex-end;
}

.message.bot.error {
    color: var(--error-color);
    background-color: var(--error-bg);
    border: 1px solid var(--error-border);
}

.message.bot.loading {
    color: #4b5563;
    font-style: italic;
    min-width: 60px;
}

.loading-dots::after {
    content: ' .';
    animation: dots 1.5s steps(5, end) infinite;
}

@keyframes dots {
    0%, 20% { content: ' .'; }
    40% { content: ' ..'; }
    60% { content: ' ...'; }
    80%, 100% { content: ''; }
}

.bot-link {
    color: #0d9488;
    text-decoration: underline;
    margin: 0 3px;
}

.options-container {
    display: flex;
    flex-direction: column;
    gap: 8px;
    margin-top: -8px;
}

.option-btn {
    background: white;
    border: 1px solid #9ca3af;
    padding: 10px 12px;
    border-radius: 12px;
    text-align: left;
    cursor: pointer;
    display: flex;
    justify-content: space-between;
    align-items: center;
    transition: all 0.2s;
    white-space: normal;

    width: 100%;
    max-width: 100%;
    font-family: inherit;
    font-size: inherit;
    color: inherit;
}

.option-btn:hover, .option-btn:focus {
    background-color: #f0fdfa;
    border-color: #99f6e4;
}

.option-btn.btn-more {
    border-color: #9ca3af; /* 與一般按鈕一致，符合 3:1 對比度 */
    background-color: #f0fdfa; /* 淡藍綠色背景 */
    color: #0D9488; /* 深藍綠色文字 */
    font-weight: bold; /* 粗體 */
    justify-content: center; /* 置中 */
}

.option-btn.btn-more:hover, .option-btn.btn-more:focus {
    background-color: #e6fffa; /* 懸停時稍微變深 */
    border-color: #99f6e4;
}

.option-btn.btn-more .option-title {
    color: #0D9488; /* 與一般候選按鈕標題一致的深藍綠色 */
    text-align: center;
}

.option-info {
    display: flex;
    flex-direction: column;
    pointer-events: none;
    flex: 1;
    min-width: 0;
}

.option-title {
    font-weight: 600;
    color: #134e4a;
    font-size: 0.9rem;
    word-break: break-word;
    overflow-wrap: break-word;
    line-height: 1.3;
    max-width: 16em;
}

.option-cat {
    font-size: 0.75rem;
    color: #134e4a;
    font-weight: normal;
    margin-top: 2px;
}

.option-funname {
    font-size: 0.68rem;
    color: #9ca3af;
    font-weight: normal;
    margin-top: 2px;
}

.drilldown-container {
    display: flex;
    flex-wrap: wrap;
    gap: 8px;
    margin-top: -8px;
}

.drilldown-btn {
    background-color: #fff;
    border: 1px solid #0d9488;
    color: #0d9488;
    padding: 6px 14px;
    border-radius: 20px;
    font-size: 0.85rem;
    font-weight: 500;
    cursor: pointer;
    transition: all 0.2s;
    font-family: inherit;
}

.drilldown-btn:hover, .drilldown-btn:focus-visible {
    background-color: #e6f7f5;
    color: #0d9488;
}

.drilldown-btn.btn-batch {
    border-color: #e11d48;
    color: #e11d48;
    background-color: #fff1f2;
}
.drilldown-btn.btn-batch:hover, .drilldown-btn.btn-batch:focus-visible {
    background-color: #e11d48;
    color: white;
}
.drilldown-btn.active {
    background-color: #0d9488;
    color: white;
    pointer-events: none;
}

.result-card {
    background: white;
    border: 1px solid #5eead4;
    border-radius: 12px;
    overflow: hidden;
    margin-top: 8px;
    animation: slideUp 0.4s ease;
    white-space: normal !important;
    box-shadow: 0 4px 6px -1px rgba(0, 0, 0, 0.05);
}

.card-header {
    background: #f0fdfa;
    padding: 10px 16px;
    border-bottom: 1px solid #ccfbf1;
    display: flex;
    justify-content: space-between;
    /* 改為 flex-start 讓標題與副標題在換行時靠上對齊 */
    align-items: flex-start; 
    /* 允許標題區塊內容換行 */
    flex-wrap: wrap; 
    gap: 6px;
    font-size: 0.9rem;
    font-weight: 600;
    color: #0f766e;
}

.card-header h2 {
    margin: 0;
    font-size: 0.9rem;
    font-weight: 600;
    color: #0f766e;

    /* 彈性佈局，讓文字佔據剩餘空間 */
    flex: 1 1 auto;

    /* 保持圖示與文字同行，但允許內部換行 */
    display: inline-flex;
    flex-wrap: wrap;
    align-items: center;

    /* 增加行高，多行時較易閱讀 */
    line-height: 1.4;

    /* 確保長字串可以換行 */
    white-space: normal;
    word-break: break-word;

    /* 設定最小寬度，避免被擠壓到剩一個字 */
    min-width: 60%;
}

.card-subtitle {
    font-size: 0.85em;
    opacity: 0.9;
    font-weight: normal;
    /* 禁止副標題換行，保持完整性 */
    white-space: nowrap; 
    /* 確保靠右或自動推擠 */
    margin-left: auto;
    /* 配合 flex-start 的微調 */
    margin-top: 2px;
    flex-shrink: 0;
}

.card-body {
    padding: 12px 20px; 
    display: flex;
    flex-direction: column;
    align-items: center;
}

.card-footer {
    background: #f3f4f6;
    padding: 8px 16px;
    font-size: 0.75rem;
    /* 原本 #6b7280 (4.6:1 勉強通過)，改為 #4b5563 (7:1 更安全) */
    color: #4b5563;
    border-top: 1px solid #f3f4f6;
    line-height: 1.4;
    text-align: right;
}

.footer-link-row {
    margin-top: 4px;
    display: flex;
    align-items: flex-start;
    justify-content: flex-end;
}

.footer-source-label {
    color: #9ca3af;
    white-space: nowrap;
    flex-shrink: 0;
}

.card-link {
    color: #0d9488;
    text-decoration: underline;
    text-underline-offset: 2px;
    white-space: pre-wrap;
    text-align: left;
}

.sr-only {
    position: absolute;
    width: 1px;
    height: 1px;
    padding: 0;
    margin: -1px;
    overflow: hidden;
    clip: rect(0, 0, 0, 0);
    white-space: nowrap;
    border-width: 0;
}

.single-val {
    font-size: 2.3rem;
    font-weight: 600; 
    color: var(--primary-dark);
    text-align: center;
    margin: 10px 0;
}

.single-meta {
    display: block;
    text-align: center;
    color: #4b5563;
    font-size: 0.75rem;
    margin-bottom: 4px;
    background: #eceef1;
    padding: 2px 10px;
    border-radius: 99px;
}

.unit-span {
    font-size: 1rem;
    color: #4b5563;
    margin-left: 6px;
    font-weight: 400;
}

/* ── 單數字卡片：較上年同期增減 ── */
.change-block {
    text-align: center;
    margin: 0 0 4px;
    padding: 0 20px;
    font-size: 0.73rem;
    color: #374151;
    display: flex;
    justify-content: center;
    align-items: center;
    gap: 10px;
    flex-wrap: wrap;
}
.change-header {
    color: #6b7280;
    white-space: nowrap;
}
.change-item {
    white-space: nowrap;
    color: var(--primary-dark);
    font-weight: 600;
}
.change-item-label {
    color: #6b7280;
    margin-right: 3px;
    font-weight: 400;
}

/* Batch Table Styles */
.batch-table {
    width: 100%;
    border-collapse: collapse;
    font-size: 0.9rem;
    margin-top: -8px; /* Offset the body padding removal */
    margin-bottom: 4px;
}
.batch-table th, .batch-table td {
    padding: 6px 12px;
    border-bottom: 1px solid #f0f9ff;
    text-align: left;
    color: var(--text-primary);
}
.batch-table th {
    background-color: #f8fafc;
    color: #475569;
    font-weight: 500;
    font-size: 0.85rem;
    white-space: nowrap;
}
.batch-table .text-right {
    text-align: right;
}
.batch-table .text-error {
    color: #ef4444;
    font-size: 0.8em;
}
.batch-table .highlight-val {
    color: var(--primary-dark);
    font-weight: 600;
}
.batch-table tr:last-child td {
    border-bottom: none;
}

.batch-table .row-total td {
    font-weight: 700;
    /* color: var(--primary-dark); */
    background-color: transparent; 
}

/* [修正權重] 增加選擇器權重 .batch-table .expand-row-cell 以覆蓋 .batch-table td 的顏色設定 */
.batch-table .expand-row-cell {
    text-align: center;
    padding: 12px !important;
    font-size: 0.8rem; /* 比細項 (0.9rem) 小 */
    color: var(--primary-dark); /* 與標題同色 #0f766e */
    font-weight: 500;
    cursor: pointer;
    background-color: #f9fafb;
    transition: background-color 0.2s;
    user-select: none;
}

.batch-table .expand-row-cell:hover {
    background-color: #f0fdfa;
    /* 移除舊有的 color 覆寫，保持與標題同色 */
}

.expand-indicator {
    font-size: 1em; /* 相對縮小後的字體大小 */
    margin-right: 4px;
    display: inline-block;
    animation: bounce 1.5s infinite ease-in-out; /* 持續跳動 */
}

@keyframes bounce {
    0%, 100% { transform: translateY(0); }
    50% { transform: translateY(3px); }
}

.fade-in-row {
    animation: fadeInRow 0.4s ease-out;
}

@keyframes fadeInRow {
    from { opacity: 0; background-color: #fffbeb; }
    to { opacity: 1; background-color: transparent; }
}

#input-area {
    padding: 16px;
    background: white;
    border-top: 1px solid #e5e7eb;
    z-index: 20;
}

.input-wrapper {
    display: flex;
    align-items: center;
    background: white;
    border-radius: 999px;
    padding: 6px 8px 6px 16px;
    border: 1px solid #9ca3af;
}

.input-wrapper:hover {
    border-color: #6b7280;
    box-shadow: 0 0 0 3px rgba(13, 148, 136, 0.08);
}

.input-wrapper:focus-within {
    border-color: var(--primary);
    box-shadow: 0 0 0 3px rgba(13, 148, 136, 0.15);
}

input[type="text"] {
    flex: 1;
    border: none;
    background: transparent;
    padding: 8px;
    outline: none;
    font-size: 16px;
    color: var(--text-primary);
}

input[type="text"]::placeholder {
    color: #9ca3af;
    opacity: 1;
}

button.send-btn {
    background-color: var(--primary);
    color: white;
    border: none;
    border-radius: 50%;
    width: 36px;
    height: 36px;
    flex-shrink: 0;
    display: flex;
    align-items: center;
    justify-content: center;
    cursor: pointer;
    transition: background-color 0.2s, box-shadow 0.2s;
}

button.send-btn:hover:not(.recording):not(:disabled) {
    box-shadow: 0 0 0 5px rgba(13, 148, 136, 0.2);
}

button.send-btn:disabled {
    background-color: #9ca3af;
    cursor: not-allowed;
}

button.send-btn.recording {
    background-color: #ef4444;
    animation: pulse-record 1.2s ease-in-out infinite;
}

@keyframes pulse-record {
    0%, 100% { box-shadow: 0 0 0 0 rgba(239, 68, 68, 0.5); }
    50%       { box-shadow: 0 0 0 7px rgba(239, 68, 68, 0); }
}

.icon-svg {
    pointer-events: none;
}

button.icon-btn {
    background: none;
    border: none;
    color: white;
    cursor: pointer;
    padding: 8px;
    position: relative;
}

/* 按鈕浮動提示 (Tooltip) */
button.icon-btn::after {
    content: attr(data-tooltip);
    position: absolute;
    bottom: -35px;
    right: 0;
    background-color: rgba(0, 0, 0, 0.85);
    color: white;
    padding: 6px 10px;
    border-radius: 6px;
    font-size: 0.75rem;
    white-space: nowrap;
    opacity: 0;
    pointer-events: none;
    transition: opacity 0.2s ease;
    z-index: 100;
}

button.icon-btn::before {
    content: '';
    position: absolute;
    bottom: -8px;
    right: 12px;
    border-left: 6px solid transparent;
    border-right: 6px solid transparent;
    border-bottom: 6px solid rgba(0, 0, 0, 0.85);
    opacity: 0;
    pointer-events: none;
    transition: opacity 0.2s ease;
    z-index: 100;
}

@media (hover: hover) {
    button.icon-btn:hover::after,
    button.icon-btn:hover::before {
        opacity: 1;
    }
}

/* ── Header button hover animations ── */

/* Reset button: SVG 持續旋轉 */
@keyframes spin-reset {
    from { transform: rotate(0deg); }
    to   { transform: rotate(360deg); }
}
/* 3. Expand button: SVG 向外擴張脈動 */
@keyframes expand-pulse {
    0%, 100% { transform: scale(1); }
    50%       { transform: scale(1.22); }
}
/* 3b. Collapse (already expanded): SVG 向內收縮脈動 */
@keyframes collapse-pulse {
    0%, 100% { transform: scale(1); }
    50%       { transform: scale(0.78); }
}

/* Help button: SVG 困惑搖頭 */
@keyframes help-wobble {
    0%   { transform: rotate(0deg)   scale(1);    }
    10%  { transform: rotate(-22deg) scale(1.15); }
    25%  { transform: rotate(22deg)  scale(1.15); }
    40%  { transform: rotate(-16deg) scale(1.1);  }
    55%  { transform: rotate(16deg)  scale(1.1);  }
    68%  { transform: rotate(-8deg)  scale(1.05); }
    78%  { transform: rotate(8deg)   scale(1.05); }
    88%  { transform: rotate(-3deg)  scale(1);    }
    100% { transform: rotate(0deg)   scale(1);    }
}

@media (hover: hover) {
    #reset-btn:hover .icon-svg {
        animation: spin-reset 0.9s linear infinite;
    }
    #expand-btn:hover .icon-svg {
        animation: expand-pulse 1s ease-in-out infinite;
    }
    #app-container.expanded #expand-btn:hover .icon-svg {
        animation: collapse-pulse 1s ease-in-out infinite;
    }
    #help-btn:hover .icon-svg {
        animation: help-wobble 1.1s ease-in-out infinite;
    }
}

@media (pointer: coarse) {
    #expand-btn { display: none; }
    .help-expand-item { display: none; }
}

.footer-info {
    text-align: center;
    font-size: 0.65rem;
    color: #4b5563;
    margin-top: 8px;
}

/* Info Icon Styles - Updated to blend better */
.info-icon {
    appearance: none;
    padding: 0;
    line-height: 1;
    font-family: inherit;

    display: inline-flex;
    align-items: center;
    justify-content: center;
    width: 18px; /* 稍微縮小 */
    height: 18px;
    background-color: transparent; /* 改為透明背景 */
    color: var(--primary); /* 使用主色調文字 */
    border: 1.5px solid var(--primary); /* 加入主色調邊框 */
    border-radius: 50%;
    font-size: 12px;
    font-weight: bold;
    margin-left: 8px;
    cursor: pointer;
    vertical-align: middle;
    /* user-select: none; */ /* 按鈕不需要此屬性 */
    transition: all 0.2s;
    flex-shrink: 0;
}

.info-icon:hover, .info-icon:focus {
    transform: scale(1.1);
    background-color: var(--primary); /* Hover 時變為實心 */
    color: white;
}

/* ── 初始載入遮罩（JS 就緒前擋住整個畫面，script.js DOMContentLoaded 時移除）── */
#app-loading {
    position: fixed;
    inset: 0;
    background: rgba(0, 0, 0, 0.5);
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    gap: 14px;
    z-index: 2000;
    color: #fff;
    font-size: 15px;
    letter-spacing: 0.5px;
}
#app-loading p {
    margin: 0;
}
.app-loading-spinner {
    width: 40px;
    height: 40px;
    border: 4px solid rgba(255, 255, 255, 0.3);
    border-top-color: #fff;
    border-radius: 50%;
    animation: app-spin 0.9s linear infinite;
}
@keyframes app-spin { to { transform: rotate(360deg); } }
/* 逾時提示：純 CSS 延遲 15 秒後淡入（CSP 禁 inline script，故不用 JS 計時） */
.app-loading-timeout {
    opacity: 0;
    font-size: 13px;
    color: rgba(255, 255, 255, 0.85);
    animation: app-timeout-show 0.4s ease 15s forwards;
}
@keyframes app-timeout-show { to { opacity: 1; } }

@keyframes fadeIn { from { opacity: 0; transform: translateY(5px); } to { opacity: 1; transform: translateY(0); } }
@keyframes slideUp { from { opacity: 0; transform: translateY(10px); } to { opacity: 1; transform: translateY(0); } }

/* ── 同意書 Modal（與 log/log_style.css 相同；放在主樣式表隨頁面先載入，避免同意書以無樣式狀態閃現；移除 log 模組時可一併刪除）── */
#consent-overlay {
    position: fixed;
    inset: 0;
    background: rgba(0,0,0,0.45);
    display: flex;
    align-items: center;
    justify-content: center;
    z-index: 1000;
    animation: fadeIn 0.2s ease;
}
#consent-card {
    position: relative;
    background: #fff;
    border-radius: 12px;
    padding: 44px 24px 20px;
    max-width: 360px;
    width: calc(100% - 40px);
    box-shadow: 0 8px 32px rgba(0,0,0,0.18);
    display: flex;
    flex-direction: column;
    gap: 16px;
}
#consent-card p {
    margin: 0;
    font-size: 16px;
    line-height: 1.7;
    color: var(--text-primary);
}
.consent-close {
    position: absolute;
    top: 10px;
    right: 12px;
    background: none;
    border: none;
    font-size: 18px;
    color: var(--text-secondary);
    cursor: pointer;
    line-height: 1;
    padding: 4px 6px;
}
.consent-close:hover { color: var(--text-primary); }
.consent-btn-confirm {
    align-self: center;
    min-width: 100px;
    padding: 10px 24px;
    border: none;
    border-radius: 8px;
    font-size: 15px;
    font-weight: 600;
    cursor: pointer;
    background: var(--primary);
    color: #fff;
    transition: filter 0.15s;
}
.consent-btn-confirm:hover { filter: brightness(0.92); }
.consent-btn-confirm:focus { outline: none; }