/* ============================
   MARKDOWN RENDERED STYLES
   ============================ */

/* --- Headers --- */
.msg-bubble h1.md-h1,
.msg-bubble h2.md-h2,
.msg-bubble h3.md-h3,
.msg-bubble h4.md-h4,
.msg-bubble h5.md-h5,
.msg-bubble h6.md-h6,
.chat-bubble h1.md-h1,
.chat-bubble h2.md-h2,
.chat-bubble h3.md-h3,
.chat-bubble h4.md-h4,
.chat-bubble h5.md-h5,
.chat-bubble h6.md-h6 {
    margin: 12px 0 8px 0;
    font-weight: 700;
    line-height: 1.3;
    color: var(--text-primary);
}

.msg-bubble h1.md-h1:first-child,
.msg-bubble h2.md-h2:first-child,
.msg-bubble h3.md-h3:first-child,
.chat-bubble h1.md-h1:first-child,
.chat-bubble h2.md-h2:first-child,
.chat-bubble h3.md-h3:first-child {
    margin-top: 0;
}

.msg-bubble h1.md-h1, .chat-bubble h1.md-h1 { font-size: 1.4em; }
.msg-bubble h2.md-h2, .chat-bubble h2.md-h2 { font-size: 1.25em; }
.msg-bubble h3.md-h3, .chat-bubble h3.md-h3 { font-size: 1.1em; }
.msg-bubble h4.md-h4, .chat-bubble h4.md-h4 { font-size: 1em; }
.msg-bubble h5.md-h5, .chat-bubble h5.md-h5 { font-size: 0.9em; }
.msg-bubble h6.md-h6, .chat-bubble h6.md-h6 { font-size: 0.85em; color: var(--text-secondary); }

/* --- Paragraphs --- */
.msg-bubble p,
.chat-bubble p {
    margin: 6px 0;
    line-height: 1.75;
}

.msg-bubble p:first-child,
.chat-bubble p:first-child {
    margin-top: 0;
}

.msg-bubble p:last-child,
.chat-bubble p:last-child {
    margin-bottom: 0;
}

/* --- Bold & Italic --- */
.msg-bubble strong,
.chat-bubble strong {
    font-weight: 700;
    color: var(--primary-light);
}

.msg-bubble em,
.chat-bubble em {
    font-style: italic;
    color: var(--text-primary);
}

.msg-bubble del,
.chat-bubble del {
    text-decoration: line-through;
    opacity: 0.6;
}

/* --- Inline Code --- */
.md-inline-code {
    background: rgba(201, 168, 76, 0.12);
    color: var(--primary-light);
    padding: 2px 7px;
    border-radius: 4px;
    font-family: 'Fira Code', 'SF Mono', 'Cascadia Code', 'Consolas', monospace;
    font-size: 0.88em;
    border: 1px solid rgba(201, 168, 76, 0.2);
    white-space: nowrap;
}

/* --- Code Blocks --- */
.md-code-block {
    position: relative;
    margin: 12px 0;
    border-radius: 10px;
    overflow: hidden;
    border: 1px solid var(--border);
    background: var(--code-bg);
    max-width: 100%;
}

.md-code-lang {
    display: inline-block;
    padding: 4px 12px;
    font-size: 0.72em;
    font-weight: 600;
    text-transform: uppercase;
    letter-spacing: 0.5px;
    color: var(--primary);
    background: rgba(201, 168, 76, 0.08);
    border-bottom: 1px solid var(--border);
    width: 100%;
}

.md-code-copy {
    position: absolute;
    top: 4px;
    right: 8px;
    background: rgba(255, 255, 255, 0.06);
    border: 1px solid var(--border);
    border-radius: 6px;
    padding: 4px 8px;
    font-size: 0.75rem;
    cursor: pointer;
    opacity: 0;
    transition: all 0.2s;
    color: var(--text-secondary);
    z-index: 2;
}

.md-code-block:hover .md-code-copy {
    opacity: 1;
}

.md-code-copy:hover {
    background: rgba(201, 168, 76, 0.15);
    border-color: var(--primary);
}

.md-code-copy.copied {
    opacity: 1;
}

.md-code-block pre {
    margin: 0;
    padding: 14px 16px;
    overflow-x: auto;
    -webkit-overflow-scrolling: touch;
}

.md-code-block code {
    font-family: 'Fira Code', 'SF Mono', 'Cascadia Code', 'Consolas', monospace;
    font-size: 0.85em;
    line-height: 1.6;
    color: #c8d3e0;
    white-space: pre;
    tab-size: 4;
}

/* Scrollbar for code blocks */
.md-code-block pre::-webkit-scrollbar {
    height: 6px;
}

.md-code-block pre::-webkit-scrollbar-track {
    background: transparent;
}

.md-code-block pre::-webkit-scrollbar-thumb {
    background: rgba(201, 168, 76, 0.3);
    border-radius: 3px;
}

/* --- Blockquotes --- */
.md-blockquote {
    margin: 10px 0;
    padding: 10px 16px;
    border-left: 3px solid var(--primary);
    background: rgba(201, 168, 76, 0.06);
    border-radius: 0 8px 8px 0;
    font-style: italic;
    color: var(--text-secondary);
}

.md-blockquote p {
    margin: 4px 0;
}

/* --- Lists --- */
.md-list {
    margin: 8px 0;
    padding-left: 24px;
}

.md-list li {
    margin: 4px 0;
    line-height: 1.65;
    color: var(--text-primary);
}

.md-list li::marker {
    color: var(--primary);
}

ol.md-list {
    list-style-type: decimal;
}

ol.md-list li::marker {
    font-weight: 600;
}

ul.md-list {
    list-style-type: disc;
}

/* Nested lists (if any) */
.md-list .md-list {
    margin: 4px 0;
}

/* --- Tables --- */
.md-table-wrapper {
    margin: 12px 0;
    overflow-x: auto;
    border-radius: 8px;
    border: 1px solid var(--border);
    -webkit-overflow-scrolling: touch;
    max-width: 100%;
}

.md-table {
    width: 100%;
    border-collapse: collapse;
    font-size: 0.88em;
    white-space: nowrap;
}

.md-table th {
    background: rgba(201, 168, 76, 0.1);
    color: var(--primary-light);
    font-weight: 700;
    text-align: left;
    padding: 10px 14px;
    border-bottom: 2px solid var(--primary);
}

.md-table td {
    padding: 8px 14px;
    border-bottom: 1px solid var(--border);
    color: var(--text-primary);
}

.md-table tr:last-child td {
    border-bottom: none;
}

.md-table tr:hover td {
    background: rgba(255, 255, 255, 0.03);
}

/* Table scrollbar */
.md-table-wrapper::-webkit-scrollbar {
    height: 4px;
}

.md-table-wrapper::-webkit-scrollbar-track {
    background: transparent;
}

.md-table-wrapper::-webkit-scrollbar-thumb {
    background: rgba(201, 168, 76, 0.3);
    border-radius: 2px;
}

/* --- Links --- */
.md-link {
    color: var(--primary);
    text-decoration: none;
    border-bottom: 1px solid rgba(201, 168, 76, 0.3);
    transition: all 0.2s;
}

.md-link:hover {
    color: var(--primary-light);
    border-bottom-color: var(--primary-light);
}

/* --- Horizontal Rule --- */
.md-hr {
    margin: 16px 0;
    border: none;
    height: 1px;
    background: linear-gradient(90deg, transparent, var(--primary), transparent);
    opacity: 0.4;
}

/* --- User bubble overrides (keep user text simple) --- */
.msg-user .msg-bubble strong,
.chat-user .chat-bubble strong {
    color: inherit;
}

.msg-user .msg-bubble .md-inline-code,
.chat-user .chat-bubble .md-inline-code {
    background: rgba(0, 0, 0, 0.15);
    color: inherit;
    border-color: rgba(0, 0, 0, 0.2);
}

/* --- Mobile adjustments --- */
@media (max-width: 768px) {
    .md-code-block pre {
        padding: 10px 10px;
    }

    .md-code-block code {
        font-size: 0.85em;
    }

    .md-code-copy {
        opacity: 1;
        padding: 3px 6px;
        font-size: 0.72rem;
    }

    .md-inline-code {
        font-size: 0.9em;
        padding: 1px 5px;
    }

    .md-table {
        font-size: 0.9em;
    }

    .md-table th,
    .md-table td {
        padding: 6px 8px;
    }

    .md-list {
        padding-left: 14px;
    }

    .md-list li {
        font-size: inherit;
    }

    .md-blockquote {
        padding: 8px 8px;
        font-size: 0.98em;
    }

    .msg-bubble h1.md-h1, .chat-bubble h1.md-h1 { font-size: 1.25em; }
    .msg-bubble h2.md-h2, .chat-bubble h2.md-h2 { font-size: 1.15em; }
    .msg-bubble h3.md-h3, .chat-bubble h3.md-h3 { font-size: 1.08em; }
}

/* --- Light Mode Overrides --- */
:root.light-mode .md-inline-code {
    background: rgba(154, 123, 45, 0.1);
    border-color: rgba(154, 123, 45, 0.2);
    color: #7a6020;
}

:root.light-mode .md-code-block {
    border-color: var(--border);
}

:root.light-mode .md-code-block code {
    color: #333;
}

:root.light-mode .md-code-lang {
    background: rgba(154, 123, 45, 0.06);
}

:root.light-mode .md-code-copy {
    background: rgba(0, 0, 0, 0.04);
    color: var(--text-secondary);
}

:root.light-mode .md-blockquote {
    background: rgba(154, 123, 45, 0.04);
    border-left-color: var(--primary);
}

:root.light-mode .md-table th {
    background: rgba(154, 123, 45, 0.08);
}

:root.light-mode .md-table tr:hover td {
    background: rgba(154, 123, 45, 0.04);
}

:root.light-mode strong {
    color: var(--primary);
}
