/* AI Text Explanation + Chat Styles */
/* Issue #73: Select-to-Explain + Chat with Voice Input */

/* Explain Button (Floating Trigger) */
.ai-explain-btn {
  position: absolute;
  background: var(--color-primary);
  color: white;
  border: none;
  padding: 0.5rem 1rem;
  border-radius: 18px;
  font-weight: 600;
  font-size: 0.875rem;
  cursor: pointer;
  display: none;
  align-items: center;
  gap: 0.375rem;
  box-shadow: 0 2px 12px rgba(0, 0, 0, 0.15);
  z-index: var(--z-tooltip);
  transition: all 150ms ease;
  opacity: 0;
  transform: scale(0.9);
}

.ai-explain-btn-visible {
  opacity: 1;
  transform: scale(1);
}

.ai-explain-btn:hover {
  transform: translateY(-1px) scale(1);
  box-shadow: 0 4px 16px rgba(0, 0, 0, 0.2);
  background: var(--color-primary-dark);
}

.ai-explain-btn svg {
  flex-shrink: 0;
}

/* Explanation Card */
.ai-explain-card {
  position: absolute;
  width: min(400px, 90vw);
  background: white;
  border-radius: 12px;
  box-shadow: 0 4px 24px rgba(0, 0, 0, 0.12);
  z-index: var(--z-tooltip);
  display: none;
  opacity: 0;
  transform: scale(0.95);
  transition: all 200ms ease;
}

.ai-explain-card-visible {
  opacity: 1;
  transform: scale(1);
}

.ai-explain-card-header {
  background: #f8fafc;
  padding: 0.75rem 1rem;
  border-bottom: 1px solid #e2e8f0;
  border-radius: 12px 12px 0 0;
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: 0.75rem;
}

.ai-explain-selected-text {
  font-size: 0.8125rem;
  color: var(--color-muted);
  font-style: italic;
  flex: 1;
  line-height: 1.4;
}

.ai-explain-close {
  background: transparent;
  border: none;
  color: var(--color-muted);
  cursor: pointer;
  padding: 0.25rem;
  border-radius: 4px;
  display: flex;
  align-items: center;
  justify-content: center;
  flex-shrink: 0;
  transition: all 150ms ease;
}

.ai-explain-close:hover {
  background: #e2e8f0;
  color: var(--color-text);
}

.ai-explain-content {
  padding: 1rem;
  min-height: 80px;
  max-height: 300px;
  overflow-y: auto;
}

.ai-explain-loading {
  display: flex;
  align-items: center;
  gap: 0.75rem;
  color: var(--color-muted);
  font-size: 0.9375rem;
}

.ai-explain-spinner {
  width: 16px;
  height: 16px;
  border: 2px solid #e2e8f0;
  border-top-color: var(--color-primary);
  border-radius: 50%;
  animation: spin 0.6s linear infinite;
}

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

.ai-explain-text {
  font-size: 0.9375rem;
  line-height: 1.6;
  color: var(--color-text);
}

.ai-explain-actions {
  padding: 0.75rem 1rem;
  border-top: 1px solid #e2e8f0;
  display: flex;
  justify-content: flex-end;
  gap: 0.5rem;
}

.ai-explain-copy-btn {
  background: transparent;
  border: 1px solid var(--color-border);
  color: var(--color-muted);
  padding: 0.375rem 0.875rem;
  border-radius: 6px;
  font-size: 0.875rem;
  font-weight: 600;
  cursor: pointer;
  transition: all 150ms ease;
}

.ai-explain-copy-btn:hover {
  background: #f8fafc;
  border-color: var(--color-primary);
  color: var(--color-primary);
}

/* Chat Bar (Sticky Bottom) */
.ai-chat-bar {
  position: fixed;
  bottom: 20px;
  left: 50%;
  transform: translateX(-50%);
  width: min(600px, calc(100vw - 32px));
  background: white;
  border-radius: 24px;
  box-shadow: 0 8px 32px rgba(0, 0, 0, 0.12);
  z-index: var(--z-fixed);
  transition: all 300ms ease;
}

.ai-chat-bar-expanded {
  border-radius: 16px;
}

.ai-chat-messages-header {
  display: none;
  justify-content: flex-end;
  padding: 0.25rem 0.75rem 0;
}

.ai-chat-collapse-btn {
  background: transparent;
  border: none;
  color: var(--color-muted);
  cursor: pointer;
  padding: 0.25rem;
  border-radius: 6px;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: all 150ms ease;
}

.ai-chat-collapse-btn:hover {
  background: #f1f5f9;
  color: var(--color-text);
}

.ai-chat-messages {
  max-height: 400px;
  overflow-y: auto;
  padding: 1rem;
  display: none;
  flex-direction: column;
  gap: 1rem;
}

.ai-chat-message {
  display: flex;
  flex-direction: column;
  align-items: flex-start;
}

.ai-chat-message-user {
  align-items: flex-end;
}

.ai-chat-bubble {
  background: #f1f5f9;
  color: var(--color-text);
  padding: 0.75rem 1rem;
  border-radius: 12px;
  max-width: 80%;
  font-size: 0.9375rem;
  line-height: 1.5;
  word-wrap: break-word;
}

.ai-chat-message-user .ai-chat-bubble {
  background: var(--color-primary);
  color: white;
  border-bottom-right-radius: 2px;
}

.ai-chat-message-ai .ai-chat-bubble {
  background: #f8fafc;
  border: 1px solid #e2e8f0;
  border-bottom-left-radius: 2px;
}

/* Rendered markdown inside AI bubbles and explain card */
.ai-chat-message-ai .ai-chat-bubble p,
.ai-explain-text p {
  margin: 0 0 0.5em;
}

.ai-chat-message-ai .ai-chat-bubble p:last-child,
.ai-explain-text p:last-child {
  margin-bottom: 0;
}

.ai-chat-message-ai .ai-chat-bubble ol,
.ai-chat-message-ai .ai-chat-bubble ul,
.ai-explain-text ol,
.ai-explain-text ul {
  margin: 0.5em 0;
  padding-left: 1.5em;
}

.ai-chat-message-ai .ai-chat-bubble li,
.ai-explain-text li {
  margin-bottom: 0.25em;
}

.ai-chat-message-ai .ai-chat-bubble li:last-child,
.ai-explain-text li:last-child {
  margin-bottom: 0;
}

.ai-chat-message-ai .ai-chat-bubble code,
.ai-explain-text code {
  background: rgba(0, 0, 0, 0.06);
  padding: 0.1em 0.3em;
  border-radius: 3px;
  font-size: 0.875em;
}

.ai-chat-message-ai .ai-chat-bubble strong,
.ai-explain-text strong {
  font-weight: 600;
}

.ai-chat-input-container {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  padding: 0.75rem 1rem;
}

.ai-chat-input {
  flex: 1;
  border: none;
  background: transparent;
  font-family: var(--font-body);
  font-size: 0.9375rem;
  color: var(--color-text);
  outline: none;
}

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

.ai-chat-mic-btn,
.ai-chat-send-btn {
  background: transparent;
  border: none;
  color: var(--color-muted);
  cursor: pointer;
  padding: 0.5rem;
  border-radius: 8px;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: all 150ms ease;
  flex-shrink: 0;
}

.ai-chat-mic-btn:hover,
.ai-chat-send-btn:hover {
  background: #f1f5f9;
  color: var(--color-primary);
}

.ai-chat-send-btn:hover {
  transform: translateX(2px);
}

/* Recording State */
.ai-chat-recording {
  color: #ef4444;
  animation: pulse 1.5s ease-in-out infinite;
}

@keyframes pulse {
  0%, 100% { opacity: 1; }
  50% { opacity: 0.5; }
}

/* Mobile Adjustments */
@media (max-width: 768px) {
  .ai-explain-card {
    left: 16px !important;
    right: 16px;
    width: auto;
  }

  .ai-chat-bar {
    width: calc(100vw - 16px);
    bottom: 16px;
  }

  .ai-chat-messages {
    max-height: 50vh;
  }

  .ai-chat-bubble {
    max-width: 90%;
  }
}

/* Prevent text selection UI interference */
.ai-explain-btn::selection,
.ai-explain-card::selection,
.ai-chat-bar::selection {
  background: transparent;
}
