/* ========== FrostFarm — Chat (ลูกค้า + แอดมิน) ========== */

/* ---------- ข้อความ ---------- */
.chat-msgs {
  display: flex; flex-direction: column; gap: 6px; overflow-y: auto; overscroll-behavior: contain;
  padding: 14px 14px 8px; scroll-behavior: smooth;
}
.msg { display: flex; }
.msg.mine { justify-content: flex-end; }
.msg-bubble {
  position: relative; max-width: 78%; padding: 8px 12px 6px; border-radius: 18px;
  font-size: .94rem; line-height: 1.5; word-wrap: break-word; overflow-wrap: anywhere;
  animation: msg-in .22s cubic-bezier(.2, .7, .3, 1);
}
.msg.theirs .msg-bubble {
  background: #fff; color: var(--ink-900); border: 1px solid var(--ice-200);
  border-bottom-left-radius: 6px; box-shadow: 0 4px 12px -8px rgba(13, 43, 69, .25);
}
.msg.mine .msg-bubble {
  color: #fff; border-bottom-right-radius: 6px;
  background: linear-gradient(135deg, var(--frost-500), var(--frost-700));
  box-shadow: 0 6px 14px -8px rgba(24, 95, 156, .8);
}
.msg-who { display: block; font-size: .72rem; font-weight: 600; color: var(--frost-600); margin-bottom: 2px; }
.msg-text { margin: 0; white-space: pre-wrap; }
.msg-time { display: block; margin-top: 2px; font-size: .68rem; text-align: right; opacity: .7; }
.msg.mine .msg-time { color: #dbeeff; }
.msg-bubble.has-img { padding: 4px 4px 6px; }
.msg-bubble.has-img .msg-text, .msg-bubble.has-img .msg-time, .msg-bubble.has-img .msg-who { padding: 0 8px; }
/* กว้างคงที่ → กรอบรูปมีขนาดก่อนรูปโหลด (ไม่งั้น lazy-load ไม่ทำงาน และข้อความกระโดด) */
.msg-img {
  display: block; width: 240px; max-width: 100%; padding: 0; border: 0; cursor: zoom-in; background: var(--ice-100);
  border-radius: 14px; overflow: hidden;
}
.msg-img img { display: block; width: 100%; min-height: 60px; max-height: 320px; object-fit: cover; aspect-ratio: 4 / 3; }
.msg-img:focus-visible { outline: 3px solid var(--frost-400); outline-offset: 2px; }
.msg.sending .msg-bubble { opacity: .65; }
.msg.failed .msg-bubble { opacity: .9; outline: 2px solid #f6b9b3; }
.msg-retry { border: 0; background: none; padding: 0; font: inherit; font-weight: 700; color: inherit; text-decoration: underline; cursor: pointer; }
.msg-day {
  align-self: center; margin: 10px 0 4px; padding: 2px 12px; border-radius: 999px;
  font-size: .72rem; font-weight: 600; color: var(--ink-500); background: rgba(255, 255, 255, .75); border: 1px solid var(--ice-200);
}
@keyframes msg-in { from { opacity: 0; transform: translateY(6px) scale(.98); } to { opacity: 1; transform: none; } }

/* ---------- กล่องพิมพ์ ---------- */
.chat-compose {
  display: flex; align-items: flex-end; gap: 8px; padding: 10px 12px;
  border-top: 1px solid var(--ice-200); background: rgba(255, 255, 255, .9);
}
.chat-compose textarea {
  flex: 1; min-width: 0; resize: none; max-height: 140px; overflow-y: hidden; padding: 10px 14px; border-radius: 20px;
  border: 1px solid var(--ice-200); background: var(--ice-50); outline: none;
  font: inherit; font-size: .95rem; line-height: 1.4; color: var(--ink-900);
  transition: border-color .15s, box-shadow .15s, background .15s;
}
.chat-compose textarea:focus { background: #fff; border-color: var(--frost-400); box-shadow: 0 0 0 3px rgba(107, 182, 238, .22); }
.chat-tool, .chat-send {
  flex: none; display: grid; place-items: center; width: 42px; height: 42px; border-radius: 50%;
  border: 0; cursor: pointer; transition: background .15s, transform .15s, filter .15s;
}
.chat-tool { color: var(--frost-600); background: var(--ice-100); }
.chat-tool:hover { background: var(--ice-200); }
.chat-send { color: #fff; background: linear-gradient(135deg, var(--frost-500), var(--frost-700)); box-shadow: 0 6px 14px -8px rgba(24, 95, 156, .9); }
.chat-send:hover { filter: brightness(1.08); transform: scale(1.04); }
.chat-tool svg, .chat-send svg { width: 20px; height: 20px; }
.chat-tool:focus-visible, .chat-send:focus-visible { outline: 3px solid var(--ice-300); outline-offset: 2px; }

.chat-chips { display: flex; gap: 6px; overflow-x: auto; padding: 4px 12px 10px; scrollbar-width: none; }
.chat-chips::-webkit-scrollbar { display: none; }
.chat-chip {
  flex: none; border: 1px solid var(--ice-300); background: #fff; color: var(--frost-700); cursor: pointer;
  font: inherit; font-size: .82rem; font-weight: 600; padding: 6px 12px; border-radius: 999px; white-space: nowrap;
  transition: background .15s, border-color .15s;
}
.chat-chip:hover { background: var(--ice-100); border-color: var(--frost-400); }

.chat-notice {
  margin: 0 12px 10px; padding: 9px 12px; border-radius: 12px; font-size: .85rem;
  color: #8a5a00; background: #fffaf0; border: 1px solid #f3d38a;
}
.chat-error {
  margin: 8px 12px 0; padding: 8px 12px; border-radius: 10px; font-size: .85rem;
  color: #9c3a32; background: #fff4f2; border: 1px solid #f6d3cd;
}

/* ---------- ดูรูปเต็ม ---------- */
.lightbox {
  padding: 0; border: 0; background: transparent; max-width: 94vw; max-height: 92vh; overflow: visible;
}
.lightbox::backdrop { background: rgba(6, 22, 38, .82); backdrop-filter: blur(4px); }
.lightbox img { display: block; max-width: 94vw; max-height: 92vh; border-radius: 12px; box-shadow: 0 30px 80px -20px #000; }
.lightbox-close {
  position: fixed; top: 16px; right: 16px; width: 42px; height: 42px; border-radius: 50%; border: 0; cursor: pointer;
  font-size: 1.5rem; color: #fff; background: rgba(255, 255, 255, .15);
}

/* ================= หน้าลูกค้า: ปุ่มลอย + กล่องแชท ================= */
.chat { position: fixed; right: 20px; bottom: 20px; z-index: 60; }
.chat-fab {
  position: relative; display: flex; align-items: center; gap: 10px; border: 0; cursor: pointer;
  padding: 12px 20px 12px 14px; border-radius: 999px; color: #fff; font: inherit; font-weight: 600;
  background:
    radial-gradient(120% 120% at 0% 0%, rgba(143, 208, 255, .6), transparent 50%),
    linear-gradient(135deg, var(--frost-500), var(--frost-700));
  box-shadow: 0 16px 34px -14px rgba(11, 58, 99, .8), inset 0 1px 0 rgba(255, 255, 255, .35);
  transition: transform .2s, box-shadow .2s;
}
.chat-fab:hover { transform: translateY(-2px); box-shadow: 0 20px 40px -14px rgba(11, 58, 99, .9); }
.chat-fab:focus-visible { outline: 3px solid var(--ice-300); outline-offset: 3px; }
.chat-fab-icon { display: grid; place-items: center; width: 34px; height: 34px; border-radius: 50%; background: rgba(255, 255, 255, .18); }
.chat-fab-icon svg { width: 20px; height: 20px; }
.chat-badge {
  position: absolute; top: -4px; right: -4px; min-width: 22px; height: 22px; padding: 0 6px; border-radius: 999px;
  display: grid; place-items: center; font-size: .75rem; font-weight: 700; color: #fff; background: #e0645c;
  box-shadow: 0 0 0 3px #fff; animation: badge-pop .3s ease;
}
@keyframes badge-pop { from { transform: scale(.4); } to { transform: scale(1); } }
.chat.open .chat-fab { display: none; }

.chat-panel {
  position: relative; width: 380px; height: min(620px, calc(100vh - 40px)); display: flex; flex-direction: column;
  border-radius: 24px; overflow: hidden;
  background:
    radial-gradient(500px 300px at 100% 0%, rgba(191, 226, 255, .7), transparent 60%),
    linear-gradient(180deg, #f6fbff, #e9f3fd);
  border: 1px solid rgba(255, 255, 255, .95);
  box-shadow: 0 30px 70px -24px rgba(11, 58, 99, .55);
  animation: panel-in .28s cubic-bezier(.2, .7, .3, 1);
}
@keyframes panel-in { from { opacity: 0; transform: translateY(16px) scale(.97); } to { opacity: 1; transform: none; } }

.chat-head {
  position: relative; display: flex; align-items: center; gap: 10px; padding: 14px 12px 14px 16px; color: #fff;
  background:
    radial-gradient(120% 140% at 100% 0%, rgba(143, 208, 255, .5), transparent 55%),
    linear-gradient(135deg, #2b86d0, var(--frost-900));
}
.chat-head-avatar {
  flex: none; display: grid; place-items: center; width: 40px; height: 40px; border-radius: 14px;
  background: rgba(255, 255, 255, .16); border: 1px solid rgba(255, 255, 255, .28);
}
.chat-head-avatar svg { width: 22px; height: 22px; }
.chat-head-text { flex: 1; min-width: 0; }
.chat-head-text b { display: block; font-family: var(--font-head); font-size: 1.02rem; }
.chat-head-text small { display: flex; align-items: center; gap: 6px; font-size: .78rem; opacity: .9; }
.chat-head-text small::before { content: ""; width: 7px; height: 7px; border-radius: 50%; background: #7ff0c9; }
.chat-head-btn {
  flex: none; display: grid; place-items: center; width: 34px; height: 34px; border-radius: 10px; border: 0; cursor: pointer;
  color: #fff; background: rgba(255, 255, 255, .12); font-size: 1.2rem; line-height: 1;
}
.chat-head-btn:hover { background: rgba(255, 255, 255, .22); }
.chat-head-btn:focus-visible { outline: 2px solid #fff; outline-offset: 1px; }
.chat-menu {
  position: absolute; top: 56px; right: 52px; z-index: 2; display: grid; min-width: 210px; padding: 6px; border-radius: 14px;
  background: #fff; border: 1px solid var(--ice-200); box-shadow: 0 16px 34px -14px rgba(13, 43, 69, .5);
}
.chat-menu button {
  border: 0; background: none; text-align: left; cursor: pointer; padding: 9px 12px; border-radius: 10px;
  font: inherit; font-size: .9rem; color: var(--ink-900);
}
.chat-menu button:hover { background: var(--ice-50); }

.chat-body { flex: 1; min-height: 0; display: flex; flex-direction: column; }
.chat-body .chat-msgs { flex: 1; }

/* หน้าตั้งชื่อเล่น */
.chat-intro { flex: 1; overflow-y: auto; padding: 22px 22px 16px; text-align: center; }
.chat-intro-art {
  width: 74px; height: 74px; margin: 6px auto 14px; border-radius: 24px; display: grid; place-items: center;
  font-size: 2rem; color: #fff; background: linear-gradient(135deg, #8fd0ff, var(--frost-600));
  box-shadow: 0 14px 28px -12px rgba(33, 121, 196, .9); animation: spin 18s linear infinite;
}
.chat-intro h3 { font-size: 1.25rem; margin-bottom: 6px; }
.chat-intro > p { margin: 0 0 18px; color: var(--ink-500); font-size: .92rem; }
.chat-intro form { display: grid; gap: 10px; text-align: left; }
.chat-intro label { display: grid; gap: 4px; font-size: .82rem; font-weight: 600; color: var(--ink-700); }
.chat-intro input {
  font: inherit; font-size: 1rem; padding: 11px 14px; border-radius: 14px; border: 1px solid var(--ice-200); background: #fff; outline: none;
}
.chat-intro input:focus { border-color: var(--frost-400); box-shadow: 0 0 0 3px rgba(107, 182, 238, .22); }
.chat-intro .btn-frost { margin-top: 0; }
.chat-link { margin-top: 12px; border: 0; background: none; cursor: pointer; font: inherit; font-size: .85rem; color: var(--frost-600); text-decoration: underline; }
.chat-note { margin: 16px 0 0; font-size: .78rem; color: var(--ink-500); }
.chat-restore { margin-top: 10px; }

.chat-drop {
  position: absolute; inset: 0; z-index: 3; display: grid; place-items: center; font-weight: 600; color: var(--frost-700);
  background: rgba(230, 242, 252, .92); border: 3px dashed var(--frost-400); border-radius: 24px; pointer-events: none;
}

/* กล่องย่อย (เปลี่ยนชื่อ / รหัสแชท) */
.chat-dialog {
  width: min(360px, calc(100vw - 32px)); padding: 22px; border: 0; border-radius: 20px;
  color: var(--ink-900); background: #fff; box-shadow: 0 30px 70px -20px rgba(13, 43, 69, .5);
}
.chat-dialog::backdrop { background: rgba(13, 43, 69, .3); backdrop-filter: blur(4px); }
.chat-dialog h3 { font-size: 1.1rem; margin-bottom: 8px; }
.chat-dialog p { margin: 0 0 12px; font-size: .88rem; color: var(--ink-500); }
.chat-dialog input, .chat-dialog .chat-code {
  width: 100%; font: inherit; padding: 10px 12px; border-radius: 12px; border: 1px solid var(--ice-200); background: var(--ice-50);
}
.chat-dialog .chat-code { font-family: var(--font-mono); font-size: .85rem; word-break: break-all; user-select: all; }
.chat-dialog-actions { display: flex; justify-content: flex-end; gap: 8px; margin-top: 14px; }
.chat-dialog-actions button {
  border: 0; cursor: pointer; font: inherit; font-weight: 600; padding: 9px 16px; border-radius: 12px;
  color: var(--frost-700); background: var(--ice-100);
}
.chat-dialog-actions button.primary { color: #fff; background: linear-gradient(135deg, var(--frost-500), var(--frost-700)); }
.chat-warn { padding: 8px 10px; border-radius: 10px; background: #fffaf0; border: 1px solid #f3d38a; color: #8a5a00 !important; }

/* ปุ่มแชทในหน้าออเดอร์ */
.ov-chat {
  display: inline-flex; align-items: center; gap: 6px; border: 0; cursor: pointer; font: inherit; font-weight: 600; font-size: .88rem;
  color: #fff; padding: 8px 14px; border-radius: 12px; background: linear-gradient(135deg, var(--frost-500), var(--frost-700));
}
.ov-chat svg { width: 16px; height: 16px; }

@media (max-width: 560px) {
  .chat { right: 14px; bottom: 14px; }
  .chat-fab-label { display: none; }
  .chat-fab { padding: 12px; }
  .chat.open { inset: 0; right: 0; bottom: 0; }
  .chat.open .chat-panel { width: 100%; height: 100%; border-radius: 0; border: 0; }
  body.chat-open { overflow: hidden; } /* มือถือ: หน้าเว็บข้างหลังไม่เลื่อนตาม */
  .msg-bubble { max-width: 86%; }
}
@media (prefers-reduced-motion: reduce) {
  .chat-intro-art { animation: none; }
}
