:root {
  --bg: #ffffff;
  --bg-sidebar: #f7f6f3;
  --text: #1b1b1b;
  --text-muted: #6b6b6b;
  --border: #e4e2dd;
  --link: #a8562e;
  --link-hover: #7d3f20;
  --code-bg: #f1f0ec;
  --mark-bg: #f2cb8e;
  --mark-text: #3a2a12;
  --max-width: 780px;
  --sidebar-width: 260px;
}

@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #171614;
    --bg-sidebar: #1e1c19;
    --text: #e8e6e1;
    --text-muted: #9a978f;
    --border: #322f2a;
    --link: #e0925f;
    --link-hover: #f0ac82;
    --code-bg: #24221e;
    --mark-bg: #6b4a1e;
    --mark-text: #ffe4b0;
  }
}

/* Explicit theme override (set via the toggle button; wins over the OS
   preference in both directions) */
:root[data-theme="dark"] {
  --bg: #171614;
  --bg-sidebar: #1e1c19;
  --text: #e8e6e1;
  --text-muted: #9a978f;
  --border: #322f2a;
  --link: #e0925f;
  --link-hover: #f0ac82;
  --code-bg: #24221e;
  --mark-bg: #6b4a1e;
  --mark-text: #ffe4b0;
}

* { box-sizing: border-box; }

html { scroll-behavior: smooth; }

body {
  margin: 0;
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Helvetica, Arial, sans-serif;
  background: var(--bg);
  color: var(--text);
  line-height: 1.6;
}

.layout {
  display: flex;
  min-height: 100vh;
}

/* Sidebar */
.sidebar {
  width: var(--sidebar-width);
  flex-shrink: 0;
  background: var(--bg-sidebar);
  border-right: 1px solid var(--border);
  padding: 1.5rem 1rem;
  position: sticky;
  top: 0;
  height: 100vh;
  overflow-y: auto;
}

.sidebar h2 {
  font-size: 0.75rem;
  text-transform: uppercase;
  letter-spacing: 0.06em;
  color: var(--text-muted);
  margin: 0 0 0.75rem 0.5rem;
}

.sidebar nav ul {
  list-style: none;
  margin: 0;
  padding: 0;
}

.sidebar nav a {
  display: block;
  padding: 0.3rem 0.5rem;
  border-radius: 6px;
  color: var(--text);
  text-decoration: none;
  font-size: 0.9rem;
}

.sidebar nav a:hover {
  background: var(--border);
}

.sidebar nav a.active {
  background: var(--border);
  color: var(--link);
  box-shadow: inset 3px 0 0 0 var(--link);
}

/* Top-level sections read as category headers */
.sidebar nav li.level-1 {
  margin-top: 1.1rem;
}

.sidebar nav li.level-1:first-child {
  margin-top: 0;
}

.sidebar nav li.level-1 a {
  font-weight: 700;
  color: var(--text);
}

.sidebar nav li.level-2 a {
  padding-left: 1rem;
  font-size: 0.87rem;
}

.sidebar nav li.level-3 a {
  padding-left: 1.75rem;
  font-size: 0.83rem;
  color: var(--text-muted);
}

/* Search */
.sidebar-search {
  position: relative;
  margin-bottom: 1.1rem;
}

.sidebar-search input {
  width: 100%;
  padding: 0.5rem 0.7rem;
  border-radius: 7px;
  border: 1px solid var(--border);
  background: var(--bg);
  color: var(--text);
  font-size: 0.88rem;
  font-family: inherit;
}

.sidebar-search input::placeholder {
  color: var(--text-muted);
}

.sidebar-search input:focus {
  outline: 2px solid var(--link);
  outline-offset: 1px;
}

.search-results {
  display: none;
  position: absolute;
  top: 100%;
  left: 0;
  right: 0;
  margin-top: 0.4rem;
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: 8px;
  max-height: 65vh;
  overflow-y: auto;
  box-shadow: 0 10px 28px rgba(0, 0, 0, 0.28);
  z-index: 20;
}

.search-results.open {
  display: block;
}

.search-result {
  display: block;
  padding: 0.6rem 0.75rem;
  text-decoration: none;
  color: var(--text);
  border-bottom: 1px solid var(--border);
}

.search-result:last-child {
  border-bottom: none;
}

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

.search-result .result-section {
  font-size: 0.7rem;
  text-transform: uppercase;
  letter-spacing: 0.05em;
  color: var(--text-muted);
  margin-bottom: 0.2rem;
}

.search-result .result-snippet {
  font-size: 0.85rem;
  line-height: 1.4;
}

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

.search-empty {
  padding: 0.75rem;
  color: var(--text-muted);
  font-size: 0.85rem;
}

@keyframes search-flash {
  0% { background: var(--mark-bg); }
  100% { background: transparent; }
}

.search-highlight {
  animation: search-flash 1.6s ease;
  border-radius: 6px;
}

/* Main content */
main {
  flex: 1;
  min-width: 0;
  padding: 3rem 2rem 6rem;
  overflow-x: hidden;
}

.content {
  max-width: var(--max-width);
  margin: 0 auto;
}

.content h1, .content h2, .content h3 {
  scroll-margin-top: 1.5rem;
  line-height: 1.3;
}

.content h1 {
  font-size: 1.9rem;
  border-bottom: 1px solid var(--border);
  padding-bottom: 0.5rem;
  margin-top: 2.5rem;
}

.content h1:first-child {
  margin-top: 0;
}

.content h2 {
  font-size: 1.4rem;
  margin-top: 2rem;
}

.content h3 {
  font-size: 1.1rem;
  margin-top: 1.5rem;
}

.content a {
  color: var(--link);
}

.content a:hover {
  color: var(--link-hover);
}

.content img {
  max-width: 100%;
  border-radius: 8px;
  border: 1px solid var(--border);
  margin: 0.5rem 0.5rem 0.5rem 0;
}

.content code {
  background: var(--code-bg);
  padding: 0.15em 0.4em;
  border-radius: 4px;
  font-size: 0.9em;
}

.content ul, .content ol {
  padding-left: 1.4rem;
}

.content table {
  width: 100%;
  border-collapse: collapse;
  margin: 1rem 0 1.5rem;
  font-size: 0.95rem;
}

.content th, .content td {
  border: 1px solid var(--border);
  padding: 0.5em 0.8em;
  text-align: left;
}

.content th {
  background: var(--bg-sidebar);
  font-weight: 600;
}

.content hr {
  border: none;
  border-top: 1px solid var(--border);
  margin: 2rem 0;
}

/* Mobile */
.menu-toggle {
  display: none;
}

@media (max-width: 860px) {
  .layout {
    display: block;
  }

  .menu-toggle {
    display: block;
    position: sticky;
    top: 0;
    z-index: 10;
    width: 100%;
    text-align: left;
    padding: 0.9rem 1rem;
    background: var(--bg-sidebar);
    border: none;
    border-bottom: 1px solid var(--border);
    color: var(--text);
    font-size: 1rem;
    font-weight: 600;
  }

  .sidebar {
    position: static;
    height: auto;
    width: auto;
    display: none;
    border-right: none;
    border-bottom: 1px solid var(--border);
  }

  .sidebar.open {
    display: block;
  }

  main {
    padding: 1.5rem 1.25rem 4rem;
  }
}

.loading, .error {
  color: var(--text-muted);
  padding: 2rem 0;
}

/* ─── Password gate ─── */
.gate-hidden { display: none !important; }

.gate-screen {
  display: flex;
  align-items: center;
  justify-content: center;
  min-height: 100vh;
  padding: 2rem;
}

.gate-box {
  width: 100%;
  max-width: 360px;
  text-align: center;
}

.gate-box h1 {
  font-size: 1.4rem;
  margin-bottom: 0.5rem;
}

.gate-box p {
  color: var(--text-muted);
  font-size: 0.9rem;
  margin-bottom: 1.5rem;
}

.gate-box input[type="password"] {
  width: 100%;
  box-sizing: border-box;
  padding: 0.75rem 1rem;
  border: 1px solid var(--border);
  border-radius: 6px;
  background: var(--bg);
  color: var(--text);
  font-size: 1rem;
  text-align: center;
  margin-bottom: 1rem;
}

.gate-box button {
  width: 100%;
  padding: 0.75rem 1rem;
  border: none;
  border-radius: 6px;
  background: var(--text);
  color: var(--bg);
  font-size: 0.85rem;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.08em;
  cursor: pointer;
}

.gate-error {
  color: #c0392b;
  font-size: 0.85rem;
  margin-top: 0.75rem;
}

/* ─── Theme toggle ─── */
.theme-toggle {
  position: fixed;
  top: 1rem;
  right: 1rem;
  z-index: 40;
  width: 2.25rem;
  height: 2.25rem;
  border-radius: 50%;
  border: 1px solid var(--border);
  background: var(--bg-sidebar);
  color: var(--text);
  font-size: 1.05rem;
  line-height: 1;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 0;
}

.theme-toggle:hover {
  background: var(--border);
}

@media (max-width: 860px) {
  .theme-toggle {
    top: 0.6rem;
    right: 0.75rem;
    width: 2rem;
    height: 2rem;
    font-size: 0.95rem;
  }
}

/* ─── Collapsible TOC sections ─── */
.sidebar nav li {
  position: relative;
}

.sidebar nav .toc-row {
  display: flex;
  align-items: center;
}

.sidebar nav .toc-row a {
  flex: 1;
  min-width: 0;
}

.toc-toggle {
  flex: none;
  width: 1.1rem;
  height: 1.1rem;
  margin-left: 0.1rem;
  padding: 0;
  border: none;
  background: none;
  color: var(--text-muted);
  cursor: pointer;
  font-size: 0.7rem;
  line-height: 1;
  display: flex;
  align-items: center;
  justify-content: center;
}

.toc-toggle:hover {
  color: var(--text);
}

.toc-toggle::before {
  content: "\2212";
}

li.toc-collapsed > .toc-row > .toc-toggle::before {
  content: "+";
}

li.toc-collapsed > ul {
  display: none;
}
