:root {
    --bg-color: #09090b;
    --card-bg: #18181b;
    --text-primary: #fafafa;
    --text-secondary: #a1a1aa;
    --accent-color: #6366f1;
    --accent-hover: #4f46e5;
    --user-msg-bg: #4f46e5;
    --ai-msg-bg: #27272a;
    --border-color: #27272a;
    --shadow-sm: 0 1px 2px 0 rgb(0 0 0 / 0.05);
    --shadow-md: 0 4px 6px -1px rgb(0 0 0 / 0.1), 0 2px 4px -2px rgb(0 0 0 / 0.1);
    --shadow-lg: 0 10px 15px -3px rgb(0 0 0 / 0.1), 0 4px 6px -4px rgb(0 0 0 / 0.1);
}

* {
    box-sizing: border-box;
    margin: 0;
    padding: 0;
    /* 移除从 * 选择器设置 font-family，避免覆盖 KaTeX 字体 */
}

body {
    background-color: var(--bg-color);
    color: var(--text-primary);
    height: 100vh;
    display: flex;
    flex-direction: column;
    overflow: hidden;
    /* 在 body 上设置默认字体，允许 KaTeX 使用自己的字体 */
    font-family: 'Inter', -apple-system, BlinkMacSystemFont, sans-serif;
}

/* 确保 KaTeX 使用其自己的字体，不继承全局字体 */
.katex,
.katex * {
    font-family: inherit;
}

/* Login Overlay - 保持不变 */
.overlay {
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background: rgba(9, 9, 11, 0.95);
    display: flex;
    justify-content: center;
    align-items: center;
    z-index: 1000;
    backdrop-filter: blur(8px);
}

.login-card {
    background: var(--card-bg);
    padding: 2.5rem;
    border-radius: 1.5rem;
    width: 100%;
    max-width: 420px;
    text-align: center;
    border: 1px solid var(--border-color);
    box-shadow: var(--shadow-lg);
}

.login-card h2 {
    margin-bottom: 0.75rem;
    font-size: 1.5rem;
    background: linear-gradient(to right, #818cf8, #c084fc);
    -webkit-background-clip: text;
    background-clip: text;
    -webkit-text-fill-color: transparent;
}

.login-card p {
    color: var(--text-secondary);
    margin-bottom: 2rem;
}

.login-card input {
    width: 100%;
    padding: 1rem;
    border-radius: 0.75rem;
    border: 1px solid var(--border-color);
    background: var(--bg-color);
    color: white;
    margin-bottom: 1.25rem;
    outline: none;
    transition: border-color 0.2s, box-shadow 0.2s;
}

.login-card input:focus {
    border-color: var(--accent-color);
    box-shadow: 0 0 0 2px rgba(99, 102, 241, 0.2);
}

.login-card button {
    width: 100%;
    padding: 1rem;
    border-radius: 0.75rem;
    border: none;
    background: var(--accent-color);
    color: white;
    font-weight: 600;
    cursor: pointer;
    transition: background 0.2s, transform 0.1s;
}

.login-card button:hover {
    background: var(--accent-hover);
}

.login-card button:active {
    transform: scale(0.98);
}

.error-msg {
    color: #ef4444;
    font-size: 0.875rem;
    margin-top: 1rem;
    min-height: 1.25rem;
}

.hidden {
    display: none !important;
}

/* App Layout */
#app-container {
    display: flex;
    height: 100%;
    width: 100%;
    max-width: 100%;
    position: relative;
    overflow: hidden;
}

/* Sidebar */
.sidebar {
    width: 280px;
    background: #000000;
    border-right: 1px solid var(--border-color);
    display: flex;
    flex-direction: column;
    transition: transform 0.3s ease;
    z-index: 100;
    flex-shrink: 0;
}

.sidebar-header {
    padding: 1rem 1.25rem;
    display: flex;
    justify-content: space-between;
    align-items: center;
    border-bottom: 1px solid var(--border-color);
}

.sidebar-logo {
    display: flex;
    align-items: center;
}

.logo-img {
    width: 36px;
    height: 36px;
    border-radius: 8px;
    object-fit: cover;
    filter: drop-shadow(0 2px 4px rgba(0, 0, 0, 0.3));
    transition: transform 0.2s ease;
}

.logo-img:hover {
    transform: scale(1.05);
}

.sidebar-header-actions {
    display: flex;
    gap: 0.25rem;
    align-items: center;
}

.sidebar-header h2 {
    font-size: 1.1rem;
    font-weight: 600;
    color: var(--text-primary);
}

/* Search Panel */
.search-panel {
    padding: 0.75rem 1rem;
    border-bottom: 1px solid var(--border-color);
    background: rgba(0, 0, 0, 0.2);
}

.search-input-wrapper {
    display: flex;
    align-items: center;
    background: var(--card-bg);
    border: 1px solid var(--border-color);
    border-radius: 0.75rem;
    padding: 0.5rem 0.75rem;
    gap: 0.5rem;
    transition: border-color 0.2s, box-shadow 0.2s;
}

.search-input-wrapper:focus-within {
    border-color: var(--accent-color);
    box-shadow: 0 0 0 2px rgba(99, 102, 241, 0.15);
}

.search-icon {
    color: var(--text-secondary);
    flex-shrink: 0;
}

.search-panel input {
    flex: 1;
    background: transparent;
    border: none;
    color: var(--text-primary);
    outline: none;
    font-size: 0.875rem;
}

.search-panel input::placeholder {
    color: var(--text-secondary);
}

.clear-search-btn {
    background: transparent;
    border: none;
    color: var(--text-secondary);
    cursor: pointer;
    padding: 2px;
    display: flex;
    align-items: center;
    justify-content: center;
    border-radius: 4px;
    transition: all 0.2s;
}

.clear-search-btn:hover {
    color: var(--text-primary);
    background: rgba(255, 255, 255, 0.1);
}

.search-results {
    margin-top: 0.75rem;
    max-height: 300px;
    overflow-y: auto;
}

.search-result-item {
    padding: 0.625rem 0.75rem;
    border-radius: 0.5rem;
    cursor: pointer;
    margin-bottom: 0.25rem;
    transition: all 0.2s;
    border: 1px solid transparent;
}

.search-result-item:hover {
    background: var(--card-bg);
    border-color: var(--border-color);
}

.search-result-title {
    font-size: 0.875rem;
    font-weight: 500;
    color: var(--text-primary);
    margin-bottom: 0.25rem;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

.search-result-match {
    font-size: 0.75rem;
    color: var(--text-secondary);
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

.search-result-match .highlight {
    background: rgba(99, 102, 241, 0.3);
    color: var(--text-primary);
    padding: 0 2px;
    border-radius: 2px;
}

.search-no-results {
    text-align: center;
    color: var(--text-secondary);
    font-size: 0.875rem;
    padding: 1rem;
}

/* History Section */
.history-section {
    flex: 1;
    display: flex;
    flex-direction: column;
    overflow: hidden;
}

.history-title {
    font-size: 0.75rem;
    font-weight: 600;
    color: var(--text-secondary);
    text-transform: uppercase;
    letter-spacing: 0.05em;
    padding: 1rem 1.25rem 0.5rem;
}

/* 新增：侧边栏按钮容器 */
.sidebar-actions {
    padding: 1rem;
    border-bottom: 1px solid var(--border-color);
}

/* 新增：侧边栏新对话按钮样式 */
.new-chat-primary-btn {
    width: 100%;
    background: var(--accent-color);
    border: none;
    color: white;
    padding: 0.75rem;
    border-radius: 0.75rem;
    cursor: pointer;
    font-weight: 500;
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 0.5rem;
    transition: background 0.2s;
    font-size: 0.95rem;
}

.new-chat-primary-btn:hover {
    background: var(--accent-hover);
}

#history-list {
    flex: 1;
    overflow-y: auto;
    padding: 0.75rem;
    /* 增加滚动条美化 (可选) */
    scrollbar-width: thin;
    scrollbar-color: var(--border-color) transparent;
}

/* 新增：日期分组标题 */
.history-date-label {
    font-size: 0.75rem;
    font-weight: 600;
    color: var(--text-secondary);
    margin: 1.25rem 0 0.5rem 0.5rem;
    text-transform: uppercase;
    letter-spacing: 0.05em;
}

.history-item {
    padding: 0.75rem 0.875rem;
    margin-bottom: 0.25rem;
    border-radius: 0.5rem;
    cursor: pointer;
    color: var(--text-secondary);
    font-size: 0.9rem;
    transition: all 0.2s;
    border: 1px solid transparent;

    /*以此实现标题和删除按钮的布局*/
    display: flex;
    justify-content: space-between;
    align-items: center;
    /* 用于悬停检测 */
}

.history-item span {
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
    flex: 1;
}

.history-item:hover {
    background: var(--card-bg);
    color: var(--text-primary);
}

.history-item.active {
    background: var(--card-bg);
    color: var(--text-primary);
    border-color: var(--border-color);
    font-weight: 500;
}

/* 新增：删除按钮 */
.delete-chat-btn {
    opacity: 0;
    /* 默认隐藏 */
    background: transparent;
    border: none;
    color: var(--text-secondary);
    cursor: pointer;
    padding: 4px;
    border-radius: 4px;
    display: flex;
    align-items: center;
    transition: all 0.2s;
    margin-left: 8px;
}

.history-item:hover .delete-chat-btn {
    opacity: 1;
    /* 悬停时显示 */
}

.delete-chat-btn:hover {
    color: #ef4444;
    /* 红色 */
    background: rgba(239, 68, 68, 0.1);
}

/* Main Content */
.main-content {
    flex: 1;
    display: flex;
    flex-direction: column;
    height: 100%;
    position: relative;
    min-width: 0;
}

header {
    padding: 1rem 1.5rem;
    display: flex;
    align-items: center;
    background: rgba(9, 9, 11, 0.8);
    backdrop-filter: blur(8px);
    z-index: 10;
    border-bottom: 1px solid transparent;
}

.header-left {
    display: flex;
    align-items: center;
    gap: 1rem;
}

header h1 {
    font-size: 1.25rem;
    font-weight: 600;
    background: linear-gradient(to right, #fafafa, #a1a1aa);
    -webkit-background-clip: text;
    background-clip: text;
    -webkit-text-fill-color: transparent;
}

.icon-btn {
    background: transparent;
    border: none;
    color: var(--text-secondary);
    cursor: pointer;
    padding: 0.5rem;
    border-radius: 0.5rem;
    display: flex;
    align-items: center;
    justify-content: center;
    transition: all 0.2s;
}

.icon-btn:hover {
    background: var(--card-bg);
    color: var(--text-primary);
}

/* Chat Container */
#chat-container {
    flex: 1;
    overflow-y: auto;
    padding: 2rem;
    display: flex;
    flex-direction: column;
    gap: 1.25rem;
    max-width: 900px;
    margin: 0 auto;
    width: 100%;
    scroll-behavior: smooth;
    padding-bottom: 4rem;
    /* 增加底部间距，防止重试按钮被遮挡 */
}

.welcome-message {
    text-align: center;
    margin-top: 20vh;
    color: var(--text-secondary);
    animation: fadeIn 0.5s ease-out;
}

.welcome-message h3 {
    font-size: 2rem;
    margin-bottom: 1rem;
    color: var(--text-primary);
}

/* Messages */
.message {
    display: flex;
    gap: 0.75rem;
    max-width: 100%;
    animation: slideIn 0.3s ease-out;
    position: relative;
    transition: transform 0.2s ease;
    align-items: flex-start;
}

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

/* 头像样式优化 */
.avatar {
    width: 40px;
    height: 40px;
    border-radius: 50%;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 1.25rem;
    flex-shrink: 0;
    background: linear-gradient(135deg, #27272a 0%, #18181b 100%);
    border: 1px solid rgba(255, 255, 255, 0.08);
    box-shadow: 0 2px 8px rgba(0, 0, 0, 0.3);
    transition: transform 0.2s ease, box-shadow 0.2s ease;
}

.message:hover .avatar {
    transform: scale(1.05);
    box-shadow: 0 4px 12px rgba(0, 0, 0, 0.4);
}

.message.user .avatar {
    background: linear-gradient(135deg, #6366f1 0%, #4f46e5 50%, #4338ca 100%);
    color: white;
    border: none;
    box-shadow: 0 2px 12px rgba(99, 102, 241, 0.4);
}

.message.user:hover .avatar {
    box-shadow: 0 4px 16px rgba(99, 102, 241, 0.5);
}

/* 消息气泡样式优化 */
.message-content {
    padding: 1rem 1.25rem;
    border-radius: 1.25rem;
    font-size: 1rem;
    line-height: 1.7;
    max-width: 80%;
    position: relative;
    word-wrap: break-word;
    min-width: 120px;
    transition: transform 0.2s ease, box-shadow 0.2s ease;
    margin-top: 0;
}

/* 用户消息气泡 - 渐变紫色风格 */
.message.user .message-content {
    background: linear-gradient(135deg, #6366f1 0%, #4f46e5 50%, #4338ca 100%);
    color: white;
    border-top-right-radius: 0.375rem;
    box-shadow:
        0 4px 14px rgba(99, 102, 241, 0.35),
        0 2px 4px rgba(0, 0, 0, 0.1),
        inset 0 1px 0 rgba(255, 255, 255, 0.15);
}

.message.user:hover .message-content {
    transform: translateY(-1px);
    box-shadow:
        0 6px 20px rgba(99, 102, 241, 0.45),
        0 3px 6px rgba(0, 0, 0, 0.15),
        inset 0 1px 0 rgba(255, 255, 255, 0.2);
}

/* AI 消息气泡 - 玻璃拟态风格 */
.message.ai .message-content {
    background: linear-gradient(135deg, rgba(39, 39, 42, 0.95) 0%, rgba(24, 24, 27, 0.98) 100%);
    color: var(--text-primary);
    border-top-left-radius: 0.375rem;
    padding-bottom: 3rem;
    border: 1px solid rgba(255, 255, 255, 0.06);
    box-shadow:
        0 4px 16px rgba(0, 0, 0, 0.25),
        0 2px 4px rgba(0, 0, 0, 0.1),
        inset 0 1px 0 rgba(255, 255, 255, 0.05);
    backdrop-filter: blur(10px);
}

.message.ai:hover .message-content {
    transform: translateY(-1px);
    border-color: rgba(255, 255, 255, 0.1);
    box-shadow:
        0 8px 24px rgba(0, 0, 0, 0.35),
        0 3px 6px rgba(0, 0, 0, 0.15),
        inset 0 1px 0 rgba(255, 255, 255, 0.08);
}

/* 消息内图片样式 */
.message-content img {
    max-width: 300px;
    max-height: 300px;
    border-radius: 12px;
    margin-top: 12px;
    cursor: zoom-in;
    border: 1px solid rgba(255, 255, 255, 0.1);
    transition: all 0.3s ease;
    display: block;
    box-shadow: 0 4px 12px rgba(0, 0, 0, 0.3);
}

.message-content img:hover {
    transform: scale(1.03);
    box-shadow: 0 8px 24px rgba(0, 0, 0, 0.4);
    border-color: rgba(255, 255, 255, 0.2);
}

/* ===== 增强的 Markdown 渲染样式 ===== */

/* 段落与行距优化 */
.message-content p {
    margin: 0 0 0.65em 0;
    line-height: 1.6;
    letter-spacing: 0.01em;
}

.message-content p:last-child {
    margin-bottom: 0;
}

.message-content>*:first-child {
    margin-top: 0;
}

/* 代码块样式优化 */
.message-content pre {
    background: linear-gradient(135deg, #0d1117 0%, #161b22 100%);
    padding: 0.875rem 1rem;
    border-radius: 0.5rem;
    overflow-x: auto;
    margin: 0.75em 0;
    border: 1px solid rgba(255, 255, 255, 0.1);
    box-shadow: inset 0 1px 3px rgba(0, 0, 0, 0.3);
    position: relative;
}

.message-content pre code {
    font-family: 'JetBrains Mono', 'Fira Code', 'Monaco', 'Consolas', monospace;
    font-size: 0.85rem;
    line-height: 1.5;
    background: transparent;
    padding: 0;
    border-radius: 0;
    color: #e6edf3;
}

/* 行内代码 */
.message-content code {
    font-family: 'JetBrains Mono', 'Fira Code', 'Monaco', 'Consolas', monospace;
    font-size: 0.875em;
    background: rgba(110, 118, 129, 0.25);
    padding: 0.2em 0.45em;
    border-radius: 0.375rem;
    color: #e6edf3;
    word-break: break-word;
}

/* 标题样式 */
.message-content h1,
.message-content h2,
.message-content h3,
.message-content h4,
.message-content h5,
.message-content h6 {
    margin: 1em 0 0.5em 0;
    font-weight: 600;
    line-height: 1.35;
    color: var(--text-primary);
}

.message-content h1:first-child,
.message-content h2:first-child,
.message-content h3:first-child {
    margin-top: 0;
}

.message-content h1 {
    font-size: 1.4em;
}

.message-content h2 {
    font-size: 1.25em;
}

.message-content h3 {
    font-size: 1.1em;
}

.message-content h4 {
    font-size: 1.05em;
}

.message-content h5 {
    font-size: 1em;
}

.message-content h6 {
    font-size: 0.95em;
    color: var(--text-secondary);
}

/* 列表样式 */
.message-content ul,
.message-content ol {
    margin: 0.5em 0;
    padding-left: 1.5em;
    line-height: 1.6;
}

.message-content li {
    margin: 0.2em 0;
    padding-left: 0.15em;
}

.message-content li::marker {
    color: var(--accent-color);
}

.message-content ul ul,
.message-content ol ol,
.message-content ul ol,
.message-content ol ul {
    margin: 0.2em 0;
}

/* 嵌套列表的列表项不增加额外间距 */
.message-content li>ul,
.message-content li>ol {
    margin-top: 0.2em;
    margin-bottom: 0;
}

/* 引用块样式 */
.message-content blockquote {
    margin: 0.65em 0;
    padding: 0.5em 1em;
    border-left: 3px solid var(--accent-color);
    background: rgba(99, 102, 241, 0.06);
    border-radius: 0 0.375rem 0.375rem 0;
    color: var(--text-secondary);
    font-style: italic;
}

.message-content blockquote p {
    margin: 0;
}

.message-content blockquote p+p {
    margin-top: 0.5em;
}

/* 表格样式 */
.message-content table {
    width: 100%;
    border-collapse: collapse;
    margin: 0.75em 0;
    font-size: 0.9em;
    overflow-x: auto;
    display: block;
    border-radius: 0.375rem;
    border: 1px solid rgba(255, 255, 255, 0.12);
}

.message-content thead {
    background: rgba(99, 102, 241, 0.12);
}

.message-content th {
    padding: 0.6em 0.875em;
    text-align: left;
    font-weight: 600;
    color: var(--text-primary);
    border-bottom: 2px solid rgba(255, 255, 255, 0.15);
    white-space: nowrap;
}

.message-content td {
    padding: 0.5em 0.875em;
    border-bottom: 1px solid rgba(255, 255, 255, 0.08);
    color: var(--text-primary);
}

.message-content tbody tr:last-child td {
    border-bottom: none;
}

.message-content tbody tr:nth-child(even) {
    background: rgba(255, 255, 255, 0.02);
}

.message-content tbody tr:hover {
    background: rgba(99, 102, 241, 0.06);
}

/* 水平分隔线 */
.message-content hr {
    height: 1px;
    background: linear-gradient(to right, transparent, rgba(255, 255, 255, 0.15), transparent);
    border: none;
    margin: 1em 0;
}

/* 链接样式 */
.message-content a {
    color: #818cf8;
    text-decoration: none;
    border-bottom: 1px solid transparent;
    transition: all 0.2s ease;
}

.message-content a:hover {
    color: #a5b4fc;
    border-bottom-color: #a5b4fc;
}

/* 强调样式 */
.message-content strong {
    font-weight: 600;
    color: var(--text-primary);
}

.message-content em {
    font-style: italic;
    color: var(--text-secondary);
}

/* 删除线 */
.message-content del {
    color: var(--text-secondary);
    text-decoration: line-through;
}

/* KaTeX 数学公式样式 */
/* 块级公式包装器：独立一行，居中显示 */
.message-content .math-display-block {
    display: block !important;
    width: 100%;
    margin: 1.2em 0;
    text-align: center;
    overflow-x: auto;
    overflow-y: hidden;
    padding: 0.5em 0.5em;
    background: rgba(99, 102, 241, 0.06);
    border-radius: 0.625rem;
    border: 1px solid rgba(99, 102, 241, 0.12);
    box-shadow: inset 0 1px 3px rgba(0, 0, 0, 0.1);
}

/* 块级公式（display math）：居中显示，字体稍大 */
.message-content .math-display-block .katex-display,
.message-content .katex-display {
    display: block !important;
    margin: 0 auto !important;
    text-align: center !important;
    overflow-x: auto;
    overflow-y: hidden;
}

/* 块级公式内的 KaTeX 渲染：更大字号 */
.message-content .math-display-block .katex,
.message-content .katex-display>.katex,
.message-content .katex-display .katex {
    font-size: 1.2em !important;
}

/* 行内公式 */
.message-content .katex {
    font-size: 1.1em;
}

/* 行内公式在段落中略微减小字号 */
.message-content p .katex {
    font-size: 1.05em;
}

/* 清理空段落（display math 转换产生的） */
.message-content p:empty {
    display: none;
}

/* 任务列表 */
.message-content input[type="checkbox"] {
    margin-right: 0.5em;
    accent-color: var(--accent-color);
}

/* 图片样式优化 */
.message-content p>img {
    display: block;
    margin: 0.75em 0;
}

/* ===== 用户消息特殊样式覆盖 ===== */
/* 用户消息使用紫色背景和白色文字，需要覆盖某些样式 */
.message.user .message-content p,
.message.user .message-content li,
.message.user .message-content strong,
.message.user .message-content em {
    color: white;
}

.message.user .message-content a {
    color: rgba(255, 255, 255, 0.9);
    border-bottom-color: rgba(255, 255, 255, 0.4);
}

.message.user .message-content a:hover {
    color: white;
    border-bottom-color: white;
}

.message.user .message-content code {
    background: rgba(255, 255, 255, 0.2);
    color: white;
}

.message.user .message-content li::marker {
    color: rgba(255, 255, 255, 0.7);
}

/* 消息工具栏样式优化 */
.message-toolbar {
    position: absolute;
    bottom: 0.625rem;
    right: 0.625rem;
    display: flex;
    gap: 0.375rem;
    transition: all 0.2s ease;
    opacity: 0;
    transform: translateY(4px);
}

.message.ai:hover .message-toolbar {
    opacity: 1;
    transform: translateY(0);
}

/* 工具栏按钮样式优化 */
.toolbar-btn {
    background: rgba(255, 255, 255, 0.08);
    border: 1px solid rgba(255, 255, 255, 0.06);
    color: var(--text-secondary);
    font-size: 0.75rem;
    font-weight: 500;
    cursor: pointer;
    display: flex;
    align-items: center;
    gap: 0.375rem;
    padding: 0.4rem 0.75rem;
    border-radius: 0.5rem;
    transition: all 0.2s ease;
    backdrop-filter: blur(4px);
}

.toolbar-btn:hover {
    background: rgba(255, 255, 255, 0.15);
    border-color: rgba(255, 255, 255, 0.12);
    color: var(--text-primary);
    transform: translateY(-1px);
    box-shadow: 0 2px 8px rgba(0, 0, 0, 0.2);
}

.toolbar-btn:active {
    transform: translateY(0) scale(0.98);
}

.toolbar-btn svg {
    opacity: 0.8;
    transition: opacity 0.2s ease;
}

.toolbar-btn:hover svg {
    opacity: 1;
}

/* Footer & Input Area */
footer {
    padding: 1.5rem 2rem;
    background: transparent;
    display: flex;
    justify-content: center;
    position: relative;
}

.input-area {
    width: 100%;
    max-width: 900px;
    display: flex;
    flex-direction: column;
    gap: 0.5rem;
}

/* 新增：图片预览样式 */
.image-preview-container {
    display: flex;
    flex-wrap: wrap;
    gap: 0.75rem;
    padding: 0.75rem;
    background: var(--card-bg);
    border: 1px solid var(--border-color);
    border-radius: 1rem;
    margin-bottom: 0.5rem;
}

.preview-item {
    position: relative;
    width: 60px;
    height: 60px;
    border-radius: 0.5rem;
    overflow: hidden;
    border: 1px solid var(--border-color);
}

.preview-item img {
    width: 100%;
    height: 100%;
    object-fit: cover;
}

.preview-remove-btn {
    position: absolute;
    top: 2px;
    right: 2px;
    width: 16px;
    height: 16px;
    background: rgba(0, 0, 0, 0.6);
    color: white;
    border: none;
    border-radius: 50%;
    display: flex;
    align-items: center;
    justify-content: center;
    cursor: pointer;
    font-size: 10px;
}

.preview-remove-btn:hover {
    background: #ef4444;
}

.input-wrapper {
    display: flex;
    gap: 0.75rem;
    background: var(--card-bg);
    padding: 0.75rem 1rem;
    border-radius: 1.25rem;
    border: 1px solid var(--border-color);
    box-shadow: var(--shadow-lg);
    transition: border-color 0.2s;
    align-items: flex-end;
    /* 让按钮底部对齐 */
}

.input-wrapper:focus-within {
    border-color: var(--accent-color);
}

textarea {
    flex: 1;
    background: transparent;
    border: none;
    color: var(--text-primary);
    resize: none;
    padding: 0.5rem 0;
    outline: none;
    max-height: 200px;
    font-size: 1rem;
    line-height: 1.5;
}

#send-btn,
#upload-trigger-btn {
    background: transparent;
    border: none;
    width: 40px;
    height: 40px;
    border-radius: 0.75rem;
    display: flex;
    align-items: center;
    justify-content: center;
    cursor: pointer;
    transition: all 0.2s;
    flex-shrink: 0;
    color: var(--text-secondary);
}

#send-btn {
    background: var(--accent-color);
    color: white;
}

#send-btn:hover {
    background: var(--accent-hover);
    transform: scale(1.05);
}

#upload-trigger-btn:hover {
    background: rgba(255, 255, 255, 0.1);
    color: var(--text-primary);
}

#send-btn:disabled {
    opacity: 0.5;
    cursor: not-allowed;
    transform: none;
}

/* Menu Bar */
.menu-bar {
    display: flex;
    justify-content: flex-start;
    padding: 0 0.5rem;
}

select#model-select {
    background: var(--card-bg);
    border: 1px solid var(--border-color);
    color: var(--text-primary);
    padding: 0.5rem 1rem;
    border-radius: 0.75rem;
    cursor: pointer;
    font-size: 0.875rem;
    outline: none;
    transition: all 0.2s;
}

select#model-select:hover {
    border-color: var(--text-secondary);
}

/* Animations */
@keyframes fadeIn {
    from {
        opacity: 0;
        transform: translateY(10px);
    }

    to {
        opacity: 1;
        transform: translateY(0);
    }
}

@keyframes slideIn {
    from {
        opacity: 0;
        transform: translateY(20px);
    }

    to {
        opacity: 1;
        transform: translateY(0);
    }
}

/* Mobile Responsive - 移动端适配 */
@media (max-width: 768px) {

    /* 侧边栏移动端样式 */
    .sidebar {
        position: fixed;
        top: 0;
        left: 0;
        height: 100%;
        width: 85%;
        max-width: 320px;
        transform: translateX(-100%);
        box-shadow: var(--shadow-lg);
        z-index: 1000;
    }

    .sidebar.open {
        transform: translateX(0);
    }

    /* 侧边栏遮罩层样式（移动端生效） */
    .sidebar-overlay {
        position: fixed;
        top: 0;
        left: 0;
        width: 100%;
        height: 100%;
        background: rgba(0, 0, 0, 0.5);
        z-index: 999;
        opacity: 0;
        visibility: hidden;
        transition: opacity 0.3s ease, visibility 0.3s ease;
    }

    .sidebar-overlay.active {
        opacity: 1;
        visibility: visible;
    }

    .mobile-only {
        display: flex;
    }

    /* 头部样式 */
    header {
        padding: 0.75rem 1rem;
    }

    header h1 {
        font-size: 1.1rem;
    }

    /* 聊天区域移动端优化 */
    #chat-container {
        padding: 1rem 0.75rem;
        padding-bottom: 3rem;
        gap: 1rem;
    }

    /* 消息样式移动端优化 */
    .message {
        gap: 0.5rem;
    }

    .avatar {
        width: 32px;
        height: 32px;
        font-size: 1rem;
        flex-shrink: 0;
    }

    .message-content {
        max-width: 85%;
        padding: 0.875rem 1rem;
        font-size: 0.9375rem;
        line-height: 1.6;
        border-radius: 1rem;
    }

    .message.ai .message-content {
        padding-bottom: 2.5rem;
        border-top-left-radius: 0.25rem;
    }

    .message.user .message-content {
        border-top-right-radius: 0.25rem;
    }

    /* 消息内图片移动端优化 */
    .message-content img {
        max-width: 100%;
        max-height: 250px;
    }

    /* 代码块移动端优化 */
    .message-content pre {
        padding: 0.75rem;
        font-size: 0.8rem;
        margin: 0.5em 0;
    }

    .message-content pre code {
        font-size: 0.8rem;
    }

    .message-content code {
        font-size: 0.8125em;
        padding: 0.15em 0.35em;
    }

    /* 表格移动端优化 */
    .message-content table {
        font-size: 0.8rem;
    }

    .message-content th,
    .message-content td {
        padding: 0.4em 0.5em;
    }

    /* 数学公式移动端优化 */
    .message-content .math-display-block {
        padding: 0.4em 0.3em;
        margin: 0.8em 0;
    }

    .message-content .math-display-block .katex,
    .message-content .katex-display>.katex {
        font-size: 1.05em !important;
    }

    .message-content .katex {
        font-size: 1em;
    }

    /* 工具栏移动端优化 */
    .message-toolbar {
        opacity: 1;
        bottom: 0.5rem;
        right: 0.5rem;
    }

    .toolbar-btn {
        padding: 0.35rem 0.6rem;
        font-size: 0.7rem;
    }

    /* 用户消息操作按钮 */
    .user-message-actions {
        opacity: 1;
        margin-right: 4px;
        flex-direction: row;
        gap: 2px;
    }

    .user-action-btn {
        width: 28px;
        height: 28px;
    }

    /* 底部输入区域移动端优化 */
    footer {
        padding: 0.75rem 0.5rem;
        padding-bottom: calc(0.75rem + env(safe-area-inset-bottom, 0));
    }

    .input-area {
        gap: 0.375rem;
    }

    .input-wrapper {
        padding: 0.5rem 0.75rem;
        gap: 0.5rem;
        border-radius: 1rem;
    }

    textarea {
        font-size: 1rem;
        padding: 0.375rem 0;
    }

    #send-btn,
    #upload-trigger-btn {
        width: 36px;
        height: 36px;
    }

    #send-btn svg,
    #upload-trigger-btn svg {
        width: 18px;
        height: 18px;
    }

    /* 模型选择器移动端优化 */
    .menu-bar {
        padding: 0 0.25rem;
    }

    select#model-select {
        padding: 0.4rem 0.75rem;
        font-size: 0.8rem;
        border-radius: 0.6rem;
    }

    /* 图片预览容器移动端优化 */
    .image-preview-container {
        gap: 0.5rem;
        padding: 0.5rem;
        border-radius: 0.75rem;
    }

    .preview-item {
        width: 50px;
        height: 50px;
    }

    /* 编辑模式移动端优化 */
    .edit-container {
        max-width: 100%;
    }

    .edit-textarea {
        padding: 0.75rem 1rem;
        font-size: 0.9375rem;
        border-radius: 0.875rem;
    }

    .edit-actions {
        gap: 8px;
    }

    .edit-cancel-btn,
    .edit-update-btn {
        padding: 6px 14px;
        font-size: 0.85rem;
    }

    /* 欢迎消息移动端优化 */
    .welcome-message {
        margin-top: 15vh;
        padding: 0 1rem;
    }

    .welcome-message h3 {
        font-size: 1.5rem;
    }

    .welcome-message p {
        font-size: 0.9rem;
    }

    /* Toast 提示移动端优化 */
    .toast-message {
        bottom: 80px;
        padding: 10px 20px;
        font-size: 0.85rem;
        max-width: 90%;
    }

    /* 登录卡片移动端优化 */
    .login-card {
        padding: 1.75rem;
        margin: 1rem;
        max-width: 100%;
        border-radius: 1.25rem;
    }

    .login-card h2 {
        font-size: 1.3rem;
    }

    .login-card input {
        padding: 0.875rem;
    }

    .login-card button {
        padding: 0.875rem;
    }

    /* 侧边栏内部优化 */
    .sidebar-header {
        padding: 0.875rem 1rem;
    }

    .new-chat-primary-btn {
        padding: 0.625rem;
        font-size: 0.9rem;
    }

    .history-item {
        padding: 0.625rem 0.75rem;
        font-size: 0.85rem;
    }

    .history-date-label {
        font-size: 0.7rem;
        margin: 1rem 0 0.375rem 0.375rem;
    }

    /* 搜索面板移动端优化 */
    .search-panel {
        padding: 0.6rem 0.75rem;
    }

    .search-input-wrapper {
        padding: 0.4rem 0.6rem;
    }

    .search-panel input {
        font-size: 0.9rem;
    }
}

/* 超小屏幕适配 (小于 480px) */
@media (max-width: 480px) {
    .message-content {
        max-width: 88%;
        padding: 0.75rem 0.875rem;
        font-size: 0.9rem;
    }

    .avatar {
        width: 28px;
        height: 28px;
        font-size: 0.875rem;
    }

    header h1 {
        font-size: 1rem;
    }

    .input-wrapper {
        padding: 0.4rem 0.6rem;
    }

    #send-btn,
    #upload-trigger-btn {
        width: 34px;
        height: 34px;
    }

    .toolbar-btn {
        padding: 0.3rem 0.5rem;
        font-size: 0.65rem;
    }

    .message.ai .message-content {
        padding-bottom: 2.25rem;
    }
}

/* 横屏移动端适配 */
@media (max-width: 768px) and (orientation: landscape) {
    #chat-container {
        padding-bottom: 2rem;
    }

    .welcome-message {
        margin-top: 8vh;
    }

    footer {
        padding: 0.5rem;
    }
}

/* 桌面端样式 */
@media (min-width: 769px) {
    .mobile-only {
        display: none;
    }

    /* 确保侧边栏在桌面端正常显示 */
    .sidebar {
        position: relative;
        transform: none;
    }
}

.sidebar.collapsed {
    margin-left: -280px;
}

/* Error Text */
.error-text {
    color: #ef4444;
    font-weight: 500;
}

/* --- Custom Scrollbar (Dark Theme) --- */

/* For Webkit browsers (Chrome, Edge, Safari) */
::-webkit-scrollbar {
    width: 8px;
    height: 8px;
}

::-webkit-scrollbar-track {
    background: transparent;
}

::-webkit-scrollbar-thumb {
    background-color: var(--border-color);
    /* Zinc 800 */
    border-radius: 4px;
    border: 1px solid transparent;
    background-clip: content-box;
    /* 让滚动条这稍微留点白边，更精致 */
}

::-webkit-scrollbar-thumb:hover {
    background-color: var(--text-secondary);
    /* Zinc 400 */
}

/* For Firefox */
* {
    scrollbar-width: thin;
    scrollbar-color: var(--border-color) transparent;
}

/* --- Lightbox (灯箱) --- */
.lightbox-overlay {
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background: rgba(0, 0, 0, 0.9);
    z-index: 2000;
    display: flex;
    justify-content: center;
    align-items: center;
    opacity: 0;
    pointer-events: none;
    /* 隐藏时不阻挡点击 */
    transition: opacity 0.3s ease;
}

.lightbox-overlay.active {
    opacity: 1;
    pointer-events: auto;
}

.lightbox-content {
    max-width: 90%;
    max-height: 90%;
    position: relative;
}

.lightbox-image {
    max-width: 100%;
    max-height: 90vh;
    object-fit: contain;
    border-radius: 4px;
    box-shadow: 0 0 20px rgba(0, 0, 0, 0.5);
}

.lightbox-close {
    position: absolute;
    top: 20px;
    right: 30px;
    color: white;
    font-size: 40px;
    cursor: pointer;
    z-index: 2001;
    user-select: none;
}

.lightbox-nav {
    position: absolute;
    top: 50%;
    transform: translateY(-50%);
    color: white;
    font-size: 50px;
    cursor: pointer;
    padding: 20px;
    user-select: none;
    background: rgba(0, 0, 0, 0.2);
    border-radius: 5px;
    transition: background 0.2s;
}

.lightbox-nav:hover {
    background: rgba(0, 0, 0, 0.5);
}

.lightbox-prev {
    left: 30px;
}

.lightbox-next {
    right: 30px;
}

/* 移动端适配 */
@media (max-width: 768px) {
    .message-content img {
        max-width: 100%;
        /* 移动端可以稍大 */
    }

    .lightbox-nav {
        font-size: 30px;
        padding: 10px;
    }

    .lightbox-prev {
        left: 10px;
    }

    .lightbox-next {
        right: 10px;
    }
}

/* 停止生成按钮样式 */
#send-btn.stop-mode {
    background: #ef4444;
    animation: pulse-stop 1.5s ease-in-out infinite;
}

#send-btn.stop-mode:hover {
    background: #dc2626;
    transform: scale(1.05);
}

@keyframes pulse-stop {

    0%,
    100% {
        box-shadow: 0 0 0 0 rgba(239, 68, 68, 0.4);
    }

    50% {
        box-shadow: 0 0 0 6px rgba(239, 68, 68, 0);
    }
}

/* === 用户消息操作按钮 === */
.user-message-actions {
    display: flex;
    flex-direction: column;
    gap: 4px;
    opacity: 0;
    transition: opacity 0.2s ease;
    margin-right: 8px;
    align-self: flex-end;
}

.message.user:hover .user-message-actions {
    opacity: 1;
}

.user-action-btn {
    background: rgba(255, 255, 255, 0.1);
    border: none;
    color: var(--text-secondary);
    width: 32px;
    height: 32px;
    border-radius: 8px;
    display: flex;
    align-items: center;
    justify-content: center;
    cursor: pointer;
    transition: all 0.2s ease;
}

.user-action-btn:hover {
    background: rgba(255, 255, 255, 0.2);
    color: var(--text-primary);
    transform: scale(1.05);
}

/* === 编辑模式样式 === */
.edit-container {
    display: flex;
    flex-direction: column;
    gap: 12px;
    flex: 1;
    max-width: 80%;
}

.edit-textarea {
    width: 100%;
    background: var(--bg-color);
    border: 2px solid var(--accent-color);
    color: var(--text-primary);
    border-radius: 1rem;
    padding: 1rem 1.25rem;
    font-size: 1rem;
    line-height: 1.6;
    resize: none;
    outline: none;
    min-height: 50px;
    max-height: 300px;
    font-family: inherit;
    transition: border-color 0.2s;
}

.edit-textarea:focus {
    border-color: var(--accent-hover);
    box-shadow: 0 0 0 3px rgba(99, 102, 241, 0.2);
}

.edit-actions {
    display: flex;
    justify-content: flex-end;
    gap: 12px;
}

.edit-cancel-btn,
.edit-update-btn {
    padding: 8px 20px;
    border-radius: 8px;
    font-size: 0.9rem;
    font-weight: 500;
    cursor: pointer;
    transition: all 0.2s ease;
    border: none;
}

.edit-cancel-btn {
    background: transparent;
    color: var(--text-secondary);
}

.edit-cancel-btn:hover {
    color: var(--text-primary);
    background: rgba(255, 255, 255, 0.1);
}

.edit-update-btn {
    background: var(--ai-msg-bg);
    color: var(--text-primary);
}

.edit-update-btn:hover {
    background: var(--border-color);
    transform: translateY(-1px);
}

/* === Toast 提示 === */
.toast-message {
    position: fixed;
    bottom: 100px;
    left: 50%;
    transform: translateX(-50%);
    background: var(--card-bg);
    color: var(--text-primary);
    padding: 12px 24px;
    border-radius: 8px;
    font-size: 0.9rem;
    box-shadow: var(--shadow-lg);
    border: 1px solid var(--border-color);
    z-index: 3000;
    animation: toast-in 0.3s ease;
}

.toast-message.fade-out {
    animation: toast-out 0.3s ease forwards;
}

@keyframes toast-in {
    from {
        opacity: 0;
        transform: translateX(-50%) translateY(20px);
    }

    to {
        opacity: 1;
        transform: translateX(-50%) translateY(0);
    }
}

@keyframes toast-out {
    from {
        opacity: 1;
        transform: translateX(-50%) translateY(0);
    }

    to {
        opacity: 0;
        transform: translateX(-50%) translateY(-20px);
    }
}

/* 移动端适配 */
@media (max-width: 768px) {
    .user-message-actions {
        opacity: 1;
        /* 移动端始终显示 */
    }

    .edit-container {
        max-width: 100%;
    }
}

/* ===== Mermaid 图表样式 ===== */
.mermaid-container {
    display: block;
    width: 100%;
    margin: 1em 0;
    padding: 1rem;
    background: linear-gradient(135deg, rgba(15, 23, 42, 0.95) 0%, rgba(30, 41, 59, 0.9) 100%);
    border-radius: 0.75rem;
    border: 1px solid rgba(99, 102, 241, 0.2);
    box-shadow:
        0 4px 12px rgba(0, 0, 0, 0.2),
        inset 0 1px 0 rgba(255, 255, 255, 0.05);
    overflow-x: auto;
    text-align: center;
}

.mermaid-container svg {
    max-width: 100%;
    height: auto !important;
}

/* Mermaid 加载状态 */
.mermaid-loading {
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 0.5rem;
    padding: 2rem;
    color: var(--text-secondary);
    font-size: 0.875rem;
}

.mermaid-loading::before {
    content: '';
    width: 16px;
    height: 16px;
    border: 2px solid rgba(99, 102, 241, 0.3);
    border-top-color: var(--accent-color);
    border-radius: 50%;
    animation: mermaid-spin 0.8s linear infinite;
}

@keyframes mermaid-spin {
    to {
        transform: rotate(360deg);
    }
}

/* Mermaid 错误状态 */
.mermaid-error {
    padding: 1rem;
    background: rgba(239, 68, 68, 0.1);
    border: 1px solid rgba(239, 68, 68, 0.3);
    border-radius: 0.5rem;
    color: #f87171;
    font-size: 0.875rem;
    text-align: left;
}

.mermaid-error-title {
    font-weight: 600;
    margin-bottom: 0.5rem;
    display: flex;
    align-items: center;
    gap: 0.5rem;
}

.mermaid-error-title::before {
    content: '⚠️';
}

.mermaid-error code {
    display: block;
    margin-top: 0.5rem;
    padding: 0.5rem;
    background: rgba(0, 0, 0, 0.3);
    border-radius: 0.25rem;
    font-family: 'JetBrains Mono', monospace;
    font-size: 0.8rem;
    white-space: pre-wrap;
    color: #fca5a5;
}

/* 移动端 Mermaid 适配 */
@media (max-width: 768px) {
    .mermaid-container {
        padding: 0.75rem;
        margin: 0.75em 0;
    }

    .mermaid-container svg {
        min-width: unset;
    }
}