/* =============================================================================
   1. Imports & font faces
   ========================================================================== */
/* — Inter (variable, by Rasmus Andersson) — */
@font-face {
  font-family: "Inter";
  font-style: normal;
  font-weight: 100 900;
  font-display: swap;
  src: url("https://rsms.me/inter/font-files/InterVariable.woff2?v=4.1") format("woff2");
}

@font-face {
  font-family: "Inter";
  font-style: italic;
  font-weight: 100 900;
  font-display: swap;
  src: url("https://rsms.me/inter/font-files/InterVariable-Italic.woff2?v=4.1") format("woff2");
}

/* — Iosevka Extended (by Belleve Invis), official build via cdnjs.
     Family "Iosevka Web" + font-stretch:expanded re-exposed under a flat name. — */
@font-face {
  font-family: "Iosevka Extended";
  font-style: normal;
  font-weight: 400;
  font-display: swap;
  src: url("https://cdnjs.cloudflare.com/ajax/libs/Iosevka/11.1.1/iosevka/woff2/iosevka-extended.woff2") format("woff2");
}
@font-face {
  font-family: "Iosevka Extended";
  font-style: italic;
  font-weight: 400;
  font-display: swap;
  src: url("https://cdnjs.cloudflare.com/ajax/libs/Iosevka/11.1.1/iosevka/woff2/iosevka-extendeditalic.woff2") format("woff2");
}
@font-face {
  font-family: "Iosevka Extended";
  font-style: normal;
  font-weight: 700;
  font-display: swap;
  src: url("https://cdnjs.cloudflare.com/ajax/libs/Iosevka/11.1.1/iosevka/woff2/iosevka-extendedbold.woff2") format("woff2");
}

/* =============================================================================
   2. Design tokens
   --------------------------------------------------------------------------
   Single source of truth. Themed colors use the modern light-dark() function,
   so the dark palette lives next to the light one (no duplicated blocks).
   `color-scheme` on :root[data-theme="…"] forces the resolution.
   ========================================================================== */
:root {
  color-scheme: light dark;

  /* — Typography — */
  --font-sans: "Inter", -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
  --font-mono: "Iosevka Extended", ui-monospace, "SF Mono", "Cascadia Code", Consolas, "Liberation Mono", Menlo, monospace;
  --font: var(--font-sans);
  --mono: var(--font-mono);

  --fs-xs: 0.72em;
  --fs-sm: 0.85em;
  --fs-md: 0.9em;
  --fs-base: 1em;
  --fs-lg: 1.05em;
  --fs-h3: 1em;
  --fs-h2: 1.6em;
  --fs-h1: 3.2em;
  --fs-code: 0.875em;          /* inline + block code */
  --fs-code-inline: 0.9em;     /* exposed for legacy var(--code-font-size) */
  --code-font-size: var(--fs-code-inline);

  --lh-base: 1.5;
  --lh-tight: 1.4;
  --lh-flat: 1;

  --feature-stylistic: "ss01" 1, "ss02" 1, "ss03" 1, "ss08" 1;
  --feature-display: "ss01" 1, "ss03" 1, "ss04" 1;

  /* — Radii — */
  --radius-xs: 3px;
  --radius-sm: 4px;
  --radius-input: 8px;
  --radius: 10px;              /* primary card / button radius */
  --radius-pill: 999px;
  --code-radius: 0;

  /* — Borders — */
  --border-w: 1px;

  /* — Layout — */
  --shell-max: 64rem;
  --nav-w: 12.5rem;
  --menubar-h: 3.25rem;

  /* — Z-index scale — */
  --z-copy-btn: 1;
  --z-overlay: 2;
  --z-backdrop: 600;
  --z-drawer: 700;
  --z-menubar: 800;
  --z-floating: 850;

  /* — Motion — */
  --ease-out: cubic-bezier(0.4, 0, 0.2, 1);
  --t-fast: 120ms;
  --t-base: 200ms;
  --t-slow: 280ms;

  /* — Accent palette (theme-agnostic) — */
  --red: #f03009;
  --yellow: #fff310;
  --light-yellow: rgb(255, 255, 179);
  --blue: rgb(3, 102, 230);
  --green: rgb(38, 220, 78);

  --accent-success: #16a34a;
  --accent-error: #dc2626;
  --accent-mark-light: #f59e0b;
  --accent-mark-dark: #d97706;

  /* — Prism (always dark, regardless of theme) — */
  --prism-bg: #272822;
  --prism-fg: #f8f8f2;

  /* — Surfaces & text (light → dark via light-dark()) — */
  --bg:                      light-dark(#fff,                       #111);
  --fg:                      light-dark(#000,                       #fafafa);
  --fg-bright:               light-dark(#000,                       #fff);
  --fg-5:                    light-dark(rgba(0, 0, 0, 0.5),         rgba(255, 255, 255, 0.5));
  --fg-1:                    light-dark(rgba(0, 0, 0, 0.08),        rgba(255, 255, 255, 0.08));
  --muted:                   var(--fg-5);
  --border:                  light-dark(rgba(0, 0, 0, 0.15),        rgba(255, 255, 255, 0.18));
  --underline-color:         light-dark(rgba(0, 0, 0, 0.3),         rgba(255, 255, 255, 0.3));
  --underline-color-bright:  light-dark(rgba(0, 0, 0, 0.6),         rgba(255, 255, 255, 0.6));
  --unit-bg:                 light-dark(rgba(0, 0, 0, 0.08),        #333);
  --code-bg:                 var(--unit-bg);
  --blockquote-bg:           light-dark(rgba(0, 0, 0, 0.08),        rgba(255, 255, 255, 0.06));
  --blockquote-fg:           var(--fg);

  --link-color:              light-dark(var(--blue),                var(--light-yellow));
  --link-hover-color:        var(--link-color);
  --link-underline:          var(--underline-color);
  --selection-bg:            light-dark(rgba(3, 102, 230, 0.18),    rgba(255, 255, 179, 0.22));
  --active-bgcolor:          var(--yellow);

  /* — Derived surfaces — */
  --surface-subtle:          color-mix(in srgb, var(--bg) 96%, var(--fg) 4%);
  --surface-muted:           color-mix(in srgb, var(--bg) 94%, var(--fg) 6%);
  --surface-raised:          color-mix(in srgb, var(--bg) 92%, var(--fg) 8%);
  --surface-hover:           color-mix(in srgb, var(--fg) 8%, var(--bg));
  --surface-input:           var(--bg);
  --shadow-drawer:
    0 0 0 var(--border-w) color-mix(in srgb, var(--border) 50%, transparent),
    0 12px 40px           color-mix(in srgb, #000 18%, transparent);
  --shadow-floating:         0 4px 12px color-mix(in srgb, #000 10%, transparent);
}

/* =============================================================================
   3. Theme overrides
   --------------------------------------------------------------------------
   Forcing a scheme is enough — light-dark() resolves accordingly.
   ========================================================================== */
:root[data-theme="light"] { color-scheme: light; }
:root[data-theme="dark"]  { color-scheme: dark; }

/* =============================================================================
   4. Base
   ========================================================================== */
* { box-sizing: border-box; }

html {
  -webkit-text-size-adjust: 100%;
  text-size-adjust: 100%;
  font-synthesis: none;
  font-feature-settings: var(--feature-stylistic);
}

body {
  margin: 0;
  min-height: 100vh;
  background: var(--bg);
  color: var(--fg);
  font: 400 1rem/var(--lh-base) var(--font-sans);
  font-feature-settings: var(--feature-stylistic);
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
}

::selection { background: var(--selection-bg); }

body.nav-lock {
  overflow: hidden;
  touch-action: none;
  overscroll-behavior: none;
}

.loading,
.error { color: var(--muted); }

/* =============================================================================
   5. Shell layout
   ========================================================================== */
.shell {
  display: flex;
  align-items: flex-start;
  max-width: var(--shell-max);
  margin: 0 auto;
  min-height: 100vh;
}

.shell > #nav {
  flex: 0 0 var(--nav-w);
  position: sticky;
  top: 0;
  height: 100vh;
  height: 100dvh;
  overflow: auto;
  padding: 1.25rem 0.9rem 2rem 1.1rem;
  border-right: var(--border-w) solid var(--border);
  background: var(--surface-muted);
}

.shell > #content {
  flex: 1 1 0;
  min-width: 0;
  max-width: none;
  padding: 1.5rem 1.25rem 2.5rem 1.1rem;
}

/* =============================================================================
   6. Sidebar (nav tree + filter)
   ========================================================================== */
#nav nav { font-size: var(--fs-md); }

#nav ul {
  list-style: none;
  margin: 0;
  padding: 0;
}
#nav nav > ul > li { margin-bottom: 0.4rem; }
#nav ul ul {
  margin: 0.35rem 0 0 0.4rem;
  padding-left: 0.5rem;
  border-left: var(--border-w) solid color-mix(in srgb, var(--border) 60%, transparent);
}
#nav li { margin: 0.1rem 0 0.25rem; }

#nav a,
#nav span.label {
  display: block;
  line-height: var(--lh-tight);
  padding: 0.2rem 0.15rem;
  border-radius: var(--radius-sm);
  color: inherit;
  text-decoration: none;
}
#nav a[aria-current="page"] {
  font-weight: 600;
  background: color-mix(in srgb, var(--fg) 8%, transparent);
}
#nav a:hover,
#nav a:focus-visible {
  background: color-mix(in srgb, var(--fg) 5%, transparent);
  outline: none;
}
#nav a.branch { font-weight: 500; }

#nav span.label {
  color: var(--muted);
  font-weight: 600;
  font-size: var(--fs-md);
  margin-top: 0.2rem;
}

#nav mark {
  padding: 0 0.08em;
  border-radius: var(--radius-xs);
  background: light-dark(
    color-mix(in srgb, var(--accent-mark-light) 40%, var(--bg) 60%),
    color-mix(in srgb, var(--accent-mark-dark)  45%, var(--bg) 55%)
  );
  color: inherit;
  font: inherit;
}

/* — Sidebar-filter echo inside the rendered article — */
.filter-value {
  padding: 0 0.08em;
  border-radius: var(--radius-xs);
  background: light-dark(
    color-mix(in srgb, var(--accent-mark-light) 40%, var(--bg) 60%),
    color-mix(in srgb, var(--accent-mark-dark)  45%, var(--bg) 55%)
  );
  color: inherit;
  font: inherit;
}

.filter-value.is-active {
  outline: 1px solid color-mix(in srgb, var(--fg) 42%, transparent);
  outline-offset: 1px;
}

#nav .count {
  font-weight: 600;
  color: var(--muted);
  font-size: 0.92em;
  white-space: nowrap;
}

/* — Sidebar filter — */
#nav .search { margin: 0 0 0.85rem; }

#nav .search > label {
  position: absolute;
  width: 1px; height: 1px;
  padding: 0; margin: -1px;
  overflow: hidden;
  clip: rect(0, 0, 0, 0);
  white-space: nowrap;
  border: 0;
}
#nav .search > input {
  display: block;
  width: 100%;
  margin: 0 0 0.35rem;
  padding: 0.4rem 0.5rem;
  font: inherit;
  font-size: var(--fs-md);
  color: inherit;
  background: var(--surface-input);
  border: var(--border-w) solid var(--border);
  border-radius: var(--radius-input);
}
#nav .search > input::placeholder {
  color: var(--muted);
  opacity: 0.9;
}
#nav .search > input:focus {
  outline: 2px solid color-mix(in srgb, var(--fg) 35%, var(--border));
  outline-offset: 1px;
}
#nav .search > .status {
  margin: 0;
  min-height: 1.2em;
  font-size: 0.75rem;
  line-height: 1.3;
  color: var(--muted);
}

/* =============================================================================
   7. Mobile menubar + drawer
   ========================================================================== */
#menubar {
  display: none;
  position: fixed;
  top: 0; left: 0; right: 0;
  z-index: var(--z-menubar);
  align-items: center;
  gap: 0.5rem;
  min-height: calc(var(--menubar-h) + env(safe-area-inset-top, 0));
  padding: env(safe-area-inset-top, 0) 0.5rem 0.25rem 0.6rem;
  border-bottom: var(--border-w) solid var(--border);
  background: color-mix(in srgb, var(--bg) 94%, var(--fg) 4%);
  backdrop-filter: blur(8px);
}

#menubar > button {
  display: inline-flex;
  align-items: center;
  gap: 0.45rem;
  margin: 0;
  padding: 0.45rem 0.55rem;
  border: var(--border-w) solid var(--border);
  border-radius: var(--radius);
  background: color-mix(in srgb, var(--bg) 90%, var(--fg) 4%);
  color: inherit;
  font: inherit;
  font-weight: 600;
  font-size: 0.95rem;
  cursor: pointer;
  -webkit-tap-highlight-color: transparent;
}
#menubar > button:hover,
#menubar > button:focus-visible {
  background: var(--surface-hover);
  outline: none;
}

/* — Hamburger icon — */
#menubar > button > span:first-child {
  position: relative;
  display: flex;
  flex-direction: column;
  justify-content: center;
  gap: 5px;
  width: 1.15rem;
  height: 0.9rem;
}
#menubar > button > span:first-child > span {
  display: block;
  height: 2px;
  width: 100%;
  border-radius: 1px;
  background: currentColor;
  transition: transform var(--t-base) ease, opacity var(--t-base) ease;
}

html.nav-open #menubar > button > span:first-child { gap: 0; }
html.nav-open #menubar > button > span:first-child > span {
  position: absolute;
  left: 0; right: 0;
  top: 50%;
  margin-top: -1px;
  width: 100%;
}
html.nav-open #menubar > button > span:first-child > span:nth-child(1) { transform: rotate(45deg); }
html.nav-open #menubar > button > span:first-child > span:nth-child(2) { opacity: 0; transform: scale(0.2); }
html.nav-open #menubar > button > span:first-child > span:nth-child(3) { transform: rotate(-45deg); }

#menubar > span:last-child {
  font-weight: 700;
  font-size: var(--fs-lg);
  letter-spacing: -0.02em;
  flex: 1;
  min-width: 0;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

#backdrop {
  display: none;
  position: fixed;
  inset: 0;
  z-index: var(--z-backdrop);
  background: color-mix(in srgb, #0f172a 55%, transparent);
  -webkit-tap-highlight-color: transparent;
  touch-action: manipulation;
}
html.nav-open #backdrop {
  display: block;
  animation: backdrop-in var(--t-base) ease-out;
}
@keyframes backdrop-in {
  from { opacity: 0; }
  to   { opacity: 1; }
}

@media (max-width: 52rem) {
  #menubar:not([hidden]) { display: flex; }
  #menubar:not([hidden]) ~ .shell {
    padding-top: calc(var(--menubar-h) + env(safe-area-inset-top, 0));
  }

  .shell { flex-direction: column; }

  .shell > #nav {
    position: fixed;
    top: calc(var(--menubar-h) + env(safe-area-inset-top, 0));
    left: 0; bottom: 0;
    z-index: var(--z-drawer);
    width: min(20rem, 90vw);
    max-width: 100%;
    max-height: none;
    height: auto;
    margin: 0;
    padding: 0.9rem 0.9rem 2rem 1.1rem;
    padding-left: max(1.1rem, env(safe-area-inset-left, 0));
    border-right: var(--border-w) solid var(--border);
    border-bottom: none;
    background: var(--surface-muted);
    box-shadow: var(--shadow-drawer);
    overflow: auto;
    -webkit-overflow-scrolling: touch;
    overscroll-behavior: contain;
    transform: translate3d(-105%, 0, 0);
    transition: transform var(--t-slow) var(--ease-out);
    will-change: transform;
  }
  .shell > #nav[hidden]      { display: none; }
  .shell > #nav:not([hidden]) { display: block; }
  html.nav-open .shell > #nav:not([hidden]) { transform: translate3d(0, 0, 0); }
}

@media (max-width: 52rem) and (prefers-reduced-motion: reduce) {
  .shell > #nav { transition: none; }
  html.nav-open #backdrop { animation: none; }
}

@media (min-width: 52.01rem) {
  #backdrop[hidden] { display: none !important; }
}

/* =============================================================================
   8. Article (rendered markdown + UI blocks)
   ========================================================================== */
#content > article > :first-child,
.md > :first-child { margin-top: 0; }

.md {
  position: relative;
  margin: 0 0 1.25rem;
}

/* — Headings — */
#content > article > h1,
.md h1 {
  font-size: var(--fs-h1);
  font-weight: 600;
  font-style: italic;
  font-feature-settings: var(--feature-display);
  font-variation-settings: "opsz" 16.77, "wght" 400;
  font-optical-sizing: none;
  letter-spacing: -0.01em;
  line-height: var(--lh-flat);
  color: var(--fg-bright);
  margin: 0 0 1.2rem;
}
.md h2 {
  font-size: var(--fs-h2);
  font-weight: 600;
  font-style: italic;
  font-feature-settings: var(--feature-display);
  font-variation-settings: "opsz" 16.77, "wght" 500;
  font-optical-sizing: none;
  letter-spacing: -0.01em;
  line-height: 1.8rem;
  margin: 2.4rem 0 1.2rem;
}
.md h3 {
  font-size: var(--fs-h3);
  font-weight: 600;
  margin: 1.8rem 0 1.2rem;
}
.md h4,
.md h5,
.md h6 {
  font-weight: 640;
  margin: 1.2rem 0 0.6rem;
}
.md * + h1 { margin-top: 3.6rem; }
.md * + h2 { margin-top: 2.4rem; }
.md * + h3 { margin-top: 1.8rem; }
.md * + h4,
.md * + h5,
.md * + h6 { margin-top: 1.2rem; }
.md > h1:first-child,
.md > h2:first-child,
.md > h3:first-child,
.md > h4:first-child,
.md > h5:first-child,
.md > h6:first-child { margin-top: 0; }

.md h1, .md h2, .md h3, .md h4, .md h5, .md h6 {
  scroll-margin-top: 0.9rem;
  padding: 0.35rem 0 0.35rem 1.5rem;     /* internal breathing room inside bg-highlight */
  margin-left: -1.0625rem;
  background-color: var(--surface-muted);
}

/* — Heading anchor link — */
.md h1 > a, .md h2 > a, .md h3 > a, .md h4 > a, .md h5 > a, .md h6 > a {
  color: inherit;
  text-decoration: none;
}
.md h1 > a.heading-link,
.md h2 > a.heading-link,
.md h3 > a.heading-link,
.md h4 > a.heading-link,
.md h5 > a.heading-link,
.md h6 > a.heading-link {
  margin-left: 0.45em;
  color: var(--muted);
  text-decoration: none;
  font-weight: 500;
  opacity: 0;
  transition: opacity var(--t-fast) ease;
}
.md h1:hover > a.heading-link,
.md h2:hover > a.heading-link,
.md h3:hover > a.heading-link,
.md h4:hover > a.heading-link,
.md h5:hover > a.heading-link,
.md h6:hover > a.heading-link,
.md h1 > a.heading-link:focus-visible,
.md h2 > a.heading-link:focus-visible,
.md h3 > a.heading-link:focus-visible,
.md h4 > a.heading-link:focus-visible,
.md h5 > a.heading-link:focus-visible,
.md h6 > a.heading-link:focus-visible {
  opacity: 1;
}

/* — "Copy markdown" button — */
.md > button.copy-md-btn {
  position: absolute;
  top: 0;
  right: 0;
  z-index: var(--z-overlay);
  border: var(--border-w) solid var(--border);
  border-radius: 0.35rem;
  background: var(--surface-raised);
  color: var(--fg);
  font: inherit;
  font-size: var(--fs-xs);
  font-weight: 600;
  line-height: var(--lh-flat);
  padding: 0.28rem 0.45rem;
  cursor: pointer;
  opacity: 0.85;
}
.md > button.copy-md-btn:hover,
.md > button.copy-md-btn:focus-visible {
  opacity: 1;
  background: color-mix(in srgb, var(--bg) 82%, var(--fg) 18%);
  outline: none;
}
.md > button.copy-md-btn[data-state="success"] {
  border-color: color-mix(in srgb, var(--accent-success) 45%, var(--border));
}
.md > button.copy-md-btn[data-state="error"] {
  border-color: color-mix(in srgb, var(--accent-error) 45%, var(--border));
}

/* — Block elements — */
.md p { margin: 0 0 1.1rem; }
.md ul, .md ol { margin: 0 0 1.1rem; padding-left: 1.4em; }
.md li { margin-bottom: 0.25em; }
.md hr {
  border: 0;
  height: var(--border-w);
  background: var(--border);
  margin: 2.2rem 0;
}

/* — Links — */
.md a {
  color: var(--fg);
  text-decoration: underline;
  text-decoration-color: var(--underline-color);
  text-decoration-thickness: var(--border-w);
  text-underline-offset: 0.2em;
  text-decoration-skip-ink: auto;
  word-break: break-word;
  word-wrap: break-word;
}
.md a:hover {
  color: var(--link-hover-color);
  text-decoration-color: var(--link-hover-color);
}
.md a[href^="#"]:hover {
  text-decoration-style: dashed;
}

/* — Blockquote — */
.md blockquote {
  margin: 0 0 1.25rem;
  padding: 0.5em 1em;
  border-radius: 0.2em;
  background: var(--blockquote-bg);
  color: var(--blockquote-fg);
}
.md blockquote > :last-child { margin-bottom: 0; }

/* — Tables —
   Always render as a real CSS table at 100% width. Cell content wraps
   (including long words / URLs / inline code) instead of triggering a
   horizontal scroller — important for print where scrollers can't render. */
.md table {
  display: table;
  width: 100%;
  table-layout: auto;
  border-collapse: collapse;
  border-top: var(--border-w) solid var(--border);
  border-right: var(--border-w) solid var(--border);
  margin: 0 0 1.5rem;
}
.md th,
.md td {
  padding: 0.5em 0.75em;
  border-bottom: var(--border-w) solid var(--border);
  border-left: var(--border-w) solid var(--border);
  line-height: var(--lh-base);
  vertical-align: top;
  text-align: left;
  overflow-wrap: anywhere;
  word-break: break-word;
}
.md th { font-weight: 600; }

/* Inline code / <pre> inside cells default to `white-space: pre` and refuse
   to wrap — override so they break to the next line within tables. */
.md table :not(pre) > code,
.md table tt {
  white-space: pre-wrap;
  word-break: break-word;
}
.md table pre {
  white-space: pre-wrap;
  word-break: break-word;
}

/* — Code (inline + block) — */
.md pre,
.md code,
.md tt,
.md kbd {
  font-family: var(--font-mono);
  font-feature-settings: "calt" 0;
  line-height: inherit;
}

/* Reset Prism defaults. Selectors deliberately match original specificity
   (0,1,1) so `.md pre { padding: 0.75rem 1rem }` below wins by source order. */
.md code[class*=language-],
pre[class*=language-] {
  font-family: var(--font-mono);
  border-radius: 0;
  padding: 0;
  text-shadow: none !important;
  text-decoration: none !important;
  text-decoration-thickness: var(--border-w) !important;
  text-underline-offset: xxx-small !important;
  text-decoration-skip-ink: auto !important;
  white-space: pre;
  word-break: normal;
  word-wrap: normal;
}

.md :not(pre) > code,
.md tt {
  background: var(--unit-bg);
  border-radius: 0.3em;
  padding: 0 0.3em;
  white-space: pre;
  font-size: var(--fs-code-inline);
}
.md h1 code, .md h2 code, .md h3 code,
.md h4 code, .md h5 code, .md h6 code {
  background: none;
  border-radius: 0;
  padding: 0;
  font-weight: inherit;
  font-size: 0.9em;
}
.md kbd {
  border: var(--border-w) solid var(--border);
  border-radius: var(--radius-xs);
  padding: 0.1em 0.3em;
  margin: 0 0.1em;
  background: transparent;
  font-feature-settings: "liga" 1, "calt" 1, "kern" 1, "case" 1;
}
.md pre {
  background: var(--code-bg);
  border-radius: var(--code-radius);
  padding: 0.75rem 1rem;
  overflow-x: auto;
  overflow-y: hidden;
  line-height: var(--lh-base);
  white-space: pre;
  line-break: auto;
}
.md pre > code {
  background: none;
  padding: 0;
  font-weight: inherit;
  font-size: var(--fs-code);
  white-space: pre;
}

/* — Prism (always dark) — */
.md pre.prism {
  background: var(--prism-bg);
  color: var(--prism-fg);
  position: relative;
  padding-top: 2.2rem;
}
.md pre.prism > code {
  color: inherit;
  background: transparent;
}
.md pre.prism > button.copy-btn {
  position: absolute;
  top: 0.45rem;
  right: 0.55rem;
  z-index: var(--z-copy-btn);
  border: var(--border-w) solid color-mix(in srgb, #fff 24%, transparent);
  background: color-mix(in srgb, #000 20%, transparent);
  color: var(--prism-fg);
  font: inherit;
  font-size: 0.78rem;
  font-weight: 600;
  line-height: var(--lh-flat);
  padding: 0.3rem 0.45rem;
  cursor: pointer;
  opacity: 0.9;
}
.md pre.prism > button.copy-btn:hover,
.md pre.prism > button.copy-btn:focus-visible {
  opacity: 1;
  background: color-mix(in srgb, #000 36%, transparent);
  outline: none;
}
.md pre.prism > button.copy-btn[data-state="success"] {
  border-color: color-mix(in srgb, #86efac 60%, transparent);
  color: #bbf7d0;
}
.md pre.prism > button.copy-btn[data-state="error"] {
  border-color: color-mix(in srgb, #fca5a5 60%, transparent);
  color: #fecaca;
}

/* =============================================================================
   9. UI block (forms inside markdown)
   ========================================================================== */
.ui { margin: 1.5rem 0; }

.ui > form,
.ui > div {
  border-radius: var(--radius);
  padding: 1rem 1.1rem 1.15rem;
  background: var(--surface-subtle);
}
.ui > div.empty {
  background: transparent;
  color: var(--muted);
  font-style: italic;
}

.ui .stack {
  display: flex;
  flex-direction: column;
  gap: 0.85rem;
}

.ui .control > label {
  display: block;
  font-weight: 600;
  margin-bottom: 0.3rem;
}
.ui .control > input,
.ui .control > textarea,
.ui .control > select {
  width: 100%;
  max-width: 28rem;
  font: inherit;
  padding: 0.4rem 0.55rem;
  border: var(--border-w) solid var(--border);
  border-radius: var(--radius-input);
  background: var(--surface-input);
  color: inherit;
}
.ui .control > textarea {
  min-height: 5rem;
  resize: vertical;
}

.ui .control.checkbox > label,
.ui .control.radio > label {
  display: inline-flex;
  align-items: center;
  gap: 0.45rem;
  font-weight: 400;
  margin: 0;
}

.ui .control > button {
  display: inline-flex;
  align-items: center;
  padding: 0.45rem 0.95rem;
  border: var(--border-w) solid var(--border);
  border-radius: var(--radius);
  background: var(--surface-raised);
  font: inherit;
  font-weight: 600;
  cursor: pointer;
}
.ui .control > button:hover { filter: brightness(1.05); }

@media (prefers-reduced-motion: reduce) {
  .ui .control > button:hover {
    filter: none;
    background-color: var(--bg) 50%;
  }
}

.ui fieldset {
  border: var(--border-w) solid var(--border);
  border-radius: var(--radius);
  margin: 0;
  padding: 0.75rem 0.9rem 0.9rem;
}
.ui fieldset.nested {
  border-style: dashed;
  border-radius: var(--radius);
  padding: 0.4rem 0.65rem 0.5rem;
}
.ui fieldset > legend {
  padding: 0 0.2rem;
  font-weight: 600;
}

.ui .group   { display: contents; }
.ui .unknown { color: var(--muted); font-style: italic; }

/* =============================================================================
   10. Mermaid output
   ========================================================================== */
.mermaid {
  margin: 0 0 1.25rem;
  padding: 0.25rem 0;
  max-width: 100%;
  overflow-x: auto;
  overflow-y: hidden;
  border-radius: 0;
  background: transparent;
  border: var(--border-w) solid var(--border);
}
.mermaid svg {
  max-width: 100%;
  height: auto;
  display: block;
  margin-left: auto;
  margin-right: auto;
  font-family: var(--font-sans);
  color: var(--fg);
  background: transparent;
}
.mermaid.err {
  font-size: 0.85em;
  font-family: var(--font-mono);
  color: light-dark(#a00, #c00);
  white-space: pre-wrap;
}

/* =============================================================================
   11. Theme switcher button
   ========================================================================== */
#theme-btn {
  position: fixed;
  top: calc(env(safe-area-inset-top, 0) + 0.6rem);
  right: calc(env(safe-area-inset-right, 0) + 0.75rem);
  z-index: var(--z-floating);
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 2.4rem;
  height: 2.4rem;
  margin: 0;
  padding: 0;
  border: var(--border-w) solid var(--border);
  border-radius: var(--radius-pill);
  background: color-mix(in srgb, var(--bg) 88%, var(--fg) 8%);
  color: inherit;
  cursor: pointer;
  -webkit-tap-highlight-color: transparent;
  backdrop-filter: blur(8px);
  box-shadow: var(--shadow-floating);
  transition: background var(--t-fast) ease, transform var(--t-fast) ease;
}
#theme-btn:hover         { background: color-mix(in srgb, var(--fg) 12%, var(--bg)); }
#theme-btn:focus-visible {
  outline: 2px solid color-mix(in srgb, var(--fg) 35%, var(--border));
  outline-offset: 2px;
}
#theme-btn:active        { transform: scale(0.94); }

#theme-btn > span {
  display: none;
  width: 1.15rem;
  height: 1.15rem;
}
#theme-btn > span > svg {
  width: 100%;
  height: 100%;
  display: block;
}

/* Default (light): show first icon. Dark: show last icon. */
#theme-btn > span:first-child { display: inline-flex; }

:root[data-theme="dark"] #theme-btn > span:first-child { display: none; }
:root[data-theme="dark"] #theme-btn > span:last-child  { display: inline-flex; }

@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) #theme-btn > span:first-child { display: none; }
  :root:not([data-theme="light"]) #theme-btn > span:last-child  { display: inline-flex; }
}

/* =============================================================================
   12. Print
   ========================================================================== */
@media print {
  /* Always print with the light palette regardless of the active theme.
     Forcing `color-scheme: light` resolves every `light-dark()` token to its
     first (light) argument — fixes dark-theme bodies printing as light-on-white. */
  :root,
  :root[data-theme="dark"] {
    color-scheme: light;
  }

  body {
    margin: 0;
    background: #fff;
    color: #000;
  }

  /* Prism blocks are NOT theme-aware (always dark bg + light text). Browsers
     drop the dark background by default → light tokens become invisible on
     white paper. Render Prism as monochrome dark-on-white for print. */
  .md pre.prism,
  .md pre.prism > code,
  .md pre.prism * {
    background: transparent !important;
    color: #000 !important;
    text-shadow: none !important;
  }
  .md pre.prism {
    border: var(--border-w) solid var(--border) !important;
    padding-top: 0.75rem !important;        /* drop the room reserved for copy-btn */
  }

  /* Hide on-screen affordances (copy buttons, nav, theme switcher, etc). */
  #menubar,
  #nav,
  #backdrop,
  #theme-btn,
  #print-footer,
  button.copy-btn,
  button.copy-md-btn {
    display: none !important;
  }

  .shell {
    display: block;
    max-width: none;
    min-height: 0;
    margin: 0;
  }
  .shell > #content {
    padding: 0;
    max-width: none;
  }

  @page {
    margin: 14mm 12mm 18mm;

    @bottom-left {
      content: attr(data-print-date);
      font-family: var(--font-sans);
      font-size: 10pt;
      font-weight: 400;
      color: rgba(0, 0, 0, 0.7);
    }
    @bottom-center {
      content: attr(data-print-title);
      font-family: var(--font-sans);
      font-size: 10pt;
      font-weight: 400;
      color: rgba(0, 0, 0, 0.7);
    }
    @bottom-right {
      content: counter(page) " / " counter(pages);
      font-family: var(--font-sans);
      font-size: 10pt;
      font-weight: 400;
      color: rgba(0, 0, 0, 0.7);
    }
  }
}
