:root {
    /* Dark Theme - Professional & Modern */
    --bg-color: #0f1419;
    --surface-color: #1a1f28;
    --surface-light: #252d3a;
    --primary-color: #6366f1;
    --primary-variant: #4f46e5;
    --secondary-color: #06b6d4;
    --text-primary: #f1f5f9;
    --text-secondary: #94a3b8;
    --accent-gradient: linear-gradient(135deg, #6366f1, #06b6d4);
    --glass-bg: rgba(26, 31, 40, 0.9);
    --glass-border: rgba(148, 163, 184, 0.12);
    --msg-user-bg: #4f46e5;
    --msg-bot-bg: #252d3a;
    --transition-speed: 0.3s;
    --border-radius: 16px;
    --sidebar-width: 280px;
    --shadow-sm: 0 1px 2px 0 rgba(0, 0, 0, 0.3);
    --shadow-md: 0 4px 6px -1px rgba(0, 0, 0, 0.4);
    --shadow-lg: 0 10px 15px -3px rgba(0, 0, 0, 0.5);
    --modal-overlay-bg: rgba(11, 15, 20, 0.92);

    /* 🔧 [安全区域] 增加默认值防止布局塌陷 */
    --sat: env(safe-area-inset-top, 0px);
    --sar: env(safe-area-inset-right, 0px);
    --sab: env(safe-area-inset-bottom, 0px);
    --sal: env(safe-area-inset-left, 0px);

    /* 关键：强制锁定一个由于浏览器重绘可能丢失的计算值 */
    --safe-top: max(var(--sat), 0px);

    /* 🔑 [v7.0 键盘适配系统] */
    /* --multiplier: 补偿倍率，由 JS 控制 (0=系统Resize模式, 1=Overlay模式需CSS补偿) */
    --multiplier: 1;
    /* --kb-height: 当前键盘高度，由 JS 实时更新 */
    --kb-height: 0px;
    /* --kb-transition: 键盘相关元素的过渡设置，默认无动画 */
    --kb-transition: none;

    /* 🔑 [统一高度系统] 所有顶部栏的内容区基准高度 - 包含 4px 底部间距 */
    --header-content-height: 44px;
}

/* Light Theme - Clean & Professional */
body.light-theme {
    --bg-color: #f8fafc;
    --surface-color: #ffffff;
    --surface-light: #f1f5f9;
    --primary-color: #4f46e5;
    --primary-variant: #6366f1;
    --secondary-color: #0891b2;
    --text-primary: #1e293b;
    --text-secondary: #64748b;
    --accent-gradient: linear-gradient(135deg, #4f46e5, #0891b2);
    --glass-bg: rgba(255, 255, 255, 0.9);
    --glass-border: rgba(100, 116, 139, 0.15);
    --msg-user-bg: #4f46e5;
    --msg-bot-bg: #f1f5f9;
    --shadow-sm: 0 1px 2px 0 rgba(0, 0, 0, 0.05);
    --shadow-md: 0 4px 6px -1px rgba(0, 0, 0, 0.1);
    --shadow-lg: 0 10px 15px -3px rgba(0, 0, 0, 0.15);
    --modal-overlay-bg: rgba(248, 250, 252, 0.92);
}

html {
    scrollbar-gutter: stable;
}

* {
    margin: 0;
    padding: 0;
    box-sizing: border-box;
}

body {
    font-family: 'Inter', 'Outfit', system-ui, -apple-system, sans-serif;
    background-color: var(--bg-color);
    color: var(--text-primary);
    overflow: hidden;
    height: 100vh;
    height: 100dvh;
    width: 100vw;
    position: relative;
    -webkit-overflow-scrolling: touch;
    line-height: 1.5;
}

/* ==================== 全局加载遮罩 (Global Loading Overlay) ==================== */
.global-loading-overlay {
    position: fixed !important;
    top: 0;
    left: 0;
    width: 100vw;
    height: 100vh;
    background: rgba(10, 10, 15, 0.95) !important;
    /* 深色背景，高不透明度 */
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    z-index: 999999 !important;
    /* 绝对最高层级，凌驾于一切之上 */
    /* 性能优化：静态全屏遮罩只做深色，不渲染实时滤镜 */
    background: rgba(10, 10, 15, 0.98) !important;
    backdrop-filter: none;
}

.loading-retry-text {
    margin-top: 6px;
    min-height: 1.2em;
    color: var(--text-secondary);
    font-size: 0.85rem;
    opacity: 0.9;
    letter-spacing: 0.3px;
    text-align: center;
}

.loading-elapsed-text {
    margin-top: 4px;
    min-height: 1.2em;
    color: var(--text-secondary);
    font-size: 0.85rem;
    opacity: 0.9;
    text-align: center;
    white-space: nowrap;
    font-variant-numeric: tabular-nums;
    font-feature-settings: "tnum" 1;
}

/* ==================== 动态滚动条系统 ==================== */
/* 优化：始终保留滚动条布局空间，防止内容抖动 */
/* 默认状态：保留空间但不可见 */
.custom-scrollbar {
    scrollbar-width: thin !important;
    scrollbar-color: transparent transparent !important;
    position: relative;
}

/* Webkit: 始终显示轨道(占位)，但全透明 */
.custom-scrollbar::-webkit-scrollbar {
    width: 6px !important;
    height: 6px !important;
    display: block !important;
    background: transparent;
}

/* 轨道背景透明 */
.custom-scrollbar::-webkit-scrollbar-track {
    background: transparent !important;
}

/* 滑块默认透明 */
.custom-scrollbar::-webkit-scrollbar-thumb {
    background-color: transparent !important;
    border-radius: 20px !important;
    border: 1px solid transparent !important;
    background-clip: content-box !important;
    transition: background-color 0.2s;
}

/* --- 激活状态 (Scrolling) --- */
/* Firefox: 显示滑块颜色 */
.custom-scrollbar.scrolling {
    scrollbar-color: rgba(0, 0, 0, 0.2) transparent !important;
}

/* Webkit: 显示滑块颜色 */
.custom-scrollbar.scrolling::-webkit-scrollbar-thumb {
    background-color: rgba(0, 0, 0, 0.2) !important;
}

/* 悬停时加深 */
.custom-scrollbar.scrolling::-webkit-scrollbar-thumb:hover {
    background-color: rgba(0, 0, 0, 0.4) !important;
}

/* --- 暗色模式适配 --- */
body:not(.light-theme) .custom-scrollbar {
    scrollbar-color: transparent transparent !important;
}

/* 暗色模式下默认也是透明 */
body:not(.light-theme) .custom-scrollbar::-webkit-scrollbar-thumb {
    background-color: transparent !important;
}

/* 暗色模式滚动时显示白色半透明 */
body:not(.light-theme) .custom-scrollbar.scrolling {
    scrollbar-color: rgba(255, 255, 255, 0.2) transparent !important;
}

body:not(.light-theme) .custom-scrollbar.scrolling::-webkit-scrollbar-thumb {
    background-color: rgba(255, 255, 255, 0.2) !important;
}

body:not(.light-theme) .custom-scrollbar.scrolling::-webkit-scrollbar-thumb:hover {
    background-color: rgba(255, 255, 255, 0.4) !important;
}

/* 核心强制类应用 */
#chat-container,
#character-list,
.modal-body,
.char-modal-content,
#history-list,
#hall-characters-container,
.settings-content {
    overflow-y: auto !important;
    /* 🚀 核心优化：强制开启 GPU 渲染层，解决长列表滚动卡顿 */
    transform: translateZ(0);
    -webkit-overflow-scrolling: touch;
}

/* 确保这些元素默认应用 custom-scrollbar 类名 */
#chat-container,
#character-list,
.modal-body,
.char-modal-content {
    scrollbar-width: none;
    -ms-overflow-style: none;
}

#chat-container::-webkit-scrollbar,
#character-list::-webkit-scrollbar,
.modal-body::-webkit-scrollbar,
.char-modal-content::-webkit-scrollbar {
    display: none;
}

/* App Container */
.app-container {
    width: 100%;
    max-width: 100%;
    /* 🔑 [v7.0] 高度使用 dvh 单位，自动适应视口变化 */
    height: 100vh;
    height: 100dvh;
    box-sizing: border-box;
    display: flex;
    position: relative;
    background: transparent;
    /* 🔑 [v7.0 无动画] 禁用容器过渡 */
    transition: none !important;
}

@media (max-width: 768px) {
    .app-container {
        flex-direction: column;
    }
    
    /* 🔑 [v7.0 移动端键盘适配] 禁用所有可能影响布局的过渡动画 */
    .app-container,
    .main-chat,
    .chat-messages,
    .chat-input-area,
    .scroll-bottom-btn {
        transition: none !important;
        -webkit-transition: none !important;
    }
}

/* ==================== Z-Index 层级标准化体系 ==================== */
/* 
   - Global Loading Overlay: 999999
   - Toast/Global Notify: 100000
   - Confirm Modals: 90000
   - Model Hub (NEW): 85000
   - Character Hall: 80000
   - Settings / History Panels / Side Panel: 70000
   - Sidebar: 65000
   - Sidebar Overlay (Invisible Clicker): 60000
*/

.toast {
    z-index: 100000 !important;
}

#reset-confirm-modal,
#galgame-reset-modal,
.modal-overlay {
    z-index: 120000 !important;
}

#model-hub-modal {
    z-index: 125000 !important;
}

#character-hall-modal {
    z-index: 120000 !important;
}

.settings-panel,
.history-panel {
    z-index: 70000 !important;
}

.sidebar {
    z-index: 65000 !important;
}

.overlay {
    z-index: 60000 !important;
}

/* 全局遮罩层 (DIM Layer) - 性能优化：移除引起重绘的全屏模糊 */
.overlay.active {
    background: rgba(0, 0, 0, 0.5) !important;
    backdrop-filter: none !important;
    -webkit-backdrop-filter: none !important;
}

/* 亮色模式下也保持完全透明 */
body.light-theme .overlay.active {
    background: transparent !important;
    backdrop-filter: none !important;
}

#character-hall-modal {
    /* 🔑 层级由全局标准化体系管理 (80000) */
    background: #0b0f14 !important;
    backdrop-filter: none !important;
}

#character-hall-modal .modal-content {
    background: var(--surface-color) !important;
    border: none !important;
    width: 100% !important;
    height: 100% !important;
    max-width: none !important;
    max-height: none !important;
    border-radius: 0 !important;
    box-shadow: none !important;
    margin: 0 !important;
    /* 🔑 强制消除居中造成的卡片错觉 */
    display: flex;
    flex-direction: column;
    padding: 0 !important;
}

@media (max-width: 768px) {
    #character-hall-modal .modal-content {
        background: var(--surface-color) !important;
        border: none !important;
        border-radius: 0 !important;
    }
}

/* Overlay for mobile */
.overlay {
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background: transparent;
    z-index: 60000 !important;
    /* Standardization: Below Sidebar */
    display: none;
    opacity: 0;
    pointer-events: none;
    transition: opacity 0.3s;
}

.overlay.active,
.overlay.overlay.overlay.overlay.active {
    opacity: 1 !important;
    pointer-events: auto !important;
    display: block !important;
    visibility: visible !important;
    /* 移除这里的 background: transparent，让上面的定义生效 */
}

/* 亮色模式移动端适配 */
body.light-theme .overlay.active,
body.light-theme .overlay.overlay.overlay.overlay.active {
    background: transparent !important;
    backdrop-filter: none !important;
}

@keyframes spin {
    0% {
        transform: rotate(0deg);
    }

    100% {
        transform: rotate(360deg);
    }
}

/* ==================== 离线模式样式 (Offline Mode) ==================== */
/* 顶部提示条已移除，改用输入框内提示 - 见 chat.css */

/* 离线模式下禁用发送按钮 (变红但不透明) */
body.is-offline #send-btn,
body.is-offline #action-send {
    opacity: 1 !important;
    color: #ef4444 !important;
    pointer-events: none;
    cursor: not-allowed;
}

/* 离线模式下的输入框提示 */
body.is-offline #message-input::placeholder {
    color: #ef4444 !important;
}

/* ==================== 远程锁定样式 (Remote Locked) ==================== */
.remote-locked {
    opacity: 0.5 !important;
    pointer-events: none !important;
    cursor: not-allowed !important;
    filter: grayscale(100%) !important;
    position: relative;
}

/* 按钮锁定时的状态 */
button.remote-locked {
    background-color: var(--surface-light) !important;
    color: var(--text-secondary) !important;
    border-color: transparent !important;
    box-shadow: none !important;
}