/* ================= 1. 全局与基础样式 ================= */
:root {
    --primary-color: #4facfe;
    --secondary-color: #00f2fe;
    --bg-color: #f0f2f5;
    --chat-bg: #ffffff;
    --sidebar-bg: #ffffff;
    --user-msg-bg: #007aff;
    --ai-msg-bg: #f7f7f8;
    --text-color: #333;
}

/* 禁止整个页面在手机上左右晃动，并锁定高度 */
html, body {
    margin: 0;
    padding: 0;
    width: 100%;
    height: 100%; 
    overflow: hidden; /* 核心：禁止 body 滚动 */
    font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
    background-color: var(--bg-color);
    -webkit-text-size-adjust: 100%;
    position: fixed; /* 🔒 手机端防抖动终极大法 */
    top: 0;
    left: 0;
}

/* ================= 2. 布局容器 (APP 级布局) ================= */
.app-container {
    width: 100%;
    height: 100%; /* 继承 body 高度 */
    display: flex;
    background: var(--chat-bg);
    position: relative;
    overflow: hidden; /* 再次确保不外溢 */
}

/* 电脑端优化 */
@media (min-width: 769px) {
    body {
        position: static; /* 电脑端取消 fixed */
        display: flex;
        justify-content: center;
        align-items: center;
        background-color: #eef1f5;
    }
    .app-container {
        width: 95%;
        max-width: 1200px;
        height: 90vh;
        border-radius: 16px;
        box-shadow: 0 10px 30px rgba(0,0,0,0.1);
    }
}

/* ================= 3. 左侧侧边栏 ================= */
.sidebar {
    width: 280px;
    background-color: var(--sidebar-bg);
    border-right: 1px solid #eee;
    display: flex;
    flex-direction: column;
    padding: 20px;
    z-index: 200;
    flex-shrink: 0;
}

.brand {
    font-size: 20px;
    font-weight: 800;
    color: #007aff;
    margin-bottom: 20px;
    display: flex;
    align-items: center;
    gap: 10px;
}

.upload-card {
    border: 1px dashed #ddd;
    border-radius: 12px;
    padding: 15px;
    text-align: center;
    margin-bottom: 20px;
    background: #fafafa;
}

#fileInput { display: none; }

.upload-btn-styled {
    display: inline-block;
    padding: 8px 20px;
    background: linear-gradient(to right, var(--primary-color), var(--secondary-color));
    color: white;
    border-radius: 20px;
    cursor: pointer;
    font-size: 14px;
    font-weight: 600;
    margin-bottom: 8px;
}

.file-name-display {
    font-size: 13px;
    color: #555;
    word-break: break-all;
    margin-bottom: 10px;
    min-height: 20px;
}

.action-buttons {
    display: flex;
    flex-direction: column;
    gap: 12px;
}

.btn {
    border: none;
    padding: 12px;
    border-radius: 8px;
    cursor: pointer;
    font-size: 15px;
    font-weight: 500;
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 8px;
}
.btn-primary { background: #007aff; color: white; }
.btn-danger { background: #fff0f0; color: #ff3b30; border: 1px solid #ffccc7; }
.btn-ghost { background: transparent; color: #555; justify-content: flex-start; padding-left: 0; }

/* ================= 4. 右侧聊天主区 (核心修复区) ================= */
.main-chat {
    flex: 1;
    display: flex;
    flex-direction: column;
    position: relative;
    width: 100%;
    background-color: #fff;
    /* 🚨 核心修复：防止Flex子元素撑破容器，必须设为0 */
    min-height: 0; 
    overflow: hidden; /* 确保滚动条只出现在 chat-box */
}

.mobile-header {
    display: none;
    padding: 12px 15px;
    background: white;
    border-bottom: 1px solid #f0f0f0;
    align-items: center;
    justify-content: space-between;
    flex-shrink: 0;
    z-index: 10;
}

.chat-box {
    flex: 1;
    padding: 15px;
    /* 🚨 核心修复：确保只有这里能滚 */
    overflow-y: auto; 
    overflow-x: hidden;
    background: #fff;
    scroll-behavior: smooth;
    -webkit-overflow-scrolling: touch; /* iOS 丝滑滚动 */
    /* 防止滚动穿透 */
    overscroll-behavior: contain;
}

/* 消息气泡 */
.msg-row {
    display: flex;
    flex-direction: column;
    margin-bottom: 24px;
    animation: fadeIn 0.3s ease;
}
@keyframes fadeIn { from { opacity: 0; transform: translateY(5px); } to { opacity: 1; transform: translateY(0); } }

.msg-role {
    font-size: 12px;
    color: #999;
    margin-bottom: 6px;
    padding: 0 4px;
}

.msg-bubble {
    max-width: 88%;
    padding: 12px 16px;
    border-radius: 12px;
    line-height: 1.6;
    font-size: 16px;
    word-wrap: break-word;
}

.msg-user { align-items: flex-end; }
.msg-user .msg-bubble { background: var(--user-msg-bg); color: white; border-radius: 18px 18px 4px 18px; }

.msg-ai { align-items: flex-start; }
.msg-ai .msg-bubble { background: var(--ai-msg-bg); color: #1a1a1a; border-radius: 18px 18px 18px 4px; }

/* Markdown 样式 */
.markdown-body strong { color: #d35400; background: #fff8e1; padding: 0 4px; border-radius: 4px; }
.markdown-body p { margin: 0 0 10px 0; }
.markdown-body ul, .markdown-body ol { padding-left: 20px; margin: 8px 0; }
.markdown-body h1, .markdown-body h2, .markdown-body h3 { margin-top: 15px; font-weight: 700; color: #333; }
.markdown-body code { background: #eee; padding: 2px 5px; border-radius: 3px; font-family: monospace; color: #c7254e; font-size: 0.9em; }
.markdown-body pre { background: #2d2d2d; color: #ccc; padding: 12px; border-radius: 8px; overflow-x: auto; margin: 10px 0; }

/* 底部输入框 */
.input-area {
    padding: 12px 15px;
    background: white;
    border-top: 1px solid #f0f0f0;
    display: flex;
    gap: 10px;
    align-items: center;
    flex-shrink: 0;
    padding-bottom: env(safe-area-inset-bottom);
}

.input-box {
    flex: 1;
    border: 1px solid #ddd;
    padding: 12px 16px;
    border-radius: 24px;
    font-size: 16px;
    outline: none;
    background: #f9f9f9;
    transition: border-color 0.2s;
}
.input-box:focus { border-color: #007aff; background: #fff; }

.send-btn {
    background: #007aff;
    color: white;
    border: none;
    width: 44px;
    height: 44px;
    border-radius: 50%;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    flex-shrink: 0;
}

/* ================= 5. 手机端适配 (Media Queries) ================= */
@media (max-width: 768px) {
    .app-container {
        flex-direction: column;
        border-radius: 0;
    }
    
    .mobile-header { display: flex; }
    
    .sidebar {
        position: absolute;
        top: 0;
        left: 0;
        height: 100%;
        width: 85%;
        transform: translateX(-100%);
        box-shadow: 2px 0 15px rgba(0,0,0,0.2);
    }
    .sidebar.active { transform: translateX(0); }
    
    .overlay {
        display: none;
        position: fixed; top: 0; left: 0; right: 0; bottom: 0;
        background: rgba(0,0,0,0.5);
        z-index: 100;
        backdrop-filter: blur(2px);
    }
    .overlay.active { display: block; }
    
    /* 修复 iOS 输入框点击时页面放大问题 */
    .input-box {
        font-size: 16px !important;
    }
}