/* ================= 全局重置 ================= */
* {
    box-sizing: border-box;
    -webkit-tap-highlight-color: transparent;
}

body, html {
    margin: 0;
    padding: 0;
    height: 100%;
    font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
    background-color: #f5f5f5;
    overscroll-behavior: none;
}

/* ================= 容器布局 ================= */
.chat-container {
    display: flex;
    flex-direction: column;
    height: 100%;
    width: 100%;
    position: absolute;
    left: 0;
    top: 0;
}

/* ================= 头部 ================= */
.chat-header {
    height: 50px;
    background: #fff;
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 0 15px;
    border-bottom: 1px solid #eee;
    flex-shrink: 0;
    z-index: 10;
}

.header-left { display: flex; flex-direction: column; }
.header-left h3 { margin: 0; font-size: 16px; color: #333; font-weight: 600; }
.status-badge { font-size: 11px; display: flex; align-items: center; margin-top: 2px; color: #999; }
.status-dot { width: 6px; height: 6px; border-radius: 50%; margin-right: 4px; background: #999; }
.status-badge.connecting .status-dot { background: #e6a23c; animation: blink 1s infinite; }
.status-badge.online .status-dot { background: #67c23a; }
.status-badge.offline .status-dot { background: #f56c6c; }

.close-btn {
    border: 1px solid #ff4d4f;
    color: #ff4d4f;
    background: #fff;
    padding: 4px 12px;
    border-radius: 15px;
    font-size: 12px;
    cursor: pointer;
    outline: none;
}

/* ================= 消息区域 (核心修复) ================= */
.chat-messages {
    flex: 1;
    overflow-y: auto;
    padding: 15px 12px;
    -webkit-overflow-scrolling: touch;
    background: #f5f5f5;
    display: flex;
    flex-direction: column;
}

/* 消息行容器：关键 Flex 修复 */
.message-item {
    display: flex; /* 必须是 flex */
    margin-bottom: 20px;
    width: 100%;
    align-items: flex-start; /* 顶部对齐 */
}

/* 自己发的消息靠右 */
.message-item.right-align {
    flex-direction: row; /* 保持 row，但在 HTML 结构上把头像放后面 */
    justify-content: flex-end; /* 整体靠右 */
}

/* 头像通用样式 */
.avatar {
    width: 40px;
    height: 40px;
    border-radius: 6px;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 16px;
    color: #fff;
    flex-shrink: 0; /* 防止被挤压 */
    overflow: hidden;
    box-shadow: 0 1px 2px rgba(0,0,0,0.1);
}

.avatar img { width: 100%; height: 100%; object-fit: cover; }

/* 客服头像 (左侧) */
.agent-avatar {
    background: #fff;
    color: #1890ff;
    border: 1px solid #eee;
    font-weight: bold;
    margin-right: 10px; /* 右侧留空给气泡 */
}

/* 用户头像 (右侧) */
.user-avatar {
    background: #409eff;
    margin-left: 10px; /* 左侧留空给气泡 */
    font-weight: 500;
}

/* 消息内容包装区 */
.message-body {
    max-width: 75%;
    display: flex;
    flex-direction: column; /* 气泡在上，状态在下 */
}

/* 左侧消息 (客服) 内容左对齐 */
.left-align .message-body {
    align-items: flex-start;
}

/* 右侧消息 (用户) 内容右对齐 */
.right-align .message-body {
    align-items: flex-end;
}

/* 气泡通用样式 */
.bubble {
    padding: 10px 14px;
    border-radius: 6px;
    font-size: 15px;
    line-height: 1.5;
    word-break: break-all;
    position: relative;
    box-shadow: 0 1px 2px rgba(0,0,0,0.05);
}

/* 客服气泡 */
.agent-bubble {
    background: #fff;
    color: #333;
    border-top-left-radius: 2px;
}
.agent-bubble img { max-width: 100%; height: auto; border-radius: 4px; display: block; }

/* 用户气泡 */
.user-bubble {
    background: #95ec69;
    color: #000;
    border-top-right-radius: 2px;
}

/* 图片/视频容器 */
.is-image {
    padding: 0;
    background: transparent;
    box-shadow: none;
    overflow: hidden;
    border-radius: 6px;
}
.chat-img { max-width: 140px; max-height: 200px; display: block; border-radius: 6px; border: 1px solid #eee; background: #fff;}
.chat-video { max-width: 200px; max-height: 300px; border-radius: 6px; background: #000; display: block; }

/* 底部元数据行 (时间 + 状态) */
.meta-row {
    display: flex;
    align-items: center;
    margin-top: 4px;
    font-size: 10px;
    color: #b2b2b2;
    gap: 6px;
}

/* 未读/已读 状态颜色 */
.status-text {
    color: #f56c6c; /* 未读默认红色 */
}
.status-text.read {
    color: #b2b2b2; /* 已读变灰 */
}

/* 进度条圆环 */
.upload-mask {
    position: absolute; top: 0; left: 0; width: 100%; height: 100%;
    background: rgba(0,0,0,0.5); display: flex; align-items: center; justify-content: center;
    border-radius: 6px;
    flex-direction: column;
}
.circular-chart { display: block; margin: 0 auto; max-width: 40px; max-height: 40px; }
.circle-bg { fill: none; stroke: rgba(255,255,255,0.3); stroke-width: 3; }
.circle { fill: none; stroke: #fff; stroke-width: 3; stroke-linecap: round; transition: stroke-dasharray 0.3s ease; }

/* 系统通知条 */
.notice-bar { text-align: center; margin: 15px 0; font-size: 12px; color: #999; width: 100%; }
.notice-bar span { background: #e9e9e9; padding: 3px 10px; border-radius: 10px; }
.notice-bar.warning span { background: #fdf6ec; color: #e6a23c; }
.notice-bar.info span { background: #e9e9e9; color: #909399; }

/* ================= 底部输入框 ================= */
.chat-footer { background: #f7f7f7; border-top: 1px solid #e5e5e5; padding: 10px; flex-shrink: 0; }
.toolbar { display: flex; padding-bottom: 8px; gap: 20px; padding-left: 5px; }
.tool-btn { width: 26px; height: 26px; opacity: 0.7; }
.input-row { display: flex; align-items: flex-end; gap: 10px; background: #fff; border-radius: 6px; padding: 8px; }
#msg-input { flex: 1; border: none; outline: none; background: transparent; resize: none; max-height: 80px; padding: 2px 0; font-size: 15px; line-height: 20px; font-family: inherit; }
.send-btn { background: #07c160; color: #fff; border: none; padding: 6px 14px; border-radius: 4px; font-size: 14px; flex-shrink: 0; font-weight: 500; }
.send-btn:disabled { background: #ccecdf; color: #f0f0f0; }
.emoji-panel { height: 180px; overflow-y: auto; background: #f7f7f7; border-top: 1px solid #eee; display: grid; grid-template-columns: repeat(8, 1fr); padding: 10px; gap: 5px; }
.emoji-item { font-size: 22px; text-align: center; padding: 5px 0; }
.safe-area-bottom { height: env(safe-area-inset-bottom); background: #f7f7f7; }

@keyframes blink { 0% { opacity: 1; } 50% { opacity: 0.4; } 100% { opacity: 1; } }