/* L1 design tokens for the Index design. Names follow AtomUI so its release theme can replace this file.
   Type is the reader's own book serif, everywhere: no web fonts are loaded.
   Colours are light-dark() pairs: the system preference picks one unless data-theme pins it. */
:root {
  color-scheme: light dark;
  --color-chassis: light-dark(#fbfaf6, #191814);
  --color-inset: light-dark(#f2f0e8, #24231e);
  --color-panel: light-dark(#fbfaf6, #191814);
  --color-edge: light-dark(#dcd8cc, #3a3831);
  --color-edge-strong: light-dark(#bdb8aa, #56534a);
  --color-text: light-dark(#1b1a17, #ebe7dc);
  --color-text-muted: light-dark(#6d6a62, #a8a397);
  --color-accent: light-dark(#7b7fd9, #9a9df0);
  --color-accent-press: light-dark(#5c60c4, #b9bbf6);
  --color-on-accent: light-dark(#fbfaf6, #191814);

  --heat-none: light-dark(#d9d4c7, #4a463d);
  --heat-fuzzy-low: light-dark(#babce8, #4d517f);
  --heat-fuzzy-high: light-dark(#7277c4, #888cc4);
  --heat-exact: light-dark(#1b1a17, #d6d0bd);


  --space-1: 0.25rem;
  --space-2: 0.5rem;
  --space-3: 1rem;
  --space-4: 1.5rem;
  --space-5: 2.5rem;
  --space-6: 4rem;

  --font-family-text: 'Iowan Old Style', 'Palatino Linotype', Palatino, 'Book Antiqua', Charter, 'Bitstream Charter', Georgia, serif;
  --font-family-mono: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
  --font-size-xs: 0.78rem;
  --font-size-sm: 0.88rem;
  --font-size-md: clamp(15px, 14px + 0.15vw, 16.5px);
  --font-size-lg: 1.15rem;
  --font-size-xl: 1.45rem;
  --font-size-3xl: clamp(2.2rem, 1.6rem + 2.5vw, 3.4rem);
  --line-height-prose: 1.6;
  --line-height-tight: 1.15;
  --tracking-label: 0.2em;
  --radius: 0;

  --measure: 40rem;
  --index-width: 19rem;
}

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

:root[data-theme='dark'] {
  color-scheme: dark;
}

/* AtomUI atoms as plain CSS. Container owns every layout through flexbox (Layout.Axis, Gap). */
*,
*::before,
*::after {
  box-sizing: border-box;
  margin: 0;
  padding: 0;
}

html {
  font-family: var(--font-family-text);
  font-size: var(--font-size-md);
  line-height: var(--line-height-prose);
  color: var(--color-text);
  background: var(--color-chassis);
  -webkit-text-size-adjust: 100%;
  text-rendering: optimizeLegibility;
}

a {
  color: var(--color-accent);
  text-decoration: underline;
  text-decoration-thickness: 1px;
  text-underline-offset: 0.18em;
}

a:hover {
  color: var(--color-accent-press);
}

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

img {
  display: block;
  max-width: 100%;
  height: auto;
}

[hidden] {
  display: none !important;
}

/* Container: Layout.Axis and Gap */
.container {
  display: flex;
  min-width: 0;
}

.axis-row {
  flex-direction: row;
}

.axis-column {
  flex-direction: column;
}

.gap-1 {
  gap: var(--space-1);
}

.gap-3 {
  gap: var(--space-3);
}

/* Table */
.table {
  width: 100%;
  border-collapse: collapse;
  font-family: var(--font-family-mono);
  font-size: var(--font-size-xs);
  font-variant-numeric: tabular-nums;
}

.table th,
.table td {
  padding: var(--space-2) var(--space-2);
  border-bottom: 1px solid var(--color-edge);
  text-align: right;
  white-space: nowrap;
}

.table th:first-child,
.table td:first-child,
.table th:nth-child(2),
.table td:nth-child(2) {
  text-align: left;
}

.table th {
  font-weight: 500;
  color: var(--color-text-muted);
}

/* Button */
.button {
  display: inline-flex;
  align-items: center;
  padding: var(--space-1) var(--space-3);
  font-family: var(--font-family-mono);
  font-size: var(--font-size-sm);
  color: var(--color-text);
  text-decoration: none;
  background: transparent;
  border: 1px solid var(--color-edge-strong);
  border-radius: var(--radius);
  cursor: pointer;
}

.button:hover {
  color: var(--color-on-accent);
  background: var(--color-accent);
  border-color: var(--color-accent);
}

/* TextInput */
.text-input {
  width: 100%;
  padding: var(--space-2);
  font-family: var(--font-family-mono);
  font-size: var(--font-size-sm);
  color: var(--color-text);
  background: var(--color-panel);
  border: 1px solid var(--color-edge-strong);
  border-radius: var(--radius);
}

/* L2 components for the Index design: the book index beside every page, the text column and the ROM memory map. */
body {
  background: var(--color-chassis);
}

a {
  color: inherit;
  text-decoration-color: var(--color-accent);
  text-decoration-thickness: 1px;
  text-underline-offset: 0.15em;
}

.book {
  display: flex;
  flex-direction: column;
  max-width: 68rem;
  margin: 0 auto;
}

/* The index: one serif at one size and weight; plain labels, nested sections indented under them */
.index {
  display: flex;
  flex-direction: column;
  gap: var(--space-3);
  padding: var(--space-4) var(--space-4) var(--space-3);
  line-height: 1.75;
  border-bottom: 1px solid var(--color-edge);
}

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

.index a:hover {
  color: var(--color-accent-press);
  text-decoration: underline;
  text-decoration-color: var(--color-accent);
}

.index a[aria-current='page'] {
  color: var(--color-accent-press);
}

.brand {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: var(--space-3);
}

/* The site title: the index serif, lowercase, untracked, on the index line height */
.index .name {
  font-weight: 700;
  letter-spacing: normal;
  text-transform: none;
}

.index .name:hover {
  color: var(--color-text);
  text-decoration: none;
}

.caret {
  animation: caret 1.6s steps(1) infinite;
}

@keyframes caret {
  50% {
    opacity: 0;
  }
}

@media (prefers-reduced-motion: reduce) {
  .caret {
    animation: none;
  }
}

.theme-toggle {
  display: flex;
  padding: var(--space-1);
  color: var(--color-text-muted);
  background: none;
  border: 0;
  cursor: pointer;
}

.theme-toggle:hover {
  color: var(--color-accent-press);
}

.role,
.links {
  font-size: var(--font-size-sm);
  color: var(--color-text-muted);
  text-wrap: balance;
}

.links {
  display: none;
  flex-wrap: wrap;
  gap: 0 var(--space-3);
}

.index .links a {
  color: var(--color-text-muted);
}

/* Phone: one clean line of labels; nested sections are laptop only */
.entries {
  display: flex;
  flex-wrap: wrap;
  gap: 0 var(--space-4);
  list-style: none;
}

.entry ul {
  display: none;
}

@media (min-width: 60rem) {
  .book {
    flex-direction: row;
    align-items: flex-start;
  }

  .index {
    position: sticky;
    top: 0;
    flex: 0 0 var(--index-width);
    max-height: 100vh;
    overflow-y: auto;
    gap: var(--space-4);
    padding: var(--space-6) var(--space-5);
    border-bottom: 0;
    border-right: 1px solid var(--color-edge);
  }

  .entries {
    display: block;
  }

  .entry ul {
    display: block;
    padding-left: var(--space-3);
    list-style: none;
  }

  .entry ul a {
    color: var(--color-text-muted);
  }

  .links {
    display: flex;
  }
}

/* The text column */
.page {
  display: flex;
  flex-direction: column;
  flex: 1 1 auto;
  gap: var(--space-3);
  min-width: 0;
  max-width: calc(var(--measure) + 2 * var(--space-6));
  padding: var(--space-5) var(--space-4) var(--space-6);
  line-height: var(--line-height-prose);
}

@media (min-width: 60rem) {
  .page {
    padding: var(--space-6) var(--space-6) 8rem;
  }
}

.page-head h1 {
  font-size: var(--font-size-3xl);
  font-weight: 400;
  line-height: var(--line-height-tight);
}

.sub {
  font-style: italic;
  color: var(--color-text-muted);
}

.back {
  font-size: var(--font-size-sm);
  color: var(--color-text-muted);
}

.prose {
  display: flex;
  flex-direction: column;
  gap: var(--space-3);
  max-width: var(--measure);
}

.prose h2,
.page section > h2 {
  margin-top: var(--space-4);
  padding-top: var(--space-4);
  font-size: var(--font-size-xl);
  font-weight: 400;
  border-top: 1px solid var(--color-edge);
}

.prose h2:target {
  color: var(--color-accent-press);
}

.prose ul {
  padding-left: 1.3em;
}

.prose iframe {
  width: 100%;
  height: 80vh;
  border: 1px solid var(--color-edge);
}

.prose .actions {
  display: flex;
  flex-wrap: wrap;
  gap: var(--space-2);
}

.button {
  padding: 0.35em 0.9em;
  font-family: var(--font-family-text);
  font-size: var(--font-size-sm);
  letter-spacing: 0;
  color: var(--color-text);
  text-decoration: none;
  border: 1px solid var(--color-text);
}

.button:hover {
  color: var(--color-on-accent);
  background: var(--color-text);
  border-color: var(--color-text);
}

/* Attic list */
.projects {
  display: flex;
  flex-direction: column;
  list-style: none;
  max-width: var(--measure);
  border-top: 1px solid var(--color-edge);
}

.projects a {
  display: flex;
  align-items: center;
  gap: var(--space-3);
  padding: var(--space-3) 0;
  text-decoration: none;
  border-bottom: 1px solid var(--color-edge);
}

.projects strong {
  font-size: var(--font-size-lg);
  font-weight: 400;
}

.projects a:hover strong {
  color: var(--color-accent-press);
}

/* Blog */
.posts {
  display: flex;
  flex-direction: column;
  gap: var(--space-2);
  list-style: none;
}

.posts li {
  display: flex;
  flex-wrap: wrap;
  gap: var(--space-3);
}

.posts time {
  flex: 0 0 10ch;
  color: var(--color-text-muted);
  font-variant-numeric: tabular-nums;
}

.posts li > span {
  display: flex;
  flex-direction: column;
}

.tags a {
  font-size: var(--font-size-sm);
  color: var(--color-text-muted);
}

/* Contact form */
.contact-form {
  max-width: 34rem;
  margin-top: var(--space-3);
}

.field span {
  font-size: var(--font-size-sm);
  color: var(--color-text-muted);
}

.text-input {
  font-family: var(--font-family-text);
  background: transparent;
  border: 1px solid var(--color-edge-strong);
  border-radius: 0;
}

/* Cake Unbake: spec rows, version lines and the ROM memory map */
.rom {
  display: flex;
  flex-direction: column;
  gap: var(--space-3);
}

.rom > h2 {
  margin-top: var(--space-4);
  padding-top: var(--space-4);
  font-family: var(--font-family-mono);
  font-size: var(--font-size-lg);
  font-weight: 400;
  border-top: 1px solid var(--color-edge);
}

.rom > h2 a {
  color: inherit;
  text-decoration: none;
}

.rom > h2 a:hover {
  color: var(--color-accent-press);
  text-decoration: underline;
  text-decoration-color: var(--color-accent);
}

.spec {
  display: flex;
  flex-direction: column;
  max-width: var(--measure);
  font-size: var(--font-size-sm);
}

.spec > div {
  display: flex;
  flex-wrap: wrap;
  gap: 0 var(--space-3);
  padding: 0.15em 0;
  border-bottom: 1px dotted var(--color-edge);
}

.spec > div > a {
  font-size: var(--font-size-sm);
}

.spec dt {
  flex: 0 0 9rem;
  font-style: italic;
  color: var(--color-text-muted);
}

.spec dd {
  flex: 1 1 14rem;
  min-width: 0;
  font-family: var(--font-family-mono);
  font-size: 0.92em;
  font-variant-numeric: tabular-nums;
  overflow-wrap: anywhere;
}

.figure {
  display: flex;
  flex-direction: column;
  gap: var(--space-2);
}

.rom .status {
  font-family: var(--font-family-mono);
  font-size: var(--font-size-xs);
  color: var(--color-text-muted);
}

/* Room for the map while it loads, so the page below it does not jump */
.figure > .status:only-child {
  min-height: 24rem;
}

.numbers {
  display: flex;
  flex-direction: column;
  gap: var(--space-3);
}

.map {
  container-type: inline-size;
  position: relative;
  display: flex;
  flex-direction: column;
  gap: 1px;
  font-family: var(--font-family-mono);
}

.seg {
  margin-top: var(--space-2);
  font-size: 0.62rem;
  color: var(--color-text-muted);
}

.row {
  display: flex;
  align-items: center;
  gap: var(--space-2);
}

.off {
  flex: 0 0 auto;
  font-size: 0.58rem;
  color: var(--color-text-muted);
  font-variant-numeric: tabular-nums;
}

.track {
  position: relative;
  flex: 1 1 auto;
  height: clamp(5px, 1.5cqw, 11px);
  background: var(--color-inset);
}

.track i {
  position: absolute;
  top: 0;
  bottom: 0;
}

.track i.s {
  box-shadow: inset -1px 0 0 var(--color-inset);
}

.track i.src {
  cursor: pointer;
}

.map.lit .track i {
  opacity: 0.55;
}

.map.lit .track i.on,
.map.lit .track i.sib {
  opacity: 1;
}

.track i.on {
  outline: 1px solid var(--color-text);
  z-index: 1;
}

.track i.sib {
  outline: 1px solid var(--color-accent);
}

.row.at .off {
  color: var(--color-accent-press);
}

.cross {
  position: absolute;
  top: 0;
  bottom: 0;
  width: 0;
  border-left: 1px dashed var(--color-accent);
  pointer-events: none;
  z-index: 3;
}

.readout {
  min-height: calc(1.6em + 2 * var(--space-1) + 1px);
  position: sticky;
  top: 0;
  z-index: 4;
  padding: var(--space-1) 0;
  font-family: var(--font-family-mono);
  font-size: var(--font-size-xs);
  color: var(--color-text);
  background: var(--color-chassis);
  border-bottom: 1px solid var(--color-edge);
}

.edge {
  position: absolute;
  top: -3px;
  bottom: -3px;
  width: 0;
  border-left: 1px solid var(--color-edge-strong);
  z-index: 2;
}

.track i.exact,
.scale i.exact {
  background: var(--heat-exact);
}

.track i.none,
.scale i.none {
  background: var(--heat-none);
}

.track i.fuzzy {
  background: color-mix(in oklab, var(--heat-fuzzy-low), var(--heat-fuzzy-high) var(--p));
}

/* The key: one bar from none through the fuzzy ramp to exact, 0 % under its left end and 100 % under its right. */
.legend {
  margin-top: var(--space-2);
  font-family: var(--font-family-mono);
  font-size: var(--font-size-xs);
  color: var(--color-text-muted);
}

.scale {
  display: grid;
  grid-template-columns: 2rem 10rem 2rem;
  column-gap: 2px;
  row-gap: var(--space-1);
  width: max-content;
}

.scale i {
  height: 0.5rem;
}

.scale i.fuzzy {
  background: linear-gradient(to right, var(--heat-fuzzy-low), var(--heat-fuzzy-high));
}

.scale span {
  white-space: nowrap;
}

.scale span:last-of-type {
  grid-column: 3;
  justify-self: end;
}

/* Version lines: one compact unwrapped row each. A narrow column stacks each version into three short lines
   (version and cartridge, then counts with short labels), so the table never scrolls sideways. */
.versions {
  container-type: inline-size;
}

.versions .table th,
.versions .table td {
  padding: var(--space-1) var(--space-2);
}

.versions .table th {
  font-family: var(--font-family-text);
  font-style: italic;
}

@container (max-width: 34rem) {
  .versions thead {
    position: absolute;
    width: 1px;
    height: 1px;
    overflow: hidden;
    clip-path: inset(50%);
  }

  .versions tr {
    display: grid;
    grid-template-columns: repeat(3, auto);
    grid-template-areas: 'v c c' 'f t z' 'b b a';
    justify-content: space-between;
    gap: 0 var(--space-3);
    padding: var(--space-2) 0;
    border-bottom: 1px solid var(--color-edge);
  }

  .versions .table td {
    padding: 0;
    text-align: left;
    border: 0;
  }

  .versions td:nth-child(1) { grid-area: v; }
  .versions td:nth-child(2) { grid-area: c; }
  .versions td:nth-child(3) { grid-area: f; }
  .versions td:nth-child(4) { grid-area: t; }
  .versions td:nth-child(5) { grid-area: b; }
  .versions td:nth-child(6) { grid-area: z; }
  .versions td:nth-child(7) { grid-area: a; }

  .versions td[data-label]::before {
    content: attr(data-label) ' ';
    font-family: var(--font-family-text);
    font-style: italic;
    color: var(--color-text-muted);
  }
}

/* Archive */
.archive {
  display: flex;
  flex-direction: column;
  gap: var(--space-3);
  max-width: var(--measure);
  list-style: none;
}

.archive li {
  display: flex;
  align-items: flex-start;
  gap: var(--space-3);
}

.icon {
  flex: 0 0 20px;
  width: 20px;
  height: 20px;
  margin-top: 0.2em;
}

.archive li > span {
  display: flex;
  flex-direction: column;
}

.note {
  font-size: var(--font-size-sm);
  color: var(--color-text-muted);
}

/* Dialogs: the resume and one function of the memory map, on the page's own paper */
.modal {
  margin: auto;
  width: min(44rem, calc(100vw - 2 * var(--space-3)));
  max-height: calc(100dvh - 2 * var(--space-3));
  padding: var(--space-3) var(--space-4) var(--space-4);
  font-family: var(--font-family-text);
  color: var(--color-text);
  background: var(--color-chassis);
  border: 1px solid var(--color-edge-strong);
}

.modal::backdrop {
  background: rgb(0 0 0 / 0.5);
}

.modal-head {
  display: flex;
  align-items: baseline;
  justify-content: space-between;
  gap: var(--space-3);
  margin-bottom: var(--space-2);
}

.modal h2 {
  margin: 0;
  font-size: var(--font-size-xl);
  font-weight: 400;
  overflow-wrap: anywhere;
}

.modal .close,
.modal .step {
  padding: var(--space-1) var(--space-2);
  font: inherit;
  font-size: var(--font-size-sm);
  color: var(--color-text);
  cursor: pointer;
  background: none;
  border: 1px solid var(--color-edge-strong);
}

.modal .step:disabled {
  cursor: default;
  opacity: 0.4;
}

.modal .close:hover,
.modal .step:not(:disabled):hover {
  border-color: var(--color-text);
}

.modal iframe {
  display: block;
  width: 100%;
  height: min(70dvh, 56rem);
  border: 1px solid var(--color-edge);
}

.modal-actions {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: var(--space-2) var(--space-3);
  margin: var(--space-3) 0 0;
  font-size: var(--font-size-sm);
}

.modal .spec dt {
  flex-basis: 5rem;
}

.fn-body {
  display: flex;
  flex-direction: column;
  gap: var(--space-3);
  margin-top: var(--space-3);
  min-width: 0;
}

.fn-body h3 {
  margin: 0 0 var(--space-1);
  font-size: var(--font-size-sm);
  font-style: italic;
  font-weight: 400;
  color: var(--color-text-muted);
}

.fn-what {
  margin: 0;
}

.fn-body pre {
  margin: 0;
  max-height: 20rem;
  padding: var(--space-2);
  overflow: auto;
  font-family: var(--font-family-mono);
  font-size: var(--font-size-xs);
  line-height: 1.5;
  background: var(--color-inset);
  border: 1px solid var(--color-edge);
}

.tk { color: var(--color-accent-press); }
.tc { color: var(--color-text-muted); font-style: italic; }
.ts { color: light-dark(#2f6b3a, #8fc79a); }
.tn { color: light-dark(#8a4b0f, #d9a566); }
.tp { color: light-dark(#6b3a8a, #b793d1); }

@media (max-width: 40rem) {
  .modal {
    padding: var(--space-3);
  }
}
