/* ==========================================================================
   Bookrenia AI assistant — floating chat widget (public site only)
   Uses the site tokens from style.css; fallbacks keep it intact if a page
   ever loads it without them.
   ========================================================================== */

.cb-widget {
  --cb-brand: var(--primary, #6307f8);
  --cb-brand-dark: var(--primary-dark, #5106d0);
  --cb-ink: var(--deep-iris, #15002c);
  --cb-surface: var(--surface, #ffffff);
  --cb-canvas: var(--surface-sunken, #f8f8fd);
  --cb-soft: var(--primary-soft, #e1e2fe);
  --cb-border: var(--border, #dedee8);
  --cb-text: var(--text, #060000);
  --cb-muted: var(--text-muted, #5a5a72);
  --cb-danger: var(--danger, #c0304a);
  --cb-danger-soft: var(--danger-soft, #fbeaee);
  --cb-radius: var(--radius, 14px);

  position: fixed;
  right: 1.25rem;
  bottom: calc(1.25rem + var(--cb-lift, 0px) + env(safe-area-inset-bottom, 0px));
  z-index: 140; /* below the cookie banner (150) and lightbox (200) */
  font-family: var(--font-body, Inter, system-ui, sans-serif);
  transition: bottom .3s ease; /* glides down once the cookie banner closes */
}

/* ── Launcher ──────────────────────────────────────────────────────────── */
.cb-launcher {
  position: relative;
  display: flex;
  align-items: center;
  justify-content: center;
  width: 60px;
  height: 60px;
  border: 0;
  border-radius: 50%;
  background: var(--cb-brand);
  color: #fff;
  font-size: 1.35rem;
  cursor: pointer;
  box-shadow: var(--shadow-primary, 0 8px 24px rgba(99, 7, 248, .24));
  transition: transform .2s ease, background .2s ease;
}
.cb-launcher:hover { background: var(--cb-brand-dark); transform: translateY(-2px); }

/* Soft rings pulsing out of the launcher, like a signal — idle state only */
.cb-launcher::before,
.cb-launcher::after {
  content: "";
  position: absolute;
  inset: 0;
  border-radius: 50%;
  background: var(--cb-brand);
  opacity: 0;
  z-index: -1;
  pointer-events: none;
  animation: cb-ring 2.8s ease-out infinite;
}
.cb-launcher::after { animation-delay: 1.4s; }
.cb-widget.is-open .cb-launcher::before,
.cb-widget.is-open .cb-launcher::after { animation: none; }

@keyframes cb-ring {
  0%   { opacity: .35; transform: scale(1); }
  100% { opacity: 0;   transform: scale(1.75); }
}

/* Robot mark: head takes `fill`, eyes and mouth take `color` */
.cb-robot {
  width: 30px;
  height: 30px;
  fill: #fff;
  color: var(--cb-brand);
  transition: color .2s ease;
}
.cb-launcher:hover .cb-robot { color: var(--cb-brand-dark); }
.cb-avatar .cb-robot { width: 22px; height: 22px; }
.cb-launcher:focus-visible { outline: 3px solid var(--cb-soft); outline-offset: 3px; }
.cb-launcher-close { display: none; }
.cb-widget.is-open .cb-launcher-open { display: none; }
.cb-widget.is-open .cb-launcher-close { display: inline-block; }

/* ── Panel ─────────────────────────────────────────────────────────────── */
.cb-panel {
  position: absolute;
  right: 0;
  bottom: 76px;
  display: flex;
  flex-direction: column;
  width: 380px;
  max-width: calc(100vw - 2.5rem);
  height: 560px;
  max-height: calc(100vh - 120px);
  overflow: hidden;
  background: var(--cb-surface);
  border: 1px solid var(--cb-border);
  border-radius: var(--radius-xl, 20px);
  box-shadow: var(--shadow-xl, 0 20px 60px rgba(21, 0, 44, .16));
  animation: cb-rise .18s ease;
}
.cb-panel[hidden] { display: none; }

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

.cb-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: .75rem;
  padding: .9rem 1rem;
  background: var(--cb-ink);
  color: #fff;
  flex-shrink: 0;
}
.cb-header-info { display: flex; align-items: center; gap: .7rem; min-width: 0; }
.cb-avatar {
  display: flex;
  align-items: center;
  justify-content: center;
  width: 36px;
  height: 36px;
  border-radius: 50%;
  background: var(--cb-brand);
  font-size: .95rem;
  flex-shrink: 0;
}
.cb-title {
  margin: 0;
  font-family: var(--font-body, Inter, system-ui, sans-serif);
  font-size: .95rem;
  font-weight: 500;
  line-height: 1.2;
  color: #fff; /* headings on dark surfaces must set their own colour */
}
.cb-status {
  display: flex;
  align-items: center;
  gap: .35rem;
  font-size: .75rem;
  color: var(--text-on-dark-muted, #a8a3bd);
}
.cb-status-dot { width: 7px; height: 7px; border-radius: 50%; background: #4ade80; }
.cb-close {
  display: flex;
  align-items: center;
  justify-content: center;
  width: 30px;
  height: 30px;
  border: 0;
  border-radius: var(--radius-sm, 10px);
  background: rgba(255, 255, 255, .1);
  color: #fff;
  cursor: pointer;
}
.cb-close:hover { background: rgba(255, 255, 255, .2); }

/* ── Messages ──────────────────────────────────────────────────────────── */
.cb-messages {
  flex: 1;
  display: flex;
  flex-direction: column;
  gap: .6rem;
  padding: 1rem;
  overflow-y: auto;
  background: var(--cb-canvas);
  overscroll-behavior: contain;
}
.cb-msg { display: flex; }
.cb-msg-user { justify-content: flex-end; }
.cb-bubble {
  max-width: 85%;
  padding: .6rem .85rem;
  border-radius: var(--cb-radius);
  font-size: .875rem;
  font-weight: 400;
  line-height: 1.55;
  word-break: break-word;
}
.cb-msg-bot .cb-bubble {
  background: var(--cb-surface);
  border: 1px solid var(--cb-border);
  border-bottom-left-radius: 4px;
  color: var(--cb-text);
}
.cb-msg-user .cb-bubble {
  background: var(--cb-brand);
  border-bottom-right-radius: 4px;
  color: #fff;
}
.cb-msg-error .cb-bubble {
  background: var(--cb-danger-soft);
  border-color: transparent;
  color: var(--cb-danger);
}
.cb-bubble p { margin: 0 0 .5rem; }
.cb-bubble p:last-child { margin-bottom: 0; }
.cb-bubble ul, .cb-bubble ol { margin: .25rem 0 .5rem; padding-left: 1.15rem; }
.cb-bubble li { margin-bottom: .2rem; }
.cb-bubble strong { font-weight: 500; }
.cb-bubble a { color: var(--cb-brand); font-weight: 500; text-decoration: underline; word-break: break-all; }
.cb-msg-user .cb-bubble a { color: #fff; }

/* ── Suggestions ───────────────────────────────────────────────────────── */
.cb-suggestions {
  display: flex;
  flex-wrap: wrap;
  gap: .4rem;
  padding: 0 1rem .75rem;
  background: var(--cb-canvas);
}
.cb-suggestions[hidden] { display: none; }
.cb-chip {
  padding: .35rem .75rem;
  border: 1px solid var(--cb-border);
  border-radius: var(--radius-pill, 9999px);
  background: var(--cb-surface);
  color: var(--cb-ink);
  font-size: .78rem;
  cursor: pointer;
  transition: border-color .15s ease, background .15s ease;
}
.cb-chip:hover { border-color: var(--cb-brand); background: var(--cb-soft); }

/* ── Typing indicator ──────────────────────────────────────────────────── */
.cb-typing {
  display: flex;
  gap: 4px;
  padding: 0 1rem .75rem;
  background: var(--cb-canvas);
}
.cb-typing[hidden] { display: none; }
.cb-typing span {
  width: 6px;
  height: 6px;
  border-radius: 50%;
  background: var(--cb-muted);
  animation: cb-dot 1s infinite ease-in-out;
}
.cb-typing span:nth-child(2) { animation-delay: .15s; }
.cb-typing span:nth-child(3) { animation-delay: .3s; }
@keyframes cb-dot {
  0%, 60%, 100% { opacity: .35; transform: none; }
  30% { opacity: 1; transform: translateY(-3px); }
}

/* ── Input ─────────────────────────────────────────────────────────────── */
.cb-form {
  display: flex;
  align-items: center;
  gap: .5rem;
  padding: .75rem;
  border-top: 1px solid var(--cb-border);
  background: var(--cb-surface);
  flex-shrink: 0;
}
.cb-input {
  flex: 1;
  min-width: 0;
  padding: .6rem 1rem;
  border: 1px solid var(--cb-border);
  border-radius: var(--radius-pill, 9999px);
  background: var(--cb-canvas);
  color: var(--cb-text);
  font-size: .875rem;
  outline: none;
  transition: border-color .15s ease;
}
.cb-input:focus { border-color: var(--cb-brand); }
.cb-input:disabled { opacity: .6; }
.cb-send {
  display: flex;
  align-items: center;
  justify-content: center;
  width: 38px;
  height: 38px;
  border: 0;
  border-radius: 50%;
  background: var(--cb-brand);
  color: #fff;
  cursor: pointer;
  flex-shrink: 0;
}
.cb-send:hover { background: var(--cb-brand-dark); }
.cb-send:disabled { opacity: .45; cursor: not-allowed; }

.cb-footer {
  display: flex;
  flex-direction: column;
  gap: .1rem;
  padding: 0 .9rem .6rem;
  background: var(--cb-surface);
  font-size: .68rem;
  line-height: 1.35;
  text-align: center;
  color: var(--text-subtle, #8d8da5);
}
.cb-footer #cbQuota:empty { display: none; }
.cb-footer .is-limit { color: var(--cb-danger); font-weight: 500; }

/* ── Small screens: the panel takes the viewport ───────────────────────────
   Width ≤ 480px (phones) or height ≤ 500px (phones in landscape).
   --cb-vh / --cb-top come from chatbot.js (visualViewport), so the input bar
   stays above the on-screen keyboard; 100dvh is the no-JS fallback.        */
@media (max-width: 480px), (max-height: 500px) {
  .cb-widget { right: 1rem; bottom: calc(1rem + var(--cb-lift, 0px) + env(safe-area-inset-bottom, 0px)); }
  .cb-widget.is-open { z-index: 1000; } /* over the cookie banner while chatting */
  .cb-widget.is-open .cb-launcher { display: none; }
  .cb-panel {
    position: fixed;
    top: var(--cb-top, 0px);
    left: 0;
    right: 0;
    bottom: auto;
    width: 100%;
    max-width: none;
    height: var(--cb-vh, 100dvh);
    max-height: none;
    border: 0;
    border-radius: 0;
    animation: none;
  }
  .cb-header { padding-top: calc(.9rem + env(safe-area-inset-top, 0px)); }
  .cb-header,
  .cb-messages,
  .cb-suggestions,
  .cb-typing,
  .cb-form {
    padding-left: max(1rem, env(safe-area-inset-left, 0px));
    padding-right: max(1rem, env(safe-area-inset-right, 0px));
  }
  .cb-close { width: 40px; height: 40px; } /* comfortable touch target */
  .cb-bubble { max-width: 90%; }
  .cb-chip { padding: .45rem .8rem; }
  .cb-input { font-size: 1rem; } /* under 16px, iOS Safari zooms on focus */
  .cb-send { width: 42px; height: 42px; }
  .cb-footer { padding-bottom: calc(.6rem + env(safe-area-inset-bottom, 0px)); }
}

/* Landscape phones: every pixel of height goes to the conversation */
@media (max-height: 500px) {
  .cb-header { padding-top: calc(.5rem + env(safe-area-inset-top, 0px)); padding-bottom: .5rem; }
  .cb-status, .cb-suggestions { display: none; }
  .cb-footer span:last-child { display: none; }
}

/* Page behind the full-screen chat must not scroll (class set by chatbot.js) */
html.cb-scroll-lock,
html.cb-scroll-lock body { overflow: hidden; }

@media (prefers-reduced-motion: reduce) {
  .cb-panel, .cb-typing span,
  .cb-launcher::before, .cb-launcher::after { animation: none; }
  .cb-widget, .cb-launcher { transition: none; }
}
