/* 
==========================================================================
   1. Variables
========================================================================== */
:root {
  --bg-color: #fdfbf7;
  --text-color: #222;
  --heading-color: #111;
  --link-color: #0056b3;
  --accent-color: #444;
  --meta-color: #666;
  --code-bg: #f5f5f5;
  --border-color: #e0e0e0;
  --font-serif: 'Lora', serif;
  --font-sans: 'Inter', sans-serif;
  --toggle-icon: "🌙";
}

/* Dark Mode */
:root[data-theme="dark"] {
  --bg-color: #1a1a1a;
  --text-color: #e6e6e6;
  --heading-color: #ffffff;
  --link-color: #66b3ff;
  --accent-color: #b0b0b0;
  --meta-color: #a0a0a0;
  --code-bg: #2d2d2d;
  --border-color: #444;
  --toggle-icon: "☀️";
}

@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg-color: #1a1a1a;
    --text-color: #e6e6e6;
    --heading-color: #ffffff;
    --link-color: #66b3ff;
    --accent-color: #b0b0b0;
    --meta-color: #a0a0a0;
    --code-bg: #2d2d2d;
    --border-color: #444;
    --toggle-icon: "☀️";
  }
}

/* 
==========================================================================
   2. Base Styles
========================================================================== */
body {
  font-family: var(--font-serif);
  color: var(--text-color);
  background-color: var(--bg-color);
  line-height: 1.8;
  margin: 0;
  padding: 0;
  font-size: 20px;
  transition: background-color 0.3s ease, color 0.3s ease;
}

h1,
h2,
h3,
h4,
h5,
h6 {
  font-family: var(--font-sans);
  color: var(--heading-color);
  margin-top: 2em;
  margin-bottom: 0.5em;
  line-height: 1.3;
}

h1 {
  font-size: 2.2em;
  border-bottom: 2px solid var(--text-color);
  padding-bottom: 10px;
}

h2 {
  font-size: 1.7em;
}

h3 {
  font-size: 1.4em;
}

a {
  color: var(--link-color);
  text-decoration: underline;
  text-decoration-thickness: 1px;
  text-underline-offset: 2px;
}

a:hover {
  color: #003d82;
  text-decoration-thickness: 2px;
}

blockquote {
  border-left: 4px solid var(--text-color);
  margin: 2em 0;
  padding-left: 20px;
  font-style: italic;
  color: var(--meta-color);
}

img {
  max-width: 100%;
  height: auto;
  display: block;
  margin: 2em auto;
  border-radius: 4px;
}

/* Code Blocks */
pre,
code {
  font-family: 'Menlo', 'Monaco', 'Courier New', monospace;
  font-size: 0.9em;
}

code {
  background-color: var(--code-bg);
  padding: 2px 4px;
  border-radius: 3px;
  color: var(--text-color);
}

pre {
  background-color: var(--code-bg);
  padding: 15px;
  overflow-x: auto;
  border-radius: 5px;
  border: 1px solid var(--border-color);
}

pre code {
  background-color: transparent;
  padding: 0;
  color: inherit;
}

/* 
==========================================================================
   3. Layout
========================================================================== */
.wrapper {
  max-width: 1100px;
  margin-right: auto;
  margin-left: auto;
  padding-right: 30px;
  padding-left: 30px;
  display: flex;
  flex-direction: column;
  align-items: center;
}

.site-header .wrapper {
  width: 100%;
  max-width: 1100px;
  display: flex;
  flex-direction: row;
  justify-content: space-between;
  align-items: center;
}

.post {
  width: 100%;
  max-width: 1100px;
  /* Allow full width for layout flexibility */
}

.post-content {
  width: 100%;
  max-width: 760px;
  /* Widened from 700px */
  margin-left: 0;
  margin-right: auto;
  position: relative;
}

.post-header {
  width: 100%;
  max-width: 760px;
  /* Match content width */
  margin-left: 0;
  margin-right: auto;
  text-align: left;
  /* Left align header */
}

.site-footer .wrapper {
  width: 100%;
  max-width: 760px;
  /* Match content width */
  margin-left: 0;
  margin-right: auto;
}

/* TOC Sidebar */
.toc-sidebar {
  float: right;
  clear: right;
  margin-right: -42%;
  width: 38%;
  position: -webkit-sticky;
  position: sticky;
  top: 20px;
  max-height: calc(100vh - 40px);
  overflow-y: auto;
  z-index: 100;
  /* Increased z-index */
  background-color: var(--bg-color);
  padding: 15px;
  border-radius: 8px;
  margin-bottom: 20px;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.05);
}

.toc-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  cursor: pointer;
  margin-bottom: 10px;
  user-select: none;
}

.toc-toggle-btn {
  background: none;
  border: none;
  color: var(--meta-color);
  font-size: 0.8rem;
  cursor: pointer;
}

.toc-title {
  font-weight: 600;
  font-size: 0.9rem;
  color: var(--meta-color);
  text-transform: uppercase;
  letter-spacing: 1px;
}

.toc-content {
  display: block;
}

.toc-content.collapsed {
  display: none;
}

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

.toc-list li {
  margin-bottom: 6px;
}

.toc-list a {
  text-decoration: none;
  color: var(--meta-color);
  transition: color 0.2s ease;
  display: block;
}

.toc-list a:hover,
.toc-list a.active {
  color: var(--link-color);
  font-weight: 600;
}

.toc-list .toc-h3 {
  padding-left: 15px;
  font-size: 0.85rem;
}

.home-layout,
.home-layout .post-content {
  max-width: 100%;
}

.site-footer .wrapper {
  display: flex;
  justify-content: center;
  align-items: center;
}

/* 
==========================================================================
   4. Components
========================================================================== */

/* Header */
.site-header {
  border-bottom: 1px solid var(--border-color);
  padding: 20px 0;
  margin-bottom: 40px;
  font-family: var(--font-sans);
}

.header-left {
  flex: 1;
  text-align: left;
}

.site-nav {
  flex: 1;
  text-align: center;
}

.header-right {
  flex: 1;
  display: flex;
  justify-content: flex-end;
  align-items: center;
  gap: 15px;
}

.site-title {
  font-size: 24px;
  font-weight: 700;
  color: var(--heading-color);
  text-decoration: none;
  letter-spacing: -0.5px;
}

.site-nav .page-link {
  color: var(--text-color);
  text-decoration: none;
  margin-left: 20px;
  font-size: 16px;
}

.site-nav .page-link:hover {
  text-decoration: underline;
}

.social-links-header {
  display: flex;
  gap: 10px;
  align-items: center;
}

.social-links-header a {
  color: var(--meta-color);
  display: flex;
  align-items: center;
  transition: color 0.2s ease;
}

.social-links-header a:hover {
  color: var(--link-color);
}

.theme-toggle {
  background: none;
  border: none;
  cursor: pointer;
  font-size: 20px;
  padding: 0;
  margin-left: 20px;
  line-height: 1;
  color: var(--text-color);
}

/* Post List (Homepage) */
.home-container {
  display: grid;
  grid-template-columns: 1fr 250px;
  gap: 40px;
  align-items: start;
}

.home-main {
  min-width: 0;
  padding-bottom: 80px;
  /* Ensure space at bottom */
}

.home-sidebar {
  position: sticky;
  top: 40px;
  text-align: left;
  /* Changed from right to left */
  padding-left: 20px;
  border-left: 1px solid var(--border-color);
}

.home-sidebar h3 {
  font-size: 1.2rem;
  margin-top: 0;
  margin-bottom: 20px;
  border-bottom: none;
  font-weight: 700;
}

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

.category-list li {
  margin-bottom: 8px;
}

.category-list a {
  color: var(--meta-color);
  text-decoration: none;
  font-size: 0.95rem;
  display: block;
  padding: 5px 0;
  transition: color 0.2s ease;
}

.category-list a:hover,
.category-list a.active {
  color: var(--link-color);
  font-weight: bold;
}

.post-item {
  display: flex;
  gap: 30px;
  margin-bottom: 40px;
  padding-bottom: 40px;
  border-bottom: 1px solid var(--border-color);
  align-items: baseline;
}

.post-item:last-child {
  border-bottom: none;
}

.post-date-side {
  flex: 0 0 120px;
  font-family: var(--font-sans);
  font-size: 0.9rem;
  color: var(--meta-color);
  text-align: left;
  padding-top: 8px;
}

.read-time {
  display: block;
  font-size: 0.8rem;
  margin-top: 5px;
  opacity: 0.8;
  text-align: left;
}

.post-content-side {
  flex: 1;
  min-width: 0;
}

.post-content-side h3 {
  margin-top: 0;
  margin-bottom: 8px;
}

.post-content-side .post-tags {
  margin-top: 5px;
  margin-bottom: 10px;
}

.post-content-side .post-subtitle {
  margin-top: 0;
  margin-bottom: 0;
  font-size: 1rem;
}

.post-content-wrapper {
  display: flex;
  justify-content: space-between;
  align-items: flex-start;
  gap: 20px;
}

.post-text-col {
  flex: 1;
  min-width: 0;
}

.post-thumbnail {
  flex: 0 0 120px;
  width: 120px;
  height: 80px;
  object-fit: cover;
  border-radius: 6px;
  border: 1px solid var(--border-color);
  background-color: var(--code-bg);
}

.post-link-inline {
  font-size: 18px;
  font-weight: 600;
  color: var(--link-color);
  text-decoration: none;
}

.post-link-inline:hover {
  text-decoration: underline;
}

/* Restored .post-link for homepage titles */
.post-link {
  font-size: 1.5rem;
  font-weight: 700;
  color: var(--heading-color);
  text-decoration: none;
  font-family: var(--font-sans);
  display: block;
  margin-bottom: 0.2rem;
  cursor: pointer;
}

.post-link:hover {
  text-decoration: none;
  color: var(--heading-color);
}

/* Inline links for Notes page */
.post-link-inline {
  font-weight: 600;
  color: var(--text-color);
  text-decoration: none;
  border-bottom: 1px solid transparent;
  transition: border-color 0.2s ease;
}

.post-link-inline:hover {
  border-bottom-color: var(--text-color);
  text-decoration: none;
}

.post-meta {
  font-size: 0.9rem;
  color: var(--meta-color);
  margin-bottom: 0.5rem;
  display: block;
}

/* Post Detail */
.post-header {
  margin-bottom: 30px;
}

.post-title {
  font-size: 2.5rem;
  margin-bottom: 10px;
  border-bottom: none;
}

.post-subtitle {
  font-size: 1.1rem;
  color: var(--text-color);
  margin-top: 5px;
  margin-bottom: 10px;
  line-height: 1.5;
}

.post-meta {
  font-family: var(--font-sans);
  color: var(--meta-color);
  font-size: 0.9rem;
}

.post-tags {
  display: flex;
  gap: 8px;
  margin-top: 10px;
  flex-wrap: wrap;
}

.post-tag {
  display: inline-block;
  padding: 1px 4px;
  border: 1px solid var(--border-color);
  border-radius: 4px;
  font-size: 0.65rem;
  color: var(--meta-color);
  text-decoration: none;
  text-transform: uppercase;
  letter-spacing: 0.5px;
  transition: all 0.2s ease;
}

.post-tag:hover {
  border-color: var(--link-color);
  color: var(--link-color);
  background-color: var(--code-bg);
}

/* Sidenotes */
.sidenote {
  float: right;
  clear: right;
  margin-right: -42%;
  /* Match TOC sidebar */
  width: 38%;
  /* Match TOC sidebar */
  margin-top: 0.3rem;
  margin-bottom: 0;
  font-size: 0.9rem;
  line-height: 1.4;
  vertical-align: baseline;
  position: relative;
  color: var(--meta-color);
}

.sidenote-number {
  counter-increment: sidenote-counter;
}

.sidenote-number:after,
.sidenote:before {
  content: counter(sidenote-counter);
  font-family: var(--font-sans);
  font-size: 0.7rem;
  top: -0.5rem;
  left: 0.1rem;
  position: relative;
  vertical-align: baseline;
  color: var(--link-color);
  font-weight: bold;
  margin-right: 4px;
}

.sidenote:before {
  content: counter(sidenote-counter) ". ";
}

input.margin-toggle {
  display: none;
}

label.sidenote-number {
  display: inline;
  cursor: pointer;
}

label.margin-toggle:not(.sidenote-number) {
  display: none;
}

article.post {
  counter-reset: sidenote-counter;
}

/* Search */
.search-toggle {
  background-color: transparent;
  border: none;
  cursor: pointer;
  padding: 0;
  color: var(--text-color);
  display: flex;
  align-items: center;
  justify-content: center;
  transition: color 0.2s ease;
}

.search-toggle:hover {
  color: var(--link-color);
}

.search-modal {
  position: fixed;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  background: rgba(0, 0, 0, 0.5);
  backdrop-filter: blur(2px);
  z-index: 9999;
  display: none;
  align-items: flex-start;
  justify-content: center;
  padding-top: 100px;
}

.search-modal.active {
  display: flex;
}

.search-wrapper {
  background: var(--bg-color);
  width: 600px;
  max-width: 90%;
  border-radius: 8px;
  box-shadow: 0 10px 25px rgba(0, 0, 0, 0.1);
  overflow: hidden;
  border: 1px solid var(--border-color);
}

.search-header {
  display: flex;
  align-items: center;
  padding: 15px 20px;
  border-bottom: 1px solid var(--border-color);
}

.search-icon {
  color: var(--meta-color);
  margin-right: 15px;
}

#search-input {
  flex: 1;
  border: none;
  background: none;
  font-size: 18px;
  color: var(--text-color);
  font-family: var(--font-sans);
  outline: none;
}

.close-search {
  background: none;
  border: 1px solid var(--border-color);
  border-radius: 4px;
  padding: 4px 8px;
  font-size: 12px;
  cursor: pointer;
  color: var(--meta-color);
  margin-left: 10px;
}

.search-results {
  max-height: 400px;
  overflow-y: auto;
}

.search-result-item {
  display: block;
  padding: 15px 20px;
  border-bottom: 1px solid var(--border-color);
  text-decoration: none;
  transition: background-color 0.2s ease;
}

.search-result-item:last-child {
  border-bottom: none;
}

.search-result-item:hover {
  background-color: var(--code-bg);
}

.search-result-title {
  font-family: var(--font-sans);
  font-weight: 600;
  color: var(--heading-color);
  margin-bottom: 4px;
}

.search-result-date {
  font-size: 12px;
  color: var(--meta-color);
}

.search-no-results {
  padding: 20px;
  text-align: center;
  color: var(--meta-color);
}

.search-snippet {
  font-size: 0.85rem;
  color: var(--meta-color);
  margin-top: 4px;
  line-height: 1.4;
  display: -webkit-box;
  -webkit-line-clamp: 2;
  line-clamp: 2;
  -webkit-box-orient: vertical;
  overflow: hidden;
}

.search-highlight {
  background-color: rgba(255, 215, 0, 0.3);
  color: var(--heading-color);
  font-weight: 600;
  border-radius: 2px;
  padding: 0 2px;
}

/* 
==========================================================================
   5. Responsive
========================================================================== */
@media (max-width: 1200px) {
  .wrapper {
    max-width: 90%;
  }

  .sidenote {
    float: none;
    margin: 1rem 0;
    width: 100%;
    position: static;
    clear: both;
    display: block;
  }

  .toc-sidebar {
    float: none;
    margin: 0 0 20px 0;
    width: 100%;
    position: static;
    clear: both;
    max-height: none;
  }

  .post-content,
  .post-header {
    max-width: 100%;
  }

  .margin-toggle:checked+.sidenote {
    display: block;
  }

  .sidenote-number {
    text-decoration: underline;
    text-decoration-style: dotted;
    color: var(--link-color);
  }
}

@media (max-width: 800px) {
  .home-container {
    grid-template-columns: 1fr;
    gap: 40px;
  }

  .home-sidebar {
    order: -1;
    border-left: none;
    border-top: 1px solid var(--border-color);
    padding-left: 0;
    padding-top: 30px;
    position: static;
  }

  /* 404 Page */
  .not-found-container {
    text-align: center;
    padding: 60px 20px;
    max-width: 600px;
    margin: 0 auto;
  }

  .not-found-title {
    font-size: 6rem;
    font-weight: 700;
    margin-bottom: 10px;
    color: var(--link-color);
    border-bottom: none;
    line-height: 1;
  }

  .not-found-text {
    font-size: 1.5rem;
    margin-bottom: 10px;
    color: var(--heading-color);
  }

  .not-found-subtext {
    color: var(--meta-color);
    margin-bottom: 40px;
  }

  .back-home-btn {
    display: inline-block;
    padding: 12px 24px;
    background-color: var(--text-color);
    color: var(--bg-color);
    text-decoration: none;
    border-radius: 4px;
    font-weight: 600;
    transition: opacity 0.2s ease;
  }

  .back-home-btn:hover {
    opacity: 0.9;
    color: var(--bg-color);
    text-decoration: none;
  }

  .post-item {
    flex-direction: column;
    gap: 5px;
  }

  .post-date-side {
    text-align: left;
    flex: auto;
    padding-top: 0;
    font-size: 0.85rem;
  }
}