/* Chat Page Specific Styles */

/* Ensure no bottom gaps - reset all potential spacing */
.content-wrapper {
    margin-bottom: 0 !important;
    padding-bottom: 0 !important;
}

/* Override base.html body padding for chat page */
body.chat-page {
    padding-top: var(--nav-height) !important;
    padding-bottom: 0 !important; /* Remove any footer padding */
    margin-bottom: 0 !important;
    overflow: hidden; /* Prevent outer page scroll */
}

/* Chat page specific styles - ChatGPT layout with header only (no footer) */
.chat-container {
    /* Fill exactly the space between fixed navbar and bottom of viewport */
    height: calc(100vh - var(--nav-height));
    height: calc(100dvh - var(--nav-height)); /* Use dynamic viewport height for mobile */
    display: flex;
    background: var(--bg-main);
    overflow: hidden;
    margin: 0;
    padding: 0;
    position: relative;
    box-sizing: border-box;
}

/* Sidebar styles */
.chat-sidebar {
    width: 320px;
    background: white;
    border-right: 1px solid var(--border-light);
    display: flex;
    flex-direction: column;
    position: relative;
    transition: all 0.3s ease;
    box-shadow: 2px 0 10px rgba(0,0,0,0.05);
}

.chat-sidebar.collapsed {
    width: 60px;
}

/* Sidebar header */
.sidebar-header {
    padding: 1rem;
    border-bottom: 1px solid var(--border-light);
    display: flex;
    align-items: center;
    justify-content: space-between;
    min-height: 60px;
    flex-shrink: 0;
}

.sidebar-title {
    font-weight: 600;
    color: var(--text-dark);
    font-size: 1.1rem;
    white-space: nowrap;
    overflow: hidden;
}

.sidebar-toggle {
    background: none;
    border: none;
    color: var(--text-light);
    font-size: 1.1rem;
    padding: 0.5rem;
    border-radius: 6px;
    transition: all 0.3s ease;
    cursor: pointer;
}

.sidebar-toggle:hover {
    background: rgba(86, 145, 227, 0.1);
    color: var(--primary);
}

/* Conversation list */
.conversation-list {
    flex: 1;
    overflow-y: auto;
    padding: 0.5rem;
}

.new-chat-btn {
    width: 100%;
    margin-bottom: 1rem;
    background: var(--primary);
    border: none;
    color: white;
    padding: 0.75rem 1rem;
    border-radius: 12px;
    font-weight: 500;
    transition: all 0.3s ease;
    display: flex;
    align-items: center;
    gap: 0.5rem;
}

.new-chat-btn:hover {
    background: var(--secondary);
    transform: translateY(-1px);
}

.conversation-item {
    padding: 0.75rem 1rem;
    margin-bottom: 0.5rem;
    border-radius: 12px;
    cursor: pointer;
    transition: all 0.3s ease;
    border: 1px solid transparent;
    position: relative;
}

.conversation-item:hover {
    background: rgba(86, 145, 227, 0.05);
    border-color: var(--border-light);
}

.conversation-item.active {
    background: rgba(86, 145, 227, 0.1);
    border-color: var(--primary);
}

.conversation-title {
    font-weight: 500;
    font-size: 0.9rem;
    line-height: 1.3;
    margin-bottom: 0.25rem;
    display: -webkit-box;
    -webkit-line-clamp: 2;
    -webkit-box-orient: vertical;
    overflow: hidden;
}

.conversation-time {
    font-size: 0.75rem;
    color: var(--text-light);
}

/* Main chat area */
.chat-main {
    flex: 1;
    display: flex;
    flex-direction: column;
    min-width: 0;
}

/* Chat header */
.chat-header {
    padding: 1rem 1.5rem;
    background: white;
    border-bottom: 1px solid var(--border-light);
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 1rem;
    min-height: 70px;
    flex-shrink: 0;
}

.mobile-menu-btn {
    display: none;
    background: none;
    border: none;
    color: var(--text-light);
    font-size: 1.2rem;
    padding: 0.5rem;
    border-radius: 6px;
    cursor: pointer;
}

.mobile-menu-btn:hover {
    background: rgba(86, 145, 227, 0.1);
    color: var(--primary);
}

.chat-title {
    flex: 1;
    font-weight: 600;
    font-size: 1.1rem;
    color: var(--text-dark);
}

.chat-controls {
    display: flex;
    align-items: center;
    gap: 0.5rem;
}

.language-toggle {
    display: flex;
    align-items: center;
    background: var(--card-bg);
    border: 1px solid var(--border-light);
    border-radius: 20px;
    padding: 0.15rem;
}

.language-toggle button {
    background: none;
    border: none;
    padding: 0.3rem 0.6rem;
    border-radius: 15px;
    font-size: 0.8rem;
    font-weight: 500;
    transition: all 0.3s ease;
    cursor: pointer;
    color: var(--text-light);
}

.language-toggle button.active {
    background: var(--primary);
    color: white;
}

/* Chat messages area */
.chat-messages {
    flex: 1;
    overflow-y: auto;
    padding: 1.5rem;
    display: flex;
    flex-direction: column;
    gap: 1.5rem;
    background: #f8f9fa;
    position: relative;
}

.message-wrapper {
    display: flex;
    align-items: flex-start;
    gap: 1rem;
}

.message-wrapper.user {
    flex-direction: row-reverse;
}

.message-avatar {
    width: 36px;
    height: 36px;
    border-radius: 50%;
    display: flex;
    align-items: center;
    justify-content: center;
    flex-shrink: 0;
    font-size: 1rem;
}

.user .message-avatar {
    background: linear-gradient(135deg, var(--primary), var(--secondary));
    color: white;
}

.assistant .message-avatar {
    background: var(--card-bg);
    color: var(--primary);
    border: 2px solid var(--border-light);
}

.message-content {
    max-width: 70%;
    padding: 1rem 1.25rem;
    border-radius: 20px;
    word-wrap: break-word;
    line-height: 1.5;
    position: relative;
}

.user .message-content {
    background: linear-gradient(135deg, var(--primary), var(--secondary));
    color: white;
    border-bottom-right-radius: 6px;
}

.assistant .message-content {
    background: white;
    color: var(--text-dark);
    border: 1px solid var(--border-light);
    border-bottom-left-radius: 6px;
    box-shadow: 0 2px 8px rgba(0,0,0,0.05);
    /* Prevent visible text caret/insertion cursor inside assistant bubbles */
    caret-color: transparent !important;
    outline: none;
    user-select: text;
    /* Ensure no focus */
    pointer-events: auto;
    -webkit-user-modify: read-only;
    -moz-user-modify: read-only;
}

/* Comprehensive caret suppression for all descendants */
.assistant .message-content,
.assistant .message-content *,
.assistant .message-content pre,
.assistant .message-content code,
.assistant .message-content table,
.assistant .message-content td,
.assistant .message-content th,
.assistant .message-content a,
.assistant .message-content .markdown-content,
.assistant .message-content .markdown-content *,
.answer,
.answer * {
    caret-color: transparent !important;
    -webkit-user-modify: read-only !important;
    -moz-user-modify: read-only !important;
}

/* Additional caret suppression for contenteditable=false elements */
[contenteditable="false"] {
    caret-color: transparent !important;
}

/* 完全禁用打字游標動畫 - 只保留逐字渲染功能 */
.assistant .message-content::after,
.assistant .message-content[data-typing="true"]::after,
.assistant .message-content[data-typing="false"]::after,
.assistant .message-content:not([data-typing])::after,
.assistant .message-content[data-typing=""]::after {
    display: none !important;
    content: none !important;
    animation: none !important;
    border: none !important;
}

/* 移除眨眼動畫 */
@keyframes blink {
    /* 動畫已禁用 */
}

/* Special handling for typing animation state */
.assistant .message-content[data-typing="true"] {
    pointer-events: none !important;
    caret-color: transparent !important;
    cursor: default !important;
}

/* After completion: hide system caret everywhere inside the answer */
.assistant .message-content[data-typing="false"],
.assistant .message-content[data-typing="false"] *,
.answer[data-typing="false"],
.answer[data-typing="false"] * {
    caret-color: transparent !important;
}

/* HOTFIX: Enhanced safety nets for all browsers */
/* Safari/WebKit: forbid accidental editability */
.answer,
.assistant .message-content {
    outline: none !important;
    -webkit-user-modify: read-only !important;
    -moz-user-modify: read-only !important;
    user-modify: read-only !important;
}

/* Ensure no platform caret paints anywhere inside finished answers */
.answer[data-typing="false"],
.answer[data-typing="false"] * {
    caret-color: transparent !important;
    -webkit-user-modify: read-only !important;
    -moz-user-modify: read-only !important;
    user-modify: read-only !important;
}

/* Optional: during typing, avoid hover/focus artifacts */
.answer[data-typing="true"],
.assistant .message-content[data-typing="true"] {
    pointer-events: none !important;
}

/* Force hidden textareas to be invisible */
.answer textarea.hidden,
.answer textarea.copy-buffer,
.answer textarea[aria-hidden="true"],
.answer textarea[style*="opacity:0"],
.answer textarea[style*="visibility:hidden"],
.assistant .message-content textarea {
    position: fixed !important;
    left: -10000px !important;
    top: -10000px !important;
    caret-color: transparent !important;
    pointer-events: none !important;
}

.answer[contenteditable="true"],
.assistant .message-content[contenteditable="true"] {
    contenteditable: false !important;
}

/* Hide any cursor in CodeMirror/Monaco editors if present */
.cm-editor:not(.cm-focused) .cm-cursor,
.monaco-editor .cursor {
    display: none !important;
}

/* Ensure code blocks and buttons remain interactive but without caret */
.assistant .message-content .copy-button {
    pointer-events: auto;
    caret-color: transparent !important;
}

.assistant .message-content a {
    pointer-events: auto;
}

/* Empty state */
.empty-state {
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    height: 100%;
    text-align: center;
    padding: 2rem;
    color: var(--text-light);
}

.empty-state i {
    font-size: 4rem;
    color: var(--primary);
    margin-bottom: 1.5rem;
    opacity: 0.7;
}

.empty-state h3 {
    font-weight: 600;
    margin-bottom: 0.5rem;
    color: var(--text-dark);
}

.sample-questions {
    display: flex;
    flex-wrap: wrap;
    gap: 0.75rem;
    justify-content: center;
    margin-top: 2rem;
}

.sample-question {
    background: white;
    border: 1px solid var(--border-light);
    color: var(--text-dark);
    padding: 0.5rem 1rem;
    border-radius: 20px;
    font-size: 0.85rem;
    transition: all 0.3s ease;
    cursor: pointer;
}

.sample-question:hover {
    background: var(--primary);
    color: white;
    transform: translateY(-1px);
}

/* Chat input area - ensure it touches bottom edge */
.chat-input-area {
    padding: 1rem 1.5rem 1rem 1.5rem;
    padding-bottom: max(1rem, env(safe-area-inset-bottom)); /* Add safe-area for iOS */
    background: white;
    border-top: 1px solid var(--border-light);
    flex-shrink: 0;
    margin: 0;
}

.chat-input-form {
    display: flex;
    align-items: flex-end;
    gap: 1rem;
    max-width: 100%;
}

.input-wrapper {
    flex: 1;
    position: relative;
}

.message-input {
    width: 100%;
    border: 2px solid var(--border-light);
    border-radius: 20px;
    padding: 0.75rem 1rem;
    resize: none;
    font-size: 0.95rem;
    line-height: 1.4;
    min-height: 44px;
    max-height: 120px;
    transition: all 0.3s ease;
    font-family: inherit;
}

.message-input:focus {
    outline: none;
    border-color: var(--primary);
    box-shadow: 0 0 0 0.2rem rgba(86, 145, 227, 0.15);
}

.send-button {
    background: var(--primary);
    border: none;
    color: white;
    width: 44px;
    height: 44px;
    border-radius: 50%;
    display: flex;
    align-items: center;
    justify-content: center;
    cursor: pointer;
    transition: all 0.3s ease;
    flex-shrink: 0;
}

.send-button:hover {
    background: var(--secondary);
    transform: translateY(-1px);
}

.send-button:disabled {
    background: #ccc;
    cursor: not-allowed;
    transform: none;
}

/* Jump to bottom button */
.jump-bottom {
    position: absolute;
    bottom: 1rem;
    right: 1rem;
    background: white;
    border: 1px solid var(--border-light);
    border-radius: 50%;
    width: 48px;
    height: 48px;
    display: flex;
    align-items: center;
    justify-content: center;
    cursor: pointer;
    box-shadow: 0 4px 12px rgba(0,0,0,0.1);
    opacity: 0;
    transform: translateY(20px);
    transition: all 0.3s ease;
    z-index: 10;
}

.jump-bottom.show {
    opacity: 1;
    transform: translateY(0);
}

.jump-bottom:hover {
    background: var(--primary);
    color: white;
}

/* Mobile overlay */
.mobile-overlay {
    position: fixed;
    top: var(--nav-height);
    left: 0;
    width: 100%;
    height: calc(100vh - var(--nav-height));
    height: calc(100dvh - var(--nav-height)); /* Use dynamic viewport height for mobile */
    background: rgba(0,0,0,0.5);
    z-index: 998;
    display: none;
}

/* Loading animation */
.typing-indicator {
    display: flex;
    align-items: center;
    gap: 0.25rem;
    padding: 1rem 1.25rem;
}

.typing-dot {
    width: 6px;
    height: 6px;
    border-radius: 50%;
    background: var(--text-light);
    animation: typing 1.4s infinite ease-in-out;
}

.typing-dot:nth-child(2) { animation-delay: 0.2s; }
.typing-dot:nth-child(3) { animation-delay: 0.4s; }

@keyframes typing {
    0%, 60%, 100% { transform: translateY(0); opacity: 0.4; }
    30% { transform: translateY(-10px); opacity: 1; }
}

/* Streaming cursor for markdown content */
.streaming-cursor {
    display: inline-block;
    width: 2px;
    height: 1em;
    background: var(--primary);
    vertical-align: baseline;
    animation: none;
    margin: 0;
}

/* Hide streaming cursor when typing is false */
.assistant .message-content[data-typing="false"] .streaming-cursor {
    display: none !important;
}

/* Responsive design */
@media (max-width: 768px) {
    .chat-sidebar {
        position: fixed;
        top: var(--nav-height);
        left: 0;
        height: calc(100vh - var(--nav-height));
        height: calc(100dvh - var(--nav-height)); /* Use dynamic viewport height for mobile */
        z-index: 999;
        transform: translateX(-100%);
        width: 280px;
    }
    
    .chat-sidebar.mobile-visible {
        transform: translateX(0);
    }
    
    .mobile-menu-btn {
        display: flex !important;
    }
    
    .message-content {
        max-width: 85%;
    }
    
    .chat-messages {
        padding: 1rem;
        gap: 1rem;
    }
    
    .chat-input-area {
        padding: 1rem 1rem 1rem 1rem;
        padding-bottom: max(1rem, env(safe-area-inset-bottom)); /* Add safe-area for iOS mobile */
    }
    
    .sample-questions {
        flex-direction: column;
        align-items: stretch;
    }
    
    .chat-header {
        padding: 1rem;
    }
    
    .sidebar-header {
        padding: 1rem;
    }
}

@media (max-width: 480px) {
    .message-content {
        max-width: 90%;
    }
    
    /* Extra small screens - ensure no bottom gaps */
    .chat-container {
        height: calc(100vh - var(--nav-height));
        height: calc(100dvh - var(--nav-height));
        height: -webkit-fill-available; /* Fallback for older webkit */
        min-height: calc(100vh - var(--nav-height));
    }
    
    .chat-input-area {
        padding-bottom: max(0.5rem, env(safe-area-inset-bottom));
    }
}

/* iOS Safari specific fixes */
@supports (-webkit-appearance: none) {
    .chat-container {
        height: -webkit-fill-available;
        height: calc(100vh - var(--nav-height));
    }
}

/* Additional viewport unit support */
@supports (height: 100dvh) {
    .chat-container {
        height: calc(100dvh - var(--nav-height)) !important;
    }
    
    @media (max-width: 768px) {
        .chat-sidebar {
            height: calc(100dvh - var(--nav-height)) !important;
        }
        
        .mobile-overlay {
            height: calc(100dvh - var(--nav-height)) !important;
        }
    }
}

/* Collapsed sidebar styles */
.chat-sidebar.collapsed .sidebar-title,
.chat-sidebar.collapsed .new-chat-btn span,
.chat-sidebar.collapsed .conversation-title,
.chat-sidebar.collapsed .conversation-time {
    display: none;
}

.chat-sidebar.collapsed .new-chat-btn {
    padding: 0.75rem;
    justify-content: center;
}

.chat-sidebar.collapsed .conversation-item {
    padding: 0.75rem;
    text-align: center;
}

/* Markdown Styles */
.markdown-content {
    font-size: 0.95rem;
    line-height: 1.6;
    color: var(--text-dark);
}

.markdown-content h1,
.markdown-content h2,
.markdown-content h3,
.markdown-content h4,
.markdown-content h5,
.markdown-content h6 {
    margin-top: 1.5rem;
    margin-bottom: 0.75rem;
    font-weight: 600;
    line-height: 1.3;
}

.markdown-content h1 { font-size: 1.5rem; }
.markdown-content h2 { font-size: 1.3rem; }
.markdown-content h3 { font-size: 1.15rem; }
.markdown-content h4 { font-size: 1.05rem; }
.markdown-content h5 { font-size: 0.95rem; }
.markdown-content h6 { font-size: 0.9rem; }

.markdown-content p {
    margin-bottom: 0.75rem;
}

.markdown-content ul,
.markdown-content ol {
    margin-bottom: 0.75rem;
    padding-left: 1.5rem;
}

.markdown-content li {
    margin-bottom: 0.25rem;
}

.markdown-content blockquote {
    margin: 0.75rem 0;
    padding: 0.5rem 1rem;
    border-left: 3px solid var(--primary);
    background: rgba(86, 145, 227, 0.05);
    border-radius: 4px;
}

.markdown-content code {
    padding: 0.2rem 0.4rem;
    background: rgba(0, 0, 0, 0.06);
    border-radius: 3px;
    font-family: 'JetBrains Mono', 'Courier New', monospace;
    font-size: 0.85em;
}

.markdown-content pre {
    margin: 0.75rem 0;
    padding: 1rem;
    background: #2d2d2d;
    border-radius: 8px;
    overflow-x: auto;
    position: relative;
}

.markdown-content pre code {
    background: none;
    padding: 0;
    color: #f8f8f2;
    font-size: 0.9rem;
    line-height: 1.5;
}

/* Code block enhancements */
.code-block-wrapper {
    position: relative;
    margin: 0.75rem 0;
}

.code-block-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 0.5rem 1rem;
    background: #1e1e1e;
    border-radius: 8px 8px 0 0;
    font-size: 0.85rem;
    color: #888;
    font-family: 'JetBrains Mono', monospace;
}

.code-block-wrapper pre {
    margin-top: 0;
    border-radius: 0 0 8px 8px;
}

.copy-button {
    padding: 0.25rem 0.75rem;
    background: rgba(255, 255, 255, 0.1);
    border: 1px solid rgba(255, 255, 255, 0.2);
    color: #fff;
    border-radius: 4px;
    font-size: 0.75rem;
    cursor: pointer;
    transition: all 0.2s ease;
}

.copy-button:hover {
    background: rgba(255, 255, 255, 0.2);
}

.copy-button.copied {
    background: #4caf50;
    border-color: #4caf50;
}

/* Tables */
.markdown-content table {
    width: 100%;
    margin: 0.75rem 0;
    border-collapse: collapse;
    overflow-x: auto;
    display: block;
}

.markdown-content th,
.markdown-content td {
    padding: 0.5rem 0.75rem;
    border: 1px solid var(--border-light);
    text-align: left;
}

.markdown-content th {
    background: var(--bg-main);
    font-weight: 600;
}

.markdown-content tr:nth-child(even) {
    background: rgba(0, 0, 0, 0.02);
}

/* Links */
.markdown-content a {
    color: var(--primary);
    text-decoration: none;
    border-bottom: 1px solid transparent;
    transition: border-color 0.2s ease;
}

.markdown-content a:hover {
    border-bottom-color: var(--primary);
}

/* Horizontal rule */
.markdown-content hr {
    margin: 1.5rem 0;
    border: none;
    border-top: 1px solid var(--border-light);
}

/* Task lists */
.markdown-content .task-list-item {
    list-style: none;
    margin-left: -1.5rem;
}

.markdown-content .task-list-item input[type="checkbox"] {
    margin-right: 0.5rem;
    pointer-events: none;
}

/* Math styles */
.markdown-content .katex {
    font-size: 1.1em;
}

.markdown-content .katex-display {
    margin: 1rem 0;
    overflow-x: auto;
    overflow-y: hidden;
}

/* Streaming indicator */
.streaming-cursor {
    display: inline-block;
    width: 2px;
    height: 1em;
    background: var(--primary);
    vertical-align: baseline;
    animation: none;
    margin: 0;
}

/* Dark theme for code (matches prism-tomorrow) */
.user .markdown-content code {
    background: rgba(255, 255, 255, 0.15);
    color: white;
}

/* Responsive tables */
@media (max-width: 768px) {
    .markdown-content table {
        font-size: 0.85rem;
    }
    
    .markdown-content th,
    .markdown-content td {
        padding: 0.35rem 0.5rem;
    }
}
