/* ============================================================
   chat-widget.css — แชทฝั่งลูกค้า ดีไซน์ใหม่
   วางที่ /assets/css/chat-widget.css  (โหลดหลัง CSS เดิมใน footer จึงทับของเก่าได้)
   ============================================================ */
#chat-widget-container {
    --ipw-p: var(--fp, #6f42c1);
    --ipw-pd: var(--fp-dark, #3d1a72);
    --ipw-line: #ece6f7;
    --ipw-text: #2b2540;
    --ipw-muted: #8b84a0;
    /* เดิม bottom:100px ทำให้ปุ่ม "สอบถาม AI" (ปุ่มรูปแคปซูลนี้) อยู่ห่างจากปุ่มโทร (วงกลมม่วง มุมขวาล่าง
       ซึ่งอยู่ที่ bottom:28px สูง 56px = ขอบบนอยู่ที่ 84px) แค่ 16px เท่านั้น
       เงาเรืองแสง (box-shadow) ของทั้งสองปุ่มเลยไปoverlapกันจนดูเหมือนปุ่มซ้อนกัน
       ขยับขึ้นมาเป็น 150px ให้มีช่องว่างจริงๆ ~66px เพื่อให้เงาไม่ไปชนกัน */
    position: fixed; bottom: 150px; right: 20px; z-index: 9980;
    font-family: 'Kanit', 'Prompt', sans-serif;
}
#chat-widget-container [hidden] { display: none !important; }

/* ---------- ปุ่มเปิดแชท ---------- */
#chat-widget-container .chat-trigger {
    display: inline-flex; align-items: center; gap: 10px;
    padding: 12px 20px 12px 16px; border-radius: 999px;
    background: linear-gradient(135deg, var(--ipw-p), var(--ipw-pd));
    color: #fff; font-size: .9rem; font-weight: 500; cursor: pointer; user-select: none;
    box-shadow: 0 8px 28px rgba(111, 66, 193, .45);
    transition: transform .25s, box-shadow .25s;
}
#chat-widget-container .chat-trigger i { font-size: 1.05rem; }
#chat-widget-container .chat-trigger:hover { transform: translateY(-3px); box-shadow: 0 12px 34px rgba(111, 66, 193, .55); }

/* ---------- หน้าต่างแชท ---------- */
#chat-widget-container .chat-window {
    display: none; flex-direction: column;
    /* เดิม bottom:62px ตอนกล่องแม่ (#chat-widget-container) อยู่ที่ bottom:100px (รวม = 162px)
       ตอนนี้กล่องแม่ขยับขึ้นเป็น 150px แล้ว เลยลดค่านี้ลงเท่ากับส่วนต่าง (100->150 = +50)
       ให้หน้าต่างแชทจริงยังคงเปิดที่ตำแหน่งเดิมเป๊ะ (150+12=162 เท่าเดิม) มีแค่ปุ่มแคปซูลที่ขยับขึ้น */
    position: absolute; right: 0; bottom: 12px;
    width: 372px; max-width: calc(100vw - 24px);
    height: min(600px, calc(100vh - 180px));
    height: min(600px, calc(100dvh - 180px));
    background: #fff; border-radius: 22px; overflow: hidden;
    border: 1px solid var(--ipw-line);
    box-shadow: 0 24px 70px rgba(40, 18, 90, .28), 0 4px 14px rgba(40, 18, 90, .08);
}
#chat-widget-container .chat-window.open { display: flex; animation: ipwIn .28s cubic-bezier(.2, .8, .2, 1); }
@keyframes ipwIn { from { opacity: 0; transform: translateY(16px) scale(.97); } to { opacity: 1; transform: none; } }

/* ---------- ส่วนหัว ---------- */
#chat-widget-container .chat-header {
    position: relative; overflow: hidden; flex-shrink: 0;
    display: flex; align-items: center; justify-content: space-between; gap: 10px;
    padding: 14px 16px; color: #fff;
    background: linear-gradient(135deg, var(--ipw-p) 0%, var(--ipw-pd) 100%);
}
#chat-widget-container .chat-header::after {
    content: ''; position: absolute; top: -50px; right: -30px; width: 140px; height: 140px;
    border-radius: 50%; background: radial-gradient(circle, rgba(255, 255, 255, .18), transparent 70%);
    pointer-events: none;
}
.ipw-head-left { display: flex; align-items: center; gap: 12px; min-width: 0; position: relative; z-index: 1; }
.ipw-avatar {
    position: relative; width: 42px; height: 42px; border-radius: 50%; flex-shrink: 0;
    background: rgba(255, 255, 255, .2); display: flex; align-items: center; justify-content: center; font-size: 1.1rem;
}
.ipw-avatar .ic-staff { display: none; }
.ipw-avatar.staff .ic-staff { display: inline; }
.ipw-avatar.staff .ic-bot { display: none; }
.ipw-presence {
    position: absolute; right: -1px; bottom: -1px; width: 12px; height: 12px; border-radius: 50%;
    background: #34d399; border: 2px solid var(--ipw-pd);
    animation: ipwPulse 1.8s infinite;
}
@keyframes ipwPulse { 0% { box-shadow: 0 0 0 0 rgba(52, 211, 153, .6); } 70% { box-shadow: 0 0 0 7px rgba(52, 211, 153, 0); } 100% { box-shadow: 0 0 0 0 rgba(52, 211, 153, 0); } }
#chat-widget-container .chat-header h3 { margin: 0; font-size: 1rem; font-weight: 600; color: #fff; line-height: 1.2; }
#chat-widget-container .chat-header h3::before { content: none; }
.ipw-sub { font-size: .74rem; opacity: .85; margin-top: 2px; }
#chat-widget-container #chat-close-btn {
    position: relative; z-index: 1; flex-shrink: 0;
    width: 32px; height: 32px; border-radius: 50%; border: none; cursor: pointer;
    background: rgba(255, 255, 255, .18); color: #fff; font-size: 1.3rem; line-height: 1;
    display: flex; align-items: center; justify-content: center; transition: background .2s;
}
#chat-widget-container #chat-close-btn:hover { background: rgba(255, 255, 255, .32); }

/* ---------- ปุ่มโทร ---------- */
.ipw-call { flex-shrink: 0; display: flex; gap: 8px; padding: 10px 12px; background: #fff; border-bottom: 1px solid var(--ipw-line); }
.ipw-call-btn {
    flex: 1; min-width: 0; display: flex; align-items: center; gap: 9px;
    padding: 7px 10px; border-radius: 14px; text-decoration: none;
    background: #f3fbf6; border: 1px solid #cfeedd; color: #14532d;
    transition: transform .2s, box-shadow .2s, background .2s;
}
.ipw-call-btn i {
    width: 30px; height: 30px; flex-shrink: 0; border-radius: 50%;
    background: #22c55e; color: #fff; font-size: .8rem;
    display: flex; align-items: center; justify-content: center;
}
.ipw-call-btn span { display: flex; flex-direction: column; line-height: 1.25; min-width: 0; }
.ipw-call-btn b { font-size: .72rem; font-weight: 600; }
.ipw-call-btn small { font-size: .8rem; white-space: nowrap; }
.ipw-call-btn:hover { background: #e6f8ed; transform: translateY(-1px); box-shadow: 0 4px 12px rgba(34, 197, 94, .25); color: #14532d; }

/* ---------- พื้นที่ข้อความ ---------- */
#chat-widget-container .chat-body {
    flex: 1 1 auto; min-height: 120px; max-height: none; overflow-y: auto;
    padding: 16px 14px; display: flex; flex-direction: column; gap: 8px;
    background: linear-gradient(180deg, #f8f6fd, #f3f0fa);
}
#chat-widget-container .chat-body::-webkit-scrollbar { width: 4px; }
#chat-widget-container .chat-body::-webkit-scrollbar-thumb { background: rgba(111, 66, 193, .25); border-radius: 10px; }

#chat-widget-container .chat-message {
    position: relative; max-width: 84%; padding: 9px 13px 6px;
    font-size: .88rem; line-height: 1.55; word-wrap: break-word; overflow-wrap: anywhere;
    animation: ipwMsg .22s ease;
}
@keyframes ipwMsg { from { opacity: 0; transform: translateY(6px); } to { opacity: 1; transform: none; } }
#chat-widget-container .chat-message.admin {
    align-self: flex-start; background: #fff; color: var(--ipw-text);
    border: 1px solid var(--ipw-line); border-radius: 16px 16px 16px 5px;
    box-shadow: 0 1px 4px rgba(40, 18, 90, .06);
}
#chat-widget-container .chat-message.admin.bot { background: #fbf9ff; }
#chat-widget-container .chat-message.user {
    align-self: flex-end; color: #fff; border-radius: 16px 16px 5px 16px;
    background: linear-gradient(135deg, var(--ipw-p), var(--ipw-pd));
    box-shadow: 0 3px 10px rgba(111, 66, 193, .28);
}
#chat-widget-container .chat-message[data-who]::before {
    content: attr(data-who); display: block; margin-bottom: 2px;
    font-size: .66rem; font-weight: 600; letter-spacing: .02em; color: var(--ipw-p);
}
#chat-widget-container .chat-message a { color: var(--ipw-p); word-break: break-all; }
#chat-widget-container .chat-message.user a { color: #fff; text-decoration: underline; }
.ipw-time { font-size: .64rem; opacity: .6; text-align: right; margin-top: 3px; }

/* รูป / ไฟล์ / การ์ดสินค้า ในข้อความ */
.ipw-img { display: block; max-width: 100%; max-height: 220px; border-radius: 12px; margin-top: 2px; background: #eee; }
#chat-widget-container .ipw-file {
    display: flex; align-items: center; gap: 8px; padding: 8px 10px; border-radius: 10px;
    background: rgba(111, 66, 193, .08); color: inherit; text-decoration: none; word-break: break-all;
}
#chat-widget-container .chat-message.user .ipw-file { background: rgba(255, 255, 255, .16); color: #fff; text-decoration: none; }
.ipw-product {
    display: flex; gap: 10px; align-items: center; min-width: 200px; padding: 8px;
    background: #f7f4fd; border: 1px solid var(--ipw-line); border-radius: 12px; color: var(--ipw-text);
}
.ipw-product img { width: 48px; height: 48px; flex-shrink: 0; object-fit: contain; background: #fff; border-radius: 8px; border: 1px solid var(--ipw-line); }
.ipw-product b { display: block; font-size: .82rem; line-height: 1.3; }
.ipw-product small { display: block; margin-top: 2px; font-size: .72rem; color: var(--ipw-p); }
#chat-widget-container .chat-message.user .ipw-product { background: rgba(255, 255, 255, .14); border-color: rgba(255, 255, 255, .25); color: #fff; }
#chat-widget-container .chat-message.user .ipw-product small { color: #e9ddff; }

/* ---------- แถบสถานะ (กำลังพิมพ์ / อ่านแล้ว) ---------- */
.ipw-status {
    flex-shrink: 0; display: flex; align-items: center; gap: 7px; min-height: 30px;
    padding: 6px 14px; font-size: .74rem; color: var(--ipw-muted);
    background: #fff; border-top: 1px solid var(--ipw-line);
}
.ipw-status.typing { color: var(--ipw-p); font-weight: 500; }
.ipw-status.read { color: #16a34a; }
.ipw-dots { display: inline-flex; gap: 3px; }
.ipw-dots i { width: 5px; height: 5px; border-radius: 50%; background: currentColor; animation: ipwDot 1.2s infinite ease-in-out; }
.ipw-dots i:nth-child(2) { animation-delay: .15s; }
.ipw-dots i:nth-child(3) { animation-delay: .3s; }
@keyframes ipwDot { 0%, 60%, 100% { transform: translateY(0); opacity: .4; } 30% { transform: translateY(-4px); opacity: 1; } }

/* ---------- ปุ่มลัดคำถาม ---------- */
.ipw-chips { flex-shrink: 0; display: flex; gap: 6px; overflow-x: auto; padding: 8px 12px 2px; background: #fff; scrollbar-width: none; }
.ipw-chips::-webkit-scrollbar { display: none; }
.ipw-chip {
    flex-shrink: 0; border: 1px solid var(--ipw-line); background: #faf8ff; color: var(--ipw-p);
    border-radius: 999px; padding: 6px 12px; font-size: .78rem; font-family: inherit; cursor: pointer;
    transition: background .2s, color .2s, border-color .2s;
}
.ipw-chip i { margin-right: 4px; }
.ipw-chip:hover { background: var(--ipw-p); color: #fff; border-color: var(--ipw-p); }

/* ---------- ช่องพิมพ์ ---------- */
#chat-widget-container .chat-footer { flex-shrink: 0; padding: 10px 12px 12px; background: #fff; border-top: none; }
#chat-widget-container #chat-form {
    display: flex; align-items: center; gap: 4px; padding: 4px 4px 4px 6px;
    background: #f5f2fc; border: 1.5px solid transparent; border-radius: 999px;
    transition: border-color .2s, background .2s;
}
#chat-widget-container #chat-form:focus-within { border-color: var(--ipw-p); background: #fff; }
#chat-widget-container .chat-file-btn {
    width: 36px; height: 36px; flex-shrink: 0; border: none; border-radius: 50%;
    background: transparent; color: var(--ipw-muted); font-size: .95rem; cursor: pointer;
    display: flex; align-items: center; justify-content: center; transition: color .2s, background .2s;
}
#chat-widget-container .chat-file-btn:hover { color: var(--ipw-p); background: rgba(111, 66, 193, .1); }
#chat-widget-container #chat-message-input {
    flex: 1; min-width: 0; border: none; outline: none; background: transparent;
    padding: 8px 4px; font-family: inherit; font-size: .9rem; color: var(--ipw-text);
}
#chat-widget-container #chat-form > button[type="submit"] {
    width: 38px; height: 38px; flex-shrink: 0; border: none; border-radius: 50%; cursor: pointer;
    background: linear-gradient(135deg, var(--ipw-p), var(--ipw-pd)); color: #fff; font-size: .9rem;
    display: flex; align-items: center; justify-content: center;
    transition: transform .2s, box-shadow .2s;
}
#chat-widget-container #chat-form > button[type="submit"]:hover { transform: scale(1.08); box-shadow: 0 4px 14px rgba(111, 66, 193, .45); }

/* ---------- มือถือ ---------- */
@media (max-width: 480px) {
    #chat-widget-container .chat-window {
        position: fixed; left: 10px; right: 10px; bottom: 90px; width: auto; max-width: none;
        height: min(600px, calc(100vh - 110px));
        height: min(600px, calc(100dvh - 110px));
    }
    .ipw-call-btn small { font-size: .76rem; }
}