:root {
  --font-sans: "Public Sans", "Segoe UI", "Helvetica Neue", Arial, sans-serif;
  --font-serif: "Public Sans", "Segoe UI", "Helvetica Neue", Arial, sans-serif;

  --bg: #d9d9d9;
  --bg-elev: #cfcfcf;
  --surface: #e3e3e3;
  --ink: #404040;
  --muted: #595959;
  --line: #737373;
  --accent: #4a4a4a;
  --accent-strong: #333333;
  --focus: #595959;
  --border-dim: #c5c5c5;
}

[data-theme="semi-dark"] {
  --bg: #03070d;
  --bg-elev: #00142a;
  --surface: #2e4559;
  --ink: #e4edf3;
  --muted: #a3b4bf;
  --line: #51768c;
  --accent: #7e9caf;
  --accent-strong: #a3b4bf;
  --focus: #a3b4bf;
  --border-dim: #152738;
}

[data-theme="paper"] {
  --bg: #f2eadf;
  --bg-elev: #e4d9cd;
  --surface: #d9ccc1;
  --ink: #3f3730;
  --muted: #595048;
  --line: #8c837b;
  --accent: #595048;
  --accent-strong: #403831;
  --focus: #8c837b;
  --border-dim: #c5bdb5;
}

[data-theme="dark"] {
  --bg: #17191b;
  --bg-elev: #131517;
  --surface: #1f2226;
  --ink: #f6f7f8;
  --muted: #d0d4d9;
  --line: #4e545d;
  --accent: #bcc2cc;
  --accent-strong: #eceff4;
  --focus: #d2d8e2;
  --border-dim: #282d33;
}

* {
  box-sizing: border-box;
}

html,
body {
  margin: 0;
  padding: 0;
  background: var(--bg);
  color: var(--ink);
  font-family: var(--font-sans);
  line-height: 1.55;
  font-size: 16px;
  min-height: 100%;
}

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

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

a:focus-visible,
button:focus-visible,
label:focus-visible {
  outline: 3px solid var(--focus);
  outline-offset: 2px;
}

.textpad:focus,
.textpad:focus-visible {
  outline: none;
  border-color: var(--line);
}

/* ── Topbar ───────────────────────────────────────────────── */

.topbar {
  position: sticky;
  top: 0;
  z-index: 10;
  background: var(--bg-elev);
  border-bottom: 1px solid var(--line);
}

.topbar-inner {
  max-width: 1200px;
  margin: 0 auto;
  padding: 0.85rem 1rem 0;
  display: flex;
  gap: 0.75rem;
  align-items: flex-end;
  justify-content: space-between;
  flex-wrap: wrap;
}

.topbar-controls {
  display: flex;
  align-items: center;
  gap: 0.45rem;
  flex: 0 0 auto;
  align-self: center;
  padding-bottom: 0.55rem;
}

.brand-wrap {
  display: flex;
  flex-direction: column;
  gap: 0.15rem;
  align-self: center;
  padding-bottom: 0.55rem;
}

.brand {
  font-family: var(--font-serif);
  font-size: 1.06rem;
  font-weight: 500;
  letter-spacing: 0.01em;
  color: var(--ink);
  text-decoration: none;
}

.brand-sub {
  font-size: 0.7rem;
  color: var(--muted);
  font-weight: 400;
  letter-spacing: 0.01em;
  line-height: 1;
}

.topnav {
  display: flex;
  flex-wrap: wrap;
  gap: 0.15rem;
  padding-top: 0.3rem;
}

/* Tab buttons */
.topnav-tab {
  text-decoration: none;
  border: 1px solid var(--line);
  border-bottom: 1px solid var(--line);
  color: var(--ink);
  background: var(--bg-elev);
  opacity: 0.42;
  border-top-left-radius: 6px;
  border-top-right-radius: 6px;
  border-bottom-left-radius: 0;
  border-bottom-right-radius: 0;
  padding: 0.38rem 0.68rem;
  font-size: 0.82rem;
  position: relative;
  top: 0;
  cursor: pointer;
  font-family: var(--font-sans);
  transition: opacity 80ms ease;
}

.topnav-tab[aria-selected="true"] {
  background: transparent;
  opacity: 1;
  color: var(--ink);
  border-color: var(--line);
  border-bottom-color: transparent;
  font-weight: 600;
  top: 0;
  margin-bottom: -1px;
}

.topnav-tab:hover {
  opacity: 0.75;
}

.topnav-tab[aria-selected="true"]:hover {
  opacity: 1;
}

/* ── Theme Picker ─────────────────────────────────────────── */

.theme-picker {
  display: flex;
  align-items: center;
  gap: 0.2rem;
  padding: 0 0.1rem;
  flex: 0 0 auto;
}

.theme-picker button {
  border: 1px solid var(--line);
  background: transparent;
  color: transparent;
  border-radius: 999px;
  width: 0.82rem;
  height: 0.82rem;
  padding: 0;
  cursor: pointer;
  opacity: 0.6;
  transition: opacity 120ms ease, transform 120ms ease, border-color 120ms ease;
}

.theme-picker button span {
  display: block;
  width: 100%;
  height: 100%;
  border-radius: 999px;
  background: var(--line);
}

.theme-picker button[aria-pressed="true"] {
  border-color: var(--accent);
  opacity: 1;
  transform: scale(1.08);
}

.theme-picker button[aria-pressed="true"] span {
  background: var(--accent);
}

.theme-picker button:hover,
.theme-picker button:focus-visible {
  opacity: 1;
}

/* ── Layout ───────────────────────────────────────────────── */

.sr-only {
  position: absolute;
  width: 1px;
  height: 1px;
  padding: 0;
  margin: -1px;
  overflow: hidden;
  clip: rect(0, 0, 0, 0);
  border: 0;
}

.page {
  max-width: 1200px;
  margin: 1.3rem auto 2.6rem;
  padding: 0 1rem;
}

.tab-panel[hidden] {
  display: none;
}

/* ── Page Intro ───────────────────────────────────────────── */

.eyebrow {
  font-size: 0.72rem;
  text-transform: uppercase;
  letter-spacing: 0.1em;
  color: var(--muted);
  margin: 0 0 0.4rem;
}

.page-intro {
  margin: 0 0 1.5rem;
}

.page-title {
  margin: 0 0 0.35rem;
  font-family: var(--font-serif);
  font-size: clamp(1.3rem, 1.8vw, 1.9rem);
  line-height: 1.25;
}

.page-subtitle {
  margin: 0;
  color: var(--muted);
}

/* ── Flow Section ─────────────────────────────────────────── */

.flow-section {
  margin: 0 0 2.3rem;
  padding: 0;
}

.flow-section + .flow-section::before {
  content: "";
  display: block;
  height: 1px;
  background: linear-gradient(to right, transparent 0%, var(--line) 18%, var(--line) 82%, transparent 100%);
  margin: 0 0 1.3rem;
}

.flow-section + .flow-section {
  position: relative;
}

.flow-section + .flow-section::after {
  content: "*";
  position: absolute;
  left: 50%;
  top: -0.34rem;
  transform: translateX(-50%);
  color: var(--line);
  background: var(--bg);
  padding: 0 0.48rem;
  font-size: 0.7rem;
  line-height: 1;
}

.flow-section h2 {
  margin: 0 0 0.4rem;
  font-family: var(--font-serif);
  font-size: 1.05rem;
}

.flow-section p,
.flow-section li {
  max-width: 74ch;
  color: var(--muted);
}

.flow-section ul,
.flow-section ol {
  margin: 0.3rem 0 0.8rem 1.2rem;
}

/* ── Notes ────────────────────────────────────────────────── */

.textpad {
  width: 100%;
  min-height: 58vh;
  resize: vertical;
  border: 1px solid var(--border-dim);
  border-radius: 2px;
  padding: 0.9rem;
  background: var(--surface);
  color: var(--ink);
  font-family: var(--font-body, var(--font-sans));
  font-size: 1rem;
  line-height: 1.6;
}

.toolbar {
  margin-top: 0.65rem;
  display: flex;
  gap: 0.45rem;
  flex-wrap: wrap;
  align-items: center;
}

.btn {
  border: 1px solid var(--line);
  background: var(--surface);
  color: var(--ink);
  border-radius: 2px;
  padding: 0.42rem 0.7rem;
  cursor: pointer;
  font-family: var(--font-sans);
  font-size: 0.9rem;
  text-decoration: none;
  display: inline-block;
  transition: opacity 80ms ease;
}

.btn:hover {
  opacity: 0.8;
}

.btn.primary {
  border-color: var(--accent);
  color: var(--accent);
}

.badge {
  border: 1px solid var(--line);
  background: var(--surface);
  color: var(--muted);
  border-radius: 2px;
  padding: 0.2rem 0.46rem;
  font-size: 0.74rem;
}

/* ── MD Reader ────────────────────────────────────────────── */

.reader-bar {
  margin-bottom: 1.2rem;
  display: flex;
  align-items: center;
  gap: 0.5rem;
  flex-wrap: wrap;
}

.readme-link {
  margin-left: auto;
  font-size: 0.8rem;
  color: var(--muted);
  text-decoration: underline;
  text-underline-offset: 2px;
  white-space: nowrap;
  cursor: pointer;
}

.readme-link:hover,
.readme-link:focus-visible {
  color: var(--accent);
}

.layout-with-sidebar {
  display: grid;
  grid-template-columns: minmax(0, 1fr) 236px;
  gap: 1rem;
}

.sidebar {
  border: 0;
  border-radius: 2px;
  background: transparent;
  padding: 0.9rem;
  position: sticky;
  top: 82px;
  max-height: calc(100vh - 100px);
  overflow: auto;
}

.sidebar h2 {
  margin: 0 0 0.5rem;
  font-family: var(--font-serif);
  font-size: 1rem;
}

.toc {
  list-style: none;
  margin: 0;
  padding: 0;
}

.toc li {
  margin: 0.35rem 0;
}

.toc a {
  text-decoration: none;
  font-size: 0.76rem;
  color: var(--muted);
}

.toc a:hover,
.toc a:focus-visible {
  color: var(--accent);
}

.content-wrap {
  border: 0;
  border-radius: 2px;
  background: transparent;
  padding: 0.5rem 0;
}

.content h1,
.content h2,
.content h3,
.content h4 {
  line-height: 1.3;
}

.content h1 {
  font-family: var(--font-serif);
  font-size: 1.42rem;
  margin: 0 0 0.9rem;
  border-bottom: 1px solid var(--line);
  padding-bottom: 0.75rem;
}

.content h2 {
  margin: 2rem 0 0.55rem;
  color: var(--accent);
  font-size: 0.98rem;
  text-transform: uppercase;
  letter-spacing: 0.04em;
}

.content h2:not(:first-of-type)::before {
  content: "";
  display: block;
  height: 1px;
  background: linear-gradient(to right, transparent 0%, var(--line) 18%, var(--line) 82%, transparent 100%);
  margin: 0 0 1.3rem;
}

.content h2:not(:first-of-type) {
  position: relative;
}

.content h2:not(:first-of-type)::after {
  content: "*";
  position: absolute;
  left: 50%;
  top: -1.72rem;
  transform: translateX(-50%);
  color: var(--line);
  background: var(--bg);
  padding: 0 0.48rem;
  font-size: 0.7rem;
  line-height: 1;
}

.content h3 {
  margin: 1rem 0 0.35rem;
  color: var(--accent-strong);
  font-size: 0.93rem;
}

.content h4 {
  margin: 0.8rem 0 0.3rem;
  font-size: 0.88rem;
}

.content p,
.content li {
  max-width: 74ch;
}

.content ul,
.content ol {
  margin: 0.35rem 0 0.8rem 1.2rem;
}

.content hr {
  border: 0;
  border-top: 1px solid var(--line);
  margin: 1.2rem 0;
}

.content code {
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: 2px;
  padding: 0.1em 0.35em;
  font-size: 0.88em;
}

/* ── Footer ───────────────────────────────────────────────── */

.footer-note {
  margin-top: 1rem;
  text-align: center;
  color: var(--muted);
  font-size: 0.82rem;
}

/* ── Responsive ───────────────────────────────────────────── */

@media (max-width: 940px) {
  .layout-with-sidebar {
    grid-template-columns: minmax(0, 1fr);
  }

  .sidebar {
    position: static;
    max-height: none;
    order: -1;
    margin-bottom: 1rem;
  }
}

@media (max-width: 680px) {
  .topbar-inner {
    padding: 0.75rem;
  }

  .page {
    margin-top: 1rem;
    padding: 0 0.7rem 2rem;
  }
}

/* ── Content Width (CSS var set by JS slider) ───────── */

.page { max-width: var(--page-max-w, 1100px); }

/* ── Font Body ───────────────────────────────────── */

body[data-font="sans"]       { --font-body: "Public Sans", system-ui, sans-serif; }
body[data-font="serif"]      { --font-body: "Lora", Georgia, serif; }
body[data-font="mono"]       { --font-body: "JetBrains Mono", monospace; }
body[data-font="typewriter"] { --font-body: "Special Elite", "Courier New", monospace; }
body[data-font="script"]     { --font-body: "Caveat", cursive; }

.textpad,
.content {
  font-family: var(--font-body, var(--font-sans));
  font-size: var(--content-font-size, 1rem);
}

/* ── Settings Button ──────────────────────────────────────── */

.settings-btn {
  background: transparent;
  border: 1px solid transparent;
  color: var(--muted);
  border-radius: 4px;
  padding: 0.18rem 0.32rem;
  cursor: pointer;
  font-size: 1rem;
  line-height: 1;
  transition: color 120ms ease, border-color 120ms ease;
}

.settings-btn:hover,
.settings-btn:focus-visible {
  color: var(--ink);
  border-color: var(--line);
}

/* ── Settings Modal ───────────────────────────────────────── */

dialog {
  background: var(--bg-elev);
  color: var(--ink);
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 0;
  max-width: 400px;
  width: 90vw;
  box-shadow: 0 8px 32px rgba(0, 0, 0, 0.4);
}

dialog::backdrop {
  background: rgba(0, 0, 0, 0.45);
}

.modal-inner {
  padding: 1.2rem 1.4rem 1.5rem;
}

.modal-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 1.1rem;
}

.modal-title {
  margin: 0;
  font-size: 1rem;
  font-family: var(--font-serif);
}

.modal-close {
  background: transparent;
  border: 1px solid transparent;
  color: var(--muted);
  border-radius: 4px;
  padding: 0.2rem 0.5rem;
  cursor: pointer;
  font-size: 0.9rem;
  transition: color 120ms ease, border-color 120ms ease;
}

.modal-close:hover {
  color: var(--ink);
  border-color: var(--line);
}

.settings-group {
  border: 1px solid var(--line);
  border-radius: 4px;
  padding: 0.75rem 1rem 0.85rem;
  margin: 0 0 0.75rem;
}

.settings-group:last-child {
  margin-bottom: 0;
}

.settings-group legend {
  padding: 0 0.4rem;
  font-size: 0.75rem;
  text-transform: uppercase;
  letter-spacing: 0.07em;
  color: var(--muted);
}

/* ── Width Slider ─────────────────────────────────────── */

.width-slider-wrap {
  display: flex;
  align-items: center;
  gap: 0.65rem;
  margin-top: 0.55rem;
}

.width-slider-wrap input[type="range"] {
  flex: 1;
  accent-color: var(--accent);
  cursor: pointer;
  height: 4px;
}

.width-val {
  font-size: 0.8rem;
  color: var(--muted);
  min-width: 4.5ch;
  text-align: right;
}

.setting-row {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  font-size: 0.9rem;
  cursor: pointer;
  margin-top: 0.4rem;
  user-select: none;
}

.setting-row input {
  accent-color: var(--accent);
}

/* ── Font Options ─────────────────────────────────── */

.font-options {
  display: flex;
  flex-direction: column;
  gap: 0.28rem;
  margin-top: 0.45rem;
}

.font-opt {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  cursor: pointer;
  user-select: none;
  font-size: 0.95rem;
}

.font-opt input {
  accent-color: var(--accent);
  flex-shrink: 0;
}

.font-size-row {
  display: flex;
  align-items: center;
  gap: 0.65rem;
  margin-top: 0.65rem;
  border-top: 1px solid var(--border-dim);
  padding-top: 0.6rem;
}

.font-size-row label {
  font-size: 0.8rem;
  color: var(--muted);
  white-space: nowrap;
}

.font-size-row input[type="range"] {
  flex: 1;
  accent-color: var(--accent);
  cursor: pointer;
  height: 4px;
}

/* ── Token Badge ──────────────────────────────────────────── */

.token-badge {
  margin-left: auto;
  font-variant-numeric: tabular-nums;
  white-space: nowrap;
}

/* ── TOC Collapsible ──────────────────────────────────────── */

.toc-toggle-btn {
  font-size: 0.8rem;
  padding: 0.28rem 0.55rem;
}

.toc-collapsed .sidebar {
  display: none;
}

.toc-collapsed {
  grid-template-columns: minmax(0, 1fr);
}

/* ── Blockquote + Code Block ──────────────────────────────── */

blockquote {
  border-left: 3px solid var(--line);
  margin: 0.8rem 0;
  padding: 0.05rem 0 0.05rem 1rem;
  color: var(--muted);
}

blockquote p {
  margin: 0;
}

pre {
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: 4px;
  padding: 0.75rem 1rem;
  overflow-x: auto;
  font-size: 0.87em;
  line-height: 1.5;
}

pre code {
  background: none;
  border: none;
  padding: 0;
  font-size: inherit;
}
