:root {
  --bg: #f7f6f2;
  --surface: #ffffff;
  --surface-2: #f0efe9;
  --text: #1f2328;
  --muted: #5f6670;
  --border: #e3e1d8;
  --code-bg: #f3f2ec;
  --tip-bg: #eaf5ef; --tip-bd: #2f8f5b;
  --warn-bg: #fdf3e2; --warn-bd: #d98a00;
  --shadow: 0 1px 2px rgba(0,0,0,.04), 0 4px 16px rgba(0,0,0,.05);
  --accent: #2563eb;
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #15171a; --surface: #1d2024; --surface-2: #25292e; --text: #e6e6e3; --muted: #9aa1a9;
    --border: #30353b; --code-bg: #24282d; --tip-bg: #1b2d23; --tip-bd: #4cb77c;
    --warn-bg: #30271a; --warn-bd: #e0a23a; --shadow: none; color-scheme: dark;
  }
}
:root[data-theme="dark"] {
  --bg: #15171a; --surface: #1d2024; --surface-2: #25292e; --text: #e6e6e3; --muted: #9aa1a9;
  --border: #30353b; --code-bg: #24282d; --tip-bg: #1b2d23; --tip-bd: #4cb77c;
  --warn-bg: #30271a; --warn-bd: #e0a23a; --shadow: none; color-scheme: dark;
}

* { box-sizing: border-box; }
html { scroll-padding-top: 72px; -webkit-text-size-adjust: 100%; }
body {
  margin: 0; background: var(--bg); color: var(--text);
  font-family: "IBM Plex Sans Thai", "Segoe UI", Tahoma, sans-serif;
  font-size: 17px; line-height: 1.8;
}
a { color: var(--accent); }
img { max-width: 100%; height: auto; }

#progress { position: fixed; top: 0; left: 0; height: 3px; width: 0; background: var(--accent); z-index: 100; }

.topbar {
  position: sticky; top: 0; z-index: 40; height: 56px;
  display: flex; align-items: center; justify-content: space-between;
  padding: 0 20px; background: color-mix(in srgb, var(--bg) 88%, transparent);
  backdrop-filter: blur(10px); border-bottom: 1px solid var(--border);
}
.brand { font-weight: 700; text-decoration: none; color: var(--text); font-size: 1.05rem; }
.icon-btn {
  width: 36px; height: 36px; border-radius: 10px; border: 1px solid var(--border);
  background: var(--surface); color: var(--text); font-size: 1.1rem; cursor: pointer;
}

.wrap { max-width: 980px; margin: 0 auto; padding: 24px 16px 64px; }
.crumbs { font-size: .88rem; color: var(--muted); margin-bottom: 12px; }
.crumbs a { color: var(--muted); text-decoration: none; }
.crumbs a:hover { color: var(--accent); }

/* home */
.home-hero { padding: 40px 0 28px; }
.home-hero h1 { font-size: clamp(2rem, 5vw, 2.8rem); line-height: 1.2; margin: 0 0 8px; }
.home-hero p { color: var(--muted); margin: 0 0 24px; font-size: 1.05rem; }
.search { position: relative; max-width: 560px; }
.search input {
  width: 100%; padding: 14px 16px; border-radius: 14px; font: inherit;
  border: 1px solid var(--border); background: var(--surface); color: var(--text); box-shadow: var(--shadow);
}
.search input:focus { outline: 2px solid var(--accent); outline-offset: 1px; }
#results { list-style: none; margin: 6px 0 0; padding: 0; position: absolute; left: 0; right: 0; z-index: 30;
  background: var(--surface); border: 1px solid var(--border); border-radius: 12px; box-shadow: var(--shadow);
  max-height: 60vh; overflow: auto; }
#results:empty { display: none; }
#results a { display: block; padding: 10px 14px; text-decoration: none; color: var(--text); border-bottom: 1px solid var(--border); line-height: 1.5; }
#results a:hover, #results a:focus { background: var(--surface-2); }
#results small { color: var(--muted); display: block; }

.subject-grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(260px, 1fr)); gap: 16px; }
.subject-card {
  display: flex; flex-direction: column; padding: 22px; border-radius: 18px; text-decoration: none; color: var(--text);
  background: var(--surface); border: 1px solid var(--border); box-shadow: var(--shadow);
  border-top: 4px solid var(--accent); transition: transform .15s, border-color .15s;
}
.subject-card:hover { transform: translateY(-2px); border-color: var(--accent); }
.sc-icon { font-size: 2rem; }
.code { font-weight: 700; color: var(--accent); letter-spacing: .04em; font-size: .9rem; }
.subject-card h2 { font-size: 1.2rem; line-height: 1.35; margin: 4px 0 4px; }
.subject-card p { margin: 0 0 16px; color: var(--muted); font-size: .95rem; }
.sc-meta { margin-top: auto; display: flex; gap: 14px; font-size: .9rem; color: var(--muted); }

/* subject page */
.subject-hero { display: flex; gap: 18px; align-items: center; padding: 20px 0 24px; }
.big-icon { font-size: 3rem; width: 76px; height: 76px; display: grid; place-items: center; border-radius: 20px;
  background: color-mix(in srgb, var(--accent) 14%, var(--surface)); flex: none; }
.subject-hero h1 { margin: 0; font-size: clamp(1.5rem, 4vw, 2rem); line-height: 1.25; }
.subject-hero p { margin: 4px 0 0; color: var(--muted); }
.lesson-list { list-style: none; padding: 0; margin: 0; display: grid; gap: 10px; }
.lesson-row { display: flex; align-items: center; gap: 14px; padding: 14px 16px; background: var(--surface);
  border: 1px solid var(--border); border-radius: 14px; }
.lesson-row:hover { border-color: var(--accent); }
.num { font-family: "JetBrains Mono", monospace; color: var(--accent); font-weight: 600; flex: none; }
.lesson-link { flex: 1; min-width: 0; text-decoration: none; color: var(--text); line-height: 1.45; }
.lesson-link small { display: block; color: var(--muted); font-size: .8rem; }
.lesson-link span { font-weight: 600; }
.row-actions { display: flex; gap: 8px; flex: none; }
.chip { padding: 6px 12px; border-radius: 999px; font-size: .88rem; text-decoration: none; white-space: nowrap;
  border: 1px solid var(--border); color: var(--text); background: var(--surface-2); }
.chip:hover { border-color: var(--accent); color: var(--accent); }
.chip-quiz { background: color-mix(in srgb, var(--accent) 12%, var(--surface)); color: var(--accent); border-color: transparent; }
@media (max-width: 600px) {
  .lesson-row { flex-wrap: wrap; }
  .row-actions { width: 100%; padding-left: 34px; }
}

/* lesson page */
.lesson-layout { display: grid; grid-template-columns: 270px minmax(0, 1fr); gap: 40px; max-width: 1180px; margin: 0 auto; padding: 24px 16px 64px; }
.toc { position: sticky; top: 76px; align-self: start; max-height: calc(100vh - 96px); overflow: auto; font-size: .9rem; line-height: 1.45; }
.toc-toggle { display: none; }
.toc-back { display: inline-block; margin-bottom: 12px; text-decoration: none; font-weight: 600; }
.toc ol { list-style: none; padding: 0; margin: 0; border-left: 2px solid var(--border); }
.toc li a { display: block; padding: 6px 12px; margin-left: -2px; border-left: 2px solid transparent; color: var(--muted); text-decoration: none; }
.toc li a:hover { color: var(--text); }
.toc li a.active { color: var(--accent); border-left-color: var(--accent); font-weight: 600; }
.lesson { max-width: 820px; min-width: 0; }
.lesson-title { font-size: clamp(1.6rem, 4vw, 2.2rem); line-height: 1.3; margin: 0 0 12px; }
.lesson-actions { margin-bottom: 20px; }
.btn { display: inline-flex; align-items: center; gap: 6px; padding: 10px 18px; border-radius: 12px; font-weight: 600; text-decoration: none; }
.btn-quiz { background: var(--accent); color: #fff; }
.btn-quiz:hover { filter: brightness(1.08); }

@media (max-width: 900px) {
  .lesson-layout { grid-template-columns: 1fr; gap: 0; padding-top: 0; }
  .toc { position: sticky; top: 56px; z-index: 30; max-height: none; overflow: visible; margin: 0 -16px 12px;
    background: var(--bg); border-bottom: 1px solid var(--border); }
  .toc-toggle { display: block; width: 100%; text-align: left; padding: 10px 16px; font: inherit; font-weight: 600;
    background: none; border: 0; color: var(--text); cursor: pointer; }
  .toc-inner { display: none; max-height: 60vh; overflow: auto; padding: 0 16px 12px; }
  .toc.open .toc-inner { display: block; }
  .lesson-layout .lesson { padding-top: 8px; }
}

/* prose */
.prose h2 { font-size: 1.5rem; line-height: 1.35; margin: 2.4em 0 .6em; padding-top: .2em; }
.prose h3 { font-size: 1.2rem; margin: 1.8em 0 .5em; }
.prose h4 { font-size: 1.05rem; margin: 1.5em 0 .4em; }
.prose p, .prose ul, .prose ol { margin: 0 0 1em; }
.prose li { margin: .2em 0; }
.prose li > ul, .prose li > ol { margin: .2em 0; }
.prose hr { border: 0; border-top: 1px solid var(--border); margin: 2.4em 0; }
.prose strong { font-weight: 700; }
.prose code { font-family: "JetBrains Mono", Consolas, monospace; font-size: .86em; background: var(--code-bg);
  padding: .1em .35em; border-radius: 5px; }
.prose pre { background: var(--code-bg); border: 1px solid var(--border); border-radius: 12px; padding: 14px 16px;
  overflow-x: auto; line-height: 1.5; font-size: .92rem; }
.prose pre code { background: none; padding: 0; font-size: inherit; }
.prose img { display: block; margin: 1em auto; border-radius: 10px; border: 1px solid var(--border); background: #fff; cursor: zoom-in; }
.table-wrap { overflow-x: auto; margin: 0 0 1.2em; border: 1px solid var(--border); border-radius: 12px; background: var(--surface); }
.prose table { border-collapse: collapse; width: 100%; font-size: .94rem; line-height: 1.6; }
.prose th, .prose td { padding: 9px 12px; border-bottom: 1px solid var(--border); text-align: left; vertical-align: top; }
.prose th { background: var(--surface-2); font-weight: 600; }
.prose tr:last-child td { border-bottom: 0; }
.prose td + td, .prose th + th { border-left: 1px solid var(--border); }
.callout { margin: 0 0 1.2em; padding: 12px 16px; border-left: 4px solid var(--accent); border-radius: 0 12px 12px 0;
  background: color-mix(in srgb, var(--accent) 7%, var(--surface)); }
.callout p:last-child { margin-bottom: 0; }
.callout .c-tip { background: var(--tip-bg); border-left: 3px solid var(--tip-bd); padding: 6px 10px; border-radius: 6px; }
.callout .c-warn { background: var(--warn-bg); border-left: 3px solid var(--warn-bd); padding: 6px 10px; border-radius: 6px; }

.quiz-cta { display: flex; align-items: center; justify-content: space-between; gap: 16px; flex-wrap: wrap;
  margin: 48px 0 24px; padding: 20px 22px; border-radius: 16px; border: 1px solid var(--border);
  background: color-mix(in srgb, var(--accent) 9%, var(--surface)); }
.quiz-cta p { margin: 2px 0 0; color: var(--muted); font-size: .95rem; }
.pager { display: grid; grid-template-columns: 1fr 1fr; gap: 12px; }
.pg { display: block; padding: 14px 16px; border: 1px solid var(--border); border-radius: 14px; text-decoration: none;
  color: var(--text); background: var(--surface); line-height: 1.45; }
.pg:hover { border-color: var(--accent); }
.pg small { display: block; color: var(--muted); }
.pg span { font-weight: 600; }
.pg.next { text-align: right; }
@media (max-width: 600px) { .pager { grid-template-columns: 1fr; } .pg.next { text-align: left; } }

/* lightbox */
.lightbox { position: fixed; inset: 0; z-index: 200; background: rgba(0,0,0,.85); display: grid; place-items: center; padding: 16px; cursor: zoom-out; }
.lightbox img { max-width: 100%; max-height: 100%; border-radius: 8px; background: #fff; }

.foot { text-align: center; color: var(--muted); font-size: .85rem; padding: 24px 16px 40px; }
@media print { .topbar, .toc, .pager, .quiz-cta, .lesson-actions, #progress { display: none; } .lesson-layout { display: block; } }
