/* =========================================================
   Deay Bypass — แชทกับแอดมิน (ปุ่มลอยมุมขวาล่างทุกหน้า)
   ========================================================= */
.chat { position: fixed; right: 18px; bottom: 18px; z-index: 95; font-size: 14px; }
/* ปุ่มกลับขึ้นบนเลื่อนขึ้นไปอยู่เหนือปุ่มแชท */
.has-chat .to-top { bottom: 88px; }

/* ---------- ปุ่มลอย ---------- */
.chat-fab {
  position: relative; display: grid; place-items: center;
  width: 58px; height: 58px; border-radius: 50%; color: #fff;
  background: linear-gradient(145deg, var(--accent-2), var(--accent) 60%, var(--accent-dark));
  box-shadow: 0 14px 30px -10px color-mix(in srgb, var(--accent) 85%, transparent), inset 0 1px 0 rgba(255, 255, 255, .35);
  transition: transform .25s cubic-bezier(.2, .8, .2, 1), box-shadow .25s;
}
.chat-fab:hover { transform: translateY(-2px) scale(1.04); box-shadow: 0 18px 36px -10px color-mix(in srgb, var(--accent) 95%, transparent), inset 0 1px 0 rgba(255, 255, 255, .35); }
.chat-fab:focus-visible { outline: 2px solid #fff; outline-offset: 3px; }
.chat-fab svg { position: absolute; width: 25px; height: 25px; transition: transform .3s cubic-bezier(.2, .8, .2, 1), opacity .2s; }
.chat-fab .i-close { opacity: 0; transform: rotate(-90deg) scale(.6); }
.chat.open .chat-fab .i-chat { opacity: 0; transform: rotate(90deg) scale(.6); }
.chat.open .chat-fab .i-close { opacity: 1; transform: none; }
/* วงแสงกระพริบตอนมีข้อความใหม่ */
.chat-fab::after { content: ""; position: absolute; inset: 0; border-radius: 50%; border: 2px solid var(--accent-2); opacity: 0; pointer-events: none; }
.chat-fab.has-unread::after { animation: chatPing 1.8s ease-out infinite; }
.chat-badge {
  position: absolute; top: -3px; right: -3px; min-width: 20px; height: 20px; padding: 0 5px;
  border-radius: 999px; background: #ef4444; color: #fff; box-shadow: 0 0 0 2px var(--bg);
  font-size: 11.5px; font-weight: 700; line-height: 20px; text-align: center;
  animation: badgePop .35s cubic-bezier(.2, .8, .2, 1);
}

/* ---------- ข้อความทักทาย ---------- */
.chat-tip {
  position: absolute; right: 0; bottom: 72px; width: max-content; max-width: 250px;
  padding: 10px 34px 10px 14px; border-radius: 14px 14px 4px 14px;
  border: 1px solid var(--line-2); background: #16171c; color: var(--text-2);
  box-shadow: 0 16px 40px rgba(0, 0, 0, .5);
  font-size: 13.5px; line-height: 1.5; cursor: pointer;
  animation: tipIn .45s cubic-bezier(.2, .8, .2, 1) both;
}
.chat-tip[hidden] { display: none; }
.chat-tip button { position: absolute; top: 7px; right: 7px; display: grid; place-items: center; width: 22px; height: 22px; border-radius: 6px; color: var(--dim); }
.chat-tip button:hover { color: #fff; background: rgba(255, 255, 255, .06); }
.chat-tip svg { width: 14px; height: 14px; }

/* ---------- กล่องแชท ---------- */
.chat-panel {
  position: absolute; right: 0; bottom: 72px;
  display: flex; flex-direction: column; overflow: hidden;
  width: 370px; height: min(560px, calc(100vh - 120px));
  border-radius: 20px; border: 1px solid var(--line-2);
  background: linear-gradient(180deg, #131419, #0b0c0f);
  box-shadow: 0 30px 80px rgba(0, 0, 0, .6), 0 0 40px -20px color-mix(in srgb, var(--accent) 60%, transparent);
  transform-origin: 100% 100%;
  opacity: 0; visibility: hidden; transform: translateY(14px) scale(.96);
  transition: opacity .22s, transform .3s cubic-bezier(.2, .8, .2, 1), visibility 0s .3s;
}
.chat.open .chat-panel { opacity: 1; visibility: visible; transform: none; transition: opacity .22s, transform .3s cubic-bezier(.2, .8, .2, 1); }

.chat-head {
  position: relative; display: flex; align-items: center; gap: 12px; padding: 14px 12px 14px 16px;
  border-bottom: 1px solid var(--line);
  background: radial-gradient(120% 160% at 0% 0%, color-mix(in srgb, var(--accent) 24%, transparent), transparent 60%), #111217;
}
.chat-head::before {
  content: ""; position: absolute; left: 0; right: 0; top: 0; height: 1px;
  background: linear-gradient(90deg, transparent, color-mix(in srgb, var(--accent-2) 70%, transparent), transparent);
}
.chat-agent {
  position: relative; flex: none; display: grid; place-items: center; width: 40px; height: 40px; border-radius: 12px;
  border: 1px solid rgba(129, 140, 248, .35); background: linear-gradient(135deg, #312e81, #0f0f1a);
}
.chat-agent svg, .chat-agent .logo-img { width: 24px; height: 24px; }
.chat-dot { position: absolute; right: -3px; bottom: -3px; width: 12px; height: 12px; border-radius: 50%; background: #71717a; box-shadow: 0 0 0 2px #111217; transition: background .3s, box-shadow .3s; }
.chat.online .chat-dot { background: #22c55e; box-shadow: 0 0 0 2px #111217, 0 0 10px rgba(34, 197, 94, .7); }
.chat-head-text { flex: 1; min-width: 0; }
.chat-head-text b { display: block; font-size: 15.5px; font-weight: 700; }
.chat-status { display: block; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; font-size: 12.5px; color: var(--muted); }
.chat.online .chat-status { color: #4ade80; }
.chat-x { flex: none; display: grid; place-items: center; width: 34px; height: 34px; border-radius: 10px; color: var(--muted); transition: background .2s, color .2s; }
.chat-x:hover { color: #fff; background: rgba(255, 255, 255, .06); }
.chat-x svg { width: 18px; height: 18px; }

/* ---------- ข้อความ ---------- */
.chat-body {
  flex: 1; display: flex; flex-direction: column; gap: 8px; overflow-y: auto; overscroll-behavior: contain;
  padding: 16px 14px 10px; scrollbar-width: thin; scrollbar-color: #2a2b31 transparent;
}
.cm { display: flex; flex-direction: column; gap: 3px; max-width: 84%; animation: cmIn .3s cubic-bezier(.2, .8, .2, 1) both; }
.cm p { padding: 9px 13px; border-radius: 16px; font-size: 14px; line-height: 1.55; white-space: pre-wrap; overflow-wrap: anywhere; }
.cm p a { color: inherit; text-decoration: underline; text-underline-offset: 2px; }
.cm-meta { display: flex; align-items: center; gap: 6px; padding: 0 6px; font-size: 10.5px; color: var(--dim); }
.cm-user .cm-meta { justify-content: flex-end; }
/* ส่งแล้ว ✓ / อ่านแล้ว ✓✓ ใต้ข้อความล่าสุดของลูกค้า */
.cm-seen { display: inline-flex; align-items: center; gap: 3px; font-weight: 600; color: var(--dim); transition: color .3s; }
.cm-seen svg { width: 13px; height: 13px; }
.cm-seen.read { color: var(--accent-2); }
.cm-seen.pop { animation: seenPop .5s cubic-bezier(.2, .8, .2, 1); }
.cm-seen.pop svg { animation: seenTick .6s cubic-bezier(.2, .8, .2, 1); }
/* แอดมินกำลังพิมพ์ */
.cm-typing p { display: flex; align-items: center; gap: 4px; padding: 13px 15px; }
.cm-typing span { width: 7px; height: 7px; border-radius: 50%; background: var(--muted); animation: dotBounce 1.1s ease-in-out infinite; }
.cm-typing span:nth-child(2) { animation-delay: .15s; }
.cm-typing span:nth-child(3) { animation-delay: .3s; }
.chat.typing .chat-status { color: var(--accent-2); }
.cm-admin { align-self: flex-start; }
.cm-admin p { border: 1px solid var(--line); border-bottom-left-radius: 6px; background: #1b1c22; color: var(--text-2); }
.cm-user { align-self: flex-end; align-items: flex-end; }
.cm-user p {
  border-bottom-right-radius: 6px; color: #fff;
  background: linear-gradient(135deg, var(--accent-2), var(--accent));
  box-shadow: 0 6px 16px -8px color-mix(in srgb, var(--accent) 85%, transparent);
}
.cm.pending p { opacity: .6; }
.cm.failed { cursor: pointer; }
.cm.failed p { background: #3a1c1f; box-shadow: inset 0 0 0 1px rgba(248, 113, 113, .45); }
.cm.failed time { color: #f87171; }
.cm-day { align-self: center; margin: 4px 0; padding: 3px 10px; border-radius: 999px; background: rgba(255, 255, 255, .04); font-size: 11px; color: var(--dim); }
.chat-loading { display: flex; gap: 5px; align-self: center; padding: 18px; }
.chat-loading span { width: 7px; height: 7px; border-radius: 50%; background: var(--accent-2); animation: dotBounce 1s ease-in-out infinite; }
.chat-loading span:nth-child(2) { animation-delay: .15s; }
.chat-loading span:nth-child(3) { animation-delay: .3s; }

/* การ์ดเข้าสู่ระบบ / ใช้งานไม่ได้ */
.chat-card {
  display: flex; flex-direction: column; align-items: center; gap: 10px; margin-top: 6px; padding: 16px;
  border-radius: 14px; border: 1px solid rgba(99, 102, 241, .35); background: rgba(99, 102, 241, .08); text-align: center;
  animation: cmIn .35s .1s cubic-bezier(.2, .8, .2, 1) both;
}
.chat-card p { font-size: 13.5px; line-height: 1.65; color: var(--text-2); }
.chat-card .btn { padding: 9px 16px; font-size: 14px; }
.chat-card .btn svg { width: 18px; height: 18px; }
.chat-card-icon { display: grid; place-items: center; width: 38px; height: 38px; border-radius: 50%; background: rgba(99, 102, 241, .16); color: var(--accent-2); }
.chat-card-icon svg { width: 18px; height: 18px; }
.chat-alt { font-size: 13px; font-weight: 600; color: var(--accent-2); }
.chat-alt:hover { text-decoration: underline; }

/* คำถามด่วน */
.chat-chips { display: flex; flex-wrap: wrap; gap: 6px; padding: 0 14px 10px; }
.chat-chips[hidden] { display: none; }
.chat-chips button {
  padding: 6px 11px; border-radius: 999px; font-size: 12.5px; color: #c7d2fe;
  border: 1px solid rgba(129, 140, 248, .35); background: rgba(99, 102, 241, .1);
  transition: background .2s, border-color .2s, transform .2s;
}
.chat-chips button:hover { background: rgba(99, 102, 241, .2); border-color: rgba(129, 140, 248, .6); transform: translateY(-1px); }

/* ช่องพิมพ์ */
.chat-form { display: flex; align-items: flex-end; gap: 8px; padding: 10px 12px 12px; border-top: 1px solid var(--line); background: #0d0e11; }
.chat-form[hidden] { display: none; }
.chat-form textarea {
  flex: 1; min-height: 42px; max-height: 120px; padding: 10px 13px; resize: none; outline: none;
  border-radius: 14px; border: 1px solid var(--line-2); background: #08090b; color: var(--text);
  font: inherit; font-size: 14px; line-height: 1.45;
  transition: border-color .2s, box-shadow .2s;
}
.chat-form textarea::placeholder { color: var(--dim); }
.chat-form textarea:focus { border-color: rgba(129, 140, 248, .7); box-shadow: 0 0 0 4px rgba(99, 102, 241, .15); }
.chat-attach { flex: none; display: grid; place-items: center; width: 38px; height: 42px; border-radius: 12px; color: var(--muted); transition: color .2s, background .2s; }
.chat-attach:hover { color: #fff; background: rgba(255, 255, 255, .06); }
.chat-attach svg { width: 20px; height: 20px; }
.cm p.cm-img { padding: 3px; line-height: 0; }
.cm-img img { display: block; width: 220px; max-width: 100%; max-height: 280px; object-fit: cover; border-radius: 13px; background: rgba(255, 255, 255, .06); cursor: zoom-in; }
.cm.pending .cm-img img { filter: saturate(.6) brightness(.8); }
.chat.drop .chat-panel { box-shadow: 0 0 0 2px var(--accent-2), 0 30px 80px rgba(0, 0, 0, .6); }.chat-look { position: fixed; inset: 0; z-index: 200; display: grid; place-items: center; padding: 24px; background: rgba(0, 0, 0, .72); -webkit-backdrop-filter: blur(12px); backdrop-filter: blur(12px); cursor: zoom-out; animation: lookFade .2s ease; }
.chat-look img { max-width: min(92vw, 1200px); max-height: 88vh; border-radius: 12px; box-shadow: 0 30px 90px rgba(0, 0, 0, .6); animation: lookIn .28s cubic-bezier(.2, .8, .2, 1); }
@keyframes lookFade { from { opacity: 0; } }
@keyframes lookIn { from { opacity: 0; transform: scale(.92); } }
.chat-send { flex: none; display: grid; place-items: center; width: 42px; height: 42px; border-radius: 12px; color: #fff; background: var(--accent); transition: background .2s, transform .2s, opacity .2s; }
.chat-send:hover:not(:disabled) { background: var(--accent-dark); transform: translateY(-1px); }
.chat-send:disabled { opacity: .4; cursor: default; }
.chat-send svg { width: 18px; height: 18px; }

@keyframes chatPing { 0% { transform: scale(1); opacity: .7; } 80%, 100% { transform: scale(1.55); opacity: 0; } }
@keyframes badgePop { from { transform: scale(.4); } to { transform: none; } }
@keyframes cmIn { from { opacity: 0; transform: translateY(8px) scale(.98); } to { opacity: 1; transform: none; } }
@keyframes tipIn { from { opacity: 0; transform: translateY(10px) scale(.95); } to { opacity: 1; transform: none; } }
@keyframes seenPop { 0% { opacity: 0; transform: translateY(4px); } 60% { opacity: 1; transform: translateY(-1px); } 100% { transform: none; } }
@keyframes seenTick { 0% { transform: scale(.4) rotate(-15deg); } 60% { transform: scale(1.3); } 100% { transform: none; } }
@keyframes dotBounce { 0%, 100% { transform: translateY(0); opacity: .5; } 50% { transform: translateY(-5px); opacity: 1; } }

@media (max-width: 600px) {
  .chat { right: 12px; bottom: 12px; }
  .chat-fab { width: 54px; height: 54px; }
  .has-chat .to-top { bottom: 76px; }
  .chat-panel { position: fixed; left: 8px; right: 8px; bottom: 76px; width: auto; height: min(600px, calc(100dvh - 96px)); }
  .chat-tip { bottom: 66px; }
}
