:root {
  --bg: #fdfdfd;
  --text: #222;
  --muted: #666;
  --rule: rgba(0, 0, 0, 0.12);

  --link: #2b59c3;
  --link-underline: rgba(43, 89, 195, 0.25);
  --link-underline-hover: rgba(43, 89, 195, 0.7);

  --panel: rgba(0, 0, 0, 0.03);
  --shadow: rgba(0, 0, 0, 0.12);
  --code-bg: #f6f6f6;
  --code-border: rgba(0, 0, 0, 0.12);
  --overlay-bg: rgba(253, 253, 253, 0.85);

  --progress: #2b59c3;
}

:root[data-theme="dark"] {
  --bg: #0f1115;
  --text: #e9e9e9;
  --muted: rgba(233, 233, 233, 0.7);
  --rule: rgba(255, 255, 255, 0.16);

  --link: #8ab4f8;
  --link-underline: rgba(138, 180, 248, 0.28);
  --link-underline-hover: rgba(138, 180, 248, 0.75);

  --panel: rgba(255, 255, 255, 0.06);
  --shadow: rgba(0, 0, 0, 0.42);
  --code-bg: rgba(255, 255, 255, 0.06);
  --code-border: rgba(255, 255, 255, 0.12);
  --overlay-bg: rgba(15, 17, 21, 0.88);

  --progress: #8ab4f8;
}

* {
  box-sizing: border-box;
}

html {
  font-size: 18px;
  -webkit-text-size-adjust: 100%;
  text-size-adjust: 100%;
}

@media (prefers-reduced-motion: no-preference) {
  html {
    scroll-behavior: smooth;
  }
}

body {
  margin: 0;
  background: var(--bg);
  color: var(--text);

  /* LaTeX-style serif body */
  font-family:
    "Computer Modern Serif",
    "CMU Serif",
    "Latin Modern Roman",
    "KaTeX_Main",
    Georgia,
    "Times New Roman",
    serif;

  line-height: 1.7;
  font-kerning: normal;
  text-rendering: optimizeLegibility;
}

.wrap {
  max-width: 680px;
  margin: 0 auto;
  padding: 0 1.25rem;
}

.wrap-wide {
  max-width: 1040px;
  margin: 0 auto;
  padding: 0 1.25rem;
}

.visually-hidden {
  position: absolute;
  width: 1px;
  height: 1px;
  padding: 0;
  margin: -1px;
  overflow: hidden;
  clip: rect(0, 0, 0, 0);
  white-space: nowrap;
  border: 0;
}

.header-row {
  display: flex;
  align-items: baseline;
  justify-content: space-between;
  gap: 1rem;
}

.header-actions {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  min-width: 0;
}

.site-header {
  padding: 1.25rem 0;
  border-bottom: 1px solid var(--rule);
}

.header-search-field {
  flex: 0 1 0;
  max-width: 0;
  opacity: 0;
  overflow: hidden;
  pointer-events: none;
  transform: translateY(-1px);
  transition:
    max-width 260ms ease,
    opacity 180ms ease,
    transform 260ms ease;
}

.site-header.search-open .header-search-field {
  flex: 1 1 auto;
  max-width: min(22rem, 52vw);
  opacity: 1;
  pointer-events: auto;
  transform: translateY(0);
}

.header-search-inputwrap {
  position: relative;
  width: 100%;
}

.header-search-input {
  width: 100%;
  min-width: 0;
  padding: 0.45rem 2.05rem 0.45rem 0.65rem;
  border-radius: 999px;
  border: 1px solid var(--rule);
  background: transparent;
  color: var(--text);
  font: inherit;
  line-height: 1.2;
  transition:
    background 180ms ease,
    border-color 180ms ease,
    box-shadow 180ms ease;
}

.header-search-input::-webkit-search-cancel-button,
.header-search-input::-webkit-search-decoration {
  -webkit-appearance: none;
  appearance: none;
  display: none;
}

.header-search-input::-ms-clear {
  display: none;
}

.header-search-input:hover {
  background: var(--panel);
  border-color: rgba(0, 0, 0, 0.22);
}

:root[data-theme="dark"] .header-search-input:hover {
  border-color: rgba(255, 255, 255, 0.24);
}

.header-search-input::placeholder {
  color: var(--muted);
}

.header-search-input:focus {
  outline: none;
  border-color: rgba(43, 89, 195, 0.55);
  box-shadow: 0 0 0 3px rgba(43, 89, 195, 0.16);
}

:root[data-theme="dark"] .header-search-input:focus {
  border-color: rgba(138, 180, 248, 0.55);
  box-shadow: 0 0 0 3px rgba(138, 180, 248, 0.16);
}

.header-search-clear {
  position: absolute;
  top: 50%;
  right: 0.35rem;
  transform: translateY(-50%);
  width: 1.7rem;
  height: 1.7rem;
  border: 0;
  border-radius: 999px;
  background: transparent;
  color: var(--muted);
  cursor: pointer;
  opacity: 0;
  pointer-events: none;
  transition:
    opacity 150ms ease,
    background 150ms ease,
    color 150ms ease;
}

.header-search-input:not(:placeholder-shown) + .header-search-clear {
  opacity: 1;
  pointer-events: auto;
}

.header-search-clear:hover {
  background: var(--panel);
  color: var(--text);
}

.site-header.search-has-query #searchToggle {
  border-color: rgba(43, 89, 195, 0.45);
  background: rgba(43, 89, 195, 0.06);
}

:root[data-theme="dark"] .site-header.search-has-query #searchToggle {
  border-color: rgba(138, 180, 248, 0.5);
  background: rgba(138, 180, 248, 0.1);
}

.header-search-input.placeholder-animate::placeholder {
  animation: placeholderPulse 1.7s ease-in-out infinite;
}

@keyframes placeholderPulse {
  0%,
  100% {
    opacity: 0.55;
  }
  50% {
    opacity: 0.95;
  }
}

.site-title {
  color: var(--text);
  text-decoration: none;
  border-bottom: 0;
  font-size: 1.05rem;
  letter-spacing: 0.02em;
}

main.wrap,
main.wrap-wide {
  padding-top: 2.75rem;
  padding-bottom: 3.25rem;
}

h1,
h2,
h3,
h4 {
  line-height: 1.25;
  margin: 2.2rem 0 1rem;
  font-weight: 600;
}

h1 {
  font-size: 2rem;
  margin-top: 0;
}

h2 {
  font-size: 1.5rem;
}

h3 {
  font-size: 1.2rem;
}

p {
  margin: 1.1rem 0;
}

ul,
ol {
  padding-left: 1.2rem;
}

li {
  margin: 0.35rem 0;
}

a {
  color: var(--link);
  text-decoration: none;
  border-bottom: 1px solid var(--link-underline);
}

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

.muted {
  color: var(--muted);
  font-size: 0.95rem;
}

.icon-button {
  appearance: none;
  border: 1px solid var(--rule);
  background: transparent;
  color: var(--text);
  border-radius: 10px;
  padding: 0.35rem 0.55rem;
  font: inherit;
  line-height: 1;
  cursor: pointer;
}

.icon-button:hover {
  background: var(--panel);
}

.text-button {
  appearance: none;
  border: 1px solid var(--rule);
  background: transparent;
  color: var(--text);
  border-radius: 999px;
  padding: 0.35rem 0.7rem;
  font: inherit;
  cursor: pointer;
  border-bottom: 0;
}

.text-button:hover {
  background: var(--panel);
}

/* Homepage controls */
.home-controls {
  margin-top: 1.5rem;
  padding: 0.9rem 1rem;
  border: 1px solid var(--rule);
  border-radius: 14px;
  background: var(--panel);
}

.search input[type="search"] {
  width: 100%;
  padding: 0.65rem 0.75rem;
  border-radius: 12px;
  border: 1px solid var(--rule);
  background: transparent;
  color: var(--text);
  font: inherit;
}

.search input[type="search"]::placeholder {
  color: var(--muted);
}

.tag-filters-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 0.75rem;
  margin-top: 0;
  flex-wrap: wrap;
}

.tag-filters {
  display: flex;
  gap: 0.45rem;
  flex-wrap: wrap;
}

.tag-pill {
  border: 1px solid var(--rule);
  background: transparent;
  color: var(--text);
  border-radius: 999px;
  padding: 0.25rem 0.6rem;
  font: inherit;
  font-size: 0.92rem;
  cursor: pointer;
}

.tag-pill:hover {
  background: rgba(0, 0, 0, 0.02);
}

:root[data-theme="dark"] .tag-pill:hover {
  background: rgba(255, 255, 255, 0.06);
}

.tag-pill[data-selected="true"] {
  border-color: rgba(43, 89, 195, 0.55);
  background: rgba(43, 89, 195, 0.12);
}

:root[data-theme="dark"] .tag-pill[data-selected="true"] {
  border-color: rgba(138, 180, 248, 0.55);
  background: rgba(138, 180, 248, 0.12);
}

/* Post list */
.post-list {
  list-style: none;
  padding: 0;
  margin: 1.5rem 0 0;
}

.post-item {
  padding: 1.25rem 0;
  border-bottom: 1px solid var(--rule);
}

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

.post-link {
  font-size: 1.15rem;
}

.post-excerpt {
  color: var(--muted);
  margin-top: 0.55rem;
}

.post-tags {
  display: flex;
  gap: 0.35rem;
  flex-wrap: wrap;
  margin-top: 0.6rem;
}

.post-tag {
  border: 1px solid var(--rule);
  border-radius: 999px;
  padding: 0.15rem 0.55rem;
  font-size: 0.9rem;
  color: var(--muted);
  background: transparent;
}

/* Post page layout */
.post-layout {
  display: grid;
  grid-template-columns: minmax(0, 680px) 280px;
  justify-content: space-between;
  gap: 3rem;
  align-items: start;
}

.toc-panel {
  position: sticky;
  top: 5.25rem;
  max-height: calc(100vh - 6.5rem);
  overflow: auto;
  padding-left: 1.25rem;
  border-left: 1px solid var(--rule);
}

.toc-title {
  font-size: 0.95rem;
  color: var(--muted);
  margin: 0 0 0.65rem;
}

.toc {
  font-size: 0.95rem;
}

.toc a {
  display: block;
  padding: 0.22rem 0.35rem;
  border-bottom: 0;
  color: rgba(0, 0, 0, 0.62);
  border-radius: 10px;
  transition:
    color 160ms ease,
    background 160ms ease;
}

:root[data-theme="dark"] .toc a {
  color: rgba(233, 233, 233, 0.72);
}

.toc a:hover {
  color: var(--text);
  background: var(--panel);
}

.toc a.active {
  color: var(--link);
  background: rgba(43, 89, 195, 0.06);
}

.toc a.active:hover {
  background: rgba(43, 89, 195, 0.1);
}

:root[data-theme="dark"] .toc a.active {
  background: rgba(138, 180, 248, 0.1);
}

:root[data-theme="dark"] .toc a.active:hover {
  background: rgba(138, 180, 248, 0.14);
}

.toc .toc-level-2 {
  padding-left: 0.65rem;
}

.toc .toc-level-3 {
  padding-left: 1.2rem;
}

.toc .toc-level-4 {
  padding-left: 1.75rem;
}

.toc .toc-level-5 {
  padding-left: 2.3rem;
}

.toc .toc-level-6 {
  padding-left: 2.85rem;
}

/* Reading progress bar (top) */
.progress {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  height: 3px;
  z-index: 100;
  background: transparent;
}

.progress-bar {
  height: 100%;
  width: 0%;
  background: var(--progress);
}

/* Markdown content styling */
.markdown :is(h1, h2, h3, h4, h5, h6) {
  scroll-margin-top: 5.75rem;
}

.heading-anchor {
  opacity: 0;
  margin-left: 0.35rem;
  border-bottom: 0;
  color: var(--muted);
  font-size: 0.95em;
  text-decoration: none;
}

.markdown :is(h1, h2, h3, h4):hover .heading-anchor {
  opacity: 1;
}

.heading-anchor::before {
  content: attr(data-icon);
}

figure {
  margin: 2rem 0;
}

figure > img,
figure > svg {
  display: block;
  max-width: 100%;
  height: auto;
  margin: 0 auto;
  border: 1px solid var(--rule);
  background: #fff;
}

:is(figure > img, .markdown img):not(.no-zoom) {
  cursor: zoom-in;
}

:root[data-theme="dark"] figure > img,
:root[data-theme="dark"] figure > svg {
  background: rgba(255, 255, 255, 0.04);
}

figcaption {
  margin-top: 0.75rem;
  color: var(--muted);
  font-size: 0.95rem;
  font-style: italic;
  text-align: center;
}

.image-lightbox[hidden] {
  display: none;
}

.image-lightbox {
  position: fixed;
  inset: 0;
  z-index: 200;
}

.image-lightbox-backdrop {
  position: absolute;
  inset: 0;
  background: rgba(0, 0, 0, 0.55);
}

:root[data-theme="dark"] .image-lightbox-backdrop {
  background: rgba(0, 0, 0, 0.72);
}

.image-lightbox-content {
  position: relative;
  width: min(1100px, calc(100vw - 2rem));
  margin: 6vh auto 0;
  padding: 0.85rem 0.85rem 0.9rem;
  border-radius: 16px;
  border: 1px solid var(--rule);
  background: var(--overlay-bg);
  box-shadow: 0 18px 60px var(--shadow);
}

.image-lightbox-close {
  position: absolute;
  top: 0.6rem;
  right: 0.6rem;
  appearance: none;
  border: 1px solid var(--rule);
  background: transparent;
  color: var(--text);
  border-radius: 10px;
  padding: 0.28rem 0.5rem;
  font: inherit;
  line-height: 1;
  cursor: pointer;
}

.image-lightbox-close:hover {
  background: var(--panel);
}

#imageLightboxImg {
  display: block;
  max-width: 100%;
  max-height: 78vh;
  margin: 0 auto;
  height: auto;
  cursor: zoom-out;
}

.image-lightbox-caption {
  margin-top: 0.65rem;
  text-align: center;
  color: var(--muted);
  font-size: 0.95rem;
  font-style: italic;
}

body.lightbox-open {
  overflow: hidden;
}

blockquote {
  margin: 1.6rem 0;
  padding: 0.2rem 0 0.2rem 1rem;
  border-left: 3px solid var(--rule);
  color: rgba(0, 0, 0, 0.82);
}

:root[data-theme="dark"] blockquote {
  color: rgba(233, 233, 233, 0.86);
}

details {
  margin: 1.2rem 0;
  border: 1px solid var(--rule);
  border-radius: 12px;
  padding: 0.6rem 0.8rem;
  background: var(--panel);
}

details > summary {
  cursor: pointer;
  color: var(--text);
  font-weight: 600;
}

table {
  width: 100%;
  border-collapse: collapse;
  margin: 1.5rem 0;
  font-size: 0.98rem;
}

thead th {
  text-align: left;
  border-bottom: 1px solid var(--rule);
  padding: 0.45rem 0.4rem;
}

tbody td {
  border-bottom: 1px solid var(--rule);
  padding: 0.45rem 0.4rem;
  vertical-align: top;
}

code,
pre {
  font-family:
    ui-monospace,
    SFMono-Regular,
    Menlo,
    Monaco,
    Consolas,
    "Liberation Mono",
    "Courier New",
    monospace;
  font-size: 0.95em;
}

:not(pre) > code {
  padding: 0.08rem 0.35rem;
  border-radius: 8px;
  border: 1px solid var(--code-border);
  background: var(--code-bg);
}

pre {
  position: relative;
  background: var(--code-bg);
  padding: 1rem;
  overflow-x: auto;
  border-radius: 12px;
  border: 1px solid var(--code-border);
}

pre code {
  background: transparent;
  padding: 0;
}

.code-copy {
  position: absolute;
  top: 0.55rem;
  right: 0.55rem;
  border: 1px solid var(--rule);
  background: var(--overlay-bg);
  color: var(--text);
  border-radius: 10px;
  padding: 0.25rem 0.5rem;
  font: inherit;
  font-size: 0.9rem;
  cursor: pointer;
  opacity: 0;
}

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

hr {
  border: 0;
  border-top: 1px solid var(--rule);
  margin: 2rem 0;
}

.katex-display {
  margin: 1.4rem 0;
}

.footnotes {
  margin-top: 2.25rem;
  padding-top: 1.25rem;
  border-top: 1px solid var(--rule);
}

.footnotes ol {
  padding-left: 1.25rem;
}

.footnotes li {
  margin: 0.6rem 0;
}

/* Post footer navigation */
.post-footer {
  margin-top: 2.5rem;
}

.post-pagination {
  display: flex;
  justify-content: space-between;
  gap: 1rem;
  border-top: 1px solid var(--rule);
  padding-top: 1.25rem;
}

.post-nav-link {
  border-bottom: 0;
  display: inline-block;
  padding: 0.35rem 0.6rem;
  border-radius: 12px;
  border: 1px solid var(--rule);
  color: var(--text);
  background: transparent;
  max-width: 48%;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.post-nav-link:hover {
  background: var(--panel);
}

/* Mobile TOC */
.toc-fab {
  display: none;
  position: fixed;
  right: 1rem;
  bottom: 1rem;
  z-index: 120;
  border: 1px solid var(--rule);
  background: var(--overlay-bg);
  color: var(--text);
  border-radius: 999px;
  padding: 0.5rem 0.75rem;
  font: inherit;
  cursor: pointer;
  box-shadow: 0 8px 26px var(--shadow);
}

.toc-drawer {
  position: fixed;
  right: 1rem;
  left: 1rem;
  bottom: 1rem;
  top: 6rem;
  z-index: 130;
  border: 1px solid var(--rule);
  background: var(--bg);
  border-radius: 16px;
  padding: 0.9rem 0.9rem 1rem;
  box-shadow: 0 18px 48px var(--shadow);
  overflow: auto;
}

.toc-drawer-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 1rem;
  padding-bottom: 0.6rem;
  border-bottom: 1px solid var(--rule);
  margin-bottom: 0.75rem;
}

.toast {
  position: fixed;
  left: 50%;
  transform: translateX(-50%);
  bottom: 1rem;
  z-index: 140;
  padding: 0.5rem 0.75rem;
  border-radius: 999px;
  border: 1px solid var(--rule);
  background: var(--overlay-bg);
  color: var(--text);
  box-shadow: 0 8px 26px var(--shadow);
  font-size: 0.95rem;
}

.site-footer {
  padding: 1.75rem 0 2.5rem;
  border-top: 1px solid var(--rule);
  color: var(--muted);
  font-size: 0.95rem;
}

/* Responsiveness */
@media (max-width: 1020px) {
  .post-layout {
    grid-template-columns: minmax(0, 680px) 240px;
    gap: 2rem;
  }
}

@media (max-width: 920px) {
  .post-layout {
    grid-template-columns: minmax(0, 1fr);
  }

  .toc-panel {
    display: none;
  }

  .toc-fab {
    display: inline-block;
  }
}

@media (max-width: 720px) {
  html {
    font-size: 17px;
  }

  main.wrap,
  main.wrap-wide {
    padding-top: 2.2rem;
    padding-bottom: 2.8rem;
  }

  .post-link {
    font-size: 1.08rem;
  }

  .header-actions {
    flex-wrap: wrap;
    justify-content: flex-end;
  }

  .site-header.search-open .header-search-field {
    flex: 1 1 100%;
    max-width: 100%;
    margin-top: 0.55rem;
    order: 10;
  }
}

@media (max-width: 420px) {
  html {
    font-size: 16px;
  }

  .wrap,
  .wrap-wide {
    padding: 0 1rem;
  }
}
