/* ===== CSS Variables ===== */
:root {
  --bg: #faf8f5;
  --bg2: #f0ece6;
  --ink: #2c2420;
  --muted: #7a6e66;
  --rule: #d4ccc4;
  --accent: #c45c3e;
  --accent2: #2e7d6d;
  --accent-light: #f5e6e0;
  --accent2-light: #e0f0ec;
}

/* ===== Fonts ===== */
@font-face { font-family: 'Crimson'; src: url('../_shared/fonts/CrimsonPro-Regular.ttf') format('truetype'); font-weight: 400; }
@font-face { font-family: 'Crimson'; src: url('../_shared/fonts/CrimsonPro-Bold.ttf') format('truetype'); font-weight: 700; }
@font-face { font-family: 'WorkSans'; src: url('../_shared/fonts/WorkSans-Regular.ttf') format('truetype'); font-weight: 400; }
@font-face { font-family: 'WorkSans'; src: url('../_shared/fonts/WorkSans-Bold.ttf') format('truetype'); font-weight: 700; }
@font-face { font-family: 'JetBrains'; src: url('../_shared/fonts/JetBrainsMono-Regular.ttf') format('truetype'); font-weight: 400; }
@font-face { font-family: 'JetBrains'; src: url('../_shared/fonts/JetBrainsMono-Bold.ttf') format('truetype'); font-weight: 700; }

/* ===== Base ===== */
* { box-sizing: border-box; margin: 0; padding: 0; }
html { scroll-behavior: smooth; }
body {
  font-family: 'WorkSans', 'Noto Sans CJK SC', sans-serif;
  font-size: 16px;
  line-height: 1.8;
  color: var(--ink);
  background: var(--bg);
}

/* ===== Layout ===== */
.container { max-width: 900px; margin: 0 auto; padding: 0 24px; }

/* ===== Header ===== */
.hero {
  background: linear-gradient(135deg, var(--ink) 0%, #4a3f38 100%);
  color: #fff;
  padding: 60px 24px 40px;
  text-align: center;
  position: relative;
  overflow: hidden;
}
.hero::before {
  content: '';
  position: absolute;
  top: -50%; left: -50%;
  width: 200%; height: 200%;
  background: radial-gradient(circle at 30% 70%, rgba(196,92,62,0.15) 0%, transparent 50%),
              radial-gradient(circle at 70% 30%, rgba(46,125,109,0.12) 0%, transparent 50%);
  pointer-events: none;
}
.hero-content { position: relative; z-index: 1; }
.hero h1 { font-family: 'Crimson', serif; font-size: 2.4rem; font-weight: 700; margin-bottom: 0.3rem; }
.hero .chapter-label { font-size: 0.95rem; color: rgba(255,255,255,0.6); margin-bottom: 0.5rem; }
.hero .subtitle { font-size: 1.05rem; color: rgba(255,255,255,0.75); }

/* ===== Navigation ===== */
.nav-bar {
  background: var(--bg2);
  border-bottom: 1px solid var(--rule);
  padding: 10px 0;
  position: sticky;
  top: 0;
  z-index: 100;
}
.nav-bar .container { display: flex; align-items: center; justify-content: space-between; flex-wrap: wrap; gap: 8px; }
.nav-brand { font-family: 'Crimson', serif; font-weight: 700; font-size: 1.05rem; color: var(--ink); }
.nav-links { display: flex; gap: 14px; list-style: none; flex-wrap: wrap; }
.nav-links a { color: var(--muted); text-decoration: none; font-size: 0.85rem; transition: color 0.2s; }
.nav-links a:hover { color: var(--accent); }
.nav-links a.active { color: var(--accent); font-weight: 700; }

/* ===== Section Styling ===== */
.section { padding: 50px 0; border-bottom: 1px solid var(--rule); }
.section:last-child { border-bottom: none; }

.section-header { display: flex; align-items: baseline; gap: 16px; margin-bottom: 28px; }
.section-number { font-family: 'Crimson', serif; font-size: 2.5rem; font-weight: 700; color: var(--accent); line-height: 1; opacity: 0.35; }
.section-title { font-family: 'Crimson', serif; font-size: 1.7rem; font-weight: 700; color: var(--ink); }

h3 { font-family: 'Crimson', serif; font-size: 1.3rem; font-weight: 700; margin: 28px 0 14px; color: var(--ink); }
h4 { font-size: 1.05rem; font-weight: 700; margin: 20px 0 10px; color: var(--ink); }
p { margin-bottom: 0.9rem; }

/* ===== Callouts ===== */
.callout {
  background: var(--bg2);
  border-left: 4px solid var(--accent);
  padding: 18px 22px;
  margin: 22px 0;
  border-radius: 0 8px 8px 0;
}
.callout-title { font-weight: 700; color: var(--accent); margin-bottom: 6px; font-size: 0.95rem; }
.callout.accent2 { border-left-color: var(--accent2); }
.callout.accent2 .callout-title { color: var(--accent2); }

/* ===== Insight Box ===== */
.insight { background: var(--accent-light); padding: 18px 22px; margin: 22px 0; border-radius: 8px; }
.insight-title { font-weight: 700; color: var(--accent); margin-bottom: 6px; }

/* ===== MATLAB Box ===== */
.matlab-box { background: #1e1e1e; color: #d4d4d4; border-radius: 8px; margin: 22px 0; overflow: hidden; }
.matlab-header { background: #2d2d2d; padding: 10px 16px; font-size: 0.85rem; color: #9cdcfe; display: flex; align-items: center; gap: 8px; }
.matlab-header::before { content: ''; display: inline-block; width: 12px; height: 12px; background: #c45c3e; border-radius: 50%; }
.matlab-body { padding: 16px 20px; font-family: 'JetBrains', monospace; font-size: 0.82rem; line-height: 1.7; white-space: pre-wrap; overflow-x: auto; }
.matlab-comment { color: #6a9955; }
.matlab-keyword { color: #569cd6; }
.matlab-string { color: #ce9178; }
.matlab-number { color: #b5cea8; }

/* ===== Example Box ===== */
.example-box { background: var(--bg2); border: 1px solid var(--rule); border-radius: 10px; margin: 24px 0; overflow: hidden; }
.example-header { background: var(--accent); color: #fff; padding: 10px 20px; font-weight: 700; font-size: 0.95rem; }
.example-header.accent2 { background: var(--accent2); }
.example-body { padding: 20px 24px; }
.example-body p { margin-bottom: 0.6rem; }
.example-step { margin: 12px 0; padding-left: 20px; border-left: 2px solid var(--rule); }
.example-step-label { font-weight: 700; color: var(--accent); font-size: 0.9rem; }
.example-result { background: var(--accent-light); padding: 12px 18px; border-radius: 6px; margin-top: 12px; font-weight: 700; }

/* ===== Figure & Chart ===== */
figure { margin: 28px 0; }
figure img { max-width: 100%; height: auto; border-radius: 8px; }
figcaption { font-size: 0.85rem; color: var(--muted); text-align: center; margin-top: 8px; }
.chart-figure { margin: 28px 0; }
.chart-figure figcaption { font-size: 0.95rem; font-weight: 600; color: var(--ink); margin-bottom: 12px; text-align: left; }

/* ===== Tables ===== */
.table-wrap { overflow-x: auto; margin: 22px 0; }
table { width: 100%; border-collapse: collapse; font-size: 0.9rem; }
th, td { padding: 11px 14px; text-align: left; border-bottom: 1px solid var(--rule); }
th { background: var(--bg2); font-weight: 700; color: var(--ink); }
tr:hover { background: rgba(196,92,62,0.03); }

/* ===== Formula ===== */
.formula { background: var(--bg2); padding: 14px 22px; margin: 18px 0; border-radius: 8px; text-align: center; font-family: 'Crimson', serif; font-size: 1.15rem; color: var(--ink); overflow-x: auto; }

/* ===== Lists ===== */
ul, ol { margin: 10px 0 10px 24px; }
li { margin-bottom: 4px; }

/* ===== Chapter Nav (prev/next) ===== */
.chapter-nav { display: flex; justify-content: space-between; margin-top: 40px; padding-top: 20px; border-top: 1px solid var(--rule); }
.chapter-nav a {
  display: inline-flex; align-items: center; gap: 8px;
  color: var(--accent); text-decoration: none; font-weight: 700; font-size: 0.95rem;
  padding: 10px 18px; border: 1px solid var(--accent); border-radius: 8px;
  transition: all 0.2s;
}
.chapter-nav a:hover { background: var(--accent); color: #fff; }

/* ===== Footer ===== */
footer { background: var(--ink); color: rgba(255,255,255,0.6); padding: 30px 24px; margin-top: 40px; font-size: 0.85rem; }
footer a { color: var(--accent); text-decoration: none; }

/* ===== Scroll to top ===== */
#toTop {
  position: fixed; bottom: 24px; right: 24px;
  background: var(--accent); color: #fff;
  width: 44px; height: 44px; border-radius: 50%;
  border: none; cursor: pointer; font-size: 1.2rem;
  display: none; align-items: center; justify-content: center;
  z-index: 200; box-shadow: 0 2px 8px rgba(0,0,0,0.2);
}
#toTop:hover { background: #a84d32; }

/* ===== Responsive ===== */
@media (max-width: 768px) {
  .hero h1 { font-size: 1.8rem; }
  .section-number { font-size: 2rem; }
  .section-title { font-size: 1.4rem; }
  .nav-links { display: none; }
  .container { padding: 0 16px; }
  .chapter-nav { flex-direction: column; gap: 10px; }
}

/* ===== Print ===== */
@media print {
  .nav-bar { display: none; }
  .hero { background: #fff; color: #000; padding: 20px 0; }
  .section { break-inside: avoid; }
  #toTop { display: none !important; }
}

/* ===== Mermaid ===== */
.mermaid { background: transparent; }
