/* ================================================
   Chat Widget - Wishonia Book Chat
   ================================================ */

/* Floating Action Button */
.chat-fab {
  position: fixed;
  bottom: 24px;
  right: 24px;
  width: 56px;
  height: 56px;
  border-radius: 50%;
  background: var(--bs-primary, #2a6b4f);
  color: #fff;
  border: none;
  cursor: pointer;
  z-index: 9998;
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.25);
  display: flex;
  align-items: center;
  justify-content: center;
  transition: transform 0.2s ease, box-shadow 0.2s ease;
  font-size: 24px;
  line-height: 1;
}

.chat-fab:hover {
  transform: scale(1.08);
  box-shadow: 0 6px 20px rgba(0, 0, 0, 0.35);
}

.chat-fab.chat-open {
  transform: rotate(90deg);
}

/* Chat Panel */
.chat-panel {
  position: fixed;
  bottom: 96px;
  right: 24px;
  width: 400px;
  height: 560px;
  max-height: calc(100vh - 120px);
  background: var(--bs-body-bg, #fff);
  color: var(--bs-body-color, #212529);
  border-radius: 16px;
  box-shadow: 0 8px 32px rgba(0, 0, 0, 0.2);
  z-index: 9999;
  display: none;
  flex-direction: column;
  overflow: hidden;
  font-family: inherit;
  border: 1px solid var(--bs-border-color, #dee2e6);
}

.chat-panel.chat-visible {
  display: flex;
  animation: chatSlideIn 0.25s ease-out;
}

@keyframes chatSlideIn {
  from {
    opacity: 0;
    transform: translateY(16px) scale(0.96);
  }
  to {
    opacity: 1;
    transform: translateY(0) scale(1);
  }
}

/* Header */
.chat-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 14px 16px;
  background: var(--bs-primary, #2a6b4f);
  color: #fff;
  flex-shrink: 0;
}

.chat-header-title {
  font-size: 15px;
  font-weight: 600;
  letter-spacing: 0.02em;
}

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

.chat-close-btn,
.chat-newchat-btn,
.chat-fullscreen-btn {
  background: none;
  border: none;
  color: #fff;
  font-size: 16px;
  cursor: pointer;
  padding: 2px 6px;
  opacity: 0.7;
  line-height: 1;
  border-radius: 4px;
}

.chat-close-btn { font-size: 20px; }

.chat-close-btn:hover,
.chat-newchat-btn:hover,
.chat-fullscreen-btn:hover {
  opacity: 1;
  background: rgba(255, 255, 255, 0.15);
}

/* Messages Container */
.chat-messages {
  flex: 1;
  overflow-y: auto;
  padding: 16px;
  display: flex;
  flex-direction: column;
  gap: 12px;
}

/* Message Bubbles */
.chat-msg {
  max-width: 85%;
  padding: 10px 14px;
  border-radius: 14px;
  font-size: 14px;
  line-height: 1.5;
  white-space: pre-wrap;
  word-wrap: break-word;
}

.chat-msg-user {
  align-self: flex-end;
  background: var(--bs-primary, #2a6b4f);
  color: #fff;
  border-bottom-right-radius: 4px;
}

.chat-msg-assistant {
  align-self: flex-start;
  background: var(--bs-tertiary-bg, #f0f0f0);
  color: var(--bs-body-color, #212529);
  border-bottom-left-radius: 4px;
}

/* Voice button on assistant messages */
.chat-tts-btn {
  display: inline-block;
  background: none;
  border: none;
  cursor: pointer;
  font-size: 14px;
  padding: 2px 6px;
  margin-top: 4px;
  opacity: 0.6;
  color: inherit;
}

.chat-tts-btn:hover {
  opacity: 1;
}

.chat-tts-btn.loading {
  opacity: 0.4;
  cursor: wait;
}

/* Input Area */
.chat-input-area {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 12px 16px;
  border-top: 1px solid var(--bs-border-color, #dee2e6);
  flex-shrink: 0;
}

.chat-input {
  flex: 1;
  border: 1px solid var(--bs-border-color, #dee2e6);
  border-radius: 20px;
  padding: 8px 14px;
  font-size: 14px;
  font-family: inherit;
  background: var(--bs-body-bg, #fff);
  color: var(--bs-body-color, #212529);
  outline: none;
  resize: none;
  min-height: 38px;
  max-height: 100px;
}

.chat-input:focus {
  border-color: var(--bs-primary, #2a6b4f);
  box-shadow: 0 0 0 2px rgba(42, 107, 79, 0.15);
}

.chat-send-btn,
.chat-mic-btn {
  width: 36px;
  height: 36px;
  border-radius: 50%;
  border: none;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 16px;
  flex-shrink: 0;
  transition: background 0.15s;
}

.chat-send-btn {
  background: var(--bs-primary, #2a6b4f);
  color: #fff;
}

.chat-send-btn:hover {
  filter: brightness(1.1);
}

.chat-send-btn:disabled {
  opacity: 0.5;
  cursor: not-allowed;
}

.chat-mic-btn {
  background: var(--bs-tertiary-bg, #f0f0f0);
  color: var(--bs-body-color, #212529);
}

.chat-mic-btn:hover {
  background: var(--bs-secondary-bg, #e0e0e0);
}

/* Voice chat toggle button */
.chat-voicechat-btn {
  background: var(--bs-tertiary-bg, #f0f0f0);
  color: var(--bs-body-color, #212529);
  width: 36px;
  height: 36px;
  border-radius: 50%;
  border: none;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 16px;
  flex-shrink: 0;
  transition: background 0.15s;
}

.chat-voicechat-btn:hover {
  background: var(--bs-secondary-bg, #e0e0e0);
}

.chat-voicechat-btn.voice-active {
  background: var(--bs-primary, #2a6b4f);
  color: #fff;
}

.chat-voicechat-btn.recording {
  background: #d9534f;
  color: #fff;
  animation: micPulse 1s ease-in-out infinite;
}

.chat-mic-btn.recording {
  background: #d9534f;
  color: #fff;
  animation: micPulse 1s ease-in-out infinite;
}

@keyframes micPulse {
  0%, 100% { box-shadow: 0 0 0 0 rgba(217, 83, 79, 0.4); }
  50% { box-shadow: 0 0 0 8px rgba(217, 83, 79, 0); }
}

/* Welcome message */
.chat-welcome {
  text-align: center;
  padding: 24px 16px;
  color: var(--bs-secondary-color, #6c757d);
  font-size: 13px;
  line-height: 1.6;
}

.chat-welcome p {
  margin: 0 0 12px;
}

/* Hint buttons */
.chat-hints {
  display: flex;
  flex-direction: column;
  gap: 6px;
  margin-top: 8px;
}

.chat-hint-btn {
  background: var(--bs-tertiary-bg, #f0f0f0);
  color: var(--bs-body-color, #212529);
  border: 1px solid var(--bs-border-color, #dee2e6);
  border-radius: 12px;
  padding: 8px 14px;
  font-size: 13px;
  cursor: pointer;
  text-align: left;
  font-family: inherit;
  transition: background 0.15s, border-color 0.15s;
}

.chat-hint-btn:hover {
  background: var(--bs-secondary-bg, #e0e0e0);
  border-color: var(--bs-primary, #2a6b4f);
}

/* Links inside messages */
.chat-msg a {
  color: var(--bs-primary, #2a6b4f);
  text-decoration: underline;
  text-decoration-thickness: 1px;
  text-underline-offset: 2px;
}

.chat-msg a:hover {
  text-decoration-thickness: 2px;
}

.chat-msg-text {
  white-space: pre-wrap;
  word-wrap: break-word;
}

.chat-msg-assistant .chat-msg-text {
  white-space: normal;
}

/* Rich markdown: headers */
.chat-h3 {
  font-size: 15px;
  font-weight: 700;
  margin: 10px 0 4px;
}

.chat-h4 {
  font-size: 14px;
  font-weight: 600;
  margin: 8px 0 4px;
}

/* Rich markdown: lists */
.chat-list {
  margin: 6px 0;
  padding-left: 20px;
}

.chat-list li {
  margin: 2px 0;
}

/* Rich markdown: blockquotes */
.chat-blockquote {
  border-left: 3px solid var(--bs-primary, #2a6b4f);
  padding: 4px 10px;
  margin: 6px 0;
  color: var(--bs-secondary-color, #6c757d);
  font-style: italic;
}

/* Rich markdown: code */
.chat-codeblock {
  background: rgba(0, 0, 0, 0.06);
  border-radius: 6px;
  padding: 8px 10px;
  margin: 6px 0;
  font-family: 'SFMono-Regular', Consolas, 'Liberation Mono', Menlo, monospace;
  font-size: 12px;
  overflow-x: auto;
  white-space: pre;
}

.chat-inline-code {
  background: rgba(0, 0, 0, 0.06);
  padding: 1px 4px;
  border-radius: 3px;
  font-family: 'SFMono-Regular', Consolas, 'Liberation Mono', Menlo, monospace;
  font-size: 12px;
}

/* Source pills */
.chat-sources {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
  margin-top: 8px;
}

.chat-source-pill {
  display: inline-block;
  padding: 4px 10px;
  border-radius: 12px;
  font-size: 12px;
  background: rgba(42, 107, 79, 0.1);
  color: var(--bs-primary, #2a6b4f);
  text-decoration: none;
  border: 1px solid rgba(42, 107, 79, 0.2);
  transition: background 0.15s, border-color 0.15s;
  max-width: 200px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.chat-source-pill:hover {
  background: rgba(42, 107, 79, 0.2);
  border-color: var(--bs-primary, #2a6b4f);
  text-decoration: none;
}

/* Chat images */
.chat-image-container {
  margin-top: 8px;
}

.chat-image-thumb {
  max-width: 100%;
  max-height: 180px;
  border-radius: 8px;
  cursor: pointer;
  transition: opacity 0.15s;
  object-fit: contain;
}

.chat-image-thumb:hover {
  opacity: 0.85;
}

/* Lightbox overlay */
.chat-lightbox {
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.85);
  z-index: 10001;
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
}

.chat-lightbox img {
  max-width: 90vw;
  max-height: 90vh;
  border-radius: 8px;
  box-shadow: 0 8px 32px rgba(0, 0, 0, 0.5);
}

/* LaTeX pending */
.chat-latex-pending {
  font-style: italic;
  color: var(--bs-secondary-color, #6c757d);
}

.chat-latex-display {
  display: block;
  text-align: center;
  margin: 8px 0;
}

/* Fullscreen mode */
.chat-panel.chat-fullscreen {
  width: calc(100vw - 32px);
  height: calc(100vh - 32px);
  max-height: calc(100vh - 32px);
  bottom: 16px;
  right: 16px;
  border-radius: 12px;
}

/* Typing indicator (text generation) */
.chat-typing {
  align-self: flex-start;
  padding: 10px 14px;
  font-size: 20px;
  letter-spacing: 4px;
  color: var(--bs-secondary-color, #6c757d);
}

@keyframes typingDot {
  0%, 60%, 100% { opacity: 0.3; }
  30% { opacity: 1; }
}

.chat-typing span:nth-child(1) { animation: typingDot 1.4s infinite 0s; }
.chat-typing span:nth-child(2) { animation: typingDot 1.4s infinite 0.2s; }
.chat-typing span:nth-child(3) { animation: typingDot 1.4s infinite 0.4s; }

/* Voice thinking indicator (orbiting rings + rotating quips) */
.chat-thinking-indicator {
  align-self: flex-start;
  display: flex;
  align-items: center;
  gap: 14px;
  padding: 16px;
}

.chat-thinking-brain {
  position: relative;
  width: 36px;
  height: 36px;
  flex-shrink: 0;
}

.chat-brain-ring {
  position: absolute;
  inset: 0;
  border-radius: 50%;
  border: 2px solid transparent;
}

.chat-brain-ring:nth-child(1) {
  border-top-color: #d100b1;
  animation: brainSpin 1.2s linear infinite;
}

.chat-brain-ring:nth-child(2) {
  inset: 4px;
  border-right-color: #36E2F8;
  animation: brainSpin 1.8s linear infinite reverse;
}

.chat-brain-ring:nth-child(3) {
  inset: 8px;
  border-bottom-color: #C6CBF5;
  animation: brainSpin 0.9s linear infinite;
}

@keyframes brainSpin {
  to { transform: rotate(360deg); }
}

.chat-thinking-text-area {
  display: flex;
  flex-direction: column;
  gap: 4px;
  min-width: 0;
}

.chat-thinking-label {
  font-size: 11px;
  color: var(--bs-secondary-color, #888);
  opacity: 0.7;
}

.chat-thinking-quip {
  font-size: 13px;
  color: var(--bs-secondary-color, #999);
  font-style: italic;
  transition: opacity 0.3s;
  min-height: 1.4em;
}

/* Audio waveform animation (TTS generation) */
.chat-voice-loading {
  align-self: flex-start;
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 12px 16px;
  color: var(--bs-secondary-color, #6c757d);
  font-size: 12px;
}

.chat-waveform {
  display: flex;
  align-items: center;
  gap: 3px;
  height: 28px;
}

.chat-waveform-bar {
  width: 3px;
  border-radius: 2px;
  background: var(--bs-primary, #2a6b4f);
  animation: waveform 1.2s ease-in-out infinite;
}

.chat-waveform-bar:nth-child(1) { height: 8px;  animation-delay: 0s; }
.chat-waveform-bar:nth-child(2) { height: 16px; animation-delay: 0.1s; }
.chat-waveform-bar:nth-child(3) { height: 24px; animation-delay: 0.2s; }
.chat-waveform-bar:nth-child(4) { height: 12px; animation-delay: 0.3s; }
.chat-waveform-bar:nth-child(5) { height: 20px; animation-delay: 0.15s; }
.chat-waveform-bar:nth-child(6) { height: 10px; animation-delay: 0.25s; }
.chat-waveform-bar:nth-child(7) { height: 18px; animation-delay: 0.05s; }

@keyframes waveform {
  0%, 100% { transform: scaleY(0.3); opacity: 0.5; }
  50% { transform: scaleY(1); opacity: 1; }
}

/* Listening state indicator */
.chat-listening-indicator {
  align-self: center;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 8px;
  padding: 16px;
  color: var(--bs-secondary-color, #6c757d);
  font-size: 12px;
}

.chat-listening-ring {
  width: 48px;
  height: 48px;
  border-radius: 50%;
  border: 3px solid #d9534f;
  animation: listenPulse 1.5s ease-in-out infinite;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 20px;
}

/* Mic volume visualizer bars */
.chat-volume-bars {
  display: flex;
  align-items: flex-end;
  justify-content: center;
  gap: 3px;
  height: 36px;
  padding: 4px 0;
}

.chat-vol-bar {
  width: 4px;
  height: 4px;
  min-height: 4px;
  border-radius: 2px;
  background: var(--bs-primary, #2a6b4f);
  transition: height 60ms ease-out;
}

@keyframes listenPulse {
  0%, 100% { transform: scale(1); box-shadow: 0 0 0 0 rgba(217, 83, 79, 0.4); }
  50% { transform: scale(1.08); box-shadow: 0 0 0 12px rgba(217, 83, 79, 0); }
}

/* Voice chat connected state */
.chat-voicechat-btn.voice-active {
  background: #28a745;
  color: #fff;
  box-shadow: 0 0 0 3px rgba(40, 167, 69, 0.3);
}

.chat-voicechat-btn.voice-active:hover {
  background: #218838;
}

.chat-voicechat-btn.voice-active.recording {
  background: #28a745;
  animation: voicePulse 1.5s ease-in-out infinite;
}

@keyframes voicePulse {
  0%, 100% { box-shadow: 0 0 0 3px rgba(40, 167, 69, 0.3); }
  50% { box-shadow: 0 0 0 8px rgba(40, 167, 69, 0); }
}

/* Voice response card */
.chat-voice-card {
  background: var(--bs-tertiary-bg, #f8f8f8);
  border-radius: 10px;
  padding: 12px 14px;
  margin-top: 4px;
}

/* Collapsible thinking block */
.chat-thinking {
  margin-bottom: 8px;
}

.chat-thinking summary {
  cursor: pointer;
  font-size: 12px;
  color: var(--bs-secondary-color, #888);
  user-select: none;
}

.chat-thinking summary:hover {
  color: var(--bs-body-color, #333);
}

.chat-thinking-text {
  font-size: 12px;
  line-height: 1.5;
  color: var(--bs-secondary-color, #888);
  white-space: pre-wrap;
  word-wrap: break-word;
  margin: 6px 0 0;
  padding: 8px 10px;
  background: var(--bs-body-bg, #fff);
  border-radius: 6px;
  border: 1px solid var(--bs-border-color, #dee2e6);
  max-height: 200px;
  overflow-y: auto;
}

.chat-voice-note {
  font-size: 13px;
  color: var(--bs-secondary-color, #888);
  padding: 4px 0 0;
}

/* Stop generation button */
.chat-stop-btn {
  width: 36px;
  height: 36px;
  border-radius: 50%;
  border: 1px solid var(--bs-border-color, #dee2e6);
  background: var(--bs-tertiary-bg, #f0f0f0);
  color: var(--bs-body-color, #212529);
  cursor: pointer;
  display: none;
  align-items: center;
  justify-content: center;
  font-size: 14px;
  flex-shrink: 0;
  transition: background 0.15s;
}

.chat-stop-btn:hover {
  background: #d9534f;
  color: #fff;
  border-color: #d9534f;
}

/* Copy button on assistant messages */
.chat-copy-btn {
  display: inline-block;
  background: none;
  border: none;
  cursor: pointer;
  font-size: 14px;
  padding: 2px 6px;
  margin-top: 4px;
  opacity: 0.6;
  color: inherit;
}

.chat-copy-btn:hover {
  opacity: 1;
}

.chat-copy-btn.copied {
  opacity: 1;
  color: #28a745;
}

/* Message action bar */
.chat-msg-actions {
  display: flex;
  gap: 2px;
  margin-top: 4px;
}

/* Scroll to bottom button */
.chat-scroll-bottom {
  position: absolute;
  bottom: 70px;
  left: 50%;
  transform: translateX(-50%);
  width: 32px;
  height: 32px;
  border-radius: 50%;
  background: var(--bs-body-bg, #fff);
  color: var(--bs-body-color, #212529);
  border: 1px solid var(--bs-border-color, #dee2e6);
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.15);
  cursor: pointer;
  display: none;
  align-items: center;
  justify-content: center;
  font-size: 16px;
  z-index: 10;
  transition: opacity 0.15s;
}

.chat-scroll-bottom:hover {
  background: var(--bs-secondary-bg, #e0e0e0);
}

/* Textarea auto-grow input */
.chat-input {
  overflow-y: hidden;
}

/* Mobile responsive */
@media (max-width: 480px) {
  .chat-panel {
    width: calc(100vw - 16px);
    height: calc(100vh - 100px);
    max-height: calc(100vh - 100px);
    bottom: 88px;
    right: 8px;
    border-radius: 12px;
  }

  .chat-fab {
    bottom: 16px;
    right: 16px;
  }
}
