/* ==========================================================================
   TT Shop Live Chat — user chat, admin chat center, floating widget
   UI/mock only — see js/chat.js for the localStorage-backed ChatStore.
   ========================================================================== */

/* ---------- Chat page shell (pages/chat.php, pages/admin_chat.php) ---------- */
.chat-page {
  height: 100vh;
  height: 100dvh;
  display: flex;
  flex-direction: column;
  background: var(--surface);
}
.chat-page-topbar {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 12px 16px;
  padding-top: calc(12px + env(safe-area-inset-top, 0px));
  border-bottom: 1px solid var(--border);
  flex-shrink: 0;
}
.chat-page-topbar .navbar-logo { font-size: 1.15rem; }
.chat-page-topbar-title { font-size: 1rem; font-weight: 600; flex: 1; }
.chat-body {
  flex: 1;
  min-height: 0;
  display: flex;
  overflow: hidden;
}

/* ---------- Conversation list panel ---------- */
.conv-panel {
  width: 100%;
  display: flex;
  flex-direction: column;
  min-height: 0;
  border-right: 1px solid var(--border);
}
.conv-panel-header { padding: 14px 16px; flex-shrink: 0; }
.conv-panel-header h1 { font-size: 1.2rem; margin-bottom: 12px; }
.conv-search { position: relative; }
.conv-search input {
  width: 100%;
  height: 42px;
  border-radius: var(--radius-pill);
  border: 1.5px solid var(--border);
  background: var(--background);
  padding: 0 16px 0 40px;
}
.conv-search .search-icon { position: absolute; left: 16px; top: 50%; transform: translateY(-50%); color: var(--text-faint); font-size: 0.9rem; }

.conv-filter-tabs { display: flex; gap: 6px; padding: 0 16px 12px; overflow-x: auto; scrollbar-width: none; flex-shrink: 0; }
.conv-filter-tabs::-webkit-scrollbar { display: none; }
.conv-filter-tabs .toolbar-btn { flex-shrink: 0; }

.conv-list { flex: 1; overflow-y: auto; }
.conv-item {
  display: flex;
  align-items: flex-start;
  gap: 12px;
  padding: 12px 16px;
  width: 100%;
  text-align: left;
  border-bottom: 1px solid var(--border);
  position: relative;
  transition: background var(--dur-fast) var(--ease);
}
.conv-item:hover, .conv-item:focus-visible { background: var(--background); }
.conv-item.active { background: var(--primary-light); }
.conv-avatar-wrap { position: relative; flex-shrink: 0; }
.conv-avatar-wrap img { width: 48px; height: 48px; border-radius: 50%; object-fit: cover; }
.conv-online-dot {
  position: absolute; bottom: -1px; right: -1px;
  width: 12px; height: 12px; border-radius: 50%;
  background: var(--text-faint); border: 2px solid var(--surface);
}
.conv-online-dot.online { background: var(--success); }
.conv-main { flex: 1; min-width: 0; }
.conv-top-row { display: flex; align-items: baseline; justify-content: space-between; gap: 8px; }
.conv-name { font-size: 0.9rem; font-weight: 600; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; display: flex; align-items: center; gap: 5px; }
.conv-name i.fa-thumbtack { font-size: 0.7rem; color: var(--text-faint); }
.conv-time { font-size: 0.7rem; color: var(--text-faint); flex-shrink: 0; }
.conv-bottom-row { display: flex; align-items: center; justify-content: space-between; gap: 8px; margin-top: 2px; }
.conv-preview { font-size: 0.8rem; color: var(--text-muted); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; flex: 1; }
.conv-item.has-unread .conv-preview { color: var(--text); font-weight: 500; }
.conv-badges { display: flex; align-items: center; gap: 5px; flex-shrink: 0; }
.conv-unread-badge {
  min-width: 20px; height: 20px; padding: 0 6px; border-radius: var(--radius-pill);
  background: var(--primary); color: #fff; font-size: 0.68rem; font-weight: 700;
  display: flex; align-items: center; justify-content: center;
}
.conv-topic-tag { font-size: 0.66rem; color: var(--text-faint); margin-top: 3px; display: block; }
.conv-priority-dot { width: 7px; height: 7px; border-radius: 50%; flex-shrink: 0; }
.conv-priority-dot.high { background: var(--warning); }
.conv-priority-dot.urgent { background: var(--danger); }

/* ---------- Chat thread panel ---------- */
.thread-panel {
  width: 100%;
  display: flex;
  flex-direction: column;
  min-height: 0;
  min-width: 0;
}
.thread-header {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 10px 16px;
  border-bottom: 1px solid var(--border);
  flex-shrink: 0;
}
.thread-back-btn { display: inline-flex; flex-shrink: 0; }
.thread-header img { width: 40px; height: 40px; border-radius: 50%; object-fit: cover; }
.thread-header-info { flex: 1; min-width: 0; }
.thread-header-name { font-size: 0.94rem; font-weight: 600; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.thread-header-status { font-size: 0.74rem; color: var(--text-faint); display: flex; align-items: center; gap: 5px; }
.thread-header-status .dot { width: 7px; height: 7px; border-radius: 50%; background: var(--text-faint); }
.thread-header-status .dot.online { background: var(--success); }
.thread-header-actions { display: flex; align-items: center; gap: 4px; flex-shrink: 0; }

.thread-messages {
  flex: 1;
  overflow-y: auto;
  padding: 16px;
  display: flex;
  flex-direction: column;
  gap: 4px;
  position: relative;
}
.date-separator { display: flex; align-items: center; gap: 10px; margin: 14px 0; color: var(--text-faint); font-size: 0.72rem; }
.date-separator::before, .date-separator::after { content: ''; flex: 1; height: 1px; background: var(--border); }

.msg-row { display: flex; gap: 8px; max-width: 82%; margin-bottom: 6px; }
.msg-row.from-user { align-self: flex-end; flex-direction: row-reverse; }
.msg-row.from-admin { align-self: flex-start; }
.msg-row-avatar { width: 26px; height: 26px; border-radius: 50%; object-fit: cover; flex-shrink: 0; align-self: flex-end; visibility: hidden; }
.msg-row.show-avatar .msg-row-avatar { visibility: visible; }
.msg-bubble-col { display: flex; flex-direction: column; min-width: 0; }
.msg-row.from-user .msg-bubble-col { align-items: flex-end; }
.msg-row.from-admin .msg-bubble-col { align-items: flex-start; }

.msg-bubble {
  padding: 10px 14px;
  border-radius: 16px;
  font-size: 0.86rem;
  line-height: 1.5;
  word-break: break-word;
  white-space: pre-line;
  position: relative;
}
.msg-row.from-user .msg-bubble { background: var(--primary); color: #fff; border-bottom-right-radius: 4px; }
.msg-row.from-admin .msg-bubble { background: var(--background); color: var(--text); border-bottom-left-radius: 4px; }
.msg-bubble.is-note { background: #fff2d9; color: #7a4c00; border: 1px dashed #d4a017; font-size: 0.8rem; }
.msg-meta { display: flex; align-items: center; gap: 5px; margin-top: 3px; font-size: 0.68rem; color: var(--text-faint); }
.msg-row.from-user .msg-meta { justify-content: flex-end; }
.msg-status-icon.read { color: #2563eb; }
.msg-status-icon.failed { color: var(--danger); }

.msg-bubble img.msg-image { max-width: 200px; border-radius: 12px; display: block; cursor: zoom-in; }
.msg-file-chip { display: flex; align-items: center; gap: 10px; }
.msg-file-chip i { font-size: 1.3rem; }
.msg-file-chip .file-meta { font-size: 0.76rem; opacity: 0.85; }

.chat-card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius-md);
  overflow: hidden;
  width: 220px;
  max-width: 100%;
}
.chat-card img { width: 100%; aspect-ratio: 4/3; object-fit: cover; }
.chat-card-body { padding: 10px 12px; }
.chat-card-title { font-size: 0.82rem; font-weight: 600; margin-bottom: 4px; }
.chat-card-sub { font-size: 0.78rem; color: var(--text-muted); margin-bottom: 8px; }
.chat-card-price { font-weight: 700; color: var(--primary); font-size: 0.9rem; }
.chat-card .btn { width: 100%; margin-top: 8px; }

.typing-row { display: flex; align-items: center; gap: 8px; padding: 4px 0; }
.typing-dots { display: inline-flex; gap: 3px; background: var(--background); padding: 10px 14px; border-radius: 16px; }
.typing-dots span { width: 6px; height: 6px; border-radius: 50%; background: var(--text-faint); animation: typingBounce 1.2s ease infinite; }
.typing-dots span:nth-child(2) { animation-delay: 0.15s; }
.typing-dots span:nth-child(3) { animation-delay: 0.3s; }
@keyframes typingBounce { 0%, 60%, 100% { transform: translateY(0); opacity: 0.5; } 30% { transform: translateY(-4px); opacity: 1; } }
.typing-label { font-size: 0.74rem; color: var(--text-faint); }

.new-messages-pill {
  position: sticky;
  bottom: 8px;
  align-self: center;
  background: var(--secondary);
  color: #fff;
  font-size: 0.76rem;
  font-weight: 600;
  padding: 7px 16px;
  border-radius: var(--radius-pill);
  box-shadow: var(--shadow-md);
  display: flex;
  align-items: center;
  gap: 6px;
  z-index: 2;
}

/* ---------- Quick replies ---------- */
.quick-replies { display: flex; gap: 8px; padding: 8px 16px; overflow-x: auto; scrollbar-width: none; flex-shrink: 0; border-top: 1px solid var(--border); }
.quick-replies::-webkit-scrollbar { display: none; }
.quick-reply-chip { flex-shrink: 0; padding: 8px 14px; border-radius: var(--radius-pill); border: 1.5px solid var(--border); font-size: 0.78rem; white-space: nowrap; }
.quick-reply-chip:hover { border-color: var(--primary); color: var(--primary); }

/* ---------- Real FAQ area (js/chat.js's renderFaqArea) ---------- */
.faq-area { display: flex; flex-direction: column; gap: 8px; width: 100%; min-width: 0; }
.faq-toggle-btn {
  display: flex;
  align-items: center;
  gap: 6px;
  align-self: flex-start;
  padding: 6px 4px;
  font-size: 0.78rem;
  font-weight: 600;
  color: var(--text-muted);
  min-height: 32px;
}
.faq-toggle-btn:hover { color: var(--primary); }
.faq-toggle-btn i.fa-chevron-up, .faq-toggle-btn i.fa-chevron-down { font-size: 0.68rem; opacity: 0.7; }
.faq-chip-row { display: flex; flex-wrap: wrap; gap: 8px; }

/* ---------- Thread greeting (shown only alongside the empty state — never a persisted message, no avatar/bubble, can't be mistaken for a real bot reply) ---------- */
.chat-thread-greeting { text-align: center; padding: 28px 16px 8px; }
.chat-thread-greeting p:first-child { font-size: 1.1rem; font-weight: 600; margin-bottom: 6px; }
.chat-thread-greeting p:last-child { font-size: 0.84rem; color: var(--text-muted); }

/* ---------- Handoff-to-human button (only ever rendered under the real fallback bot message) ---------- */
.chat-handoff-btn { margin-top: 8px; }

/* ---------- Reply preview ---------- */
.reply-preview {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 8px 16px;
  background: var(--background);
  border-top: 1px solid var(--border);
  font-size: 0.78rem;
}
.reply-preview-bar { width: 3px; align-self: stretch; background: var(--primary); border-radius: 2px; flex-shrink: 0; }
.reply-preview-text { flex: 1; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; color: var(--text-muted); }
.reply-preview-text strong { color: var(--text); }

/* ---------- Input bar ---------- */
.chat-input-bar {
  display: flex;
  align-items: flex-end;
  gap: 6px;
  padding: 10px 12px;
  padding-bottom: calc(10px + env(safe-area-inset-bottom, 0px));
  border-top: 1px solid var(--border);
  flex-shrink: 0;
  position: relative;
}
.chat-input-bar .icon-btn { flex-shrink: 0; }
.chat-input-field-wrap { flex: 1; min-width: 0; }
.chat-input-field {
  width: 100%;
  max-height: 100px;
  min-height: 40px;
  border-radius: 20px;
  border: 1.5px solid var(--border);
  background: var(--background);
  padding: 9px 16px;
  font-size: 0.88rem;
  resize: none;
  line-height: 1.4;
}
.chat-input-field:focus { border-color: var(--primary); background: var(--surface); }
.chat-send-btn {
  width: 40px; height: 40px; border-radius: 50%;
  background: var(--primary); color: #fff;
  display: flex; align-items: center; justify-content: center;
  flex-shrink: 0; transition: transform var(--dur-fast) var(--ease), opacity var(--dur-fast) var(--ease);
}
.chat-send-btn:disabled { opacity: 0.4; }
.chat-send-btn:active:not(:disabled) { transform: scale(0.9); }

.chat-mode-toggle { display: flex; gap: 6px; padding: 8px 16px 0; }
.chat-mode-btn { flex: 1; padding: 8px; border-radius: var(--radius-sm); font-size: 0.78rem; font-weight: 600; background: var(--background); color: var(--text-muted); text-align: center; }
.chat-mode-btn.active { background: var(--primary); color: #fff; }
.chat-mode-btn.active.note-mode { background: #d4a017; }

/* ---------- Attach menu ---------- */
.attach-menu {
  position: absolute;
  bottom: calc(100% + 8px);
  left: 12px;
  background: var(--surface);
  border-radius: var(--radius-md);
  box-shadow: var(--shadow-lg);
  border: 1px solid var(--border);
  padding: 8px;
  display: grid;
  grid-template-columns: repeat(2, 1fr);
  gap: 4px;
  width: 200px;
  z-index: 20;
}
.attach-menu-item { display: flex; align-items: center; gap: 8px; padding: 10px; border-radius: var(--radius-sm); font-size: 0.78rem; }
.attach-menu-item:hover { background: var(--background); }
.attach-menu-item i { width: 30px; height: 30px; border-radius: 50%; background: var(--primary-light); color: var(--primary); display: flex; align-items: center; justify-content: center; font-size: 0.85rem; }

/* ---------- Emoji picker ---------- */
.emoji-picker {
  position: absolute;
  bottom: calc(100% + 8px);
  left: 12px;
  background: var(--surface);
  border-radius: var(--radius-md);
  box-shadow: var(--shadow-lg);
  border: 1px solid var(--border);
  padding: 10px;
  display: grid;
  grid-template-columns: repeat(6, 1fr);
  gap: 2px;
  z-index: 20;
}
.emoji-picker button { font-size: 1.3rem; padding: 6px; border-radius: var(--radius-sm); }
.emoji-picker button:hover { background: var(--background); }

/* ---------- Attachment preview (before send) ---------- */
.attachment-preview-bar { display: flex; align-items: center; gap: 10px; padding: 10px 16px; border-top: 1px solid var(--border); background: var(--background); }
.attachment-preview-bar img { width: 52px; height: 52px; border-radius: var(--radius-sm); object-fit: cover; }
.attachment-preview-bar .file-chip { display: flex; align-items: center; gap: 8px; font-size: 0.8rem; }
.attachment-preview-bar .info { flex: 1; font-size: 0.8rem; color: var(--text-muted); }

/* ---------- Message actions (copy/reply/note) ---------- */
.msg-action-menu {
  position: absolute;
  background: var(--surface);
  border-radius: var(--radius-sm);
  box-shadow: var(--shadow-lg);
  border: 1px solid var(--border);
  display: flex;
  overflow: hidden;
  z-index: 30;
}
.msg-action-menu button { padding: 9px 14px; font-size: 0.78rem; display: flex; align-items: center; gap: 6px; white-space: nowrap; }
.msg-action-menu button:hover { background: var(--background); }
.msg-action-menu button + button { border-left: 1px solid var(--border); }

/* ---------- Empty / offline / maintenance states ---------- */
.chat-empty-state, .chat-offline-state, .chat-maintenance-state {
  flex: 1; display: flex; flex-direction: column; align-items: center; justify-content: center;
  text-align: center; padding: 32px; gap: 6px;
}
.chat-empty-state .icon, .chat-offline-state .icon, .chat-maintenance-state .icon {
  width: 72px; height: 72px; border-radius: 50%; background: var(--background); color: var(--text-faint);
  display: flex; align-items: center; justify-content: center; font-size: 1.8rem; margin-bottom: 10px;
}
.chat-offline-state .icon { background: #fff2d9; color: #a15400; }
.chat-empty-state h3, .chat-offline-state h3, .chat-maintenance-state h3 { font-size: 1rem; }
.chat-empty-state p, .chat-offline-state p, .chat-maintenance-state p { color: var(--text-muted); font-size: 0.85rem; max-width: 300px; }

/* ---------- Floating chat widget ---------- */
.chat-fab {
  position: fixed;
  right: 20px;
  bottom: calc(20px + var(--bottom-nav-h) + env(safe-area-inset-bottom, 0px));
  width: 56px;
  height: 56px;
  border-radius: 50%;
  background: var(--primary);
  color: #fff;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 1.4rem;
  box-shadow: var(--shadow-lg);
  z-index: 150;
  transition: transform var(--dur-fast) var(--ease);
}
.chat-fab:hover { transform: scale(1.06); }
.chat-fab.has-unread { animation: fabPulse 1.8s ease infinite; }
@keyframes fabPulse { 0%, 100% { box-shadow: var(--shadow-lg), 0 0 0 0 rgba(255,59,92,0.5); } 50% { box-shadow: var(--shadow-lg), 0 0 0 10px rgba(255,59,92,0); } }
.chat-fab-badge {
  position: absolute; top: -4px; right: -4px;
  min-width: 22px; height: 22px; padding: 0 5px; border-radius: var(--radius-pill);
  background: var(--danger); color: #fff; font-size: 0.7rem; font-weight: 700;
  display: flex; align-items: center; justify-content: center; border: 2px solid var(--surface);
}

.chat-widget-panel {
  position: fixed;
  right: 20px;
  bottom: 88px;
  width: 380px;
  height: 560px;
  max-height: calc(100vh - 120px);
  background: var(--surface);
  border-radius: var(--radius-lg);
  box-shadow: var(--shadow-lg);
  z-index: 149;
  display: flex;
  flex-direction: column;
  overflow: hidden;
  transform: translateY(16px) scale(0.97);
  opacity: 0;
  visibility: hidden;
  transition: transform var(--dur-base) var(--ease), opacity var(--dur-base) var(--ease), visibility var(--dur-base);
}
.chat-widget-panel.open { transform: translateY(0) scale(1); opacity: 1; visibility: visible; }
.chat-widget-header {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 14px 16px;
  background: linear-gradient(135deg, var(--primary), var(--accent));
  color: #fff;
  flex-shrink: 0;
}
.chat-widget-header img { width: 36px; height: 36px; border-radius: 50%; object-fit: cover; border: 2px solid rgba(255,255,255,0.5); }
.chat-widget-header-info { flex: 1; min-width: 0; }
.chat-widget-header-info .name { font-size: 0.9rem; font-weight: 600; }
.chat-widget-header-info .status { font-size: 0.72rem; opacity: 0.9; display: flex; align-items: center; gap: 5px; }
.chat-widget-header-info .status .dot { width: 7px; height: 7px; border-radius: 50%; background: rgba(255,255,255,0.55); flex-shrink: 0; }
.chat-widget-header-info .status .dot.online { background: #4ade80; }
.chat-widget-header .icon-btn { color: #fff; }
.chat-widget-header .icon-btn:hover { background: rgba(255,255,255,0.15); }
.chat-widget-view-all { display: block; text-align: center; padding: 10px; font-size: 0.78rem; font-weight: 600; color: var(--primary); border-top: 1px solid var(--border); flex-shrink: 0; }

@media (max-width: 640px) {
  .chat-widget-panel {
    right: 0;
    bottom: 0;
    left: 0;
    top: 0;
    width: 100%;
    height: 100%;
    max-height: none;
    border-radius: 0;
    transform: translateY(24px);
  }
  .chat-widget-panel.open { transform: translateY(0); }
  .chat-widget-header { padding-top: calc(14px + env(safe-area-inset-top, 0px)); }
  .chat-fab { right: 16px; }
}

/* ---------- Responsive: two-panel -> stacked on mobile ---------- */
.chat-body[data-active-view="list"] .thread-panel { display: none; }
.chat-body[data-active-view="thread"] .conv-panel { display: none; }
.thread-back-btn { display: inline-flex; }

/* ---------- Admin: 3rd context panel (mobile: off-canvas drawer) ---------- */
.context-panel-backdrop {
  position: fixed;
  inset: 0;
  background: rgba(15, 15, 18, 0.45);
  z-index: 199;
  opacity: 0;
  visibility: hidden;
  transition: opacity var(--dur-base) var(--ease), visibility var(--dur-base);
}
.context-panel-backdrop.open { opacity: 1; visibility: visible; }
.context-panel {
  position: fixed;
  top: 0;
  right: 0;
  bottom: 0;
  width: 85%;
  max-width: 340px;
  background: var(--surface);
  z-index: 200;
  display: flex;
  flex-direction: column;
  overflow-y: auto;
  padding: 18px;
  gap: 16px;
  transform: translateX(100%);
  transition: transform var(--dur-base) var(--ease);
  box-shadow: var(--shadow-lg);
  padding-top: calc(18px + env(safe-area-inset-top, 0px));
}
.context-panel.open { transform: translateX(0); }
.context-panel-close {
  display: inline-flex;
  align-self: flex-end;
  margin-bottom: -8px;
}
.context-profile { text-align: center; }
.context-profile img { width: 68px; height: 68px; border-radius: 50%; object-fit: cover; margin: 0 auto 10px; }
.context-profile h3 { font-size: 1rem; margin-bottom: 4px; }
.context-section { border-top: 1px solid var(--border); padding-top: 14px; }
.context-section h4 { font-size: 0.78rem; text-transform: uppercase; letter-spacing: 0.04em; color: var(--text-faint); margin-bottom: 10px; }
.context-stat-row { display: flex; justify-content: space-between; font-size: 0.85rem; padding: 6px 0; }
.context-stat-row span:first-child { color: var(--text-muted); }
.context-stat-row span:last-child { font-weight: 600; }
.context-order-item { display: flex; justify-content: space-between; align-items: center; padding: 8px 0; font-size: 0.8rem; border-bottom: 1px solid var(--border); }
.context-order-item:last-child { border-bottom: none; }

.admin-toolbar { display: flex; align-items: center; gap: 8px; padding: 10px 16px; border-bottom: 1px solid var(--border); flex-wrap: wrap; }
.admin-toolbar .toolbar-btn { flex-shrink: 0; }
.admin-toolbar-spacer { flex: 1; }
.admin-context-toggle { display: none; }

/* Priority / status pills reuse .status-badge tones from seller.css */
.priority-urgent-flag { display: inline-flex; align-items: center; gap: 4px; font-size: 0.68rem; font-weight: 700; color: var(--danger); }
