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

:root {
    --primary: #3b82f6;
    --primary-dark: #2563eb;
    --bg: #f0f2f5;
    --panel: #ffffff;
    --text: #1f2937;
    --text-muted: #6b7280;
    --border: #e5e7eb;
    --visitor-bubble: #eef2ff;
    --admin-bubble: #3b82f6;
}

html, body {
    height: 100%;
}

body {
    font-family: "Segoe UI", "Microsoft YaHei", "PingFang SC", Arial, sans-serif;
    color: var(--text);
    background: var(--bg);
    font-size: 14px;
}

button {
    cursor: pointer;
    font-family: inherit;
}

input, textarea {
    font-family: inherit;
    color: var(--text);
}

/* 按钮 */
.btn {
    border: none;
    border-radius: 6px;
    background: var(--border);
    color: var(--text);
    padding: 8px 16px;
    font-size: 14px;
    transition: background .15s;
}

.btn:hover {
    background: #d1d5db;
}

.btn-primary {
    background: var(--primary);
    color: #fff;
}

.btn-primary:hover {
    background: var(--primary-dark);
}

.btn-sm {
    padding: 5px 12px;
    font-size: 13px;
}

.btn-block {
    width: 100%;
    padding: 12px;
    font-size: 15px;
}

.btn-ghost {
    background: transparent;
    color: var(--text-muted);
}

.btn-ghost:hover {
    background: var(--border);
}

/* ===== 访客页面 ===== */
.visitor-wrap {
    height: 100vh;
    display: flex;
    align-items: center;
    justify-content: center;
    padding: 16px;
}

.chat-panel {
    width: 100%;
    max-width: 760px;
    height: 100%;
    max-height: 90vh;
    background: var(--panel);
    border-radius: 12px;
    box-shadow: 0 4px 24px rgba(0, 0, 0, .08);
    display: flex;
    flex-direction: column;
    overflow: hidden;
}

.chat-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 12px;
    padding: 12px 16px;
    background: var(--panel);
    border-bottom: 1px solid var(--border);
    flex-wrap: wrap;
}

.header-left {
    display: flex;
    align-items: center;
    gap: 8px;
}

.logo-dot {
    width: 10px;
    height: 10px;
    border-radius: 50%;
    background: #22c55e;
    box-shadow: 0 0 0 3px rgba(34, 197, 94, .2);
}

.title {
    font-size: 16px;
    font-weight: 600;
}

.header-right {
    display: flex;
    align-items: center;
    gap: 8px;
}

.nickname-input {
    border: 1px solid var(--border);
    border-radius: 6px;
    padding: 5px 10px;
    font-size: 13px;
    width: 140px;
    outline: none;
}

.nickname-input:focus {
    border-color: var(--primary);
}

.conn-status {
    font-size: 12px;
    color: var(--text-muted);
}

.conn-status.online {
    color: #16a34a;
}

/* 消息区 */
.chat-body {
    flex: 1;
    overflow-y: auto;
    min-height: 0;
    padding: 16px;
    display: flex;
    flex-direction: column;
    gap: 12px;
}

.empty-tip {
    margin: auto;
    text-align: center;
    color: var(--text-muted);
    font-size: 14px;
    line-height: 1.6;
}

.msg {
    display: flex;
    flex-direction: column;
    max-width: 75%;
}

.msg.visitor {
    align-self: flex-end;
    align-items: flex-end;
}

.msg.admin {
    align-self: flex-start;
    align-items: flex-start;
}

.msg .meta {
    font-size: 12px;
    color: var(--text-muted);
    margin-bottom: 4px;
}

.msg .bubble {
    padding: 10px 14px;
    border-radius: 12px;
    line-height: 1.5;
    word-break: break-word;
    white-space: pre-wrap;
    font-size: 14px;
}

.msg.visitor .bubble {
    background: var(--visitor-bubble);
    color: var(--text);
    border-top-right-radius: 2px;
}

.msg.admin .bubble {
    background: var(--admin-bubble);
    color: #fff;
    border-top-left-radius: 2px;
}

/* 管理后台：管理员是"自己"，消息靠右；访客靠左 */
body.admin-panel .msg.admin {
    align-self: flex-end;
    align-items: flex-end;
}

body.admin-panel .msg.visitor {
    align-self: flex-start;
    align-items: flex-start;
}

body.admin-panel .msg.admin .bubble {
    border-top-left-radius: 12px;
    border-top-right-radius: 2px;
}

body.admin-panel .msg.visitor .bubble {
    border-top-left-radius: 2px;
    border-top-right-radius: 12px;
}

/* 输入区 */
.chat-footer {
    display: flex;
    align-items: flex-end;
    gap: 10px;
    padding: 12px 16px;
    border-top: 1px solid var(--border);
    background: var(--panel);
    flex: none; /* 防止被压缩挤出屏幕，始终固定在底部 */
}

.msg-input {
    flex: 1;
    border: 1px solid var(--border);
    border-radius: 8px;
    padding: 10px 12px;
    font-size: 14px;
    resize: none;
    height: 60px;
    outline: none;
    line-height: 1.5;
}

.msg-input:focus {
    border-color: var(--primary);
}

/* ===== 登录页 ===== */
.login-wrap {
    height: 100vh;
    display: flex;
    align-items: center;
    justify-content: center;
    padding: 16px;
}

.login-card {
    width: 100%;
    max-width: 340px;
    background: var(--panel);
    border-radius: 12px;
    padding: 32px 28px;
    box-shadow: 0 4px 24px rgba(0, 0, 0, .08);
}

.login-card h2 {
    text-align: center;
    font-size: 20px;
    margin-bottom: 24px;
}

.login-input {
    width: 100%;
    border: 1px solid var(--border);
    border-radius: 6px;
    padding: 11px 12px;
    font-size: 14px;
    margin-bottom: 14px;
    outline: none;
}

.login-input:focus {
    border-color: var(--primary);
}

.login-error {
    color: #dc2626;
    font-size: 13px;
    text-align: center;
    margin-top: 10px;
    min-height: 18px;
}

/* ===== 管理后台 ===== */
.admin-wrap {
    height: 100vh;
    height: 100dvh; /* 移动端浏览器地址栏伸缩时保持一屏，防止底部输入区被挤出屏幕 */
    display: flex;
    overflow: hidden;
}

.sidebar {
    width: 220px;
    min-width: 220px;
    background: #fff;
    border-right: 1px solid var(--border);
    display: flex;
    flex-direction: column;
    min-height: 0;
    overflow: hidden;
}

.sidebar-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 14px 16px;
    border-bottom: 1px solid var(--border);
}

.conv-list {
    flex: 1;
    overflow-y: auto;
}

.conv-item {
    display: flex;
    align-items: center;
    gap: 8px;
    padding: 10px 12px;
    border-bottom: 1px solid #f3f4f6;
    cursor: pointer;
    transition: background .15s;
}

.conv-item:hover {
    background: #f9fafb;
}

.conv-item.active {
    background: var(--visitor-bubble);
}

.conv-avatar {
    width: 34px;
    height: 34px;
    min-width: 34px;
    border-radius: 50%;
    background: var(--primary);
    color: #fff;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 15px;
    font-weight: 600;
}

.conv-info {
    flex: 1;
    min-width: 0;
}

.conv-top {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 8px;
}

.conv-name {
    font-weight: 600;
    font-size: 14px;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}

.conv-time {
    font-size: 12px;
    color: var(--text-muted);
    white-space: nowrap;
}

.conv-preview {
    font-size: 13px;
    color: var(--text-muted);
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
    margin-top: 3px;
}

.conv-meta {
    font-size: 12px;
    color: #9ca3af;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
    margin-top: 2px;
}

.rename-tip {
    font-size: 13px;
    color: var(--text-muted);
    line-height: 1.6;
    margin-bottom: 12px;
}

.conv-badge {
    background: #ef4444;
    color: #fff;
    font-size: 12px;
    border-radius: 10px;
    padding: 1px 7px;
    min-width: 18px;
    text-align: center;
}

.main-area {
    flex: 1;
    display: flex;
    flex-direction: column;
    background: var(--bg);
    min-height: 0;
    overflow: hidden;
}

.main-empty {
    flex: 1;
    display: flex;
    align-items: center;
    justify-content: center;
    color: var(--text-muted);
    font-size: 15px;
}

.main-chat {
    flex: 1;
    display: flex;
    flex-direction: column;
    background: var(--panel);
    min-height: 0;
    overflow: hidden;
}

.conv-empty {
    color: var(--text-muted);
    text-align: center;
    padding: 30px;
    font-size: 14px;
}

/* 搜索框 */
.sidebar-search {
    padding: 10px 12px;
    border-bottom: 1px solid var(--border);
}

.search-input {
    width: 100%;
    border: 1px solid var(--border);
    border-radius: 6px;
    padding: 8px 10px;
    font-size: 13px;
    outline: none;
}

.search-input:focus {
    border-color: var(--primary);
}

/* 侧边栏底部 */
.sidebar-footer {
    padding: 10px 12px;
    border-top: 1px solid var(--border);
}

/* 弹窗 */
.modal-mask {
    position: fixed;
    inset: 0;
    background: rgba(0, 0, 0, .4);
    display: flex;
    align-items: center;
    justify-content: center;
    z-index: 100;
}

.modal {
    width: 100%;
    max-width: 340px;
    background: #fff;
    border-radius: 12px;
    padding: 24px;
    box-shadow: 0 8px 32px rgba(0, 0, 0, .2);
}

.modal h3 {
    font-size: 18px;
    text-align: center;
    margin-bottom: 20px;
}

.modal .login-input {
    margin-bottom: 12px;
}

.modal-actions {
    display: flex;
    gap: 10px;
    margin-top: 6px;
}

.modal-actions .btn {
    flex: 1;
}

/* 图标按钮（图片 / 语音，类似 QQ 微信） */
.icon-btn {
    width: 40px;
    height: 40px;
    min-width: 40px;
    border-radius: 50%;
    background: #f5f6fa;
    border: none;
    display: flex;
    align-items: center;
    justify-content: center;
    color: #60656f;
    flex-shrink: 0;
    cursor: pointer;
    transition: background .15s, color .15s;
}

.icon-btn:hover {
    background: #e8eaef;
    color: #333;
}

.icon-btn svg {
    width: 22px;
    height: 22px;
}

.icon-btn.disabled {
    opacity: .4;
    pointer-events: none;
}

/* 图片消息：固定尺寸缩略图（类似 QQ），点击弹出查看大图 */
.msg .bubble.bubble-img {
    padding: 4px;
    background: transparent;
}

.msg .bubble.bubble-img img {
    width: 180px;
    height: 180px;
    object-fit: cover;
    border-radius: 10px;
    display: block;
    cursor: zoom-in;
}

/* 大图预览遮罩 */
.lightbox {
    position: fixed;
    inset: 0;
    background: rgba(0, 0, 0, .85);
    display: flex;
    align-items: center;
    justify-content: center;
    z-index: 200;
}

.lightbox img {
    max-width: 92vw;
    max-height: 92vh;
    border-radius: 6px;
    background: #fff;
}

.lightbox-close {
    position: absolute;
    top: 14px;
    right: 18px;
    color: #fff;
    font-size: 30px;
    line-height: 1;
    cursor: pointer;
    padding: 4px;
    user-select: none;
}

/* 录音按钮 */
.voice-btn {
    touch-action: none;
    -webkit-user-select: none;
    user-select: none;
    -webkit-touch-callout: none;
    position: relative;
}

.voice-btn .voice-rec-tip {
    display: none;
    font-size: 13px;
    font-weight: 600;
    white-space: nowrap;
}

.voice-btn.recording {
    background: #ef4444;
    color: #fff;
    width: 88px;
    border-radius: 20px;
}

.voice-btn.recording .voice-icon {
    display: none;
}

.voice-btn.recording .voice-rec-tip {
    display: block;
}

/* 语音消息气泡 */
.msg .bubble.bubble-voice {
    display: flex;
    align-items: center;
    gap: 8px;
    cursor: pointer;
    background: var(--admin-bubble);
    color: #fff;
    min-width: 130px;
}

.msg.visitor .bubble.bubble-voice {
    background: var(--visitor-bubble);
    color: var(--text);
}

.voice-play {
    font-size: 14px;
    line-height: 1;
}

.voice-bar {
    flex: 1;
    height: 3px;
    border-radius: 2px;
    background: rgba(255, 255, 255, .4);
    overflow: hidden;
    position: relative;
}

.msg.visitor .voice-bar {
    background: rgba(0, 0, 0, .12);
}

.voice-bar::after {
    content: '';
    position: absolute;
    left: 0;
    top: 0;
    bottom: 0;
    width: 0;
    background: #fff;
    border-radius: 2px;
    transition: width .3s linear;
}

.msg.visitor .voice-bar::after {
    background: var(--text);
}

.msg .bubble.bubble-voice.playing .voice-bar::after {
    width: 100%;
}

.voice-time {
    font-size: 12px;
    opacity: .85;
    white-space: nowrap;
}

/* 返回按钮：默认（电脑端）隐藏，手机端显示 */
.back-btn {
    display: none;
}

/* ===== 手机端适配（客服后台） ===== */
@media (max-width: 768px) {
    .admin-wrap {
        flex-direction: row;
    }

    /* 默认只显示会话列表，聊天区隐藏 */
    .sidebar {
        width: 100%;
        min-width: 0;
        border-right: none;
    }

    .main-area {
        display: none;
        position: absolute;
        inset: 0;
        z-index: 20;
        background: var(--panel);
    }

    /* 打开会话后：显示聊天区，隐藏列表 */
    body.chat-open .sidebar {
        display: none;
    }

    body.chat-open .main-area {
        display: flex;
    }

    body.chat-open .back-btn {
        display: inline-flex;
    }

    .admin-wrap {
        position: relative;
    }

    .chat-header {
        padding: 10px 12px;
    }

    .chat-body {
        padding: 12px;
    }

    .msg {
        max-width: 85%;
    }

    .msg .bubble.bubble-img img {
        width: 140px;
        height: 140px;
    }

    .chat-footer {
        padding: 10px 12px;
        gap: 8px;
    }

    .msg-input {
        font-size: 16px; /* 防止 iOS 聚焦时自动放大 */
    }

    /* 登录卡片 */
    .login-card {
        margin: 0 16px;
    }
}
