        :root {
            --bg-dark: #1e1f22;
            --bg-panel: #2b2d31;
            --bg-input: #1e1f22;
            --accent-color: #5865F2;
            --accent-hover: #4752c4;
            --text-primary: #dbdee1;
            --text-secondary: #949ba4;
            --msg-bg-incoming: #2b2d31;
            --msg-bg-outgoing: #5865F2;
            --border-radius: 12px;
            --shadow: 0 4px 12px rgba(0, 0, 0, 0.2);
            --error-color: #ed4245;
            --success-color: #3ba55c;
            --new-chat-highlight: #3ba55c;
            --reply-bg: #1a1b1e;
            --edited-label: #949ba4;
            --group-icon-bg: #eb459e;
        }

        * { box-sizing: border-box; margin: 0; padding: 0; font-family: 'Segoe UI', Roboto, Helvetica, Arial, sans-serif; }

        body {
            background-color: var(--bg-dark);
            color: var(--text-primary);
            height: 100vh;
            overflow: hidden;
            display: flex;
            justify-content: center;
            align-items: center;
        }

        .auth-container {
            width: 100%; max-width: 400px; padding: 30px;
            background-color: var(--bg-panel);
            border-radius: var(--border-radius);
            box-shadow: var(--shadow);
            text-align: center;
            display: none;
        }
        .auth-container.active { display: block; }
        .auth-title { font-size: 24px; margin-bottom: 20px; font-weight: 600; }
        .form-group { margin-bottom: 16px; text-align: left; }
        .form-label { display: block; margin-bottom: 6px; font-size: 14px; color: var(--text-secondary); }
        .form-input {
            width: 100%; padding: 12px 16px; background-color: var(--bg-dark);
            border: 1px solid transparent; border-radius: 8px;
            color: var(--text-primary); outline: none; transition: all 0.3s ease;
        }
        .form-input:focus { border-color: var(--accent-color); box-shadow: 0 0 0 3px rgba(88, 101, 242, 0.2); }
        .btn-primary {
            width: 100%; padding: 12px; background-color: var(--accent-color);
            color: white; border: none; border-radius: 8px;
            font-size: 16px; font-weight: 600; cursor: pointer; transition: background 0.2s; margin-top: 10px;
        }
        .btn-primary:hover { background-color: var(--accent-hover); }
        .btn-primary:disabled { background-color: var(--text-secondary); cursor: not-allowed; }
        .auth-switch { margin-top: 16px; font-size: 14px; color: var(--text-secondary); }
        .auth-switch a { color: var(--accent-color); text-decoration: none; cursor: pointer; }
        .auth-switch a:hover { text-decoration: underline; }
        .error-msg { color: var(--error-color); font-size: 13px; margin-top: 4px; display: none; }

        .app-layout { display: none; width: 100%; height: 100vh; }
        .app-layout.active { display: flex; }

        .sidebar {
            width: 280px; background-color: #1e1f22; border-right: 1px solid #1f2023;
            display: flex; flex-direction: column; padding: 16px; transition: transform 0.3s ease;
        }
        .app-header { display: flex; justify-content: space-between; align-items: center; margin-bottom: 20px; }
        .app-title { font-size: 18px; font-weight: 700; display: flex; align-items: center; gap: 10px; }
        .sidebar-back-btn {
            width: 40px;
            height: 40px;
            border-radius: 50%;
            border: none;
            background: var(--bg-panel);
            color: var(--text-primary);
            display: inline-flex;
            align-items: center;
            justify-content: center;
            cursor: pointer;
            transition: transform 0.2s, background 0.2s;
            flex-shrink: 0;
        }
        .sidebar-back-btn:hover {
            background: #35373c;
            transform: translateY(-1px);
        }

        .user-profile {
            display: flex; align-items: center; padding: 12px;
            background-color: var(--bg-panel); border-radius: var(--border-radius); margin-bottom: 16px;
            gap: 12px;
        }
        .user-profile-main {
            display: flex;
            align-items: center;
            min-width: 0;
            flex: 1;
        }
        .user-profile-settings-btn {
            width: 40px;
            height: 40px;
            border-radius: 50%;
            border: none;
            background: var(--bg-panel);
            color: var(--text-primary);
            display: inline-flex;
            align-items: center;
            justify-content: center;
            cursor: pointer;
            transition: transform 0.2s, background 0.2s;
            flex-shrink: 0;
        }
        .user-profile-settings-btn:hover {
            background: #35373c;
            transform: translateY(-1px);
        }
        .user-avatar { width: 40px; height: 40px; border-radius: 50%; margin-right: 12px; }
        .user-info h4 { font-size: 14px; font-weight: 600; }
        .user-info p { font-size: 12px; color: var(--text-secondary); }
        
        .sidebar-actions {
            display: flex;
            gap: 10px;
            margin-bottom: 10px;
        }
        .sidebar-btn {
            flex: 1;
            padding: 8px;
            background-color: var(--bg-panel);
            border: 1px solid #3f4147;
            color: var(--text-primary);
            border-radius: 6px;
            cursor: pointer;
            font-size: 12px;
            transition: 0.2s;
        }
        .sidebar-btn:hover { background-color: #3f4147; }

        .chat-list { list-style: none; overflow-y: auto; flex: 1; }
        .chat-item {
            display: flex; align-items: center; padding: 12px; border-radius: var(--border-radius);
            cursor: pointer; transition: background 0.2s; margin-bottom: 4px; position: relative;
        }
        .chat-item:hover, .chat-item.active { 
            background-color: var(--bg-panel); 
        }
        .chat-item.new-chat {
            animation: highlightNewChat 2s ease-out;
        }
        @keyframes highlightNewChat {
            0% { background-color: var(--new-chat-highlight); }
            100% { background-color: transparent; }
        }
        .avatar {
            width: 40px; height: 40px; border-radius: 50%;
            background: linear-gradient(135deg, #ff6b6b, #feca57);
            margin-right: 12px; flex-shrink: 0; object-fit: cover;
        }
        .avatar.group-avatar {
            background: var(--group-icon-bg);
            display: flex;
            align-items: center;
            justify-content: center;
            font-weight: bold;
            color: white;
            font-size: 18px;
        }
        .chat-info h4 { font-size: 14px; font-weight: 600; margin-bottom: 4px; }
        .chat-info p {
            font-size: 12px; color: var(--text-secondary);
            white-space: nowrap; overflow: hidden; text-overflow: ellipsis; max-width: 150px;
        }

        /* Delete Chat Button */
        .delete-chat-btn {
            display: none;
            background: none;
            border: none;
            color: var(--text-secondary);
            cursor: pointer;
            font-size: 16px;
            padding: 4px;
            border-radius: 4px;
            margin-left: auto;
            opacity: 0.7;
            transition: all 0.2s;
        }
        .chat-item:hover .delete-chat-btn {
            display: block;
        }
        .delete-chat-btn:hover {
            opacity: 1;
            color: var(--error-color);
            background-color: rgba(237, 66, 69, 0.1);
        }

        .main-content { flex: 1; display: flex; flex-direction: column; position: relative; background-color: var(--bg-dark); }

        .search-screen {
            flex: 1; display: flex; flex-direction: column;
            align-items: center; justify-content: center; padding: 20px;
        }
        .search-container { width: 100%; max-width: 600px; text-align: center; }
        .search-title { font-size: 24px; margin-bottom: 30px; font-weight: 600; }
        .search-bar-wrapper { position: relative; margin-bottom: 20px; }
        .search-input {
            width: 100%; padding: 16px 24px; font-size: 18px;
            background-color: var(--bg-panel); border: 1px solid transparent;
            border-radius: 30px; color: var(--text-primary); outline: none;
            box-shadow: var(--shadow); transition: all 0.3s ease;
        }
        .search-input:focus { border-color: var(--accent-color); box-shadow: 0 0 0 4px rgba(88, 101, 242, 0.2); }
        .search-hint { font-size: 13px; color: var(--text-secondary); margin-bottom: 20px; }
        .search-results {
            width: 100%; max-width: 600px; text-align: left; display: none;
            max-height: 400px; overflow-y: auto;
        }
        .user-result {
            display: flex; align-items: center; padding: 12px 16px;
            background-color: var(--bg-panel); border-radius: var(--border-radius);
            margin-bottom: 10px; cursor: pointer; transition: transform 0.2s, background 0.2s;
        }
        .user-result:hover { transform: translateY(-2px); background-color: #35373c; }
        .user-result .avatar { background: var(--accent-color); }

        .chat-screen { flex: 1; display: none; flex-direction: row; height: 100%; position: relative; }
        .chat-screen.active { display: flex; }
        
        .chat-screen.drag-over::after {
            content: "📁 Отпустите, чтобы прикрепить";
            position: absolute;
            top: 0; left: 0; right: 0; bottom: 0;
            background-color: rgba(88, 101, 242, 0.15);
            border: 4px dashed var(--accent-color);
            z-index: 1000;
            display: flex;
            justify-content: center;
            align-items: center;
            font-size: 24px;
            font-weight: bold;
            color: var(--accent-color);
            pointer-events: none;
        }

        .chat-main-area {
            flex: 1;
            display: flex;
            flex-direction: column;
            position: relative;
            background-color: var(--bg-dark);
            border-right: 1px solid #1f2023;
        }

        .chat-header {
            padding: 16px 24px; border-bottom: 1px solid #1f2023;
            display: flex; align-items: center; background-color: var(--bg-dark); z-index: 10;
        }
        .back-btn {
            display: none; background: none; border: none; color: var(--text-secondary);
            font-size: 20px; margin-right: 12px; cursor: pointer;
        }
        .messages-area {
            flex: 1; padding: 24px; overflow-y: auto;
            display: flex; flex-direction: column; gap: 16px;
        }

        .message { 
            max-width: 70%; 
            display: flex; 
            flex-direction: row;
            align-items: flex-end;
            position: relative;
        }
        
        .message.incoming { 
            align-self: flex-start; 
            flex-direction: row-reverse;
        }
        
        .message.outgoing { 
            align-self: flex-end; 
            flex-direction: row;
            align-items: flex-end;
        }
        
        .message-bubble {
            padding: 8px 12px;
            border-radius: 18px;
            font-size: 15px;
            line-height: 1.4;
            position: relative;
            word-wrap: break-word;
            display: flex;
            flex-direction: column;
            gap: 6px;
            max-width: 100%;
        }
        .incoming .message-bubble { 
            background-color: var(--msg-bg-incoming); 
            border-bottom-left-radius: 4px;
        }
        .outgoing .message-bubble { 
            background-color: var(--msg-bg-outgoing); 
            color: white;
            border-bottom-right-radius: 4px;
        }
        
        .message-time { 
            font-size: 11px; 
            color: var(--text-secondary); 
            margin-top: 2px; 
            padding: 0 4px; 
            text-align: right;
            align-self: flex-end;
        }
        .outgoing .message-time {
            color: rgba(255, 255, 255, 0.7);
        }
        .date-divider {
            align-self: center;
            margin: 10px 0 14px;
            padding: 6px 12px;
            background-color: rgba(255, 255, 255, 0.06);
            border: 1px solid rgba(255, 255, 255, 0.08);
            border-radius: 999px;
            color: var(--text-secondary);
            font-size: 12px;
            font-weight: 600;
            letter-spacing: 0.2px;
        }
        .message-edited { font-size: 10px; color: var(--edited-label); margin-left: 4px; font-style: italic; }
        
        .reply-preview {
            background-color: rgba(0, 0, 0, 0.2);
            padding: 6px 10px;
            border-radius: 8px;
            margin-bottom: 4px;
            border-left: 3px solid var(--accent-color);
            font-size: 13px;
            max-width: 100%;
            overflow: hidden;
        }
        .outgoing .reply-preview {
            background-color: rgba(255, 255, 255, 0.1);
            border-left-color: #fff;
        }
        .reply-preview .reply-author {
            font-weight: 600;
            color: var(--accent-color);
            margin-bottom: 2px;
            font-size: 12px;
        }
        .outgoing .reply-preview .reply-author {
            color: #fff;
        }
        .reply-preview .reply-text {
            color: var(--text-secondary);
            white-space: nowrap;
            overflow: hidden;
            text-overflow: ellipsis;
        }
        .outgoing .reply-preview .reply-text {
            color: rgba(255,255,255, 0.8);
        }
        .reply-preview.deleted {
            opacity: 0.6;
            font-style: italic;
        }

        .message-actions {
            display: none;
            flex-direction: row;
            gap: 4px;
            padding: 4px 8px;
            background-color: var(--bg-panel);
            border-radius: 12px;
            box-shadow: 0 2px 5px rgba(0,0,0,0.2);
            margin-bottom: 2px;
            z-index: 5;
        }

        .message:hover .message-actions {
            display: flex;
        }

        .incoming .message-actions {
            margin-left: 8px;
        }
        .outgoing .message-actions {
            margin-right: 8px;
        }
        
        .message-action-btn {
            background-color: transparent;
            border: none;
            color: var(--text-secondary);
            width: 28px;
            height: 28px;
            border-radius: 50%;
            cursor: pointer;
            display: flex;
            align-items: center;
            justify-content: center;
            font-size: 12px;
            transition: all 0.2s;
        }
        .message-action-btn:hover {
            background-color: var(--bg-dark);
            color: var(--text-primary);
        }
        .message-action-btn.delete:hover {
            color: var(--error-color);
        }

        .input-area { 
            padding: 16px 24px; 
            background-color: var(--bg-dark); 
            display: flex; 
            flex-direction: column; 
            gap: 12px;
            position: relative;
        }
        
        .drag-hint {
            position: absolute;
            top: 50%;
            left: 50%;
            transform: translate(-50%, -50%);
            background-color: var(--bg-panel);
            padding: 20px 40px;
            border-radius: 12px;
            font-size: 16px;
            color: var(--text-primary);
            display: none;
            z-index: 100;
            pointer-events: none;
        }
        
        .reply-indicator {
            background-color: var(--reply-bg);
            padding: 12px 16px;
            border-radius: 8px;
            border-left: 3px solid var(--accent-color);
            display: none;
            justify-content: space-between;
            align-items: center;
        }
        .reply-indicator.active {
            display: flex;
        }
        .reply-indicator-text {
            flex: 1;
        }
        .reply-indicator-author {
            font-weight: 600;
            color: var(--accent-color);
            font-size: 13px;
        }
        .reply-indicator-message {
            color: var(--text-secondary);
            font-size: 12px;
            white-space: nowrap;
            overflow: hidden;
            text-overflow: ellipsis;
        }
        .reply-cancel-btn {
            background: none;
            border: none;
            color: var(--text-secondary);
            cursor: pointer;
            font-size: 18px;
            padding: 4px 8px;
        }
        .reply-cancel-btn:hover {
            color: var(--error-color);
        }
        
        .image-preview-container {
            display: none;
            margin-bottom: 8px;
        }
        .image-preview-container.active { 
            display: block; 
        }
        
        .image-preview-horizontal {
            display: flex;
            gap: 8px;
            overflow-x: auto;
            padding: 4px;
            scrollbar-width: thin;
            scrollbar-color: var(--bg-panel) var(--bg-dark);
        }
        
        .image-preview-horizontal::-webkit-scrollbar {
            height: 4px;
        }
        
        .image-preview-horizontal::-webkit-scrollbar-track {
            background: var(--bg-dark);
        }
        
        .image-preview-horizontal::-webkit-scrollbar-thumb {
            background: var(--bg-panel);
            border-radius: 2px;
        }
        
        .image-preview-item {
            position: relative;
            flex-shrink: 0;
            width: 80px;
            height: 80px;
            border-radius: 8px;
            overflow: hidden;
            border: 2px solid var(--bg-panel);
            background: #000;
            display: flex;
            align-items: center;
            justify-content: center;
        }
        
        .image-preview-item img, 
        .image-preview-item video, 
        .image-preview-item audio {
            width: 100%;
            height: 100%;
            object-fit: cover;
        }
        
        .image-preview-item audio {
            width: 100%;
            height: auto;
            padding: 4px;
        }
        
        .remove-image-btn {
            position: absolute;
            top: 4px; 
            right: 4px;
            background: rgba(237, 66, 69, 0.95);
            color: white;
            border: none;
            border-radius: 50%;
            width: 24px; 
            height: 24px;
            cursor: pointer;
            display: flex; 
            align-items: center; 
            justify-content: center;
            font-size: 14px;
            transition: transform 0.2s;
            z-index: 2;
        }
        .remove-image-btn:hover { 
            background: var(--error-color);
            transform: scale(1.1);
        }
        
        .image-count-badge {
            position: absolute;
            bottom: 4px;
            right: 4px;
            background: rgba(0, 0, 0, 0.7);
            color: white;
            padding: 2px 6px;
            border-radius: 4px;
            font-size: 11px;
            z-index: 2;
        }

        .message-input-wrapper {
            display: flex;
            align-items: flex-end;
            gap: 12px;
        }
        
        .message-input {
            flex: 1; 
            padding: 12px 20px; 
            background-color: var(--bg-panel);
            border: none; 
            border-radius: 24px; 
            color: var(--text-primary);
            font-size: 15px; 
            outline: none;
            resize: none;
            min-height: 44px;
            max-height: 150px;
            overflow-y: auto;
            line-height: 1.4;
            font-family: inherit;
        }
        
        .send-btn {
            background-color: var(--accent-color); color: white; border: none;
            width: 44px; height: 44px; border-radius: 50%;
            display: flex; align-items: center; justify-content: center;
            cursor: pointer; transition: background 0.2s;
            flex-shrink: 0;
            margin-bottom: 0;
        }
        .send-btn:hover { background-color: var(--accent-hover); }
        .send-btn:disabled { background-color: var(--text-secondary); cursor: not-allowed; }
        
        .attach-btn {
            background-color: transparent; color: var(--text-secondary); border: none;
            width: 44px; height: 44px; border-radius: 50%;
            display: flex; align-items: center; justify-content: center;
            cursor: pointer; transition: color 0.2s;
            flex-shrink: 0;
            margin-bottom: 0;
        }
        .attach-btn:hover { color: var(--text-primary); background-color: var(--bg-panel); }

        .message-images-container {
            display: flex;
            flex-direction: column;
            gap: 4px;
            margin-top: 4px;
            width: 320px; 
        }
        
        .message-image-wrapper {
            position: relative;
            overflow: hidden;
            cursor: pointer;
            background-color: rgba(0,0,0,0.2);
            border-radius: 8px;
            display: flex;
            align-items: center;
            justify-content: center;
        }
        
        .message-image-wrapper.single-image {
            width: 100%;
            height: 320px;
        }
        
        .message-image-wrapper.single-image img, 
        .message-image-wrapper.single-image video,
        .message-image-wrapper.single-image audio {
            width: 100%;
            height: 100%;
            object-fit: cover;
            display: block;
        }
        
        .message-image-wrapper audio {
            width: 100%;
            height: auto;
            padding: 8px;
            cursor: default;
        }
        
        .images-two {
            display: flex;
            gap: 4px;
            width: 100%;
        }
        
        .images-two .message-image-wrapper {
            width: calc(50% - 2px);
            height: 158px;
        }
        
        .images-two .message-image-wrapper img, 
        .images-two .message-image-wrapper video,
        .images-two .message-image-wrapper audio {
            width: 100%;
            height: 100%;
            object-fit: cover;
            display: block;
        }
        
        .images-three {
            display: grid;
            grid-template-columns: 1fr 1fr;
            grid-template-rows: 158px 158px;
            gap: 4px;
            width: 100%;
        }
        
        .images-three .message-image-wrapper {
            width: 100%;
            height: 100%;
        }
        
        .images-three .message-image-wrapper:first-child {
            grid-column: 1 / -1;
        }
        
        .images-three .message-image-wrapper img, 
        .images-three .message-image-wrapper video,
        .images-three .message-image-wrapper audio {
            width: 100%;
            height: 100%;
            object-fit: cover;
            display: block;
        }
        
        .images-four {
            display: grid;
            grid-template-columns: 1fr 1fr;
            grid-template-rows: 158px 158px;
            gap: 4px;
            width: 100%;
        }
        
        .images-four .message-image-wrapper {
            width: 100%;
            height: 100%;
        }
        
        .images-four .message-image-wrapper img, 
        .images-four .message-image-wrapper video,
        .images-four .message-image-wrapper audio {
            width: 100%;
            height: 100%;
            object-fit: cover;
            display: block;
        }
        
        .images-five {
            display: grid;
            grid-template-columns: 1fr 1fr;
            grid-template-rows: 158px 158px 158px;
            gap: 4px;
            width: 100%;
        }
        
        .images-five .message-image-wrapper:nth-child(1) {
            grid-column: 1 / -1;
            grid-row: 1;
            width: 100%;
            height: 100%;
        }
        
        .images-five .message-image-wrapper:nth-child(n+2) {
            width: 100%;
            height: 100%;
        }
        
        .images-five .message-image-wrapper img, 
        .images-five .message-image-wrapper video,
        .images-five .message-image-wrapper audio {
            width: 100%;
            height: 100%;
            object-fit: cover;
            display: block;
        }
        
        .loading { color: var(--text-secondary); font-size: 14px; text-align: center; padding: 20px; }

        .members-sidebar {
            width: 260px;
            background-color: #1e1f22;
            display: none;
            flex-direction: column;
            padding: 16px;
            border-left: 1px solid #1f2023;
        }
        .members-sidebar.active {
            display: flex;
        }
        .members-header {
            display: flex;
            justify-content: space-between;
            align-items: center;
            margin-bottom: 16px;
            padding-bottom: 10px;
            border-bottom: 1px solid #3f4147;
        }
        .members-title {
            font-size: 16px;
            font-weight: 600;
        }
        .add-member-btn {
            background: none;
            border: none;
            color: var(--accent-color);
            cursor: pointer;
            font-size: 20px;
            padding: 4px;
            border-radius: 4px;
        }
        .add-member-btn:hover {
            background-color: var(--bg-panel);
        }
        .members-list {
            list-style: none;
            overflow-y: auto;
            flex: 1;
        }
        .member-item {
            display: flex;
            align-items: center;
            padding: 8px;
            border-radius: 8px;
            margin-bottom: 4px;
            transition: background 0.2s;
            position: relative;
        }
        .member-item:hover {
            background-color: var(--bg-panel);
        }
        .member-item img {
            width: 32px;
            height: 32px;
            border-radius: 50%;
            margin-right: 10px;
        }
        .member-name {
            font-size: 14px;
            font-weight: 500;
            flex: 1;
        }
        .member-you {
            font-size: 11px;
            color: var(--text-secondary);
            margin-left: 6px;
        }
        .remove-member-btn {
            display: none;
            background: none;
            border: none;
            color: var(--error-color);
            cursor: pointer;
            font-size: 18px;
            padding: 4px;
            border-radius: 4px;
            opacity: 0.7;
            transition: opacity 0.2s;
        }
        .member-item:hover .remove-member-btn {
            display: block;
        }
        .remove-member-btn:hover {
            opacity: 1;
            background-color: rgba(237, 66, 69, 0.1);
        }

        .modal-overlay {
            position: fixed;
            top: 0;
            left: 0;
            right: 0;
            bottom: 0;
            background-color: rgba(0, 0, 0, 0.7);
            display: none;
            justify-content: center;
            align-items: center;
            z-index: 1000;
        }
        .modal-overlay.active {
            display: flex;
        }
        .modal {
            background-color: var(--bg-panel);
            padding: 24px;
            border-radius: var(--border-radius);
            width: 90%;
            max-width: 500px;
        }
        .modal-title {
            font-size: 18px;
            font-weight: 600;
            margin-bottom: 16px;
        }
        .modal-input {
            width: 100%;
            padding: 12px;
            background-color: var(--bg-dark);
            border: 1px solid transparent;
            border-radius: 8px;
            color: var(--text-primary);
            font-size: 15px;
            outline: none;
            margin-bottom: 16px;
        }
        .modal-buttons {
            display: flex;
            gap: 12px;
            justify-content: flex-end;
        }
        .modal-btn {
            padding: 10px 20px;
            border: none;
            border-radius: 8px;
            cursor: pointer;
            font-size: 14px;
            font-weight: 600;
        }
        .modal-btn.cancel {
            background-color: var(--bg-dark);
            color: var(--text-secondary);
        }
        .modal-btn.save {
            background-color: var(--accent-color);
            color: white;
        }
        .modal-btn.save:hover {
            background-color: var(--accent-hover);
        }
        
        .member-select-list {
            max-height: 200px;
            overflow-y: auto;
            background: var(--bg-dark);
            border-radius: 8px;
            margin-bottom: 16px;
            padding: 8px;
        }
        .member-checkbox {
            display: flex;
            align-items: center;
            padding: 8px;
            cursor: pointer;
        }
        .member-checkbox:hover {
            background-color: var(--bg-panel);
        }
        .member-checkbox input {
            margin-right: 10px;
        }

        /* System Message Styles - теперь выглядит как обычное входящее сообщение */
        .system-message {
            align-self: flex-start;
            max-width: 70%;
            display: flex;
            flex-direction: row;
            align-items: flex-end;
            position: relative;
        }
        
        .system-message .message-bubble {
            background-color: var(--msg-bg-incoming);
            border-radius: 18px;
            border-bottom-left-radius: 4px;
            padding: 8px 12px;
            font-size: 15px;
            line-height: 1.4;
            color: var(--text-primary);
            word-wrap: break-word;
            display: flex;
            flex-direction: column;
            gap: 6px;
            max-width: 100%;
        }
        
        .system-message .system-text {
            display: block;
        }
        
        .system-message .message-time {
            font-size: 11px;
            color: var(--text-secondary);
            margin-top: 2px;
            padding: 0 4px;
            text-align: right;
            align-self: flex-end;
        }

        @media (max-width: 768px) {
            .sidebar {
                position: absolute; left: -280px; height: 100%; z-index: 100;
                background-color: var(--bg-dark); box-shadow: 2px 0 10px rgba(0,0,0,0.5);
            }
            .sidebar.open { transform: translateX(280px); }
            .back-btn { display: block; }
            .message { max-width: 85%; }
            .auth-container { margin: 20px; padding: 20px; }
            
            .members-sidebar {
                position: absolute;
                right: -260px;
                height: 100%;
                z-index: 90;
                transition: transform 0.3s ease;
            }
            .members-sidebar.active {
                transform: translateX(-260px);
            }
            
            .message-images-container {
                width: 260px;
            }
            .images-two .message-image-wrapper { height: 128px; }
            .images-three, .images-four { grid-template-rows: 128px 128px; }
            .images-five {
                grid-template-rows: 128px 128px 128px;
            }
            .images-five .message-image-wrapper:nth-child(1) { height: 100%; }
            .images-five .message-image-wrapper:nth-child(n+2) { 
                width: 100%;
                height: 100%;
            }
        }
    
        .chat-actions {
            margin-left: auto;
            display: flex;
            align-items: center;
            gap: 10px;
        }
        .header-action-btn {
            width: 40px;
            height: 40px;
            border-radius: 50%;
            border: none;
            background: var(--bg-panel);
            color: var(--text-primary);
            cursor: pointer;
            display: inline-flex;
            align-items: center;
            justify-content: center;
            transition: transform 0.2s, background 0.2s;
        }
        .header-action-btn:hover { background: #35373c; transform: translateY(-1px); }
        .header-action-btn.active { background: var(--accent-color); color: white; }
        .call-banner {
            display: none;
            align-items: center;
            justify-content: space-between;
            gap: 16px;
            padding: 12px 16px;
            background: linear-gradient(90deg, rgba(67,181,129,0.18), rgba(88,101,242,0.18));
            border-bottom: 1px solid rgba(255,255,255,0.06);
        }
        .call-banner.active { display: flex; }
        .call-banner-title { font-weight: 700; color: var(--text-primary); }
        .call-banner-subtitle { font-size: 13px; color: var(--text-secondary); margin-top: 3px; }
        .call-banner-actions { display: flex; gap: 8px; flex-wrap: wrap; }
        .call-banner-btn {
            border: none;
            border-radius: 10px;
            padding: 10px 14px;
            cursor: pointer;
            font-weight: 600;
            background: var(--bg-panel);
            color: var(--text-primary);
        }
        .call-banner-btn.primary { background: var(--accent-color); color: white; }
        .call-banner-btn.danger { background: var(--error-color); color: white; }
        .call-panel {
            display: none;
            flex-direction: column;
            gap: 16px;
            padding: 18px 20px;
            border-bottom: 1px solid #1f2023;
            background: #202225;
        }
        .call-panel.active { display: flex; }
        .call-panel-top {
            display: flex;
            justify-content: space-between;
            align-items: center;
            gap: 12px;
            flex-wrap: wrap;
        }
        .call-panel-title { font-size: 18px; font-weight: 700; }
        .call-panel-meta { font-size: 13px; color: var(--text-secondary); margin-top: 4px; }
        .call-grid {
            display: grid;
            grid-template-columns: repeat(auto-fit, minmax(180px, 1fr));
            gap: 12px;
        }
        .call-user-card {
            background: var(--bg-panel);
            border-radius: 16px;
            padding: 16px;
            min-height: 120px;
            display: flex;
            flex-direction: column;
            justify-content: space-between;
            box-shadow: var(--shadow);
        }
        .call-user-top {
            display: flex;
            align-items: center;
            gap: 10px;
        }
        .call-user-avatar {
            width: 42px;
            height: 42px;
            border-radius: 50%;
            background: var(--accent-color);
            display: inline-flex;
            align-items: center;
            justify-content: center;
            font-weight: 700;
            color: white;
        }
        .call-user-name { font-weight: 700; }
        .call-user-status { font-size: 12px; color: var(--text-secondary); margin-top: 2px; }
        .call-user-audio audio { width: 100%; margin-top: 12px; }
        .call-user-video {
            width: 100%;
            margin-top: 12px;
            border-radius: 12px;
            overflow: hidden;
            background: #111318;
            border: 1px solid rgba(255,255,255,0.06);
            min-height: 140px;
            display: flex;
            align-items: center;
            justify-content: center;
        }
        .call-user-video video {
            width: 100%;
            max-height: 320px;
            display: block;
            background: #111318;
        }
        .call-user-video-empty {
            color: var(--text-secondary);
            font-size: 12px;
            padding: 24px 12px;
            text-align: center;
        }
        .call-controls {
            display: flex;
            flex-wrap: wrap;
            gap: 10px;
        }
        .call-control-btn {
            border: none;
            border-radius: 12px;
            padding: 12px 16px;
            cursor: pointer;
            background: var(--bg-panel);
            color: var(--text-primary);
            font-weight: 600;
        }
        .call-control-btn.primary { background: var(--accent-color); color: white; }
        .call-control-btn.danger { background: var(--error-color); color: white; }
        .call-control-btn.disabled-feature { opacity: 0.8; }
        .incoming-call-overlay {
            position: fixed;
            inset: 0;
            background: rgba(0, 0, 0, 0.68);
            backdrop-filter: blur(8px);
            display: none;
            align-items: center;
            justify-content: center;
            z-index: 2000;
            padding: 20px;
        }
        .incoming-call-overlay.active { display: flex; }
        .incoming-call-modal {
            width: min(100%, 420px);
            background: linear-gradient(180deg, #2a2d33 0%, #202225 100%);
            border: 1px solid rgba(255,255,255,0.08);
            border-radius: 24px;
            box-shadow: 0 24px 60px rgba(0, 0, 0, 0.45);
            padding: 28px 24px;
            text-align: center;
        }
        .incoming-call-avatar {
            width: 72px;
            height: 72px;
            margin: 0 auto 16px;
            border-radius: 50%;
            background: linear-gradient(135deg, var(--accent-color), #43b581);
            display: flex;
            align-items: center;
            justify-content: center;
            font-size: 28px;
            font-weight: 800;
            color: white;
            box-shadow: 0 10px 24px rgba(88,101,242,0.3);
        }
        .incoming-call-title {
            font-size: 24px;
            font-weight: 800;
            color: var(--text-primary);
        }
        .incoming-call-subtitle {
            margin-top: 10px;
            color: var(--text-secondary);
            line-height: 1.5;
            font-size: 14px;
        }
        .incoming-call-actions {
            display: flex;
            gap: 12px;
            margin-top: 22px;
            flex-wrap: wrap;
            justify-content: center;
        }
        .incoming-call-btn {
            min-width: 150px;
            border: none;
            border-radius: 14px;
            padding: 14px 18px;
            cursor: pointer;
            font-weight: 700;
            font-size: 15px;
            transition: transform 0.18s ease, opacity 0.18s ease;
        }
        .incoming-call-btn:hover {
            transform: translateY(-1px);
        }
        .incoming-call-btn.accept {
            background: linear-gradient(135deg, #43b581, #2bb673);
            color: white;
        }
        .incoming-call-btn.reject {
            background: #ed4245;
            color: white;
        }


        .settings-overlay {
            padding: 24px;
            align-items: center;
            justify-content: center;
            z-index: 1300;
            overscroll-behavior: contain;
        }
        .settings-modal {
            position: relative;
            width: min(1040px, calc(100vw - 48px));
            max-height: calc(100vh - 48px);
            background: #1a1b1f;
            border: 1px solid #2b2e34;
            border-radius: 24px;
            box-shadow: 0 30px 80px rgba(0, 0, 0, 0.45);
            display: flex;
            overflow: hidden;
        }
        .settings-sidebar {
            width: 240px;
            background: #121316;
            border-right: 1px solid #25282e;
            padding: 24px 12px 24px 16px;
            display: flex;
            flex-direction: column;
            gap: 14px;
            overflow-y: auto;
            overscroll-behavior: contain;
        }
        .settings-close-btn {
            position: absolute;
            top: 22px;
            right: 22px;
            width: 40px;
            height: 40px;
            border-radius: 12px;
            border: 1px solid #2f3339;
            background: #1d1f24;
            color: var(--text-secondary);
            cursor: pointer;
            display: inline-flex;
            align-items: center;
            justify-content: center;
            transition: 0.2s ease;
            z-index: 2;
        }
        .settings-close-btn:hover {
            color: var(--text-primary);
            background: #272a31;
            border-color: #41454f;
        }
        .settings-nav {
            display: flex;
            flex-direction: column;
            gap: 6px;
        }
        .settings-nav-item {
            width: 100%;
            padding: 12px 14px;
            border: none;
            border-radius: 12px;
            background: transparent;
            color: var(--text-secondary);
            text-align: left;
            cursor: pointer;
            font-size: 15px;
            font-weight: 600;
            transition: 0.2s ease;
        }
        .settings-nav-item.active,
        .settings-nav-item:hover {
            background: #24272d;
            color: var(--text-primary);
        }
        .settings-content {
            flex: 1;
            background: #1e1f22;
            padding: 30px 32px;
            overflow-y: auto;
            overscroll-behavior: contain;
        }
        .settings-panel {
            max-width: 700px;
            display: none;
        }
        .settings-panel.active {
            display: block;
        }
        .settings-title {
            font-size: 30px;
            font-weight: 700;
            margin-bottom: 10px;
        }
        .settings-subtitle {
            color: var(--text-secondary);
            font-size: 14px;
            margin-bottom: 26px;
            line-height: 1.5;
        }
        .profile-hero {
            display: flex;
            align-items: center;
            gap: 16px;
            padding: 18px;
            background: linear-gradient(180deg, #252831 0%, #1e2026 100%);
            border: 1px solid #30343d;
            border-radius: 18px;
            margin-bottom: 18px;
        }
        .profile-hero-avatar {
            width: 72px;
            height: 72px;
            border-radius: 50%;
            object-fit: cover;
            border: 3px solid rgba(255,255,255,0.08);
            flex-shrink: 0;
        }
        .profile-hero-info h3 {
            font-size: 20px;
            margin-bottom: 6px;
        }
        .profile-hero-info p {
            color: var(--text-secondary);
            font-size: 14px;
        }
        .settings-section {
            border-top: 1px solid #2b2e34;
            padding: 20px 0;
        }
        .settings-section:first-of-type {
            border-top: none;
            padding-top: 0;
        }
        .settings-row {
            display: flex;
            align-items: center;
            justify-content: space-between;
            gap: 18px;
        }
        .settings-row + .settings-row {
            margin-top: 18px;
        }
        .settings-row-info {
            min-width: 0;
        }
        .settings-row-title {
            font-size: 16px;
            font-weight: 600;
            margin-bottom: 6px;
        }
        .settings-row-description {
            color: var(--text-secondary);
            font-size: 13px;
            line-height: 1.5;
        }
        .settings-row-value {
            margin-top: 6px;
            color: var(--text-primary);
            font-size: 14px;
            word-break: break-word;
        }

        .settings-switch {
            position: relative;
            width: 52px;
            height: 30px;
            flex-shrink: 0;
            display: inline-flex;
            align-items: center;
            cursor: pointer;
        }
        .settings-switch input {
            position: absolute;
            opacity: 0;
            pointer-events: none;
        }
        .settings-switch-slider {
            position: relative;
            width: 100%;
            height: 100%;
            border-radius: 999px;
            background: #17191d;
            border: 1px solid #32353c;
            box-shadow: inset 0 0 0 1px rgba(255,255,255,0.02);
            transition: background 0.2s ease, border-color 0.2s ease;
        }
        .settings-switch-slider::before {
            content: '';
            position: absolute;
            top: 3px;
            left: 3px;
            width: 22px;
            height: 22px;
            border-radius: 50%;
            background: #f5f6f7;
            box-shadow: 0 2px 10px rgba(0,0,0,0.35);
            transition: transform 0.2s ease;
        }
        .settings-switch input:checked + .settings-switch-slider {
            background: #262a31;
            border-color: #4a505c;
        }
        .settings-switch input:checked + .settings-switch-slider::before {
            transform: translateX(22px);
        }
        .settings-switch input:disabled + .settings-switch-slider {
            opacity: 0.65;
            cursor: wait;
        }
        .settings-action-btn {
            padding: 10px 16px;
            border-radius: 10px;
            border: 1px solid #41454f;
            background: #2a2d33;
            color: var(--text-primary);
            font-size: 14px;
            font-weight: 600;
            cursor: pointer;
            transition: 0.2s ease;
            white-space: nowrap;
        }
        .settings-action-btn:hover {
            background: #353941;
            border-color: #505661;
        }
        .settings-action-btn.danger {
            background: rgba(237, 66, 69, 0.1);
            border-color: rgba(237, 66, 69, 0.35);
            color: #ff8f8f;
        }
        .settings-action-btn.danger:hover {
            background: rgba(237, 66, 69, 0.18);
            border-color: rgba(237, 66, 69, 0.5);
        }
        .settings-note {
            margin-top: 22px;
            padding: 14px 16px;
            border-radius: 14px;
            background: #181a1e;
            border: 1px solid #2a2d34;
            color: var(--text-secondary);
            font-size: 13px;
            line-height: 1.5;
        }
        .settings-upload-status {
            margin-top: 12px;
            font-size: 13px;
            color: var(--text-secondary);
            min-height: 18px;
        }
        .settings-upload-status.success {
            color: var(--success-color);
        }
        .settings-upload-status.error {
            color: var(--error-color);
        }
        .settings-action-btn:disabled {
            opacity: 0.65;
            cursor: wait;
        }
        .settings-action-btn-secondary {
            background: transparent;
            border: 1px solid #3b3d44;
            color: var(--text-primary);
        }
        .settings-action-btn-secondary:hover {
            background: rgba(255, 255, 255, 0.06);
        }
        .settings-row-stack {
            align-items: flex-start;
        }
        .settings-inline-editor {
            margin-top: 14px;
            display: none;
            flex-direction: column;
            gap: 10px;
            max-width: 420px;
        }
        .settings-inline-editor.active {
            display: flex;
        }
        .settings-inline-input {
            width: 100%;
            padding: 12px 14px;
            border-radius: 12px;
            border: 1px solid #3b3d44;
            background: #111318;
            color: var(--text-primary);
            outline: none;
            transition: border-color 0.2s, box-shadow 0.2s;
        }
        .settings-inline-input:focus {
            border-color: var(--accent-color);
            box-shadow: 0 0 0 3px rgba(88, 101, 242, 0.16);
        }
        .settings-inline-actions {
            display: flex;
            gap: 10px;
            flex-wrap: wrap;
        }
        .settings-inline-status {
            min-height: 18px;
            font-size: 13px;
            color: var(--text-secondary);
        }
        .settings-inline-status.success {
            color: var(--success-color);
        }
        .settings-inline-status.error {
            color: var(--error-color);
        }
        .device-list {
            display: flex;
            flex-direction: column;
            gap: 14px;
        }
        .device-card {
            display: flex;
            align-items: flex-start;
            justify-content: space-between;
            gap: 16px;
            padding: 16px 18px;
            border-radius: 18px;
            background: linear-gradient(180deg, #242730 0%, #1d1f25 100%);
            border: 1px solid #30343d;
        }
        .device-main {
            display: flex;
            gap: 14px;
            min-width: 0;
        }
        .device-icon {
            width: 44px;
            height: 44px;
            border-radius: 14px;
            background: #2c3039;
            border: 1px solid #3a3f49;
            display: flex;
            align-items: center;
            justify-content: center;
            color: var(--text-primary);
            flex-shrink: 0;
        }
        .device-info {
            min-width: 0;
        }
        .device-title-row {
            display: flex;
            align-items: center;
            flex-wrap: wrap;
            gap: 8px;
            margin-bottom: 6px;
        }
        .device-title {
            font-size: 16px;
            font-weight: 600;
        }
        .device-badge {
            display: inline-flex;
            align-items: center;
            padding: 4px 8px;
            border-radius: 999px;
            font-size: 11px;
            font-weight: 700;
            letter-spacing: 0.2px;
            background: rgba(88, 101, 242, 0.18);
            color: #b7c0ff;
            border: 1px solid rgba(88, 101, 242, 0.3);
        }
        .device-meta,
        .device-location {
            color: var(--text-secondary);
            font-size: 13px;
            line-height: 1.5;
        }
        .device-actions {
            display: flex;
            align-items: center;
            gap: 10px;
            flex-shrink: 0;
        }

        @media (max-width: 760px) {
            .settings-overlay {
                padding: 12px;
            }
            .settings-modal {
                width: 100%;
                max-height: calc(100vh - 24px);
                flex-direction: column;
                border-radius: 18px;
            }
            .settings-sidebar {
                width: 100%;
                border-right: none;
                border-bottom: 1px solid #25282e;
            }
            .settings-content {
                padding: 22px 18px 24px;
            }
            .settings-row {
                align-items: flex-start;
                flex-direction: column;
            }
            .settings-action-btn {
                width: 100%;
            }
        }

    
