/**
 * Chapter Two-Column Layout - Right Sidebar TOC
 * Grid layout with sticky right sidebar
 */

/* Remove width constraints on book pages to allow full expansion */
body.path-node.page-node-type-book .layout-container,
body.path-node.page-node-type-book .layout-content,
body.path-node.page-node-type-book .main-content__container,
body.path-node.page-node-type-book .container,
body.path-node.page-node-type-book .layout-main-wrapper,
body.path-node.page-node-type-book .layout-main,
body.path-node.page-node-type-book .main-content,
body.path-node.page-node-type-book .site-main,
body.path-node.page-node-type-book main,
body.path-chapter .layout-container,
body.path-chapter .layout-content,
body.path-chapter .main-content__container,
body.path-chapter .container,
body.path-chapter .layout-main-wrapper,
body.path-chapter .layout-main,
body.path-chapter .main-content,
body.path-chapter .site-main,
body.path-chapter main,
body.path-chapter-1 .layout-container,
body.path-chapter-1 .layout-content,
body.path-chapter-1 .main-content__container,
body.path-chapter-1 .container,
body.path-chapter-1 .layout-main-wrapper,
body.path-chapter-1 .layout-main,
body.path-chapter-1 .main-content,
body.path-chapter-1 .site-main,
body.path-chapter-1 main {
  max-width: none !important;
}

/* Make the overall chapter area wider */
.chapter-layout {
  width: 100%;
  max-width: none;
  margin-inline: auto;
  padding-inline: 0;
  display: grid;
  gap: 2rem;
  align-items: stretch; /* Stretch both columns to full height */
}

/* Align chapter-layout with breadcrumbs - same padding */
body.path-chapter .chapter-layout,
body.path-node.page-node-type-book .chapter-layout {
  max-width: none !important;
  margin-inline: auto !important;
  padding-inline: 0 !important;
}

/* Ensure article container doesn't add extra padding */
body.path-chapter article.node--book-page,
body.path-node.page-node-type-book article.node--book-page {
  padding: 0 !important;
  margin: 0 !important;
}

/* Remove any extra centering/max-width on chapter pages */
.path-chapter .chapter-layout,
.path-chapter .chapter-hero,
.path-chapter .chapter-main,
.path-chapter .chapter-card {
  margin-left: auto !important;
  margin-right: auto !important;
}

/* Ensure chapter grid fills the container */
.path-chapter .chapter-layout {
  width: 100%;
}

/* Make sure ALL titles align with breadcrumbs - use same padding as breadcrumbs */
body.path-node.page-node-type-book h1.page-title,
body.path-node.page-node-type-book .node__title,
body.path-node.page-node-type-book article.node--book-page > h1,
body.path-node.page-node-type-book h1,
.path-chapter h1.page-title,
.path-chapter .node__title,
.path-chapter article.node--book-page > h1,
.path-chapter h1 {
  margin-left: 0 !important;
  margin-right: 0 !important;
  padding-inline: var(--container-padding, 1.5rem) !important;
  max-width: none !important;
}

/* Specifically target the page title block if it exists */
body.path-node.page-node-type-book .block-page-title-block,
body.path-node.page-node-type-book .block-page-title-block .page-title,
.path-chapter .block-page-title-block,
.path-chapter .block-page-title-block .page-title {
  padding-inline: 0 !important;
  margin-inline: 0 !important;
  max-width: none !important;
}

.page-node-type-book h1.page-title,
.page-node-type-book .page-title {
  display: none;
}

body.path-node.page-node-type-book article.node--book-page > h1,
body.path-chapter article.node--book-page > h1 {
  display: none !important;
}

/* Use the custom chapter TOC panel instead of Drupal's legacy book pager links. */
body.path-node.page-node-type-book nav.book-navigation,
body.path-node.page-node-type-book .book-navigation,
body.path-chapter nav.book-navigation,
body.path-chapter .book-navigation,
body.path-node.page-node-type-book .book-pager,
body.path-chapter .book-pager {
  display: none !important;
}

/* Path-agnostic fallback so chapter aliases still hide legacy prev/up/next links. */
article.node--book-page nav.book-navigation,
article.node--book-page .book-navigation,
article.node--book-page .book-pager {
  display: none !important;
}

/* Drupal's book template may render traversal links without a .book-navigation class. */
article.node--book-page .chapter-main nav[aria-labelledby^="book-label-"],
article.node--book-page .chapter-main .book-traversal__list {
  display: none !important;
}

/* Breadcrumb keeps Olivero defaults; chapter body is aligned to the same container. */

/* Uncap text containers from Olivero/linear_book constraints */
.path-chapter .node__content,
.path-chapter .field--name-body,
.path-chapter .text-formatted {
  max-width: none !important;
}

/* Let the content column expand to the same left edge as breadcrumbs */
.page-node-type-book .chapter-card,
.page-node-type-book .chapter-main,
.page-node-type-book .node__content {
  max-width: none !important;
  margin-left: 0 !important;
  margin-right: 0 !important;
}

/* Give more space to the content column */
.chapter-layout--right {
  grid-template-columns: minmax(0, 1fr) 200px;
}

/* LEFT sidebar: TOC left, content right */
.chapter-layout--left {
  grid-template-columns: 200px minmax(0, 1fr);
}

/* Chapter pages: make the main layout use full container width */
.page-node-type-book .chapter-layout {
  width: 100%;
  max-width: none;
  display: grid;
  grid-template-columns: minmax(0, 1fr) 200px; /* content | toc */
  gap: 2.25rem;
  align-items: start;
}

.page-node-type-book .chapter-layout--left,
.path-chapter .chapter-layout--left {
  grid-template-columns: 200px minmax(0, 1fr);
}

/* Remove Olivero's text-content width clamp on book pages */
.page-node-type-book .text-content,
.page-node-type-book .field--name-body .field__item {
  max-width: none !important;
  width: 100% !important;
}

/* Ensure the main column can actually grow */
.page-node-type-book .chapter-main {
  min-width: 0;            /* important for grid/flex overflow */
}

/* Remove any node-level max-width clamps */
.page-node-type-book .node,
.page-node-type-book article.node {
  max-width: none !important;
}

/* Path-specific chapter grid rules */
body.path-node.page-node-type-book .chapter-layout,
body.path-chapter .chapter-layout {
  width: 100%;
  max-width: none;
  margin-inline: auto;
  padding-inline: 0;
  display: grid;
  grid-template-columns: minmax(0, 1fr) 200px;
  gap: 2.25rem;
  align-items: stretch; /* Allow sidebar to be full height for sticky */
}

/* Left sidebar variant must override path-level default with same specificity. */
body.path-node.page-node-type-book .chapter-layout.chapter-layout--left,
body.path-chapter .chapter-layout.chapter-layout--left {
  grid-template-columns: 200px minmax(0, 1fr);
}

body.path-node.page-node-type-book .chapter-main,
body.path-chapter .chapter-main {
  min-width: 0;
  align-self: start; /* Keep main content at top */
}

/* Make the right content panel wider by tightening page gutters and sidebar space. */
body.path-node.page-node-type-book .layout-container,
body.path-node.page-node-type-book .layout-content,
body.path-node.page-node-type-book .main-content__container,
body.path-node.page-node-type-book .container,
body.path-node.page-node-type-book main,
body.path-chapter .layout-container,
body.path-chapter .layout-content,
body.path-chapter .main-content__container,
body.path-chapter .container,
body.path-chapter main {
  padding-inline: 0.75rem !important;
}

body.path-node.page-node-type-book .chapter-layout,
body.path-chapter .chapter-layout {
  grid-template-columns: minmax(0, 1fr) 190px;
  gap: 1.5rem;
}

body.path-node.page-node-type-book .chapter-layout.chapter-layout--left,
body.path-chapter .chapter-layout.chapter-layout--left {
  grid-template-columns: 190px minmax(0, 1fr);
}

body.path-node.page-node-type-book .chapter-aside,
body.path-chapter .chapter-aside {
  align-self: start; /* Keep aside at top but full height available */
}

/* Remove any max-width clamp inside the main column */
body.path-node.page-node-type-book .chapter-main .node__content,
body.path-node.page-node-type-book .chapter-main .field--name-body,
body.path-node.page-node-type-book .chapter-main .text-formatted,
body.path-chapter .chapter-main .node__content,
body.path-chapter .chapter-main .field--name-body,
body.path-chapter .chapter-main .text-formatted {
  max-width: none !important;
  width: 100% !important;
}

/* Ensure chapter-card is full width on book pages */
body.path-node.page-node-type-book .chapter-card {
  max-width: none !important;
  width: 100% !important;
  margin: 0 !important;
}

/* Widen chapter body panel so it aligns better with breadcrumb region */
body.path-node.page-node-type-book .chapter-main .chapter-content,
body.path-chapter .chapter-main .chapter-content {
  max-width: none !important;
  width: 100% !important;
  margin-inline: 0 !important;
}

/* Ensure the chapter card lines up left */
.chapter-main .chapter-card,
.chapter-main .node__content {
  margin-left: 0 !important;
}

/* Critical for grid layouts with long content */
.chapter-main {
  min-width: 0;
}

/* Kill Olivero/region max-width limits INSIDE your chapter layout */
.chapter-layout .chapter-main,
.chapter-layout .chapter-main .node__content,
.chapter-layout .chapter-main .layout__region,
.chapter-layout .chapter-main .block-system-main-block,
.chapter-layout .chapter-main .field--name-body,
.chapter-layout .chapter-main .text-formatted,
.chapter-layout .chapter-main .field,
.chapter-layout .chapter-main .field__item {
  max-width: none !important;
  width: 100% !important;
}

/* Make the TOC actually "float" */
.chapter-aside {
  position: relative;
}

/* This is the sticky element */
.chapter-aside .chapter-toc-wrapper {
  position: sticky;
  top: 110px;
  width: calc(100% - 12px);
  height: calc(100vh - 130px);
  overflow: hidden;
  background: #ffffff;
  border: none;
  border-radius: 8px;
  padding: 1rem 0;
  margin-inline: auto;
  box-shadow: var(--shadow-soft, 0 8px 28px rgba(2, 6, 23, 0.08));
  z-index: 100;
}

/* Prevent "sticky inside sticky" conflicts */
.chapter-aside .chapter-toc-bar {
  position: static;
  margin-bottom: 0;
  padding-bottom: 0.4rem;
  border-bottom: none;
}

/* Dark mode TOC box */
html.dark-mode .chapter-aside .chapter-toc-wrapper {
  background: #1e293b;
  box-shadow: 0 16px 40px rgba(0, 0, 0, 0.45);
}

/* Dark mode via OS preference (covers cases where html.dark-mode class is not set) */
@media (prefers-color-scheme: dark) {
  .chapter-aside .chapter-toc-wrapper {
    background: #1e293b;
    box-shadow: 0 16px 40px rgba(0, 0, 0, 0.45);
  }
  .chapter-aside .chapter-toc-bar {
    border-bottom: none;
  }
  .chapter-toc-list,
  .chapter-toc-list a,
  .chapter-toc-list .toc-h2-link,
  .chapter-toc-list .toc-h3-link {
    color: rgba(191, 219, 254, 0.95);
  }
  .chapter-toc-list a:hover {
    background: rgba(148, 163, 184, 0.12);
    color: #ffffff;
  }
}

/* Mobile stacking — single column, TOC naturally appears above content
   since aside is first in the DOM (no order override needed). */
@media (max-width: 1100px) {
  .chapter-layout--right,
  .chapter-layout--left {
    grid-template-columns: 1fr;
  }
}

/* Scrollbar styling for the TOC list (not wrapper) */
.chapter-aside .chapter-toc::-webkit-scrollbar {
  width: 8px;
}

.chapter-aside .chapter-toc::-webkit-scrollbar-track {
  background: transparent;
}

.chapter-aside .chapter-toc::-webkit-scrollbar-thumb {
  background: rgba(100, 116, 139, 0.4);
  border-radius: 4px;
}

.chapter-aside .chapter-toc::-webkit-scrollbar-thumb:hover {
  background: rgba(100, 116, 139, 0.6);
}

/* Dark mode scrollbar */
html.dark-mode .chapter-aside .chapter-toc::-webkit-scrollbar-thumb {
  background: rgba(148, 163, 184, 0.3);
}

html.dark-mode .chapter-aside .chapter-toc::-webkit-scrollbar-thumb:hover {
  background: rgba(148, 163, 184, 0.5);
}

/* =========================
   TOC (Chapter Navigation) — Dark mode fixes
   Replace ".dark-mode" with whatever class your toggle adds to <body>
   ========================= */

html.dark-mode .chapter-toc,
body.dark-mode .chapter-toc,
html.dark-mode .chapter-toc__card,
body.dark-mode .chapter-toc__card,
html.dark-mode .chapter-toc__panel,
body.dark-mode .chapter-toc__panel {
  background: #0b1220;              /* dark panel */
  color: #e5e7eb;
  border-color: rgba(148, 163, 184, 0.25);
  box-shadow: 0 18px 48px rgba(0,0,0,0.45);
}

/* Links */
html.dark-mode .chapter-toc a,
body.dark-mode .chapter-toc a,
html.dark-mode .chapter-toc a:visited,
body.dark-mode .chapter-toc a:visited {
  color: #c7d2fe;                    /* readable link color */
  text-decoration-color: rgba(199, 210, 254, 0.35);
}

html.dark-mode .chapter-toc a:hover,
body.dark-mode .chapter-toc a:hover {
  color: #ffffff;
  text-decoration-color: rgba(255, 255, 255, 0.55);
}

/* "Currently in …" label */
html.dark-mode .chapter-toc__current,
body.dark-mode .chapter-toc__current {
  color: #e5e7eb;
  border-bottom-color: transparent;
}

/* Active highlight (yellow background + dark text) */
html.dark-mode .chapter-toc .is-active > a,
body.dark-mode .chapter-toc .is-active > a,
html.dark-mode .chapter-toc a.is-active,
body.dark-mode .chapter-toc a.is-active {
  background: #fde047;              /* yellow */
  color: #000000 !important;        /* max contrast on yellow in dark mode */
  font-weight: 800;
  border-radius: 6px;
  padding: 2px 6px;
}

/* If you highlight the active leaf item with a <mark> or span */
html.dark-mode .chapter-toc mark,
body.dark-mode .chapter-toc mark,
html.dark-mode .chapter-toc .active-highlight,
body.dark-mode .chapter-toc .active-highlight {
  background: #fde047;
  color: #111827;
}

/* Chevron/twisty color */
html.dark-mode .chapter-toc .toc-toggle,
body.dark-mode .chapter-toc .toc-toggle,
html.dark-mode .chapter-toc summary::marker,
body.dark-mode .chapter-toc summary::marker,
html.dark-mode .chapter-toc summary,
body.dark-mode .chapter-toc summary,
html.dark-mode .chapter-toc .toc-arrow,
body.dark-mode .chapter-toc .toc-arrow {
  color: #e5e7eb;
}

/* Scrollbar (optional) */
html.dark-mode .chapter-toc,
body.dark-mode .chapter-toc {
  scrollbar-color: rgba(148,163,184,0.55) rgba(15,23,42,0.35);
}
html.dark-mode .chapter-toc::-webkit-scrollbar,
body.dark-mode .chapter-toc::-webkit-scrollbar {
  width: 10px;
}
html.dark-mode .chapter-toc::-webkit-scrollbar-thumb,
body.dark-mode .chapter-toc::-webkit-scrollbar-thumb {
  background: rgba(148,163,184,0.55);
  border-radius: 999px;
}
html.dark-mode .chapter-toc::-webkit-scrollbar-track,
body.dark-mode .chapter-toc::-webkit-scrollbar-track {
  background: rgba(15,23,42,0.35);
}

/* TOC toggle and active label styling */

.chapter-aside .chapter-toc-bar .chapter-toc-toggle {
  display: none; /* Hide toggle in sidebar as TOC is always visible */
}

.chapter-aside .chapter-toc-bar .chapter-toc-active {
  /* Fixed height so the bar never jumps when text appears/disappears */
  display: block;
  min-height: 2.6rem;
  padding: 0.5rem 0.75rem;
  max-width: 100%;
  /* Match TOC link typography */
  font-size: 0.9rem;
  font-weight: 600;
  line-height: 1.4;
  color: var(--toc-link, #0b3a82);
  text-align: center;
  background: transparent;
  border-radius: 0;
  box-shadow: none;
}

/* Dark mode label color matches TOC links */
html.dark-mode .chapter-aside .chapter-toc-bar .chapter-toc-active,
body.dark-mode .chapter-aside .chapter-toc-bar .chapter-toc-active {
  background: transparent;
  color: rgba(191, 219, 254, 0.95);
  box-shadow: none;
}

@media (prefers-color-scheme: dark) {
  .chapter-aside .chapter-toc-bar .chapter-toc-active {
    background: transparent;
    color: rgba(191, 219, 254, 0.95);
    box-shadow: none;
  }
}

/* Make the inside list scroll (so sticky stays fixed) */
.chapter-aside .chapter-toc {
  display: block !important;
  padding: 0;
  height: calc(100vh - 180px);
  overflow: auto;
}

/* Defensive visibility rules: keep TOC visible even if other styles conflict. */
body.path-node.page-node-type-book .chapter-aside,
body.path-chapter .chapter-aside,
body.path-node.page-node-type-book .chapter-aside .chapter-toc-wrapper,
body.path-chapter .chapter-aside .chapter-toc-wrapper,
body.path-node.page-node-type-book .chapter-aside #chapter-toc,
body.path-chapter .chapter-aside #chapter-toc {
  display: block !important;
  visibility: visible !important;
  opacity: 1 !important;
}

body.path-node.page-node-type-book .chapter-aside #chapter-toc,
body.path-chapter .chapter-aside #chapter-toc {
  color: var(--toc-text, #0f172a) !important;
}

body.path-node.page-node-type-book .chapter-aside #chapter-toc a,
body.path-chapter .chapter-aside #chapter-toc a {
  color: inherit;
}

@media (prefers-color-scheme: dark) {
  body.path-node.page-node-type-book .chapter-aside #chapter-toc,
  body.path-chapter .chapter-aside #chapter-toc {
    color: rgba(226, 232, 240, 0.95) !important;
  }

  body.path-node.page-node-type-book .chapter-aside #chapter-toc a,
  body.path-chapter .chapter-aside #chapter-toc a {
    color: rgba(191, 219, 254, 0.95) !important;
  }
}

/* Grid-column overrides are not needed — DOM order now matches visual order.
   Left layout: aside first in DOM → auto-placed in column 1 (200px left).
   Right layout: main first in DOM → auto-placed in column 1 (flexible).
   Desktop: just ensure the aside stretches to full column width. */
@media (min-width: 1101px) {
  .chapter-layout--right .chapter-aside,
  .chapter-layout--left .chapter-aside {
    justify-self: stretch;
    align-self: start;
  }
}

/* Path-specific TOC sticky behavior for chapter pages */
body.path-chapter .chapter-aside .chapter-toc-wrapper {
  position: sticky;
  top: 110px;
  height: calc(100vh - 130px);
  overflow: hidden; /* wrapper does not scroll */
}

body.path-chapter .chapter-aside .chapter-toc {
  height: calc(100vh - 190px); /* list scrolls */
  overflow: auto;
}

/* Path-specific TOC sticky behavior for book node pages */
body.path-node.page-node-type-book .chapter-aside .chapter-toc-wrapper {
  position: sticky;
  top: 110px;
  height: calc(100vh - 130px);
  overflow: hidden;
}

body.path-node.page-node-type-book .chapter-aside .chapter-toc {
  height: calc(100vh - 190px);
  overflow: auto;
}

/* Prevent containers from breaking sticky positioning */
body.path-chapter .layout-container,
body.path-chapter .layout-content,
body.path-chapter .region-content,
body.path-chapter .block-system-main-block,
body.path-chapter .chapter-layout,
body.path-chapter .chapter-main,
body.path-chapter article {
  overflow: visible !important;
  transform: none !important;
}

body.path-chapter .chapter-aside {
  position: relative;
  overflow: visible !important;
  transform: none !important;
  min-height: 100%; /* Ensure aside stretches to full container height */
}

/* Prevent containers from breaking sticky positioning on book node pages */
body.path-node.page-node-type-book .layout-container,
body.path-node.page-node-type-book .layout-content,
body.path-node.page-node-type-book .region-content,
body.path-node.page-node-type-book .block-system-main-block,
body.path-node.page-node-type-book .chapter-layout,
body.path-node.page-node-type-book .chapter-main,
body.path-node.page-node-type-book article {
  overflow: visible !important;
  transform: none !important;
}

body.path-node.page-node-type-book .chapter-aside {
  position: relative;
  overflow: visible !important;
  transform: none !important;
  min-height: 100%;
}

/* Override column layout for sidebar */
.chapter-aside .chapter-toc-list {
  columns: 1;
  column-gap: 0;
}

/* Adjust spacing for sidebar TOC */
.chapter-aside .toc-h2-item {
  margin: 0 0 0.6rem 0;
}

.chapter-aside .toc-h2-row {
  gap: 0.3rem;
}

.chapter-aside .toc-h2-toggle {
  width: 28px;
  height: 28px;
}

.chapter-aside .toc-h3-link {
  font-size: 0.88rem;
}
  .chapter-aside .toc-level-3 {
    list-style: none;
    margin: 0.4rem 0 0.6rem 1.2rem;
    padding: 0;
    max-height: 0;
    opacity: 0;
    overflow: hidden;
    transition: max-height 0.2s ease, opacity 0.2s ease;
  }
    .chapter-aside .toc-level-3:not([hidden]) {
      max-height: 1000px;
      opacity: 1;
    }
  .chapter-aside .toc-level-3 li {
    margin: 0.25rem 0;
  }

/* =========================
   TOC color system (base)
   ========================= */

:root{
  --toc-bg: rgba(255,255,255,0.92);
  --toc-border: rgba(15, 23, 42, 0.12);
  --toc-text: #0f172a;
  --toc-muted: rgba(15, 23, 42, 0.70);
  --toc-link: #0b3a82;
  --toc-link-hover-bg: rgba(15, 23, 42, 0.06);

  /* active highlight */
  --toc-active-bg: #fde047;
  --toc-active-text: #111827;

  /* divider line */
  --toc-rule: rgba(15, 23, 42, 0.14);
}

/* Try multiple common "dark mode" selectors (use whichever matches your site) */
@media (prefers-color-scheme: dark){
  :root{
    --toc-bg: rgba(15, 23, 42, 0.92);
    --toc-border: rgba(148, 163, 184, 0.22);
    --toc-text: rgba(226, 232, 240, 0.95);
    --toc-muted: rgba(226, 232, 240, 0.72);
    --toc-link: rgba(191, 219, 254, 0.95);
    --toc-link-hover-bg: rgba(148, 163, 184, 0.12);

    /* Keep highlight consistent with light mode and preserve contrast. */
    --toc-active-bg: #fde047;
    --toc-active-text: #111827;

    --toc-rule: rgba(148, 163, 184, 0.22);
  }
}

/* If your toggle uses a class or attribute, keep these too:
   (Leave them even if you're unsure; they won't hurt.) */
html.dark, body.dark, body.is-dark, html[data-theme="dark"], body[data-theme="dark"]{
  --toc-bg: rgba(15, 23, 42, 0.92);
  --toc-border: rgba(148, 163, 184, 0.22);
  --toc-text: rgba(226, 232, 240, 0.95);
  --toc-muted: rgba(226, 232, 240, 0.72);
  --toc-link: rgba(191, 219, 254, 0.95);
  --toc-link-hover-bg: rgba(148, 163, 184, 0.12);
  --toc-active-bg: #fde047;
  --toc-active-text: #111827;
  --toc-rule: rgba(148, 163, 184, 0.22);
}

/* =========================
   Apply variables to TOC UI
   ========================= */

/* Update selector to match your TOC wrapper class */
.chapter-toc, .toc-box, .chapter-nav, .chapter-toc-box {
  background: transparent;
  color: var(--toc-text);
  border: none;
}

.chapter-toc .toc-current,
.chapter-toc .toc-title,
.chapter-toc .toc-header{
  color: var(--toc-text);
  border-bottom: none;
}

/* Links */
.chapter-toc a{
  color: var(--toc-link);
  text-decoration: none;
}
.chapter-toc a:hover{
  background: var(--toc-link-hover-bg);
  border-radius: 6px;
}

/* Active highlight */
.chapter-toc a.is-active,
.chapter-toc li.is-active > a{
  background: var(--toc-active-bg);
  color: var(--toc-active-text) !important;
  font-weight: 700;
  border-radius: 6px;
}

/* Muted secondary text (optional) */
.chapter-toc .muted,
.chapter-toc small{
  color: var(--toc-muted);
}

/* Final visibility guardrails for chapter pages.
   Keep TOC content readable even if other global link/color rules cascade in. */
body.path-node.page-node-type-book .chapter-layout.chapter-layout--left .chapter-aside,
body.path-chapter .chapter-layout.chapter-layout--left .chapter-aside,
body.path-node.page-node-type-book .chapter-layout.chapter-layout--right .chapter-aside,
body.path-chapter .chapter-layout.chapter-layout--right .chapter-aside {
  min-width: 200px;
}

body.path-node.page-node-type-book .chapter-aside .chapter-toc-wrapper,
body.path-chapter .chapter-aside .chapter-toc-wrapper {
  color: var(--toc-text, #0f172a) !important;
}

body.path-node.page-node-type-book .chapter-aside #chapter-toc,
body.path-chapter .chapter-aside #chapter-toc,
body.path-node.page-node-type-book .chapter-aside #chapter-toc .chapter-toc-list,
body.path-chapter .chapter-aside #chapter-toc .chapter-toc-list,
body.path-node.page-node-type-book .chapter-aside #chapter-toc .chapter-toc-list li,
body.path-chapter .chapter-aside #chapter-toc .chapter-toc-list li {
  visibility: visible !important;
  opacity: 1 !important;
}

body.path-node.page-node-type-book .chapter-aside #chapter-toc .toc-h2-row,
body.path-chapter .chapter-aside #chapter-toc .toc-h2-row {
  display: flex;
  align-items: flex-start;
}

body.path-node.page-node-type-book .chapter-aside #chapter-toc .toc-h2-link,
body.path-node.page-node-type-book .chapter-aside #chapter-toc .toc-h3-link,
body.path-chapter .chapter-aside #chapter-toc .toc-h2-link,
body.path-chapter .chapter-aside #chapter-toc .toc-h3-link {
  color: var(--toc-link, #0b3a82) !important;
  line-height: 1.4;
}

html.dark-mode body.path-node.page-node-type-book .chapter-aside #chapter-toc .toc-h2-link,
html.dark-mode body.path-node.page-node-type-book .chapter-aside #chapter-toc .toc-h3-link,
html.dark-mode body.path-chapter .chapter-aside #chapter-toc .toc-h2-link,
html.dark-mode body.path-chapter .chapter-aside #chapter-toc .toc-h3-link,
body.dark-mode.path-node.page-node-type-book .chapter-aside #chapter-toc .toc-h2-link,
body.dark-mode.path-node.page-node-type-book .chapter-aside #chapter-toc .toc-h3-link,
body.dark-mode.path-chapter .chapter-aside #chapter-toc .toc-h2-link,
body.dark-mode.path-chapter .chapter-aside #chapter-toc .toc-h3-link {
  color: rgba(191, 219, 254, 0.95) !important;
}

/* Active highlighted links must keep dark text on yellow, even against
   the high-specificity path-level link color rules above. */
body.path-node.page-node-type-book .chapter-aside #chapter-toc a.is-active,
body.path-chapter .chapter-aside #chapter-toc a.is-active,
body.path-node.page-node-type-book .chapter-aside #chapter-toc li.is-active > a,
body.path-chapter .chapter-aside #chapter-toc li.is-active > a,
html.dark-mode body.path-node.page-node-type-book .chapter-aside #chapter-toc a.is-active,
html.dark-mode body.path-chapter .chapter-aside #chapter-toc a.is-active,
html.dark-mode body.path-node.page-node-type-book .chapter-aside #chapter-toc li.is-active > a,
html.dark-mode body.path-chapter .chapter-aside #chapter-toc li.is-active > a,
body.dark-mode.path-node.page-node-type-book .chapter-aside #chapter-toc a.is-active,
body.dark-mode.path-chapter .chapter-aside #chapter-toc a.is-active,
body.dark-mode.path-node.page-node-type-book .chapter-aside #chapter-toc li.is-active > a,
body.dark-mode.path-chapter .chapter-aside #chapter-toc li.is-active > a {
  background: #fde047 !important;
  color: #000000 !important;
}

/* ============================================================
   Chapter TOC - Enhanced Styling
   ============================================================ */

/* Remove default list bullets */
.chapter-toc ul,
.chapter-toc ol {
  list-style: none;
  margin: 0;
  padding-left: 0;
}

/* Remove Square Frame Around Toggle */
.chapter-toc button,
.chapter-toc .toc-toggle {
  background: none;
  border: none;
  box-shadow: none;
  padding: 0;
}

/* Larger clean twirl arrow */
.chapter-toc .toc-h2-toggle::before {
  content: "▸";
  display: inline-block;
  margin-right: 0.6rem;
  font-size: 1rem;
  font-weight: 700;
  transition: transform 0.2s ease;
  color: var(--toc-link, #0b3a82); /* explicit color so dark mode can override */
}

/* Dark mode: make the arrow light so it shows against the dark panel */
html.dark-mode .chapter-toc .toc-h2-toggle::before,
body.dark-mode .chapter-toc .toc-h2-toggle::before {
  color: rgba(191, 219, 254, 0.95);
}

@media (prefers-color-scheme: dark) {
  .chapter-toc .toc-h2-toggle::before {
    color: rgba(191, 219, 254, 0.95);
  }
}
.chapter-aside .toc-level-3 {
  list-style: none;
  margin: 0.4rem 0 0.6rem 1.2rem;
  padding: 0;
  display: block;
  max-height: 500px;
  opacity: 1;
  overflow: visible;
}

.chapter-aside .toc-level-3[hidden] {
  display: none;
}

.chapter-aside .toc-level-3 li {
  margin: 0.25rem 0;
}

/* Tab/mobile: stack layout */
@media (max-width: 768px) {
  .chapter-layout {
    grid-template-columns: 1fr;
    gap: 1.5rem;
  }

  .chapter-aside {
    order: -1;
  }

  .chapter-aside .chapter-toc-wrapper {
    position: relative;
    top: auto;
    max-height: none;
    overflow: visible;
  }

  .chapter-aside .chapter-toc {
    padding: 0.75rem 0;
  }
}

/* Ensure content respects grid boundaries */
.chapter-main {
  word-wrap: break-word;
  overflow-wrap: break-word;
}

.chapter-main img,
.chapter-main video,
.chapter-main iframe {
  max-width: 100%;
  height: auto;
}

/* =========================================================
   Professional chapter accent sync (TOC + title hierarchy)
   ========================================================= */

body.path-chapter,
body.path-node.page-node-type-book {
  --chapter-accent: #5A6B7D;
  --chapter-accent-rgb: 90, 107, 125;
}

@media (prefers-color-scheme: dark) {
  body.path-chapter,
  body.path-node.page-node-type-book {
    --chapter-accent: #9CBBD1;
    --chapter-accent-rgb: 156, 187, 209;
  }
}

html.dark-mode body.path-chapter,
html.dark-mode body.path-node.page-node-type-book,
body.dark-mode.path-chapter,
body.dark-mode.path-node.page-node-type-book {
  --chapter-accent: #9CBBD1;
  --chapter-accent-rgb: 156, 187, 209;
}

body.path-chapter .chapter-aside .chapter-toc-wrapper,
body.path-node.page-node-type-book .chapter-aside .chapter-toc-wrapper {
  border: none;
}

body.path-chapter .chapter-aside #chapter-toc .toc-h2-link,
body.path-chapter .chapter-aside #chapter-toc .toc-h3-link,
body.path-node.page-node-type-book .chapter-aside #chapter-toc .toc-h2-link,
body.path-node.page-node-type-book .chapter-aside #chapter-toc .toc-h3-link {
  color: var(--chapter-accent) !important;
  font-weight: 500;
}

body.path-chapter .chapter-aside #chapter-toc .toc-h2-link,
body.path-node.page-node-type-book .chapter-aside #chapter-toc .toc-h2-link {
  font-weight: 600;
  background: none !important;
}

body.path-chapter .chapter-aside #chapter-toc a:hover,
body.path-node.page-node-type-book .chapter-aside #chapter-toc a:hover {
  background: rgba(var(--chapter-accent-rgb), 0.12) !important;
}

body.path-chapter .chapter-aside #chapter-toc a.is-active,
body.path-chapter .chapter-aside #chapter-toc li.is-active > a,
body.path-node.page-node-type-book .chapter-aside #chapter-toc a.is-active,
body.path-node.page-node-type-book .chapter-aside #chapter-toc li.is-active > a {
  background: #fde047 !important;
  color: #1a1a1a !important;
  border-left: none !important;
  padding: 0.35rem 0.5rem !important;
  border-radius: 5px;
  font-weight: 600;
  box-shadow: none;
  display: block;
}

html.dark-mode body.path-chapter .chapter-aside #chapter-toc a.is-active,
html.dark-mode body.path-chapter .chapter-aside #chapter-toc li.is-active > a,
html.dark-mode body.path-node.page-node-type-book .chapter-aside #chapter-toc a.is-active,
html.dark-mode body.path-node.page-node-type-book .chapter-aside #chapter-toc li.is-active > a,
body.dark-mode.path-chapter .chapter-aside #chapter-toc a.is-active,
body.dark-mode.path-chapter .chapter-aside #chapter-toc li.is-active > a,
body.dark-mode.path-node.page-node-type-book .chapter-aside #chapter-toc a.is-active,
body.dark-mode.path-node.page-node-type-book .chapter-aside #chapter-toc li.is-active > a {
  background: #fde047 !important;
  color: #1a1a1a !important;
}

body.path-chapter .chapter-aside .chapter-toc-bar .chapter-toc-active,
body.path-node.page-node-type-book .chapter-aside .chapter-toc-bar .chapter-toc-active {
  background: transparent;
  color: var(--toc-link, #0b3a82);
  border: none;
  text-align: center;
}

html.dark-mode body.path-chapter .chapter-aside .chapter-toc-bar .chapter-toc-active,
html.dark-mode body.path-node.page-node-type-book .chapter-aside .chapter-toc-bar .chapter-toc-active,
body.dark-mode.path-chapter .chapter-aside .chapter-toc-bar .chapter-toc-active,
body.dark-mode.path-node.page-node-type-book .chapter-aside .chapter-toc-bar .chapter-toc-active {
  background: transparent;
  color: rgba(191, 219, 254, 0.95);
  box-shadow: none;
}
