:root {
  --bg: #0f1419;
  --bg-elevated: #1a2332;
  --bg-sidebar: #151c27;
  --text: #e7ecf3;
  --text-muted: #9aa8bc;
  --accent: #5b9fd4;
  --accent-soft: rgba(91, 159, 212, 0.15);
  --border: #2a3544;
  --code-bg: #0d1117;
  --shadow: 0 4px 24px rgba(0, 0, 0, 0.35);
  --topbar-h: 3.25rem;
  --sidebar-w: min(18rem, 88vw);
  --content-max: 46rem;
  --radius: 8px;
  --font: system-ui, -apple-system, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  --font-mono: ui-monospace, "Cascadia Code", "Segoe UI Mono", Consolas, monospace;
}

body.light {
  --bg: #f4f6f9;
  --bg-elevated: #ffffff;
  --bg-sidebar: #eef1f6;
  --text: #1a2332;
  --text-muted: #5a6578;
  --accent: #2563a8;
  --accent-soft: rgba(37, 99, 168, 0.12);
  --border: #d4dae3;
  --code-bg: #eef1f6;
  --shadow: 0 4px 20px rgba(0, 0, 0, 0.08);
}

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

html {
  -webkit-text-size-adjust: 100%;
  scroll-behavior: smooth;
}

body {
  margin: 0;
  font-family: var(--font);
  font-size: 1rem;
  line-height: 1.6;
  color: var(--text);
  background: var(--bg);
  overflow-x: hidden;
}

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

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

/* Top bar */
.topbar {
  position: sticky;
  top: 0;
  z-index: 100;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 0.75rem;
  height: var(--topbar-h);
  padding: 0 1rem;
  background: var(--bg-elevated);
  border-bottom: 1px solid var(--border);
}

.topbar-left {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  min-width: 0;
}

.brand {
  font-weight: 600;
  font-size: 0.95rem;
  color: var(--text);
  text-decoration: none;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.brand span {
  color: var(--accent);
}

.actions {
  display: flex;
  align-items: center;
  gap: 0.35rem;
  flex-shrink: 0;
}

.icon-btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  min-width: 2.75rem;
  min-height: 2.75rem;
  padding: 0.35rem 0.65rem;
  font-size: 1.1rem;
  line-height: 1;
  color: var(--text);
  background: transparent;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  cursor: pointer;
  touch-action: manipulation;
}

.icon-btn:hover,
.icon-btn:focus-visible {
  background: var(--accent-soft);
  border-color: var(--accent);
  outline: none;
}

#menu-toggle {
  display: none;
}

/* Layout */
.layout {
  display: flex;
  min-height: calc(100vh - var(--topbar-h));
}

.sidebar-overlay {
  display: none;
  position: fixed;
  inset: var(--topbar-h) 0 0 0;
  z-index: 80;
  background: rgba(0, 0, 0, 0.45);
  border: none;
  cursor: pointer;
}

body.sidebar-open .sidebar-overlay {
  display: block;
}

#sidebar {
  position: sticky;
  top: var(--topbar-h);
  flex: 0 0 var(--sidebar-w);
  width: var(--sidebar-w);
  max-height: calc(100vh - var(--topbar-h));
  overflow-y: auto;
  overscroll-behavior: contain;
  padding: 1rem 0.75rem 1.5rem;
  background: var(--bg-sidebar);
  border-right: 1px solid var(--border);
  -webkit-overflow-scrolling: touch;
}

.search {
  margin-bottom: 0.75rem;
  padding: 0 0.25rem;
}

.search input {
  width: 100%;
  min-height: 2.75rem;
  padding: 0.5rem 0.75rem;
  font-size: 1rem;
  font-family: inherit;
  color: var(--text);
  background: var(--bg-elevated);
  border: 1px solid var(--border);
  border-radius: var(--radius);
}

.search input:focus {
  outline: 2px solid var(--accent);
  outline-offset: 1px;
}

#nav {
  display: flex;
  flex-direction: column;
  gap: 0.2rem;
}

.phase-link {
  display: block;
  padding: 0.65rem 0.75rem;
  font-size: 0.875rem;
  line-height: 1.35;
  color: var(--text-muted);
  text-decoration: none;
  border-radius: var(--radius);
  border-left: 3px solid transparent;
  touch-action: manipulation;
}

.phase-link:hover,
.phase-link:focus-visible {
  color: var(--text);
  background: var(--accent-soft);
  outline: none;
}

.phase-link.active {
  color: var(--text);
  background: var(--accent-soft);
  border-left-color: var(--accent);
  font-weight: 600;
}

.sidebar-extra {
  margin-top: 1rem;
  padding-top: 1rem;
  border-top: 1px solid var(--border);
}

.sidebar-extra .phase-link {
  font-size: 0.8125rem;
}

.nav-empty {
  padding: 0.75rem;
  font-size: 0.875rem;
  color: var(--text-muted);
}

main {
  flex: 1;
  min-width: 0;
  padding: 1rem 1rem 3rem;
}

.hero {
  max-width: var(--content-max);
  margin: 0 auto 1.5rem;
  padding: 1.25rem 1rem;
  background: var(--bg-elevated);
  border: 1px solid var(--border);
  border-radius: calc(var(--radius) + 2px);
  box-shadow: var(--shadow);
}

.hero.is-collapsed {
  display: none;
}

.eyebrow {
  margin: 0 0 0.35rem;
  font-size: 0.7rem;
  font-weight: 700;
  letter-spacing: 0.08em;
  color: var(--accent);
}

.hero h1 {
  margin: 0 0 0.5rem;
  font-size: clamp(1.35rem, 4vw, 1.85rem);
  line-height: 1.25;
}

.hero > p:not(.eyebrow) {
  margin: 0 0 1rem;
  color: var(--text-muted);
  font-size: 0.95rem;
}

.stats {
  display: flex;
  flex-wrap: wrap;
  gap: 1rem 1.5rem;
}

.stats div {
  display: flex;
  flex-direction: column;
  gap: 0.1rem;
}

.stats b {
  font-size: 1.25rem;
  color: var(--text);
}

.stats span {
  font-size: 0.75rem;
  color: var(--text-muted);
  text-transform: uppercase;
  letter-spacing: 0.04em;
}

.content {
  max-width: var(--content-max);
  margin: 0 auto;
}

.content h1 {
  font-size: clamp(1.4rem, 4vw, 1.75rem);
  line-height: 1.3;
  margin: 0 0 1rem;
  word-wrap: break-word;
}

.content h2 {
  font-size: clamp(1.1rem, 3.2vw, 1.35rem);
  line-height: 1.35;
  margin: 2rem 0 0.75rem;
  padding-top: 0.5rem;
  border-top: 1px solid var(--border);
  word-wrap: break-word;
}

.content h2:first-of-type {
  border-top: none;
  margin-top: 0;
  padding-top: 0;
}

.content h3 {
  font-size: 1.05rem;
  margin: 1.25rem 0 0.5rem;
}

.content h4 {
  font-size: 0.98rem;
  margin: 1rem 0 0.4rem;
}

.content p {
  margin: 0 0 0.85rem;
  font-size: 0.9375rem;
  overflow-wrap: anywhere;
}

.content blockquote {
  margin: 0 0 1rem;
  padding: 0.75rem 1rem;
  border-left: 3px solid var(--accent);
  background: var(--accent-soft);
  border-radius: 0 var(--radius) var(--radius) 0;
  color: var(--text-muted);
  font-size: 0.9rem;
}

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

.content strong {
  color: var(--text);
  font-weight: 600;
}

.content code {
  font-family: var(--font-mono);
  font-size: 0.85em;
  padding: 0.12em 0.35em;
  background: var(--code-bg);
  border-radius: 4px;
  word-break: break-word;
}

.content pre {
  margin: 0 0 1rem;
  padding: 1rem;
  overflow-x: auto;
  -webkit-overflow-scrolling: touch;
  background: var(--code-bg);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  max-width: 100%;
}

.content pre code {
  display: block;
  padding: 0;
  font-size: 0.8125rem;
  line-height: 1.5;
  white-space: pre;
  word-break: normal;
  background: transparent;
}

.content ul,
.content ol {
  margin: 0 0 1rem;
  padding-left: 1.35rem;
  font-size: 0.9375rem;
}

.content li {
  margin-bottom: 0.35rem;
}

.content table {
  display: block;
  width: 100%;
  max-width: 100%;
  overflow-x: auto;
  -webkit-overflow-scrolling: touch;
  border-collapse: collapse;
  margin: 0 0 1rem;
  font-size: 0.875rem;
}

.content th,
.content td {
  padding: 0.5rem 0.65rem;
  border: 1px solid var(--border);
  text-align: left;
  vertical-align: top;
}

.content th {
  background: var(--bg-elevated);
  font-weight: 600;
}

.load-error {
  padding: 1rem;
  border: 1px solid #c45;
  border-radius: var(--radius);
  background: rgba(200, 68, 85, 0.12);
  color: var(--text);
  font-size: 0.9rem;
}

.load-status {
  padding: 2rem 1rem;
  text-align: center;
  color: var(--text-muted);
}

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

/* Mobile */
@media (max-width: 768px) {
  #menu-toggle {
    display: inline-flex;
  }

  #sidebar {
    position: fixed;
    top: var(--topbar-h);
    left: 0;
    bottom: 0;
    z-index: 90;
    transform: translateX(-105%);
    transition: transform 0.2s ease;
    box-shadow: var(--shadow);
  }

  body.sidebar-open #sidebar {
    transform: translateX(0);
  }

  main {
    padding: 0.75rem 0.85rem 2.5rem;
  }

  .hero {
    padding: 1rem 0.85rem;
    margin-bottom: 1rem;
  }
}

@media (min-width: 769px) {
  .sidebar-overlay {
    display: none !important;
  }
}

@media (min-width: 1024px) {
  main {
    padding: 1.5rem 2rem 3rem;
  }
}
