/**
 * 客服对话样式
 * 支持三种主题配色：light（白色）、dark（黑色）、blue（蓝色）
 * 兼容低版本iOS设备（间距、CSS兼容）
 */

/* 基础样式 */
.kefu-overlay {
    position: fixed;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    background: rgba(0, 0, 0, 0.5);
    z-index: 10000;
    display: none;
    /* 未激活时完全不影响页面，允许页面滚动 */
    pointer-events: none;
    touch-action: auto;
    /* iOS低版本兼容 */
    -webkit-backdrop-filter: blur(4px);
    backdrop-filter: blur(4px);
}

.kefu-overlay.active {
    display: -webkit-box;
    display: -webkit-flex;
    display: -ms-flexbox;
    display: flex;
    -webkit-box-align: center;
    -webkit-align-items: center;
    -ms-flex-align: center;
    align-items: center;
    -webkit-box-pack: center;
    -webkit-justify-content: center;
    -ms-flex-pack: center;
    justify-content: center;
    /* 激活时阻止背景滚动，但允许内容区域滚动 */
    pointer-events: auto;
    touch-action: none;
}

.kefu-modal {
    width: 100%;
    height: 100%;
    background: #fff;
    display: -webkit-box;
    display: -webkit-flex;
    display: -ms-flexbox;
    display: flex;
    -webkit-box-orient: vertical;
    -webkit-box-direction: normal;
    -webkit-flex-direction: column;
    -ms-flex-direction: column;
    flex-direction: column;
    position: relative;
    /* 允许内容区域滚动 */
    touch-action: pan-y;
    pointer-events: auto;
    /* iOS安全区域适配 */
    padding-top: env(safe-area-inset-top, 0px);
    padding-bottom: env(safe-area-inset-bottom, 0px);
    padding-left: env(safe-area-inset-left, 0px);
    padding-right: env(safe-area-inset-right, 0px);
}

.kefu-back {
    position: absolute;
    top: calc(15px + env(safe-area-inset-top, 0px));
    left: 15px;
    z-index: 10;
    width: 40px;
    height: 40px;
    border: none;
    background: rgba(0, 0, 0, 0.1);
    border-radius: 50%;
    display: -webkit-box;
    display: -webkit-flex;
    display: -ms-flexbox;
    display: flex;
    -webkit-box-align: center;
    -webkit-align-items: center;
    -ms-flex-align: center;
    align-items: center;
    -webkit-box-pack: center;
    -webkit-justify-content: center;
    -ms-flex-pack: center;
    justify-content: center;
    cursor: pointer;
    /* iOS低版本兼容 */
    appearance: none;
    -webkit-appearance: none;
    -webkit-border-radius: 50%;
    -webkit-tap-highlight-color: transparent;
}

.back-icon {
    width: 24px;
    height: 24px;
    color: #333;
}

.kefu-header {
    padding: 15px 20px;
    border-bottom: 1px solid #e0e0e0;
    /* iOS低版本兼容 - 确保间距 */
    min-height: 60px;
    -webkit-box-sizing: border-box;
    box-sizing: border-box;
    padding-top: calc(15px + env(safe-area-inset-top, 0px));
    display: -webkit-box;
    display: -webkit-flex;
    display: -ms-flexbox;
    display: flex;
    -webkit-box-pack: center;
    -webkit-justify-content: center;
    -ms-flex-pack: center;
    justify-content: center;
    -webkit-box-align: center;
    -webkit-align-items: center;
    -ms-flex-align: center;
    align-items: center;
}

.kefu-title {
    text-align: center;
}

.kefu-title h3 {
    font-size: 18px;
    font-weight: 600;
    margin-bottom: 4px;
    color: #333;
    text-align: center;
}

.kefu-status {
    display: -webkit-box;
    display: -webkit-flex;
    display: -ms-flexbox;
    display: flex;
    -webkit-box-align: center;
    -webkit-align-items: center;
    -ms-flex-align: center;
    align-items: center;
    font-size: 12px;
    color: #999;
}

.status-dot {
    width: 8px;
    height: 8px;
    border-radius: 50%;
    background: #4caf50;
    margin-right: 6px;
    /* iOS低版本兼容 */
    -webkit-border-radius: 50%;
}

.kefu-messages {
    flex: 1;
    overflow-y: auto;
    padding: 20px;
    /* 允许垂直滚动 */
    touch-action: pan-y;
    pointer-events: auto;
    /* iOS低版本兼容 - 平滑滚动 */
    -webkit-overflow-scrolling: touch;
}

.kefu-loading {
    text-align: center;
    padding: 40px 20px;
    color: #999;
}

.kefu-message-item {
    margin-bottom: 15px;
    display: -webkit-box;
    display: -webkit-flex;
    display: -ms-flexbox;
    display: flex;
    -webkit-box-orient: vertical;
    -webkit-box-direction: normal;
    -webkit-flex-direction: column;
    -ms-flex-direction: column;
    flex-direction: column;
    max-width: 100%;
    /* iOS低版本兼容 - 确保间距 */
    word-wrap: break-word;
    -webkit-word-wrap: break-word;
}

.kefu-message-item.message-user {
    -webkit-box-align: end;
    -webkit-align-items: flex-end;
    -ms-flex-align: end;
    align-items: flex-end;
    margin-left: auto;
}

.kefu-message-item.message-staff {
    -webkit-box-align: start;
    -webkit-align-items: flex-start;
    -ms-flex-align: start;
    align-items: flex-start;
    margin-right: auto;
}

.kefu-message-content {
    padding: 10px 15px;
    border-radius: 12px;
    word-wrap: break-word;
    word-break: break-word;
    overflow-wrap: anywhere;
    white-space: pre-wrap;
    line-height: 1.6;
    display: inline-block;
    max-width: min(480px, 90vw);
    /* iOS低版本兼容 */
    -webkit-border-radius: 12px;
    -webkit-word-wrap: break-word;
    line-break: anywhere;
    -webkit-line-break: anywhere;
    user-select: text;
    -webkit-user-select: text;
}

.message-user .kefu-message-content {
    background: #e0e0e0;
    color: #333;
}

.message-staff .kefu-message-content {
    background: #667eea;
    color: #fff;
}

.kefu-message-time {
    font-size: 11px;
    color: #999;
    margin-top: 4px;
    padding: 0 5px;
}

.kefu-input-tools {
    display: -webkit-box;
    display: -webkit-flex;
    display: -ms-flexbox;
    display: flex;
    -webkit-box-align: center;
    -webkit-align-items: center;
    -ms-flex-align: center;
    align-items: center;
    margin-right: 8px; /* 低版本iOS兼容：使用margin替代gap */
}

.kefu-input-tools > * {
    margin-right: 8px; /* 低版本iOS兼容：使用margin替代gap */
}

.kefu-input-tools > *:last-child {
    margin-right: 0; /* 最后一个元素不需要右边距 */
}

.kefu-tool-btn {
    width: 36px;
    height: 36px;
    border: none;
    background: rgba(102, 126, 234, 0.1);
    border-radius: 6px;
    cursor: pointer;
    font-size: 18px;
    display: -webkit-box;
    display: -webkit-flex;
    display: -ms-flexbox;
    display: flex;
    -webkit-box-align: center;
    -webkit-align-items: center;
    -ms-flex-align: center;
    align-items: center;
    -webkit-box-pack: center;
    -webkit-justify-content: center;
    -ms-flex-pack: center;
    justify-content: center;
    /* iOS低版本兼容 */
    appearance: none;
    -webkit-appearance: none;
    -webkit-border-radius: 6px;
    -webkit-tap-highlight-color: transparent;
}

.kefu-tool-btn:active {
    opacity: 0.7;
}

.kefu-message-media {
    margin: 4px 0;
    max-width: 100%;
    display: block;
    position: relative;
    overflow: hidden;
    isolation: isolate;
}

.kefu-media-image {
    width: auto;
    max-width: 260px;
    max-height: 320px;
    border-radius: 8px;
    cursor: pointer;
    display: block;
    object-fit: contain;
    background: rgba(0, 0, 0, 0.08);
    transition: opacity 0.2s ease;
    /* iOS低版本兼容 */
    -webkit-border-radius: 8px;
}

.kefu-media-image:hover {
    opacity: 0.9;
}

.kefu-media-video {
    width: auto;
    max-width: 300px;
    max-height: 400px;
    border-radius: 8px;
    display: block;
    background: rgba(0, 0, 0, 0.1);
    /* iOS低版本兼容 */
    -webkit-border-radius: 8px;
}

.kefu-input-area {
    padding: 15px 20px;
    border-top: 1px solid #e0e0e0;
    display: -webkit-box;
    display: -webkit-flex;
    display: -ms-flexbox;
    display: flex;
    -webkit-box-align: end;
    -webkit-align-items: flex-end;
    -ms-flex-align: end;
    align-items: flex-end;
    /* 低版本iOS兼容：使用margin替代gap */
    /* 允许交互 */
    touch-action: manipulation;
    pointer-events: auto;
    /* iOS低版本兼容 */
    -webkit-box-sizing: border-box;
    box-sizing: border-box;
    /* iOS安全区域适配 */
    padding-bottom: calc(15px + env(safe-area-inset-bottom, 0px));
}

.kefu-input-area > * {
    margin-right: 10px; /* 低版本iOS兼容：使用margin替代gap */
}

.kefu-input-area > *:last-child {
    margin-right: 0; /* 最后一个元素不需要右边距 */
}

.kefu-input {
    flex: 1;
    padding: 10px 15px;
    border: 1px solid #e0e0e0;
    border-radius: 6px;
    font-size: 16px; /* 16px避免iOS自动缩放 */
    resize: none;
    max-height: 120px;
    font-family: inherit;
    /* iOS低版本兼容 */
    appearance: none;
    -webkit-appearance: none;
    -webkit-border-radius: 6px;
}

body.kefu-image-preview-lock {
    overflow: hidden;
}

.kefu-image-preview {
    position: fixed;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    z-index: 11000; /* 高于 .kefu-overlay，确保可见 */
    display: none;
    -webkit-box-align: center;
    -webkit-align-items: center;
    -ms-flex-align: center;
    align-items: center;
    -webkit-box-pack: center;
    -webkit-justify-content: center;
    -ms-flex-pack: center;
    justify-content: center;
    padding: 20px;
}

.kefu-image-preview.active {
    display: -webkit-box;
    display: -webkit-flex;
    display: -ms-flexbox;
    display: flex;
}

.kefu-image-preview-mask {
    position: absolute;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    background: rgba(0, 0, 0, 0.65);
    backdrop-filter: blur(2px);
}

.kefu-image-preview-content {
    position: relative;
    max-width: 90vw;
    max-height: 90vh;
    z-index: 1;
    display: -webkit-box;
    display: -webkit-flex;
    display: -ms-flexbox;
    display: flex;
    -webkit-box-align: center;
    -webkit-align-items: center;
    -ms-flex-align: center;
    align-items: center;
    -webkit-box-pack: center;
    -webkit-justify-content: center;
    -ms-flex-pack: center;
    justify-content: center;
    padding: 10px;
    background: rgba(0, 0, 0, 0.2);
    border-radius: 12px;
    -webkit-box-shadow: 0 20px 40px rgba(0, 0, 0, 0.3);
    box-shadow: 0 20px 40px rgba(0, 0, 0, 0.3);
}

.kefu-image-preview-content img {
    max-width: 88vw;
    max-height: 80vh;
    border-radius: 8px;
    display: block;
    background: #000;
}

.kefu-input:focus {
    outline: none;
    border-color: #667eea;
}

.kefu-send-btn {
    padding: 10px 24px;
    background: #667eea;
    color: #fff;
    border: none;
    border-radius: 6px;
    cursor: pointer;
    font-size: 14px;
    font-weight: 600;
    /* iOS低版本兼容 */
    appearance: none;
    -webkit-appearance: none;
    -webkit-border-radius: 6px;
    -webkit-tap-highlight-color: transparent;
}

.kefu-send-btn:active {
    opacity: 0.8;
}

.kefu-send-btn:disabled {
    opacity: 0.5;
    cursor: not-allowed;
}

/* =========================
   黑色主题（默认）
========================= */
body:not([data-theme]) .kefu-modal,
#theme-dark:checked ~ * .kefu-modal {
    background: #1a1a1a;
    color: #e0e0e0;
}

body:not([data-theme]) .kefu-header,
#theme-dark:checked ~ * .kefu-header {
    border-bottom-color: #404040;
}

body:not([data-theme]) .kefu-title h3,
#theme-dark:checked ~ * .kefu-title h3 {
    color: #ffffff;
}

body:not([data-theme]) .kefu-back,
#theme-dark:checked ~ * .kefu-back {
    background: rgba(255, 255, 255, 0.1);
}

body:not([data-theme]) .back-icon,
#theme-dark:checked ~ * .back-icon {
    color: #e0e0e0;
}

body:not([data-theme]) .message-user .kefu-message-content,
#theme-dark:checked ~ * .message-user .kefu-message-content {
    background: #2d2d2d;
    color: #e0e0e0;
}

body:not([data-theme]) .kefu-input-area,
#theme-dark:checked ~ * .kefu-input-area {
    border-top-color: #404040;
}

body:not([data-theme]) .kefu-input,
#theme-dark:checked ~ * .kefu-input {
    background: #2d2d2d;
    border-color: #404040;
    color: #e0e0e0;
}

body:not([data-theme]) .kefu-input:focus,
#theme-dark:checked ~ * .kefu-input:focus {
    border-color: #667eea;
}

body:not([data-theme]) .kefu-send-btn,
#theme-dark:checked ~ * .kefu-send-btn {
    background: -webkit-linear-gradient(135deg, #FFC107, #FF8F00);
    background: -moz-linear-gradient(135deg, #FFC107, #FF8F00);
    background: linear-gradient(135deg, #FFC107, #FF8F00);
    color: #ffffff;
}

/* =========================
   白色主题
========================= */
#theme-light:checked ~ * .kefu-modal {
    background: #ffffff;
    color: #333333;
}

#theme-light:checked ~ * .kefu-header {
    border-bottom-color: #e0e0e0;
}

#theme-light:checked ~ * .kefu-title h3 {
    color: #333333;
}

#theme-light:checked ~ * .kefu-back {
    background: rgba(0, 0, 0, 0.1);
}

#theme-light:checked ~ * .back-icon {
    color: #333333;
}

#theme-light:checked ~ * .message-user .kefu-message-content {
    background: #f5f5f5;
    color: #333333;
}

#theme-light:checked ~ * .kefu-input-area {
    border-top-color: #e0e0e0;
}

#theme-light:checked ~ * .kefu-input {
    background: #ffffff;
    border-color: #e0e0e0;
    color: #333333;
}

#theme-light:checked ~ * .kefu-input:focus {
    border-color: #4CAF50;
}

#theme-light:checked ~ * .kefu-send-btn {
    background: #4CAF50;
}

/* =========================
   蓝色主题
========================= */
#theme-blue:checked ~ * .kefu-modal {
    background: #e3f2fd;
    color: #1976d2;
}

#theme-blue:checked ~ * .kefu-header {
    border-bottom-color: #90caf9;
}

#theme-blue:checked ~ * .kefu-title h3 {
    color: #1976d2;
}

#theme-blue:checked ~ * .kefu-back {
    background: rgba(25, 118, 210, 0.1);
}

#theme-blue:checked ~ * .back-icon {
    color: #1976d2;
}

#theme-blue:checked ~ * .message-user .kefu-message-content {
    background: #bbdefb;
    color: #0d47a1;
}

#theme-blue:checked ~ * .kefu-input-area {
    border-top-color: #90caf9;
}

#theme-blue:checked ~ * .kefu-input {
    background: #ffffff;
    border-color: #90caf9;
    color: #1976d2;
}

#theme-blue:checked ~ * .kefu-input:focus {
    border-color: #2196F3;
}

#theme-blue:checked ~ * .kefu-send-btn {
    background: #2196F3;
}

/* iOS低版本特殊兼容 */
@supports (-webkit-touch-callout: none) {
    .kefu-modal {
        /* iOS Safari需要明确高度 */
        height: 100vh;
        height: -webkit-fill-available;
    }
    
    .kefu-header,
    .kefu-input-area {
        /* 确保iOS低版本间距正确 */
        padding-left: max(15px, env(safe-area-inset-left, 0px));
        padding-right: max(15px, env(safe-area-inset-right, 0px));
    }
}

/* 响应式设计 */
@media (max-width: 480px) {
    .kefu-modal {
        width: 100%;
        height: 100%;
    }
    
    .kefu-message-item {
        max-width: 85%;
    }
    
    .kefu-input-area {
        padding: 10px 15px;
        padding-bottom: calc(10px + env(safe-area-inset-bottom, 0px));
    }
}

