@import url("https://fonts.googleapis.com/css2?family=EB+Garamond:ital,wght@0,400;0,500;0,600;0,700;1,400;1,500&family=Crimson+Pro:ital,wght@0,300;0,400;0,500;0,600;0,700;1,400&display=swap");

:root {
  --bg: #e8dcc8;
  --bg-ink: #1a1610;
  --panel: #f5eed8;
  --panel-2: #ebe2ca;
  --text: #2b2218;
  --muted: #6b5d4d;
  --line: #c4b08a;
  --accent: #8b3a2a;
  --accent-deep: #5c2316;
  --accent-soft: #f0ddd0;
  --gold: #a6882b;
  --gold-light: #d4b94e;
  --gold-soft: #f5ebc8;
  --olive: #4a5c32;
  --code-bg: #1e1a14;
  --code-fg: #e6dcc8;
  --shadow: 0 12px 32px rgba(26, 22, 16, 0.18);
  --sidebar-w: 290px;
  --toc-w: 280px;
  --panel-transition: 280ms ease;
  --heading-font: "EB Garamond", "Palatino Linotype", "Palatino", "Georgia", serif;
  --body-font: "Crimson Pro", "Palatino", "Georgia", serif;
}
* { box-sizing: border-box; }
html, body { height: 100%; }
body {
  margin: 0;
  font-family: var(--body-font);
  color: var(--text);
  background:
    radial-gradient(ellipse 900px 500px at 10% 0%, rgba(166, 136, 43, 0.1) 0%, transparent 60%),
    radial-gradient(ellipse 700px 400px at 95% 5%, rgba(139, 58, 42, 0.07) 0%, transparent 50%),
    radial-gradient(ellipse 1200px 600px at 50% 100%, rgba(74, 92, 50, 0.05) 0%, transparent 50%),
    var(--bg);
  line-height: 1.72;
  letter-spacing: 0.005em;
  font-size: 16.5px;
}

/* ── Parchment grain texture ── */
body::after {
  content: '';
  position: fixed;
  inset: 0;
  pointer-events: none;
  z-index: 9999;
  opacity: 0.045;
  background-image: url("data:image/svg+xml,%3Csvg viewBox='0 0 256 256' xmlns='http://www.w3.org/2000/svg'%3E%3Cfilter id='n'%3E%3CfeTurbulence type='fractalNoise' baseFrequency='0.7' numOctaves='5' stitchTiles='stitch'/%3E%3C/filter%3E%3Crect width='100%25' height='100%25' filter='url(%23n)'/%3E%3C/svg%3E");
  background-repeat: repeat;
  background-size: 256px 256px;
}

/* ── Custom scrollbars ── */
.sidebar, .toc-panel, .doc-list, .search-results {
  scrollbar-width: thin;
  scrollbar-color: var(--line) transparent;
}
.sidebar::-webkit-scrollbar, .toc-panel::-webkit-scrollbar,
.doc-list::-webkit-scrollbar, .search-results::-webkit-scrollbar {
  width: 5px;
}
.sidebar::-webkit-scrollbar-thumb, .toc-panel::-webkit-scrollbar-thumb,
.doc-list::-webkit-scrollbar-thumb, .search-results::-webkit-scrollbar-thumb {
  background: var(--line);
  border-radius: 3px;
}
.sidebar::-webkit-scrollbar-track, .toc-panel::-webkit-scrollbar-track,
.doc-list::-webkit-scrollbar-track, .search-results::-webkit-scrollbar-track {
  background: transparent;
}

/* ── Layout grid ── */
.app {
  display: grid;
  grid-template-columns: var(--sidebar-w) minmax(0, 1fr) var(--toc-w);
  min-height: 100vh;
  gap: 18px;
  padding: 18px;
  transition: grid-template-columns var(--panel-transition);
}
.app.no-toc {
  grid-template-columns: var(--sidebar-w) minmax(0, 1fr);
}

/* ── Sidebar ── */
.sidebar {
  position: sticky;
  top: 18px;
  align-self: start;
  height: calc(100vh - 36px);
  background: linear-gradient(180deg, var(--panel), #ede4cc);
  border: 1px solid var(--line);
  border-radius: 14px;
  box-shadow: var(--shadow);
  display: flex;
  flex-direction: column;
  overflow: hidden;
  min-width: 0;
  transition: opacity 200ms ease, visibility 200ms ease;
}
.brand {
  padding: 20px 16px 14px;
  border-bottom: 1px solid var(--line);
  background: linear-gradient(160deg, #f5ebc8, var(--panel) 70%);
  text-align: center;
}
.brand h1 {
  margin: 0;
  font-size: 1.05rem;
  font-family: var(--heading-font);
  letter-spacing: 0.14em;
  text-transform: uppercase;
  color: var(--accent-deep);
  font-weight: 600;
}
.brand p { margin: 6px 0 0; color: var(--muted); font-size: 0.78rem; font-style: italic; }
/* Decorative rule under brand */
.brand::after {
  content: '';
  display: block;
  width: 60px;
  height: 1px;
  background: linear-gradient(90deg, transparent, var(--gold), transparent);
  margin: 12px auto 0;
}
.search-wrap { padding: 12px 12px 8px; border-bottom: 1px solid var(--line); position: relative; background: rgba(245,238,216,0.5); }
.search-wrap input {
  width: 100%;
  padding: 9px 12px;
  border: 1px solid var(--line);
  border-radius: 8px;
  font: inherit;
  font-size: 0.9rem;
  outline: none;
  background: rgba(255,252,240,0.8);
  color: var(--text);
}
.search-wrap input::placeholder { color: var(--muted); font-style: italic; }
.search-wrap input:focus { border-color: var(--gold); box-shadow: 0 0 0 2px rgba(166, 136, 43, 0.18); }
.search-results { margin-top: 8px; border: 1px solid var(--line); border-radius: 8px; background: var(--panel); max-height: 220px; overflow: auto; display: none; }
.search-results.show { display: block; }
.search-item { display: block; padding: 8px 10px; text-decoration: none; color: inherit; border-bottom: 1px solid #ddd0b4; }
.search-item:last-child { border-bottom: 0; }
.search-item:hover, .search-item.active { background: var(--gold-soft); }
.search-item .t { font-weight: 600; font-size: 0.9rem; font-family: var(--heading-font); }
.search-item .e { display: block; color: var(--muted); font-size: 0.78rem; margin-top: 2px; }
.doc-list { padding: 8px; overflow: auto; display: grid; gap: 4px; }
.doc-link {
  display: block;
  text-decoration: none;
  color: inherit;
  padding: 10px 11px;
  border: 1px solid transparent;
  border-radius: 8px;
  transition: 150ms ease;
  background: transparent;
}
.doc-link:hover { border-color: #c9b88a; background: rgba(245,235,200,0.5); }
.doc-link.active { border-color: var(--gold); background: var(--gold-soft); box-shadow: inset 2px 0 0 var(--gold); }
.doc-title { display: block; font-weight: 600; font-size: 0.92rem; font-family: var(--heading-font); }
.doc-file { display: block; font-size: 0.75rem; color: var(--muted); margin-top: 2px; font-style: italic; }

/* ── Content area ── */
.content-wrap {
  background: linear-gradient(180deg, #faf5e4, var(--panel));
  border: 1px solid var(--line);
  border-radius: 14px;
  box-shadow: var(--shadow);
  overflow: hidden;
  min-width: 0;
}
.topbar {
  padding: 10px 20px;
  border-bottom: 1px solid var(--line);
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 12px;
  background: linear-gradient(160deg, #f0e6cc, var(--panel) 70%);
}
.crumb { color: var(--muted); font-size: 0.85rem; font-style: italic; }
.crumb a { color: var(--accent); text-decoration: none; font-weight: 500; }
.crumb a:hover { text-decoration: underline; }
.crumb span { margin: 0 6px; color: var(--line); }
.top-actions { display: flex; align-items: center; gap: 8px; }
.home-link { color: var(--accent); text-decoration: none; font-weight: 600; font-family: var(--heading-font); font-size: 0.9rem; }
.home-link:hover { text-decoration: underline; }
.toggle-btn {
  border: 1px solid var(--line);
  background: rgba(245,235,200,0.6);
  color: var(--accent-deep);
  border-radius: 6px;
  font-size: 0.75rem;
  padding: 4px 11px;
  cursor: pointer;
  font-weight: 600;
  font-family: var(--body-font);
  letter-spacing: 0.02em;
}
.toggle-btn:hover { background: var(--gold-soft); border-color: var(--gold); }
.toggle-btn:focus-visible { outline: 2px solid var(--gold); outline-offset: 2px; }

/* ── Article ── */
article { padding: 36px 44px; }
article > :first-child { margin-top: 0; }
h1, h2, h3 {
  line-height: 1.2;
  scroll-margin-top: 16px;
  font-family: var(--heading-font);
  color: var(--accent-deep);
}
h1 { font-size: 2.3rem; font-weight: 500; letter-spacing: -0.01em; }
h2 {
  font-size: 1.6rem;
  margin-top: 2.4rem;
  font-weight: 500;
  padding-bottom: 6px;
  border-bottom: none;
  position: relative;
}
/* Renaissance ornamental rule under h2 */
h2::after {
  content: '';
  display: block;
  width: 100%;
  height: 1px;
  margin-top: 6px;
  background: linear-gradient(90deg, var(--gold), var(--line) 50%, transparent);
}
h3 { font-size: 1.25rem; margin-top: 1.8rem; font-weight: 600; color: var(--text); }
.heading-wrap { display: inline-flex; align-items: center; gap: 8px; flex-wrap: wrap; }
.anchor-copy {
  border: 1px solid var(--line);
  background: rgba(245,235,200,0.6);
  color: var(--muted);
  border-radius: 4px;
  font-size: 0.68rem;
  padding: 2px 8px;
  cursor: pointer;
  font-weight: 600;
  font-family: var(--body-font);
  opacity: 0;
  transition: opacity 150ms ease, background 150ms ease;
}
.heading-wrap:hover .anchor-copy,
.anchor-copy:focus-visible,
.anchor-copy.done { opacity: 1; }
.anchor-copy:hover { background: var(--gold-soft); border-color: var(--gold); }
.anchor-copy.done { border-color: var(--olive); color: var(--olive); background: #ecf2e0; }

/* ── Drop cap on first paragraph ── */
article > p:first-of-type::first-letter {
  float: left;
  font-family: var(--heading-font);
  font-size: 3.2em;
  line-height: 0.8;
  padding-right: 8px;
  padding-top: 4px;
  color: var(--accent-deep);
  font-weight: 500;
}

a { color: var(--accent); text-underline-offset: 3px; }
a:hover { color: var(--accent-deep); }
/* Table wrapper for horizontal scroll on mobile */
article .table-wrap { overflow-x: auto; margin: 20px 0; border-radius: 8px; -webkit-overflow-scrolling: touch; }
table { border-collapse: collapse; width: 100%; font-size: 0.92rem; background: rgba(255,252,240,0.7); border-radius: 8px; overflow: hidden; margin: 0; }
article > table { margin: 20px 0; }
th, td { border: 1px solid #d4c49a; padding: 9px 12px; text-align: left; vertical-align: top; }
th { background: linear-gradient(180deg, #ede3c4, #e6d9b8); color: var(--accent-deep); font-weight: 600; font-family: var(--heading-font); font-size: 0.95rem; letter-spacing: 0.02em; }
code { background: #ede3c4; color: var(--accent-deep); padding: 2px 6px; border-radius: 4px; font-size: 0.88em; }
pre { background: var(--code-bg); color: var(--code-fg); padding: 16px; border-radius: 8px; overflow-x: auto; border: 1px solid #3a332a; }
pre code { background: transparent; padding: 0; color: inherit; }
blockquote {
  margin: 20px 0;
  padding: 14px 18px;
  border-left: 3px solid var(--gold);
  color: #4a3c2c;
  background: linear-gradient(135deg, rgba(245,235,200,0.5), rgba(240,221,208,0.3));
  border-radius: 0 8px 8px 0;
  font-style: italic;
}
blockquote p { margin: 0 0 8px; }
blockquote p:last-child { margin-bottom: 0; }
hr {
  border: 0;
  height: 1px;
  margin: 32px 0;
  background: linear-gradient(90deg, transparent, var(--gold), var(--line), transparent);
}
.meta { color: var(--muted); font-size: 0.82rem; margin-top: 28px; font-style: italic; }

/* ── TOC panel ── */
.toc-panel {
  position: sticky;
  top: 18px;
  align-self: start;
  max-height: calc(100vh - 36px);
  overflow: auto;
  background: linear-gradient(180deg, var(--panel), #ede4cc);
  border: 1px solid var(--line);
  border-radius: 14px;
  box-shadow: var(--shadow);
  padding: 16px 14px 18px;
  min-width: 0;
  transition: opacity 200ms ease, visibility 200ms ease;
}
.toc-panel h3 {
  margin: 2px 2px 8px;
  font-size: 0.85rem;
  font-family: var(--heading-font);
  text-transform: uppercase;
  letter-spacing: 0.12em;
  color: var(--gold);
  font-weight: 600;
}
/* Ornamental rule under TOC heading */
.toc-panel h3::after {
  content: '';
  display: block;
  width: 40px;
  height: 1px;
  background: linear-gradient(90deg, var(--gold), transparent);
  margin-top: 6px;
}
.toc-panel .toc ul { list-style: none; padding-left: 12px; margin: 6px 0; }
.toc-panel .toc > ul { padding-left: 0; }
.toc-panel .toc li { margin: 3px 0; }
.toc-panel .toc a {
  text-decoration: none;
  color: var(--muted);
  font-size: 0.85rem;
  font-family: var(--body-font);
  padding: 2px 8px;
  border-radius: 4px;
  border-left: 2px solid transparent;
  transition: all 150ms ease;
  display: inline-block;
}
.toc-panel .toc a:hover { color: var(--accent); }
.toc-panel .toc a.toc-active {
  background: var(--gold-soft);
  color: var(--accent-deep);
  font-weight: 600;
  border-left-color: var(--gold);
}
.toc-empty { color: var(--muted); font-size: 0.85rem; margin: 0; font-style: italic; }

/* ── Dashboard ── */
.dashboard { display: grid; gap: 20px; }
.stats { display: grid; grid-template-columns: repeat(3, minmax(0, 1fr)); gap: 12px; }
.stat {
  border: 1px solid var(--line);
  border-radius: 10px;
  padding: 18px 16px;
  background: linear-gradient(180deg, var(--panel), #ede4cc);
  text-align: center;
}
.stat .n { font-family: var(--heading-font); font-weight: 500; font-size: 1.9rem; color: var(--accent-deep); }
.stat .l { color: var(--muted); font-size: 0.8rem; margin-top: 4px; font-style: italic; }
.cards { display: grid; grid-template-columns: repeat(auto-fill, minmax(250px, 1fr)); gap: 14px; }
.card {
  border: 1px solid var(--line);
  border-radius: 10px;
  padding: 16px;
  background: linear-gradient(180deg, var(--panel), #ede4cc);
  text-decoration: none;
  color: inherit;
  transform: translateY(0);
  transition: 180ms ease;
}
.card:hover { border-color: var(--gold); box-shadow: 0 8px 20px rgba(26, 22, 16, 0.12); transform: translateY(-2px); }
.card h3 { margin: 0; font-size: 1.12rem; font-family: var(--heading-font); font-weight: 500; color: var(--accent-deep); }
.card p { margin: 8px 0; color: var(--muted); font-size: 0.88rem; }
.card small { color: var(--muted); font-style: italic; font-size: 0.78rem; }

/* ── Overlay ── */
.overlay { display: none; position: fixed; inset: 0; background: rgba(26, 22, 16, 0.42); border: 0; z-index: 35; cursor: default; }

/* ── Desktop panel toggling (smooth, no layout jump) ── */
@media (min-width: 841px) {
  body.hide-sidebar .sidebar { opacity: 0; visibility: hidden; width: 0; min-width: 0; overflow: hidden; padding: 0; border: 0; }
  body.hide-sidebar .app { grid-template-columns: 0px minmax(0, 1fr) var(--toc-w); gap: 18px; }
  body.hide-toc .toc-panel { opacity: 0; visibility: hidden; width: 0; min-width: 0; overflow: hidden; padding: 0; border: 0; }
  body.hide-toc .app { grid-template-columns: var(--sidebar-w) minmax(0, 1fr) 0px; gap: 18px; }
  body.hide-sidebar.hide-toc .app { grid-template-columns: 0px minmax(0, 1fr) 0px; }
  body.hide-sidebar .app.no-toc { grid-template-columns: 0px minmax(0, 1fr); }
  .app.no-toc { grid-template-columns: var(--sidebar-w) minmax(0, 1fr); }
}

/* ── Tablet: TOC as drawer ── */
@media (max-width: 1180px) and (min-width: 841px) {
  .app { grid-template-columns: 260px minmax(0, 1fr); }
  .toc-panel {
    position: fixed;
    right: 10px;
    top: 10px;
    bottom: 10px;
    width: min(78vw, 300px);
    max-height: none;
    transform: translateX(115%);
    transition: transform 220ms ease;
    z-index: 40;
  }
  body.show-toc .toc-panel { transform: translateX(0); }
  body.show-toc .overlay { display: block; }
}

/* ── Animations (gentle fade, befitting the scholarly tone) ── */
@keyframes riseIn { from { opacity: 0; transform: translateY(6px); } to { opacity: 1; transform: translateY(0); } }
article > * { opacity: 0; animation: riseIn 500ms ease forwards; }
article > *:nth-child(1) { animation-delay: 60ms; }
article > *:nth-child(2) { animation-delay: 120ms; }
article > *:nth-child(3) { animation-delay: 180ms; }
article > *:nth-child(4) { animation-delay: 240ms; }
article > *:nth-child(5) { animation-delay: 300ms; }

/* ── Mobile ── */
@media (max-width: 840px) {
  .app { grid-template-columns: 1fr; padding: 10px; }
  .sidebar, .toc-panel {
    position: fixed;
    top: 10px;
    bottom: 10px;
    height: auto;
    max-height: none;
    z-index: 40;
    transition: transform 220ms ease;
    opacity: 1;
    visibility: visible;
  }
  .sidebar { left: 10px; width: min(86vw, 310px); transform: translateX(-115%); }
  .toc-panel { right: 10px; width: min(86vw, 310px); transform: translateX(115%); }
  body.show-sidebar .sidebar { transform: translateX(0); }
  body.show-toc .toc-panel { transform: translateX(0); }
  body.show-sidebar .overlay, body.show-toc .overlay { display: block; }
  article { padding: 24px 18px; }
  article > p:first-of-type::first-letter { font-size: 2.6em; }
  .stats { grid-template-columns: 1fr; }
  .home-link { display: none; }
  .toggle-btn { padding: 4px 10px; font-size: 0.7rem; }
  .topbar { padding: 10px 14px; gap: 8px; }
  .crumb { font-size: 0.8rem; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
  .anchor-copy { opacity: 1; }
  h1 { font-size: 1.75rem; }
  h2 { font-size: 1.3rem; }
  .brand::after { margin-top: 8px; }
}

/* ── Access gate (lead capture) ── */
.access-gate {
  position: fixed;
  inset: 0;
  z-index: 10000;
  display: flex;
  align-items: center;
  justify-content: center;
  background:
    radial-gradient(ellipse 600px 400px at 50% 40%, rgba(245,235,200,0.95), rgba(232,220,200,0.97)),
    var(--bg);
  backdrop-filter: blur(8px);
  transition: opacity 500ms ease, visibility 500ms ease;
}
.access-gate.hidden { opacity: 0; visibility: hidden; pointer-events: none; }
.gate-card {
  width: min(420px, 90vw);
  background: linear-gradient(180deg, var(--panel), #ede4cc);
  border: 1px solid var(--line);
  border-radius: 14px;
  box-shadow: 0 20px 50px rgba(26, 22, 16, 0.22);
  padding: 40px 36px 36px;
  text-align: center;
}
.gate-card h2 {
  font-family: var(--heading-font);
  font-weight: 500;
  font-size: 1.8rem;
  color: var(--accent-deep);
  margin: 0 0 6px;
  border: none;
}
.gate-card h2::after { display: none; }
.gate-card .gate-subtitle {
  color: var(--muted);
  font-size: 0.9rem;
  font-style: italic;
  margin: 0 0 8px;
}
.gate-card .gate-rule {
  width: 60px;
  height: 1px;
  background: linear-gradient(90deg, transparent, var(--gold), transparent);
  margin: 16px auto;
  border: none;
}
.gate-card p.gate-desc {
  color: var(--text);
  font-size: 0.92rem;
  line-height: 1.6;
  margin: 0 0 24px;
}
.gate-field {
  display: block;
  width: 100%;
  padding: 11px 14px;
  border: 1px solid var(--line);
  border-radius: 8px;
  font-family: var(--body-font);
  font-size: 0.95rem;
  color: var(--text);
  background: rgba(255,252,240,0.8);
  outline: none;
  margin-bottom: 12px;
}
.gate-field::placeholder { color: var(--muted); font-style: italic; }
.gate-field:focus { border-color: var(--gold); box-shadow: 0 0 0 2px rgba(166, 136, 43, 0.18); }
.gate-submit {
  display: block;
  width: 100%;
  padding: 12px;
  margin-top: 8px;
  border: 1px solid var(--accent-deep);
  border-radius: 8px;
  background: linear-gradient(180deg, var(--accent), var(--accent-deep));
  color: #f5eed8;
  font-family: var(--heading-font);
  font-size: 1.05rem;
  font-weight: 500;
  letter-spacing: 0.04em;
  cursor: pointer;
  transition: all 200ms ease;
}
.gate-submit:hover { background: linear-gradient(180deg, #9e4232, var(--accent-deep)); box-shadow: 0 4px 12px rgba(92, 35, 22, 0.25); }
.gate-submit:focus-visible { outline: 2px solid var(--gold); outline-offset: 2px; }
.gate-submit:disabled { opacity: 0.6; cursor: not-allowed; }
.gate-error { color: var(--accent); font-size: 0.82rem; margin: 8px 0 0; min-height: 1.2em; }
.gate-fine {
  color: var(--muted);
  font-size: 0.76rem;
  margin-top: 16px;
  font-style: italic;
}

/* ── Skip link (a11y) ── */
.skip-link {
  position: absolute;
  top: -40px;
  left: 0;
  background: var(--accent-deep);
  color: #f5eed8;
  padding: 8px 16px;
  z-index: 100;
  border-radius: 0 0 8px 0;
  font-weight: 600;
  text-decoration: none;
  font-family: var(--body-font);
}
.skip-link:focus { top: 0; }

/* ── Footer ── */
.site-footer {
  border-top: 1px solid var(--rule);
  padding: 24px 32px;
  text-align: center;
  font-family: var(--body-font);
  font-size: 0.82rem;
  color: var(--muted);
  background: linear-gradient(180deg, transparent, rgba(26, 22, 16, 0.04));
}
.site-footer .footer-rule {
  width: 80px;
  height: 2px;
  background: linear-gradient(90deg, transparent, var(--gold), transparent);
  margin: 0 auto 14px;
  border: none;
}
.site-footer a {
  color: var(--accent-deep);
  text-decoration: none;
  font-weight: 600;
  transition: color 0.2s;
}
.site-footer a:hover { color: var(--accent); }
.footer-links { margin-bottom: 8px; }
.footer-links a + a { margin-left: 18px; }
.footer-copy { font-style: italic; font-size: 0.76rem; }
