/* ===== Sam AI Chat Widget ===== */

/* iOS/Android safe-area insets (0 on most devices) */
:root {
  --sam-safe-top: env(safe-area-inset-top, 0px);
  --sam-safe-right: env(safe-area-inset-right, 0px);
  --sam-safe-bottom: env(safe-area-inset-bottom, 0px);
  --sam-safe-left: env(safe-area-inset-left, 0px);
  /* Updated by JS via VisualViewport on mobile browsers */
  --sam-vh: 100vh;
  --sam-vw: 100vw;
  --sam-vv-bottom: 0px;
  --sam-vv-right: 0px;
}

/* Prevent page scrolling behind the widget (applied via JS on mobile when open) */
.sam-chat-scroll-lock,
.sam-chat-scroll-lock body {
  height: 100%;
  overflow: hidden;
  overscroll-behavior: none;
}

/* Toggle button */
.sam-chat-toggle {
  position: fixed;
  bottom: calc(2rem + var(--sam-safe-bottom) + var(--sam-vv-bottom, 0px));
  right: calc(2rem + var(--sam-safe-right) + var(--sam-vv-right, 0px));
  width: 70px;
  height: 70px;
  border-radius: 50%;
  background: var(--color-accent-primary);
  color: var(--color-text-inverse);
  border: 2px solid var(--color-border-strong);
  box-shadow: var(--shadow-md);
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 1500;
  transition: background 0.25s ease, transform 0.25s ease;
  padding: 0;
  font-family: inherit;
}
.sam-chat-toggle:hover {
  background: var(--color-accent-hover);
  transform: scale(1.08);
}
.sam-chat-toggle svg {
  width: 30px;
  height: 30px;
  fill: currentColor;
  transition: transform 0.3s ease;
}
.sam-chat-toggle.open svg {
  transform: rotate(90deg);
}

/* Chat container */
.sam-chat-container {
  position: fixed;
  bottom: calc(6rem + var(--sam-safe-bottom) + var(--sam-vv-bottom, 0px));
  right: calc(2rem + var(--sam-safe-right) + var(--sam-vv-right, 0px));
  width: 400px;
  max-width: calc(var(--sam-vw, 100vw) - 4rem - var(--sam-safe-left) - var(--sam-safe-right));
  height: 520px;
  max-height: calc(var(--sam-vh, 100vh) - 7rem - var(--sam-safe-top) - var(--sam-safe-bottom));
  background: var(--color-bg-secondary);
  border: 1px solid var(--color-border-strong);
  border-radius: var(--radius);
  box-shadow: var(--shadow-lg);
  z-index: 1500;
  display: flex;
  flex-direction: column;
  overflow: hidden;
  opacity: 0;
  transform: translateY(20px) scale(0.95);
  pointer-events: none;
  transition: opacity 0.3s ease, transform 0.3s ease;
}
.sam-chat-container.visible {
  opacity: 1;
  transform: translateY(0) scale(1);
  pointer-events: all;
}

/* Header */
.sam-chat-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0.85rem 1rem;
  background: var(--color-accent-primary);
  color: var(--color-text-inverse);
  flex-shrink: 0;
}
.sam-chat-header-left {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  font-weight: 700;
  font-size: var(--fs-md);
}
.sam-chat-header-dot {
  width: 8px;
  height: 8px;
  border-radius: 50%;
  background: #4ade80;
  flex-shrink: 0;
}
.sam-chat-header-actions {
  display: flex;
  align-items: center;
  gap: 0.5rem;
}
.sam-chat-new {
  background: rgba(255, 255, 255, 0.15);
  border: 1px solid rgba(255, 255, 255, 0.35);
  color: var(--color-text-inverse);
  cursor: pointer;
  padding: 0.3rem 0.75rem;
  border-radius: 999px;
  font-size: 0.75rem;
  font-weight: 600;
  font-family: inherit;
  letter-spacing: 0.02em;
  transition: background 0.2s, border-color 0.2s, transform 0.15s;
}
.sam-chat-new:hover {
  background: rgba(255, 255, 255, 0.28);
  border-color: rgba(255, 255, 255, 0.5);
  transform: scale(1.04);
}
.sam-chat-close {
  background: rgba(255, 255, 255, 0.12);
  border: none;
  color: var(--color-text-inverse);
  cursor: pointer;
  width: 28px;
  height: 28px;
  border-radius: 50%;
  font-size: 1.1rem;
  font-family: inherit;
  display: flex;
  align-items: center;
  justify-content: center;
  line-height: 1;
  transition: background 0.2s, transform 0.15s;
  padding: 0;
}
.sam-chat-close:hover {
  background: rgba(255, 255, 255, 0.28);
  transform: scale(1.1);
}

/* Messages area */
.sam-chat-messages {
  flex: 1;
  min-height: 0;
  overflow-y: auto;
  overscroll-behavior: contain;
  -webkit-overflow-scrolling: touch;
  padding: 1rem;
  display: flex;
  flex-direction: column;
  gap: 0.75rem;
  scroll-behavior: smooth;
}
.sam-chat-messages::-webkit-scrollbar {
  width: 5px;
}
.sam-chat-messages::-webkit-scrollbar-track {
  background: transparent;
}
.sam-chat-messages::-webkit-scrollbar-thumb {
  background: var(--color-border-strong);
  border-radius: 10px;
}

/* Message bubbles */
.sam-chat-msg {
  max-width: 85%;
  padding: 0.7rem 1rem;
  border-radius: 14px;
  font-size: var(--fs-md);
  line-height: 1.55;
  word-wrap: break-word;
  animation: samChatFadeIn 0.3s ease;
}
.sam-chat-msg.assistant {
  align-self: flex-start;
  background: var(--color-bg-tertiary);
  color: var(--color-text-primary);
  border: 1px solid var(--color-border);
  border-bottom-left-radius: 4px;
}
.sam-chat-msg.user {
  align-self: flex-end;
  background: var(--color-accent-primary);
  color: var(--color-text-inverse);
  border-bottom-right-radius: 4px;
}
.sam-chat-msg a {
  color: inherit;
  text-decoration: underline;
}
.sam-chat-msg.assistant a {
  color: var(--color-accent-primary);
}

/* Typing indicator */
.sam-chat-typing {
  display: flex;
  gap: 4px;
  align-items: center;
  padding: 0.7rem 1rem;
  align-self: flex-start;
}
.sam-chat-typing-dot {
  width: 7px;
  height: 7px;
  border-radius: 50%;
  background: var(--color-text-muted);
  animation: samChatBounce 1.4s infinite ease-in-out both;
}
.sam-chat-typing-dot:nth-child(1) { animation-delay: -0.32s; }
.sam-chat-typing-dot:nth-child(2) { animation-delay: -0.16s; }
.sam-chat-typing-dot:nth-child(3) { animation-delay: 0s; }

/* Session limit notice */
.sam-chat-limit {
  text-align: center;
  padding: 1rem;
  font-size: var(--fs-sm);
  color: var(--color-text-muted);
}
.sam-chat-limit a {
  color: var(--color-accent-primary);
  font-weight: 700;
  text-decoration: none;
}
.sam-chat-limit a:hover {
  text-decoration: underline;
}

/* Input area */
.sam-chat-input-area {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  padding: 0.75rem 1rem;
  border-top: 1px solid var(--color-border);
  background: var(--color-bg-secondary);
  flex-shrink: 0;
}
.sam-chat-input {
  flex: 1;
  padding: 0.6rem 0.9rem;
  border: 1px solid var(--color-border-strong);
  border-radius: 999px;
  background: var(--color-bg-primary);
  color: var(--color-text-primary);
  font-size: var(--fs-md);
  font-family: inherit;
  outline: none;
  transition: border-color 0.2s;
}
.sam-chat-input:focus {
  border-color: var(--color-accent-primary);
}
.sam-chat-input::placeholder {
  color: var(--color-text-muted);
}
.sam-chat-send {
  width: 38px;
  height: 38px;
  border-radius: 50%;
  background: var(--color-accent-primary);
  color: var(--color-text-inverse);
  border: none;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  flex-shrink: 0;
  transition: background 0.2s, transform 0.15s;
  padding: 0;
}
.sam-chat-send:hover:not(:disabled) {
  background: var(--color-accent-hover);
  transform: scale(1.05);
}
.sam-chat-send:disabled {
  opacity: 0.4;
  cursor: not-allowed;
}
.sam-chat-send svg {
  width: 18px;
  height: 18px;
  fill: currentColor;
}

/* Prompt suggestions */
.sam-chat-prompts {
  display: flex;
  flex-wrap: wrap;
  gap: 0.5rem;
  padding: 0.25rem 0;
  margin-top: auto;
  animation: samChatFadeIn 0.3s ease;
}
.sam-chat-prompt-btn {
  padding: 0.45rem 0.85rem;
  border-radius: 999px;
  border: 1px solid var(--color-accent-primary);
  background: transparent;
  color: var(--color-accent-primary);
  font-size: 0.8rem;
  font-family: inherit;
  cursor: pointer;
  transition: background 0.2s, color 0.2s, transform 0.15s;
  line-height: 1.3;
}
.sam-chat-prompt-btn:hover {
  background: var(--color-accent-primary);
  color: var(--color-text-inverse);
  transform: scale(1.03);
}

/* Book cover cards */
.sam-chat-book-cards {
  display: flex;
  gap: 8px;
  margin-top: 10px;
  padding-top: 10px;
  border-top: 1px solid var(--color-border);
  flex-wrap: wrap;
}
.sam-chat-book-card {
  display: flex;
  flex-direction: column;
  align-items: center;
  text-decoration: none !important;
  border-radius: 6px;
  overflow: hidden;
  background: var(--color-bg-primary);
  border: 1px solid var(--color-border);
  max-width: 70px;
  transition: transform 0.2s, box-shadow 0.2s;
}
.sam-chat-book-card:hover {
  transform: translateY(-2px);
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.12);
}
.sam-chat-book-card img {
  width: 60px;
  height: auto;
  display: block;
}
.sam-chat-book-card span {
  font-size: 0.6rem;
  padding: 3px 4px;
  text-align: center;
  color: var(--color-text-secondary);
  line-height: 1.15;
  width: 100%;
}

/* Amazon link */
.sam-chat-amazon-link {
  color: var(--color-accent-primary) !important;
  font-weight: 600;
  text-decoration: none !important;
}
.sam-chat-amazon-link:hover {
  text-decoration: underline !important;
}

/* Inline formatting */
.sam-chat-msg strong {
  font-weight: 600;
}

/* Animations */
@keyframes samChatFadeIn {
  from { opacity: 0; transform: translateY(8px); }
  to   { opacity: 1; transform: translateY(0); }
}
@keyframes samChatBounce {
  0%, 80%, 100% { transform: scale(0); }
  40% { transform: scale(1); }
}

/* Desktop — smaller chat font */
@media (min-width: 769px) {
  .sam-chat-msg {
    font-size: 0.8rem;
  }
  .sam-chat-input {
    font-size: 0.8rem;
  }
}

/* Mobile full-screen */
@media (max-width: 768px) {
  .sam-chat-container {
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    width: 100%;
    max-width: none;
    height: 100dvh;
    height: var(--sam-vh, 100vh);
    max-height: none;
    border-radius: 0;
    border: none;
  }
  .sam-chat-header {
    padding-top: calc(0.85rem + var(--sam-safe-top));
    padding-left: calc(1rem + var(--sam-safe-left));
    padding-right: calc(1rem + var(--sam-safe-right));
  }
  .sam-chat-messages {
    padding-left: calc(1rem + var(--sam-safe-left));
    padding-right: calc(1rem + var(--sam-safe-right));
  }
  .sam-chat-input-area {
    padding-bottom: calc(0.75rem + var(--sam-safe-bottom));
    padding-left: calc(1rem + var(--sam-safe-left));
    padding-right: calc(1rem + var(--sam-safe-right));
  }
  .sam-chat-toggle {
    bottom: calc(1.25rem + var(--sam-safe-bottom) + var(--sam-vv-bottom, 0px));
    right: calc(1.25rem + var(--sam-safe-right) + var(--sam-vv-right, 0px));
    width: 50px;
    height: 50px;
  }
  .sam-chat-toggle svg {
    width: 22px;
    height: 22px;
  }
}
