/* ============================================
   Bot Doctor — Chat Widget CSS
   Matches site aesthetic: white bg, black text,
   Instrument Serif + Inter fonts, dark mode support
   Sharp corners: border-radius: 2px (brand spec)
   ============================================ */

/* CSS Variables — inherit from site theme where possible */
:root {
  --cw-bg: #ffffff;
  --cw-surface: #f5f5f5;
  --cw-border: #e0e0e0;
  --cw-text: #111111;
  --cw-text-muted: #666666;
  --cw-accent: #111111;
  --cw-accent-text: #ffffff;
  --cw-radius: 2px;
  --cw-shadow: 0 4px 24px rgba(0, 0, 0, 0.1);
  --cw-shadow-bubble: 0 2px 12px rgba(0, 0, 0, 0.15);
  --cw-font-display: 'Instrument Serif', Georgia, serif;
  --cw-font-body: 'Inter', system-ui, sans-serif;
  --cw-bubble-size: 56px;
  --cw-panel-width: 380px;
  --cw-panel-height: 520px;
  --cw-offset: 24px;
}

/* Dark mode — mirror site theme */
[data-theme="dark"] {
  --cw-bg: #111111;
  --cw-surface: #1c1c20;
  --cw-border: #333333;
  --cw-text: #f0f0f0;
  --cw-text-muted: #c0c0c0;
  --cw-accent: #f0f0f0;
  --cw-accent-text: #111111;
  --cw-shadow: 0 4px 24px rgba(0, 0, 0, 0.3);
  --cw-shadow-bubble: 0 2px 12px rgba(0, 0, 0, 0.4);
}

/* ---- Chat Widget Container ---- */

#chat-widget {
  position: fixed;
  bottom: var(--cw-offset);
  right: var(--cw-offset);
  z-index: 9999;
  font-family: var(--cw-font-body);
  font-size: 14px;
  line-height: 1.5;
  color: var(--cw-text);
}

/* ---- Chat Bubble Button ---- */

#chat-bubble {
  width: var(--cw-bubble-size);
  height: var(--cw-bubble-size);
  border-radius: 50%;
  background: var(--cw-accent);
  border: none;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  box-shadow: var(--cw-shadow-bubble);
  transition: transform 0.2s ease;
  padding: 0;
  overflow: hidden;
}

#chat-bubble:hover {
  transform: scale(1.05);
}

#chat-bubble:active {
  transform: scale(0.97);
}

#chat-bubble .bubble-avatar {
  width: 100%;
  height: 100%;
  object-fit: cover;
  border-radius: 50%;
}

/* Unread badge on bubble */
#chat-bubble .unread-badge {
  position: absolute;
  top: -4px;
  right: -4px;
  width: 18px;
  height: 18px;
  border-radius: 50%;
  background: #e53935;
  color: #fff;
  font-size: 11px;
  font-weight: 600;
  display: none;
  align-items: center;
  justify-content: center;
}

#chat-bubble.has-unread .unread-badge {
  display: flex;
}

/* ---- Chat Panel ---- */

#chat-panel {
  position: fixed;
  bottom: calc(var(--cw-bubble-size) + var(--cw-offset) + 12px);
  right: var(--cw-offset);
  width: var(--cw-panel-width);
  height: var(--cw-panel-height);
  background: var(--cw-bg);
  border: 1px solid var(--cw-border);
  border-radius: var(--cw-radius);
  box-shadow: var(--cw-shadow);
  display: flex;
  flex-direction: column;
  overflow: hidden;

  /* Animations */
  opacity: 0;
  transform: scale(0.92) translateY(8px);
  transform-origin: bottom right;
  pointer-events: none;
  transition: opacity 0.2s ease, transform 0.2s ease;
}

#chat-panel.is-open {
  opacity: 1;
  transform: scale(1) translateY(0);
  pointer-events: auto;
}

/* ---- Chat Header ---- */

#chat-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 14px 16px;
  border-bottom: 1px solid var(--cw-border);
  background: var(--cw-bg);
  flex-shrink: 0;
  gap: 10px;
}

.chat-header-avatar {
  width: 36px;
  height: 36px;
  border-radius: 50%;
  overflow: hidden;
  flex-shrink: 0;
  background: var(--cw-surface);
}

.chat-header-avatar img {
  width: 100%;
  height: 100%;
  object-fit: cover;
}

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

#chat-header-title {
  font-family: var(--cw-font-display);
  font-size: 1.1rem;
  font-weight: 400;
  color: var(--cw-text);
  letter-spacing: -0.01em;
  margin: 0;
}

#chat-header-subtitle {
  font-size: 0.75rem;
  color: var(--cw-text-muted);
  margin: 2px 0 0;
  font-family: var(--cw-font-body);
}

#chat-close {
  width: 32px;
  height: 32px;
  border-radius: var(--cw-radius);
  background: transparent;
  border: 1px solid var(--cw-border);
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  color: var(--cw-text);
  transition: background 0.15s ease, border-color 0.15s ease;
  padding: 0;
  flex-shrink: 0;
}

#chat-close:hover {
  background: var(--cw-surface);
  border-color: var(--cw-text-muted);
}

#chat-close svg {
  width: 14px;
  height: 14px;
  stroke: currentColor;
  stroke-width: 2;
  stroke-linecap: round;
}

#chat-back {
  display: none;
  width: 32px;
  height: 32px;
  border-radius: var(--cw-radius);
  background: transparent;
  border: 1px solid var(--cw-border);
  cursor: pointer;
  align-items: center;
  justify-content: center;
  color: var(--cw-text);
  padding: 0;
  flex-shrink: 0;
}

#chat-back svg {
  width: 18px;
  height: 18px;
  stroke: currentColor;
  stroke-width: 2;
  stroke-linecap: round;
  stroke-linejoin: round;
  fill: none;
}

/* ---- Messages Area ---- */

#chat-messages {
  flex: 1;
  overflow-y: auto;
  padding: 16px;
  display: flex;
  flex-direction: column;
  gap: 10px;
  scroll-behavior: smooth;
}

/* Scrollbar styling */
#chat-messages::-webkit-scrollbar {
  width: 4px;
}

#chat-messages::-webkit-scrollbar-track {
  background: transparent;
}

#chat-messages::-webkit-scrollbar-thumb {
  background: var(--cw-border);
  border-radius: 2px;
}

#chat-messages::-webkit-scrollbar-thumb:hover {
  background: var(--cw-text-muted);
}

/* ---- Individual Messages ---- */

.chat-message {
  max-width: 85%;
  padding: 10px 14px;
  font-size: 14px;
  line-height: 1.5;
  word-wrap: break-word;
  animation: messageIn 0.2s ease;
}

@keyframes messageIn {
  from {
    opacity: 0;
    transform: translateY(6px);
  }
  to {
    opacity: 1;
    transform: translateY(0);
  }
}

.chat-message--bot {
  align-self: flex-start;
  background: var(--cw-surface);
  color: var(--cw-text);
  border-radius: 2px 2px 2px 0;
  display: flex;
  align-items: flex-start;
  gap: 8px;
}

.msg-avatar {
  width: 24px;
  height: 24px;
  border-radius: 50%;
  object-fit: cover;
  flex-shrink: 0;
  margin-top: 2px;
}

.msg-content {
  flex: 1;
  min-width: 0;
}

.chat-message--user {
  align-self: flex-end;
  background: var(--cw-accent);
  color: var(--cw-accent-text);
  border-radius: 2px 2px 0 2px;
}

/* Markdown content in bot messages */
.msg-content p {
  margin: 0 0 0.5em;
}

.msg-content p:last-child {
  margin-bottom: 0;
}

.msg-content strong {
  font-weight: 600;
}

.msg-content em {
  font-style: italic;
}

.msg-content a {
  color: var(--cw-accent);
  text-decoration: underline;
}

.msg-content ul,
.msg-content ol {
  margin: 0.5em 0;
  padding-left: 1.4em;
}

.msg-content li {
  margin: 0.25em 0;
}

/* ---- Typing Indicator ---- */

#chat-typing {
  display: none;
  align-self: flex-start;
  background: var(--cw-surface);
  border-radius: 2px 2px 2px 0;
  padding: 12px 16px;
  gap: 4px;
}

#chat-typing.is-visible {
  display: flex;
  align-items: center;
}

.typing-dot {
  width: 6px;
  height: 6px;
  border-radius: 50%;
  background: var(--cw-text-muted);
  animation: typingBounce 1.2s ease-in-out infinite;
}

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

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

@keyframes typingBounce {
  0%, 60%, 100% {
    transform: translateY(0);
    opacity: 0.4;
  }
  30% {
    transform: translateY(-4px);
    opacity: 1;
  }
}

/* ---- Input Area ---- */

#chat-input-area {
  display: flex;
  align-items: flex-end;
  padding: 12px 16px;
  border-top: 1px solid var(--cw-border);
  gap: 10px;
  background: var(--cw-bg);
  flex-shrink: 0;
}

#chat-input {
  flex: 1;
  border: none;
  border-bottom: 1px solid var(--cw-border);
  padding: 10px 0;
  font-family: var(--cw-font-body);
  font-size: 14px;
  color: var(--cw-text);
  background: transparent;
  resize: none;
  min-height: 40px;
  max-height: 100px;
  line-height: 1.5;
  transition: border-color 0.15s ease;
  outline: none;
}

#chat-input::placeholder {
  color: var(--cw-text-muted);
}

#chat-input:focus {
  border-color: var(--cw-text-muted);
}

#chat-send {
  width: 40px;
  height: 40px;
  border-radius: var(--cw-radius);
  background: var(--cw-accent);
  border: none;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  flex-shrink: 0;
  transition: background 0.15s ease, transform 0.1s ease;
  padding: 0;
}

#chat-send:hover {
  opacity: 0.85;
}

#chat-send:active {
  transform: scale(0.95);
}

#chat-send svg {
  width: 18px;
  height: 18px;
  stroke: var(--cw-accent-text);
  stroke-width: 2;
  stroke-linecap: round;
  stroke-linejoin: round;
  fill: none;
}

/* ---- Mobile Styles (< 640px) ---- */

@media (max-width: 639px) {
  #chat-widget {
    bottom: 0;
    right: 0;
  }

  #chat-bubble {
    bottom: var(--cw-offset);
    right: var(--cw-offset);
  }

  #chat-panel {
    position: fixed;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    width: 100%;
    height: 100%;
    border-radius: 0;
    bottom: auto;
  }

  #chat-panel.is-open {
    opacity: 1;
    transform: scale(1);
  }

  #chat-back {
    display: flex;
  }

  /* On mobile, hide the bubble when panel is open */
  #chat-bubble.is-hidden {
    opacity: 0;
    pointer-events: none;
    transform: scale(0.8);
  }
}

/* ---- Focus Styles (Accessibility) ---- */

#chat-bubble:focus-visible,
#chat-close:focus-visible,
#chat-back:focus-visible,
#chat-send:focus-visible,
#chat-input:focus-visible {
  outline: 2px solid var(--cw-accent);
  outline-offset: 2px;
}

/* ---- Welcome Message special styling ---- */

.chat-message--welcome {
  background: transparent;
  color: var(--cw-text-muted);
  font-size: 0.85rem;
  font-style: italic;
  text-align: center;
  padding: 8px 0;
  border: none;
  align-self: center;
  max-width: 90%;
}
