/* ============================================
   코웨이 채팅 위젯 — 모던 디자인
   ============================================ */

:root {
    --primary: #667eea;
    --primary-dark: #764ba2;
    --primary-light: #eef2ff;
    --primary-mid: #c7d2fe;
    --text-main: #1e293b;
    --text-sub: #64748b;
    --bg-body: #f1f5f9;
    --bg-card: #ffffff;
    --border: #e2e8f0;
    --shadow: 0 8px 32px rgba(102,126,234,0.18);
    --radius: 20px;
    --radius-sm: 12px;
}

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

body {
    font-family: 'Segoe UI', 'Malgun Gothic', Tahoma, Geneva, Verdana, sans-serif;
    background: var(--bg-body);
    display: flex;
    justify-content: center;
    align-items: center;
    min-height: 100vh;
    padding: 10px;
}

/* ── 채팅 컨테이너 ── */
.chat-container {
    width: 100%;
    max-width: 440px;
    height: 640px;
    background: var(--bg-card);
    border-radius: var(--radius);
    box-shadow: var(--shadow), 0 2px 8px rgba(0,0,0,0.08);
    display: flex;
    flex-direction: column;
    overflow: hidden;
    border: 1px solid rgba(102,126,234,0.12);
}

/* ── 헤더 ── */
.chat-header {
    background: linear-gradient(135deg, var(--primary) 0%, var(--primary-dark) 100%);
    color: white;
    padding: 14px 16px;
    display: flex;
    align-items: center;
    justify-content: space-between;
    flex-shrink: 0;
    position: relative;
    overflow: hidden;
}

.chat-header::before {
    content: '';
    position: absolute;
    top: -30px; right: -20px;
    width: 100px; height: 100px;
    border-radius: 50%;
    background: rgba(255,255,255,0.08);
}

.header-left {
    display: flex;
    align-items: center;
    gap: 12px;
    z-index: 1;
}

.header-avatar {
    position: relative;
    width: 42px;
    height: 42px;
    flex-shrink: 0;
    border-radius: 50%;
    background: rgba(255,255,255,0.2);
    display: flex;
    align-items: center;
    justify-content: center;
}

/* 이미지가 로드되면 이모티콘 위를 덮음 (z-index 2) */
.header-avatar img {
    position: absolute;
    inset: 0;
    width: 100%;
    height: 100%;
    border-radius: 50%;
    object-fit: cover;
    border: 2px solid rgba(255,255,255,0.4);
    z-index: 2;
}

/* 이미지 실패 시 fallback (z-index 1) */
.avatar-emoji {
    font-size: 22px;
    line-height: 1;
    z-index: 1;
}

.online-dot {
    position: absolute;
    bottom: 1px; right: 1px;
    width: 11px; height: 11px;
    border-radius: 50%;
    background: #4ade80;
    border: 2px solid rgba(255,255,255,0.9);
    box-shadow: 0 0 6px #4ade80;
    z-index: 3;
}

.header-info h1 {
    font-size: 15px;
    font-weight: 700;
    letter-spacing: -0.3px;
    color: white;
    line-height: 1.2;
}

.header-info p {
    font-size: 13px;
    color: rgba(255,255,255,0.8);
    margin-top: 1px;
    letter-spacing: 0.04em;
}

.header-actions {
    display: flex;
    align-items: center;
    gap: 6px;
    z-index: 1;
}

.header-btn {
    width: 30px; height: 30px;
    border-radius: 50%;
    border: 1.5px solid rgba(255,255,255,0.35);
    background: rgba(255,255,255,0.15);
    color: white;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    transition: background 0.2s;
    padding: 0;
}

.header-btn:hover {
    background: rgba(255,255,255,0.3);
}

/* ── 배너 메시지 카드 (메시지 영역 맨 위, 여백 없이 꽉 차게) ── */
.banner-message-card {
    position: relative;
    /* 메시지 영역 padding(16px 14px)을 음수 마진으로 상쇄하여 양쪽 끝까지 꽉 참 */
    margin: -16px -14px 12px -14px;
    border-radius: 0;
    overflow: hidden;
    flex-shrink: 0;
}

.banner-msg-close { display: none; }

.banner-msg-content {
    font-size: 13px;
    line-height: 1.5;
    color: #1e293b;
}

/* 이미지 배너: 꽉 차게, 잘림 없이 */
.banner-msg-content img {
    display: block;
    width: 100%;
    height: auto;
    border-radius: 0;
}

.banner-msg-content a { color: #1d4ed8; text-decoration: underline; }

/* 텍스트 배너 */
.banner-msg-content:not(:has(img)) {
    background: linear-gradient(135deg, #fef3c7 0%, #fde68a 100%);
    padding: 12px 32px 12px 14px;
    color: #92400e;
}

/* ── 메시지 영역 ── */
.chat-messages {
    flex: 1;
    overflow-y: auto;
    padding: 16px 14px;
    display: flex;
    flex-direction: column;
    gap: 4px;
    background: #f8fafc;
}

.chat-messages::-webkit-scrollbar { width: 4px; }
.chat-messages::-webkit-scrollbar-track { background: transparent; }
.chat-messages::-webkit-scrollbar-thumb { background: #cbd5e1; border-radius: 4px; }
.chat-messages::-webkit-scrollbar-thumb:hover { background: #94a3b8; }

/* ── 메시지 공통 ── */
.message {
    display: flex;
    flex-direction: column;
    gap: 3px;
    animation: msgIn 0.25s ease-out;
    margin-bottom: 6px;
}

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

.message-time {
    font-size: 10px;
    color: #94a3b8;
    padding: 0 4px;
}

/* ── 봇 메시지 ── */
.message.bot {
    flex-direction: row;
    align-items: flex-end;
    gap: 8px;
}

.message.bot .message-body {
    display: flex;
    flex-direction: column;
    gap: 4px;
    flex: 0 1 auto;
    min-width: 0;
    max-width: calc(100% - 44px);
}

.message.bot .message-time {
    text-align: left;
}

.message.bot p {
    width: fit-content;
    max-width: 100%;
    padding: 10px 14px;
    background: white;
    color: var(--text-main);
    border-radius: 4px 16px 16px 16px;
    font-size: 13.5px;
    line-height: 1.65;
    word-break: keep-all;
    overflow-wrap: break-word;
    white-space: pre-wrap;
    box-shadow: 0 1px 4px rgba(0,0,0,0.06);
    border: 1px solid var(--border);
}

/* ── 사용자 메시지 ── */
.message.user {
    align-items: flex-end;
}

.message.user .message-time {
    text-align: right;
}

.message.user p {
    width: fit-content;
    max-width: 78%;
    align-self: flex-end;
    padding: 10px 14px;
    background: linear-gradient(135deg, var(--primary), var(--primary-dark));
    color: white;
    border-radius: 16px 4px 16px 16px;
    font-size: 13.5px;
    line-height: 1.65;
    word-break: keep-all;
    overflow-wrap: break-word;
    white-space: pre-wrap;
    align-self: flex-end;
    box-shadow: 0 2px 8px rgba(102,126,234,0.3);
}

/* ── 봇 아바타 ── */
.bot-avatar {
    width: 34px;
    height: 34px;
    border-radius: 50%;
    background: linear-gradient(135deg, var(--primary), var(--primary-dark));
    flex-shrink: 0;
    display: flex;
    align-items: center;
    justify-content: center;
    position: relative;
    align-self: flex-end;
    box-shadow: 0 2px 8px rgba(102,126,234,0.3);
    overflow: hidden;
}

/* 이미지가 로드되면 이모티콘을 덮음 (z-index 2) */
.bot-avatar img {
    position: absolute;
    inset: 0;
    width: 100%;
    height: 100%;
    object-fit: cover;
    border-radius: 50%;
    z-index: 2;
}

/* 이미지 로드 실패 시 fallback으로만 보임 (z-index 1) */
.bot-avatar-icon {
    font-size: 18px;
    line-height: 1;
    z-index: 1;
    flex-shrink: 0;
}

/* ── 첨부파일 미리보기 ── */
.preview-area {
    padding: 8px 14px;
    background: #f0f4ff;
    border-top: 1px solid var(--primary-mid);
    flex-shrink: 0;
}

.preview-chip {
    display: inline-flex;
    align-items: center;
    gap: 7px;
    background: white;
    border: 1.5px solid var(--primary-mid);
    border-radius: 22px;
    padding: 5px 10px 5px 5px;
    max-width: 300px;
    box-shadow: 0 1px 4px rgba(102,126,234,0.1);
}

.preview-thumb {
    width: 32px; height: 32px;
    border-radius: 12px;
    object-fit: cover;
    flex-shrink: 0;
    border: 1px solid var(--primary-mid);
}

.preview-icon {
    font-size: 20px;
    flex-shrink: 0;
    line-height: 1;
    padding: 0 3px;
}

.preview-name {
    flex: 1;
    font-size: 12px;
    font-weight: 500;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
    color: #4338ca;
    max-width: 160px;
}

.preview-remove {
    width: 18px; height: 18px;
    border-radius: 50%;
    background: #94a3b8;
    color: white;
    border: none;
    font-size: 10px;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    padding: 0;
    flex-shrink: 0;
    font-weight: bold;
    transition: background 0.2s;
    line-height: 1;
}

.preview-remove:hover { background: #ef4444; }

/* ── 입력 영역 ── */
.chat-input-area {
    display: flex;
    align-items: center;
    padding: 10px 12px;
    gap: 8px;
    border-top: 1px solid var(--border);
    background: white;
    flex-shrink: 0;
}

.attach-btn {
    background: transparent;
    color: #94a3b8;
    border: 1.5px solid var(--border);
    border-radius: 50%;
    width: 38px; height: 38px;
    padding: 0;
    display: flex;
    align-items: center;
    justify-content: center;
    cursor: pointer;
    transition: color 0.2s, border-color 0.2s, background 0.2s;
    flex-shrink: 0;
}

.attach-btn:hover {
    color: var(--primary);
    border-color: var(--primary);
    background: var(--primary-light);
}

#question {
    flex: 1;
    border: 1.5px solid var(--border);
    border-radius: 22px;
    padding: 9px 16px;
    font-size: 13.5px;
    outline: none;
    background: #f8fafc;
    color: var(--text-main);
    transition: border-color 0.2s, background 0.2s, box-shadow 0.2s;
    font-family: inherit;
}

#question:focus {
    border-color: var(--primary);
    background: white;
    box-shadow: 0 0 0 3px rgba(102,126,234,0.12);
}

#question::placeholder {
    color: #94a3b8;
}

.send-btn {
    background: linear-gradient(135deg, var(--primary), var(--primary-dark));
    color: white;
    border: none;
    border-radius: 50%;
    width: 38px; height: 38px;
    padding: 0;
    display: flex;
    align-items: center;
    justify-content: center;
    cursor: pointer;
    transition: transform 0.15s, box-shadow 0.2s;
    flex-shrink: 0;
    box-shadow: 0 2px 8px rgba(102,126,234,0.35);
}

.send-btn:hover {
    transform: scale(1.08);
    box-shadow: 0 4px 14px rgba(102,126,234,0.45);
}

.send-btn:active {
    transform: scale(0.95);
}

/* ── 로딩/에러 ── */
.loading { display: none; }
.loading.hidden { display: none !important; }

.error {
    background: #fef2f2;
    color: #dc2626;
    padding: 8px 14px;
    border-top: 1px solid #fecaca;
    font-size: 12px;
    flex-shrink: 0;
}

.error.hidden { display: none; }

/* ── 타이핑 인디케이터 ── */
.typing-indicator {
    display: flex;
    gap: 5px;
    align-items: center;
    padding: 10px 14px;
    background: white;
    border-radius: 4px 16px 16px 16px;
    width: fit-content;
    border: 1px solid var(--border);
    box-shadow: 0 1px 4px rgba(0,0,0,0.06);
}

.typing-dot {
    width: 7px; height: 7px;
    background: var(--primary);
    border-radius: 50%;
    animation: typingAnim 1.4s infinite;
    opacity: 0.6;
}

.typing-dot:nth-child(2) { animation-delay: 0.2s; }
.typing-dot:nth-child(3) { animation-delay: 0.4s; }

@keyframes typingAnim {
    0%, 60%, 100% { opacity: 0.3; transform: translateY(0); }
    30%            { opacity: 1;   transform: translateY(-5px); }
}

/* ── 스피너 ── */
.spinner {
    width: 18px; height: 18px;
    border: 2.5px solid #e2e8f0;
    border-top: 2.5px solid var(--primary);
    border-radius: 50%;
    animation: spin 0.9s linear infinite;
}

@keyframes spin { 0%{transform:rotate(0deg)} 100%{transform:rotate(360deg)} }

/* ── 첨부 파일 (사용자 메시지 내부) ── */
.attached-file {
    display: flex;
    flex-direction: column;
    gap: 5px;
    align-self: flex-end;
    max-width: 240px;
    margin-bottom: 2px;
}

.attached-image-wrap {
    position: relative;
    display: inline-block;
    max-width: 220px;
    border-radius: 12px;
    overflow: hidden;
    line-height: 0;
    align-self: flex-end;
}

.attached-image {
    max-width: 220px;
    max-height: 200px;
    border-radius: 12px;
    object-fit: cover;
    cursor: pointer;
    display: block;
    border: 2px solid rgba(255,255,255,0.5);
    width: 100%;
    transition: opacity 0.2s;
}

.attached-image-wrap:hover .attached-image { opacity: 0.88; }

.img-dl-overlay {
    position: absolute;
    bottom: 8px; right: 8px;
    background: rgba(0,0,0,0.55);
    color: white;
    border-radius: 10px;
    padding: 4px 10px;
    font-size: 11px;
    font-weight: 500;
    text-decoration: none;
    display: flex;
    align-items: center;
    gap: 3px;
    opacity: 0;
    transition: opacity 0.2s;
    cursor: pointer;
    line-height: 1.4;
}

.attached-image-wrap:hover .img-dl-overlay { opacity: 1; }
.img-dl-overlay:hover { background: rgba(0,0,0,0.75); }

.file-card {
    display: flex;
    align-items: center;
    gap: 10px;
    background: linear-gradient(135deg, var(--primary), var(--primary-dark));
    border: none;
    border-radius: 12px;
    padding: 10px 12px;
    max-width: 230px;
    align-self: flex-end;
    box-shadow: 0 2px 8px rgba(102,126,234,0.3);
}

.file-card-icon { font-size: 22px; flex-shrink: 0; }

.file-card-info {
    flex: 1;
    min-width: 0;
    display: flex;
    flex-direction: column;
    gap: 2px;
}

.file-card-name {
    font-size: 12.5px;
    font-weight: 600;
    color: white;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

.file-card-size { font-size: 11px; color: rgba(255,255,255,0.7); }

/* 상담사(bot) 메시지 안의 파일카드는 흰색 배경 */
.message.bot .file-card {
    background: #fff;
    border: 1px solid #e2e8f0;
    box-shadow: 0 1px 4px rgba(0,0,0,0.07);
    align-self: flex-start;
}
.message.bot .file-card .file-card-name { color: #1a202c; }
.message.bot .file-card .file-card-size { color: #6b7280; }
.message.bot .file-card .download-btn {
    color: #4f46e5;
    background: #f0f4ff;
    border-color: #c7d2fe;
}

.download-btn {
    display: inline-flex;
    align-items: center;
    gap: 3px;
    font-size: 11.5px;
    color: white;
    background: rgba(255,255,255,0.2);
    border: 1px solid rgba(255,255,255,0.35);
    border-radius: 6px;
    padding: 3px 8px;
    cursor: pointer;
    text-decoration: none;
    transition: background 0.2s;
    margin-top: 2px;
    align-self: flex-start;
}

.download-btn:hover { background: rgba(255,255,255,0.3); }

/* ── 라이트박스 ── */
.lb-download-btn {
    position: fixed;
    top: 18px; left: 24px;
    color: white;
    font-size: 13px;
    background: rgba(255,255,255,0.15);
    padding: 6px 14px;
    border-radius: 20px;
    text-decoration: none;
    border: 1px solid rgba(255,255,255,0.3);
    transition: background 0.2s;
    z-index: 10000;
}

.lb-download-btn:hover { background: rgba(255,255,255,0.25); }

.img-lightbox {
    display: none;
    position: fixed;
    inset: 0;
    background: rgba(0,0,0,0.88);
    z-index: 9999;
    justify-content: center;
    align-items: center;
    cursor: zoom-out;
}

.img-lightbox.open { display: flex; }

.img-lightbox img {
    max-width: 90vw;
    max-height: 90vh;
    border-radius: 8px;
    box-shadow: 0 8px 40px rgba(0,0,0,0.5);
    cursor: default;
    animation: lbIn 0.2s ease;
}

@keyframes lbIn {
    from { opacity: 0; transform: scale(0.92); }
    to   { opacity: 1; transform: scale(1); }
}

.img-lightbox-close {
    position: fixed;
    top: 18px; right: 24px;
    color: white;
    font-size: 34px;
    font-weight: bold;
    line-height: 1;
    cursor: pointer;
    user-select: none;
    opacity: 0.8;
    transition: opacity 0.2s;
}

.img-lightbox-close:hover { opacity: 1; }

/* ── 제품 이미지 (봇 응답) ── */
.chat-images {
    display: flex;
    flex-wrap: wrap;
    gap: 8px;
    margin-top: 6px;
}

.chat-image-wrap {
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 3px;
}

.chat-image {
    width: 220px;
    max-width: 100%;
    border-radius: 10px;
    border: 1px solid var(--border);
    cursor: pointer;
    transition: transform 0.2s;
}

.chat-image:hover { transform: scale(1.04); }

.chat-image-label {
    font-size: 10.5px;
    color: #94a3b8;
}

/* ── 독립 창 전체화면 모드 ── */
html.fullscreen-mode body {
    padding: 0;
}
html.fullscreen-mode .chat-container {
    max-width: 100%;
    width: 100%;
    height: 100vh;
    border-radius: 0;
    box-shadow: none;
    border: none;
}

/* ── iframe 임베드 모드 ── */
html.embed-mode body {
    background: transparent;
    padding: 0;
    align-items: stretch;
    justify-content: stretch;
    min-height: 100vh;
}

html.embed-mode .chat-container {
    max-width: 100%;
    width: 100%;
    height: 100vh;
    border-radius: 0;
    box-shadow: none;
    border: none;
}

/* ── 모바일 ── */
@media (max-width: 460px) {
    body { padding: 0; }

    .chat-container {
        max-width: 100%;
        height: 100vh;
        border-radius: 0;
        box-shadow: none;
        border: none;
    }
}

/* ── 날짜/시스템 구분선 ── */
.chat-date-divider {
    display: flex;
    align-items: center;
    gap: 10px;
    margin: 10px 0 6px;
    color: #94a3b8;
    font-size: 11px;
}

.chat-date-divider::before,
.chat-date-divider::after {
    content: '';
    flex: 1;
    height: 1px;
    background: #e2e8f0;
}

/* ── 업무시간 외 부재중 이미지 ── */
/* ── 부재중 모드: 메시지 영역 = 배너 높이만 ── */
.chat-messages.offhours-mode {
    flex: 0 0 auto !important;
    overflow: hidden !important;
    padding: 0 !important;
    gap: 0 !important;
    background: white;
}
.chat-messages.offhours-mode .banner-message-card {
    margin: 0;
    border-radius: 0;
}

/* ── 배너 아래 구분 여백 (chat-container의 flex 자식) ── */
.offhours-gap {
    height: 10px;
    flex-shrink: 0;
    background: white;
}

/* ── 부재중 이미지 영역: padding 없이 img box가 남은 높이 전부 사용 ── */
.offhours-image-wrap {
    flex: 1;
    min-height: 0;
    display: flex;
    flex-direction: column;
    overflow: hidden;
    background: white;
}

/* 이미지: 남은 공간 전체 사용, 비율 유지, 잘림 없음 */
.offhours-image-wrap img {
    flex: 1;
    min-height: 0;
    width: 100%;
    object-fit: contain;
    object-position: top center;
    display: block;
}
