/* Shared by the Astro, React and Vanilla Sidebar variants. */

/*
 * Custom properties, read with a fallback so they can be set on the sidebar or on any ancestor (:root):
 *   --exo-sidebar-width    the desktop width. Default --exo-size-column-sidebar (300px).
 *   --exo-sidebar-top      how far from the top of the viewport it sticks, such as under a fixed app bar. Default 0.
 *   --exo-sidebar-height   the desktop height. Default the viewport height less --exo-sidebar-top.
 *   --exo-sidebar-padding  the padding, and the space between the header and the body's sections. Default --exo-space-5.
 *   --exo-sidebar-divider  the hairline color between the sidebar, its header, its sections and the page.
 */

/* An optional wrapper: the sidebar and the main column side by side, stacked at 768px and below.
   Other children (the main column) take the rest of the width. */
.exo-sidebar-layout {
  display: flex;
  min-height: 100vh;
  min-height: 100dvh;
}

.exo-sidebar-layout > :not(.exo-sidebar) {
  flex: 1;
  min-width: 0;
}

/* Desktop: a fixed-width column that sticks to the top of the viewport and scrolls on its own. */
.exo-sidebar {
  box-sizing: border-box;
  position: sticky;
  top: var(--exo-sidebar-top, 0px);
  display: flex;
  flex: none;
  flex-direction: column;
  gap: var(--exo-sidebar-padding, var(--exo-space-5, 16px));
  width: var(--exo-sidebar-width, var(--exo-size-column-sidebar, 300px));
  height: var(--exo-sidebar-height, calc(100vh - var(--exo-sidebar-top, 0px)));
  height: var(--exo-sidebar-height, calc(100dvh - var(--exo-sidebar-top, 0px)));
  padding: var(--exo-sidebar-padding, var(--exo-space-5, 16px));
  overflow-y: auto;
  overscroll-behavior: contain;
  font-family: var(--exo-font-family-sans, system-ui, sans-serif);
  color: var(--exo-color-text, #333333);
  background: var(--exo-color-bg-subtle, #f5f5f5);
  border-inline-end: var(--exo-border-width-1, 1px) solid
    var(--exo-sidebar-divider, color-mix(in srgb, var(--exo-color-text, #333333) 6%, transparent));
}

/* A sidebar after the main column (on the end side) draws its border on the start side instead. */
.exo-sidebar-layout > :not(.exo-sidebar) ~ .exo-sidebar {
  border-inline-end: 0;
  border-inline-start: var(--exo-border-width-1, 1px) solid
    var(--exo-sidebar-divider, color-mix(in srgb, var(--exo-color-text, #333333) 6%, transparent));
}

/* The header row: a wordmark at the start, controls such as a theme select at the end. */
.exo-sidebar__header {
  display: flex;
  flex: none;
  align-items: center;
  justify-content: space-between;
  gap: var(--exo-space-3, 8px);
  padding-bottom: var(--exo-sidebar-padding, var(--exo-space-5, 16px));
  border-bottom: var(--exo-border-width-1, 1px) solid
    var(--exo-sidebar-divider, color-mix(in srgb, var(--exo-color-text, #333333) 6%, transparent));
}

/* The body's children are sections (a nav Accordion, a block of controls), split by a hairline. Hidden ones are
   skipped, so filtering sections out leaves no stray line. */
.exo-sidebar__body {
  display: flex;
  flex-direction: column;
  gap: var(--exo-sidebar-padding, var(--exo-space-5, 16px));
  min-width: 0;
}

.exo-sidebar__body > :not([hidden]) ~ :not([hidden]) {
  padding-top: var(--exo-sidebar-padding, var(--exo-space-5, 16px));
  border-top: var(--exo-border-width-1, 1px) solid
    var(--exo-sidebar-divider, color-mix(in srgb, var(--exo-color-text, #333333) 6%, transparent));
}

/* A nav Accordion in the body (the usual way to group links) spaces and splits its sections like the
   body's own: by --exo-sidebar-padding, with the --exo-sidebar-divider hairline. */
.exo-sidebar__body > .exo-accordion--nav {
  --exo-accordion-nav-gap: var(--exo-sidebar-padding, var(--exo-space-5, 16px));
  --exo-accordion-nav-divider: var(--exo-sidebar-divider, color-mix(in srgb, var(--exo-color-text, #333333) 6%, transparent));
}

.exo-sidebar-link:focus-visible {
  outline: var(--exo-border-width-2, 2px) solid var(--exo-color-accent, #2f5fdb);
  outline-offset: calc(-1 * var(--exo-border-width-1, 1px));
}

/* Optional classes for a group of links: a bare list, and links with the hover tint and the
   current page marked by aria-current="page". */
.exo-sidebar-list {
  display: flex;
  flex-direction: column;
  gap: var(--exo-space-3, 8px);
  margin: 0;
  padding: 0;
  list-style: none;
}

.exo-sidebar-link {
  display: block;
  padding: var(--exo-space-1, 4px);
  font-size: var(--exo-font-size-sm, 14px);
  line-height: var(--exo-font-line-height-snug, 1.25);
  color: inherit;
  text-decoration: none;
  border-radius: var(--exo-radius-3, 6px);
}

.exo-sidebar-link:hover,
.exo-sidebar-link[aria-current="page"] {
  background: var(--exo-color-bg-hover, rgb(0 0 0 / 0.1));
}

/*
 * Stacked, at 768px and below: the layout becomes a column and the sidebar spans the width, above or
 * below the content (or split around it), at its natural height. Only here is the padding widened to
 * the device's safe areas (notches, rounded corners, the home indicator); on desktop the sidebar's
 * own padding already insets its contents. The safe areas are non-zero only with
 * <meta name="viewport" content="…, viewport-fit=cover">.
 */
@media (max-width: 768px) {
  .exo-sidebar-layout {
    flex-direction: column;
  }

  .exo-sidebar {
    position: static;
    width: auto;
    height: auto;
    overflow: visible;
    padding-right: max(var(--exo-sidebar-padding, var(--exo-space-5, 16px)), env(safe-area-inset-right, 0px));
    padding-left: max(var(--exo-sidebar-padding, var(--exo-space-5, 16px)), env(safe-area-inset-left, 0px));
  }

  .exo-sidebar,
  .exo-sidebar-layout > :not(.exo-sidebar) ~ .exo-sidebar {
    border-inline: 0;
  }

  .exo-sidebar--stack-above {
    order: -1;
    padding-top: max(var(--exo-sidebar-padding, var(--exo-space-5, 16px)), env(safe-area-inset-top, 0px));
    border-bottom: var(--exo-border-width-1, 1px) solid
      var(--exo-sidebar-divider, color-mix(in srgb, var(--exo-color-text, #333333) 6%, transparent));
  }

  .exo-sidebar--stack-below {
    order: 1;
    padding-bottom: max(var(--exo-sidebar-padding, var(--exo-space-5, 16px)), env(safe-area-inset-bottom, 0px));
    border-top: var(--exo-border-width-1, 1px) solid
      var(--exo-sidebar-divider, color-mix(in srgb, var(--exo-color-text, #333333) 6%, transparent));
  }

  /* Split: the <aside> steps out of the layout so its header and body become items of the stacked
     column, the header above the content and the body below it. Each gets the sidebar's surface. */
  .exo-sidebar--stack-split {
    display: contents;
  }

  .exo-sidebar--stack-split > .exo-sidebar__header,
  .exo-sidebar--stack-split > .exo-sidebar__body {
    padding-right: max(var(--exo-sidebar-padding, var(--exo-space-5, 16px)), env(safe-area-inset-right, 0px));
    padding-left: max(var(--exo-sidebar-padding, var(--exo-space-5, 16px)), env(safe-area-inset-left, 0px));
    background: var(--exo-color-bg-subtle, #f5f5f5);
  }

  .exo-sidebar--stack-split > .exo-sidebar__header {
    order: -1;
    padding-top: max(var(--exo-sidebar-padding, var(--exo-space-5, 16px)), env(safe-area-inset-top, 0px));
  }

  .exo-sidebar--stack-split > .exo-sidebar__body {
    order: 1;
    padding-top: var(--exo-sidebar-padding, var(--exo-space-5, 16px));
    padding-bottom: max(var(--exo-sidebar-padding, var(--exo-space-5, 16px)), env(safe-area-inset-bottom, 0px));
    border-top: var(--exo-border-width-1, 1px) solid
      var(--exo-sidebar-divider, color-mix(in srgb, var(--exo-color-text, #333333) 6%, transparent));
  }
}
