:root {
  --bg: #faf9f6;
  --bg-elevated: #f3f1eb;
  --bg-subtle: #eae7df;
  --fg: #1a1917;
  --fg-muted: #5d5a52;
  --fg-faint: #8b877d;
  --border: #dfdbd1;
  --border-strong: #c8c3b6;
  --accent: #1f4e79;
  --accent-hover: #163a5c;
  --accent-soft: rgba(31, 78, 121, 0.08);
  --mark-bg: #f6e7a8;

  --font-sans: "Pretendard Variable", "Pretendard", -apple-system, BlinkMacSystemFont,
    "Apple SD Gothic Neo", "Noto Sans KR", "Segoe UI", system-ui, sans-serif;
  --font-mono: "JetBrains Mono", "Berkeley Mono", ui-monospace, SFMono-Regular, Menlo,
    Monaco, Consolas, monospace;

  --measure: 700px;
  --wide: 1020px;
  --radius: 6px;

  /* Pele diagram theme tokens (light) */
  --pele-bg: var(--bg-elevated);
  --pele-fg: var(--fg);
  --pele-muted: var(--fg-muted);
  --pele-line: var(--fg-muted);
  --pele-surface: var(--bg);
  --pele-surface-alt: var(--bg-subtle);
  --pele-border: var(--border-strong);
  --pele-accent: var(--accent);
  --pele-font: var(--font-sans);
  --pele-font-mono: var(--font-mono);
  --pele-radius: 5px;
  --pele-series-1: #1f4e79;
  --pele-series-2: #2f7d57;
  --pele-series-3: #b45309;
  --pele-series-4: #8c3b4a;
  --pele-series-5: #5c5470;
  --pele-series-6: #3b7a8c;
}

:root[data-theme="dark"] {
  --bg: #131416;
  --bg-elevated: #1b1d21;
  --bg-subtle: #23262c;
  --fg: #e7e5df;
  --fg-muted: #9e9b93;
  --fg-faint: #6f6d67;
  --border: #2a2d34;
  --border-strong: #3b3f49;
  --accent: #7ab3df;
  --accent-hover: #9ec8e9;
  --accent-soft: rgba(122, 179, 223, 0.12);
  --mark-bg: rgba(234, 197, 79, 0.25);

  /* Pele diagram theme tokens (dark) */
  --pele-bg: var(--bg-elevated);
  --pele-fg: var(--fg);
  --pele-muted: var(--fg-muted);
  --pele-line: var(--fg-muted);
  --pele-surface: var(--bg);
  --pele-surface-alt: var(--bg-subtle);
  --pele-border: var(--border-strong);
  --pele-accent: var(--accent);
  --pele-series-1: #7ab3df;
  --pele-series-2: #6bc497;
  --pele-series-3: #e5a35d;
  --pele-series-4: #db8292;
  --pele-series-5: #a79ec4;
  --pele-series-6: #74b8c8;
}

@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #131416;
    --bg-elevated: #1b1d21;
    --bg-subtle: #23262c;
    --fg: #e7e5df;
    --fg-muted: #9e9b93;
    --fg-faint: #6f6d67;
    --border: #2a2d34;
    --border-strong: #3b3f49;
    --accent: #7ab3df;
    --accent-hover: #9ec8e9;
    --accent-soft: rgba(122, 179, 223, 0.12);
    --mark-bg: rgba(234, 197, 79, 0.25);

    --pele-bg: var(--bg-elevated);
    --pele-fg: var(--fg);
    --pele-muted: var(--fg-muted);
    --pele-line: var(--fg-muted);
    --pele-surface: var(--bg);
    --pele-surface-alt: var(--bg-subtle);
    --pele-border: var(--border-strong);
    --pele-accent: var(--accent);
  }
}

*,
*::before,
*::after {
  box-sizing: border-box;
}

html {
  background: var(--bg);
  color: var(--fg);
  font-family: var(--font-sans);
  font-size: 16px;
  line-height: 1.75;
  -webkit-font-smoothing: antialiased;
  text-rendering: optimizeLegibility;
  scroll-behavior: smooth;
}

body {
  margin: 0;
  min-height: 100vh;
  display: flex;
  flex-direction: column;
  background: var(--bg);
  color: var(--fg);
}

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

a:hover {
  color: var(--accent-hover);
}

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

.skip-link {
  position: absolute;
  left: -9999px;
  top: 0;
  background: var(--accent);
  color: #fff;
  padding: 0.5rem 0.9rem;
  font-size: 0.875rem;
  z-index: 100;
}

.skip-link:focus {
  left: 0.75rem;
  top: 0.75rem;
}

/* Header */
.site-header {
  border-bottom: 1px solid var(--border);
  background: var(--bg);
}

.site-header-inner {
  max-width: var(--wide);
  margin: 0 auto;
  padding: 1rem 1.5rem;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 1rem;
}

.site-brand {
  font-family: var(--font-mono);
  font-size: 0.95rem;
  font-weight: 600;
  letter-spacing: -0.02em;
  color: var(--fg);
  text-decoration: none;
}

.site-brand:hover {
  color: var(--accent);
}

.site-nav {
  display: flex;
  align-items: center;
  gap: 1.25rem;
  font-size: 0.9rem;
}

.site-nav a {
  color: var(--fg-muted);
  text-decoration: none;
}

.site-nav a:hover,
.site-nav a[aria-current="page"] {
  color: var(--fg);
}

.site-nav a[aria-current="page"] {
  font-weight: 600;
}

.theme-toggle {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 32px;
  height: 32px;
  padding: 0;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: transparent;
  color: var(--fg-muted);
  cursor: pointer;
}

.theme-toggle:hover {
  color: var(--fg);
  border-color: var(--border-strong);
  background: var(--bg-elevated);
}

.theme-toggle .icon-sun {
  display: none;
}

:root[data-theme="dark"] .theme-toggle .icon-sun {
  display: block;
}

:root[data-theme="dark"] .theme-toggle .icon-moon {
  display: none;
}

/* Main container */
.site-main {
  flex: 1;
  width: 100%;
  max-width: var(--measure);
  margin: 0 auto;
  padding: 3rem 1.5rem 4.5rem;
}

.page-post .site-main {
  max-width: var(--wide);
}

/* Home & Index */
.intro {
  margin-bottom: 2.75rem;
  padding-bottom: 2rem;
  border-bottom: 1px solid var(--border);
}

.intro h1 {
  margin: 0 0 0.4rem;
  font-size: 1.65rem;
  font-weight: 650;
  letter-spacing: -0.03em;
  line-height: 1.3;
}

.intro-subtitle {
  margin: 0;
  color: var(--fg-muted);
  font-size: 1rem;
}

.intro-tags {
  margin-top: 1.1rem;
  display: flex;
  flex-wrap: wrap;
  gap: 0.45rem 0.75rem;
}

.page-header {
  margin-bottom: 2rem;
  padding-bottom: 1.25rem;
  border-bottom: 1px solid var(--border);
}

.page-header h1 {
  margin: 0 0 0.35rem;
  font-size: 1.6rem;
  font-weight: 650;
  letter-spacing: -0.025em;
  line-height: 1.3;
}

.page-subtitle {
  margin: 0;
  color: var(--fg-muted);
  font-size: 0.95rem;
}

.breadcrumb {
  margin: 0 0 0.5rem;
  font-size: 0.85rem;
}

.breadcrumb a {
  color: var(--fg-muted);
  text-decoration: none;
}

.breadcrumb a:hover {
  color: var(--fg);
}

/* Post list */
.post-list {
  list-style: none;
  margin: 0;
  padding: 0;
  display: flex;
  flex-direction: column;
  gap: 2.25rem;
}

.post-item-meta,
.post-meta {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  font-family: var(--font-mono);
  font-size: 0.8rem;
  color: var(--fg-muted);
  font-variant-numeric: tabular-nums;
}

.meta-sep {
  color: var(--fg-faint);
}

.post-item-title {
  margin: 0.35rem 0 0.45rem;
  font-size: 1.28rem;
  font-weight: 620;
  line-height: 1.4;
  letter-spacing: -0.02em;
}

.post-item-title a {
  color: var(--fg);
  text-decoration: none;
}

.post-item-title a:hover {
  color: var(--accent);
}

.post-item-desc {
  margin: 0 0 0.7rem;
  color: var(--fg-muted);
  font-size: 0.95rem;
  line-height: 1.65;
}

.tag-row {
  display: flex;
  flex-wrap: wrap;
  gap: 0.35rem 0.65rem;
}

.tag-link {
  font-family: var(--font-mono);
  font-size: 0.78rem;
  color: var(--fg-muted);
  text-decoration: none;
}

.tag-link:hover {
  color: var(--accent);
}

.tag-count {
  color: var(--fg-faint);
}

/* Post layout with sticky desktop ToC */
.post-layout {
  display: block;
}

.post-article {
  max-width: var(--measure);
  min-width: 0;
}

.toc-desktop {
  display: none;
}

@media (min-width: 1024px) {
  .post-layout.has-toc {
    display: grid;
    grid-template-columns: minmax(0, var(--measure)) minmax(200px, 240px);
    justify-content: space-between;
    gap: 3rem;
    align-items: start;
  }

  .toc-desktop {
    display: block;
  }

  .toc-mobile {
    display: none;
  }
}

.toc-sticky {
  position: sticky;
  top: 2rem;
  padding-left: 1rem;
  border-left: 1px solid var(--border);
}

.toc-title {
  font-family: var(--font-mono);
  font-size: 0.75rem;
  font-weight: 600;
  color: var(--fg-faint);
  margin-bottom: 0.75rem;
}

.toc-list {
  list-style: none;
  margin: 0;
  padding: 0;
  display: flex;
  flex-direction: column;
  gap: 0.4rem;
  font-size: 0.84rem;
  line-height: 1.45;
}

.toc-list a {
  color: var(--fg-muted);
  text-decoration: none;
  display: block;
}

.toc-list a:hover {
  color: var(--fg);
}

.toc-depth-3 {
  padding-left: 0.85rem;
}

.toc-depth-4 {
  padding-left: 1.6rem;
}

.toc-mobile {
  margin: 0 0 2rem;
  padding: 0.75rem 1rem;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: var(--bg-elevated);
}

.toc-mobile summary {
  cursor: pointer;
  font-size: 0.88rem;
  font-weight: 600;
  color: var(--fg);
}

.toc-mobile nav {
  margin-top: 0.75rem;
}

/* Post Header */
.post-header {
  margin-bottom: 2.25rem;
  padding-bottom: 1.75rem;
  border-bottom: 1px solid var(--border);
}

.post-title {
  margin: 0.55rem 0 0.75rem;
  font-size: clamp(1.6rem, 3vw, 2.05rem);
  font-weight: 700;
  line-height: 1.3;
  letter-spacing: -0.03em;
  word-break: keep-all;
}

.post-lead {
  margin: 0 0 1.1rem;
  color: var(--fg-muted);
  font-size: 1.03rem;
  line-height: 1.65;
  word-break: keep-all;
}

/* Article Prose */
.prose {
  font-size: 1.01rem;
  line-height: 1.82;
  word-break: keep-all;
  overflow-wrap: break-word;
}

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

.prose h2,
.prose h3,
.prose h4 {
  scroll-margin-top: 2rem;
  font-weight: 650;
  letter-spacing: -0.025em;
  line-height: 1.35;
  color: var(--fg);
}

.prose h2 {
  margin: 2.75rem 0 0.9rem;
  font-size: 1.4rem;
  padding-bottom: 0.35rem;
  border-bottom: 1px solid var(--border);
}

.prose h3 {
  margin: 2rem 0 0.65rem;
  font-size: 1.15rem;
}

.prose h4 {
  margin: 1.5rem 0 0.5rem;
  font-size: 1rem;
}

.heading-anchor {
  margin-left: 0.45rem;
  color: var(--fg-faint);
  text-decoration: none;
  font-weight: 400;
  opacity: 0;
  transition: opacity 0.12s ease;
}

.prose h2:hover .heading-anchor,
.prose h3:hover .heading-anchor,
.prose h4:hover .heading-anchor,
.heading-anchor:focus {
  opacity: 1;
}

.prose p {
  margin: 0 0 1.2rem;
}

.prose ul,
.prose ol {
  margin: 0 0 1.3rem;
  padding-left: 1.45rem;
}

.prose li {
  margin-bottom: 0.35rem;
}

.prose blockquote {
  margin: 1.5rem 0;
  padding: 0.25rem 0 0.25rem 1.1rem;
  border-left: 2px solid var(--border-strong);
  color: var(--fg-muted);
}

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

.prose hr {
  border: 0;
  border-top: 1px solid var(--border);
  margin: 2.5rem 0;
}

.prose code {
  font-family: var(--font-mono);
  font-size: 0.87em;
  padding: 0.14em 0.36em;
  border-radius: 4px;
  background: var(--bg-elevated);
  border: 1px solid var(--border);
}

/* Shiki Code Blocks */
.code-block {
  margin: 1.5rem 0;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: var(--bg-elevated);
  overflow: hidden;
}

.code-header {
  display: flex;
  justify-content: flex-end;
  padding: 0.35rem 0.85rem;
  border-bottom: 1px solid var(--border);
  background: var(--bg-subtle);
}

.code-lang {
  font-family: var(--font-mono);
  font-size: 0.72rem;
  color: var(--fg-muted);
}

.code-block pre.shiki {
  margin: 0;
  padding: 1rem 1.1rem;
  overflow-x: auto;
  background: transparent !important;
  font-family: var(--font-mono);
  font-size: 0.86rem;
  line-height: 1.65;
}

.code-block pre.shiki code {
  padding: 0;
  border: 0;
  background: transparent;
  font-size: inherit;
}

.shiki,
.shiki span {
  color: var(--shiki-light);
}

:root[data-theme="dark"] .shiki,
:root[data-theme="dark"] .shiki span {
  color: var(--shiki-dark);
}

@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) .shiki,
  :root:not([data-theme="light"]) .shiki span {
    color: var(--shiki-dark);
  }
}

/* KaTeX Math */
.math-block {
  margin: 1.6rem 0;
  padding: 0.5rem 0.25rem;
  overflow-x: auto;
  overflow-y: hidden;
  text-align: center;
}

.math-block .katex-display {
  margin: 0;
}

.math-inline .katex {
  font-size: 1.03em;
}

/* Pele Diagrams */
.diagram {
  margin: 1.75rem 0;
  padding: 1.25rem 1rem;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: var(--bg-elevated);
  overflow-x: auto;
  display: flex;
  justify-content: center;
}

.diagram svg.pele {
  display: block;
  max-width: 100%;
  height: auto;
}

/* Tables */
.table-wrap {
  margin: 1.5rem 0;
  overflow-x: auto;
}

.prose table {
  width: 100%;
  border-collapse: collapse;
  font-size: 0.92rem;
}

.prose th,
.prose td {
  padding: 0.55rem 0.75rem;
  border-bottom: 1px solid var(--border);
  text-align: left;
}

.prose th {
  font-weight: 600;
  border-bottom: 1px solid var(--border-strong);
  color: var(--fg);
}

/* Post Pager */
.post-pager {
  margin-top: 3.5rem;
  padding-top: 1.5rem;
  border-top: 1px solid var(--border);
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 1.25rem;
}

.pager-slot {
  display: flex;
  flex-direction: column;
  gap: 0.25rem;
}

.pager-next {
  text-align: right;
}

.pager-label {
  font-family: var(--font-mono);
  font-size: 0.76rem;
  color: var(--fg-faint);
}

.pager-slot a {
  color: var(--fg);
  text-decoration: none;
  font-size: 0.94rem;
  font-weight: 500;
}

.pager-slot a:hover {
  color: var(--accent);
}

/* Comments / Giscus */
.comments {
  margin-top: 3rem;
  padding-top: 2rem;
  border-top: 1px solid var(--border);
}

.comments-heading {
  margin: 0 0 1rem;
  font-size: 1.05rem;
  font-weight: 600;
}

.comments-note {
  margin: 0;
  padding: 0.9rem 1.1rem;
  border: 1px dashed var(--border-strong);
  border-radius: var(--radius);
  background: var(--bg-elevated);
  color: var(--fg-muted);
  font-size: 0.88rem;
  line-height: 1.65;
}

.comments-note code {
  font-family: var(--font-mono);
  font-size: 0.86em;
  color: var(--fg);
}

/* Tags directory */
.tag-directory {
  list-style: none;
  margin: 0;
  padding: 0;
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(180px, 1fr));
  gap: 0.75rem;
}

.tag-directory-item a {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 0.65rem 0.9rem;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: var(--bg-elevated);
  color: var(--fg);
  text-decoration: none;
  font-family: var(--font-mono);
  font-size: 0.86rem;
}

.tag-directory-item a:hover {
  border-color: var(--accent);
  color: var(--accent);
}

.tag-directory-count {
  color: var(--fg-faint);
}

/* Search Page */
.search-input {
  width: 100%;
  padding: 0.75rem 1rem;
  font-family: inherit;
  font-size: 1rem;
  color: var(--fg);
  background: var(--bg-elevated);
  border: 1px solid var(--border-strong);
  border-radius: var(--radius);
  outline: none;
}

.search-input:focus {
  border-color: var(--accent);
  box-shadow: 0 0 0 3px var(--accent-soft);
}

.search-status {
  margin: 0.85rem 0 1.25rem;
  font-family: var(--font-mono);
  font-size: 0.82rem;
  color: var(--fg-muted);
  min-height: 1.4em;
}

.search-results {
  list-style: none;
  margin: 0;
  padding: 0;
  display: flex;
  flex-direction: column;
  gap: 1.5rem;
}

.search-result-item {
  padding-bottom: 1.5rem;
  border-bottom: 1px solid var(--border);
}

.search-result-title {
  margin: 0 0 0.35rem;
  font-size: 1.12rem;
  font-weight: 620;
}

.search-result-title a {
  color: var(--fg);
  text-decoration: none;
}

.search-result-title a:hover {
  color: var(--accent);
}

.search-result-meta {
  font-family: var(--font-mono);
  font-size: 0.78rem;
  color: var(--fg-faint);
  margin-bottom: 0.35rem;
}

.search-result-excerpt {
  margin: 0;
  font-size: 0.93rem;
  color: var(--fg-muted);
  line-height: 1.65;
}

.search-result-excerpt mark {
  background: var(--mark-bg);
  color: var(--fg);
  padding: 0 0.15em;
  border-radius: 2px;
}

/* 404 */
.not-found {
  padding: 2rem 0;
}

.not-found-code {
  margin: 0;
  font-family: var(--font-mono);
  font-size: 0.9rem;
  color: var(--fg-faint);
}

.back-home {
  font-family: var(--font-mono);
  font-size: 0.9rem;
}

/* Footer */
.site-footer {
  border-top: 1px solid var(--border);
  margin-top: auto;
}

.site-footer-inner {
  max-width: var(--wide);
  margin: 0 auto;
  padding: 1.25rem 1.5rem;
  display: flex;
  justify-content: space-between;
  align-items: center;
  font-family: var(--font-mono);
  font-size: 0.8rem;
  color: var(--fg-faint);
}

.footer-links {
  display: flex;
  gap: 0.5rem;
}

.footer-links a {
  color: var(--fg-muted);
  text-decoration: none;
}

.footer-links a:hover {
  color: var(--fg);
}
