:root {
  color-scheme: light;
  --bg: #ffffff;
  --text: #111827;
  --heading: #030712;
  --soft: #374151;
  --muted: #6b7280;
  --line: #e5e7eb;
  --quote-line: #d1d5db;
  --accent: #1d4ed8;
  --accent-soft: rgb(29 78 216 / 0.1);
  --surface: rgb(0 0 0 / 0.04);
  --panel: #ffffff;
  --shadow: 0 12px 32px rgb(15 23 42 / 0.12), 0 2px 6px rgb(15 23 42 / 0.06);

  --hl-text: #383a42;
  --hl-comment: #a0a1a7;
  --hl-keyword: #a626a4;
  --hl-string: #50a14f;
  --hl-number: #986801;
  --hl-function: #4078f2;
  --hl-type: #c18401;
  --hl-name: #e45649;

  --info: #3b82f6;  --info-bg: rgb(96 165 250 / 0.1);
  --danger: #ef4444; --danger-bg: rgb(248 113 113 / 0.1);
  --success: #22c55e; --success-bg: rgb(74 222 128 / 0.1);
  --warning: #eab308; --warning-bg: rgb(250 204 21 / 0.1);

  --font: "Noto Sans TC", system-ui, sans-serif;
  --mono: ui-monospace, SFMono-Regular, Menlo, Consolas, "Noto Sans TC", monospace;
  --nav-h: 3.75rem;
}

:root.dark {
  color-scheme: dark;
  --bg: #030712;
  --text: #e5e7eb;
  --heading: #eff6ff;
  --soft: #d1d5db;
  --muted: #9ca3af;
  --line: #374151;
  --quote-line: #374151;
  --accent: #93c5fd;
  --accent-soft: rgb(147 197 253 / 0.1);
  --surface: rgb(255 255 255 / 0.07);
  --panel: #111827;
  --shadow: 0 12px 32px rgb(0 0 0 / 0.5), 0 0 0 1px #1f2937;

  --hl-text: #abb2bf;
  --hl-comment: #7f848e;
  --hl-keyword: #c678dd;
  --hl-string: #98c379;
  --hl-number: #d19a66;
  --hl-function: #61afef;
  --hl-type: #e5c07b;
  --hl-name: #e06c75;

  --info: #1d4ed8;  --info-bg: rgb(147 197 253 / 0.1);
  --danger: #b91c1c; --danger-bg: rgb(252 165 165 / 0.1);
  --success: #15803d; --success-bg: rgb(134 239 172 / 0.1);
  --warning: #a16207; --warning-bg: rgb(253 224 71 / 0.1);
}

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

html {
  scroll-behavior: smooth;
  scroll-padding-top: calc(var(--nav-h) + 0.5rem);
  -webkit-text-size-adjust: 100%;
}

body {
  margin: 0;
  min-height: 100dvh;
  display: flex;
  flex-direction: column;
  background: var(--bg);
  color: var(--text);
  font-family: var(--font);
  font-weight: 300;
  line-height: 1.6;
  transition: background-color 0.5s ease, color 0.5s ease;
}

a { color: inherit; text-decoration: none; }
img { max-width: 100%; height: auto; }
button { font: inherit; color: inherit; background: none; border: 0; padding: 0; cursor: pointer; }

.icon { width: 1em; height: 1em; flex: none; vertical-align: -0.125em; }

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

/* 版面 */
.wrap { width: 100%; margin-inline: auto; padding-inline: 1rem; }
@media (min-width: 640px)  { .wrap { max-width: 640px; } }
@media (min-width: 768px)  { .wrap { max-width: 768px; } }
@media (min-width: 1024px) { .wrap { max-width: calc(992px * 5 / 6 + 2rem); } }
@media (min-width: 1280px) { .wrap { max-width: calc(1248px * 3 / 4 + 2rem); } }
@media (min-width: 1536px) { .wrap { max-width: calc(1504px * 3 / 4 + 2rem); } }

.page { margin-top: 1.25rem; }

.layout { display: flex; }
.main { flex: 1; min-width: 0; }
.side { display: none; }

@media (min-width: 1024px) {
  .side { display: block; flex: 0 0 25%; padding-right: 2.5rem; }
  .side-inner { position: sticky; top: 4.75rem; }
}

/* 導覽列 */
.nav {
  position: sticky;
  top: 0;
  z-index: 100;
  padding-block: 0.75rem;
  background: color-mix(in srgb, var(--bg) 72%, transparent);
  backdrop-filter: blur(4px);
  -webkit-backdrop-filter: blur(4px);
}

.nav-inner { display: flex; align-items: center; gap: 0.5rem; }

.brand {
  flex: 1;
  font-size: 1.5rem;
  font-weight: 700;
  color: var(--heading);
  white-space: nowrap;
}

.nav-links { display: flex; align-items: center; }

.nav-links > a,
.nav-links > button {
  padding-inline: 0.75rem;
  color: var(--heading);
  white-space: nowrap;
  transition: color 0.2s;
}

.nav-links > a:hover,
.nav-links > button:hover,
.nav-links > a[aria-current="page"] { color: var(--accent); }

.theme-toggle { font-size: 1.5rem; display: grid; place-items: center; color: var(--heading); }

@media (max-width: 480px) {
  .brand { font-size: 1.25rem; }
  .nav-links > a, .nav-links > button { padding-inline: 0.5rem; }
}

.menu {
  margin: 0;
  inset: auto;
  position: fixed;
  top: calc(var(--nav-h) - 0.25rem);
  right: max(1rem, (100vw - 72rem) / 2);
  min-width: 12rem;
  padding: 0.375rem;
  border: 1px solid var(--line);
  border-radius: 0.5rem;
  background: var(--panel);
  color: var(--text);
  box-shadow: var(--shadow);
}

.menu a {
  display: flex;
  justify-content: space-between;
  gap: 1.5rem;
  padding: 0.375rem 0.625rem;
  border-radius: 0.25rem;
}

.menu a:hover { background: var(--surface); color: var(--accent); }
.menu small { color: var(--muted); font-size: 0.8125rem; }

/* 側欄 */
.avatar { display: block; width: 100%; max-width: 200px; aspect-ratio: 1; border-radius: 50%; object-fit: cover; margin-bottom: 1rem; }
.side-name { margin: 0 0 1rem; font-size: 1.5rem; font-weight: 700; color: var(--heading); }
.side-bio { margin: 0; font-size: 0.875rem; color: var(--soft); }
.side-links { list-style: none; margin: 1rem 0 0; padding: 0; display: grid; gap: 0.5rem; justify-items: start; }
.side-links a { display: inline-flex; align-items: center; gap: 0.375rem; color: var(--heading); transition: color 0.2s; }
.side-links a:hover { color: var(--accent); }

/* 文章列表 */
.archive-title { margin: 0 0 1rem; font-size: 0.875rem; font-weight: 400; color: var(--muted); }

.post-list { display: flex; flex-direction: column; gap: 1rem; }

.post-item {
  display: block;
  padding: 0.5rem;
  border-left: 1px solid var(--line);
  border-bottom: 1px solid var(--line);
}

.post-cats { display: flex; flex-wrap: wrap; gap: 0.5rem; font-size: 0.875rem; color: var(--muted); }
.post-cats a:hover { color: var(--accent); }

.post-title {
  margin: 0.375rem 0;
  font-size: 1.5rem;
  font-weight: 500;
  line-height: 1.4;
  color: var(--heading);
  text-wrap: pretty;
  transition: color 0.2s;
}

.bar {
  display: inline-block;
  width: 0.5rem;
  height: 1em;
  margin-right: 0.375rem;
  border-radius: 2px;
  background: var(--accent);
  vertical-align: -0.075em;
  transition: width 0.3s;
}

.post-title .icon-chevron {
  opacity: 0;
  translate: -0.5rem 0;
  transition: opacity 0.5s, translate 0.5s;
}

.post-item:hover .post-title { color: var(--accent); }
.post-item:hover .bar { width: 0.75rem; }
.post-item:hover .icon-chevron { opacity: 1; translate: 0 0; }

.post-date { display: block; font-size: 0.875rem; color: var(--muted); }

.tags { display: flex; flex-wrap: wrap; gap: 0.5rem; margin-top: 0.5rem; }
.tags > * {
  padding: 0 0.5rem;
  border-radius: 2px;
  font-size: 0.875rem;
  color: var(--accent);
  background: var(--accent-soft);
}

.pager { margin-top: 2rem; }
.pages { list-style: none; margin: 0; padding: 0; display: flex; justify-content: center; align-items: center; gap: 0.25rem; }
.pages :is(a, span) {
  display: grid;
  place-items: center;
  min-width: 2.25rem;
  height: 2.25rem;
  padding-inline: 0.5rem;
  border-radius: 0.375rem;
  font-size: 0.875rem;
  color: var(--muted);
  transition: color 0.2s, background-color 0.2s;
}
.pages a:hover { color: var(--accent); background: var(--surface); }
.pages .current a { color: var(--accent); background: var(--accent-soft); font-weight: 500; }
.pages .prev .icon { rotate: 180deg; }
.pager.loading .pages { visibility: hidden; }
.pager.loading::after {
  content: "";
  display: block;
  width: 1.25rem;
  height: 1.25rem;
  margin: -1.5rem auto 0;
  border: 2px solid var(--line);
  border-top-color: var(--accent);
  border-radius: 50%;
  animation: spin 0.8s linear infinite;
}
@keyframes spin { to { rotate: 360deg; } }

.empty { color: var(--muted); }

/* 文章頁 */
.post-head { margin-bottom: 1.5rem; }
.post-head h1 {
  margin: 0.375rem 0;
  font-size: 2rem;
  font-weight: 700;
  line-height: 1.3;
  color: var(--heading);
  text-wrap: pretty;
}

.md { color: var(--text); overflow-wrap: anywhere; }
.md > :first-child { margin-top: 0; }

.md h1, .md h2, .md h3, .md h4, .md h5, .md h6 {
  margin: 1rem 0;
  font-weight: 700;
  line-height: 1.4;
  color: var(--heading);
}
.md h1 { font-size: 1.875rem; padding-bottom: 0.5rem; border-bottom: 2px solid var(--line); }
.md h2 { font-size: 1.5rem; }
.md h3 { font-size: 1.25rem; }
.md h4 { font-size: 1.125rem; }
.md h5 { font-size: 1rem; }
.md h6 { font-size: 0.875rem; }
.md h1:hover, .md h2:hover, .md h3:hover { color: var(--accent); }

.md h2::before {
  content: "";
  display: inline-block;
  width: 0.5rem;
  height: 1em;
  margin-right: 0.375rem;
  border-radius: 2px;
  background: var(--accent);
  vertical-align: -0.075em;
}

.md p { margin: 0.5rem 0; }
.md a { color: var(--accent); }
.md a:hover { text-decoration: underline; }
.md :is(h1, h2, h3, h4) > a { color: inherit; }
.md :is(h1, h2, h3, h4) > a:hover { text-decoration: none; }
.md strong, .md b { font-weight: 700; }
.md ul { list-style: disc; padding-left: 1.25rem; }
.md ol { list-style: decimal; padding-left: 1.25rem; }
.md ul ul { list-style: revert; }
.md img { border-radius: 0.375rem; }
.md hr { border: 0; border-top: 1px solid var(--line); margin: 1.5rem 0; }

.md blockquote {
  margin: 1rem 0;
  padding: 0.5rem 1rem;
  border-left: 4px solid var(--quote-line);
  background: var(--surface);
}
.md blockquote > :first-child { margin-top: 0; }
.md blockquote > :last-child { margin-bottom: 0; }

.md table { border-collapse: collapse; display: block; max-width: 100%; overflow-x: auto; }
.md th { padding: 0.5rem 1rem; text-align: left; font-weight: 700; border-bottom: 2px solid var(--quote-line); }
.md td { padding: 0.5rem 1rem; border-bottom: 1px solid var(--quote-line); }

.md :not(pre) > code {
  padding: 0.125em 0.375em;
  border-radius: 0.25rem;
  background: var(--surface);
  font-family: var(--mono);
  font-size: 0.875em;
}

/* 程式碼區塊 */
.code { margin: 1rem 0; padding-block: 0.5rem; border-radius: 0.375rem; background: var(--surface); }
.code-head { display: flex; align-items: center; gap: 0.5rem; padding-inline: 0.75rem; margin-bottom: 0.5rem; font-size: 0.875rem; color: var(--muted); font-family: var(--mono); }
.copy { margin-left: auto; display: grid; place-items: center; color: var(--muted); transition: color 0.2s; }
.copy:hover { color: var(--text); }
.copy .icon-check, .copy.done .icon-copy { display: none; }
.copy.done .icon-check { display: block; color: var(--success); }
.code pre { margin: 0; padding-inline: 0.75rem; overflow-x: auto; }
.code code { font-family: var(--mono); font-size: 0.875rem; line-height: 1.6; color: var(--hl-text); }

.hljs-comment, .hljs-quote { color: var(--hl-comment); font-style: italic; }
.hljs-keyword, .hljs-selector-tag, .hljs-doctag, .hljs-formula { color: var(--hl-keyword); }
.hljs-string, .hljs-regexp, .hljs-addition, .hljs-attribute, .hljs-meta .hljs-string { color: var(--hl-string); }
.hljs-number, .hljs-literal, .hljs-attr, .hljs-variable, .hljs-template-variable, .hljs-selector-attr, .hljs-selector-pseudo { color: var(--hl-number); }
.hljs-title, .hljs-title.function_, .hljs-section, .hljs-meta, .hljs-link, .hljs-bullet, .hljs-symbol { color: var(--hl-function); }
.hljs-built_in, .hljs-type, .hljs-class .hljs-title, .hljs-title.class_ { color: var(--hl-type); }
.hljs-name, .hljs-tag, .hljs-selector-id, .hljs-selector-class, .hljs-deletion, .hljs-subst { color: var(--hl-name); }
.hljs-emphasis { font-style: italic; }
.hljs-strong { font-weight: 700; }

/* 提示框 <info t="標題">…</info> */
.md :is(info, danger, success, warning) {
  --c: var(--info);
  --c-bg: var(--info-bg);
  position: relative;
  display: block;
  margin: 1rem 0;
  padding: 0.5rem 1rem;
  border-left: 4px solid var(--c);
  background: var(--c-bg);
}
.md danger { --c: var(--danger); --c-bg: var(--danger-bg); }
.md success { --c: var(--success); --c-bg: var(--success-bg); }
.md warning { --c: var(--warning); --c-bg: var(--warning-bg); }
.md :is(info, danger, success, warning)[t] { margin-top: 2.75rem; border-top: 4px solid var(--c); }
.md :is(info, danger, success, warning)[t]::before {
  content: attr(t);
  position: absolute;
  top: -2em;
  left: -4px;
  padding: 0.25rem 0.5rem;
  border-radius: 0.375rem 0.375rem 0 0;
  background: var(--c);
  color: #fff;
  font-size: 0.875rem;
  line-height: 1.3;
}

/* 目錄（Notion 式） */
.toc {
  position: fixed;
  top: 6rem;
  right: 1.25rem;
  z-index: 50;
  display: none;
}

@media (min-width: 1280px) { .toc { display: block; } }

.toc ol {
  list-style: none;
  margin: 0;
  padding: 0.5rem 0;
  display: flex;
  flex-direction: column;
  align-items: flex-end;
  gap: 0.75rem;
  max-height: calc(100dvh - 8rem);
  overflow: hidden;
}

.toc a { display: block; position: relative; }
.toc a::after {
  content: "";
  display: block;
  width: 1rem;
  height: 2px;
  border-radius: 1px;
  background: var(--line);
  transition: background-color 0.2s;
}
.toc .toc-h3 a::after { width: 0.75rem; }
.toc .toc-h4 a::after { width: 0.5rem; }
.toc a.active::after { background: var(--heading); }
.toc span { display: none; }

.toc:is(:hover, :focus-within) ol {
  align-items: stretch;
  gap: 0;
  width: 15rem;
  padding: 0.5rem;
  overflow-y: auto;
  border: 1px solid var(--line);
  border-radius: 0.5rem;
  background: var(--panel);
  box-shadow: var(--shadow);
  animation: toc-in 0.15s ease-out;
}
.toc:is(:hover, :focus-within) a::after { display: none; }
.toc:is(:hover, :focus-within) span {
  display: block;
  padding: 0.25rem 0.5rem;
  border-radius: 0.25rem;
  font-size: 0.875rem;
  line-height: 1.4;
  color: var(--muted);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.toc:is(:hover, :focus-within) .toc-h3 span { padding-left: 1.25rem; }
.toc:is(:hover, :focus-within) .toc-h4 span { padding-left: 2rem; }
.toc a:hover span { background: var(--surface); color: var(--text); }
.toc a.active span { color: var(--accent); }

@keyframes toc-in {
  from { opacity: 0; translate: 0.5rem 0; }
}

/* 留言 */
.comments { margin-top: 3rem; padding-top: 1.5rem; border-top: 1px solid var(--line); }
.comments h2 { margin: 0 0 1rem; font-size: 1.25rem; font-weight: 700; color: var(--heading); }
.comments h2 small { font-size: 0.875rem; font-weight: 400; color: var(--muted); }
.comment-list, .comment-children ol { list-style: none; margin: 0; padding: 0; }
.comment { padding: 0.75rem 0 0.75rem 0.75rem; border-left: 1px solid var(--line); }
.comment + .comment { margin-top: 0.5rem; }
.comment-meta { display: flex; flex-wrap: wrap; align-items: baseline; gap: 0.75rem; font-size: 0.875rem; color: var(--muted); }
.comment-author { font-weight: 500; color: var(--heading); }
.comment-meta a { margin-left: auto; color: var(--accent); }
.comment-content p { margin: 0.25rem 0; }
.comment-children { margin-top: 0.5rem; }

.respond { margin-top: 1.5rem; }
.cancel-comment-reply { font-size: 0.875rem; margin-bottom: 0.5rem; }
.cancel-comment-reply a { color: var(--accent); }
.comment-form { display: grid; gap: 0.5rem; }
.comment-fields { display: grid; grid-template-columns: repeat(auto-fit, minmax(10rem, 1fr)); gap: 0.5rem; }
.comment-form :is(input, textarea) {
  width: 100%;
  padding: 0.5rem 0.75rem;
  border: 1px solid var(--line);
  border-radius: 0.375rem;
  background: transparent;
  color: var(--text);
  font: inherit;
}
.comment-form textarea { resize: vertical; }
.comment-form :is(input, textarea):focus { outline: none; border-color: var(--accent); }
.comment-form button {
  justify-self: end;
  padding: 0.375rem 1.25rem;
  border-radius: 0.375rem;
  background: var(--accent);
  color: var(--bg);
  font-weight: 500;
}

/* 關於我 */
.about { max-width: 36rem; display: grid; justify-items: center; text-align: center; gap: 1rem; padding-top: 1.5rem; }
.about .avatar { width: 10rem; margin: 0; }
.about h1 { margin: 0; font-size: 2rem; font-weight: 700; color: var(--heading); }
.about .md { color: var(--soft); }
.about .side-links { margin: 0.5rem 0 0; display: flex; flex-wrap: wrap; justify-content: center; gap: 0.5rem 1.5rem; }

/* 404 */
.not-found { flex: 1; display: grid; place-items: center; align-content: center; gap: 1rem; }
.not-found img { max-width: min(100%, 40rem); }
.not-found a { color: var(--accent); }
.dark .only-light, :root:not(.dark) .only-dark { display: none; }

.footer { margin-top: auto; padding-block: 1.25rem; text-align: center; font-size: 0.875rem; color: var(--muted); }

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