/* =============================================================
   Thesis Web — Main Stylesheet
   Distill.pub-inspired academic design
   ============================================================= */

/* --- CSS Custom Properties --- */
:root {
  /* Colors from thesis (01-Colors.sty) */
  --main-color: #8b2500;         /* red!45!black — links, accents */
  --chapter-red: #8b1a1a;        /* red!30!black — chapter numbers */
  --chapter-gray: #b3b3b3;       /* gray!80!white — modern style fallback */
  --frontpage-dark: #1c1c1c;

  /* Admonition / block colors */
  --note-blue: #0969da;
  --tip-green: #1a7f37;
  --warning-brown: #9a6700;
  --todo-red: #d1242f;

  /* Highlight boxes */
  --highlight-bg: #eff6ff;       /* blue!5 */
  --highlight-border: #1e3a5f;   /* blue!40!black */
  --openquestion-bg: #fef2f2;    /* red!5 */
  --openquestion-border: #7a2020;/* red!50!black */

  /* Layout */
  --bg: #fafaf9;
  --bg-alt: #f5f5f4;
  --text: #1a1a1a;
  --text-muted: #6b7280;
  --border: #e5e7eb;
  --sidebar-width: 260px;
  --content-width: 720px;
  --breakout-width: 960px;
  --margin-width: 220px;

  /* Typography */
  --font-body: "Palatino Linotype", "Book Antiqua", Palatino, "TeX Gyre Pagella", serif;
  --font-heading: "Crimson Pro", "Crimson Text", Georgia, serif;
  --font-ui: "Lato", -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
  --font-mono: "Fira Code", "JetBrains Mono", "Source Code Pro", Consolas, monospace;
  --font-size-base: 18px;
  --line-height: 1.72;
}

/* --- Reset & Base --- */
*, *::before, *::after {
  box-sizing: border-box;
}

html {
  font-size: var(--font-size-base);
  scroll-behavior: smooth;
  -webkit-text-size-adjust: 100%;
}

body {
  margin: 0;
  padding: 0;
  font-family: var(--font-body);
  font-size: 1rem;
  line-height: var(--line-height);
  color: var(--text);
  background: var(--bg);
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
}

/* --- Reading Progress Bar --- */
.progress-bar {
  position: fixed;
  top: 0;
  left: 0;
  width: 0%;
  height: 3px;
  background: var(--main-color);
  z-index: 1000;
  transition: width 50ms linear;
}

/* --- Layout Shell --- */
.page-wrapper {
  display: flex;
  min-height: 100vh;
}

/* --- Sidebar --- */
.sidebar {
  position: fixed;
  top: 0;
  left: 0;
  width: var(--sidebar-width);
  height: 100vh;
  background: var(--bg-alt);
  border-right: 1px solid var(--border);
  overflow-y: auto;
  padding: 2rem 0;
  z-index: 100;
  transition: transform 0.3s ease;
}

.sidebar-header {
  padding: 0 1.25rem 1.5rem;
  border-bottom: 1px solid var(--border);
  margin-bottom: 1rem;
}

.sidebar-title {
  font-family: var(--font-heading);
  font-size: 0.95rem;
  font-weight: 600;
  color: var(--main-color);
  text-decoration: none;
  line-height: 1.3;
  display: block;
}

.sidebar-title:hover {
  color: var(--chapter-red);
}

.sidebar-author {
  font-family: var(--font-ui);
  font-size: 0.72rem;
  color: var(--text-muted);
  margin-top: 0.35rem;
  text-transform: uppercase;
  letter-spacing: 0.05em;
}

.sidebar-nav {
  list-style: none;
  padding: 0;
  margin: 0;
}

.sidebar-nav li {
  margin: 0;
}

.sidebar-nav a {
  display: block;
  padding: 0.4rem 1.25rem;
  font-family: var(--font-ui);
  font-size: 0.82rem;
  color: var(--text-muted);
  text-decoration: none;
  border-left: 3px solid transparent;
  transition: all 0.15s ease;
}

.sidebar-nav a:hover {
  color: var(--text);
  background: rgba(0, 0, 0, 0.03);
}

.sidebar-nav a.active {
  color: var(--main-color);
  border-left-color: var(--main-color);
  font-weight: 600;
  background: rgba(139, 37, 0, 0.04);
}

.sidebar-nav .chapter-link {
  font-weight: 500;
  color: var(--text);
}

.sidebar-nav .section-list {
  list-style: none;
  padding: 0;
  margin: 0;
  max-height: 0;
  overflow: hidden;
  transition: max-height 0.3s ease;
}

.sidebar-nav .chapter-item.expanded .section-list {
  max-height: 500px;
}

.sidebar-nav .section-list a {
  padding-left: 2.25rem;
  font-size: 0.78rem;
  font-weight: 400;
}

.sidebar-nav .nav-divider {
  height: 1px;
  background: var(--border);
  margin: 0.75rem 1.25rem;
}

/* --- Hamburger (mobile) --- */
.hamburger {
  display: none;
  position: fixed;
  top: 1rem;
  left: 1rem;
  z-index: 200;
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: 6px;
  padding: 0.5rem;
  cursor: pointer;
  box-shadow: 0 1px 3px rgba(0,0,0,0.1);
}

.hamburger svg {
  width: 20px;
  height: 20px;
  stroke: var(--text);
}

/* --- Main Content --- */
.main-content {
  margin-left: var(--sidebar-width);
  flex: 1;
  display: flex;
  justify-content: center;
  padding: 3rem 2rem 6rem;
}

.content-column {
  max-width: var(--content-width);
  width: 100%;
}

/* --- Chapter Header --- */
.chapter-header {
  margin-bottom: 3rem;
  padding-bottom: 2rem;
  border-bottom: 1px solid var(--border);
}

.chapter-number {
  font-family: var(--font-heading);
  font-size: clamp(3.5rem, 8vw, 6rem);
  font-weight: 700;
  color: var(--chapter-red);
  line-height: 1;
  margin: 0;
  opacity: 0.85;
}

.chapter-title {
  font-family: var(--font-heading);
  font-size: clamp(1.6rem, 3vw, 2.2rem);
  font-weight: 400;
  color: var(--text);
  margin: 0.25rem 0 0;
  line-height: 1.2;
}

/* --- Mini-TOC --- */
.mini-toc {
  background: var(--bg-alt);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 1.25rem 1.5rem;
  margin-bottom: 2.5rem;
}

.mini-toc-title {
  font-family: var(--font-ui);
  font-size: 0.75rem;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 0.08em;
  color: var(--text-muted);
  margin: 0 0 0.75rem;
}

.mini-toc ol {
  list-style: none;
  padding: 0;
  margin: 0;
  columns: 2;
  column-gap: 2rem;
}

.mini-toc li {
  break-inside: avoid;
  margin-bottom: 0.35rem;
}

.mini-toc a {
  font-family: var(--font-ui);
  font-size: 0.85rem;
  color: var(--text-muted);
  text-decoration: none;
  transition: color 0.15s;
}

.mini-toc a:hover {
  color: var(--main-color);
}

/* --- Typography --- */
h1, h2, h3, h4, h5, h6 {
  font-family: var(--font-heading);
  font-weight: 600;
  color: var(--text);
  margin-top: 2.5rem;
  margin-bottom: 0.75rem;
  line-height: 1.25;
}

h2 {
  font-size: 1.55rem;
  padding-bottom: 0.3rem;
  border-bottom: 1px solid var(--border);
}

h3 {
  font-size: 1.25rem;
}

h4 {
  font-size: 1.05rem;
}

h5 {
  font-size: 1rem;
  font-weight: 700;
  margin-top: 1.75rem;
  margin-bottom: 0.5rem;
}

p {
  margin: 0 0 1.15rem;
  text-align: justify;
  hyphens: auto;
}

a {
  color: var(--main-color);
  text-decoration: none;
}

a:hover {
  text-decoration: underline;
}

a.ref-link {
  color: var(--main-color);
  text-decoration: none;
  border-bottom: 1px dotted var(--main-color);
}

a.ref-link:hover {
  border-bottom-style: solid;
}

blockquote {
  margin: 1.5rem 0;
  padding: 0.5rem 0 0.5rem 1.5rem;
  border-left: 3px solid var(--border);
  color: var(--text-muted);
  font-style: italic;
}

strong {
  font-weight: 700;
}

em {
  font-style: italic;
}

/* --- Figures --- */
figure {
  margin: 2rem 0;
  text-align: center;
}

/* Breakout figures for wide content */
figure.wide {
  max-width: var(--breakout-width);
  margin-left: calc((var(--content-width) - var(--breakout-width)) / 2);
  margin-right: calc((var(--content-width) - var(--breakout-width)) / 2);
}

figure > p {
  text-align: center;
  margin: 0;
}

figure img,
figure svg,
figure object {
  max-width: 100%;
  height: auto;
  display: inline-block;
  vertical-align: middle;
  mix-blend-mode: multiply;
}

/* Single image in a figure: fill the content width */
figure > img:only-of-type,
figure > p > img:only-of-type,
figure > div > p > img:only-of-type,
figure > div.center > img:only-of-type {
  width: 100%;
}

/* Images inside figure tables (converted from LaTeX tabular grids) should
   fill their cell rather than honour the inline width:48% from \textwidth. */
figure table img {
  width: 100% !important;
  max-width: 100%;
}



.fig-label,
.tab-label {
  font-family: var(--font-ui);
  font-weight: 700;
  color: var(--text);
}

figcaption {
  font-family: var(--font-ui);
  font-size: 0.85rem;
  color: var(--text-muted);
  margin-top: 0.75rem;
  line-height: 1.5;
  text-align: justify;
  hyphens: auto;
}

figcaption strong {
  color: var(--text);
}

/* Center divs (from LaTeX \centering) */
.center {
  text-align: center;
}

/* Subfigures grid */
.subfigures {
  display: flex;
  flex-wrap: wrap;
  gap: 1rem;
  justify-content: center;
  align-items: flex-end;
}

.subfigure {
  flex: 1 1 45%;
  min-width: 200px;
  text-align: center;
}

.subfigure img {
  max-width: 100%;
}

.subfigure-caption {
  font-family: var(--font-ui);
  font-size: 0.8rem;
  color: var(--text-muted);
  margin-top: 0.5rem;
}

/* --- Tables --- */
table {
  width: 100%;
  border-collapse: collapse;
  margin: 1.5rem 0;
  font-size: 0.9rem;
}

thead {
  border-bottom: 2px solid var(--text);
}

th {
  font-family: var(--font-ui);
  font-weight: 600;
  text-align: left;
  padding: 0.6rem 0.75rem;
  font-size: 0.85rem;
}

td {
  padding: 0.5rem 0.75rem;
  border-bottom: 1px solid var(--border);
}

tbody tr:hover {
  background: rgba(0, 0, 0, 0.02);
}

caption {
  font-family: var(--font-ui);
  font-size: 0.85rem;
  color: var(--text-muted);
  caption-side: top;
  text-align: justify;
  hyphens: auto;
  line-height: 1.5;
  margin-bottom: 0.5rem;
}

/* Scrollable table container for landscape tables — break out of content column */
.table-scroll {
  overflow-x: auto;
  margin: 1.5rem 0;
  -webkit-overflow-scrolling: touch;
  max-width: var(--breakout-width);
  margin-left: calc((var(--content-width) - var(--breakout-width)) / 2);
  margin-right: calc((var(--content-width) - var(--breakout-width)) / 2);
}

.table-scroll table {
  font-size: 0.8rem;
}

.table-scroll th,
.table-scroll td {
  padding: 0.4rem 0.5rem;
}

/* Colored cells for best/second-best results */
td.best {
  background: rgba(128, 0, 128, 0.08);
  font-weight: 600;
}

td.second-best {
  background: rgba(0, 128, 128, 0.08);
}

/* --- Equations --- */
/* Hide raw LaTeX until MathJax renders it */
.math.display, .math.inline {
  opacity: 0;
  transition: opacity 0.15s;
}
.mathjax-rendered .math.display,
.mathjax-rendered .math.inline {
  opacity: 1;
}

mjx-container {
  font-size: 100% !important;
}

.MathJax {
  overflow-x: hidden;
  overflow-y: hidden;
}

mjx-container[display="true"] {
  margin: 1.5rem 0 !important;
  overflow: hidden;
  padding: 0.25rem 0;
  max-width: 100%;
  scrollbar-width: none;
  -ms-overflow-style: none;
}
mjx-container[display="true"]::-webkit-scrollbar {
  display: none;
}

.equation-wrapper {
  position: relative;
}

/* --- Lists --- */
ul, ol {
  padding-left: 1.5rem;
  margin: 0.75rem 0;
}

li {
  margin-bottom: 0.35rem;
}

li > p {
  margin-bottom: 0.5rem;
}

/* --- QED Square --- */
.qed-square {
  display: block;
  text-align: right;
  font-size: 0.9rem;
  margin-top: -0.5em;
  color: var(--text-muted);
}

/* --- References Section (pandoc-citeproc) --- */
.references {
  font-size: 0.85rem;
  line-height: 1.6;
  margin-top: 3rem;
  padding-top: 1.5rem;
  border-top: 1px solid var(--border);
}


.references h2,
.references h3 {
  font-family: var(--font-ui);
  font-size: 1.1rem;
  margin-bottom: 1rem;
}

.csl-entry {
  margin-bottom: 0.75rem;
  padding-left: 2em;
  text-indent: -2em;
}

.csl-left-margin {
  float: left;
  padding-right: 0.5em;
  min-width: 2em;
}

.csl-right-inline {
  margin-left: 2em;
}

/* --- Citations --- */
.citation {
  cursor: pointer;
  color: var(--main-color);
  position: relative;
}

.citation:hover {
  text-decoration: underline;
}

.citation-tooltip {
  display: none;
  position: absolute;
  bottom: 100%;
  left: 50%;
  transform: translateX(-50%);
  background: white;
  border: 1px solid var(--border);
  border-radius: 6px;
  padding: 0.75rem 1rem;
  box-shadow: 0 4px 12px rgba(0,0,0,0.1);
  font-family: var(--font-ui);
  font-size: 0.8rem;
  line-height: 1.5;
  color: var(--text);
  max-width: 350px;
  width: max-content;
  z-index: 50;
  pointer-events: none;
}

.citation:hover .citation-tooltip {
  display: block;
}

/* --- Citation Popup (click-to-show) --- */
.citation-overlay {
  display: none;
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.2);
  z-index: 900;
}

.citation-overlay.visible {
  display: block;
}

.citation-popup {
  display: none;
  position: absolute;
  z-index: 910;
  background: white;
  border: 1px solid var(--border);
  border-radius: 8px;
  box-shadow: 0 8px 30px rgba(0, 0, 0, 0.15);
  max-width: 550px;
  width: 90vw;
  padding: 1.25rem 2.5rem 1.25rem 1.5rem;
  font-family: var(--font-ui);
  font-size: 0.85rem;
  line-height: 1.6;
  color: var(--text);
}

.citation-popup.visible {
  display: block;
}

.citation-popup-close {
  position: absolute;
  top: 0.5rem;
  right: 0.75rem;
  background: none;
  border: none;
  font-size: 1.3rem;
  color: var(--text-muted);
  cursor: pointer;
  padding: 0.25rem;
  line-height: 1;
}

.citation-popup-close:hover {
  color: var(--text);
}

.citation-popup-entry {
  padding: 0.5rem 0;
  border-bottom: 1px solid var(--border);
}

.citation-popup-entry:last-child {
  border-bottom: none;
}

.citation-popup-entry .csl-left-margin {
  display: none;
}

.citation-popup-entry .csl-right-inline {
  margin: 0;
}

/* --- Sidenotes / Margin Notes (Distill-style) --- */
.sidenote {
  float: right;
  clear: right;
  margin-right: calc(-1 * var(--margin-width) - 2rem);
  width: var(--margin-width);
  font-family: var(--font-ui);
  font-size: 0.78rem;
  line-height: 1.5;
  color: var(--text-muted);
  position: relative;
}

.sidenote-number {
  font-size: 0.7rem;
  vertical-align: super;
  color: var(--main-color);
  font-weight: 600;
}

/* --- Margin Citation Notes --- */
.citation-margin-note {
  float: right;
  clear: right;
  margin-right: calc(-1 * var(--margin-width) - 2rem);
  width: var(--margin-width);
  font-family: var(--font-ui);
  font-size: 0.75rem;
  line-height: 1.5;
  color: var(--text-muted);
  position: relative;
  padding: 0.5rem 1.5rem 0.5rem 0.75rem;
  border-left: 2px solid var(--main-color);
  margin-top: 0;
  margin-bottom: 0.5rem;
}

.citation-margin-close {
  position: absolute;
  top: 0;
  right: 0;
  background: none;
  border: none;
  font-size: 1rem;
  color: var(--text-muted);
  cursor: pointer;
  padding: 0.1rem 0.3rem;
  line-height: 1;
}

.citation-margin-close:hover {
  color: var(--text);
}

.citation-margin-entry {
  margin-bottom: 0.5rem;
}

.citation-margin-entry:last-child {
  margin-bottom: 0;
}

.citation-margin-entry .csl-left-margin {
  display: none;
}

.citation-margin-entry .csl-right-inline {
  margin: 0;
}

/* --- Prev / Next Navigation --- */
.chapter-nav {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 2rem;
  margin-top: 4rem;
  padding-top: 2rem;
  border-top: 1px solid var(--border);
}

.chapter-nav a {
  display: flex;
  flex-direction: column;
  text-decoration: none;
  max-width: 45%;
}

.chapter-nav-label {
  font-family: var(--font-ui);
  font-size: 0.72rem;
  text-transform: uppercase;
  letter-spacing: 0.08em;
  color: var(--text-muted);
  margin-bottom: 0.25rem;
}

.chapter-nav-title {
  font-family: var(--font-heading);
  font-size: 1rem;
  color: var(--main-color);
  font-weight: 500;
}

.chapter-nav a:hover .chapter-nav-title {
  text-decoration: underline;
}

.chapter-nav .next {
  text-align: right;
  margin-left: auto;
}

/* --- Footer --- */
.page-footer {
  margin-top: 4rem;
  padding: 2rem 0;
  border-top: 1px solid var(--border);
  font-family: var(--font-ui);
  font-size: 0.78rem;
  color: var(--text-muted);
  text-align: center;
}

/* Mid-table section header rows (e.g. RMSE row in multi-section tables) */
.table-section-header {
  border-top: 2px solid var(--text);
}

.table-section-header th {
  font-family: var(--font-ui);
  font-weight: 600;
  font-size: 0.85rem;
  padding-top: 0.8rem;
}

.table-section-header + tr td {
  font-family: var(--font-ui);
  font-weight: 600;
  font-size: 0.82rem;
  border-bottom: 1px solid var(--text);
}

/* --- Appendix B: wider content for large tables --- */
.page-appendix-b .content-column {
  max-width: 1200px;
}

.page-appendix-b table {
  font-size: 0.72rem;
}

.page-appendix-b .main-content {
  padding-left: 1rem;
  padding-right: 1rem;
}

/* --- Responsive --- */
@media (max-width: 1200px) {
  :root {
    --margin-width: 0px;
  }

  .sidenote {
    float: none;
    display: block;
    margin: 0.75rem 0 0.75rem 1.5rem;
    width: auto;
    padding: 0.5rem 0.75rem;
    background: var(--bg-alt);
    border-left: 2px solid var(--border);
    border-radius: 0 4px 4px 0;
  }
}

@media (max-width: 768px) {
  html {
    font-size: 16px;
  }

  .sidebar {
    transform: translateX(-100%);
  }

  .sidebar.open {
    transform: translateX(0);
    box-shadow: 4px 0 20px rgba(0,0,0,0.15);
  }

  .hamburger {
    display: block;
  }

  .main-content {
    margin-left: 0;
    padding: 1.5rem 1rem 4rem;
  }

  .chapter-number {
    font-size: 3rem;
  }

  .mini-toc ol {
    columns: 1;
  }

  .chapter-nav {
    flex-direction: column;
    gap: 1rem;
  }

  .chapter-nav a {
    max-width: 100%;
  }

  .chapter-nav .next {
    text-align: left;
  }

  figure.wide {
    margin-left: -1rem;
    margin-right: -1rem;
    max-width: calc(100% + 2rem);
  }

  mjx-container[display="true"] {
    font-size: 0.9rem;
  }
}

/* --- Print --- */
@media print {
  .sidebar,
  .hamburger,
  .progress-bar,
  .chapter-nav {
    display: none !important;
  }

  .main-content {
    margin-left: 0;
  }

  body {
    font-size: 11pt;
    line-height: 1.5;
  }

  a {
    color: var(--text);
    text-decoration: none;
  }

  a[href]::after {
    content: none;
  }
}
