/* ═══════════════════════════════════════════════════════════
   chatbot/chatbot.css — BPLPT Library Chatbot
   FIX: Header visibility + RTL positioning + overflow issues
   ═══════════════════════════════════════════════════════════ */

@import url('https://fonts.googleapis.com/css2?family=Tajawal:wght@300;400;500;700&display=swap');

/* ── CSS Variables ─────────────────────────────────────────── */
:root {
  --chat-accent: #1a6b9a;
  --chat-accent-dark: #145880;
  --chat-accent-light: #e8f4fd;
  --chat-gold: #c8962a;
  --chat-bg: #ffffff;
  --chat-surface: #f5f7fa;
  --chat-border: rgba(0, 0, 0, 0.08);
  --chat-text: #2d3748;
  --chat-text-muted: #718096;
  --chat-bot-bg: #f0f4f8;
  --chat-user-bg: linear-gradient(135deg, #1a6b9a, #145880);
  --chat-user-text: #ffffff;
  --chat-shadow: 0 8px 40px rgba(0, 0, 0, 0.18), 0 2px 12px rgba(0, 0, 0, 0.10);
  --chat-btn-shadow: 0 4px 20px rgba(26, 107, 154, 0.45);
  --chat-radius: 16px;
  --chat-font: 'Tajawal', 'Segoe UI', system-ui, sans-serif;
  --chat-z: 99999;
  --chat-width: 370px;
  --chat-height: 540px;
}

/* ═══════════════════════════════════════════════════════════
   FLOATING BUTTON
   ═══════════════════════════════════════════════════════════ */

.bplpt-chat-fab {
  position: fixed;
  bottom: 24px;
  right: 24px;
  /* RTL default: bottom-right */
  left: auto;
  width: 58px;
  height: 58px;
  border-radius: 50%;
  background: linear-gradient(135deg, var(--chat-accent), #0f4c75);
  color: #fff;
  border: none;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 1.45rem;
  box-shadow: var(--chat-btn-shadow);
  z-index: var(--chat-z);
  transition: transform 0.25s cubic-bezier(.34, 1.56, .64, 1), box-shadow 0.25s ease;
  animation: bplpt-fab-enter 0.45s cubic-bezier(.34, 1.56, .64, 1) both;
}

/* LTR: move to bottom-left */
body:not([data-dir="rtl"]) .bplpt-chat-fab {
  right: auto;
  left: 24px;
}

.bplpt-chat-fab:hover {
  transform: scale(1.1) translateY(-2px);
  box-shadow: 0 8px 28px rgba(26, 107, 154, 0.55);
}

.bplpt-chat-fab:active {
  transform: scale(0.95);
}

.bplpt-chat-fab::before {
  content: '';
  position: absolute;
  inset: -5px;
  border-radius: 50%;
  border: 2px solid rgba(26, 107, 154, 0.35);
  animation: bplpt-pulse 2.5s ease-in-out infinite;
}

@keyframes bplpt-pulse {

  0%,
  100% {
    transform: scale(1);
    opacity: .7;
  }

  50% {
    transform: scale(1.18);
    opacity: 0;
  }
}

@keyframes bplpt-fab-enter {
  from {
    transform: scale(0) rotate(-30deg);
    opacity: 0;
  }

  to {
    transform: scale(1) rotate(0deg);
    opacity: 1;
  }
}

/* Unread badge */
.bplpt-chat-badge {
  position: absolute;
  top: -3px;
  left: -3px;
  min-width: 20px;
  height: 20px;
  border-radius: 10px;
  background: #e53e3e;
  color: #fff;
  font-size: 0.65rem;
  font-weight: 700;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 0 4px;
  border: 2px solid #fff;
  animation: bplpt-badge-pop 0.3s cubic-bezier(.34, 1.56, .64, 1);
}

body:not([data-dir="rtl"]) .bplpt-chat-badge {
  left: auto;
  right: -3px;
}

@keyframes bplpt-badge-pop {
  from {
    transform: scale(0);
  }

  to {
    transform: scale(1);
  }
}

/* ═══════════════════════════════════════════════════════════
   CHAT WINDOW
   ═══════════════════════════════════════════════════════════ */

.bplpt-chat-window {
  position: fixed;
  bottom: 94px;
  /* above the FAB */
  right: 24px;
  /* RTL default */
  left: auto;
  width: var(--chat-width);
  height: var(--chat-height);
  max-height: calc(100dvh - 110px);
  /* never overflow screen */
  background: var(--chat-bg);
  border-radius: var(--chat-radius);
  box-shadow: var(--chat-shadow);
  z-index: var(--chat-z);
  display: flex;
  flex-direction: column;
  overflow: hidden;
  font-family: var(--chat-font);
  border: 1px solid var(--chat-border);

  /* ★ KEY FIX: scale from bottom-right for RTL */
  transform-origin: bottom right;
  transform: scale(0.85) translateY(16px);
  opacity: 0;
  pointer-events: none;
  transition: transform 0.28s cubic-bezier(.34, 1.56, .64, 1),
    opacity 0.22s ease;
  will-change: transform, opacity;
}

/* LTR: position bottom-left */
body:not([data-dir="rtl"]) .bplpt-chat-window {
  right: auto;
  left: 24px;
  transform-origin: bottom left;
}

.bplpt-chat-window.bplpt-chat-open {
  transform: scale(1) translateY(0);
  opacity: 1;
  pointer-events: all;
}

.bplpt-chat-window.bplpt-chat-minimized {
  height: 68px !important;
  min-height: 68px;
  overflow: hidden;
}

/* ── Header ── ★ FIX: explicit sizing so it's never clipped */

.bplpt-chat-header {
  background: linear-gradient(135deg, #0f4c75 0%, var(--chat-accent) 60%, #1e8bc3 100%);
  color: #fff;
  padding: 13px 14px;
  display: flex;
  align-items: center;
  gap: 10px;
  flex-shrink: 0;
  /* ★ never shrink */
  min-height: 68px;
  /* ★ always visible */
  position: relative;
  overflow: visible;
  /* ★ don't clip content */
  z-index: 2;
  box-sizing: border-box;
}

.bplpt-chat-header::after {
  content: '';
  position: absolute;
  inset: 0;
  background: url("data:image/svg+xml,%3Csvg width='40' height='40' viewBox='0 0 40 40' xmlns='http://www.w3.org/2000/svg'%3E%3Ccircle cx='20' cy='20' r='20' fill='%23fff' fill-opacity='0.03'/%3E%3C/svg%3E");
  pointer-events: none;
}

.bplpt-chat-avatar {
  width: 42px;
  height: 42px;
  border-radius: 50%;
  background: rgba(255, 255, 255, 0.15);
  border: 2px solid rgba(255, 255, 255, 0.3);
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 1.2rem;
  flex-shrink: 0;
}

.bplpt-chat-header-info {
  flex: 1;
  min-width: 0;
}

.bplpt-chat-title {
  font-size: 0.92rem;
  font-weight: 700;
  margin: 0 0 2px;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.bplpt-chat-status {
  font-size: 0.70rem;
  opacity: 0.88;
  display: flex;
  align-items: center;
  gap: 5px;
}

.bplpt-chat-status-dot {
  width: 7px;
  height: 7px;
  border-radius: 50%;
  background: #48bb78;
  box-shadow: 0 0 0 2px rgba(72, 187, 120, 0.3);
  animation: bplpt-blink 2s ease-in-out infinite;
  flex-shrink: 0;
}

@keyframes bplpt-blink {

  0%,
  100% {
    opacity: 1;
  }

  50% {
    opacity: 0.35;
  }
}

.bplpt-chat-header-actions {
  display: flex;
  gap: 4px;
  flex-shrink: 0;
  align-items: center;
}

.bplpt-chat-btn-icon {
  width: 30px;
  height: 30px;
  border-radius: 8px;
  background: rgba(255, 255, 255, 0.12);
  border: none;
  color: #fff;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 0.78rem;
  transition: background 0.18s;
  flex-shrink: 0;
}

.bplpt-chat-btn-icon:hover {
  background: rgba(255, 255, 255, 0.25);
}

/* ── Messages Area ─────────────────────────────────────────── */

.bplpt-chat-messages {
  flex: 1;
  overflow-y: auto;
  overflow-x: hidden;
  padding: 14px 12px;
  display: flex;
  flex-direction: column;
  gap: 10px;
  background: #fafbfd;
  scrollbar-width: thin;
  scrollbar-color: rgba(0, 0, 0, 0.12) transparent;
  min-height: 0;
  /* ★ allow flex shrink */
}

.bplpt-chat-messages::-webkit-scrollbar {
  width: 4px;
}

.bplpt-chat-messages::-webkit-scrollbar-thumb {
  background: rgba(0, 0, 0, 0.12);
  border-radius: 2px;
}

/* ── Message Bubbles ───────────────────────────────────────── */

.bplpt-chat-msg {
  display: flex;
  align-items: flex-end;
  gap: 7px;
  animation: bplpt-msg-in 0.22s ease both;
  max-width: 100%;
}

@keyframes bplpt-msg-in {
  from {
    opacity: 0;
    transform: translateY(6px);
  }

  to {
    opacity: 1;
    transform: translateY(0);
  }
}

/* Bot: icon left in LTR, right in RTL */
.bplpt-chat-msg.bplpt-msg-bot {
  flex-direction: row;
}

.bplpt-chat-msg.bplpt-msg-user {
  flex-direction: row-reverse;
}

[dir="rtl"] .bplpt-chat-msg.bplpt-msg-bot {
  flex-direction: row-reverse;
}

[dir="rtl"] .bplpt-chat-msg.bplpt-msg-user {
  flex-direction: row;
}

.bplpt-chat-msg-avatar {
  width: 30px;
  height: 30px;
  border-radius: 50%;
  flex-shrink: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 0.82rem;
}

.bplpt-msg-bot .bplpt-chat-msg-avatar {
  background: var(--chat-accent);
  color: #fff;
}

.bplpt-msg-user .bplpt-chat-msg-avatar {
  background: var(--chat-gold);
  color: #fff;
}

.bplpt-chat-msg-content {
  max-width: 78%;
  display: flex;
  flex-direction: column;
  gap: 3px;
}

.bplpt-msg-user .bplpt-chat-msg-content {
  align-items: flex-end;
}

[dir="rtl"] .bplpt-msg-bot .bplpt-chat-msg-content {
  align-items: flex-end;
}

[dir="rtl"] .bplpt-msg-user .bplpt-chat-msg-content {
  align-items: flex-start;
}

.bplpt-chat-bubble {
  padding: 9px 13px;
  border-radius: 15px;
  font-size: 0.855rem;
  line-height: 1.58;
  word-break: break-word;
}

.bplpt-msg-bot .bplpt-chat-bubble {
  background: var(--chat-bot-bg);
  color: var(--chat-text);
  border-bottom-left-radius: 4px;
  border: 1px solid rgba(0, 0, 0, 0.05);
}

[dir="rtl"] .bplpt-msg-bot .bplpt-chat-bubble {
  border-bottom-left-radius: 15px;
  border-bottom-right-radius: 4px;
}

.bplpt-msg-user .bplpt-chat-bubble {
  background: var(--chat-user-bg);
  color: var(--chat-user-text);
  border-bottom-right-radius: 4px;
}

[dir="rtl"] .bplpt-msg-user .bplpt-chat-bubble {
  border-bottom-right-radius: 15px;
  border-bottom-left-radius: 4px;
}

.bplpt-chat-bubble strong {
  font-weight: 700;
}

.bplpt-chat-bubble a {
  color: inherit;
  text-decoration: underline;
  opacity: .9;
}

.bplpt-chat-bubble a:hover {
  opacity: 1;
}

.bplpt-msg-bot .bplpt-chat-bubble a {
  color: var(--chat-accent);
}

.bplpt-msg-user .bplpt-chat-bubble a {
  color: rgba(255, 255, 255, .9);
}

.bplpt-chat-msg-time {
  font-size: 0.62rem;
  color: var(--chat-text-muted);
  padding: 0 4px;
}

/* ── Book Cards ────────────────────────────────────────────── */

.bplpt-chat-books-grid {
  display: flex;
  flex-direction: column;
  gap: 7px;
  width: 100%;
}

.bplpt-chat-book-card {
  background: var(--chat-bg);
  border: 1px solid var(--chat-border);
  border-radius: 11px;
  padding: 9px 12px;
  border-left: 3px solid var(--chat-accent);
  transition: box-shadow 0.2s, transform 0.2s;
  animation: bplpt-msg-in 0.22s ease both;
}

[dir="rtl"] .bplpt-chat-book-card {
  border-left: none;
  border-right: 3px solid var(--chat-accent);
}

.bplpt-chat-book-card:hover {
  box-shadow: 0 3px 14px rgba(0, 0, 0, 0.08);
  transform: translateY(-1px);
}

.bplpt-book-title {
  font-weight: 700;
  font-size: 0.83rem;
  color: var(--chat-text);
  margin-bottom: 4px;
  line-height: 1.3;
}

.bplpt-book-meta {
  font-size: 0.71rem;
  color: var(--chat-text-muted);
  display: flex;
  flex-wrap: wrap;
  gap: 5px 10px;
  margin-bottom: 5px;
}

.bplpt-book-meta span {
  display: flex;
  align-items: center;
  gap: 3px;
}

.bplpt-book-avail {
  font-size: 0.71rem;
  font-weight: 600;
}

/* ── Typing Indicator ──────────────────────────────────────── */

.bplpt-typing-bubble {
  background: var(--chat-bot-bg);
  border-radius: 15px 15px 15px 4px;
  padding: 11px 15px;
  display: flex;
  align-items: center;
  gap: 4px;
  border: 1px solid rgba(0, 0, 0, 0.05);
}

[dir="rtl"] .bplpt-typing-bubble {
  border-radius: 15px 15px 4px 15px;
}

.bplpt-typing-dot {
  width: 7px;
  height: 7px;
  border-radius: 50%;
  background: var(--chat-accent);
  animation: bplpt-typing 1.2s ease-in-out infinite;
}

.bplpt-typing-dot:nth-child(2) {
  animation-delay: .2s;
}

.bplpt-typing-dot:nth-child(3) {
  animation-delay: .4s;
}

@keyframes bplpt-typing {

  0%,
  60%,
  100% {
    transform: translateY(0);
    opacity: .35;
  }

  30% {
    transform: translateY(-6px);
    opacity: 1;
  }
}

/* ── Input Area ────────────────────────────────────────────── */

.bplpt-chat-footer {
  padding: 11px 12px;
  background: var(--chat-bg);
  border-top: 1px solid var(--chat-border);
  display: flex;
  gap: 8px;
  align-items: flex-end;
  flex-shrink: 0;
  min-height: 58px;
  box-sizing: border-box;
}

.bplpt-chat-input-wrap {
  flex: 1;
  background: var(--chat-surface);
  border: 1.5px solid transparent;
  border-radius: 22px;
  display: flex;
  align-items: center;
  padding: 7px 13px;
  transition: border-color 0.2s;
  min-width: 0;
}

.bplpt-chat-input-wrap:focus-within {
  border-color: var(--chat-accent);
  background: #fff;
}

.bplpt-chat-input {
  flex: 1;
  border: none;
  background: transparent;
  outline: none;
  font-family: var(--chat-font);
  font-size: 0.875rem;
  color: var(--chat-text);
  resize: none;
  max-height: 80px;
  line-height: 1.4;
  min-width: 0;
}

.bplpt-chat-input::placeholder {
  color: var(--chat-text-muted);
}

.bplpt-chat-send-btn {
  width: 42px;
  height: 42px;
  border-radius: 50%;
  background: linear-gradient(135deg, var(--chat-accent), #0f4c75);
  border: none;
  color: #fff;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 0.95rem;
  flex-shrink: 0;
  transition: transform 0.2s, box-shadow 0.2s;
  box-shadow: 0 2px 8px rgba(26, 107, 154, 0.3);
}

.bplpt-chat-send-btn:hover {
  transform: scale(1.08);
  box-shadow: 0 4px 16px rgba(26, 107, 154, 0.45);
}

.bplpt-chat-send-btn:active {
  transform: scale(0.95);
}

.bplpt-chat-send-btn:disabled {
  opacity: .5;
  cursor: not-allowed;
  transform: none;
}

/* ── Spinner ───────────────────────────────────────────────── */

.bplpt-chat-spinner {
  display: none;
  position: absolute;
  inset: 0;
  background: rgba(255, 255, 255, 0.72);
  border-radius: var(--chat-radius);
  z-index: 10;
  align-items: center;
  justify-content: center;
}

.bplpt-chat-spinner.show {
  display: flex;
}

.bplpt-spinner-ring {
  width: 38px;
  height: 38px;
  border: 3px solid rgba(26, 107, 154, 0.15);
  border-top-color: var(--chat-accent);
  border-radius: 50%;
  animation: bplpt-spin 0.75s linear infinite;
}

@keyframes bplpt-spin {
  to {
    transform: rotate(360deg);
  }
}

/* ── Suggested Chips ───────────────────────────────────────── */

.bplpt-chips-wrap {
  display: flex;
  flex-wrap: wrap;
  gap: 7px;
  padding: 6px 10px 10px;
}

.bplpt-chip-btn {
  background: var(--chat-surface);
  border: 1px solid var(--chat-border);
  border-radius: 18px;
  padding: 5px 13px;
  font-size: 0.76rem;
  cursor: pointer;
  color: var(--chat-text);
  transition: background 0.18s, border-color 0.18s, transform 0.12s;
  white-space: nowrap;
  font-family: var(--chat-font);
}

.bplpt-chip-btn:hover {
  background: var(--chat-accent);
  border-color: var(--chat-accent);
  color: #fff;
  transform: translateY(-1px);
}

/* ── Date separator ────────────────────────────────────────── */

.bplpt-chat-date-sep {
  text-align: center;
  font-size: 0.63rem;
  color: var(--chat-text-muted);
  position: relative;
  margin: 4px 0;
}

.bplpt-chat-date-sep::before,
.bplpt-chat-date-sep::after {
  content: '';
  position: absolute;
  top: 50%;
  width: 30%;
  height: 1px;
  background: var(--chat-border);
}

.bplpt-chat-date-sep::before {
  left: 0;
}

.bplpt-chat-date-sep::after {
  right: 0;
}

/* ── Mobile ────────────────────────────────────────────────── */

@media (max-width: 480px) {
  .bplpt-chat-window {
    left: 0 !important;
    right: 0 !important;
    bottom: 0 !important;
    width: 100% !important;
    height: 100dvh !important;
    max-height: 100dvh !important;
    border-radius: 0 !important;
    transform-origin: bottom center !important;
  }

  .bplpt-chat-fab {
    bottom: 18px;
    right: 18px !important;
    left: auto !important;
  }
}