:root {
  color-scheme: light dark;
  --bg: #F6F7F5;
  --surface: #ECEFE9;
  --text: #1B1F1C;
  --muted: #555D57;
  --accent: #2B533F;
  --accent-hover: #1E3D2E;
  --border: #DCE0DA;
  --code-bg: #ECEFE9;

  --font-sans: system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI",
    "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei",
    "Noto Sans CJK SC", "Source Han Sans SC", sans-serif;
  --font-mono: ui-monospace, SFMono-Regular, "SF Mono", Menlo, Consolas,
    "Liberation Mono", monospace;

  --text-heading: 1.25rem;
  --text-body: 1rem;
  --text-sm: 0.875rem;

  --measure: 660px;
  --radius: 6px;
}

@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #141715;
    --surface: #1D221E;
    --text: #E5E8E4;
    --muted: #9AA39C;
    --accent: #7EB396;
    --accent-hover: #9BC7AF;
    --border: #2A302B;
    --code-bg: #1D221E;
  }
}

:root[data-theme="dark"] {
  --bg: #141715;
  --surface: #1D221E;
  --text: #E5E8E4;
  --muted: #9AA39C;
  --accent: #7EB396;
  --accent-hover: #9BC7AF;
  --border: #2A302B;
  --code-bg: #1D221E;
}

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

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

body {
  margin: 0;
  padding: 0;
  background-color: var(--bg);
  color: var(--text);
  font-family: var(--font-sans);
  font-size: var(--text-body);
  line-height: 1.8;
  text-autospace: normal;
  overflow-x: hidden;
  transition: background-color 180ms cubic-bezier(0.22, 1, 0.36, 1),
    color 180ms cubic-bezier(0.22, 1, 0.36, 1);
}

@media (prefers-reduced-motion: reduce) {
  *,
  *::before,
  *::after {
    transition-duration: 0.01ms !important;
    animation-duration: 0.01ms !important;
    scroll-behavior: auto !important;
  }
}

/* Skip link for keyboard accessibility */
.skip-link {
  position: absolute;
  left: -9999px;
  top: 0;
  z-index: 100;
  padding: 0.5rem 0.875rem;
  background: var(--accent);
  color: var(--bg);
  font-size: var(--text-sm);
  text-decoration: none;
  border-radius: var(--radius);
}

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

:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 3px;
}

/* Container */
.wrap {
  width: 100%;
  max-width: var(--measure);
  margin: 0 auto;
  padding: 2rem 1.25rem 3.5rem;
}

/* Header & compact single-line navigation */
.site-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 1rem;
  padding-bottom: 1.25rem;
  margin-bottom: 2.5rem;
  border-bottom: 1px solid var(--border);
}

.brand {
  display: inline-flex;
  align-items: center;
  gap: 0.5rem;
  color: var(--text);
  text-decoration: none;
  font-size: 1.125rem;
  font-weight: 650;
  letter-spacing: -0.02em;
  white-space: nowrap;
}

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

.brand svg {
  flex-shrink: 0;
  color: var(--accent);
}

.site-nav {
  display: flex;
  align-items: center;
  gap: 1.125rem;
  flex-wrap: nowrap;
}

.site-nav a {
  color: var(--muted);
  text-decoration: none;
  font-size: var(--text-sm);
  white-space: nowrap;
  padding: 0.2rem 0;
  border-bottom: 1px solid transparent;
  transition: color 160ms cubic-bezier(0.22, 1, 0.36, 1),
    border-color 160ms cubic-bezier(0.22, 1, 0.36, 1);
}

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

.theme-toggle {
  appearance: none;
  background: transparent;
  border: 1px solid var(--border);
  color: var(--muted);
  font-family: inherit;
  font-size: var(--text-sm);
  line-height: 1.2;
  padding: 0.25rem 0.55rem;
  border-radius: var(--radius);
  cursor: pointer;
  white-space: nowrap;
  transition: color 160ms cubic-bezier(0.22, 1, 0.36, 1),
    border-color 160ms cubic-bezier(0.22, 1, 0.36, 1),
    background-color 160ms cubic-bezier(0.22, 1, 0.36, 1);
}

.theme-toggle:hover {
  color: var(--text);
  border-color: var(--muted);
  background-color: var(--surface);
}

/* Typography */
h1,
h2,
h3 {
  color: var(--text);
  line-height: 1.3;
  letter-spacing: -0.02em;
  text-wrap: balance;
  margin-top: 0;
}

h1 {
  font-size: clamp(1.625rem, 3.2vw, 2rem);
  font-weight: 650;
  margin-bottom: 0.875rem;
}

h2 {
  font-size: var(--text-heading);
  font-weight: 600;
  margin-top: 2.5rem;
  margin-bottom: 1rem;
}

h3 {
  font-size: var(--text-body);
  font-weight: 600;
  margin-top: 1.75rem;
  margin-bottom: 0.625rem;
}

p,
ul,
ol {
  font-size: var(--text-body);
  margin-top: 0;
  margin-bottom: 1.25rem;
  text-wrap: pretty;
}

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

li {
  margin-bottom: 0.45rem;
}

:lang(zh) em,
:lang(zh) i {
  font-style: normal;
  font-weight: 600;
}

a {
  color: var(--accent);
  text-decoration: underline;
  text-underline-offset: 0.2em;
  text-decoration-thickness: 1px;
  transition: color 160ms cubic-bezier(0.22, 1, 0.36, 1);
}

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

blockquote {
  margin: 1.5rem 0;
  padding: 0.15rem 0 0.15rem 1rem;
  border-left: 2px solid var(--accent);
  color: var(--muted);
}

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

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

/* Code blocks */
code {
  font-family: var(--font-mono);
  font-size: 0.9em;
  background-color: var(--code-bg);
  padding: 0.12em 0.35em;
  border-radius: 4px;
}

pre {
  margin: 1.35rem 0;
  padding: 1rem 1.125rem;
  background-color: var(--code-bg);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  overflow-x: auto;
  line-height: 1.55;
  max-width: 100%;
}

pre code {
  background: none;
  padding: 0;
  font-size: var(--text-sm);
  border-radius: 0;
}

/* Responsive tables */
.table-wrap {
  width: 100%;
  overflow-x: auto;
  margin: 1.35rem 0;
  border: 1px solid var(--border);
  border-radius: var(--radius);
}

table {
  width: 100%;
  border-collapse: collapse;
  font-size: var(--text-sm);
  line-height: 1.6;
  font-variant-numeric: tabular-nums;
}

th,
td {
  text-align: left;
  padding: 0.625rem 0.875rem;
  border-bottom: 1px solid var(--border);
  vertical-align: top;
}

tr:last-child td {
  border-bottom: 0;
}

th {
  background-color: var(--surface);
  font-weight: 600;
  color: var(--text);
  white-space: nowrap;
}

.formula {
  margin: 1.25rem 0;
  padding: 0.875rem 1.125rem;
  background-color: var(--surface);
  border-left: 2px solid var(--accent);
  font-family: var(--font-mono);
  font-size: var(--text-sm);
  line-height: 1.65;
  overflow-x: auto;
}

.list-more {
  margin-top: 1.125rem;
  font-size: var(--text-sm);
}

/* Pure HTML collapsible TOC */
.toc {
  margin: 0 0 2rem;
  padding: 0.75rem 1rem;
  background-color: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  font-size: var(--text-sm);
}

.toc summary {
  cursor: pointer;
  font-weight: 600;
  color: var(--text);
  user-select: none;
}

.toc ol {
  margin: 0.65rem 0 0.15rem;
  padding-left: 1.25rem;
  font-size: var(--text-sm);
  line-height: 1.65;
}

.toc li {
  margin-bottom: 0.25rem;
}

.toc a {
  text-decoration: none;
}

.toc a:hover {
  text-decoration: underline;
}

/* Intro block on home */
.intro {
  margin-bottom: 2.25rem;
}

.intro p {
  margin-bottom: 0.75rem;
}

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

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

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

.post-item:last-child {
  border-bottom: 1px solid var(--border);
}

.post-row {
  display: flex;
  align-items: baseline;
  gap: 1rem;
}

.post-date {
  flex-shrink: 0;
  font-size: var(--text-sm);
  color: var(--muted);
  font-variant-numeric: tabular-nums;
}

.post-title {
  font-size: var(--text-body);
  font-weight: 550;
  text-decoration: none;
  color: var(--text);
}

.post-title:hover {
  color: var(--accent);
  text-decoration: underline;
}

.post-summary {
  margin-top: 0.3rem;
  margin-bottom: 0;
  padding-left: calc(5.75rem + 1rem);
  font-size: var(--text-sm);
  color: var(--muted);
  line-height: 1.65;
}

/* Article meta & prose */
.article-header {
  margin-bottom: 2rem;
  padding-bottom: 1.25rem;
  border-bottom: 1px solid var(--border);
}

.article-meta {
  font-size: var(--text-sm);
  color: var(--muted);
  font-variant-numeric: tabular-nums;
  margin: 0;
}

.article-body {
  margin-bottom: 2.5rem;
}

.article-nav {
  display: flex;
  justify-content: space-between;
  gap: 1rem;
  padding-top: 1.25rem;
  border-top: 1px solid var(--border);
  font-size: var(--text-sm);
}

/* Reading shelf & archive list */
.entry-list {
  list-style: none;
  padding: 0;
  margin: 0;
}

.entry-item {
  padding: 0.875rem 0;
  border-top: 1px solid var(--border);
}

.entry-item:last-child {
  border-bottom: 1px solid var(--border);
}

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

.entry-title {
  font-weight: 600;
  color: var(--text);
}

.entry-meta {
  font-size: var(--text-sm);
  color: var(--muted);
  font-variant-numeric: tabular-nums;
  flex-shrink: 0;
}

.entry-note {
  margin-top: 0.35rem;
  margin-bottom: 0;
  font-size: var(--text-sm);
  color: var(--muted);
  line-height: 1.7;
}

.meta-note {
  font-size: var(--text-sm);
  color: var(--muted);
  font-variant-numeric: tabular-nums;
}

/* Footer */
.site-footer {
  margin-top: 3.5rem;
  padding-top: 1.25rem;
  border-top: 1px solid var(--border);
  display: flex;
  flex-wrap: wrap;
  justify-content: space-between;
  align-items: center;
  gap: 0.75rem 1.25rem;
  font-size: var(--text-sm);
  color: var(--muted);
}

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

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

.footer-links a:hover {
  color: var(--text);
  text-decoration: underline;
}

/* Mobile adjustments */
@media (max-width: 560px) {
  .wrap {
    padding: 1.375rem 1rem 2.75rem;
  }

  .site-header {
    margin-bottom: 2rem;
    gap: 0.5rem;
  }

  .site-nav {
    gap: 0.75rem;
  }

  .post-row {
    flex-direction: column;
    gap: 0.15rem;
  }

  .post-summary {
    padding-left: 0;
  }

  .entry-head {
    flex-direction: column;
    gap: 0.15rem;
  }

  .article-nav {
    flex-direction: column;
  }
}

/* Print stylesheet */
@media print {
  body {
    background: #FFFFFF;
    color: #000000;
  }

  .skip-link,
  .site-nav,
  .theme-toggle,
  .toc,
  .site-footer,
  .article-nav {
    display: none;
  }

  .wrap {
    max-width: 100%;
    padding: 0;
  }

  .site-header {
    border-bottom: 1px solid #CCCCCC;
    margin-bottom: 1.5rem;
  }

  a {
    color: #000000;
    text-decoration: underline;
  }

  .article-body a[href^="http"]::after {
    content: " (" attr(href) ")";
    font-size: 0.875em;
  }

  pre {
    border-color: #CCCCCC;
    background: #F7F7F7;
    white-space: pre-wrap;
  }
}
