/**
 * Markdown 渲染樣式 - 共用元件
 *
 * 使用方式：在 HTML 中引入此 CSS 後，
 * 對包含 Markdown 內容的容器加上 .message-text 類別
 *
 * 範例：<div class="message-text">${renderedMarkdown}</div>
 */

/* ===== 基礎設定 ===== */
.message-text {
    line-height: 1.6;
    word-wrap: break-word;
    overflow-wrap: break-word;
}

.message-text > *:first-child {
    margin-top: 0;
}

.message-text > *:last-child {
    margin-bottom: 0;
}

/* ===== 段落 ===== */
.message-text p {
    margin: 0.5rem 0;
    line-height: 1.6;
}

/* ===== 標題 ===== */
.message-text h1,
.message-text h2,
.message-text h3,
.message-text h4,
.message-text h5,
.message-text h6 {
    margin: 1rem 0 0.5rem 0;
    font-weight: 600;
    line-height: 1.3;
    color: var(--text-primary, #1e293b);
}

.message-text h1 {
    font-size: var(--text-2xl);
    border-bottom: 2px solid var(--border, #e2e8f0);
    padding-bottom: 0.3rem;
}

.message-text h2 {
    font-size: 1.3rem;
}

.message-text h3 {
    font-size: 1.1rem;
}

.message-text h4 {
    font-size: var(--text-base);
}

.message-text h5,
.message-text h6 {
    font-size: 0.95rem;
}

/* ===== 程式碼區塊 ===== */
.message-text pre {
    background: #f6f8fa;
    border: 1px solid #e1e4e8;
    border-radius: var(--radius-md);
    padding: 12px;
    margin: 0.75rem 0;
    overflow-x: auto;
    font-family: 'Consolas', 'Monaco', 'Courier New', monospace;
    font-size: 0.9rem;
    line-height: 1.5;
}

.message-text pre code {
    background: none;
    padding: 0;
    border: none;
    color: #24292e;
}

/* ===== 行內程式碼 ===== */
.message-text code {
    background: rgba(175, 184, 193, 0.2);
    color: #24292f;
    padding: 2px 6px;
    border-radius: var(--radius-xs);
    font-family: 'Consolas', 'Monaco', 'Courier New', monospace;
    font-size: 0.9em;
}

/* ===== 無序列表 ===== */
.message-text ul {
    margin: 0.75rem 0;
    padding-left: 0;
    list-style: none !important;  /* 覆蓋 base.css 全域重置 */
    line-height: 1.8;
}

.message-text ul li {
    margin: 0.35rem 0;
    padding-left: 1.5rem;
    position: relative;
    list-style: none !important;  /* 確保移除預設標記 */
}

/* 使用圓點符號 */
.message-text ul li::before {
    content: '●';
    position: absolute;
    left: 0;
    color: var(--text-secondary, #64748b);
    font-size: 0.7em;
    top: 0.4em;
}

/* ===== 有序列表 ===== */
.message-text ol {
    margin: 0.75rem 0;
    padding-left: 1.8rem;
    list-style: decimal !important;  /* 覆蓋 base.css 全域重置 */
    line-height: 1.8;
}

.message-text ol li {
    margin: 0.35rem 0;
    padding-left: 0.5rem;
    list-style-type: inherit;  /* 繼承父元素的 list-style */
}

/* 數字列表樣式優化 */
.message-text ol li::marker {
    color: var(--text-primary, #1e293b);
    font-weight: 600;
}

/* ===== 巢狀列表 ===== */
.message-text ul ul,
.message-text ol ul {
    margin-top: 0.25rem;
    margin-bottom: 0.25rem;
}

.message-text ul ul li::before {
    content: '○';
    font-size: 0.6em;
    top: 0.45em;
}

/* ===== 文字樣式 ===== */
.message-text strong {
    font-weight: 600;
    color: var(--text-primary, #1e293b);
}

.message-text em {
    font-style: italic;
    color: var(--text-secondary, #64748b);
}

/* ===== 引用 ===== */
.message-text blockquote {
    margin: 0.75rem 0;
    padding: 0.5rem 1rem;
    border-left: 4px solid var(--border, #e2e8f0);
    background: rgba(0, 0, 0, 0.03);
    color: var(--text-secondary, #64748b);
    font-style: italic;
}

.message-text blockquote p {
    margin: 0;
}

/* ===== 水平線 ===== */
.message-text hr {
    margin: 1rem 0;
    border: none;
    border-top: 2px solid var(--border, #e2e8f0);
}

/* 隱藏訊息末尾多餘的 hr（AI 回覆常在結尾產生 ---） */
.message-text > hr:last-child {
    display: none;
}

/* ===== 連結 ===== */
.message-text a {
    color: var(--primary, #6366f1);
    text-decoration: none;
    border-bottom: 1px solid transparent;
    transition: border-color var(--duration-normal) var(--ease-out);
}

.message-text a:hover {
    border-bottom-color: var(--primary, #6366f1);
}

/* ===== 表格 ===== */
.message-text table {
    margin: 0.75rem 0;
    border-collapse: collapse;
    width: 100%;
    font-size: var(--text-sm);
}

.message-text table th,
.message-text table td {
    padding: 0.5rem;
    border: 1px solid var(--border, #e2e8f0);
    text-align: left;
}

.message-text table th {
    background: rgba(0, 0, 0, 0.04);
    font-weight: 600;
}

.message-text table tr:nth-child(even) {
    background: rgba(0, 0, 0, 0.02);
}

/* ===== 圖片 ===== */
.message-text img {
    max-width: 100%;
    height: auto;
    border-radius: var(--radius-md);
    margin: 0.5rem 0;
}

/* ===== 任務列表 (GitHub 風格) ===== */
.message-text input[type="checkbox"] {
    margin-right: 0.5rem;
}

/* ===== 漸進式 Fade-in 動畫行 ===== */
.message-text .fade-line {
    display: inline;
    /* 過渡效果由 JavaScript 控制 */
}

/* 區塊級 fade-line（如清單、程式碼塊） */
.message-text .fade-line[style*="display: block"] {
    display: block;
    margin: 0.25rem 0;
}

/* 確保 fade-line 內的元素正確顯示 */
.message-text .fade-line p {
    display: inline;
    margin: 0;
}

.message-text .fade-line ul,
.message-text .fade-line ol {
    margin: 0.25rem 0;
}

.message-text .fade-line pre {
    margin: 0.5rem 0;
}

/* ===== 串流逐字 Fade-in（StreamingMarkdownRenderer） ===== */
/* 每個新到達的 chunk 字元包在 <span class="streaming-char">，
   用 keyframes 做 opacity + color 漸變。動畫結束後 JS 會 unwrap span。*/
.message-text .streaming-char {
    display: inline;
    animation: streamingCharFadeIn 0.45s ease-out both;
}

/* 區塊級 streaming-char（包住 list item / code block / table 的情況）*/
.message-text .streaming-char:has(p),
.message-text .streaming-char:has(pre),
.message-text .streaming-char:has(ul),
.message-text .streaming-char:has(ol),
.message-text .streaming-char:has(table) {
    display: block;
}

@keyframes streamingCharFadeIn {
    0% {
        opacity: 0;
        color: rgba(147, 112, 219, 0.85);
    }
    60% {
        opacity: 1;
        color: rgba(147, 112, 219, 0.85);
    }
    100% {
        opacity: 1;
        color: inherit;
    }
}

/* 降級：不支援 animation 時直接顯示，避免內容不可見 */
@media (prefers-reduced-motion: reduce) {
    .message-text .streaming-char {
        animation: none;
        opacity: 1;
        color: inherit;
    }
}

/* ===== 媒體素材圖片 (MarkdownRenderer.parseMediaAssets) ===== */
.media-asset-container {
    margin: 0.75rem 0;
    display: inline-block;
    max-width: 100%;
}

.media-asset-image {
    max-width: 100%;
    max-height: 400px;
    width: auto;
    height: auto;
    border-radius: var(--radius-lg);
    border: 1px solid var(--border, #e2e8f0);
    cursor: pointer;
    transition: transform var(--duration-normal) var(--ease-out), box-shadow var(--duration-normal) var(--ease-out);
    object-fit: contain;
    background-color: var(--bg-secondary);
}

.media-asset-image:hover {
    transform: scale(1.02);
    box-shadow: var(--shadow-md);
}

.media-asset-image.media-asset-error {
    opacity: 0.5;
    cursor: not-allowed;
}

/* ===== 圖片檢視器 (燈箱效果) ===== */
.media-asset-viewer {
    position: fixed;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    z-index: var(--z-toast);
    display: flex;
    align-items: center;
    justify-content: center;
    opacity: 0;
    visibility: hidden;
    transition: opacity var(--duration-slow) var(--ease-out), visibility var(--duration-slow) var(--ease-out);
}

.media-asset-viewer.show {
    opacity: 1;
    visibility: visible;
}

.media-asset-viewer .viewer-backdrop {
    position: absolute;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    background: rgba(0, 0, 0, 0.85);
    cursor: pointer;
}

.media-asset-viewer .viewer-content {
    position: relative;
    z-index: 1;
    max-width: 90vw;
    max-height: 90vh;
    display: flex;
    align-items: center;
    justify-content: center;
}

.media-asset-viewer .viewer-image {
    max-width: 90vw;
    max-height: 85vh;
    width: auto;
    height: auto;
    border-radius: var(--radius-lg);
    box-shadow: var(--shadow-2xl);
    object-fit: contain;
    background-color: var(--bg-primary);
}

.media-asset-viewer .viewer-close {
    position: absolute;
    top: -40px;
    right: -40px;
    width: 40px;
    height: 40px;
    border: none;
    background: rgba(255, 255, 255, 0.9);
    border-radius: var(--radius-full);
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    transition: background var(--duration-normal) var(--ease-out), transform var(--duration-normal) var(--ease-out);
    color: var(--neutral-800);
}

.media-asset-viewer .viewer-close:hover {
    background: var(--bg-primary);
    transform: scale(1.1);
}

.media-asset-viewer .viewer-close svg {
    width: 20px;
    height: 20px;
}

/* 響應式調整 */
@media (max-width: 768px) {
    .media-asset-image {
        max-height: 300px;
    }

    .media-asset-viewer .viewer-close {
        top: 10px;
        right: 10px;
    }

    .media-asset-viewer .viewer-image {
        max-width: 95vw;
        max-height: 80vh;
    }
}
