/* ---------------------------------------------------------
   กล่องข้อความ LINE — ธีมสว่างแบบหน้าจัดการแชท
   โครง: รางไอคอน / รายชื่อแชท / ห้องแชท / แผงข้อมูลลูกค้า
   --------------------------------------------------------- */

:root {
    --bg:        #F2F4F6;   /* พื้นหลังห้องแชท */
    --surface:   #FFFFFF;   /* การ์ดและแถบต่าง ๆ */
    --rail:      #FAFAFA;   /* รางไอคอนซ้ายสุด */
    --line:      #E4E7EA;   /* เส้นคั่น */
    --line-soft: #EFF1F3;
    --ink:       #1F2326;   /* ตัวอักษรหลัก */
    --ink-soft:  #6B7278;   /* ตัวอักษรรอง */
    --ink-faint: #9AA0A6;
    --green:     #06C755;   /* สีหลัก */
    --green-dk:  #05A648;
    --green-bg:  #E9F9EF;
    --bubble-in: #FFFFFF;   /* ฟองฝั่งลูกค้า */
    --bubble-out:#DCE9F8;   /* ฟองฝั่งเรา */
    --bubble-bot:#EDEFF2;   /* ฟองของบอท */
    --select:    #EDF2F7;   /* แถวที่เลือกอยู่ */
    --danger:    #D7443E;

    --r-sm: 6px;
    --r-md: 10px;
    --r-bubble: 16px;

    --font: "IBM Plex Sans Thai", "Noto Sans Thai", system-ui, sans-serif;
}

* { box-sizing: border-box; }

html, body { height: 100%; margin: 0; }

body {
    background: var(--bg);
    color: var(--ink);
    font-family: var(--font);
    font-size: 14px;
    line-height: 1.6;
    -webkit-font-smoothing: antialiased;
}

button, input, textarea { font: inherit; color: inherit; }
button { cursor: pointer; border: none; background: none; }
:focus-visible { outline: 2px solid var(--green); outline-offset: 1px; }

/* ===== โครงหน้า ===== */

.app {
    display: grid;
    grid-template-columns: 56px 300px minmax(0, 1fr) 300px;
    height: 100dvh;
    max-width: 100vw;
    overflow: hidden;
}

/* ===== รางไอคอน ===== */

.rail {
    background: var(--rail);
    border-right: 1px solid var(--line);
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 4px;
    padding: 12px 0;
}

.rail__mark {
    width: 30px;
    height: 30px;
    border-radius: 50%;
    background: var(--green);
    color: #fff;
    display: grid;
    place-items: center;
    font-weight: 600;
    font-size: 15px;
    margin-bottom: 14px;
}

.rail__btn {
    width: 40px;
    height: 40px;
    border-radius: var(--r-sm);
    display: grid;
    place-items: center;
    color: var(--ink-faint);
    transition: background .15s, color .15s;
}

.rail__btn:hover { background: #ECEEF0; color: var(--ink-soft); }
.rail__btn.is-on { color: var(--green); }
.rail__spacer { flex: 1; }

/* ===== รายชื่อแชท ===== */

.list {
    background: var(--surface);
    border-right: 1px solid var(--line);
    display: flex;
    flex-direction: column;
    min-height: 0;
    min-width: 0;
    overflow: hidden;
}

.list__head { padding: 14px 16px 10px; }

.list__title {
    font-size: 16px;
    font-weight: 600;
    margin: 0;
}

.live {
    display: inline-flex;
    align-items: center;
    gap: 6px;
    font-size: 12px;
    color: var(--ink-faint);
}

.live__dot {
    width: 6px;
    height: 6px;
    border-radius: 50%;
    background: var(--green);
    animation: pulse 2.4s infinite;
}

@keyframes pulse {
    0%   { box-shadow: 0 0 0 0 rgba(6, 199, 85, .5); }
    70%  { box-shadow: 0 0 0 7px rgba(6, 199, 85, 0); }
    100% { box-shadow: 0 0 0 0 rgba(6, 199, 85, 0); }
}

.search { margin: 2px 14px 10px; position: relative; }

.search input {
    width: 100%;
    background: var(--bg);
    border: 1px solid transparent;
    border-radius: var(--r-sm);
    padding: 7px 11px 7px 31px;
}

.search input:focus { background: var(--surface); border-color: var(--green); outline: none; }
.search input::placeholder { color: var(--ink-faint); }
.search svg { position: absolute; left: 10px; top: 9px; color: var(--ink-faint); }

.filters {
    display: flex;
    gap: 5px;
    overflow-x: auto;
    scrollbar-width: none;
    padding: 0 14px 10px;
    border-bottom: 1px solid var(--line);
}

.chip {
    padding: 4px 10px;
    white-space: nowrap;
    border-radius: 999px;
    font-size: 12.5px;
    color: var(--ink-soft);
    border: 1px solid var(--line);
    transition: background .15s, color .15s, border-color .15s;
}

.chip:hover { background: var(--bg); }
.chip.is-on { background: var(--green); border-color: var(--green); color: #fff; }

.threads { overflow-y: auto; flex: 1; }

.thread {
    display: grid;
    grid-template-columns: 40px 1fr;
    gap: 10px;
    width: 100%;
    text-align: start;
    padding: 11px 16px;
    border-bottom: 1px solid var(--line-soft);
    transition: background .14s;
}

.thread > div { min-width: 0; }
.thread__top { min-width: 0; }
.thread:hover { background: #F7F8F9; }
.thread.is-active { background: var(--select); }

.avatar {
    width: 40px;
    height: 40px;
    border-radius: 50%;
    display: grid;
    place-items: center;
    font-weight: 500;
    color: #fff;
    background: #B6BEC6;
    object-fit: cover;
}

.avatar--waiting { background: #DDA94B; }
.avatar--closed  { background: #C4CAD0; }

.thread__top {
    display: flex;
    justify-content: space-between;
    align-items: baseline;
    gap: 8px;
}

.thread__name {
    font-weight: 500;
    font-size: 14px;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

.thread__ago { font-size: 11px; color: var(--ink-faint); flex: none; }

.thread__preview {
    font-size: 12.5px;
    color: var(--ink-soft);
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

.badge {
    display: inline-grid;
    place-items: center;
    min-width: 18px;
    height: 18px;
    padding: 0 5px;
    border-radius: 999px;
    background: var(--green);
    color: #fff;
    font-size: 11px;
    font-weight: 600;
}

/* ===== ห้องแชท ===== */

.room {
    background: var(--bg);
    display: flex;
    flex-direction: column;
    min-height: 0;
    min-width: 0;
}

#roomView { display: contents; }
[hidden] { display: none !important; }

.room__head {
    display: flex;
    align-items: center;
    gap: 10px;
    padding: 10px 18px;
    background: var(--surface);
    border-bottom: 1px solid var(--line);
}

.room__head .avatar { width: 34px; height: 34px; font-size: 13px; }
.room__who { flex: 1; min-width: 0; }
.room__name, .room__sub { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.room__name { font-size: 15px; font-weight: 600; margin: 0; }
.room__sub { font-size: 12px; color: var(--ink-faint); }

.state {
    font-size: 12px;
    padding: 3px 10px;
    border-radius: 999px;
    background: var(--green-bg);
    color: var(--green-dk);
}

.state--waiting { background: #FDF2DC; color: #B57A14; }
.state--closed  { background: #EEF0F2; color: var(--ink-soft); }

.ghost {
    padding: 6px 12px;
    border-radius: var(--r-sm);
    border: 1px solid var(--line);
    background: var(--surface);
    font-size: 13px;
    color: var(--ink-soft);
    transition: border-color .15s, color .15s, background .15s;
}

.ghost:hover { border-color: #C9CFD4; background: #F7F8F9; color: var(--ink); }

.stream {
    flex: 1;
    overflow-y: auto;
    padding: 18px 22px 10px;
    display: flex;
    flex-direction: column;
    gap: 8px;
    min-width: 0;
}

/* แถวข้อความ: ฟอง + เวลาอยู่ข้างนอกแบบหน้าจัดการแชท */
.msg {
    display: flex;
    align-items: flex-end;
    gap: 6px;
    max-width: 78%;
    min-width: 0;
}

.msg--out { align-self: flex-end; flex-direction: row; justify-content: flex-end; }

.msg__meta {
    display: flex;
    flex-direction: column;
    align-items: flex-end;
    font-size: 11px;
    color: var(--ink-faint);
    white-space: nowrap;
    padding-bottom: 2px;
}

.msg:not(.msg--out) .msg__meta { align-items: flex-start; }
.msg__tag { color: var(--ink-faint); }

.bubble {
    padding: 8px 13px;
    border-radius: var(--r-bubble);
    background: var(--bubble-in);
    border: 1px solid var(--line);
    white-space: pre-wrap;
    overflow-wrap: anywhere;
    min-width: 0;
    animation: rise .18s ease-out;
}

.bubble--out { background: var(--bubble-out); border-color: #CBDCF1; }
.bubble--bot { background: var(--bubble-bot); border-color: #E2E5E9; color: var(--ink-soft); }

@keyframes rise {
    from { opacity: 0; transform: translateY(4px); }
    to   { opacity: 1; transform: none; }
}

/* สติกเกอร์และรูป ไม่ต้องมีกรอบฟอง */
.bubble--sticker,
.bubble--image {
    background: none;
    border: none;
    padding: 0;
}

.bubble--sticker img { width: 124px; height: 124px; object-fit: contain; display: block; }

.bubble--image img {
    max-width: 260px;
    max-height: 300px;
    object-fit: contain;
    border-radius: 12px;
    display: block;
    cursor: zoom-in;
    background: var(--surface);
}

.typing {
    align-self: flex-end;
    display: flex;
    gap: 4px;
    padding: 11px 15px;
    border-radius: var(--r-bubble);
    background: var(--bubble-bot);
}

.typing span {
    width: 6px;
    height: 6px;
    border-radius: 50%;
    background: var(--ink-faint);
    animation: bounce 1.1s infinite;
}

.typing span:nth-child(2) { animation-delay: .15s; }
.typing span:nth-child(3) { animation-delay: .3s; }

@keyframes bounce {
    0%, 60%, 100% { transform: none; opacity: .45; }
    30%           { transform: translateY(-4px); opacity: 1; }
}

.stream.is-dropping {
    outline: 2px dashed var(--green);
    outline-offset: -8px;
    background-color: var(--green-bg);
}

/* ===== กล่องพิมพ์ ===== */

.composer {
    background: var(--surface);
    border-top: 1px solid var(--line);
    padding: 10px 16px 14px;
    min-width: 0;
}

.quick {
    display: flex;
    gap: 6px;
    flex-wrap: wrap;
    margin-bottom: 9px;
    min-width: 0;
}

.quick { overflow: hidden; }

.quick button {
    font-size: 12.5px;
    padding: 4px 11px;
    border-radius: 999px;
    border: 1px solid var(--line);
    color: var(--ink-soft);
    transition: background .15s, color .15s, border-color .15s;
}

.quick button:hover { background: var(--green-bg); color: var(--green-dk); border-color: #BCE7CD; }

.composer__row {
    display: grid;
    grid-template-columns: auto auto auto auto minmax(0, 1fr) auto;
    gap: 6px;
    align-items: end;
    min-width: 0;
}

.icon-btn {
    height: 38px;
    width: 38px;
    border-radius: var(--r-sm);
    color: var(--ink-faint);
    display: grid;
    place-items: center;
    transition: background .15s, color .15s;
}

.icon-btn:hover { background: var(--bg); color: var(--ink-soft); }
.icon-btn.is-on { background: var(--green-bg); color: var(--green-dk); }

.composer textarea {
    resize: none;
    width: 100%;
    min-width: 0;
    min-height: 38px;
    max-height: 220px;
    overflow-y: auto;
    line-height: 1.55;
    border: 1px solid var(--line);
    border-radius: var(--r-sm);
    padding: 9px 12px;
    background: var(--surface);
    transition: border-color .15s;
}

.composer textarea:focus { border-color: var(--green); outline: none; }

.send {
    height: 38px;
    padding: 0 18px;
    border-radius: var(--r-sm);
    background: var(--green);
    color: #fff;
    font-weight: 500;
    display: inline-flex;
    align-items: center;
    gap: 7px;
    transition: background .15s;
}

.send:hover { background: var(--green-dk); }
.send[disabled] { background: #C9CFD4; cursor: not-allowed; }

/* ===== แผงลอย: สติกเกอร์ อีโมจิ ข้อความสำเร็จรูป ===== */

.picker {
    border: 1px solid var(--line);
    border-radius: var(--r-md);
    background: var(--surface);
    margin-bottom: 10px;
    overflow: hidden;
    min-width: 0;
}

.picker__tabs { display: flex; gap: 4px; padding: 8px 10px 0; flex-wrap: wrap; }

.picker__tabs button {
    font-size: 12.5px;
    padding: 4px 11px;
    border-radius: 999px;
    color: var(--ink-soft);
    transition: background .15s, color .15s;
}

.picker__tabs button:hover { background: var(--bg); }
.picker__tabs button.is-on { background: var(--green); color: #fff; }

.picker__grid {
    display: grid;
    grid-template-columns: repeat(auto-fill, minmax(58px, 1fr));
    gap: 3px;
    padding: 9px;
    max-height: 200px;
    overflow-y: auto;
}

.picker__grid button {
    aspect-ratio: 1;
    border-radius: var(--r-sm);
    padding: 4px;
    transition: background .14s;
}

.picker__grid button:hover { background: var(--bg); }
.picker__grid img { width: 100%; height: 100%; object-fit: contain; }

.picker__note {
    margin: 0;
    padding: 7px 12px 9px;
    font-size: 11.5px;
    color: var(--ink-faint);
    border-top: 1px solid var(--line-soft);
}

.emoji__grid {
    display: grid;
    grid-template-columns: repeat(auto-fill, minmax(38px, 1fr));
    gap: 2px;
    padding: 9px;
    max-height: 200px;
    overflow-y: auto;
}

.emoji__grid button {
    aspect-ratio: 1;
    border-radius: var(--r-sm);
    font-size: 21px;
    line-height: 1;
    transition: background .14s;
}

.emoji__grid button:hover { background: var(--bg); }

/* ข้อความสำเร็จรูป */

.reply__bar {
    display: grid;
    grid-template-columns: 1fr auto;
    gap: 8px;
    padding: 10px;
    border-bottom: 1px solid var(--line-soft);
}

.reply__bar input {
    border: 1px solid var(--line);
    border-radius: var(--r-sm);
    padding: 7px 11px;
    min-width: 0;
}

.reply__bar input:focus { border-color: var(--green); outline: none; }
.reply__bar .ghost { white-space: nowrap; }

.reply__form {
    display: grid;
    gap: 8px;
    padding: 12px 10px;
    background: #FBFCFD;
    border-bottom: 1px solid var(--line-soft);
}

.reply__form input,
.reply__form textarea {
    border: 1px solid var(--line);
    border-radius: var(--r-sm);
    padding: 8px 11px;
    background: var(--surface);
    resize: vertical;
    width: 100%;
}

.reply__form input:focus,
.reply__form textarea:focus { border-color: var(--green); outline: none; }

.reply__insert { display: flex; gap: 6px; align-items: center; flex-wrap: wrap; }
.reply__insert .reply__mini { border: 1px solid var(--line); background: var(--surface); }
.reply__hint { font-size: 11.5px; color: var(--ink-faint); }
.reply__actions { display: flex; gap: 8px; }

.reply__list { max-height: 220px; overflow-y: auto; padding: 4px; }

.reply__item {
    display: grid;
    grid-template-columns: 1fr auto auto;
    gap: 6px;
    align-items: center;
    padding: 8px 10px;
    border-radius: var(--r-sm);
    transition: background .14s;
}

.reply__item:hover { background: var(--bg); }
.reply__pick { text-align: start; min-width: 0; }
.reply__pick b { display: block; font-size: 13.5px; font-weight: 500; }

.reply__pick span {
    display: block;
    font-size: 12px;
    color: var(--ink-soft);
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

.reply__mini {
    font-size: 12px;
    color: var(--ink-soft);
    padding: 4px 9px;
    border-radius: var(--r-sm);
    transition: background .14s, color .14s;
}

.reply__mini:hover { background: var(--surface); color: var(--green-dk); }
.reply__mini.is-danger:hover { color: var(--danger); }
.reply__empty { padding: 16px; text-align: center; color: var(--ink-faint); font-size: 13px; }

/* ===== แผงข้อมูลลูกค้า ===== */

.panel {
    background: var(--surface);
    border-left: 1px solid var(--line);
    padding: 16px;
    overflow-y: auto;
    min-width: 0;
}

.panel h2 {
    font-size: 12px;
    font-weight: 600;
    color: var(--ink-faint);
    margin: 20px 0 9px;
}

.panel h2:first-child { margin-top: 0; }

.metric { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; }

.metric > div {
    background: var(--bg);
    border-radius: var(--r-md);
    padding: 10px 11px;
}

.metric b { font-size: 21px; font-weight: 600; display: block; line-height: 1.2; }
.metric small { font-size: 11.5px; color: var(--ink-soft); }
.metric .up { color: var(--green-dk); }
.metric .warn { color: #B57A14; }

.spark {
    display: flex;
    align-items: flex-end;
    gap: 3px;
    height: 60px;
    padding: 7px 9px;
    background: var(--bg);
    border-radius: var(--r-md);
}

.spark i { flex: 1; background: var(--green); border-radius: 2px 2px 0 0; min-height: 3px; opacity: .85; }

.facts { font-size: 13px; }

.facts div {
    display: flex;
    justify-content: space-between;
    gap: 10px;
    padding: 7px 0;
    border-bottom: 1px solid var(--line-soft);
    min-width: 0;
}

.facts span { color: var(--ink-soft); flex: none; }
.facts b { font-weight: 500; overflow: hidden; text-overflow: ellipsis; }

.panel .ghost { width: 100%; margin-top: 8px; text-align: center; }

/* ===== อื่น ๆ ===== */

.empty { margin: auto; text-align: center; color: var(--ink-soft); max-width: 34ch; }
.empty b { display: block; font-size: 16px; color: var(--ink); margin-bottom: 5px; font-weight: 600; }

.lightbox {
    position: fixed;
    inset: 0;
    background: rgba(24, 28, 31, .86);
    display: grid;
    place-items: center;
    padding: 28px;
    z-index: 60;
    cursor: zoom-out;
}

.lightbox img { max-width: 100%; max-height: 100%; border-radius: 8px; }

.toast {
    position: fixed;
    bottom: 22px;
    left: 50%;
    transform: translate(-50%, 16px);
    background: #2B3033;
    color: #fff;
    padding: 10px 17px;
    border-radius: var(--r-sm);
    font-size: 13.5px;
    opacity: 0;
    pointer-events: none;
    transition: opacity .2s, transform .2s;
    z-index: 70;
}

.toast.is-on { opacity: 1; transform: translate(-50%, 0); }

/* ===== จอเล็ก ===== */

.back { display: none; }

@media (max-width: 1180px) {
    .app { grid-template-columns: 56px 280px minmax(0, 1fr); }
    .panel { display: none; }
}

@media (max-width: 780px) {
    .app { grid-template-columns: 1fr; grid-template-rows: auto minmax(0, 1fr); }
    .rail {
        flex-direction: row;
        padding: 6px 10px;
        border-right: none;
        border-bottom: 1px solid var(--line);
    }
    .rail__mark { margin: 0 8px 0 0; }
    .room { display: none; }
    body.is-room .list { display: none; }
    body.is-room .room { display: flex; }
    .back {
        display: grid;
        place-items: center;
        width: 32px;
        height: 32px;
        border-radius: var(--r-sm);
        color: var(--ink-soft);
    }
    .stream { padding: 14px; }
    .msg { max-width: 88%; }
}

@media (prefers-reduced-motion: reduce) {
    * { animation: none !important; transition: none !important; }
}
