/* Kho tri thức — trình đọc tối giản, phong cách học thuật.
   Ba giao diện: light / dark / paper. Đáp ứng mobile · tablet · desktop. */

:root {
  --font-serif: "Literata", "Iowan Old Style", "Palatino Linotype", Palatino,
    "Book Antiqua", Georgia, serif;
  --font-mono: "IBM Plex Mono", "SF Mono", Menlo, Consolas, monospace;
  --font-sans: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;

  --measure: 40rem;            /* độ rộng dòng tối ưu cho việc đọc */
  --measure-wide: min(58rem, 94vw);
  --radius: 6px;
  --radius-pill: 999px;
  --transition: 0.18s ease;

  --space-block: clamp(1.5rem, 2vw + 1rem, 2.25rem);
}

/* ---------- Bảng màu ---------- */

:root,
[data-theme="light"] {
  --bg: #fbfbfa;
  --bg-elevated: #ffffff;
  --bg-subtle: #f3f4f6;
  --text: #1b1f24;
  --text-muted: #5f6671;
  --text-faint: #8a909b;
  --border: #e6e8ec;
  --border-strong: #d3d7de;
  --link: #1f5299;
  --link-hover: #123a73;
  --code-bg: #f1f3f6;
  --accent: #2f6e4a;
  --accent-soft: rgba(47, 110, 74, 0.1);
  --header-bg: rgba(251, 251, 250, 0.82);
  --shadow: 0 1px 2px rgba(20, 24, 31, 0.05);
}

[data-theme="dark"] {
  --bg: #121419;
  --bg-elevated: #191c22;
  --bg-subtle: #20242c;
  --text: #e7e9ee;
  --text-muted: #9aa1ad;
  --text-faint: #717886;
  --border: #2a2f38;
  --border-strong: #39404b;
  --link: #84bcff;
  --link-hover: #aed5ff;
  --code-bg: #20242c;
  --accent: #7fc79a;
  --accent-soft: rgba(127, 199, 154, 0.14);
  --header-bg: rgba(18, 20, 25, 0.82);
  --shadow: 0 1px 2px rgba(0, 0, 0, 0.4);
}

[data-theme="paper"] {
  --bg: #f1e7d0;
  --bg-elevated: #f8f1e1;
  --bg-subtle: #ebdfc4;
  --text: #322b1c;
  --text-muted: #6c5f47;
  --text-faint: #8c7e62;
  --border: #ddceb0;
  --border-strong: #cdba97;
  --link: #3f5630;
  --link-hover: #2a3a1f;
  --code-bg: #e9ddc2;
  --accent: #8a5a2b;
  --accent-soft: rgba(138, 90, 43, 0.12);
  --header-bg: rgba(241, 231, 208, 0.85);
  --shadow: 0 1px 2px rgba(50, 43, 28, 0.08);
}

/* ---------- Cơ bản ---------- */

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

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

body {
  margin: 0;
  min-height: 100vh;
  font-family: var(--font-serif);
  font-size: 1.0625rem;
  line-height: 1.7;
  color: var(--text);
  background: var(--bg);
  -webkit-font-smoothing: antialiased;
  text-rendering: optimizeLegibility;
  transition: background var(--transition), color var(--transition);
}

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

.skip-link {
  position: absolute;
  left: -999px;
  top: 0;
  z-index: 100;
  padding: 0.5rem 0.9rem;
  background: var(--accent);
  color: #fff;
  font-family: var(--font-sans);
  font-size: 0.8rem;
  border-radius: var(--radius);
}

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

/* ---------- Header ---------- */

.site-header {
  position: sticky;
  top: 0;
  z-index: 10;
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  justify-content: space-between;
  gap: 0.6rem 1.25rem;
  padding: 0.7rem clamp(1rem, 4vw, 2.25rem);
  background: var(--header-bg);
  border-bottom: 1px solid var(--border);
  backdrop-filter: saturate(140%) blur(8px);
  -webkit-backdrop-filter: saturate(140%) blur(8px);
}

.site-brand {
  font-family: var(--font-sans);
  font-size: 0.9rem;
  font-weight: 600;
  letter-spacing: 0.01em;
  text-decoration: none;
  color: var(--text);
}

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

.site-nav {
  display: flex;
  gap: 1.1rem;
  margin-right: auto;
  margin-left: 1.5rem;
  font-family: var(--font-sans);
  font-size: 0.82rem;
}

.site-nav a {
  color: var(--text-muted);
  text-decoration: none;
  padding-bottom: 2px;
  border-bottom: 1px solid transparent;
  transition: color var(--transition), border-color var(--transition);
}

.site-nav a:hover {
  color: var(--link);
  border-bottom-color: var(--link);
}

/* Bộ chọn giao diện kiểu segmented control */
.theme-switcher {
  display: inline-flex;
  padding: 2px;
  gap: 2px;
  border: 1px solid var(--border-strong);
  border-radius: var(--radius-pill);
  background: var(--bg-subtle);
  font-family: var(--font-sans);
  font-size: 0.72rem;
}

.theme-switcher button {
  padding: 0.28rem 0.7rem;
  border: none;
  border-radius: var(--radius-pill);
  background: transparent;
  color: var(--text-muted);
  cursor: pointer;
  line-height: 1.4;
  transition: background var(--transition), color var(--transition);
}

.theme-switcher button:hover {
  color: var(--text);
}

.theme-switcher button[aria-pressed="true"] {
  background: var(--bg-elevated);
  color: var(--text);
  box-shadow: var(--shadow);
}

/* ---------- Khung nội dung ---------- */

.site-main {
  max-width: var(--measure);
  margin: 0 auto;
  padding: clamp(1.75rem, 5vw, 3.25rem) clamp(1rem, 4vw, 2rem) 4rem;
}

.site-main--wide,
.site-main--index {
  max-width: var(--measure-wide);
}

.site-footer {
  max-width: var(--measure-wide);
  margin: 0 auto;
  padding: 1.5rem clamp(1rem, 4vw, 2rem) 2.5rem;
  border-top: 1px solid var(--border);
  font-family: var(--font-sans);
  font-size: 0.74rem;
  color: var(--text-faint);
  text-align: center;
}

/* ---------- Bài viết ---------- */

.article {
  word-wrap: break-word;
  overflow-wrap: break-word;
}

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

.article h1 {
  font-size: clamp(1.7rem, 1rem + 2.6vw, 2.2rem);
  font-weight: 600;
  line-height: 1.2;
  letter-spacing: -0.02em;
  margin: 0 0 1.5rem;
}

.article h2 {
  font-size: 1.3rem;
  font-weight: 600;
  line-height: 1.3;
  letter-spacing: -0.01em;
  margin: var(--space-block) 0 0.6rem;
}

.article h3 {
  font-size: 1.08rem;
  font-weight: 600;
  margin: 1.6rem 0 0.45rem;
}

.article p {
  margin: 0 0 1.05rem;
}

.article ul,
.article ol {
  margin: 0 0 1.05rem;
  padding-left: 1.4rem;
}

.article li {
  margin-bottom: 0.4rem;
}

.article li::marker {
  color: var(--text-faint);
}

.article a {
  color: var(--link);
  text-decoration: underline;
  text-decoration-color: color-mix(in srgb, var(--link) 35%, transparent);
  text-decoration-thickness: 1px;
  text-underline-offset: 2px;
  transition: color var(--transition), text-decoration-color var(--transition);
}

.article a:hover {
  color: var(--link-hover);
  text-decoration-color: currentColor;
}

.article strong {
  font-weight: 600;
}

.article blockquote {
  margin: 1.25rem 0;
  padding: 0.4rem 0 0.4rem 1.1rem;
  border-left: 3px solid var(--accent);
  color: var(--text-muted);
  font-style: italic;
}

.article hr {
  border: none;
  border-top: 1px solid var(--border);
  margin: var(--space-block) 0;
}

/* Bảng */
.article .table-wrap {
  margin: 1.25rem 0;
  overflow-x: auto;
  -webkit-overflow-scrolling: touch;
}

.article table {
  width: 100%;
  margin: 1.25rem 0;
  border-collapse: collapse;
  font-family: var(--font-sans);
  font-size: 0.9rem;
}

.article th,
.article td {
  padding: 0.55rem 0.8rem;
  border: 1px solid var(--border);
  text-align: left;
  vertical-align: top;
}

.article th {
  background: var(--bg-subtle);
  font-weight: 600;
}

.article tbody tr:nth-child(even) td {
  background: color-mix(in srgb, var(--bg-subtle) 45%, transparent);
}

/* Mã nguồn */
.article code {
  font-family: var(--font-mono);
  font-size: 0.86em;
  padding: 0.12em 0.36em;
  border-radius: 4px;
  background: var(--code-bg);
  border: 1px solid color-mix(in srgb, var(--border) 60%, transparent);
}

.article pre {
  margin: 1.25rem 0;
  padding: 1rem 1.1rem;
  overflow-x: auto;
  border-radius: var(--radius);
  background: var(--code-bg);
  border: 1px solid var(--border);
  font-size: 0.85rem;
  line-height: 1.55;
}

.article pre code {
  padding: 0;
  background: none;
  border: none;
  font-size: inherit;
}

.article .mermaid {
  margin: 1.5rem 0;
  padding: 1rem;
  text-align: center;
  background: var(--bg-elevated);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  overflow-x: auto;
}

.article img {
  max-width: 100%;
  height: auto;
  border-radius: var(--radius);
}

/* ---------- Mục phụ trợ: Nguồn tham khảo · Liên kết · Tags ---------- */

.article-meta-heading {
  font-family: var(--font-sans);
  font-size: 0.82rem;
  font-weight: 600;
  letter-spacing: 0.06em;
  text-transform: uppercase;
  color: var(--text-faint);
  margin-top: var(--space-block);
  padding-top: 1rem;
  border-top: 1px solid var(--border);
}

.article-meta-heading + ul {
  font-family: var(--font-sans);
  font-size: 0.92rem;
  line-height: 1.55;
}

.article-meta-heading + ul a {
  text-decoration-color: color-mix(in srgb, var(--link) 25%, transparent);
}

/* Tags dạng chip */
ul.tag-list {
  list-style: none;
  display: flex;
  flex-wrap: wrap;
  gap: 0.5rem;
  padding-left: 0;
  margin-top: 0.85rem;
}

ul.tag-list li {
  margin: 0;
}

ul.tag-list a {
  display: inline-block;
  padding: 0.22rem 0.75rem;
  font-family: var(--font-sans);
  font-size: 0.78rem;
  letter-spacing: 0.01em;
  color: var(--text-muted);
  text-decoration: none;
  background: var(--bg-subtle);
  border: 1px solid var(--border-strong);
  border-radius: var(--radius-pill);
  transition: color var(--transition), background var(--transition),
    border-color var(--transition);
}

ul.tag-list a:hover {
  color: var(--accent);
  background: var(--accent-soft);
  border-color: var(--accent);
}

/* ---------- Trang chủ: mục lục 3 cột ---------- */

.site-main--index {
  max-width: min(72rem, 95vw);
}

.index-summary {
  font-family: var(--font-sans);
  font-size: 0.85rem;
  color: var(--text-muted);
  margin: -0.75rem 0 0;
}

.index-layout {
  display: grid;
  grid-template-columns: 1.75rem minmax(0, 1fr) 12.5rem;
  grid-template-areas: "alpha list tags";
  gap: clamp(1.25rem, 3vw, 2.5rem);
  align-items: start;
  margin-top: 1.75rem;
}

/* Rail chữ cái */
.index-alpha {
  grid-area: alpha;
  position: sticky;
  top: 4rem;
  align-self: start;
  display: flex;
  flex-direction: column;
  gap: 0.1rem;
  font-family: var(--font-sans);
  font-size: 0.78rem;
  font-weight: 500;
}

.index-alpha a {
  display: block;
  text-align: center;
  padding: 0.16rem 0;
  color: var(--text-muted);
  text-decoration: none;
  border-radius: 4px;
  transition: color var(--transition), background var(--transition);
}

.index-alpha a:hover {
  color: var(--accent);
  background: var(--accent-soft);
}

/* Cột giữa: danh sách tri thức */
.index-list {
  grid-area: list;
}

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

.index-list li {
  margin: 0;
  border-bottom: 1px solid var(--border);
  scroll-margin-top: 4.75rem;
}

.index-list li a {
  display: block;
  padding: 0.6rem 0.25rem;
  font-size: 1rem;
  color: var(--text);
  text-decoration: none;
  transition: color var(--transition), padding var(--transition);
}

.index-list li:first-child a {
  padding-top: 0.16rem;
}

.index-list li a:hover {
  color: var(--link);
  padding-left: 0.5rem;
}

/* Cột phải: tag */
.index-tags {
  grid-area: tags;
  position: sticky;
  top: 4rem;
  align-self: start;
}

.article .index-aside-title {
  font-family: var(--font-sans);
  font-size: 0.78rem;
  font-weight: 600;
  letter-spacing: 0.06em;
  text-transform: uppercase;
  color: var(--text-faint);
  margin: 0 0 0.85rem;
  padding-top: 0.16rem;
}

.tag-list--index {
  margin-top: 0;
}

.tag-list--index .tag-count {
  margin-left: 0.4rem;
  font-size: 0.72rem;
  color: var(--text-faint);
}

.index-aside-more {
  margin: 1rem 0 0;
  font-family: var(--font-sans);
  font-size: 0.82rem;
}

@media (max-width: 880px) {
  .index-layout {
    grid-template-columns: 1fr;
    grid-template-areas:
      "alpha"
      "list"
      "tags";
    gap: 1.25rem;
  }

  .index-alpha {
    position: sticky;
    top: 3.3rem;
    z-index: 5;
    flex-direction: row;
    flex-wrap: wrap;
    gap: 0.1rem 0.25rem;
    padding: 0.45rem 0;
    background: var(--header-bg);
    backdrop-filter: saturate(140%) blur(8px);
    -webkit-backdrop-filter: saturate(140%) blur(8px);
  }

  .index-alpha a {
    padding: 0.12rem 0.4rem;
  }

  .index-tags {
    position: static;
    border-top: 1px solid var(--border);
    padding-top: 1.25rem;
  }
}

/* ---------- Trang tag ---------- */

.tag-page-meta {
  font-family: var(--font-sans);
  font-size: 0.85rem;
  color: var(--text-muted);
  margin: -0.75rem 0 1.5rem;
}

.tag-cloud {
  display: flex;
  flex-wrap: wrap;
  align-items: baseline;
  justify-content: center;
  gap: 0.6rem 1.15rem;
  margin: 2.25rem 0;
  line-height: 1.5;
  font-family: var(--font-sans);
}

.tag-cloud-item {
  color: var(--link);
  text-decoration: none;
  font-weight: 500;
  transition: color var(--transition);
  white-space: nowrap;
}

.tag-cloud-item:hover {
  color: var(--link-hover);
  text-decoration: underline;
  text-underline-offset: 3px;
}

.tag-index-note {
  font-family: var(--font-sans);
  font-size: 0.8rem;
  color: var(--text-faint);
  text-align: center;
}

/* ---------- Trang đồ thị tri thức ---------- */

.site-main--graph {
  max-width: min(82rem, 96vw);
}

.graph-intro {
  font-family: var(--font-sans);
  font-size: 0.9rem;
  color: var(--text-muted);
  margin: -0.5rem 0 1.25rem;
  max-width: 48rem;
}

.graph-toolbar {
  display: flex;
  flex-wrap: wrap;
  gap: 0.6rem;
  align-items: center;
  margin-bottom: 1rem;
  font-family: var(--font-sans);
}

.graph-search,
.graph-tag-filter,
.graph-btn {
  font-family: var(--font-sans);
  font-size: 0.85rem;
  color: var(--text);
  background: var(--bg-elevated);
  border: 1px solid var(--border-strong);
  border-radius: var(--radius);
  padding: 0.4rem 0.7rem;
}

.graph-search {
  flex: 1 1 14rem;
  min-width: 10rem;
}

.graph-btn {
  cursor: pointer;
  color: var(--text-muted);
  transition: color var(--transition), border-color var(--transition);
}

.graph-btn:hover {
  color: var(--text);
  border-color: var(--text-muted);
}

.graph-btn[aria-pressed="true"] {
  color: var(--accent);
  border-color: var(--accent);
  background: var(--accent-soft);
}

.graph-stage {
  position: relative;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: var(--bg-elevated);
  overflow: hidden;
}

#kb-graph {
  position: relative;
  z-index: 1;
  width: 100%;
  height: min(82vh, 900px);
  background: transparent;
}

.graph-field {
  position: absolute;
  inset: 0;
  z-index: 0;
  width: 100%;
  height: 100%;
  pointer-events: none;
  filter: blur(7px);
}

.graph-empty {
  padding: 2rem;
  text-align: center;
  color: var(--text-muted);
  font-family: var(--font-sans);
}

.graph-legend {
  position: absolute;
  top: 0.75rem;
  left: 0.75rem;
  z-index: 2;
  max-width: 14rem;
  display: flex;
  flex-wrap: wrap;
  gap: 0.3rem 0.7rem;
  padding: 0.6rem 0.75rem;
  background: color-mix(in srgb, var(--bg-elevated) 88%, transparent);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  font-family: var(--font-sans);
  font-size: 0.72rem;
  color: var(--text-muted);
  backdrop-filter: blur(4px);
  -webkit-backdrop-filter: blur(4px);
}

.graph-legend-item {
  display: inline-flex;
  align-items: center;
  gap: 0.3rem;
  white-space: nowrap;
}

.graph-legend-dot {
  width: 0.7rem;
  height: 0.7rem;
  border-radius: 50%;
  flex: none;
}

.graph-field-toggle {
  position: absolute;
  top: 0.75rem;
  right: 0.75rem;
  z-index: 2;
  padding: 0.35rem 0.7rem;
  font-family: var(--font-sans);
  font-size: 0.76rem;
  color: var(--text-muted);
  background: color-mix(in srgb, var(--bg-elevated) 88%, transparent);
  border: 1px solid var(--border-strong);
  border-radius: var(--radius-pill);
  cursor: pointer;
  backdrop-filter: blur(4px);
  -webkit-backdrop-filter: blur(4px);
  transition: color var(--transition), border-color var(--transition),
    background var(--transition);
}

.graph-field-toggle:hover {
  color: var(--text);
  border-color: var(--text-muted);
}

.graph-field-toggle[aria-pressed="true"] {
  color: var(--accent);
  border-color: var(--accent);
  background: var(--accent-soft);
}

.graph-panel {
  position: absolute;
  top: 0;
  right: 0;
  z-index: 3;
  width: min(20rem, 82vw);
  max-height: 100%;
  overflow-y: auto;
  padding: 1.1rem 1.25rem 1.4rem;
  background: var(--bg-elevated);
  border-left: 1px solid var(--border);
  font-family: var(--font-sans);
  box-shadow: -4px 0 16px rgba(0, 0, 0, 0.06);
}

.graph-panel h2 {
  font-family: var(--font-serif);
  font-size: 1.15rem;
  font-weight: 600;
  margin: 0 2rem 0.4rem 0;
  line-height: 1.3;
}

.graph-panel-tags {
  font-size: 0.78rem;
  color: var(--text-faint);
  margin: 0 0 0.85rem;
}

.graph-open {
  display: inline-block;
  font-size: 0.85rem;
  color: var(--link);
  margin-bottom: 1rem;
}

.graph-panel-label {
  font-size: 0.72rem;
  text-transform: uppercase;
  letter-spacing: 0.06em;
  color: var(--text-faint);
  margin: 0 0 0.5rem;
}

.graph-links {
  list-style: none;
  padding: 0;
  margin: 0;
  font-size: 0.85rem;
}

.graph-links li {
  padding: 0.5rem 0;
  border-top: 1px solid var(--border);
}

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

.graph-links a:hover {
  color: var(--link);
}

.graph-reason {
  display: block;
  font-size: 0.76rem;
  color: var(--text-muted);
  margin-top: 0.15rem;
  font-style: italic;
}

.graph-panel-close {
  position: absolute;
  top: 0.6rem;
  right: 0.7rem;
  border: none;
  background: transparent;
  color: var(--text-muted);
  font-size: 1.3rem;
  line-height: 1;
  cursor: pointer;
}

.graph-panel-close:hover {
  color: var(--text);
}

.graph-tooltip {
  position: absolute;
  z-index: 50;
  max-width: 22rem;
  padding: 0.4rem 0.65rem;
  font-family: var(--font-sans);
  font-size: 0.78rem;
  color: var(--text);
  background: var(--bg-elevated);
  border: 1px solid var(--border-strong);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  pointer-events: none;
}

@media (max-width: 600px) {
  #kb-graph {
    height: 64vh;
  }

  .graph-panel {
    width: 100%;
    border-left: none;
    border-top: 1px solid var(--border);
  }
}

/* Pygments */
.codehilite .hll {
  background-color: rgba(127, 127, 127, 0.15);
}

/* ---------- Đáp ứng ---------- */

@media (max-width: 600px) {
  body {
    font-size: 1rem;
    line-height: 1.65;
  }

  .site-header {
    gap: 0.5rem 0.75rem;
  }

  .site-nav {
    margin-left: 0;
    order: 3;
    width: 100%;
  }

  .article h2 {
    font-size: 1.2rem;
  }
}

@media (min-width: 1024px) {
  body {
    font-size: 1.09rem;
  }
}

/* ---------- In ấn ---------- */

@media print {
  .site-header,
  .site-footer,
  .skip-link {
    display: none;
  }

  body {
    background: #fff;
    color: #000;
    font-size: 11pt;
  }

  .site-main {
    max-width: none;
  }

  .article a {
    color: #000;
    text-decoration: underline;
  }
}

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