/* Shared safety floor for every selectable skin. Individual skins define their
   visual language; this layer keeps labels, code, and controls usable. */
html[data-skin] :is(
  p, li, a, button, input, select, label, summary, time, small, dt, dd, th, td,
  code, pre, kbd, samp, span
) {
  font-size: max(1rem, 1em) !important;
}

html[data-skin] :is(.prose code, .prose pre, .katex, .katex-display, .code-frame *) {
  font-size: max(1rem, 1em) !important;
}

html[data-skin] .theme-toggle[hidden] {
  display: none !important;
}

html[data-skin] :focus-visible {
  outline: 3px solid currentColor;
  outline-offset: 4px;
}

html[data-skin] .site-header,
html[data-skin] .site-footer,
html[data-skin] .masthead,
html[data-skin] .article__content,
html[data-skin] .page-prose {
  overflow: visible;
}

html[data-skin] .masthead,
html[data-skin] .masthead__copy {
  min-height: 0 !important;
}

/* A single dark code surface lets the complete Rouge palette stay legible. */
html[data-skin] .prose :is(.code-frame, .code-frame pre, .code-frame pre code, .code-frame__gutter, .highlight) {
  color: #f8fafc !important;
  background-color: #111827 !important;
}

html[data-skin] .prose .highlight span { background: transparent !important; }
html[data-skin] .prose .highlight :is(.c, .c1, .cm, .cp, .cs) { color: #cbd5e1 !important; }
html[data-skin] .prose .highlight .err { color: #fff !important; background: #be123c !important; }
html[data-skin] .prose .highlight :is(.k, .kc, .kd, .kn, .kp, .kr, .kt) { color: #67e8f9 !important; }
html[data-skin] .prose .highlight :is(.o, .ow) { color: #fde68a !important; }
html[data-skin] .prose .highlight :is(.n, .na, .nb, .nc, .nd, .ne, .nf, .ni, .nl, .nn, .no, .nt, .nv) { color: #f8fafc !important; }
html[data-skin] .prose .highlight :is(.nc, .nf) { color: #f9a8d4 !important; }
html[data-skin] .prose .highlight :is(.s, .s1, .s2, .sa, .sb, .sc, .sd, .se, .sh, .si, .sr, .ss, .sx) { color: #fde68a !important; }
html[data-skin] .prose .highlight :is(.m, .mb, .mf, .mh, .mi, .mo, .il) { color: #fdba74 !important; }
html[data-skin] .prose .highlight .gd { color: #fda4af !important; }
html[data-skin] .prose .highlight .gi { color: #86efac !important; }
html[data-skin] .prose .highlight :is(.gh, .gu) { color: #bfdbfe !important; }

/* Keep titles and header controls calm across every skin. */
html[data-skin] :is(.masthead h1, .index-heading h1, .page-heading h1, .error-shell h1) {
  font-size: clamp(2rem, 5vw, 4.5rem) !important;
  line-height: 1.05 !important;
}

html[data-skin] .record-head h1 {
  font-size: clamp(2rem, 4vw, 4rem) !important;
  line-height: 1.05 !important;
}

html[data-skin] .site-nav {
  align-items: start !important;
  grid-auto-rows: auto !important;
}

html[data-skin] .site-nav > :is(a, button, details),
html[data-skin] :is(.action, .record-actions a, .quick-filters a, .pagination a, .code-frame__actions button) {
  align-self: start;
  min-height: 2.75rem !important;
  padding-block: .35rem !important;
  line-height: 1.25 !important;
}

html[data-skin] .skin-menu__panel {
  position: static !important;
  width: 100% !important;
  max-width: 100% !important;
  box-sizing: border-box;
}

/* The unified header carries more routes than the writeup register did, so
   skin nav grids sized for six items would overlap. On wide screens every
   skin gets a wrapping flex nav instead of a fixed grid. */
@media (min-width: 68rem) {
  html[data-skin] .site-nav {
    display: flex !important;
    flex-wrap: wrap !important;
    grid-template-columns: none !important;
    grid-template-areas: none !important;
    grid-auto-flow: row !important;
    justify-content: flex-end !important;
  }

  html[data-skin] .site-nav > :is(a, details, .skin-menu) {
    grid-area: auto !important;
    width: auto !important;
    min-width: 7rem !important;
    max-width: none !important;
  }
}

/* Theme choice is an unobtrusive dice in the upper-right corner. Appearance
   lives separately in a lower-right dock, outside every theme header. Both
   controls are quiet glass buttons that adapt to light and dark. */
.skin-dice,
.theme-toggle {
  position: fixed !important;
  z-index: 90;
  display: grid !important;
  width: 2.6rem !important;
  min-width: 2.6rem !important;
  height: 2.6rem !important;
  min-height: 2.6rem !important;
  max-width: 2.6rem !important;
  max-height: 2.6rem !important;
  place-items: center;
  padding: 0 !important;
  color: CanvasText !important;
  background: color-mix(in srgb, Canvas 72%, transparent) !important;
  border: 1px solid color-mix(in srgb, CanvasText 55%, transparent) !important;
  border-radius: 50% !important;
  box-shadow: 0 .12rem .4rem color-mix(in srgb, CanvasText 22%, transparent);
  font: 700 1rem/1 system-ui, sans-serif !important;
  cursor: pointer;
  backdrop-filter: blur(.35rem);
  -webkit-backdrop-filter: blur(.35rem);
  transition: box-shadow .18s ease, transform .18s ease, border-color .18s ease;
}

.skin-dice {
  top: auto !important;
  left: auto !important;
  right: .8rem !important;
  bottom: 3.9rem !important;
  border-radius: 28% !important;
}

.theme-toggle {
  top: auto !important;
  left: auto !important;
  right: .8rem !important;
  bottom: .8rem !important;
}

.skin-dice__face {
  width: 1.35rem;
  height: 1.35rem;
}

.theme-toggle__icon {
  width: 1.3rem;
  height: 1.3rem;
}

.theme-toggle .theme-toggle__icon--moon {
  display: block;
}

.theme-toggle .theme-toggle__icon--sun {
  display: none;
}

html[data-theme="dark"] .theme-toggle .theme-toggle__icon--moon {
  display: none;
}

html[data-theme="dark"] .theme-toggle .theme-toggle__icon--sun {
  display: block;
}

@keyframes skin-dice-roll {
  0% { transform: rotate(0deg) scale(1); }
  45% { transform: rotate(210deg) scale(.8); }
  80% { transform: rotate(335deg) scale(1.08); }
  100% { transform: rotate(360deg) scale(1); }
}

.skin-dice.is-rolling .skin-dice__face {
  animation: skin-dice-roll .55s cubic-bezier(.3, .7, .3, 1);
}

.skin-dice:hover,
.skin-dice:focus-visible,
.theme-toggle:hover,
.theme-toggle:focus-visible {
  transform: translateY(-.1rem);
  border-color: color-mix(in srgb, CanvasText 85%, transparent) !important;
  box-shadow: 0 .22rem .65rem color-mix(in srgb, CanvasText 30%, transparent);
}

.skin-dice:active,
.theme-toggle:active {
  transform: translateY(0);
  box-shadow: 0 .08rem .25rem color-mix(in srgb, CanvasText 24%, transparent);
}

/* A desktop article reserves a readable prose track. Narrow screens stack the
   navigation rail and let code scroll inside its own frame, not the page. */
@media (min-width: 70rem) {
  html[data-skin] .record-page .article__content {
    min-width: 0;
  }
}

html[data-skin] .prose .code-frame__viewport,
html[data-skin] .prose .code-frame pre {
  overflow-x: auto !important;
  overflow-y: hidden !important;
}

html[data-skin] .prose .code-frame pre code {
  display: block;
  min-width: 80ch;
}

html[data-skin] .prose .katex-display {
  max-width: 100% !important;
  overflow: visible !important;
}

html[data-skin] .prose .katex-display > .katex {
  max-width: 100%;
  white-space: normal !important;
}

@media (forced-colors: active) {
  html[data-skin] .prose .highlight span {
    color: CanvasText !important;
    background: Canvas !important;
  }
}

@media (max-width: 70rem) {
  html[data-skin] .record-page .record-grid {
    grid-template-columns: minmax(0, 1fr) !important;
  }

  html[data-skin] .record-page .record-grid > .record-index {
    position: static !important;
    width: 100%;
  }
}

@media (max-width: 44rem) {
  html[data-skin] :is(.record-columns, .record-row__link, .record-facts, .site-footer__links) {
    min-width: 0;
  }
}

@media (prefers-reduced-motion: reduce) {
  html[data-skin] *,
  html[data-skin] *::before,
  html[data-skin] *::after {
    scroll-behavior: auto !important;
    transition-duration: .01ms !important;
    animation-duration: .01ms !important;
    animation-iteration-count: 1 !important;
  }

  .skin-dice.is-rolling .skin-dice__face {
    animation: none;
  }
}

/* Unified content geometry
   ------------------------
   Skins may paint these blocks, but they must not move shared content into
   implicit grid tracks or outside the viewport. Keep this contract complete:
   overriding a template without overriding its child placements caused the
   earlier article collisions. */
html[data-skin] :is(.home-shell, .archive-shell, .record-page, .page-shell, .error-shell) {
  box-sizing: border-box !important;
  width: min(calc(100% - 2rem), 96rem) !important;
  max-width: 96rem !important;
  margin-inline: auto !important;
  overflow-x: clip !important;
}

html[data-skin] :is(.home-shell, .archive-shell, .record-page) {
  display: block !important;
  grid-template: none !important;
}

html[data-skin] :is(
  .home-shell > .masthead,
  .home-shell > .record-index,
  .archive-shell > .index-heading,
  .archive-shell > .archive-tools,
  .archive-shell > .archive-groups,
  .archive-shell > .record-end,
  .record-page > .record-head,
  .record-page > .record-grid,
  .record-page > .record-end
) {
  position: relative !important;
  inset: auto !important;
  box-sizing: border-box !important;
  width: auto !important;
  max-width: none !important;
  margin-inline: 0 !important;
  transform: none !important;
  overflow-x: clip !important;
  grid-area: auto !important;
  grid-column: auto !important;
  grid-row: auto !important;
}

html[data-skin] .record-page > .record-head {
  display: grid !important;
  grid-template-columns: minmax(0, 1fr) minmax(16rem, 22rem) !important;
  grid-template-areas:
    "key facts"
    "title facts"
    "abstract facts"
    "tags tags"
    "actions actions" !important;
  gap: clamp(.75rem, 1.5vw, 1.25rem) clamp(1rem, 2.5vw, 2rem) !important;
  align-items: start !important;
}

html[data-skin] .record-head > .record-head__key { grid-area: key !important; }
html[data-skin] .record-head > h1 { grid-area: title !important; }
html[data-skin] .record-head > .record-head__abstract { grid-area: abstract !important; }
html[data-skin] .record-head > .record-facts { grid-area: facts !important; }
html[data-skin] .record-head > .tag-line { grid-area: tags !important; }
html[data-skin] .record-head > .record-actions { grid-area: actions !important; }

html[data-skin] .record-page > .record-grid {
  display: grid !important;
  grid-template-columns: minmax(0, 1fr) minmax(16rem, 22rem) !important;
  grid-template-areas: "article rail" !important;
  gap: clamp(1.25rem, 2.5vw, 2.5rem) !important;
  align-items: start !important;
}

html[data-skin] .record-grid > .article__content {
  grid-area: article !important;
  grid-column: auto !important;
  grid-row: auto !important;
  box-sizing: border-box !important;
  width: auto !important;
  max-width: none !important;
  min-width: 0 !important;
  margin-inline: 0 !important;
  transform: none !important;
  justify-self: stretch !important;
  align-self: start !important;
}

html[data-skin] .record-grid > .record-index {
  grid-area: rail !important;
  grid-column: auto !important;
  grid-row: auto !important;
  box-sizing: border-box !important;
  width: auto !important;
  max-width: none !important;
  min-width: 0 !important;
  margin-inline: 0 !important;
  transform: none !important;
  justify-self: stretch !important;
  align-self: start !important;
}

html[data-skin] :is(.site-brand__name, .masthead h1, .index-heading h1, .page-heading h1, .record-head h1) {
  overflow-wrap: normal !important;
  word-break: normal !important;
  hyphens: none !important;
}

@media (max-width: 54rem) {
  html[data-skin] :is(.home-shell, .archive-shell, .record-page, .page-shell, .error-shell) {
    width: 100% !important;
  }

  html[data-skin] .record-page > .record-head {
    grid-template-columns: minmax(0, 1fr) !important;
    grid-template-areas:
      "key"
      "title"
      "abstract"
      "facts"
      "tags"
      "actions" !important;
  }

  html[data-skin] .record-page > .record-grid {
    grid-template-columns: minmax(0, 1fr) !important;
    grid-template-areas: "article" "rail" !important;
  }

  html[data-skin] .record-row__link {
    display: grid !important;
    grid-template-columns: minmax(0, 1fr) !important;
    grid-template-areas: "date" "title" "event" "primitive" !important;
    width: auto !important;
    max-width: none !important;
  }

  html[data-skin] .record-row__date { grid-area: date !important; }
  html[data-skin] .record-row__title { grid-area: title !important; }
  html[data-skin] .record-row__event { grid-area: event !important; }
  html[data-skin] .record-row__primitive { grid-area: primitive !important; }

  html[data-skin] .record-row__link > :is(.record-row__date, .record-row__title, .record-row__event, .record-row__primitive) {
    grid-column: auto !important;
    grid-row: auto !important;
    width: auto !important;
    max-width: none !important;
    min-width: 0 !important;
    margin-inline: 0 !important;
    position: relative !important;
    inset: auto !important;
    transform: none !important;
  }
}

/* Keyboard-only skip link. Skin rules must not leave it covering the masthead. */
html[data-skin] .skip-link {
  position: fixed !important;
  z-index: 200 !important;
  top: 0 !important;
  left: 1rem !important;
  right: auto !important;
  bottom: auto !important;
  transform: translateY(calc(-100% - 1rem)) !important;
}

html[data-skin] .skip-link:focus-visible {
  transform: translateY(.75rem) !important;
}

html[data-skin] .article__content :is(h1, h2, h3, h4, h5, h6) {
  max-width: 100% !important;
  overflow-wrap: anywhere !important;
  word-break: break-word !important;
}

html[data-skin] .record-row__date {
  white-space: nowrap !important;
  overflow-wrap: normal !important;
  word-break: normal !important;
}

@media (max-width: 54rem) {
  /* On narrow screens the controls occupy a reserved top strip and scroll
     away with the document instead of covering article text. */
  html[data-skin] body {
    position: relative;
    padding-top: 3.35rem !important;
  }

  html[data-skin] :is(.skin-dice, .theme-toggle) {
    position: absolute !important;
    top: .4rem !important;
    bottom: auto !important;
  }

  html[data-skin] .skin-dice {
    right: 3.65rem !important;
  }

  html[data-skin] .theme-toggle {
    right: .6rem !important;
  }
}
