/* Course-contents sidebar, TOP-style. Two presentations of one element:
   ≥1280px — a full-height rail docked to the viewport's left edge, full-bleed
   (NOT inside .container, like theodinproject.com); <1280px — an off-canvas
   drawer. Stages are native <details> accordions: the current one starts open.

   1280px is The Odin Project's single `xl` threshold: the left rail and the
   right TOC appear together only when the viewport fits all three columns
   (20 + ~44 + 15rem) — below it there's no half-docked middle zone. */
.lesson-layout {
  display: block;
}

@media (min-width: 1280px) {
  .lesson-layout {
    align-items: start;
    display: grid;
    grid-template-columns: 20rem minmax(0, 1fr) 15rem;
  }

  .lesson-sidebar {
    block-size: calc(100dvh - var(--header-height));
    inset-block-start: var(--header-height); /* clears the sticky header; matches its own natural flow position, so it locks in place with zero scroll creep */
    position: sticky;
    /* NOT overflow-y: auto here — it would clip the ::after divider below,
       which deliberately extends above this box's own top edge. Scrolling
       lives on .lesson-sidebar__scroll instead (see _sidebar.html.erb). */
  }

  /* The divider rides along with .lesson-sidebar itself (position: absolute
     against the sticky box, NOT position: fixed against the viewport — a
     fixed line has no notion of where the sidebar ends and used to bleed
     into the footer/subscribe sections past the end of the lesson). Stretched
     upward by --header-height so it still reaches the very top when the
     header auto-hides (header-reveal controller), without moving the
     sidebar's own box — the pseudo-element just extends past its top edge. */
  .lesson-sidebar::after {
    background-color: var(--color-subtle);
    block-size: calc(100% + var(--header-height));
    content: "";
    inline-size: 1px;
    inset-block-start: calc(-1 * var(--header-height));
    inset-inline-end: 0;
    pointer-events: none;
    position: absolute;
  }

  .lesson-sidebar__scroll {
    block-size: 100%;
    overflow-y: auto;
    overscroll-behavior: contain;
    padding-block: 1.5rem var(--block-space-double);
    padding-inline: 1.5rem 1.25rem;
  }

  .lesson-sidebar__close,
  .lesson-sidebar-backdrop {
    display: none;
  }
}

@media (max-width: 1279.98px) {
  .lesson-sidebar {
    background-color: var(--color-bg);
    border-inline-end: 1px solid var(--color-subtle);
    inline-size: min(21rem, 86vw);
    inset-block: 0;
    inset-inline-start: 0;
    position: fixed;
    transform: translateX(-100%);
    transition: transform 250ms ease;
    visibility: hidden;
    z-index: 60; /* above the sticky site header */
  }

  .lesson-sidebar__scroll {
    block-size: 100%;
    overflow-y: auto;
    overscroll-behavior: contain;
    padding: 1.25rem;
  }

  .lesson-layout--sidebar-open .lesson-sidebar {
    transform: none;
    visibility: visible;
  }

  .lesson-sidebar-backdrop {
    background-color: oklch(var(--lch-always-black) / 0.6);
    display: none;
    inset: 0;
    position: fixed;
    z-index: 50;
  }

  .lesson-layout--sidebar-open .lesson-sidebar-backdrop {
    display: block;
  }

  /* Page behind the drawer must not scroll. */
  body:has(.lesson-layout--sidebar-open) {
    overflow: hidden;
  }
}

/* The "open contents" affordance — unboxed, first on the breadcrumb row (it
   keeps its word: primary reader navigation earns text, not a frame). */
.lesson-contents-toggle {
  align-items: center;
  background: none;
  border: 0;
  color: var(--color-subtle-dark);
  cursor: pointer;
  display: inline-flex;
  flex-shrink: 0;
  font: inherit;
  font-size: 0.85rem;
  gap: 0.4em;
  padding: 0;
  transition: color 150ms ease;

  .icon {
    block-size: 1.1em;
    inline-size: 1.1em;
  }

  @media (any-hover: hover) {
    &:hover {
      color: var(--color-ink);
    }
  }

  /* Lives HERE, after the base rule: an earlier `display: none` would lose
     the same-specificity cascade to `display: inline-flex` below it. */
  @media (min-width: 1280px) {
    display: none;
  }
}

/* ── Head: back link, path title, progress ─────────────────────────── */
.lesson-sidebar__head {
  border-block-end: 1px solid var(--color-subtle);
  display: flex;
  flex-direction: column;
  gap: 0.75rem;
  margin-block-end: 0.75rem;
  padding-block-end: 1.25rem;
  position: relative;
}

.lesson-sidebar__back {
  align-items: center;
  color: var(--color-subtle-dark);
  display: inline-flex;
  font-size: 0.8rem;
  gap: 0.25rem;
  text-decoration: none;
  transition: color 150ms ease;

  .icon {
    block-size: 0.9em;
    inline-size: 0.9em;
  }

  @media (any-hover: hover) {
    &:hover {
      color: var(--color-ink);
    }
  }
}

.lesson-sidebar__path {
  color: var(--color-ink);
  font-family: var(--font-display);
  font-size: 1rem;
  font-weight: 700;
  text-decoration: none;

  @media (any-hover: hover) {
    &:hover {
      text-decoration: underline;
    }
  }
}

.lesson-sidebar__close {
  background: none;
  border: 0;
  color: var(--color-subtle-dark);
  cursor: pointer;
  inset-block-start: -0.25rem;
  inset-inline-end: 0;
  padding: 0.25rem;
  position: absolute;

  .icon {
    block-size: 1.25rem;
    inline-size: 1.25rem;
  }

  @media (any-hover: hover) {
    &:hover {
      color: var(--color-ink);
    }
  }
}

/* ── Stage accordion ───────────────────────────────────────────────── */
.lesson-sidebar__stage {
  border-block-end: 1px solid var(--color-subtle);
}

.lesson-sidebar__stage-summary {
  align-items: center;
  border-radius: 0.4em;
  cursor: pointer;
  display: flex;
  gap: var(--block-space-half);
  list-style: none;
  padding: 0.7rem 0.5rem;
  transition: background-color 150ms ease;

  &::-webkit-details-marker {
    display: none;
  }

  @media (any-hover: hover) {
    &:hover {
      background-color: var(--color-subtle-light);
    }
  }
}

.lesson-sidebar__stage-title {
  color: var(--color-ink);
  flex: 1;
  font-size: 0.85rem;
  font-weight: 600;
  min-inline-size: 0;
}

.lesson-sidebar__stage-count {
  color: var(--color-subtle-dark);
  font-size: 0.75rem;
  font-variant-numeric: tabular-nums;
}

.lesson-sidebar__chevron {
  color: var(--color-subtle-dark);
  display: inline-flex;
  transition: transform 200ms ease;

  .icon {
    block-size: 1rem;
    inline-size: 1rem;
  }
}

.lesson-sidebar__stage[open] > .lesson-sidebar__stage-summary .lesson-sidebar__chevron {
  transform: rotate(180deg);
}

/* ── Lesson rows ───────────────────────────────────────────────────── */
.lesson-sidebar__list {
  display: flex;
  flex-direction: column;
  gap: 0.125rem;
  list-style: none;
  margin-block: 0 var(--block-space-half);
  padding-inline-start: 0;
}

.lesson-sidebar__item {
  align-items: flex-start;
  border-radius: 0.4em;
  color: var(--color-subtle-dark);
  display: flex;
  font-size: 0.875rem;
  gap: var(--block-space-half);
  line-height: 1.3;
  padding: 0.45rem 0.5rem;
  text-decoration: none;
  transition: background-color 150ms ease, color 150ms ease;

  @media (any-hover: hover) {
    &:hover {
      background-color: var(--color-subtle-light);
      color: var(--color-ink);
    }
  }
}

/* "You are here" — filled row, ink text; the strongest state in the list. */
.lesson-sidebar__item[aria-current="page"] {
  background-color: var(--color-subtle);
  color: var(--color-ink);
  font-weight: 600;
}

.lesson-sidebar__kind {
  display: inline-flex;
  flex-shrink: 0;
  margin-block-start: 0.05rem;

  .icon {
    block-size: 1rem;
    inline-size: 1rem;
  }
}

.lesson-sidebar__item-title {
  flex: 1;
  min-inline-size: 0;
}

/* TOP-style completion mark on the right: the same solid check-circle,
   gray while pending, green once done. */
.lesson-sidebar__status {
  color: var(--color-subtle);
  display: inline-flex;
  flex-shrink: 0;
  margin-block-start: 0.05rem;
  transition: color 150ms ease;

  .icon {
    block-size: 1.05rem;
    inline-size: 1.05rem;
  }
}

.lesson-sidebar__item--done .lesson-sidebar__status {
  color: var(--color-positive);
}

/* ── Right rail: "В этой статье" (TOP's "Lesson contents") ──────────────
   The lesson's own table of contents — fixed sections + its ## headings.
   Wide screens only; sticky, with a left border rail and the current entry
   marked by the lesson-toc Stimulus controller (aria-current). */
.lesson-toc {
  display: none;
}

@media (min-width: 1280px) {
  .lesson-toc {
    display: block;
    font-family: "IBM Plex Mono", var(--font-mono);
    font-size: 0.8rem;
    inset-block-start: var(--header-height); /* clears the sticky header */
    max-block-size: calc(100dvh - var(--header-height));
    overflow-y: auto;
    overscroll-behavior: contain;
    padding-block: 2.5rem;
    padding-inline: 0.5rem 1.5rem;
    position: sticky;
  }
}

/* Icon-only, not the "В ЭТОЙ СТАТЬЕ" caption it replaced — the accessible
   name lives on .lesson-toc's own aria-label instead of visible text. */
.lesson-toc__title {
  --icon-size: 1.15rem;
  color: var(--color-subtle-dark);
  margin-block: 0 0.75rem;
}

.lesson-toc__list {
  list-style: none;
  margin-block: 0;
  padding-inline-start: 0;
}

.lesson-toc__link {
  border-inline-start: 2px solid var(--color-subtle);
  color: var(--color-subtle-dark);
  display: block;
  line-height: 1.35;
  padding: 0.3rem 0 0.3rem 0.85rem;
  text-decoration: none;
  transition: border-color 150ms ease, color 150ms ease;

  @media (any-hover: hover) {
    &:hover {
      color: var(--color-ink);
    }
  }
}

/* In-body ## headings sit one step in, under "Что изучить". */
.lesson-toc__link--sub {
  padding-inline-start: 1.85rem;
}

/* "Документы и ресурсы" is worth a reader's extra attention — weight only,
   one step past [aria-current]'s 600 so the two states stay distinguishable
   even when this section is the one being read. */
.lesson-toc__link--emphasis {
  font-weight: 700;
}

/* "You are reading this" — ink text + ink rail segment, same brightness
   hierarchy as the course sidebar's current item. */
.lesson-toc__link[aria-current] {
  border-inline-start-color: var(--color-ink);
  color: var(--color-ink);
  font-weight: 600;
}

/* ── Reading mode ──────────────────────────────────────────────────────
   Chrome off, words on. Toggled by the reading-mode Stimulus controller,
   persisted in a cookie so the server renders the next lesson already
   stripped. These rules live at the END of this file ON PURPOSE —
   `.lesson-layout--reading { display: block }` must out-cascade the
   same-specificity grid rules above (source order wins). */
body:has(.lesson-layout--reading) :is(.header, .support-cta, .footer) {
  display: none;
}

.lesson-layout--reading {
  display: block;

  .lesson-sidebar,
  .lesson-sidebar-backdrop,
  .lesson-contents-toggle,
  .lesson-toc,
  .breadcrumbs,
  .lesson-section-edit {
    display: none;
  }
}

/* The mode toggle: a quiet meta-row button normally; once reading, it becomes
   a fixed pill in the corner so leaving the mode never requires scrolling. */
.lesson-reading-toggle__enter,
.lesson-reading-toggle__exit {
  align-items: center;
  display: inline-flex;
  gap: var(--inline-space-half);
}

.lesson-reading-toggle__exit {
  display: none;
}

.lesson-layout--reading .lesson-reading-toggle__enter {
  display: none;
}

.lesson-layout--reading .lesson-reading-toggle__exit {
  display: inline-flex;
}

/* Icon-only corner exit, dimmed to the edge of visibility — it must not pull
   peripheral vision off the text, only be findable when looked for. Hover or
   keyboard focus raises it back to a full button. Esc works too. */
.lesson-layout--reading .lesson-reading-toggle {
  background-color: transparent;
  border: 1px solid transparent;
  border-radius: 50%;
  color: var(--color-subtle-dark);
  inset-block-start: var(--block-space);
  inset-inline-end: 1.5rem;
  padding: 0.6em;
  position: fixed;
  transition-duration: 200ms;
  transition-property: color, border-color, background-color;
  transition-timing-function: ease-out;
  z-index: 40;
}

.lesson-layout--reading .lesson-reading-toggle:focus-visible,
.lesson-layout--reading .lesson-reading-toggle:hover {
  background-color: var(--color-bg);
  border-color: var(--color-subtle);
  color: var(--color-ink);
}
