* {
    box-sizing: border-box;
    margin: 0;
    padding: 0;
}

html,
body {
    height: 100%;
    overflow: hidden;
    /* 防止系统滚动条，应用自身负责滚动 */
    font-family: system-ui, -apple-system, 'Segoe UI', Roboto, sans-serif;
    background: #f0f2f5;
}

details {
    background: #f8f9fa;
    border-radius: 12px;
    padding: 8px 12px;
    margin-bottom: 8px;
}

summary {
    font-weight: bold;
    cursor: pointer;
    color: #2c3e50;
}

details[open] summary {
    margin-bottom: 8px;
}

#app,
.app-container {
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    height: 100dvh;
    /* 动态视口高度，键盘弹出时自动缩小 */
    display: flex;
    flex-direction: column;
}

.mobile-header {
    display: none;
    background: #1a2a3f;
    color: white;
    padding: 12px 16px;
    justify-content: space-between;
    align-items: center;
}

.menu-btn {
    background: none;
    border: none;
    color: white;
    font-size: 24px;
    cursor: pointer;
}

.main-layout {
    display: flex;
    flex: 1;
    min-height: 0;
    overflow: hidden;
}

.sidebar {
    width: 280px;
    background: #f8f9fa;
    border-right: 1px solid #e9ecef;
    display: flex;
    flex-direction: column;
    overflow-y: auto;
}

.sidebar-header {
    padding: 20px;
    font-weight: bold;
    border-bottom: 1px solid #dee2e6;
}

.new-chat-btn {
    margin: 16px;
    background: #1a2a3f;
    color: white;
    border: none;
    padding: 10px;
    border-radius: 12px;
    cursor: pointer;
}

.conversation-list {
    flex: 1;
    overflow-y: auto;
    padding: 0 12px;
}

.conv-item {
    padding: 12px;
    border-radius: 12px;
    margin-bottom: 8px;
    cursor: pointer;
    background: white;
    border: 1px solid #e9ecef;
    display: flex;
    justify-content: space-between;
    align-items: center;
}

.conv-item.active {
    background: #e7f1ff;
    border-color: #1a2a3f;
}

.conv-title {
    flex: 1;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

.delete-conv {
    color: #dc2626;
    background: none;
    border: none;
    font-size: 18px;
    cursor: pointer;
}

.chat-area {
    flex: 1;
    display: flex;
    flex-direction: column;
    min-height: 0;
    min-width: 0;
    /* 新增：防止被内容撑开 */
    background: white;
}

.chat-header {
    padding: 16px 24px;
    border-bottom: 1px solid #e9ecef;
    display: flex;
    justify-content: space-between;
    align-items: center;
    background: white;
    flex-wrap: wrap;
    gap: 12px;
}

.settings {
    font-size: 12px;
    display: flex;
    gap: 12px;
    align-items: center;
}

.messages {
    flex: 1;
    overflow-y: auto;
    overflow-x: hidden;
    /* 新增：强制裁剪横向溢出 */
    width: 100%;
    min-width: 0;
    /* 新增：允许收缩 */
    min-height: 0;
    padding: 20px;
    background: #fafcff;
}

.message {
    margin-bottom: 20px;
    display: flex;
    max-width: 100%;
}

.user-message {
    justify-content: flex-end;
}

.assistant-message {
    justify-content: flex-start;
}

.bubble {
    max-width: 80%;
    overflow-x: auto;
    padding: 12px 18px;
    border-radius: 24px;
    line-height: 1.5;
    word-wrap: break-word;
    word-break: break-word;
    overflow-wrap: break-word;
    white-space: normal;
}

.bubble .katex {
    max-width: 100%;
    overflow-x: auto;
    overflow-y: hidden;
    padding-bottom: 2px;
}

.user-message .bubble {
    background: #1a2a3f;
    color: white;
}

.assistant-message .bubble {
    background: #e9eef3;
    color: #0f172a;
}

.input-area {
    min-width: 0;
    flex-shrink: 0;
    padding: 16px 20px;
    background: white;
    border-top: 1px solid #e9ecef;
    display: flex;
    gap: 12px;
    align-items: flex-end;
}

textarea {
    flex: 1;
    padding: 12px;
    border: 1px solid #ced4da;
    border-radius: 24px;
    resize: none;
    font-family: inherit;
    font-size: 0.95rem;
    outline: none;
}

button {
    background: #1a2a3f;
    color: white;
    border: none;
    border-radius: 40px;
    padding: 10px 20px;
    cursor: pointer;
}

button:disabled {
    opacity: 0.6;
}

input[type="range"]:disabled {
    opacity: 0.4;
    cursor: not-allowed;
}

/* ========== OCR 预览卡片 ========== */
.ocr-preview {
    margin: 0 20px 8px;
    padding: 10px 12px;
    background: #f0f7ff;
    border: 1px solid #b8d4f0;
    border-radius: 12px;
    display: flex;
    gap: 12px;
    align-items: flex-start;
    max-height: 160px;
    overflow: hidden;
    flex-shrink: 0;
}

.ocr-thumb {
    width: 64px;
    height: 64px;
    object-fit: cover;
    border-radius: 8px;
    flex-shrink: 0;
    cursor: pointer;
    border: 2px solid transparent;
    transition: border-color 0.2s;
}
.ocr-thumb:hover {
    border-color: #dc2626;
}

.ocr-text-list {
    flex: 1;
    overflow-y: auto;
    max-height: 130px;
    font-size: 0.82rem;
    line-height: 1.5;
    min-width: 0;
}

.ocr-text-line {
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
    padding: 1px 0;
}

.ocr-conf {
    display: inline-block;
    width: 36px;
    font-size: 0.7rem;
    font-weight: 600;
    text-align: right;
    margin-right: 6px;
    flex-shrink: 0;
}

.ocr-close-btn {
    flex-shrink: 0;
    background: none;
    border: none;
    font-size: 1.1rem;
    color: #999;
    cursor: pointer;
    padding: 0 4px;
    line-height: 1;
    border-radius: 4px;
}
.ocr-close-btn:hover {
    color: #dc2626;
    background: #fee2e2;
}

.ocr-loading {
    display: flex;
    align-items: center;
    gap: 10px;
    padding: 12px 0;
    width: 100%;
    color: #666;
    font-size: 0.9rem;
}

.ocr-spinner {
    width: 20px;
    height: 20px;
    border: 3px solid #e0e0e0;
    border-top-color: #1a2a3f;
    border-radius: 50%;
    animation: ocr-spin 0.8s linear infinite;
    flex-shrink: 0;
}

@keyframes ocr-spin {
    to { transform: rotate(360deg); }
}

.ocr-error {
    width: 100%;
    color: #dc2626;
    font-size: 0.85rem;
    display: flex;
    align-items: center;
    gap: 8px;
    padding: 4px 0;
}

.ocr-retry-btn {
    background: none;
    border: 1px solid #dc2626;
    color: #dc2626;
    padding: 2px 10px;
    border-radius: 12px;
    cursor: pointer;
    font-size: 0.8rem;
}

/* ========== 用户消息气泡中的 OCR 标记 ========== */
.ocr-badge {
    font-size: 0.7rem;
    opacity: 0.55;
    margin-bottom: 4px;
    user-select: none;
}

/* ========== 编辑模式中的 OCR 块 ========== */
.ocr-edit-block {
    background: #f0f7ff;
    border: 1px solid #b8d4f0;
    border-radius: 10px;
    padding: 10px;
    margin-bottom: 10px;
    font-size: 0.82rem;
    max-height: 120px;
    overflow-y: auto;
}

.ocr-edit-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-bottom: 6px;
    font-weight: 600;
    color: #1a2a3f;
}

.ocr-edit-header button {
    font-size: 0.75rem;
    padding: 2px 10px;
    background: none;
    border: 1px solid #dc2626;
    color: #dc2626;
    border-radius: 12px;
    cursor: pointer;
}
.ocr-edit-header button:hover {
    background: #fee2e2;
}

/* ========== OCR 上传按钮 ========== */
#ocrBtn {
    background: #f0f2f5;
    color: #1a2a3f;
    font-size: 1.15rem;
    padding: 10px 14px;
    border-radius: 40px;
    flex-shrink: 0;
    transition: background 0.2s;
}
#ocrBtn:hover:not(:disabled) {
    background: #e0e4e8;
}
#ocrBtn:disabled {
    opacity: 0.4;
    cursor: not-allowed;
}

/* 登录/注册卡片容器 */
.login-container {
    width: 100%;
    max-width: 400px;
    /* 最大宽度，可改为 360px 或 450px */
    margin: auto;
    /* 水平居中 */
    background: white;
    padding: 30px 40px 40px;
    /* 上 右 下 左，可根据需要调整 */
    border-radius: 24px;
    text-align: center;
    box-shadow: 0 4px 12px rgba(0, 0, 0, 0.1);

    /* 使用 flex 布局垂直居中内容（也可以改为 flex-start 让内容靠上） */
    display: flex;
    flex-direction: column;
    justify-content: center;
    /* 垂直居中；若想靠上改为 flex-start */
    min-height: 460px;
    /* 固定最小高度，防止太扁，可根据实际调整 */
}

/* 标题 "DeepSeek 聊天室" */
.login-container h2 {
    margin-top: 0;
    margin-bottom: 24px;
    /* 标题与下方账号框的距离 */
    font-size: 1.8rem;
    /* 标题字体大小，可改 */
    font-weight: 600;
    color: #1a2a3f;
}

/* 输入框（用户名/密码） */
.login-container input {
    width: 100%;
    padding: 12px 16px;
    /* 内边距，影响高度 */
    margin: 12px 0;
    /* 上下外边距，形成输入框之间的间距 */
    border: 1px solid #ced4da;
    border-radius: 12px;
    font-size: 1rem;
    box-sizing: border-box;
}

/* 第一个输入框（账号）可单独调整上外边距（可选） */
.login-container input:first-of-type {
    margin-top: 0;
}

/* 最后一个输入框（密码）可单独调整下外边距（可选） */
.login-container input:last-of-type {
    margin-bottom: 0;
}

/* 登录/注册按钮 */
.login-container button {
    width: 100%;
    background: #1a2a3f;
    color: white;
    border: none;
    border-radius: 40px;
    padding: 12px;
    font-size: 1rem;
    font-weight: 500;
    cursor: pointer;
    margin-top: 20px;
    /* 按钮与上方密码框的间距 */
}

/* 提示行（"没有账号？注册" 或 "返回登录"） */
.login-container p {
    margin-top: 20px;
    /* 与上方按钮的间距 */
    margin-bottom: 0;
    font-size: 0.9rem;
}

/* 注册按钮（在注册界面也有一个"注册"按钮，但它也是 button，上面已统一定义） */
/* 如果需要单独调整"返回登录"按钮的样式，可使用 id 选择器 */
#backToLoginBtn {
    background: none;
    border: none;
    color: #1a2a3f;
    text-decoration: underline;
    cursor: pointer;
    padding: 0;
    margin-top: 16px;
    /* 与上方内容的间距 */
}

.login-container button:last-of-type {
    margin-bottom: 0;
}

.error {
    color: red;
    font-size: 14px;
    margin-top: 8px;
}

.welcome {
    text-align: center;
    color: #6c757d;
    margin-top: 40px;
}

@media (max-width: 768px) {
    .mobile-header {
        display: flex;
    }

    .sidebar {
        position: absolute;
        top: 0;
        left: 0;
        height: 100%;
        transform: translateX(-100%);
        transition: transform 0.3s ease;
        z-index: 20;
        box-shadow: 2px 0 8px rgba(0, 0, 0, 0.1);
    }

    .sidebar.open {
        transform: translateX(0);
    }

    .overlay {
        position: fixed;
        top: 0;
        left: 0;
        right: 0;
        bottom: 0;
        background: rgba(0, 0, 0, 0.3);
        z-index: 15;
        display: none;
    }

    .overlay.open {
        display: block;
    }

}

/* 气泡容器：始终允许横向滚动，不撑开父级 */
.bubble {
    max-width: 80%;
    overflow-x: auto;
    -webkit-overflow-scrolling: touch;
    padding: 12px 18px;
    border-radius: 24px;
    line-height: 1.5;
    word-wrap: break-word;
}

/* 电脑端可以稍微增加最大宽度 */
@media (min-width: 769px) {
    .bubble {
        max-width: 85%;
    }
}

/* 移动端保持原有的最大宽度 */
@media (max-width: 768px) {
    .bubble {
        max-width: 95%;
    }
}

/* ===== KaTeX 公式滚动与美化 ===== */

/* 行内公式：可能超宽时可滚动 */
.bubble .katex {
    max-width: 100%;
    overflow-x: auto;
    overflow-y: hidden;
    /* 关键：阻止垂直滚动条 */
    padding-bottom: 2px;
    /* 给横向滚动条留一丝空间 */
}

/* 块级公式：允许水平滚动，永远不要垂直滚动条 */
.bubble .katex-display {
    display: block;
    max-width: 100%;
    overflow-x: auto;
    overflow-y: hidden;
    /* 关键 */
    padding-bottom: 4px;
    /* 稍多一点，给横向滚动条缓冲 */
    margin: 0.5em 0;
}

/* 保持所有公式内部不换行（避免不必要的纵向溢出） */
.bubble .katex,
.bubble .katex-display,
.bubble .katex * {
    white-space: nowrap !important;
}

/* 自定义滚动条样式：窄条 + 圆角，更美观 */
.bubble .katex::-webkit-scrollbar,
.bubble .katex-display::-webkit-scrollbar {
    height: 4px;
    /* 只显示很窄的水平滚动条 */
    width: 0;
    /* 垂直滚动条宽度为0，彻底隐藏 */
}

.bubble .katex::-webkit-scrollbar-thumb,
.bubble .katex-display::-webkit-scrollbar-thumb {
    background: #bbb;
    border-radius: 4px;
}

.bubble .katex::-webkit-scrollbar-track,
.bubble .katex-display::-webkit-scrollbar-track {
    background: transparent;
}

/* 对于 Firefox 等其他浏览器，也尽量隐藏垂直滚动条 */
.bubble .katex,
.bubble .katex-display {
    scrollbar-width: thin;
    scrollbar-color: #bbb transparent;
}

/* 代码块可换行，避免太长 */
.bubble pre,
.bubble code {
    white-space: pre-wrap;
    word-break: break-word;
}

/* 修复 Markdown 列表项缩进，防止符号超出气泡边界 */
.bubble ul,
.bubble ol {
    padding-left: 1.5em;
    margin: 0.5em 0;
}

/* 确保列表项内的内容正常换行 */
.bubble li {
    word-wrap: break-word;
}

/* 对于嵌套列表，增加缩进 */
.bubble ul ul,
.bubble ol ol,
.bubble ul ol,
.bubble ol ul {
    padding-left: 1.2em;
}

/* 移动端可能因为字体大小需要微调 */
@media (max-width: 768px) {

    .bubble ul,
    .bubble ol {
        padding-left: 1.2em;
    }
}
