/* ==============================================================================
   The Principles of Diffusion Models - Responsive HTML Edition Stylesheet
   Designed for comfortable reading on Mobile, Tablet, and Desktop screens.
   Zero emojis - clean, elegant, professional academic book typography.
   ============================================================================== */

:root {
  --font-sans: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  --font-serif: "Charter", "Bitstream Charter", "Sitka Text", "Cambria", Georgia, serif;
  --font-mono: "SFMono-Regular", Consolas, "Liberation Mono", Menlo, Courier, monospace;

  /* Light Theme (Default) */
  --bg-body: #fbfbfd;
  --bg-content: #ffffff;
  --bg-sidebar: #f5f5f7;
  --bg-card: #f8f9fa;
  --bg-code: #f1f3f5;
  --border-color: #e5e5ea;
  --border-light: #ececf1;
  --text-main: #1d1d1f;
  --text-muted: #6e6e73;
  --text-link: #0066cc;
  --accent-color: #9a5b32; /* StoryBrown from original book */
  --accent-light: #fbf6f0;
  --shadow-sm: 0 1px 3px rgba(0, 0, 0, 0.05);
  --shadow-md: 0 4px 12px rgba(0, 0, 0, 0.08);
  --shadow-lg: 0 8px 24px rgba(0, 0, 0, 0.12);

  /* Callout colors */
  --callout-guide-border: #23415c;
  --callout-guide-bg: #f0f4f8;
  --callout-takeaway-border: #4f7c63;
  --callout-takeaway-bg: #eef6f0;
  --callout-def-border: #9a5b32;
  --callout-def-bg: #fbf6f0;
  --callout-thm-border: #e67814;
  --callout-thm-bg: #fef8f0;
  --callout-proof-border: #6d5b8c;
  --callout-proof-bg: #f6f4f9;

  --font-base-size: 17px;
  --line-height-base: 1.7;
  --content-max-width: 820px;
  --sidebar-width: 310px;
  --topbar-height: 56px;
}

/* Sepia Theme */
[data-theme="sepia"] {
  --bg-body: #f4ecd8;
  --bg-content: #fbf0d9;
  --bg-sidebar: #ede2c9;
  --bg-card: #f2e7ce;
  --bg-code: #eae0c8;
  --border-color: #d8cbb0;
  --border-light: #e4d8be;
  --text-main: #3d3226;
  --text-muted: #7d6e5a;
  --text-link: #8d4925;
  --accent-color: #8d4925;
  --accent-light: #f5e9d0;
  --shadow-sm: 0 1px 3px rgba(60, 40, 20, 0.08);

  --callout-guide-border: #3d586e;
  --callout-guide-bg: #e8e3d5;
  --callout-takeaway-border: #4b6b55;
  --callout-takeaway-bg: #e5ecd6;
  --callout-def-border: #8d4925;
  --callout-def-bg: #f0e2cc;
  --callout-thm-border: #af5c1b;
  --callout-thm-bg: #f4e8d2;
  --callout-proof-border: #614f7d;
  --callout-proof-bg: #eee5f0;
}

/* Dark Theme (Pure OLED Black #000000) */
[data-theme="dark"] {
  --bg-body: #000000;
  --bg-content: #000000;
  --bg-sidebar: #000000;
  --bg-card: #0d0d0f;
  --bg-code: #121215;
  --border-color: #202024;
  --border-light: #161619;
  --text-main: #f0f0f3;
  --text-muted: #8e8e93;
  --text-link: #64a8ff;
  --accent-color: #e5a93c;
  --accent-light: #1a1409;
  --shadow-sm: 0 1px 3px rgba(0, 0, 0, 0.8);

  --callout-guide-border: #60a5fa;
  --callout-guide-bg: #091321;
  --callout-takeaway-border: #34d399;
  --callout-takeaway-bg: #06170f;
  --callout-def-border: #e5a93c;
  --callout-def-bg: #171105;
  --callout-thm-border: #fb923c;
  --callout-thm-bg: #180e05;
  --callout-proof-border: #a78bfa;
  --callout-proof-bg: #120a1f;
}

/* Base Styles */
* {
  box-sizing: border-box;
  margin: 0;
  padding: 0;
}

html {
  font-size: var(--font-base-size);
  scroll-behavior: smooth;
  scroll-padding-top: calc(var(--topbar-height) + 1rem);
  -webkit-text-size-adjust: 100%;
  overflow-x: clip;
}

body {
  font-family: var(--font-serif);
  font-size: 1rem;
  line-height: var(--line-height-base);
  color: var(--text-main);
  background-color: var(--bg-body);
  transition:
    background-color 0.2s ease,
    color 0.2s ease;
  min-height: 100vh;
  display: flex;
  flex-direction: column;
  text-align: left;
  hyphens: none;
  -webkit-hyphens: none;
  word-break: normal;
  padding-top: var(--topbar-height);
  overflow-x: clip;
}

/* Fixed Top Navigation Bar (Always pinned at top of viewport) */
.topbar {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  height: var(--topbar-height);
  background: var(--bg-content);
  border-bottom: 1px solid var(--border-color);
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0 1rem;
  z-index: 100;
  backdrop-filter: blur(10px);
  -webkit-backdrop-filter: blur(10px);
  width: 100%;
}

.topbar-left,
.topbar-right {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  flex-shrink: 0;
}

.topbar-center {
  flex: 1;
  min-width: 0; /* CRITICAL: Enables flex child to shrink below intrinsic content size */
  text-align: center;
  font-family: var(--font-sans);
  font-weight: 600;
  font-size: 0.95rem;
  color: var(--text-main);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  padding: 0 0.5rem;
}

.btn-icon {
  background: none;
  border: 1px solid var(--border-color);
  border-radius: 6px;
  padding: 0.4rem 0.65rem;
  font-size: 0.9rem;
  font-family: var(--font-sans);
  color: var(--text-main);
  cursor: pointer;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 0.25rem;
  text-decoration: none !important;
  transition:
    background 0.15s ease,
    border-color 0.15s ease;
}

.btn-icon:hover,
.btn-icon:focus,
.btn-icon:active {
  background: var(--bg-card);
  border-color: var(--text-muted);
  text-decoration: none !important;
}

.reading-progress-bar {
  position: absolute;
  bottom: 0;
  left: 0;
  height: 2.5px;
  background: var(--accent-color);
  width: 0%;
  transition: width 0.1s ease;
}

/* Layout: Sidebar + Main Content */
.app-container {
  display: flex;
  flex: 1;
  position: relative;
  width: 100%;
  max-width: 100%;
  min-width: 0;
}

/* Sidebar Drawer */
.sidebar {
  width: var(--sidebar-width);
  background: var(--bg-sidebar);
  border-right: 1px solid var(--border-color);
  height: calc(100vh - var(--topbar-height));
  position: sticky;
  top: var(--topbar-height);
  overflow-y: auto;
  padding: 1.2rem 1rem 3rem;
  flex-shrink: 0;
  font-family: var(--font-sans);
  font-size: 0.88rem;
  transition:
    margin-left 0.25s ease,
    opacity 0.2s ease;
}

/* Collapsed state on desktop */
.sidebar.collapsed {
  margin-left: calc(-1 * var(--sidebar-width));
  opacity: 0;
  pointer-events: none;
}

.sidebar-header {
  margin-bottom: 1.2rem;
  padding-bottom: 0.8rem;
  border-bottom: 1px solid var(--border-color);
}

.sidebar-title {
  font-weight: 700;
  font-size: 1.05rem;
  color: var(--accent-color);
  text-decoration: none;
  display: block;
}

.sidebar-subtitle {
  font-size: 0.8rem;
  color: var(--text-muted);
  margin-top: 0.2rem;
}

.sidebar-part {
  font-weight: 700;
  font-size: 0.76rem;
  text-transform: uppercase;
  letter-spacing: 0.05em;
  color: var(--text-muted);
  margin: 1.2rem 0 0.4rem 0.4rem;
}

.sidebar-nav {
  list-style: none;
}

.sidebar-link {
  display: block;
  padding: 0.4rem 0.6rem;
  border-radius: 6px;
  color: var(--text-main);
  text-decoration: none;
  line-height: 1.4;
  margin-bottom: 0.15rem;
  transition:
    background 0.15s ease,
    color 0.15s ease;
}

.sidebar-link:hover {
  background: var(--bg-card);
  color: var(--accent-color);
}

.sidebar-link.active {
  background: var(--accent-light);
  color: var(--accent-color);
  font-weight: 600;
}

/* Main Reading Article */
.main-wrapper {
  flex: 1;
  display: flex;
  justify-content: center;
  padding: 2rem 1.5rem 5rem;
  min-width: 0;
  max-width: 100%;
}

.content-article {
  max-width: var(--content-max-width);
  width: 100%;
  min-width: 0;
  background: var(--bg-content);
  padding: 3rem 2.8rem;
  border-radius: 12px;
  border: 1px solid var(--border-color);
  box-shadow: var(--shadow-sm);
  overflow-wrap: break-word;
}

[data-theme="dark"] .content-article {
  box-shadow: none;
  border-color: #1c1c20;
}

/* Typography Inside Article */
h1 {
  font-family: var(--font-sans);
  font-size: 2.2rem;
  font-weight: 700;
  line-height: 1.25;
  color: var(--text-main);
  margin-bottom: 1.5rem;
  padding-bottom: 0.6rem;
  border-bottom: 1.5px solid var(--border-color);
}

h2 {
  font-family: var(--font-sans);
  font-size: 1.6rem;
  font-weight: 600;
  line-height: 1.35;
  color: var(--text-main);
  margin-top: 2.4rem;
  margin-bottom: 0.9rem;
}

h3 {
  font-family: var(--font-sans);
  font-size: 1.25rem;
  font-weight: 600;
  line-height: 1.4;
  color: var(--text-main);
  margin-top: 1.8rem;
  margin-bottom: 0.6rem;
}

h4 {
  font-family: var(--font-sans);
  font-size: 1.15rem;
  font-weight: 600;
  line-height: 1.4;
  color: var(--text-main);
  margin-top: 1.6rem;
  margin-bottom: 0.5rem;
}

h5 {
  font-family: var(--font-sans);
  font-size: 1.06rem;
  font-weight: 700;
  line-height: 1.45;
  color: var(--text-main);
  margin-top: 1.4rem;
  margin-bottom: 0.45rem;
}

h6 {
  font-family: var(--font-sans);
  font-size: 1rem;
  font-weight: 600;
  line-height: 1.45;
  color: var(--text-main);
  margin-top: 1.2rem;
  margin-bottom: 0.4rem;
}

p {
  margin-bottom: 1.25rem;
  text-align: left;
  hyphens: none;
  -webkit-hyphens: none;
  word-break: normal;
  overflow-wrap: break-word;
}

a {
  color: var(--text-link);
  text-decoration: underline;
  text-underline-offset: 2px;
}

ul,
ol {
  margin-bottom: 1.25rem;
  padding-left: 1.6rem;
}

li {
  margin-bottom: 0.4rem;
}

blockquote {
  border-left: 4px solid var(--accent-color);
  padding: 0.6rem 1.2rem;
  background: var(--bg-card);
  border-radius: 0 8px 8px 0;
  margin: 1.4rem 0;
  font-style: italic;
}

code {
  font-family: var(--font-mono);
  font-size: 0.88em;
  background: var(--bg-code);
  padding: 0.15em 0.35em;
  border-radius: 4px;
}

pre {
  background: var(--bg-code);
  padding: 1rem 1.2rem;
  border-radius: 8px;
  overflow-x: auto;
  margin: 1.4rem 0;
}

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

/* Figures & Diagrams (White card background guarantees dark mode legibility) */
.figure-container,
figure {
  margin: 2rem 0;
  text-align: center;
}

.figure-img,
figure img {
  max-width: 100%;
  height: auto;
  display: block;
  margin: 0 auto;
  background-color: #ffffff;
  padding: 0.65rem;
  border-radius: 8px;
  border: 1px solid var(--border-color);
  box-shadow: var(--shadow-sm);
}

figcaption,
.figure-caption {
  font-family: var(--font-sans);
  font-size: 0.88rem;
  line-height: 1.5;
  color: var(--text-muted);
  margin-top: 0.75rem;
  text-align: center;
  padding: 0 1rem;
}

/* Callouts / Pedagogical Boxes */
.callout {
  border-left: 4.5px solid var(--border-color);
  padding: 1.1rem 1.3rem;
  margin: 1.6rem 0;
  border-radius: 0 8px 8px 0;
  background: var(--bg-card);
  max-width: 100%;
  min-width: 0;
  overflow-x: auto;
}

.callout > p:first-child {
  margin-top: 0;
}

.callout > p:last-child {
  margin-bottom: 0;
}

.callout-chapterguide {
  border-left-color: var(--callout-guide-border);
  background: var(--callout-guide-bg);
}

.callout-takeaway {
  border-left-color: var(--callout-takeaway-border);
  background: var(--callout-takeaway-bg);
}

.callout-defn,
.callout-asum,
.callout-prob {
  border-left-color: var(--callout-def-border);
  background: var(--callout-def-bg);
}

.callout-thm,
.callout-lem,
.callout-prop,
.callout-cor,
.callout-claim,
.callout-fact,
.callout-example,
.callout-msg {
  border-left-color: var(--callout-thm-border);
  background: var(--callout-thm-bg);
}

.callout-proof {
  border-left-color: var(--callout-proof-border);
  background: var(--callout-proof-bg);
}

.callout-proof .qed,
.qed {
  display: inline-block;
  margin-left: 0.35em;
  font-size: 0.85em;
  line-height: 1;
  vertical-align: baseline;
}

.callout-remark {
  border-left-color: var(--accent-color);
  background: var(--bg-card);
}

.callout-algorithm,
.algorithm {
  border: 1px solid var(--border-color);
  border-left: 4.5px solid var(--callout-guide-border);
  padding: 1.1rem 1.3rem;
  margin: 1.8rem 0;
  border-radius: 8px;
  background: var(--bg-card);
  overflow-x: auto;
}

.algorithm .algorithmic,
.callout-algorithm .algorithmic {
  font-family: var(--font-mono);
  font-size: 0.9em;
  line-height: 1.6;
}

.question {
  border-left: 4.5px solid var(--accent-color);
  padding: 1.1rem 1.3rem;
  margin: 1.6rem 0;
  border-radius: 0 8px 8px 0;
  background: var(--callout-guide-bg);
  font-style: italic;
}

.question p:first-child {
  margin-top: 0;
}
.question p:last-child {
  margin-bottom: 0;
}

.mdframed {
  border: 1px solid var(--border-color);
  background: var(--bg-card);
  border-radius: 8px;
  padding: 1rem 1.25rem;
  margin: 1.5rem 0;
  box-shadow: var(--shadow-sm);
  overflow-x: auto;
}

.mdframed p:first-child {
  margin-top: 0;
}
.mdframed p:last-child {
  margin-bottom: 0;
}

.equation-anchor {
  position: relative;
  top: -80px;
  display: block;
  visibility: hidden;
}

/* Math Display Blocks (Horizontally scrollable on mobile, compact textbook spacing) */
span.math.display {
  display: block;
  margin: 0;
  padding: 0;
}

mjx-container[display="true"],
.math-display {
  display: block !important;
  overflow-x: auto !important;
  overflow-y: hidden !important;
  max-width: 100% !important;
  padding: 0.1rem 0 !important;
  margin: 0.45rem 0 !important;
  -webkit-overflow-scrolling: touch;
}

/* Eliminate excessive paragraph spacing when a paragraph contains display math */
p:has(> span.math.display:only-child),
p:has(> mjx-container[display="true"]:only-child) {
  margin-bottom: 0.6rem;
}

/* Inline Math Equations (Preserves natural typographic baseline alignment) */
.math.inline,
span.math.inline {
  display: inline;
  overflow: visible;
}

mjx-container:not([display="true"]) {
  display: inline-block;
  overflow: visible !important;
}

/* Tables in Responsive Scroll Container */
.table-wrapper {
  width: 100%;
  max-width: 100%;
  overflow-x: auto;
  margin: 1.5rem 0;
  -webkit-overflow-scrolling: touch;
}

table {
  width: 100%;
  max-width: 100%;
  border-collapse: collapse;
  font-family: var(--font-sans);
  font-size: 0.92rem;
}

th,
td {
  padding: 0.65rem 0.9rem;
  border: 1px solid var(--border-color);
  text-align: left;
}

th {
  background: var(--bg-card);
  font-weight: 600;
}

/* Chapter Navigation Footer */
.chapter-nav-footer {
  display: flex;
  justify-content: space-between;
  gap: 1.2rem;
  margin-top: 3.5rem;
  padding-top: 2rem;
  border-top: 1px solid var(--border-color);
}

.nav-button {
  flex: 1;
  padding: 1rem 1.25rem;
  border-radius: 8px;
  border: 1px solid var(--border-color);
  background: var(--bg-card);
  text-decoration: none !important;
  color: var(--text-main);
  transition:
    transform 0.15s ease,
    border-color 0.15s ease,
    box-shadow 0.15s ease;
}

.nav-button:hover {
  border-color: var(--accent-color);
  transform: translateY(-2px);
  box-shadow: var(--shadow-sm);
  text-decoration: none !important;
}

.nav-button-label {
  font-family: var(--font-sans);
  font-size: 0.76rem;
  text-transform: uppercase;
  letter-spacing: 0.05em;
  color: var(--text-muted);
  font-weight: 600;
  display: block;
  margin-bottom: 0.3rem;
}

.nav-button-title {
  font-family: var(--font-sans);
  font-size: 1rem;
  font-weight: 600;
  color: var(--text-link);
  display: block;
}

.nav-button.next {
  text-align: right;
}

/* Back to Top Floating Button */
.back-to-top {
  position: fixed;
  bottom: 2rem;
  right: 2rem;
  width: 44px;
  height: 44px;
  border-radius: 50%;
  background: var(--accent-color);
  color: #ffffff;
  border: none;
  font-size: 1.2rem;
  font-weight: 700;
  cursor: pointer;
  box-shadow: var(--shadow-md);
  opacity: 0;
  pointer-events: none;
  transition:
    opacity 0.2s ease,
    transform 0.15s ease;
  z-index: 80;
}

.back-to-top.visible {
  opacity: 1;
  pointer-events: auto;
}

.back-to-top:hover {
  transform: translateY(-2px);
}

/* Backdrop for Mobile Sidebar */
.sidebar-backdrop {
  position: fixed;
  inset: 0;
  top: var(--topbar-height);
  background: rgba(0, 0, 0, 0.4);
  z-index: 90;
  display: none;
  backdrop-filter: blur(2px);
}

/* Responsive Media Queries */
@media (max-width: 1024px) {
  .sidebar {
    position: fixed;
    left: 0;
    top: var(--topbar-height);
    margin-left: 0 !important;
    transform: translateX(-100%);
    z-index: 95;
    box-shadow: var(--shadow-lg);
    opacity: 1 !important;
    pointer-events: auto !important;
    transition: transform 0.25s ease;
  }

  .sidebar.open {
    transform: translateX(0);
  }

  .sidebar-backdrop.open {
    display: block;
  }

  .content-article {
    padding: 2.2rem 1.8rem;
  }
}

@media (max-width: 640px) {
  :root {
    --font-base-size: 16px;
    --line-height-base: 1.65;
  }

  .topbar {
    padding: 0 0.5rem;
  }

  .topbar-center {
    font-size: 0.85rem;
    padding: 0 0.35rem;
  }

  .topbar-left,
  .topbar-right {
    gap: 0.25rem;
  }

  .btn-icon {
    padding: 0.35rem 0.5rem;
    font-size: 0.82rem;
  }

  .main-wrapper {
    padding: 1rem 0.4rem 3rem;
  }

  .content-article {
    padding: 1.25rem 0.8rem;
    border-radius: 6px;
    border: none;
    box-shadow: none;
  }

  h1 {
    font-size: 1.55rem;
  }

  h2 {
    font-size: 1.25rem;
  }

  h3 {
    font-size: 1.1rem;
  }

  .chapter-nav-footer {
    flex-direction: column;
  }

  .nav-button {
    max-width: 100%;
  }

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

/* Print Stylesheet (Optimized for academic paper/PDF export) */
@media print {
  .topbar,
  .sidebar,
  .sidebar-backdrop,
  .back-to-top,
  .reading-progress-bar,
  .chapter-nav-footer {
    display: none !important;
  }

  body {
    padding-top: 0 !important;
    background: #ffffff !important;
    color: #000000 !important;
    font-size: 11pt !important;
    line-height: 1.5 !important;
  }

  .main-wrapper {
    padding: 0 !important;
  }

  .content-article {
    border: none !important;
    box-shadow: none !important;
    padding: 0 !important;
    max-width: 100% !important;
  }

  a {
    color: #000000 !important;
    text-decoration: underline !important;
  }

  mjx-container[display="true"],
  .math-display {
    overflow: visible !important;
  }
}
