/* ===== CompTIA lesson theme =====
   สว่าง: Notion บนพื้นควันบุหรี่ #E9E8E5 | มืด: Instagram */
:root {
  --bg: #E9E8E5;
  --surface: #F4F3F1;
  --surface-2: #E1E0DC;
  --border: #D3D1CC;
  --text: #2F2E2B;
  --muted: #6E6B65;
  --accent: #2E6FD8;
  --tip-bg: #F6EBD3; --tip-bd: #D9A441;
  --mem-bg: #DDEBE2; --mem-bd: #4E9A6B;
  --warn-bg: #F3DCDA; --warn-bd: #C8524A;
  --code-bg: #E4E2DE;
  --d1: #2E6FD8; --d2: #C8524A; --d3: #B7861C; --d4: #3E8E5E; --d5: #7A55C2;
  --shadow: 0 1px 2px rgba(0,0,0,.05);
  --on-dc: #fff;
  --radius: 10px;
  --top: calc(56px + env(safe-area-inset-top));
  --sidebar: 280px;
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #000; --surface: #121212; --surface-2: #1C1C1C; --border: #262626;
    --text: #F5F5F5; --muted: #A8A8A8; --accent: #4F9BFF;
    --tip-bg: #2A2312; --tip-bd: #D9A441; --mem-bg: #12251A; --mem-bd: #5CB37E;
    --warn-bg: #2B1514; --warn-bd: #E0675E; --code-bg: #1F1F1F;
    --d1: #4F9BFF; --d2: #FF6B61; --d3: #F2B53A; --d4: #5CC98A; --d5: #A98BFF;
    --shadow: none; --on-dc: #0B0B0B;
  }
}
:root[data-theme="dark"] {
  --bg: #000; --surface: #121212; --surface-2: #1C1C1C; --border: #262626;
  --text: #F5F5F5; --muted: #A8A8A8; --accent: #4F9BFF;
  --tip-bg: #2A2312; --tip-bd: #D9A441; --mem-bg: #12251A; --mem-bd: #5CB37E;
  --warn-bg: #2B1514; --warn-bd: #E0675E; --code-bg: #1F1F1F;
  --d1: #4F9BFF; --d2: #FF6B61; --d3: #F2B53A; --d4: #5CC98A; --d5: #A98BFF;
  --shadow: none; --on-dc: #0B0B0B;
}

* { box-sizing: border-box; }
html { scroll-behavior: smooth; scroll-padding-top: calc(var(--top) + 16px); }
body {
  margin: 0; background: var(--bg); color: var(--text);
  font: 16px/1.75 "IBM Plex Sans Thai", "Sukhumvit Set", -apple-system, system-ui, sans-serif;
  -webkit-font-smoothing: antialiased;
  -webkit-text-size-adjust: 100%; text-size-adjust: 100%;
}
a { color: var(--accent); text-decoration: none; }
a:hover { text-decoration: underline; }
code, .mono { font-family: "JetBrains Mono", ui-monospace, Menlo, monospace; font-size: .88em; }
code { background: var(--code-bg); padding: .1em .4em; border-radius: 5px; }
strong { font-weight: 600; }

/* ---------- top bar ---------- */
.topbar {
  position: sticky; top: 0; z-index: 30; height: var(--top);
  padding-top: env(safe-area-inset-top) !important;
  display: flex; align-items: center; gap: 12px;
  padding: 0 max(16px, env(safe-area-inset-right)) 0 max(16px, env(safe-area-inset-left));
  background: color-mix(in srgb, var(--bg) 88%, transparent);
  backdrop-filter: blur(10px); border-bottom: 1px solid var(--border);
}
.topbar .brand { font-weight: 700; color: var(--text); letter-spacing: .2px; }
.topbar .crumb { color: var(--muted); font-size: 14px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.topbar .spacer { flex: 1; }
.icon-btn {
  border: 1px solid var(--border); background: var(--surface); color: var(--text);
  border-radius: 8px; height: 34px; min-width: 34px; padding: 0 10px; cursor: pointer; font: inherit; font-size: 14px;
}
.icon-btn:hover { background: var(--surface-2); }
#menuBtn { display: none; }

/* ---------- layout ---------- */
.layout { display: grid; grid-template-columns: var(--sidebar) minmax(0, 1fr); }
.sidebar {
  position: sticky; top: var(--top); height: calc(100vh - var(--top)); height: calc(100dvh - var(--top)); overflow-y: auto;
  padding: 20px 12px 40px; border-right: 1px solid var(--border);
}
.main { padding: 32px 40px 120px; max-width: 860px; width: 100%; margin: 0 auto; }

/* ---------- sidebar ---------- */
.progress-box { padding: 4px 8px 16px; }
.progress-label { display: flex; justify-content: space-between; font-size: 13px; color: var(--muted); }
.bar { height: 6px; background: var(--surface-2); border-radius: 99px; overflow: hidden; margin-top: 6px; }
.bar > i { display: block; height: 100%; width: 0; background: var(--accent); border-radius: 99px; transition: width .3s; }
.toc { list-style: none; margin: 0; padding: 0; }
.toc-domain > a {
  display: flex; align-items: center; gap: 8px; padding: 8px; border-radius: 8px;
  font-weight: 600; font-size: 14px; color: var(--text);
}
.toc-domain > a .num {
  flex: none; width: 26px; height: 22px; display: grid; place-items: center; border-radius: 6px;
  font-size: 11px; color: var(--on-dc); background: var(--dc);
}
.toc-domain > a .cnt { margin-left: auto; font-weight: 400; font-size: 12px; color: var(--muted); }
.toc-topics { list-style: none; margin: 0 0 6px; padding: 0 0 0 20px; }
.toc-topics a {
  display: flex; gap: 8px; padding: 4px 8px; border-radius: 6px; font-size: 13.5px; line-height: 1.45; color: var(--muted);
}
.toc-topics a::before { content: "○"; flex: none; font-size: 11px; line-height: 1.8; }
.toc-topics a.done::before { content: "●"; color: var(--mem-bd); }
.toc a:hover { background: var(--surface-2); text-decoration: none; }
.toc a.active { background: var(--surface); color: var(--text); box-shadow: var(--shadow); }

/* ---------- hero ---------- */
.hero { margin-bottom: 40px; }
.eyebrow { font-size: 13px; font-weight: 600; letter-spacing: 1px; text-transform: uppercase; color: var(--muted); }
h1 { font-size: 34px; line-height: 1.25; margin: 6px 0 10px; letter-spacing: -.3px; }
.lead { font-size: 17px; color: var(--muted); margin: 0 0 20px; }
.chips { display: flex; flex-wrap: wrap; gap: 8px; }
.chip {
  background: var(--surface); border: 1px solid var(--border); border-radius: 99px;
  padding: 3px 12px; font-size: 13px; color: var(--muted);
}
.chip b { color: var(--text); font-weight: 600; }

/* น้ำหนักข้อสอบ */
.weights { margin-top: 24px; background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); padding: 16px; box-shadow: var(--shadow); }
.weights h4 { margin: 0 0 10px; font-size: 14px; }
.wbar { display: flex; height: 12px; border-radius: 99px; overflow: hidden; gap: 2px; }
.wbar span { background: var(--dc); }
.wlegend { display: grid; grid-template-columns: repeat(auto-fill, minmax(190px, 1fr)); gap: 4px 16px; margin-top: 12px; font-size: 13.5px; }
.wlegend a { display: flex; align-items: center; gap: 8px; color: var(--text); }
.wlegend i { width: 10px; height: 10px; border-radius: 3px; background: var(--dc); flex: none; }
.wlegend em { margin-left: auto; font-style: normal; color: var(--muted); font-variant-numeric: tabular-nums; }

/* ---------- domain ---------- */
.domain { margin-top: 64px; }
.domain-head {
  display: flex; align-items: center; gap: 14px; padding-bottom: 14px; margin-bottom: 8px;
  border-bottom: 2px solid var(--dc);
}
.domain-head .num {
  flex: none; width: 52px; height: 52px; border-radius: 12px; display: grid; place-items: center;
  background: var(--dc); color: var(--on-dc); font-weight: 700; font-size: 18px;
}
.domain-head h2 { margin: 0; font-size: 24px; line-height: 1.3; }
.domain-head .sub { color: var(--muted); font-size: 14px; }
.domain-head .weight { margin-left: auto; text-align: right; font-size: 13px; color: var(--muted); white-space: nowrap; }
.domain-head .weight b { display: block; font-size: 22px; color: var(--dc); line-height: 1.1; }
.domain-intro { color: var(--muted); margin: 12px 0 0; }

/* ---------- topic ---------- */
.topic {
  margin-top: 24px; background: var(--surface); border: 1px solid var(--border);
  border-radius: var(--radius); padding: 22px 24px; box-shadow: var(--shadow);
}
.topic-head { display: flex; align-items: flex-start; gap: 12px; }
.topic h3 { margin: 0; font-size: 19px; line-height: 1.4; flex: 1; }
.topic .en { display: block; font-size: 13px; font-weight: 400; color: var(--muted); margin-top: 2px; }
.obj-code {
  flex: none; font-family: "JetBrains Mono", monospace; font-size: 13px; font-weight: 600;
  color: var(--dc); border: 1px solid var(--dc); border-radius: 6px; padding: 1px 7px; margin-top: 3px;
}
.done-btn {
  flex: none; border: 1px solid var(--border); background: transparent; color: var(--muted);
  border-radius: 99px; padding: 3px 12px; font: inherit; font-size: 13px; cursor: pointer; white-space: nowrap;
}
.done-btn:hover { border-color: var(--mem-bd); color: var(--text); }
.topic.is-done .done-btn { background: var(--mem-bd); border-color: var(--mem-bd); color: var(--on-dc); }
.topic p { margin: 12px 0 0; }
.topic ul, .topic ol { margin: 10px 0 0; padding-left: 22px; }
.topic li { margin: 4px 0; }
.topic h4 { margin: 18px 0 0; font-size: 15px; }

/* ตาราง */
.table-wrap { overflow-x: auto; margin-top: 14px; border: 1px solid var(--border); border-radius: 8px; }
table { border-collapse: collapse; width: 100%; font-size: 14px; line-height: 1.55; }
th, td { text-align: left; padding: 8px 12px; border-bottom: 1px solid var(--border); vertical-align: top; }
th { background: var(--surface-2); font-weight: 600; white-space: nowrap; }
tr:last-child td { border-bottom: 0; }
td.k { font-weight: 600; white-space: nowrap; }

/* กล่องเน้น */
.callout { margin-top: 14px; border-left: 4px solid; border-radius: 0 8px 8px 0; padding: 10px 14px; font-size: 15px; }
.callout::before { display: block; font-weight: 700; font-size: 13px; margin-bottom: 2px; }
.callout.tip { background: var(--tip-bg); border-color: var(--tip-bd); }
.callout.tip::before { content: "🎯 ข้อสอบชอบออก"; }
.callout.mem { background: var(--mem-bg); border-color: var(--mem-bd); }
.callout.mem::before { content: "🧠 จำง่าย"; }
.callout.warn { background: var(--warn-bg); border-color: var(--warn-bd); }
.callout.warn::before { content: "⚠️ ระวังสับสน"; }

/* การ์ดคำศัพท์ */
.terms { display: grid; grid-template-columns: repeat(auto-fill, minmax(220px, 1fr)); gap: 10px; margin-top: 14px; }
.term { background: var(--bg); border: 1px solid var(--border); border-radius: 8px; padding: 10px 12px; font-size: 14.5px; line-height: 1.55; }
.term b { display: block; font-size: 14px; color: var(--dc); }

/* ขั้นตอน */
.steps { counter-reset: s; list-style: none; padding: 0 !important; }
.steps li { counter-increment: s; position: relative; padding-left: 40px; margin: 10px 0; }
.steps li::before {
  content: counter(s); position: absolute; left: 0; top: 1px; width: 26px; height: 26px; border-radius: 50%;
  background: var(--dc); color: var(--on-dc); font-size: 13px; font-weight: 700; display: grid; place-items: center;
}

/* เฉลยพับได้ */
details.more { margin-top: 14px; border: 1px dashed var(--border); border-radius: 8px; padding: 8px 12px; }
details.more summary { cursor: pointer; font-weight: 600; font-size: 14px; color: var(--muted); }
details.more[open] summary { margin-bottom: 6px; }

.src-fig { margin: 12px 0 0; }
.src-fig a.zoom { display: block; cursor: zoom-in; }
.src-fig figcaption { font-size: 12px; color: var(--muted); margin-top: 6px; }
.src-fig img { display: block; width: 100%; max-width: 640px; height: auto; border-radius: 8px; background: #F8FAFC; }
:root[data-theme="dark"] .src-fig img { filter: brightness(.88); }
@media (prefers-color-scheme: dark) { :root:not([data-theme="light"]) .src-fig img { filter: brightness(.88); } }

/* แผนภาพ (inline SVG เปลี่ยนสีตามธีม) */
.fig { margin: 16px 0 0; padding: 14px; background: var(--bg); border: 1px solid var(--border); border-radius: 10px; }
.fig svg { display: block; width: 100%; height: auto; font-family: inherit; }
.fig-title { font-weight: 700; font-size: 14.5px; margin-bottom: 10px; }
.fgrid { display: grid; gap: 12px; }
.fgrid.two { grid-template-columns: repeat(auto-fit, minmax(240px, 1fr)); }
.fgrid.two svg { max-width: 360px; margin: 0 auto; }
.fgrid.four { grid-template-columns: repeat(4, 1fr); }
.tp { text-align: center; }
.tp svg { max-width: 160px; margin: 0 auto; }
.tp b { display: block; font-size: 14px; }
.tp span { display: block; font-size: 12.5px; color: var(--muted); line-height: 1.4; }

/* encapsulation */
.encap { display: grid; gap: 8px; }
.er { display: grid; grid-template-columns: 74px 1fr; gap: 10px; align-items: center; }
.el b { display: block; font-size: 14px; }
.el span { display: block; font-size: 12px; color: var(--muted); line-height: 1.2; }
.eb { display: flex; justify-content: flex-end; gap: 3px; min-width: 0; }
.eb i { font-style: normal; font-size: 13px; font-weight: 600; padding: 5px 9px; border-radius: 5px; white-space: nowrap; }
.eb .h { background: var(--dc); color: var(--on-dc); }
.eb .d { flex: 0 0 42%; text-align: center; border: 1.5px solid var(--dc); color: var(--text); font-weight: 400; }
.eb.bits { font-family: "JetBrains Mono", monospace; font-size: 13px; letter-spacing: 1px; color: var(--muted); overflow: hidden; }

/* subnet */
.snet { display: grid; grid-template-columns: repeat(4, 1fr); gap: 8px; }
.sn { border-radius: 8px; overflow: hidden; border: 1px solid var(--border); background: var(--surface); font-size: 13px; line-height: 1.5; }
.sn b { display: block; background: var(--dc); color: var(--on-dc); padding: 6px 10px; font-size: 13.5px; }
.sn code { display: block; background: none; padding: 6px 10px 2px; font-size: 12.5px; color: var(--text); }
.sn span { display: block; padding: 0 10px; color: var(--muted); }
.sn span:last-child { padding-bottom: 8px; }

/* troubleshooting flow */
.fig .flow { list-style: none; margin: 0; padding: 0; display: grid; grid-template-columns: repeat(auto-fit, minmax(150px, 1fr)); gap: 8px; counter-reset: none; }
.fig .flow li { margin: 0; display: flex; align-items: center; gap: 10px; padding: 9px 12px; border: 1.5px solid var(--dc); border-radius: 8px; background: var(--surface); font-size: 14px; }
.fig .flow li i { flex: none; width: 24px; height: 24px; border-radius: 50%; display: grid; place-items: center; font-style: normal; font-size: 12.5px; font-weight: 700; background: var(--dc); color: var(--on-dc); }
.fig .flow li.loop { border-color: var(--warn-bd); }
.loopnote { margin-top: 10px; font-size: 13.5px; font-weight: 600; color: var(--warn-bd); }

@media (max-width: 640px) {
  .fig { padding: 12px; }
  .fgrid.four { grid-template-columns: repeat(2, 1fr); row-gap: 18px; }
  .snet { grid-template-columns: repeat(2, 1fr); }
  .er { grid-template-columns: 58px 1fr; gap: 6px; }
  .eb i { font-size: 11.5px; padding: 4px 6px; }
  .eb .d { flex-basis: 34%; }
  .fig .flow { grid-template-columns: 1fr 1fr; }
}
.fig figcaption { font-size: 13px; color: var(--muted); margin-top: 8px; }
.fig .t { fill: var(--text); font-size: 13px; }
.fig .tb { fill: var(--text); font-size: 14px; font-weight: 700; }
.fig .tm { fill: var(--muted); font-size: 12px; }
.fig .tw { fill: var(--on-dc); font-size: 13px; font-weight: 600; }
.fig .ln { stroke: var(--muted); stroke-width: 1.6; fill: none; }
.fig .ac { stroke: var(--dc); stroke-width: 1.8; fill: none; }
.fig .nd { fill: var(--dc); }
.fig .hub { fill: var(--tip-bd); }
.fig .box { fill: var(--surface); stroke: var(--border); }
.fig .boxa { fill: var(--surface); stroke: var(--dc); stroke-width: 1.5; }
.fig .warn { stroke: var(--warn-bd); stroke-width: 1.8; fill: none; stroke-dasharray: 5 4; }
.fig .tr { fill: var(--warn-bd); font-size: 12px; font-weight: 600; }

.quiz-link {
  display: block; margin-top: 20px; padding: 14px 18px; border-radius: var(--radius);
  border: 1px dashed var(--dc); color: var(--dc); font-weight: 600; text-align: center;
}
.quiz-link:hover { background: var(--surface); text-decoration: none; }

.source { margin-top: 64px; font-size: 13px; color: var(--muted); border-top: 1px solid var(--border); padding-top: 16px; }

/* ---------- mobile ---------- */
.scrim { display: none; }
@media (max-width: 900px) {
  #menuBtn { display: inline-block; }
  .layout { grid-template-columns: minmax(0, 1fr); }
  .topbar .crumb { display: none; }
  .sidebar {
    position: fixed; top: var(--top); left: 0; bottom: 0; width: min(86vw, 320px); z-index: 25;
    background: var(--bg); transform: translateX(-100%); transition: transform .2s;
  }
  body.nav-open .sidebar { transform: none; }
  body.nav-open .scrim { display: block; position: fixed; inset: var(--top) 0 0; background: rgba(0,0,0,.35); z-index: 20; }
  .main { padding: 24px max(16px, env(safe-area-inset-right)) calc(100px + env(safe-area-inset-bottom)) max(16px, env(safe-area-inset-left)); }
  h1 { font-size: 27px; }
  .topic { padding: 18px 16px; }
  .topic-head { flex-wrap: wrap; }
  .topic h3 { flex: 1 1 220px; }
  .domain-head .num { width: 44px; height: 44px; font-size: 16px; }
  .domain-head h2 { font-size: 20px; }
}
/* ตารางบนจอแคบ: แต่ละแถวกลายเป็นการ์ด */
@media (max-width: 640px) {
  .table-wrap { overflow: visible; border: 0; }
  table.stack, table.stack tbody { display: block; }
  table.stack tr:first-child { display: none; }
  table.stack tr {
    display: block; margin-top: 10px; padding: 8px 12px;
    background: var(--bg); border: 1px solid var(--border); border-radius: 10px;
  }
  table.stack td { display: grid; grid-template-columns: 34% 1fr; gap: 10px; padding: 5px 0; border: 0; white-space: normal; }
  table.stack td::before { content: attr(data-label); color: var(--muted); font-size: 12.5px; font-weight: 400; font-family: "IBM Plex Sans Thai", -apple-system, system-ui, sans-serif; letter-spacing: 0; }
  table.stack td:first-child { display: block; font-size: 15px; font-weight: 600; padding: 4px 0 6px; border-bottom: 1px solid var(--border); margin-bottom: 2px; }
  table.stack td:first-child::before { content: none; }
  table.stack td[data-label=""]::before, table.stack td:not([data-label])::before { content: none; }
  table.stack td:empty { display: none; }
  table.stack.numcol td:first-child { display: inline-block; border: 0; margin: 0 8px 0 0; color: var(--dc); }
  table.stack.numcol td:nth-child(2) { display: inline-block; font-size: 15px; font-weight: 600; padding: 4px 0 6px; }
  table.stack.numcol td:nth-child(2)::before { content: none; }
}

@media print {
  .topbar, .sidebar, .done-btn { display: none !important; }
  .layout { display: block; }
  .topic { break-inside: avoid; box-shadow: none; }
}
