:root {
    --bg: #f5f5f9;
    --surface: #ffffff;
    --text: #111827;
    --muted: #4b5563;
    --border: #e5e7eb;
    --accent: #4f46e5;
    --accent-soft: #eef2ff;
    --accent-strong: #312e81;
    --danger: #b91c1c;
    --radius: 12px;
    --shadow: 0 16px 40px rgba(15,23,42,0.08);
    --toc-width: 290px;
  }

  @media (prefers-color-scheme: dark) {
    :root {
      --bg: #020617;
      --surface: #020617;
      --text: #e5e7eb;
      --muted: #9ca3af;
      --border: #1f2937;
      --accent: #818cf8;
      --accent-soft: #111827;
      --accent-strong: #6366f1;
      --shadow: 0 18px 50px rgba(0,0,0,0.7);
    }
  }

  * {
    box-sizing: border-box;
  }

  html, body {
    margin: 0;
    padding: 0;
    height: 100vh;
    overflow: hidden; /* Prevent body scrolling */
    font-family: system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
    background: radial-gradient(circle at top, #e0e7ff 0, var(--bg) 40%);
    color: var(--text);
  }

  a:before {
    content: '🔗 ';
    font-size: 0.9rem;
    margin-right: 0.2em;
    position: relative;
    top: -1px;
  }

  a {
    text-decoration: none;
    color: var(--accent);
    background: color-mix(in oklab, var(--accent-weak) 40%, transparent);
    border-radius: 1rem; /* Pill shape */
    padding: 0.05rem 0.5rem;
    transition: all 0.2s ease;
    display: inline-block;
    font-weight: 500;
    border: 1px solid color-mix(in oklab, var(--accent) 20%, transparent);
  }

  a:hover {
    background: color-mix(in oklab, var(--accent-weak) 70%, transparent);
    border-color: color-mix(in oklab, var(--accent) 40%, transparent);
    transform: translateY(-1px);
    box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
  }

  header {
    background: linear-gradient(135deg, #6a11cb, #2575fc);
    color: #fff;
    position: sticky;
    top: 0;
    z-index: 20;
    box-shadow: var(--shadow);
  }

  .headbar {
    max-width: 1240px;
    margin: 0 auto;
    padding: 0.75rem 1.25rem;
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 1rem;
    position: relative;
  }

  .brand {
    display: flex;
    align-items: baseline;
    gap: 0.4rem;
    position: absolute;
    left: 50%;
    transform: translateX(-50%);
  }

  .brand h1 {
    margin: 0;
    font-size: 1.1rem;
    letter-spacing: 0.08em;
    text-transform: uppercase;
    color: #e5e7eb;
  }

  .brand span {
    font-size: 0.85rem;
    color: #9ca3af;
  }

  .primary {
    margin-left: auto;
  }

  .primary picture {
    display: flex;
    align-items: center;
  }

  .primary img {
    height: 32px;
    width: auto;
  }

  .header-actions {
    display: flex;
    align-items: center;
    gap: 0.75rem;
  }

  /* Hide hamburger in header on large screens - it will be positioned above sidebar */
  @media (min-width: 981px) {
    .header-actions {
      display: none;
    }
  }

  .iconbtn {
    border-radius: 999px;
    border: 1px solid rgba(148,163,184,0.5);
    background: rgba(15,23,42,0.6);
    color: #e5e7eb;
    padding: 0.4rem 0.7rem;
    display: inline-flex;
    align-items: center;
    gap: 0.4rem;
    cursor: pointer;
    font-size: 0.8rem;
  }

  .iconbtn svg {
    width: 18px;
    height: 18px;
  }

  .iconbtn:hover {
    background: rgba(15,23,42,0.9);
  }

  .reading-progress {
    position: fixed;
    top: 3.9rem;
    left: 0;
    height: 3px;
    width: 100%;
    background: linear-gradient(to right, transparent, var(--accent-strong));
    transform-origin: 0 50%;
    transform: scaleX(var(--progress, 0));
    transition: transform 0.05s linear;
    z-index: 10;
  }

  #app {
    max-width: 1240px;
    margin: 1rem auto 2rem;
    padding: 0 1.25rem 2rem;
    display: block; /* no grid, sidebar is not part of this layout anymore */
    height: calc(100vh - 3.5rem); /* Account for top margin (1.5rem) + bottom margin (2rem) */
    overflow: hidden; /* Prevent app from scrolling */
    box-sizing: border-box;
  }
  
  .sidebar {
    position: fixed;
    top: 4.25rem;                 /* under the header */
    left: 0;
    width: var(--toc-width);      /* e.g. 290px */
    max-height: calc(100vh - 4.5rem);
    overflow: hidden;
    border-radius: var(--radius);
    background: var(--surface);
    border: 1px solid var(--border);
    box-shadow: var(--shadow);
    padding: 0.85rem 0.85rem 0.95rem;
    z-index: 15;                  /* below header, above content */
    transition: transform 0.3s ease-out, opacity 0.3s ease-out;
  }

  /* Collapsed state for sidebar on large screens */
  @media (min-width: 981px) {
    .sidebar.collapsed {
      transform: translateX(calc(-100% - 1rem));
      opacity: 0;
      pointer-events: none;
    }

    /* Position toggle button above sidebar on large screens */
    .sidebar-toggle-container {
      position: fixed;
      top: 0.35rem;
      left: 0;
      z-index: 26;
      padding: 0.5rem 0.75rem;
    }
  }

  .sidebar .search-group {
    margin-bottom: 0.7rem;
  }

  .sidebar input[type="search"] {
    width: 100%;
    padding: 0.35rem 0.6rem;
    border-radius: 999px;
    border: 1px solid var(--border);
    background: var(--surface);
    color: var(--text);
    font-size: 0.8rem;
    box-shadow: 0 1px 2px rgba(0, 0, 0, 0.05);
  }

  .sidebar input[type="search"]:focus {
    outline: 2px solid var(--accent);
    outline-offset: 2px;
    border-color: var(--accent);
  }

  .sidebar .count {
    font-size: 0.7rem;
    color: var(--muted);
    margin-bottom: 0.4rem;
  }

  .sidebar .list {
    overflow-y: auto;
    max-height: calc(100vh - 8.5rem);
    padding-right: 0.2rem;
  }

  .toc-item {
    display: block;
    border-radius: 0.55rem;
    padding: 0.3rem 0.45rem;
    margin-bottom: 0.1rem;
    font-size: 0.82rem;
    color: var(--muted);
    cursor: pointer;
    width: 100%;
    max-width: 100%;
    box-sizing: border-box;
    text-align: left;
  }

  .toc-item.level-1 {
    font-weight: 600;
    margin-top: 0.25rem;
    margin-left: 0.5rem;
    width: calc(100% - 0.5rem);
  }

  .toc-item.level-2 {
    padding-left: 0.9rem;
    margin-left: 1rem;
    width: calc(100% - 1rem);
  }

  .toc-item.level-3 {
    padding-left: 1.4rem;
    font-size: 0.78rem;
    margin-left: 1.5rem;
    width: calc(100% - 1.5rem);
  }

  .toc-item.active {
    background: var(--accent-soft);
    color: var(--accent-strong);
    border-left: 2px solid var(--accent);
  }

  /* Highlight effect for clicked sections */
  .main-section.highlighted,
  .sub-section.highlighted,
  .sub-section2.highlighted {
    position: relative;
    border: 2px solid var(--accent);
    border-radius: var(--radius);
    padding: 0.5rem;
    margin: -0.5rem;
    box-shadow: 0 0 0 4px var(--accent-soft);
    transition: opacity 0.6s ease-out, border-color 0.6s ease-out, box-shadow 0.6s ease-out;
  }

  .main-section.highlighted.fading,
  .sub-section.highlighted.fading,
  .sub-section2.highlighted.fading {
    opacity: 0.3;
    border-color: color-mix(in oklab, var(--accent) 30%, transparent);
    box-shadow: 0 0 0 4px color-mix(in oklab, var(--accent-soft) 30%, transparent);
  }

  .content {
    border-radius: 24px;
    background: rgba(248,250,252,0.88);
    border: 1px solid rgba(226,232,240,0.9);
    box-shadow: var(--shadow);
    overflow: hidden;
    display: flex;
    flex-direction: column;
    height: 100%; /* Fill available space in #app */
  
    /* NEW: keep it within the space that remains after the sidebar */
    max-width: min(960px, calc(100vw - var(--toc-width) - 3.5rem));
    margin: 0 auto; /* center within the viewport */
  }

  .content-body {
    padding: 1.25rem 2rem 2rem;
    margin-inline: auto;
    overflow-y: auto;
    flex: 1; /* Fill available space in .content */
    min-height: 0; /* Allow flex item to shrink below content size */
    scroll-behavior: smooth;
  }

  .headline {
    font-size: 1.9rem;
    font-weight: 700;
    margin: 0 0 0.4rem;
    letter-spacing: 0.08em;
    text-transform: uppercase;
    color: var(--accent-strong);
  }

  .headline-sub {
    font-size: 0.9rem;
    color: var(--muted);
    margin-bottom: 1.5rem;
  }

  .badge-row {
    display: flex;
    flex-wrap: wrap;
    gap: 0.45rem;
    margin-bottom: 1.4rem;
  }

  .badge {
    font-size: 0.75rem;
    text-transform: uppercase;
    letter-spacing: 0.08em;
    padding: 0.1rem 0.5rem;
    border-radius: 999px;
    border: 1px solid rgba(148,163,184,0.8);
    color: var(--muted);
    background: rgba(15,23,42,0.02);
  }

  .badge.highlight {
    border-color: var(--accent);
    color: var(--accent-strong);
    background: var(--accent-soft);
  }

  .main-section {
    margin-top: 1.8rem;
    padding-top: 0.8rem;
    border-top: 1px solid rgba(209,213,219,0.7);
  }

  .main-section:first-of-type {
    border-top: none;
    padding-top: 0;
    margin-top: 0.8rem;
  }

  .main-section > h1 {
    font-size: 1.4rem;
    margin: 0 0 0.4rem;
  }

  .main-section > h2 {
    font-size: 1.3rem;
    margin: 0 0 0.4rem;
  }

  .sub-section,
  .sub-section2 {
    margin-top: 1.4rem;
  }

  .sub-section h2,
  .sub-section2 h2,
  .sub-section h3,
  .sub-section2 h3 {
    font-size: 1.1rem;
    margin: 0 0 0.25rem;
  }

  p {
    margin: 0.35rem 0;
    line-height: 1.5;
    font-size: 0.95rem;
    color: var(--text);
  }

  .muted {
    color: var(--muted);
    font-size: 0.9rem;
  }

  ul, ol {
    padding-left: 1.25rem;
    margin: 0.35rem 0 0.25rem;
  }

  li {
    margin: 0.18rem 0;
    font-size: 0.94rem;
  }

  .callout {
    margin: 0.9rem 0;
    padding: 0.75rem 0.9rem;
    border-radius: 0.7rem;
    background: rgba(249,250,251,0.8);
    border: 1px solid rgba(209,213,219,0.9);
    font-size: 0.9rem;
  }

  .callout strong {
    display: inline-block;
    margin-bottom: 0.15rem;
  }

  .callout.emphasis {
    border-color: var(--accent);
    background: var(--accent-soft);
  }

  .tagline {
    font-size: 1rem;
    margin-top: 0.3rem;
    color: var(--muted);
  }

  code {
    font-family: ui-monospace, SFMono-Regular, Menlo, Monaco, Consolas, "Liberation Mono", "Courier New", monospace;
    font-size: 0.85rem;
    background: rgba(15,23,42,0.04);
    padding: 0.05rem 0.25rem;
    border-radius: 0.25rem;
  }

  .quick-grid {
    display: grid;
    grid-template-columns: repeat(auto-fit,minmax(220px,1fr));
    gap: 0.8rem;
    margin-top: 0.6rem;
  }

  .quick-card {
    border-radius: 0.8rem;
    border: 1px solid rgba(209,213,219,0.9);
    background: rgba(249,250,251,0.92);
    padding: 0.7rem 0.8rem;
    font-size: 0.9rem;
  }

  .quick-card h3 {
    font-size: 0.95rem;
    margin: 0 0 0.25rem;
  }

  .label {
    display: inline-block;
    font-size: 0.72rem;
    font-weight: 600;
    letter-spacing: 0.08em;
    text-transform: uppercase;
    color: var(--muted);
    margin-bottom: 0.15rem;
  }

  .pill {
    display: inline-flex;
    align-items: center;
    gap: 0.3rem;
    font-size: 0.8rem;
    padding: 0.1rem 0.45rem;
    border-radius: 999px;
    background: rgba(15,23,42,0.03);
    border: 1px solid rgba(209,213,219,0.9);
  }

  .pill span {
    font-weight: 600;
  }

  .muted-list {
    font-size: 0.9rem;
    color: var(--muted);
  }

  .coalition-type {
    margin: 2rem 0;
    padding: 1rem 1.1rem;
    border-radius: 0.8rem;
    border: 1px solid rgba(209,213,219,0.9);
    background: rgba(249,250,251,0.92);
  }

  .coalition-type h3 {
    font-size: 1.05rem;
    margin: 0 0 0.5rem;
    color: var(--accent-strong);
    cursor: pointer;
    display: flex;
    align-items: center;
    gap: 0.5rem;
  }

  .coalition-type h3::before {
    content: '▼';
    font-size: 0.7rem;
    transition: transform 0.2s ease;
    color: var(--muted);
  }

  .coalition-type.collapsed h3::before {
    transform: rotate(-90deg);
  }

  .coalition-type > div {
    overflow: hidden;
    transition: max-height 0.3s ease, opacity 0.2s ease;
  }

  .coalition-type.collapsed > div {
    max-height: 0;
    opacity: 0;
    margin: 0;
    padding: 0;
  }

  .coalition-type:not(.collapsed) > div {
    max-height: 5000px;
    opacity: 1;
  }

  .coalition-type > div > p:first-of-type {
    margin-top: 0;
  }

  .coalition-type p:has(> strong:only-child) {
    margin-top: 0.6rem;
    margin-bottom: 0.25rem;
  }

  .coalition-type p:has(> strong:only-child) strong {
    font-size: 0.9rem;
    text-transform: uppercase;
    letter-spacing: 0.05em;
    color: var(--muted);
    font-weight: 600;
  }

  .coalition-type p:has(> strong:only-child):first-of-type {
    margin-top: 0;
  }

  @media (max-width: 980px) {
  #app {
    max-width: 1240px;
    padding: 0 0.75rem 2rem;
  }

  /* Hide sidebar toggle container on mobile - use header button */
  .sidebar-toggle-container {
    display: none;
  }

  .sidebar {
    position: fixed;
    top: 3.1rem;
    left: 0;
    width: 100%;
    max-height: calc(100vh - 3.3rem);
    border-radius: 0;
    border-left: none;
    border-right: none;
    transform: translateY(-120%);
    opacity: 0;
    pointer-events: none;
    transition: transform 0.2s ease-out, opacity 0.2s ease-out;
    z-index: 25;
  }

  .sidebar.open {
    transform: translateY(0);
    opacity: 1;
    pointer-events: auto;
  }

  .content {
    max-width: 100%;      /* full width again on mobile */
    height: 100%; /* Fill available space */
  }

  .content-body {
    padding: 1.4rem 1.4rem 1.8rem;
    flex: 1; /* Ensure it fills available space */
    min-height: 0;
  }

  .headbar {
    padding-inline: 0.9rem;
  }

  .brand span {
    display: none;
  }
}
  

 @media print {
  .no-print, .no-print * {
    display: none !important;
  }
}