/* ==========================================================================
   RAGE Engine Technical Notes — documentation theme
   Dark navy, muted blue accents, no frameworks.
   ========================================================================== */

:root {
  --bg: #0b1220;
  --bg-elev: #0f1a2e;
  --bg-elev-2: #132038;
  --bg-code: #0a1424;
  --border: #1f2e4a;
  --border-strong: #2b3f63;
  --text: #d5deec;
  --text-strong: #f1f5fb;
  --text-muted: #94a3bd;      /* 6.9:1 on --bg */
  --accent: #7fb0f5;          /* 8.4:1 on --bg */
  --accent-strong: #a8c9fa;
  --accent-soft: rgba(127, 176, 245, 0.12);
  --warn-soft: rgba(245, 190, 110, 0.08);
  --focus: #ffcf66;

  --font-sans: -apple-system, BlinkMacSystemFont, "Segoe UI", "Inter", Roboto, "Helvetica Neue", Arial, sans-serif;
  --font-mono: ui-monospace, SFMono-Regular, "JetBrains Mono", "Cascadia Code", Menlo, Consolas, "Liberation Mono", monospace;

  --topbar-h: 60px;
  --sidebar-w: 300px;
  --content-max: 880px;
  --radius: 10px;
  --radius-sm: 6px;
  --ease: cubic-bezier(.2, .7, .2, 1);

  color-scheme: dark;
}

*, *::before, *::after { box-sizing: border-box; }

html {
  scroll-padding-top: calc(var(--topbar-h) + 20px);
  -webkit-text-size-adjust: 100%;
  text-size-adjust: 100%;
}
@media (prefers-reduced-motion: no-preference) {
  html { scroll-behavior: smooth; }
}

body {
  margin: 0;
  background:
    radial-gradient(1200px 600px at 85% -10%, rgba(76, 123, 191, 0.10), transparent 60%),
    var(--bg);
  background-attachment: fixed;
  color: var(--text);
  font-family: var(--font-sans);
  font-size: 1.0625rem;
  line-height: 1.7;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
}

::selection { background: rgba(127, 176, 245, 0.35); color: var(--text-strong); }

a { color: var(--accent); text-decoration: underline; text-decoration-color: rgba(127,176,245,.35); text-underline-offset: 3px; }
a:hover { color: var(--accent-strong); text-decoration-color: currentColor; }

:focus-visible {
  outline: 2px solid var(--focus);
  outline-offset: 2px;
  border-radius: 4px;
}
[tabindex="-1"]:focus:not(:focus-visible) { outline: none; }

.skip-link {
  position: absolute;
  left: 12px;
  top: -100px;
  z-index: 1000;
  padding: 10px 16px;
  background: var(--accent);
  color: #08101d;
  font-weight: 600;
  border-radius: var(--radius-sm);
  text-decoration: none;
}
.skip-link:focus { top: 10px; color: #08101d; }

/* ---------- Top bar ---------- */
.topbar {
  position: fixed;
  inset: 0 0 auto 0;
  height: var(--topbar-h);
  z-index: 100;
  display: flex;
  align-items: center;
  gap: 14px;
  padding: 0 20px;
  background: rgba(11, 18, 32, 0.86);
  -webkit-backdrop-filter: saturate(140%) blur(10px);
  backdrop-filter: saturate(140%) blur(10px);
  border-bottom: 1px solid var(--border);
}

.brand {
  display: inline-flex;
  align-items: center;
  gap: 10px;
  color: var(--text-strong);
  text-decoration: none;
  font-weight: 650;
  letter-spacing: .01em;
  min-width: 0;
}
.brand:hover { color: var(--text-strong); }
.brand__mark {
  display: grid;
  place-items: center;
  width: 30px; height: 30px;
  border-radius: 7px;
  background: linear-gradient(140deg, #2d5592, #1a2f55);
  border: 1px solid #3d68a8;
  color: #dbe8fb;
  font-family: var(--font-mono);
  font-size: .95rem;
  font-weight: 700;
  flex: none;
}
.brand__text { white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.brand__sub { color: var(--text-muted); font-weight: 450; margin-left: 4px; }

.topbar__links { margin-left: auto; display: flex; gap: 6px; }
.topbar__links a {
  display: inline-flex;
  align-items: center;
  gap: 7px;
  padding: 7px 12px;
  border-radius: var(--radius-sm);
  border: 1px solid transparent;
  color: var(--text);
  text-decoration: none;
  font-size: .9rem;
  transition: background-color .15s var(--ease), border-color .15s var(--ease);
}
.topbar__links a:hover { background: var(--bg-elev-2); border-color: var(--border); color: var(--text-strong); }

.nav-toggle {
  display: none;
  flex-direction: column;
  justify-content: center;
  gap: 4px;
  width: 40px; height: 40px;
  padding: 0 10px;
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  background: var(--bg-elev);
  cursor: pointer;
  flex: none;
}
.nav-toggle__bar {
  display: block;
  height: 2px;
  width: 100%;
  background: var(--text);
  border-radius: 2px;
  transition: transform .2s var(--ease), opacity .2s var(--ease);
}
.nav-open .nav-toggle__bar:nth-child(1) { transform: translateY(6px) rotate(45deg); }
.nav-open .nav-toggle__bar:nth-child(2) { opacity: 0; }
.nav-open .nav-toggle__bar:nth-child(3) { transform: translateY(-6px) rotate(-45deg); }

/* ---------- Layout shell ---------- */
.shell {
  display: grid;
  grid-template-columns: var(--sidebar-w) minmax(0, 1fr);
  max-width: calc(var(--sidebar-w) + var(--content-max) + 140px);
  margin: 0 auto;
  padding-top: var(--topbar-h);
}

.sidebar {
  position: sticky;
  top: var(--topbar-h);
  height: calc(100vh - var(--topbar-h));
  height: calc(100dvh - var(--topbar-h));
  border-right: 1px solid var(--border);
}
.sidebar__inner {
  height: 100%;
  overflow-y: auto;
  overscroll-behavior: contain;
  padding: 28px 18px 40px 20px;
  scrollbar-width: thin;
  scrollbar-color: var(--border-strong) transparent;
}
.sidebar__title {
  margin: 0 0 12px 10px;
  font-size: .75rem;
  font-weight: 650;
  text-transform: uppercase;
  letter-spacing: .09em;
  color: var(--text-muted);
}

.toc__list { list-style: none; margin: 0; padding: 0; }
.toc__list--sub { margin: 2px 0 6px 12px; padding-left: 8px; border-left: 1px solid var(--border); }
.toc__link {
  display: block;
  padding: 5px 10px;
  border-radius: var(--radius-sm);
  color: var(--text-muted);
  text-decoration: none;
  font-size: .9rem;
  line-height: 1.45;
  border-left: 2px solid transparent;
  transition: color .15s var(--ease), background-color .15s var(--ease), border-color .15s var(--ease);
}
.toc__item--h3 .toc__link { font-size: .85rem; padding-top: 4px; padding-bottom: 4px; }
.toc__link:hover { color: var(--text-strong); background: var(--bg-elev); }
.toc__link.is-active {
  color: var(--accent-strong);
  background: var(--accent-soft);
  border-left-color: var(--accent);
  border-top-left-radius: 0;
  border-bottom-left-radius: 0;
}
.toc__empty { color: var(--text-muted); font-size: .9rem; margin: 0 10px; }

.sidebar-backdrop { display: none; }

.main {
  min-width: 0;
  padding: 40px 56px 64px;
}
.main:focus { outline: none; }

/* ---------- Article typography ---------- */
.doc { max-width: var(--content-max); overflow-wrap: break-word; }
.doc a { overflow-wrap: anywhere; }

.doc h1, .doc h2, .doc h3, .doc h4 {
  position: relative;
  color: var(--text-strong);
  line-height: 1.25;
  letter-spacing: -0.012em;
  font-weight: 700;
}
.doc h1 {
  font-size: clamp(1.9rem, 1.3rem + 2.2vw, 2.7rem);
  margin: 0 0 .35em;
  letter-spacing: -0.022em;
}
.doc h1 + p em { color: var(--text-muted); font-size: 1.08rem; }
.doc h2 {
  font-size: clamp(1.4rem, 1.15rem + 1vw, 1.75rem);
  margin: 2.6em 0 .8em;
  padding-top: .9em;
  border-top: 1px solid var(--border);
}
.doc h3 { font-size: 1.22rem; margin: 2em 0 .6em; }
.doc h4 { font-size: 1.05rem; margin: 1.6em 0 .5em; }

.heading-anchor {
  margin-left: .4em;
  color: var(--text-muted);
  text-decoration: none;
  font-weight: 500;
  opacity: 0;
  transition: opacity .15s var(--ease), color .15s var(--ease);
}
.doc :is(h2, h3, h4):hover .heading-anchor,
.heading-anchor:focus-visible { opacity: 1; }
.heading-anchor:hover { color: var(--accent); }
@media (hover: none) { .heading-anchor { opacity: .5; } }

.doc p, .doc ul, .doc ol, .doc blockquote, .doc pre, .doc .table-wrap, .doc figure { margin: 0 0 1.2em; }
.doc ul, .doc ol { padding-left: 1.5em; }
.doc li { margin: .3em 0; }
.doc li::marker { color: var(--accent); }
.doc strong { color: var(--text-strong); font-weight: 650; }
.doc em { color: #e2e9f4; }
.doc hr { border: 0; border-top: 1px solid var(--border); margin: 2.5em 0; }

.doc blockquote {
  padding: 14px 18px;
  border: 1px solid var(--border);
  border-left: 3px solid var(--accent);
  border-radius: 0 var(--radius) var(--radius) 0;
  background: var(--accent-soft);
  color: var(--text);
}
.doc blockquote > :last-child { margin-bottom: 0; }

/* Inline code */
.doc :not(pre) > code {
  padding: .14em .42em;
  border-radius: 5px;
  background: var(--bg-elev-2);
  border: 1px solid var(--border);
  color: #cfe0fb;
  font-family: var(--font-mono);
  font-size: .86em;
  overflow-wrap: anywhere;
}

/* ---------- Tables ---------- */
.table-wrap {
  overflow-x: auto;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: var(--bg-elev);
  scrollbar-width: thin;
  scrollbar-color: var(--border-strong) transparent;
}
.doc table {
  width: 100%;
  border-collapse: collapse;
  font-size: .94rem;
  line-height: 1.5;
}
.doc th, .doc td {
  padding: 10px 14px;
  border-bottom: 1px solid var(--border);
  vertical-align: top;
  text-align: left;
}
.doc th {
  background: var(--bg-elev-2);
  color: var(--text-strong);
  font-weight: 650;
  font-size: .82rem;
  text-transform: uppercase;
  letter-spacing: .05em;
  white-space: nowrap;
}
.doc td[style*="right"], .doc th[style*="right"] { font-variant-numeric: tabular-nums; white-space: nowrap; }
.doc tbody tr:last-child td { border-bottom: 0; }
.doc tbody tr:hover td { background: rgba(127, 176, 245, 0.04); }
.doc td code { white-space: nowrap; }

/* ---------- Code blocks + Rouge syntax highlighting ---------- */
.doc pre, .doc div.highlighter-rouge {
  font-family: var(--font-mono);
}
.doc div.highlighter-rouge {
  position: relative;
  margin: 0 0 1.4em;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: var(--bg-code);
  overflow: hidden;
}
.doc div.language-cpp::before,
.doc div.language-c::before,
.doc div.language-js::before,
.doc div.language-javascript::before,
.doc div.language-python::before,
.doc div.language-bash::before,
.doc div.language-shell::before,
.doc div.language-yaml::before,
.doc div.language-json::before {
  display: block;
  padding: 6px 14px;
  border-bottom: 1px solid var(--border);
  background: var(--bg-elev);
  color: var(--text-muted);
  font-family: var(--font-sans);
  font-size: .74rem;
  font-weight: 600;
  letter-spacing: .08em;
  text-transform: uppercase;
}
.doc div.language-cpp::before { content: "C++"; }
.doc div.language-c::before { content: "C"; }
.doc div.language-js::before, .doc div.language-javascript::before { content: "JavaScript"; }
.doc div.language-python::before { content: "Python"; }
.doc div.language-bash::before, .doc div.language-shell::before { content: "Shell"; }
.doc div.language-yaml::before { content: "YAML"; }
.doc div.language-json::before { content: "JSON"; }
.doc .highlight { margin: 0; background: transparent; }
.doc pre {
  margin: 0;
  padding: 16px 18px;
  overflow-x: auto;
  font-size: .86rem;
  line-height: 1.6;
  color: #d6e2f3;
  tab-size: 4;
  scrollbar-width: thin;
  scrollbar-color: var(--border-strong) transparent;
}
.doc pre code { font-family: inherit; font-size: inherit; background: none; border: 0; padding: 0; }

/* Rouge token colours (muted, high-contrast on #0a1424) */
.highlight .c, .highlight .c1, .highlight .cm, .highlight .cs, .highlight .cd { color: #7f93b2; font-style: italic; }
.highlight .cp, .highlight .cpf { color: #c9a6f2; }
.highlight .k, .highlight .kd, .highlight .kn, .highlight .kp, .highlight .kr, .highlight .kv { color: #c79bf2; }
.highlight .kt, .highlight .nc, .highlight .nn { color: #7fd1c7; }
.highlight .kc { color: #f2a97f; }
.highlight .nf, .highlight .fm { color: #8ab9ff; }
.highlight .nb, .highlight .bp { color: #7fd1c7; }
.highlight .na, .highlight .nt { color: #8ab9ff; }
.highlight .nv, .highlight .vi, .highlight .vg, .highlight .vc { color: #e8c58a; }
.highlight .s, .highlight .s1, .highlight .s2, .highlight .sb, .highlight .sc, .highlight .sd,
.highlight .se, .highlight .sh, .highlight .si, .highlight .sx, .highlight .sr, .highlight .ss, .highlight .dl { color: #a5d6a0; }
.highlight .m, .highlight .mi, .highlight .mf, .highlight .mh, .highlight .mo, .highlight .il, .highlight .mb { color: #f2a97f; }
.highlight .o, .highlight .ow { color: #9fb6d6; }
.highlight .p, .highlight .pi { color: #b4c3da; }
.highlight .n, .highlight .nx, .highlight .ni, .highlight .nl { color: #d6e2f3; }
.highlight .err { color: #ff9b9b; background: none; }
.highlight .gd { color: #ff9b9b; }
.highlight .gi { color: #a5d6a0; }

/* ---------- Mermaid diagram containers ---------- */
.diagram {
  margin: 1.6em 0 1.8em;
  border-radius: var(--radius);
  background:
    linear-gradient(var(--bg-elev), var(--bg-elev)) padding-box,
    linear-gradient(160deg, #2a4470, var(--border) 40%) border-box;
  border: 1px solid transparent;
  overflow: hidden;
}
.diagram__caption {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  padding: 8px 14px;
  border-bottom: 1px solid var(--border);
  background: var(--bg-elev-2);
  color: var(--text-muted);
  font-size: .78rem;
  font-weight: 600;
  letter-spacing: .06em;
  text-transform: uppercase;
}
.diagram__permalink { color: var(--text-muted); text-decoration: none; padding: 0 4px; }
.diagram__permalink:hover { color: var(--accent); }

.diagram__scroll {
  position: relative;
  overflow-x: auto;
  overflow-y: hidden;
  padding: 22px 18px;
  text-align: center;
  background:
    radial-gradient(circle at 1px 1px, rgba(127,176,245,.07) 1px, transparent 0) 0 0 / 22px 22px,
    #0d1729;
  scrollbar-width: thin;
  scrollbar-color: var(--border-strong) transparent;
}
.diagram__scroll svg {
  display: block;
  margin: 0 auto;
  height: auto;
  max-width: 100%;
}
.diagram[data-state="rendered"] .diagram__scroll svg { animation: diagram-in .35s var(--ease) both; }
@keyframes diagram-in { from { opacity: 0; transform: translateY(4px); } to { opacity: 1; transform: none; } }
@media (prefers-reduced-motion: reduce) {
  .diagram[data-state="rendered"] .diagram__scroll svg { animation: none; }
}

/* Source shown while loading / when Mermaid is unavailable */
.diagram__fallback {
  text-align: left;
  margin: 0;
  padding: 0;
  font-size: .82rem;
  color: #c5d3e8;
  white-space: pre;
}
.diagram[data-state="pending"] .diagram__fallback { opacity: .55; }
.diagram__note {
  margin: 0;
  padding: 10px 14px;
  background: var(--warn-soft);
  border-bottom: 1px solid var(--border);
  color: #f0d3a6;
  font-size: .88rem;
}

.diagram--overflow .diagram__caption::after {
  content: "Scroll →";
  margin-left: auto;
  color: var(--accent);
  letter-spacing: .04em;
}
.diagram--overflow .diagram__permalink { order: 2; }

.diagram__source { border-top: 1px solid var(--border); }
.diagram__source > summary {
  cursor: pointer;
  padding: 8px 14px;
  color: var(--text-muted);
  font-size: .82rem;
  list-style-position: inside;
}
.diagram__source > summary:hover { color: var(--text-strong); }
.diagram__source pre { background: var(--bg-code); border-top: 1px solid var(--border); font-size: .8rem; }

/* Mermaid text inherits our font; keep edge labels readable on dark */
.diagram svg .edgeLabel, .diagram svg .edgeLabel p { background-color: #0f1a2e !important; }

/* ---------- Footer ---------- */
.doc-footer {
  max-width: var(--content-max);
  margin-top: 64px;
  padding-top: 20px;
  border-top: 1px solid var(--border);
  display: flex;
  flex-wrap: wrap;
  justify-content: space-between;
  gap: 8px 24px;
  color: var(--text-muted);
  font-size: .88rem;
}
.doc-footer p { margin: 0; }
.back-to-top { white-space: nowrap; }

/* ---------- Responsive ---------- */
@media (max-width: 1279px) {
  :root { --sidebar-w: 270px; }
  .main { padding: 36px 40px 56px; }
}

/* Tablet + mobile: sidebar becomes an off-canvas drawer */
@media (max-width: 1023px) {
  .nav-toggle { display: inline-flex; }
  .shell { display: block; }

  .sidebar {
    position: fixed;
    top: var(--topbar-h);
    left: 0;
    bottom: 0;
    height: auto;
    width: min(86vw, 340px);
    z-index: 90;
    background: var(--bg-elev);
    border-right: 1px solid var(--border-strong);
    box-shadow: 24px 0 48px rgba(0, 0, 0, .45);
    transform: translateX(-102%);
    visibility: hidden;
    transition: transform .25s var(--ease), visibility 0s linear .25s;
  }
  .nav-open .sidebar {
    transform: none;
    visibility: visible;
    transition: transform .25s var(--ease), visibility 0s;
  }
  .sidebar-backdrop {
    display: block;
    position: fixed;
    inset: var(--topbar-h) 0 0 0;
    z-index: 80;
    background: rgba(4, 8, 16, .6);
    animation: fade-in .2s var(--ease) both;
  }
  .sidebar-backdrop[hidden] { display: none; }
  html:has(body.nav-open), .nav-open { overflow: hidden; }

  .main { padding: 30px 28px 48px; }
}
@keyframes fade-in { from { opacity: 0; } to { opacity: 1; } }

@media (max-width: 640px) {
  :root { --topbar-h: 56px; }
  body { font-size: 1rem; line-height: 1.65; }
  .topbar { padding: 0 12px; gap: 10px; }
  .brand__sub { display: none; }
  .topbar__links a span { display: none; }
  .topbar__links a { padding: 9px; }
  .main { padding: 24px 16px 40px; }
  .doc h2 { margin-top: 2.2em; }
  .doc pre { padding: 14px; font-size: .8rem; }
  .diagram__scroll { padding: 16px 12px; }
  .doc th, .doc td { padding: 9px 11px; }
}

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { transition-duration: 0s !important; animation-duration: 0s !important; }
}

/* ---------- Print ---------- */
@media print {
  .topbar, .sidebar, .sidebar-backdrop, .skip-link, .heading-anchor, .diagram__source, .back-to-top { display: none !important; }
  .shell { display: block; padding: 0; }
  .main { padding: 0; }
}
