/* Plot styling - match code block borders */
/* Bootstrap 5 Sandstone uses --bs-border-radius: .375rem for pre/code */
img.r-plt,
.figure img,
figure img,
img.figure-img,
svg.inline-svg {
  max-width: 100%;
  width: 100%;
  height: auto;
  border: 1px solid #DFD7CA;
  border-radius: .375rem;
  overflow: hidden;
  display: block;
  margin-bottom: 1rem;
}

/* Mobile: code block text wrapping (LaTeX-style) */
@media (max-width: 575.98px) {
  pre.sourceCode, .sourceCode pre {
    white-space: pre-wrap !important;
    word-break: break-all !important;
    overflow-x: hidden !important;
    hyphens: auto !important;
    -webkit-hyphens: auto !important;
  }
  pre.sourceCode code, .sourceCode pre code {
    white-space: pre-wrap !important;
    word-break: break-all !important;
    hyphens: auto !important;
    -webkit-hyphens: auto !important;
  }
}

/* Mobile: match code block full-width behavior */
@media (max-width: 575.98px) {
  p:has(> svg.inline-svg), p:has(> img.r-plt) {
    margin-left: calc(var(--bs-gutter-x, 1.5rem) * -.5) !important;
    margin-right: calc(var(--bs-gutter-x, 1.5rem) * -.5) !important;
    width: calc(100% + var(--bs-gutter-x, 1.5rem)) !important;
    max-width: none !important;
  }
  p > svg.inline-svg, p > img.r-plt {
    border-radius: 0 !important;
    border: 1px solid #DFD7CA !important;
    width: 100% !important;
    max-width: none !important;
  }
  [data-bs-theme="dark"] p > svg.inline-svg,
  [data-bs-theme="dark"] p > img.r-plt {
    border: 1px solid #495057 !important;
  }
  .section .figure, .section figure, div > div > .figure, div > div > figure {
    margin-left: calc(var(--bs-gutter-x, 1.5rem) * -.5) !important;
    margin-right: calc(var(--bs-gutter-x, 1.5rem) * -.5) !important;
    width: calc(100% + var(--bs-gutter-x, 1.5rem)) !important;
    max-width: none !important;
    border-radius: 0;
  }
  .section .figure img, .section figure img,
  div > div > .figure img, div > div > figure img {
    border-radius: 0 !important;
    width: 100% !important;
    max-width: none !important;
  }
}

/* Dark mode border */
[data-bs-theme="dark"] img.r-plt, [data-bs-theme="dark"] .figure img,
[data-bs-theme="dark"] figure img, [data-bs-theme="dark"] img.figure-img,
[data-bs-theme="dark"] svg.inline-svg {
  border-color: #495057;
}

/* Dark mode colors for base R svg format */
[data-bs-theme="dark"] .theme-bg { fill: #343739 !important; }
[data-bs-theme="dark"] .theme-text { fill: #DFD7CA !important; }

/* Hide clipPath rects */
svg.inline-svg clipPath rect, svg.inline-svg defs rect {
  stroke: none !important;
  fill: none !important;
}

/* Dark mode svglite format */
[data-bs-theme="dark"] svg.inline-svg .svg-bg {
  fill: #343739 !important;
  stroke: none !important;
}
svg.inline-svg .svg-bg { stroke: none !important; }
/* Dark mode stroke override */
[data-bs-theme="dark"] svg.inline-svg .svglite line:not([style*="stroke:"]),
[data-bs-theme="dark"] svg.inline-svg .svglite polyline:not([style*="stroke:"]),
[data-bs-theme="dark"] svg.inline-svg .svglite polygon:not([style*="stroke:"]),
[data-bs-theme="dark"] svg.inline-svg .svglite path:not([style*="stroke:"]),
[data-bs-theme="dark"] svg.inline-svg .svglite circle:not([style*="stroke:"]) {
  stroke: #DFD7CA !important;
}

[data-bs-theme="dark"] svg.inline-svg .svglite line[style*="stroke: #3E3F3A"],
[data-bs-theme="dark"] svg.inline-svg .svglite polyline[style*="stroke: #3E3F3A"],
[data-bs-theme="dark"] svg.inline-svg .svglite polygon[style*="stroke: #3E3F3A"],
[data-bs-theme="dark"] svg.inline-svg .svglite path[style*="stroke: #3E3F3A"],
[data-bs-theme="dark"] svg.inline-svg .svglite circle[style*="stroke: #3E3F3A"] {
  stroke: #DFD7CA !important;
}

[data-bs-theme="dark"] svg.inline-svg .svglite text:not(.preserve-color) {
  fill: #DFD7CA !important;
}
svg.inline-svg .svglite text.preserve-color { fill: #3E3F3A !important; }

svg.inline-svg .svglite > g > rect[rx] { stroke: none !important; }

[data-bs-theme="dark"] svg.inline-svg .svglite rect[style*="fill: #F5F6F8"]:not(.svg-bg),
[data-bs-theme="dark"] svg.inline-svg .svglite rect.legend-bg {
  fill: #343739 !important;
}

/* Grid lines */
svg.inline-svg .svglite polyline[style*="stroke: #FFFFFF"],
svg.inline-svg .svglite polyline[style*="stroke: #ffffff"] {
  stroke: #3E3F3A !important;
}
[data-bs-theme="dark"] svg.inline-svg .svglite polyline[style*="stroke: #FFFFFF"],
[data-bs-theme="dark"] svg.inline-svg .svglite polyline[style*="stroke: #ffffff"],
[data-bs-theme="dark"] svg.inline-svg .svglite polyline[style*="stroke: #3E3F3A"],
[data-bs-theme="dark"] svg.inline-svg .svglite polyline[style*="stroke: #3e3f3a"] {
  stroke: #DFD7CA !important;
}

[data-bs-theme="dark"] svg.inline-svg .svglite line[style*="stroke: #EBEBEB"],
[data-bs-theme="dark"] svg.inline-svg .svglite line[style*="stroke: #ebebeb"],
[data-bs-theme="dark"] svg.inline-svg .svglite polyline[style*="stroke: #EBEBEB"],
[data-bs-theme="dark"] svg.inline-svg .svglite polyline[style*="stroke: #ebebeb"] {
  stroke: #495057 !important;
}

[data-bs-theme="dark"] svg.inline-svg line[style*="stroke: #EBEBEB"],
[data-bs-theme="dark"] svg.inline-svg line[style*="stroke: #ebebeb"],
[data-bs-theme="dark"] svg.inline-svg line[style*="stroke: grey"],
[data-bs-theme="dark"] svg.inline-svg line[style*="stroke: gray"] {
  stroke: #495057 !important;
}
/* SVG copy button */
.svg-container { position: relative; display: block; }
.btn-copy-svg {
  position: absolute;
  top: 0.5rem;
  right: 0.5rem;
  opacity: 0;
  transition: opacity 0.15s ease-in-out;
  z-index: 10;
  padding: 0.25rem 0.5rem;
  font-size: 0.875rem;
}
.svg-container:hover .btn-copy-svg,
.svg-container:focus-within .btn-copy-svg { opacity: 1; }
.btn-copy-svg:focus { opacity: 1; }

@media (max-width: 575.98px) {
  .svg-container { position: relative; }
}

/* Math equations - responsive */
.MathJax_Display, mjx-container[display="true"], .katex-display {
  overflow-x: auto;
  overflow-y: hidden;
  max-width: 100%;
  padding: 0.5rem 0;
}
mjx-container:not([display="true"]), .katex:not(.katex-display .katex) {
  display: inline-block;
  max-width: 100%;
  overflow-x: auto;
  vertical-align: middle;
}

