/* The docs (templates/layout.html and the docs pages): the site's reading style, from tokens.css,
   elements.css and site.css, with Workroom's yellow as the fill of its marks. */

:root {
  --docs-sidebar: 16rem;
  /* About 90 characters a line of page text, inside the content's padding. */
  --docs-content: 53rem;
  /* 3rem of it is the gap before On this page. */
  --docs-toc: 16rem;
}

/* Inside, so the ring doesn't cover the next of the sidebar's tight rows. */
.docs-sidebar :is(a, summary):focus-visible {
  outline-offset: -2px;
}

/* --- Shell: sidebar, content, On this page --- */

.docs-shell {
  display: grid;
  /* The content track is capped, not 1fr, so spare width goes to the outer margins rather than a
     gap between the content and On this page. */
  grid-template-columns: var(--docs-sidebar) minmax(0, var(--docs-content)) var(--docs-toc);
  justify-content: center;
  align-items: start;
  max-width: 90rem;
  margin: 0 auto;
}

:is(.docs-sidebar, .docs-toc) {
  position: sticky;
  top: var(--header);
  max-height: calc(100vh - var(--header));
  overflow-y: auto;
  overscroll-behavior: contain;
  scrollbar-width: thin;
}

.docs-sidebar {
  display: block;
  align-self: stretch;
  margin: 0;
  padding: calc(var(--space) * 1.5) var(--space) calc(var(--space) * 3);
  border-right: 1px solid var(--line-1);

  /* Each nav group is a <details>; its summary is the group's heading with a chevron. */
  details {
    margin: 0;
    padding: 0;
    background: none;
    border: 0;
  }

  details + details {
    margin-top: calc(var(--space) * 0.75);
  }

  summary {
    display: flex;
    align-items: center;
    justify-content: space-between;
    margin-left: -0.6rem;
    padding: 0.3rem 0.6rem;
    list-style: none;
    border-radius: var(--radius);

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

    &::after {
      width: 0.4rem;
      height: 0.4rem;
      border-right: 1.5px solid var(--text-3);
      border-bottom: 1.5px solid var(--text-3);
      transform: rotate(45deg) translateY(-0.1rem);
      transition: transform 0.15s ease-out;
      content: "";
    }

    &:hover {
      background: var(--bg-3);

      h2 {
        color: var(--text-1);
      }
    }
  }

  details:not([open]) > summary::after {
    transform: rotate(-45deg);
  }

  h2 {
    margin: 0;
    font-size: 0.85rem;
    font-weight: 650;
    letter-spacing: 0.04em;
    text-transform: uppercase;
    color: var(--text-3);
  }

  ul {
    display: block;
    list-style: none;
    margin: 0;
    padding: 0;
  }

  li + li {
    margin-top: 1px;
  }

  /* Indented under the group's heading, whose text sits 0.6rem left of the links' text. */
  a {
    display: block;
    padding: 0.2rem 0.6rem;
    color: var(--text-2);
    text-decoration: none;
    border-radius: var(--radius);

    /* Finger-sized rows on touch screens. */
    @media (pointer: coarse) {
      padding-block: 0.5rem;
    }

    &:hover {
      color: var(--text-1);
      background: var(--bg-3);
    }

    /* The current page: a lit room, the yellow as the mark's own square beside it. */
    &[aria-current="page"] {
      color: var(--text-1);
      font-weight: 600;
      background: var(--bg-3);
      box-shadow: inset 3px 0 0 var(--workroom-yellow);
    }
  }
}

/* Reading size: about 15px over the 13px root, so sizes inside are in em. */
.docs-content {
  min-width: 0;
  max-width: var(--docs-content);
  padding: calc(var(--space) * 2) calc(var(--space) * 2) calc(var(--space) * 4);
  font-size: 1.15rem;
  line-height: 1.55;

  > h1 {
    margin: 2rem 0 0.5rem;
    font-size: 2em;
    font-weight: 700;
    letter-spacing: -0.022em;
    line-height: 1.15;
  }

  .lede {
    margin: 0 0 calc(var(--space) * 1.5);
    font-size: 1.15em;
    color: var(--text-2);
  }
}

.docs-toc {
  /* Top matches .docs-content, so On this page lines up with the breadcrumbs. */
  padding: calc(var(--space) * 2) var(--space) calc(var(--space) * 2) calc(var(--space) * 4);

  h2 {
    margin: 0 0 0.5rem;
    font-size: 0.85rem;
    font-weight: 650;
    letter-spacing: 0.04em;
    text-transform: uppercase;
    color: var(--text-3);
  }

  ul {
    list-style: none;
    margin: 0;
    padding: 0;
    border-left: 1px solid var(--line-2);
  }

  li {
    margin: 0;

    &.level-3 a {
      padding-left: 1.5rem;
    }
  }

  /* The section being read gets the sidebar's yellow bar on the rail (docs-page.js sets it). */
  a {
    display: block;
    margin-left: -1px;
    padding: 0.2rem 0 0.2rem 0.75rem;
    color: var(--text-2);
    text-decoration: none;
    border-left: 2px solid transparent;
    transition:
      color 0.15s ease-out,
      border-color 0.15s ease-out;

    &:hover {
      color: var(--text-1);
    }

    &[aria-current="true"] {
      color: var(--text-1);
      border-left-color: var(--workroom-yellow);
    }
  }
}

.breadcrumbs {
  margin: 0;
  gap: 0.5rem;
  font-size: 0.875em;
  color: var(--text-3);

  a {
    color: var(--text-2);

    &:hover {
      color: var(--text-1);
    }
  }
}

/* --- The page's Markdown --- */

.prose {
  line-height: 1.65;

  /* Links read as links in running text: an underline in a wash of the link colour, which comes
     up full on hover. */
  a:not(.anchor, .nightly-badge) {
    text-decoration: underline;
    text-decoration-color: color-mix(in srgb, var(--link) 35%, transparent);
    text-decoration-thickness: 1px;
    text-underline-offset: 0.2em;
    transition: text-decoration-color 0.15s ease-out;

    &:hover {
      color: var(--link);
      text-decoration-color: currentColor;
    }
  }

  h2,
  h3,
  h4 {
    position: relative;
    margin: calc(var(--space) * 3) 0 var(--space);
    line-height: 1.3;
    letter-spacing: -0.01em;
  }

  /* A clear step between levels, over a page-wide rule for each section. */
  h2 {
    padding-top: calc(var(--space) * 1.25);
    font-size: 1.45em;
    border-top: 1px solid var(--line-1);
  }

  h3 {
    margin-top: calc(var(--space) * 2.25);
    font-size: 1.15em;
  }

  h4 {
    font-size: 1em;
    color: var(--text-2);
  }

  /* The heading's own link: a "#" that shows on hover, for copying a link to the section. */
  .anchor {
    margin-left: 0.4rem;
    color: var(--text-4);
    text-decoration: none;
    opacity: 0;
    transition: opacity 0.15s ease-out;

    &::before {
      content: "#";
    }

    &:hover {
      color: var(--link);
    }
  }

  :is(h2, h3, h4):hover .anchor,
  .anchor:focus-visible {
    opacity: 1;
  }

  li + li {
    margin-top: 0.3rem;
  }

  li > p {
    margin-bottom: 0.5rem;
  }

  /* The sans font at near text size: the mono font draws ⌘ ⌥ ⇧ ⌃ too small to read. */
  kbd {
    padding: 0.05em 0.4em;
    font-family: var(--font-sans);
    font-size: 0.95em;
    letter-spacing: 0.08em;
    color: var(--text-1);
    background: var(--bg-2);
    border-color: var(--line-3);
    border-bottom-width: 2px;
  }

  /* Code blocks are the terminal's dark surface (site.css), in either theme. */
  pre {
    position: relative;
    margin: 0 0 calc(var(--space) * 1.25);
    padding: 0.9rem 1.1rem;
    font-size: 0.85em;
    line-height: 1.6;
    border-radius: calc(var(--radius) * 1.5);
    scrollbar-color: var(--line-3) transparent;
  }

  /* docs-page.js adds it: it shows on hover, on the code's own surface. */
  .copy-code {
    position: absolute;
    top: 0.45rem;
    right: 0.45rem;
    min-height: 0;
    padding: 0.15rem 0.55rem;
    font-size: 0.75rem;
    color: var(--text-2);
    background: var(--bg-3);
    border: 1px solid var(--line-2);
    box-shadow: none;
    opacity: 0;
    transition: opacity 0.15s ease-out;

    &:hover:not(:disabled) {
      color: var(--text-1);
      filter: none;
    }
  }

  pre:hover .copy-code,
  .copy-code:focus-visible {
    opacity: 1;
  }

  @media (hover: none) {
    .copy-code {
      opacity: 1;
    }
  }

  .table-scroll {
    overflow-x: auto;
    margin: 0 0 calc(var(--space) * 1.25);

    table {
      margin: 0;
    }
  }

  /* Same size as the text, and flush with it at both edges. So no fills, which would end exactly
     at the text: the header row is set off by a firmer rule instead. */
  table {
    font-size: 0.95em;
  }

  td,
  th {
    vertical-align: top;

    &:first-child {
      padding-left: 0;
    }

    &:last-child {
      padding-right: 0;
    }
  }

  th {
    background: none;
    border-bottom-color: var(--line-3);
  }

  img {
    border: 1px solid var(--line-2);
    border-radius: var(--radius);
  }

  blockquote {
    margin-inline: 0;
  }

  /* GitHub-style alerts: > [!NOTE] and friends, each a wash of its role's colour. */
  .markdown-alert {
    --alert: var(--note);
    margin: calc(var(--space) * 1.25) 0;
    padding: 0.75rem 1rem;
    background: color-mix(in srgb, var(--alert) 7%, var(--bg-0));
    border: 1px solid color-mix(in srgb, var(--alert) 26%, transparent);
    border-radius: var(--radius);

    > :last-child {
      margin-bottom: 0;
    }
  }

  .markdown-alert-title {
    margin: 0 0 0.25rem;
    font-weight: 650;
    color: var(--alert);
  }

  .markdown-alert-tip {
    --alert: var(--tip);
  }

  .markdown-alert-important {
    --alert: var(--important);
  }

  .markdown-alert-warning {
    --alert: var(--warning);
  }

  .markdown-alert-caution {
    --alert: var(--danger);
  }
}

/* Syntax colours (#857's highlighter: syntect's scope atoms as `hl-` classes, nested as the
   scopes are). Coloured from the tokens, so code follows the surface it is on. Shell and JSON are
   what the docs highlight. */
.highlight {
  .hl-comment {
    color: var(--code-comment);
    font-style: italic;
  }

  :is(.hl-keyword, .hl-storage) {
    color: var(--code-keyword);
  }

  .hl-keyword.hl-operator {
    color: var(--text-2);
  }

  .hl-string {
    color: var(--code-string);
  }

  .hl-constant {
    color: var(--code-constant);
  }

  /* A command, or a function's name. */
  :is(.hl-entity.hl-name.hl-function, .hl-variable.hl-function) {
    color: var(--code-function);
  }

  :is(.hl-support, .hl-entity.hl-name.hl-tag, .hl-entity.hl-other.hl-attribute-name) {
    color: var(--code-builtin);
  }

  /* A shell option or variable. */
  :is(.hl-variable.hl-parameter, .hl-variable.hl-other) {
    color: var(--code-variable);
  }

  /* A JSON key, set apart from the strings it maps to. */
  .hl-key .hl-string {
    color: var(--code-builtin);
  }

  .hl-punctuation {
    color: var(--text-2);
  }

  /* A string's quotes, a variable's $ and a comment's # are part of what they mark. */
  :is(.hl-string, .hl-variable, .hl-comment, .hl-keyword, .hl-constant) .hl-punctuation {
    color: inherit;
  }

  :is(.hl-invalid, .hl-markup.hl-deleted) {
    color: var(--code-deleted);
  }

  .hl-markup.hl-inserted {
    color: var(--code-inserted);
  }
}

.nightly-badge {
  position: relative;
  display: inline-block;
  margin-left: 0.5rem;
  padding: 0.05rem 0.45rem;
  font-size: 0.7rem;
  font-weight: 700;
  letter-spacing: 0.04em;
  line-height: 1.5;
  text-transform: uppercase;
  vertical-align: middle;
  color: var(--on-workroom-yellow);
  text-decoration: none;
  background: var(--workroom-yellow);
  border-radius: calc(var(--radius) / 1.5);

  &:hover,
  &:focus-visible {
    color: var(--on-workroom-yellow);
    text-decoration: none;
  }

  /* What Nightly means, on hover and on keyboard focus. It sits inside the link, so the pointer
     can move onto it without it closing, and it is part of the link's name for a screen reader. */
  .nightly-tip {
    position: absolute;
    z-index: 10;
    top: calc(100% + 0.4rem);
    left: 0;
    width: max-content;
    max-width: 18rem;
    padding: 0.5rem 0.65rem;
    font-size: 0.85rem;
    font-weight: 400;
    line-height: 1.4;
    letter-spacing: normal;
    text-align: left;
    text-transform: none;
    white-space: normal;
    color: var(--text-1);
    background: var(--bg-2);
    border: 1px solid var(--line-2);
    border-radius: var(--radius);
    box-shadow: var(--shadow-dropdown);
    opacity: 0;
    visibility: hidden;
    translate: 0 -2px;
    transition:
      opacity 0.12s ease-out,
      translate 0.12s ease-out,
      visibility 0.12s;
  }

  &:hover .nightly-tip,
  &:focus-visible .nightly-tip {
    opacity: 1;
    visibility: visible;
    translate: 0 0;
  }
}

/* On a narrow screen the badge can sit near the right edge, so the tip hangs from the heading's
   own left edge and never runs off the page. */
@media (max-width: 40rem) {
  :is(h1, h2, h3, h4):has(> .nightly-badge) {
    position: relative;
  }

  .nightly-badge {
    position: static;

    .nightly-tip {
      max-width: 100%;
    }
  }
}

.nightly-note {
  padding: 0.6rem var(--space);
  color: var(--text-2);
  background: color-mix(in srgb, var(--workroom-yellow) 12%, var(--bg-0));
  border: 1px solid color-mix(in srgb, var(--workroom-yellow) 40%, transparent);
  border-radius: var(--radius);
}

/* --- The foot of a page --- */

.page-actions {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  justify-content: space-between;
  gap: var(--space);
  margin-top: calc(var(--space) * 3);
  padding-top: var(--space);
  border-top: 1px solid var(--line-1);
  font-size: 0.875em;
  color: var(--text-3);

  p {
    margin: 0;
  }

  div {
    display: flex;
    flex-wrap: wrap;
    align-items: center;
    gap: var(--space);
  }

  /* "Copy page" is a link-styled button, coloured like the links beside it. */
  button {
    color: var(--link);
    line-height: inherit;
  }
}

.pager {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: var(--space);
  margin-top: calc(var(--space) * 1.5);

  a {
    display: flex;
    flex-direction: column;
    gap: 0.1rem;
    padding: 0.75rem var(--space);
    text-decoration: none;
    border: 1px solid var(--line-2);
    border-radius: var(--radius);
    transition:
      border-color 0.15s ease-out,
      background-color 0.15s ease-out;

    &:hover {
      border-color: var(--line-3);
      background: var(--bg-2);
    }
  }

  a[rel="next"] {
    grid-column: 2;
    text-align: right;
  }

  small {
    color: var(--text-2);
    font-size: 0.85em;
  }

  span {
    color: var(--text-1);
    font-weight: 600;
  }
}

/* --- Home --- */

.docs-home {
  grid-column: 2 / -1;
  max-width: 64rem;

  /* The lit rooms behind the intro, off to the right of its words. */
  &.lit-rooms {
    --rooms-across: linear-gradient(to left, black 15%, transparent 40%);
    --rooms-down: linear-gradient(to bottom, black 10rem, transparent 26rem);

    &::before,
    &::after {
      mask-position:
        right top 1.5rem,
        0 0,
        0 0;
    }
  }

  > h1 {
    font-size: 2.4em;
  }

  > .prose {
    max-width: 40rem;
    font-size: 1.1em;
    color: var(--text-2);
  }

  .home-search {
    width: 100%;
    max-width: 36rem;
    min-height: 3rem;
    margin: var(--space) 0 calc(var(--space) * 2);
    padding: 0.5rem 0.6rem 0.5rem 1rem;
    font-size: 1.05em;
    box-shadow: var(--shadow-card);
  }

  .cards {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(15rem, 1fr));
    gap: var(--space);
  }

  .card {
    padding: var(--space) calc(var(--space) * 1.25);
    color: var(--text-1);
    text-decoration: none;
    background: var(--bg-2);
    border: 1px solid var(--line-2);
    border-radius: var(--radius);
    box-shadow: var(--shadow-card);
    transition: border-color 0.15s ease-out;

    &:hover {
      border-color: var(--line-3);
    }

    h2 {
      margin: 0 0 0.35rem;
      font-size: 1.15em;
    }

    p {
      margin: 0;
      color: var(--text-2);
    }
  }

  .sections {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(13rem, 1fr));
    gap: calc(var(--space) * 1.5);
    margin-top: calc(var(--space) * 3);

    section {
      margin: 0;
    }

    h2 {
      margin: 0 0 0.5rem;
      font-size: 0.85rem;
      font-weight: 650;
      letter-spacing: 0.04em;
      text-transform: uppercase;
      color: var(--text-3);
    }

    ul {
      list-style: none;
      margin: 0;
      padding: 0;
    }

    li {
      margin: 0.25rem 0;
    }
  }
}

/* --- Smaller screens --- */

@media (max-width: 75rem) {
  .docs-shell {
    grid-template-columns: var(--docs-sidebar) minmax(0, 1fr);
  }

  .docs-toc {
    display: none;
  }
}

@media (max-width: 48rem) {
  .docs-shell {
    grid-template-columns: minmax(0, 1fr);
  }

  .docs-home {
    grid-column: 1;
  }

  .site-header .sidebar-toggle {
    display: inline-flex;
    flex-shrink: 0;
    width: 44px;
    height: 44px;
    padding: 0;
    align-items: center;
    justify-content: center;
    color: var(--text-1);
    background: transparent;
    border: 1px solid var(--line-2);
    box-shadow: none;

    svg {
      width: 1.25rem;
      height: 1.25rem;
    }

    .menu-close {
      display: none;
    }

    &[aria-expanded="true"] {
      .menu-open {
        display: none;
      }

      .menu-close {
        display: block;
      }
    }
  }

  .docs-sidebar {
    position: fixed;
    inset: var(--header) 0 0 0;
    z-index: 15;
    display: none;
    max-height: none;
    background: var(--bg-1);
    border-right: 0;
  }

  :root[data-docs-sidebar-open] {
    overflow: hidden;

    .docs-sidebar {
      display: block;
    }
  }

  .docs-content {
    padding: var(--space) var(--space) calc(var(--space) * 3);

    > h1 {
      margin-top: 1rem;
      font-size: 1.75em;
    }
  }

  .pager {
    grid-template-columns: 1fr;

    a[rel="next"] {
      grid-column: 1;
    }
  }
}
