/* Chapter presentation overrides for LaTeXML/ar5iv output */

/* Shared design tokens used across all pages (landing, contributors, chapters) */
:root {
  --bg: #ffffff;
  --bg-soft: #fafbff;
  --panel: #ffffff;
  --accent: #3b82f6;
  --accent-2: #6366f1;
  --text: #0b1020;
  --muted: #475569;
  --border: #e8edf6;
  --ok: #16a34a;
}

/* Base typography and responsive overrides */
html { scroll-behavior: smooth; }
body { 
  font-family: Inter, system-ui, -apple-system, Segoe UI, Roboto, Helvetica, Arial, "Apple Color Emoji", "Segoe UI Emoji"; 
  color: #0b1020;
  min-width: 0 !important; /* Override any Bootstrap/external min-width constraints */
}

/* Force responsiveness - override external CSS constraints */
html, body, * {
  max-width: 100% !important;
  box-sizing: border-box !important;
}

/* Aggressive overrides for external CSS that might interfere */
img, 
.img-fluid,
.img-responsive {
  max-width: 100% !important;
  width: auto !important;
  height: auto !important;
}

/* Override Bootstrap and ar5iv image constraints */
.container img,
.container-fluid img,
.ltx_page_main img,
.ltx_page_content img {
  max-width: 100% !important;
  width: auto !important;
  height: auto !important;
}

/* Ensure containers can shrink */
.container, .container-fluid, .ltx_page_main, .ltx_page_content {
  min-width: 0 !important;
  max-width: 100% !important;
}


/* --- New Ultra-Mobile Breakpoints for Very Narrow Screens --- */

@media (max-width: 414px) {
  /* Large phone adjustments */
  .ltx_page_main {
    padding: 6px 6px 12px !important; /* Minimal padding for large phones */
  }
  
  /* Ensure content doesn't overflow at common phone widths */
  .ltx_chapter {
    font-size: 0.92rem !important;
    line-height: 1.45 !important;
  }
  
  .ltx_title.ltx_title_chapter,
  .ltx_title.ltx_title_appendix {
    font-size: 1.5rem !important;
  }
  
  .ltx_title.ltx_title_section {
    font-size: 1.15rem !important;
  }
  
  /* More aggressive equation sizing for phones */
  .ltx_equation, .ltx_equationgroup {
    font-size: 0.88em !important;
    padding: 0.15rem 0.3rem !important;
  }
  
  .ltx_Math {
    font-size: 0.88em !important;
  }
}

@media (max-width: 375px) {
  /* Modern small phone (iPhone SE, iPhone 12 mini, etc.) */
  .ltx_page_main {
    padding: 4px 4px 8px !important;
  }
  
  .ltx_chapter {
    font-size: 0.9rem !important;
    line-height: 1.4 !important;
  }
  
  .ltx_title.ltx_title_chapter,
  .ltx_title.ltx_title_appendix {
    font-size: 1.4rem !important;
  }
  
  .ltx_title.ltx_title_section {
    font-size: 1.1rem !important;
  }
  
  /* Ultra-compact equations */
  .ltx_equation, .ltx_equationgroup {
    font-size: 0.85em !important;
    padding: 0.1rem 0.25rem !important;
  }
  
  .ltx_Math {
    font-size: 0.85em !important;
  }
  
  /* Very compact tables */
  .ltx_tabular {
    border-spacing: 4px 2px;
    font-size: 0.8em;
  }
  
  .ltx_tabular .ltx_td {
    padding: 2px 3px !important;
  }
}

@media (max-width: 320px) {
  /* Ultra-narrow phones and minimum viable width */
  .ltx_page_main {
    padding: 3px 3px 6px !important; /* Absolute minimum padding */
  }
  
  /* Ultra-compact typography */
  .ltx_chapter {
    font-size: 0.85rem !important;
    line-height: 1.35 !important;
  }
  
  .ltx_title.ltx_title_chapter,
  .ltx_title.ltx_title_appendix {
    font-size: 1.3rem !important;
  }
  
  .ltx_title.ltx_title_section {
    font-size: 1.05rem !important;
  }
  
  .ltx_title.ltx_title_subsection {
    font-size: 0.95rem !important;
  }
  
  /* Minimal equations - prioritize horizontal scrolling over readability */
  .ltx_equation, .ltx_equationgroup {
    font-size: 0.8em !important;
    padding: 0.08rem 0.2rem !important;
    overflow-x: auto !important;
    -webkit-overflow-scrolling: touch;
  }
  
  .ltx_Math {
    font-size: 0.8em !important;
  }
  
  /* Minimal tables */
  .ltx_tabular {
    border-spacing: 3px 1px;
    font-size: 0.75em;
    overflow-x: auto;
  }
  
  .ltx_tabular .ltx_td {
    padding: 1px 2px !important;
  }
  
  /* Ensure figures can scroll if needed */
  .ltx_figure {
    padding: 6px; /* a bit more padding to avoid touching edges at ultra-narrow */
    overflow-x: visible; /* disable by default; wide figures opt-in via class */
  }
  
  /* Ultra-compact theorems */
  .ltx_theorem {
    padding: 4px 6px;
    margin: 0.2rem auto;
  }
  
  /* Reduce spacing everywhere */
  .ltx_p {
    margin-bottom: 0.4rem !important;
  }
  
  .ltx_section, .ltx_subsection {
    margin-bottom: 0.6rem;
  }
}
/* (Topbar search UI rules defined in common.css) */

.ltx_page_main { max-width: 1040px; margin: 0 auto; padding: 24px 16px 64px; }
.ltx_page_header { position: sticky; top: 42px; z-index: 30; background: #fff; border-bottom: 1px solid #e8edf6; }
.ltx_page_header .ltx_align_center { display: flex; gap: 16px; align-items: center; justify-content: space-between; padding: 10px 16px; max-width: 1040px; margin: 0 auto; }

/* Ensure non-chapter simple pages account for fixed top bar */
.page { padding-top: var(--book-topbar-h, 64px); }

/* Top navbar TOC strip */
.ltx_page_navbar { position: sticky; top: 0; z-index: 40; background: #0f172a; color: #e2e8f0; border-bottom: 1px solid #1e293b; padding: 8px 12px; }
.ltx_page_navbar a { color: #c7d2fe; text-decoration: none; }
.ltx_page_navbar .ltx_TOC { display: none; }

/* Chapter and appendix title */
.ltx_title.ltx_title_chapter,
.ltx_title.ltx_title_appendix { font-weight: 800; letter-spacing: -0.01em; color: #0b1020; }
.ltx_title.ltx_title_section { margin-top: 2rem; border-top: 1px solid #eef2ff; padding-top: 1rem; }
.ltx_title a { text-decoration: none; color: inherit; }

/* Paragraph headings: intentionally much smaller than subsection headings */
.ltx_chapter .ltx_title.ltx_title_paragraph {
  font-size: 1.0rem !important;
  line-height: 1.35;
  margin-top: 0.75rem;
  margin-bottom: 0.25rem;
  color: #0f172a;
}

/* Headings anchor links (right-side icon on hover) */
.ltx_title_section, .ltx_title_subsection, .ltx_title_paragraph, .ltx_title_theorem { position: relative; }
/* Add space on the right for anchor icon */
.ltx_title_section, .ltx_title_subsection, .ltx_title_paragraph { padding-right: 1.5rem; padding-left: 0; }
.heading-anchor {
  position: absolute;
  right: 0.25rem;
  left: auto;
  top: 50%;
  transform: translateY(-50%);
  opacity: 0;
  transition: opacity 120ms ease-in-out;
  font-weight: 600;
  color: #64748b;
  text-decoration: none;
  z-index: 5;
  font-size: 14px; /* fixed size independent of heading context */
  line-height: 1;
  pointer-events: none; /* avoid invisible anchor capturing hover */
}
.heading-anchor:hover { color: #334155; }
/* Show anchor when heading is hovered (primary discoverability) */
.ltx_title_section:hover .heading-anchor, .ltx_title_subsection:hover .heading-anchor, .ltx_title_paragraph:hover .heading-anchor, .ltx_title_theorem:hover .heading-anchor { opacity: 1; pointer-events: auto; }
/* (Removed suppressor that kept anchors hidden) */
/* Still show when the anchor itself is hovered or focused (keyboard accessibility) */
.heading-anchor:hover, .heading-anchor:focus, .heading-anchor:focus-visible { opacity: 1; pointer-events: auto; }
/* Allow pointer interaction for anchors placed inside figure/table containers when the container is hovered */
.ltx_figure:hover .heading-anchor,
.ltx_table:hover .heading-anchor,
.ltx_tabular:hover .heading-anchor,
.ltx_float.ltx_float_algorithm:hover .heading-anchor { pointer-events: auto; }
/* Keep anchors visible under sticky headers */
h1[id], h2[id], h3[id], h4[id], section[id], .ltx_theorem[id], .ltx_equation[id], .ltx_equationgroup[id], .ltx_figure[id], .ltx_table[id], .ltx_tabular[id], .ltx_float[id] { scroll-margin-top: 90px; }

/* Math and equations */
.ltx_equation { position: relative; margin: 1rem auto; padding: 1.5rem 0.75rem; background: transparent; border: none;  text-align: center; }
.ltx_eqn_table, .ltx_equation table { margin-left: auto; margin-right: auto; }
.ltx_eqn_cell { text-align: center; }
.ltx_equation .ltx_tag_equation { color: #64748b; }
.ltx_equationgroup { position: relative; }
.ltx_tag_equation {color: #64748b; padding-left: 3rem;}
.ltx_tag_equationgroup {color: #64748b; padding-left: 3rem;}

@media (max-width: 600px) {
  .ltx_tag_equation, .ltx_tag_equationgroup { padding-left: 0rem; }
}

.eq-toolbar { display: inline-flex; gap: 8px; margin-left: 10px; }
.eq-btn { border: 1px solid #e5e7eb; background: #fff; border-radius: 6px; padding: 2px 6px; font-size: 12px; color: #334155; cursor: pointer; }
.eq-btn:hover { background: #f8fafc; }

/* Figures/Tables */
.ltx_figure, .ltx_table, .ltx_tabular { position: relative; background: #fff; border: 1px solid #e8edf6; border-radius: 10px; padding: 12px; margin: 1rem auto; box-shadow: 0 1px 2px rgba(0,0,0,0.02); }
.ltx_figure { text-align: center; }
.ltx_graphics { display: block; margin: 0 auto; max-width: 100%; height: auto; }
.ltx_tabular table { margin-left: auto; margin-right: auto; }
.ltx_caption { color: #475569; }

/* Notes and footnotes */
.ltx_note .ltx_note_outer { display: none; }
.footnote-pop { position: absolute; z-index: 60; max-width: 360px; background: #0f172a; color: #e2e8f0; border: 1px solid #1e293b; border-radius: 8px; padding: 8px 10px; font-size: 12px; box-shadow: 0 10px 30px rgba(2,6,23,0.25); }

/* Theorem-like environments */
.ltx_theorem {
  background: var(--thm-bg, #f8fafc);
  border: 1px solid var(--thm-border, #e2e8f0);
  border-left: 6px solid var(--thm-accent, #94a3b8);
  border-radius: 10px;
  padding: 12px 14px;
  margin: 1rem auto;
  box-shadow: 0 1px 2px rgba(0,0,0,0.02);
}
.ltx_theorem .ltx_title_theorem { position: relative; margin-top: 0; font-weight: 700; color: #0f172a; padding-right: 1.5rem; padding-left: 0; }
.ltx_theorem .ltx_tag_theorem { color: var(--thm-accent, #334155); }
/* Trim inner paragraph spacing to avoid extra half-line at bottom */
.ltx_theorem .ltx_p:first-child { margin-top: 0; }
.ltx_theorem .ltx_p:last-child { margin-bottom: 0; }

/* For theorem titles, place the anchor on the right to avoid the left accent bar */
.ltx_title_theorem .heading-anchor {
  right: 0.25rem;
  left: auto;
  transform: translateY(-50%);
}
/* Place section/subsection anchors on the right */
.ltx_title_section .heading-anchor, .ltx_title_subsection .heading-anchor, .ltx_title_paragraph .heading-anchor {
  right: 0.25rem;
  left: auto;
}
/* Fix spacing for section anchors */
.ltx_title_section .heading-anchor { top: 66.66%; }

/* Also reveal the anchor when hovering anywhere over the theorem box */
.ltx_theorem:hover .ltx_title_theorem .heading-anchor { opacity: 1; }

/* Inline anchor for equation numbers */
/* New: outside-positioned anchor for equations */
.heading-anchor-outside {
  position: absolute;
  top: 50%;
  right: -1.75rem; /* place just outside the right border */
  transform: translateY(-50%);
  opacity: 0;
  transition: opacity 120ms ease-in-out;
  font-weight: 600;
  color: #64748b;
  text-decoration: none;
  font-size: 14px; /* fixed size independent of context */
  line-height: 1;
}
.ltx_equation:hover .heading-anchor-outside,
.ltx_equationgroup:hover .heading-anchor-outside { opacity: 1; }

/* Place anchors for figures/tables/algorithms in the top-right corner */
.ltx_figure .heading-anchor,
.ltx_table .heading-anchor,
.ltx_tabular .heading-anchor,
.ltx_float.ltx_float_algorithm .heading-anchor {
  left: auto;
  right: 0.5rem;
  top: 0.5rem;
  transform: none;
}
.ltx_figure:hover .heading-anchor,
.ltx_table:hover .heading-anchor,
.ltx_tabular:hover .heading-anchor,
.ltx_float.ltx_float_algorithm:hover .heading-anchor { opacity: 1; pointer-events: auto; }

/* Type-specific pastel colors */
.ltx_theorem.ltx_theorem_theorem {
  --thm-bg: #f0fdf4;      /* light green */
  --thm-border: #bbf7d0;
  --thm-accent: #22c55e;
}
.ltx_theorem.ltx_theorem_proposition {
  --thm-bg: #eff6ff;      /* light blue */
  --thm-border: #bfdbfe;
  --thm-accent: #3b82f6;
}
.ltx_theorem.ltx_theorem_lemma {
  --thm-bg: #ecfeff;      /* light cyan */
  --thm-border: #a5f3fc;
  --thm-accent: #06b6d4;
}
.ltx_theorem.ltx_theorem_definition {
  --thm-bg: #fffbeb;      /* light yellow */
  --thm-border: #fde68a;
  --thm-accent: #f59e0b;
}
.ltx_theorem.ltx_theorem_corollary {
  --thm-bg: #f5f3ff;      /* light purple */
  --thm-border: #ddd6fe;
  --thm-accent: #8b5cf6;
}
.ltx_theorem.ltx_theorem_remark {
  --thm-bg: #f8fafc;      /* light gray */
  --thm-border: #e2e8f0;
  --thm-accent: #64748b;
}
.ltx_theorem.ltx_theorem_example {
  --thm-bg: #fefce8;      /* pale yellow */
  --thm-border: #fde68a;
  --thm-accent: #ca8a04;
}

/* Algorithm environments */
.ltx_float.ltx_float_algorithm {
  position: relative;
  /* Style like theorem boxes; use the lemma palette (light cyan) */
  background: #ecfeff;
  border: 1px solid #a5f3fc;
  border-left: 6px solid #06b6d4;
  border-radius: 10px;
  padding: 12px 14px;
  margin: 1rem auto;
  box-shadow: 0 1px 2px rgba(0,0,0,0.02);
}

.ltx_float.ltx_float_algorithm .ltx_caption {
  font-weight: 700;
  color: #0f172a;
  margin-bottom: 12px;
  font-family: Inter, system-ui, -apple-system, Segoe UI, Roboto, Helvetica, Arial, sans-serif;
}

/* Match theorem-like tag styling for the Algorithm label */
.ltx_float.ltx_float_algorithm .ltx_caption .ltx_tag.ltx_tag_float .ltx_text {
  color: #06b6d4; /* lemma accent */
  font-weight: 700;
}

/* Algorithm listing lines: add spacing between line numbers and text */
.ltx_float.ltx_float_algorithm .ltx_listing {
  font-family: ui-monospace, SFMono-Regular, Menlo, Monaco, Consolas, Liberation Mono, monospace;
  font-size: 0.95rem;
  line-height: 1.5;
}

.ltx_float.ltx_float_algorithm .ltx_listingline {
  position: relative;
  padding-left: 3.2em; /* space reserved for the number + gap */
  margin: 0.1rem 0;
}

.ltx_float.ltx_float_algorithm .ltx_tag.ltx_tag_listingline {
  position: absolute;
  left: 0;
  top: 0;
  width: 2.4em; /* width of number column */
  text-align: right;
  color: #64748b;
  font-weight: 600;
  pointer-events: none;
}

/* Place inline right comments at the right edge without affecting text flow */
.ltx_float.ltx_float_algorithm .ltx_listingline .ltx_text[style*="float:right"] {
  float: none !important;
  position: absolute;
  right: 0;
  top: 0;
  white-space: nowrap;
  color: #64748b;
}

/* Typewriter comments: keep inline (not right-floated) */
.ltx_float.ltx_float_algorithm .ltx_listingline .ltx_text.ltx_font_typewriter {
  float: none;
  color: #64748b;
}

/* --- Mobile-friendly algorithm layout --- */
@media (max-width: 600px) {
  .ltx_float.ltx_float_algorithm {
    padding: 8px 10px;
    margin: 0.75rem auto;
  }

  .ltx_float.ltx_float_algorithm .ltx_listingline {
    padding-left: 2.6em; /* tighter number gutter */
    padding-right: 0 !important; /* JS will skip padding on mobile */
  }

  .ltx_float.ltx_float_algorithm .ltx_tag.ltx_tag_listingline {
    width: 2em;
  }

  /* Comments move below and wrap on small screens */
  .ltx_float.ltx_float_algorithm .ltx_listingline .ltx_text[style*="float:right"] {
    position: static;
    float: none !important;
    display: block;
    white-space: normal;
    text-align: right;
    margin-top: 2px;
  }

  .ltx_float.ltx_float_algorithm .heading-anchor {
    right: 0.35rem;
    top: 0.35rem;
  }
}

/* Removed IO labels; kept rule empty to avoid layout shift if nodes persist briefly */
.ltx_float.ltx_float_algorithm .alg-io-label { display: none; }

/* Algorithmic content styling */
.ltx_algorithmic {
  counter-reset: algorithm-line;
}

.ltx_algorithmic .ltx_item {
  display: flex;
  align-items: flex-start;
  margin-bottom: 0.3rem;
  counter-increment: algorithm-line;
}

.ltx_algorithmic .ltx_item::before {
  content: counter(algorithm-line);
  color: #64748b;
  font-weight: 600;
  min-width: 2rem;
  margin-right: 1rem;
  text-align: right;
  user-select: none;
  font-size: 0.9em;
}

.ltx_algorithmic .ltx_item > .ltx_para {
  flex: 1;
  margin: 0;
}

/* Algorithm keywords styling */
.ltx_algorithmic .ltx_text.ltx_font_smallcaps,
.ltx_algorithmic .ltx_text[data-keyword] {
  font-weight: 700;
  color: #3b82f6;
  text-transform: uppercase;
  font-size: 0.85em;
  letter-spacing: 0.05em;
}

/* Indentation for nested algorithm structures */
.ltx_algorithmic .ltx_item.ltx_indent {
  margin-left: 1.5rem;
}

.ltx_algorithmic .ltx_item.ltx_indent::before {
  margin-left: -1.5rem;
}

/* Comments in algorithms */
.ltx_algorithmic .ltx_text.ltx_font_italic {
  color: #64748b;
  margin-left: 0.5rem;
}

/* Code blocks */
pre, code { font-family: ui-monospace, SFMono-Regular, Menlo, Monaco, Consolas, Liberation Mono, monospace; }
/* Ensure wrapping for all pre elements everywhere without adding visual styles */
pre { white-space: pre-wrap; }
pre code { white-space: inherit; }
.ltx_page_main pre {
  background: var(--bg-soft, #fafbff);
  color: var(--text, #0b1020);
  border-radius: 10px;
  padding: 12px;
  overflow: auto;
  border: 1px solid var(--border, #e8edf6);
}
/* In chapter pages, nested code should inherit wrapping */
.ltx_page_main pre code { white-space: inherit; }

/* Content width for better reading */
.ltx_page_content { display: grid; grid-template-columns: 1fr; gap: 16px; }
.ltx_chapter { line-height: 1.7; font-size: 1.02rem; }

/* Mini TOC */
.mini-toc { border: 1px solid #e8edf6; background: #fafbff; border-radius: 12px; padding: 12px 14px; margin: 16px 0 20px; }
.mini-toc-title { font-size: 13px; font-weight: 800; letter-spacing: 0.08em; text-transform: uppercase; color: #64748b; margin: 0 0 8px 0; }
.mini-toc ul { list-style: none; padding: 0; margin: 0; display: grid; gap: 6px; }
.mini-toc li a { text-decoration: none; color: #1e293b; }
.mini-toc li a:hover { text-decoration: underline; }
.mini-toc .mini-toc-sub { margin-top: 4px; padding-left: 10px; border-left: 2px solid #e8edf6; display: grid; gap: 4px; }

/* (Mobile search styles moved to common.css) */

/* Remove old per-section copy button styling */

/* Lists: keep number and first line on same row */
.ltx_itemize, .ltx_enumerate { padding-left: 0; }
.ltx_item { display: grid; grid-template-columns: auto 1fr; align-items: start; column-gap: 0.6rem; }
.ltx_item > .ltx_tag_item { grid-column: 1; }
.ltx_item > .ltx_para { grid-column: 2; margin: 0; }
.ltx_item > .ltx_para > .ltx_p { margin: 0; }

/* (Sidebar layout and mobile behavior defined in common.css) */

/* --- Fixes for LaTeXML artifacts and tcolorbox-like environments --- */
/* Hide stray tagging macro artifacts rendered as errors */
.ltx_ERROR { display: none !important; }

/* Generic replacement styling for tcolorbox-like callouts */
.tcbox {
  background: #fafbff;
  border: 1px solid #e8edf6;
  border-left: 6px solid #94a3b8;
  border-radius: 12px;
  padding: 12px 14px;
  margin: 16px 0;
  box-shadow: 0 1px 2px rgba(0,0,0,0.03);
}
.tcbox .ltx_p:first-child { margin-top: 0; }
.tcbox .ltx_p:last-child { margin-bottom: 0; }
.tcbox .ltx_text.ltx_font_bold:first-child { margin-right: 6px; }
/* Stacked topbars overrides extracted from inline styles */
:root { --navbar-h: 0px; --book-topbar-h: 64px; }
html, body { margin: 0 !important; padding: 0 !important; }
body > *:first-child { margin-top: 0 !important; }
.ltx_page_main { padding: 16px 16px 32px !important; /* Reduced padding since viewport is now properly positioned */ }
.ltx_page_navbar { display: none !important; }
.ltx_page_header {
  position: fixed !important;
  top: var(--book-topbar-h) !important;
  left: 0;
  right: 0;
  z-index: 30;
  background: #fff;
  margin: 0 !important;
  border-top: none !important;
}
.ltx_page_navbar, .ltx_page_header { margin-top: 0 !important; }
h1[id], h2[id], h3[id], h4[id], section[id], .ltx_theorem[id], .ltx_equation[id], .ltx_equationgroup[id], .ltx_figure[id], .ltx_table[id], .ltx_tabular[id], .ltx_float[id] {
  scroll-margin-top: calc(var(--book-topbar-h) + var(--navbar-h) + 20px);
}

/* (AI Chat Panel styles live in common.css) */

/* (AI Chat typing indicator lives in common.css) */

/* --- Tables (LaTeXML tabular/figure tables) --- */
/* Improve column separation and make tables respect their containers */
.ltx_table, .ltx_tabular {
  max-width: 100%;
}

/* Allow wide tables to scroll horizontally instead of overflowing */
.ltx_table { overflow-x: auto; }
.ltx_tabular { overflow-x: auto; border-collapse: separate; border-spacing: 16px 8px; table-layout: auto; }
/* When LaTeXML emits a wrapper element with an inner <table>, apply spacing to the inner table */
.ltx_tabular table { border-collapse: separate; border-spacing: 16px 8px; table-layout: auto; }

/* Increase cell padding; use !important to override inline LaTeXML styles when present */
.ltx_tabular .ltx_td { padding: 8px 12px !important; }
.ltx_tabular thead .ltx_td { font-weight: 700; }

/* Fix LaTeXML transformed wrappers that set fixed width/height causing crop/overlap */
.ltx_table .ltx_transformed_outer,
.ltx_tabular .ltx_transformed_outer {
  max-width: 100% !important;
  width: 100% !important;
  height: auto !important;
  overflow-x: auto;
  overflow-y: visible;
}

/* Keep transform origin consistent to avoid spilling outside left/top */
.ltx_table .ltx_transformed_inner,
.ltx_tabular .ltx_transformed_inner { transform-origin: top left; }

/* --- Responsive Content Layout --- */

/* Mobile-first responsive breakpoints for LaTeX content */
@media (max-width: 768px) {
  .ltx_page_main {
    padding: 12px 12px 24px !important; /* Reduced padding for tablets */
  }
  
  /* Reduce spacing in content elements */
  .ltx_section, .ltx_subsection {
    margin-bottom: 1.5rem; /* Reduced from default */
  }
  
  .ltx_theorem, .ltx_figure, .ltx_table, .ltx_tabular {
    margin: 0.75rem auto; /* Reduced vertical spacing */
  }
}

@media (max-width: 600px) {
  .ltx_page_main {
    padding: 8px 8px 16px !important; /* Minimal padding for phones */
  }
  
  /* Further reduce spacing for mobile */
  .ltx_section, .ltx_subsection {
    margin-bottom: 1rem;
  }
  
  .ltx_theorem, .ltx_figure, .ltx_table, .ltx_tabular {
    margin: 0.5rem auto;
  }
  
  /* Reduce padding in content boxes */
  .ltx_theorem {
    padding: 8px 10px;
  }
  
  .ltx_figure, .ltx_table, .ltx_tabular {
    padding: 8px;
  }
}

@media (max-width: 480px) {
  .ltx_page_main {
    padding: 6px 6px 12px !important; /* Ultra-minimal for small phones */
  }
  
  /* Minimal spacing for ultra-mobile */
  .ltx_section, .ltx_subsection {
    margin-bottom: 0.75rem;
  }
  
  .ltx_theorem, .ltx_figure, .ltx_table, .ltx_tabular {
    margin: 0.25rem auto;
    padding: 6px;
  }
}

/* --- Responsive Mathematical Content --- */

/* Ensure math fits container; avoid scrollbars by default (enable on small screens below) */
.ltx_equation, .ltx_equationgroup {
  max-width: 100% !important;
  overflow-x: hidden !important; /* prevent stray scrollbars when content fits */
  /* Hide scrollbar tracks by default (we still allow scroll on small screens) */
  -ms-overflow-style: none; /* IE/Edge */
  scrollbar-width: none;    /* Firefox */
}
.ltx_equation::-webkit-scrollbar, .ltx_equationgroup::-webkit-scrollbar { display: none; height: 0; background: transparent; }

/* Math content respects container; default without scrollbars */
.ltx_equation .ltx_Math, .ltx_equationgroup .ltx_Math {
  max-width: 100% !important;
  overflow: visible !important; /* avoid inner scrollbars */
  white-space: nowrap; /* keep equations on one line when possible */
  display: inline-block; /* stabilize width calculation to reduce phantom overflow */
  box-sizing: border-box;
}

@media (max-width: 768px) {
  /* Equations: ensure they can scroll horizontally if needed */
  .ltx_equation {
    padding: 0.25rem 0.5rem !important; /* Reduced padding */
    overflow-x: auto !important; /* Allow horizontal scrolling for long equations */
    -webkit-overflow-scrolling: touch; /* Smooth scrolling on mobile */
    -ms-overflow-style: none; /* hide track while preserving scroll */
    scrollbar-width: none;
  }
  
  .ltx_equationgroup {
    overflow-x: auto !important;
    -webkit-overflow-scrolling: touch;
    -ms-overflow-style: none;
    scrollbar-width: none;
  }
  .ltx_equation::-webkit-scrollbar, .ltx_equationgroup::-webkit-scrollbar { display: none; height: 0; background: transparent; }
  
  /* Slightly smaller math font on tablets */
  .ltx_Math {
    font-size: 1.02em !important; /* bumped up for readability */
  }
  /* Allow inner math to scroll on small screens */
  .ltx_equation .ltx_Math, .ltx_equationgroup .ltx_Math {
    overflow-x: auto !important;
    -ms-overflow-style: none;
    scrollbar-width: none;
  }
  .ltx_equation .ltx_Math::-webkit-scrollbar, .ltx_equationgroup .ltx_Math::-webkit-scrollbar { display: none; height: 0; background: transparent; }
}

@media (max-width: 600px) {
  /* More aggressive equation formatting for phones */
  .ltx_equation {
    padding: 0.2rem 0.4rem !important;
    font-size: 1.0em !important; /* larger for mobile readability */
  }
  
  .ltx_equationgroup {
    font-size: 1.0em !important;
  }
  
  /* Ensure equation numbers don't cause overflow */
  .ltx_equation .ltx_tag_equation {
    font-size: 0.9em !important;
  }
  
  /* Math in general text should wrap better */
  .ltx_Math {
    font-size: 1.0em !important;
    word-wrap: break-word !important;
  }
}

@media (max-width: 480px) {
  /* Ultra-mobile math adjustments */
  .ltx_equation {
    padding: 0.15rem 0.3rem !important;
    font-size: 0.95em !important; /* larger on very small screens */
  }
  
  .ltx_equationgroup {
    font-size: 0.95em !important;
  }
  
  .ltx_Math {
    font-size: 0.95em !important;
  }
  
  /* Very small equation tags */
  .ltx_equation .ltx_tag_equation {
    font-size: 0.8em !important;
  }
}

/* --- Responsive Tables and Figures --- */

/* Critical addition: ensure all images are responsive by default */
.ltx_graphics,
img.ltx_graphics,
.ltx_figure img,
.ltx_figure_panel img,
img[width] {
  max-width: 100% !important;
  width: auto !important; /* Override any fixed widths from LaTeXML */
  height: auto !important;
  display: block;
  margin: 0 auto;
}

/* Even more specific selectors to override external CSS */
.ltx_figure .ltx_graphics,
.ltx_figure_panel .ltx_graphics,
.ltx_flex_cell img {
  max-width: 100% !important;
  width: auto !important;
  height: auto !important;
}

/* Ensure figure containers don't cause overflow */
.ltx_figure, .ltx_figure_panel {
  max-width: 100% !important;
  overflow-x: visible !important; /* default: no scroll; use .ltx_figure_wide to enable */
}

/* Critical: Handle LaTeXML flex layouts that can cause overflow */
.ltx_flex_figure, 
.ltx_flex_cell {
  max-width: 100% !important;
  min-width: 0 !important; /* Allow flex items to shrink below content width */
  flex-shrink: 1 !important; /* Allow shrinking */
  overflow-x: auto !important;
}

/* Ensure flex layouts wrap on mobile */
.ltx_flex_figure {
  flex-wrap: wrap !important;
  display: flex !important;
  justify-content: center; /* center panels within figure */
  align-items: center;
}

/* For very narrow screens, make flex cells stack vertically */
@media (max-width: 600px) {
  .ltx_flex_figure {
    flex-direction: column !important;
  }
  
  .ltx_flex_cell {
    width: 100% !important;
    flex-basis: auto !important;
  }
}

/* For very wide figures that need horizontal scrolling */
.ltx_figure.ltx_figure_wide {
  overflow-x: auto;
  -webkit-overflow-scrolling: touch;
}

@media (max-width: 768px) {
  /* Tables: reduce spacing but maintain readability */
  .ltx_tabular {
    border-spacing: 12px 6px; /* Reduced from 16px 8px */
  }
  
  .ltx_tabular table {
    border-spacing: 12px 6px;
  }
  
  .ltx_tabular .ltx_td {
    padding: 6px 8px !important; /* Reduced from 8px 12px */
  }
  
  /* Figures: ensure they scale properly */
  .ltx_graphics {
    max-width: 100%;
    height: auto;
  }
  
  /* Captions should wrap nicely */
  .ltx_caption {
    font-size: 0.9em;
    line-height: 1.4;
  }
}

@media (max-width: 600px) {
  /* More aggressive table compression for phones */
  .ltx_tabular {
    border-spacing: 8px 4px;
    font-size: 0.9em;
  }
  
  .ltx_tabular table {
    border-spacing: 8px 4px;
  }
  
  .ltx_tabular .ltx_td {
    padding: 4px 6px !important;
  }
  
  /* Smaller captions for mobile */
  .ltx_caption {
    font-size: 0.85em;
    line-height: 1.3;
  }
  
  /* Code blocks: ensure they don't overflow */
  .ltx_page_main pre {
    padding: 8px;
    font-size: 0.9em;
    overflow-x: auto;
  }
  /* Slightly more padding on small screens for figures/tables to avoid crowding */
  .ltx_figure, .ltx_table, .ltx_tabular { padding: 10px; }
}

@media (max-width: 480px) {
  /* Ultra-compact tables for small phones */
  .ltx_tabular {
    border-spacing: 6px 3px;
    font-size: 0.85em;
  }
  
  .ltx_tabular table {
    border-spacing: 6px 3px;
  }
  
  .ltx_tabular .ltx_td {
    padding: 3px 4px !important;
  }
  
  /* Very compact captions */
  .ltx_caption {
    font-size: 0.8em;
    line-height: 1.2;
  }
  
  /* Ultra-compact code blocks */
  .ltx_page_main pre {
    padding: 6px;
    font-size: 0.85em;
  }
  /* Maintain centering and some breathing room */
  .ltx_figure, .ltx_table, .ltx_tabular { padding: 8px; }
}

/* --- Responsive Typography and Final Refinements --- */

@media (max-width: 768px) {
  /* Responsive typography for tablets - use !important to override external CSS */
  .ltx_chapter {
    font-size: 1rem !important; /* Slightly smaller base font */
    line-height: 1.6 !important; /* Tighter line height */
  }
  
  .ltx_title.ltx_title_chapter,
  .ltx_title.ltx_title_appendix {
    font-size: 1.8rem !important; /* Smaller chapter titles */
  }
  
  .ltx_title.ltx_title_section {
    font-size: 1.3rem !important;
    margin-top: 1.5rem !important;
    padding-top: 0.75rem !important;
  }
  
  .ltx_title.ltx_title_subsection {
    font-size: 1.1rem !important;
  }

  /* Paragraph headings stay much smaller than subsections */
  .ltx_chapter .ltx_title.ltx_title_paragraph {
    font-size: 0.88rem !important;
  }
  
  /* Paragraph spacing */
  .ltx_p {
    margin-bottom: 0.75rem !important; /* Reduced from default */
  }
}

@media (max-width: 600px) {
  /* Mobile typography adjustments - use !important to override external CSS */
  .ltx_chapter {
    font-size: 0.95rem !important;
    line-height: 1.5 !important;
  }
  
  .ltx_title.ltx_title_chapter,
  .ltx_title.ltx_title_appendix {
    font-size: 1.6rem !important;
  }
  
  .ltx_title.ltx_title_section {
    font-size: 1.2rem !important;
    margin-top: 1.25rem !important;
    padding-top: 0.5rem !important;
  }
  
  .ltx_title.ltx_title_subsection {
    font-size: 1.05rem !important;
  }

  /* Keep paragraph headings clearly smaller on phones */
  .ltx_chapter .ltx_title.ltx_title_paragraph {
    font-size: 0.85rem !important;
  }
  
  /* Tighter paragraph spacing */
  .ltx_p {
    margin-bottom: 0.6rem !important;
  }
  
  /* Lists: more compact */
  .ltx_itemize, .ltx_enumerate {
    margin: 0.5rem 0 !important;
  }
  
  .ltx_item {
    margin-bottom: 0.3rem !important;
  }
}

@media (max-width: 480px) {
  /* Ultra-mobile typography - use !important to override external CSS */
  .ltx_chapter {
    font-size: 0.9rem !important;
    line-height: 1.4 !important;
  }
  
  .ltx_title.ltx_title_chapter,
  .ltx_title.ltx_title_appendix {
    font-size: 1.4rem !important;
  }
  
  .ltx_title.ltx_title_section {
    font-size: 1.1rem !important;
    margin-top: 1rem !important;
    padding-top: 0.4rem !important;
  }
  
  .ltx_title.ltx_title_subsection {
    font-size: 1rem !important;
  }

  /* Extra-small paragraph headings for very small screens */
  .ltx_chapter .ltx_title.ltx_title_paragraph {
    font-size: 0.82rem !important;
  }
  
  /* Very tight spacing */
  .ltx_p {
    margin-bottom: 0.5rem !important;
  }
  
  .ltx_itemize, .ltx_enumerate {
    margin: 0.4rem 0 !important;
  }
  
  .ltx_item {
    margin-bottom: 0.2rem !important;
  }
  
  /* Theorem environments: ultra-compact */
  .ltx_theorem .ltx_title_theorem {
    font-size: 0.9rem !important;
  }
}

/* (Language selector visibility controlled in common.css) */

/* === FINAL AGGRESSIVE RESPONSIVE OVERRIDES === */
/* These must be at the end to override all external CSS */

/* Ensure ALL images are responsive - final override */
img,
.ltx_graphics,
.ltx_figure img,
.ltx_figure_panel img,
.ltx_flex_cell img,
img[width],
img[height] {
  max-width: 100% !important;
  width: auto !important;
  height: auto !important;
}

/* Ensure ALL containers can shrink */
div, 
section, 
figure,
.ltx_figure,
.ltx_figure_panel,
.ltx_flex_figure,
.ltx_flex_cell,
.container,
.container-fluid {
  max-width: 100% !important;
  min-width: 0 !important;
  box-sizing: border-box !important;
}

/* Force flex layouts to be responsive */
.ltx_flex_figure {
  display: flex !important;
  flex-wrap: wrap !important;
  max-width: 100% !important;
}

.ltx_flex_cell {
  flex-shrink: 1 !important;
  min-width: 0 !important;
  max-width: 100% !important;
}

/* Emergency mobile override for any remaining overflow */
@media (max-width: 600px) {
  * {
    max-width: 100% !important;
  }
  
  .ltx_flex_figure {
    flex-direction: column !important;
  }
  
  .ltx_flex_cell {
    width: 100% !important;
    flex-basis: auto !important;
  }
}

/* --- Viewport-based sizing for figures and multi-panel figures --- */
/* Cap figure-like containers to a percentage of the viewport; adapt cap at wider screens */
:root { --figure-max-width: 95vw; }
@media (min-width: 768px) { :root { --figure-max-width: 80vw; } }
@media (min-width: 1200px) { :root { --figure-max-width: 60vw; } }
@media (min-width: 1600px) { :root { --figure-max-width: 50vw; } }

/* Additional height cap for figure media; adapts by viewport width and falls back to 400px */
:root { --figure-max-height: 20vh; }
@media (max-width: 600px) { :root { --figure-max-height: 30vh; } }
@media (min-width: 1600px) { :root { --figure-max-height: 16vh; } }

.ltx_page_main .ltx_figure,
.ltx_page_main .ltx_figure_panel,
.ltx_page_main .ltx_table,
.ltx_page_main .ltx_tabular,
.ltx_page_main .ltx_flex_figure {
  max-width: min(100%, var(--figure-max-width)) !important;
  margin-left: auto;
  margin-right: auto;
}

/* (reverted) allow responsive upscaling within container caps */

/* Ensure media inside figures respects the viewport cap */
.ltx_page_main .ltx_figure img,
.ltx_page_main .ltx_figure_panel img,
.ltx_page_main .ltx_flex_cell img,
.ltx_page_main .ltx_figure .ltx_graphics,
.ltx_page_main .ltx_figure_panel .ltx_graphics {
  max-width: min(100%, var(--figure-max-width)) !important;
  width: 100% !important;
  height: auto !important;
  max-height: min(var(--figure-max-height), 400px) !important;
  object-fit: contain;
}

/* Multi-panel figures: give sensible intrinsic basis that adapts to viewport */
.ltx_page_main .ltx_flex_figure {
  max-width: min(100%, var(--figure-max-width)) !important;
  gap: 10px;
}
.ltx_page_main .ltx_flex_figure .ltx_flex_cell {
  flex: 1 1 clamp(180px, calc(var(--figure-max-width) / 2), 520px);
  max-width: 100%;
}


/* Large screens get padding and anchor tags for large environments go in the gutter */
@media (min-width: 601px) {
  .ltx_chapter { padding: 0 1.5rem; }
  /* Ensure left-gutter anchors are within the heading hover area */
  .ltx_title_section, 
  .ltx_title_subsection, 
  .ltx_title_paragraph {
    padding-left: 1.75rem;
    margin-left: -1.75rem; /* expand hover/click area into gutter without visual indent */
  }
  
  /* Position section/subsection anchors on the left side in gutter */
  .ltx_title_section .heading-anchor, 
  .ltx_title_subsection .heading-anchor,
  .ltx_title_paragraph .heading-anchor {
    left: 0.25rem;
    right: auto;
    top: 50%;
    transform: translateY(-50%);
    bottom: auto;
  }
  
  /* Section anchors need to account for the border-top and padding-top offset */
  .ltx_title_section .heading-anchor { top: 66.66%; }
}