/* ============ ฐาน / ตัวแปรสี ============ */
:root {
  --bg: #ffffff;
  --surface: #f6f7f9;
  --border: #e6e8eb;
  --text: #1f2328;
  --muted: #6b7280;
  --primary: #2563eb;
  --primary-dark: #1d4ed8;
  --free: #16a34a;
  --paid: #d97706;
  --radius: 14px;
  --maxw: 980px;
  --shadow: 0 1px 3px rgba(0,0,0,.08), 0 1px 2px rgba(0,0,0,.04);
}
* { box-sizing: border-box; }
html, body { margin: 0; padding: 0; }
body {
  font-family: "Sarabun", system-ui, -apple-system, "Segoe UI", sans-serif;
  color: var(--text);
  background: var(--bg);
  line-height: 1.6;
  font-size: 17px;
  -webkit-text-size-adjust: 100%;
}
img { max-width: 100%; display: block; }
a { color: var(--primary); text-decoration: none; }
.container { max-width: var(--maxw); margin: 0 auto; padding: 0 16px; }

/* ============ ปุ่ม ============ */
.btn {
  display: inline-flex; align-items: center; justify-content: center;
  gap: 6px; border: 1px solid transparent; border-radius: 10px;
  padding: 12px 18px; font-size: 16px; font-weight: 600;
  cursor: pointer; font-family: inherit; transition: .15s; line-height: 1.2;
}
.btn-sm { padding: 8px 12px; font-size: 14px; }
.btn-block { display: flex; width: 100%; margin-top: 10px; }
.btn-primary { background: var(--primary); color: #fff; }
.btn-primary:hover { background: var(--primary-dark); }
.btn-ghost { background: transparent; color: var(--text); border-color: var(--border); }
.btn-ghost:hover { background: var(--surface); }
.btn-free { background: var(--free); color: #fff; }
.btn:disabled { opacity: .55; cursor: not-allowed; }

/* ============ header ============ */
#app-header {
  position: sticky; top: 0; z-index: 50;
  display: flex; align-items: center; justify-content: space-between;
  gap: 8px; padding: 10px 14px; background: rgba(255,255,255,.95);
  backdrop-filter: blur(8px); border-bottom: 1px solid var(--border);
}
.brand { font-weight: 800; font-size: 17px; color: var(--text); display: inline-flex; align-items: center; gap: 4px; flex: none; }
.head-nav { display: flex; align-items: center; gap: 12px; flex-wrap: nowrap; }
.head-nav a { color: var(--text); font-weight: 700; font-size: 16px; white-space: nowrap; }
.nav-admin { color: var(--primary) !important; }
@media (max-width: 480px) {
  .brand { font-size: 15px; }
  .head-nav { gap: 10px; }
  .head-nav a { font-size: 14px; }
  #app-header { padding: 8px 10px; }
}

/* ============ หน้าแรก ============ */
.hero-img {
  width: 100%; aspect-ratio: 16 / 9; object-fit: cover;
  border-radius: var(--radius); background: var(--surface); margin-top: 16px;
}
.hero-text { margin: 18px 0; }
.hero-text h1 { font-size: 26px; margin: 0 0 8px; line-height: 1.3; }
.hero-text p {
  color: var(--text);
  margin: 0;
  white-space: pre-wrap;
  font-size: 24px;
  line-height: 1.7;
}
.video-embed {
  width: 100%; aspect-ratio: 16 / 9; border: 0;
  border-radius: var(--radius); background: #000; margin: 16px 0;
}
/* Player แนวตั้ง 9:16 สำหรับวีดีโอ portrait — จำกัดกว้างไม่ให้สูงเกิน บน desktop */
.video-embed.vertical {
  aspect-ratio: 9 / 16;
  max-width: 420px;
  margin: 16px auto;
}
.cta-center { text-align: center; margin: 24px 0; }

/* ============ Dashboard actions bar ============ */
.dash-actions {
  display: flex; justify-content: flex-end; gap: 8px;
  margin: 0 0 16px;
}
@media (max-width: 480px) {
  .dash-actions { justify-content: stretch; }
  .dash-actions .btn { width: 100%; }
}

/* ============ Certificate promo card ============ */
.cert-promo {
  display: flex; align-items: center; gap: 16px;
  padding: 16px 18px; margin: 0 0 20px;
  background: linear-gradient(135deg, #dbeafe 0%, #eff6ff 100%);
  border: 1px solid #93c5fd;
  border-left: 5px solid #2563eb;
  border-radius: 12px;
}
.cert-promo-text { flex: 1; min-width: 0; }
.cert-promo-headline {
  font-weight: 800; font-size: 16px; color: #1e3a8a;
  margin-bottom: 4px; line-height: 1.4;
}
.cert-promo-sub {
  font-size: 13px; color: #1e40af;
  line-height: 1.5; opacity: 0.85;
}
.cert-promo-btn {
  flex-shrink: 0; white-space: nowrap;
}
@media (max-width: 600px) {
  .cert-promo { flex-direction: column; align-items: stretch; text-align: center; }
  .cert-promo-btn { width: 100%; }
}

/* ============ Dashboard: คอร์สของฉัน ============ */
.dash-grid {
  display: grid; grid-template-columns: 1fr; gap: 16px;
  margin: 8px 0 32px;
}
@media (min-width: 720px) {
  .dash-grid { grid-template-columns: repeat(2, 1fr); }
}
.dash-card {
  display: flex; flex-direction: column;
  border: 1px solid var(--border); border-radius: 14px;
  background: #fff; box-shadow: var(--shadow);
  overflow: hidden;
}
.dash-cover {
  display: block; width: 100%; aspect-ratio: 16/9;
  background: var(--surface); overflow: hidden;
}
.dash-cover img { width: 100%; height: 100%; object-fit: cover; display: block; }
.dash-cover-ph {
  display: flex; align-items: center; justify-content: center;
  width: 100%; height: 100%; font-size: 48px; color: var(--muted);
}
.dash-body { display: flex; flex-direction: column; gap: 6px; padding: 14px 16px 16px; }
.dash-top { margin-bottom: 2px; }
.dash-card h3 { margin: 0; font-size: 17px; line-height: 1.35; }
.dash-card h3 a { color: var(--text); text-decoration: none; }
.dash-card h3 a:hover { color: var(--primary); }
.dash-card .progress { margin: 6px 0 2px; }
.dash-card .btn { margin-top: 6px; }
.dash-badge {
  display: inline-block; padding: 2px 10px; border-radius: 999px;
  font-size: 12px; font-weight: 700;
}
.dash-badge.done { background: #dcfce7; color: #166534; }
.dash-badge.progress { background: #dbeafe; color: #1e40af; }
.dash-badge.new { background: #fef3c7; color: #92400e; }

/* ปุ่ม CTA ใต้รูป hero — เด่นชัด ใหญ่ ติด primary color */
.hero-cta-wrap { text-align: center; margin: 14px 0 6px; }
.btn-hero-cta {
  display: inline-flex; align-items: center; justify-content: center; gap: 8px;
  width: 100%; max-width: 380px;
  padding: 14px 24px;
  font-size: 17px; font-weight: 700;
  border-radius: 12px;
  box-shadow: 0 4px 12px rgba(37, 99, 235, .25);
  transition: transform .15s, box-shadow .15s;
}
.btn-hero-cta:hover {
  transform: translateY(-1px);
  box-shadow: 0 6px 16px rgba(37, 99, 235, .35);
}

/* ============ การ์ดคอร์ส (grid) ============ */
.course-grid {
  display: grid; grid-template-columns: 1fr; gap: 16px; margin: 16px 0 40px;
}
.course-card {
  border: 1px solid var(--border); border-radius: var(--radius);
  overflow: hidden; background: var(--bg); box-shadow: var(--shadow);
  transition: transform .12s;
}
.course-card:active { transform: scale(.99); }
.course-card .cover {
  width: 100%; aspect-ratio: 16 / 9; object-fit: cover; background: var(--surface);
}
.course-card .body { padding: 12px 14px 16px; }
.course-card h3 {
  font-size: 17px; margin: 0 0 6px; line-height: 1.4;
  /* >>> แก้ปัญหาเว็บเดิม: ชื่อยาวต้องตัดคำขึ้นบรรทัด ไม่ตกขอบ <<< */
  overflow-wrap: anywhere; word-break: break-word; white-space: normal;
}
.course-card .desc {
  color: var(--muted); font-size: 14px; margin: 0;
  display: -webkit-box; -webkit-line-clamp: 2; -webkit-box-orient: vertical; overflow: hidden;
}
.card-top { display: flex; justify-content: space-between; align-items: center; gap: 8px; margin-bottom: 6px; }

/* ============ ป้าย ============ */
.badge { font-size: 13px; font-weight: 700; padding: 3px 10px; border-radius: 999px; white-space: nowrap; }
.badge-free { background: #dcfce7; color: var(--free); }
.badge-paid { background: #fef3c7; color: var(--paid); }
.badge-managed { background: var(--surface); color: var(--muted); }
.badge-old { opacity: .65; font-weight: 500; margin-right: 2px; }
.badge-promo { background: #fef3c7; color: #92400e; }
.badge-discount { background: #fee2e2; color: #b91c1c; }

/* ============ bundle cards (หน้าแรก) ============ */
.bundle-grid {
  display: grid; grid-template-columns: 1fr; gap: 16px; margin: 12px 0 24px;
}
.bundle-card {
  position: relative; display: block;
  border: 2px solid #fcd34d; border-radius: var(--radius);
  background: linear-gradient(135deg, #fffbeb 0%, #fff 60%);
  overflow: hidden; box-shadow: var(--shadow); color: var(--text);
  transition: transform .12s;
}
.bundle-card:hover { transform: translateY(-2px); }
.bundle-card .cover { width: 100%; aspect-ratio: 16/9; object-fit: cover; background: var(--surface); }
.bundle-card .body { padding: 14px 16px 18px; }
.bundle-card h3 { font-size: 18px; margin: 4px 0 6px; overflow-wrap: anywhere; }
.bundle-card .desc {
  color: var(--muted); font-size: 14px; margin: 0 0 10px;
  display: -webkit-box; -webkit-line-clamp: 2; -webkit-box-orient: vertical; overflow: hidden;
}
.bundle-price { display: flex; align-items: baseline; gap: 8px; flex-wrap: wrap; }
.bundle-price s { color: var(--muted); font-weight: 500; }
.bundle-price b { color: var(--paid); font-size: 20px; }
.bundle-save { font-size: 13px; color: var(--free); font-weight: 700; }

/* ไอคอน 🎁 บนการ์ดคอร์ส (ที่อยู่ใน bundle) */
.course-card { position: relative; }
.card-promo-flag {
  position: absolute; top: 8px; left: 8px; z-index: 2;
  background: #fef3c7; border: 1px solid #fcd34d;
  font-size: 18px; padding: 4px 8px; border-radius: 999px;
  box-shadow: 0 1px 3px rgba(0,0,0,.15);
}

/* ============ banner "คอร์สนี้อยู่ในโปร" บนหน้าคอร์ส ============ */
.bundle-promo-banner {
  display: flex; align-items: center; gap: 12px;
  padding: 12px 14px; margin: 12px 0;
  background: linear-gradient(135deg, #fef3c7, #fffbeb);
  border: 1px solid #fcd34d; border-radius: var(--radius);
  color: var(--text); box-shadow: var(--shadow);
}
.bundle-promo-banner:hover { background: linear-gradient(135deg, #fde68a, #fef3c7); }
.bpb-icon { font-size: 24px; flex: none; }
.bpb-body { flex: 1; min-width: 0; }
.bpb-body b { display: block; overflow-wrap: anywhere; }
.bpb-detail { font-size: 13px; color: var(--muted); }
.bpb-arrow { font-size: 20px; color: var(--primary); font-weight: 800; flex: none; }

/* ============ หน้า bundle.html ============ */
.bundle-summary { text-align: center; padding: 18px; }
.bundle-price-big { display: flex; align-items: baseline; gap: 12px; justify-content: center; flex-wrap: wrap; }
.bundle-price-big .big { font-size: 32px; color: var(--paid); }
.bundle-price-big s { font-size: 18px; }
.bundle-save-big { margin-top: 6px; color: var(--free); }
.bundle-save-big b { font-size: 17px; }

.bundle-course-list { display: flex; flex-direction: column; gap: 10px; margin: 12px 0 24px; }
.bundle-course-item {
  display: flex; gap: 12px; padding: 10px;
  border: 1px solid var(--border); border-radius: var(--radius);
  background: var(--bg); color: var(--text); box-shadow: var(--shadow);
}
.bundle-course-item:hover { border-color: var(--primary); }
.bundle-course-item .cover {
  width: 120px; aspect-ratio: 16/9; object-fit: cover; flex: none;
  border-radius: 8px; background: var(--surface);
}
.bci-body { flex: 1; min-width: 0; }
.bci-body h3 { margin: 0 0 4px; font-size: 16px; overflow-wrap: anywhere; }
.bci-body p { font-size: 13px; margin: 0 0 6px;
  display: -webkit-box; -webkit-line-clamp: 2; -webkit-box-orient: vertical; overflow: hidden; }
.bci-meta { display: flex; gap: 8px; align-items: center; flex-wrap: wrap; font-size: 13px; }
@media (max-width: 480px) {
  .bundle-course-item .cover { width: 90px; }
}

/* ============ progress bar ============ */
.progress { height: 8px; background: var(--surface); border-radius: 999px; overflow: hidden; margin-top: 8px; }
.progress > span { display: block; height: 100%; background: var(--free); width: 0; }
.progress-label { font-size: 13px; color: var(--muted); margin-top: 4px; }

/* ============ หน้าเรียน: สารบัญบท ============ */
.lesson-list { list-style: none; padding: 0; margin: 0; }
.lesson-item {
  display: flex; align-items: flex-start; gap: 10px;
  padding: 12px 14px; border: 1px solid var(--border); border-radius: 10px;
  margin-bottom: 8px; cursor: pointer; background: var(--bg);
}
.lesson-item.active { border-color: var(--primary); background: #eff6ff; }
.lesson-item .lx-title {
  /* >>> ชื่อบทยาวต้องตัดคำ ไม่โดน ... ตัดหาย <<< */
  flex: 1; min-width: 0; overflow-wrap: anywhere; word-break: break-word; line-height: 1.4;
}
/* ปุ่มสถานะบทเรียน — สีชัดเจน: เขียว=ดูแล้ว / น้ำเงิน=ยังไม่ดู / เทา=ล็อก */
.lesson-item .lx-ico {
  flex: none;
  display: inline-flex; align-items: center; justify-content: center;
  width: 36px; height: 36px;
  border-radius: 50%;
  background: var(--primary); color: #fff;
  font-weight: 700; font-size: 15px; line-height: 1;
}
.lesson-item.done .lx-ico { background: var(--free); color: #fff; }
.lesson-item.locked .lx-ico { background: #9ca3af; color: #fff; }
.lesson-item.locked { opacity: .85; }
.lesson-item.locked .lx-title { color: var(--muted); }

/* หัวข้อหมวด (Section) ในสารบัญบท */
.section-head {
  list-style: none;
  font-weight: 800;
  font-size: 15px;
  color: var(--text);
  background: var(--surface);
  padding: 10px 14px;
  border-radius: 10px;
  margin: 14px 0 8px;
  overflow-wrap: anywhere;
}
.section-head .muted { font-weight: 500; font-size: 13px; }

/* ============ in-app browser gate ============ */
.inapp-gate {
  position: fixed; inset: 0; z-index: 9999; background: rgba(15,23,42,.6);
  display: flex; align-items: center; justify-content: center; padding: 20px;
}
.inapp-card {
  background: #fff; border-radius: var(--radius); padding: 26px 22px; max-width: 360px; text-align: center;
}
.inapp-card h2 { font-size: 20px; margin: 8px 0 12px; }
.inapp-card p { color: var(--muted); margin: 6px 0; }
.inapp-icon { font-size: 40px; }
.inapp-hint { font-size: 14px; }
.inapp-manual { font-size: 13px; margin-top: 14px; }

/* ============ ปุ่มติดต่อลอย ============ */
#contact-fab { position: fixed; right: 14px; bottom: 14px; z-index: 60; display: flex; flex-direction: column; gap: 10px; }
.fab {
  width: auto; min-width: 52px; height: 52px; padding: 0 14px; border-radius: 999px;
  display: flex; align-items: center; justify-content: center; color: #fff; font-weight: 800;
  box-shadow: 0 4px 12px rgba(0,0,0,.18);
}
.fab-fb { background: #1877f2; font-size: 22px; }
.fab-line { background: #06c755; font-size: 14px; }

/* ============ ทั่วไป ============ */
.section-title { font-size: 22px; margin: 24px 0 4px; }
.muted { color: var(--muted); }
.center { text-align: center; }
.loading { text-align: center; color: var(--muted); padding: 40px 0; }
.notice { background: var(--surface); border: 1px solid var(--border); border-radius: 10px; padding: 14px; margin: 14px 0; }
.lock-note { background: #fff7ed; border: 1px solid #fed7aa; color: #9a3412; border-radius: 10px; padding: 14px; }
.pay-info-box {
  background: var(--surface); border: 1px solid var(--border); border-radius: 10px;
  padding: 14px; margin: 10px 0;
}
.pay-info-label {
  font-size: 13px; color: var(--muted); font-weight: 700;
  margin-bottom: 6px; letter-spacing: .5px;
}
.pay-info-body {
  white-space: pre-wrap; font-size: 16px; font-weight: 600;
  color: var(--text); overflow-wrap: anywhere;
}

/* ============ กล่องอัปโหลดสลิป ============ */
.slip-box {
  background: #f0fdf4; border: 1px solid #86efac; border-radius: 10px;
  padding: 14px; margin: 10px 0;
}
.slip-label {
  font-weight: 700; font-size: 15px; margin-bottom: 8px; color: #166534;
}
.slip-thumb {
  display: inline-block; border: 1px solid var(--border); border-radius: 8px;
  overflow: hidden; max-width: 200px; background: #fff;
}
.slip-thumb img { display: block; width: 100%; height: auto; }
.slip-thumb:hover { border-color: var(--primary); }
.adm-slip { max-width: 160px; }

/* ============ หน้าเรียน (single-column flow: บทก่อน → วีดีโอ → บทถัดไป) ============ */
.learn-flow { max-width: 700px; margin: 0 auto; }
.back-link { display: inline-block; padding: 6px 0; }
.learn-title { font-size: 21px; margin: 8px 0 14px; line-height: 1.4; overflow-wrap: anywhere; }
.learn-nav { display: flex; align-items: center; justify-content: space-between; gap: 12px; margin: 16px 0 4px; }
.current-lesson {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 14px 14px 12px;
  margin: 12px 0;
  scroll-margin-top: 70px; /* offset ให้ pinned header */
  box-shadow: var(--shadow);
}
.lesson-text { font-size: 17px; }
.lesson-text h2 { font-size: 20px; margin: 18px 0 8px; }
.lesson-text h3 { font-size: 18px; margin: 16px 0 6px; }
.lesson-text p { margin: 0 0 12px; overflow-wrap: anywhere; }

/* ============ ไฟล์แถม (ebook) ============ */
.ebook-list { margin: 12px 0 24px; display: flex; flex-direction: column; gap: 8px; }
.ebook-row {
  display: flex; align-items: center; gap: 10px;
  padding: 12px 14px; border: 1px solid var(--border); border-radius: 10px;
  background: var(--bg);
}
.ebook-row.error { background: #fef2f2; border-color: #fecaca; }
.ebook-icon { flex: none; font-size: 20px; }
.ebook-title {
  flex: 1; min-width: 0; font-weight: 600;
  overflow-wrap: anywhere; word-break: break-word;
}
.ebook-actions { display: flex; gap: 6px; flex: none; flex-wrap: wrap; }

/* Modal สำหรับอ่าน */
.ebook-overlay {
  position: fixed; inset: 0; z-index: 9999;
  background: rgba(15, 23, 42, .75);
  display: flex; align-items: center; justify-content: center; padding: 16px;
}
.ebook-modal {
  background: #fff; border-radius: var(--radius);
  width: 100%; max-width: 1000px; height: 90vh;
  display: flex; flex-direction: column; overflow: hidden;
  box-shadow: 0 20px 50px rgba(0, 0, 0, .3);
}
.ebook-mhead {
  display: flex; align-items: center; justify-content: space-between;
  padding: 12px 16px; border-bottom: 1px solid var(--border);
}
.ebook-mhead h3 { margin: 0; font-size: 17px; overflow-wrap: anywhere; }
.ebook-x {
  background: transparent; border: 0; font-size: 22px; cursor: pointer;
  color: var(--muted); padding: 4px 10px;
}
.ebook-x:hover { color: var(--text); }
.ebook-frame { flex: 1; border: 0; width: 100%; background: #f3f4f6; }

/* ============ จอใหญ่ขึ้น ============ */
@media (min-width: 640px) {
  .course-grid { grid-template-columns: 1fr 1fr; }
  .bundle-grid { grid-template-columns: 1fr 1fr; }
  .hero-text h1 { font-size: 32px; }
}
@media (min-width: 900px) {
  .course-grid { grid-template-columns: 1fr 1fr 1fr; }
}
