/* --- Vendored mono ---------------------------------------------------------
   Spline Sans Mono v13 (variable, wght 300-700), SIL OFL 1.1.
   Self-hosted so every viewer CSP can stay `font-src 'self'`; published HTML
   and /raw keep `font-src 'none'`. See fonts/FONT-LICENSE.txt + FONT-SHA256.txt.
   URLs are stylesheet-relative and version-stamped in the filename, so an
   upgrade never shadows a cached copy. ------------------------------------- */

@font-face {
  font-family: "Spline Sans Mono";
  font-style: normal;
  font-weight: 300 700;
  font-display: swap;
  src: url("fonts/spline-sans-mono-v13-latin.woff2") format("woff2");
  unicode-range: U+0000-00FF, U+0131, U+0152-0153, U+02BB-02BC, U+02C6, U+02DA,
    U+02DC, U+0304, U+0308, U+0329, U+2000-206F, U+20AC, U+2122, U+2191, U+2193,
    U+2212, U+2215, U+FEFF, U+FFFD;
}

@font-face {
  font-family: "Spline Sans Mono";
  font-style: normal;
  font-weight: 300 700;
  font-display: swap;
  src: url("fonts/spline-sans-mono-v13-latin-ext.woff2") format("woff2");
  unicode-range: U+0100-02BA, U+02BD-02C5, U+02C7-02CC, U+02CE-02D7, U+02DD-02FF,
    U+0304, U+0308, U+0329, U+1D00-1DBF, U+1E00-1E9F, U+1EF2-1EFF, U+2020,
    U+20A0-20AB, U+20AD-20C0, U+2113, U+2C60-2C7F, U+A720-A7FF;
}

/* --- Design tokens ---------------------------------------------------------
   Dark values are duplicated in `html[data-theme="dark"]` and in the
   `prefers-color-scheme: dark` block for `html[data-theme="system"]`. Tokens
   defined with `var()` (--link, --focus-ring, --fmt-html) resolve per theme and
   must NOT be redeclared there. ------------------------------------------- */

:root {
  color-scheme: light;
  --bg: #f7f7f8;
  --surface: #ffffff;
  --surface-sunken: #f0f1f3;
  --text: #1a1a1a;
  --text-strong: #0d0f12;
  --muted: #5f6368;
  --border: #e3e5e8;
  --accent: #0f5f57;
  --link: var(--accent);
  --code-bg: #f1f3f5;
  /* Format identity doubles as the ledger's only color: Markdown is plain ink
     because it is plain text; HTML takes the accent because it renders. */
  --fmt-markdown: #414852;
  --fmt-html: var(--accent);
  /* Functional, not decorative: an error must not read as a success state. */
  --danger: #b3261e;
  --toolbar-height: 3rem;
  --shadow-menu: 0 2px 8px rgba(0, 0, 0, 0.08);
  --hover-bg: #e8eaed;
  --focus-ring: var(--accent);
  --font-sans: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  --font-mono: "Spline Sans Mono", ui-monospace, SFMono-Regular, Menlo, Consolas,
    monospace;
  --radius-sm: 0.25rem;
  --radius-md: 0.375rem;
  --radius-lg: 0.75rem;
  --measure-prose: 52rem;
  --measure-data: 64rem;
}

html[data-theme="light"] {
  color-scheme: light;
}

html[data-theme="dark"] {
  color-scheme: dark;
  --bg: #111318;
  --surface: #1b1f27;
  --surface-sunken: #15181e;
  --text: #f3f4f6;
  --text-strong: #ffffff;
  --muted: #aab2bf;
  --border: #343b48;
  --accent: #7fd8c8;
  --code-bg: #252b35;
  --fmt-markdown: #c5ccd8;
  --danger: #ffb4ab;
  --shadow-menu: 0 4px 16px rgba(0, 0, 0, 0.5);
  --hover-bg: #2d3440;
}

html[data-theme="system"] {
  color-scheme: light;
}

@media (prefers-color-scheme: dark) {
  html[data-theme="system"] {
    color-scheme: dark;
    --bg: #111318;
    --surface: #1b1f27;
    --surface-sunken: #15181e;
    --text: #f3f4f6;
    --text-strong: #ffffff;
    --muted: #aab2bf;
    --border: #343b48;
    --accent: #7fd8c8;
    --code-bg: #252b35;
    --fmt-markdown: #c5ccd8;
    --danger: #ffb4ab;
    --shadow-menu: 0 4px 16px rgba(0, 0, 0, 0.5);
    --hover-bg: #2d3440;
  }
}

* {
  box-sizing: border-box;
}

body {
  margin: 0;
  font-family: var(--font-sans);
  line-height: 1.6;
  color: var(--text);
  background: var(--bg);
}

/* --- Shared toolbar chrome --- */

.viewer-toolbar {
  display: flex;
  align-items: center;
  gap: 0.75rem;
  height: var(--toolbar-height);
  padding: 0 1rem;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  position: sticky;
  top: 0;
  z-index: 10;
}

body.viewer-markdown-host .viewer-toolbar {
  transition: opacity 0.2s ease, transform 0.2s ease;
}

body.viewer-markdown-host .viewer-toolbar.viewer-toolbar-hidden {
  opacity: 0;
  transform: translateY(-100%);
  pointer-events: none;
}

body.viewer-html-host .viewer-toolbar {
  position: relative;
  flex: 0 0 var(--toolbar-height);
}

/* Wordmark doubles as the way home. On an artifact page below 34rem the page
   title earns the space instead, so the wordmark steps aside. */
.viewer-wordmark {
  flex: 0 0 auto;
  font-family: var(--font-mono);
  font-size: 0.875rem;
  font-weight: 500;
  letter-spacing: -0.01em;
  color: var(--text-strong);
  text-decoration: none;
  white-space: nowrap;
  border-radius: var(--radius-sm);
}

a.viewer-wordmark:hover {
  text-decoration: underline;
}

a.viewer-wordmark:focus-visible {
  outline: 2px solid var(--focus-ring);
  outline-offset: 2px;
}

@media (max-width: 34rem) {
  body.viewer-markdown-host .viewer-wordmark,
  body.viewer-html-host .viewer-wordmark {
    display: none;
  }
}

.sr-only {
  position: absolute;
  width: 1px;
  height: 1px;
  padding: 0;
  margin: -1px;
  overflow: hidden;
  clip-path: inset(50%);
  white-space: nowrap;
  border: 0;
}

.viewer-toolbar-title {
  flex: 1 1 auto;
  min-width: 0;
  margin: 0;
  font-size: 1rem;
  line-height: 1.25;
  font-weight: 600;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.viewer-toolbar-actions {
  display: flex;
  flex-shrink: 0;
  flex-wrap: nowrap;
  align-items: center;
  gap: 0.75rem;
}

.viewer-toolbar-actions .viewer-versions {
  flex-wrap: nowrap;
  gap: 0;
}

.viewer-toolbar-actions .viewer-version-menu-list,
.viewer-toolbar-actions .viewer-theme-menu-panel {
  right: 0;
  left: auto;
}

@media (max-width: 22rem) {
  body.viewer-html-host .viewer-toolbar-actions .viewer-version-menu-list,
  body.viewer-html-host .viewer-toolbar-actions .viewer-theme-menu-panel {
    right: auto;
    left: 0;
  }
}

.viewer-toolbar-fullscreen,
.viewer-toolbar-download {
  display: inline-flex;
  align-items: center;
  gap: 0.3rem;
  color: var(--link);
  font-size: 0.9rem;
  text-decoration: none;
  white-space: nowrap;
  border-radius: 0.25rem;
}

.viewer-toolbar-download svg,
.viewer-toolbar-fullscreen-icon {
  flex: 0 0 auto;
}

.viewer-toolbar-fullscreen-icon {
  display: none;
}

.viewer-toolbar-fullscreen:hover,
.viewer-toolbar-download:hover {
  text-decoration: underline;
}

/* HTML host packs version + theme + Download + fullscreen. Below ~40rem the
   intrinsic nowrap labels exceed the docked toolbar (overflow hidden), so
   collapse action labels to icons while aria-label stays. */
@media (max-width: 40rem) {
  body.viewer-html-host .viewer-toolbar-actions {
    gap: 0.5rem;
  }

  body.viewer-html-host .viewer-toolbar-action-label {
    position: absolute;
    width: 1px;
    height: 1px;
    padding: 0;
    margin: -1px;
    overflow: hidden;
    clip-path: inset(50%);
    white-space: nowrap;
    border: 0;
  }

  body.viewer-html-host .viewer-toolbar-fullscreen-icon {
    display: block;
  }
}

/* --- Markdown layout (normally scrolling readable document) --- */

.viewer-main {
  max-width: 52rem;
  margin: 0 auto;
  padding: 0 1.25rem 2rem;
}

body.viewer-markdown-host .viewer-main {
  padding-top: clamp(0.875rem, 2vw, 1.25rem);
}

.viewer-content {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius-lg);
  padding: 1.5rem;
}

/* --- Version menu (shared toolbar) --- */

.viewer-versions {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 0.5rem;
  color: var(--muted);
  font-size: 0.9rem;
}

.viewer-versions-label {
  margin-right: 0.25rem;
}

/* Version numbers are machine record, so they take the mono voice. */
.viewer-version-indicator,
.viewer-version-menu-summary,
.viewer-version-menu-item a,
.viewer-version-menu-item strong {
  font-family: var(--font-mono);
  font-variant-numeric: tabular-nums;
}

.viewer-version-indicator {
  color: var(--muted);
  font-size: 0.9rem;
  white-space: nowrap;
}

.viewer-version-menu {
  position: relative;
}

.viewer-version-menu-summary {
  cursor: pointer;
  color: var(--link);
  font-size: 0.9rem;
  white-space: nowrap;
  list-style: none;
  border-radius: 0.25rem;
}

.viewer-version-menu-summary::-webkit-details-marker {
  display: none;
}

.viewer-version-menu-summary::marker {
  content: "";
}

.viewer-version-menu-summary::after {
  content: " ▾";
  font-size: 0.75em;
  color: var(--muted);
}

.viewer-version-menu[open] > .viewer-version-menu-summary {
  color: var(--text);
}

.viewer-version-menu-list {
  position: absolute;
  top: calc(100% + 0.25rem);
  left: 0;
  z-index: 20;
  margin: 0;
  padding: 0.25rem 0;
  min-width: 4.5rem;
  max-height: min(16rem, 50vh);
  overflow-y: auto;
  list-style: none;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 0.375rem;
  box-shadow: var(--shadow-menu);
}

.viewer-version-menu-item {
  margin: 0;
  padding: 0;
}

.viewer-version-menu-item a,
.viewer-version-menu-item strong {
  display: block;
  padding: 0.35rem 0.75rem;
  font-size: 0.9rem;
  line-height: 1.4;
}

.viewer-version-menu-item .viewer-version-current {
  font-weight: 600;
}

.viewer-version-link {
  color: var(--link);
  text-decoration: none;
  border-radius: 0.25rem;
}

.viewer-version-link:hover {
  text-decoration: underline;
  background: var(--hover-bg);
}

.viewer-version-current {
  color: var(--text);
}

/* --- Theme menu (shared toolbar) --- */

.viewer-theme-menu {
  position: relative;
  flex-shrink: 0;
}

.viewer-theme-menu-summary {
  cursor: pointer;
  color: var(--link);
  font-size: 0.9rem;
  white-space: nowrap;
  list-style: none;
  border-radius: 0.25rem;
}

.viewer-theme-menu-summary::-webkit-details-marker {
  display: none;
}

.viewer-theme-menu-summary::marker {
  content: "";
}

.viewer-theme-menu-summary::after {
  content: " ▾";
  font-size: 0.75em;
  color: var(--muted);
}

.viewer-theme-menu[open] > .viewer-theme-menu-summary {
  color: var(--text);
}

.viewer-theme-menu-panel {
  position: absolute;
  top: calc(100% + 0.25rem);
  left: 0;
  z-index: 20;
  min-width: 12rem;
  max-width: min(18rem, 80vw);
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 0.375rem;
  box-shadow: var(--shadow-menu);
}

.viewer-theme-menu-description {
  margin: 0;
  padding: 0.5rem 0.75rem 0.35rem;
  color: var(--muted);
  font-size: 0.75rem;
  line-height: 1.35;
  border-bottom: 1px solid var(--border);
}

.viewer-theme-menu-list {
  margin: 0;
  padding: 0.25rem 0;
  list-style: none;
}

.viewer-theme-menu-item {
  margin: 0;
  padding: 0;
}

.viewer-theme-menu-item a,
.viewer-theme-menu-item strong {
  display: block;
  padding: 0.35rem 0.75rem;
  font-size: 0.9rem;
  line-height: 1.4;
}

.viewer-theme-menu-item .viewer-theme-current {
  font-weight: 600;
}

.viewer-theme-link {
  color: var(--link);
  text-decoration: none;
  border-radius: 0.25rem;
}

.viewer-theme-link:hover {
  text-decoration: underline;
  background: var(--hover-bg);
}

.viewer-theme-current {
  color: var(--text);
}

/* --- Focus outlines (toolbar, version, theme controls) --- */

.viewer-version-menu-summary:focus-visible,
.viewer-version-link:focus-visible,
.viewer-theme-menu-summary:focus-visible,
.viewer-theme-link:focus-visible,
.viewer-toolbar-fullscreen:focus-visible,
.viewer-toolbar-download:focus-visible {
  outline: 2px solid var(--focus-ring);
  outline-offset: 2px;
}

/* --- HTML host (full viewport, outer page does not scroll) --- */

html:has(body.viewer-html-host) {
  overflow: hidden;
}

body.viewer-html-host {
  display: flex;
  flex-direction: column;
  overflow: hidden;
  height: 100vh;
  height: 100dvh;
}

body.viewer-html-host .viewer-main.viewer-main-iframe {
  flex: 1 1 auto;
  min-height: 0;
  width: 100%;
  max-width: none;
  margin: 0;
  padding: 0;
}

body.viewer-html-host .viewer-iframe {
  display: block;
  width: 100%;
  height: 100%;
  margin: 0;
  border: 0;
  border-radius: 0;
  background: var(--surface);
}

@media (prefers-reduced-motion: reduce) {
  body.viewer-markdown-host .viewer-toolbar {
    transition: none;
  }
}

/* --- Markdown body typography --- */

.markdown-body h1,
.markdown-body h2,
.markdown-body h3,
.markdown-body h4,
.markdown-body h5,
.markdown-body h6 {
  margin-top: 1.5rem;
  margin-bottom: 0.75rem;
  line-height: 1.25;
}

.markdown-body h1:first-child,
.markdown-body h2:first-child,
.markdown-body h3:first-child {
  margin-top: 0;
}

.markdown-body p,
.markdown-body ul,
.markdown-body ol,
.markdown-body blockquote,
.markdown-body pre,
.markdown-body table {
  margin: 0 0 1rem;
}

.markdown-body a {
  color: var(--link);
  overflow-wrap: anywhere;
}

.markdown-body code {
  font-family: var(--font-mono);
  font-size: 0.92em;
  background: var(--code-bg);
  padding: 0.1em 0.35em;
  border-radius: 0.25rem;
}

.markdown-body pre {
  overflow-x: auto;
  background: var(--code-bg);
  padding: 1rem;
  border-radius: 0.5rem;
}

.markdown-body pre code {
  background: transparent;
  padding: 0;
  white-space: pre;
  overflow-wrap: normal;
}

/* Emergency wrap for long tokens that would otherwise force page overflow */
.markdown-body p,
.markdown-body li,
.markdown-body h1,
.markdown-body h2,
.markdown-body h3,
.markdown-body h4,
.markdown-body h5,
.markdown-body h6 {
  overflow-wrap: anywhere;
}

.markdown-body :not(pre) > code {
  overflow-wrap: anywhere;
  word-break: break-word;
}

.markdown-body blockquote {
  margin-left: 0;
  padding-left: 1rem;
  border-left: 3px solid var(--border);
  color: var(--muted);
}

/* Scroll shell keeps native table display/semantics for accessibility */
.markdown-body .markdown-table-scroll {
  margin: 0 0 1rem;
  overflow-x: auto;
  -webkit-overflow-scrolling: touch;
}

.markdown-body .markdown-table-scroll:focus-visible {
  outline: 2px solid var(--focus-ring);
  outline-offset: -2px;
}

.markdown-body table {
  width: 100%;
  margin: 0;
  border-collapse: collapse;
}

.markdown-body th,
.markdown-body td {
  border: 1px solid var(--border);
  padding: 0.5rem 0.75rem;
  text-align: left;
}

.markdown-body img {
  max-width: 100%;
  height: auto;
}

/* --- Markdown Mermaid render fallback and static diagram image --- */

.markdown-body .mermaid-render {
  max-width: 100%;
  margin: 0 0 1rem;
  overflow-x: auto;
  overflow-y: hidden;
  border: 1px solid var(--border);
  border-radius: 0.5rem;
  background: var(--surface);
}

.markdown-body .mermaid-render-image {
  display: block;
  width: 100%;
  max-width: 100%;
  height: auto;
  margin: 0 auto;
}

.markdown-body .mermaid-render-source {
  margin: 0;
  border-top: 1px solid var(--border);
}

.markdown-body .mermaid-render-source > summary {
  padding: 0.5rem 0.75rem;
  color: var(--muted);
  cursor: pointer;
}

.markdown-body .mermaid-render-source > summary:focus-visible {
  outline: 2px solid var(--focus-ring);
  outline-offset: -2px;
}

.markdown-body .mermaid-render-source pre {
  margin: 0;
  border-radius: 0;
}

.markdown-body .mermaid-render-error {
  margin: 0 0 1rem;
  padding: 0.75rem 1rem;
  color: var(--text);
  background: var(--code-bg);
  border: 1px solid var(--border);
  border-left: 0.25rem solid var(--focus-ring);
  border-radius: 0.5rem;
}

/* Keep the sandboxed renderer measurable while removing it from interaction. */
.viewer-mermaid-renderer-frame {
  position: fixed;
  width: 1024px;
  height: 768px;
  top: -2000px;
  left: -2000px;
  border: 0;
  opacity: 0;
  pointer-events: none;
}

.markdown-body hr {
  border: 0;
  border-top: 1px solid var(--border);
  margin: 1.5rem 0;
}

.markdown-body input[type="checkbox"] {
  margin-right: 0.35rem;
}

/* --- Host error pages ------------------------------------------------------
   The home page's hero is a URL that works; this is the same typographic
   object in its failed state. Status eyebrow, the requested URL at hero
   scale, then what to do about it. Lives in viewer.css rather than
   viewer-home.css because error pages do not load the home stylesheet. --- */

body.viewer-error-host .viewer-main {
  padding-top: clamp(1.5rem, 6vw, 3.5rem);
}

.viewer-error {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius-lg);
  padding: clamp(1.5rem, 4vw, 2.5rem);
}

.viewer-error-status {
  margin: 0 0 0.875rem;
  font-family: var(--font-mono);
  font-size: 0.6875rem;
  font-weight: 500;
  letter-spacing: 0.14em;
  text-transform: uppercase;
  color: var(--muted);
}

.viewer-error-path {
  margin: 0 0 1.25rem;
  font-family: var(--font-mono);
  font-size: clamp(1.125rem, 3.6vw, 1.75rem);
  font-weight: 500;
  line-height: 1.3;
  letter-spacing: -0.02em;
  color: var(--text-strong);
  /* Requested paths are arbitrary and already length-capped server-side. */
  overflow-wrap: anywhere;
}

.viewer-error-path-host {
  color: var(--muted);
}

.viewer-error-title {
  margin: 0 0 0.375rem;
  font-size: 1.125rem;
  line-height: 1.3;
  font-weight: 600;
}

.viewer-error-message {
  margin: 0;
  max-width: 34rem;
  color: var(--muted);
}

.viewer-error-home {
  display: inline-block;
  margin-top: 1.5rem;
  font-family: var(--font-mono);
  font-size: 0.8125rem;
  color: var(--link);
  text-decoration: none;
  border-radius: var(--radius-sm);
}

.viewer-error-home:hover {
  text-decoration: underline;
}

.viewer-error-home:focus-visible {
  outline: 2px solid var(--focus-ring);
  outline-offset: 2px;
}

/* --- PIN unlock form ------------------------------------------------------- */

body.viewer-unlock-host .viewer-main {
  padding-top: clamp(1.5rem, 6vw, 3.5rem);
}

.viewer-unlock {
  max-width: 28rem;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius-lg);
  padding: clamp(1.5rem, 4vw, 2.5rem);
}

.viewer-unlock-eyebrow {
  margin: 0 0 0.5rem;
  font-family: var(--font-mono);
  font-size: 0.6875rem;
  font-weight: 500;
  letter-spacing: 0.14em;
  text-transform: uppercase;
  color: var(--muted);
}

.viewer-unlock-title {
  margin: 0 0 0.75rem;
  font-size: 1.25rem;
  line-height: 1.3;
  font-weight: 600;
}

.viewer-unlock-lede {
  margin: 0 0 1.25rem;
  color: var(--muted);
  font-size: 0.9375rem;
}

.viewer-unlock-label {
  display: block;
  margin-bottom: 0.4rem;
  font-family: var(--font-mono);
  font-size: 0.6875rem;
  font-weight: 500;
  letter-spacing: 0.1em;
  text-transform: uppercase;
  color: var(--muted);
}

.viewer-unlock-input {
  display: block;
  width: 100%;
  padding: 0.5rem 0.65rem;
  font-family: var(--font-mono);
  font-size: 1rem;
  letter-spacing: 0.2em;
  color: var(--text);
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: var(--radius-md);
}

.viewer-unlock-input:focus-visible {
  border-color: var(--accent);
  outline: 2px solid var(--focus-ring);
  outline-offset: 1px;
}

.viewer-unlock-error {
  margin: 0.75rem 0 0;
  font-size: 0.875rem;
  color: var(--error, #c0392b);
}

.viewer-unlock-submit {
  margin-top: 1rem;
  padding: 0.5rem 1rem;
  font-family: var(--font-mono);
  font-size: 0.8125rem;
  font-weight: 500;
  color: var(--surface);
  background: var(--accent);
  border: 1px solid var(--accent);
  border-radius: var(--radius-md);
  cursor: pointer;
}

.viewer-unlock-submit:hover {
  opacity: 0.88;
}

.viewer-unlock-submit:focus-visible {
  outline: 2px solid var(--focus-ring);
  outline-offset: 2px;
}

.viewer-unlock-agent {
  margin: 1.25rem 0 0;
  color: var(--muted);
  font-size: 0.8125rem;
  line-height: 1.5;
}

.viewer-unlock-agent code {
  font-family: var(--font-mono);
  font-size: 0.75rem;
  word-break: break-all;
}

.viewer-unlock-agent a {
  color: var(--link);
}
