/* Midnight docs theme.
   Dark ("midnight") is the default; `[data-theme="light"]` is the day theme.
   The theme switch sets `data-theme` on <body> (midnight `onclick` action),
   so everything is driven by these variables. */

:root {
  color-scheme: dark;

  --bg: #0b0f1d;
  --bg-glow: rgba(120, 140, 220, 0.1);
  --sidebar-bg: #0e1428;
  --text: #e8ebf4;
  --muted: #8f97ad;
  --border: #1e2642;
  --accent: #b9c4ff;
  --accent-strong: #d0d8ff;
  --accent-soft: rgba(165, 180, 252, 0.14);
  --code-bg: rgba(255, 255, 255, 0.07);
  --code-block-bg: #090d19;
  --code-block-text: #e6edf3;
  --table-stripe: rgba(255, 255, 255, 0.025);
  --star-opacity: 0.9;

  /* syntax highlighting */
  --tok-keyword: #c792ea;
  --tok-function: #82aaff;
  --tok-builtin: #89ddff;
  --tok-string: #c3e88d;
  --tok-number: #f78c6c;
  --tok-const: #f78c6c;
  --tok-comment: #697098;
  --tok-operator: #c2cadb;
  --tok-punct: #8f97ad;
  --tok-interp: #ffcb6b;
  --tok-tag: #f07178;
  --tok-attr: #ffcb6b;

  /* tonight's moon (see Moon in layout.midnight) */
  --moon-lit: #f6f1d8;
  --moon-dark: #1b2340;
  --moon-glow: rgba(246, 241, 216, 0.45);
}

[data-theme='light'] {
  color-scheme: light;

  --bg: #f7f8fb;
  --bg-glow: rgba(255, 240, 210, 0.6);
  --sidebar-bg: #eef0f6;
  --text: #1c2027;
  --muted: #5a6270;
  --border: #d9dde5;
  --accent: #4f46e5;
  --accent-strong: #4338ca;
  --accent-soft: #e7e9ff;
  --code-bg: #eceef3;
  --code-block-bg: #151823;
  --code-block-text: #e6e8eb;
  --table-stripe: rgba(0, 0, 0, 0.025);
  --star-opacity: 0;

  /* (code blocks stay dark at dawn, so they keep the night's token colors) */

  /* a moon that's still up at dawn: paler, warmer */
  --moon-lit: #eab04a;
  --moon-dark: #c8cddc;
  --moon-glow: rgba(240, 196, 108, 0.4);
}

* {
  box-sizing: border-box;
}

html,
body {
  margin: 0;
  padding: 0;
}

body {
  font-family:
    system-ui,
    -apple-system,
    'Segoe UI',
    Roboto,
    sans-serif;
  color: var(--text);
  line-height: 1.6;
  background: var(--bg);
  transition:
    background 0.25s ease,
    color 0.25s ease;
  -webkit-tap-highlight-color: transparent;
}

/* moonlit glow, drawn as a fixed layer — `background-attachment: fixed` is
   ignored on iOS (and would scroll away), so a fixed pseudo-element keeps it
   pinned to the viewport on every device */
body::before {
  content: '';
  position: fixed;
  inset: 0;
  pointer-events: none;
  z-index: 0;
  background: radial-gradient(
    1100px 700px at 85% -10%,
    var(--bg-glow),
    transparent 60%
  );
}

/* starfield: a 1px dot scattered across the (fixed) viewport via box-shadow */
body::after {
  content: '';
  position: fixed;
  top: 0;
  left: 0;
  width: 1px;
  height: 1px;
  border-radius: 50%;
  pointer-events: none;
  z-index: 0;
  opacity: var(--star-opacity);
  transition: opacity 0.3s ease;
  box-shadow:
    24px 38px #fff,
    76px 132px #fff,
    142px 27px #fff,
    203px 298px #fff,
    258px 84px #fff,
    331px 216px #fff,
    397px 58px #fff,
    466px 342px #fff,
    528px 148px #fff,
    607px 42px #fff,
    683px 262px #fff,
    754px 118px #fff,
    826px 402px #fff,
    901px 196px #fff,
    984px 76px #fff,
    1053px 324px #fff,
    1126px 158px #fff,
    1198px 46px #fff,
    1277px 286px #fff,
    1358px 128px #fff,
    1433px 52px #fff,
    1508px 240px #fff,
    63px 460px #fff,
    158px 620px #fff,
    247px 388px #fff,
    312px 540px #fff,
    388px 700px #fff,
    472px 462px #fff,
    556px 648px #fff,
    641px 392px #fff,
    716px 736px #fff,
    802px 520px #fff,
    889px 676px #fff,
    966px 434px #fff,
    1044px 588px #fff,
    1121px 712px #fff,
    1203px 386px #fff,
    1289px 652px #fff,
    1372px 474px #fff,
    1449px 706px #fff,
    1526px 414px #fff,
    44px 168px rgba(255, 255, 255, 0.55),
    214px 470px rgba(255, 255, 255, 0.55),
    596px 86px rgba(255, 255, 255, 0.55),
    928px 356px rgba(255, 255, 255, 0.55),
    1163px 234px rgba(255, 255, 255, 0.55),
    1412px 596px rgba(255, 255, 255, 0.55),
    174px 256px 0 1px #fff,
    762px 178px 0 1px #fff,
    1090px 84px 0 1px #fff,
    1335px 372px 0 1px #fff;
  animation: twinkle 5s ease-in-out infinite alternate;
}

@keyframes twinkle {
  from {
    opacity: 0.35;
  }
  to {
    opacity: 0.9;
  }
}

[data-theme='light'] body::after,
:root[data-theme='light']::after {
  animation: none;
  opacity: 0;
}

/* a second handful of stars, twinkling out of step withthe first, so the sky
   shimmers instead of dimming all at once (the root's ::after, since body's
   pseudo-elements are taken) */
:root::after {
  content: '';
  position: fixed;
  top: 0;
  left: 0;
  width: 1px;
  height: 1px;
  border-radius: 50%;
  pointer-events: none;
  z-index: 0;
  opacity: var(--star-opacity);
  box-shadow:
    603px 859px #fff,
    44px 616px rgba(255, 255, 255, 0.6),
    877px 398px rgba(255, 255, 255, 0.6),
    276px 753px #fff,
    557px 465px #fff,
    462px 645px 0 0.5px #dfe6ff,
    60px 118px #fff,
    1464px 520px 0 0.5px #dfe6ff,
    50px 534px 0 0.5px #dfe6ff,
    745px 799px #fff,
    431px 811px #fff,
    1216px 387px #fff,
    1281px 231px rgba(255, 255, 255, 0.6),
    1168px 788px rgba(255, 255, 255, 0.6),
    1181px 298px rgba(255, 255, 255, 0.6),
    1183px 97px 0 0.5px #dfe6ff,
    373px 745px 0 0.5px #dfe6ff,
    1219px 820px 0 0.5px #dfe6ff,
    1393px 667px #fff,
    382px 828px #fff,
    1102px 341px #fff,
    1202px 127px #fff,
    211px 446px #fff,
    1173px 292px rgba(255, 255, 255, 0.6),
    1218px 348px #fff,
    767px 305px rgba(255, 255, 255, 0.6);
  animation: twinkle 3.7s ease-in-out -1.8s infinite alternate-reverse;
}

/* dawn: sunrise warming the bottom of the sky */
[data-theme='light'] body::before {
  background:
    radial-gradient(1100px 700px at 85% -10%, var(--bg-glow), transparent 60%),
    radial-gradient(
      1400px 420px at 30% 115%,
      rgba(255, 186, 140, 0.22),
      transparent 70%
    );
}

@media (prefers-reduced-motion: reduce) {
  body::after,
  :root::after {
    animation: none;
  }
}

.layout {
  position: relative;
  z-index: 1;
  display: grid;
  grid-template-columns: 240px 1fr;
  min-height: 100vh;
}

/* Sidebar ---------------------------------------------------------------- */

.sidebar {
  background: var(--sidebar-bg);
  border-right: 1px solid var(--border);
  padding: 1.5rem 1rem;
  position: sticky;
  top: 0;
  height: 100vh;
  overflow-y: auto;
  display: flex;
  flex-direction: column;
  transition: background 0.25s ease;
}

.brand {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  font-size: 1.35rem;
  font-weight: 700;
  letter-spacing: -0.02em;
  color: var(--text);
  text-decoration: none;
  margin-bottom: 1.25rem;
  padding: 0 0.5rem;
}

.moon {
  flex-shrink: 0;
  filter: drop-shadow(0 0 6px var(--moon-glow));
}

.moon-lit {
  fill: var(--moon-lit);
}

.moon-dark {
  fill: var(--moon-dark);
}

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

.nav li {
  margin: 0;
}

.nav a {
  display: block;
  padding: 0.4rem 0.5rem;
  border-radius: 0.4rem;
  color: var(--muted);
  text-decoration: none;
  font-size: 0.95rem;
}

.nav a:hover {
  color: var(--text);
  background: var(--accent-soft);
}

.nav a.active {
  color: var(--accent-strong);
  background: var(--accent-soft);
  font-weight: 600;
}

.theme-toggle {
  margin-top: auto;
  align-self: flex-start;
  display: inline-flex;
  align-items: center;
  gap: 0.4rem;
  background: var(--accent-soft);
  color: var(--text);
  border: 1px solid var(--border);
  border-radius: 999px;
  padding: 0.35rem 0.7rem;
  font-size: 0.85rem;
  cursor: pointer;
  transition: background 0.15s ease;
}

.theme-toggle:hover {
  background: var(--accent-soft);
  border-color: var(--accent);
}

.theme-toggle .icon {
  width: 16px;
  height: 16px;
  flex-shrink: 0;
  background-color: currentColor;
  -webkit-mask-repeat: no-repeat;
  mask-repeat: no-repeat;
  -webkit-mask-position: center;
  mask-position: center;
  -webkit-mask-size: contain;
  mask-size: contain;
}

.theme-toggle .icon-sun {
  -webkit-mask-image: url('/img/sun.svg');
  mask-image: url('/img/sun.svg');
}

.theme-toggle .icon-moon {
  -webkit-mask-image: url('/img/moon.svg');
  mask-image: url('/img/moon.svg');
  display: none;
}

:root[data-theme='light'] .theme-toggle .icon-sun {
  display: none;
}

:root[data-theme='light'] .theme-toggle .icon-moon {
  display: block;
}

/* Search (public/js/search.js) ------------------------------------------ */

.search-open {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  width: 100%;
  margin: 0 0 1rem;
  padding: 0.45rem 0.6rem;
  font: inherit;
  font-size: 0.9rem;
  color: var(--muted);
  background: var(--accent-soft);
  border: 1px solid var(--border);
  border-radius: 0.5rem;
  cursor: pointer;
  transition:
    color 0.15s ease,
    border-color 0.15s ease;
}

.search-open:hover {
  color: var(--text);
  border-color: var(--accent);
}

.search-icon {
  flex-shrink: 0;
}

.search-open .search-label {
  flex: 1;
  text-align: left;
}

kbd {
  font-family:
    ui-monospace, SFMono-Regular, 'SF Mono', Menlo, Consolas, monospace;
  font-size: 0.72rem;
  padding: 0.1rem 0.35rem;
  color: var(--muted);
  border: 1px solid var(--border);
  border-radius: 0.3rem;
}

.search {
  width: min(40rem, calc(100vw - 2rem));
  max-height: min(34rem, calc(100dvh - 6rem));
  margin: 10vh auto auto;
  padding: 0;
  color: var(--text);
  background: var(--sidebar-bg);
  border: 1px solid var(--border);
  border-radius: 0.75rem;
  box-shadow: 0 24px 60px rgba(0, 0, 0, 0.45);
  overflow: hidden;
}

.search[open] {
  display: flex;
  flex-direction: column;
}

.search::backdrop {
  background: rgba(4, 6, 14, 0.55);
  backdrop-filter: blur(3px);
}

.search-box {
  display: flex;
  align-items: center;
  gap: 0.6rem;
  padding: 0.85rem 1rem;
  color: var(--muted);
  border-bottom: 1px solid var(--border);
}

.search-box input {
  flex: 1;
  min-width: 0;
  font: inherit;
  font-size: 1.05rem;
  color: var(--text);
  background: none;
  border: 0;
  outline: none;
}

/* (the browser's clear button: esc clears and closes already) */
.search-box input::-webkit-search-cancel-button {
  display: none;
}

.search-results {
  overflow-y: auto;
  overscroll-behavior: contain;
  padding: 0.5rem;
}

.search-message {
  margin: 0;
  padding: 1.5rem 0.75rem;
  text-align: center;
  font-size: 0.9rem;
  color: var(--muted);
}

.search-group + .search-group {
  margin-top: 0.5rem;
}

.search-page {
  margin: 0;
  padding: 0.4rem 0.75rem 0.25rem;
  font-size: 0.72rem;
  font-weight: 600;
  letter-spacing: 0.08em;
  text-transform: uppercase;
  color: var(--muted);
}

.search-result {
  display: block;
  padding: 0.5rem 0.75rem;
  border-radius: 0.45rem;
  color: var(--text);
  text-decoration: none;
  scroll-margin: 0.5rem;
}

.search-result.active {
  background: var(--accent-soft);
  box-shadow: inset 2px 0 var(--accent);
}

.search-title {
  display: block;
  font-weight: 600;
}

.search-result.active .search-title {
  color: var(--accent-strong);
}

.search-snippet {
  display: block;
  margin-top: 0.15rem;
  font-size: 0.85rem;
  line-height: 1.45;
  color: var(--muted);
  overflow-wrap: anywhere;
}

.search mark {
  color: inherit;
  background: none;
  text-decoration: underline;
  text-decoration-color: var(--accent);
  text-decoration-thickness: 2px;
  text-underline-offset: 2px;
}

.search-title mark {
  color: var(--accent-strong);
}

/* Hamburger menu toggle (mobile only) ----------------------------------- */

.menu-toggle {
  display: none;
  align-items: center;
  justify-content: center;
  flex-direction: column;
  gap: 4px;
  width: 40px;
  height: 40px;
  padding: 0;
  background: transparent;
  border: 1px solid var(--border);
  border-radius: 0.5rem;
  cursor: pointer;
}

.menu-toggle .bar {
  width: 18px;
  height: 2px;
  background: var(--text);
  border-radius: 2px;
  transition:
    transform 0.2s ease,
    opacity 0.2s ease;
}

.sidebar.open .menu-toggle .bar:nth-child(1) {
  transform: translateY(6px) rotate(45deg);
}

.sidebar.open .menu-toggle .bar:nth-child(2) {
  opacity: 0;
}

.sidebar.open .menu-toggle .bar:nth-child(3) {
  transform: translateY(-6px) rotate(-45deg);
}

/* Content ---------------------------------------------------------------- */

.content {
  padding: 2.5rem 3rem 4rem;
  max-width: 52rem;
  min-width: 0;
  overflow-wrap: anywhere;
}

.content h1 {
  font-size: 2rem;
  letter-spacing: -0.02em;
  margin: 0 0 0.5rem;
}

/* a page's parts (h1s after its title, ex. the reference's "Beyond Lua") */
.content h1 ~ h1 {
  font-size: 1.7rem;
  margin: 4rem 0 0.5rem;
  padding-top: 1.5rem;
  border-top: 2px solid var(--accent-soft);
}

.content h2 {
  font-size: 1.35rem;
  letter-spacing: -0.01em;
  margin: 2.25rem 0 0.75rem;
  padding-bottom: 0.4rem;
  border-bottom: 1px solid var(--border);
}

.content h3 {
  font-size: 1.1rem;
  margin: 1.75rem 0 0.5rem;
}

.content p {
  margin: 0.75rem 0;
}

.content a {
  color: var(--accent);
  text-decoration: none;
}

.content a:hover {
  text-decoration: underline;
}

.lede {
  font-size: 1.15rem;
  color: var(--muted);
}

.content img {
  max-width: 100%;
  height: auto;
}

/* "this site runs on midnight": a link to the site's source, under every page */
.site-footer {
  margin-top: 4rem;
  padding-top: 1.25rem;
  border-top: 1px solid var(--border);
  font-size: 0.85rem;
}

.content .site-footer a {
  display: inline-flex;
  align-items: center;
  gap: 0.4rem;
  color: var(--muted);
}

.content .site-footer a:hover {
  color: var(--text);
  text-decoration: none;
}

.site-footer .moon {
  width: 16px;
  height: 16px;
}

/* (room above a heading you jump to; on phones, for the top bar too) */
.content :is(h2, h3) {
  scroll-margin-top: 1.5rem;
}

/* Table of contents ------------------------------------------------------ */

/* The page's sections: a column on the right on wide screens (.toc), and
   nested under the page's link in the nav otherwise (.nav-toc, h2s only).
   The scroll spy in layout.midnight marks the current one. */

.toc {
  display: none;
  position: sticky;
  top: 0;
  align-self: start;
  max-height: 100vh;
  overflow-y: auto;
  padding: 2.75rem 1.5rem 2rem 0.5rem;
  font-size: 0.85rem;
  line-height: 1.35;
  scrollbar-width: thin;
}

.toc-title {
  margin: 0 0 0.6rem;
  font-size: 0.72rem;
  font-weight: 600;
  letter-spacing: 0.08em;
  text-transform: uppercase;
  color: var(--muted);
}

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

.toc a {
  display: block;
  margin-left: -1px;
  padding: 0.25rem 0.75rem;
  border-left: 2px solid transparent;
  color: var(--muted);
  text-decoration: none;
  transition:
    color 0.15s ease,
    border-color 0.15s ease;
}

.toc .toc-h1 a,
.nav-toc .toc-h1 a {
  margin-top: 0.75rem;
  font-size: 0.7rem;
  font-weight: 600;
  letter-spacing: 0.08em;
  text-transform: uppercase;
}

.toc .toc-h1:first-child a,
.nav-toc .toc-h1:first-child a {
  margin-top: 0;
}

.toc .toc-h3 a {
  padding-left: 1.5rem;
  font-size: 0.8rem;
}

.toc a:hover {
  color: var(--text);
}

.toc a.current {
  color: var(--accent-strong);
  font-weight: 600;
  border-left-color: var(--accent);
  background: linear-gradient(to right, var(--accent-soft), transparent);
}

:is(.toc, .nav-toc) code {
  font-size: 0.9em;
  padding: 0.05em 0.3em;
}

/* In the nav, the current page's name toggles its sections (a long page's
   sections bury the other pages). Its plain link is only for wide screens,
   where the sections are in the column instead. */
.nav a.has-sections {
  display: none;
}

.nav-toc summary {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0.4rem 0.5rem;
  border-radius: 0.4rem;
  font-size: 0.95rem;
  color: var(--accent-strong);
  background: var(--accent-soft);
  font-weight: 600;
  cursor: pointer;
  list-style: none;
  -webkit-tap-highlight-color: transparent;
}

.nav-toc summary::-webkit-details-marker {
  display: none;
}

/* points right when closed, down when open */
.nav-toc .caret {
  width: 0.45em;
  height: 0.45em;
  margin-right: 0.25rem;
  border-right: 2px solid currentColor;
  border-bottom: 2px solid currentColor;
  transform: rotate(-45deg);
  transition: transform 0.15s ease;
}

.nav-toc[open] .caret {
  transform: rotate(45deg);
}

/* (open, the page and its sections are one box, so the sections read as
   "on this page", not as more pages) */
.nav-toc[open] {
  margin: 0.25rem 0;
  border: 1px solid var(--border);
  border-radius: 0.5rem;
}

.nav-toc[open] summary {
  border-radius: 0.45rem 0.45rem 0 0;
}

.nav .nav-toc ul {
  list-style: none;
  margin: 0.5rem 0.5rem 0.5rem 1.25rem;
  padding: 0 0 0 0.25rem;
  border-left: 1px solid var(--border);
}

.nav-toc .toc-h3 {
  display: none;
}

.nav .nav-toc a {
  padding: 0.25rem 0.5rem;
  font-size: 0.85rem;
  line-height: 1.35;
}

.nav .nav-toc a.current {
  color: var(--accent-strong);
  font-weight: 600;
}

/* the column fits beside the text: the sections move there */
@media (min-width: 1180px) {
  .layout.has-toc {
    grid-template-columns: 240px minmax(0, 52rem) 16rem;
  }

  .toc {
    display: block;
  }

  .nav-toc {
    display: none;
  }

  .nav a.has-sections {
    display: block;
  }
}

/* Code ------------------------------------------------------------------- */

code {
  font-family:
    ui-monospace, SFMono-Regular, 'SF Mono', Menlo, Consolas, monospace;
  font-size: 0.9em;
  background: var(--code-bg);
  padding: 0.15em 0.35em;
  border-radius: 0.3rem;
}

pre.code {
  background: var(--code-block-bg);
  color: var(--code-block-text);
  padding: 1rem 1.25rem;
  border-radius: 0.5rem;
  overflow-x: auto;
  max-width: 100%;
  -webkit-overflow-scrolling: touch;
  margin: 1rem 0;
  line-height: 1.5;
  border: 1px solid var(--border);
}

pre.code code {
  background: none;
  padding: 0;
  font-size: 0.85rem;
  color: inherit;
}

/* Copyable code blocks (opt-in via ```lang copy) ------------------------ */

/* (a header row with the language and the buttons, so they never cover the
   code, however long its first line) */
.code-block {
  margin: 1rem 0;
  background: var(--code-block-bg);
  border: 1px solid var(--border);
  border-radius: 0.5rem;
  overflow: hidden;
}

.code-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 0.5rem;
  padding: 0.35rem 0.5rem 0.35rem 1.25rem;
  border-bottom: 1px solid var(--border);
}

.code-lang {
  font-family:
    ui-monospace, SFMono-Regular, 'SF Mono', Menlo, Consolas, monospace;
  font-size: 0.72rem;
  color: var(--muted);
}

.code-block > pre.code {
  margin: 0;
  border: 0;
  border-radius: 0;
}

.code-actions {
  display: flex;
  gap: 0.4rem;
}

.copy-btn,
.try-btn {
  padding: 0.25rem 0.6rem;
  font-size: 0.75rem;
  line-height: 1.4;
  color: var(--muted);
  background: var(--accent-soft);
  border: 1px solid var(--border);
  border-radius: 0.4rem;
  cursor: pointer;
  transition:
    color 0.15s ease,
    border-color 0.15s ease;
}

.copy-btn:hover,
.try-btn:hover {
  color: var(--text);
  border-color: var(--accent);
}

:is(pre.code, .pg-highlight) .tok-keyword {
  color: var(--tok-keyword);
}
:is(pre.code, .pg-highlight) .tok-function {
  color: var(--tok-function);
}
:is(pre.code, .pg-highlight) .tok-builtin {
  color: var(--tok-builtin);
}
:is(pre.code, .pg-highlight) .tok-string {
  color: var(--tok-string);
}
:is(pre.code, .pg-highlight) .tok-number {
  color: var(--tok-number);
}
:is(pre.code, .pg-highlight) .tok-const {
  color: var(--tok-const);
}
:is(pre.code, .pg-highlight) .tok-comment {
  color: var(--tok-comment);
  font-style: italic;
}
:is(pre.code, .pg-highlight) .tok-operator {
  color: var(--tok-operator);
}
:is(pre.code, .pg-highlight) .tok-punct {
  color: var(--tok-punct);
}
:is(pre.code, .pg-highlight) .tok-interp {
  color: var(--tok-interp);
  font-weight: 600;
}
:is(pre.code, .pg-highlight) .tok-tag {
  color: var(--tok-tag);
}
:is(pre.code, .pg-highlight) .tok-attr {
  color: var(--tok-attr);
}

/* Playground (src/playground.midnight, public/js/play) ------------------------ */

.content.wide {
  max-width: none;
}

.playground {
  display: flex;
  flex-direction: column;
  gap: 0.75rem;
}

/* the editor and the output side by side, each withits bar on top (in the
   same row, so they line up); stacked on phones */
.pg-grid {
  display: grid;
  grid-template-columns: minmax(0, 1fr) minmax(0, 1fr);
  grid-template-rows: auto max(24rem, calc(100vh - 14rem));
  grid-template-areas:
    'toolbar tabs'
    'editor output';
  gap: 0.5rem 0.75rem;
}

.pg-toolbar {
  grid-area: toolbar;
}

.pg-editor {
  grid-area: editor;
}

.pg-tabs {
  grid-area: tabs;
}

.pg-output {
  grid-area: output;
}

.pg-toolbar,
.pg-tabs {
  display: flex;
  align-items: center;
  gap: 0.4rem;
  min-width: 0;
}

/* (the tab buttons, together in their own row) */
.pg-tabs > div:not(.pg-status) {
  display: flex;
  gap: 0.4rem;
}

.pg-toolbar button,
.pg-toolbar select,
.pg-tabs button {
  font: inherit;
  font-size: 0.85rem;
  color: var(--text);
  background: var(--accent-soft);
  border: 1px solid var(--border);
  border-radius: 0.4rem;
  padding: 0.35rem 0.8rem;
  cursor: pointer;
}

.pg-toolbar button:hover:not(:disabled),
.pg-tabs button:hover:not(:disabled) {
  border-color: var(--accent);
}

.pg-toolbar .pg-run {
  font-weight: 600;
  color: var(--accent-strong);
  border-color: var(--accent);
}

.pg-toolbar button:disabled {
  opacity: 0.5;
  cursor: progress;
}

/* the examples menu: its own chevron, inside its padding (the browser's
   ignores the padding, and sits on the edge). Gradients, so it's the text
   color in both themes. */
.pg-toolbar select {
  appearance: none;
  padding-right: 2rem;
  background-image:
    linear-gradient(45deg, transparent 50%, currentColor 50%),
    linear-gradient(135deg, currentColor 50%, transparent 50%);
  background-position:
    right 0.95rem center,
    right 0.65rem center;
  background-size: 0.3rem 0.3rem;
  background-repeat: no-repeat;
}

/* (out of the way: it's not something you do every run) */
.pg-toolbar .pg-share {
  margin-left: auto;
  color: var(--muted);
  background: none;
  border-color: transparent;
}

.pg-tabs button.active {
  color: var(--accent-strong);
  border-color: var(--accent);
  font-weight: 600;
}

.pg-tabs button:disabled {
  opacity: 0.45;
  cursor: default;
}

.pg-status {
  margin-left: auto;
  min-width: 0;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  font-size: 0.85rem;
  color: var(--muted);
}

.pg-status.err {
  color: var(--tok-tag);
}

/* the editor: a transparent textarea over its highlighted copy, withline
   numbers (the gutter) beside them, and problems' lines marked behind them */
.pg-editor {
  --gutter: 3rem;
  /* (the textarea, its highlighted copy, the line numbers and the marks are
     all this size, or they'd drift apart) */
  --code-size: 0.875rem;
  position: relative;
  min-height: 0;
  background: var(--code-block-bg);
  border: 1px solid var(--border);
  border-radius: 0.5rem;
  overflow: hidden;
}

.pg-editor:focus-within {
  border-color: var(--accent);
}

.pg-highlight,
.pg-input,
.pg-marks {
  position: absolute;
  inset: 0 0 0 var(--gutter);
  margin: 0;
  padding: 1rem 1.25rem;
  border: 0;
  border-radius: 0;
  font-family:
    ui-monospace, SFMono-Regular, 'SF Mono', Menlo, Consolas, monospace;
  font-size: var(--code-size);
  line-height: 1.5;
  white-space: pre;
  tab-size: 2;
  overflow: auto;
}

.pg-highlight,
.pg-marks {
  pointer-events: none;
  overflow: hidden;
  background: none;
}

.pg-highlight code {
  font: inherit;
  padding: 0;
  background: none;
}

.pg-input {
  width: calc(100% - var(--gutter));
  height: 100%;
  resize: none;
  outline: none;
  background: transparent;
  color: transparent;
  caret-color: var(--code-block-text);
}

.pg-input::selection {
  background: rgba(185, 196, 255, 0.25);
}

/* (scrolled withthe textarea, see editor.js) */
.pg-gutter {
  position: absolute;
  inset: 0 auto 0 0;
  width: var(--gutter);
  overflow: hidden;
  border-right: 1px solid var(--border);
  user-select: none;
}

.pg-gutter > div {
  /* (the textarea's padding, and room for its scrollbar at the bottom) */
  padding: 1rem 0.6rem 2.5rem 0;
  font-family:
    ui-monospace, SFMono-Regular, 'SF Mono', Menlo, Consolas, monospace;
  font-size: var(--code-size);
  line-height: 1.5;
  text-align: right;
  color: var(--muted);
}

.pg-gutter span {
  display: block;
  opacity: 0.6;
}

/* a line witha lint problem (warn), or a syntax / runtime error (err) */
.pg-gutter .warn,
.pg-gutter .err {
  opacity: 1;
  font-weight: 700;
  box-shadow: inset 3px 0 currentColor;
  cursor: help;
}

.pg-gutter .warn,
.pg-marks .warn {
  color: var(--tok-number);
}

.pg-gutter .err,
.pg-marks .err {
  color: var(--tok-tag);
}

/* the marked lines, behind the code: tinted, witha dotted underline */
.pg-marks > div {
  position: relative;
}

.pg-marks span {
  position: absolute;
  left: -1.25rem;
  right: -100vw;
  height: 1.5em;
  background: color-mix(in srgb, currentColor 9%, transparent);
  border-bottom: 1px dotted color-mix(in srgb, currentColor 70%, transparent);
}

/* what's wrong witha marked line, on hover (or a tap on its number) */
.pg-tip {
  position: absolute;
  z-index: 2;
  max-width: min(32rem, calc(100% - 1rem));
  padding: 0.35rem 0.6rem;
  font-size: 0.8rem;
  line-height: 1.4;
  color: var(--code-block-text);
  background: var(--bg);
  border: 1px solid var(--border);
  border-left: 3px solid var(--tok-number);
  border-radius: 0.35rem;
  box-shadow: 0 6px 18px rgba(0, 0, 0, 0.35);
  pointer-events: none;
}

.pg-tip.err {
  border-left-color: var(--tok-tag);
}

.pg-tip .rule {
  color: var(--muted);
  margin-left: 0.4rem;
}

.pg-output {
  display: flex;
  flex-direction: column;
  min-height: 0;
}

.pg-pane {
  flex: 1;
  min-height: 0;
  margin: 0;
  overflow: auto;
}

pre.code.pg-pane {
  margin: 0;
  font-family:
    ui-monospace, SFMono-Regular, 'SF Mono', Menlo, Consolas, monospace;
  font-size: 0.85rem;
  white-space: pre-wrap;
}

.pg-pane .pg-err {
  color: var(--tok-tag);
}

.pg-pane .pg-note {
  color: var(--muted);
  font-style: italic;
}

iframe.pg-pane {
  width: 100%;
  border: 1px solid var(--border);
  border-radius: 0.5rem;
  background: #fff;
}

.pg-about {
  font-size: 0.85rem;
  color: var(--muted);
  margin: 0;
}

@media (max-width: 900px) {
  .pg-grid {
    grid-template-columns: minmax(0, 1fr);
    grid-template-rows: auto 22rem auto minmax(14rem, auto);
    grid-template-areas:
      'toolbar'
      'editor'
      'tabs'
      'output';
  }

  .pg-tabs {
    margin-top: 0.25rem;
    flex-wrap: wrap;
  }

  /* (on its own line, under the tabs, wrapping: a long message would push
     them around, and get cut off) */
  .pg-status {
    flex-basis: 100%;
    margin-left: 0;
    white-space: normal;
  }

  .pg-pane {
    min-height: 14rem;
  }

  /* (as tall as its page, see page.js: the page scrolls withthe playground) */
  iframe.pg-pane {
    flex: none;
  }
}

/* iOS zooms in on a text field witha font under 16px when it's tapped */
@media (pointer: coarse) {
  .pg-editor {
    --code-size: 16px;
  }
}

@media (max-width: 900px) and (pointer: coarse) {
  /* Run floats in the bottom right, where a thumb can reach it (touch
     screens only: a narrow desktop window keeps it in the toolbar) */
  .pg-toolbar .pg-run {
    position: fixed;
    right: 1rem;
    bottom: calc(1rem + env(safe-area-inset-bottom));
    z-index: 3;
    min-width: 4.5rem;
    min-height: 3rem;
    padding: 0.75rem 1.4rem;
    font-size: 1rem;
    border-radius: 999px;
    color: var(--bg);
    background: var(--accent);
    box-shadow:
      0 6px 20px rgba(0, 0, 0, 0.35),
      0 0 0 1px var(--accent-strong);
  }

  /* (room to scroll past it) */
  .playground {
    padding-bottom: 4.5rem;
  }
}

/* Tables ----------------------------------------------------------------- */

table {
  border-collapse: collapse;
  width: 100%;
  margin: 1rem 0;
  font-size: 0.95rem;
}

th,
td {
  border: 1px solid var(--border);
  padding: 0.5rem 0.75rem;
  text-align: left;
}

th {
  background: var(--accent-soft);
  font-weight: 600;
}

/* .content breaks long words anywhere, which inside a table lets a wordy
   column squeeze its neighbor down to a letter per line (on phones). In
   tables, words break normally, and the first column (usually a name) stays
   on one line; .table-scroll scrolls what doesn't fit. */
table {
  overflow-wrap: normal;
}

td:first-child code,
th:first-child {
  white-space: nowrap;
}

tr:nth-child(even) td {
  background: var(--table-stripe);
}

/* Blockquotes, rules, lists --------------------------------------------- */

blockquote {
  margin: 1rem 0;
  padding: 0.25rem 1rem;
  border-left: 3px solid var(--accent);
  color: var(--muted);
}

blockquote p {
  margin: 0.5rem 0;
}

hr {
  border: none;
  border-top: 1px solid var(--border);
  margin: 2rem 0;
}

.content ul,
.content ol {
  padding-left: 1.5rem;
}

.content li {
  margin: 0.25rem 0;
}

/* Responsive ------------------------------------------------------------ */

.table-scroll {
  overflow-x: auto;
  max-width: 100%;
  -webkit-overflow-scrolling: touch;
}

@media (max-width: 768px) {
  .layout {
    grid-template-columns: 1fr;
  }

  .sidebar {
    position: sticky;
    top: 0;
    z-index: 2;
    height: auto;
    padding: 0.75rem 1rem;
    border-right: none;
    border-bottom: 1px solid var(--border);
    display: grid;
    grid-template-columns: 1fr auto auto auto;
    grid-template-areas:
      'brand search theme menu'
      'nav   nav    nav   nav';
    align-items: center;
    gap: 0.5rem 0.75rem;
    transition:
      transform 0.25s ease,
      background 0.25s ease;
  }

  .sidebar.bar-hidden {
    transform: translateY(-100%);
  }

  /* (a long menu, with a page's sections, 
       scrolls instead of running off the screen) */
  .sidebar.open {
    max-height: 100vh;
    max-height: 100dvh;
    overflow-y: auto;
    overscroll-behavior: contain;
  }

  .content :is(h2, h3) {
    scroll-margin-top: 4.5rem;
  }

  .brand {
    grid-area: brand;
    margin: 0;
    padding: 0;
    font-size: 1.2rem;
  }

  .theme-toggle {
    grid-area: theme;
    margin: 0;
    min-height: 40px;
    padding: 0.5rem 0.9rem;
  }

  .menu-toggle {
    grid-area: menu;
    display: inline-flex;
  }

  /* (just the icon, a button like the menu's) */
  .search-open {
    grid-area: search;
    justify-content: center;
    width: 40px;
    height: 40px;
    margin: 0;
    padding: 0;
    color: var(--text);
    background: transparent;
  }

  .search-open .search-label,
  .search-open .search-kbd,
  .search-esc {
    display: none;
  }

  .search-open .search-icon {
    width: 18px;
    height: 18px;
  }

  .search {
    margin-top: 1rem;
    max-height: calc(100dvh - 2rem);
  }

  .nav {
    grid-area: nav;
    display: none;
    border-top: 1px solid var(--border);
    padding-top: 0.5rem;
  }

  .sidebar.open .nav {
    display: block;
  }

  .nav ul {
    display: block;
    margin: 0;
    padding: 0;
  }

  .nav a {
    display: block;
    padding: 0.6rem 0.5rem;
    font-size: 1rem;
  }

  .nav-toc summary {
    padding: 0.6rem 0.5rem;
    font-size: 1rem;
  }

  .nav .nav-toc a {
    padding: 0.45rem 0.75rem;
    font-size: 0.88rem;
  }

  .content {
    padding: 1.5rem 1.25rem 3rem;
  }

  .content h1 {
    font-size: 1.6rem;
  }

  .content h2 {
    font-size: 1.2rem;
    margin-top: 1.75rem;
  }

  .content h3 {
    font-size: 1.05rem;
  }

  pre.code {
    padding: 0.75rem 1rem;
  }
}

@media (max-width: 480px) {
  .content {
    padding: 1.25rem 1rem 2.5rem;
  }

  .content h1 {
    font-size: 1.45rem;
  }
}
