/* ============================================
   Apple-Style Blog Design System
   ============================================ */

:root {
  --font-sans: -apple-system, BlinkMacSystemFont, 'SF Pro Display', 'SF Pro Text', 'Helvetica Neue', 'Segoe UI', sans-serif;
  --font-mono: 'SF Mono', 'Fira Code', 'JetBrains Mono', monospace;

  --color-bg: #fafafa;
  --color-text: #1d1d1f;
  --color-text-secondary: #86868b;
  --color-accent: #0071e3;
  --color-accent-hover: #0077ed;
  --color-code-bg: #f5f5f7;
  --color-card-bg: #ffffff;
  --color-border: #d2d2d7;

  --max-width-article: 720px;
  --max-width-index: 1200px;
  --section-padding: 80px;

  --shadow-card: 0 2px 12px rgba(0, 0, 0, 0.08);
  --radius-card: 16px;
  --radius-code: 12px;
  --radius-inline-code: 6px;
}

/* ---- Reset & Base ---- */

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

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

body {
  font-family: var(--font-sans);
  font-size: 18px;
  font-weight: 400;
  line-height: 1.75;
  color: var(--color-text);
  background-color: var(--color-bg);
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
}

::selection {
  background-color: var(--color-accent);
  color: #ffffff;
}

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

/* ---- Typography ---- */

h1, h2, h3, h4, h5, h6 {
  color: var(--color-text);
  margin-bottom: 0.5em;
}

h1 {
  font-size: 56px;
  font-weight: 700;
  letter-spacing: -0.025em;
  line-height: 1.1;
}

h2 {
  font-size: 40px;
  font-weight: 600;
  letter-spacing: -0.02em;
  line-height: 1.2;
}

h3 {
  font-size: 28px;
  font-weight: 600;
  letter-spacing: -0.015em;
  line-height: 1.3;
}

h4 {
  font-size: 20px;
  font-weight: 600;
  line-height: 1.4;
}

p {
  margin-bottom: 1.5em;
}

small, .caption {
  font-size: 14px;
  color: var(--color-text-secondary);
}

strong {
  font-weight: 600;
}

/* ---- Links ---- */

a {
  color: var(--color-accent);
  text-decoration: none;
  transition: color 0.2s ease;
}

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

/* ---- Code ---- */

code {
  font-family: var(--font-mono);
  font-size: 0.9em;
  background-color: var(--color-code-bg);
  padding: 2px 8px;
  border-radius: var(--radius-inline-code);
}

pre {
  background-color: var(--color-code-bg);
  border-radius: var(--radius-code);
  padding: 20px;
  overflow-x: auto;
  margin-bottom: 1.5em;
}

pre code {
  background: none;
  padding: 0;
  border-radius: 0;
  font-size: 15px;
  line-height: 1.6;
}

/* ---- Blockquotes ---- */

blockquote {
  border-left: 3px solid var(--color-accent);
  background-color: var(--color-code-bg);
  padding: 16px 24px;
  margin: 0 0 1.5em 0;
  border-radius: 0 var(--radius-inline-code) var(--radius-inline-code) 0;
}

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

/* ---- Tables ---- */

table {
  width: 100%;
  border-collapse: collapse;
  margin-bottom: 1.5em;
  font-size: 16px;
}

thead {
  background-color: var(--color-code-bg);
}

th, td {
  padding: 12px 16px;
  text-align: left;
  border-bottom: 1px solid var(--color-border);
}

th {
  font-weight: 600;
  font-size: 14px;
  text-transform: uppercase;
  letter-spacing: 0.05em;
  color: var(--color-text-secondary);
}

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

tbody tr:nth-child(odd) {
  background-color: var(--color-card-bg);
}

/* ---- Lists ---- */

ul, ol {
  margin-bottom: 1.5em;
  padding-left: 1.5em;
}

li {
  margin-bottom: 0.5em;
}

li::marker {
  color: var(--color-text-secondary);
}

/* ---- Horizontal Rule ---- */

hr {
  border: none;
  height: 1px;
  background-color: var(--color-border);
  margin: var(--section-padding) 0;
}

/* ============================================
   Layout
   ============================================ */

.container {
  max-width: var(--max-width-article);
  margin: 0 auto;
  padding: 0 24px;
}

.container--wide {
  max-width: var(--max-width-index);
}

/* ---- Section ---- */

.section {
  padding: var(--section-padding) 0;
}

/* ============================================
   Animations
   ============================================ */

@keyframes fadeInUp {
  from {
    opacity: 0;
    transform: translateY(30px);
  }
  to {
    opacity: 1;
    transform: translateY(0);
  }
}

@keyframes fadeIn {
  from {
    opacity: 0;
  }
  to {
    opacity: 1;
  }
}

.animate-in {
  opacity: 0;
  animation: fadeInUp 0.8s ease forwards;
}

.animate-delay-1 { animation-delay: 0.1s; }
.animate-delay-2 { animation-delay: 0.2s; }
.animate-delay-3 { animation-delay: 0.3s; }
.animate-delay-4 { animation-delay: 0.4s; }

/* ============================================
   Index Page — Hero
   ============================================ */

.hero {
  text-align: center;
  padding: 120px 24px 80px;
}

.hero__title {
  font-size: 64px;
  font-weight: 700;
  letter-spacing: -0.03em;
  line-height: 1.05;
  margin-bottom: 24px;
  background: linear-gradient(135deg, var(--color-text) 0%, var(--color-text-secondary) 100%);
  -webkit-background-clip: text;
  -webkit-text-fill-color: transparent;
  background-clip: text;
}

.hero__subtitle {
  font-size: 24px;
  font-weight: 400;
  color: var(--color-text-secondary);
  max-width: 600px;
  margin: 0 auto;
  line-height: 1.5;
}

/* ============================================
   Index Page — Chapter Cards Grid
   ============================================ */

.chapters-grid {
  display: grid;
  grid-template-columns: repeat(2, 1fr);
  gap: 24px;
  max-width: var(--max-width-index);
  margin: 0 auto;
  padding: 0 24px;
}

.chapter-card {
  background-color: var(--color-card-bg);
  border-radius: var(--radius-card);
  box-shadow: var(--shadow-card);
  padding: 32px;
  transition: transform 0.3s ease, box-shadow 0.3s ease;
  text-decoration: none;
  color: inherit;
  display: flex;
  flex-direction: column;
}

.chapter-card:hover {
  transform: translateY(-4px);
  box-shadow: 0 8px 30px rgba(0, 0, 0, 0.12);
  color: inherit;
}

.chapter-card__number {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 40px;
  height: 40px;
  border-radius: 50%;
  background-color: var(--color-accent);
  color: #ffffff;
  font-size: 16px;
  font-weight: 600;
  margin-bottom: 16px;
  flex-shrink: 0;
}

.chapter-card__title {
  font-size: 24px;
  font-weight: 600;
  letter-spacing: -0.01em;
  margin-bottom: 8px;
  line-height: 1.3;
}

.chapter-card__excerpt {
  font-size: 16px;
  color: var(--color-text-secondary);
  line-height: 1.6;
  flex-grow: 1;
}

.chapter-card__meta {
  font-size: 14px;
  color: var(--color-text-secondary);
  margin-top: 16px;
  padding-top: 16px;
  border-top: 1px solid var(--color-border);
}

/* ============================================
   Article Page — Top Navigation
   ============================================ */

.article-nav {
  position: sticky;
  top: 0;
  z-index: 100;
  background-color: rgba(250, 250, 250, 0.8);
  backdrop-filter: saturate(180%) blur(20px);
  -webkit-backdrop-filter: saturate(180%) blur(20px);
  border-bottom: 1px solid var(--color-border);
  padding: 0 24px;
  height: 52px;
  display: flex;
  align-items: center;
  justify-content: space-between;
}

.article-nav__back {
  font-size: 14px;
  font-weight: 500;
  display: flex;
  align-items: center;
  gap: 6px;
}

.article-nav__back::before {
  content: '←';
  font-size: 16px;
}

.article-nav__title {
  font-size: 14px;
  font-weight: 600;
  color: var(--color-text);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  max-width: 50%;
}

/* ---- Progress Bar ---- */

.progress-bar {
  position: fixed;
  top: 0;
  left: 0;
  width: 0%;
  height: 3px;
  background: linear-gradient(90deg, var(--color-accent), #40a9ff);
  z-index: 101;
  transition: width 0.1s linear;
}

/* ============================================
   Article Page — Hero
   ============================================ */

.article-hero {
  text-align: center;
  padding: 80px 24px var(--section-padding);
}

.article-hero__chapter {
  display: inline-block;
  font-size: 14px;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.1em;
  color: var(--color-accent);
  margin-bottom: 16px;
}

.article-hero__title {
  font-size: 56px;
  font-weight: 700;
  letter-spacing: -0.025em;
  line-height: 1.1;
  margin-bottom: 16px;
}

.article-hero__meta {
  font-size: 16px;
  color: var(--color-text-secondary);
}

/* ============================================
   Article Page — Content
   ============================================ */

.article-content {
  max-width: var(--max-width-article);
  margin: 0 auto;
  padding: 0 24px var(--section-padding);
}

.article-content section {
  margin-bottom: var(--section-padding);
}

.article-content section:last-child {
  margin-bottom: 0;
}

/* ============================================
   Article Page — Table of Contents (Sidebar)
   ============================================ */

.article-layout {
  display: flex;
  gap: 48px;
  max-width: 1100px;
  margin: 0 auto;
  padding: 0 24px var(--section-padding);
  align-items: flex-start;
}

.article-layout__content {
  flex: 1;
  min-width: 0;
}

.article-layout__sidebar {
  width: 240px;
  flex-shrink: 0;
  position: sticky;
  top: 80px;
}

.toc {
  font-size: 14px;
}

.toc__title {
  font-size: 12px;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.08em;
  color: var(--color-text-secondary);
  margin-bottom: 12px;
}

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

.toc__list li {
  margin-bottom: 8px;
}

.toc__list a {
  color: var(--color-text-secondary);
  transition: color 0.2s ease;
  display: block;
  line-height: 1.4;
}

.toc__list a:hover,
.toc__list a.active {
  color: var(--color-accent);
}

.toc__list .toc-indent {
  padding-left: 16px;
  font-size: 13px;
}

/* ============================================
   Article Page — References / Footnotes
   ============================================ */

.references {
  border-top: 1px solid var(--color-border);
  padding-top: var(--section-padding);
  margin-top: var(--section-padding);
}

.references__title {
  font-size: 20px;
  font-weight: 600;
  margin-bottom: 16px;
}

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

.references__list li {
  font-size: 15px;
  color: var(--color-text-secondary);
  margin-bottom: 8px;
  padding-left: 24px;
  position: relative;
}

.references__list li::before {
  content: attr(data-ref);
  position: absolute;
  left: 0;
  font-weight: 600;
  color: var(--color-accent);
  font-size: 13px;
}

.references__list a {
  color: var(--color-accent);
  word-break: break-all;
}

/* ============================================
   Article Page — Navigation (Prev / Next)
   ============================================ */

.article-pager {
  display: flex;
  justify-content: space-between;
  gap: 24px;
  max-width: var(--max-width-article);
  margin: 0 auto;
  padding: 0 24px var(--section-padding);
}

.article-pager__link {
  flex: 1;
  display: flex;
  flex-direction: column;
  padding: 24px;
  background-color: var(--color-card-bg);
  border-radius: var(--radius-card);
  box-shadow: var(--shadow-card);
  transition: transform 0.3s ease, box-shadow 0.3s ease;
  text-decoration: none;
  color: inherit;
}

.article-pager__link:hover {
  transform: translateY(-2px);
  box-shadow: 0 4px 20px rgba(0, 0, 0, 0.1);
  color: inherit;
}

.article-pager__link--next {
  text-align: right;
  align-items: flex-end;
}

.article-pager__label {
  font-size: 13px;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.06em;
  color: var(--color-text-secondary);
  margin-bottom: 4px;
}

.article-pager__title {
  font-size: 18px;
  font-weight: 600;
  color: var(--color-text);
}

/* ============================================
   Mobile Navigation Toggle
   ============================================ */

.nav-toggle {
  display: none;
  background: none;
  border: none;
  cursor: pointer;
  padding: 8px;
  color: var(--color-text);
}

.nav-toggle__bar {
  display: block;
  width: 20px;
  height: 2px;
  background-color: var(--color-text);
  margin: 4px 0;
  border-radius: 1px;
  transition: transform 0.3s ease, opacity 0.3s ease;
}

.nav-toggle.active .nav-toggle__bar:nth-child(1) {
  transform: rotate(45deg) translate(4px, 4px);
}

.nav-toggle.active .nav-toggle__bar:nth-child(2) {
  opacity: 0;
}

.nav-toggle.active .nav-toggle__bar:nth-child(3) {
  transform: rotate(-45deg) translate(4px, -4px);
}

/* ============================================
   Responsive — Tablet (< 1024px)
   ============================================ */

@media (max-width: 1024px) {
  .article-layout__sidebar {
    display: none;
  }

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

/* ============================================
   Responsive — Mobile (< 768px)
   ============================================ */

@media (max-width: 768px) {
  :root {
    --section-padding: 48px;
  }

  body {
    font-size: 16px;
  }

  h1 {
    font-size: 36px;
  }

  h2 {
    font-size: 28px;
  }

  h3 {
    font-size: 22px;
  }

  h4 {
    font-size: 18px;
  }

  .hero {
    padding: 80px 20px 48px;
  }

  .hero__title {
    font-size: 40px;
  }

  .hero__subtitle {
    font-size: 18px;
  }

  .chapters-grid {
    grid-template-columns: 1fr;
    gap: 16px;
    padding: 0 20px;
  }

  .chapter-card {
    padding: 24px;
  }

  .article-hero {
    padding: 60px 20px 48px;
  }

  .article-hero__title {
    font-size: 36px;
  }

  .article-nav__title {
    display: none;
  }

  .article-pager {
    flex-direction: column;
    padding: 0 20px 48px;
  }

  .article-pager__link--next {
    text-align: left;
    align-items: flex-start;
  }

  .container {
    padding: 0 20px;
  }

  pre {
    padding: 16px;
    border-radius: 8px;
  }

  pre code {
    font-size: 13px;
  }

  blockquote {
    padding: 12px 16px;
  }

  table {
    font-size: 14px;
  }

  th, td {
    padding: 8px 12px;
  }

  .nav-toggle {
    display: block;
  }
}

/* ============================================
   Utility Classes
   ============================================ */

.text-center { text-align: center; }
.text-secondary { color: var(--color-text-secondary); }
.mt-0 { margin-top: 0; }
.mb-0 { margin-bottom: 0; }
.mb-1 { margin-bottom: 0.5em; }
.mb-2 { margin-bottom: 1em; }
.mb-3 { margin-bottom: 1.5em; }
.visually-hidden {
  position: absolute;
  width: 1px;
  height: 1px;
  padding: 0;
  margin: -1px;
  overflow: hidden;
  clip: rect(0, 0, 0, 0);
  white-space: nowrap;
  border: 0;
}

/* ============================================
   Disable Copy / Selection
   ============================================ */

body {
  -webkit-user-select: none;
  -moz-user-select: none;
  -ms-user-select: none;
  user-select: none;
}

/* Allow selection inside code blocks and input fields */
pre, code, input, textarea {
  -webkit-user-select: text;
  -moz-user-select: text;
  -ms-user-select: text;
  user-select: text;
}

/* ============================================
   Collapsible References (details/summary)
   ============================================ */

.references details {
  border-top: 1px solid var(--color-border);
  padding-top: var(--section-padding);
  margin-top: var(--section-padding);
}

.references details summary {
  cursor: pointer;
  list-style: none;
  display: flex;
  align-items: center;
  gap: 8px;
  font-size: 20px;
  font-weight: 600;
  color: var(--color-text);
  padding: 12px 0;
  transition: color 0.2s ease;
}

.references details summary::-webkit-details-marker {
  display: none;
}

.references details summary::before {
  content: '▸';
  font-size: 14px;
  color: var(--color-accent);
  transition: transform 0.3s ease;
  display: inline-block;
}

.references details[open] summary::before {
  transform: rotate(90deg);
}

.references details summary:hover {
  color: var(--color-accent);
}

.references details .references__list {
  margin-top: 16px;
}

.references details summary h2,
.references details summary h3 {
  margin: 0;
  font-size: inherit;
  font-weight: inherit;
  letter-spacing: inherit;
  line-height: inherit;
}

/* ============================================
   Math Rendering (KaTeX)
   ============================================ */

.math-block {
  overflow-x: auto;
  margin: 1.5em 0;
  padding: 16px 0;
  text-align: center;
}

.math-block .katex-display {
  margin: 0;
}

span.math .katex {
  font-size: 1.05em;
}
