/* === Reset & Base === */
*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }

:root {
  --font-body: 'Inter', -apple-system, BlinkMacSystemFont, 'Segoe UI', sans-serif;
  --font-mono: 'JetBrains Mono', 'SF Mono', 'Fira Code', monospace;
  --color-bg: #ffffff;
  --color-text: #1a1a2e;
  --color-text-secondary: #555;
  --color-heading: #0f0f23;
  --color-link: #2563eb;
  --color-link-hover: #1d4ed8;
  --color-border: #e5e7eb;
  --color-code-bg: #f8f9fa;
  --color-blockquote-bg: #f0f4ff;
  --color-blockquote-border: #93a8d0;
  --color-table-header: #f1f5f9;
  --color-table-stripe: #f8fafc;
  --color-toc-active: #2563eb;
  --content-max-width: 780px;
  --toc-width: 260px;
}

@media (prefers-color-scheme: dark) {
  :root {
    --color-bg: #0d1117;
    --color-text: #c9d1d9;
    --color-text-secondary: #8b949e;
    --color-heading: #e6edf3;
    --color-link: #58a6ff;
    --color-link-hover: #79c0ff;
    --color-border: #30363d;
    --color-code-bg: #161b22;
    --color-blockquote-bg: #161b22;
    --color-blockquote-border: #3b5998;
    --color-table-header: #161b22;
    --color-table-stripe: #0d1117;
    --color-toc-active: #58a6ff;
  }
}

html { font-size: 16px; scroll-behavior: smooth; scroll-padding-top: 2rem; }

body {
  font-family: var(--font-body);
  color: var(--color-text);
  background: var(--color-bg);
  line-height: 1.75;
  -webkit-font-smoothing: antialiased;
}

/* === Layout === */
.layout {
  display: flex;
  max-width: calc(var(--content-max-width) + var(--toc-width) + 80px);
  margin: 0 auto;
  padding: 3rem 2rem 2rem;
  gap: 3rem;
}

.content {
  flex: 1;
  min-width: 0;
  max-width: var(--content-max-width);
}

/* === Typography === */
h1, h2, h3, h4, h5, h6 {
  color: var(--color-heading);
  font-weight: 600;
  line-height: 1.3;
  margin-top: 2.5rem;
  margin-bottom: 0.75rem;
}

h1 { font-size: 2rem; margin-top: 0; padding-bottom: 0.5rem; border-bottom: 2px solid var(--color-border); }
h2 { font-size: 1.5rem; padding-bottom: 0.35rem; border-bottom: 1px solid var(--color-border); }
h3 { font-size: 1.2rem; }
h4 { font-size: 1.05rem; }

p { margin-bottom: 1rem; }

a { color: var(--color-link); text-decoration: none; transition: color 0.15s; }
a:hover { color: var(--color-link-hover); text-decoration: underline; }

strong { font-weight: 600; }

hr {
  border: none;
  border-top: 1px solid var(--color-border);
  margin: 2.5rem 0;
}

/* === Lists === */
ul, ol {
  margin-bottom: 1rem;
  padding-left: 1.5rem;
}

li { margin-bottom: 0.35rem; }
li > ul, li > ol { margin-top: 0.35rem; margin-bottom: 0; }

/* === Code === */
code {
  font-family: var(--font-mono);
  font-size: 0.875em;
  background: var(--color-code-bg);
  padding: 0.15em 0.4em;
  border-radius: 4px;
  border: 1px solid var(--color-border);
}

pre {
  background: var(--color-code-bg);
  border: 1px solid var(--color-border);
  border-radius: 8px;
  padding: 1rem 1.25rem;
  overflow-x: auto;
  margin-bottom: 1.5rem;
  line-height: 1.5;
}

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

/* === Blockquotes === */
blockquote {
  background: var(--color-blockquote-bg);
  border-left: 4px solid var(--color-blockquote-border);
  padding: 1rem 1.25rem;
  margin: 1.5rem 0;
  border-radius: 0 6px 6px 0;
  color: var(--color-text-secondary);
}

blockquote p { margin-bottom: 0.5rem; }
blockquote p:last-child { margin-bottom: 0; }

/* === Tables === */
table {
  width: 100%;
  border-collapse: collapse;
  margin: 1.5rem 0;
  font-size: 0.9rem;
}

thead th {
  background: var(--color-table-header);
  font-weight: 600;
  text-align: left;
  padding: 0.75rem 1rem;
  border-bottom: 2px solid var(--color-border);
}

tbody td {
  padding: 0.65rem 1rem;
  border-bottom: 1px solid var(--color-border);
}

tbody tr:nth-child(even) { background: var(--color-table-stripe); }

/* === TOC Sidebar === */
.toc-sidebar {
  width: var(--toc-width);
  flex-shrink: 0;
}

.toc-nav {
  position: sticky;
  top: 2rem;
  max-height: calc(100vh - 4rem);
  overflow-y: auto;
}

.github-link {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  font-size: 0.8rem;
  font-weight: 500;
  color: var(--color-text-secondary);
  padding: 0.5rem 0.5rem;
  margin-bottom: 0.75rem;
  border-radius: 6px;
  transition: all 0.15s;
  text-decoration: none;
}

.github-link:hover {
  color: var(--color-text);
  background: var(--color-table-stripe);
  text-decoration: none;
}

.github-link svg { flex-shrink: 0; }

.toc-title {
  font-size: 0.85rem;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.05em;
  color: var(--color-text-secondary);
  margin-bottom: 0.75rem;
  padding-bottom: 0.5rem;
  border-bottom: 1px solid var(--color-border);
}

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

.toc-nav li { margin-bottom: 0.15rem; }

.toc-nav a {
  display: block;
  font-size: 0.8rem;
  color: var(--color-text-secondary);
  padding: 0.25rem 0.5rem;
  border-left: 2px solid transparent;
  border-radius: 0 4px 4px 0;
  transition: all 0.15s;
  text-decoration: none;
  line-height: 1.4;
}

.toc-nav a:hover {
  color: var(--color-text);
  background: var(--color-table-stripe);
}

.toc-nav a.active {
  color: var(--color-toc-active);
  border-left-color: var(--color-toc-active);
  font-weight: 500;
}

/* Indent sub-items (h3) */
.toc-nav ul ul { padding-left: 0.75rem; }
.toc-nav ul ul a { font-size: 0.75rem; }

/* === Footer === */
.site-footer {
  text-align: center;
  padding: 2rem;
  color: var(--color-text-secondary);
  font-size: 0.85rem;
  border-top: 1px solid var(--color-border);
  max-width: calc(var(--content-max-width) + var(--toc-width) + 80px);
  margin: 0 auto;
}

/* === Mobile TOC Toggle Button === */
.toc-toggle {
  display: none;
  position: fixed;
  top: auto;
  left: auto;
  bottom: 1.5rem;
  right: 1.5rem;
  z-index: 1000;
  width: 48px;
  height: 48px;
  border-radius: 50%;
  border: 1px solid var(--color-border);
  background: var(--color-bg);
  color: var(--color-text);
  cursor: pointer;
  box-shadow: 0 2px 12px rgba(0,0,0,0.15);
  align-items: center;
  justify-content: center;
  transition: transform 0.2s, box-shadow 0.2s;
}

.toc-toggle:hover { transform: scale(1.05); box-shadow: 0 4px 16px rgba(0,0,0,0.2); }
.toc-toggle:active { transform: scale(0.95); }

/* === Mobile TOC Overlay === */
.toc-overlay {
  display: none;
  position: fixed;
  inset: 0;
  z-index: 1001;
  background: rgba(0,0,0,0.4);
  backdrop-filter: blur(2px);
  -webkit-backdrop-filter: blur(2px);
  opacity: 0;
  transition: opacity 0.3s;
}
.toc-overlay.open { display: block; opacity: 1; }

/* === Mobile TOC Close Button === */
.toc-close {
  display: none;
  position: absolute;
  top: 0.75rem;
  right: 0.75rem;
  width: 32px;
  height: 32px;
  border: none;
  background: transparent;
  color: var(--color-text-secondary);
  font-size: 1.5rem;
  line-height: 1;
  cursor: pointer;
  border-radius: 6px;
  transition: background 0.15s, color 0.15s;
}
.toc-close:hover { background: var(--color-table-stripe); color: var(--color-text); }

/* === Responsive === */
@media (max-width: 1024px) {
  .toc-toggle { display: flex; }
  .toc-close { display: block; }

  .toc-sidebar {
    display: block;
    position: fixed;
    top: 0;
    left: auto;
    right: 0;
    bottom: 0;
    width: 300px;
    max-width: 85vw;
    z-index: 1002;
    background: var(--color-bg);
    border-left: 1px solid var(--color-border);
    box-shadow: -4px 0 24px rgba(0,0,0,0.1);
    transform: translateX(100%);
    transition: transform 0.3s cubic-bezier(0.4, 0, 0.2, 1);
    overflow-y: auto;
    padding: 1.5rem;
  }
  .toc-sidebar.open { transform: translateX(0); }

  .toc-nav { position: static; max-height: none; }

  .layout { max-width: var(--content-max-width); }
}

@media (max-width: 640px) {
  .layout { padding: 1.5rem 1rem; }
  h1 { font-size: 1.6rem; }
  h2 { font-size: 1.3rem; }
  table { font-size: 0.8rem; }
  thead th, tbody td { padding: 0.5rem 0.65rem; }
}
