/* =====================================================
   Telegram Live Chat Widget Styles
   ===================================================== */
#tg-chat-widget {
    position: fixed;
    bottom: 20px;
    right: 20px;
    /* 高于常见底部导航栏（如 mobile_royal .royal-nav 99999），避免气泡被挡住无法点击 */
    z-index: 100002;
    font-family: "Inter", "Microsoft YaHei", sans-serif;
}

/* ── 悬浮气泡 ── */
.chat-bubble {
    width: 60px;
    height: 60px;
    background: linear-gradient(135deg, #d4af37, #8b5cf6);
    border-radius: 50%;
    display: flex;
    align-items: center;
    justify-content: center;
    cursor: pointer;
    box-shadow: 0 5px 20px rgba(139,92,246,0.45);
    transition: transform 0.3s ease, box-shadow 0.3s ease;
    position: relative;
}
.chat-bubble:hover { transform: scale(1.08); box-shadow: 0 8px 25px rgba(139,92,246,0.55); }
.chat-icon i { color: white; font-size: 24px; }

.unread-badge {
    position: absolute;
    top: -5px; right: -5px;
    background: #ef4444;
    color: white;
    font-size: 12px;
    font-weight: bold;
    width: 20px; height: 20px;
    border-radius: 50%;
    display: flex;
    align-items: center;
    justify-content: center;
    box-shadow: 0 2px 5px rgba(0,0,0,0.2);
}

/* ── 聊天面板 ── */
.chat-panel {
    position: absolute;
    bottom: 80px; right: 0;
    width: 340px;
    max-height: 520px;
    background: #fff;
    border-radius: 16px;
    box-shadow: 0 12px 40px rgba(0,0,0,0.18);
    display: none;
    flex-direction: column;
    /* overflow:hidden removed — it was blocking toolbar button clicks and clipping emoji panel */
    border: 1px solid rgba(0,0,0,0.06);
}
.chat-panel.active {
    display: flex;
    animation: slideUp 0.25s ease forwards;
}

@keyframes slideUp {
    from { opacity: 0; transform: translateY(20px); }
    to   { opacity: 1; transform: translateY(0); }
}

/* ── 标题栏 ── */
.chat-header {
    background: linear-gradient(135deg, #d4af37, #8b5cf6);
    color: white;
    padding: 14px 16px;
    display: flex;
    justify-content: space-between;
    align-items: center;
    font-weight: bold;
    font-size: 15px;
    flex-shrink: 0;
}
.chat-header-close { cursor: pointer; opacity: 0.8; transition: opacity 0.2s; }
.chat-header-close:hover { opacity: 1; }

/* ── 消息区 ── */
.chat-body {
    flex: 1;
    padding: 14px;
    overflow-y: auto;   /* 只在消息区域滚动，而非整个面板 */
    background: #f4f5f7;
    display: flex;
    flex-direction: column;
    gap: 10px;
    min-height: 0;
    border-radius: 0;   /* 内部不需要圆角 */
}

.chat-msg { max-width: 82%; animation: fadeIn 0.25s ease; }
.chat-msg.sent    { align-self: flex-end; }
.chat-msg.received { align-self: flex-start; }
@keyframes fadeIn { from { opacity: 0; transform: translateY(6px); } to { opacity: 1; transform: none; } }

.chat-msg .msg-content {
    padding: 9px 13px;
    border-radius: 18px;
    font-size: 14px;
    line-height: 1.5;
    word-break: break-word;
}
.chat-msg.sent .msg-content {
    background: linear-gradient(135deg, #8b5cf6, #6d28d9);
    color: white;
    border-bottom-right-radius: 4px;
}
.chat-msg.received .msg-content {
    background: white;
    color: #374151;
    border: 1px solid #e5e7eb;
    border-bottom-left-radius: 4px;
    box-shadow: 0 2px 5px rgba(0,0,0,0.04);
}
/* 图片消息 */
.chat-msg .msg-image {
    max-width: 200px;
    border-radius: 12px;
    cursor: pointer;
    display: block;
    box-shadow: 0 3px 10px rgba(0,0,0,0.15);
}
.chat-msg.sending .msg-content {
    opacity: 0.6;
    font-style: italic;
}

/* ── 工具栏（表情 + 图片按钮） ── */
.chat-toolbar {
    display: flex;
    align-items: center;
    padding: 6px 12px 0;
    gap: 4px;
    background: white;
    flex-shrink: 0;
}
.toolbar-btn {
    background: none;
    border: none;
    cursor: pointer;
    width: 32px; height: 32px;
    border-radius: 8px;
    display: flex;
    align-items: center;
    justify-content: center;
    color: #6b7280;
    font-size: 18px;
    transition: background 0.15s, color 0.15s;
}
.toolbar-btn:hover { background: #f3f4f6; color: #8b5cf6; }
.toolbar-btn.active { color: #d4af37; }

/* 单一文件选择器：勿用 display:none，否则部分移动浏览器拒绝程序化 .click() */
.chat-file-input-hidden {
    position: fixed;
    left: 0;
    top: 0;
    width: 0.01px;
    height: 0.01px;
    opacity: 0;
    overflow: hidden;
    pointer-events: none;
}

/* 登录页等：隐藏右下角悬浮气泡，保留脚本触发 open（如「联系客服」链接触发 toggle） */
#tg-chat-widget .chat-bubble.chat-bubble--hidden-ui {
    position: absolute !important;
    width: 1px !important;
    height: 1px !important;
    margin: -1px !important;
    padding: 0 !important;
    overflow: hidden !important;
    clip: rect(0, 0, 0, 0) !important;
    white-space: nowrap !important;
    border: 0 !important;
    opacity: 0 !important;
    pointer-events: none !important;
}

/* ── 表情面板 ── */
.emoji-panel {
    background: white;
    border-top: 1px solid #f0f0f0;
    padding: 8px 10px;
    flex-shrink: 0;
}
.emoji-grid {
    display: grid;
    grid-template-columns: repeat(10, 1fr);
    gap: 2px;
    max-height: 140px;
    overflow-y: auto;
}
.emoji-grid span {
    font-size: 20px;
    text-align: center;
    cursor: pointer;
    padding: 3px;
    border-radius: 6px;
    line-height: 1.4;
    transition: background 0.1s, transform 0.1s;
    user-select: none;
}
.emoji-grid span:hover { background: #f3e8ff; transform: scale(1.25); }

/* ── 图片预览条 ── */
.image-preview-bar {
    display: flex;
    align-items: center;
    gap: 10px;
    padding: 6px 12px;
    background: #faf5ff;
    border-top: 1px solid #ede9fe;
    flex-shrink: 0;
}
.image-preview-bar img {
    width: 40px; height: 40px;
    object-fit: cover;
    border-radius: 8px;
    border: 2px solid #8b5cf6;
}
.image-preview-bar span {
    flex: 1;
    font-size: 12px;
    color: #6b7280;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}
#image-preview-cancel {
    background: none;
    border: none;
    color: #ef4444;
    cursor: pointer;
    font-size: 14px;
    padding: 2px 6px;
    border-radius: 6px;
    transition: background 0.15s;
}
#image-preview-cancel:hover { background: #fee2e2; }

/* ── 输入区 ── */
.chat-footer {
    padding: 8px 12px 12px;
    border-top: 1px solid #e5e7eb;
    display: flex;
    gap: 8px;
    background: white;
    align-items: center;
    flex-shrink: 0;
}
.chat-footer input {
    flex: 1;
    border: 1px solid #d1d5db;
    border-radius: 20px;
    padding: 8px 14px;
    outline: none;
    font-size: 14px;
    transition: border 0.2s;
    min-width: 0;
}
.chat-footer input:focus { border-color: #8b5cf6; }
.chat-footer button {
    background: linear-gradient(135deg, #d4af37, #8b5cf6);
    color: white;
    border: none;
    width: 36px; height: 36px;
    border-radius: 50%;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    transition: opacity 0.2s, transform 0.2s;
    flex-shrink: 0;
}
.chat-footer button:hover { opacity: 0.9; transform: scale(1.05); }
.chat-footer button:disabled { opacity: 0.5; cursor: not-allowed; transform: none; }

/* ── 移动端：沉浸式全屏聊天窗口 ── */
@media (max-width: 480px) {
    #tg-chat-widget {
        bottom: 80px !important;
        right: 15px !important;
        /* 防止全屏时 z-index 过低被遮挡 */
        z-index: 100005;
    }
    
    .chat-panel.active {
        position: fixed !important;
        top: 0 !important;
        left: 0 !important;
        right: 0 !important;
        bottom: 0 !important;
        width: 100vw !important;
        /* 优先使用 100dvh，遇到不支持的浏览器则回退到底部 */
        height: 100vh !important;
        height: 100dvh !important; 
        max-height: none !important;
        border-radius: 0 !important;
        border: none !important; /* 去除边框，原生的无缝感 */
        margin: 0 !important;
        display: flex !important;
        flex-direction: column !important;
        background: #f4f5f7 !important; /* 让整个背景和消息区融为一体 */
        /* 动画：从底部全尺寸顺滑覆盖 */
        animation: slideUpFullscreen 0.35s cubic-bezier(0.16, 1, 0.3, 1) forwards !important;
    }
    
    /* 让标题栏更沉浸一些 */
    .chat-panel .chat-header {
        padding: 16px 20px !important;
        font-size: 16px !important;
        border-radius: 0 !important;
        box-shadow: 0 1px 12px rgba(0,0,0,0.12); /* 悬浮阴影，增加层次感 */
    }
    
    /* 底部输入框空间适配：补偿 iPhone X 及以上底部黑条安全区 */
    .chat-panel .chat-footer {
        padding-bottom: max(12px, env(safe-area-inset-bottom)) !important;
    }

    /* 键盘弹起时的展示：JS 接管 top 和 height 后，我们只需拿掉不需要的安全区留白 */
    #tg-chat-widget.mobile-keyboard-open .chat-panel .chat-footer {
        padding-bottom: 8px !important; /* 紧贴键盘，无需留安全区 */
    }
    
    /* 遮盖聊天输入框后，隐藏本身悬浮小气泡 */
    #tg-chat-widget.mobile-keyboard-open .chat-bubble {
        display: none !important;
    }
    
    .emoji-grid { grid-template-columns: repeat(8, 1fr) !important; }
}

@keyframes slideUpFullscreen {
    from { opacity: 0; transform: translateY(100%); }
    to   { opacity: 1; transform: translateY(0); }
}

/* ── 纯表情大字显示（连续 Emoji 且无其他文字时） ── */
.chat-msg .msg-content.emoji-only {
    font-size: 28px;
    background: transparent !important;
    border: none !important;
    box-shadow: none !important;
    padding: 4px 8px;
    line-height: 1.3;
}

/* ── 收到图片消息样式 ── */
.chat-msg .msg-image-recv {
    max-width: 200px;
    border-radius: 12px;
    cursor: pointer;
    display: block;
    box-shadow: 0 3px 10px rgba(0,0,0,0.2);
    transition: transform 0.2s;
}
.chat-msg .msg-image-recv:hover { transform: scale(1.03); }

