/* ================================================================== */
/*  Mobile-safe math + tables + code (ported from fmin.xyz/optim)       */
/* ================================================================== */

/* KaTeX display blocks: scroll wide formulas instead of bleeding off-screen.
   Tiny vertical padding prevents top/bottom glyph crop (accents, surds). */
.katex-display {
  overflow-x: auto;
  overflow-y: hidden;
  padding-top: 1px;
  padding-bottom: 1px;
  -webkit-overflow-scrolling: touch;
}

/* \tag{} positioning: absolute (right-edge) on desktop, inline on phones —
   else the tag floats off-screen and gets clipped by the new overflow:auto. */
.katex-display > .katex > .katex-html > .tag {
  padding-left: 10px !important;
  margin-left: 10px !important;
  right: 0 !important;
}
@media (min-width: 613px) {
  .katex-display > .katex > .katex-html > .tag { position: absolute !important; }
}
@media (max-width: 612px) {
  .katex-display > .katex > .katex-html > .tag { position: static !important; }
}

/* MathML mirror that KaTeX emits is visually hidden but, in some Quarto
   builds, leaks intrinsic width and pushes the layout. Pin it to zero box. */
.katex-display .katex-mathml,
.katex .katex-mathml {
  position: absolute;
  clip: rect(1px, 1px, 1px, 1px);
  padding: 0;
  border: 0;
  height: 1px;
  width: 1px;
  overflow: hidden;
}

/* Tables: horizontal scroll instead of overflow. */
.table,
table {
  max-width: 100%;
  display: block;
  overflow-x: auto;
  -webkit-overflow-scrolling: touch;
}

/* Code blocks: Quarto already sets overflow:auto, but pin width too. */
div.sourceCode,
pre,
pre.sourceCode {
  max-width: 100%;
  overflow-x: auto;
}

/* Long inline literals (URLs, identifiers) must wrap, not stretch the row. */
code, kbd, samp { overflow-wrap: anywhere; word-break: break-word; }

/* Mermaid / SVG figures should never be wider than their column. */
svg, .cell-output-display svg { max-width: 100%; height: auto; }

/* Page-level guard: clip any stray bleed at the body edge so phones don't
   get ghost horizontal scroll from MathJax/KaTeX paint artefacts. */
html, body { overflow-x: hidden; }

/* ================================================================== */
/*  EXISTING: callouts                                                  */
/* ================================================================== */
/* Tighten things up + book-feel */
.callout-historybox {
  border-left: 4px solid #a07050;
  background: #faf6ef;
  margin: 16px 0;
  padding: 10px 16px;
  border-radius: 4px;
}
.callout-historybox::before {
  content: "📜 Историческая справка";
  font-weight: 600;
  display: block;
  margin-bottom: 6px;
  color: #7a5030;
  font-family: ui-sans-serif, system-ui, sans-serif;
}
.callout-tasks {
  border-left: 4px solid #2e7d32;
  background: #f3faf3;
  margin: 16px 0;
  padding: 10px 16px;
  border-radius: 4px;
}
.callout-tasks::before {
  content: "✍️ Задачи";
  font-weight: 600;
  display: block;
  margin-bottom: 6px;
  color: #1e5021;
  font-family: ui-sans-serif, system-ui, sans-serif;
}
img { max-width: 100%; height: auto; }
figure img { display: block; margin: 0 auto; }
figure figcaption, .figure-caption { text-align: center; font-style: italic; color: #555; }

/* Index page hero */
.hero {
  background: linear-gradient(135deg, #f5f7fa 0%, #e6eef7 100%);
  border-radius: 12px;
  padding: 32px 28px;
  margin: 8px 0 28px 0;
  border: 1px solid #d9e2ec;
}
.hero h1 { margin-top: 0; font-size: 2.4em; }
.hero p { font-size: 1.08em; }
.hero .btn { margin-top: 8px; }
.hero-dop { background: linear-gradient(135deg, #fff7ed 0%, #fed7aa 100%); border-color: #fdba74; }
.hero-10  { background: linear-gradient(135deg, #eff6ff 0%, #bfdbfe 100%); border-color: #93c5fd; }
.hero-11  { background: linear-gradient(135deg, #ecfdf5 0%, #bbf7d0 100%); border-color: #6ee7b7; }

/* Status legend strip */
.legend {
  background: #fafafa;
  border: 1px solid #e5e7eb;
  border-radius: 6px;
  padding: 8px 14px;
  margin: 8px 0 16px 0;
  font-size: 0.92em;
}

/* Map tables — compact + zebra */
.grid table {
  margin: 0;
  font-size: 0.95em;
}
.grid table td:first-child {
  width: 32px;
  text-align: center;
  font-size: 1.15em;
  vertical-align: middle;
}
.grid table tbody tr:hover { background: #f5f9fd; }

/* Mermaid wider */
.cell-output-display .mermaid svg { max-width: 100% !important; height: auto; }

/* Book cards on landing -------------------------------- */
.book-cards {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(300px, 1fr));
  gap: 18px;
  margin: 18px 0 32px 0;
}
.book-card {
  display: block;
  padding: 24px 22px;
  border-radius: 14px;
  border: 1px solid #e5e7eb;
  text-decoration: none !important;
  color: inherit;
  transition: transform 0.15s ease, box-shadow 0.15s ease;
  background: #fff;
}
.book-card:hover {
  transform: translateY(-4px);
  box-shadow: 0 10px 30px rgba(0,0,0,0.10);
  text-decoration: none !important;
}
.book-card .book-icon { font-size: 2.6em; line-height: 1; margin-bottom: 8px; }
.book-card h3 { margin: 4px 0 6px 0; font-size: 1.35em; }
.book-card .book-tagline { color: #6b7280; font-size: 0.92em; margin: 0 0 14px 0; }
.book-card .book-toc { list-style: none; padding: 0; margin: 0 0 14px 0; }
.book-card .book-toc li {
  padding: 6px 0;
  border-bottom: 1px dashed #eef0f3;
  font-size: 0.9em;
}
.book-card .book-toc li:last-child { border-bottom: none; }
.book-card .book-toc em { color: #9ca3af; font-size: 0.86em; font-style: normal; }
.book-card .book-status {
  margin-top: 12px;
  padding: 6px 10px;
  border-radius: 6px;
  background: #f7f8fa;
  font-size: 0.86em;
  color: #4b5563;
}
.book-dop { border-top: 4px solid #fb923c; }
.book-10  { border-top: 4px solid #60a5fa; }
.book-11  { border-top: 4px solid #34d399; }

/* Heatmap ---------------------------------------------- */
.hm-legend {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
  margin: 4px 0 18px 0;
  font-size: 0.82em;
}
.hm-pill {
  padding: 4px 10px;
  border-radius: 999px;
  border: 1px solid rgba(0,0,0,0.08);
  white-space: nowrap;
}
.hm-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(180px, 1fr));
  gap: 6px;
  margin: 8px 0 22px 0;
}
.hm-sub {
  font-size: 0.7em;
  font-weight: 400;
  color: #888;
  margin-left: 8px;
}

/* Mega section titles */
.hm-section-title {
  margin-top: 32px;
  margin-bottom: 4px;
  font-size: 1.4em;
  border-bottom: 2px solid #e5e7eb;
  padding-bottom: 6px;
}
.hm-section-sub {
  font-size: 0.65em;
  font-weight: 400;
  color: #888;
}
.hm-section-meta {
  margin: 0 0 14px 0;
  color: #6b7280;
  font-size: 0.88em;
}

/* Chapter card */
.hm-chapter {
  margin: 10px 0 18px 0;
  padding: 8px 12px 12px 12px;
  border: 1px solid #e5e7eb;
  border-radius: 10px;
  background: #fcfcfd;
}
.hm-chapter-head {
  display: flex;
  flex-wrap: wrap;
  align-items: baseline;
  gap: 8px;
  padding: 8px 12px;
  border-radius: 8px;
  text-decoration: none !important;
  margin-bottom: 8px;
  transition: transform 0.08s ease, box-shadow 0.08s ease;
}
.hm-chapter-head:hover { transform: translateX(2px); box-shadow: 0 2px 8px rgba(0,0,0,0.08); }
.hm-chapter-num { font-weight: 700; }
.hm-chapter-title { font-weight: 600; font-size: 1.05em; }
.hm-meta { margin-left: auto; font-size: 0.78em; opacity: 0.75; }

/* Sub-grid: smaller cells inside each chapter */
.hm-sub-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(170px, 1fr));
  gap: 4px;
}
.hm-sub-cell {
  padding: 8px 8px;
  border-radius: 6px;
  font-size: 0.78em;
  line-height: 1.25;
  min-height: 42px;
  display: flex;
  align-items: center;
  justify-content: center;
  text-align: center;
  border: 1px solid rgba(0,0,0,0.05);
}
.hm-sub-cell.hm-0 { background: #ececec; color: #6b7280; }
.hm-sub-cell.hm-1 { background: #dbeafe; color: #1e3a8a; }
.hm-sub-cell.hm-2 { background: #fef3c7; color: #92400e; }
.hm-sub-cell.hm-3 { background: #fed7aa; color: #7c2d12; }
.hm-sub-cell.hm-4 { background: #bbf7d0; color: #064e3b; }
.hm-sub-cell.hm-5 { background: #4ade80; color: #052e16; font-weight: 600; }

/* Dark mode sub-cells */
.quarto-dark .hm-chapter { background: #1c1f24; border-color: #353a42; }
.quarto-dark .hm-sub-cell.hm-0 { background: #2a2e35; color: #8b95a5; }
.quarto-dark .hm-sub-cell.hm-1 { background: #1e3a8a; color: #c7d8ff; }
.quarto-dark .hm-sub-cell.hm-2 { background: #78350f; color: #ffe4a1; }
.quarto-dark .hm-sub-cell.hm-3 { background: #7c2d12; color: #ffd2b0; }
.quarto-dark .hm-sub-cell.hm-4 { background: #064e3b; color: #b6f4d4; }
.quarto-dark .hm-sub-cell.hm-5 { background: #15803d; color: #d3ffd9; }
.hm-cell {
  padding: 12px 10px;
  border-radius: 8px;
  font-size: 0.84em;
  line-height: 1.25;
  min-height: 56px;
  display: flex;
  align-items: center;
  justify-content: center;
  text-align: center;
  text-decoration: none !important;
  cursor: default;
  transition: transform 0.08s ease, box-shadow 0.08s ease;
  border: 1px solid rgba(0,0,0,0.06);
}
a.hm-cell { cursor: pointer; }
a.hm-cell:hover { transform: translateY(-2px); box-shadow: 0 4px 12px rgba(0,0,0,0.10); }

.hm-0 { background: #ececec; color: #6b7280; }
.hm-1 { background: #dbeafe; color: #1e3a8a; }
.hm-2 { background: #fef3c7; color: #92400e; }
.hm-3 { background: #fed7aa; color: #7c2d12; }
.hm-4 { background: #bbf7d0; color: #064e3b; }
.hm-5 { background: #4ade80; color: #052e16; font-weight: 600; }

/* Dark mode */
.quarto-dark .hm-0 { background: #2a2e35; color: #8b95a5; }
.quarto-dark .hm-1 { background: #1e3a8a; color: #c7d8ff; }
.quarto-dark .hm-2 { background: #78350f; color: #ffe4a1; }
.quarto-dark .hm-3 { background: #7c2d12; color: #ffd2b0; }
.quarto-dark .hm-4 { background: #064e3b; color: #b6f4d4; }
.quarto-dark .hm-5 { background: #15803d; color: #d3ffd9; }
