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

html {
  font-size: 18px;
  line-height: 1.6;
  scroll-behavior: smooth;
}

body {
  font-family: Georgia, 'Times New Roman', serif;
  color: #222;
  background: #fff;
  margin: 0;
  padding: 0;
}

/* Layout */
article {
  max-width: 48rem;
  margin: 0 auto;
  padding: 2rem 1.5rem;
}

/* Title */
h1.title {
  font-size: 1.8rem;
  margin: 1rem 0 1rem;
  line-height: 1.3;
  text-align: center;
}

/* Top-level sections (Pandoc uses h1 for \section) */
main h1 {
  font-size: 1.4rem;
}

/* Abstract */
.abstract {
  text-align: left;
  margin: 0 auto 2rem;
  max-width: 40rem;
  font-size: 0.95rem;
  font-weight: bold;
}

/* ============ Page toolbar (Contents + Cite) ============ */
#page-toolbar {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  z-index: 1001;
  background: #f0f4f8;
  padding: 0.4rem 20px;
}

#page-toolbar-spacer {
  height: 2.2rem;
}

#page-toolbar-inner {
  max-width: 800px;
  margin: 0 auto;
  display: flex;
  justify-content: space-between;
  align-items: center;
}

/* ============ Sidebar TOC ============ */
#toc-toggle {
  background: transparent;
  border: 1px solid #ddd;
  border-radius: 4px;
  padding: 0.4rem 0.8rem;
  font-size: 0.85rem;
  font-family: inherit;
  cursor: pointer;
  color: #333;
}

#toc-toggle:hover {
  background: #eee;
}

nav#TOC {
  position: fixed;
  top: 0;
  left: 0;
  width: 320px;
  max-width: 85vw;
  height: 100vh;
  background: #fff;
  border-right: 1px solid #ddd;
  z-index: 1002;
  transform: translateX(-100%);
  transition: transform 0.3s ease;
  overflow-y: auto;
  padding: 0;
}

nav#TOC.open {
  transform: translateX(0);
}

.toc-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 1rem 1.25rem;
  border-bottom: 1px solid #eee;
  position: sticky;
  top: 0;
  background: #fff;
}

.toc-header h2 {
  margin: 0;
  font-size: 1.1rem;
  border-bottom: none;
  padding-bottom: 0;
}

#toc-close {
  background: none;
  border: none;
  font-size: 1.5rem;
  cursor: pointer;
  color: #666;
  padding: 0 0.25rem;
  line-height: 1;
}

#toc-close:hover {
  color: #222;
}

.toc-body {
  padding: 1rem 1.25rem;
}

.toc-body ul {
  margin: 0;
  padding-left: 0;
  list-style: none;
}

.toc-body ul ul {
  padding-left: 1.25rem;
  overflow: hidden;
  max-height: 0;
  transition: max-height 0.25s ease;
}

.toc-body li.expanded > ul {
  max-height: 500px;
}

.toc-body li {
  margin: 0;
  font-size: 0.9rem;
}

.toc-body li > a,
.toc-body li > .toc-item {
  display: flex;
  align-items: baseline;
  padding: 0.3rem 0;
}

.toc-toggle-arrow {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 1rem;
  flex-shrink: 0;
  cursor: pointer;
  color: #888;
  font-size: 0.65rem;
  transition: transform 0.2s ease;
  user-select: none;
  margin-right: 0.15rem;
}

.toc-toggle-arrow:hover {
  color: #333;
}

li.expanded > .toc-item > .toc-toggle-arrow {
  transform: rotate(90deg);
}

/* Leaf items (no children) get a spacer instead of arrow */
.toc-spacer {
  display: inline-block;
  width: 1rem;
  flex-shrink: 0;
  margin-right: 0.15rem;
}

.toc-body a {
  color: #333;
  text-decoration: none;
}

.toc-body a:hover {
  text-decoration: underline;
}

#toc-overlay {
  position: fixed;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  background: rgba(0,0,0,0.3);
  z-index: 1000;
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.3s ease;
}

#toc-overlay.open {
  opacity: 1;
  pointer-events: auto;
}

/* ============ Cite link ============ */
#cite-link {
  background: transparent;
  border: 1px solid #ddd;
  border-radius: 4px;
  padding: 0.4rem 0.8rem;
  font-size: 0.85rem;
  font-family: inherit;
  color: #1a5276;
  text-decoration: none;
}

#cite-link:hover {
  background: #eee;
  text-decoration: underline;
}

/* ============ Headings ============ */
h1, h2, h3, h4, h5, h6 {
  color: #111;
  margin-top: 2rem;
  margin-bottom: 0.75rem;
  line-height: 1.3;
  scroll-margin-top: 3rem;
}

h2 {
  font-size: 1.2rem;
  border-bottom: 1px solid #eee;
  padding-bottom: 0.3rem;
}

h3 {
  font-size: 1.1rem;
}

h4 {
  font-size: 1.0rem;
}

/* Paragraphs */
p {
  margin: 0 0 1rem;
}

/* Links */
a {
  color: #1a5276;
}

a:hover {
  color: #0d2f4a;
}

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

figure img {
  max-width: 100%;
  height: auto;
  display: block;
  margin: 0 auto;
}

figcaption {
  font-size: 0.85rem;
  color: #444;
  text-align: left;
  margin-top: 0.75rem;
  line-height: 1.5;
  padding: 0 1rem;
}

/* Math display */
.math.display {
  overflow-x: auto;
  padding: 0.5rem 0;
}

/* ============ Notation table ============ */
table {
  border-collapse: collapse;
  margin: 1.5rem auto;
  font-size: 0.9rem;
  width: 100%;
  border: 1px solid #333;
}

td, th {
  padding: 0.4rem 0.75rem;
  text-align: left;
  vertical-align: top;
}

td:first-child {
  white-space: nowrap;
  padding-right: 1.5rem;
}

/* ============ Code ============ */
code {
  font-family: 'Menlo', 'Consolas', 'Courier New', monospace;
  font-size: 0.88em;
  padding: 0;
  background: transparent;
}

/* Colored code (from Lua filter or Pandoc textcolor) should have transparent background */
code[style*="color"],
span[style*="color"] > code {
  background: transparent;
  padding: 0;
}

pre {
  background: #f4f4f4;
  padding: 1rem;
  overflow-x: auto;
  border-radius: 4px;
  font-size: 0.85rem;
}

pre code {
  padding: 0;
  background: none;
}

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

li {
  margin-bottom: 0.25rem;
}

/* ============ Blockquotes ============ */
blockquote {
  border-left: 3px solid #ccc;
  margin: 1rem 0;
  padding: 0.5rem 1rem;
  color: #555;
}

/* ============ References / bibliography ============ */
#refs {
  font-size: 0.85rem;
}

#refs .csl-entry {
  display: flex;
  margin-bottom: 0.75rem;
}

.csl-left-margin {
  min-width: 2rem;
  flex-shrink: 0;
}

.csl-right-inline {
  flex: 1;
}

/* ============ Footnotes ============ */
.footnotes {
  font-size: 0.85rem;
  border-top: 1px solid #ddd;
  margin-top: 2rem;
  padding-top: 1rem;
}

/* Footnote hover tooltip */
.footnote-tooltip {
  display: none;
  position: absolute;
  bottom: calc(100% + 4px);
  left: 50%;
  transform: translateX(-50%);
  width: 320px;
  max-width: 90vw;
  background: #fff;
  border: 1px solid #ddd;
  border-radius: 4px;
  padding: 0.6rem 0.8rem;
  font-size: 0.85rem;
  line-height: 1.4;
  box-shadow: 0 2px 8px rgba(0,0,0,0.15);
  z-index: 100;
}

.footnote-tooltip.visible {
  display: block;
}

.footnote-tooltip p {
  margin: 0;
}

/* Citation hover tooltip */
.citation-tooltip {
  display: none;
  position: absolute;
  bottom: calc(100% + 4px);
  left: 50%;
  transform: translateX(-50%);
  width: 360px;
  max-width: 90vw;
  background: #fff;
  border: 1px solid #ddd;
  border-radius: 4px;
  padding: 0.6rem 0.8rem;
  font-size: 0.82rem;
  line-height: 1.4;
  box-shadow: 0 2px 8px rgba(0,0,0,0.15);
  z-index: 100;
  text-decoration: none;
  color: #222;
  font-weight: normal;
}

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

/* Figure hover tooltip */
#figure-tooltip {
  display: none;
  position: fixed;
  width: 420px;
  max-width: 90vw;
  background: #fff;
  border: 1px solid #ddd;
  border-radius: 4px;
  padding: 0.5rem;
  box-shadow: 0 2px 8px rgba(0,0,0,0.15);
  z-index: 2000;
  pointer-events: none;
}

#figure-tooltip.visible {
  display: block;
}

#figure-tooltip img {
  width: 100%;
  height: auto;
  display: block;
}

/* ============ Citation section ============ */
#citation {
  border-top: 2px solid #ddd;
  margin-top: 3rem;
  padding-top: 2rem;
  scroll-margin-top: 3rem;
}

.citation-box {
  background: #f0f4f8;
  border: 1px solid #e0e0e0;
  border-radius: 4px;
  padding: 1rem 1.25rem;
  margin: 1rem 0;
}

.citation-box h3 {
  margin-top: 0;
  margin-bottom: 0.5rem;
  font-size: 1rem;
  border-bottom: none;
  padding-bottom: 0;
}

.citation-box pre {
  margin: 0;
  border: none;
}

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

  article {
    padding: 1rem;
  }

  figcaption {
    padding: 0;
  }
}

@media (max-width: 480px) {
  html {
    font-size: 15px;
  }

  td:first-child {
    white-space: normal;
  }

  .footnote-tooltip {
    left: 0;
    transform: none;
    width: 260px;
  }
}

/* ============ Print ============ */
@media print {
  #toc-toggle,
  #toc-overlay,
  nav#TOC,
  #cite-link {
    display: none;
  }

  article {
    max-width: none;
    padding: 0;
  }

  a {
    color: #000;
    text-decoration: none;
  }
}
