/* AI Assistant Chat Widget Styles */
.ai-assistant {
  position: fixed;
  bottom: var(--space-lg);
  right: var(--space-lg);
  z-index: 9999;
  font-family: var(--font-family-primary);
}

/* Chat Toggle Button */
.ai-assistant__toggle {
  width: 60px;
  height: 60px;
  border-radius: var(--border-radius-circle);
  background: linear-gradient(135deg, var(--color-accent) 0%, var(--color-primary) 100%);
  border: none;
  color: var(--color-white);
  font-size: 28px;
  cursor: grab;
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.15);
  transition: all var(--transition);
  display: flex;
  align-items: center;
  justify-content: center;
  user-select: none;
}

.ai-assistant__toggle:hover {
  transform: scale(1.1);
  box-shadow: 0 6px 20px rgba(0, 0, 0, 0.2);
}

.ai-assistant__toggle:active {
  cursor: grabbing;
}

.ai-assistant__toggle--active {
  background: var(--color-gray-dark);
}

/* Chat Window */
.ai-assistant__window {
  position: fixed;
  width: min(380px, 90vw);
  height: min(500px, 70vh);
  min-width: 280px;
  min-height: 300px;
  max-width: 90vw;
  max-height: 70vh;
  background: var(--color-white);
  border-radius: var(--border-radius);
  box-shadow: 0 10px 40px rgba(0, 0, 0, 0.15);
  display: none;
  flex-direction: column;
  overflow: hidden;
  border: 1px solid var(--color-gray-medium);
  overscroll-behavior: contain;
}

.ai-assistant__window--open {
  display: flex;
  animation: slideUp 0.3s ease-out;
}

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

/* Chat Header */
.ai-assistant__header {
  flex: 0 0 auto;
  padding: var(--space-md);
  background: linear-gradient(135deg, var(--color-primary) 0%, var(--color-accent) 100%);
  color: var(--color-white);
  display: flex;
  justify-content: space-between;
  align-items: center;
  user-select: none;
}

.ai-assistant__title {
  font-size: var(--font-size-md);
  font-weight: 600;
  margin: 0;
  display: flex;
  align-items: center;
  gap: var(--space-xs);
}

.ai-assistant__status {
  width: 8px;
  height: 8px;
  border-radius: var(--border-radius-circle);
  background: #4ade80;
  animation: pulse 2s infinite;
}

.ai-assistant__status--offline {
  background: #ef4444;
  animation: none;
}

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

.ai-assistant__header-actions {
  display: flex;
  align-items: center;
  gap: var(--space-xs);
}

.ai-assistant__new-session {
  background: rgba(255, 255, 255, 0.15);
  border: 1px solid rgba(255, 255, 255, 0.3);
  color: var(--color-white);
  font-size: var(--font-size-xs);
  font-weight: 500;
  cursor: pointer;
  padding: 4px 8px;
  border-radius: var(--border-radius-sm);
  transition: all var(--transition);
  white-space: nowrap;
}

.ai-assistant__new-session:hover {
  background: rgba(255, 255, 255, 0.25);
  border-color: rgba(255, 255, 255, 0.5);
}

.ai-assistant__close {
  background: none;
  border: none;
  color: var(--color-white);
  font-size: 24px;
  cursor: pointer;
  padding: 0;
  width: 30px;
  height: 30px;
  display: flex;
  align-items: center;
  justify-content: center;
  border-radius: var(--border-radius-sm);
  transition: background var(--transition);
}

.ai-assistant__close:hover {
  background: rgba(255, 255, 255, 0.2);
}

/* Chat Messages */
.ai-assistant__messages {
  flex: 1 1 auto;
  min-height: 0;
  overflow-y: auto;
  padding: var(--space-md);
  background: var(--color-secondary);
  display: flex;
  flex-direction: column;
  gap: var(--space-md);
  overscroll-behavior: contain;
}

.ai-message {
  display: flex;
  gap: var(--space-sm);
  animation: fadeIn 0.3s ease-out;
}

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

.ai-message--user {
  flex-direction: row-reverse;
}

.ai-message__avatar {
  width: 32px;
  height: 32px;
  border-radius: var(--border-radius-circle);
  display: flex;
  align-items: center;
  justify-content: center;
  flex-shrink: 0;
  font-size: 16px;
}

.ai-message--assistant .ai-message__avatar {
  background: linear-gradient(135deg, var(--color-accent) 0%, var(--color-primary) 100%);
  color: var(--color-white);
}

.ai-message--user .ai-message__avatar {
  background: var(--color-gray-medium);
  color: var(--color-text);
}

.ai-message__content {
  max-width: 70%;
  padding: var(--space-sm) var(--space-md);
  border-radius: var(--border-radius);
  line-height: 1.5;
  font-size: var(--font-size-sm);
  word-wrap: break-word;
  overflow-wrap: break-word;
  word-break: break-word;
}

.ai-message--assistant .ai-message__content {
  background: var(--color-white);
  color: var(--color-text);
  border-bottom-left-radius: 4px;
}

.ai-message--user .ai-message__content {
  background: var(--color-accent);
  color: var(--color-white);
  border-bottom-right-radius: 4px;
}

/* Context badge for user messages - positioned outside message box on the left */
.ai-message__context-badge {
  display: flex;
  align-items: center;
  justify-content: center;
  width: 24px;
  height: 24px;
  font-size: 14px;
  background: rgba(255, 255, 255, 0.9);
  border-radius: 50%;
  box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
  flex-shrink: 0;
  border: 1px solid rgba(0, 0, 0, 0.05);
  order: 10;
}

/* For messages with context badge: remove gap and add specific margins */
.ai-message--has-context.ai-message--user {
  gap: 0;
}

.ai-message--has-context.ai-message--user .ai-message__avatar {
  margin-left: var(--space-sm);
}

.ai-message--has-context.ai-message--user .ai-message__content {
  margin-left: 2px;
}

/* Image in user messages */
.ai-message__image {
  width: auto;
  min-width: 100px;
  max-width: 100%;
  height: auto;
  max-height: 200px;
  border-radius: var(--border-radius);
  margin-bottom: var(--space-xs);
  display: block !important;
  opacity: 1 !important; /* Fix for opacity issue */
  visibility: visible !important;
  object-fit: contain;
  cursor: pointer;
  transition: transform var(--transition);
  background: rgba(0, 0, 0, 0.05);
  border: 1px solid rgba(0, 0, 0, 0.1);
}

.ai-message__image:hover {
  transform: scale(1.02);
}

/* Fallback if image fails to load */
.ai-message__image[src=""],
.ai-message__image:not([src]) {
  display: none;
}

/* Markdown formatting in messages */
.ai-message__content strong {
  font-weight: 600;
}

.ai-message__content em {
  font-style: italic;
}

.ai-message__content code {
  background: rgba(0, 0, 0, 0.05);
  padding: 2px 6px;
  border-radius: 3px;
  font-family: 'Monaco', 'Menlo', 'Consolas', monospace;
  font-size: 0.9em;
}

.ai-message__content a {
  color: var(--color-primary);
  text-decoration: underline;
  transition: color var(--transition);
}

.ai-message__content a:hover {
  color: var(--color-accent);
}

.ai-message__content ol,
.ai-message__content ul {
  margin: 8px 0;
  padding-left: 20px;
}

.ai-message__content li {
  margin: 4px 0;
}

.ai-message__content h1,
.ai-message__content h2,
.ai-message__content h3 {
  margin: 12px 0 8px 0;
  font-weight: 600;
}

.ai-message__content h1 {
  font-size: 1.3em;
}

.ai-message__content h2 {
  font-size: 1.2em;
}

.ai-message__content h3 {
  font-size: 1.1em;
}

/* Typing Indicator */
.ai-message__typing {
  display: flex;
  gap: 4px;
  padding: var(--space-sm) var(--space-md);
}

.ai-message__typing span {
  width: 8px;
  height: 8px;
  border-radius: var(--border-radius-circle);
  background: var(--color-text-muted);
  animation: typing 1.4s infinite;
}

.ai-message__typing span:nth-child(2) {
  animation-delay: 0.2s;
}

.ai-message__typing span:nth-child(3) {
  animation-delay: 0.4s;
}

@keyframes typing {
  0%, 60%, 100% {
    transform: translateY(0);
  }
  30% {
    transform: translateY(-10px);
  }
}

/* Chat Input */
.ai-assistant__input-container {
  flex: 0 0 auto;
  padding: var(--space-md);
  background: var(--color-white);
  border-top: 1px solid var(--color-gray-medium);
  display: flex;
  gap: var(--space-sm);
  position: relative;
}

.ai-assistant__input-wrapper {
  flex: 1;
  display: flex;
  align-items: center;
  gap: var(--space-xs);
  border: 1px solid var(--color-gray-medium);
  border-radius: var(--border-radius);
  padding: var(--space-xs);
  transition: border-color var(--transition);
}

.ai-assistant__input-wrapper:focus-within {
  border-color: var(--color-accent);
}

.ai-context-tag-display {
  display: flex;
  align-items: center;
  flex-shrink: 0;
}

/* Image upload button (only visible on beer page) */
.ai-assistant__upload-btn {
  display: flex;
  align-items: center;
  justify-content: center;
  width: 32px;
  height: 32px;
  border: none;
  background: transparent;
  color: var(--color-text-secondary);
  font-size: 18px;
  cursor: pointer;
  border-radius: var(--border-radius);
  transition: all var(--transition);
  flex-shrink: 0;
  padding: 0;
}

.ai-assistant__upload-btn:hover {
  background: var(--color-gray-light);
  color: var(--color-accent);
}

.ai-assistant__upload-btn:active {
  transform: scale(0.95);
}

.ai-assistant__upload-btn--selected {
  background: var(--color-accent);
  color: var(--color-white);
}

.ai-assistant__upload-btn--selected:hover {
  background: var(--color-accent);
  color: var(--color-white);
  opacity: 0.9;
}

.ai-assistant__input {
  flex: 1;
  padding: var(--space-xs) var(--space-sm);
  border: none;
  font-size: var(--font-size-sm);
  font-family: var(--font-family-primary);
  min-height: 32px;
  max-height: 100px;
  overflow-y: auto;
  overflow-x: hidden;
  word-wrap: break-word;
  white-space: pre-wrap;
}

.ai-assistant__input:focus {
  outline: none;
}

.ai-assistant__input:empty:before {
  content: attr(data-placeholder);
  color: var(--color-text-secondary);
  opacity: 0.6;
  pointer-events: none;
}

.ai-assistant__input[contenteditable="false"] {
  background: var(--color-gray-light);
  cursor: not-allowed;
  opacity: 0.6;
}

.ai-assistant__send {
  padding: var(--space-sm) var(--space-md);
  background: var(--color-accent);
  color: var(--color-white);
  border: none;
  border-radius: var(--border-radius);
  font-weight: 500;
  cursor: pointer;
  transition: all var(--transition);
  display: flex;
  align-items: center;
  gap: var(--space-xs);
}

.ai-assistant__send:hover:not(:disabled) {
  background: var(--color-primary);
  transform: translateY(-1px);
}

.ai-assistant__send:disabled {
  background: var(--color-gray-medium);
  cursor: not-allowed;
  transform: none;
}

/* Connection Error */
.ai-assistant__error {
  background: #fef2f2;
  color: #dc2626;
  padding: var(--space-sm);
  margin: var(--space-sm);
  border-radius: var(--border-radius-sm);
  font-size: var(--font-size-xs);
  text-align: center;
  border: 1px solid #fecaca;
}

/* Mobile Responsive */
@media (max-width: 480px) {
  .ai-assistant {
    bottom: var(--space-sm);
    right: var(--space-sm);
  }

  .ai-assistant__window {
    width: min(calc(100vw - var(--space-lg)), 90vw);
    height: min(400px, 70vh);
    max-width: 90vw;
    max-height: 70vh;
  }

  .ai-assistant__toggle {
    width: 50px;
    height: 50px;
    font-size: 24px;
  }
}

/* Dark mode support (if you add it later) */
@media (prefers-color-scheme: dark) {
  .ai-assistant__window {
    background: #1f2937;
    border-color: #374151;
  }

  .ai-assistant__messages {
    background: #111827;
  }

  .ai-message--assistant .ai-message__content {
    background: #1f2937;
    color: #e5e7eb;
  }

  .ai-assistant__input-container {
    background: #1f2937;
    border-top-color: #374151;
  }

  .ai-assistant__input {
    background: #111827;
    border-color: #374151;
    color: #e5e7eb;
  }
}

/* Resize Handles (dynamically shown based on window position) */
.ai-assistant__resize-handle {
  position: absolute;
  z-index: 10;
  user-select: none;
  -webkit-user-select: none;
  display: none; /* Hidden by default, shown dynamically */
}

.ai-assistant__resize-handle--active {
  display: block;
}

/* Top-left corner */
.ai-assistant__resize-handle--nw {
  top: 0;
  left: 0;
  width: 20px;
  height: 20px;
  cursor: nw-resize;
}

.ai-assistant__resize-handle--nw::after {
  content: '';
  position: absolute;
  width: 10px;
  height: 10px;
  background: rgba(74, 144, 226, 0.3);
  border: 1px solid rgba(74, 144, 226, 0.5);
  border-radius: 2px;
  top: 4px;
  left: 4px;
}

/* Top-right corner */
.ai-assistant__resize-handle--ne {
  top: 0;
  right: 0;
  width: 20px;
  height: 20px;
  cursor: ne-resize;
}

.ai-assistant__resize-handle--ne::after {
  content: '';
  position: absolute;
  width: 10px;
  height: 10px;
  background: rgba(74, 144, 226, 0.3);
  border: 1px solid rgba(74, 144, 226, 0.5);
  border-radius: 2px;
  top: 4px;
  right: 4px;
}

/* Bottom-left corner */
.ai-assistant__resize-handle--sw {
  bottom: 0;
  left: 0;
  width: 20px;
  height: 20px;
  cursor: sw-resize;
}

.ai-assistant__resize-handle--sw::after {
  content: '';
  position: absolute;
  width: 10px;
  height: 10px;
  background: rgba(74, 144, 226, 0.3);
  border: 1px solid rgba(74, 144, 226, 0.5);
  border-radius: 2px;
  bottom: 4px;
  left: 4px;
}

/* Bottom-right corner */
.ai-assistant__resize-handle--se {
  bottom: 0;
  right: 0;
  width: 20px;
  height: 20px;
  cursor: se-resize;
}

.ai-assistant__resize-handle--se::after {
  content: '';
  position: absolute;
  width: 10px;
  height: 10px;
  background: rgba(74, 144, 226, 0.3);
  border: 1px solid rgba(74, 144, 226, 0.5);
  border-radius: 2px;
  bottom: 4px;
  right: 4px;
}

/* Mention Dropdown */
.ai-mention-dropdown {
  position: absolute;
  bottom: 100%;
  left: 0;
  width: 100%;
  max-height: 200px;
  background: var(--color-white);
  border: 1px solid var(--color-border);
  border-radius: var(--border-radius-sm);
  box-shadow: 0 -4px 16px rgba(0, 0, 0, 0.15);
  overflow-y: auto;
  z-index: 1000;
  margin-bottom: 4px;
}

.ai-mention-dropdown__title {
  padding: 8px 12px;
  font-size: 0.75rem;
  font-weight: 600;
  color: var(--color-text-secondary);
  text-transform: uppercase;
  letter-spacing: 0.5px;
  background: linear-gradient(to bottom, rgba(74, 144, 226, 0.03), transparent);
  border-bottom: 1px solid rgba(0, 0, 0, 0.08);
  position: sticky;
  top: 0;
  z-index: 1;
}

.ai-mention-dropdown__option {
  display: flex;
  align-items: center;
  gap: var(--space-sm);
  padding: 10px 12px;
  cursor: pointer;
  transition: all 0.2s ease;
  border-bottom: 1px solid rgba(0, 0, 0, 0.06);
  position: relative;
}

.ai-mention-dropdown__option:last-child {
  border-bottom: none;
}

.ai-mention-dropdown__option:hover {
  background: linear-gradient(to right, rgba(74, 144, 226, 0.08), rgba(74, 144, 226, 0.04));
}

.ai-mention-dropdown__option--highlighted {
  background: linear-gradient(to right, rgba(74, 144, 226, 0.12), rgba(74, 144, 226, 0.06));
  border-left: 3px solid var(--color-primary);
  padding-left: 9px;
}

.ai-mention-dropdown__option--highlighted .ai-mention-dropdown__label {
  color: var(--color-primary);
  font-weight: 600;
}

.ai-mention-dropdown__icon {
  font-size: 1.3rem;
  flex-shrink: 0;
  transition: transform 0.2s ease;
}

.ai-mention-dropdown__option:hover .ai-mention-dropdown__icon,
.ai-mention-dropdown__option--highlighted .ai-mention-dropdown__icon {
  transform: scale(1.1);
}

.ai-mention-dropdown__label {
  font-size: 0.9rem;
  color: var(--color-text-primary);
  font-weight: 500;
  transition: all 0.2s ease;
}

/* Input container needs relative positioning for dropdown */
.ai-assistant__input-container {
  position: relative;
}

/* Context tag styles */
.ai-context-tag {
  display: inline-flex;
  align-items: center;
  gap: 4px;
  padding: 2px 8px;
  border-radius: 4px;
  font-size: 0.85rem;
  font-weight: 500;
  user-select: none;
  vertical-align: baseline;
  margin: 0 2px;
  white-space: nowrap;
}

.ai-context-tag--beer {
  background-color: rgba(255, 165, 0, 0.2);
  color: #cc8400;
  border: 1px solid rgba(255, 165, 0, 0.4);
}

.ai-context-tag--paper {
  background-color: rgba(70, 130, 180, 0.2);
  color: #3a6b9e;
  border: 1px solid rgba(70, 130, 180, 0.4);
}