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

:root {
  --bg: #303030;
  --bg-nav: #282828;
  --bg-card: #393939;
  --bg-hover: #3a3a3a;
  --text: #ccc;
  --text-dim: #888;
  --accent: #6ac;
  --accent-hover: #8cd;
  --link: #6ac;
  --link-hover: #8cd;
  --border: #444;
  --tag-bg: #3a3a3a;
  --code-bg: #262626;
  --font-sans: Consolas, 'Courier New', monospace;
  --font-mono: Consolas, 'Courier New', monospace;
}

/* === Light Theme === */
[data-theme="light"] {
  --bg: #e8e4df;
  --bg-nav: #ddd8d2;
  --bg-card: #d5d0c9;
  --bg-hover: #cec8c0;
  --text: #222;
  --text-dim: #555;
  --accent: #8a6420;
  --accent-hover: #a07828;
  --link: #8a6420;
  --link-hover: #a07828;
  --border: #b5afa6;
  --tag-bg: #ccc7bf;
  --code-bg: #d0cbc4;
}

[data-theme="light"] .article-content h2,
[data-theme="light"] .article-header h1,
[data-theme="light"] .home-header h1 {
  color: #111;
}

[data-theme="light"] .article-content h3 {
  color: #222;
}

[data-theme="light"] .article-content h2::before,
[data-theme="light"] .article-content h3::before {
  color: #888;
}

html { font-size: 14px; scroll-behavior: smooth; }
body {
  font-family: var(--font-sans);
  background: var(--bg-nav);
  color: var(--text);
  line-height: 1.6;
  min-height: 100vh;
}

.main {
  background: var(--bg);
}

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

/* === Layout === */
.layout {
  display: grid;
  grid-template-columns: 260px minmax(0, 800px) 220px;
  max-width: 1400px;
  margin: 0 auto;
  min-height: 100vh;
}

/* Home page has no TOC sidebar */
.layout:not(:has(.toc-sidebar)) {
  grid-template-columns: 260px 1fr;
  max-width: 1100px;
}

/* === Sidebar === */
.sidebar {
  background: var(--bg-nav);
  border-right: 1px solid var(--border);
  padding: 1rem 0;
  position: sticky;
  top: 0;
  height: 100vh;
  overflow-y: auto;
  display: flex;
  flex-direction: column;
}

.sidebar-header {
  padding: 0.5rem 1rem 1rem;
  border-bottom: 1px solid var(--border);
  margin-bottom: 0.75rem;
  position: relative;
}

.site-logo {
  width: 28px;
  height: 28px;
  vertical-align: middle;
  margin-right: 0.3rem;
  border-radius: 3px;
}

.sidebar-header h1 {
  font-size: 1.1rem;
  font-weight: 700;
  color: var(--accent);
}

.sidebar-header h1 a { color: var(--accent); text-decoration: none; }
.sidebar-header h1 a:hover { color: var(--accent-hover); text-decoration: none; }

.theme-toggle {
  position: absolute;
  top: 0.75rem;
  right: 0.75rem;
  background: var(--bg-card);
  border: 1px solid var(--border);
  color: var(--text-dim);
  padding: 0.25rem 0.5rem;
  font-family: var(--font-sans);
  font-size: 0.7rem;
  cursor: pointer;
  transition: all 0.1s;
}

.theme-toggle:hover {
  color: var(--text);
  border-color: var(--text-dim);
}

.sidebar-header .subtitle {
  font-size: 0.75rem;
  color: var(--text-dim);
  margin-top: 0.15rem;
}

/* === Search === */
.search-box {
  padding: 0 0.75rem;
  margin-bottom: 0.75rem;
}

.search-box input {
  width: 100%;
  padding: 0.4rem 0.6rem;
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: 2px;
  color: var(--text);
  font-family: var(--font-sans);
  font-size: 0.85rem;
  outline: none;
  transition: border-color 0.1s;
}

.search-box input:focus {
  border-color: var(--accent);
}

.search-results {
  padding: 0 0.75rem;
  margin-bottom: 0.75rem;
  display: none;
}

.search-results.active { display: block; }

.search-results ul { list-style: none; }
.search-results li { padding: 0.25rem 0; }
.search-results a { font-size: 0.85rem; }

/* === Nav Latest === */
.nav-latest {
  padding: 0 0.5rem;
  margin-bottom: 0.75rem;
  border-bottom: 1px solid var(--border);
  padding-bottom: 0.75rem;
}

.nav-latest summary {
  cursor: pointer;
  font-size: 0.75rem;
  font-weight: bold;
  letter-spacing: 0.04em;
  color: var(--text);
  padding: 0.3rem 0.5rem;
  user-select: none;
}

.nav-latest summary:hover {
  color: var(--accent);
}

.nav-latest ul {
  list-style: none;
  padding: 0;
  margin: 0.2rem 0 0;
}

.nav-latest li a {
  display: block;
  padding: 0.25rem 0.5rem;
  font-size: 0.82rem;
  color: var(--text-dim);
  text-decoration: none;
  transition: all 0.1s;
}

.nav-latest li a:hover {
  color: var(--text);
  background: var(--bg-hover);
  text-decoration: none;
}

/* === Nav Tree === */
.nav-section {
  padding: 0 0.5rem;
  flex: 1;
  overflow-y: auto;
}

.nav-tree {
  list-style: none;
  padding-left: 0;
}

.nav-tree .nav-tree {
  padding-left: 0.9rem;
}

.nav-folder > details > summary {
  cursor: pointer;
  padding: 0.3rem 0.5rem;
  font-size: 0.85rem;
  font-weight: bold;
  color: var(--text);
  text-transform: uppercase;
  font-size: 0.75rem;
  letter-spacing: 0.04em;
  user-select: none;
}

.nav-folder > details > summary:hover {
  color: var(--accent);
}

.nav-folder > details > summary::marker {
  color: var(--text-dim);
}

.nav-leaf {
  padding: 0;
}

.nav-article-row {
  display: flex;
  align-items: center;
}

.nav-article-row > a {
  flex: 1;
}

.nav-article-toggle.collapsed ~ .nav-headings,
.nav-article-row.collapsed + .nav-headings {
  max-height: 0 !important;
  overflow: hidden;
}

.nav-leaf a {
  display: block;
  padding: 0.25rem 0.5rem;
  font-size: 0.85rem;
  color: var(--text-dim);
  text-decoration: none;
  transition: all 0.1s;
}

.nav-leaf a:hover {
  background: var(--bg-hover);
  color: var(--text);
  text-decoration: none;
}

.nav-leaf a.active {
  background: var(--bg-hover);
  color: var(--accent);
  border-left: 2px solid var(--accent);
}

/* === Nav Headings (TOC) === */
.nav-headings {
  list-style: none;
  padding-left: 0;
  margin: 0.15rem 0 0.4rem;
  overflow: hidden;
}

.nav-headings.collapsed {
  max-height: 0 !important;
  margin: 0;
}

.nav-heading-h2 {
  position: relative;
}

.nav-heading-h2 > .nav-h2-row {
  display: flex;
  align-items: center;
}

.nav-heading-h2 > .nav-h2-row > a {
  flex: 1;
  display: block;
  padding: 0.25rem 0.5rem 0.25rem 0.75rem;
  font-size: 0.8rem;
  color: var(--text-dim);
  text-decoration: none;
  border-left: 2px solid var(--border);
  transition: all 0.1s;
}

.nav-heading-h2 > .nav-h2-row > a:hover {
  color: var(--text);
  text-decoration: none;
  border-left-color: var(--text-dim);
}

.nav-heading-h2 > .nav-h2-row > a.active {
  color: var(--accent);
  border-left-color: var(--accent);
}

.nav-toggle {
  background: none;
  border: none;
  color: var(--text-dim);
  cursor: pointer;
  font-size: 0.65rem;
  padding: 0.2rem 0.4rem;
  opacity: 0.5;
  transition: opacity 0.1s;
  line-height: 1;
}

.nav-toggle:hover {
  opacity: 1;
  color: var(--text);
}

.nav-toggle.collapsed {
  transform: rotate(-90deg);
}

.nav-subheadings {
  list-style: none;
  padding-left: 0;
  margin: 0;
  overflow: hidden;
  transition: max-height 0.15s ease;
}

.nav-subheadings.collapsed {
  max-height: 0 !important;
}

.nav-heading-h3 a {
  display: block;
  padding: 0.2rem 0.5rem 0.2rem 1.5rem;
  font-size: 0.75rem;
  color: var(--text-dim);
  text-decoration: none;
  border-left: 2px solid transparent;
  opacity: 0.75;
  transition: all 0.1s;
}

.nav-heading-h3 a:hover {
  color: var(--text);
  text-decoration: none;
  opacity: 1;
}

.nav-heading-h3 a.active {
  color: var(--accent);
  border-left-color: var(--accent);
  opacity: 1;
}

/* === Main Content === */
.main {
  padding: 1.5rem 2rem;
  max-width: 800px;
}

/* === Article === */
.article-header {
  margin-bottom: 1.5rem;
  padding-bottom: 1rem;
  border-bottom: 1px solid var(--border);
}

.article-header h1 {
  font-size: 1.6rem;
  font-weight: 700;
  line-height: 1.3;
  color: #e0e0e0;
}

.article-meta {
  margin-top: 0.5rem;
  display: flex;
  align-items: center;
  gap: 0.75rem;
  flex-wrap: wrap;
}

.article-meta time {
  font-size: 0.8rem;
  color: var(--text-dim);
}

.article-summary {
  margin-top: 0.5rem;
  font-size: 0.9rem;
  color: var(--text-dim);
  font-style: italic;
}

/* === Article Content === */
.article-content {
  counter-reset: h2counter;
}

.article-content h2 {
  font-size: 1.2rem;
  margin: 1.5rem 0 0.5rem;
  color: #ddd;
  border-bottom: 1px solid var(--border);
  padding-bottom: 0.3rem;
  counter-increment: h2counter;
  counter-reset: h3counter;
}

.article-content h2::before {
  content: counter(h2counter) ". ";
  color: var(--text-dim);
}

.article-content h3 {
  font-size: 1rem;
  margin: 1.2rem 0 0.4rem;
  color: #ccc;
  counter-increment: h3counter;
}

.article-content h3::before {
  content: counter(h2counter) "." counter(h3counter) ". ";
  color: var(--text-dim);
}

.article-content p {
  margin-bottom: 0.8rem;
  padding-left: 1rem;
  text-align: justify;
}

.article-content ul, .article-content ol {
  margin: 0.4rem 0 0.8rem 2.5rem;
}

.article-content li {
  margin-bottom: 0.3rem;
}

.article-content blockquote {
  border-left: 3px solid var(--text-dim);
  padding: 0.5rem 0.75rem;
  margin: 0.8rem 0;
  background: var(--bg-nav);
  color: var(--text-dim);
}

.article-content code {
  font-family: var(--font-mono);
  background: var(--code-bg);
  padding: 0.1rem 0.35rem;
  font-size: 0.9em;
}

.article-content pre {
  background: var(--code-bg);
  padding: 0.75rem 1rem;
  overflow-x: auto;
  margin: 0.8rem 0;
  border: 1px solid var(--border);
}

.article-content pre code {
  background: none;
  padding: 0;
}

.article-content table {
  width: 100%;
  border-collapse: collapse;
  margin: 0.8rem 0;
}

.article-content th, .article-content td {
  padding: 0.5rem 0.6rem;
  text-align: left;
  border: 1px solid var(--border);
}

.article-content th {
  background: var(--bg-card);
  font-weight: bold;
}

.article-content img {
  max-width: 100%;
  margin: 0.8rem 0;
}

/* === Tags === */
.tag {
  display: inline-block;
  background: var(--tag-bg);
  color: var(--text-dim);
  padding: 0.1rem 0.45rem;
  border: 1px solid var(--border);
  font-size: 0.75rem;
}

/* === Home Page === */
.home-header {
  margin-bottom: 1.5rem;
}

.site-logo-large {
  width: 48px;
  height: 48px;
  vertical-align: middle;
  margin-right: 0.5rem;
  border-radius: 4px;
}

.home-header h1 {
  font-size: 1.6rem;
  font-weight: 700;
  color: #e0e0e0;
}

.home-header p {
  color: var(--text-dim);
  margin-top: 0.3rem;
}

.home-stats {
  margin-top: 0.5rem;
  font-size: 0.85rem;
  color: var(--text-dim);
}

.latest-section h2 {
  font-size: 1.1rem;
  color: var(--text);
  margin-bottom: 0.75rem;
  text-transform: uppercase;
  font-size: 0.8rem;
  letter-spacing: 0.05em;
  color: var(--text-dim);
}

.latest-list {
  list-style: none;
}

.latest-list li {
  padding: 0.6rem 0.5rem;
  border-bottom: 1px solid var(--border);
  display: flex;
  flex-wrap: wrap;
  align-items: baseline;
  gap: 0.5rem;
}

.latest-list li:hover {
  background: var(--bg-card);
}

.latest-list li:last-child { border-bottom: none; }

.latest-list a {
  font-size: 0.95rem;
}

.latest-date {
  font-size: 0.8rem;
  color: var(--text-dim);
}

.latest-summary {
  width: 100%;
  font-size: 0.82rem;
  color: var(--text-dim);
  margin-top: 0.15rem;
}

/* === TOC Sidebar (right) === */
.toc-sidebar {
  padding: 1.5rem 0.75rem;
  position: sticky;
  top: 0;
  height: 100vh;
  overflow-y: auto;
  border-left: 1px solid var(--border);
}

.toc {
  font-size: 0.8rem;
}

.toc-header {
  font-weight: bold;
  font-size: 0.75rem;
  text-transform: uppercase;
  letter-spacing: 0.05em;
  color: var(--text-dim);
  margin-bottom: 0.6rem;
  padding-bottom: 0.4rem;
  border-bottom: 1px solid var(--border);
}

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

.toc li {
  margin: 0;
}

.toc li a {
  display: block;
  padding: 0.25rem 0.4rem;
  color: var(--text-dim);
  text-decoration: none;
  border-left: 2px solid transparent;
  transition: all 0.1s;
  font-size: 0.78rem;
  line-height: 1.4;
}

.toc li a:hover {
  color: var(--text);
  text-decoration: none;
}

.toc li a.active {
  color: var(--accent);
  border-left-color: var(--accent);
}

.toc li.toc-h3 a {
  padding-left: 1.2rem;
  font-size: 0.73rem;
  opacity: 0.8;
}

/* === Mobile === */
@media (max-width: 768px) {
  .layout {
    grid-template-columns: 1fr !important;
  }

  .sidebar {
    position: relative;
    height: auto;
    border-right: none;
    border-bottom: 1px solid var(--border);
  }

  .toc-sidebar {
    display: none;
  }

  .main {
    padding: 1rem;
  }
}
