/* ---------- */
/* Typography */
/* ---------- */

/* Sprig Sans, normal */
@font-face {
  font-family: "Sprig Sans";
  font-style: normal;
  font-display: swap;
  font-weight: 400;
  src: url(/fonts/sprig-sans-regular.woff2) format("woff2");
}

/* Sprig Sans, bold */
@font-face {
  font-family: "Sprig Sans";
  font-style: normal;
  font-display: swap;
  font-weight: 700;
  src: url(/fonts/sprig-sans-bold.woff2) format("woff2");
}

/* Monospace, normal */
@font-face {
  font-family: "iA Writer Mono";
  font-style: normal;
  font-display: swap;
  font-weight: 400;
  src: url(https://cdn.jsdelivr.net/fontsource/fonts/ia-writer-mono@5.3.0/latin-400-normal.woff2)
    format("woff2");
}

/* --------------------------------- */
/* Reset, custom properties, globals */
/* --------------------------------- */

*,
*::before,
*::after {
  box-sizing: border-box;
}

:root {
  --black: #000;
  --white: #fff;
  --beige: #f8f4f2;
  --green: #9bab7f;
  --orange: #b27a59;
  --purple: #8f7fab;
  --blue: #0e99e9;

  --sprig-sans: "Sprig Sans", system-ui, sans-serif;
  --writer-mono: "iA Writer Mono", Menlo, monospace;

  --content-max-width: 760px;
}

body {
  margin: 0;
  line-height: 1.5;
  font-family: var(--sprig-sans);
  color: var(--black);
  background-color: var(--white);
  -webkit-font-smoothing: antialiased;

  ::selection {
    color: var(--white);
    background: var(--purple);
  }
}

h1,
h2,
h3,
h4,
h5,
h6 {
  text-wrap: balance;
  overflow-wrap: break-word;
}

p {
  text-wrap: pretty;
  overflow-wrap: break-word;
}

img,
picture,
video,
canvas,
svg {
  display: block;
  max-width: 100%;
}

input,
button,
textarea,
select {
  font: inherit;
}

/* 1. Green for internal docs links */
/* 2. Blue for val.town and val.run links */
/* 3. Orange for any other external links */
a {
  --link: var(--green);
  color: var(--black);
  text-decoration-color: var(--link);
  text-underline-offset: 2px;
  border-radius: 2px;

  &[href^="http://"],
  &[href^="https://"] {
    --link: var(--orange);
  }

  &[href*="//val.town"],
  &[href*=".val.town"],
  &[href*="//val.run"],
  &[href*=".val.run"],
  &[href*="@val.town"] {
    --link: var(--blue);
  }

  &:hover {
    text-decoration: none;
    color: var(--link);
    --highlight: color-mix(in srgb, var(--link) 30%, var(--white));
    background: var(--highlight);
    outline: 2px solid var(--highlight);
  }
}

blockquote {
  margin: 0;
  padding-inline: 16px;
  padding-block: 4px;
  border-left: 2px solid var(--orange);
  border-top-right-radius: 4px;
  border-bottom-right-radius: 4px;
  background: color-mix(in srgb, var(--orange) 30%, var(--white));
}

table {
  width: 100%;
  border-collapse: collapse;
  font-size: calc(14 / 16 * 1rem);

  & th,
  & td {
    padding: 0.5rem;
    text-align: left;
    vertical-align: top;
    border-bottom: 1px solid var(--beige);
  }

  & th {
    font-weight: 600;
  }
}

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

code {
  font-family: var(--writer-mono);
  font-size: 0.9rem;
  padding: 0.1em 0.3em;
  background-color: var(--beige);
  border-radius: 4px;
}

pre {
  font-family: var(--writer-mono);
  padding: 16px;
  overflow-x: auto;
  background-color: var(--beige);
  border-radius: 4px;

  code {
    background: none;
    padding: 0;
  }
}

.code-block {
  position: relative;
}

.code-block .copy-code {
  position: absolute;
  top: 8px;
  right: 8px;
  padding: 2px 8px;
  font-size: calc(12 / 16 * 1rem);
  background: var(--white);
  border: 1px solid var(--beige);
  border-radius: 4px;
  cursor: pointer;
  opacity: 0;
}

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

.code-block .copy-code:hover {
  color: var(--orange);
  border-color: color-mix(in srgb, var(--orange) 30%, var(--white));
}

.code-block .copy-code.copied {
  color: var(--green);
  border-color: color-mix(in srgb, var(--green) 30%, var(--white));
  opacity: 1;
}

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

hr {
  border-style: solid;
  border-color: var(--beige);
  margin-block: 16px;
}

/* ------------------- */
/* Syntax highlighting */
/* ------------------- */

.hljs {
  color: var(--black);
  background: transparent;
}

.hljs-comment,
.hljs-quote,
.hljs-meta {
  color: color-mix(in srgb, var(--black) 50%, var(--white));
  font-style: italic;
}

.hljs-keyword,
.hljs-selector-tag,
.hljs-name {
  color: color-mix(in srgb, var(--purple) 80%, var(--black));
}

.hljs-built_in,
.hljs-type,
.hljs-title,
.hljs-title.function_,
.hljs-title.class_,
.hljs-section {
  color: color-mix(in srgb, var(--blue) 80%, var(--black));
}

.hljs-string,
.hljs-regexp,
.hljs-symbol,
.hljs-addition {
  color: color-mix(in srgb, var(--green) 80%, var(--black));
}

.hljs-number,
.hljs-literal,
.hljs-attr,
.hljs-attribute,
.hljs-property,
.hljs-selector-attr,
.hljs-selector-pseudo,
.hljs-deletion {
  color: color-mix(in srgb, var(--orange) 80%, var(--black));
}

.hljs-emphasis {
  font-style: italic;
}

.hljs-strong {
  font-weight: 700;
}


#content {
  grid-area: main;
  min-width: 0;
  width: 100%;
  max-width: var(--content-max-width);
  padding: 20px 80px;
  border-right: 2px solid var(--beige);

  .table-scroll {
    max-width: 100%;
    overflow-x: auto;

    table {
      min-width: 36rem;
    }

    th,
    td {
      overflow-wrap: anywhere;
    }
  }

  menu {
    width: 100%;
    padding: unset;
    margin: unset;
    margin-left: 60px;
    display: flex;
    justify-content: flex-end;
    align-items: flex-end;
    gap: 8px;
    font-size: calc(14 / 16 * 1rem);

    button#copy {
      background: var(--beige);
      border: 2px solid var(--beige);
      padding: 4px 8px;
      border-radius: 4px;
      transition: color 0.15s, background 0.15s, border-color 0.15s;

      &:hover {
        color: var(--orange);
        --light-orange: color-mix(in srgb, var(--orange) 30%, var(--white));
        background: var(--light-orange);
        border: 2px solid var(--light-orange);
        outline: none;
      }

      &.copied {
        color: var(--green);
        --light-green: color-mix(in srgb, var(--green) 30%, var(--white));
        background: var(--light-green);
        border: 2px solid var(--light-green);
      }
    }

    a[href="/source"] {
      text-decoration: none;
      padding: 4px 8px;
      border: 2px solid var(--beige);
      border-radius: 4px;

      &:hover {
        color: var(--blue);
        --light-blue: color-mix(in srgb, var(--blue) 30%, var(--white));
        background: var(--light-blue);
        border: 2px solid var(--light-blue);
        outline: none;
      }
    }
  }

  @media (max-width: 768px) {
    & {
      padding-inline: 20px;
    }
    menu {
      margin-left: 0;
      justify-content: flex-start;
    }
  }

  @media (max-width: 500px) {
    menu {
      flex-direction: column;
    }
  }

  iframe.val-embed {
    display: block;
    width: 100%;
    max-width: 100%;
    height: 400px;
    margin: 20px 0;
    border: 1px solid var(--beige);
    border-radius: 8px;
  }

  .permalink {
    color: inherit;
    text-decoration: none;

    &:hover {
      color: inherit;
      background: none;
      outline: none;
      text-decoration: underline;
      text-decoration-color: var(--green);

      .permalink-hash {
        opacity: 1;
      }
    }
  }

  .permalink-hash {
    display: inline-block;
    margin-left: 0.3em;
    color: var(--green);
    opacity: 0;
    transition: opacity 0.15s;
  }

  .last-updated {
    margin-top: 40px;
    padding-top: 16px;
    font-size: calc(14 / 16 * 1rem);
    opacity: 0.7;
  }
}


#footer {
  grid-area: footer;
  max-width: var(--content-max-width);
  padding: 16px 24px;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  border-top: 2px solid var(--beige);
  border-right: 2px solid var(--beige);
  font-style: italic;

  svg {
    flex-shrink: 0;
    transform: skewX(-12deg);
    transform-origin: center;
  }

  @media (max-width: 600px) {
    span {
      display: none;
    }

    a {
      text-transform: capitalize;
    }
  }
}


#header {
  grid-area: header;
  position: sticky;
  top: 0;
  z-index: 10;
  height: var(--header-h);
  display: flex;
  padding: 16px 20px;
  background: var(--white);
  border-bottom: 2px solid var(--beige);

  button {
    padding: unset;
    background: unset;
    border: unset;
    margin-left: -4px;
    margin-right: 4px;
    margin-top: 4px;
    display: inline-flex;
    align-items: center;
    border-radius: 4px;
    cursor: pointer;

    &:hover {
      background: var(--beige);
    }

    &[popovertarget] {
      display: none;

      @media (max-width: 768px) {
        display: inline-flex;
      }
    }
  }
}


body {
  --sidebar-w: 240px;
  --toc-w: 260px;
  --content-w: 760px;
  --header-h: 72px;
  min-height: 100vh;
  display: grid;
  grid-template-rows: auto auto 1fr auto;
  grid-template-columns:
    var(--sidebar-w)
    minmax(0, var(--content-w)) var(--toc-w) 1fr;
  grid-template-areas:
    "announcement announcement announcement announcement"
    "header  header header  header"
    "sidebar main   toc     ."
    "sidebar footer toc     .";
}

.migration-announcement {
  grid-area: announcement;
  padding: 11px 24px;
  border-bottom: 1px solid #ddd2e9;
  background: #eee8f5;
  color: #362449;
  text-align: center;
  font-size: 14px;
  line-height: 22px;

  a {
    margin-left: 6px;
    color: inherit;
    font-weight: 600;
    text-decoration: underline;
    text-underline-offset: 3px;
  }
}

@media (max-width: 1024px) {
  body {
    --toc-w: 0px;
  }
}

@media (max-width: 768px) {
  body {
    grid-template-columns: minmax(0, 1fr);
    grid-template-areas:
      "announcement"
      "header"
      "main"
      "footer";
  }
}

@media (min-width: 641px) {
  html.sidebar-collapsed body {
    --sidebar-w: 56px;
  }

  html.sidebar-collapsed #sidebar .nav-scroll {
    display: none;
  }
}


#lockup {
  display: flex;
  align-items: center;

  .logo {
    background: unset;
    margin-right: 12px;

    &:hover {
      color: var(--blue);
      background: revert;
      outline: revert;
    }

    svg[data-logo="mobile"] {
      display: none;
    }

    svg[data-logo="desktop"] {
      display: block;
    }

    @media (max-width: 768px) {
      svg[data-logo="mobile"] {
        display: block;
      }

      svg[data-logo="desktop"] {
        display: none;
      }
    }
  }

  .slash {
    margin-top: -4px;
  }

  .docs {
    background: unset;
    margin-top: -8px;
    margin-left: 6px;

    &:hover {
      color: var(--green);
      background: revert;
      outline: revert;
    }
  }
}


/*
 * Top-of-page progress bar for client-side navigation. Hidden by default; the
 * router only reveals it when a page takes longer than a moment to arrive, so
 * cached and prefetched clicks stay silent.
 */
#nav-progress {
  position: fixed;
  top: 0;
  left: 0;
  z-index: 20; /* above the sticky #header */
  width: 0;
  height: 2px;
  background: var(--purple);
  opacity: 0;
  pointer-events: none;
}

/*
 * Creeps toward 80% and stalls there — it can't know the real percentage, only
 * that something is still in flight.
 */
#nav-progress.loading {
  width: 80%;
  opacity: 1;
  transition: width 4s cubic-bezier(0.1, 0.85, 0.2, 1);
}

#nav-progress.done {
  width: 100%;
  opacity: 0;
  transition:
    width 0.15s ease-out,
    opacity 0.25s ease-out 0.15s;
}

@media (prefers-reduced-motion: reduce) {
  #nav-progress.loading,
  #nav-progress.done {
    transition: opacity 0.1s linear;
  }
}


#search-trigger {
  width: 100%;
  height: 33px;
  margin-bottom: 16px;
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 4px 8px;
  color: color-mix(in srgb, var(--black) 55%, var(--white));
  background: var(--white);
  border: 2px solid var(--beige);
  border-radius: 4px;
  cursor: pointer;
  font-size: calc(14 / 16 * 1rem);
  text-align: left;

  &:hover {
    border-color: var(--green);
    color: var(--black);
  }

  span {
    flex: 1;
  }

  kbd {
    font-family: var(--mono);
    font-size: calc(12 / 16 * 1rem);
    padding-inline: 4px;
    background: var(--beige);
    border-radius: 4px;
  }
}

#search-dialog {
  width: min(600px, calc(100vw - 32px));
  margin-top: 12vh;
  margin-inline: auto;
  padding: 0;
  border: 2px solid var(--beige);
  border-radius: 10px;
  background: var(--white);

  &::backdrop {
    background: color-mix(in srgb, var(--black) 35%, transparent);
  }
}

.search-box {
  display: flex;
  flex-direction: column;
}

#search-input {
  width: 100%;
  padding: 16px;
  font-size: calc(18 / 16 * 1rem);
  color: var(--black);
  background: var(--white);
  border: none;
  border-bottom: 2px solid var(--beige);
  border-radius: 10px 10px 0 0;
  outline: none;

  &::placeholder {
    color: color-mix(in srgb, var(--black) 45%, var(--white));
  }
}

#search-results {
  list-style: none;
  margin: 0;
  padding: 8px;
  max-height: 60vh;
  overflow-y: auto;

  &:empty {
    padding: 0;
  }

  li a {
    display: flex;
    flex-direction: column;
    gap: 2px;
    padding: 10px 12px;
    text-decoration: none;
    color: var(--black);
    border-radius: unset;
  }

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

.search-hit-head {
  display: flex;
  align-items: baseline;
  justify-content: space-between;
  gap: 12px;
}

.search-title {
  font-weight: 700;
}

.search-section {
  flex-shrink: 0;
  font-size: calc(12 / 16 * 1rem);
  color: color-mix(in srgb, var(--black) 55%, var(--white));
  font-family: var(--mono);
}

.search-desc {
  font-size: calc(14 / 16 * 1rem);
  color: color-mix(in srgb, var(--black) 65%, var(--white));
  overflow: hidden;
  display: -webkit-box;
  -webkit-line-clamp: 1;
  -webkit-box-orient: vertical;
}

#search-results mark {
  background: color-mix(in srgb, var(--green) 40%, var(--white));
  color: inherit;
  border-radius: 2px;
}

#search-empty {
  padding: 16px;
  margin: 0;
  color: color-mix(in srgb, var(--black) 55%, var(--white));
}


#sidebar {
  position: sticky;
  inset: auto; /* Reset the browser's default popover insets. */
  top: var(--header-h);
  grid-area: sidebar;
  align-self: start;
  display: flex;
  flex-direction: column;
  justify-content: space-between;
  width: auto;
  height: calc(100dvh - var(--header-h));
  overflow: visible;
  margin: 0;
  padding: 0;
  line-height: 1.6;
  border: 0;
  border-right: 2px solid var(--beige);
  font-size: calc(14 / 16 * 1rem);

  scrollbar-width: none; /* Firefox */
  ::-webkit-scrollbar {
    display: none; /* Chrome, Safari, Edge */
  }

  .nav-scroll {
    flex: 1;
    min-height: 0;
    overflow-y: auto;
    padding: 20px;
  }

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

  details ul {
    margin-left: 11px;
    padding-left: 4px;
    border-left: 1px solid var(--beige);
  }

  summary {
    user-select: none;
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 8px;
    padding-block: 4px;
    padding-left: 8px;
    font-weight: 700;
    list-style: none;

    &::-webkit-details-marker {
      display: none; /* Safari / older Chrome */
    }

    &::after {
      content: "";
      flex-shrink: 0;
      width: 6px;
      height: 6px;
      border-right: 2px solid currentColor;
      border-bottom: 2px solid currentColor;
      transform: rotate(-45deg);
      transition: transform 0.15s ease;
    }

    &:hover {
      color: color-mix(in srgb, currentColor 60%, transparent);
    }
  }

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

  li:not(:last-of-type) {
    margin-bottom: 4px;
  }

  a {
    display: block;
    padding: 2px 8px;
    border-radius: 4px;
    text-decoration: none;

    &[aria-current="page"] {
      font-weight: 700;
      color: color-mix(in srgb, var(--purple) 70%, var(--black));
      background: color-mix(in srgb, var(--purple) 30%, var(--white));
    }

    &:hover {
      background: color-mix(in srgb, var(--green) 30%, var(--white));
      outline: none;
    }
  }

  #sidebar-toggle {
    height: 58px;
    margin-top: auto;
    position: sticky;
    bottom: 0;
    display: inline-flex;
    align-items: center;
    justify-content: flex-end;
    padding: 12px 16px;
    background: var(--white);
    border: 0;
    border-top: 2px solid var(--beige);

    &:hover {
      background: var(--beige);
    }
  }

  @media (max-width: 768px) {
    & {
      position: fixed;
      inset: 0 auto 0 0;
      width: 280px;
      max-width: 80vw;
      height: 100dvh;
      margin: 0;
      background: var(--white);
    }

    &:not(:popover-open) {
      display: none;
    }

    &::backdrop {
      background: rgb(0 0 0 / 0.3);
    }

    #sidebar-toggle {
      display: none;
    }
  }
}

#sidebar-toggle [data-icon="panel-left-open"] {
  display: none;
}

html.sidebar-collapsed #sidebar-toggle [data-icon="panel-left-close"] {
  display: none;
}

html.sidebar-collapsed #sidebar-toggle [data-icon="panel-left-open"] {
  display: revert;
}


#toc {
  grid-area: toc;
  align-self: start;
  position: sticky;
  top: var(--header-h);
  max-height: calc(100dvh - var(--header-h));
  overflow-y: auto;
  padding: 24px;
  font-size: calc(14 / 16 * 1rem);

  .toc-title {
    margin-top: 4px;
    margin-bottom: 4px;
    color: color-mix(in srgb, var(--black) 60%, var(--white));
    text-transform: uppercase;
    font-size: calc(12 / 16 * 1rem);
  }

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

  .toc-item {
    margin-block: 8px;
  }

  .toc-h3 {
    padding-left: 16px;
  }

  a {
    display: block;
    padding: 2px 8px;
    margin-left: -8px;
    text-decoration: none;
    overflow: hidden;
    white-space: nowrap;
    text-overflow: ellipsis;
  }

  @media (max-width: 1024px) {
    & {
      display: none;
    }
  }
}
