/**
 * Bengal SSG - Layout Compositions
 * Following CUBE CSS methodology
 *
 * These are structural compositions that define relationships
 * between elements. They should be layout-agnostic and reusable.
 */

/* ============================================
   THREE-COLUMN DOCUMENTATION LAYOUT
   App Shell Pattern - Full-width edge-to-edge
   ============================================ */

/**
 * Main documentation layout with sidebar navigation,
 * content area, and table of contents.
 *
 * App Shell Pattern:
 * - Sidebar touches left edge (with internal padding)
 * - TOC touches right edge (with internal padding)
 * - Content column is fluid, prose inside is constrained
 * - No outer gutters - feels like a native app
 *
 * Usage:
 * <div class="docs-layout">
 *   <aside class="docs-sidebar">...</aside>
 *   <main class="docs-main">...</main>
 *   <aside class="docs-toc">...</aside>
 * </div>
 */

.docs-children {
  display: flex;
  flex-direction: column;
  gap: var(--space-2);
}

.docs-layout--three-panel {
  display: grid;
  grid-template-columns: 1fr 2fr 1fr;
  gap: var(--space-6);
}

@media (max-width: 1024px) {
  .docs-layout--three-panel {
    grid-template-columns: 1fr;
  }
}

.docs-layout {
  display: grid;
  grid-template-areas:
    "sidebar content toc";
  grid-template-columns: 260px minmax(0, 1fr) 240px;
  gap: 0;
  /* Columns handle their own internal padding */
  max-width: none;
  /* Full-width app shell */
  padding-block: 0;
  align-items: start;
  width: 100%;
}

/* Container becomes full-width when it holds docs-layout (avoids 100vw scrollbar issue) */
.container:has(> .docs-layout) {
  max-width: 100%;
  padding-inline: 0;
}

/* Left Sidebar (Navigation) - touches left edge */
.docs-sidebar {
  grid-area: sidebar;
  position: sticky;
  top: 60px;
  /* Tighter to header */
  max-height: calc(100vh - 60px);
  overflow-y: auto;
  overflow-x: hidden; /* Prevent horizontal scroll from long nav item names */
  overscroll-behavior: contain;
  /* Internal padding - content doesn't touch edges */
  padding-block: var(--space-5) var(--space-8);
  padding-inline-start: var(--space-4);
  padding-inline-end: var(--space-3);
  /* Clean separation - border only, no background */
  border-right: 1px solid var(--color-border-light, var(--color-border));
}

/* Hide empty sidebars */
.docs-sidebar:empty,
.docs-toc:empty {
  display: none;
}

/* Adjust grid when sidebars are empty - center content */
.docs-layout:has(.docs-sidebar:empty):has(.docs-toc:empty) {
  grid-template-areas: "content";
  grid-template-columns: minmax(0, 1fr);
}

/* When only left sidebar is empty */
.docs-layout:has(.docs-sidebar:empty):not(:has(.docs-toc:empty)) {
  grid-template-areas: "content toc";
  grid-template-columns: minmax(0, 1fr) 240px;
}

/* When only right sidebar is empty */
.docs-layout:has(.docs-toc:empty):not(:has(.docs-sidebar:empty)) {
  grid-template-areas: "sidebar content";
  grid-template-columns: 260px minmax(0, 1fr);
}

/* Main Content */
.docs-main {
  grid-area: content;
  min-width: 0;
  /* Prevent grid blowout */
  /* Generous padding for breathing room */
  padding-block: var(--space-5) var(--space-8);
  padding-inline: var(--space-6);
}

/* Constrain prose content for readability */
.docs-main .prose {
  max-width: var(--prose-width, 75ch);
  margin-inline: auto;
}

/* ============================================
   AUTODOC PAGE TYPE CONSTRAINTS

   Page types (set via metadata.type):
   - autodoc-python: Python API docs
   - autodoc-cli: CLI reference
   - autodoc-rest: OpenAPI/REST docs
   - autodoc-hub: Hub pages aggregating API sections

   All use consistent prose-width for readability.
   ============================================ */

/* All autodoc reference pages: constrain at parent level for DRY.
   Both hero and article inherit the same max-width. */
body[data-type="autodoc-python"] .docs-main>*,
body[data-type="autodoc-cli"] .docs-main>*,
body[data-type="autodoc-rest"] .docs-main>* {
  max-width: var(--prose-width, 75ch);
  margin-inline: auto;
}

/* Page navigation should span full width to match docs layout */
body[data-type="autodoc-python"] .docs-main>.page-navigation,
body[data-type="autodoc-cli"] .docs-main>.page-navigation,
body[data-type="autodoc-rest"] .docs-main>.page-navigation {
  max-width: none;
}


/* Right Sidebar (TOC) - touches right edge */
.docs-toc {
  grid-area: toc;
  position: sticky;
  top: 60px;
  /* Match sidebar */
  max-height: calc(100vh - 60px);
  overflow-y: auto;
  overscroll-behavior: contain;
  /* Internal padding - content doesn't touch edges */
  padding-block: var(--space-5) var(--space-8);
  padding-inline-start: var(--space-4);
  padding-inline-end: var(--space-5);
  /* Clean separation - border only, no background */
  border-left: 1px solid var(--color-border-light, var(--color-border));
}

/* ============================================
   RESPONSIVE: ULTRA-WIDE (Constrain App Shell)
   ============================================ */

/* Ultra-wide screens (3xl breakpoint) */
@media (min-width: 1920px) {
  .docs-layout {
    /* Constrain the app shell on ultra-wide monitors */
    max-width: var(--container-3xl, 1920px);
    margin-inline: auto;
    /* Reset the breakout technique */
    width: 100%;
    margin-left: auto;
    margin-right: auto;
  }
}

/* ============================================
   RESPONSIVE: TABLET (Hide TOC)
   ============================================ */

/* Laptop and below (lg breakpoint) */
@media (max-width: 1024px) {
  .docs-layout {
    grid-template-areas:
      "sidebar content";
    grid-template-columns: 240px minmax(0, 1fr);
  }

  .docs-sidebar {
    padding-inline-start: var(--space-4);
  }

  .docs-main {
    grid-area: content;
    padding-inline: var(--space-5);
  }

  .docs-toc {
    display: none;
  }
}

/* ============================================
   RESPONSIVE: MOBILE (Sidebar Becomes Drawer)
   ============================================ */

/* Tablet and below (md breakpoint) */
@media (max-width: 768px) {
  .docs-layout {
    grid-template-areas:
      "content";
    grid-template-columns: 1fr;
    /* Reset breakout on mobile - use normal container flow */
    width: 100%;
    position: static;
    left: auto;
    right: auto;
    margin-left: 0;
    margin-right: 0;
  }

  .docs-main {
    grid-area: content;
    padding-block: var(--space-4) var(--space-6);
    padding-inline: var(--space-4);
  }

  .docs-sidebar {
    /* Convert to mobile drawer */
    position: fixed;
    top: 60px;
    /* Mobile header height */
    left: 0;
    bottom: 0;
    width: min(280px, 90vw);
    max-width: 90vw;
    max-height: calc(100vh - 60px);
    padding: var(--space-4);
    background: var(--color-bg-primary);
    border-right: 1px solid var(--color-border);
    box-shadow: var(--elevation-high);
    transform: translateX(-100%);
    transition: transform var(--transition-base);
    z-index: var(--z-sticky);
    overflow-y: auto;
  }

  .docs-sidebar[data-open] {
    transform: translateX(0);
  }

  .docs-toc {
    display: none;
  }
}

/* ============================================
   SIDEBAR OVERLAY (Mobile)
   ============================================ */

.docs-sidebar-overlay {
  display: none;
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background: var(--color-bg-overlay);
  z-index: calc(var(--z-sticky) - 1);
  opacity: 0;
  transition: opacity var(--transition-base);
}

.docs-sidebar-overlay.visible {
  display: block;
  opacity: 1;
}

@media (min-width: 769px) {
  .docs-sidebar-overlay {
    display: none !important;
  }
}

/* ============================================
   MOBILE SIDEBAR TOGGLE BUTTON
   ============================================ */

/* Removed floating sidebar toggle button - users can access sidebar via main hamburger menu */
.docs-sidebar-toggle {
  display: none !important;
}

/* ============================================
   SCROLLBAR STYLING (Sidebars)
   ============================================ */

.docs-sidebar::-webkit-scrollbar,
.docs-toc::-webkit-scrollbar {
  width: 6px;
}

.docs-sidebar::-webkit-scrollbar-track,
.docs-toc::-webkit-scrollbar-track {
  background: transparent;
}

.docs-sidebar::-webkit-scrollbar-thumb,
.docs-toc::-webkit-scrollbar-thumb {
  background: var(--color-border);
  border-radius: 3px;
}

.docs-sidebar::-webkit-scrollbar-thumb:hover,
.docs-toc::-webkit-scrollbar-thumb:hover {
  background: var(--color-text-tertiary);
}

/* ============================================
   STACK LAYOUT (Vertical Flow)
   ============================================ */

/**
 * Simple vertical stack with consistent gap
 *
 * Usage:
 * <div class="stack">
 *   <div>Item 1</div>
 *   <div>Item 2</div>
 * </div>
 */

.stack {
  display: flex;
  flex-direction: column;
  gap: var(--stack-gap, var(--space-component-gap));
}

.stack-small {
  --stack-gap: var(--size-2);
}

.stack-large {
  --stack-gap: var(--space-section-gap);
}

/* ============================================
   CLUSTER LAYOUT (Horizontal Flow)
   ============================================ */

/**
 * Horizontal layout with wrapping
 *
 * Usage:
 * <div class="cluster">
 *   <button>Action 1</button>
 *   <button>Action 2</button>
 * </div>
 */

.cluster {
  display: flex;
  flex-wrap: wrap;
  gap: var(--cluster-gap, var(--space-component-gap));
  align-items: var(--cluster-align, center);
  justify-content: var(--cluster-justify, flex-start);
}

.cluster-small {
  --cluster-gap: var(--size-2);
}

.cluster-large {
  --cluster-gap: var(--space-section-gap);
}

.cluster-center {
  --cluster-justify: center;
}

.cluster-between {
  --cluster-justify: space-between;
}

/* ============================================
   GRID LAYOUT (Auto-Fit Grid)
   ============================================ */

/**
 * Responsive grid that auto-fits items
 *
 * Usage:
 * <div class="grid">
 *   <div class="card">...</div>
 *   <div class="card">...</div>
 * </div>
 */

.grid {
  display: grid;
  grid-template-columns: repeat(auto-fit,
      minmax(var(--grid-min, 280px), 1fr));
  gap: var(--grid-gap, var(--space-component-gap));
}

.grid-2 {
  --grid-min: calc(50% - var(--grid-gap) / 2);
}

.grid-3 {
  --grid-min: calc(33.333% - var(--grid-gap) * 2 / 3);
}

.grid-4 {
  --grid-min: calc(25% - var(--grid-gap) * 3 / 4);
}

/* ============================================
   CENTER LAYOUT (Max-Width Container)
   ============================================ */

/**
 * Center content with max-width
 *
 * Usage:
 * <div class="center">
 *   <article>...</article>
 * </div>
 */

.center {
  box-sizing: content-box;
  max-width: var(--center-max-width, var(--container-xl));
  margin-inline: auto;
  padding-inline: var(--center-padding, var(--space-content-padding));
}

.center-narrow {
  --center-max-width: var(--container-md);
}

.center-wide {
  --center-max-width: var(--container-2xl);
}

/* ============================================
   SIDEBAR LAYOUT (Asymmetric)
   ============================================ */

/**
 * Sidebar layout with flexible content
 *
 * Usage:
 * <div class="with-sidebar">
 *   <aside>Sidebar</aside>
 *   <main>Content</main>
 * </div>
 */

.with-sidebar {
  display: flex;
  flex-wrap: wrap;
  gap: var(--sidebar-gap, var(--space-section-gap));
}

.with-sidebar>* {
  flex-grow: 1;
}

.with-sidebar> :first-child {
  flex-basis: var(--sidebar-width, 300px);
  flex-grow: 0;
}

.with-sidebar> :last-child {
  flex-basis: 0;
  flex-grow: 999;
  min-width: 50%;
}

/* ============================================
   PRINT STYLES
   ============================================ */

@media print {
  .docs-layout {
    display: block;
  }

  .docs-sidebar,
  .docs-toc,
  .docs-sidebar-toggle {
    display: none !important;
  }

  .docs-main {
    max-width: 100%;
  }
}
