/* ============================================
   New Styles for Enhanced Features
   ============================================ */

/* Copy Link Button */
.copy-link-btn {
  position: relative;
  background: none;
  border: 1px solid #e2e8f0;
  cursor: pointer;
  padding: 0.75rem;
  border-radius: 8px;
  color: #64748b;
  transition: all 0.2s ease;
  display: flex;
  align-items: center;
  gap: 0.5rem;
}

.copy-link-btn:hover {
  background: #f8fafc;
  border-color: #cbd5e1;
  color: #334155;
}

.copy-link-btn.copied {
  background: #ecfdf5;
  border-color: #10b981;
  color: #10b981;
}

.copy-tooltip {
  font-size: 0.75rem;
  font-weight: 500;
  white-space: nowrap;
}

/* Author Box with Dynamic Image */
.author-box-avatar {
  position: relative;
  width: 64px;
  height: 64px;
  border-radius: 50%;
  overflow: hidden;
  background: linear-gradient(135deg, #2563eb 0%, #1d4ed8 100%);
  display: flex;
  align-items: center;
  justify-content: center;
  flex-shrink: 0;
}

.author-avatar-img {
  width: 100%;
  height: 100%;
  object-fit: cover;
  border-radius: 50%;
}

.author-avatar-img.logo-style {
  object-fit: contain;
  padding: 8px;
  background: #fff;
}

/* Wider container for logo avatars */
.author-box-avatar.has-logo {
  width: auto;
  min-width: 64px;
  max-width: 140px;
  padding: 12px 16px;
  border-radius: 12px;
  background: linear-gradient(135deg, #f8fafc 0%, #f1f5f9 100%);
  border: 1px solid #e2e8f0;
}

.author-box-avatar.has-logo .author-avatar-img {
  width: auto;
  height: 40px;
  border-radius: 0;
  padding: 0;
  background: transparent;
}

.author-initials {
  color: #fff;
  font-size: 1.25rem;
  font-weight: 600;
  text-transform: uppercase;
}

/* Hero Author Avatar (smaller) */
.article-meta .author-avatar {
  width: 44px;
  height: 44px;
  border-radius: 50%;
  overflow: hidden;
  background: linear-gradient(135deg, #2563eb 0%, #1d4ed8 100%);
  display: flex;
  align-items: center;
  justify-content: center;
  flex-shrink: 0;
}

.hero-author-img {
  width: 100%;
  height: 100%;
  object-fit: cover;
  border-radius: 50%;
}

.hero-author-img.logo-style {
  object-fit: contain;
  padding: 6px;
  background: #fff;
}

/* Wider container for logo avatars in hero */
.article-meta .author-avatar.has-logo {
  width: auto;
  min-width: 44px;
  max-width: 110px;
  padding: 8px 12px;
  border-radius: 8px;
  background: linear-gradient(135deg, #f8fafc 0%, #f1f5f9 100%);
  border: 1px solid #e2e8f0;
}

.article-meta .author-avatar.has-logo .hero-author-img {
  width: auto;
  height: 28px;
  border-radius: 0;
  padding: 0;
  background: transparent;
}

.hero-author-initials {
  color: #fff;
  font-size: 0.875rem;
  font-weight: 600;
  text-transform: uppercase;
}

/* ============================================
   Hide Empty Featured Image Containers
   ============================================ */

/* Hide related card image container when img has no valid src */
.related-card-image:has(img[src=""]),
.related-card-image:has(img[src^="data:image/gif"]),
.related-card-image:has(img:not([src])) {
  display: none;
}

/* Hide article hero image container when empty */
.hero-image:has(img[src=""]),
.hero-image:has(img[src^="data:image/gif"]),
.hero-image:has(img:not([src])) {
  display: none;
}

/* When hero-image is hidden, make the grid single column so content takes full width */
.hero-container:has(.hero-image:empty),
.hero-container:has(.hero-image[style*="display: none"]),
.hero-container:has(.hero-image:has(img[src=""])),
.hero-container:has(.hero-image:has(img:not([src]))),
.hero-container:has(.hero-image.no-image) {
  grid-template-columns: 1fr;
}

/* Also handle case where hero-image was completely removed from DOM */
.hero-container:not(:has(.hero-image)) {
  grid-template-columns: 1fr;
}

/* SVG data URIs should maintain two-column layout (override single-column rules above) */
.hero-container:has(.hero-image:has(img[src^="data:image/svg"])),
.hero-container:has(.hero-image:has(img[src$=".svg"])) {
  grid-template-columns: 1fr 1fr;
}

/* When container is single column, allow hero-content to take full width */
.hero-container:not(:has(.hero-image)) .hero-content,
.hero-container:has(.hero-image.no-image) .hero-content {
  max-width: 100%;
}

/* Fallback for browsers without :has() support - hide via JS class */
.related-card-image.no-image,
.hero-image.no-image {
  display: none;
}

/* JS fallback: when hero-image has no-image class, make container single column */
.hero-container.single-column {
  grid-template-columns: 1fr;
}

/* JS fallback: allow hero-content full width in single column mode */
.hero-container.single-column .hero-content {
  max-width: 100%;
}

/* ============================================
   Hero Viewer (Spreadsheet in Hero Grid)
   ============================================ */

/* Collapse hero-viewer when empty (no xlsx attachments) */
.hero-viewer:empty {
  display: none;
}

/* --- Scenario A: No featured image, viewer exists --- */
/* When hero-image is hidden/removed AND viewer has content: two-column layout (content left, viewer right) */
.hero-container:not(:has(.hero-image)):has(.hero-viewer:not(:empty)) {
  grid-template-columns: 1fr 1fr;
}

.hero-container:has(.hero-image.no-image):has(.hero-viewer:not(:empty)) {
  grid-template-columns: 1fr 1fr;
}

.hero-container:has(.hero-image:has(img[src=""])):has(.hero-viewer:not(:empty)) {
  grid-template-columns: 1fr 1fr;
}

.hero-container:has(.hero-image:has(img:not([src]))):has(.hero-viewer:not(:empty)) {
  grid-template-columns: 1fr 1fr;
}

/* Style the viewer when it takes the image column position */
.hero-container:not(:has(.hero-image)) .hero-viewer,
.hero-container:has(.hero-image.no-image) .hero-viewer {
  width: 100%;
  max-height: 450px;
  overflow: hidden;
  border-radius: var(--radius-xl);
  box-shadow: var(--shadow-lg);
}

/* --- Scenario B: Featured image AND viewer both exist --- */
/* Viewer gets full-width row below the two-column header */
.hero-container:has(.hero-image img[src]:not([src=""]):not([src^="data:image/gif"])):has(.hero-viewer:not(:empty)) {
  grid-template-rows: auto auto;
}

.hero-container:has(.hero-image img[src]:not([src=""]):not([src^="data:image/gif"])) .hero-viewer {
  grid-column: 1 / -1;
  border-radius: var(--radius-xl);
  box-shadow: var(--shadow-lg);
  overflow: hidden;
  max-height: 450px;
}

/* SVG featured images also count as "image exists" */
.hero-container:has(.hero-image:has(img[src^="data:image/svg"])) .hero-viewer,
.hero-container:has(.hero-image:has(img[src$=".svg"])) .hero-viewer,
.hero-container:has(.hero-image > div:has(svg)) .hero-viewer {
  grid-column: 1 / -1;
}

/* --- JS Fallback classes (browsers without :has()) --- */
/* When JS detects viewer-replaces-image scenario */
.hero-container.viewer-replaces-image {
  grid-template-columns: 1fr 1fr;
}

.hero-container.viewer-replaces-image .hero-viewer {
  width: 100%;
  max-height: 450px;
  overflow: hidden;
  border-radius: var(--radius-xl);
  box-shadow: var(--shadow-lg);
}

/* When JS detects viewer-below-image scenario */
.hero-container.viewer-below-image .hero-viewer {
  grid-column: 1 / -1;
  border-radius: var(--radius-xl);
  box-shadow: var(--shadow-lg);
  overflow: hidden;
  max-height: 450px;
}

/* --- Responsive: tablet and below --- */
@media (max-width: 1024px) {
  /* On tablet, viewer stacks below content in single column */
  .hero-viewer {
    max-height: 400px;
  }

  /* Fix: Override Scenario A two-column back to single column on tablet/mobile */
  .hero-container:not(:has(.hero-image)):has(.hero-viewer:not(:empty)) {
    grid-template-columns: 1fr;
  }
  .hero-container:has(.hero-image.no-image):has(.hero-viewer:not(:empty)) {
    grid-template-columns: 1fr;
  }
  .hero-container:has(.hero-image:has(img[src=""])):has(.hero-viewer:not(:empty)) {
    grid-template-columns: 1fr;
  }
  .hero-container:has(.hero-image:has(img:not([src]))):has(.hero-viewer:not(:empty)) {
    grid-template-columns: 1fr;
  }

  /* Fix: JS fallback class */
  .hero-container.viewer-replaces-image {
    grid-template-columns: 1fr;
  }
}

@media (max-width: 768px) {
  .hero-viewer {
    max-height: 350px;
  }
}

/* Show placeholder when image is missing */
.related-card-image:has(img[src=""]) .related-card-placeholder,
.related-card-image:has(img:not([src])) .related-card-placeholder {
  display: flex !important;
}

/* ============================================
   Responsive Overflow Clamps
   ============================================ */

/* audit:overflow-clamp */
/* Display math (block equations) - centered and full width */
mjx-container[display="true"],
.MathJax_Display {
  max-width: 100%;
  overflow-x: auto;
  display: block;
  text-align: left;
  width: 100%;
}

/* Inline math - stays inline with text (important for lists!) */
mjx-container:not([display="true"]) {
  display: inline;
  max-width: 100%;
  overflow-x: auto;
}

pre,
code {
  overflow-x: auto;
}

img,
svg,
video,
iframe {
  max-width: 100%;
  height: auto;
}

/* ============================================
   Related Card - No Image Handling
   ============================================ */

/* Hide related card image container when image fails to load */
.related-card-image:has(img[src=""]),
.related-card-image:has(img[src^="data:image/gif"]),
.related-card-image:has(img:not([src])) {
  display: none;
}

/* Hide related card image container when completely empty (no img tag at all) */
.related-card-image:empty {
  display: none;
}

/* Related card without image - add padding to content for visual balance */
.related-card:has(.related-card-image[style*="display: none"]) .related-card-content,
.related-card:has(.related-card-image:empty) .related-card-content {
  padding: 32px 20px;
  min-height: 140px;
  display: flex;
  flex-direction: column;
  justify-content: center;
}

/* JS fallback for browsers without :has() support */
.related-card.no-image .related-card-content {
  padding: 32px 20px;
  min-height: 140px;
  display: flex;
  flex-direction: column;
  justify-content: center;
}
