:root {
    --primary: #1677ff;
    --safe-bottom: env(safe-area-inset-bottom, 0px);
}

* { box-sizing: border-box; margin: 0; padding: 0; -webkit-tap-highlight-color: transparent; }

html, body {
    height: 100%;
    height: 100dvh;
}

body {
    font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", "PingFang SC", sans-serif;
    background: #e8ecf0;
    overflow: hidden;
}

.chat-app {
    display: flex;
    flex-direction: column;
    height: 100%;
    width: 100%;
    max-width: 800px;
    margin: 0 auto;
    background: #f5f7fa;
}

.chat-header {
    background: linear-gradient(135deg, #1677ff, #4096ff);
    color: #fff;
    padding: 14px 16px;
    padding-top: calc(14px + env(safe-area-inset-top, 0px));
    display: flex;
    align-items: center;
    justify-content: space-between;
    flex-shrink: 0;
    box-shadow: 0 2px 8px rgba(22,119,255,.25);
}
.chat-header h1 { font-size: 17px; font-weight: 600; }
.status { font-size: 12px; opacity: .95; }
.status.online::before {
    content: '';
    display: inline-block;
    width: 7px;
    height: 7px;
    background: #52c41a;
    border-radius: 50%;
    margin-right: 6px;
    vertical-align: middle;
    box-shadow: 0 0 0 2px rgba(82,196,26,.3);
}

.chat-messages {
    flex: 1;
    min-height: 0;
    overflow-y: auto;
    overflow-x: hidden;
    padding: 16px;
    display: flex;
    flex-direction: column;
    gap: 14px;
    -webkit-overflow-scrolling: touch;
}

.msg { display: flex; flex-direction: column; max-width: 85%; }
.msg.visitor { align-self: flex-end; align-items: flex-end; }
.msg.agent, .msg.system { align-self: flex-start; align-items: flex-start; }

.msg-bubble {
    padding: 11px 15px;
    border-radius: 16px;
    font-size: 15px;
    line-height: 1.55;
    word-break: break-word;
}
.msg.visitor .msg-bubble {
    background: var(--primary);
    color: #fff;
    border-bottom-right-radius: 4px;
}
.msg.agent .msg-bubble {
    background: #fff;
    color: #333;
    border-bottom-left-radius: 4px;
    box-shadow: 0 1px 6px rgba(0,0,0,.06);
}
.msg.system .msg-bubble {
    background: #fff7e6;
    color: #ad6800;
    font-size: 13px;
    border-radius: 12px;
}
.msg-sender { font-size: 12px; color: #666; margin-bottom: 4px; }
.msg-time { font-size: 11px; color: #aaa; margin-top: 5px; }
.chat-messages.has-bg {
    background-size: cover;
    background-position: center;
    background-repeat: no-repeat;
}
.chat-messages.has-bg .msg-bubble {
    backdrop-filter: blur(2px);
}
.chat-session-divider {
    align-self: center;
    margin: 10px 0 6px;
    padding: 4px 12px;
    font-size: 11px;
    color: #8c8c8c;
    background: rgba(0, 0, 0, 0.06);
    border-radius: 10px;
    text-align: center;
}
.msg-pending { opacity: 0.65; }
.msg-pending .msg-bubble { position: relative; }

.chat-input textarea:disabled {
    background: #f5f5f5;
    color: #999;
}
.chat-input.is-blocked {
    opacity: .92;
}
.chat-input .btn-upload.is-disabled {
    opacity: .45;
    pointer-events: none;
}

/* 微信名片消息 */
.msg-bubble--card {
    background: transparent !important;
    padding: 0 !important;
    box-shadow: none !important;
}
.wechat-card {
    display: flex;
    align-items: center;
    gap: 12px;
    padding: 12px 14px;
    background: #fff;
    border: 1px solid #e8e8e8;
    border-radius: 8px;
    text-decoration: none;
    color: #333;
    min-width: 210px;
    max-width: 280px;
    transition: box-shadow .15s;
}
.wechat-card:hover {
    box-shadow: 0 2px 8px rgba(0,0,0,.1);
}
.wechat-card-icon {
    width: 48px;
    height: 48px;
    background: linear-gradient(145deg, #3dd068 0%, #07c160 100%);
    color: #fff;
    border-radius: 10px;
    display: flex;
    align-items: center;
    justify-content: center;
    flex-shrink: 0;
    box-shadow: 0 2px 8px rgba(7, 193, 96, .28);
}
.wechat-card-icon svg {
    width: 28px;
    height: 28px;
    display: block;
}
.wechat-card-title {
    font-size: 15px;
    font-weight: 500;
    color: #333;
    line-height: 1.3;
}
.wechat-card-subtitle {
    font-size: 12px;
    color: #999;
    margin-top: 3px;
}
.msg img {
    max-width: min(240px, 75vw);
    max-height: 240px;
    border-radius: 10px;
    cursor: pointer;
    display: block;
}

.chat-input {
    display: flex;
    align-items: flex-end;
    gap: 8px;
    padding: 10px 12px;
    padding-bottom: calc(10px + var(--safe-bottom));
    background: #fff;
    border-top: 1px solid #eee;
    flex-shrink: 0;
}
.chat-input textarea {
    flex: 1;
    border: 1px solid #e8e8e8;
    border-radius: 22px;
    padding: 11px 16px;
    font-size: 16px;
    resize: none;
    max-height: 120px;
    outline: none;
    font-family: inherit;
    line-height: 1.4;
    min-height: 42px;
}
.chat-input textarea:focus { border-color: var(--primary); }

.btn-upload {
    display: flex;
    align-items: center;
    justify-content: center;
    width: 44px;
    height: 44px;
    color: #666;
    cursor: pointer;
    border-radius: 50%;
    flex-shrink: 0;
}
.btn-upload:active { background: #f0f0f0; color: var(--primary); }

#sendBtn {
    background: var(--primary);
    color: #fff;
    border: none;
    border-radius: 22px;
    padding: 0 20px;
    font-size: 15px;
    cursor: pointer;
    flex-shrink: 0;
    min-height: 44px;
    font-weight: 500;
}
#sendBtn:disabled { opacity: .5; cursor: not-allowed; }
#sendBtn:active { background: #4096ff; }

.image-preview {
    position: fixed;
    inset: 0;
    z-index: 100;
    display: flex;
    align-items: center;
    justify-content: center;
}
.image-preview[hidden] { display: none; }
.preview-backdrop { position: absolute; inset: 0; background: rgba(0,0,0,.75); }
.image-preview img {
    position: relative;
    max-width: 92vw;
    max-height: 88vh;
    border-radius: 10px;
    z-index: 1;
}

@media (min-width: 769px) {
    body { background: #dde3ea; }
    .chat-app {
        margin-top: 24px;
        margin-bottom: 24px;
        height: calc(100% - 48px);
        border-radius: 16px;
        overflow: hidden;
        box-shadow: 0 8px 32px rgba(0,0,0,.12);
    }
}
