/* ===== 基础重置 ===== */
*, *::before, *::after {
    box-sizing: border-box;
    margin: 0;
    padding: 0;
}

:root {
    --primary: #2AABEE;
    --primary-hover: #229ED9;
    --primary-light: #E3F2FD;
    --bg: #E8ECF0;
    --bg-white: #ffffff;
    --text: #1f2937;
    --text-secondary: #8D98A8;
    --border: #D4DAE2;
    --danger: #ef4444;
    --success: #22c55e;
    --sidebar-bg: #1A2435;
    --sidebar-text: #e2e8f0;
    --sidebar-hover: #253245;
    --msg-own: #EEF7FF;
    --msg-own-text: #1f2937;
    --msg-other: #ffffff;
    --msg-other-text: #1f2937;
    --shadow: 0 1px 2px rgba(0,0,0,0.08);
    --radius: 12px;
}

body {
    font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, "PingFang SC", "Microsoft YaHei", sans-serif;
    background: var(--bg);
    color: var(--text);
    height: 100%;
    overflow: hidden;
    -webkit-tap-highlight-color: transparent;
    position: fixed;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
}

/* ===== 页面切换 ===== */
.page {
    display: none;
    width: 100%;
    height: 100%;
}
.page.active {
    display: flex;
}

/* ===== 登录页 ===== */
#login-page {
    justify-content: center;
    align-items: center;
    background: linear-gradient(135deg, #2AABEE 0%, #229ED9 100%);
}

.login-container {
    background: #fff;
    padding: 40px;
    border-radius: 16px;
    box-shadow: 0 20px 60px rgba(0,0,0,0.2);
    width: 100%;
    max-width: 380px;
    margin: 20px;
}

.login-header {
    text-align: center;
    margin-bottom: 30px;
}
.login-header h1 { font-size: 28px; margin-bottom: 8px; }
.login-header p { color: var(--text-secondary); }

/* ===== 表单 ===== */
.form-group {
    margin-bottom: 16px;
}
.form-group label {
    display: block;
    font-size: 14px;
    font-weight: 500;
    margin-bottom: 6px;
    color: var(--text);
}
.form-group input,
.form-group select {
    width: 100%;
    padding: 10px 14px;
    border: 2px solid var(--border);
    border-radius: var(--radius);
    font-size: 15px;
    transition: border-color 0.2s;
    background: #fff;
}
.form-group input:focus,
.form-group select:focus {
    outline: none;
    border-color: var(--primary);
}

.btn-primary {
    display: block;
    width: 100%;
    padding: 12px;
    background: var(--primary);
    color: #fff;
    border: none;
    border-radius: var(--radius);
    font-size: 16px;
    font-weight: 600;
    cursor: pointer;
    transition: background 0.2s;
}
.btn-primary:hover { background: var(--primary-hover); }
.btn-primary:active { transform: scale(0.98); }

.btn-text {
    background: none;
    border: none;
    color: var(--text-secondary);
    font-size: 14px;
    cursor: pointer;
    padding: 8px 12px;
    border-radius: var(--radius);
}
.btn-text:hover { background: rgba(0,0,0,0.05); }

.btn-icon {
    background: none;
    border: none;
    font-size: 20px;
    cursor: pointer;
    padding: 4px 8px;
    border-radius: var(--radius);
    color: var(--text-secondary);
    line-height: 1;
}
.btn-icon:hover { background: rgba(0,0,0,0.08); }

.error-msg {
    color: var(--danger);
    font-size: 14px;
    margin-top: 8px;
    text-align: center;
    min-height: 20px;
}

/* ===== 聊天页面布局 ===== */
#chat-page {
    flex-direction: row;
    position: fixed;
    top: 0;
    left: 0;
    right: 0;
    bottom: auto;
    height: 100vh;
    height: 100dvh;
}

/* ===== 侧边栏 ===== */
.sidebar {
    width: 280px;
    min-width: 280px;
    background: var(--sidebar-bg);
    color: var(--sidebar-text);
    display: flex;
    flex-direction: column;
    transition: transform 0.3s;
    z-index: 100;
}

.sidebar-header {
    padding: 16px;
    display: flex;
    justify-content: space-between;
    align-items: center;
    border-bottom: 1px solid rgba(255,255,255,0.1);
}
.sidebar-header h2 { font-size: 18px; }

#sidebar-close {
    display: none;
    color: var(--sidebar-text);
    font-size: 24px;
}

.sidebar-user {
    padding: 12px 16px;
    border-bottom: 1px solid rgba(255,255,255,0.1);
    display: flex;
    align-items: center;
    gap: 8px;
}
.role-badge {
    font-size: 11px;
    padding: 2px 8px;
    border-radius: 10px;
    background: rgba(255,255,255,0.2);
}

.sidebar-section {
    flex: 0 0 auto;
    overflow-y: auto;
    max-height: 45vh;
    padding: 12px 0;
}
.section-title {
    font-size: 12px;
    text-transform: uppercase;
    color: rgba(255,255,255,0.5);
    padding: 4px 16px;
    margin-bottom: 4px;
    letter-spacing: 0.5px;
}

.sidebar-footer {
    padding: 12px 16px;
    border-top: 1px solid rgba(255,255,255,0.1);
    display: flex;
    flex-direction: column;
    gap: 4px;
    margin-top: auto;
}
.sidebar-footer .btn-text { color: var(--sidebar-text); }
.sidebar-footer .btn-text:hover { background: var(--sidebar-hover); }

/* 频道和私聊列表项 */
.channel-item, .dm-item {
    padding: 5px 16px;
    cursor: pointer;
    display: flex;
    align-items: center;
    gap: 6px;
    font-size: 14px;
    transition: background 0.15s;
    border-left: 3px solid transparent;
    line-height: 1.3;
}
.channel-item:hover, .dm-item:hover { background: var(--sidebar-hover); }
.channel-item.active, .dm-item.active {
    background: var(--sidebar-hover);
    border-left-color: var(--primary);
}
.channel-item .channel-icon { font-size: 16px; }
.channel-item .lock-icon { font-size: 12px; opacity: 0.6; }

/* ===== 聊天区域 ===== */
.chat-area {
    flex: 1;
    display: flex;
    flex-direction: column;
    min-height: 0;
    background: var(--bg);
}

.chat-header {
    padding: 12px 16px;
    border-bottom: 1px solid var(--border);
    display: flex;
    align-items: center;
    gap: 12px;
    background: #fff;
    min-height: 56px;
}
.menu-btn {
    display: none;
    font-size: 24px;
}
.chat-header-info {
    flex: 1;
    overflow: hidden;
}
#chat-title {
    font-size: 16px;
    font-weight: 600;
    display: block;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}
.chat-subtitle {
    font-size: 12px;
    color: var(--text-secondary);
}
.online-dot {
    width: 8px;
    height: 8px;
    background: var(--success);
    border-radius: 50%;
    display: inline-block;
}

/* ===== 消息列表 ===== */
.message-container {
    flex: 1;
    overflow-y: auto;
    padding: 16px;
    display: flex;
    flex-direction: column;
    -webkit-overflow-scrolling: touch;
}

.message-list {
    display: flex;
    flex-direction: column;
    gap: 1px;
}

.load-more {
    text-align: center;
    padding: 12px;
}

/* 消息气泡 */
.message {
    display: flex;
    margin-bottom: 2px;
    max-width: 85%;
}

.message.own {
    align-self: flex-end;
}

.message.other {
    align-self: flex-start;
}

.msg-bubble {
    padding: 8px 12px;
    border-radius: 4px;
    font-size: 14px;
    line-height: 1.5;
    word-break: break-word;
}

.message.own .msg-bubble {
    background: var(--msg-own);
    color: var(--msg-own-text);
    border-radius: 12px 12px 4px 12px;
    box-shadow: 0 1px 0 rgba(0,0,0,0.05);
}

.message.other .msg-bubble {
    background: var(--msg-other);
    color: var(--msg-other-text);
    border-radius: 12px 12px 12px 4px;
}

.msg-meta {
    font-size: 11px;
    color: var(--text-secondary);
    padding: 2px 8px;
}

.message.own .msg-meta {
    text-align: right;
    color: #94a3b8;
}

.msg-sender {
    font-size: 12px;
    font-weight: 600;
    margin-bottom: 2px;
    padding: 0 4px;
    color: var(--text-secondary);
}

/* 消息类型 */
.msg-image {
    max-width: 240px;
    max-height: 320px;
    border-radius: 8px;
    cursor: pointer;
    display: block;
}
.msg-image.own-img {
    border: 2px solid rgba(255,255,255,0.3);
}

.msg-audio {
    max-width: 200px;
    height: 36px;
}

.msg-file {
    display: flex;
    align-items: center;
    gap: 8px;
    padding: 8px 12px;
    background: rgba(0,0,0,0.05);
    border-radius: 8px;
    text-decoration: none;
    color: inherit;
    font-size: 13px;
}
.msg-file:hover {
    background: rgba(0,0,0,0.1);
}

.msg-system {
    text-align: center;
    font-size: 12px;
    color: var(--text-secondary);
    padding: 4px;
    opacity: 0.7;
}

.msg-time-divider {
    text-align: center;
    font-size: 12px;
    color: var(--text-secondary);
    padding: 12px 0 8px;
    opacity: 0.6;
}

/* ===== 输入区域 ===== */
.input-area {
    padding: 12px 16px;
    border-top: 1px solid var(--border);
    background: #fff;
}
.reply-preview, .upload-preview {
    padding: 8px 12px;
    background: var(--bg);
    border-radius: var(--radius);
    margin-bottom: 8px;
    display: flex;
    align-items: center;
    gap: 8px;
    font-size: 13px;
}
#upload-preview-img {
    max-width: 100px;
    max-height: 100px;
    border-radius: 4px;
}
#upload-preview-audio {
    height: 32px;
}

.input-row {
    display: flex;
    align-items: flex-end;
    gap: 4px;
}
.input-btn {
    font-size: 22px;
    padding: 4px 6px;
    flex-shrink: 0;
}
#message-input {
    flex: 1;
    padding: 10px 14px;
    border: 1px solid var(--border);
    border-radius: 4px;
    font-size: 15px;
    resize: none;
    max-height: 120px;
    font-family: inherit;
    outline: none;
    transition: border-color 0.2s;
    background: #fff;
}
#message-input:focus {
    border-color: var(--primary);
}
.btn-send {
    padding: 10px 20px;
    background: var(--primary);
    color: #fff;
    border: none;
    border-radius: 4px;
    font-size: 15px;
    font-weight: 500;
    cursor: pointer;
    flex-shrink: 0;
    transition: background 0.2s;
}
.btn-send:hover { background: var(--primary-hover); }
.btn-send:active { transform: scale(0.97); }
.btn-send:disabled {
    background: #9ca3af;
    cursor: not-allowed;
}

/* ===== 语音录制指示器 ===== */
.voice-indicator {
    position: fixed;
    top: 0; left: 0; right: 0; bottom: 0;
    background: rgba(0,0,0,0.7);
    display: flex;
    justify-content: center;
    align-items: center;
    z-index: 1000;
}
.voice-indicator-inner {
    background: #fff;
    padding: 32px;
    border-radius: 16px;
    text-align: center;
}
.voice-wave {
    font-size: 64px;
    animation: pulse 1s ease-in-out infinite;
    margin-bottom: 16px;
}
@keyframes pulse {
    0%, 100% { transform: scale(1); opacity: 1; }
    50% { transform: scale(1.2); opacity: 0.7; }
}
.voice-indicator-inner p {
    margin-bottom: 8px;
    font-size: 16px;
}
.voice-indicator-inner .btn-primary {
    margin-top: 12px;
}

/* ===== 弹窗 ===== */
.modal {
    position: fixed;
    top: 0; left: 0; right: 0; bottom: 0;
    background: rgba(0,0,0,0.5);
    display: flex;
    justify-content: center;
    align-items: center;
    z-index: 500;
    overflow-y: auto;
    padding: 20px;
}
.modal-content {
    background: #fff;
    border-radius: 12px;
    width: 100%;
    max-width: 600px;
    max-height: 80vh;
    overflow-y: auto;
    box-shadow: 0 20px 60px rgba(0,0,0,0.3);
}
.admin-modal {
    max-width: 700px;
}
.small-modal {
    max-width: 400px;
}
.modal-header {
    padding: 16px 20px;
    border-bottom: 1px solid var(--border);
    display: flex;
    justify-content: space-between;
    align-items: center;
    position: sticky;
    top: 0;
    background: #fff;
    border-radius: 12px 12px 0 0;
    z-index: 1;
}
.modal-header h2 {
    font-size: 18px;
}

/* 管理后台标签 */
.admin-tabs {
    display: flex;
    border-bottom: 1px solid var(--border);
}
.admin-tab {
    flex: 1;
    padding: 12px;
    border: none;
    background: none;
    font-size: 14px;
    cursor: pointer;
    color: var(--text-secondary);
    border-bottom: 2px solid transparent;
    transition: all 0.2s;
}
.admin-tab.active {
    color: var(--primary);
    border-bottom-color: var(--primary);
    font-weight: 600;
}
.admin-panel {
    padding: 16px;
}

.admin-actions {
    margin-bottom: 12px;
}
.admin-actions .btn-primary {
    display: inline-block;
    width: auto;
}

.admin-table-wrap {
    overflow-x: auto;
}
.admin-table {
    width: 100%;
    border-collapse: collapse;
    font-size: 13px;
}
.admin-table th {
    text-align: left;
    padding: 8px 10px;
    border-bottom: 2px solid var(--border);
    font-size: 12px;
    color: var(--text-secondary);
    white-space: nowrap;
}
.admin-table td {
    padding: 8px 10px;
    border-bottom: 1px solid var(--border);
}
.admin-table .btn-text {
    font-size: 12px;
    padding: 4px 8px;
    color: var(--primary);
}
.admin-table .btn-text.danger {
    color: var(--danger);
}

/* 大图弹窗 */
#image-modal {
    z-index: 600;
    cursor: pointer;
}
#image-modal-img {
    max-width: 90vw;
    max-height: 90vh;
    border-radius: 8px;
}

/* ===== iOS 安全区 + 键盘适配 ===== */
@supports(padding-bottom: env(safe-area-inset-bottom)){
  .input-area{padding-bottom: max(12px, env(safe-area-inset-bottom));}
}

/* ===== 响应式 - 手机 ===== */
@media (max-width: 768px) {
    .sidebar {
        position: fixed;
        top: 0; left: 0; bottom: 0;
        transform: translateX(-100%);
    }
    .sidebar.open {
        transform: translateX(0);
    }

    #sidebar-close { display: block; }

    .menu-btn { display: block; }

    #chat-page .chat-area {
        width: 100%;
    }

    .message {
        max-width: 90%;
    }

    .msg-image {
        max-width: 180px;
        max-height: 240px;
    }

    .login-container {
        padding: 24px;
        margin: 16px;
    }
    .login-header h1 { font-size: 24px; }

    .modal-content {
        max-width: 95vw;
        max-height: 90vh;
    }
}

@media (max-width: 480px) {
    .input-btn { font-size: 18px; padding: 2px 4px; }
    #message-input { font-size: 14px; }
    .btn-send { padding: 8px 16px; font-size: 14px; }

    .admin-table { font-size: 12px; }
    .admin-table th, .admin-table td { padding: 6px 4px; }
}

/* ===== 消息状态勾 ===== */
.msg-status { font-size: 10px; margin-left: 3px; }
.msg-status.sent { color: #8D98A8; }
.msg-status.read { color: #4FC3F7; }

/* ===== 右键/长按菜单 ===== */
.context-menu {
    position: fixed; z-index: 800;
    background: #fff; border-radius: 8px;
    box-shadow: 0 4px 16px rgba(0,0,0,0.2);
    min-width: 160px; padding: 4px 0;
    display: none; flex-direction: column;
}
.ctx-item {
    display: block; width: 100%;
    padding: 10px 16px; border: none; background: none;
    font-size: 14px; text-align: left; cursor: pointer;
    color: var(--text);
}
.ctx-item:hover { background: var(--primary-light); }
.ctx-item.danger { color: var(--danger); }
.ctx-item.danger:hover { background: #FEE2E2; }

/* ===== 转发列表 ===== */
.forward-item {
    padding: 10px 12px; cursor: pointer;
    border-radius: 6px; font-size: 14px;
}
.forward-item:hover { background: var(--primary-light); color: var(--primary); }

/* ===== 系统消息(撤回等) ===== */
.msg-system {
    text-align: center; font-size: 12px;
    color: var(--text-secondary); padding: 4px;
    opacity: 0.7; max-width: 100% !important;
}
.msg-system .msg-bubble {
    background: transparent !important;
    border: 1px dashed #D4DAE2;
    box-shadow: none;
}

/* ===== C1-C5 新增样式 ===== */

/* 日期分隔线 */
.msg-date-divider {
    text-align: center;
    font-size: 12px;
    color: var(--text-secondary);
    padding: 16px 0 10px;
    opacity: 0.7;
    font-weight: 500;
}

/* 消息气泡分组 */
.message.grouped {
    margin-top: 1px;
}
.message.grouped .msg-sender {
    display: none;
}
.message.grouped .msg-bubble {
    border-radius: 4px 12px 12px 4px;
}
.message.grouped.own .msg-bubble {
    border-radius: 12px 4px 4px 12px;
}

/* 历史消息不带动画 */
.message.history .msg-bubble {
    animation: none !important;
}

/* 消息弹入动画 */
@keyframes msgPopIn {
    0% { transform: scale(0.85); opacity: 0; }
    60% { transform: scale(1.03); }
    100% { transform: scale(1); opacity: 1; }
}

/* 滚动到底部按钮 */
.scroll-bottom-btn {
    position: fixed;
    bottom: 80px;
    right: 24px;
    background: var(--primary);
    color: #fff;
    border: none;
    border-radius: 20px;
    padding: 8px 16px;
    font-size: 13px;
    font-weight: 600;
    cursor: pointer;
    z-index: 50;
    box-shadow: 0 2px 8px rgba(0,0,0,0.2);
    transition: transform 0.2s, opacity 0.2s;
}
.scroll-bottom-btn:hover {
    transform: scale(1.05);
}

/* 新消息浮动指示器 */
.new-msg-indicator {
    position: fixed;
    bottom: 120px;
    left: 50%;
    transform: translateX(-50%);
    background: rgba(0,0,0,0.8);
    color: #fff;
    padding: 8px 20px;
    border-radius: 20px;
    font-size: 13px;
    cursor: pointer;
    z-index: 50;
    white-space: nowrap;
    box-shadow: 0 2px 8px rgba(0,0,0,0.3);
}
.new-msg-indicator:hover {
    background: rgba(0,0,0,0.9);
}

/* 内联代码样式 (B4) */
.msg-bubble code {
    background: rgba(0,0,0,0.08);
    padding: 1px 5px;
    border-radius: 3px;
    font-family: 'SF Mono','Fira Code','Consolas',monospace;
    font-size: 0.9em;
    color: #e83e8c;
}
.message.own .msg-bubble code {
    background: rgba(0,0,0,0.06);
}

/* 代码块样式 (B7) */
.msg-bubble pre {
    background: #1e1e1e;
    color: #d4d4d4;
    padding: 12px 16px;
    border-radius: 8px;
    overflow-x: auto;
    margin: 4px 0;
    font-family: 'SF Mono','Fira Code','Consolas',monospace;
    font-size: 13px;
    line-height: 1.5;
}
.msg-bubble pre code {
    background: none;
    padding: 0;
    color: inherit;
    font-size: inherit;
}

/* 引用回复块 (B1) */
.reply-preview-bar {
    background: rgba(0,0,0,0.05);
    border-left: 3px solid var(--primary);
    padding: 4px 10px;
    border-radius: 4px;
    margin-bottom: 4px;
    font-size: 12px;
    color: var(--text-secondary);
    cursor: pointer;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
    max-width: 100%;
}
.message.own .reply-preview-bar {
    border-left-color: rgba(255,255,255,0.5);
}

/* ===== E2E 加密对话 ===== */
.e2e-placeholder {
    color: #059669;
    font-style: italic;
}

/* ===== 搜索栏 (G1) ===== */
.search-bar {
    padding: 8px 12px;
    background: #fff;
    border-bottom: 1px solid var(--border);
    display: flex;
    align-items: center;
    gap: 6px;
}
.search-bar input {
    flex: 1;
    padding: 6px 10px;
    border: 1px solid var(--border);
    border-radius: 6px;
    font-size: 14px;
    outline: none;
}
.search-bar input:focus { border-color: var(--primary); }
#search-count { font-size: 12px; color: var(--text-secondary); white-space: nowrap; }

/* ===== 未读徽章 (D3) ===== */
.unread-badge {
    background: var(--danger);
    color: #fff;
    border-radius: 10px;
    padding: 0 6px;
    font-size: 11px;
    font-weight: 600;
    min-width: 18px;
    text-align: center;
    margin-left: auto;
    line-height: 18px;
}

/* ===== 字体大小 (H4) ===== */
:root { --msg-font-size: 15px; }
.msg-bubble { font-size: var(--msg-font-size); }

/* ===== 图片画廊 (I1) ===== */
.gallery-nav {
    position: absolute; top: 50%; transform: translateY(-50%);
    background: rgba(255,255,255,0.2); color: #fff; border: none;
    font-size: 24px; padding: 16px 8px; cursor: pointer; z-index: 5;
    border-radius: 4px; transition: background 0.2s;
}
.gallery-nav:hover { background: rgba(255,255,255,0.4); }
.gallery-prev { left: 10px; }
.gallery-next { right: 10px; }
.gallery-count {
    position: absolute; bottom: 16px; left: 50%; transform: translateX(-50%);
    background: rgba(0,0,0,0.6); color: #fff; padding: 4px 12px;
    border-radius: 12px; font-size: 13px; z-index: 5;
}

/* ===== 多选模式 (E4) ===== */
.select-bar {
    padding: 8px 12px; background: #E3F2FD; border-bottom: 1px solid #90CAF9;
    display: flex; align-items: center; gap: 8px; font-size: 14px;
}
.message.selectable { cursor: pointer; }
.message.selected { background: rgba(42,171,238,0.1); border-radius: 4px; }
.message.selected .msg-bubble {
    box-shadow: 0 0 0 2px var(--primary);
}

/* ===== 左滑消息 (E2) ===== */
.message {
    position: relative; overflow: visible;
    transition: transform 0.2s ease;
}

/* ===== 共享媒体 (I2) ===== */
.media-tab.active {
    color: var(--primary); border-bottom: 2px solid var(--primary) !important;
    font-weight: 600;
}
.media-grid {
    display: grid; grid-template-columns: repeat(3,1fr); gap: 4px;
}
.media-grid-item {
    aspect-ratio: 1; overflow: hidden; cursor: pointer; border-radius: 4px;
}
.media-grid-item img {
    width: 100%; height: 100%; object-fit: cover;
}
.media-file-item {
    display: flex; align-items: center; gap: 8px; padding: 10px 8px;
    border-bottom: 1px solid var(--border); font-size: 13px;
}
.media-file-item a { color: var(--primary); text-decoration: none; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.media-file-item a:hover { text-decoration: underline; }
