/* The chrome every page shares (site/DESIGN.md): the header, the footer, the theme button, and
   the two Workroom touches any page can use, the lit rooms and the dark surface. Colour only
   from tokens.css; Workroom's yellow is only ever a fill. */

:root {
  --header: 3.5rem;
  scroll-padding-top: calc(var(--header) + var(--space));
}

@media (prefers-reduced-motion: no-preference) {
  :root {
    scroll-behavior: smooth;
  }
}

/* The site's transitions are small state changes, so a reader who asks for less motion gets
   the end state at once. */
@media (prefers-reduced-motion: reduce) {
  *,
  ::before,
  ::after {
    transition-duration: 0s !important;
  }
}

body {
  display: flex;
  flex-direction: column;
  min-width: 20rem;
  min-height: 100vh;
  min-height: 100dvh;
  overflow-x: clip;
}

/* The page's own part grows, so a short page still has its footer at the bottom. */
body > :is(main, .docs-shell) {
  flex: 1 0 auto;
  width: 100%;
}

.visually-hidden {
  position: absolute;
  width: 1px;
  height: 1px;
  overflow: hidden;
  clip-path: inset(50%);
  white-space: nowrap;
}

.skip-link {
  position: absolute;
  left: var(--space);
  top: -10rem;
  z-index: 30;
  padding: 0.5rem 0.75rem;
  background: var(--bg-2);
  border-radius: var(--radius);
  box-shadow: var(--shadow-dropdown);

  &:focus {
    top: 0.5rem;
  }
}

/* An image with a .dark twin: one of the pair shows, by the page's light or dark. */
.dark-only {
  display: none;
}

:root[data-theme="dark"] {
  .dark-only {
    display: revert;
  }

  .light-only {
    display: none;
  }
}

@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    .dark-only {
      display: revert;
    }

    .light-only {
      display: none;
    }
  }
}

/* --- The dark surface ---

   A terminal's surface, dark in either page theme: `color-scheme: dark` turns every token inside
   it to its dark value, so it needs no colours of its own. Inherited colours resolve where they
   are set, so it sets its own text colour again. On the dark page its surface would be nearly the
   page's, so it is lifted and given a firmer edge. Code blocks and the footer use it, and so does
   the landing page's terminal band. */

.dark-surface,
.site-footer,
.prose pre {
  color-scheme: dark;
  color: var(--text-1);
  background: var(--bg-1);
  border-color: var(--line-2);
}

@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) :is(.dark-surface, .site-footer, .prose pre) {
    background: color-mix(in srgb, var(--bg-1), var(--bg-3));
    border-color: var(--line-3);
  }
}

:root[data-theme="dark"] :is(.dark-surface, .site-footer, .prose pre) {
  background: color-mix(in srgb, var(--bg-1), var(--bg-3));
  border-color: var(--line-3);
}

/* --- Lit rooms ---

   A faint field of squares in the proportions of Workroom's 3x3 mark, a few of them lit in its
   yellow, like workrooms in use. Outlines (::before) and lit rooms (::after) are two layers so
   each takes its own token. A user sets --rooms-across and --rooms-down, the two gradients that
   keep the field off its text; both default to fading from the right edge. */

.lit-rooms {
  --rooms-across: linear-gradient(to left, black, transparent 55%);
  --rooms-down: linear-gradient(to bottom, black 40%, transparent);
  --rooms-lit: color-mix(in srgb, var(--workroom-yellow) 18%, transparent);
  position: relative;
  isolation: isolate;

  &::before,
  &::after {
    position: absolute;
    inset: 0;
    z-index: -1;
    pointer-events: none;
    content: "";
    mask-size:
      384px 256px,
      100% 100%,
      100% 100%;
    mask-repeat: repeat, no-repeat, no-repeat;
    mask-composite: intersect;
  }

  &::before {
    background: var(--line-2);
    mask-image: url(../images/lit-rooms.svg), var(--rooms-across), var(--rooms-down);
  }

  &::after {
    background: var(--rooms-lit);
    mask-image: url(../images/lit-rooms-lit.svg), var(--rooms-across), var(--rooms-down);
  }
}

/* The yellow carries further on the light page, so the dark page takes a stronger wash. */
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) .lit-rooms {
    --rooms-lit: color-mix(in srgb, var(--workroom-yellow) 28%, transparent);
  }
}

:root[data-theme="dark"] .lit-rooms {
  --rooms-lit: color-mix(in srgb, var(--workroom-yellow) 28%, transparent);
}

/* At phone width the margins are too narrow to keep the rooms off the words. */
@media (max-width: 48rem) {
  .lit-rooms::before,
  .lit-rooms::after {
    content: none;
  }
}

/* --- Header --- */

.site-header {
  position: sticky;
  top: 0;
  z-index: 20;
  display: flex;
  align-items: center;
  gap: 0.75rem;
  height: var(--header);
  padding: 0 var(--space);
  background: color-mix(in srgb, var(--bg-1) 88%, transparent);
  border-bottom: 1px solid var(--line-2);
  backdrop-filter: saturate(1.4) blur(12px);

  a {
    color: var(--text-1);
    text-decoration: none;
  }

  .brand {
    display: flex;
    flex-shrink: 0;
    align-items: center;
    gap: 0.5rem;
    font-size: 1.05rem;
    font-weight: 650;
    letter-spacing: -0.01em;

    img {
      flex-shrink: 0;
      border-radius: calc(var(--radius) * 1.25);
    }
  }

  .brand-docs {
    padding: 0.3rem 0.5rem;
    color: var(--text-2);
    border-radius: var(--radius);

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

  /* The hairline between the wordmark and Docs: a border, not a "|", so it isn't read aloud. */
  .brand + .brand-docs {
    position: relative;
    margin-left: 0.5rem;

    &::before {
      position: absolute;
      top: 0.35rem;
      bottom: 0.35rem;
      left: -0.6rem;
      border-left: 1px solid var(--line-3);
      content: "";
    }
  }

  .search-button {
    margin-left: auto;
  }

  .icon-button {
    display: inline-flex;
    flex-shrink: 0;
    width: 2.25rem;
    height: 2.25rem;
    min-height: 0;
    padding: 0;
    align-items: center;
    justify-content: center;
    color: var(--text-2);
    background: transparent;
    border: 0;
    border-radius: var(--radius);
    box-shadow: none;

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

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

  /* Subtly highlighted: a wash of Workroom's yellow, which is only ever a fill. */
  .download {
    display: inline-flex;
    flex-shrink: 0;
    align-items: center;
    gap: 0.4rem;
    min-height: 2.25rem;
    padding: 0 0.75rem;
    font-size: 0.95rem;
    font-weight: 550;
    white-space: nowrap;
    background: color-mix(in srgb, var(--workroom-yellow) 22%, transparent);
    border: 1px solid color-mix(in srgb, var(--workroom-yellow) 55%, transparent);
    border-radius: var(--radius);
    transition: background-color 0.15s ease-out;

    &:hover {
      background: color-mix(in srgb, var(--workroom-yellow) 40%, transparent);
    }

    svg {
      flex-shrink: 0;
      width: 1rem;
      height: 1rem;
      fill: currentColor;
    }
  }
}

/* The theme button holds all three icons and shows the current choice's; CSS picks it, so it is
   right before any script runs. */
[data-site-theme] svg {
  display: none;
}

:root:not([data-theme]) [data-site-theme] .theme-auto,
:root[data-theme="light"] [data-site-theme] .theme-light,
:root[data-theme="dark"] [data-site-theme] .theme-dark {
  display: block;
}

/* The search box's face, in the header and wherever a page offers search (Lucide's search
   icon, ISC). */
:is(.search-button, .home-search) {
  display: inline-flex;
  align-items: center;
  justify-content: space-between;
  gap: 1.5rem;
  min-width: 14rem;
  min-height: 2.25rem;
  padding: 0.3rem 0.35rem 0.3rem 0.75rem;
  color: var(--text-2);
  font: inherit;
  text-align: left;
  background: var(--bg-2);
  border: 1px solid var(--line-2);
  border-radius: var(--radius);
  box-shadow: none;
  cursor: pointer;

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

  > span {
    display: inline-flex;
    align-items: center;
    gap: 0.5rem;
  }

  > span::before {
    width: 0.95rem;
    height: 0.95rem;
    background: currentColor;
    mask: url(../images/search.svg) center / contain no-repeat;
    content: "";
  }

  kbd {
    padding: 0.05rem 0.4rem;
    font-family: var(--font-sans);
    font-size: 0.85rem;
    color: var(--text-3);
    background: var(--bg-3);
  }
}

.sidebar-toggle {
  display: none;
}

/* --- Footer --- */

.site-footer {
  border-top: 1px solid var(--line-2);
  font-size: 1rem;

  /* The words stack on the left; the lit rooms take the right. 85rem is the docs' three
     columns, so the mark lines up with the sidebar's text above it. */
  > div {
    display: grid;
    justify-items: start;
    gap: 1.1rem;
    max-width: 85rem;
    margin: 0 auto;
    padding: calc(var(--space) * 2.5) var(--space) calc(var(--space) * 2.75);
  }

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

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

  .brand {
    display: inline-flex;
    align-items: center;
    gap: 0.5rem;
    color: var(--text-1);
    font-weight: 650;
    font-size: 1.05rem;

    img {
      border-radius: var(--radius);
    }
  }

  nav {
    gap: 0.25rem 1.25rem;
    margin: 0;
  }

  /* Develop with Style's wordmark, as its own sites carry it: bold mono, "style" on its pink. */
  .dws {
    margin-top: 0.4rem;
    color: var(--text-2);
    font-family: var(--font-mono);
    font-size: 0.85rem;
    font-weight: 700;
    white-space: nowrap;

    i {
      font-style: normal;
    }

    em {
      padding: 0 0.25em;
      color: var(--on-dws-pink);
      font-style: normal;
      background: var(--dws-pink);
    }

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

/* The footer's rooms fill its right half, clear of the words on the left, with their first row
   level with the mark. Always on the dark surface, so one wash in either theme (`:root`
   outranks the dark page's wash above). */
:root .site-footer.lit-rooms {
  --rooms-across: linear-gradient(to left, black 10%, transparent 50%);
  --rooms-down: linear-gradient(to bottom, black 60%, transparent);
  --rooms-lit: color-mix(in srgb, var(--workroom-yellow) 22%, transparent);

  &::before,
  &::after {
    top: calc(var(--space) * 2.5 - 8px);
    mask-position:
      right -8px top,
      0 0,
      0 0;
  }
}

/* --- Search: the header's search opens it on every page --- */

.search-dialog {
  width: min(40rem, calc(100vw - 2rem));
  margin-top: 10vh;
  padding: 0;
  overflow: hidden;

  form {
    margin: 0;
  }

  input {
    width: 100%;
    padding: 1rem 1.1rem;
    font-size: 1.1rem;
    border: 0;
    border-bottom: 1px solid var(--line-2);
    border-radius: 0;
    background: transparent;

    &:focus-visible {
      outline: none;
    }

    /* The browser's own clear button is blue, off the palette; Esc clears the box. */
    &::-webkit-search-cancel-button {
      appearance: none;
    }
  }

  ul {
    list-style: none;
    margin: 0;
    padding: 0.4rem;
    max-height: min(60vh, 28rem);
    overflow-y: auto;
    /* The docs pages' reading size (.docs-content). */
    font-size: 1.15rem;

    &:empty {
      display: none;
    }
  }

  li a {
    display: flex;
    flex-direction: column;
    gap: 0.15rem;
    padding: 0.55rem 0.7rem;
    color: var(--text-1);
    text-decoration: none;
    border-radius: var(--radius);
  }

  li[aria-selected="true"] a,
  li a:hover {
    background: var(--bg-3);
  }

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

  .search-empty {
    padding: 0.75rem;
    color: var(--text-2);
  }

  .search-hint {
    margin: 0;
    padding: 0.5rem 1rem;
    font-size: 0.85rem;
    color: var(--text-2);
    border-top: 1px solid var(--line-1);

    /* As in the prose: the mono font draws ↑ ↓ ↵ too small to read. */
    kbd {
      font-family: var(--font-sans);
      font-size: 0.95em;
    }
  }
}

/* --- Small screens --- */

@media (max-width: 48rem) {
  /* Nor arrow keys or Esc. */
  .search-dialog .search-hint {
    display: none;
  }

  .site-header {
    gap: 0.5rem;
    padding: 0 0.75rem;

    .brand-docs,
    .github,
    .search-button kbd {
      display: none;
    }

    .search-button {
      min-width: 0;
      margin-left: auto;
      padding-right: 0.75rem;
    }

    /* Finger-sized. Download is its icon alone, leaving room for "Search": a phone can't
       install a Mac app anyway. */
    :is(.search-button, .download, .icon-button) {
      min-width: 44px;
      min-height: 44px;
    }

    .icon-button {
      width: 44px;
      height: 44px;
    }

    .download {
      justify-content: center;
      padding: 0;

      span {
        position: absolute;
        width: 1px;
        height: 1px;
        overflow: hidden;
        clip-path: inset(50%);
        white-space: nowrap;
      }
    }

    /* "Search the docs" is too long here. */
    .search-button .search-label-long {
      display: none;
    }
  }

  .site-footer > div {
    padding: calc(var(--space) * 2) var(--space) calc(var(--space) * 2.5);
  }
}

/* Finger-sized footer links on touch screens. */
@media (pointer: coarse) {
  .site-footer nav a {
    display: inline-flex;
    align-items: center;
    min-height: 44px;
  }

  .site-footer > div {
    gap: 0.5rem;
  }
}

@media (min-width: 48.001rem) {
  .search-label-short {
    display: none;
  }
}

/* The narrowest phones (320px) keep the mark and drop the wordmark beside it. */
@media (max-width: 23rem) {
  .site-header .brand span {
    position: absolute;
    width: 1px;
    height: 1px;
    overflow: hidden;
    clip-path: inset(50%);
    white-space: nowrap;
  }
}
