/**
 * Dark-first monospace grid stylesheet
 * Adapted from The Monospace Web by Oskar Wickström (MIT)
 * https://github.com/owickstrom/the-monospace-web
 */
@import url('https://fonts.cdnfonts.com/css/jetbrains-mono-2');

/* === VARIABLES === */
:root {
  --font-family: "JetBrains Mono", monospace;
  --line-height: 1.20rem;
  --border-thickness: 2px;
  --text-color: #fff;
  --text-color-alt: #aaa;
  --background-color: #000;
  --background-color-alt: #111;
  --font-weight-normal: 500;
  --font-weight-medium: 600;
  --font-weight-bold: 800;

  font-family: var(--font-family);
  font-optical-sizing: auto;
  font-weight: var(--font-weight-normal);
  font-style: normal;
  font-variant-numeric: tabular-nums lining-nums;
  font-size: 16px;
}

/* === BASE === */
* { box-sizing: border-box; }
* + * { margin-top: var(--line-height); }

html {
  display: flex;
  width: 100%;
  margin: 0;
  padding: 0;
  flex-direction: column;
  align-items: center;
  background: var(--background-color);
  color: var(--text-color);
}

body {
  position: relative;
  width: 100%;
  margin: 0;
  padding: var(--line-height) 2ch;
  max-width: calc(min(80ch, round(down, 100%, 1ch)));
  line-height: var(--line-height);
  overflow-x: hidden;
}

@media screen and (max-width: 480px) {
  :root { font-size: 14px; }
  body { padding: var(--line-height) 1ch; }
  .width-min { white-space: normal; }
}

/* === TYPOGRAPHY === */
h1, h2, h3, h4, h5, h6 {
  font-weight: var(--font-weight-bold);
  margin: calc(var(--line-height) * 2) 0 var(--line-height);
  line-height: var(--line-height);
}

h1 {
  font-size: 2rem;
  line-height: calc(2 * var(--line-height));
  margin-bottom: calc(var(--line-height) * 2);
  text-transform: uppercase;
}

h2 {
  font-size: 1rem;
  text-transform: uppercase;
}

p {
  margin-bottom: var(--line-height);
  word-break: break-word;
  word-wrap: break-word;
  hyphens: auto;
}

strong { font-weight: var(--font-weight-bold); }
em { font-style: italic; }

a { text-decoration-thickness: var(--border-thickness); }
a:link, a:visited { color: var(--text-color); }

/* === HORIZONTAL RULE === */
hr {
  position: relative;
  display: block;
  height: var(--line-height);
  margin: calc(var(--line-height) * 1.5) 0;
  border: none;
  color: var(--text-color);
}
hr:after {
  display: block;
  content: "";
  position: absolute;
  top: calc(var(--line-height) / 2 - var(--border-thickness));
  left: 0;
  width: 100%;
  border-top: calc(var(--border-thickness) * 3) double var(--text-color);
  height: 0;
}

/* === TABLES === */
table {
  position: relative;
  top: calc(var(--line-height) / 2);
  width: calc(round(down, 100%, 1ch));
  border-collapse: collapse;
  margin: 0 0 calc(var(--line-height) * 2);
}

th, td {
  border: var(--border-thickness) solid var(--text-color);
  padding:
    calc((var(--line-height) / 2))
    calc(1ch - var(--border-thickness) / 2)
    calc((var(--line-height) / 2) - (var(--border-thickness)));
  line-height: var(--line-height);
  vertical-align: top;
  text-align: left;
}

table tbody tr:first-child > * {
  padding-top: calc((var(--line-height) / 2) - var(--border-thickness));
}

th { font-weight: 700; }
.width-min { width: 0%; white-space: nowrap; }
.width-fit { width: 0%; white-space: nowrap; }
@media screen and (max-width: 768px) { .width-fit { white-space: normal; } }
.width-auto { width: 100%; }

/* === HEADER TABLE === */
.header { margin-bottom: calc(var(--line-height) * 2); }
.header h1 { margin: 0; }
.header .subtitle { margin: 0; }
.header tr td:last-child { text-align: right; }

/* === LISTS === */
ul, ol {
  padding: 0;
  margin: 0 0 var(--line-height);
}

ul {
  list-style-type: square;
  padding: 0 0 0 2ch;
}

ol {
  list-style-type: none;
  counter-reset: item;
  padding: 0;
}

ol ul, ol ol, ul ol, ul ul {
  padding: 0 0 0 3ch;
  margin: 0;
}

ol li:before {
  content: counters(item, ".") ". ";
  counter-increment: item;
  font-weight: var(--font-weight-medium);
}

li { margin: 0; padding: 0; }
li::marker { line-height: 0; }

/* === DETAILS / COLLAPSIBLE === */
details {
  border: var(--border-thickness) solid var(--text-color);
  padding: calc(var(--line-height) - var(--border-thickness)) 1ch;
  margin-bottom: var(--line-height);
}

summary {
  font-weight: var(--font-weight-medium);
  cursor: pointer;
  list-style: none;
  margin: 0;
}

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

summary::before {
  content: '\25B6\0020';
}

details[open] > summary::before {
  content: '\25BC\0020';
}

details[open] > summary {
  margin-bottom: var(--line-height);
}

details :last-child { margin-bottom: 0; }
details:not([open]) > *:not(summary) { display: none !important; }
details:not([open]) { padding: 0 1ch; }
details:not([open]) > summary { padding: calc(var(--line-height) - var(--border-thickness)) 0; }

/* === CODE / PRE === */
pre {
  white-space: pre;
  overflow-x: auto;
  margin: var(--line-height) 0;
  overflow-y: hidden;
}

figure pre { margin: 0; }
pre, code { font-family: var(--font-family); }
code { font-weight: var(--font-weight-medium); }

/* === FIGURE === */
figure {
  margin: calc(var(--line-height) * 2) 3ch;
  overflow-x: auto;
  overflow-y: hidden;
}

figcaption {
  display: block;
  font-style: italic;
  margin-top: var(--line-height);
}

/* === IMAGES === */
img, video {
  display: block;
  width: 100%;
  object-fit: contain;
  overflow: hidden;
}

img {
  font-style: italic;
  color: var(--text-color-alt);
}

/* === EMBEDS === */
.embed {
  position: relative;
  width: 100%;
  padding-bottom: 56.25%; /* 16:9 */
  height: 0;
  overflow: hidden;
  margin-bottom: var(--line-height);
}

.embed iframe {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  border: none;
  margin: 0;
}

/* YouTube thumbnail fallback */
.embed-link {
  position: relative;
  display: block;
  width: 100%;
  overflow: hidden;
  margin-bottom: var(--line-height);
}

.embed-link img {
  width: 100%;
  display: block;
  aspect-ratio: 16 / 9;
  object-fit: cover;
}

.embed-link .play-btn {
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  width: 6ch;
  height: calc(var(--line-height) * 3);
  border: var(--border-thickness) solid var(--text-color);
  background: var(--background-color);
  color: var(--text-color);
  font: inherit;
  font-weight: var(--font-weight-bold);
  display: flex;
  align-items: center;
  justify-content: center;
  pointer-events: none;
  margin: 0;
}

.embed-link:hover .play-btn {
  background: var(--text-color);
  color: var(--background-color);
}

/* === GRID === */
.grid {
  --grid-cells: 0;
  display: flex;
  gap: 1ch;
  align-items: flex-start;
  width: calc(round(down, 100%, (1ch * var(--grid-cells)) - (1ch * var(--grid-cells) - 1)));
  margin-bottom: var(--line-height);
}

.grid > *,
.grid > input {
  flex: 0 0 calc(round(down, (100% - (1ch * (var(--grid-cells) - 1))) / var(--grid-cells), 1ch));
  margin-top: 0;
  padding-bottom: 0 !important;
}

.grid:has(> :last-child:nth-child(1)) { --grid-cells: 1; }
.grid:has(> :last-child:nth-child(2)) { --grid-cells: 2; }
.grid:has(> :last-child:nth-child(3)) { --grid-cells: 3; }
.grid:has(> :last-child:nth-child(4)) { --grid-cells: 4; }
.grid:has(> :last-child:nth-child(5)) { --grid-cells: 5; }
.grid:has(> :last-child:nth-child(6)) { --grid-cells: 6; }

/* === INPUTS === */
input, button, textarea {
  border: var(--border-thickness) solid var(--text-color);
  padding:
    calc(var(--line-height) / 2 - var(--border-thickness))
    calc(1ch - var(--border-thickness));
  margin: 0;
  font: inherit;
  font-weight: inherit;
  height: calc(var(--line-height) * 2);
  width: auto;
  overflow: visible;
  background: var(--background-color);
  color: var(--text-color);
  line-height: normal;
  -webkit-font-smoothing: inherit;
  -moz-osx-font-smoothing: inherit;
  -webkit-appearance: none;
}
input[type=checkbox],
input[type=radio] {
  display: inline-grid;
  place-content: center;
  vertical-align: top;
  width: 2ch;
  height: var(--line-height);
  background: transparent;
  cursor: pointer;
}
input[type=checkbox]:checked:before,
input[type=radio]:checked:before {
  content: "";
  width: 1ch;
  height: calc(var(--line-height) / 2);
  background: var(--text-color);
}
input[type=radio],
input[type=radio]:before {
  border-radius: 100%;
}
button:focus, input:focus {
  --border-thickness: 3px;
  outline: none;
}
input {
  width: calc(round(down, 100%, 1ch));
}
label {
  display: block;
  width: calc(round(down, 100%, 1ch));
  height: auto;
  line-height: var(--line-height);
  font-weight: var(--font-weight-medium);
  margin: 0;
}
label input { width: 100%; }

/* === BUTTONS === */
button {
  border: var(--border-thickness) solid var(--text-color);
  padding:
    calc(var(--line-height) / 2 - var(--border-thickness))
    calc(1ch - var(--border-thickness));
  margin: 0;
  font: inherit;
  font-weight: var(--font-weight-medium);
  height: calc(var(--line-height) * 2);
  width: auto;
  overflow: visible;
  background: transparent;
  color: var(--text-color);
  line-height: normal;
  text-transform: uppercase;
  cursor: pointer;
  -webkit-appearance: none;
}

button:hover, .btn:hover { background: rgba(255,255,255,0.05); }
button:active, .btn:active { transform: translate(2px, 2px); }

.btn {
  display: inline-block;
  border: var(--border-thickness) solid var(--text-color);
  padding:
    calc(var(--line-height) / 2 - var(--border-thickness))
    calc(1ch - var(--border-thickness));
  margin: 0;
  font: inherit;
  font-weight: var(--font-weight-medium);
  height: calc(var(--line-height) * 2);
  background: transparent;
  color: var(--text-color);
  line-height: normal;
  text-transform: uppercase;
  text-decoration: none;
  cursor: pointer;
  text-align: center;
}

/* === SUBSCRIPT === */
sub {
  position: relative;
  display: inline-block;
  margin: 0;
  vertical-align: sub;
  line-height: 0;
  width: calc(1ch / 0.75);
  font-size: .75rem;
}

/* === NAV === */
nav { color: var(--text-color-alt); }
nav a:link, nav a:visited { color: var(--text-color-alt); }
nav a.btn:link, nav a.btn:visited { color: var(--text-color); }
nav a:hover { color: var(--text-color); }

.nav-buttons {
  display: flex;
  flex-wrap: wrap;
  gap: 1ch;
}
.nav-buttons > * {
  margin-top: 0;
}

/* === DEBUG === */
.debug .debug-grid {
  --color: color-mix(in srgb, var(--text-color) 10%, var(--background-color) 90%);
  position: absolute;
  top: 0; left: 0; right: 0; bottom: 0;
  z-index: -1;
  background-image:
    repeating-linear-gradient(var(--color) 0 1px, transparent 1px 100%),
    repeating-linear-gradient(90deg, var(--color) 0 1px, transparent 1px 100%);
  background-size: 1ch var(--line-height);
  margin: 0;
}
.debug .off-grid {
  background: rgba(255, 0, 0, 0.1);
}
.debug-toggle-label {
  text-align: right;
}

/* === CHAOS === */

.chaos {
  --chaos-hue: 0;
}
html.chaos-active, html.chaos-active *, .chaos, .chaos * {
  cursor: url("data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAQAAAC1HAwCAAAAC0lEQVR42mNkYAAAAAYAAjCB0C8AAAAASUVORK5CYII=") 0 0, none !important;
}
.chaos-cursor {
  position: fixed;
  pointer-events: none;
  z-index: 10000;
  font-size: 28px;
  line-height: 1;
  margin: 0 !important;
  transform: translate(-50%, -50%);
}

/* Everything reacts — no transitions, instant */
.chaos h1, .chaos h2, .chaos h3,
.chaos p, .chaos li, .chaos span,
.chaos th, .chaos td,
.chaos a, .chaos button, .chaos .btn,
.chaos label, .chaos summary,
.chaos img, .chaos video, .chaos figure,
.chaos details, .chaos hr, .chaos nav,
.chaos pre, .chaos code {
  will-change: transform, filter;
}

/* Cursor glow — radial gradient that follows the mouse */
.chaos-glow {
  position: fixed;
  width: 400px;
  height: 400px;
  border-radius: 50%;
  pointer-events: none;
  z-index: 0;
  opacity: 0.15;
  mix-blend-mode: screen;
  margin: 0 !important;
  transform: translate(-50%, -50%);
}

/* Link hover — instant RGB split + flash */
.chaos a:link, .chaos a:visited {
  text-decoration-color: hsl(calc(var(--chaos-hue, 0)), 100%, 60%);
}

.chaos a:hover {
  color: hsl(calc(var(--chaos-hue, 0)), 100%, 65%) !important;
  text-shadow:
    -0.15ch 0 hsl(calc(var(--chaos-hue, 0) + 120), 100%, 50%),
     0.15ch 0 hsl(calc(var(--chaos-hue, 0) + 240), 100%, 50%);
  text-decoration-color: hsl(calc(var(--chaos-hue, 0) + 180), 100%, 50%);
  letter-spacing: 0.08ch;
}

.chaos .btn:hover {
  color: hsl(calc(var(--chaos-hue, 0)), 100%, 65%) !important;
  border-color: hsl(calc(var(--chaos-hue, 0)), 100%, 60%);
  background: hsla(calc(var(--chaos-hue, 0)), 100%, 60%, 0.1);
  text-shadow:
    -0.1ch 0 hsl(calc(var(--chaos-hue, 0) + 120), 100%, 50%),
     0.1ch 0 hsl(calc(var(--chaos-hue, 0) + 240), 100%, 50%);
  box-shadow:
    0 0 8px hsla(calc(var(--chaos-hue, 0)), 100%, 60%, 0.4),
    inset 0 0 8px hsla(calc(var(--chaos-hue, 0)), 100%, 60%, 0.1);
}

/* Title — letter spacing burst + hard RGB split */
.chaos .title:hover {
  letter-spacing: 0.4ch;
  color: hsl(calc(var(--chaos-hue, 0)), 100%, 65%);
  text-shadow:
    -0.2ch 0.05ch hsl(calc(var(--chaos-hue, 0) + 120), 100%, 50%),
     0.2ch -0.05ch hsl(calc(var(--chaos-hue, 0) + 240), 100%, 50%);
}

/* Summary toggle labels */
.chaos summary:hover {
  color: hsl(calc(var(--chaos-hue, 0) + 60), 90%, 65%);
  letter-spacing: 0.1ch;
  text-shadow:
    -0.1ch 0 hsl(calc(var(--chaos-hue, 0) + 180), 100%, 50%),
     0.1ch 0 hsl(calc(var(--chaos-hue, 0) + 300), 100%, 50%);
}

/* Trail dots — instant, no transition delays */
.chaos-trail-dot {
  position: fixed;
  pointer-events: none;
  z-index: 9999;
  mix-blend-mode: difference;
  margin: 0 !important;
  width: 2ch;
  height: calc(var(--line-height, 1.2rem) * 1.5);
}
