/* คลังเอกสารประกอบการเรียน ป.บัณฑิต การบริหารการศึกษา — "ห้องสมุดกระดาษอุ่น" */

@import url("https://fonts.googleapis.com/css2?family=Noto+Serif+Thai:wght@600;700&family=IBM+Plex+Sans+Thai:wght@400;500;600&display=swap");

:root {
  --paper: #F6F2EA;
  --card: #FFFDF8;
  --ink: #1E2433;
  --ink-2: #5A6072;
  --line: #E4DDCF;
  --brand: #1F3A5F;
  --brand-ink: #FFFFFF;
  --accent: #B8862B;
  --danger: #8A3B3B;
  --radius-card: 14px;
  --radius-pill: 999px;
  --shadow: 0 1px 2px rgb(30 36 51 / .06), 0 4px 16px rgb(30 36 51 / .05);
  --font-serif: "Noto Serif Thai", "Sukhumvit Set", "Leelawadee UI", serif;
  --font-sans: "IBM Plex Sans Thai", system-ui, "Sukhumvit Set", "Leelawadee UI", sans-serif;
  color-scheme: light;
}

@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --paper: #15171C;
    --card: #1D2027;
    --ink: #ECE7DC;
    --ink-2: #A6A9B4;
    --line: #2C303A;
    --brand: #8FB3E0;
    --brand-ink: #10151F;
    --accent: #D9AE5C;
    --danger: #C97A7A;
    color-scheme: dark;
  }
}

:root[data-theme="dark"] {
  --paper: #15171C;
  --card: #1D2027;
  --ink: #ECE7DC;
  --ink-2: #A6A9B4;
  --line: #2C303A;
  --brand: #8FB3E0;
  --brand-ink: #10151F;
  --accent: #D9AE5C;
  --danger: #C97A7A;
  color-scheme: dark;
}

* { box-sizing: border-box; }
html { -webkit-text-size-adjust: 100%; }

body {
  margin: 0;
  background: var(--paper);
  color: var(--ink);
  font-family: var(--font-sans);
  font-size: 17px;
  line-height: 1.7;
  font-variant-numeric: tabular-nums;
}

@media (min-width: 900px) {
  body { font-size: 16px; }
}

a { color: inherit; }
button { font-family: inherit; }

.visually-hidden {
  position: absolute; width: 1px; height: 1px; padding: 0; margin: -1px;
  overflow: hidden; clip: rect(0,0,0,0); white-space: nowrap; border: 0;
}

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

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { animation-duration: .001ms !important; transition-duration: .001ms !important; }
}

/* ---------- Header ---------- */
.site-header {
  position: sticky; top: 0; z-index: 40;
  display: flex; align-items: center; justify-content: space-between;
  gap: 12px;
  padding: 14px clamp(16px, 4vw, 32px);
  background: color-mix(in srgb, var(--paper) 82%, transparent);
  backdrop-filter: blur(10px);
  -webkit-backdrop-filter: blur(10px);
  border-bottom: 1px solid var(--line);
}

.brand-logo {
  display: flex; align-items: baseline; gap: 8px;
  font-family: var(--font-serif); font-weight: 700; font-size: 20px;
  text-decoration: none; color: var(--ink);
}

.brand-logo .rule {
  display: inline-block; width: 22px; height: 2px; background: var(--accent);
}

.btn-ask-ai {
  display: inline-flex; align-items: center; gap: 8px;
  min-height: 44px; padding: 0 18px;
  border-radius: var(--radius-pill);
  border: 1.5px solid var(--accent);
  background: transparent; color: var(--ink);
  font-weight: 600; font-size: 15px; cursor: pointer;
  transition: background .15s ease;
}
.btn-ask-ai:hover { background: color-mix(in srgb, var(--accent) 12%, transparent); }
.btn-ask-ai svg { color: var(--accent); }

/* ---------- Hero ---------- */
.hero {
  padding: 28px clamp(16px, 4vw, 32px) 20px;
  max-width: 980px; margin: 0 auto;
}
.hero-eyebrow {
  font-size: 13.5px; color: var(--ink-2); font-weight: 500;
  margin: 0 0 10px;
}
.hero h1 {
  font-family: var(--font-serif); font-weight: 700;
  font-size: clamp(28px, 5.5vw, 40px);
  margin: 0 0 10px; color: var(--ink);
}
.hero-sub {
  color: var(--ink-2); margin: 0 0 22px; font-size: 16.5px; max-width: 60ch;
}

.search-box {
  position: relative; display: flex; align-items: center;
  height: 56px; background: var(--card); border: 1.5px solid var(--line);
  border-radius: var(--radius-card); box-shadow: var(--shadow);
  padding: 0 16px; gap: 10px;
}
.search-box svg { flex: none; color: var(--ink-2); }
.search-box input {
  flex: 1; border: 0; background: transparent; outline: none;
  font-size: 16px; color: var(--ink); font-family: inherit; min-width: 0;
}
.search-box input::placeholder { color: var(--ink-2); }
.search-box input::-webkit-search-cancel-button,
.search-box input::-webkit-search-decoration { -webkit-appearance: none; appearance: none; display: none; }
.search-clear {
  flex: none; border: 0; background: transparent; color: var(--ink-2);
  cursor: pointer; display: none; align-items: center; justify-content: center;
  width: 32px; height: 32px; border-radius: 50%;
}
.search-clear.show { display: inline-flex; }
.search-clear:hover { background: var(--line); }

.stats-row {
  display: flex; flex-wrap: wrap; gap: 10px 28px;
  margin-top: 22px;
}
.stat-item { display: flex; align-items: baseline; gap: 6px; }
.stat-num { font-family: var(--font-serif); font-weight: 700; font-size: 22px; color: var(--brand); }
.stat-label { color: var(--ink-2); font-size: 14.5px; }

/* ---------- Course chips ---------- */
.chip-bar {
  position: sticky; top: 65px; z-index: 30;
  display: flex; gap: 8px; overflow-x: auto;
  padding: 10px clamp(16px, 4vw, 32px);
  background: color-mix(in srgb, var(--paper) 90%, transparent);
  backdrop-filter: blur(8px);
  border-bottom: 1px solid var(--line);
  scrollbar-width: thin;
}
.chip-bar::-webkit-scrollbar { height: 6px; }
.chip {
  flex: none; display: inline-flex; align-items: center; gap: 7px;
  height: 38px; padding: 0 16px; border-radius: var(--radius-pill);
  border: 1.5px solid var(--line); background: var(--card);
  color: var(--ink); font-size: 14.5px; font-weight: 500; cursor: pointer;
  white-space: nowrap; transition: border-color .15s ease, background .15s ease;
}
.chip .dot { width: 9px; height: 9px; border-radius: 50%; flex: none; }
.chip[aria-pressed="true"] {
  background: var(--brand); border-color: var(--brand); color: var(--brand-ink);
}
.chip[aria-pressed="true"] .dot { background: var(--brand-ink) !important; }
.chip:not([aria-pressed="true"]):hover { border-color: var(--ink-2); }

/* ---------- Course sections ---------- */
.courses-wrap { max-width: 1080px; margin: 0 auto; padding: 8px clamp(16px, 4vw, 32px) 40px; }

.course-section { padding-top: 32px; scroll-margin-top: 130px; }
.course-head {
  display: flex; flex-wrap: wrap; align-items: baseline; gap: 8px 14px;
  margin-bottom: 16px; padding-bottom: 12px; border-bottom: 1px solid var(--line);
}
.course-code {
  font-family: var(--font-serif); font-weight: 700; font-size: 24px;
}
.course-name { font-weight: 600; font-size: 17px; }
.course-meta { color: var(--ink-2); font-size: 14px; }
.course-count { color: var(--ink-2); font-size: 14px; margin-left: auto; }
.btn-course-summary {
  display: inline-flex; align-items: center; gap: 6px;
  height: 34px; padding: 0 14px; border-radius: var(--radius-pill);
  border: 1.5px solid var(--accent); background: transparent; color: var(--ink);
  font-size: 13.5px; font-weight: 600; cursor: pointer;
}
.btn-course-summary svg { color: var(--accent); width: 15px; height: 15px; }
.btn-course-summary:hover { background: color-mix(in srgb, var(--accent) 12%, transparent); }

.doc-grid {
  display: grid; grid-template-columns: 1fr; gap: 14px;
}
@media (min-width: 900px) {
  .doc-grid { grid-template-columns: 1fr 1fr; }
}

/* ---------- Doc card ---------- */
.doc-card {
  position: relative;
  background: var(--card); border: 1px solid var(--line);
  border-radius: var(--radius-card); box-shadow: var(--shadow);
  padding: 16px 16px 14px 18px; overflow: hidden;
}
.doc-card::before {
  content: ""; position: absolute; left: 0; top: 0; bottom: 0; width: 4px;
  background: var(--course-color, var(--brand));
}
.doc-row-top {
  display: flex; align-items: center; gap: 10px; flex-wrap: wrap;
  color: var(--ink-2); font-size: 13px; margin-bottom: 8px;
}
.doc-type-badge {
  display: inline-flex; align-items: center; height: 24px; padding: 0 10px;
  border-radius: var(--radius-pill); background: color-mix(in srgb, var(--course-color, var(--brand)) 14%, var(--card));
  color: var(--ink); font-weight: 500; font-size: 12.5px;
}
.doc-title {
  font-weight: 600; font-size: 16.5px; margin: 0 0 6px;
  display: -webkit-box; -webkit-line-clamp: 2; -webkit-box-orient: vertical; overflow: hidden;
}
.doc-tldr {
  color: var(--ink-2); font-size: 14.5px; margin: 0 0 12px;
  display: -webkit-box; -webkit-line-clamp: 2; -webkit-box-orient: vertical; overflow: hidden;
  min-height: 1em;
}
.doc-tldr:empty { display: none; }

.doc-actions { display: flex; flex-wrap: wrap; gap: 8px; }
.btn {
  display: inline-flex; align-items: center; gap: 7px;
  min-height: 44px; padding: 0 16px; border-radius: var(--radius-pill);
  font-size: 14.5px; font-weight: 600; cursor: pointer; border: 1.5px solid transparent;
  text-decoration: none;
}
.btn-primary { background: var(--brand); color: var(--brand-ink); }
.btn-primary:hover { filter: brightness(1.08); }
.btn-outline { background: transparent; border-color: var(--line); color: var(--ink); }
.btn-outline:hover { border-color: var(--ink-2); }
.btn-ghost-gold { background: transparent; border-color: var(--accent); color: var(--ink); }
.btn-ghost-gold svg { color: var(--accent); }
.btn-ghost-gold:hover { background: color-mix(in srgb, var(--accent) 12%, transparent); }
.btn-ghost-gold[aria-hidden-empty="true"] { display: none; }

/* accordion สรุป AI ในการ์ด */
.doc-summary-panel {
  display: grid; grid-template-rows: 0fr; transition: grid-template-rows .28s ease;
  margin-top: 0;
}
.doc-summary-panel.open { grid-template-rows: 1fr; margin-top: 14px; }
.doc-summary-inner { overflow: hidden; min-height: 0; }
.doc-summary-body {
  border-top: 1px dashed var(--line); padding-top: 14px; font-size: 14.5px;
}
.doc-summary-body h4 { margin: 14px 0 8px; font-size: 13.5px; color: var(--ink-2); font-weight: 600; }
.doc-summary-body h4:first-child { margin-top: 0; }
.doc-summary-body ul { margin: 0; padding-left: 20px; }
.doc-summary-body li { margin-bottom: 5px; }
.keyword-chip {
  display: inline-flex; align-items: center; height: 28px; padding: 0 12px;
  border-radius: var(--radius-pill); background: var(--paper); border: 1px solid var(--line);
  font-size: 13px; margin: 0 6px 6px 0; cursor: pointer; color: var(--ink);
}
.keyword-chip:hover { border-color: var(--accent); }
.no-summary-note { color: var(--ink-2); font-size: 14px; font-style: normal; }

.mini-ask-row { display: flex; gap: 8px; margin-top: 14px; }
.mini-ask-row input {
  flex: 1; height: 44px; border-radius: var(--radius-pill); border: 1.5px solid var(--line);
  background: var(--paper); padding: 0 16px; font-size: 14.5px; color: var(--ink); font-family: inherit;
}
.mini-ask-row button {
  flex: none; width: 44px; height: 44px; border-radius: 50%; border: 0;
  background: var(--brand); color: var(--brand-ink); display: inline-flex;
  align-items: center; justify-content: center; cursor: pointer;
}

/* ---------- Empty state ---------- */
.empty-state {
  display: flex; flex-direction: column; align-items: center; text-align: center;
  gap: 14px; padding: 60px 20px; color: var(--ink-2);
}
.empty-state svg { color: var(--ink-2); opacity: .6; }
.empty-state .btn-outline { margin-top: 4px; }

/* ---------- Footer ---------- */
.site-footer {
  text-align: center; color: var(--ink-2); font-size: 13.5px;
  padding: 28px 20px 40px; border-top: 1px solid var(--line); max-width: 1080px; margin: 0 auto;
}

/* ---------- Toast ---------- */
.toast {
  position: fixed; left: 50%; bottom: 24px; transform: translateX(-50%) translateY(10px);
  background: var(--ink); color: var(--paper); padding: 12px 20px; border-radius: var(--radius-pill);
  font-size: 14px; box-shadow: var(--shadow); z-index: 100; opacity: 0; pointer-events: none;
  transition: opacity .2s ease, transform .2s ease; max-width: 92vw; text-align: center;
  display: flex; align-items: center; gap: 10px;
}
.toast.show { opacity: 1; transform: translateX(-50%) translateY(0); pointer-events: auto; }
.toast a, .toast button { color: var(--accent); font-weight: 700; background: none; border: 0; cursor: pointer; text-decoration: underline; }

/* ---------- Modal (สรุปทั้งวิชา) ---------- */
.modal-backdrop {
  position: fixed; inset: 0; background: rgb(20 22 28 / .5); z-index: 90;
  display: flex; align-items: center; justify-content: center; padding: 20px;
}
.modal-card {
  background: var(--card); border-radius: 18px; max-width: 560px; width: 100%;
  max-height: 82vh; overflow-y: auto; padding: 26px; box-shadow: var(--shadow);
  border: 1px solid var(--line);
}
.modal-card h3 { font-family: var(--font-serif); margin: 0 0 12px; font-size: 22px; }
.modal-close-row { display: flex; justify-content: flex-end; margin-top: 18px; }

/* ---------- Reader page ---------- */
.reader-shell { display: flex; flex-direction: column; height: 100vh; height: 100dvh; background: #EDE7DA; }
:root[data-theme="dark"] .reader-shell { background: #0F1115; }
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) .reader-shell { background: #0F1115; }
}

.reader-toolbar {
  display: flex; align-items: center; gap: 10px; padding: 10px 14px;
  background: var(--card); border-bottom: 1px solid var(--line); z-index: 20;
}
.reader-back {
  flex: none; width: 40px; height: 40px; border-radius: 50%; border: 0; background: transparent;
  display: inline-flex; align-items: center; justify-content: center; cursor: pointer; color: var(--ink);
}
.reader-back:hover { background: var(--line); }
.reader-title { flex: 1; min-width: 0; font-weight: 600; font-size: 15px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.reader-page-num { flex: none; color: var(--ink-2); font-size: 13.5px; white-space: nowrap; }
.reader-icon-btn {
  flex: none; width: 40px; height: 40px; border-radius: 50%; border: 1px solid var(--line); background: var(--card);
  display: inline-flex; align-items: center; justify-content: center; cursor: pointer; color: var(--ink);
  text-decoration: none;
}
.reader-icon-btn:hover { border-color: var(--ink-2); }
.reader-icon-btn.accent { border-color: var(--accent); color: var(--accent); }

.reader-progress { height: 3px; background: var(--line); overflow: hidden; }
.reader-progress-bar { height: 100%; width: 0%; background: var(--accent); transition: width .15s ease; }
.reader-progress-label {
  text-align: center; font-size: 12.5px; color: var(--ink-2); padding: 4px 0; background: var(--card);
}
.reader-progress-label:empty { display: none; }

.reader-main { flex: 1; display: flex; overflow: hidden; min-height: 0; }
.pdf-scroll { position: relative; flex: 1; overflow: auto; padding: 18px 0 60px; -webkit-overflow-scrolling: touch; }
.pdf-page-wrap { display: flex; justify-content: safe center; margin-bottom: 16px; }
.pdf-page-canvas { box-shadow: 0 2px 10px rgb(0 0 0 / .18); background: #fff; flex: none; }
.pdf-page-placeholder {
  background: #fff; box-shadow: 0 2px 10px rgb(0 0 0 / .1); display: flex; align-items: center; justify-content: center;
  color: #99927f; font-size: 13px;
}
.pdf-fallback {
  margin: 60px auto; text-align: center; color: var(--ink-2); max-width: 320px;
}

.zoom-controls {
  position: fixed; right: 18px; bottom: 18px; display: none; flex-direction: column; gap: 8px; z-index: 15;
}
@media (min-width: 720px) {
  .zoom-controls { display: flex; }
}
.zoom-controls button {
  width: 42px; height: 42px; border-radius: 50%; border: 1px solid var(--line); background: var(--card);
  color: var(--ink); cursor: pointer; box-shadow: var(--shadow); display: inline-flex; align-items: center; justify-content: center;
}

/* AI panel: sidebar (desktop) / bottom sheet (มือถือ) */
.ai-panel {
  background: var(--card); border-left: 1px solid var(--line);
  display: flex; flex-direction: column; z-index: 25;
}
.ai-panel[hidden] { display: none; }

@media (min-width: 900px) {
  .ai-panel { width: 380px; flex: none; position: relative; }
}
@media (max-width: 899.98px) {
  .ai-panel {
    position: fixed; left: 0; right: 0; bottom: 0; top: 14vh;
    border-left: 0; border-top: 1px solid var(--line);
    border-radius: 18px 18px 0 0; box-shadow: 0 -6px 24px rgb(0 0 0 / .18);
  }
  .ai-panel-handle {
    width: 40px; height: 5px; border-radius: 3px; background: var(--line); margin: 10px auto 4px; flex: none;
  }
}
.ai-panel-handle { display: none; }
@media (max-width: 899.98px) { .ai-panel-handle { display: block; } }

.ai-panel-head {
  display: flex; align-items: center; justify-content: space-between; padding: 12px 16px;
  border-bottom: 1px solid var(--line);
}
.ai-panel-head h3 { margin: 0; font-size: 16px; font-family: var(--font-serif); display: flex; align-items: center; gap: 8px; }
.ai-panel-head h3 svg { color: var(--accent); }
.ai-panel-close {
  width: 34px; height: 34px; border-radius: 50%; border: 0; background: transparent; cursor: pointer; color: var(--ink-2);
  display: inline-flex; align-items: center; justify-content: center;
}
.ai-panel-close:hover { background: var(--line); }

.ai-panel-body { flex: 1; overflow-y: auto; padding: 16px; display: flex; flex-direction: column; gap: 12px; }
.ai-suggested { display: flex; flex-direction: column; gap: 8px; }
.ai-suggested button {
  text-align: left; padding: 10px 14px; border-radius: 12px; border: 1px solid var(--line); background: var(--paper);
  color: var(--ink); font-size: 14px; cursor: pointer; font-family: inherit;
}
.ai-suggested button:hover { border-color: var(--accent); }

.ai-msg { max-width: 88%; padding: 10px 14px; border-radius: 14px; font-size: 14.5px; line-height: 1.65; }
.ai-msg.user { align-self: flex-end; background: var(--brand); color: var(--brand-ink); border-bottom-right-radius: 4px; }
.ai-msg.assistant { align-self: flex-start; background: var(--paper); border: 1px solid var(--line); color: var(--ink); border-bottom-left-radius: 4px; }
.ai-msg.assistant.error { border-color: var(--danger); }
.ai-msg p { margin: 0 0 8px; }
.ai-msg p:last-child { margin-bottom: 0; }
.ai-msg ul { margin: 6px 0; padding-left: 20px; }
.ai-msg strong { font-weight: 700; }

.ai-typing { display: inline-flex; gap: 4px; align-self: flex-start; padding: 12px 14px; }
.ai-typing span {
  width: 7px; height: 7px; border-radius: 50%; background: var(--ink-2); display: inline-block;
  animation: ai-bounce 1.1s infinite ease-in-out;
}
.ai-typing span:nth-child(2) { animation-delay: .15s; }
.ai-typing span:nth-child(3) { animation-delay: .3s; }
@keyframes ai-bounce { 0%, 80%, 100% { transform: translateY(0); opacity: .5; } 40% { transform: translateY(-5px); opacity: 1; } }

.ai-retry-btn {
  align-self: flex-start; margin-top: 6px; height: 34px; padding: 0 14px; border-radius: var(--radius-pill);
  border: 1.5px solid var(--danger); background: transparent; color: var(--danger); font-size: 13.5px; cursor: pointer;
}

.ai-panel-foot { padding: 12px 16px; border-top: 1px solid var(--line); }
.ai-disclaimer { color: var(--ink-2); font-size: 12px; margin: 0 0 8px; text-align: center; }
.ai-input-row { display: flex; gap: 8px; }
.ai-input-row input {
  flex: 1; height: 46px; border-radius: var(--radius-pill); border: 1.5px solid var(--line); background: var(--paper);
  padding: 0 16px; font-size: 15px; color: var(--ink); font-family: inherit; min-width: 0;
}
.ai-input-row button {
  flex: none; width: 46px; height: 46px; border-radius: 50%; border: 0; background: var(--brand); color: var(--brand-ink);
  display: inline-flex; align-items: center; justify-content: center; cursor: pointer;
}
.ai-input-row button:disabled { opacity: .5; cursor: default; }

/* full-page AI panel overlay used from index.html (ถามทั้งคลัง) */
.ai-overlay-backdrop {
  position: fixed; inset: 0; background: rgb(20 22 28 / .45); z-index: 80;
  display: flex; justify-content: flex-end;
}
@media (max-width: 899.98px) {
  .ai-overlay-backdrop { align-items: flex-end; justify-content: stretch; }
}
.ai-overlay-backdrop .ai-panel {
  position: static; width: 420px; max-width: 100%; height: 100%; border-radius: 0;
  box-shadow: -8px 0 24px rgb(0 0 0 / .1);
}
@media (max-width: 899.98px) {
  .ai-overlay-backdrop .ai-panel { width: 100%; height: 82vh; border-radius: 18px 18px 0 0; }
}

[hidden] { display: none !important; }

/* ปรับละเอียด (Opus): หัวเรื่องไม่ตัดกลางวลี + ปุ่มการ์ด 3 ปุ่มอยู่แถวเดียวบนมือถือ */
.nobr { display: inline-block; white-space: nowrap; }
@media (max-width: 520px) {
  .doc-actions { display: grid; grid-template-columns: 1fr 1.25fr 1.1fr; gap: 6px; }
  .doc-actions .btn { justify-content: center; padding: 0 6px; gap: 5px; font-size: 14px; white-space: nowrap; }
  .doc-actions .btn svg { width: 16px; height: 16px; flex: none; }
}

/* แผงสรุป AI (ไม่มีแชท) */
.ai-panel-body.summary-only { display: block; font-size: 15px; line-height: 1.75; }
.ai-panel-body.summary-only h4 { margin: 18px 0 8px; font-size: 13.5px; color: var(--ink-2); font-weight: 600; }
.ai-panel-body.summary-only p { margin: 0; }
.ai-panel-body.summary-only ul { margin: 0; padding-left: 20px; }
.ai-panel-body.summary-only li { margin-bottom: 6px; }
.summary-tldr { font-weight: 600; color: var(--ink); border-left: 3px solid var(--accent); padding-left: 12px; }
.summary-keywords .keyword-chip { text-decoration: none; }
.ai-panel-foot .ai-disclaimer { margin: 0; }
