/* ============================================
   AI 智能客服 - 白色格调 UI
   ============================================ */

:root {
    --bg-primary: #ffffff;
    --bg-secondary: #f9f9f9;
    --bg-sidebar: #f5f5f5;
    --bg-hover: #ebebeb;
    --bg-message-user: #f5f5f5;
    --bg-message-ai: transparent;
    --bg-input: #f5f5f5;
    --bg-input-focus: #eeeeee;
    --text-primary: #1a1a1a;
    --text-secondary: #555555;
    --text-muted: #999999;
    --border-color: #e0e0e0;
    --border-light: #eeeeee;
    --accent: #10a37f;
    --accent-hover: #0d8c6d;
    --danger: #ef4444;
    --scrollbar-bg: transparent;
    --scrollbar-thumb: #cccccc;
    --shadow: 0 1px 3px rgba(0,0,0,0.08), 0 0 0 1px rgba(0,0,0,0.04);
    --radius: 12px;
    --radius-sm: 8px;
    --radius-lg: 24px;
    --transition: 0.2s ease;
    --max-chat-width: 768px;
    --font: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
}

* { margin: 0; padding: 0; box-sizing: border-box; }
html, body { height: 100%; overflow: hidden; }
body {
    font-family: var(--font);
    background: var(--bg-primary);
    color: var(--text-primary);
    font-size: 15px;
    line-height: 1.6;
    -webkit-font-smoothing: antialiased;
}

/* ============ 滚动条 ============ */
::-webkit-scrollbar { width: 6px; }
::-webkit-scrollbar-track { background: transparent; }
::-webkit-scrollbar-thumb { background: var(--scrollbar-thumb); border-radius: 3px; }
::-webkit-scrollbar-thumb:hover { background: #aaa; }

/* ============ 布局 ============ */
.app-container { display: flex; height: 100vh; }

/* ============ 侧边栏 ============ */
.sidebar {
    width: 260px;
    background: var(--bg-sidebar);
    display: flex;
    flex-direction: column;
    border-right: 1px solid var(--border-color);
    flex-shrink: 0;
    transition: transform var(--transition);
}

.sidebar-header { padding: 12px; }

.btn-new-chat {
    width: 100%;
    padding: 10px 16px;
    background: transparent;
    border: 1px solid var(--border-color);
    color: var(--text-primary);
    border-radius: var(--radius-sm);
    cursor: pointer;
    font-size: 14px;
    display: flex;
    align-items: center;
    gap: 8px;
    transition: background var(--transition);
}
.btn-new-chat:hover { background: var(--bg-hover); }
.btn-new-chat svg { width: 18px; height: 18px; stroke: currentColor; fill: none; stroke-width: 2; }

.session-list {
    flex: 1;
    overflow-y: auto;
    padding: 4px 8px;
}

.session-item {
    padding: 10px 12px;
    border-radius: var(--radius-sm);
    cursor: pointer;
    margin-bottom: 2px;
    display: flex;
    align-items: center;
    gap: 8px;
    transition: background var(--transition);
    position: relative;
    group: true;
}
.session-item:hover { background: var(--bg-hover); }
.session-item.active { background: var(--bg-hover); }

.session-title {
    flex: 1;
    font-size: 14px;
    color: var(--text-secondary);
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}
.session-item.active .session-title,
.session-item:hover .session-title { color: var(--text-primary); }

.session-time { display: none; }

.btn-delete-session {
    opacity: 0;
    background: none;
    border: none;
    color: var(--text-muted);
    cursor: pointer;
    font-size: 16px;
    padding: 2px 6px;
    border-radius: 4px;
    transition: all var(--transition);
    flex-shrink: 0;
}
.session-item:hover .btn-delete-session { opacity: 1; }
.btn-delete-session:hover { color: var(--danger); background: rgba(239,68,68,0.1); }

.empty-sessions {
    text-align: center;
    padding: 40px 16px;
    color: var(--text-muted);
    font-size: 13px;
}

.sidebar-footer {
    padding: 12px;
    border-top: 1px solid var(--border-color);
}

/* ============ 主聊天区域 ============ */
.chat-main {
    flex: 1;
    display: flex;
    flex-direction: column;
    min-width: 0;
    position: relative;
}

.chat-header {
    height: 48px;
    display: flex;
    align-items: center;
    justify-content: center;
    padding: 0 16px;
    border-bottom: 1px solid var(--border-color);
    flex-shrink: 0;
    position: relative;
    background: var(--bg-primary);
}
.chat-header h3 {
    font-size: 16px;
    font-weight: 600;
    color: var(--text-primary);
}
.chat-header-actions {
    position: absolute;
    right: 16px;
}
.model-badge {
    font-size: 12px;
    color: var(--text-muted);
    background: var(--bg-hover);
    padding: 3px 10px;
    border-radius: 12px;
}

/* ============ 消息区域 ============ */
.chat-messages {
    flex: 1;
    overflow-y: auto;
    padding: 0;
    background: var(--bg-primary);
}

/* 欢迎屏幕 */
.welcome-screen {
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    height: 100%;
    gap: 12px;
    color: var(--text-secondary);
    padding: 40px;
}
.welcome-icon {
    font-size: 48px;
    margin-bottom: 8px;
}
.welcome-screen h2 {
    font-size: 28px;
    font-weight: 700;
    color: var(--text-primary);
}
.welcome-screen p {
    font-size: 16px;
    color: var(--text-muted);
}
.quick-actions {
    display: flex;
    gap: 12px;
    margin-top: 20px;
}
.quick-btn {
    padding: 10px 20px;
    background: var(--accent);
    color: #fff;
    border: none;
    border-radius: var(--radius-lg);
    cursor: pointer;
    font-size: 14px;
    font-weight: 500;
    text-decoration: none;
    transition: background var(--transition);
}
.quick-btn:hover { background: var(--accent-hover); }

.empty-chat {
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    height: 100%;
    color: var(--text-muted);
}
.empty-icon { font-size: 36px; margin-bottom: 12px; }

/* ============ 消息气泡 - ChatGPT风格 ============ */
.message {
    padding: 20px 0;
    display: flex;
    justify-content: center;
}
.message.user { background: var(--bg-primary); }
.message.assistant { background: var(--bg-secondary); }

.message-inner {
    width: 100%;
    max-width: var(--max-chat-width);
    padding: 0 24px;
    display: flex;
    gap: 16px;
    align-items: flex-start;
}

.message-avatar {
    width: 28px;
    height: 28px;
    border-radius: 4px;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 14px;
    flex-shrink: 0;
    margin-top: 2px;
}
.message.user .message-avatar {
    background: #5436DA;
    color: #fff;
    font-size: 12px;
    border-radius: 50%;
}
.message.assistant .message-avatar {
    background: var(--accent);
    color: #fff;
    font-size: 12px;
    border-radius: 4px;
}

.message-content {
    flex: 1;
    min-width: 0;
    overflow-wrap: break-word;
}
.message-bubble {
    color: var(--text-primary);
    font-size: 15px;
    line-height: 1.7;
}

/* ============ Markdown 样式 ============ */
.message-bubble p { margin-bottom: 12px; }
.message-bubble p:last-child { margin-bottom: 0; }
.message-bubble ul, .message-bubble ol { padding-left: 24px; margin-bottom: 12px; }
.message-bubble li { margin-bottom: 4px; }
.message-bubble strong { font-weight: 600; }

.message-bubble code {
    background: #f0f0f0;
    padding: 2px 6px;
    border-radius: 4px;
    font-size: 13px;
    font-family: "SF Mono", "Fira Code", monospace;
    color: #d63384;
}

.message-bubble pre {
    background: #f6f8fa;
    border-radius: var(--radius-sm);
    margin: 12px 0;
    overflow: hidden;
    border: 1px solid var(--border-color);
}
.message-bubble pre code {
    display: block;
    padding: 16px;
    overflow-x: auto;
    color: var(--text-primary);
    background: none;
    font-size: 13px;
    line-height: 1.5;
}

.message-bubble table {
    border-collapse: collapse;
    width: 100%;
    margin: 12px 0;
}
.message-bubble th, .message-bubble td {
    border: 1px solid var(--border-color);
    padding: 8px 12px;
    text-align: left;
    font-size: 14px;
}
.message-bubble th { background: var(--bg-hover); font-weight: 600; }
.message-bubble blockquote {
    border-left: 3px solid var(--accent);
    padding-left: 16px;
    color: var(--text-secondary);
    margin: 12px 0;
}
.message-bubble a { color: var(--accent); text-decoration: underline; }
.message-bubble hr { border: none; border-top: 1px solid var(--border-color); margin: 16px 0; }

/* 思考/加载动画 */
.typing-indicator {
    color: var(--text-muted);
    font-style: italic;
}
.typing-indicator::after {
    content: '';
    animation: dots 1.5s infinite;
}
@keyframes dots {
    0%, 20% { content: ''; }
    40% { content: '.'; }
    60% { content: '..'; }
    80%, 100% { content: '...'; }
}

.error-msg { color: var(--danger); }

/* ============ 输入区域 - ChatGPT 风格 ============ */
.chat-input-area {
    padding: 12px 24px 24px;
    display: flex;
    justify-content: center;
    flex-shrink: 0;
    background: var(--bg-primary);
}

.input-wrapper {
    width: 100%;
    max-width: var(--max-chat-width);
    position: relative;
    background: var(--bg-input);
    border: 1px solid var(--border-color);
    border-radius: var(--radius-lg);
    display: flex;
    align-items: flex-end;
    transition: border-color var(--transition), box-shadow var(--transition);
}
.input-wrapper:focus-within {
    border-color: #bbb;
    box-shadow: 0 0 0 2px rgba(0,0,0,0.05);
}

.input-wrapper textarea {
    flex: 1;
    border: none;
    background: transparent;
    color: var(--text-primary);
    font-size: 15px;
    line-height: 1.5;
    padding: 14px 16px;
    resize: none;
    outline: none;
    max-height: 160px;
    font-family: var(--font);
}
.input-wrapper textarea::placeholder { color: var(--text-muted); }

.btn-send {
    background: var(--text-primary);
    border: none;
    color: #fff;
    width: 32px;
    height: 32px;
    border-radius: 50%;
    display: flex;
    align-items: center;
    justify-content: center;
    cursor: pointer;
    flex-shrink: 0;
    margin: 8px 10px;
    transition: opacity var(--transition);
}
.btn-send:hover { opacity: 0.85; }
.btn-send:disabled { opacity: 0.3; cursor: default; }
.btn-send svg { width: 16px; height: 16px; }

.input-footer {
    text-align: center;
    padding: 8px 0 0;
    font-size: 12px;
    color: var(--text-muted);
    max-width: var(--max-chat-width);
    margin: 0 auto;
}

/* ============ 移动端侧边栏 ============ */
.sidebar-toggle {
    display: none;
    position: fixed;
    top: 10px;
    left: 10px;
    z-index: 100;
    background: var(--bg-hover);
    border: 1px solid var(--border-color);
    color: var(--text-primary);
    width: 36px;
    height: 36px;
    border-radius: var(--radius-sm);
    cursor: pointer;
    align-items: center;
    justify-content: center;
    font-size: 18px;
}
.sidebar-overlay {
    display: none;
    position: fixed;
    top: 0; left: 0; right: 0; bottom: 0;
    background: rgba(0,0,0,0.3);
    z-index: 49;
}

@media (max-width: 768px) {
    .sidebar {
        position: fixed;
        left: 0; top: 0; bottom: 0;
        z-index: 50;
        transform: translateX(-100%);
        width: 280px;
    }
    .sidebar.open { transform: translateX(0); }
    .sidebar-toggle { display: flex; }
    .sidebar-overlay.open { display: block; }

    .chat-header {
        padding: 0 48px;
        height: 48px;
    }
    .chat-header h3 {
        font-size: 15px;
        overflow: hidden;
        text-overflow: ellipsis;
        white-space: nowrap;
    }
    .chat-header-actions {
        right: 12px;
    }
    .model-badge {
        font-size: 11px;
        padding: 2px 8px;
    }

    .message { padding: 12px 0; }
    .message-inner { padding: 0 12px; gap: 10px; }
    .message-avatar { width: 26px; height: 26px; font-size: 11px; }
    .message-bubble { font-size: 14px; line-height: 1.65; }
    .message-bubble pre code { font-size: 12px; padding: 12px; }
    .message-bubble table { font-size: 13px; display: block; overflow-x: auto; }
    .message-bubble th, .message-bubble td { padding: 6px 8px; font-size: 13px; }

    .welcome-screen { padding: 24px 20px; gap: 10px; }
    .welcome-icon { font-size: 36px; }
    .welcome-screen h2 { font-size: 22px; }
    .welcome-screen p { font-size: 14px; text-align: center; }

    .chat-input-area { padding: 8px 10px 16px; }
    .input-wrapper { border-radius: 20px; }
    .input-wrapper textarea { padding: 12px 14px; font-size: 14px; }
    .btn-send { width: 30px; height: 30px; margin: 6px 8px; }
    .btn-send svg { width: 14px; height: 14px; }

    /* 移动端删除按钮始终可见（触屏无hover） */
    .btn-delete-session { opacity: 0.6; }
    .session-item .btn-delete-session { opacity: 0.6; }
}

/* 更小屏幕适配 */
@media (max-width: 480px) {
    .chat-header { padding: 0 44px; }
    .message-inner { padding: 0 10px; gap: 8px; }
    .message-bubble { font-size: 14px; }
    .welcome-screen h2 { font-size: 20px; }
    .welcome-screen p { font-size: 13px; }
    .chat-input-area { padding: 6px 8px 12px; }
    .input-wrapper textarea { padding: 10px 12px; font-size: 14px; }
}

/* ============================================
   管理后台样式
   ============================================ */
.admin-container {
    max-width: 960px;
    margin: 0 auto;
    padding: 32px 24px;
    min-height: 100vh;
}
.admin-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    margin-bottom: 32px;
    padding-bottom: 16px;
    border-bottom: 1px solid var(--border-color);
}
.admin-header h1 { font-size: 22px; font-weight: 700; }
.back-link {
    color: var(--text-secondary);
    text-decoration: none;
    font-size: 14px;
    padding: 6px 14px;
    border: 1px solid var(--border-color);
    border-radius: var(--radius-sm);
    transition: all var(--transition);
}
.back-link:hover { background: var(--bg-hover); color: var(--text-primary); }

/* 配置卡片 */
.config-card {
    background: var(--bg-primary);
    border: 1px solid var(--border-color);
    border-radius: var(--radius);
    padding: 24px;
    margin-bottom: 20px;
    box-shadow: var(--shadow);
}
.config-card h2 {
    font-size: 16px;
    font-weight: 600;
    margin-bottom: 20px;
    color: var(--text-primary);
}

/* 表单 */
.form-grid {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 16px;
}
.form-group { display: flex; flex-direction: column; gap: 6px; }
.form-group.full-width { grid-column: 1 / -1; }
.form-group label {
    font-size: 13px;
    color: var(--text-secondary);
    font-weight: 500;
}
.form-group input,
.form-group select,
.form-group textarea {
    padding: 10px 12px;
    background: var(--bg-secondary);
    border: 1px solid var(--border-color);
    border-radius: var(--radius-sm);
    color: var(--text-primary);
    font-size: 14px;
    outline: none;
    transition: border-color var(--transition);
    font-family: var(--font);
}
.form-group input:focus,
.form-group select:focus,
.form-group textarea:focus { border-color: var(--accent); }
.form-group input::placeholder { color: var(--text-muted); }

.form-actions {
    display: flex;
    gap: 12px;
    justify-content: flex-end;
    margin-top: 20px;
}

/* 按钮 */
.btn {
    padding: 9px 20px;
    border: none;
    border-radius: var(--radius-sm);
    cursor: pointer;
    font-size: 14px;
    font-weight: 500;
    transition: all var(--transition);
    font-family: var(--font);
}
.btn-primary { background: var(--accent); color: #fff; }
.btn-primary:hover { background: var(--accent-hover); }
.btn-secondary { background: var(--bg-hover); color: var(--text-secondary); border: 1px solid var(--border-color); }
.btn-secondary:hover { color: var(--text-primary); border-color: #bbb; }
.btn-danger { background: transparent; color: var(--danger); border: 1px solid var(--danger); }
.btn-danger:hover { background: rgba(239,68,68,0.08); }
.btn-success { background: var(--accent); color: #fff; }
.btn-success:hover { background: var(--accent-hover); }
.btn-small { padding: 5px 12px; font-size: 13px; }

/* 配置列表 */
.config-list { display: flex; flex-direction: column; gap: 8px; }
.config-item {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 14px 16px;
    background: var(--bg-secondary);
    border: 1px solid var(--border-color);
    border-radius: var(--radius-sm);
    gap: 16px;
    transition: border-color var(--transition);
}
.config-item:hover { border-color: #bbb; }
.config-item.active { border-color: var(--accent); }
.config-info { flex: 1; min-width: 0; }
.config-info h3 {
    font-size: 14px;
    font-weight: 600;
    margin-bottom: 4px;
    display: flex;
    align-items: center;
    gap: 8px;
}
.config-details {
    display: flex;
    flex-wrap: wrap;
    gap: 12px;
    font-size: 12px;
    color: var(--text-muted);
}
.config-actions { display: flex; gap: 6px; flex-shrink: 0; }

.status-badge {
    font-size: 11px;
    padding: 2px 8px;
    border-radius: 10px;
    font-weight: 500;
}
.status-active { background: rgba(16,163,127,0.12); color: var(--accent); }
.status-inactive { background: rgba(0,0,0,0.06); color: var(--text-muted); }

.empty-list {
    text-align: center;
    padding: 32px;
    color: var(--text-muted);
    font-size: 14px;
}

/* Tab 导航 */
.tab-nav { display: flex; gap: 2px; margin-bottom: 24px; background: var(--bg-secondary); border-radius: var(--radius-sm); padding: 4px; border: 1px solid var(--border-color); }
.tab-btn {
    flex: 1;
    padding: 10px 20px;
    border: none;
    background: transparent;
    color: var(--text-muted);
    border-radius: 6px;
    cursor: pointer;
    font-size: 14px;
    font-weight: 500;
    transition: all var(--transition);
    font-family: var(--font);
}
.tab-btn.active { background: var(--bg-primary); color: var(--text-primary); box-shadow: 0 1px 3px rgba(0,0,0,0.08); }
.tab-btn:hover { color: var(--text-secondary); }
.tab-content { display: none; }
.tab-content.active { display: block; }

/* Toast */
.toast {
    position: fixed;
    bottom: 24px;
    right: 24px;
    padding: 12px 20px;
    border-radius: var(--radius-sm);
    font-size: 14px;
    z-index: 999;
    animation: toastIn 0.3s ease;
    box-shadow: 0 4px 24px rgba(0,0,0,0.12);
}
.toast.success { background: var(--accent); color: #fff; }
.toast.error { background: var(--danger); color: #fff; }
@keyframes toastIn { from { opacity: 0; transform: translateY(12px); } to { opacity: 1; transform: translateY(0); } }

/* ============ 后台登录页 ============ */
.login-container {
    display: flex;
    align-items: center;
    justify-content: center;
    min-height: 100vh;
    padding: 24px;
    background: var(--bg-secondary);
}
.login-card {
    background: var(--bg-primary);
    border: 1px solid var(--border-color);
    border-radius: var(--radius);
    padding: 48px 40px;
    width: 100%;
    max-width: 400px;
    text-align: center;
    box-shadow: var(--shadow);
}
.login-card h2 {
    font-size: 22px;
    font-weight: 700;
    margin-bottom: 8px;
}
.login-card p {
    font-size: 14px;
    color: var(--text-muted);
    margin-bottom: 28px;
}
.login-card .form-group { margin-bottom: 20px; text-align: left; }
.login-card .btn-primary { width: 100%; padding: 12px; font-size: 15px; border-radius: var(--radius-lg); }
.login-error { color: var(--danger); font-size: 13px; margin-top: 12px; display: none; }

/* 测试区域 */
.test-area { margin-top: 16px; padding: 16px; background: var(--bg-secondary); border-radius: var(--radius-sm); border: 1px solid var(--border-color); }
.test-area h4 { margin-bottom: 12px; color: var(--text-secondary); font-size: 14px; }
.test-row { display: flex; gap: 8px; align-items: flex-end; }
.test-row input { flex: 1; }
.test-result { margin-top: 12px; padding: 12px; background: var(--bg-primary); border-radius: var(--radius-sm); font-size: 13px; white-space: pre-wrap; color: var(--text-secondary); max-height: 300px; overflow-y: auto; display: none; border: 1px solid var(--border-color); }

/* ============ 管理后台滚动支持 ============ */
body.admin-body { overflow: auto; background: var(--bg-secondary); }

@media (max-width: 768px) {
    .form-grid { grid-template-columns: 1fr; }
    .form-group.full-width { grid-column: 1; }

    .admin-container { padding: 16px 12px; }
    .admin-header { 
        flex-direction: column; 
        align-items: flex-start; 
        gap: 12px; 
        margin-bottom: 20px; 
    }
    .admin-header h1 { font-size: 18px; }

    .config-card { padding: 16px; margin-bottom: 14px; }
    .config-card h2 { font-size: 15px; margin-bottom: 14px; }

    .config-item { 
        flex-direction: column; 
        align-items: flex-start; 
        padding: 12px; 
        gap: 10px; 
    }
    .config-info h3 { 
        font-size: 13px; 
        flex-wrap: wrap; 
    }
    .config-details { 
        gap: 6px 10px; 
        font-size: 11px; 
    }
    .config-details span {
        word-break: break-all;
    }
    .config-actions { 
        width: 100%; 
        justify-content: flex-start; 
    }
    .config-actions .btn { padding: 6px 14px; font-size: 13px; }

    .tab-nav { flex-direction: column; }
    .tab-btn { padding: 10px 16px; text-align: center; }

    .form-actions { 
        flex-direction: column; 
    }
    .form-actions .btn { 
        width: 100%; 
        text-align: center;
        padding: 12px;
    }

    .login-container { padding: 16px; }
    .login-card { padding: 32px 20px; }
    .login-card h2 { font-size: 20px; }

    .test-row { flex-direction: column; }
    .test-row .form-group { width: 100%; }
    .test-row .btn { width: 100%; text-align: center; }

    .toast { 
        left: 16px; 
        right: 16px; 
        bottom: 16px; 
        text-align: center; 
    }

    .back-link { font-size: 13px; padding: 5px 10px; }
    .btn-small { padding: 6px 10px; font-size: 12px; }
}

@media (max-width: 480px) {
    .admin-container { padding: 12px 8px; }
    .config-card { padding: 12px; }
    .form-group input,
    .form-group select,
    .form-group textarea {
        font-size: 16px; /* 防止iOS自动缩放 */
    }
}
