
/* =====================================================
   THEME VARIABLES
===================================================== */
:root {
  --bg: #f6f7f8;
  --card: #ffffff;
  --text: #1c1c1c;
  --muted: #6b7280;
  --border: #e5e7eb;
  --accent: #16a34a;

  --code-bg: #0f172a;
  --code-text: #e5e7eb;

  --radius-lg: 16px;
  --radius-md: 12px;
  --radius-sm: 8px;
}

[data-theme="dark"] {
  --bg: #0b0f14;
  --card: #111827;
  --text: #e5e7eb;
  --muted: #9ca3af;
  --border: #1f2937;
  --accent: #22c55e;

  --code-bg: #020617;
  --code-text: #e5e7eb;
}

/* =====================================================
   RESET
===================================================== */
*,
*::before,
*::after {
  box-sizing: border-box;
}

html {
  scroll-behavior: smooth;
}

body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font-family: system-ui, -apple-system, Segoe UI, Roboto, sans-serif;
  line-height: 1.7;
}

/* =====================================================
   HEADER
===================================================== */
.site-header {
  position: sticky;
  top: 0;
  z-index: 100;
  background: var(--card);
  border-bottom: 1px solid var(--border);
}

.header-inner {
  max-width: 1200px;
  margin: 0 auto;
  padding: 16px 24px;

  display: grid;
  grid-template-columns: 1fr auto 1fr;
  align-items: center;
}

/* Logo */
.logo {
  position: relative;
  height: 44px;
  display: flex;
  justify-content: center;
}

.logo img {
  position: absolute;
  height: 44px;
  transition: opacity 0.25s ease;
}

.logo-dark {
  opacity: 0;
  pointer-events: none;
}

[data-theme="dark"] .logo-light {
  opacity: 0;
  pointer-events: none;
}

[data-theme="dark"] .logo-dark {
  opacity: 1;
}

/* Nav */
.nav {
  display: flex;
  gap: 20px;
}

.nav a {
  text-decoration: none;
  color: var(--text);
  font-size: 0.95rem;
}

/* Theme toggle */
.theme-toggle {
  justify-self: end;
  background: none;
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  padding: 6px 10px;
  cursor: pointer;
  font-size: 1rem;
  color: var(--text);
}

/* Progress bar */
.progress-track {
  height: 3px;
  background: var(--border);
}

.progress-bar {
  height: 100%;
  width: 0%;
  background: var(--accent);
  transition: width 0.12s linear;
}

/* =====================================================
   PAGE LAYOUT
===================================================== */
.page {
  max-width: 1200px;
  margin: 40px auto;
  padding: 0 24px;
align-items: start;
  display: grid;
  grid-template-columns: minmax(0, 1fr) minmax(0, 3fr);
  gap: 40px;
}

/* Explicit placement (extra safe) */
.sidebar {
  grid-column: 1;
}

.article {
  grid-column: 2;
}


/* =====================================================
   ARTICLE CARD
===================================================== */
.article {
  background: var(--card);
  border-radius: var(--radius-lg);
  padding: 48px;
}

/* Title */
.article-title {
  font-size: 2.4rem;
  line-height: 1.15;
  margin-bottom: 16px;
}

/* Meta */
.article-meta {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  font-size: 0.9rem;
  color: var(--muted);
  margin-bottom: 32px;
}

/* Hero */
.article-hero img {
  width: 100%;
  border-radius: var(--radius-md);
}

.article-hero figcaption {
  margin-top: 8px;
  font-size: 0.85rem;
  color: var(--muted);
  text-align: center;
}

/* Sections */
.article section {
  position: relative;
  padding-top: 12px;
  margin-top: 56px;
}

/* Section reading progress guide */
.article section::after {
  content: "";
  position: absolute;
  left: -24px;
  top: 0;
  width: 2px;
  height: 100%;
  background: linear-gradient(
    to bottom,
    var(--accent),
    transparent
  );
  opacity: 0.06;
}


.article h2 {
  font-size: 1.6rem;
  margin-bottom: 16px;
  scroll-margin-top: 128px;
  position: relative;
}

.article p,
.article li {
  font-size: 1rem;
  color: var(--muted);
}

.article p:empty,
.article section > *:empty {
  display: none;
}


/* =====================================================
   IMAGES
===================================================== */
.article-image {
  margin: 36px 0;
}

.article-image img {
  width: 100%;
  border-radius: var(--radius-md);
}

.article-image figcaption {
  margin-top: 6px;
  font-size: 0.85rem;
  color: var(--muted);
  text-align: center;
}

/* =====================================================
   CODE BLOCK
===================================================== */
.code-block {
  position: relative;
  background: var(--code-bg);
  color: var(--code-text);
  padding: 24px;
  border-radius: var(--radius-md);
  margin: 36px 0;
  overflow-x: auto;
}

.code-block code {
  font-family: "JetBrains Mono", ui-monospace, monospace;
  font-size: 0.9rem;
  white-space: pre;
}

/* Copy button */
.copy-btn {
  position: absolute;
  top: 12px;
  right: 12px;
  background: var(--card);
  color: var(--text);
  border: 1px solid var(--border);
  padding: 6px 10px;
  border-radius: var(--radius-sm);
  font-size: 0.75rem;
  cursor: pointer;
}

/* =====================================================
   SIDEBAR (RIGHT COLUMN)
===================================================== */
.sidebar {
  position: sticky;
  top: 120px;
  
  display: flex;
  flex-direction: column;
  gap: 24px;
}

/* TOC */
.toc {
  background: var(--card);
  padding: 20px;
  border-radius: var(--radius-md);
}

.toc h4 {
  margin-bottom: 12px;
  font-size: 0.9rem;
  font-weight: 600;
}

/* Links */
.toc nav {
  display: flex;
  flex-direction: column;
  gap: 6px;
}

.toc a {
  position: relative;
  padding: 8px 10px 8px 22px;
  border-radius: var(--radius-sm);
  font-size: 0.9rem;
  color: var(--muted);
  text-decoration: none;
  transition:
    background 0.25s ease,
    color 0.25s ease,
    transform 0.2s ease,
    opacity 0.2s ease;
  opacity: 0.85;
}


/* TOC progress indicator (JS-driven) */
.toc a::before {
  content: "";
  position: absolute;
  left: 8px;
  top: 0;
  width: 3px;
  height: var(--progress, 0%);
  background: var(--accent);
  border-radius: 2px;
  transition: height 0.18s linear;
}


.toc a.active {
  background: rgba(22, 163, 74, 0.1);
  color: var(--text);
  font-weight: 600;
  transform: translateX(4px);
  opacity: 1;
}



/* Collapsible TOC (JS-ready) */
.toc.collapsed nav {
  max-height: 0;
  overflow: hidden;
}
.toc nav {
  transition: max-height 0.25s ease;
}


.toc-toggle {
  cursor: pointer;
  font-size: 0.85rem;
  color: var(--muted);
  display: flex;
  justify-content: space-between;
  align-items: center;
}


[data-theme="dark"] .toc a.active {
  background: rgba(34, 197, 94, 0.18);
}

/* CTA */
.cta {
  background: var(--card);
  padding: 20px;
  border-radius: var(--radius-md);
}

.cta h4 {
  margin-bottom: 6px;
}

.cta p {
  font-size: 0.9rem;
  color: var(--muted);
}

.cta button {
  margin-top: 12px;
  width: 100%;
  padding: 12px;
  border-radius: var(--radius-sm);
  border: none;
  background: var(--accent);
  color: #fff;
  font-weight: 600;
  cursor: pointer;
}

.cta-link {
  display: inline-block;
  margin-top: 12px;
  font-weight: 600;
  color: var(--accent);
  text-decoration: none;
}

.cta-link:hover {
  text-decoration: underline;
}


/* =====================================================
   AUTHOR BOX
===================================================== */
.author-box {
  display: flex;
  align-items: center;
  gap: 14px;
  margin-top: 64px;
  padding-top: 24px;
  border-top: 1px solid var(--border);
}

.author-box img {
  width: 56px;
  height: 56px;
  border-radius: 50%;
  object-fit: cover;
}

.author-box p {
  font-size: 0.85rem;
  color: var(--muted);
}

/* =====================================================
   RELATED POSTS
===================================================== */
.related-posts {
  max-width: 1200px;
  margin: 80px auto;
  padding: 0 24px;
}

.related-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(240px, 1fr));
  gap: 24px;
}

.related-card {
  background: var(--card);
  padding: 20px;
  border-radius: var(--radius-md);
  text-decoration: none;
  color: inherit;
  transition: transform 0.2s ease, box-shadow 0.2s ease;
}

.related-card:hover {
  transform: translateY(-4px);
  box-shadow: 0 12px 28px rgba(0,0,0,0.08);
}

.related-card p {
  font-size: 0.9rem;
  color: var(--muted);
}

/* =====================================================
   RESPONSIVE
===================================================== */
@media (max-width: 900px) {
  .page {
    grid-template-columns: 1fr;
  }

  .sidebar {
    position: static;
    order: -1;
    margin-bottom: 24px;
  }

  .toc nav {
    max-height: 180px;
    overflow-y: auto;
  }
}

@media (max-width: 768px) {
  .header-inner {
    grid-template-columns: 1fr;
    row-gap: 12px;
  }

  .theme-toggle {
    position: absolute;
    top: 14px;
    right: 16px;
  }

  .article {
    padding: 28px;
  }
}

/* =====================================================
   INLINE TEXT TOKENS (EDITOR OUTPUT)
===================================================== */
.bold { font-weight: 600; }
.italic { font-style: italic; }
.text-red { color: #dc2626; }

.highlight {
  background: linear-gradient(180deg, transparent 60%, #fde68a 0);
}

.mono {
  font-family: "JetBrains Mono", monospace;
  background: #f1f5f9;
  padding: 2px 4px;
  border-radius: 4px;
}

/* =====================================================
   RELATED POSTS — MATCHED TO BLOG GLASS SYSTEM
===================================================== */

.related-posts {
  max-width: 1200px;
  margin: 96px auto 120px;
  padding: 0 24px;
}

.related-posts h3 {
  font-size: 1.35rem;
  font-weight: 600;
  letter-spacing: -0.01em;
  margin-bottom: 28px;
  color: var(--text);
}

/* Grid */
.related-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(260px, 1fr));
  gap: 28px;
}

/* Card */
.related-card {
  display: block;
  text-decoration: none;
  color: inherit;

  padding: 22px 22px 26px;
  border-radius: var(--radius-md);

  /* 🔑 MATCHED COLOR SYSTEM */
  background: var(--card);
  border: 1px solid var(--border);

  box-shadow:
    0 12px 30px rgba(0,0,0,0.06);

  transition:
    transform 0.25s ease,
    box-shadow 0.25s ease,
    border-color 0.25s ease;
}

.related-card:hover {
  transform: translateY(-6px);
  border-color: rgba(22,163,74,0.35); /* accent hint */
  box-shadow:
    0 22px 50px rgba(0,0,0,0.12);
}

/* Title */
.related-card h4 {
  font-size: 1.05rem;
  line-height: 1.35;
  margin-bottom: 8px;
  color: var(--text);
}

/* Description */
.related-card p {
  font-size: 0.9rem;
  color: var(--muted);
  line-height: 1.55;
}

/* =====================================================
   MOBILE — SINGLE COLUMN READING FLOW
   Sidebar → Article → CTA → Related
===================================================== */

@media (max-width: 768px) {

  /* 1️⃣ Force page into vertical flow */
  .page {
    display: flex;
    flex-direction: column;
    gap: 24px;
    margin: 20px auto;
    padding: 0 16px;
  }

  /* 2️⃣ Sidebar becomes TOP section */
  .sidebar {
    position: static;
    order: 1;
    gap: 16px;
  }

  /* TOC should feel like a top card */
  .toc {
    padding: 16px;
    border-radius: 12px;
  }

  .toc h4 {
    font-size: 0.9rem;
  }

  .toc nav {
    max-height: 220px;
    overflow-y: auto;
  }

  .toc a {
    font-size: 0.88rem;
    padding: 6px 8px 6px 18px;
  }

  /* 3️⃣ Article comes AFTER sidebar */
  .article {
    order: 2;
    padding: 24px 18px;
    border-radius: 14px;
  }

  .article-title {
    font-size: 1.75rem;
    line-height: 1.25;
  }

  .article p,
  .article li {
    font-size: 0.95rem;
    line-height: 1.65;
  }

  .article h2 {
    font-size: 1.35rem;
    margin-top: 36px;
    margin-bottom: 12px;
  }

  /* Remove desktop vertical guide on mobile */
  .article section::after {
    display: none;
  }

  /* Images scale naturally */
  .article-image {
    margin: 22px 0;
  }

  .article-image img {
    border-radius: 12px;
  }

  /* Code blocks mobile-friendly */
  .code-block {
    padding: 16px;
    font-size: 0.85rem;
    margin: 24px 0;
  }

  .code-block code {
    font-size: 0.82rem;
  }

  /* 4️⃣ CTA must come AFTER article */
  .cta {
    order: 3;
    padding: 18px;
    border-radius: 14px;
  }

  .cta h4 {
    font-size: 1rem;
  }

  .cta p {
    font-size: 0.88rem;
  }

  /* 5️⃣ Related posts at the very bottom */
  .related-posts {
    order: 4;
    margin: 48px auto 72px;
    padding: 0 16px;
  }

  .related-posts h3 {
    font-size: 1.2rem;
    margin-bottom: 20px;
  }

  .related-grid {
    grid-template-columns: 1fr;
    gap: 18px;
  }

  .related-card {
    padding: 18px;
    border-radius: 14px;
  }

  .related-card h4 {
    font-size: 1rem;
  }

  .related-card p {
    font-size: 0.88rem;
  }
}

/* =====================================================
   MOBILE IMAGE CONSTRAINT — FIX HEADER + FLOW
===================================================== */
@media (max-width: 768px) {

  /* Contain all article images */
  .article img,
  .article-hero img,
  .article-image img {
    width: 100%;
    height: auto;
    max-height: 55vh;              /* 🔑 KEY FIX */
    object-fit: cover;             /* Prevent stretching */
    display: block;
  }

  /* Ensure figures never overflow article card */
  figure,
  .article-image,
  .article-hero {
    max-width: 100%;
    overflow: hidden;
    margin-left: 0;
    margin-right: 0;
  }

  /* Hero images should feel softer on mobile */
  .article-hero img {
    max-height: 42vh;               /* Slightly smaller than content images */
    border-radius: 12px;
  }

  /* Reduce visual dominance of captions */
  .article-image figcaption,
  .article-hero figcaption {
    font-size: 0.78rem;
    line-height: 1.4;
    margin-top: 4px;
    opacity: 0.85;
  }
}
