/*
 * Code blocks look like a code editor: a tab bar with the file name or the language,
 * and a copy button, above the code. Code groups use the same bar for their tabs.
 *
 * A block's markup, from Postdoc: div.language-js > span.code-title (with a file name),
 * button.copy, span.lang and the pre that Shiki renders.
 */

:root {
  --code-font-size: 0.875rem;
  --code-line-height: 1.7;
  --code-radius: 10px;
  /* The radius inside the border, for the parts of the editor */
  --code-inner-radius: calc(var(--code-radius) - 1px);

  /* The editor is dark in both colour schemes, like a terminal. */
  --code-bg: #0d1411;
  --code-border: #26372e;
  --code-header-bg: #15211b;
  --code-tab-color: #d5e8db;
  --code-muted: #a3bcab;
  --code-accent: #8fd1a3;
  --code-button-hover-bg: #213329;
  --code-shadow: 0 1px 2px rgb(30 57 46 / 0.1), 0 10px 24px -12px rgb(30 57 46 / 0.35);

  --code-icon-file: url('data:image/svg+xml,%3Csvg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" fill="none" stroke="black" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"%3E%3Cpath d="M14 3H7a2 2 0 0 0-2 2v14a2 2 0 0 0 2 2h10a2 2 0 0 0 2-2V8z"/%3E%3Cpath d="M14 3v5h5"/%3E%3C/svg%3E');
  --code-icon-terminal: url('data:image/svg+xml,%3Csvg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" fill="none" stroke="black" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"%3E%3Crect x="3" y="4" width="18" height="16" rx="2"/%3E%3Cpath d="m7 9 3 3-3 3M13 15h4"/%3E%3C/svg%3E');
  --code-icon-copy: url('data:image/svg+xml,%3Csvg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" fill="none" stroke="black" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"%3E%3Crect x="8" y="8" width="12" height="12" rx="2"/%3E%3Cpath d="M16 8V6a2 2 0 0 0-2-2H6a2 2 0 0 0-2 2v8a2 2 0 0 0 2 2h2"/%3E%3C/svg%3E');
  --code-icon-copied: url('data:image/svg+xml,%3Csvg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" fill="none" stroke="black" stroke-width="2.5" stroke-linecap="round" stroke-linejoin="round"%3E%3Cpath d="M5 12l5 5L20 7"/%3E%3C/svg%3E');
}

[data-theme="dark"] {
  --code-shadow: none;
}

/* Shiki renders both themes. The editor is always dark, so its dark colours apply, as
   they do everywhere in dark mode. */
.pd-content .shiki,
.pd-content .shiki span,
html[data-theme="dark"] .shiki,
html[data-theme="dark"] .shiki span {
  color: var(--shiki-dark) !important;
  font-style: var(--shiki-dark-font-style) !important;
  font-weight: var(--shiki-dark-font-weight) !important;
  text-decoration: var(--shiki-dark-text-decoration) !important;
}

/*
 * The editor is a grid: the tab bar, with the tab and the copy button, above the code.
 * The bar grows with its content, so a long file name or larger text wraps instead of
 * being cut off.
 */
.pd-content div[class*=language-] {
  display: grid;
  grid-template-columns: minmax(0, 1fr) auto;
  margin: 1.5rem 0;
  border: 1px solid var(--code-border);
  border-radius: var(--code-radius);
  background-color: var(--code-bg);
  box-shadow: var(--code-shadow);
}

/* The tab bar. Its bottom line is a shadow, which the tab covers, so the tab joins the
   code below it. The editor doesn't cut off what's outside it, so that nothing is ever
   hidden; its parts have rounded corners of their own instead. */
.pd-content div[class*=language-]::before {
  content: '';
  grid-row: 1;
  grid-column: 1 / -1;
  border-radius: var(--code-inner-radius) var(--code-inner-radius) 0 0;
  background-color: var(--code-header-bg);
  box-shadow: inset 0 -1px 0 var(--code-border);
}

/* The tab: the file name, or else the language */
.pd-content div[class*=language-] > .code-title,
.pd-content div[class*=language-] > span.lang {
  grid-row: 1;
  grid-column: 1;
  justify-self: start;
  padding: 0.5rem 1rem 0.5625rem;
  border-top: 2px solid var(--code-accent);
  border-top-left-radius: var(--code-inner-radius);
  border-right: 1px solid var(--code-border);
  background-color: var(--code-bg);
  color: var(--code-tab-color);
  font-family: var(--pd-font-family-mono);
  font-size: 0.8125rem;
  line-height: 1.5;
  overflow-wrap: anywhere;
}

.pd-content div[class*=language-] > .code-title::before,
.pd-content div[class*=language-] > span.lang::before {
  content: '';
  display: inline-block;
  width: 1rem;
  height: 1rem;
  margin-right: 0.5rem;
  vertical-align: -0.2em;
  background-color: var(--code-accent);
  mask: var(--code-icon, var(--code-icon-file)) center / contain no-repeat;
}

.pd-content div[class*=language-] > .code-title ~ span.lang {
  display: none;
}

/* Readable names for the languages the docs use. Others show as they're written. */
.pd-content :is(.language-sh, .language-bash, .language-shell, .language-zsh) {
  --code-icon: var(--code-icon-terminal);
  --code-language: 'Terminal';
}

/* Each command of a terminal has a prompt, which the copy button leaves out. */
.pd-content :is(.language-sh, .language-bash, .language-shell, .language-zsh) .line:not(:empty)::before {
  content: '$ ';
  color: var(--code-muted);
}

.pd-content .language-js { --code-language: 'JavaScript'; }
.pd-content .language-ts { --code-language: 'TypeScript'; }
.pd-content .language-md { --code-language: 'Markdown'; }
.pd-content .language-html { --code-language: 'HTML'; }
.pd-content .language-ejs { --code-language: 'EJS'; }
.pd-content .language-css { --code-language: 'CSS'; }
.pd-content .language-json { --code-language: 'JSON'; }
.pd-content .language-yaml { --code-language: 'YAML'; }
.pd-content .language-toml { --code-language: 'TOML'; }
.pd-content .language-txt { --code-language: 'Text'; }

.pd-content div:is(
  .language-sh, .language-bash, .language-shell, .language-zsh, .language-js, .language-ts, .language-md,
  .language-html, .language-ejs, .language-css, .language-json, .language-yaml, .language-toml, .language-txt
) > span.lang {
  font-size: 0;
}

.pd-content div[class*=language-] > span.lang::after {
  content: var(--code-language, none);
  font-size: 0.8125rem;
}

/* The copy button, at the end of the tab bar */
.pd-content div[class*=language-] > button.copy {
  grid-row: 1;
  grid-column: 2;
  align-self: center;
  display: flex;
  align-items: center;
  gap: 0.375rem;
  margin: 0.25rem 0.5rem;
  padding: 0.3125rem 0.625rem;
  border: 0;
  border-radius: 6px;
  background-color: transparent;
  color: var(--code-muted);
  font-family: "Work Sans", sans-serif;
  font-size: 0.8125rem;
  font-weight: 500;
  cursor: pointer;
}

.pd-content div[class*=language-] > button.copy::before {
  content: '';
  width: 1rem;
  height: 1rem;
  background-color: currentColor;
  mask: var(--code-icon-copy) center / contain no-repeat;
}

.pd-content div[class*=language-] > button.copy::after {
  content: 'Copy';
}

.pd-content div[class*=language-] > button.copy:hover {
  background-color: var(--code-button-hover-bg);
  color: var(--code-tab-color);
}

.pd-content div[class*=language-] > button.copy.copied {
  color: var(--code-accent);
}

.pd-content div[class*=language-] > button.copy.copied::before {
  mask-image: var(--code-icon-copied);
}

.pd-content div[class*=language-] > button.copy.copied::after {
  content: 'Copied';
}

/* The code. Its own background replaces the ones Shiki sets for each theme. */
.pd-content div[class*=language-] pre {
  grid-row: 2;
  grid-column: 1 / -1;
  /* A long word in the code scrolls, instead of making the editor wider. */
  min-width: 0;
  margin: 0;
  border-radius: 0 0 var(--code-inner-radius) var(--code-inner-radius);
  padding: 1rem 1.25rem;
  overflow-x: auto;
  background-color: var(--code-bg) !important;
  font-family: var(--pd-font-family-mono);
  font-size: var(--code-font-size);
  line-height: var(--code-line-height);
  tab-size: 2;
}

.pd-content div[class*=language-] pre code {
  font-family: inherit;
  font-size: inherit;
}

/* The focus ring is drawn inside, where it doesn't cover the text around the editor. */
.pd-content div[class*=language-] pre:focus-visible {
  outline-offset: -3px;
}

/* On the dark editor, the ring is light in both colour schemes. */
.pd-content :is(div[class*=language-], .code-group) :focus-visible {
  outline-color: var(--code-accent);
}
