/**
 * AnythingLLM Unified Markdown Styles
 * Works for all widget types: embed, bubble, shortcode
 */

/* Base markdown styles for all message content */
.anythingllm-message-content,
.anythingllm-message-text,
.anythingllm-saved-text {
  word-wrap: break-word;
  word-break: break-word;
  overflow-wrap: break-word;
  line-height: 1.6;
}

/* Headers */
.anythingllm-message-content h1,
.anythingllm-message-content h2,
.anythingllm-message-content h3,
.anythingllm-message-content h4,
.anythingllm-message-content h5,
.anythingllm-message-content h6,
.anythingllm-message-text h1,
.anythingllm-message-text h2,
.anythingllm-message-text h3,
.anythingllm-message-text h4,
.anythingllm-message-text h5,
.anythingllm-message-text h6,
.anythingllm-saved-text h1,
.anythingllm-saved-text h2,
.anythingllm-saved-text h3,
.anythingllm-saved-text h4,
.anythingllm-saved-text h5,
.anythingllm-saved-text h6 {
  margin: 1.2em 0 0.8em 0 !important;
  font-weight: 700 !important;
  line-height: 1.3 !important;
  color: inherit !important;
}

.anythingllm-message-content h1:first-child,
.anythingllm-message-content h2:first-child,
.anythingllm-message-content h3:first-child,
.anythingllm-message-content h4:first-child,
.anythingllm-message-content h5:first-child,
.anythingllm-message-content h6:first-child,
.anythingllm-message-text h1:first-child,
.anythingllm-message-text h2:first-child,
.anythingllm-message-text h3:first-child,
.anythingllm-message-text h4:first-child,
.anythingllm-message-text h5:first-child,
.anythingllm-message-text h6:first-child,
.anythingllm-saved-text h1:first-child,
.anythingllm-saved-text h2:first-child,
.anythingllm-saved-text h3:first-child,
.anythingllm-saved-text h4:first-child,
.anythingllm-saved-text h5:first-child,
.anythingllm-saved-text h6:first-child {
  margin-top: 0 !important;
}

/* Header sizes */
.anythingllm-message-content h1,
.anythingllm-message-text h1,
.anythingllm-saved-text h1 {
  font-size: 1.8em !important;
  border-bottom: 2px solid #e5e7eb !important;
  padding-bottom: 0.5em !important;
}

.anythingllm-message-content h2,
.anythingllm-message-text h2,
.anythingllm-saved-text h2 {
  font-size: 1.5em !important;
  border-bottom: 1px solid #f3f4f6 !important;
  padding-bottom: 0.3em !important;
}

.anythingllm-message-content h3,
.anythingllm-message-text h3,
.anythingllm-saved-text h3 {
  font-size: 1.3em !important;
}

.anythingllm-message-content h4,
.anythingllm-message-text h4,
.anythingllm-saved-text h4 {
  font-size: 1.1em !important;
}

.anythingllm-message-content h5,
.anythingllm-message-text h5,
.anythingllm-saved-text h5 {
  font-size: 1em !important;
}

.anythingllm-message-content h6,
.anythingllm-message-text h6,
.anythingllm-saved-text h6 {
  font-size: 0.9em !important;
  color: inherit !important;
}

/* Paragraphs */
.anythingllm-message-content p,
.anythingllm-message-text p,
.anythingllm-saved-text p {
  margin: 0.8em 0 !important;
  line-height: 1.6 !important;
}

.anythingllm-message-content p:first-child,
.anythingllm-message-text p:first-child,
.anythingllm-saved-text p:first-child {
  margin-top: 0 !important;
}

.anythingllm-message-content p:last-child,
.anythingllm-message-text p:last-child,
.anythingllm-saved-text p:last-child {
  margin-bottom: 0 !important;
}

/* Lists */
.anythingllm-message-content ul,
.anythingllm-message-content ol,
.anythingllm-message-text ul,
.anythingllm-message-text ol,
.anythingllm-saved-text ul,
.anythingllm-saved-text ol {
  margin: 0.8em 0 !important;
  padding-left: 1.8em !important;
  width: 100% !important;
  box-sizing: border-box !important;
}

.anythingllm-message-content li,
.anythingllm-message-text li,
.anythingllm-saved-text li {
  margin: 0.4em 0 !important;
  line-height: 1.6 !important;
  word-wrap: break-word !important;
  word-break: break-word !important;
  overflow-wrap: break-word !important;
}

/* Text formatting */
.anythingllm-message-content strong,
.anythingllm-message-text strong,
.anythingllm-saved-text strong {
  font-weight: 700 !important;
  color: #1f2937 !important;
}

.anythingllm-message-content em,
.anythingllm-message-text em,
.anythingllm-saved-text em {
  font-style: italic !important;
  color: #6b7280 !important;
}

/* Links (ensure readable contrast across themes) */
.anythingllm-message-content a,
.anythingllm-message-text a,
.anythingllm-saved-text a {
  color: #4f46e5 !important; /* indigo-600 */
  text-decoration: underline !important;
}
.anythingllm-message-content a:hover,
.anythingllm-message-text a:hover,
.anythingllm-saved-text a:hover {
  color: #3730a3 !important; /* indigo-700 */
}

/* Code */
.anythingllm-message-content code,
.anythingllm-message-text code,
.anythingllm-saved-text code {
  background: rgba(0, 0, 0, 0.1) !important;
  padding: 2px 6px !important;
  border-radius: 4px !important;
  font-family: 'Monaco', 'Menlo', 'Ubuntu Mono', monospace !important;
  font-size: 0.9em !important;
}

.anythingllm-message-content pre,
.anythingllm-message-text pre,
.anythingllm-saved-text pre {
  background: #f8fafc !important;
  border: 1px solid #e2e8f0 !important;
  border-radius: 6px !important;
  padding: 12px !important;
  margin: 8px 0 !important;
  overflow-x: auto !important;
  font-family: 'Monaco', 'Menlo', 'Ubuntu Mono', monospace !important;
  font-size: 0.9em !important;
}

.anythingllm-message-content pre code,
.anythingllm-message-text pre code,
.anythingllm-saved-text pre code {
  background: none !important;
  padding: 0 !important;
  border-radius: 0 !important;
}

/* Blockquotes */
.anythingllm-message-content blockquote,
.anythingllm-message-text blockquote,
.anythingllm-saved-text blockquote {
  border-left: 4px solid #e2e8f0 !important;
  margin: 8px 0 !important;
  padding: 8px 0 8px 16px !important;
  color: #64748b !important;
  font-style: italic !important;
}

/* Tables */
.anythingllm-message-content .anythingllm-table-wrap,
.anythingllm-message-text .anythingllm-table-wrap,
.anythingllm-saved-text .anythingllm-table-wrap {
  width: 100% !important;
  overflow-x: auto !important;
  -webkit-overflow-scrolling: touch !important;
  margin: 1.5em 0 !important;
  border-radius: 12px !important;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.04) !important;
  max-width: 100% !important;
  display: block !important;
}

.anythingllm-message-content table,
.anythingllm-message-text table,
.anythingllm-saved-text table {
  /* Allow horizontal scroll instead of squashing columns */
  width: max-content !important;
  min-width: 480px !important;
  border-collapse: collapse !important;
  background: #ffffff !important;
  font-size: 14px !important;
  border: 1px solid #e5e7eb !important;
  margin: 0 !important;
}

.anythingllm-message-content table th,
.anythingllm-message-content table td,
.anythingllm-message-text table th,
.anythingllm-message-text table td,
.anythingllm-saved-text table th,
.anythingllm-saved-text table td {
  padding: 12px 16px !important;
  text-align: left !important;
  border-bottom: 1px solid #f3f4f6 !important;
  white-space: normal !important;
  word-break: normal !important;
  overflow-wrap: break-word !important;
  hyphens: auto !important;
}

.anythingllm-message-content table th,
.anythingllm-message-text table th,
.anythingllm-saved-text table th {
  background: #f9fafb !important;
  font-weight: 600 !important;
  color: #374151 !important;
  font-size: 13px !important;
  text-transform: uppercase !important;
  letter-spacing: 0.5px !important;
  border-bottom: 2px solid #e5e7eb !important;
  white-space: normal !important;
  word-break: normal !important;
  overflow-wrap: normal !important;
}

/* Auto-stacked layout for very small screens when wrapper switches to stack mode */
@media (max-width: 640px) {
  .anythingllm-message-content .anythingllm-table-wrap[data-stack="true"],
  .anythingllm-message-text .anythingllm-table-wrap[data-stack="true"],
  .anythingllm-saved-text .anythingllm-table-wrap[data-stack="true"] { overflow-x: auto !important; }
  .anythingllm-message-content .anythingllm-table-wrap[data-stack="true"] table,
  .anythingllm-message-text .anythingllm-table-wrap[data-stack="true"] table,
  .anythingllm-saved-text .anythingllm-table-wrap[data-stack="true"] table { width: 100% !important; min-width: 100% !important; }
  .anythingllm-message-content .anythingllm-table-wrap[data-stack="true"] thead,
  .anythingllm-message-text .anythingllm-table-wrap[data-stack="true"] thead,
  .anythingllm-saved-text .anythingllm-table-wrap[data-stack="true"] thead { display: none !important; }
  .anythingllm-message-content .anythingllm-table-wrap[data-stack="true"] tbody tr,
  .anythingllm-message-text .anythingllm-table-wrap[data-stack="true"] tbody tr,
  .anythingllm-saved-text .anythingllm-table-wrap[data-stack="true"] tbody tr { display: block !important; margin: 8px 0 !important; border-radius: 8px !important; }
  .anythingllm-message-content .anythingllm-table-wrap[data-stack="true"] td,
  .anythingllm-message-text .anythingllm-table-wrap[data-stack="true"] td,
  .anythingllm-saved-text .anythingllm-table-wrap[data-stack="true"] td { display: grid !important; grid-template-columns: 140px 1fr !important; align-items: start !important; }
  .anythingllm-message-content .anythingllm-table-wrap[data-stack="true"] td::before,
  .anythingllm-message-text .anythingllm-table-wrap[data-stack="true"] td::before,
  .anythingllm-saved-text .anythingllm-table-wrap[data-stack="true"] td::before { content: attr(data-label) !important; font-weight: 600 !important; color: #374151 !important; padding-right: 8px !important; }
}

.anythingllm-message-content table tr:hover,
.anythingllm-message-text table tr:hover,
.anythingllm-saved-text table tr:hover {
  background: #f9fafb !important;
}

/* Dark theme support */
:host(.anythingllm-dark) .anythingllm-message-content h1,
:host(.anythingllm-dark) .anythingllm-message-content h2,
:host(.anythingllm-dark) .anythingllm-message-content h3,
:host(.anythingllm-dark) .anythingllm-message-content h4,
:host(.anythingllm-dark) .anythingllm-message-content h5,
:host(.anythingllm-dark) .anythingllm-message-content h6,
:host(.anythingllm-dark) .anythingllm-message-text h1,
:host(.anythingllm-dark) .anythingllm-message-text h2,
:host(.anythingllm-dark) .anythingllm-message-text h3,
:host(.anythingllm-dark) .anythingllm-message-text h4,
:host(.anythingllm-dark) .anythingllm-message-text h5,
:host(.anythingllm-dark) .anythingllm-message-text h6 {
  color: inherit !important;
}

:host(.anythingllm-dark) .anythingllm-message-content strong,
:host(.anythingllm-dark) .anythingllm-message-text strong {
  color: #f9fafb !important;
}

:host(.anythingllm-dark) .anythingllm-message-content em,
:host(.anythingllm-dark) .anythingllm-message-text em {
  color: #e5e7eb !important; /* improve italic text contrast */
}

:host(.anythingllm-dark) .anythingllm-message-content code,
:host(.anythingllm-dark) .anythingllm-message-text code {
  background: rgba(255, 255, 255, 0.1) !important;
}

:host(.anythingllm-dark) .anythingllm-message-content pre,
:host(.anythingllm-dark) .anythingllm-message-text pre {
  background: #374151 !important;
  border-color: #4b5563 !important;
  color: #f9fafb !important;
}

:host(.anythingllm-dark) .anythingllm-message-content blockquote,
:host(.anythingllm-dark) .anythingllm-message-text blockquote {
  border-left-color: #4b5563 !important;
  color: #ffffff !important;
  background: rgba(255, 255, 255, 0.04) !important; /* subtle readable backdrop */
}

:host(.anythingllm-dark) .anythingllm-message-content table,
:host(.anythingllm-dark) .anythingllm-message-text table {
  background: #374151 !important;
  border-color: #4b5563 !important;
}

:host(.anythingllm-dark) .anythingllm-message-content table th,
:host(.anythingllm-dark) .anythingllm-message-text table th {
  background: #4b5563 !important;
  color: #f3f4f6 !important;
  border-bottom-color: #6b7280 !important;
}

:host(.anythingllm-dark) .anythingllm-message-content table td,
:host(.anythingllm-dark) .anythingllm-message-text table td {
  color: #f9fafb !important;
  border-bottom-color: #4b5563 !important;
}

:host(.anythingllm-dark) .anythingllm-message-content table tr:hover,
:host(.anythingllm-dark) .anythingllm-message-text table tr:hover {
  background: #4b5563 !important;
}

/* Dark theme: readable links */
:host(.anythingllm-dark) .anythingllm-message-content a,
:host(.anythingllm-dark) .anythingllm-message-text a {
  color: #a5b4fc !important; /* indigo-300 */
  text-decoration: underline !important;
}
:host(.anythingllm-dark) .anythingllm-message-content a:hover,
:host(.anythingllm-dark) .anythingllm-message-text a:hover {
  color: #c7d2fe !important; /* indigo-200 */
}

/* Dark theme: stacked table labels contrast */
:host(.anythingllm-dark) .anythingllm-message-content .anythingllm-table-wrap[data-stack="true"] td::before,
:host(.anythingllm-dark) .anythingllm-message-text .anythingllm-table-wrap[data-stack="true"] td::before {
  color: #e5e7eb !important;
}

/* Dark theme support for saved answers outside Shadow DOM */
.anythingllm-dark .anythingllm-saved-text h1,
.anythingllm-dark .anythingllm-saved-text h2,
.anythingllm-dark .anythingllm-saved-text h3,
.anythingllm-dark .anythingllm-saved-text h4,
.anythingllm-dark .anythingllm-saved-text h5,
.anythingllm-dark .anythingllm-saved-text h6,
.anythingllm-saved-panel.anythingllm-dark .anythingllm-saved-text h1,
.anythingllm-saved-panel.anythingllm-dark .anythingllm-saved-text h2,
.anythingllm-saved-panel.anythingllm-dark .anythingllm-saved-text h3,
.anythingllm-saved-panel.anythingllm-dark .anythingllm-saved-text h4,
.anythingllm-saved-panel.anythingllm-dark .anythingllm-saved-text h5,
.anythingllm-saved-panel.anythingllm-dark .anythingllm-saved-text h6 {
  color: inherit !important;
  border-bottom-color: #374151 !important;
}
.anythingllm-dark .anythingllm-saved-text strong,
.anythingllm-saved-panel.anythingllm-dark .anythingllm-saved-text strong {
  color: #f9fafb !important;
}
.anythingllm-dark .anythingllm-saved-text em,
.anythingllm-saved-panel.anythingllm-dark .anythingllm-saved-text em {
  color: #e5e7eb !important;
}
.anythingllm-dark .anythingllm-saved-text hr,
.anythingllm-saved-panel.anythingllm-dark .anythingllm-saved-text hr {
  border-color: #374151 !important;
  background: #374151 !important;
}
.anythingllm-dark .anythingllm-saved-text code,
.anythingllm-saved-panel.anythingllm-dark .anythingllm-saved-text code {
  background: rgba(255, 255, 255, 0.08) !important;
  color: #e5e7eb !important;
}
.anythingllm-dark .anythingllm-saved-text pre,
.anythingllm-saved-panel.anythingllm-dark .anythingllm-saved-text pre {
  background: #0a0f1f !important;
  color: #e5e7eb !important;
  border-color: #374151 !important;
}
.anythingllm-dark .anythingllm-saved-text blockquote,
.anythingllm-saved-panel.anythingllm-dark .anythingllm-saved-text blockquote {
  border-left-color: #475569 !important;
  color: #ffffff !important;
}
.anythingllm-dark .anythingllm-saved-text a,
.anythingllm-saved-panel.anythingllm-dark .anythingllm-saved-text a {
  color: #a5b4fc !important;
}
.anythingllm-dark .anythingllm-saved-text a:hover,
.anythingllm-saved-panel.anythingllm-dark .anythingllm-saved-text a:hover {
  color: #c7d2fe !important;
}
.anythingllm-dark .anythingllm-saved-text table,
.anythingllm-saved-panel.anythingllm-dark .anythingllm-saved-text table {
  background: #0b1220 !important;
  color: #e5e7eb !important;
  border-color: #374151 !important;
}
.anythingllm-dark .anythingllm-saved-text table tr:hover,
.anythingllm-saved-panel.anythingllm-dark .anythingllm-saved-text table tr:hover {
  background: #182030 !important;
}
.anythingllm-dark .anythingllm-saved-text .anythingllm-table-wrap[data-stack="true"] td::before,
.anythingllm-saved-panel.anythingllm-dark .anythingllm-saved-text .anythingllm-table-wrap[data-stack="true"] td::before {
  color: #e5e7eb !important;
}
.anythingllm-dark .anythingllm-saved-text thead th,
.anythingllm-saved-panel.anythingllm-dark .anythingllm-saved-text thead th {
  background: #182030 !important;
  color: #e5e7eb !important;
}
.anythingllm-dark .anythingllm-saved-text th,
.anythingllm-dark .anythingllm-saved-text td,
.anythingllm-saved-panel.anythingllm-dark .anythingllm-saved-text th,
.anythingllm-saved-panel.anythingllm-dark .anythingllm-saved-text td {
  border-color: #374151 !important;
}
.anythingllm-dark .anythingllm-saved-text tbody tr:nth-child(odd) td,
.anythingllm-saved-panel.anythingllm-dark .anythingllm-saved-text tbody tr:nth-child(odd) td {
  background: #0a0f1f !important;
}

/* Responsive design */
@media (max-width: 768px) {
  .anythingllm-message-content h1,
  .anythingllm-message-text h1,
  .anythingllm-saved-text h1 {
    font-size: 1.5em !important;
  }
  
  .anythingllm-message-content h2,
  .anythingllm-message-text h2,
  .anythingllm-saved-text h2 {
    font-size: 1.3em !important;
  }
  
  .anythingllm-message-content table,
  .anythingllm-message-text table,
  .anythingllm-saved-text table {
    font-size: 13px !important;
    min-width: 400px !important;
  }
  
  .anythingllm-message-content table th,
  .anythingllm-message-content table td,
  .anythingllm-message-text table th,
  .anythingllm-message-text table td,
  .anythingllm-saved-text table th,
  .anythingllm-saved-text table td {
    padding: 10px 12px !important;
  }
}

@media (max-width: 480px) {
  .anythingllm-message-content h1,
  .anythingllm-message-text h1,
  .anythingllm-saved-text h1 {
    font-size: 1.3em !important;
  }
  
  .anythingllm-message-content h2,
  .anythingllm-message-text h2,
  .anythingllm-saved-text h2 {
    font-size: 1.2em !important;
  }
  
  .anythingllm-message-content table,
  .anythingllm-message-text table,
  .anythingllm-saved-text table {
    font-size: 12px !important;
    min-width: 350px !important;
  }
  
  .anythingllm-message-content table th,
  .anythingllm-message-content table td,
  .anythingllm-message-text table th,
  .anythingllm-message-text table td,
  .anythingllm-saved-text table th,
  .anythingllm-saved-text table td {
    padding: 8px 10px !important;
  }
}

/* Specific styles for anythingllm-table class */
.anythingllm-table {
  width: 100% !important;
  border-collapse: collapse !important;
  background: #ffffff !important;
  font-size: 14px !important;
  border: 1px solid #e5e7eb !important;
  margin: 10px 0 !important;
}

.anythingllm-table th {
  background: #f9fafb !important;
  font-weight: 600 !important;
  color: #374151 !important;
  font-size: 13px !important;
  padding: 12px 16px !important;
  text-align: left !important;
  border-bottom: 2px solid #e5e7eb !important;
  border-right: 1px solid #e5e7eb !important;
}

.anythingllm-table td {
  padding: 12px 16px !important;
  text-align: left !important;
  border-bottom: 1px solid #f3f4f6 !important;
  border-right: 1px solid #e5e7eb !important;
  word-wrap: break-word !important;
  word-break: break-word !important;
  overflow-wrap: break-word !important;
}

.anythingllm-table tr:hover {
  background: #f9fafb !important;
}

.anythingllm-table th:last-child,
.anythingllm-table td:last-child {
  border-right: none !important;
}

/* Dark theme support for anythingllm-table */
:host(.anythingllm-dark) .anythingllm-table {
  background: #1f2937 !important;
  border: 1px solid #374151 !important;
}

:host(.anythingllm-dark) .anythingllm-table th {
  background: #374151 !important;
  color: #f9fafb !important;
  border-bottom: 2px solid #4b5563 !important;
  border-right: 1px solid #4b5563 !important;
}

:host(.anythingllm-dark) .anythingllm-table td {
  color: #e5e7eb !important;
  border-bottom: 1px solid #374151 !important;
  border-right: 1px solid #4b5563 !important;
}

:host(.anythingllm-dark) .anythingllm-table tr:hover {
  background: #374151 !important;
}