﻿:root {
  --bg-color: #ffffff;
  --text-color: #333333;
  --link-color: #2563eb;
  --toc-bg: #ffffff; 
  --border-color: #e5e7eb;
  --meta-color: #6b7280;
  --sidebar-width: 280px;
  --toc-hover-bg: #f3f4f6;
  --toc-active-bg: #eff6ff;
}

body.dark-mode {
  --bg-color: #121212;
  --text-color: #e0e0e0;
  --link-color: #60a5fa;
  --toc-bg: #121212;
  --border-color: #2d3748;
  --meta-color: #9ca3af;
  --toc-hover-bg: #1f2937;
  --toc-active-bg: rgba(96, 165, 250, 0.1);
}

html { scroll-behavior: smooth; }

body {
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  line-height: 1.7;
  background-color: var(--bg-color);
  color: var(--text-color);
  max-width: 1200px;
  margin: 0 auto;
  padding: clamp(12px, 2vw, 24px);
  transition: background-color 0.3s, color 0.3s;
}

header {
  margin-bottom: 32px;
  border-bottom: 1px solid var(--border-color);
  padding-bottom: 24px;
}

nav {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 12px;
  flex-wrap: wrap;
  margin-bottom: 24px;
}

h1 {
  font-size: clamp(1.7rem, 4vw, 2.5rem);
  margin-bottom: 10px;
  font-weight: 700;
  line-height: 1.2;
}

.post-meta {
  color: var(--meta-color);
  font-size: 0.95em;
  margin-bottom: 20px;
}

.abstract-container {
  border-left: 3px solid var(--border-color);
  color: var(--meta-color);
  padding-left: 16px;
  margin-top: 20px;
}

.theme-toggle {
  cursor: pointer;
  padding: 6px 14px;
  border: 1px solid var(--border-color);
  border-radius: 6px;
  background: transparent;
  color: var(--text-color);
  font-size: 0.85em;
  font-weight: 500;
  transition: all 0.2s;
}
.theme-toggle:hover { 
  border-color: var(--link-color); 
  color: var(--link-color);
}

/* =========================================
    LAYOUT & TOC ANIMATION
========================================= */
.main-container {
  display: grid;
  grid-template-columns: var(--sidebar-width) minmax(0, 1fr);
  gap: clamp(20px, 3vw, 44px);
  align-items: start;
  transition: grid-template-columns 0.4s cubic-bezier(0.2, 0, 0, 1), gap 0.4s cubic-bezier(0.2, 0, 0, 1);
}

body.toc-collapsed .main-container {
  grid-template-columns: 32px minmax(0, 1fr);
  gap: 0;
}

.toc-container {
  position: sticky;
  top: 24px;
  height: max-content;
  max-height: calc(100vh - 48px);
  z-index: 10;
}

.toc-container > *:not(.toc-handle) {
  width: var(--sidebar-width);
  transition: opacity 0.3s ease, visibility 0.3s, transform 0.4s cubic-bezier(0.2, 0, 0, 1);
  transform-origin: left top;
}

body.toc-collapsed .toc-container > *:not(.toc-handle) {
  opacity: 0;
  visibility: hidden;
  transform: translateX(-15px) scale(0.98);
  pointer-events: none;
}

/* =========================================
    PROFESSIONAL TOC STYLING
========================================= */
.toc {
  background: transparent;
  padding: 0 10px 0 0;
}

.toc h4 {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin: 0 0 16px 0;
  padding-bottom: 12px;
  font-size: 0.85em;
  letter-spacing: 1.2px;
  color: var(--text-color);
  text-transform: uppercase;
  font-weight: 700;
  user-select: none;
}

.toc ul { 
  list-style: none; 
  padding-left: 0; 
  margin: 0; 
  border-left: 1px solid var(--border-color);
}

.toc li { margin: 0; position: relative; }

.toc-item-wrapper {
  display: flex;
  align-items: center;
  position: relative;
}

.toc a {
  flex: 1;
  display: block;
  padding: 6px 12px 6px 16px;
  text-decoration: none;
  color: var(--meta-color);
  font-size: 0.9em;
  transition: all 0.2s ease;
  overflow-wrap: anywhere;
  line-height: 1.4;
  border-left: 2px solid transparent;
  margin-left: -1px; 
}

.toc a:hover { 
  color: var(--text-color); 
  background: var(--toc-hover-bg);
  border-left-color: var(--border-color);
}

.toc a.active {
  color: var(--link-color);
  font-weight: 600;
  background: var(--toc-active-bg);
  border-left-color: var(--link-color);
}

.toc-toggle {
  position: absolute;
  left: -10px; 
  background: var(--bg-color);
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  width: 20px;
  height: 20px;
  transition: transform 0.2s;
  color: var(--meta-color);
  user-select: none;
  z-index: 2;
  border-radius: 4px;
}
.toc-toggle:hover { color: var(--text-color); }
.toc-toggle svg { width: 14px; height: 14px; }

.toc li.has-sub.open > .toc-item-wrapper .toc-toggle { transform: rotate(90deg); }

.toc ul ul {
  display: none;
  border-left: none; 
  padding-left: 12px;
  margin-top: 2px;
  margin-bottom: 2px;
}
.toc li.has-sub.open > ul { 
  display: block; 
  animation: slideDown 0.3s ease forwards;
}

@keyframes slideDown {
  from { opacity: 0; transform: translateY(-4px); }
  to { opacity: 1; transform: translateY(0); }
}

/* =========================================
    TOC TAB (HIỂN THỊ KHI THU GỌN)
========================================= */
.toc-handle {
  position: absolute;
  top: 0;
  left: 0;
  width: 32px;
  height: 90px;
  writing-mode: vertical-rl;
  text-orientation: mixed;
  display: flex;
  align-items: center;
  justify-content: center;
  background: var(--bg-color);
  color: var(--text-color);
  border: 1px solid var(--border-color);
  border-left: none;
  border-radius: 0 8px 8px 0;
  font-size: 0.8em;
  font-weight: 600;
  letter-spacing: 2px;
  cursor: pointer;
  box-shadow: 3px 2px 10px rgba(0, 0, 0, 0.05);
  opacity: 0;
  visibility: hidden;
  transform: translateX(-100%);
  transition: all 0.4s cubic-bezier(0.2, 0, 0, 1);
  z-index: 20;
  padding-top: 8px;
}

body.dark-mode .toc-handle { box-shadow: 3px 2px 10px rgba(0, 0, 0, 0.3); }
.toc-handle:hover { color: var(--link-color); }
body.toc-collapsed .toc-handle {
  opacity: 1;
  visibility: visible;
  transform: translateX(0);
}

/* =========================================
    ACADEMIC ENVIRONMENTS (Def, Theorem, etc.)
========================================= */
.math-env {
  margin: 1.8em 0;
  padding: 16px 20px;
  border-radius: 4px 8px 8px 4px;
  font-size: 0.98em;
  box-shadow: 0 1px 3px rgba(0,0,0,0.02);
}
.math-env .env-title {
  font-weight: 700;
  margin-bottom: 8px;
  display: block;
  font-size: 1.05em;
}
.math-env p:last-child { margin-bottom: 0; }
.math-env p:first-of-type { margin-top: 0; }

/* Theorem, Proposition, Lemma (Blue) */
.env-theorem, .env-proposition, .env-lemma {
  border-left: 4px solid #3b82f6;
  background-color: rgba(59, 130, 246, 0.05);
}
.env-theorem .env-title, .env-proposition .env-title, .env-lemma .env-title { color: #2563eb; }
body.dark-mode .env-theorem .env-title, body.dark-mode .env-proposition .env-title, body.dark-mode .env-lemma .env-title { color: #60a5fa; }

/* Definition (Green) */
.env-definition {
  border-left: 4px solid #10b981;
  background-color: rgba(16, 185, 129, 0.05);
}
.env-definition .env-title { color: #059669; }
body.dark-mode .env-definition .env-title { color: #34d399; }

/* Example (Yellow/Orange) */
.env-example {
  border-left: 4px solid #f59e0b;
  background-color: rgba(245, 158, 11, 0.05);
}
.env-example .env-title { color: #d97706; }
body.dark-mode .env-example .env-title { color: #fbbf24; }

/* Proof (Gray/Transparent) */
.env-proof {
  border-left: 2px solid var(--border-color);
  padding: 12px 16px;
  margin: 1.5em 0;
  background-color: transparent;
}
.env-proof .env-title {
  font-style: italic;
  font-weight: 600;
  color: var(--text-color);
}
.env-proof::after {
  content: '■';
  display: block;
  text-align: right;
  clear: both;
  color: var(--meta-color);
  margin-top: -1em; /* Căn chỉnh hình vuông nằm chung dòng cuối */
}

/* =========================================
    BLOG CONTENT
========================================= */
.blog-content {
  max-width: 100%;
  min-width: 0;
  font-size: clamp(0.98rem, 2.2vw, 1.05rem);
}

h2, h3, h4 { scroll-margin-top: 80px; }

h2 {
  border-bottom: 1px solid var(--border-color);
  padding-bottom: 12px;
  margin-top: 50px;
  font-weight: 600;
}

h3 { margin-top: 35px; color: var(--text-color); }
p { margin: 1.5em 0; text-align: justify; }
a { color: var(--link-color); text-decoration: none; }
a:hover { text-decoration: underline; }

img, video, iframe, svg, canvas, table, pre, code { max-width: 100%; }
figure { max-width: 100%; margin-inline: auto; }
pre, .bibtex-container { overflow-x: auto; }

.mjx-chtml {
  margin: 2em 0 !important;
  overflow-x: auto;
  overflow-y: hidden;
}

footer {
  margin-top: 64px;
  padding: 28px 0;
  border-top: 1px solid var(--border-color);
  color: var(--meta-color);
  font-size: 0.85em;
  text-align: center;
}

/* =========================================
    MOBILE RESPONSIVENESS
========================================= */
@media (max-width: 1024px) {
  .main-container, body.toc-collapsed .main-container {
    grid-template-columns: 1fr;
    gap: 24px;
  }
  .toc-container, 
  body.toc-collapsed .toc-container > *:not(.toc-handle) {
    width: 100%;
    position: static;
    max-height: none;
    transform: none;
    opacity: 1;
    visibility: visible;
    pointer-events: auto;
  }
  .toc-handle { display: none !important; }
  body.toc-collapsed .toc-container > *:not(.toc-handle) { display: none; }
  .toc ul { border-left: none; }
  .toc a { margin-left: 0; border-left: none; }
}

@media (max-width: 640px) {
  body { padding: 12px; }
  h2 { margin-top: 34px; }
  .abstract-container { padding-left: 12px; }
}