* {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
}

body {
  font-family: 'Iowan Old Style', 'Noto Serif TC', 'Noto Serif', 'Palatino Linotype', 'Palatino', 'Book Antiqua', Georgia, 'Times New Roman', serif;
  font-size: calc(1rem * var(--font-size-scale, 1.125));
  line-height: 1.7;
  color: var(--text-color);
  background-color: var(--bg-color);
  padding: 2rem 1rem;
  position: relative;
  transition: background-color 0.3s ease, color 0.3s ease;
  overflow-x: hidden;
}

/* CJK (Chinese, Japanese, Korean) typography adjustments */
html:lang(zh) body,
html:lang(zh-tw) body,
html:lang(zh-cn) body,
html:lang(ja) body,
html:lang(ko) body {
  letter-spacing: 0.08em;
  line-height: 1.9;
}

/* Only enable font-size transition after initial load */
body.font-size-transition-enabled {
  transition: background-color 0.3s ease, color 0.3s ease, font-size 0.3s ease;
}

.container {
  max-width: 700px;
  margin: 0 auto;
}

/* Sticky back navigation */
.sticky-nav {
  position: sticky;
  top: 0;
  z-index: 100;
  background: var(--bg-color);
  padding: 0.75rem 0;
  margin: -0.75rem 0 1rem 0;
  border-bottom: 1px solid transparent;
  transition: border-color 0.2s ease, background-color 0.3s ease;
}

.sticky-nav.scrolled {
  border-bottom-color: var(--table-border-color, #e5e7eb);
}

/* On smaller screens, move down to avoid font-size controls */
@media (max-width: 800px) {
  .sticky-nav {
    top: 0;
    padding-top: 4.25rem;
  }
}

@media (max-width: 480px) {
  .sticky-nav {
    top: 0;
    padding-top: 3.75rem;
  }
}

.back-link {
  display: inline-block;
  color: var(--text-meta-color);
  text-decoration: none;
  font-size: 0.9rem;
  transition: color 0.2s ease;
}

.back-link:hover {
  color: var(--link-hover-color, #2563eb);
}

.back-link::before {
  content: "← ";
}

article {
  margin-top: 2rem;
}

h1 {
  font-size: 2rem;
  margin-bottom: 0.5rem;
  font-weight: 600;
}

.meta {
  color: var(--text-meta-color);
  font-size: 0.9rem;
  margin-bottom: 2rem;
}

article p {
  margin-bottom: 1rem;
}

article h2 {
  font-size: 1.5rem;
  margin-top: 2rem;
  margin-bottom: 1rem;
  font-weight: 600;
}

article h3 {
  font-size: 1.25rem;
  margin-top: 1.5rem;
  margin-bottom: 0.75rem;
  font-weight: 600;
}

article ul,
article ol {
  margin-left: 0;
  padding-left: calc(2.5rem * var(--font-size-scale, 1));
  margin-bottom: 1rem;
  list-style-position: outside;
}

article li {
  margin-bottom: 0.5rem;
  padding-left: calc(0.5rem * var(--font-size-scale, 1));
  overflow-wrap: break-word;
  word-wrap: break-word;
  margin-left: 0;
}

/* Nested ordered list numbering styles: 1 -> a -> i */
article ol {
  list-style-type: decimal;
}

article ol ol {
  list-style-type: lower-alpha;
}

article ol ol ol {
  list-style-type: lower-roman;
}

article ol ol ol ol {
  list-style-type: decimal;
}

article ol ol ol ol ol {
  list-style-type: lower-alpha;
}

article ol ol ol ol ol ol {
  list-style-type: lower-roman;
}

article a {
  color: var(--link-color);
  text-decoration: none;
  border-bottom: 1px solid var(--link-decoration-color);
  padding-bottom: 1px;
  transition: color 0.2s, border-color 0.2s;
}

/* Make unvisited links darker */
article a:link {
  color: var(--link-unvisited-color, var(--link-color));
  border-bottom-color: var(--link-decoration-unvisited-color, var(--link-decoration-color));
}

article a:hover {
  color: var(--link-hover-color);
  border-bottom-color: var(--link-decoration-hover-color);
}

article a:visited {
  color: var(--link-visited-color);
  border-bottom-color: var(--link-decoration-visited-color);
}

article a:visited:hover {
  color: var(--link-hover-color);
  border-bottom-color: var(--link-decoration-hover-color);
}

/* External link indicator for links that open in a new tab */
article a[target="_blank"] {
  position: relative;
}

article a[target="_blank"]::after {
  content: "↗";
  display: inline-block;
  font-size: 0.65em;
  font-weight: 900;
  opacity: 0.4;
  margin-left: 0.15em;
  vertical-align: super;
  line-height: 0;
  transition: opacity 0.2s;
}

article a[target="_blank"]:hover::after {
  opacity: 0.6;
}

/* Blockquote styles */
article blockquote {
  border-left: 4px solid var(--blockquote-border-color);
  padding-left: 1.5rem;
  padding-right: 1rem;
  padding-top: 0.5rem;
  padding-bottom: 0.5rem;
  margin: 1.5rem 0;
  background-color: var(--blockquote-bg);
  color: var(--blockquote-text-color);
  font-style: italic;
  border-radius: 0 4px 4px 0;
  transition: border-color 0.3s ease, background-color 0.3s ease, color 0.3s ease;
}

article blockquote p {
  margin-bottom: 0.5rem;
}

article blockquote p:last-child {
  margin-bottom: 0;
}

/* Code block styles */
pre {
  border: 1px solid var(--code-border-color);
  border-radius: 6px;
  padding: 16px;
  overflow-x: auto;
  margin-bottom: 1rem;
  position: relative;
  background-color: var(--bg-color);
  transition: border-color 0.3s ease, background-color 0.3s ease;
}

/* Copy button for code blocks */
.code-copy-btn {
  position: absolute;
  top: 8px;
  right: 12px;
  background: var(--code-label-bg);
  border: 1px solid var(--code-border-color);
  border-radius: 4px;
  padding: 4px 8px;
  cursor: pointer;
  font-size: 0.7rem;
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Oxygen, Ubuntu, Cantarell, sans-serif;
  font-weight: 500;
  color: var(--code-label-color);
  opacity: 0;
  transition: opacity 0.2s ease, background-color 0.2s ease, color 0.2s ease, transform 0.15s ease;
  display: flex;
  align-items: center;
  gap: 4px;
  z-index: 10;
}

pre:hover .code-copy-btn {
  opacity: 1;
}

.code-copy-btn:hover {
  background-color: var(--inline-code-bg);
  color: var(--text-color);
}

.code-copy-btn:active {
  transform: scale(0.95);
}

.code-copy-btn.copied {
  color: #22c55e;
  border-color: #22c55e;
}

.code-copy-btn svg {
  width: 14px;
  height: 14px;
  stroke: currentColor;
  stroke-width: 2;
  fill: none;
}

/* Adjust language label position when copy button is present */
pre:has(.code-copy-btn) .code-language-label {
  right: auto;
  left: 12px;
}

canvas {
  display: block;
  margin: 1.5rem auto;
  border: 1px solid var(--code-border-color);
  border-radius: 6px;
}

.example-canvas {
  width: 100%;
  aspect-ratio: 4 / 2;
}

.code-language-label {
  position: absolute;
  top: 8px;
  right: 12px;
  font-size: 0.75rem;
  color: var(--code-label-color);
  background: var(--code-label-bg);
  padding: 2px 8px;
  border-radius: 4px;
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Oxygen, Ubuntu, Cantarell, sans-serif;
  text-transform: uppercase;
  font-weight: 500;
  letter-spacing: 0.5px;
  transition: color 0.3s ease, background 0.3s ease;
}

code {
  font-family: 'SF Mono', Monaco, 'Cascadia Code', 'Roboto Mono', Consolas, 'Courier New', monospace;
  font-size: 85%;
}

/* Inline code (single backtick) styling */
code:not(pre code) {
  background-color: var(--inline-code-bg);
  padding: 0.2em 0.4em;
  border-radius: 3px;
  transition: background-color 0.3s ease;
}

.text-emphasis {
  position: relative;
  display: inline-block;
  color: var(--emphasis-text-color);
  padding: 0.1em 0.35em;
  font-weight: 600;
  letter-spacing: 0.01em;
  z-index: 0;
}

.text-emphasis::before {
  content: '';
  position: absolute;
  left: -0.3em;
  right: -0.35em;
  bottom: -0.25em;
  height: 65%;
  background: var(--emphasis-bg);
  border-radius: 999px;
  z-index: -1;
  clip-path: polygon(3% 55%, 12% 40%, 28% 48%, 42% 38%, 60% 50%, 78% 42%, 95% 55%, 92% 72%, 74% 68%, 58% 82%, 37% 72%, 18% 84%, 6% 73%);
  transform: rotate(-1.75deg) translateY(0.05em);
  transition: background 0.3s ease, transform 0.3s ease;
}

.remark {
  margin: 1.5rem 0;
  padding: 1rem 1.25rem;
  background-color: var(--remark-bg);
  border-left: 3px solid var(--remark-border-color);
  border-radius: 0.5rem;
  color: var(--remark-text-color);
  font-style: normal;
  line-height: 1.6;
  transition: background-color 0.3s ease, color 0.3s ease, border-color 0.3s ease;
}

.remark strong {
  color: inherit;
  font-weight: 700;
}

.remark code {
  background-color: var(--remark-inline-code-bg);
  color: inherit;
}

.tldr {
  margin: 2rem 0;
  padding: 1.25rem 1.5rem;
  background-color: var(--tldr-bg);
  border-left: 4px solid var(--tldr-border-color);
  border-radius: 0.5rem;
  color: var(--tldr-text-color);
  font-style: normal;
  line-height: 1.7;
  transition: background-color 0.3s ease, color 0.3s ease, border-color 0.3s ease;
}

.tldr > *:first-child {
  margin-top: 0;
}

.tldr > *:last-child {
  margin-bottom: 0;
}

.tldr h2,
.tldr h3,
.tldr h4 {
  color: var(--tldr-title-color);
  margin-top: 1rem;
  margin-bottom: 0.75rem;
  font-weight: 600;
}

.tldr h2:first-child,
.tldr h3:first-child,
.tldr h4:first-child {
  margin-top: 0;
}

.tldr strong {
  color: inherit;
  font-weight: 700;
}

.tldr code {
  background-color: var(--tldr-inline-code-bg);
  color: inherit;
}

pre code {
  background: transparent;
  padding: 0;
  border: none;
  display: block;
}

/* Image shadow class */
.image-shadow {
  box-shadow: var(--image-shadow, 0 4px 20px rgba(0, 0, 0, 0.15), 0 2px 6px rgba(0, 0, 0, 0.1));
  transition: box-shadow 0.3s ease;
}

/* Image rounded corners class */
.image-rounded {
  border-radius: 8px;
  overflow: hidden;
}

/* Image caption class */
.image-caption {
  display: block;
  text-align: center;
  font-size: 0.875rem;
  color: var(--text-meta-color);
  font-style: italic;
  margin-top: 0.5rem;
  margin-bottom: 1.5rem;
  line-height: 1.5;
  transition: color 0.3s ease;
}

/* Table styles */
article table {
  width: 100%;
  border-collapse: collapse;
  margin-bottom: 1rem;
}

article table thead {
  background-color: var(--table-header-bg);
}

article table th {
  padding: 12px 16px;
  text-align: left;
  font-weight: 600;
  border-bottom: 2px solid var(--table-border-color);
  color: var(--text-color);
}

article table td {
  padding: 12px 16px;
  border-bottom: 1px solid var(--table-border-color);
}

article table tbody tr:hover {
  background-color: var(--table-row-hover-bg);
  transition: background-color 0.2s ease;
}

article table tbody tr:last-child td {
  border-bottom: none;
}

/* Increase font sizes for 4K and high-resolution displays */
@media (min-width: 2560px) {
  h1 {
    font-size: 2.5rem;
  }

  article h2 {
    font-size: 2rem;
  }

  article h3 {
    font-size: 1.75rem;
  }

  .meta {
    font-size: 1rem;
  }

  code {
    font-size: 90%;
  }

  .image-caption {
    font-size: 1rem;
  }
}

@media (max-width: 980px) {
  body {
    /* Add extra top padding to account for fixed controls (font size, language, dark mode) */
    padding: 5rem 0.5rem 1rem 0.5rem;
  }
  
  .container {
    /* Ensure container respects the body padding */
    margin-top: 0;
  }
  
  h1 {
    font-size: 1.5rem;
  }
}

/* Apply top padding when window height is insufficient for content + controls */
@media (max-height: 600px) {
  body {
    /* Add extra top padding to account for fixed controls when window height is small */
    padding-top: 5rem;
  }
  
  .container {
    /* Ensure container respects the body padding */
    margin-top: 0;
  }
}

/* Image Viewer / Lightbox */
.image-viewer-overlay {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background-color: rgba(0, 0, 0, 0.9);
  z-index: 10000;
  display: none;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  opacity: 0;
  transition: opacity 0.3s ease;
}

.image-viewer-overlay.show {
  display: flex;
  opacity: 1;
}

.image-viewer-close {
  position: absolute;
  top: 1rem;
  right: 1rem;
  background: rgba(255, 255, 255, 0.1);
  border: none;
  color: white;
  font-size: 2rem;
  width: 3rem;
  height: 3rem;
  border-radius: 50%;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: background-color 0.2s ease, transform 0.2s ease;
  z-index: 10001;
}

.image-viewer-close:hover {
  background: rgba(255, 255, 255, 0.2);
  transform: scale(1.1);
}

.image-viewer-container {
  flex: 1;
  width: 100%;
  display: flex;
  align-items: center;
  justify-content: center;
  overflow: hidden;
  cursor: zoom-out;
}

.image-viewer-img {
  max-width: 90%;
  max-height: 85vh;
  object-fit: contain;
  cursor: grab;
  user-select: none;
  -webkit-user-drag: none;
  will-change: transform;
  transform-origin: center center;
  -webkit-transform-origin: center center;
  touch-action: none;
}

.image-viewer-img.dragging {
  cursor: grabbing;
  transition: none !important;
}

.image-viewer-img:not(.dragging) {
  transition: transform 0.15s ease-out;
}

.image-viewer-img:active {
  cursor: grabbing;
}

.image-viewer-controls {
  position: absolute;
  bottom: 1.5rem;
  left: 50%;
  transform: translateX(-50%);
  display: flex;
  align-items: center;
  gap: 0.5rem;
  background: rgba(0, 0, 0, 0.6);
  padding: 0.5rem 1rem;
  border-radius: 2rem;
  backdrop-filter: blur(10px);
  -webkit-backdrop-filter: blur(10px);
}

.image-viewer-btn {
  background: rgba(255, 255, 255, 0.1);
  border: none;
  color: white;
  font-size: 1rem;
  padding: 0.5rem 0.75rem;
  border-radius: 0.5rem;
  cursor: pointer;
  transition: background-color 0.2s ease;
  font-family: system-ui, -apple-system, sans-serif;
}

.image-viewer-btn:hover {
  background: rgba(255, 255, 255, 0.25);
}

.image-viewer-zoom-level {
  color: white;
  font-size: 0.875rem;
  min-width: 3rem;
  text-align: center;
  font-family: system-ui, -apple-system, sans-serif;
}

/* Zoomable image indicator */
img[data-zoomable] {
  cursor: zoom-in;
  transition: transform 0.2s ease, box-shadow 0.2s ease;
}

img[data-zoomable]:hover {
  transform: scale(1.02);
  box-shadow: 0 8px 25px rgba(0, 0, 0, 0.2);
}

/* Mobile adjustments for image viewer */
@media (max-width: 600px) {
  .image-viewer-close {
    top: 0.75rem;
    right: 0.75rem;
    width: 2.5rem;
    height: 2.5rem;
    font-size: 1.5rem;
  }

  .image-viewer-controls {
    bottom: 1rem;
    padding: 0.4rem 0.75rem;
    gap: 0.4rem;
  }

  .image-viewer-btn {
    padding: 0.4rem 0.6rem;
    font-size: 0.875rem;
  }

  .image-viewer-zoom-level {
    font-size: 0.75rem;
    min-width: 2.5rem;
  }

  .image-viewer-img {
    max-width: 95%;
    max-height: 80vh;
  }
}

/* Table of Contents Sidebar */
.toc-sidebar {
  position: fixed;
  left: 1.5rem;
  top: 50%;
  transform: translateY(-50%);
  width: 240px;
  max-height: 60vh;
  overflow-y: auto;
  z-index: 50;
  display: none;
  
  /* Custom scrollbar */
  scrollbar-width: thin;
  scrollbar-color: var(--table-border-color, rgba(0, 0, 0, 0.2)) transparent;
}

/* Webkit scrollbar styles */
.toc-sidebar::-webkit-scrollbar {
  width: 4px;
}

.toc-sidebar::-webkit-scrollbar-track {
  background: transparent;
}

.toc-sidebar::-webkit-scrollbar-thumb {
  background-color: var(--table-border-color, rgba(0, 0, 0, 0.2));
  border-radius: 4px;
}

.toc-sidebar::-webkit-scrollbar-thumb:hover {
  background-color: var(--text-meta-color);
}

.toc-nav {
  padding: 1rem;
  background-color: var(--bg-color);
  border-radius: 8px;
  border: 1px solid var(--table-border-color, rgba(0, 0, 0, 0.1));
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.05);
  transition: background-color 0.3s ease, border-color 0.3s ease;
}

.toc-title {
  font-size: calc(0.7rem * var(--font-size-scale, 1));
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.05em;
  color: var(--text-meta-color);
  margin-bottom: 0.75rem;
  padding-bottom: 0.5rem;
  border-bottom: 1px solid var(--table-border-color, rgba(0, 0, 0, 0.1));
}

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

.toc-item {
  margin: 0;
  padding: 0;
}

.toc-link {
  display: block;
  padding: 0.35rem 0;
  font-size: calc(0.75rem * var(--font-size-scale, 1));
  color: var(--text-meta-color);
  text-decoration: none;
  border-left: 2px solid transparent;
  padding-left: 0.75rem;
  margin-left: -0.75rem;
  transition: color 0.2s ease, border-color 0.2s ease, font-size 0.3s ease;
  line-height: 1.4;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.toc-link:hover {
  color: var(--text-color);
}

.toc-link.active {
  color: var(--text-color);
  border-left-color: var(--text-color);
  font-weight: 500;
}

/* Indent based on heading level */
.toc-h2 .toc-link {
  padding-left: 1.25rem;
  margin-left: -0.75rem;
}

.toc-h3 .toc-link {
  padding-left: 1.75rem;
  margin-left: -0.75rem;
  font-size: calc(0.7rem * var(--font-size-scale, 1));
}

/* Hide TOC sidebar on smaller screens where there's no room */
@media (max-width: 1200px) {
  .toc-sidebar {
    display: none !important;
  }
}

/* Mobile TOC */
.toc-mobile {
  display: none;
  position: fixed;
  bottom: 1.5rem;
  right: 1.5rem;
  z-index: 100;
}

.toc-mobile-btn {
  width: 3rem;
  height: 3rem;
  border-radius: 50%;
  background-color: var(--bg-color);
  border: 1px solid var(--table-border-color, rgba(0, 0, 0, 0.1));
  box-shadow: 0 2px 12px rgba(0, 0, 0, 0.15);
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  color: var(--text-color);
  transition: background-color 0.2s ease, box-shadow 0.2s ease, transform 0.2s ease;
}

.toc-mobile-btn:hover {
  box-shadow: 0 4px 16px rgba(0, 0, 0, 0.2);
  transform: scale(1.05);
}

.toc-mobile-btn:active {
  transform: scale(0.95);
}

.toc-mobile-btn svg {
  width: 1.25rem;
  height: 1.25rem;
}

.toc-mobile-dropdown {
  position: absolute;
  bottom: calc(100% + 0.75rem);
  right: 0;
  width: 280px;
  max-height: 60vh;
  background-color: var(--bg-color);
  border: 1px solid var(--table-border-color, rgba(0, 0, 0, 0.1));
  border-radius: 12px;
  box-shadow: 0 4px 24px rgba(0, 0, 0, 0.15);
  display: none;
  flex-direction: column;
  overflow: hidden;
  transition: background-color 0.3s ease, border-color 0.3s ease;
}

.toc-mobile-dropdown.show {
  display: flex;
}

.toc-mobile-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0.75rem 1rem;
  border-bottom: 1px solid var(--table-border-color, rgba(0, 0, 0, 0.1));
}

.toc-mobile-title {
  font-size: calc(0.8rem * var(--font-size-scale, 1));
  font-weight: 600;
  color: var(--text-color);
}

.toc-mobile-close {
  background: none;
  border: none;
  font-size: 1.5rem;
  color: var(--text-meta-color);
  cursor: pointer;
  padding: 0;
  line-height: 1;
  transition: color 0.2s ease;
}

.toc-mobile-close:hover {
  color: var(--text-color);
}

.toc-mobile-list {
  list-style: none;
  padding: 0.5rem 0;
  margin: 0;
  overflow-y: auto;
  
  /* Custom scrollbar */
  scrollbar-width: thin;
  scrollbar-color: var(--table-border-color, rgba(0, 0, 0, 0.2)) transparent;
}

.toc-mobile-list::-webkit-scrollbar {
  width: 4px;
}

.toc-mobile-list::-webkit-scrollbar-track {
  background: transparent;
}

.toc-mobile-list::-webkit-scrollbar-thumb {
  background-color: var(--table-border-color, rgba(0, 0, 0, 0.2));
  border-radius: 4px;
}

.toc-mobile-item {
  margin: 0;
  padding: 0;
}

.toc-mobile-link {
  display: block;
  padding: 0.6rem 1rem;
  font-size: calc(0.85rem * var(--font-size-scale, 1));
  color: var(--text-meta-color);
  text-decoration: none;
  transition: background-color 0.2s ease, color 0.2s ease;
  border-left: 3px solid transparent;
}

.toc-mobile-link:hover {
  background-color: rgba(0, 0, 0, 0.03);
  color: var(--text-color);
}

html:has(#dark-mode-toggle:checked) .toc-mobile-link:hover {
  background-color: rgba(255, 255, 255, 0.05);
}

.toc-mobile-link.active {
  color: var(--text-color);
  border-left-color: var(--text-color);
  font-weight: 500;
}

/* Indent based on heading level */
.toc-mobile-h2 .toc-mobile-link {
  padding-left: 1.5rem;
}

.toc-mobile-h3 .toc-mobile-link {
  padding-left: 2rem;
  font-size: calc(0.8rem * var(--font-size-scale, 1));
}

/* Show mobile TOC only on smaller screens */
@media (max-width: 1200px) {
  .toc-mobile {
    display: block;
  }
}

/* Adjust position on very small screens */
@media (max-width: 480px) {
  .toc-mobile {
    bottom: 1rem;
    right: 1rem;
  }
  
  .toc-mobile-btn {
    width: 2.75rem;
    height: 2.75rem;
  }
  
  .toc-mobile-dropdown {
    width: calc(100vw - 2rem);
    max-width: 320px;
  }
}

