/* ========================================
           CSS 变量定义区域
           定义全局颜色、间距、圆角、过渡效果等设计系统变量
           ======================================== */
        :root {
            --bg-primary: #1a1c23;
            --bg-secondary: #262933;
            --bg-tertiary: #2d303a;
            --bg-sidebar: #1f222b;
            --text-primary: #e0e0e0;
            --text-secondary: #aaa;
            --text-muted: #888;
            --accent-gold: #e6c78a;
            --accent-gold-rgb: 230, 199, 138;
            --border-color: #333;
            --border-light: rgba(255, 255, 255, 0.1);
            --shadow-lg: 0 10px 30px rgba(0, 0, 0, 0.5);
            --shadow-md: 0 4px 12px rgba(0, 0, 0, 0.3);
            --radius-sm: 6px;
            --radius-md: 8px;
            --radius-lg: 12px;
            --radius-xl: 20px;
            --transition-fast: 0.2s;
            --transition-normal: 0.3s;
            --spacing-xs: 5px;
            --spacing-sm: 8px;
            --spacing-md: 12px;
            --spacing-lg: 15px;
            --spacing-xl: 20px;
            --spacing-2xl: 25px;
            --font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
            --z-index-overlay: 100;
            --z-index-modal: 1000;
            --sidebar-width: 280px;
            --avatar-size-sm: 40px;
            --avatar-size-md: 45px;
            --bubble-max-width-desktop: 85%;
            --bubble-max-width-mobile: 90%;
        }

        /* ========================================
           全局重置样式
           移除所有元素的默认外边距和内边距，统一盒模型
           ======================================== */
        * { margin: 0; padding: 0; box-sizing: border-box; }

        /* ========================================
           主体容器样式
           设置页面背景、字体、高度，使用 Flexbox 居中布局
           ======================================== */
        html {
            height: 100%;
        }

        body {
            font-family: var(--font-family);
            background-color: var(--bg-primary);
            color: var(--text-primary);
            min-height: 100vh;
            min-height: 100dvh;
            display: flex;
            justify-content: center;
            align-items: center;
            overflow: hidden;
            padding: env(safe-area-inset-top) env(safe-area-inset-right) env(safe-area-inset-bottom) env(safe-area-inset-left);
        }

        /* ========================================
           应用主容器
           包含侧边栏和聊天区域的响应式容器
           ======================================== */
        .app-container {
            width: 95vw;
            height: 90vh;
            max-width: 1400px;
            background-color: var(--bg-secondary);
            border-radius: var(--radius-lg);
            box-shadow: var(--shadow-lg);
            display: flex;
            overflow: hidden;
            position: relative;
        }

        /* ========================================
           聊天区域样式
           占据剩余空间，包含头部、消息区和输入区
           支持背景图片自定义，带有半透明遮罩层
           ======================================== */
        .chat-area {
            flex-grow: 1;
            min-width: 0;
            display: flex;
            flex-direction: column;
            background-color: var(--bg-tertiary);
            background-size: cover;
            background-position: center;
            background-repeat: no-repeat;
            position: relative;
            transition: background-image 0.5s ease;
        }

        /* 聊天区域背景遮罩层，确保文字可读性 */
        .chat-area::before {
            content: '';
            position: absolute;
            top: 0; left: 0; right: 0; bottom: 0;
            background-color: rgba(45, 48, 58, 0.85);
            z-index: 0;
            pointer-events: none;
        }

        /* 确保头部、消息区和输入区在遮罩层之上 */
        .chat-header, .chat-messages, .chat-input-area {
            position: relative;
            z-index: 1;
        }

        /* ========================================
           侧边栏样式
           显示联系人列表、搜索框和功能按钮
           ======================================== */
        .sidebar {
            width: var(--sidebar-width);
            background-color: var(--bg-sidebar);
            display: flex;
            flex-direction: column;
            padding: var(--spacing-xl);
            border-right: 1px solid var(--border-color);
            z-index: 2;
            transition: transform var(--transition-normal) ease;
            flex-shrink: 0;
            position: relative;
        }


        .sidebar.hidden {
            transform: translateX(-100%);
            position: absolute;
            height: 100%;
            box-shadow: 2px 0 10px rgba(0,0,0,0.5);
        }

        .sidebar-header { font-size: 1.2em; font-weight: bold; margin-bottom: 15px; display: flex; align-items: center; gap: 10px; }


        .search-box-container {
            margin-bottom: 15px;
            position: relative;
        }
        .search-box {
            width: 100%;
            padding: 8px 12px 8px 35px;
            background-color: var(--bg-tertiary);
            border: 1px solid var(--border-color);
            border-radius: var(--radius-md);
            color: var(--text-primary);
            font-size: 0.9em;
            outline: none;
            transition: all var(--transition-fast);
        }
        .search-box:focus {
            border-color: var(--accent-gold);
            box-shadow: 0 0 8px rgba(230, 199, 138, 0.3);
        }
        .search-box::placeholder {
            color: var(--text-muted);
        }
        .search-icon {
            position: absolute;
            left: 10px;
            top: 50%;
            transform: translateY(-50%);
            color: var(--text-muted);
            font-size: 1em;
            pointer-events: none;
        }

        .friend-list { flex-grow: 1; overflow-y: auto; margin-bottom: 20px; }
        .friend-list::-webkit-scrollbar { width: 6px; }
        .friend-list::-webkit-scrollbar-track { background: rgba(0,0,0,0.2); border-radius: 3px; }
        .friend-list::-webkit-scrollbar-thumb { background-color: rgba(255,255,255,0.3); border-radius: 3px; transition: background-color 0.2s; }
        .friend-list::-webkit-scrollbar-thumb:hover { background-color: rgba(255,255,255,0.5); }
        .friend-item {
            display: flex; align-items: center; padding: var(--spacing-md) var(--spacing-sm);
            border-radius: var(--radius-md); cursor: pointer; transition: background-color var(--transition-fast);
            margin-bottom: var(--spacing-sm); position: relative;
        }
        .friend-item:hover { background-color: var(--bg-tertiary); }
        .friend-item.active { background-color: #3a3d47; border-left: 3px solid var(--accent-gold); }
        .friend-avatar { width: var(--avatar-size-sm); height: var(--avatar-size-sm); border-radius: 50%; object-fit: cover; margin-right: 12px; border: 2px solid transparent; }
        .friend-item.active .friend-avatar { border-color: var(--accent-gold); }
        .friend-name { font-size: 1em; font-weight: 500; }
        .friend-status { font-size: 0.75em; color: var(--text-muted); margin-left: auto; }
        .friend-pin {
            position: absolute;
            top: 5px;
            right: 5px;
            color: var(--accent-gold);
            font-size: 0.8em;
            display: none;
        }
        .friend-item.pinned .friend-pin { display: block; }

        .sidebar-footer { margin-top: auto; display: flex; justify-content: space-between; align-items: center; }
        .icon-btn {
            background: none; border: 1px solid #555; color: var(--text-secondary);
            border-radius: 50%; width: 40px; height: 40px;
            display: flex; justify-content: center; align-items: center;
            cursor: pointer; transition: all var(--transition-fast);
        }
        .icon-btn:hover { border-color: var(--accent-gold); color: #fff; transform: scale(1.1); }


        .mobile-menu-btn {
            display: none;
            position: absolute;
            top: 15px;
            left: 15px;
            z-index: 10;
            background-color: rgba(31, 34, 43, 0.9);
            border: 1px solid #555;
            color: #aaa;
            border-radius: 8px;
            width: 40px;
            height: 40px;
            font-size: 1.2em;
            cursor: pointer;
            transition: all 0.2s;
        }
        .mobile-menu-btn:hover {
            border-color: var(--accent-gold);
            color: #fff;
            background-color: rgba(31, 34, 43, 1);
        }


        .view-toggle-btn {
            position: absolute;
            top: 15px;
            right: 15px;
            z-index: 10;
            background-color: rgba(31, 34, 43, 0.9);
            border: 1px solid #555;
            color: #aaa;
            border-radius: 8px;
            width: 40px;
            height: 40px;
            font-size: 1.2em;
            cursor: pointer;
            transition: all 0.2s;
        }
        .view-toggle-btn:hover {
            border-color: var(--accent-gold);
            color: #fff;
            background-color: rgba(31, 34, 43, 1);
        }


        .sidebar-overlay {
            position: absolute;
            top: 0;
            left: 0;
            width: 100%;
            height: 100%;
            background-color: rgba(0, 0, 0, 0.5);
            z-index: 1;
            opacity: 0;
            visibility: hidden;
            transition: opacity 0.3s, visibility 0.3s;
            backdrop-filter: blur(2px);
        }
        .sidebar-overlay.show {
            opacity: 1;
            visibility: visible;
        }


        .friend-context-menu {
            position: fixed;
            background-color: #2d303a;
            border: 1px solid #444;
            border-radius: 8px;
            padding: 8px;
            display: none;
            flex-direction: column;
            gap: 5px;
            box-shadow: 0 5px 15px rgba(0,0,0,0.5);
            z-index: 1000;
            min-width: 140px;
        }
        .friend-context-menu.show { display: flex; }


        .friend-group-list {
            display: flex;
            flex-direction: column;
            gap: 8px;
        }
        .friend-group-option {
            display: flex;
            justify-content: space-between;
            align-items: center;
            padding: 12px 16px;
            border-radius: 8px;
            cursor: pointer;
            transition: all 0.2s;
            background-color: #383b45;
            border: 1px solid transparent;
        }
        .friend-group-option:hover {
            background-color: #4a4d57;
        }
        .friend-group-option.active {
            background-color: #e3f2fd;
            color: #1976d2;
            border-color: #1976d2;
        }
        .friend-group-option i {
            font-size: 1.2em;
        }


        .friend-group-header {
            display: flex;
            align-items: center;
            padding: 10px 8px 6px;
            margin-top: 12px;
            margin-bottom: 4px;
            color: #888;
            font-size: 0.85em;
            font-weight: 600;
            text-transform: uppercase;
            letter-spacing: 0.5px;
            border-bottom: 1px solid #3a3d47;
            cursor: pointer;
            user-select: none;
            transition: all 0.2s;
        }
        .friend-group-header:hover {
            color: var(--accent-gold);
            background-color: rgba(230, 199, 138, 0.1);
        }
        .friend-group-header:first-child {
            margin-top: 0;
        }
        .friend-group-header i {
            margin-right: 6px;
            font-size: 1em;
            transition: transform 0.3s;
        }
        .friend-group-header.collapsed i {
            transform: rotate(-90deg);
        }
        .friend-group-header .group-count {
            margin-left: auto;
            font-size: 0.9em;
            opacity: 0.7;
        }
        .friend-group-content {
            overflow: hidden;
            transition: max-height 0.3s ease-out;
        }
        .friend-group-content.collapsed {
            max-height: 0 !important;
        }
        .friend-menu-item {
            display: flex;
            align-items: center;
            padding: 8px 12px;
            border-radius: 6px;
            cursor: pointer;
            color: #ddd;
            font-size: 0.9em;
            transition: background-color 0.2s;
        }
        .friend-menu-item:hover { background-color: #3a3d47; color: var(--accent-gold); }
        .friend-menu-item i { margin-right: 10px; font-size: 1.1em; }
        .friend-menu-item.danger:hover { color: #f44336; }


        .chat-header {
            padding: 15px 25px; border-bottom: 1px solid rgba(255,255,255,0.1);
            font-size: 1.1em; font-weight: bold; display: flex;
            justify-content: space-between; align-items: center;
            backdrop-filter: blur(5px);
        }

        .editable-name {
            cursor: pointer; padding: 4px 8px; border-radius: 4px;
            transition: background-color 0.2s; display: inline-block;
        }
        .editable-name:hover { background-color: rgba(255,255,255,0.1); color: var(--accent-gold); }
        .name-input {
            background: transparent; border: 1px solid var(--accent-gold);
            color: var(--accent-gold); font-size: 1.1em; font-weight: bold;
            padding: 4px 8px; border-radius: 4px; outline: none; width: 150px;
        }

        .chat-messages {
            flex-grow: 1; min-height: 0; padding: 25px; overflow-y: auto;
            display: flex; flex-direction: column; gap: 20px;
            overscroll-behavior: contain;
            -webkit-overflow-scrolling: touch;
        }
        .chat-messages::-webkit-scrollbar { width: 8px; }
        .chat-messages::-webkit-scrollbar-track { background: rgba(0,0,0,0.2); }
        .chat-messages::-webkit-scrollbar-thumb { background-color: rgba(255,255,255,0.2); border-radius: 4px; }

        .message { display: flex; align-items: flex-start; max-width: 60%; animation: fadeIn 0.3s ease; }
        @keyframes fadeIn { from { opacity: 0; transform: translateY(10px); } to { opacity: 1; transform: translateY(0); } }

        .avatar {
            width: 45px; height: 45px; border-radius: 50%;
            object-fit: cover; flex-shrink: 0; cursor: pointer;
            transition: transform 0.2s; border: 2px solid transparent;
        }
        .avatar:hover { transform: scale(1.05); border-color: var(--accent-gold); }

        .message-content { display: flex; flex-direction: column; min-width: 0; max-width: 100%; }
        .nickname { font-size: 0.85em; color: #ccc; margin-bottom: 5px; text-shadow: 0 1px 2px rgba(0,0,0,0.8); }

        .bubble {
            padding: 12px 16px; border-radius: 12px; line-height: 1.5;
            word-wrap: break-word; overflow-wrap: break-word;
            max-width: 100%; position: relative;
            box-shadow: 0 2px 5px rgba(0,0,0,0.2);
            background-size: cover !important;
            background-position: center !important;
            background-repeat: no-repeat !important;
            transition: all 0.3s;
        }


        .bubble p { margin: 0 0 8px 0; }
        .bubble p:last-child { margin-bottom: 0; }
        .bubble h1, .bubble h2, .bubble h3, .bubble h4, .bubble h5, .bubble h6 {
            margin: 12px 0 8px 0;
            font-weight: bold;
            line-height: 1.4;
        }
        .bubble h1 { font-size: 1.5em; }
        .bubble h2 { font-size: 1.3em; }
        .bubble h3 { font-size: 1.2em; }
        .bubble ul, .bubble ol { margin: 8px 0; padding-left: 20px; }
        .bubble li { margin: 4px 0; }
        .bubble blockquote {
            border-left: 3px solid var(--accent-gold);
            padding-left: 12px;
            margin: 8px 0;
            color: var(--text-secondary);
            font-style: italic;
        }
        .bubble code {
            background-color: rgba(0,0,0,0.3);
            padding: 2px 6px;
            border-radius: 4px;
            font-family: 'Consolas', 'Monaco', monospace;
            font-size: 0.9em;
        }
        .bubble pre {
            background-color: rgba(0,0,0,0.4);
            padding: 12px;
            border-radius: 6px;
            overflow-x: auto;
            max-width: 100%;
            margin: 8px 0;
            -webkit-overflow-scrolling: touch;
        }
        .bubble pre code {
            background-color: transparent;
            padding: 0;
        }
        .bubble table {
            border-collapse: collapse;
            width: 100%;
            margin: 8px 0;
        }
        .bubble th, .bubble td {
            border: 1px solid rgba(255,255,255,0.2);
            padding: 8px;
            text-align: left;
        }
        .bubble th { background-color: rgba(230, 199, 138, 0.2); }
        .bubble hr {
            border: none;
            border-top: 1px solid rgba(255,255,255,0.2);
            margin: 12px 0;
        }
        .bubble a {
            color: var(--accent-gold);
            text-decoration: underline;
        }


        /* ========================================
           平板竖屏适配 (Portrait Tablet)
           针对 769px-1024px 且高度大于宽度的设备
           使用类似手机的布局体验
           ======================================== */
        @media (min-width: 769px) and (max-width: 1024px) and (orientation: portrait) {
            .app-container {
                width: 100vw;
                height: 100vh;
                height: 100dvh;
                max-width: none;
                border-radius: 0;
                box-shadow: none;
            }

            .sidebar {
                position: absolute;
                width: min(82vw, 340px);
                max-width: calc(100vw - 56px);
                height: 100%;
                z-index: 100;
                box-shadow: 2px 0 10px rgba(0,0,0,0.5);
            }

            .sidebar.hidden {
                transform: translateX(-105%);
            }

            .mobile-menu-btn {
                display: flex;
            }

            .chat-header {
                min-height: 56px;
                padding: 12px 50px 12px max(50px, env(safe-area-inset-left));
                font-size: 1em;
                gap: 8px;
            }

            #current-friend-name,
            #my-display-name {
                min-width: 0;
                overflow: hidden;
                text-overflow: ellipsis;
                white-space: nowrap;
            }

            .chat-messages {
                padding: 15px;
            }

            .chat-input-area {
                padding: 12px 15px;
                gap: 12px;
            }

            .input-wrapper input, .input-wrapper textarea {
                padding: 12px 14px;
                font-size: 1em;
            }

            .input-icons i {
                font-size: 1.2em;
            }

            #send-btn {
                font-size: 1.4em;
            }

            .message {
                max-width: 88%;
            }

            .avatar {
                width: 42px;
                height: 42px;
            }

            .bubble {
                padding: 11px 15px;
                font-size: 1em;
            }

            .modal-content {
                width: 90%;
                max-height: 85vh;
            }

            .avatar-context-menu, .ai-avatar-context-menu, .friend-context-menu {
                left: 15px !important;
                bottom: 70px !important;
                min-width: 150px;
            }

            .message-actions, .quick-replies {
                margin-left: 50px;
            }

            .view-toggle-btn {
                display: none;
            }
        }

        /* 平板横屏适配 (Landscape Tablet) - 保持桌面布局但优化间距 */
        @media (min-width: 769px) and (max-width: 1024px) and (orientation: landscape) {
            .app-container {
                width: 98vw;
                height: 95vh;
            }

            .sidebar {
                width: 240px;
                padding: var(--spacing-lg);
            }

            .bubble {
                max-width: 80% !important;
            }

            .message-actions, .quick-replies {
                margin-left: 50px;
            }
        }

        @media (min-width: 769px) {
            .bubble {
                max-width: var(--bubble-max-width, 85%) !important;
                width: fit-content;
            }
        }


        @media (max-width: 768px) {
            .bubble {
                max-width: 90% !important;
            }
        }


        .bubble.has-custom-bg {
            color: #fff;
            text-shadow: 0 1px 3px rgba(0,0,0,0.9);
            background-color: rgba(0,0,0,0.4) !important;
            border: 1px solid rgba(255,255,255,0.1);
        }

        .bubble.proactive {
            border: 1px solid var(--accent-gold);
            background: linear-gradient(135deg, rgba(230, 199, 138, 0.1), rgba(45, 48, 58, 0.4));
            box-shadow: 0 0 10px rgba(230, 199, 138, 0.2);
        }
        .proactive-tag {
            font-size: 0.7em;
            color: var(--accent-gold);
            margin-bottom: 4px;
            display: block;
            font-style: italic;
        }


        .message-actions {
            display: flex;
            gap: 8px;
            margin-top: 8px;
            margin-left: 57px;
        }
        .regenerate-btn {
            display: flex;
            align-items: center;
            gap: 4px;
            padding: 6px 12px;
            background: rgba(230, 199, 138, 0.15);
            border: 1px solid rgba(230, 199, 138, 0.3);
            border-radius: 6px;
            color: var(--accent-gold);
            font-size: 0.85em;
            cursor: pointer;
            transition: all 0.2s;
        }
        .regenerate-btn:hover {
            background: rgba(230, 199, 138, 0.25);
            transform: translateY(-1px);
        }
        .regenerate-btn:active { transform: translateY(0); }
        .regenerate-btn i { font-size: 0.9em; }


        .quick-replies {
            display: flex;
            gap: 10px;
            margin-top: 10px;
            margin-left: 57px;
            flex-wrap: wrap;
        }
        .quick-reply-btn {
            padding: 8px 16px;
            background: rgba(100, 150, 200, 0.2);
            border: 1px solid rgba(100, 150, 200, 0.4);
            border-radius: 20px;
            color: #a8d8ff;
            font-size: 0.9em;
            cursor: pointer;
            transition: all 0.2s;
            max-width: 70%;
            word-break: break-word;
        }
        .quick-reply-btn:hover {
            background: rgba(100, 150, 200, 0.35);
            transform: translateY(-2px);
            box-shadow: 0 4px 8px rgba(0,0,0,0.2);
        }

        .system-bubble {
            background-color: rgba(0,0,0,0.4);
            border: 1px dashed rgba(255,255,255,0.3);
            color: #ddd;
            font-size: 0.9em;
            text-align: center;
            width: 100%;
            max-width: 100% !important;
            align-self: center;
            margin: 10px 0;
            backdrop-filter: blur(2px);
        }

        .message-image {
            max-width: 100%;
            max-height: 300px;
            border-radius: 8px;
            display: block;
            box-shadow: 0 4px 8px rgba(0,0,0,0.3);
            cursor: pointer;
        }
        .message-image:hover { transform: scale(1.02); }

        .message.other { align-self: flex-start; }
        .message.other .bubble { background-color: var(--accent-gold); color: #333; border-top-left-radius: 2px; }
        .message.other .message-content { margin-left: 12px; }

        .message.mine { align-self: flex-end; flex-direction: row-reverse; }
        .message.mine .bubble { background-color: var(--accent-gold); color: #333; border-top-right-radius: 2px; }
        .message.mine .message-content { margin-right: 12px; align-items: flex-end; }

        .chat-input-area {
            padding: 15px 25px; border-top: 1px solid rgba(255,255,255,0.1);
            display: flex; align-items: center; gap: 15px;
            background-color: rgba(45, 48, 58, 0.9); backdrop-filter: blur(10px); position: relative;
            flex-direction: column;
        }
        .input-wrapper { flex-grow: 1; position: relative; display: flex; align-items: center; width: 100%; }
        .input-wrapper input, .input-wrapper textarea {
            width: 100%; padding: 12px 15px; border-radius: 20px;
            border: 1px solid rgba(255,255,255,0.2); background-color: rgba(0,0,0,0.3);
            color: #eee; outline: none;
        }
        .input-wrapper input::placeholder, .input-wrapper textarea::placeholder { color: #aaa; }
        .input-icons { display: flex; gap: 15px; color: #aaa; font-size: 1.2em; }
        .input-icons i { cursor: pointer; transition: color 0.2s; }
        .input-icons i:hover { color: #fff; }

        #image-upload, #avatar-upload, #bg-upload, #import-upload, #char-import-upload, #char-avatar-upload, #bubble-bg-upload { display: none; }

        .emoji-picker {
            position: absolute; bottom: 70px; left: 25px;
            background-color: #383b45; border: 1px solid #555;
            border-radius: 8px; padding: 10px;
            display: grid; grid-template-columns: repeat(8, 1fr); gap: 8px;
            box-shadow: 0 5px 15px rgba(0,0,0,0.3); z-index: 100;
            display: none; max-height: 250px; overflow-y: auto;
        }
        .emoji-picker.show { display: grid; }
        .emoji-item { font-size: 1.5em; cursor: pointer; text-align: center; padding: 5px; border-radius: 4px; }
        .emoji-item:hover { background-color: #4a4d58; }


        .avatar-context-menu {
            position: absolute;
            bottom: 80px;
            left: 25px;
            background-color: #2d303a;
            border: 1px solid #444;
            border-radius: 8px;
            padding: 8px;
            display: none;
            flex-direction: column;
            gap: 5px;
            box-shadow: 0 5px 15px rgba(0,0,0,0.5);
            z-index: 101;
            min-width: 160px;
        }
        .avatar-context-menu.show { display: flex; }
        .avatar-menu-item {
            display: flex;
            align-items: center;
            padding: 8px 12px;
            border-radius: 6px;
            cursor: pointer;
            color: #ddd;
            font-size: 0.9em;
            transition: background-color 0.2s;
        }
        .avatar-menu-item:hover { background-color: #3a3d47; color: var(--accent-gold); }
        .avatar-menu-item i { margin-right: 10px; font-size: 1.1em; }


        .ai-avatar-context-menu {
            position: absolute;
            background-color: #2d303a;
            border: 1px solid #444;
            border-radius: 8px;
            padding: 8px;
            display: none;
            flex-direction: column;
            gap: 5px;
            box-shadow: 0 5px 15px rgba(0,0,0,0.5);
            z-index: 102;
            min-width: 160px;
        }
        .ai-avatar-context-menu.show { display: flex; }


        .modal-overlay {
            position: fixed; top: 0; left: 0; width: 100%; height: 100%;
            background-color: rgba(0, 0, 0, 0.6);
            display: flex; justify-content: center; align-items: center;
            z-index: 1000; opacity: 0; visibility: hidden;
            transition: opacity 0.3s, visibility 0.3s;
            backdrop-filter: blur(3px);
        }
        .modal-overlay.show { opacity: 1; visibility: visible; }

        .modal-content {
            background-color: #2d303a;
            width: 500px;
            max-width: 90%;
            max-height: 85vh;
            border-radius: 12px;
            box-shadow: 0 10px 30px rgba(0, 0, 0, 0.5);
            display: flex;
            flex-direction: column;
            transform: translateY(-20px);
            transition: transform 0.3s;
            border: 1px solid #444;
        }
        .modal-overlay.show .modal-content { transform: translateY(0); }

        .modal-header {
            padding: 20px;
            border-bottom: 1px solid #3a3d47;
            display: flex;
            justify-content: space-between;
            align-items: center;
            flex-shrink: 0;
        }
        .modal-header h2 { font-size: 1.5em; }
        .close-modal { background: none; border: none; color: #aaa; font-size: 1.5em; cursor: pointer; }
        .close-modal:hover { color: #fff; }

        .modal-body {
            padding: 25px;
            flex-grow: 1;
            overflow-y: auto;
            scrollbar-width: thin;
            scrollbar-color: #555 #2d303a;
        }

        .modal-body::-webkit-scrollbar { width: 6px; }
        .modal-body::-webkit-scrollbar-track { background: #2d303a; border-radius: 3px; }
        .modal-body::-webkit-scrollbar-thumb { background-color: #555; border-radius: 3px; }
        .modal-body::-webkit-scrollbar-thumb:hover { background-color: #777; }

        .settings-tabs { display: flex; gap: 15px; margin-bottom: 25px; border-bottom: 1px solid #3a3d47; overflow-x: auto; }
        .tab-button {
            background: none; border: none; color: #aaa; font-size: 1em;
            padding-bottom: 10px; cursor: pointer; position: relative;
            white-space: nowrap;
        }
        .tab-button.active { color: var(--accent-gold); font-weight: bold; }
        .tab-button.active::after {
            content: ''; position: absolute; bottom: -1px; left: 0;
            width: 100%; height: 2px; background-color: var(--accent-gold);
        }
        .tab-content { display: none; }
        .tab-content.active { display: block; }

        .form-group { margin-bottom: 20px; }
        .form-group label { display: block; margin-bottom: 8px; color: #aaa; font-size: 0.9em; }
        .form-group input, .form-group select, .form-group textarea {
            width: 100%; padding: 10px 12px; border-radius: 6px;
            border: 1px solid #444; background-color: #383b45; color: #eee; outline: none;
        }
        .form-group textarea { resize: vertical; min-height: 80px; font-family: inherit; }
        .form-group input:focus, .form-group select:focus, .form-group textarea:focus { border-color: var(--accent-gold); }

        .modal-footer {
            padding: 20px;
            border-top: 1px solid #3a3d47;
            display: flex;
            justify-content: space-between;
            align-items: center;
            flex-shrink: 0;
        }
        .btn {
            padding: 10px 20px; border-radius: 6px; border: none;
            font-size: 1em; cursor: pointer; display: flex; align-items: center; gap: 8px;
        }
        .btn-secondary { background-color: #555; color: #eee; }
        .btn-secondary:hover { background-color: #666; }
        .btn-primary { background-color: #4CAF50; color: white; }
        .btn-primary:hover { background-color: #45a049; }
        .btn-test { background-color: #2196F3; color: white; }
        .btn-test:hover { background-color: #1e88e5; }
        .btn-danger { background-color: #f44336; color: white; }
        .btn-danger:hover { background-color: #d32f2f; }
        .btn-outline { background: transparent; border: 1px solid #666; color: #ddd; }
        .btn-outline:hover { border-color: var(--accent-gold); color: var(--accent-gold); }
        .btn-success { background-color: #28a745; color: white; }
        .btn-success:hover { background-color: #218838; }
        .btn-sm { padding: 5px 10px; font-size: 0.85em; }

        .test-result { font-size: 0.9em; margin-top: 5px; min-height: 1.2em; word-break: break-word; display: flex; align-items: center; gap: 8px; }
        .test-result.success { color: #4CAF50; }
        .test-result.error { color: #f44336; }
        .test-result.info { color: #2196F3; }
        .test-result.warning { color: #ff9800; }

        @keyframes spin { 0% { transform: rotate(0deg); } 100% { transform: rotate(360deg); } }
        .loading-spinner {
            display: inline-block;
            width: 14px;
            height: 14px;
            border: 2px solid rgba(255,255,255,0.3);
            border-radius: 50%;
            border-top-color: #fff;
            animation: spin 0.8s linear infinite;
            vertical-align: middle;
            margin-right: 6px;
        }


        @keyframes thinking-dot {
            0%, 100% { transform: translateY(0); opacity: 0.4; }
            50% { transform: translateY(-4px); opacity: 1; }
        }
        .thinking-dots {
            display: inline-flex;
            gap: 4px;
            align-items: center;
            justify-content: center;
            height: 20px;
        }
        .thinking-dots span {
            display: inline-block;
            width: 6px;
            height: 6px;
            background-color: #aaa;
            border-radius: 50%;
            animation: thinking-dot 1.4s ease-in-out infinite;
        }
        .thinking-dots span:nth-child(2) { animation-delay: 0.2s; }
        .thinking-dots span:nth-child(3) { animation-delay: 0.4s; }


        .config-export-btn, .config-import-label {
            background: #2d303a;
            color: var(--accent-gold);
            border: 1px solid #444;
            padding: 8px 16px;
            border-radius: 8px;
            cursor: pointer;
            font-size: 0.9em;
            display: inline-flex;
            align-items: center;
            gap: 6px;
            transition: all 0.2s;
            margin-right: 10px;
        }
        .config-export-btn:hover, .config-import-label:hover {
            background: #3a3d4a;
            border-color: var(--accent-gold);
            transform: translateY(-2px);
            box-shadow: 0 4px 12px rgba(230, 199, 138, 0.2);
        }
        #config-import-input {
            display: none;
        }


        .shortcut-hint {
            font-size: 0.75em;
            color: #888;
            margin-left: 8px;
            padding: 2px 6px;
            background: rgba(255,255,255,0.1);
            border-radius: 4px;
        }

        .import-section {
            border: 1px dashed #555;
            border-radius: 8px;
            padding: 20px;
            text-align: center;
            margin-bottom: 20px;
            transition: all 0.2s;
        }
        .import-section:hover {
            border-color: var(--accent-gold);
            background-color: rgba(230, 199, 138, 0.05);
        }
        .import-section p { color: #888; font-size: 0.85em; margin-bottom: 15px; }
        .import-hint { font-size: 0.8em; color: #666; margin-top: 10px; }

        .memory-list, .context-edit-list {
            max-height: 200px;
            overflow-y: auto;
            border: 1px solid #444;
            border-radius: 6px;
            padding: 10px;
            background: #262933;
            margin-top: 10px;
        }
        .memory-item, .context-item {
            display: flex;
            justify-content: space-between;
            align-items: center;
            padding: 8px;
            border-bottom: 1px solid #333;
            font-size: 0.9em;
        }
        .memory-item:last-child, .context-item:last-child { border-bottom: none; }
        .memory-key { color: var(--accent-gold); font-weight: bold; margin-right: 10px; }
        .memory-val { color: #ccc; flex: 1; }
        .del-memory, .del-context, .edit-context {
            cursor: pointer; font-size: 1.2em; padding: 0 5px;
        }
        .del-memory, .del-context { color: #f44336; }
        .del-memory:hover, .del-context:hover { color: #ff6b6b; }
        .edit-context { color: #4CAF50; }
        .edit-context:hover { color: #66bb6a; }

        .context-text {
            flex: 1;
            margin-right: 10px;
            white-space: nowrap;
            overflow: hidden;
            text-overflow: ellipsis;
            color: #ddd;
        }
        .context-role {
            font-weight: bold;
            margin-right: 5px;
            font-size: 0.8em;
            padding: 2px 4px;
            border-radius: 3px;
        }
        .role-mine { background-color: #3a3d47; color: var(--accent-gold); }
        .role-other { background-color: #3a3d47; color: #aaa; }


        .add-friend-modal .modal-content {
            width: 400px;
            max-height: 80vh;
            display: flex;
            flex-direction: column;
        }
        .add-friend-modal .modal-body {
            flex: 1;
            overflow-y: auto;
            padding: 20px;
            position: relative;
        }

        .add-friend-footer {
            padding: 15px 20px;
            border-top: 1px solid #444;
            background-color: #262933;
            flex-shrink: 0;
            display: flex;
            flex-direction: column;
            gap: 10px;
        }

        #friend-options-list {
            display: flex;
            flex-direction: column;
            gap: 10px;
            min-height: 100px;
        }

        .friend-option {
            display: flex;
            align-items: center;
            padding: 15px;
            border: 1px solid #444;
            border-radius: 8px;
            margin-bottom: 10px;
            cursor: pointer;
            transition: background-color 0.2s;
            flex-shrink: 0;
            position: relative;
        }
        .friend-option:hover {
            background-color: #383b45;
            border-color: var(--accent-gold);
        }
        .friend-option img {
            width: 50px;
            height: 50px;
            border-radius: 50%;
            margin-right: 15px;
            object-fit: cover;
            flex-shrink: 0;
        }
        .friend-option-info {
            flex: 1;
            overflow: hidden;
            margin-right: 10px;
        }
        .friend-option-info h3 {
            font-size: 1.1em;
            margin-bottom: 5px;
            white-space: nowrap;
            overflow: hidden;
            text-overflow: ellipsis;
        }
        .friend-option-info p {
            font-size: 0.85em;
            color: #aaa;
            white-space: nowrap;
            overflow: hidden;
            text-overflow: ellipsis;
        }
        .custom-badge {
            position: absolute;
            top: 5px;
            right: 5px;
            background-color: var(--accent-gold);
            color: #333;
            font-size: 0.7em;
            padding: 2px 6px;
            border-radius: 4px;
            font-weight: bold;
        }

        .export-char-btn {
            background: none;
            border: 1px solid #555;
            color: #aaa;
            border-radius: 50%;
            width: 32px;
            height: 32px;
            display: flex;
            justify-content: center;
            align-items: center;
            cursor: pointer;
            transition: all 0.2s;
            flex-shrink: 0;
            font-size: 1.1em;
        }
        .export-char-btn:hover {
            border-color: var(--accent-gold);
            color: var(--accent-gold);
            background-color: rgba(230, 199, 138, 0.1);
        }

       /* 创建/编辑角色弹窗样式 */
        .character-edit-modal .modal-content {
            width: 500px;
            max-height: 85vh;
        }

        .create-character-section {
            margin-top: 20px;
            padding-top: 20px;
            border-top: 1px dashed #444;
        }
        .create-character-section h3 {
            color: var(--accent-gold);
            margin-bottom: 15px;
            font-size: 1.1em;
        }
        .import-char-section {
            margin-top: 15px;
            padding: 15px;
            border: 1px dashed #666;
            border-radius: 8px;
            text-align: center;
            background-color: rgba(0,0,0,0.1);
        }
        .import-char-section p {
            font-size: 0.85em;
            color: #aaa;
            margin-bottom: 10px;
        }


        .avatar-select-group {
            display: flex;
            gap: 10px;
            align-items: center;
        }
        .avatar-select-group input {
            flex: 1;
        }
        .avatar-select-btn {
            padding: 10px 15px;
            border-radius: 6px;
            border: 1px solid #555;
            background-color: #383b45;
            color: #ddd;
            cursor: pointer;
            display: flex;
            align-items: center;
            gap: 5px;
            font-size: 0.9em;
            transition: all 0.2s;
        }
        .avatar-select-btn:hover {
            border-color: var(--accent-gold);
            color: var(--accent-gold);
            background-color: rgba(230, 199, 138, 0.1);
        }


        @media (max-width: 768px) {
            body {
                padding: 0;
            }

            .app-container,
            .app-container.mobile-view {
                width: 100vw;
                height: 100vh;
                height: 100dvh;
                border-radius: 0;
                box-shadow: none;
            }

            .sidebar,
            .app-container.mobile-view .sidebar {
                position: absolute;
                width: min(86vw, 320px);
                max-width: calc(100vw - 52px);
                height: 100%;
                z-index: 100;
                box-shadow: 2px 0 10px rgba(0,0,0,0.5);
            }

            .mobile-menu-btn {
                display: flex;
            }

            .chat-header {
                min-height: 56px;
                padding: 12px 50px 12px max(50px, env(safe-area-inset-left));
                font-size: 1em;
                gap: 8px;
            }

            #current-friend-name,
            #my-display-name {
                min-width: 0;
                overflow: hidden;
                text-overflow: ellipsis;
                white-space: nowrap;
            }

            .chat-messages {
                padding: 14px max(12px, env(safe-area-inset-right)) 14px max(12px, env(safe-area-inset-left));
                gap: 14px;
            }

            .chat-input-area {
                padding: 10px max(12px, env(safe-area-inset-right)) calc(10px + env(safe-area-inset-bottom)) max(12px, env(safe-area-inset-left));
                gap: 10px;
            }

            .input-wrapper input, .input-wrapper textarea {
                padding: 10px 12px;
                font-size: 0.9em;
            }

            .input-icons i {
                font-size: 1.1em;
            }

            #send-btn {
                font-size: 1.3em;
            }

            .message {
                max-width: 94%;
            }

            .avatar {
                width: 38px;
                height: 38px;
            }

            .bubble {
                padding: 9px 12px;
                font-size: 0.9em;
            }

            .modal-content {
                width: 95%;
                max-height: 90vh;
                padding: 15px;
            }

            .modal-header {
                padding: 15px;
            }

            .modal-header h2 {
                font-size: 1.3em;
            }

            .modal-body {
                padding: 15px;
                font-size: 0.9em;
                line-height: 1.6;
            }

            .modal-body h3 {
                font-size: 1.1em;
                margin-top: 15px;
            }

            .modal-body p {
                font-size: 0.9em;
            }

            .modal-footer {
                padding: 15px;
            }

            .avatar-context-menu, .ai-avatar-context-menu, .friend-context-menu {
                left: 15px !important;
                bottom: 70px !important;
                min-width: 140px;
            }

            .message-actions, .quick-replies {
                margin-left: 52px;
            }

            .disclaimer-text {
                font-size: 0.7em !important;
                padding: 0 10px;
            }
        }


        .app-container.mobile-view {
            width: min(100vw, 430px);
            height: min(100vh, 844px);
            height: min(100dvh, 844px);
            max-width: none;
        }

        .app-container.mobile-view .sidebar {
            position: absolute;
            height: 100%;
            z-index: 100;
            box-shadow: 2px 0 10px rgba(0,0,0,0.5);
        }

        .app-container.mobile-view .mobile-menu-btn {
            display: flex;
        }

        .app-container.mobile-view .chat-header {
            min-height: 56px;
            padding: 12px 50px 12px 50px;
            font-size: 1em;
        }

        @media (max-width: 768px), (min-width: 769px) and (max-width: 1024px) and (orientation: portrait) {
            body {
                padding: 0;
            }

            .app-container.mobile-view {
                width: 100vw;
                height: 100vh;
                height: 100dvh;
                border-radius: 0;
                box-shadow: none;
            }
        }


        .toast-container {
            position: fixed;
            bottom: 30px;
            left: 50%;
            transform: translateX(-50%) translateY(100px);
            background-color: rgba(45, 48, 58, 0.95);
            border: 1px solid var(--accent-gold);
            border-radius: var(--radius-md);
            padding: 12px 24px;
            color: var(--text-primary);
            font-size: 0.95em;
            box-shadow: 0 5px 20px rgba(0, 0, 0, 0.6);
            z-index: 10000;
            opacity: 0;
            visibility: hidden;
            transition: all 0.3s ease;
            display: flex;
            align-items: center;
            gap: 10px;
            backdrop-filter: blur(10px);
        }
        .toast-container.show {
            opacity: 1;
            visibility: visible;
            transform: translateX(-50%) translateY(0);
        }
        .toast-container i {
            font-size: 1.2em;
            color: var(--accent-gold);
        }

/* 拆分后的增强功能样式 */
.small-icon-btn {
    width: 28px;
    height: 28px;
    font-size: 0.85em;
    margin-left: auto;
}

body.light-theme {
    --bg-primary: #f4f6fb;
    --bg-secondary: #ffffff;
    --bg-tertiary: #eef1f7;
    --bg-sidebar: #f9fafc;
    --text-primary: #20242c;
    --text-secondary: #4f5b6b;
    --text-muted: #6f7c8d;
    --border-color: #d9deea;
    --border-light: rgba(0, 0, 0, 0.1);
    --shadow-lg: 0 10px 30px rgba(20, 30, 50, 0.15);
    --shadow-md: 0 4px 12px rgba(20, 30, 50, 0.12);
}

body.light-theme .chat-area::before {
    background-color: rgba(238, 241, 247, 0.82);
}

.global-search-results {
    max-height: 220px;
    overflow-y: auto;
    margin: -5px 0 12px;
    border: 1px solid var(--border-color);
    border-radius: var(--radius-md);
    background: var(--bg-tertiary);
    padding: 8px;
}

.search-result-group-title {
    color: var(--accent-gold);
    font-size: 0.82em;
    margin: 6px 0 4px;
    font-weight: 600;
}

.search-result-item {
    border: 1px solid transparent;
    border-radius: var(--radius-sm);
    padding: 7px;
    margin-bottom: 5px;
    cursor: pointer;
    color: var(--text-secondary);
    font-size: 0.84em;
    line-height: 1.35;
}

.search-result-item:hover {
    border-color: var(--accent-gold);
    color: var(--text-primary);
}

.message.search-highlight .bubble {
    outline: 2px solid var(--accent-gold);
    box-shadow: 0 0 14px rgba(230, 199, 138, 0.55);
}

.code-block-wrapper {
    position: relative;
}

.copy-code-btn {
    position: absolute;
    top: 6px;
    right: 6px;
    border: 1px solid rgba(255,255,255,0.2);
    background: rgba(0,0,0,0.55);
    color: #fff;
    border-radius: 5px;
    padding: 4px 8px;
    cursor: pointer;
    font-size: 0.75em;
    z-index: 2;
}

.command-panel {
    position: absolute;
    right: 18px;
    bottom: 86px;
    width: min(360px, calc(100vw - 40px));
    max-height: 320px;
    overflow-y: auto;
    background: var(--bg-secondary);
    border: 1px solid var(--border-color);
    border-radius: var(--radius-lg);
    box-shadow: var(--shadow-lg);
    padding: 12px;
    z-index: 5;
}

.command-panel.show { display: block; }
.command-card {
    padding: 10px;
    border-radius: var(--radius-md);
    border: 1px solid var(--border-color);
    margin-bottom: 8px;
    cursor: pointer;
}
.command-card:hover { border-color: var(--accent-gold); }
.command-card code { color: var(--accent-gold); }

@media print {
    body * { visibility: hidden; }
    #chat-messages, #chat-messages * { visibility: visible; }
    #chat-messages {
        position: absolute;
        inset: 0;
        overflow: visible !important;
        color: #111;
        background: white;
    }
    .message-actions, .chat-input-area, .sidebar, .chat-header { display: none !important; }
}
.friend-batch-checkbox {
    width: 18px;
    height: 18px;
    accent-color: var(--accent-gold);
    flex-shrink: 0;
}
.input-icons .recording,
#voice-input-btn.recording {
    color: #ff6b6b !important;
    animation: pulseMic 1s infinite;
}
@keyframes pulseMic {
    0%, 100% { transform: scale(1); }
    50% { transform: scale(1.15); }
}
.emoji-category-title {
    grid-column: 1 / -1;
    color: var(--accent-gold);
    font-size: 0.82em;
    padding: 6px 4px 2px;
}
.feature-toggle-grid {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(180px, 1fr));
    gap: 10px;
}
.feature-toggle-grid label {
    display: flex;
    align-items: center;
    gap: 8px;
    padding: 8px;
    border: 1px solid var(--border-color);
    border-radius: var(--radius-sm);
    background: rgba(255,255,255,0.03);
}

/* 离线提示与多行输入框 */
.offline-banner {
    padding: 8px 16px;
    background: rgba(255, 176, 32, 0.16);
    border-bottom: 1px solid rgba(255, 176, 32, 0.35);
    color: #ffd28a;
    font-size: 0.88em;
    text-align: center;
}

.input-wrapper textarea {
    width: 100%;
    min-height: 44px;
    max-height: 140px;
    padding: 12px 15px;
    border-radius: 20px;
    border: 1px solid rgba(255,255,255,0.2);
    background-color: rgba(0,0,0,0.3);
    color: #eee;
    outline: none;
    resize: none;
    line-height: 1.45;
    font-family: inherit;
}

.input-wrapper textarea::placeholder { color: #aaa; }

#send-btn.disabled {
    opacity: 0.55;
}

.chat-search-bar {
    display: flex;
    align-items: center;
    gap: 8px;
    padding: 8px 16px;
    background: rgba(38, 41, 51, 0.96);
    border-bottom: 1px solid rgba(255,255,255,0.08);
}
.chat-search-bar input {
    flex: 1;
    min-width: 0;
    padding: 8px 10px;
    border-radius: 14px;
    border: 1px solid rgba(255,255,255,0.18);
    background: rgba(0,0,0,0.25);
    color: #eee;
    outline: none;
}
.chat-search-bar .icon-btn { width: 28px; height: 28px; font-size: 0.9em; }
.message.search-match .bubble { box-shadow: 0 0 0 2px rgba(255, 211, 105, 0.75); }
.message.search-active .bubble { box-shadow: 0 0 0 3px var(--accent-gold), 0 0 18px rgba(230, 199, 138, 0.45); }
.audio-bubble audio { max-width: 260px; width: 100%; }
.message-meta { font-size: 0.72em; color: #888; margin-top: 4px; text-align: right; }
.message.mine .message-meta { text-align: right; }
.shortcut-list { list-style: none; display: flex; flex-direction: column; gap: 12px; }
.shortcut-list li { display: flex; align-items: center; justify-content: space-between; gap: 12px; }
kbd { background: #1f222b; border: 1px solid #555; border-bottom-width: 2px; border-radius: 5px; padding: 2px 6px; color: var(--accent-gold); font-family: inherit; }


.reply-preview {
    margin: 2px 0 6px;
    padding: 6px 10px;
    border-left: 3px solid var(--accent-gold);
    background: rgba(230, 199, 138, 0.12);
    color: #cfc6b0;
    border-radius: 6px;
    font-size: 0.82em;
    max-width: var(--bubble-max-width);
    word-break: break-word;
}
.markdown-preview-popover {
    position: absolute;
    left: 0;
    right: 42px;
    bottom: calc(100% + 8px);
    max-height: 220px;
    overflow: auto;
    padding: 12px;
    border: 1px solid rgba(230, 199, 138, 0.35);
    border-radius: 10px;
    background: rgba(26, 28, 35, 0.96);
    box-shadow: 0 10px 30px rgba(0, 0, 0, 0.35);
    color: #eee;
    z-index: 20;
}
.markdown-preview-popover[hidden] { display: none; }
.memory-tag { display: inline-block; margin-right: 4px; color: var(--accent-gold); }
