/* ========================================
   核心变量与基础
   ======================================== */
:root {
    --primary: #6366f1;
    --primary-dark: #4f46e5;
    --primary-light: #818cf8;
    --bg-gradient: linear-gradient(135deg, #f5f7fa 0%, #e4e8ec 100%);
    --card-bg: rgba(255, 255, 255, 0.9);
    --text-primary: #374151;
    /* 稍微减轻黑色，显得更高级 */
    --text-secondary: #6b7280;
    --text-muted: #9ca3af;
    --border-color: rgba(0, 0, 0, 0.06);
    --shadow-sm: 0 1px 2px rgba(0, 0, 0, 0.04);
    --shadow-lg: 0 10px 30px rgba(0, 0, 0, 0.08);
    --radius-md: 10px;
    --radius-lg: 14px;
    --radius-xl: 20px;
}

* {
    box-sizing: border-box;
    -webkit-tap-highlight-color: transparent;
}

body,
html {
    margin: 0;
    padding: 0;
    height: 100%;
    font-family: "Inter", -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
    background: var(--bg-gradient);
    color: var(--text-primary);
    line-height: 1.5;
    -webkit-font-smoothing: antialiased;
}

/* 主容器 */
.app-container {
    display: flex;
    flex-direction: column;
    height: 100vh;
    height: 100dvh;
    max-width: 1000px;
    /* 稍微加宽，适合阅读 */
    margin: 0 auto;
    background: var(--card-bg);
    backdrop-filter: blur(20px);
    position: relative;
    box-shadow: var(--shadow-lg);
}

@media (min-width: 768px) {
    body {
        padding: 15px;
        background: #f0f2f5;
    }

    .app-container {
        height: calc(100vh - 30px);
        border-radius: var(--radius-xl);
        border: 1px solid rgba(255, 255, 255, 0.5);
    }
}

/* 头部样式 */
.header {
    padding: 10px 16px;
    background: rgba(255, 255, 255, 0.8);
    border-bottom: 1px solid var(--border-color);
    display: flex;
    align-items: center;
    justify-content: space-between;
    flex-shrink: 0;
    z-index: 10;
}

.header-left {
    display: flex;
    align-items: center;
    gap: 8px;
}

.header-title h1 {
    font-size: 15px;
    font-weight: 600;
    margin: 0;
    letter-spacing: -0.2px;
}

.status-dot {
    width: 7px;
    height: 7px;
    background: #22c55e;
    border-radius: 50%;
}

.model-select {
    padding: 6px 10px;
    border-radius: 8px;
    border: 1px solid var(--border-color);
    font-size: 12px;
    max-width: 130px;
    outline: none;
    background: white;
    appearance: none;
    background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='14' height='14' viewBox='0 0 24 24' fill='none' stroke='%239ca3af' stroke-width='2'%3E%3Cpath d='M6 9l6 6 6-6'/%3E%3C/svg%3E");
    background-repeat: no-repeat;
    background-position: right 6px center;
    padding-right: 24px;
}

/* 历史抽屉 */
.history-drawer {
    position: fixed;
    inset: 0;
    z-index: 1000;
    visibility: hidden;
}

.history-drawer.open {
    visibility: visible;
}

.drawer-mask {
    position: absolute;
    inset: 0;
    background: rgba(0, 0, 0, 0.2);
    backdrop-filter: blur(2px);
}

.drawer-content {
    position: absolute;
    left: -240px;
    width: 240px;
    height: 100%;
    background: #ffffff;
    transition: transform 0.3s cubic-bezier(0.4, 0, 0.2, 1);
    display: flex;
    flex-direction: column;
}

.history-drawer.open .drawer-content {
    transform: translateX(240px);
}

/* 抽屉头部布局优化 */
.drawer-header {
    padding: 24px 16px 12px;
    display: flex;
    flex-direction: column;
    align-items: flex-start;
    gap: 8px;
}

.drawer-header h3 {
    margin: 0;
    font-size: 10px;
    font-weight: 600;
    color: #cbd5e1;
    letter-spacing: 1.5px;
    text-transform: uppercase;
}

/* 新建按钮：手机端精致版 */
.new-chat-btn {
    width: auto;
    display: inline-flex;
    align-items: center;
    gap: 5px;
    padding: 5px 12px;
    border-radius: 6px;
    border: 1px solid #eef2ff;
    background: #f8faff;
    color: var(--primary);
    font-size: 12px;
    font-weight: 500;
    cursor: pointer;
    /* 去掉默认的点击高亮阴影，使用自定义反馈 */
    -webkit-tap-highlight-color: transparent;
    transition: background 0.1s;
}

/* 仅保留点击时的瞬间反馈 */
.new-chat-btn:active {
    background: #e0e7ff;
    transform: scale(0.96);
    /* 点击时微小缩放，更有触感 */
}

.new-chat-btn svg {
    width: 14px;
    height: 14px;
    stroke-width: 2.5;
}

.drawer-list {
    flex: 1;
    overflow-y: auto;
    padding: 10px 12px;
}

/* 历史记录列表项：调整布局以容纳删除按钮 */
.session-item {
    display: flex;
    align-items: center;
    justify-content: space-between;
    /* 确保标题和删除按钮分居两侧 */
    padding: 12px 14px;
    border-radius: 8px;
    margin-bottom: 4px;
    background: transparent;
    transition: background 0.2s;
    -webkit-tap-highlight-color: transparent;
}

/* 激活状态的背景 */
.session-item.active {
    background: #f0f4ff;
}

/* 对话标题：稍微压缩宽度，给删除按钮留位子 */
.session-title {
    flex: 1;
    font-size: 13px;
    color: #4b5563;
    font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
    margin-right: 10px;
    /* 标题和删除按钮的间距 */
}

/* 删除按钮：手机端始终可见版 */
.del-btn {
    width: 28px;
    height: 28px;
    border: none;
    background: rgba(0, 0, 0, 0.03);
    /* 给个极淡的底色，像个小按钮 */
    color: #94a3b8;
    /* 默认灰色 */
    display: flex;
    align-items: center;
    justify-content: center;
    border-radius: 6px;
    flex-shrink: 0;
    opacity: 1 !important;
    /* 强制取消透明，始终可见 */
    transition: all 0.1s;
}

/* 手机点击时的反馈：变成淡红色 */
.del-btn:active {
    background: #fee2e2;
    color: #ef4444;
    transform: scale(0.9);
}

.del-btn svg {
    width: 14px;
    height: 14px;
    stroke-width: 2;
}

/* 聊天区域 */
.chat-box {
    flex: 1;
    overflow-y: auto;
    padding: 16px;
    background: #fcfcfd;
}

.message-wrapper {
    display: flex;
    gap: 10px;
    margin-bottom: 16px;
    animation: fadeIn 0.2s ease;
}

@keyframes fadeIn {
    from {
        opacity: 0;
        transform: translateY(4px);
    }

    to {
        opacity: 1;
        transform: translateY(0);
    }
}

.message-wrapper.user {
    justify-content: flex-end;
}

.avatar {
    width: 30px;
    height: 30px;
    border-radius: 8px;
    flex-shrink: 0;
    display: flex;
    align-items: center;
    justify-content: center;
    color: white;
    background: var(--primary);
}

.avatar svg {
    width: 18px;
    height: 18px;
}

.avatar.user-avatar {
    background: #4b5563;
}

/* 1. 调整气泡内边距，底部留出空间给按钮 */
.message {
    max-width: 90%;
    /* 增加底部 padding，从 26px 增加到 32px，给正文和按钮留出呼吸感 */
    padding: 10px 14px 32px 14px;
    border-radius: var(--radius-lg);
    font-size: 14px;
    position: relative;
    word-break: break-word;
    line-height: 1.6;
    /* 稍微增加行高，阅读更舒适 */
}

.message.ai {
    background: white;
    border: 1px solid var(--border-color);
    border-top-left-radius: 4px;
    box-shadow: var(--shadow-sm);
}

.message.user {
    background: var(--primary);
    color: white;
    border-top-right-radius: 4px;
    padding-bottom: 34px;
}

/* 调整按钮容器，支持多个按钮横排 */
.message-actions {
    position: absolute;
    left: 8px;
    /* 改为从左边开始计数 */
    right: 8px;
    /* 两边都固定，撑开宽度 */
    bottom: 4px;
    display: flex;
    justify-content: space-between;
    /* 关键：将内部元素推向两端 */
    opacity: 0.6;
    transition: opacity 0.2s;
    pointer-events: none;
    /* 防止容器遮挡文字选择 */
}

/* 3. 优化按钮外观，使其更小巧不突兀 */
.action-btn {
    pointer-events: auto;
    width: 24px;
    height: 24px;
    border: none;
    border-radius: 5px;
    background: rgba(0, 0, 0, 0.03);
    /* 给一个极淡的背景 */
    color: #9ca3af;
    display: flex;
    align-items: center;
    justify-content: center;
    cursor: pointer;
    -webkit-tap-highlight-color: transparent;
}

/* AI 消息（白色背景）下的按钮 */
.message.user .action-btn {
    background: rgba(255, 255, 255, 0.12);
    color: rgba(255, 255, 255, 0.8);
}

/* 适配用户消息里的删除按钮 */
.message.user .del-msg-btn {
    color: rgba(255, 255, 255, 0.7);
}

/* 用户消息（紫色背景）下的按钮 */
.message.user .action-btn {
    color: rgba(255, 255, 255, 0.7);
    background: rgba(255, 255, 255, 0.15);
}

/* 图标稍微缩小一点 */
.action-btn svg {
    width: 13px;
    height: 13px;
    stroke-width: 2;
}

/* 加载中动画 */
.message.ai.typing {
    min-width: 50px;
    min-height: 34px;
    padding-right: 14px !important;
}

.typing-dots {
    display: flex;
    gap: 4px;
}

.typing-dots span {
    width: 5px;
    height: 5px;
    background: var(--primary);
    border-radius: 50%;
    animation: typing-bounce 1.4s infinite ease-in-out both;
}

.typing-dots span:nth-child(1) {
    animation-delay: -0.32s;
}

.typing-dots span:nth-child(2) {
    animation-delay: -0.16s;
}

@keyframes typing-bounce {

    0%,
    80%,
    100% {
        transform: scale(0.6);
        opacity: 0.3;
    }

    40% {
        transform: scale(1);
        opacity: 1;
    }
}

/* 输入区域 */
.input-area {
    padding: 12px 16px;
    background: white;
    border-top: 1px solid var(--border-color);
}

.input-wrapper {
    display: flex;
    align-items: flex-end;
    /* 让发送按钮对齐文本框底部 */
    gap: 8px;
    background: #f1f5f9;
    padding: 8px 8px 8px 14px;
    border-radius: 20px;
    /* 稍微减小圆角，适应多行 */
    transition: 0.2s;
}

.input-wrapper:focus-within {
    background: white;
    box-shadow: 0 0 0 2px var(--primary-light);
}

/* 文本域样式重置 */
.text-input {
    flex: 1;
    border: none;
    background: transparent;
    font-size: 15px;
    outline: none;
    resize: none;
    /* 禁止用户手动拖动大小 */
    padding: 8px 0;
    margin: 0;
    line-height: 20px;
    max-height: 100px;
    /* 强制限制最高高度 */
    overflow-y: auto;
    /* 超过 3 行显示滚动条 */
    display: block;
    font-family: inherit;
}

/* 隐藏 textarea 的滚动条（可选，让界面更干净） */
.text-input::-webkit-scrollbar {
    width: 4px;
}

.text-input::-webkit-scrollbar-thumb {
    background: #cbd5e1;
    border-radius: 10px;
}

.icon-btn {
    width: 32px;
    height: 32px;
    border: none;
    border-radius: 8px;
    display: flex;
    align-items: center;
    justify-content: center;
    background: transparent;
    color: var(--text-muted);
    cursor: pointer;
    transition: 0.2s;
}

.icon-btn.send {
    background: var(--primary);
    color: white;
}

.icon-btn.send:disabled {
    background: #e5e7eb;
    color: #9ca3af;
    margin-bottom: 2px;
}

.icon-btn.clear:hover {
    color: #ef4444;
    background: #fee2e2;
}

/* Markdown & 代码块优化 */
.message.ai pre {
    background: #1f2937;
    color: #e5e7eb;
    padding: 12px;
    border-radius: 8px;
    font-size: 13px;
    font-family: "JetBrains Mono", Menlo, monospace;
    margin: 8px 0;
    overflow-x: auto;
}

.katex {
    font-size: 1.05em;
}

/* 公式稍微大一点点保证清晰 */