/* CS329Z Publication-Grade Technical Documentation Stylesheet */
:root {
  --paper: #fbfaf6;
  --paper-elevated: #ffffff;
  --rail: #f3f1ea;
  --rail-hover: #e8e4da;
  --ink: #1e211e;
  --ink-secondary: #3f443f;
  --muted: #686d66;
  --line: #e2dfd7;
  --line-strong: #cfcbc0;
  --accent: #8c1d18;
  --accent-hover: #6e1511;
  --accent-soft: #f3e5e2;
  --accent-border: #dfbab4;
  --code-bg: #1e2321;
  --code-header: #282e2b;
  --code-ink: #edf0ec;
  --inline-code-bg: #efece4;
  --inline-code-ink: #7a271f;
  --callout-bg: #f4f1e8;
  --table-head: #f1efe7;
  --table-hover: #f6f4ed;
  --mark-bg: #f3d98b;
  --body-size: 16.5px;
}

body[data-theme="dark"] {
  --paper: #131615;
  --paper-elevated: #1b1f1d;
  --rail: #171a19;
  --rail-hover: #232826;
  --ink: #e8ece9;
  --ink-secondary: #c5ccc7;
  --muted: #8f9891;
  --line: #282e2b;
  --line-strong: #38403c;
  --accent: #e06d64;
  --accent-hover: #eb857d;
  --accent-soft: #2e1d1c;
  --accent-border: #5c3330;
  --code-bg: #0d100f;
  --code-header: #181c1a;
  --code-ink: #e6ebe8;
  --inline-code-bg: #232826;
  --inline-code-ink: #f0958d;
  --callout-bg: #1b201e;
  --table-head: #1c211f;
  --table-hover: #181d1b;
  --mark-bg: #6b5314;
}

* {
  box-sizing: border-box;
}

html {
  scroll-padding-top: 88px;
  scroll-behavior: smooth;
}

body {
  margin: 0;
  background: var(--paper);
  color: var(--ink);
  font-family: -apple-system, BlinkMacSystemFont, "SF Pro Text", "PingFang SC", "Hiragino Sans GB", "Noto Sans SC", "Microsoft YaHei", sans-serif;
  line-height: 1.75;
  -webkit-font-smoothing: antialiased;
  transition: background-color 0.15s ease, color 0.15s ease;
}

a {
  color: var(--accent);
  text-decoration-thickness: 1px;
  text-underline-offset: 4px;
}

a:hover {
  color: var(--accent-hover);
}

button,
input {
  font: inherit;
}

button {
  cursor: pointer;
  color: var(--ink-secondary);
  border: 1px solid var(--line);
  background: var(--paper-elevated);
  border-radius: 6px;
  padding: 6px 12px;
  transition: all 0.15s ease;
}

button:hover {
  background: var(--accent-soft);
  border-color: var(--accent-border);
  color: var(--accent);
}

button:focus-visible,
a:focus-visible,
input:focus-visible,
.table-scroll:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 2px;
}

.skip {
  position: fixed;
  top: -80px;
  left: 20px;
  z-index: 30;
  background: var(--paper-elevated);
  padding: 10px 14px;
  border-radius: 6px;
  border: 1px solid var(--line);
}

.skip:focus {
  top: 12px;
}

/* Sidebar Navigation */
.sidebar {
  position: fixed;
  inset: 0 auto 0 0;
  width: 288px;
  background: var(--rail);
  border-right: 1px solid var(--line);
  display: flex;
  flex-direction: column;
  z-index: 15;
}

.sidebar-header {
  padding: 24px 20px 14px;
  border-bottom: 1px solid var(--line);
}

.brand {
  text-decoration: none;
  color: var(--ink);
  display: flex;
  flex-direction: column;
}

.brand-code {
  font-family: Georgia, "Times New Roman", "Songti SC", "Noto Serif SC", serif;
  font-size: 34px;
  font-weight: 700;
  line-height: 1.05;
  letter-spacing: -1px;
}

.brand-dot {
  color: var(--accent);
}

.brand-sub {
  font-size: 10px;
  font-weight: 600;
  letter-spacing: 2px;
  color: var(--muted);
  margin-top: 6px;
}

.edition {
  font-size: 12px;
  margin: 12px 0 14px;
  color: var(--muted);
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.edition span {
  font-size: 10px;
  font-weight: 600;
  letter-spacing: 1.2px;
  padding: 2px 6px;
  border-radius: 4px;
  background: var(--accent-soft);
  color: var(--accent);
}

.search-trigger {
  width: 100%;
  display: flex;
  justify-content: space-between;
  align-items: center;
  background: var(--paper-elevated);
  font-size: 12.5px;
  color: var(--muted);
  min-height: 38px;
  padding: 6px 11px;
  border-radius: 6px;
  border: 1px solid var(--line);
}

.search-trigger kbd {
  font-family: inherit;
  font-size: 11px;
  padding: 1px 5px;
  border-radius: 4px;
  border: 1px solid var(--line);
  background: var(--rail);
  color: var(--muted);
}

#course-nav {
  overflow-y: auto;
  overscroll-behavior: contain;
  padding: 16px 12px 24px;
  scrollbar-width: thin;
  flex: 1;
}

.nav-group {
  margin-bottom: 20px;
}

.nav-label {
  font-size: 11px;
  font-weight: 650;
  letter-spacing: 1px;
  color: var(--muted);
  margin: 0 10px 6px;
  text-transform: uppercase;
}

.nav-item {
  display: flex;
  align-items: baseline;
  gap: 10px;
  padding: 7px 10px;
  margin: 2px 0;
  line-height: 1.45;
  text-decoration: none;
  color: var(--ink-secondary);
  font-size: 13px;
  border-radius: 6px;
  transition: background-color 0.12s ease, color 0.12s ease;
}

.nav-item .num {
  font-size: 11px;
  font-weight: 600;
  font-variant-numeric: tabular-nums;
  color: var(--muted);
  min-width: 20px;
}

.nav-item:hover {
  background: var(--rail-hover);
  color: var(--ink);
}

.nav-item.active {
  background: var(--accent-soft);
  color: var(--accent);
  font-weight: 600;
}

.nav-item.active .num {
  color: var(--accent);
}

.sidebar-foot {
  border-top: 1px solid var(--line);
  font-size: 11px;
  line-height: 1.6;
  padding: 12px 20px;
  color: var(--muted);
  display: flex;
  flex-direction: column;
  gap: 2px;
}

/* Main Workspace */
.workspace {
  margin-left: 288px;
  min-width: 0;
  min-height: 100vh;
  display: flex;
  flex-direction: column;
}

.topbar {
  height: 62px;
  position: sticky;
  top: 0;
  z-index: 10;
  background: color-mix(in srgb, var(--paper) 92%, transparent);
  backdrop-filter: blur(8px);
  border-bottom: 1px solid var(--line);
  padding: 0 44px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 16px;
}

.topbar-left {
  display: flex;
  align-items: center;
  gap: 12px;
  min-width: 0;
}

.breadcrumb {
  font-size: 13px;
  font-weight: 500;
  color: var(--muted);
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.reading-tools {
  display: flex;
  gap: 8px;
  flex-shrink: 0;
}

.reading-tools button {
  font-size: 12px;
  padding: 5px 11px;
  min-height: 32px;
}

.reading-progress {
  position: sticky;
  top: 62px;
  height: 2px;
  z-index: 11;
}

#progress-bar {
  height: 2px;
  background: var(--accent);
  width: 0;
  transition: width 0.08s linear;
}

.reading-layout {
  display: grid;
  grid-template-columns: minmax(0, 810px) 220px;
  gap: 56px;
  max-width: 1180px;
  width: 100%;
  margin: 0 auto;
  padding: 44px 44px 72px;
  flex: 1;
}

main {
  min-width: 0;
  outline: none;
}

/* Chapter Meta Header */
.chapter-meta {
  display: flex;
  flex-wrap: wrap;
  gap: 12px;
  align-items: center;
  font-size: 12px;
  color: var(--muted);
  margin-bottom: 18px;
}

.chapter-meta .badge {
  font-weight: 650;
  letter-spacing: 0.8px;
  color: var(--accent);
  background: var(--accent-soft);
  border: 1px solid var(--accent-border);
  padding: 2px 9px;
  border-radius: 4px;
  font-size: 11px;
}

.chapter-meta .unit-name {
  font-weight: 500;
  color: var(--ink-secondary);
}

.chapter-meta .read-time {
  color: var(--muted);
}

/* Home Hero Banner */
.eyebrow {
  font-size: 11.5px;
  font-weight: 650;
  letter-spacing: 2px;
  color: var(--accent);
  margin: 0 0 16px;
}

.home-intro {
  border-bottom: 1px solid var(--line);
  padding-bottom: 34px;
  margin-bottom: 38px;
}

.home-intro h1 {
  font-family: Georgia, "Songti SC", "Noto Serif SC", serif;
  font-size: clamp(32px, 3.6vw, 46px);
  letter-spacing: -0.8px;
  font-weight: 600;
  line-height: 1.32;
  margin: 0 0 18px;
}

.home-intro em {
  font-style: normal;
  color: var(--accent);
}

.hero-lead {
  color: var(--ink-secondary);
  font-size: 16px;
  line-height: 1.8;
  max-width: 680px;
  margin: 0;
}

.hero-bottom {
  display: flex;
  justify-content: space-between;
  align-items: center;
  flex-wrap: wrap;
  gap: 20px;
  margin-top: 28px;
}

.hero-stats {
  display: flex;
  gap: 22px;
  font-size: 13px;
  color: var(--muted);
}

.hero-stats strong {
  color: var(--ink);
  font-size: 15px;
}

.hero-actions {
  display: flex;
  gap: 12px;
  align-items: center;
}

.start-link {
  display: inline-flex;
  gap: 10px;
  align-items: center;
  color: #ffffff !important;
  background: var(--accent);
  padding: 10px 18px;
  border-radius: 6px;
  text-decoration: none;
  font-size: 13.5px;
  font-weight: 600;
  transition: background-color 0.15s ease;
}

.start-link:hover {
  background: var(--accent-hover);
}

.secondary-link {
  display: inline-flex;
  align-items: center;
  padding: 9px 16px;
  border-radius: 6px;
  border: 1px solid var(--line-strong);
  background: var(--paper-elevated);
  color: var(--ink) !important;
  text-decoration: none;
  font-size: 13.5px;
  font-weight: 500;
}

.secondary-link:hover {
  border-color: var(--accent);
  color: var(--accent) !important;
}

/* Article Typography */
article {
  font-size: var(--body-size);
  line-height: 1.88;
  color: var(--ink);
  overflow-wrap: anywhere;
}

article h1 {
  font-family: Georgia, "Songti SC", "Noto Serif SC", serif;
  font-size: 34px;
  font-weight: 650;
  line-height: 1.38;
  margin: 0 0 26px;
  letter-spacing: -0.5px;
}

body[data-doc="overview"] article > h1 {
  display: none;
}

article h2 {
  font-size: 23px;
  font-weight: 650;
  line-height: 1.45;
  margin: 50px 0 20px;
  border-top: 1px solid var(--line);
  padding-top: 26px;
  color: var(--ink);
}

article h3 {
  font-size: 19.5px;
  font-weight: 650;
  line-height: 1.5;
  margin: 38px 0 14px;
  color: var(--accent);
}

article h4 {
  font-size: 17px;
  line-height: 1.55;
  margin: 26px 0 10px;
  font-weight: 650;
  color: var(--ink);
}

article p {
  margin: 0 0 18px;
}

article ul,
article ol {
  padding-left: 1.55em;
  margin: 14px 0 22px;
}

article li {
  padding-left: 4px;
  margin: 8px 0;
}

article li > p {
  margin: 0 0 8px;
}

article strong {
  font-weight: 650;
  color: var(--ink);
}

article hr {
  border: 0;
  border-top: 1px solid var(--line);
  margin: 40px 0;
}

article blockquote {
  margin: 22px 0;
  padding: 16px 20px;
  background: var(--callout-bg);
  border-left: 3px solid var(--accent);
  border-radius: 0 6px 6px 0;
  font-size: 0.95em;
  line-height: 1.8;
  color: var(--ink-secondary);
}

article blockquote p:last-child {
  margin: 0;
}

/* Inline & Block Code */
article code {
  font-family: "JetBrains Mono", "SFMono-Regular", Menlo, Monaco, Consolas, monospace;
  font-size: 0.86em;
  background: var(--inline-code-bg);
  padding: 2px 6px;
  border-radius: 4px;
  color: var(--inline-code-ink);
}

.code-block {
  margin: 22px 0;
  background: var(--code-bg);
  color: var(--code-ink);
  border-radius: 8px;
  border: 1px solid var(--line);
  overflow: hidden;
}

.code-header {
  padding: 7px 16px;
  background: var(--code-header);
  border-bottom: 1px solid rgba(255, 255, 255, 0.08);
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.code-lang {
  font-family: "JetBrains Mono", "SFMono-Regular", Consolas, monospace;
  font-size: 11px;
  font-weight: 600;
  letter-spacing: 0.8px;
  color: #a8b3ad;
}

.copy-btn {
  font-size: 11px;
  padding: 2px 8px;
  border-radius: 4px;
  background: rgba(255, 255, 255, 0.06);
  border: 1px solid rgba(255, 255, 255, 0.14);
  color: #cfd6d2;
}

.copy-btn:hover {
  background: rgba(255, 255, 255, 0.15);
  border-color: rgba(255, 255, 255, 0.28);
  color: #ffffff;
}

article pre {
  margin: 0;
  overflow-x: auto;
  padding: 16px 20px;
  line-height: 1.65;
  font-size: 13.5px;
}

article pre code {
  background: none;
  color: inherit;
  padding: 0;
  border-radius: 0;
  font-size: inherit;
}

/* Mathematical Expressions */
.math-inline {
  font-family: "Cambria Math", "Latin Modern Math", "STIX Two Math", Georgia, serif;
  font-size: 1.02em;
  padding: 0 2px;
  white-space: nowrap;
}

.math-block {
  font-family: "Cambria Math", "Latin Modern Math", "STIX Two Math", Georgia, serif;
  font-size: 1.08em;
  margin: 22px 0;
  padding: 16px 22px;
  background: var(--callout-bg);
  border: 1px solid var(--line);
  border-radius: 8px;
  text-align: center;
  overflow-x: auto;
}

.m-text,
.m-fn {
  font-family: -apple-system, BlinkMacSystemFont, "SF Pro Text", "PingFang SC", sans-serif;
  font-style: normal;
}

.m-fn {
  font-weight: 500;
  margin-right: 1px;
}

.m-cal {
  font-family: "Apple Chancery", "URW Chancery L", Georgia, serif;
  font-style: italic;
}

.m-op {
  font-size: 1.2em;
  vertical-align: -1px;
}

.m-frac {
  display: inline-flex;
  flex-direction: column;
  vertical-align: middle;
  text-align: center;
  padding: 0 3px;
  font-size: 0.92em;
  line-height: 1.25;
}

.m-num {
  border-bottom: 1px solid currentColor;
  padding: 0 3px 2px;
}

.m-den {
  padding: 2px 3px 0;
}

.m-binom .m-num {
  border-bottom: none;
}

.m-sqrt {
  border-top: 1px solid currentColor;
  padding-top: 1px;
}

/* Academic Tables */
.table-scroll {
  overflow-x: auto;
  margin: 24px 0;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--paper-elevated);
  scrollbar-width: thin;
}

table {
  border-collapse: collapse;
  width: 100%;
  font-size: 13.5px;
  line-height: 1.65;
}

th {
  text-align: left;
  background: var(--table-head);
  color: var(--ink);
  font-weight: 650;
  border-bottom: 1px solid var(--line-strong);
}

th,
td {
  padding: 12px 15px;
  border-bottom: 1px solid var(--line);
  vertical-align: top;
  min-width: 68px;
}

tbody tr:last-child td {
  border-bottom: none;
}

tbody tr:hover {
  background: var(--table-hover);
}

table a {
  text-decoration: none;
  font-weight: 500;
}

table a:hover {
  text-decoration: underline;
}

/* Right Table of Contents */
.toc {
  position: sticky;
  top: 98px;
  align-self: start;
  max-height: calc(100vh - 124px);
  overflow-y: auto;
  scrollbar-width: thin;
}

.toc-card {
  border-left: 1px solid var(--line);
  padding-left: 18px;
}

.toc-label {
  font-size: 11px;
  font-weight: 650;
  letter-spacing: 1.5px;
  color: var(--muted);
  margin-bottom: 12px;
  text-transform: uppercase;
}

.toc a {
  display: block;
  text-decoration: none;
  font-size: 12px;
  line-height: 1.55;
  padding: 5px 0;
  color: var(--muted);
  transition: color 0.12s ease;
}

.toc a.depth-3 {
  padding-left: 12px;
  font-size: 11.5px;
}

.toc a:hover,
.toc a.current {
  color: var(--accent);
}

.toc a.current {
  font-weight: 600;
}

.toc .source-link {
  border-top: 1px solid var(--line);
  margin-top: 18px;
  padding-top: 12px;
  font-size: 11px;
  color: var(--muted);
}

/* Prev / Next Chapter Footer */
#chapter-footer {
  margin-top: 56px;
  border-top: 1px solid var(--line);
  padding-top: 26px;
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 20px;
}

#chapter-footer a {
  display: block;
  padding: 14px 18px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--paper-elevated);
  font-size: 14px;
  font-weight: 600;
  text-decoration: none;
  transition: border-color 0.15s ease, background-color 0.15s ease;
}

#chapter-footer a:hover {
  border-color: var(--accent);
  background: var(--accent-soft);
}

#chapter-footer small {
  display: block;
  color: var(--muted);
  font-size: 11px;
  font-weight: 500;
  margin-bottom: 4px;
}

#chapter-footer a:last-child {
  text-align: right;
}

.page-footer {
  padding: 22px 44px;
  border-top: 1px solid var(--line);
  font-size: 11.5px;
  color: var(--muted);
  display: flex;
  justify-content: space-between;
  gap: 20px;
}

.mobile-only {
  display: none;
}

.shade {
  display: none;
}

[hidden] {
  display: none !important;
}

/* Search Modal */
dialog {
  padding: 0;
  border: 1px solid var(--line-strong);
  border-radius: 10px;
  background: var(--paper-elevated);
  color: var(--ink);
  width: min(720px, calc(100vw - 28px));
  max-height: 80vh;
  box-shadow: 0 24px 80px rgba(0, 0, 0, 0.25);
}

dialog::backdrop {
  background: rgba(18, 22, 20, 0.55);
  backdrop-filter: blur(3px);
}

.search-head {
  position: sticky;
  top: 0;
  display: flex;
  gap: 14px;
  align-items: center;
  padding: 16px 20px;
  background: var(--paper-elevated);
  border-bottom: 1px solid var(--line);
}

.search-head label {
  font-size: 12px;
  font-weight: 600;
  color: var(--accent);
}

.search-head input {
  min-width: 0;
  flex: 1;
  border: 0;
  background: transparent;
  font-size: 15px;
  color: var(--ink);
  padding: 6px 0;
}

.search-head input:focus {
  outline: none;
}

.search-head button {
  font-size: 11px;
  padding: 4px 8px;
}

.search-hint {
  font-size: 11.5px;
  color: var(--muted);
  padding: 4px 20px 8px;
  margin: 8px 0 0;
}

#search-results {
  padding: 0 12px 16px;
}

.search-result {
  display: block;
  text-decoration: none;
  color: var(--ink);
  border-radius: 6px;
  padding: 12px 12px;
  border-bottom: 1px solid var(--line);
}

.search-result:hover {
  background: var(--rail);
}

.search-result strong {
  display: block;
  font-size: 14px;
  margin-bottom: 4px;
  color: var(--accent);
}

.search-result span {
  font-size: 12.5px;
  color: var(--muted);
  line-height: 1.65;
  display: block;
}

.search-result mark {
  background: var(--mark-bg);
  color: inherit;
  padding: 0 2px;
  border-radius: 2px;
}

/* Responsive Breakpoints */
@media (max-width: 1220px) {
  .sidebar {
    width: 256px;
  }
  .workspace {
    margin-left: 256px;
  }
  .reading-layout {
    gap: 32px;
    padding: 36px 32px;
    grid-template-columns: minmax(0, 1fr) 175px;
  }
  .topbar {
    padding: 0 32px;
  }
}

@media (max-width: 1020px) {
  .toc {
    display: none;
  }
  .reading-layout {
    display: block;
    max-width: 820px;
  }
}

@media (max-width: 760px) {
  .sidebar {
    transform: translateX(-100%);
    width: 284px;
    transition: transform 0.2s ease;
  }
  .workspace {
    margin-left: 0;
  }
  .mobile-only {
    display: inline-flex;
  }
  .topbar {
    height: 56px;
    padding: 0 16px;
  }
  .reading-progress {
    top: 56px;
  }
  .reading-layout {
    padding: 26px 18px 44px;
  }
  .home-intro h1 {
    font-size: 30px;
  }
  article h1 {
    font-size: 27px;
  }
  article h2 {
    font-size: 21px;
    margin-top: 38px;
  }
  article h3 {
    font-size: 18px;
  }
  #chapter-footer {
    grid-template-columns: 1fr;
  }
  #chapter-footer a:last-child {
    text-align: left;
  }
  .page-footer {
    padding: 18px;
    flex-direction: column;
    gap: 6px;
  }
  .nav-open .sidebar {
    transform: translateX(0);
  }
  .nav-open .shade {
    display: block;
    position: fixed;
    inset: 0;
    background: rgba(0, 0, 0, 0.45);
    z-index: 14;
  }
}

@media print {
  .sidebar,
  .topbar,
  .toc,
  .reading-progress,
  .page-footer,
  #chapter-footer,
  .home-intro,
  .chapter-meta,
  dialog,
  .skip,
  .copy-btn {
    display: none !important;
  }
  .workspace {
    margin: 0;
  }
  .reading-layout {
    display: block;
    max-width: none;
    margin: 0;
    padding: 0;
  }
  body {
    background: white !important;
    color: #111 !important;
  }
  article {
    font-size: 11pt;
    line-height: 1.65;
  }
  article h1 {
    display: block !important;
    font-size: 22pt;
  }
  article h2 {
    break-after: avoid;
    font-size: 16pt;
  }
  article h3,
  article h4 {
    break-after: avoid;
  }
  article pre {
    white-space: pre-wrap;
    overflow-wrap: anywhere;
  }
  .table-scroll {
    overflow: visible;
  }
  .code-block {
    background: #f5f5f5 !important;
    color: #111 !important;
  }
  @page {
    margin: 18mm;
  }
}
