/*
 * chat.css — ประยุกต์ระบบ "เมล/ข้อความส่วนตัว" (admin/mail.php) ให้เป็นแชทส่วนตัวรายคน
 *  - ซ้าย: รายชื่อคู่สนทนา (เรียงตามข้อความล่าสุด) + ค้นหา + เริ่มแชทใหม่
 *  - ขวา: ห้องสนทนา bubble ซ้าย(รับ)/ขวา(ส่ง) + ช่องพิมพ์ใต้ห้อง
 *  ดีไซน์: ขาว/เทา/ฟ้า #2563eb + Noto Sans Thai (เข้าธีมระบบ ไม่เด่นเกินไป)
 */

.chat-page > h2 { display: none; }   /* ใช้หัวข้อในตัวแชทแทน */

.chat {
    display: flex;
    height: calc(100vh - 270px);
    min-height: 460px;
    margin: 4px 0 8px;
    border: 1px solid #e7e9ee;
    border-radius: 16px;
    background: #fff;
    box-shadow: 0 6px 22px rgba(16, 24, 40, .06);
    overflow: hidden;
}

/* ===================== รายชื่อคู่สนทนา (ซ้าย) ===================== */
.chat__list {
    display: flex;
    flex-direction: column;
    flex: 0 0 300px;
    min-width: 0;
    border-right: 1px solid #eef0f3;
    background: #fcfcfd;
}
.chat__list_head {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 8px;
    padding: 16px 16px 12px;
}
.chat__list_head h3 {
    margin: 0;
    font-size: 22px;
    font-weight: 800;
    letter-spacing: -.2px;
    color: #22314f;
    white-space: nowrap;
}
/* ปุ่มเริ่มแชทใหม่ = ปุ่มกลมไอคอนดินสอ (สไตล์ Messenger) — กันธีมอื่นยัด padding */
.chat__new-btn {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    flex: 0 0 auto;
    width: 38px;
    height: 38px;
    padding: 0 !important;
    border: 0;
    border-radius: 50%;
    background: #eef1f5;
    color: #22314f;
    cursor: pointer;
    transition: background-color .15s ease, transform .1s ease;
}
.chat__new-btn:hover { background: #e2e6ec; }
.chat__new-btn:active { transform: scale(.93); }
.chat__new-btn svg { flex: 0 0 auto; width: 17px; height: 17px; fill: none; stroke: currentColor; }
.chat__new-btn span { display: none; }   /* ซ่อนข้อความ ใช้ไอคอน+tooltip แทน */

.chat__search { padding: 0 14px 10px; }
.chat__search input {
    width: 100%;
    height: 38px;
    padding: 0 12px 0 34px;
    border: 1px solid #dde7f5;
    border-radius: 10px;
    background: #fff url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='16' height='16' fill='none' stroke='%2398a2b3' stroke-width='2' stroke-linecap='round'%3E%3Ccircle cx='7' cy='7' r='5'/%3E%3Cpath d='M15 15l-4-4'/%3E%3C/svg%3E") no-repeat 11px center;
    font-family: inherit;
    font-size: 13.5px;
    color: #1d2939;
    outline: none;
    transition: border-color .15s ease, box-shadow .15s ease;
}
.chat__search input:focus {
    border-color: #2563eb;
    box-shadow: 0 0 0 3px rgba(24, 119, 242, .12);
}

.chat__convs { flex: 1 1 auto; overflow-y: auto; padding: 0 8px 10px; }

.chat__conv {
    display: flex;
    align-items: center;
    gap: 11px;
    width: 100%;
    padding: 9px 10px;
    margin: 2px 0;
    border: 0;
    border-radius: 12px;
    background: transparent;
    text-align: left;
    text-decoration: none;
    cursor: pointer;
    transition: background-color .12s ease;
}
.chat__conv:hover { background: #eff4fc; }
.chat__conv.is-active { background: #e7f0fe; }

.chat__ava {
    flex: 0 0 42px;
    width: 42px;
    height: 42px;
    border-radius: 50%;
    overflow: hidden;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 16px;
    font-weight: 800;
    color: #fff;
    background: #94a3b8;
}
.chat__ava img { width: 100%; height: 100%; object-fit: cover; display: block; }

.chat__conv_main { flex: 1 1 auto; min-width: 0; }
.chat__conv_top {
    display: flex;
    align-items: baseline;
    justify-content: space-between;
    gap: 8px;
}
.chat__conv_name {
    font-size: 14px;
    font-weight: 700;
    color: #1d2939;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}
.chat__conv_time { flex: 0 0 auto; font-size: 11px; color: #98a2b3; }
.chat__conv_preview {
    margin-top: 2px;
    font-size: 12.5px;
    color: #667085;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}
.chat__conv.has-unread .chat__conv_name { font-weight: 800; }
.chat__conv.has-unread .chat__conv_preview { color: #1d2939; font-weight: 600; }
.chat__badge {
    flex: 0 0 auto;
    min-width: 19px;
    height: 19px;
    margin-left: 6px;
    padding: 0 5px;
    border-radius: 10px;
    background: #ef4444;
    color: #fff;
    font-size: 11px;
    font-weight: 700;
    line-height: 19px;
    text-align: center;
}

/* ===================== ห้องสนทนา (ขวา) ===================== */
.chat__thread {
    display: flex;
    flex-direction: column;
    flex: 1 1 auto;
    min-width: 0;
    background: #f7f8fa;
}
.chat__thread_head {
    display: flex;
    align-items: center;
    gap: 11px;
    padding: 12px 18px;
    border-bottom: 1px solid #eef0f3;
    background: #fff;
}
.chat__thread_head .chat__ava { flex-basis: 38px; width: 38px; height: 38px; font-size: 15px; }
.chat__thread_name { font-size: 15px; font-weight: 800; color: #22314f; }
.chat__thread_sub { font-size: 12px; color: #98a2b3; }
.chat__back {
    display: none;
    margin-right: 2px;
    border: 0; background: transparent; cursor: pointer; padding: 4px;
}
.chat__back svg { width: 20px; height: 20px; fill: #475467; }

.chat__messages {
    flex: 1 1 auto;
    overflow-y: auto;
    padding: 18px 18px 8px;
    display: flex;
    flex-direction: column;
    gap: 3px;
}
.chat__day {
    align-self: center;
    margin: 12px 0 8px;
    padding: 3px 12px;
    border-radius: 11px;
    background: #e9eaed;
    color: #667085;
    font-size: 11.5px;
    font-weight: 600;
}
.chat__bubble {
    max-width: 74%;
    padding: 9px 13px;
    border-radius: 16px;
    font-size: 14px;
    line-height: 1.5;
    word-wrap: break-word;
    overflow-wrap: anywhere;
}
.chat__bubble time {
    display: block;
    margin-top: 3px;
    font-size: 10.5px;
    opacity: .7;
}
.chat__bubble a { text-decoration: underline; }
/* รับ (ซ้าย) */
.chat__bubble--in {
    align-self: flex-start;
    background: #fff;
    border: 1px solid #ebedf0;
    color: #1d2939;
    border-bottom-left-radius: 5px;
}
.chat__bubble--in time { color: #98a2b3; }
/* ส่ง (ขวา) */
.chat__bubble--out {
    align-self: flex-end;
    background: #2563eb;
    color: #fff;
    border-bottom-right-radius: 5px;
}
.chat__bubble--out a { color: #fff; }

/* ติด ๆ กันจากคนเดียวกัน -> ลดมุมโค้ง + ชิดกันแบบ Messenger */
.chat__bubble--in + .chat__bubble--in { border-top-left-radius: 6px; }
.chat__bubble--out + .chat__bubble--out { border-top-right-radius: 6px; }

/* เวลา: โชว์เฉพาะ bubble สุดท้ายของกลุ่ม + ตอนเอาเมาส์ชี้ (แบบ Messenger) */
.chat__bubble time { display: none; }
.chat__bubble:hover time { display: block; }
.chat__bubble--in:not(:has(+ .chat__bubble--in)) time,
.chat__bubble--out:not(:has(+ .chat__bubble--out)) time { display: block; }

/* สถานะส่ง (AJAX): กำลังส่ง / ส่งไม่สำเร็จ (แตะเพื่อลองใหม่) */
.chat__bubble.is-sending { opacity: .55; }
.chat__bubble.is-failed {
    background: #fee2e2;
    color: #7f1d1d;
    cursor: pointer;
    outline: 1px solid #fda4af;
}
.chat__bubble.is-failed::after {
    content: "ส่งไม่สำเร็จ — แตะเพื่อลองใหม่";
    display: block;
    margin-top: 4px;
    font-size: 11px;
    font-weight: 700;
    color: #e11d48;
}
.chat__bubble.is-failed time { display: none !important; }

/* อ่านแล้ว (read receipt) ใต้ข้อความล่าสุดของเราที่อีกฝ่ายเปิดอ่าน */
.chat__seen {
    align-self: flex-end;
    margin: 2px 4px 0;
    font-size: 11px;
    color: #98a2b3;
}
.chat__seen[hidden] { display: none; }

/* ===================== ช่องพิมพ์ ===================== */
.chat__composer {
    display: flex;
    align-items: flex-end;
    gap: 9px;
    padding: 12px 16px;
    border-top: 1px solid #eef0f3;
    background: #fff;
}
.chat__composer textarea {
    flex: 1 1 auto;
    min-height: 44px;
    max-height: 140px;
    padding: 11px 14px;
    border: 1px solid #dde7f5;
    border-radius: 14px;
    background: #f7f8fa;
    font-family: inherit;
    font-size: 14px;
    line-height: 1.4;
    color: #1d2939;
    resize: none;
    outline: none;
    transition: border-color .15s ease, box-shadow .15s ease, background-color .15s ease;
}
.chat__composer textarea:focus {
    border-color: #2563eb;
    background: #fff;
    box-shadow: 0 0 0 3px rgba(24, 119, 242, .1);
}
.chat__send {
    flex: 0 0 auto;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    width: 44px;
    height: 44px;
    padding: 0 !important;      /* กันธีมอื่นยัด padding ปุ่ม (11px 22px) จนไอคอนเหลือ 0px */
    border: 0;
    border-radius: 50%;
    background: #2563eb;
    color: #fff;
    cursor: pointer;
    transition: background-color .15s ease, transform .1s ease;
}
.chat__send:hover { background: #1567d6; }
.chat__send:active { transform: scale(.94); }
.chat__send:disabled { background: #cbd5e1; cursor: default; }
.chat__send svg { flex: 0 0 auto; width: 20px; height: 20px; fill: currentColor; }

/* ===================== สถานะว่าง ===================== */
.chat__empty {
    flex: 1 1 auto;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    gap: 10px;
    padding: 30px;
    color: #98a2b3;
    text-align: center;
}
.chat__empty svg { width: 56px; height: 56px; stroke: #cbd5e1; }
.chat__empty span { font-size: 14px; }

/* ===================== แผงเริ่มแชทใหม่ ===================== */
.chat__new-panel {
    padding: 11px 14px;
    border-bottom: 1px solid #eef0f3;
    background: #fff;
}
.chat__new-panel[hidden] { display: none; }
.chat__new-panel label {
    display: block;
    margin-bottom: 6px;
    font-size: 12.5px;
    font-weight: 700;
    color: #475467;
}

/* =========================================================================
 *  ปุ่มส่งแชทในหน้าเรื่อง (admin_ticket.php) — แผงพับ ไม่เด่นเกินไป
 * ========================================================================= */
.tchat { max-width: 540px; }
.tchat__row { display: flex; align-items: center; gap: 10px; margin-bottom: 10px; }
.tchat__row label { flex: 0 0 auto; font-size: 13px; font-weight: 700; color: #475467; }
.tchat__row select {
    flex: 1 1 auto;
    height: 40px;
    padding: 0 11px;
    border: 1px solid #dde7f5;
    border-radius: 9px;
    background: #fff;
    font-family: inherit;
    font-size: 14px;
    color: #1d2939;
}
.tchat textarea {
    width: 100%;
    box-sizing: border-box;
    padding: 10px 12px;
    border: 1px solid #dde7f5;
    border-radius: 11px;
    font-family: inherit;
    font-size: 14px;
    line-height: 1.45;
    color: #1d2939;
}
.tchat textarea:focus {
    border-color: #2563eb;
    outline: none;
    box-shadow: 0 0 0 3px rgba(24, 119, 242, .1);
}
.tchat__foot { display: flex; align-items: center; justify-content: space-between; gap: 12px; margin-top: 10px; }
.tchat__open { font-size: 13px; font-weight: 600; color: #2563eb; text-decoration: none; }
.tchat__open:hover { text-decoration: underline; }
.tchat__status { font-size: 13px; font-weight: 700; }
.tchat__status.is-ok { color: #12b76a; }
.tchat__status.is-err { color: #ef4444; }

/* ===================== มือถือ ===================== */
@media screen and (max-width: 760px) {
    .chat { height: calc(100vh - 210px); }
    .chat__list { flex-basis: 100%; }
    .chat__thread { display: none; }
    .chat.is-thread .chat__list { display: none; }
    .chat.is-thread .chat__thread { display: flex; }
    .chat__back { display: inline-flex; }
}

/* ปุ่มเริ่มแชทใหม่: + เพื่อเปิด / − เพื่อย่อกลับ */
.chat__new-btn .ico-minus { display: none; }
.chat__new-btn.is-open .ico-plus { display: none; }
.chat__new-btn.is-open .ico-minus { display: block; }
.chat__new-btn.is-open { background: #e7f0fe; color: #1e40af; }
