@import url("https://fonts.googleapis.com/css2?family=IBM+Plex+Mono:ital,wght@0,400;0,600;1,400&display=swap");
@import url("./variables.css");
@import url("./forms.css");

/* Reset */

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

* {
  margin: 0;
}

html {
  font-family: var(--font);
  font-size: 112.5%;
  line-height: var(--line-height);
  background-color: var(--background-color);
  color: var(--text-color);
  -webkit-text-size-adjust: none;
  text-size-adjust: none;
  scroll-behavior: smooth;
}

@media (prefers-reduced-motion: reduce) {
  html {
    scroll-behavior: auto;
  }
}

body {
  max-width: calc(80ch + 4ch);
  min-height: 100vh;
  margin-inline: auto;
  padding: var(--row) 2ch calc(var(--row) * 3);
  -webkit-font-smoothing: antialiased;
}

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

::selection {
  background-color: var(--accent-color);
  color: var(--background-color);
}

:focus-visible {
  outline: 1px solid var(--focus-outline-color);
  outline-offset: 2px;
}

/* Vertical rhythm: every block is followed by exactly one empty row */

p,
ul,
ol,
dl,
pre,
blockquote,
table,
figure,
details,
fieldset,
form,
hr,
address,
menu,
progress,
meter {
  margin-block: 0 var(--row);
}

:is(p, ul, ol, dl, pre, blockquote, figure, details, fieldset, form, address, li, dd, section, article):last-child {
  margin-block-end: 0;
}

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

/* Headings
 *
 * Markdown source has one font size, so headings are told apart by syntax:
 * h1 is underlined setext-style and the rest keep their ATX `#` prefix.
 */

h1,
h2,
h3,
h4,
h5,
h6 {
  font-size: 1rem;
  font-weight: 600;
  line-height: var(--row);
  color: var(--strong-color);
  text-wrap: balance;
  overflow-wrap: break-word;
  margin-block: calc(var(--row) * 2) var(--row);
}

/* h1 is a setext heading: underlined with `=` exactly as long as its text */

h1 {
  position: relative;
  width: fit-content;
  max-width: 100%;
  padding-block-end: var(--row);
}

h1:first-child {
  margin-block-start: 0;
}

/* Absolutely positioned so the long run of `=` never widens the heading */
h1::after {
  content: "================================================================================================================================================================";
  position: absolute;
  inset-inline: 0;
  inset-block-end: 0;
  overflow: hidden;
  white-space: nowrap;
  color: var(--dim-color);
  font-weight: normal;
}

h2::before,
h3::before,
h4::before,
h5::before,
h6::before {
  color: var(--dim-color);
  font-weight: normal;
}

h2::before { content: "## "; }
h3::before { content: "### "; }
h4::before { content: "#### "; }
h5::before { content: "##### "; }
h6::before { content: "###### "; }

:is(h1, h2, h3, h4, h5, h6) + :is(h1, h2, h3, h4, h5, h6),
hr + :is(h1, h2, h3, h4, h5, h6),
hr + :is(section, article) > :is(h1, h2, h3, h4, h5, h6):first-child,
:is(h1, h2, h3, h4, h5, h6) + :is(section, article) > :is(h1, h2, h3, h4, h5, h6):first-child {
  margin-block-start: 0;
}

body > header > :last-child {
  margin-block-end: var(--row);
}

hgroup > :not(:first-child) {
  margin-block-start: 0;
}

hgroup > p {
  color: var(--dim-color);
}

/* Inline text: the Markdown syntax stays visible, dimmed */

:is(strong, em, del, s, mark, ins, sub, sup)::before,
:is(strong, em, del, s, mark, ins, sub, sup)::after {
  color: var(--dim-color);
  font-style: normal;
  font-weight: normal;
}

strong,
b {
  font-weight: 600;
  color: var(--strong-color);
}

strong::before,
strong::after {
  content: "**";
}

em::before,
em::after {
  content: "_";
}

small {
  font-size: 1rem;
  color: var(--dim-color);
}

sub,
sup {
  font-size: 1rem;
  white-space: nowrap;
  line-height: 0;
  vertical-align: baseline;
}

sub::before,
sub::after {
  content: "~";
}

sup::before,
sup::after {
  content: "^";
}

mark {
  background-color: transparent;
  color: var(--mark-color);
}

mark::before,
mark::after {
  content: "==";
}

del,
s {
  color: var(--dim-color);
}

del::before,
del::after,
s::before,
s::after {
  content: "~~";
}

ins {
  color: var(--accent-color);
  text-decoration: none;
}

ins::before,
ins::after {
  content: "++";
}

abbr[title] {
  text-decoration: underline dotted;
  cursor: help;
}

code,
kbd,
samp,
pre {
  font-family: inherit;
  font-size: 1rem;
}

code {
  color: var(--code-color);
}

:not(pre) > code::before,
:not(pre) > code::after {
  content: "`";
  color: var(--dim-color);
}

kbd {
  color: var(--strong-color);
}

samp {
  color: var(--strong-color);
}

/* Links: reference-style [text], external ones [text↗] */

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

a::before,
a::after {
  display: inline-block;
  color: var(--dim-color);
}

a::before {
  content: "[";
}

a::after {
  content: "]";
}

a[href^="http"]::after {
  content: "↗]";
}

a:has(img, svg)::before,
a:has(img, svg)::after {
  content: none;
}

a:hover,
a:focus-visible {
  text-decoration: underline;
  text-underline-offset: 0.2em;
}

nav ul {
  list-style: none;
  padding: 0;
  display: flex;
  flex-wrap: wrap;
  column-gap: 1ch;
}

/* Horizontal rule: a row of dashes, clipped to the column width */

hr {
  border: 0;
  margin-block: var(--row);
  height: var(--row);
  overflow: hidden;
  white-space: nowrap;
  color: var(--dim-color);
}

hr::before {
  content: "--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------";
}

/* Lists */

ul,
ol {
  padding-inline-start: 3ch;
}

ul {
  list-style-type: "-  ";
}

ul ul {
  list-style-type: "*  ";
}

li::marker {
  color: var(--dim-color);
}

ol ::marker {
  content: counter(list-item) ". ";
}

/* Definition lists render as YAML front matter: aligned `key: value` lines
 * between `---` rows. Values line up in a column after the longest key. */

dl {
  display: grid;
  grid-template-columns: max-content 1fr;
  column-gap: 1ch;
}

dl::before,
dl::after {
  content: "---";
  grid-column: 1 / -1;
  color: var(--dim-color);
}

dt {
  grid-column: 1;
  color: var(--strong-color);
}

dt::after {
  content: ":";
  color: var(--dim-color);
}

dd {
  grid-column: 2;
  margin: 0;
}

/* A heading with metadata: <header><h1>…</h1><dl>…</dl></header>. The dl sits
 * directly under the heading as a dim byline, without the `---` rows. */

:is(main, article, section) > header {
  margin-block-end: var(--row);
}

header > :is(h1, h2, h3, h4, h5, h6):has(+ dl) {
  margin-block-end: 0;
}

header > dl {
  color: var(--dim-color);
}

header > dl::before,
header > dl::after {
  content: none;
}

header > dl dt {
  color: inherit;
}

/* Block quotes: a "> " gutter that repeats on every line */

blockquote {
  position: relative;
  padding-inline-start: 2ch;
  overflow: hidden;
  color: var(--dim-color);
}

blockquote::before {
  content: ">\A>\A>\A>\A>\A>\A>\A>\A>\A>\A>\A>\A>\A>\A>\A>\A>\A>\A>\A>\A>\A>\A>\A>\A>\A>\A>\A>\A>\A>\A>\A>\A>\A>\A>\A>\A>\A>\A>\A>";
  position: absolute;
  inset-block: 0;
  inset-inline-start: 0;
  white-space: pre;
}

blockquote :is(p, li) {
  color: var(--text-color);
}

blockquote footer,
blockquote cite {
  color: var(--dim-color);
}

blockquote footer::before {
  content: "-- ";
}

fieldset {
  border: 0;
  padding: 0;
}

/* Code blocks: fenced with ```, plus the language from `data-lang` */

pre {
  overflow-x: auto;
  line-height: var(--row);
  tab-size: 2;
}

pre::before,
pre::after {
  content: "```";
  display: block;
  color: var(--dim-color);
}

pre[data-lang]::before {
  content: "```" attr(data-lang);
}

pre code {
  color: inherit;
}

/* Tables: GitHub-style pipe tables, with a |---| row under the header */

table {
  /* Block display lets wide tables scroll on their own instead of the page */
  display: block;
  max-width: 100%;
  width: fit-content;
  overflow-x: auto;
  border-collapse: collapse;
  font-variant-numeric: tabular-nums;
}

th,
td {
  position: relative;
  padding: 0 1ch 0 2ch;
  text-align: start;
  vertical-align: top;
}

th {
  color: var(--strong-color);
  font-weight: 600;
}

:is(th, td)::before,
tr::after {
  content: "|";
  color: var(--dim-color);
  white-space: pre;
}

:is(th, td)::before {
  position: absolute;
  inset-block-start: 0;
  inset-inline-start: 0;
}

/* The trailing pipe gets its own anonymous one-column cell */
tr::after {
  display: table-cell;
}

thead th::before,
thead tr::after {
  content: "|\A|";
}

thead th {
  padding-block-end: var(--row);
}

thead th::after {
  content: "--------------------------------------------------------------------------------------------------------";
  position: absolute;
  inset-inline: 1ch 0;
  inset-block-end: 0;
  overflow: hidden;
  white-space: nowrap;
  color: var(--dim-color);
}

caption {
  caption-side: bottom;
  text-align: start;
  color: var(--dim-color);
}

/* Sortable tables (with md.js): each header is a button, and the sorted
 * column shows an arrow from its aria-sort state. The arrow slot is always
 * reserved so columns don't shift when the sort changes. */

th > button,
th > button:hover:not(:disabled) {
  padding: 0;
  background: none;
  color: inherit;
  font-weight: inherit;
  text-align: inherit;
}

th > button::before {
  content: none;
}

th > button::after {
  content: "";
  display: inline-block;
  width: 2ch;
  text-align: end;
  color: var(--accent-color);
}

th > button:hover:not(:disabled) {
  color: var(--accent-color);
}

th[aria-sort="ascending"] > button::after {
  content: "↑";
}

th[aria-sort="descending"] > button::after {
  content: "↓";
}

address {
  font-style: normal;
}

/* Figures */

figure > :not(figcaption) {
  margin-block-end: 0;
}

figure > :first-child {
  margin-block-start: 0;
}

figcaption {
  color: var(--dim-color);
}

figcaption::before {
  content: "^ ";
}

/* Details: [+] / [-] toggles */

summary {
  display: block;
  cursor: pointer;
  list-style: none;
  color: var(--strong-color);
}

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

summary::before {
  content: var(--details-content);
  color: var(--accent-color);
}

details[open] > summary::before {
  content: var(--details-content-open);
}

details > :not(summary) {
  margin-inline-start: 4ch;
}

details[open] > summary {
  margin-block-end: var(--row);
}

/* Dialogs: a dashed frame. It is an inset outline rather than a border so it
 * takes up no space and the box stays a whole number of rows tall. */

dialog {
  width: min(60ch, 100% - 4ch);
  max-height: calc(100% - var(--row) * 4);
  margin: auto;
  padding: var(--row) 2ch;
  border: 0;
  outline: 1px dashed var(--dim-color);
  outline-offset: -1px;
  background-color: var(--background-color);
  color: var(--text-color);
}

dialog:focus-visible {
  outline: 1px dashed var(--dim-color);
  outline-offset: -1px;
}

dialog > header {
  display: flex;
  justify-content: space-between;
  align-items: start;
  gap: 2ch;
  margin-block-end: var(--row);
}

dialog > header :is(h1, h2, h3, h4, h5, h6) {
  margin: 0;
  padding: 0;
}

/* A footer, or a dialog form holding only buttons, is a right-aligned button row */
dialog footer,
dialog form[method="dialog"]:not(:has(> :not(button))) {
  display: flex;
  justify-content: end;
  gap: 1ch;
  margin-block-start: var(--row);
}

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