/* ==========================================================================
   xie'blog Advanced Reading Experience & Chinese Typography
   ========================================================================== */

/* 1. Fixed Reading Progress Bar */
#reading-progress {
  position: fixed;
  top: 0;
  left: 0;
  height: 2px;
  width: 0%;
  background: var(--color-text);
  z-index: 99999;
  transition: width 80ms ease-out;
  opacity: 0.85;
}

/* 2. Reading Layout Structure (Desktop Multi-column) */
.reading-layout {
  position: relative;
  display: flex;
  justify-content: center;
  align-items: flex-start;
  gap: var(--space-xl, 3rem);
  width: 100%;
  max-width: 1300px;
  margin: 0 auto;
}

.reading-main {
  flex: 1;
  max-width: 44rem;
  width: 100%;
}

/* 3. Floating Sticky TOC (Desktop) */
.reading-aside {
  position: sticky;
  top: 5.5rem;
  width: 220px;
  max-height: calc(100vh - 7.5rem);
  overflow-y: auto;
  font-family: var(--mono-font);
  font-size: 0.8rem;
  display: none;
  padding-left: 1.1rem;
  scrollbar-width: none;
}

.reading-aside::-webkit-scrollbar {
  display: none;
}

@media (min-width: 1100px) {
  .reading-aside {
    display: block;
  }
}

.toc-heading {
  font-size: 0.72rem;
  letter-spacing: 0.08em;
  opacity: 0.65;
  margin-bottom: 0.85rem;
  text-transform: uppercase;
}

.toc-container {
  position: relative;
}

/* Vertical Smooth Tracking Rail */
.toc-rail {
  position: absolute;
  left: -1.1rem;
  top: 0;
  bottom: 0;
  width: 1px;
  background: rgba(128, 128, 128, 0.2);
}

/* Apple-style Sliding Indicator */
.toc-indicator {
  position: absolute;
  left: -1.1rem;
  width: 2px;
  height: 18px;
  background: var(--color-text);
  opacity: 0;
  pointer-events: none;
  border-radius: 1px;
  transition: transform 0.22s cubic-bezier(0.16, 1, 0.3, 1), height 0.22s ease, opacity 0.2s ease;
}

.toc-list {
  list-style: none;
  padding: 0;
  margin: 0;
  display: flex;
  flex-direction: column;
  gap: 0.55rem;
}

.toc-item--depth-3 {
  padding-left: 0.75rem;
  opacity: 0.85;
  font-size: 0.75rem;
}

.toc-link {
  color: var(--color-text);
  opacity: 0.8;
  text-decoration: none;
  display: block;
  line-height: 1.45;
  transition: opacity 0.2s ease, color 0.2s ease;
  position: relative;
  font-family: var(--serif-font) !important;
  font-style: normal;
  font-size: 0.9rem;
  text-transform: none !important;
}

.toc-link:hover {
  opacity: 0.95;
  text-decoration: underline;
  text-decoration-style: dotted;
}

.toc-link.active {
  opacity: 1;
  font-weight: 600;
  color: var(--color-text);
}

/* 4. Enhanced Chinese & Latin Typography in .letter-body (Apple HIG inspired) */
.letter-body {
  font-family: var(--serif-font);
  font-size: 18px;
  line-height: 1.85;
  color: var(--color-text);
  word-break: break-word;
  text-rendering: optimizeLegibility;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
  font-feature-settings: "palt" 1, "kern" 1;
}

.letter-body p {
  margin-bottom: 0.88em;
  text-align: start;
}

.letter-body h1, 
.letter-body h2, 
.letter-body h3, 
.letter-body h4 {
  font-family: var(--serif-font);
  font-weight: 600;
  color: var(--color-text);
  line-height: 1.35;
  scroll-margin-top: 5rem;
}

/* Proximity: H2 leans closer to following prose; clean whitespace instead of fences */
.letter-body h2 {
  font-size: 1.55rem;
  margin-top: 2.5em;
  margin-bottom: 0.55em;
  padding-bottom: 0;
  border-bottom: none;
  letter-spacing: -0.01em;
}

.letter-body h3 {
  font-size: 1.25rem;
  margin-top: 2.1em;
  margin-bottom: 0.5em;
}

.letter-body strong, 
.letter-body b {
  font-weight: 700;
}

.letter-body blockquote {
  margin: 1.8em 0;
  padding: 0.6em 1.2em;
  border-left: 2px solid var(--color-text);
  opacity: 0.9;
  font-style: italic;
  background: rgba(128, 128, 128, 0.04);
}

.letter-body ul, 
.letter-body ol {
  margin: 1.2em 0 1.5em 1.5em;
  padding: 0;
}

.letter-body li {
  margin-bottom: 0.5em;
  line-height: 1.8;
}

.letter-body hr {
  border: 0;
  border-top: 1px dashed rgba(128, 128, 128, 0.3);
  margin: 3em 0;
}

/* 5. Terminal-Styled Code Blocks with Scroll Hint */
.code-block-wrapper {
  position: relative;
  margin: 1.8em 0;
  border: 1px solid rgba(128, 128, 128, 0.25);
  border-radius: 4px;
  background: rgba(0, 0, 0, 0.02);
  overflow: hidden;
}

:root.dark .code-block-wrapper,
[data-theme="dark"] .code-block-wrapper,
.theme-night .code-block-wrapper {
  background: rgba(255, 255, 255, 0.02);
}

.code-block-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 5px 12px;
  background: rgba(128, 128, 128, 0.06);
  border-bottom: 1px solid rgba(128, 128, 128, 0.15);
  font-family: var(--mono-font);
  font-size: 0.75rem;
}

.code-lang {
  letter-spacing: 0.05em;
  opacity: 0.7;
}

.code-copy-btn {
  background: none;
  border: none;
  padding: 0;
  color: var(--color-text);
  font-family: var(--mono-font);
  font-size: 0.72rem;
  cursor: pointer;
  opacity: 0.75;
  transition: opacity 0.2s, color 0.2s;
}

.code-copy-btn:hover {
  opacity: 1;
  text-decoration: underline;
  text-decoration-style: dotted;
}

.letter-body pre {
  margin: 0;
  padding: 14px 16px;
  overflow-x: auto;
  font-family: var(--mono-font);
  font-size: 0.86rem;
  line-height: 1.6;
  scrollbar-width: thin;
}

/* Horizontal scroll indicator gradient */
.code-block-wrapper.has-overflow::after {
  content: "";
  position: absolute;
  top: 30px;
  right: 0;
  bottom: 0;
  width: 24px;
  background: linear-gradient(to right, transparent, rgba(0, 0, 0, 0.08));
  pointer-events: none;
  transition: opacity 0.2s;
}

.theme-night .code-block-wrapper.has-overflow::after {
  background: linear-gradient(to right, transparent, rgba(255, 255, 255, 0.08));
}

.code-block-wrapper.scrolled-end::after {
  opacity: 0;
}

.letter-body code:not(pre code) {
  font-family: var(--mono-font);
  font-size: 0.86em;
  padding: 2px 6px;
  margin: 0 2px;
  border-radius: 3px;
  background: rgba(128, 128, 128, 0.1);
  border: 1px solid rgba(128, 128, 128, 0.15);
  vertical-align: 0.04em;
}

/* Image Click-to-Zoom Lightbox (Apple/Medium style) */
.letter-body img {
  max-width: 100%;
  height: auto;
  border-radius: 4px;
  cursor: zoom-in;
  transition: transform 0.2s ease;
}

.image-zoom-overlay { border: 0; padding: 0; width: 94vw; max-width: 1400px; max-height: 92vh; margin: auto; background: #faf9f6; color: #353431; border-radius: 6px; overflow: hidden; }
.image-zoom-overlay::backdrop { background: rgba(0,0,0,.8); }
.zoom-close { display: block; margin-left: auto; padding: 12px 20px; min-height: 44px; background: none; border: 0; font: inherit; cursor: pointer; }
.zoom-viewport { overflow: auto; max-height: calc(92vh - 52px); padding: 16px; box-sizing: border-box; }
.zoom-viewport img { display: block; max-width: 100%; height: auto; margin: auto; }
.zoom-viewport img.zoom-diagram { max-width: none; width: max(100%, 640px); }

/* 6. Post Epilogue & Next/Prev Navigation */
.post-epilogue {
  margin-top: 3.5rem;
  padding-top: 1.5rem;
  border-top: 1px dashed rgba(128, 128, 128, 0.3);
}

.author-signature {
  font-family: var(--serif-font);
  font-style: italic;
  font-size: 1rem;
  opacity: 0.85;
  margin-bottom: 2.5rem;
  text-align: right;
}

.post-nav-grid {
  display: grid;
  grid-template-columns: 1fr;
  gap: 1rem;
  margin-bottom: 2rem;
}

@media (min-width: 640px) {
  .post-nav-grid {
    grid-template-columns: 1fr 1fr;
    align-items: stretch;
  }
}

.post-nav-card {
  display: flex;
  flex-direction: column;
  justify-content: flex-start;
  padding: 14px 16px;
  border: 1px solid rgba(128, 128, 128, 0.25);
  text-decoration: none;
  color: var(--color-text);
  transition: border-color 0.2s, background 0.2s;
  height: 100%;
  box-sizing: border-box;
}

.post-nav-card:hover {
  border-color: var(--color-text);
  background: rgba(128, 128, 128, 0.04);
}

.post-nav-dir {
  font-family: var(--mono-font);
  font-size: 0.72rem;
  letter-spacing: 0.08em;
  opacity: 0.65;
  margin-bottom: 6px;
  flex: 0 0 auto;
}

.post-nav-title {
  font-family: var(--serif-font);
  font-style: italic;
  font-size: 0.95rem;
  line-height: 1.4;
  flex: 1 1 auto;
}

.post-footer-actions {
  display: flex;
  justify-content: space-between;
  font-family: var(--mono-font);
  font-size: var(--text-ui);
  padding-top: 1rem;
}

/* 7. Mobile Floating TOC Pill & Drawer */
.mobile-toc-toggle-btn {
  position: fixed;
  bottom: max(1.5rem, env(safe-area-inset-bottom, 1.5rem));
  right: 1.25rem;
  z-index: 900;
  display: flex;
  align-items: center;
  gap: 6px;
  padding: 7px 14px;
  font-family: var(--mono-font);
  font-size: 0.78rem;
  background: rgba(250, 249, 246, 0.88);
  backdrop-filter: blur(8px);
  -webkit-backdrop-filter: blur(8px);
  color: var(--color-text);
  border: 1px solid rgba(128, 128, 128, 0.4);
  border-radius: 9999px;
  box-shadow: 0 4px 16px rgba(0, 0, 0, 0.08);
  cursor: pointer;
  transition: opacity 0.25s ease, transform 0.25s ease, border-color 0.2s ease, background-color 0.2s ease;
}

.theme-night .mobile-toc-toggle-btn {
  background: rgba(30, 30, 30, 0.88);
  border-color: rgba(255, 255, 255, 0.25);
  box-shadow: 0 4px 16px rgba(0, 0, 0, 0.4);
}

.mobile-toc-toggle-btn:hover,
.mobile-toc-toggle-btn:focus-visible {
  opacity: 1 !important;
  transform: none !important;
  border-color: var(--color-text);
}

.mobile-toc-toggle-btn.is-scrolling {
  opacity: 0.35;
  transform: translateX(6px) scale(0.96);
}

@media (min-width: 1100px) {
  .mobile-toc-toggle-btn {
    display: none;
  }
}

@media (max-width: 1099px) {
  .page--post-detail .post-epilogue {
    padding-bottom: calc(4.5rem + env(safe-area-inset-bottom, 0px));
  }
}

.mobile-toc-drawer {
  position: fixed;
  bottom: 0;
  left: 0;
  right: 0;
  max-height: 70vh;
  background: rgb(var(--chrome-surface));
  border-top: 1px solid var(--color-text);
  box-shadow: 0 -4px 20px rgba(0, 0, 0, 0.15);
  z-index: 1000;
  transform: translateY(100%);
  transition: transform 0.3s cubic-bezier(0.16, 1, 0.3, 1);
  padding: 1.5rem;
  overflow-y: auto;
}

.mobile-toc-drawer.open {
  transform: translateY(0);
}

.mobile-toc-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  font-family: var(--mono-font);
  font-size: 0.85rem;
  margin-bottom: 1rem;
  padding-bottom: 0.5rem;
  border-bottom: 1px dashed rgba(128, 128, 128, 0.3);
}

.mobile-toc-close-btn {
  background: none;
  border: none;
  font-family: var(--mono-font);
  font-size: 0.85rem;
  cursor: pointer;
  color: var(--color-text);
}

/* 8. Post Header In-flow Navigation (Scrolls naturally with article, never blocks text) */
.post-header-nav {
  display: flex;
  justify-content: space-between;
  align-items: center;
  flex-wrap: wrap;
  gap: 12px;
  margin-bottom: 22px;
  padding-bottom: 12px;
  border-bottom: 1px dashed rgba(128, 128, 128, 0.2);
  font-family: var(--mono-font);
  font-size: var(--text-ui);
  line-height: var(--leading-ui);
  text-transform: uppercase;
}

.post-breadcrumbs {
  display: flex;
  align-items: center;
  gap: 6px;
}

.post-breadcrumbs .crumb-link,
.post-back-action .back-link {
  color: inherit;
  text-decoration: none;
  opacity: 0.8;
  transition: opacity 0.2s ease;
}

.post-breadcrumbs .crumb-link:hover,
.post-back-action .back-link:hover {
  opacity: 1;
  text-decoration: underline;
  text-decoration-style: dotted;
}

.post-breadcrumbs .crumb-sep {
  opacity: 0.4;
  margin: 0 1px;
}

.post-breadcrumbs .crumb-current {
  opacity: 0.5;
}

/* Hide fixed top-left header on reading page to completely prevent blocking prose text */
body:has(.page--post-detail) .site-nav-left,
body.page--post-detail-active .site-nav-left {
  display: none !important;
}


/* Article template: title, introduction, and a consistent reading measure. */
.reading-main { min-width: 0; }
.post-heading { margin-bottom: 2.25rem; }
.post-heading h1 { font: 600 clamp(1.75rem, 2.8vw, 2.4rem)/1.35 var(--serif-font); color: var(--color-text); text-wrap: pretty; margin: .65rem 0 1rem; }
.post-meta-line { display: flex; flex-wrap: wrap; gap: .4rem .65rem; font: .85rem/1.6 var(--serif-font); color: var(--color-text); opacity: .8; }
.post-tags { display: flex; flex-wrap: wrap; gap: .3rem .8rem; margin: .8rem 0 1.4rem; font: .85rem/1.7 var(--serif-font); }
.post-tags a { white-space: nowrap; color: var(--color-text); text-decoration: underline dotted; text-underline-offset: 3px; }
.post-deck { font: 1.05rem/1.8 var(--serif-font); margin: 1rem 0; color: var(--color-text); }
.post-summary { padding: 1.1rem 1.25rem; border-left: 2px solid currentColor; background: rgba(128,128,128,.055); font: .95rem/1.8 var(--serif-font); color: var(--color-text); }
.post-audience { margin-bottom: .8rem; }
.summary-label { font-weight: 600; }
.post-summary ul { padding-left: 1.2rem; margin-top: .35rem; }
.post-summary li + li { margin-top: .4rem; }
.page--post-detail .letter-body { font-size: 18px; line-height: 1.85; }
.letter-body h2, .letter-body h3 { text-wrap: pretty; }
.letter-body ol { list-style: decimal; padding-left: 1.5em; margin-left: 0; }
.letter-body ul { list-style: disc; padding-left: 1.5em; margin-left: 0; }
.letter-body li { padding-left: .15em; }
.letter-body li::before { content: none; }
.letter-body blockquote { font-style: normal; }
.letter-body .katex-display { overflow-x: auto; overflow-y: hidden; padding: .6rem 0; max-width: 100%; }
.letter-body .katex-display > .katex { text-align: left; }
.table-scroll { max-width: 100%; overflow-x: auto; margin: 1.8rem 0; scrollbar-width: thin; }
.letter-body table { border-collapse: collapse; width: 100%; min-width: 640px; font-size: .9rem; line-height: 1.65; }
.letter-body th, .letter-body td { padding: .8rem .9rem; text-align: left; vertical-align: top; border-bottom: 1px solid rgba(128,128,128,.25); word-break: normal; overflow-wrap: normal; }
.letter-body th { background: rgba(128,128,128,.07); font-weight: 600; }
.letter-body tbody tr:nth-child(even) { background: rgba(128,128,128,.025); }
.diagram-figure { margin: 2rem 0; }
.diagram-figure img { display: block; width: 100%; max-width: 380px; margin-inline: auto; background: #faf9f6; padding: 12px; box-sizing: border-box; border: 1px solid rgba(128,128,128,.2); }
.diagram-figure figcaption { font: .85rem/1.6 var(--serif-font); text-align: center; margin-top: .65rem; opacity: .8; }
.toc-item--depth-3 { display: none; }
.toc-item--depth-3.is-current-section { display: list-item; }
.toc-link { font-family: var(--serif-font) !important; }
.mobile-toc-drawer[hidden] { display: none; }
.mobile-toc-drawer.open { visibility: visible; }
.mobile-toc-toggle-btn { min-height: 44px; }
.mobile-toc-close-btn { min-height: 44px; }
.mobile-toc-drawer .toc-link { padding-block: .3rem; }
.related-reading { font: 1rem/1.7 var(--serif-font); margin: 0 0 2rem; }
.related-reading h2 { font-size: 1.1rem; margin-bottom: .7rem; }
.related-reading ul { padding-left: 1.2rem; }
.related-reading li { margin: .5rem 0; }
.related-reading a, .series-page a, .archive-page a { color: var(--color-text); text-underline-offset: 4px; }
.related-reading p { margin-top: .5rem; font-size: .9rem; opacity: .8; }
.post-nav-title { font-style: normal; }
.series-eyebrow { font: .85rem/1.6 var(--serif-font); color: var(--color-text); }
.archive-page, .series-page { color: var(--color-text); font: 1rem/1.7 var(--serif-font); }
.archive-header h1, .series-page h1 { font-size: 2.25rem; line-height: 1.35; margin: .5rem 0 1.5rem; }
.topic-filters { display: flex; flex-wrap: wrap; gap: .5rem; margin-bottom: 1rem; }
.topic-filters a { display: inline-block; padding: .4rem .9rem; border: 1px solid rgba(128,128,128,.25); border-radius: 2rem; text-decoration: none; }
.topic-filters a[aria-current="true"] { background: var(--color-text); color: rgb(var(--chrome-surface)); }
.archive-series { display: inline-block; margin: .5rem 0 1rem; font-size: .95rem; }
.archive-posts { list-style: none; margin: 0; padding: 0; }
.archive-posts > li { padding: 1.5rem 0; border-bottom: 1px solid rgba(128,128,128,.25); }
.archive-posts h2 { font-size: 1.3rem; line-height: 1.5; margin: .4rem 0; font-weight: 600; }
.archive-posts h2 a { text-decoration: none; }
.archive-posts h2 a:hover { text-decoration: underline; }
.archive-meta { display: flex; gap: 1rem; font-size: .85rem; opacity: .8; }
.archive-excerpt { font-size: .95rem; line-height: 1.8; }
.archive-footer { display: flex; gap: 1.5rem; margin-top: 2rem; }
.series-list { list-style: none; margin: 2rem 0; padding: 0; }
.series-list li { display: grid; grid-template-columns: 2rem 1fr auto; align-items: baseline; gap: .75rem; padding: 1rem 0; border-bottom: 1px solid rgba(128,128,128,.2); }
.series-number, .series-status { font-size: .8rem; opacity: .8; white-space: nowrap; }
.home-latest-header h2 { font-size: 1.65rem; }
.home-latest-header { gap: 1rem; }
.home-post-title { font-style: normal; line-height: 1.5; }
@media (max-width: 520px) {
  .page--post-detail .letter-body { font-size: 17px; line-height: 1.85; }
  .home-latest-header { flex-direction: row; align-items: center; }
  .home-section-kicker { font-size: .7rem; }
  .home-view-all { font-size: .8rem; }
  .home-post-link { grid-template-columns: minmax(0, 1fr) auto; gap: .6rem; }
  .home-post-number { display: none; }
  .post-header-nav { margin-bottom: 1rem; font-size: .75rem; }
  .post-summary { padding: 1rem; }
  .post-meta-line { font-size: .8rem; }
  .post-tags { margin-bottom: 1rem; }
  .post-back-action { display: none; }
}
@media (prefers-reduced-motion: reduce) {
  .toc-indicator, .mobile-toc-drawer, .image-zoom-overlay, .image-zoom-overlay img { transition: none; }
}
@media print {
  #canvas, .site-header, .site-footer-bar, .reading-aside, .mobile-toc-toggle-btn, .mobile-toc-drawer, .image-zoom-overlay { display: none !important; }
  .content-page { padding: 0; }
  .table-scroll, .letter-body .katex-display { overflow: visible; }
  .letter-body table { min-width: 0; font-size: 10pt; }
}

.post-epilogue, .post-footer-actions a { color: var(--color-text); }
@media (max-width: 700px) {
  .table-scroll::before { content: '左右滑动查看完整表格 →'; position: sticky; left: 0; display: block; width: fit-content; margin-bottom: .6rem; font: .8rem/1.6 var(--serif-font); opacity: .8; }
}
