/* 根变量定义区域，用于设置整个聊天界面的主题样式 */
:root {
    --primary-color: #2563eb;
    --hover-color: #1d4ed8;
    --border-radius: 12px;
    --sidebar-width: 280px;
    --button-gradient: linear-gradient(135deg, #2563eb 0%, #1d4ed8 100%);
}

/* 重置和基础样式 */
* {
    margin: 0;
    padding: 0;
    box-sizing: border-box;
}

body {
    font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
    height: 100vh;
    /* 添加稳定的字体渲染和行高 */
    -webkit-font-smoothing: antialiased;
    -moz-osx-font-smoothing: grayscale;
    font-size: 14px;
    line-height: 1.5;
    /* 防止语言切换时的布局跳动 */
    font-feature-settings: 'kern' 1;
    text-rendering: optimizeLegibility;
}

/* 为中英文混合文本提供统一的字体回退 */
body, input, button, select, textarea {
    font-family: 
        /* 英文字体 */
        -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, 'Helvetica Neue', Arial,
        /* 中文字体 */
        'PingFang SC', 'Hiragino Sans GB', 'Microsoft YaHei', 'WenQuanYi Micro Hei',
        /* 通用回退 */
        sans-serif;
    /* 统一字体大小和行高 */
    font-size: inherit;
    line-height: inherit;
}

/* 确保所有文本元素有一致的字体渲染 */
.navbar-brand, .navbar-controls, .chat-title, .message-content, 
.form-input, .auth-btn, .language-select {
    font-weight: normal;
    letter-spacing: 0;
    /* 防止文本溢出导致的布局变化 */
    word-break: break-word;
    overflow-wrap: break-word;
}

/* 为关键布局元素添加最小高度，防止内容变化时的跳动 */
.navbar {
    min-height: 60px;
    height: 60px;
}

.chat-item {
    min-height: 60px;
    /* 确保悬停状态下的高度稳定 */
    transition: background-color 0.2s ease, transform 0.1s ease;
}

.message-content {
    /* 确保消息内容的稳定渲染 */
    min-height: 1.5em;
    word-wrap: break-word;
    hyphens: auto;
}

/* 语言选择器样式优化 */
.language-select {
    /* 固定宽度防止选项变化时的布局跳动 */
    min-width: 100px;
    width: auto;
    /* 统一的内边距 */
    padding: 6px 12px;
    /* 防止选项过长时的布局问题 */
    max-width: 120px;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}

/* 认证页面样式 */
.auth-body {
    background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
    overflow: hidden;
}

.chat-body {
    background: #f8fafc;
    height: 100vh;
    overflow: hidden;
}

#auth-page {
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    display: flex;
    align-items: center;
    justify-content: center;
    background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
}

.auth-container {
    width: 100%;
    max-width: 400px;
    padding: 20px;
}

.auth-card {
    background: white;
    border-radius: 16px;
    padding: 40px;
    box-shadow: 0 20px 40px rgba(0, 0, 0, 0.1);
}

.auth-header {
    text-align: center;
    margin-bottom: 30px;
}

.auth-title {
    font-size: 32px;
    font-weight: 700;
    color: #1a1a1a;
    margin-bottom: 8px;
}

.auth-subtitle {
    color: #666;
    font-size: 14px;
    margin: 0;
}

.auth-language-selector {
    margin-top: 20px;
    text-align: center;
}

.auth-language-selector .language-select {
    padding: 8px 12px;
    border: 1px solid #d1d5db;
    border-radius: 6px;
    font-size: 14px;
    background: white;
    cursor: pointer;
    transition: border-color 0.2s;
}

.auth-language-selector .language-select:focus {
    outline: none;
    border-color: #2563eb;
    box-shadow: 0 0 0 3px rgba(37, 99, 235, 0.1);
}

.form-title {
    font-size: 24px;
    font-weight: 600;
    color: #1a1a1a;
    margin-bottom: 24px;
    text-align: center;
}

.form-group {
    margin-bottom: 20px;
}

.form-group label {
    display: block;
    margin-bottom: 6px;
    font-weight: 500;
    color: #374151;
}

.form-input {
    width: 100%;
    padding: 12px 16px;
    border: 2px solid #e5e7eb;
    border-radius: 8px;
    font-size: 16px;
    transition: border-color 0.2s;
}

.form-input:focus {
    outline: none;
    border-color: #2563eb;
    box-shadow: 0 0 0 3px rgba(37, 99, 235, 0.1);
}

.auth-btn {
    width: 100%;
    padding: 12px 24px;
    border: none;
    border-radius: 8px;
    font-size: 16px;
    font-weight: 500;
    cursor: pointer;
    transition: all 0.2s;
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 8px;
    margin-bottom: 12px;
}

.auth-btn.primary {
    background: var(--button-gradient);
    color: white;
}

.auth-btn.primary:hover {
    transform: translateY(-2px);
    box-shadow: 0 4px 12px rgba(37, 99, 235, 0.3);
}

.auth-btn.secondary {
    background: #f3f4f6;
    color: #374151;
}

.auth-btn.secondary:hover {
    background: #e5e7eb;
}

.auth-divider {
    text-align: center;
    margin: 20px 0;
    color: #6b7280;
    font-size: 14px;
}

.auth-message {
    margin-bottom: 20px;
}

.alert {
    padding: 12px 16px;
    border-radius: 8px;
    display: flex;
    align-items: center;
    gap: 8px;
}

.alert.success {
    background: #ecfdf5;
    color: #065f46;
    border: 1px solid #a7f3d0;
}

.alert.error {
    background: #fef2f2;
    color: #991b1b;
    border: 1px solid #fca5a5;
}

/* 聊天页面主体样式 */
#chat-page {
    height: 100vh;
    display: flex;
    flex-direction: column;
    overflow: hidden;
}

/* 顶部导航栏样式 */
.navbar {
    position: sticky;
    top: 0;
    background: white;
    z-index: 100;
    border-bottom: 1px solid #e5e7eb;
    height: 60px;
    min-height: 60px;
    flex-shrink: 0;
    padding: 12px 24px;
    display: flex;
    align-items: center;
    box-shadow: 0 1px 3px rgba(0, 0, 0, 0.1);
}

.navbar-content {
    display: flex;
    align-items: center;
    justify-content: space-between;
    width: 100%;
}

.navbar-brand {
    font-size: 24px;
    font-weight: 700;
    color: var(--primary-color);
}

.navbar-controls {
    display: flex;
    align-items: center;
    gap: 20px;
}

.language-select {
    padding: 6px 12px;
    border: 1px solid #d1d5db;
    border-radius: 6px;
    font-size: 14px;
    background: white;
    cursor: pointer;
    transition: border-color 0.2s;
    /* 继承优化的布局样式 */
    min-width: 100px;
    max-width: 120px;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}

.stream-toggle, .thoughts-toggle {
    display: flex;
    align-items: center;
    gap: 8px;
    font-size: 14px;
}

.input-options {
    display: flex;
    justify-content: flex-end;
    padding: 8px 0;
    gap: 20px;
    margin-bottom: 8px;
    border-bottom: 1px solid #e5e7eb;
}

.user-menu {
    display: flex;
    align-items: center;
    gap: 12px;
}

.user-name {
    font-weight: 500;
    color: #374151;
}

.logout-btn {
    background: #dc2626;
    color: white;
    border: none;
    padding: 6px 12px;
    border-radius: 6px;
    cursor: pointer;
    font-size: 14px;
    display: flex;
    align-items: center;
    gap: 4px;
    transition: background 0.2s;
}

.logout-btn:hover {
    background: #b91c1c;
}

/* 主要内容区域 */
.content-wrapper {
    flex: 1;
    display: flex;
    min-height: 0;
    height: calc(100vh - 60px);
}

/* 侧边栏样式 */
.sidebar {
    width: var(--sidebar-width);
    background: white;
    border-right: 1px solid #e5e7eb;
    padding: 20px;
    display: flex;
    flex-direction: column;
    height: 100%;
    overflow: hidden;
}

.new-chat-btn {
    background: var(--button-gradient);
    color: white;
    border: none;
    padding: 12px 16px;
    border-radius: 8px;
    cursor: pointer;
    display: flex;
    align-items: center;
    gap: 8px;
    font-weight: 500;
    margin-bottom: 20px;
    transition: all 0.2s;
}

.new-chat-btn:hover {
    transform: translateY(-2px);
    box-shadow: 0 4px 12px rgba(37, 99, 235, 0.3);
}

.chat-list {
    flex: 1;
    overflow-y: auto;
}

.chat-item {
    padding: 12px 16px;
    margin: 4px 0;
    border-radius: 8px;
    cursor: pointer;
    transition: background 0.2s;
    display: flex;
    align-items: center;
    gap: 12px;
    position: relative;
    z-index: 1;
}

.chat-item:hover {
    background: #f3f4f6;
    z-index: 5;
}

.chat-item.active {
    background: #eff6ff;
    color: var(--primary-color);
}

.chat-title {
    flex: 1;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}

.chat-time {
    color: #9ca3af;
    font-size: 12px;
}

/* 操作按钮容器 */
.chat-actions {
    display: none;
    align-items: center;
    gap: 4px;
    position: absolute;
    right: 8px;
    top: 50%;
    transform: translateY(-50%);
    background: white;
    border-radius: 6px;
    padding: 4px;
    box-shadow: 0 2px 8px rgba(0, 0, 0, 0.15);
    z-index: 10;
    border: 1px solid #e5e7eb;
}

.chat-item:hover .chat-actions {
    display: flex !important;
}

.action-btn {
    background: none;
    border: none;
    padding: 6px;
    border-radius: 4px;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    width: 28px;
    height: 28px;
    transition: all 0.2s;
    color: #6b7280;
    z-index: 11;
}

.action-btn:hover {
    background: #f3f4f6;
    transform: scale(1.1);
}

.rename-btn:hover {
    background: #dbeafe !important;
    color: #2563eb !important;
}

.delete-btn:hover {
    background: #fee2e2 !important;
    color: #dc2626 !important;
}

.action-btn i {
    font-size: 12px;
    pointer-events: none;
}

/* 主聊天区域 */
.main-content {
    flex: 1;
    display: flex;
    flex-direction: column;
    min-height: 0;
    position: relative;
}

#chat-container {
    flex: 1;
    overflow-y: auto;
    padding: 20px;
    padding-bottom: 140px;
    max-height: calc(100vh - 60px - 100px);
    scroll-behavior: smooth;
}

/* 消息样式 */
.message {
    display: flex;
    margin: 20px 0;
    align-items: flex-start;
}

.user-message {
    justify-content: flex-end;
}

.message-content {
    max-width: 70%;
    padding: 12px 16px;
    border-radius: 12px;
    line-height: 1.6;
    word-wrap: break-word;
    white-space: pre-wrap;
}

.user-message .message-content {
    background: var(--primary-color);
    color: white;
}

.ai-message .message-content {
    background: #f3f4f6;
    color: #1f2937;
}

/* 引用样式 */
.reference-container {
    width: 75%;
    margin-left: 0;
    margin-top: -16px;
    margin-bottom: 24px;
}

.reference-list {
    margin-top: 10px;
}

.reference-item {
    background: #fff;
    border: 1px solid #ddd;
    border-radius: 8px;
    margin-bottom: 10px;
    padding: 12px 16px;
}

.ref-header {
    display: flex;
    align-items: flex-start;
    gap: 8px;
    width: 100%;
}

.ref-number {
    color: var(--primary-color);
    font-weight: 500;
    flex-shrink: 0;
}

.ref-content {
    flex: 1;
}

.reference-title {
    font-weight: 600;
    color: #2c3e50;
    margin-bottom: 5px;
    padding-bottom: 5px;
    border-bottom: 1px solid #eee;
}

.reference-content {
    color: #333;
    line-height: 1.5;
    white-space: pre-wrap;
    word-wrap: break-word;
}

.expand-btn {
    background: none;
    border: none;
    color: var(--primary-color);
    cursor: pointer;
    padding: 0;
    flex-shrink: 0;
    transition: transform 0.3s ease;
    padding-top: 2px;
}

.expand-btn.expanded {
    transform: rotate(180deg);
}

.expand-btn i {
    font-size: 0.8em;
}

.ref-meta {
    display: flex;
    align-items: center;
    gap: 12px;
    margin-top: 8px;
    padding-left: 26px;
}

.ref-similarity {
    color: #64748b;
    font-size: 0.85em;
}

/* 加载动画样式 */
.loading-message .message-content {
    display: flex;
    align-items: center;
    gap: 12px;
    background: #f9fafb;
    border: 1px solid #e5e7eb;
}

.loading-spinner {
    width: 20px;
    height: 20px;
    border: 2px solid #e5e7eb;
    border-top: 2px solid var(--primary-color);
    border-radius: 50%;
    animation: spin 1s linear infinite;
}

@keyframes spin {
    0% { transform: rotate(0deg); }
    100% { transform: rotate(360deg); }
}

.loading-text {
    font-size: 14px;
    color: #6b7280;
}

/* 输入区域样式 */
.input-container {
    position: fixed;
    bottom: 0;
    right: 0;
    left: var(--sidebar-width);
    background: white;
    border-top: 1px solid #e5e7eb;
    padding: 20px;
}

.input-wrapper {
    display: flex;
    gap: 12px;
    max-width: 1000px;
    margin: 0 auto;
    align-items: flex-end;
}

.model-select {
    padding: 12px 5px;
    border: 2px solid #e5e7eb;
    border-radius: 8px;
    font-size: 14px;
    width: 120px;
}

#user-input {
    flex: 1;
    min-height: 48px;
    max-height: 120px;
    padding: 12px 16px;
    border: 2px solid #e5e7eb;
    border-radius: 8px;
    resize: vertical;
    font-size: 16px;
    line-height: 1.5;
    transition: border-color 0.2s;
}

#user-input:focus,
.model-select:focus {
    outline: none;
    border-color: var(--primary-color);
    box-shadow: 0 0 0 3px rgba(37, 99, 235, 0.1);
}

.send-btn {
    background: var(--button-gradient);
    color: white;
    border: none;
    padding: 12px 24px;
    border-radius: 8px;
    cursor: pointer;
    display: flex;
    align-items: center;
    gap: 8px;
    font-weight: 500;
    transition: all 0.2s;
}

.send-btn:hover {
    transform: translateY(-2px);
    box-shadow: 0 4px 12px rgba(37, 99, 235, 0.3);
}

.send-btn:disabled {
    background: #9ca3af;
    cursor: not-allowed;
    transform: none;
    box-shadow: none;
}

/* 响应式设计 */
@media (max-width: 768px) {
    .sidebar {
        display: none;
    }

    .input-container {
        left: 0;
    }

    .message-content {
        max-width: 85%;
    }

    .navbar-controls {
        gap: 10px;
    }

    .navbar-brand {
        font-size: 20px;
    }

    .user-menu {
        gap: 8px;
    }

    /* 移动端聊天容器滚动调整 */
    #chat-container {
        max-height: calc(100vh - 60px - 120px); /* 移动端调整高度计算 */
        padding-bottom: 160px; /* 移动端为输入框留出更多空间 */
    }

    /* 移动端参考资料样式调整 */
    .message-thoughts {
        margin: -10px 0 20px 12px; /* 在移动端减少左边距 */
        max-width: 85%; /* 与消息内容保持一致的最大宽度 */
    }
}

/* 思考过程和引用样式 */
.message-thoughts {
    margin: -10px 0 20px 0;
    max-width: 70%;
    font-size: 0.9em;
    color: #666;
}

.message-thoughts details {
    background: #f5f5f5;
    border-radius: 8px;
    padding: 12px;
    border: 1px solid #e5e7eb;
}

.message-thoughts summary {
    cursor: pointer;
    padding: 8px;
    font-weight: 500;
    color: #333;
    border-radius: 4px;
    transition: background-color 0.2s;
}

.message-thoughts summary:hover {
    background: #e9e9e9;
}

.reference-list {
    margin-top: 12px;
}

.reference-item {
    background: #fff;
    border: 1px solid #ddd;
    border-radius: 6px;
    margin-bottom: 8px;
    padding: 12px;
}

.reference-title {
    font-weight: 600;
    color: #2c3e50;
    margin-bottom: 6px;
    padding-bottom: 6px;
    border-bottom: 1px solid #eee;
}

.reference-subtitle {
    font-weight: 500;
    color: #5a6c7d;
    margin-bottom: 8px;
    font-size: 0.95em;
    font-style: italic;
}

.reference-content {
    color: #333;
    line-height: 1.5;
    white-space: pre-wrap;
    word-wrap: break-word;
    background: #fafafa;
    padding: 10px;
    border-radius: 4px;
    border-left: 3px solid #2563eb;
    margin-top: 8px;
}

/* 流式输出样式 */
.streaming-message .message-content {
    position: relative;
}

.streaming-message .streaming-cursor {
    display: inline-block;
    width: 2px;
    height: 1.2em;
    background: var(--primary-color);
    margin-left: 2px;
    animation: blink 1s infinite;
    vertical-align: text-bottom;
}

/* 确保非流式消息中的光标不显示 */
.message:not(.streaming-message) .streaming-cursor {
    display: none;
}

@keyframes blink {
    0%, 50% { opacity: 1; }
    51%, 100% { opacity: 0; }
}