:root {
  --bg: #f4f7fb;
  --surface: #ffffff;
  --border: #e2e8f0;
  --text: #0f172a;
  --muted: #64748b;
  --accent: #0d9488;
  --accent-dark: #0f766e;
  --accent-soft: #ccfbf1;
  --accent-glow: rgb(13 148 136 / 12%);
  --sidebar-width: 288px;
  --toc-width: 200px;
  --header-height: 60px;
  --radius: 12px;
  --shadow: 0 1px 3px rgb(15 23 42 / 6%), 0 8px 24px rgb(15 23 42 / 4%);
  --font: "Segoe UI", system-ui, -apple-system, BlinkMacSystemFont, sans-serif;
  --mono: ui-monospace, "Cascadia Code", "Source Code Pro", monospace;
}

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

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

body {
  margin: 0;
  font-family: var(--font);
  color: var(--text);
  background: var(--bg);
  line-height: 1.65;
  -webkit-font-smoothing: antialiased;
  overflow-x: hidden;
}

body.nav-open {
  overflow: hidden;
  touch-action: none;
}

a {
  color: var(--accent-dark);
  text-decoration: none;
}

a:hover {
  text-decoration: underline;
}

.skip-link {
  position: absolute;
  left: -9999px;
  top: 0;
  z-index: 100;
  padding: 0.75rem 1rem;
  background: var(--accent);
  color: #fff;
  border-radius: 0 0 8px 0;
}

.skip-link:focus {
  left: 0;
}

.layout {
  display: flex;
  min-height: 100vh;
}

.sidebar {
  width: min(var(--sidebar-width), 100vw);
  max-width: 100%;
  background: linear-gradient(180deg, #fff 0%, #fafcfd 100%);
  border-right: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  position: fixed;
  top: 0;
  left: 0;
  bottom: 0;
  z-index: 30;
  transform: translateX(0);
  transition: transform 0.22s ease;
  box-shadow: 2px 0 12px rgb(15 23 42 / 3%);
  -webkit-overflow-scrolling: touch;
  padding-top: env(safe-area-inset-top, 0);
  padding-bottom: env(safe-area-inset-bottom, 0);
}

.sidebar-header {
  padding: 1.15rem 1.1rem 1rem;
  border-bottom: 1px solid var(--border);
  background: linear-gradient(135deg, var(--accent-soft) 0%, #fff 55%);
}

.brand {
  display: flex;
  align-items: center;
  gap: 0.65rem;
  text-decoration: none;
  color: var(--text);
}

.brand:hover {
  text-decoration: none;
}

.brand-mark {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 2.25rem;
  height: 2.25rem;
  border-radius: 10px;
  background: var(--accent);
  color: #fff;
  font-size: 0.72rem;
  font-weight: 800;
  letter-spacing: 0.04em;
  flex-shrink: 0;
}

.brand-text {
  font-weight: 700;
  font-size: 0.98rem;
  line-height: 1.25;
}

.brand-sub {
  display: block;
  margin-top: 0.55rem;
  font-size: 0.76rem;
  color: var(--muted);
  line-height: 1.4;
}

.nav-search {
  margin: 0.85rem 1rem 0;
}

.nav-search input {
  width: 100%;
  padding: 0.55rem 0.7rem;
  border: 1px solid var(--border);
  border-radius: 10px;
  font: inherit;
  font-size: 16px;
  background: var(--surface);
  transition: border-color 0.15s, box-shadow 0.15s;
}

.nav-search input:focus {
  outline: none;
  border-color: var(--accent);
  box-shadow: 0 0 0 3px var(--accent-glow);
}

.sidebar-nav {
  flex: 1;
  overflow-y: auto;
  padding: 0.65rem 0 1.5rem;
}

.nav-section {
  margin-bottom: 0.85rem;
}

.nav-section-title {
  padding: 0.4rem 1.1rem;
  font-size: 0.68rem;
  font-weight: 700;
  letter-spacing: 0.08em;
  text-transform: uppercase;
  color: var(--muted);
}

.nav-link {
  display: flex;
  align-items: center;
  padding: 0.62rem 1.1rem;
  min-height: 44px;
  color: var(--text);
  font-size: 0.88rem;
  text-decoration: none;
  border-left: 3px solid transparent;
  transition: background 0.12s;
}

.nav-link:hover {
  background: rgb(255 255 255 / 70%);
  text-decoration: none;
}

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

.main {
  flex: 1;
  margin-left: var(--sidebar-width);
  min-width: 0;
}

.topbar {
  position: sticky;
  top: 0;
  z-index: 10;
  min-height: var(--header-height);
  display: flex;
  align-items: center;
  gap: 0.5rem;
  padding: 0.5rem max(0.75rem, env(safe-area-inset-right, 0)) 0.5rem max(0.75rem, env(safe-area-inset-left, 0));
  background: rgb(255 255 255 / 94%);
  backdrop-filter: blur(10px);
  -webkit-backdrop-filter: blur(10px);
  border-bottom: 1px solid var(--border);
}

.menu-toggle {
  display: none;
  flex-shrink: 0;
  border: 1px solid var(--border);
  background: var(--surface);
  border-radius: 10px;
  padding: 0.55rem 0.8rem;
  min-height: 44px;
  min-width: 44px;
  cursor: pointer;
  font: inherit;
  font-size: 0.875rem;
  font-weight: 600;
  color: var(--accent-dark);
}

.doc-title {
  flex: 1;
  font-size: 0.95rem;
  font-weight: 600;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.topbar-actions {
  display: flex;
  gap: 0.5rem;
  align-items: center;
}

.topbar-link {
  font-size: 0.84rem;
  color: var(--muted);
  white-space: nowrap;
  padding: 0.5rem 0.75rem;
  min-height: 44px;
  display: inline-flex;
  align-items: center;
  border-radius: 8px;
}

.topbar-link:hover {
  color: var(--accent-dark);
  background: var(--accent-soft);
  text-decoration: none;
}

.topbar-link--primary {
  color: #fff;
  background: var(--accent);
  font-weight: 600;
}

.topbar-link--primary:hover {
  background: var(--accent-dark);
  color: #fff;
}

.content-wrap {
  width: 100%;
  max-width: 920px;
  margin: 0 auto;
  padding: 2rem max(1.25rem, env(safe-area-inset-right, 0)) 4rem max(1.25rem, env(safe-area-inset-left, 0));
  display: flex;
  flex-direction: column;
  gap: 2rem;
  align-items: stretch;
}

.content-main {
  width: 100%;
  min-width: 0;
}

.page-toc {
  width: 100%;
  font-size: 0.8rem;
  order: -1;
  padding: 1rem;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
}

.page-toc-title {
  font-size: 0.68rem;
  font-weight: 700;
  letter-spacing: 0.06em;
  text-transform: uppercase;
  color: var(--muted);
  margin-bottom: 0.65rem;
}

.page-toc nav {
  display: flex;
  flex-direction: column;
  gap: 0.15rem;
  border-left: 2px solid var(--border);
  padding-left: 0.75rem;
}

.page-toc a {
  display: block;
  padding: 0.2rem 0;
  color: var(--muted);
  line-height: 1.35;
  text-decoration: none;
}

.page-toc a:hover,
.page-toc a.active {
  color: var(--accent-dark);
}

.page-toc a.toc-h3 {
  padding-left: 0.65rem;
  font-size: 0.76rem;
}

.loading,
.error-state {
  padding: 3rem 2rem;
  text-align: center;
  color: var(--muted);
}

.loading-spinner {
  display: inline-block;
  width: 1.25rem;
  height: 1.25rem;
  margin-right: 0.5rem;
  border: 2px solid var(--border);
  border-top-color: var(--accent);
  border-radius: 50%;
  animation: spin 0.7s linear infinite;
  vertical-align: middle;
}

@keyframes spin {
  to { transform: rotate(360deg); }
}

.error-state {
  color: #b91c1c;
  background: #fef2f2;
  border-radius: var(--radius);
  border: 1px solid #fecaca;
}

.markdown-body h1,
.markdown-body h2,
.markdown-body h3,
.markdown-body h4 {
  line-height: 1.25;
  scroll-margin-top: calc(var(--header-height) + 1.25rem);
}

.markdown-body h1 {
  font-size: clamp(1.75rem, 4vw, 2.15rem);
  margin: 0 0 1rem;
  letter-spacing: -0.02em;
}

.markdown-body h2 {
  font-size: clamp(1.15rem, 3.5vw, 1.4rem);
  margin: 2.25rem 0 0.75rem;
  padding-top: 0.25rem;
}

.markdown-body h3 {
  font-size: clamp(1rem, 3vw, 1.1rem);
  margin: 1.5rem 0 0.5rem;
}

.markdown-body > *:first-child {
  margin-top: 0;
}

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

.markdown-body ul,
.markdown-body ol {
  padding-left: 1.35rem;
}

.markdown-body li {
  margin: 0.3rem 0;
}

.markdown-body blockquote {
  margin-left: 0;
  padding: 0.85rem 1rem;
  border-left: 4px solid var(--accent);
  background: var(--accent-soft);
  border-radius: 0 var(--radius) var(--radius) 0;
}

.markdown-body blockquote.doc-attribution {
  border-left-color: var(--border);
  background: var(--bg);
  color: var(--muted);
  font-size: 0.84rem;
  padding: 0.6rem 0.9rem;
  margin-bottom: 1.25rem;
}

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

.markdown-body table {
  width: 100%;
  max-width: 100%;
  border-collapse: collapse;
  margin: 1.15rem 0;
  font-size: clamp(0.78rem, 2.5vw, 0.9rem);
  display: block;
  overflow-x: auto;
  -webkit-overflow-scrolling: touch;
  background: var(--surface);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
}

.markdown-body th,
.markdown-body td {
  border: 1px solid var(--border);
  padding: 0.6rem 0.8rem;
  text-align: left;
  min-width: 7rem;
  vertical-align: top;
  word-break: break-word;
}

.markdown-body th {
  background: var(--bg);
  font-weight: 600;
}

.markdown-body tr:nth-child(even) td {
  background: rgb(248 250 252 / 80%);
}

.markdown-body code {
  font-family: var(--mono);
  font-size: 0.86em;
  background: #e8eef5;
  padding: 0.14rem 0.38rem;
  border-radius: 5px;
}

.markdown-body pre {
  background: #0f172a;
  color: #e2e8f0;
  padding: 1rem 1.1rem;
  border-radius: var(--radius);
  overflow-x: auto;
  -webkit-overflow-scrolling: touch;
  max-width: 100%;
  margin: 1.1rem 0;
  box-shadow: var(--shadow);
}

.markdown-body pre code {
  background: none;
  padding: 0;
  color: inherit;
  font-size: 0.84rem;
  white-space: pre;
  word-break: normal;
}

.markdown-body .mermaid {
  margin: 1.35rem 0;
  padding: 0.75rem;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  overflow-x: auto;
  -webkit-overflow-scrolling: touch;
  max-width: 100%;
  box-shadow: var(--shadow);
}

.markdown-body .mermaid svg {
  max-width: 100%;
  height: auto !important;
}

.table-scroll-hint {
  display: none;
  font-size: 0.75rem;
  color: var(--muted);
  margin: -0.5rem 0 0.75rem;
}

.markdown-body a {
  font-weight: 500;
  word-break: break-word;
}

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

.doc-nav-footer {
  display: flex;
  justify-content: space-between;
  gap: 1rem;
  margin-top: 3rem;
  padding-top: 1.5rem;
  border-top: 1px solid var(--border);
}

.doc-nav-footer a {
  display: block;
  flex: 1 1 100%;
  max-width: 100%;
  padding: 1rem 1.1rem;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: var(--surface);
  text-decoration: none;
  color: var(--text);
  box-shadow: var(--shadow);
  transition: border-color 0.15s, transform 0.15s;
  min-height: 44px;
}

.doc-nav-footer a:hover {
  border-color: var(--accent);
  transform: translateY(-1px);
  text-decoration: none;
}

.doc-nav-footer .label {
  display: block;
  font-size: 0.72rem;
  font-weight: 600;
  letter-spacing: 0.04em;
  text-transform: uppercase;
  color: var(--muted);
  margin-bottom: 0.25rem;
}

.doc-nav-footer .next {
  margin-left: auto;
  text-align: right;
}

.site-footer {
  margin-top: 2.5rem;
  padding: 1.35rem 1rem;
  border-top: 1px solid var(--border);
  text-align: center;
  background: var(--surface);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
}

.site-footer-lead {
  margin: 0 0 0.25rem;
  font-size: 0.92rem;
  color: var(--text);
}

.site-footer-meta {
  margin: 0 0 0.65rem;
  font-size: 0.82rem;
  color: var(--muted);
}

.site-footer-credit {
  margin: 0;
  font-size: 0.76rem;
  color: var(--muted);
}

.sidebar-overlay {
  display: none;
  position: fixed;
  inset: 0;
  background: rgb(15 23 42 / 45%);
  z-index: 25;
  border: none;
  padding: 0;
  cursor: pointer;
}

/* Tablet: sidebar becomes drawer */
@media (max-width: 1024px) {
  .sidebar {
    transform: translateX(-100%);
    width: min(320px, 88vw);
  }

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

  .sidebar-overlay:not([hidden]) {
    display: block;
  }

  .main {
    margin-left: 0;
    width: 100%;
  }

  .menu-toggle {
    display: inline-flex;
    align-items: center;
    justify-content: center;
  }

  .content-wrap {
    padding-top: 1.5rem;
    padding-bottom: 3rem;
  }
}

/* Desktop: sidebar fixed, optional right TOC */
@media (min-width: 1025px) {
  .main {
    margin-left: var(--sidebar-width);
    width: calc(100% - var(--sidebar-width));
  }

  .content-wrap {
    max-width: 1080px;
    flex-direction: row;
    align-items: flex-start;
    gap: 2.5rem;
  }

  .content-main {
    flex: 1;
    min-width: 0;
  }

  .page-toc {
    display: block;
    width: var(--toc-width);
    flex-shrink: 0;
    order: 1;
    position: sticky;
    top: calc(var(--header-height) + 1.5rem);
    padding: 0;
    background: transparent;
    border: none;
    border-radius: 0;
    box-shadow: none;
  }

  .page-toc[hidden] {
    display: none !important;
  }
}

/* Large desktop */
@media (min-width: 1280px) {
  .content-wrap {
    max-width: 1180px;
    padding-left: 2rem;
    padding-right: 2rem;
  }
}

/* Mobile phones */
@media (max-width: 640px) {
  .brand-text {
    font-size: 0.88rem;
  }

  .brand-sub {
    font-size: 0.72rem;
  }

  .topbar-actions {
    gap: 0.35rem;
  }

  .topbar-link {
    font-size: 0.78rem;
    padding: 0.45rem 0.6rem;
  }

  .topbar-link--primary {
    padding: 0.45rem 0.65rem;
  }

  .content-wrap {
    padding: 1rem max(0.75rem, env(safe-area-inset-right, 0)) 2.5rem max(0.75rem, env(safe-area-inset-left, 0));
  }

  .markdown-body h1 {
    font-size: 1.55rem;
  }

  .markdown-body th,
  .markdown-body td {
    padding: 0.5rem 0.6rem;
    min-width: 6.5rem;
  }

  .table-scroll-hint {
    display: block;
  }

  .doc-nav-footer {
    flex-direction: column;
  }

  .doc-nav-footer .next {
    margin-left: 0;
    text-align: left;
  }

  .doc-nav-footer a:hover {
    transform: none;
  }

  .site-footer {
    padding: 1.1rem 0.85rem;
  }

  .page-toc nav {
    max-height: 12rem;
    overflow-y: auto;
  }
}

/* Small phones */
@media (max-width: 380px) {
  .doc-title {
    font-size: 0.85rem;
  }

  .topbar-actions .topbar-link:not(.topbar-link--primary) {
    display: none;
  }

  .menu-toggle {
    padding: 0.5rem 0.65rem;
    font-size: 0.8rem;
  }
}

/* Touch devices: remove hover lift that sticks */
@media (hover: none) {
  .doc-nav-footer a:hover {
    transform: none;
  }
}

@media (max-width: 1024px) {
  .page-toc[hidden] {
    display: none !important;
  }

  .page-toc:not([hidden]) {
    display: block;
  }
}

@media print {
  .sidebar,
  .topbar,
  .page-toc,
  .doc-nav-footer,
  .skip-link,
  .nav-search {
    display: none !important;
  }

  .main {
    margin: 0;
  }

  .content-wrap {
    max-width: none;
    padding: 0;
  }

  .markdown-body a {
    color: inherit;
    text-decoration: underline;
  }

  .markdown-body pre {
    white-space: pre-wrap;
  }
}
