/* thu-vien.css — Bộ giao diện Thư viện Nghiên cứu.
 *
 *   TÁCH HẲN khỏi kieu.css của phần Bốc Dịch. Cố ý: thư viện là văn bản dài, đọc liền mạch,
 *   có sơ đồ SVG và bảng — nhu cầu sắp chữ khác hẳn một trang tra quẻ. Trộn hai bộ vào nhau
 *   thì cả hai cùng khổ. Trang thư viện KHÔNG nạp kieu.css.
 *
 *   Mọi màu đi qua biến. Bản tối định nghĩa lại đúng bộ biến ấy, không viết lại quy tắc nào —
 *   thêm một thành phần thì nó tự có bản tối.
 */

:root {
  color-scheme: light;
  --paper:      #FBFAF7;   /* nền trang */
  --surface:    #FFFFFF;   /* mặt thẻ, khung hình */
  --surface-2:  #F2F0EA;   /* mặt chìm: ô mã, nền phụ trong SVG */
  --ink:        #17191C;   /* chữ chính */
  --muted:      #4E545C;   /* chữ phụ, còn đọc thoải mái */
  --faint:      #7C838C;   /* chú thích, nhãn nhỏ */
  --rule:       #E3E0D8;   /* kẻ mảnh */
  --rule-strong:#C9C5BA;   /* kẻ đậm, viền bảng */

  --accent:     #126E6A;   /* xanh mòng két — nhấn chính */
  --accent-deep:#0B4C49;
  --accent-soft:#E2F0EE;
  --garnet:     #9B2C3B;   /* đỏ garnet — cảnh báo, phủ định */
  --garnet-soft:#F7E6E8;
  --indigo:     #3A4E9B;   /* chàm — nhóm, phân loại */
  --indigo-soft:#E8EAF6;
  --ochre:      #8E6212;   /* hoàng thổ — nguyên tắc, ghi chú */
  --ochre-soft: #F8EFDC;

  --sans:  "Inter", system-ui, -apple-system, "Segoe UI", sans-serif;
  --serif: "Source Serif 4", Charter, Georgia, "Times New Roman", serif;
  --mono:  "IBM Plex Mono", ui-monospace, "SFMono-Regular", Menlo, monospace;

  --shell: 46rem;          /* bề rộng cột chữ — quanh 68 ký tự, mức dễ đọc nhất */
  --wide:  72rem;          /* bề rộng tối đa của hình và bảng */
}

:root[data-theme="dark"] {
  color-scheme: dark;
  --paper:      #121417;
  --surface:    #1A1D21;
  --surface-2:  #23272C;
  --ink:        #E7E5E0;
  --muted:      #AFB5BD;
  --faint:      #848B94;
  --rule:       #2C3136;
  --rule-strong:#414850;

  --accent:     #4FBDB4;
  --accent-deep:#9BDDD6;
  --accent-soft:#12302F;
  --garnet:     #E1808C;
  --garnet-soft:#331A1E;
  --indigo:     #8E9CE0;
  --indigo-soft:#1B2040;
  --ochre:      #D9A94E;
  --ochre-soft: #332811;
}

@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    color-scheme: dark;
    --paper:#121417; --surface:#1A1D21; --surface-2:#23272C;
    --ink:#E7E5E0; --muted:#AFB5BD; --faint:#848B94;
    --rule:#2C3136; --rule-strong:#414850;
    --accent:#4FBDB4; --accent-deep:#9BDDD6; --accent-soft:#12302F;
    --garnet:#E1808C; --garnet-soft:#331A1E;
    --indigo:#8E9CE0; --indigo-soft:#1B2040;
    --ochre:#D9A94E; --ochre-soft:#332811;
  }
}

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

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

body {
  margin: 0;
  background: var(--paper);
  color: var(--ink);
  font-family: var(--sans);
  font-size: 17px;
  line-height: 1.68;
  -webkit-font-smoothing: antialiased;
  text-rendering: optimizeLegibility;
}

.shell { width: 100%; max-width: var(--shell); margin-inline: auto; padding-inline: 20px; }

a { color: var(--accent); text-underline-offset: .18em; text-decoration-thickness: from-font; }
a:hover { color: var(--accent-deep); }

.skip {
  position: absolute; left: -9999px; top: 0; z-index: 99;
  background: var(--accent); color: #fff; padding: 10px 16px; border-radius: 0 0 4px 0;
}
.skip:focus { left: 0; }

:where(a, button, summary):focus-visible {
  outline: 2px solid var(--accent); outline-offset: 2px; border-radius: 2px;
}

/* ── Thanh trên cùng ─────────────────────────────────────────────────────── */
.topbar {
  position: sticky; top: 0; z-index: 20;
  background: color-mix(in srgb, var(--paper) 88%, transparent);
  backdrop-filter: saturate(180%) blur(10px);
  border-bottom: 1px solid var(--rule);
}
.topbar-in {
  display: flex; align-items: center; gap: 16px;
  max-width: var(--wide); margin-inline: auto; padding: 11px 20px;
}
.topbar-spacer { flex: 1; }
.brand { color: var(--muted); text-decoration: none; font-size: 14.5px; white-space: nowrap; }
.brand b { color: var(--ink); font-weight: 600; }
.brand:hover { color: var(--accent); }
.brand:hover b { color: var(--accent); }

.iconbtn {
  display: grid; place-items: center; width: 34px; height: 34px; padding: 0;
  background: none; border: 1px solid var(--rule); border-radius: 6px;
  color: var(--muted); cursor: pointer;
}
.iconbtn:hover { color: var(--ink); border-color: var(--rule-strong); }
.iconbtn svg { width: 17px; height: 17px; fill: none; stroke: currentColor; stroke-width: 1.7; stroke-linecap: round; stroke-linejoin: round; }
/* Hiện đúng một biểu tượng: chế độ sáng thì mời sang tối (mặt trăng), và ngược lại. */
.ico-sun { display: none; }
:root[data-theme="dark"] .ico-moon { display: none; }
:root[data-theme="dark"] .ico-sun { display: block; }
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) .ico-moon { display: none; }
  :root:not([data-theme="light"]) .ico-sun { display: block; }
}

.progress-track { height: 2px; background: transparent; }
.progress-bar { height: 100%; width: 0; background: var(--accent); transition: width .1s linear; }

/* ── Đầu bài ─────────────────────────────────────────────────────────────── */
.plate { position: relative; overflow: hidden; border-bottom: 1px solid var(--rule); background: var(--surface); }
.plate-grid {
  position: absolute; inset: 0; pointer-events: none; opacity: .5;
  background-image: linear-gradient(var(--rule) 1px, transparent 1px),
                    linear-gradient(90deg, var(--rule) 1px, transparent 1px);
  background-size: 34px 34px;
  mask-image: radial-gradient(120% 90% at 78% 0%, #000 0%, transparent 72%);
}
.plate-in { position: relative; padding-block: clamp(38px, 7vw, 76px) clamp(30px, 5vw, 52px); }

.eyebrow {
  margin: 0; font-size: 12.5px; font-weight: 600; letter-spacing: .09em; text-transform: uppercase;
  color: var(--accent);
}
.eyebrow .sep { color: var(--rule-strong); margin-inline: .45em; font-weight: 400; }

.plate-title {
  margin: .42em 0 0;
  font-family: var(--serif); font-weight: 600;
  font-size: clamp(2.05rem, 5.4vw, 3.3rem); line-height: 1.11; letter-spacing: -.018em;
  text-wrap: balance;
}
.plate-sub {
  margin: .85em 0 0; max-width: 40rem;
  font-size: 1.12rem; line-height: 1.58; color: var(--muted); text-wrap: pretty;
}

.meta-strip {
  display: grid; grid-template-columns: repeat(auto-fit, minmax(9.5rem, 1fr));
  gap: 14px 26px; margin: 30px 0 0; padding-top: 20px; border-top: 1px solid var(--rule);
}
.meta-strip > div { min-width: 0; }
.meta-strip .k {
  margin: 0; font-size: 11.5px; font-weight: 600; letter-spacing: .07em;
  text-transform: uppercase; color: var(--faint);
}
.meta-strip .v { margin: 4px 0 0; font-size: 14.5px; color: var(--ink); }

/* ── Mục lục ─────────────────────────────────────────────────────────────── */
.toc {
  margin: 30px 0 4px; padding: 16px 18px;
  background: var(--surface); border: 1px solid var(--rule); border-radius: 7px;
}
.toc .tag {
  display: block; font-size: 11.5px; font-weight: 600; letter-spacing: .07em;
  text-transform: uppercase; color: var(--faint); margin-bottom: 10px;
}
.toc ol { margin: 0; padding: 0; list-style: none; counter-reset: toc; }
.toc li { counter-increment: toc; margin: 0; }
.toc a {
  display: flex; gap: 12px; padding: 5px 0;
  color: var(--muted); text-decoration: none; font-size: 15px; line-height: 1.45;
}
.toc a::before {
  content: counter(toc, decimal-leading-zero);
  flex: none; width: 1.7em; font-family: var(--mono); font-size: 12px;
  color: var(--faint); padding-top: .18em;
}
.toc a:hover { color: var(--accent); }
.toc a[aria-current="true"] { color: var(--accent); font-weight: 600; }

/* ── Thân bài ────────────────────────────────────────────────────────────── */
.prose { padding-block: 26px 64px; }
/* KHÔNG đặt max-width:100% cho con trực tiếp: hình, bảng và dải thẻ cố ý rộng hơn cột chữ
   bằng lề âm, mà max-width sẽ kẹp bề rộng lại — hậu quả là chúng lệch hẳn sang trái. */
.prose > * { min-width: 0; }
.prose img { max-width: 100%; height: auto; }
.prose pre { overflow-x: auto; }

.prose p { margin: 0 0 1.15em; text-wrap: pretty; }
.prose .lead {
  font-size: 1.2rem; line-height: 1.58; color: var(--muted);
  margin-bottom: 1.5em;
}
.prose em { font-style: italic; }
.prose strong { font-weight: 600; color: var(--ink); }

h2.sec {
  margin: 2.5em 0 .75em; padding-top: 1.15em; border-top: 1px solid var(--rule);
  font-family: var(--serif); font-weight: 600; font-size: 1.72rem; line-height: 1.22;
  letter-spacing: -.012em; scroll-margin-top: 72px; text-wrap: balance;
}
h2.sec .num {
  display: block; margin-bottom: .42em;
  font-family: var(--sans); font-size: 12px; font-weight: 700;
  letter-spacing: .1em; text-transform: uppercase; color: var(--accent);
}
h3.sub {
  margin: 2em 0 .6em;
  font-family: var(--serif); font-weight: 600; font-size: 1.24rem; line-height: 1.3;
  scroll-margin-top: 72px;
}
.prose h4 { margin: 0 0 .4em; font-size: 1.02rem; font-weight: 600; line-height: 1.35; }

.term {
  font-weight: 600; color: var(--accent);
  border-bottom: 1px dotted currentColor; padding-bottom: .05em;
}
.mono { font-family: var(--mono); }

.prose ul, .prose ol { margin: 0 0 1.15em; padding-left: 1.35em; }
.prose li { margin: .32em 0; }

/* ── Hình và sơ đồ ───────────────────────────────────────────────────────── */
/* Hình rộng hơn cột chữ: sơ đồ SVG cần chỗ thở, chữ thì không. */
figure { margin: 2.1em 0; }
@media (min-width: 62rem) {
  figure { width: min(var(--wide), calc(100vw - 40px)); margin-inline: calc(50% - min(var(--wide), calc(100vw - 40px)) / 2); }
}
.frame {
  padding: 16px; background: var(--surface);
  border: 1px solid var(--rule); border-radius: 8px;
}
.frame svg { display: block; width: 100%; height: auto; color: var(--ink); }
.svg-title { font-family: var(--sans); font-size: 15px; font-weight: 700; }
.svg-label { font-family: var(--sans); font-size: 12.5px; }
figcaption {
  margin: 12px auto 0; max-width: var(--shell);
  font-size: 14px; line-height: 1.55; color: var(--muted); text-wrap: pretty;
}
figcaption b { color: var(--ink); font-weight: 600; }

/* ── Bảng ────────────────────────────────────────────────────────────────── */
.table-wrap { margin: 1.9em 0; overflow-x: auto; }
@media (min-width: 62rem) {
  .table-wrap { width: min(var(--wide), calc(100vw - 40px)); margin-inline: calc(50% - min(var(--wide), calc(100vw - 40px)) / 2); }
}
.table-wrap table { width: 100%; border-collapse: collapse; font-size: 14.5px; }
.table-wrap caption {
  caption-side: top; text-align: left; padding-bottom: 10px;
  font-size: 13.5px; color: var(--faint);
}
.table-wrap th, .table-wrap td {
  padding: 11px 14px; text-align: left; vertical-align: top;
  border-bottom: 1px solid var(--rule); line-height: 1.55;
}
.table-wrap thead th {
  font-size: 11.5px; font-weight: 700; letter-spacing: .06em; text-transform: uppercase;
  color: var(--faint); border-bottom: 1.5px solid var(--rule-strong); white-space: nowrap;
}
.table-wrap tbody tr:last-child td { border-bottom: 0; }
.table-wrap td strong { color: var(--ink); }

/* ── Khối nhấn ───────────────────────────────────────────────────────────── */
.callout, .disclaimer {
  margin: 1.9em 0; padding: 16px 18px;
  border-radius: 7px; font-size: 15.2px; line-height: 1.62;
  border: 1px solid var(--rule); background: var(--surface);
  border-left: 3px solid var(--rule-strong);
}
.callout > :first-child, .disclaimer > :first-child { margin-top: 0; }
.callout > :last-child, .disclaimer > :last-child { margin-bottom: 0; }
.callout p { margin: 0 0 .8em; }
.callout .tag {
  display: block; margin-bottom: .6em;
  font-size: 11.5px; font-weight: 700; letter-spacing: .07em; text-transform: uppercase;
}
.callout.note { border-left-color: var(--indigo); background: var(--indigo-soft); }
.callout.note .tag { color: var(--indigo); }
.callout.warn { border-left-color: var(--ochre); background: var(--ochre-soft); }
.callout.warn .tag { color: var(--ochre); }
.callout.risk { border-left-color: var(--garnet); background: var(--garnet-soft); }
.callout.risk .tag { color: var(--garnet); }
/* Lời miễn trừ: phải đọc được nhưng không át bài. Nền trung tính, viền garnet. */
.disclaimer { border-left-color: var(--garnet); background: var(--surface-2); font-size: 14.2px; color: var(--muted); }
.disclaimer strong { color: var(--ink); }

blockquote.pull {
  margin: 2.1em 0; padding: 0 0 0 22px;
  border-left: 2px solid var(--accent);
  font-family: var(--serif); font-size: 1.23rem; line-height: 1.5; color: var(--ink);
}
blockquote.pull p { margin: 0; text-wrap: pretty; }

/* ── Thẻ ý tưởng ─────────────────────────────────────────────────────────── */
.panel-row { display: grid; gap: 14px; margin: 1.9em 0; grid-template-columns: repeat(auto-fit, minmax(15.5rem, 1fr)); }
@media (min-width: 62rem) {
  .panel-row { width: min(var(--wide), calc(100vw - 40px)); margin-inline: calc(50% - min(var(--wide), calc(100vw - 40px)) / 2); }
}
.panel {
  padding: 16px 18px; background: var(--surface);
  border: 1px solid var(--rule); border-top: 2px solid var(--accent); border-radius: 0 0 7px 7px;
}
.panel .idx {
  display: block; margin-bottom: .5em;
  font-family: var(--mono); font-size: 11.5px; font-weight: 500;
  letter-spacing: .06em; text-transform: uppercase; color: var(--accent);
}
.panel p { margin: 0; font-size: 14.8px; line-height: 1.6; color: var(--muted); }
.panel p + p { margin-top: .7em; }

/* ── Số liệu ─────────────────────────────────────────────────────────────── */
.stat-row { display: grid; gap: 12px; margin: 1.9em 0; grid-template-columns: repeat(auto-fit, minmax(10rem, 1fr)); }
.stat {
  padding: 15px 16px; background: var(--surface);
  border: 1px solid var(--rule); border-radius: 7px;
}
.stat .k { display: block; font-size: 11.5px; font-weight: 600; letter-spacing: .06em; text-transform: uppercase; color: var(--faint); }
.stat .v { display: block; margin: .22em 0 .12em; font-family: var(--serif); font-size: 1.68rem; font-weight: 600; line-height: 1.1; color: var(--accent); }
.stat .d { display: block; font-size: 13px; line-height: 1.45; color: var(--muted); }

/* ── Các bước ────────────────────────────────────────────────────────────── */
.steps { margin: 1.9em 0; display: grid; gap: 18px; }
.step { display: grid; grid-template-columns: auto 1fr; gap: 16px; align-items: start; }
.step .dot {
  display: grid; place-items: center; width: 34px; height: 34px;
  font-family: var(--mono); font-size: 12.5px; font-weight: 600;
  color: var(--accent); background: var(--accent-soft);
  border: 1px solid var(--accent); border-radius: 50%;
}
.step p { margin: 0 0 .7em; font-size: 15.4px; }
.step > div > :last-child { margin-bottom: 0; }

/* ── Cuối bài ────────────────────────────────────────────────────────────── */
.endnote {
  margin: 3em 0 0; padding: 20px 22px;
  background: var(--surface-2); border-radius: 8px;
  font-size: 14.4px; line-height: 1.62; color: var(--muted);
}
.endnote h2 {
  margin: 0 0 .7em; font-family: var(--sans); font-size: 12px; font-weight: 700;
  letter-spacing: .09em; text-transform: uppercase; color: var(--faint);
}
.endnote p { margin: 0 0 .8em; }
.endnote p:last-child { margin-bottom: 0; }
.endnote strong { color: var(--ink); }

.pager { display: grid; gap: 12px; margin: 2.4em 0 0; grid-template-columns: repeat(auto-fit, minmax(14rem, 1fr)); }
.pager a {
  display: block; padding: 15px 17px; text-decoration: none;
  background: var(--surface); border: 1px solid var(--rule); border-radius: 7px;
}
.pager a:hover { border-color: var(--accent); }
.pager .k { display: block; font-size: 11.5px; font-weight: 600; letter-spacing: .06em; text-transform: uppercase; color: var(--faint); }
.pager .t { display: block; margin-top: .3em; font-size: 15.4px; color: var(--ink); line-height: 1.4; }
.pager a:hover .t { color: var(--accent); }

.sitefoot {
  border-top: 1px solid var(--rule); padding-block: 22px 40px;
  font-size: 13.5px; color: var(--faint);
}

/* ── Trang mục lục thư viện ──────────────────────────────────────────────── */
.kho-list { list-style: none; margin: 0; padding: 0; }
.kho-item { border-bottom: 1px solid var(--rule); }
.kho-item:first-child { border-top: 1px solid var(--rule); }
.kho-item > a {
  display: block; padding: 22px 0; text-decoration: none; color: inherit;
}
.kho-item > a:hover .kho-ten { color: var(--accent); }
.kho-linh {
  font-size: 11.5px; font-weight: 600; letter-spacing: .08em; text-transform: uppercase;
  color: var(--accent);
}
.kho-linh .sep { color: var(--rule-strong); margin-inline: .45em; font-weight: 400; }
.kho-ten {
  margin: .35em 0 0; font-family: var(--serif); font-weight: 600;
  font-size: 1.42rem; line-height: 1.22; letter-spacing: -.01em; text-wrap: balance;
}
.kho-tom { margin: .45em 0 0; font-size: 15.4px; line-height: 1.55; color: var(--muted); text-wrap: pretty; }
.kho-phu { margin: .6em 0 0; font-size: 13px; color: var(--faint); }
.kho-phu .sep { color: var(--rule-strong); margin-inline: .5em; }
.kho-trong { padding: 40px 0; color: var(--faint); }

@media (max-width: 40rem) {
  body { font-size: 16.5px; }
  .prose .lead { font-size: 1.1rem; }
  h2.sec { font-size: 1.44rem; }
  .frame { padding: 11px; border-radius: 6px; }
  .step { grid-template-columns: 1fr; gap: 8px; }
}

@media print {
  .topbar, .toc, .pager, .sitefoot, .skip { display: none; }
  body { background: #fff; color: #000; font-size: 11pt; }
  figure, .table-wrap, .panel-row { width: auto; margin-inline: 0; }
}
