/* --- Global Elements & Base Rules --- */
html {
  scroll-behavior: smooth;
}

.content {
  margin: 3rem auto;
  padding: 0;
}

.content p {
  line-height: 1.7;
}

/* TOC links active state */
.sidebar a.active,
#TableOfContents a.active {
  color: var(--accent);
  text-decoration: underline;
}

/* --- Layout Architecture --- */
.main-layout {
  display: flex;
  gap: 2.5rem;
}

.article-content {
  flex: 1;
  min-width: 0; /* Prevents code blocks or wide text from stretching/breaking the layout */
}

/* --- Sidebar & Table of Contents --- */
.sidebar {
  width: 260px; /* Explicit width prevents layout jumping */
  flex-shrink: 0;
  position: sticky;
  top: 2rem;
  max-height: calc(100vh - 4rem);
  overflow-y: auto;
  font-size: 0.9em;
}

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

.sidebar h3 {
  padding: 4px 8px;
  margin: 0;
}

#TableOfContents {
  padding: 10px;
}

#TableOfContents ul {
  list-style: none;
  padding-left: 0;
  margin: 0;
}

#TableOfContents a {
  color: var(--text-main);
  font-weight: normal;
}

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

#TableOfContents li {
  counter-increment: toc-level1;
  position: relative;
  padding-bottom: 3px;
}

#TableOfContents li::before {
  content: counters(toc-level1, ".") ". ";
  font-weight: 600;
}

#TableOfContents li ul {
  counter-reset: toc-level1;
  margin-left: 1.2em;
}

/* --- Cards --- */
.card-container {
  display: flex;
  flex-direction: column;
  gap: 1.5rem;
}

.card {
  display: grid;
  overflow: hidden;
  min-width: 200px;
  border-radius: var(--BrRadius);
  padding: 1.5rem;
  background: var(--surface);
  border: 1px solid var(--border);
  transition: box-shadow 0.2s;
}

.card:hover {
  box-shadow: 0 2px 12px rgba(0,0,0,0.08);
}

.card h2 {
  margin: 0;
}

.card h2 a {
  color: var(--text-main);
  display: block;
  margin-bottom: 0.5rem;
  font-weight: 600;
}

.card h2 a:hover {
  color: var(--accent);
  text-decoration: none;
}

.meta {
  text-transform: uppercase;
  font-size: 0.75em;
  letter-spacing: 0.5px;
  margin-bottom: 0.5rem;
  display: inline-flex;
  gap: 1rem;
  color: var(--text-dim);
}

/* --- Tags --- */
.tag-list {
  display: flex;
  flex-wrap: wrap;
  gap: 0.5rem;
  margin-top: 0.75rem;
  cursor: default;
}

.tag-list a {
  color: var(--text-dim);
  background: var(--surface);
  border: 1px solid var(--border);
  padding: 0.15rem 0.6rem;
  font-size: 0.8em;
  font-weight: 500;
  border-radius: var(--BrRadius);
  transition: color 0.2s, border-color 0.2s;
}

.tag-list a:hover {
  color: var(--accent);
  border-color: var(--accent);
  text-decoration: none;
}

/* --- Code Blocks --- */
.code-wrapper {
  position: relative;
  overflow: hidden;
  margin: 1.5em 0;
  border-radius: var(--BrRadius);
}

.code-wrapper pre {
  margin: 0;
  padding: 1em;
  overflow-x: auto;
  color: var(--text-main);
}

.copy-btn {
  position: absolute;
  top: 0.4em;
  right: 0.4em;
  padding: 0.3em 0.6em;
  border: none;
  cursor: pointer;
  color: var(--text-dim);
  background: var(--surface);
  border-radius: var(--BrRadius);
}

.copy-btn:hover {
  color: var(--text-main);
}

/* --- Pagination --- */
.page-nav {
  display: flex;
  justify-content: center;
  align-items: center;
  gap: 0.6em;
  margin-top: 2em;
  flex-wrap: wrap;
}

.page-nav a,
.page-nav span {
  padding: 0.2em 0.8em;
}

.page-nav a:hover,
.page-nav .active {
  background: var(--accent);
  text-decoration: underline;
  opacity: 0.5;
}

.page-nav .disabled {
  opacity: 0.5;
  cursor: default;
}

.page-count {
  text-align: center;
  margin: 1em 0;
  opacity: 0.8;
  font-size: 0.9em;
}

/* --- Related Posts --- */
.related {
  margin-top: 2em;
}

/* --- UI Controls (Global) --- */
.back-to-top {
  position: fixed;
  bottom: 2em;
  right: 2em;
  padding: 0.3em 0.6em;
  font-size: 1.2em;
  background: var(--surface);
  color: var(--text-dim);
  border: 1px solid var(--border);
  border-radius: var(--BrRadius);
  cursor: pointer;
  opacity: 0;
  visibility: hidden;
  transition: opacity 0.3s, visibility 0.3s, color 0.2s;
}

.back-to-top.visible {
  opacity: 1;
  visibility: visible;
}

.back-to-top:hover {
  color: var(--accent);
}


/* --- Analytics / Stats Bar --- */
.analytics-section {
  margin: 0 auto 4em;
}

.analytics-bar {
  margin-bottom: 2em;
}

.analytics-grid {
  display: flex;
  flex-wrap: wrap;
  gap: 1em;
}

.stat-card {
  flex: 1;
  min-width: 120px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--BrRadius);
  padding: 1.2em 1.5em;
  text-align: center;
  display: flex;
  flex-direction: column;
  gap: 0.3em;
}

.stat-card-sm {
  min-width: 100px;
  padding: 0.7em 1em;
}

.stat-label {
  font-size: 0.75em;
  text-transform: uppercase;
  letter-spacing: 0.5px;
  color: var(--text-dim);
  font-weight: 500;
}

.stat-value {
  font-size: 1.8em;
  font-weight: 800;
  color: var(--accent);
  line-height: 1;
}

.stat-card-sm .stat-value {
  font-size: 1.4em;
}

/* --- Responsive Media Queries --- */

/* Tablet & Smaller Desktops */
@media (max-width: 992px) {
  .main-layout {
    flex-direction: column-reverse; /* Drops TOC to top or bottom predictably depending on HTML placement */
    gap: 1rem;
  }

  .sidebar {
    position: static; /* Removes sticky behaviors for mobile flow */
    width: 100%;
    max-width: 100%;
    max-height: none;
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: var(--BrRadius);
    padding: 1rem;
    margin-top: 1rem;
    box-sizing: border-box;
  }
}

/* Mobile Screens */
@media (max-width: 768px) {
  .back-to-top {
    bottom: 1em;
    right: 1em;
  }
}

/* Ultra Small Screens */
@media (max-width: 400px) {
  .content {
    padding: 0;
  }
}