/* ============================================
   components.css — cards, alerts, code, tables…
   ============================================ */

/* ========================================================
   Hero — cinematic dark showcase (homepage centerpiece)
   ======================================================== */
.hero {
  position: relative;
  margin: -2.4rem -2.75rem 2.75rem;
  padding: 3.5rem 2.75rem 3rem;
  border-radius: var(--radius-lg) var(--radius-lg) 0 0;
  overflow: hidden;
  isolation: isolate;
  background:
    radial-gradient(60% 105% at 86% -18%, color-mix(in srgb, var(--c-primary) 18%, transparent), transparent 60%),
    radial-gradient(52% 95% at 4% -12%, color-mix(in srgb, #14b8a6 14%, transparent), transparent 58%),
    linear-gradient(180deg, #edf7f3 0%, #ffffff 72%);
  border-bottom: 1px solid var(--c-border);
}

/* Layer 1 — drifting aurora gradient mesh */
.hero-bg {
  position: absolute;
  inset: -45%;
  z-index: -3;
  background:
    radial-gradient(38% 48% at 18% 28%, rgba(20, 184, 166, 0.55), transparent 60%),
    radial-gradient(42% 52% at 82% 12%, rgba(16, 185, 129, 0.42), transparent 62%),
    radial-gradient(46% 56% at 62% 92%, rgba(13, 148, 110, 0.45), transparent 66%),
    radial-gradient(40% 50% at 95% 70%, rgba(45, 212, 191, 0.30), transparent 60%);
  filter: blur(30px) saturate(1.1);
  opacity: 0.5;
  animation: heroDrift 20s ease-in-out infinite alternate;
}
@keyframes heroDrift {
  0%   { transform: translate3d(-3%, -2%, 0) scale(1.05) rotate(0deg); }
  100% { transform: translate3d(3%, 4%, 0)  scale(1.18) rotate(4deg); }
}

/* Layer 2 — fine dot grid, fading toward the bottom */
.hero::before {
  content: '';
  position: absolute;
  inset: 0;
  z-index: -2;
  background-image: radial-gradient(rgba(12, 122, 94, 0.13) 1px, transparent 1.3px);
  background-size: 26px 26px;
  -webkit-mask-image: linear-gradient(180deg, #000 0%, transparent 85%);
  mask-image: linear-gradient(180deg, #000 0%, transparent 85%);
  pointer-events: none;
}

/* Layer 3 — vignette + top sheen for depth */
.hero::after {
  content: '';
  position: absolute;
  inset: 0;
  z-index: -1;
  background:
    radial-gradient(36% 44% at var(--mx, 78%) var(--my, 20%), rgba(20, 184, 166, 0.16), transparent 62%),
    linear-gradient(180deg, rgba(255, 255, 255, 0.55), transparent 22%);
  pointer-events: none;
}

.hero-grid {
  position: relative;
  z-index: 1;
  display: grid;
  grid-template-columns: 1.2fr 1fr;
  gap: 2.75rem;
  align-items: center;
}
/* particle network canvas — above the aurora, behind the content
   (used by both .hero and .chapter-header; overrides .chapter-header > * z-index) */
.fx-particles {
  position: absolute;
  inset: 0;
  z-index: 0;
  pointer-events: none;
}

/* entrance animation */
.hero-content > * { animation: heroRise 0.7s cubic-bezier(0.16, 1, 0.3, 1) both; }
.hero-content > :nth-child(1) { animation-delay: 0.02s; }
.hero-content > :nth-child(2) { animation-delay: 0.08s; }
.hero-content > :nth-child(3) { animation-delay: 0.14s; }
.hero-content > :nth-child(4) { animation-delay: 0.20s; }
.hero-content > :nth-child(5) { animation-delay: 0.26s; }
.hero-image { animation: heroRise 0.8s cubic-bezier(0.16, 1, 0.3, 1) 0.18s both; }
@keyframes heroRise {
  from { opacity: 0; transform: translateY(14px); }
  to   { opacity: 1; transform: translateY(0); }
}
html.reduce-motion .hero-bg { animation: none; }
html.reduce-motion .hero-content > *,
html.reduce-motion .hero-image { animation: none; }

.hero-eyebrow {
  display: inline-flex;
  align-items: center;
  gap: 0.45rem;
  font-size: 0.74rem;
  font-weight: 600;
  letter-spacing: 0.03em;
  color: var(--c-primary-dark);
  background: color-mix(in srgb, var(--c-primary) 9%, #fff);
  border: 1px solid color-mix(in srgb, var(--c-primary) 24%, transparent);
  backdrop-filter: blur(8px);
  padding: 0.32rem 0.8rem;
  border-radius: 999px;
  margin-bottom: 1.25rem;
}
.hero-eyebrow::before {
  content: '';
  width: 6px; height: 6px;
  border-radius: 50%;
  background: #2dd4bf;
  box-shadow: 0 0 0 3px rgba(45, 212, 191, 0.25), 0 0 10px #2dd4bf;
}
.hero h1 {
  font-size: 2.95rem;
  margin: 0;
  letter-spacing: -0.038em;
  line-height: 1.08;
  font-weight: 800;
  color: var(--c-text);
}
.hero-plus {
  background-image: linear-gradient(110deg, #0b7a5e 0%, #12a07c 28%, #2ee6bf 50%, #12a07c 72%, #0b7a5e 100%);
  background-size: 220% auto;
  -webkit-background-clip: text;
  background-clip: text;
  -webkit-text-fill-color: transparent;
  color: transparent;
  font-weight: 800;
  white-space: nowrap;
  animation: heroShimmer 6.5s linear infinite;
}
@keyframes heroShimmer { to { background-position: 220% center; } }
[data-theme="dark"] .hero-plus {
  background-image: linear-gradient(110deg, #34d399 0%, #5eead4 30%, #d6fff2 50%, #5eead4 70%, #34d399 100%);
  background-size: 220% auto;
}
.hero .hero-sub {
  margin-top: 1rem;
  font-size: 1.02rem;
  color: var(--c-text-soft);
  line-height: 1.7;
  max-width: 42ch;
}
.hero .hero-sub strong { color: var(--c-text); }
.hero-meta {
  display: grid;
  grid-template-columns: repeat(2, 1fr);
  gap: 0.55rem;
  margin: 1.6rem 0;
  max-width: 22rem;
}
.hero-meta > div {
  margin: 0;
  background: rgba(255, 255, 255, 0.72);
  border: 1px solid var(--c-border);
  border-radius: var(--radius);
  padding: 0.65rem 0.7rem;
  backdrop-filter: blur(8px);
}
.hero-meta dt {
  font-size: 0.62rem;
  text-transform: uppercase;
  letter-spacing: 0.07em;
  color: var(--c-muted);
  font-weight: 600;
  margin: 0;
}
.hero-meta dd {
  margin: 0.25rem 0 0;
  font-size: 1.08rem;
  color: var(--c-text);
  font-weight: 700;
  letter-spacing: -0.01em;
}
.hero-meta .hm-thb {
  display: block;
  font-weight: 400;
  font-size: 0.74rem;
  color: var(--c-muted);
  margin-top: 0.1rem;
  letter-spacing: 0;
}
.hero .hero-actions {
  margin-top: 1.3rem;
  display: flex; flex-wrap: wrap; gap: 0.6rem;
}
/* hero primary keeps its glow in both themes (ghost uses global style in light) */
.hero .btn-primary {
  padding: 0.65rem 1.3rem;
  box-shadow: inset 0 1px 0 rgba(255, 255, 255, 0.25),
              0 6px 20px rgba(20, 184, 166, 0.4);
}

.hero-image {
  position: relative;
  text-align: center;
}
/* radial spotlight glow behind the product */
.hero-image::before {
  content: '';
  position: absolute;
  inset: -8% -4%;
  z-index: 0;
  background: radial-gradient(50% 50% at 50% 45%, rgba(20, 184, 166, 0.22), transparent 70%);
  filter: blur(12px);
  pointer-events: none;
}
.hero-image img {
  position: relative;
  z-index: 1;
  max-width: 100%;
  height: auto;
  max-height: 340px;
  object-fit: contain;
  border-radius: var(--radius-lg);
  background: #ffffff;
  padding: 1.1rem;
  border: 1px solid var(--c-border);
  box-shadow: var(--shadow-lg);
  backdrop-filter: blur(4px);
}
.hero-image-cap {
  position: relative;
  z-index: 1;
  margin: 0.7rem 0 0;
  font-size: 0.76rem;
  color: var(--c-muted);
  font-style: italic;
}

/* ========================================================
   Dark mode — restore the cinematic dark showcase hero
   ======================================================== */
[data-theme="dark"] .hero {
  background: #060a10;
  border-bottom: none;
}
[data-theme="dark"] .hero-bg { opacity: 1; filter: blur(26px) saturate(1.25); }
[data-theme="dark"] .hero::before {
  background-image: radial-gradient(rgba(255, 255, 255, 0.08) 1px, transparent 1.3px);
}
[data-theme="dark"] .hero::after {
  background:
    radial-gradient(38% 46% at var(--mx, 78%) var(--my, 20%), rgba(45, 212, 191, 0.20), transparent 62%),
    linear-gradient(180deg, rgba(255, 255, 255, 0.05), transparent 18%),
    radial-gradient(130% 120% at 50% -10%, transparent 52%, rgba(0, 0, 0, 0.55));
}
[data-theme="dark"] .hero-eyebrow {
  color: #a9f3e0;
  background: rgba(255, 255, 255, 0.06);
  border-color: rgba(255, 255, 255, 0.16);
}
[data-theme="dark"] .hero h1 { color: #f3f7fa; }
[data-theme="dark"] .hero .hero-sub { color: rgba(226, 232, 240, 0.78); }
[data-theme="dark"] .hero .hero-sub strong { color: #eaf6f2; }
[data-theme="dark"] .hero-meta > div {
  background: rgba(255, 255, 255, 0.05);
  border-color: rgba(255, 255, 255, 0.1);
}
[data-theme="dark"] .hero-meta dt,
[data-theme="dark"] .hero-meta .hm-thb,
[data-theme="dark"] .hero-image-cap { color: rgba(226, 232, 240, 0.55); }
[data-theme="dark"] .hero-meta dd { color: #fff; }
[data-theme="dark"] .hero .btn-ghost {
  background: rgba(255, 255, 255, 0.06);
  color: #eaf0f4;
  border-color: rgba(255, 255, 255, 0.18);
  backdrop-filter: blur(8px);
  box-shadow: none;
}
[data-theme="dark"] .hero .btn-ghost:hover {
  background: rgba(255, 255, 255, 0.12);
  border-color: rgba(255, 255, 255, 0.32);
  color: #fff;
}
[data-theme="dark"] .hero-image::before {
  background: radial-gradient(50% 50% at 50% 45%, rgba(45, 212, 191, 0.35), transparent 70%);
}
[data-theme="dark"] .hero-image img {
  background: rgba(255, 255, 255, 0.04);
  border-color: rgba(255, 255, 255, 0.12);
  box-shadow: 0 24px 60px rgba(0, 0, 0, 0.5),
              inset 0 1px 0 rgba(255, 255, 255, 0.08);
}

@media (max-width: 860px) {
  .hero {
    margin: -1.5rem -1.25rem 1.75rem;
    padding: 2rem 1.25rem 1.5rem;
  }
  .hero-grid { grid-template-columns: 1fr; gap: 1.5rem; }
  .hero-image { order: -1; }
  .hero-image img { max-height: 240px; }
}
@media (max-width: 540px) {
  .hero h1 { font-size: 1.95rem; }
  .hero-meta { grid-template-columns: 1fr 1fr; gap: 0.5rem; }
}

/* ---------- Homepage code preview (Hero → Apps transition) ---------- */
.code-preview {
  margin: 2rem 0;
  padding: 1.25rem 1.5rem;
  background: var(--c-surface);
  border: 1px solid var(--c-border);
  border-radius: var(--radius);
}
.code-preview-label {
  font-size: 0.85rem;
  font-weight: 600;
  color: var(--c-text-soft);
  margin-bottom: 0.6rem;
}
.code-preview pre { margin: 0.5rem 0 0.6rem; }
.code-preview-note {
  margin: 0.5rem 0 0;
  font-size: 0.82rem;
  color: var(--c-muted);
  font-style: italic;
}

/* ---------- "ทำอะไรได้บ้าง" — calm 2-column list ---------- */
.app-preview {
  display: grid;
  grid-template-columns: repeat(2, 1fr);
  gap: 0.5rem 2rem;
  margin: 1rem 0 0.5rem;
}
.app-card {
  padding: 0.7rem 0;
  border-bottom: 1px solid var(--c-border);
}
.app-card h3 {
  margin: 0;
  font-size: 0.98rem;
  color: var(--c-text);
  font-weight: 600;
}
.app-card p {
  margin: 0.2rem 0 0;
  font-size: 0.86rem;
  color: var(--c-muted);
  line-height: 1.55;
}
@media (max-width: 600px) {
  .app-preview { grid-template-columns: 1fr; gap: 0; }
}

/* ---------- Start list (homepage "เริ่มจากตรงไหนดี") ---------- */
.start-list {
  list-style: none;
  padding-left: 0;
  margin: 1rem 0;
}
.start-list li {
  padding: 0.55rem 0;
  border-bottom: 1px solid var(--c-border);
  color: var(--c-text-soft);
  line-height: 1.7;
}
.start-list li:last-child { border-bottom: none; }
.start-list a { font-weight: 600; }

/* ---------- Reading order (homepage "ลำดับการอ่าน") ---------- */
.reading-order {
  counter-reset: ro;
  list-style: none;
  padding-left: 0;
  margin: 1rem 0;
}
.reading-order > li {
  counter-increment: ro;
  position: relative;
  padding: 0.65rem 0 0.65rem 2.4rem;
  border-bottom: 1px solid var(--c-border);
  line-height: 1.7;
}
.reading-order > li:last-child { border-bottom: none; }
.reading-order > li::before {
  content: counter(ro);
  position: absolute;
  left: 0;
  top: 0.7rem;
  width: 1.6rem;
  height: 1.6rem;
  display: flex;
  align-items: center;
  justify-content: center;
  border: 1px solid var(--c-border-strong);
  border-radius: 50%;
  font-size: 0.82rem;
  font-weight: 600;
  color: var(--c-text-soft);
}
.ro-note {
  display: inline-block;
  margin-left: 0.4rem;
  font-size: 0.8rem;
  color: var(--c-muted);
}

/* ---------- Image Lightbox ---------- */
.lightbox {
  position: fixed;
  inset: 0;
  z-index: 200;
  display: none;
  align-items: center;
  justify-content: center;
  background: rgba(0, 0, 0, 0.92);
  backdrop-filter: blur(4px);
  padding: 2rem;
  animation: lb-fade-in 0.2s ease;
}
.lightbox.is-open { display: flex; }
.lightbox-img {
  max-width: 95vw;
  max-height: 80vh;
  object-fit: contain;
  border-radius: var(--radius);
  box-shadow: 0 20px 60px rgba(0,0,0,0.6);
  cursor: zoom-out;
  display: block;
}
.lightbox-caption {
  position: absolute;
  bottom: 1.5rem;
  left: 50%;
  transform: translateX(-50%);
  max-width: 80vw;
  text-align: center;
  color: #fff;
  font-size: 0.9rem;
  background: rgba(0,0,0,0.5);
  padding: 0.4rem 1rem;
  border-radius: 999px;
  line-height: 1.5;
}
.lightbox-close {
  position: absolute;
  top: 1rem;
  right: 1rem;
  width: 44px;
  height: 44px;
  border-radius: 50%;
  background: rgba(255,255,255,0.12);
  color: #fff;
  border: none;
  font-size: 1.2rem;
  cursor: pointer;
  transition: background 0.15s, transform 0.15s;
}
.lightbox-close:hover {
  background: rgba(255,255,255,0.22);
  transform: rotate(90deg);
}
@keyframes lb-fade-in {
  from { opacity: 0; }
  to   { opacity: 1; }
}

/* Card icon hover — subtle */
.card-icon {
  transition: transform 0.2s ease;
}
.card:hover .card-icon {
  transform: scale(1.03);
}

/* ---------- Responsive Video Embed (YouTube) ---------- */
.video-embed {
  position: relative;
  width: 100%;
  max-width: 720px;
  margin: 1.5rem auto;
  background: var(--c-code-bg);
  border-radius: var(--radius);
  overflow: hidden;
  box-shadow: var(--shadow);
}
.video-embed::before {
  content: '';
  display: block;
  padding-bottom: 56.25%; /* 16:9 ratio */
}
.video-embed iframe,
.video-embed .video-placeholder {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  border: none;
}
.video-placeholder {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 0.75rem;
  background: linear-gradient(135deg, var(--c-primary-dark), var(--c-bg) 90%);
  color: #fff;
  text-decoration: none;
  cursor: pointer;
}
.video-placeholder:hover { text-decoration: none; }
.video-placeholder .play-icon {
  width: 70px;
  height: 70px;
  background: rgba(255,255,255,0.18);
  border: 3px solid #fff;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 2rem;
  transition: transform 0.2s, background 0.2s;
}
.video-placeholder:hover .play-icon {
  background: rgba(255,255,255,0.3);
  transform: scale(1.08);
}
.video-placeholder .play-label {
  font-size: 1rem;
  font-weight: 600;
}
.video-placeholder .play-sub {
  font-size: 0.82rem;
  opacity: 0.8;
}
.video-caption {
  text-align: center;
  font-size: 0.86rem;
  color: var(--c-muted);
  font-style: italic;
  margin-top: 0.5rem;
}

/* ---------- Section divider (decorative) ---------- */
.section-divider {
  display: flex;
  align-items: center;
  gap: 1rem;
  margin: 3rem 0;
  color: var(--c-muted);
}
.section-divider::before,
.section-divider::after {
  content: '';
  flex: 1;
  height: 1px;
  background: linear-gradient(90deg, transparent 0%, var(--c-border) 50%, transparent 100%);
}
.section-divider .divider-icon {
  font-size: 1.2rem;
  opacity: 0.7;
}

/* ---------- Buttons — minimal docs style ---------- */
.btn {
  display: inline-flex;
  align-items: center;
  gap: 0.45rem;
  padding: 0.6rem 1.1rem;
  border-radius: var(--radius-sm);
  font-weight: 600;
  font-size: 0.9rem;
  letter-spacing: -0.005em;
  border: 1px solid transparent;
  transition: background 0.13s, border-color 0.13s, color 0.13s, box-shadow 0.13s;
}
.btn:hover { text-decoration: none; }
.btn-primary {
  background: var(--grad);
  color: #fff;
  box-shadow: inset 0 1px 0 rgba(255, 255, 255, 0.18),
              0 2px 6px color-mix(in srgb, var(--c-primary) 35%, transparent);
}
.btn-primary:hover { filter: brightness(1.06) saturate(1.05); }
[data-theme="dark"] .btn-primary { color: #04130f; }
.btn-outline {
  background: transparent;
  color: var(--c-text);
  border-color: var(--c-border-strong);
}
.btn-outline:hover { border-color: var(--c-text); }
.btn-ghost {
  background: var(--c-bg);
  color: var(--c-text);
  border-color: var(--c-border-strong);
  box-shadow: var(--shadow-sm);
}
.btn-ghost:hover {
  background: var(--c-surface);
  color: var(--c-text);
  border-color: var(--c-muted);
}

/* ---------- Cards / feature grid ---------- */
.card-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(220px, 1fr));
  gap: 1rem;
  margin: 1.25rem 0;
}
.card {
  display: block;
  padding: 1.15rem 1.2rem;
  background: var(--c-bg);
  border: 1px solid var(--c-border);
  border-radius: var(--radius);
  color: var(--c-text);
  transition: border-color 0.15s, background 0.15s;
  min-width: 0;   /* กัน <pre> หรือข้อความยาวดัน grid cell จนล้น */
}
.card:hover {
  border-color: var(--c-border-strong);
  background: var(--c-surface);
  text-decoration: none;
}
.card .card-icon {
  width: 36px; height: 36px;
  display: inline-flex;
  align-items: center; justify-content: center;
  background: var(--c-surface-2);
  color: var(--c-text-soft);
  border: 1px solid var(--c-border);
  border-radius: 9px;
  font-size: 1.15rem;
  font-weight: 700;
  margin-bottom: 0.65rem;
}
.card:hover .card-icon {
  color: var(--c-primary);
  border-color: color-mix(in srgb, var(--c-primary) 40%, transparent);
}
.card h3 { margin: 0.2rem 0 0.3rem; font-size: 1.05rem; }
.card p  { margin: 0; color: var(--c-muted); font-size: 0.92rem; }

/* ---------- Alert callouts — minimal docs style ---------- */
.callout {
  display: grid;
  grid-template-columns: auto 1fr;
  gap: 0.8rem;
  padding: 0.9rem 1.1rem;
  border: 1px solid var(--c-border);
  border-left: 3px solid var(--c-border-strong);
  border-radius: var(--radius-sm);
  margin: 1.25rem 0;
  background: var(--c-surface);
}
.callout .ico {
  font-size: 1.05rem;
  line-height: 1.5;
  opacity: 0.9;
}
/* ยอมให้ช่องเนื้อหาหดได้ ไม่งั้น <pre> โค้ดบรรทัดยาวจะดันกรอบจนล้น */
.callout .body { min-width: 0; }
.callout .body p:first-child { margin-top: 0; }
.callout .body p:last-child  { margin-bottom: 0; }
.callout .title {
  display: block;
  font-weight: 600;
  margin-bottom: 0.2rem;
  color: var(--c-text);
}

.callout-warning { border-color: var(--c-warning-border); border-left-color: var(--c-warning); background: var(--c-warning-bg); }
.callout-warning .title { color: var(--c-warning); }

.callout-info    { border-color: var(--c-info-border); border-left-color: var(--c-info); background: var(--c-info-bg); }
.callout-info .title { color: var(--c-info); }

.callout-tip     { border-color: var(--c-tip-border); border-left-color: var(--c-tip); background: var(--c-tip-bg); }
.callout-tip .title { color: var(--c-tip); }

.callout-danger  { border-color: var(--c-danger-border); border-left-color: var(--c-danger); background: var(--c-danger-bg); }
.callout-danger .title { color: var(--c-danger); }

/* ---------- Code blocks ---------- */
:not(pre) > code {
  font-family: var(--font-mono);
  background: var(--c-surface);
  color: var(--c-text);
  border: 1px solid var(--c-border);
  padding: 0.08rem 0.4rem;
  border-radius: 5px;
  font-size: 0.85em;
  font-weight: 500;
}
[data-theme="dark"] :not(pre) > code {
  background: var(--c-surface-2);
  border-color: var(--c-border);
  color: #e6e9ee;
}

pre {
  position: relative;
  background: var(--c-code-bg);
  color: #dfe5ee;
  padding: 1.05rem 1.25rem;
  /* เผื่อพื้นที่ขวาให้ปุ่ม Copy ไม่ทับโค้ด */
  padding-right: 5rem;
  border: 1px solid color-mix(in srgb, var(--c-code-bg) 78%, #ffffff);
  border-radius: var(--radius);
  overflow-x: auto;
  font-family: var(--font-mono);
  font-size: 0.88rem;
  line-height: 1.7;
  margin: 1.25rem 0;
  box-shadow: var(--shadow-sm);
  -webkit-font-smoothing: auto;
}

/* ---------- Code wrapper (injected by app.js) ----------
   ห่อ <pre> เพื่อให้ปุ่ม Copy ยึดกับขอบนอก ไม่เลื่อนตามโค้ดบรรทัดยาว
   (<pre> ข้างในยังคง overflow-x:auto เลื่อนได้ตามปกติ)
   ไม่ตั้ง margin ที่นี่ — ปล่อยให้ margin ของ <pre> collapse ทะลุออกมา
   ระยะห่างจึงเท่าเดิมในทุก container (code-preview, err-fix, cheat-block ฯลฯ) */
/* .code-wrap = กรอบหน้าต่างโค้ด (ย้าย border/radius/shadow มาจาก pre)
   margin เท่าเดิมตามแต่ละ container กำหนดด้านล่าง */
.code-wrap {
  position: relative;
  width: 100%;
  margin: 1.25rem 0;
  border: 1px solid color-mix(in srgb, var(--c-code-bg) 78%, #ffffff);
  border-radius: var(--radius);
  overflow: hidden;
  box-shadow: var(--shadow-sm);
}
.code-preview .code-wrap { margin: 0.5rem 0 0.6rem; }
.cheat-block  .code-wrap { margin: 0; }
.err-fix      .code-wrap { margin: 0.5rem 0; }

/* แถบหัวจริง: [จุดไฟจราจร] [ป้ายภาษา] ...... [ปุ่มคัดลอก] เรียงด้วย flex */
.code-bar {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  height: 2.1rem;
  padding: 0 0.5rem 0 0.85rem;
  background: var(--c-code-bg-soft);
  border-bottom: 1px solid rgba(255, 255, 255, 0.08);
}
.code-dots {
  flex: none;
  width: 11px; height: 11px;
  border-radius: 50%;
  background: #ff5f57;
  box-shadow: 18px 0 0 #febc2e, 36px 0 0 #28c840;  /* จุดที่ 2-3 */
  margin-right: 2.6rem;                            /* เผื่อจุดที่ 2-3 (เงา) */
}
.code-bar-label {
  flex: none;
  background: none !important;
  border: none !important;
  border-radius: 0 !important;
  padding: 0 !important;
  margin: 0 !important;
  color: rgba(255, 255, 255, 0.55);
  font-family: var(--font-mono);
  font-size: 0.72rem;
  letter-spacing: 0.02em;
}

/* <pre> ภายในกรอบ: ไม่มี border/radius/shadow ของตัวเอง (กรอบอยู่ที่ wrap) */
.code-wrap > pre,
[data-theme="dark"] .code-wrap > pre {
  width: 100%;
  margin: 0;
  border: none;
  border-radius: 0;
  box-shadow: none;
  padding: 1.05rem 1.25rem;
}

/* ---------- Copy button (injected by app.js — อยู่บนแถบหัว) ---------- */
.copy-btn {
  margin-left: auto;            /* ดันชิดขวาสุดของแถบ — อยู่ในกระแส layout ไม่ตกขอบ */
  flex: none;
  background: #334155;
  color: #f8fafc;
  border: 1px solid #64748b;
  border-radius: 6px;
  padding: 3px 11px;
  font-size: 0.76rem;
  font-family: inherit;
  line-height: 1.35;
  cursor: pointer;
  transition: background 0.15s, border-color 0.15s;
}
.copy-btn:hover {
  background: #475569;
  border-color: #94a3b8;
}
.copy-btn.is-copied {
  background: #065f46;
  border-color: #10b981;
  color: #d1fae5;
}

/* Narrow screens: shrink copy button text → just icon */
@media (max-width: 540px) {
  .copy-btn {
    font-size: 0;
    padding: 4px 8px;
  }
  .copy-btn::before {
    content: '⧉';
    font-size: 0.95rem;
    line-height: 1;
  }
  .copy-btn.is-copied { font-size: 0; }
  .copy-btn.is-copied::before { content: '✓'; color: #6ee7b7; }
}
pre code { background: transparent; color: inherit; padding: 0; border: none; }
pre .cm { color: #8a93a3; font-style: italic; }   /* comment */
pre .kw { color: #c9a6ff; }                        /* keyword */
pre .st { color: #b6e2a1; }                        /* string */
pre .fn { color: #7cc4ff; }                        /* function name */
pre .nu { color: #f1b56c; }                        /* number */

/* ป้ายภาษา (.code-label) ก่อน JS ห่อ — แท็บเล็กๆ (เห็นแวบเดียวก่อนกลายเป็นข้อความบนแถบ) */
.code-label {
  display: inline-block;
  background: var(--c-surface-2);
  color: var(--c-muted);
  font-family: var(--font-mono);
  font-size: 0.78rem;
  padding: 0.2rem 0.55rem;
  border-radius: var(--radius-sm) var(--radius-sm) 0 0;
  border: 1px solid var(--c-border);
  border-bottom: none;
  margin-bottom: -1px;
}

/* ---------- Tables ---------- */
.table-wrap {
  overflow-x: auto;
  margin: 1.5rem 0;
  border: 1px solid var(--c-border);
  border-radius: var(--radius);
}
table {
  width: 100%;
  border-collapse: collapse;
  font-size: 0.92rem;
  background: var(--c-bg);
}
.table-wrap table { border-radius: var(--radius); overflow: hidden; }
thead {
  background: var(--c-surface);
  color: var(--c-text);
  border-bottom: 1px solid var(--c-border-strong);
}
th, td {
  padding: 0.65rem 0.95rem;
  text-align: left;
  border-bottom: 1px solid var(--c-border);
  vertical-align: top;
  line-height: 1.6;
}
th {
  font-weight: 600;
  font-size: 0.78rem;
  text-transform: uppercase;
  letter-spacing: 0.04em;
  color: var(--c-muted);
}
tbody tr:hover { background: var(--c-surface); }
tbody tr:last-child td { border-bottom: none; }

/* ---------- Figures / images with caption ---------- */
figure {
  margin: 1.5rem 0;
  padding: 0.75rem;
  background: var(--c-surface);
  border: 1px solid var(--c-border);
  border-radius: var(--radius);
  text-align: center;
}
figure img {
  margin: 0 auto;
  border-radius: var(--radius-sm);
  max-height: 420px;
  object-fit: contain;
}
figcaption {
  margin-top: 0.6rem;
  font-size: 0.88rem;
  color: var(--c-muted);
  font-style: italic;
}

/* ---------- Checklists (rendered as ul) ---------- */
.checklist {
  list-style: none;
  padding-left: 0;
}
.checklist li {
  position: relative;
  padding-left: 1.85rem;
  margin: 0.45rem 0;
}
.checklist li::before {
  content: '☐';
  position: absolute;
  left: 0; top: -1px;
  color: var(--c-primary);
  font-size: 1.15rem;
  font-weight: 700;
}

/* ---------- Step list (numbered) ---------- */
.steps {
  list-style: none;
  padding-left: 0;
  counter-reset: step;
}
.steps > li {
  position: relative;
  padding: 0.6rem 0 0.6rem 3rem;
  counter-increment: step;
  margin: 0.5rem 0;
}
.steps > li::before {
  content: counter(step);
  position: absolute;
  left: 0; top: 0.55rem;
  width: 30px; height: 30px;
  background: var(--c-primary);
  color: #fff;
  border-radius: 50%;
  display: flex;
  align-items: center; justify-content: center;
  font-weight: 700;
  font-size: 0.85rem;
}

/* ---------- Spec / KV list ---------- */
.spec-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(220px, 1fr));
  gap: 0.75rem;
  margin: 1.25rem 0;
}
.spec-item {
  background: var(--c-surface);
  border: 1px solid var(--c-border);
  border-radius: var(--radius);
  padding: 0.85rem 1rem;
}
.spec-item .k {
  display: block;
  font-size: 0.78rem;
  color: var(--c-muted);
  text-transform: uppercase;
  letter-spacing: 0.04em;
  margin-bottom: 0.2rem;
}
.spec-item .v { font-weight: 600; color: var(--c-text); }

/* ---------- FAQ accordion ---------- */
.faq details {
  border: 1px solid var(--c-border);
  border-radius: var(--radius);
  padding: 0;
  margin: 0.6rem 0;
  background: var(--c-bg);
  overflow: hidden;
}
.faq details[open] { box-shadow: var(--shadow-sm); }
.faq summary {
  cursor: pointer;
  padding: 0.85rem 1.1rem;
  font-weight: 600;
  color: var(--c-text);
  list-style: none;
  display: flex;
  align-items: center;
  gap: 0.6rem;
}
.faq summary::-webkit-details-marker { display: none; }
.faq summary::before {
  content: '＋';
  color: var(--c-primary);
  font-weight: 700;
  transition: transform 0.2s;
}
.faq details[open] summary::before { content: '－'; }
.faq summary:hover { background: var(--c-surface); }
.faq .faq-body {
  padding: 0 1.1rem 0.95rem;
  color: var(--c-text-soft);
  border-top: 1px solid var(--c-border);
}

/* ---------- Badges / tags ---------- */
.badge {
  display: inline-block;
  padding: 0.15rem 0.55rem;
  border-radius: 999px;
  font-size: 0.78rem;
  font-weight: 600;
  background: var(--c-surface-2);
  color: var(--c-text-soft);
  border: 1px solid var(--c-border);
}
.badge-primary { background: var(--c-primary-soft); color: var(--c-primary-dark); border-color: transparent; }
.badge-accent  { background: var(--c-accent-soft);  color: #92400e; border-color: transparent; }

/* ---------- Search modal & topbar trigger ---------- */
.topbar-search-btn {
  display: inline-flex;
  align-items: center;
  gap: 0.4rem;
  padding: 0.4rem 0.65rem;
  background: var(--c-surface);
  border: 1px solid var(--c-border);
  border-radius: var(--radius-sm);
  color: var(--c-text-soft);
  font-size: 0.9rem;
  cursor: pointer;
  transition: background 0.15s, border-color 0.15s;
}
.topbar-search-btn:hover { background: var(--c-surface-2); border-color: var(--c-primary); }
.topbar-search-btn kbd {
  font-family: var(--font-mono);
  font-size: 0.7rem;
  background: var(--c-bg);
  border: 1px solid var(--c-border);
  border-radius: 4px;
  padding: 0.05rem 0.35rem;
  color: var(--c-muted);
}

.search-overlay {
  position: fixed;
  inset: 0;
  background: rgba(15, 23, 42, 0.55);
  backdrop-filter: blur(4px);
  z-index: 100;
  display: none;
  align-items: flex-start;
  justify-content: center;
  padding-top: 10vh;
}
.search-overlay.is-open { display: flex; }
.search-modal {
  width: min(560px, 92vw);
  background: var(--c-bg);
  color: var(--c-text);
  border: 1px solid var(--c-border);
  border-radius: var(--radius-lg);
  box-shadow: 0 20px 60px rgba(0,0,0,0.25);
  overflow: hidden;
}
.search-input-wrap {
  display: flex;
  align-items: center;
  gap: 0.6rem;
  padding: 0.85rem 1rem;
  border-bottom: 1px solid var(--c-border);
}
.search-icon { font-size: 1.1rem; }
.search-input {
  flex: 1;
  border: none;
  outline: none;
  font: inherit;
  font-size: 1rem;
  background: transparent;
  color: var(--c-text);
}
.search-kbd {
  font-family: var(--font-mono);
  font-size: 0.72rem;
  background: var(--c-surface-2);
  border: 1px solid var(--c-border);
  border-radius: 4px;
  padding: 0.1rem 0.4rem;
  color: var(--c-muted);
}
.search-results {
  max-height: 50vh;
  overflow-y: auto;
  padding: 0.4rem 0;
}
.search-item {
  display: grid;
  grid-template-columns: auto 1fr auto;
  align-items: center;
  gap: 0.7rem;
  padding: 0.55rem 1rem;
  color: var(--c-text);
  font-size: 0.95rem;
}
.search-item:hover, .search-item.is-active {
  background: var(--c-primary-soft);
  text-decoration: none;
}
.search-item-section {
  font-size: 0.72rem;
  text-transform: uppercase;
  letter-spacing: 0.04em;
  color: var(--c-muted);
  min-width: 80px;
}
.search-item-title mark { background: var(--c-accent); color: #1f2937; padding: 0 2px; border-radius: 3px; }
.search-item-type { color: var(--c-muted); font-family: var(--font-mono); }
.search-empty { padding: 1.5rem; text-align: center; color: var(--c-muted); }
.search-hint {
  padding: 0.5rem 1rem;
  background: var(--c-surface);
  border-top: 1px solid var(--c-border);
  font-size: 0.78rem;
  color: var(--c-muted);
}
.search-hint kbd {
  font-family: var(--font-mono);
  font-size: 0.7rem;
  background: var(--c-bg);
  border: 1px solid var(--c-border);
  border-radius: 4px;
  padding: 0.05rem 0.35rem;
}

@media (max-width: 540px) {
  .search-btn-label, .topbar-search-btn kbd { display: none; }
  .topbar-search-btn { padding: 0.4rem 0.55rem; }
}

/* ---------- Skip-to-content link (a11y) ---------- */
.skip-link {
  position: fixed;
  top: -100px;
  left: 50%;
  transform: translateX(-50%);
  z-index: 200;
  background: var(--c-primary);
  color: #fff;
  padding: 0.6rem 1.2rem;
  border-radius: 0 0 var(--radius) var(--radius);
  font-weight: 600;
  font-size: 0.92rem;
  text-decoration: none;
  transition: top 0.18s ease;
}
.skip-link:focus {
  top: 0;
  text-decoration: none;
  outline: 3px solid var(--c-accent);
}

/* ---------- Back-to-top button ---------- */
.back-to-top {
  position: fixed;
  bottom: 24px;
  right: 24px;
  width: 44px;
  height: 44px;
  border-radius: 50%;
  background: var(--c-primary);
  color: #fff;
  border: none;
  font-size: 1.3rem;
  line-height: 1;
  cursor: pointer;
  box-shadow: var(--shadow);
  opacity: 0;
  transform: translateY(20px);
  transition: opacity 0.2s ease, transform 0.2s ease, background 0.15s;
  z-index: 55;
}
.back-to-top.is-visible {
  opacity: 1;
  transform: translateY(0);
}
.back-to-top:hover {
  background: var(--c-primary-dark);
  transform: translateY(-2px);
}
@media (max-width: 540px) {
  .back-to-top {
    bottom: 16px;
    right: 16px;
    width: 40px;
    height: 40px;
  }
}



/* ---------- Next Steps cards (at chapter end) ---------- */
.next-steps {
  margin: 3rem 0 1rem;
  padding: 1.5rem;
  background: var(--c-surface);
  border: 1px solid var(--c-border);
  border-radius: var(--radius-lg);
  border-left: 4px solid var(--c-primary);
}
.next-steps-title {
  margin: 0 0 1rem;
  font-size: 1.15rem;
  color: var(--c-primary-dark);
  border: none;
  padding: 0;
}
[data-theme="dark"] .next-steps-title { color: var(--c-primary); }
.next-steps-grid {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 0.75rem;
}
.ns-card {
  display: flex;
  flex-direction: column;
  gap: 0.25rem;
  padding: 0.85rem 1rem;
  background: var(--c-bg);
  border: 1px solid var(--c-border);
  border-radius: var(--radius);
  color: var(--c-text);
  text-decoration: none;
  transition: all 0.15s;
}
.ns-card:hover {
  border-color: var(--c-primary);
  transform: translateY(-2px);
  box-shadow: var(--shadow-sm);
  text-decoration: none;
  background: var(--c-primary-soft);
}
[data-theme="dark"] .ns-card:hover { background: var(--c-surface-2); }
.ns-title { font-weight: 700; font-size: 0.92rem; color: var(--c-text); }
.ns-sub { font-size: 0.78rem; color: var(--c-muted); }

@media (max-width: 720px) {
  .next-steps-grid { grid-template-columns: 1fr; }
}

/* ---------- Homepage FAQ accordion ---------- */
.home-faq {
  margin: 2rem 0;
}
.home-faq details {
  background: var(--c-bg);
  border: 1px solid var(--c-border);
  border-radius: var(--radius);
  padding: 0;
  margin: 0.5rem 0;
  overflow: hidden;
}
.home-faq details[open] {
  border-color: var(--c-primary);
  box-shadow: var(--shadow-sm);
}
.home-faq summary {
  cursor: pointer;
  padding: 0.85rem 1.1rem;
  font-weight: 600;
  list-style: none;
  display: flex;
  align-items: center;
  gap: 0.6rem;
}
.home-faq summary::-webkit-details-marker { display: none; }
.home-faq summary::before {
  content: '＋';
  color: var(--c-primary);
  font-weight: 700;
  font-size: 1.1rem;
  transition: transform 0.2s;
}
.home-faq details[open] summary::before { content: '－'; }
.home-faq summary:hover { background: var(--c-surface); }
.home-faq .faq-body {
  padding: 0 1.1rem 1rem;
  color: var(--c-text-soft);
  font-size: 0.92rem;
  line-height: 1.7;
}

/* ---------- Theme toggle button ---------- */
.theme-toggle {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 36px;
  height: 36px;
  background: transparent;
  border: 1px solid var(--c-border);
  border-radius: var(--radius-sm);
  cursor: pointer;
  font-size: 1.05rem;
  color: var(--c-text-soft);
  transition: background 0.15s, border-color 0.15s;
}
.theme-toggle:hover {
  background: var(--c-surface);
  border-color: var(--c-primary);
}
/* Show correct icon based on current theme */
.theme-toggle .icon-dark { display: inline; }
.theme-toggle .icon-light { display: none; }
[data-theme="dark"] .theme-toggle .icon-dark { display: none; }
[data-theme="dark"] .theme-toggle .icon-light { display: inline; }


/* ---------- Breadcrumb (above chapter-header) ---------- */
.breadcrumb {
  font-size: 0.86rem;
  color: var(--c-muted);
  margin-bottom: 1rem;
  display: flex;
  align-items: center;
  flex-wrap: wrap;
  gap: 0.1rem;
  line-height: 1.6;
}
.breadcrumb a {
  color: var(--c-muted);
  text-decoration: none;
  padding: 0.15rem 0.4rem;
  border-radius: var(--radius-sm);
  transition: background 0.15s, color 0.15s;
}
.breadcrumb a:hover {
  color: var(--c-primary);
  background: var(--c-primary-soft);
  text-decoration: none;
}
.bc-sep {
  margin: 0 0.15rem;
  opacity: 0.5;
  font-weight: 300;
}
.bc-section {
  padding: 0.15rem 0.4rem;
  color: var(--c-text-soft);
}
.bc-current {
  padding: 0.15rem 0.55rem;
  background: var(--c-primary-soft);
  color: var(--c-primary-dark);
  border-radius: var(--radius-sm);
  font-weight: 600;
}
[data-theme="dark"] .bc-current { color: var(--c-primary); }

/* ---------- Glossary auto-link in chapter body ---------- */
.glossary-term {
  border-bottom: 1px dotted var(--c-primary);
  cursor: help;
  color: inherit;
  text-decoration: none;
  position: relative;
}
.glossary-term:hover,
.glossary-term.is-active {
  background: var(--c-primary-soft);
  text-decoration: none;
}

/* Custom tooltip popover (works on touch too) */
.glossary-popover {
  position: absolute;
  z-index: 80;
  max-width: 280px;
  background: var(--c-bg);
  color: var(--c-text);
  border: 1px solid var(--c-primary);
  border-radius: var(--radius);
  padding: 0.7rem 0.9rem;
  font-size: 0.88rem;
  line-height: 1.5;
  box-shadow: var(--shadow-lg);
  opacity: 0;
  transform: translateY(4px);
  pointer-events: none;
  transition: opacity 0.15s, transform 0.15s;
}
.glossary-popover.is-visible {
  opacity: 1;
  transform: translateY(0);
  pointer-events: auto;
}
.glossary-popover .gp-term {
  font-weight: 700;
  color: var(--c-primary);
  display: block;
  margin-bottom: 0.3rem;
}
.glossary-popover .gp-close {
  position: absolute;
  top: 4px;
  right: 6px;
  background: transparent;
  border: none;
  color: var(--c-muted);
  cursor: pointer;
  font-size: 1rem;
  padding: 2px 6px;
  line-height: 1;
}

/* Touch devices: anchor icon visible by default (no hover available) */
@media (hover: none) {
  .anchor-link {
    opacity: 0.4;
  }
  .anchor-link:active,
  .anchor-link.is-copied {
    opacity: 1;
  }
  /* Tooltip hides on outside tap, no hover state */
  .glossary-term:hover {
    background: transparent;
  }
}

/* ---------- Static flow diagram (replaces Mermaid) ---------- */
.flow-diagram {
  display: flex;
  flex-wrap: nowrap;
  align-items: center;
  justify-content: center;
  gap: 0.4rem;
  margin: 1.5rem 0 0.5rem;
  padding: 1.5rem 0.85rem;
  background: var(--c-surface);
  border: 1px solid var(--c-border);
  border-radius: var(--radius);
  overflow-x: auto;
}
.flow-group {
  display: flex;
  flex-direction: column;
  gap: 0.45rem;
  align-items: center;
  justify-content: center;
  flex-shrink: 0;
  min-width: 96px;
}
.flow-group-label {
  font-size: 0.7rem;
  text-transform: uppercase;
  letter-spacing: 0.05em;
  color: var(--c-muted);
  margin-bottom: 0.15rem;
  font-weight: 700;
}
.flow-node {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 0.2rem;
  width: 100%;
  min-width: 92px;
  padding: 0.6rem 0.6rem;
  background: var(--c-bg);
  border: 1.5px solid var(--c-border);
  border-radius: var(--radius);
  font-size: 0.82rem;
  font-weight: 600;
  color: var(--c-text);
  text-align: center;
  line-height: 1.3;
  position: relative;
}
.flow-node .flow-icon {
  font-size: 1.3rem;
  line-height: 1;
}
.flow-node .flow-sub {
  font-size: 0.72rem;
  color: var(--c-muted);
  font-weight: 400;
}
.flow-node.flow-primary {
  background: var(--c-primary);
  color: #fff;
  border-color: var(--c-primary-dark);
}
.flow-node.flow-primary .flow-sub { color: rgba(255,255,255,0.85); }
.flow-node.flow-accent {
  background: var(--c-accent);
  color: #1f2937;
  border-color: #92400e;
}
.flow-node.flow-soft {
  background: var(--c-accent-soft);
  border-color: var(--c-accent);
  color: #92400e;
}
[data-theme="dark"] .flow-node.flow-soft { color: var(--c-accent); }

.flow-arrow {
  flex-shrink: 0;
  align-self: center;
  display: flex;
  align-items: center;
  justify-content: center;
  min-width: 22px;
  height: 32px;
  color: var(--c-primary);
  font-size: 1.25rem;
  font-weight: 700;
  position: relative;
}
.flow-arrow.flow-arrow-bi::after { content: '⇄'; }
.flow-arrow.flow-arrow-dashed {
  font-size: 1rem;
  letter-spacing: -2px;
}
.flow-arrow.flow-arrow-dashed::after { content: '⇠'; opacity: 0.7; font-style: italic; }
.flow-arrow .flow-arrow-label {
  position: absolute;
  bottom: -16px;
  font-size: 0.7rem;
  color: var(--c-muted);
  font-style: italic;
  white-space: nowrap;
}

@media (max-width: 720px) {
  .flow-diagram { flex-direction: column; }
  .flow-arrow { transform: rotate(90deg); align-self: center; }
}

/* ---------- Figure caption (used by flow-diagram + lightbox figures) ---------- */
.figure-wrap {
  margin: 1.75rem auto;
  max-width: 100%;
}
.figure-caption {
  margin: 0.7rem auto 0;
  font-size: 0.88rem;
  color: var(--c-muted);
  font-style: italic;
  text-align: center;
  max-width: 90%;
  line-height: 1.5;
}

/* ---------- Dark-mode visual tweaks ---------- */
[data-theme="dark"] .search-modal {
  background: var(--c-surface);
  color: var(--c-text);
}
[data-theme="dark"] .search-input { color: var(--c-text); }
[data-theme="dark"] .search-item.is-active,
[data-theme="dark"] .search-item:hover {
  background: var(--c-primary-soft);
}
[data-theme="dark"] .search-overlay { background: rgba(0, 0, 0, 0.65); }
[data-theme="dark"] .hero { box-shadow: var(--shadow-lg); }
[data-theme="dark"] pre { border: 1px solid var(--c-border); }
[data-theme="dark"] .card-icon { background: var(--c-primary-soft); }

/* ---------- Beginner Guide — ELI5 grid ---------- */
.eli5-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(220px, 1fr));
  gap: 0.85rem;
  margin: 1rem 0 1.5rem;
}
.eli5-card {
  padding: 0.85rem 1rem;
  background: var(--c-bg);
  border: 1px solid var(--c-border);
  border-radius: var(--radius);
  border-left: 4px solid var(--c-primary);
  transition: transform 0.15s, box-shadow 0.15s;
}
.eli5-card:hover { transform: translateY(-2px); box-shadow: var(--shadow-sm); }
.eli5-icon { font-size: 1.6rem; line-height: 1; margin-bottom: 0.3rem; display: block; }
.eli5-term {
  font-weight: 700;
  font-size: 0.95rem;
  color: var(--c-primary-dark);
  margin-bottom: 0.3rem;
}
[data-theme="dark"] .eli5-term { color: var(--c-primary); }
.eli5-def {
  font-size: 0.86rem;
  line-height: 1.55;
  color: var(--c-text-soft);
}

/* ---------- Mental Model diagram (vertical flow) ---------- */
.mental-model {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 0.4rem;
  margin: 1.5rem 0;
  padding: 1.5rem 1rem;
  background: var(--c-surface);
  border: 1px solid var(--c-border);
  border-radius: var(--radius-lg);
}
.mm-box {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 0.2rem;
  padding: 0.85rem 1.25rem;
  background: var(--c-bg);
  border: 2px solid var(--c-border);
  border-radius: var(--radius);
  text-align: center;
  min-width: 260px;
  max-width: 340px;
}
.mm-icon { font-size: 1.8rem; line-height: 1; }
.mm-label { font-weight: 700; font-size: 1rem; }
.mm-sub { font-size: 0.8rem; color: var(--c-muted); line-height: 1.4; }
.mm-arrow {
  font-size: 0.78rem;
  color: var(--c-primary);
  font-weight: 600;
  font-style: italic;
}
.mm-user    { border-color: var(--c-info); background: var(--c-info-bg); }
.mm-jetson  { border-color: var(--c-primary); background: var(--c-primary-soft); }
.mm-atom    { border-color: var(--c-accent); background: var(--c-accent-soft); color: #92400e; }
[data-theme="dark"] .mm-atom { color: var(--c-accent); }
.mm-servo   { border-color: var(--c-tip); background: var(--c-tip-bg); }

/* ---------- Annotated code (line-by-line explanation) ---------- */
.code-annotated {
  background: var(--c-code-bg);
  color: #e2e8f0;
  border-radius: var(--radius);
  padding: 1rem 1.1rem;
  margin: 1.25rem 0;
  overflow-x: auto;
}
.code-annotated .ann-line {
  margin-bottom: 0.85rem;
  padding-bottom: 0.85rem;
  border-bottom: 1px dashed rgba(255,255,255,0.1);
}
.code-annotated .ann-line:last-child {
  border-bottom: none;
  margin-bottom: 0;
  padding-bottom: 0;
}
.code-annotated code {
  display: block;
  font-family: var(--font-mono);
  font-size: 0.92rem;
  color: #f8fafc;
  background: transparent;
  border: none;
  padding: 0;
  white-space: pre;
}
.code-annotated .ann-note {
  margin-top: 0.45rem;
  padding-left: 1.5rem;
  font-size: 0.86rem;
  color: #94a3b8;
  line-height: 1.6;
  border-left: 2px solid var(--c-primary);
  padding: 0.2rem 0.85rem;
  font-family: var(--font-sans);
}
.code-annotated .ann-note code {
  display: inline;
  background: rgba(255,255,255,0.1);
  color: #fbbf24;
  padding: 1px 5px;
  border-radius: 3px;
  font-size: 0.88em;
}

/* ---------- Error Catalog page ---------- */
.error-search {
  position: sticky;
  top: calc(var(--topbar-h) + 1rem);
  z-index: 10;
  display: flex;
  align-items: center;
  gap: 1rem;
  padding: 0.85rem 1rem;
  background: var(--c-bg);
  border: 2px solid var(--c-primary);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  margin: 1.25rem 0 2rem;
}
.error-filter-input {
  flex: 1;
  border: none;
  outline: none;
  font: inherit;
  font-size: 1.05rem;
  background: transparent;
  color: var(--c-text);
}
.error-counter {
  font-size: 0.82rem;
  color: var(--c-muted);
  white-space: nowrap;
}
.err-block {
  background: var(--c-bg);
  border: 1px solid var(--c-border);
  border-left: 4px solid var(--c-danger);
  border-radius: var(--radius);
  padding: 1rem 1.1rem;
  margin: 0.85rem 0;
}
.err-title {
  font-weight: 700;
  margin-bottom: 0.4rem;
  line-height: 1.4;
}
.err-title code {
  background: var(--c-danger-bg);
  color: var(--c-danger);
  border-color: var(--c-danger-border);
  font-size: 0.88em;
  padding: 0.15rem 0.45rem;
}
.err-cause {
  color: var(--c-text-soft);
  font-size: 0.92rem;
  margin: 0.5rem 0 0.6rem;
}
.err-fix {
  background: var(--c-tip-bg);
  border-left: 3px solid var(--c-tip);
  padding: 0.7rem 0.9rem;
  border-radius: 0 var(--radius-sm) var(--radius-sm) 0;
  margin-top: 0.6rem;
}
.err-fix p { margin: 0.4rem 0; }
.err-fix ol, .err-fix ul { margin: 0.4rem 0 0.4rem 1.2rem; }
.err-fix pre { margin: 0.5rem 0; }

/* ---------- Thai-specific tips callout style ---------- */
/* thai-tip uses default tip styling — no special badge */

/* ---------- Mobile code block improvements ---------- */
@media (max-width: 540px) {
  pre {
    font-size: 0.8rem;
    line-height: 1.55;
    padding: 0.85rem 0.9rem;
    padding-right: 3.2rem;
  }
  pre code { white-space: pre; }
  /* ลด whitespace ของ code-label บนมือถือ */
  .code-label {
    font-size: 0.7rem;
    padding: 0.15rem 0.5rem;
  }
}

/* ---------- Print stylesheet — Cheatsheet specific ---------- */
@media print {
  /* Cheatsheet page: optimize for one-page-per-section printing */
  body[class*="cheat"], html:has(.cheat-toc) body {
    font-size: 9pt;
  }
  .cheat-toc { display: none !important; }
  .cheat-block {
    page-break-inside: avoid;
    border: 1px solid #999;
    margin: 0.5rem 0;
    padding: 0.5rem;
  }
  .cheat-block h3 {
    font-size: 11pt;
    margin: 0 0 0.3rem;
    border-bottom: 1px solid #ccc;
    padding-bottom: 0.2rem;
  }
  .cheat-grid {
    grid-template-columns: repeat(2, 1fr) !important;
    gap: 0.5rem;
  }
  .kv-table, table { font-size: 8.5pt; }
  .kv-table th, .kv-table td, th, td { padding: 0.2rem 0.4rem; }
  .callout {
    page-break-inside: avoid;
    border-left-width: 2px !important;
    margin: 0.4rem 0;
  }
  /* Hide non-essential elements on print */
  .copy-btn, .back-to-top, .breadcrumb { display: none !important; }
  /* Force single column */
  .layout { display: block !important; }
  .toc { display: none !important; }
  /* Show URLs after links เฉพาะ external */
  a[href^="http"]::after {
    content: " (" attr(href) ")";
    font-size: 7pt;
    color: #666;
  }
  a[href^="#"]::after { content: ''; }
}

/* ---------- Cheatsheet page ---------- */
.cheat-toc {
  display: flex;
  flex-wrap: wrap;
  gap: 0.5rem;
  margin: 1.25rem 0 2rem;
  padding: 0.85rem 1rem;
  background: var(--c-surface);
  border: 1px solid var(--c-border);
  border-radius: var(--radius);
}
.cheat-toc-label {
  font-size: 0.78rem;
  text-transform: uppercase;
  letter-spacing: 0.05em;
  color: var(--c-muted);
  font-weight: 700;
  align-self: center;
  margin-right: 0.5rem;
}
.cheat-toc a {
  padding: 0.3rem 0.7rem;
  background: var(--c-bg);
  border: 1px solid var(--c-border);
  border-radius: 999px;
  font-size: 0.83rem;
  color: var(--c-text-soft);
  transition: all 0.15s;
}
.cheat-toc a:hover {
  background: var(--c-primary-soft);
  border-color: var(--c-primary);
  color: var(--c-primary-dark);
  text-decoration: none;
}

.cheat-block {
  background: var(--c-bg);
  border: 1px solid var(--c-border);
  border-radius: var(--radius);
  padding: 1rem 1.1rem 1.1rem;
  margin: 1rem 0;
  min-width: 0;   /* ให้โค้ดยาวเลื่อนแนวนอนในกรอบ แทนที่จะดัน grid จนล้น */
}
.cheat-block h3 {
  margin: 0 0 0.5rem;
  font-size: 1rem;
  color: var(--c-primary-dark);
}
[data-theme="dark"] .cheat-block h3 { color: var(--c-primary); }
.cheat-block p {
  margin: 0.3rem 0 0.6rem;
  color: var(--c-muted);
  font-size: 0.88rem;
}
.cheat-block pre {
  margin: 0;
  font-size: 0.85rem;
}

.cheat-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(320px, 1fr));
  gap: 1rem;
  margin: 1rem 0;
}

.kv-table {
  width: 100%;
  border-collapse: collapse;
  font-size: 0.88rem;
  background: var(--c-bg);
  border: 1px solid var(--c-border);
  border-radius: var(--radius);
  overflow: hidden;
  margin: 1rem 0;
}
.kv-table th, .kv-table td {
  padding: 0.5rem 0.85rem;
  text-align: left;
  border-bottom: 1px solid var(--c-border);
  vertical-align: top;
}
.kv-table th {
  background: var(--c-surface);
  font-weight: 600;
  color: var(--c-text-soft);
  width: 30%;
}
.kv-table tr:last-child th, .kv-table tr:last-child td { border-bottom: none; }
.kv-table code { font-size: 0.85em; }

/* ---------- Image gallery (kit components, etc.) ---------- */
.gallery {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(160px, 1fr));
  gap: 0.75rem;
  margin: 1.25rem 0;
}
.gallery-item {
  background: var(--c-surface);
  border: 1px solid var(--c-border);
  border-radius: var(--radius);
  padding: 0.5rem;
  text-align: center;
  font-size: 0.85rem;
}
.gallery-item img {
  height: 110px;
  object-fit: contain;
  margin: 0 auto 0.4rem;
}

/* ========================================================
   Premium dynamic effects (progress · reveal · code window · glow)
   ======================================================== */

/* --- Reading progress bar --- */
.reading-progress {
  position: fixed;
  top: 0; left: 0;
  width: 0;
  height: 3px;
  background: var(--grad);
  box-shadow: 0 0 12px color-mix(in srgb, var(--c-primary) 55%, transparent);
  border-radius: 0 3px 3px 0;
  z-index: 60;
  transition: width 0.08s linear;
  pointer-events: none;
}

/* --- Scroll reveal (added by JS; invisible only when JS+motion allow it) --- */
.reveal {
  opacity: 0;
  transform: translateY(18px);
  transition: opacity 0.6s cubic-bezier(0.16, 1, 0.3, 1),
              transform 0.6s cubic-bezier(0.16, 1, 0.3, 1);
  will-change: opacity, transform;
}
.reveal.is-in { opacity: 1; transform: none; }

/* แถบหัว + จุดไฟจราจรเป็น element จริง (.code-bar / .code-dots) สร้างโดย app.js
   ไม่ใช้ pseudo-element บน <pre> อีกต่อไป */

/* --- Hero product image: smooth 3D tilt (transform set by JS) --- */
.hero-image { perspective: 900px; }
.hero-image img { transition: transform 0.18s ease, box-shadow 0.18s ease; transform-style: preserve-3d; }

/* --- Card hover: lift + brand glow + cursor-follow spotlight --- */
.card { position: relative; overflow: hidden; transition: border-color 0.18s, background 0.18s, box-shadow 0.18s, transform 0.18s; }
.card::after {
  content: '';
  position: absolute;
  inset: 0;
  pointer-events: none;
  background: radial-gradient(180px circle at var(--cx, 50%) var(--cy, -20%),
              color-mix(in srgb, var(--c-primary) 16%, transparent), transparent 60%);
  opacity: 0;
  transition: opacity 0.25s;
}
.card:hover::after { opacity: 1; }
.card:hover {
  border-color: color-mix(in srgb, var(--c-primary) 45%, var(--c-border));
  box-shadow: 0 1px 2px rgba(16, 24, 40, 0.04),
              0 12px 30px color-mix(in srgb, var(--c-primary) 14%, transparent);
  transform: translateY(-3px);
}
.ns-card { transition: border-color 0.18s, background 0.18s, box-shadow 0.18s, transform 0.18s; }
.ns-card:hover {
  box-shadow: 0 10px 26px color-mix(in srgb, var(--c-primary) 14%, transparent);
}

/* --- Primary button: glow + lift + light sweep + magnetic-ready --- */
.btn { transition: background 0.13s, border-color 0.13s, color 0.13s, box-shadow 0.13s, transform 0.2s ease; }
.btn-primary { position: relative; overflow: hidden; }
.btn-primary::before {
  content: '';
  position: absolute;
  top: 0; left: -65%;
  width: 45%; height: 100%;
  background: linear-gradient(100deg, transparent, rgba(255, 255, 255, 0.45), transparent);
  transform: skewX(-18deg);
  transition: left 0.6s ease;
  pointer-events: none;
}
.btn-primary:hover::before { left: 135%; }
.btn-primary:hover {
  box-shadow: inset 0 1px 0 rgba(255, 255, 255, 0.22),
              0 12px 28px color-mix(in srgb, var(--c-primary) 45%, transparent);
}

/* --- Topbar lifts a shadow once the page scrolls --- */
.topbar.is-scrolled { box-shadow: 0 1px 0 var(--c-border), 0 8px 24px rgba(16, 24, 40, 0.08); }
[data-theme="dark"] .topbar.is-scrolled { box-shadow: 0 1px 0 var(--c-border), 0 10px 30px rgba(0, 0, 0, 0.55); }

/* --- h2 gradient underline grows in when revealed --- */
.content h2.reveal::after { width: 0; transition: width 0.55s 0.12s cubic-bezier(0.16, 1, 0.3, 1); }
.content h2.reveal.is-in::after { width: 2.75rem; }

/* --- Rotating conic glow halo behind the hero product image --- */
.hero-image::after {
  content: '';
  position: absolute;
  inset: 8%;
  z-index: 0;
  border-radius: 46%;
  background: conic-gradient(#2dd4bf, #34d399, #a7f3d0, #18b39a, #2dd4bf);
  filter: blur(36px);
  opacity: 0.35;
  animation: spinRing 9s linear infinite;
}
[data-theme="dark"] .hero-image::after { opacity: 0.5; }
@keyframes spinRing { to { transform: rotate(360deg); } }

/* --- Subtle film grain for tactile depth (sits above content, never blocks clicks) --- */
body::after {
  content: '';
  position: fixed;
  inset: 0;
  z-index: 2;
  pointer-events: none;
  opacity: 0.02;
  background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='150' height='150'%3E%3Cfilter id='n'%3E%3CfeTurbulence type='fractalNoise' baseFrequency='0.8' numOctaves='2' stitchTiles='stitch'/%3E%3C/filter%3E%3Crect width='100%25' height='100%25' filter='url(%23n)'/%3E%3C/svg%3E");
}

/* --- Animated conic gradient border that reveals on card hover --- */
@property --ca { syntax: '<angle>'; inherits: false; initial-value: 0deg; }
.card::before {
  content: '';
  position: absolute;
  inset: 0;
  border-radius: inherit;
  padding: 1.5px;
  background: conic-gradient(from var(--ca),
    transparent 0deg, var(--c-primary) 70deg, #34d399 150deg, transparent 250deg);
  -webkit-mask: linear-gradient(#000 0 0) content-box, linear-gradient(#000 0 0);
  -webkit-mask-composite: xor;
          mask: linear-gradient(#000 0 0) content-box, linear-gradient(#000 0 0);
          mask-composite: exclude;
  opacity: 0;
  transition: opacity 0.3s;
  pointer-events: none;
  z-index: 2;
}
.card:hover::before { opacity: 1; animation: caSpin 3.5s linear infinite; }
@keyframes caSpin { to { --ca: 360deg; } }

/* --- Reduced-motion: disable all animation (toggleable via the ✨ button) --- */
html.reduce-motion .hero-plus,
html.reduce-motion .hero-bg,
html.reduce-motion .hero-image::after,
html.reduce-motion .chapter-header::before { animation: none; }
html.reduce-motion .reading-progress { transition: none; }
html.reduce-motion .reveal { opacity: 1; transform: none; transition: none; }
html.reduce-motion .content h2.reveal::after { width: 2.75rem; transition: none; }
html.reduce-motion .card:hover,
html.reduce-motion .btn-primary:hover { transform: none; }
html.reduce-motion .btn-primary::before { display: none; }
html.reduce-motion .card:hover::before { animation: none; }
html.reduce-motion .fx-particles { display: none; }

/* --- Motion toggle button: dim the ✨ icon when effects are off --- */
.motion-toggle.motion-off { opacity: 0.55; }

/* ========================================================
   Content prose polish (applies to every page)
   ======================================================== */

/* Lead paragraph — a touch larger, calm */
.content .lead { font-size: 1.1rem; color: var(--c-text-soft); }

/* Blockquote — tinted panel with a soft quote mark */
.content blockquote {
  position: relative;
  margin: 1.6rem 0;
  padding: 1rem 1.25rem 1rem 2.9rem;
  border-left: 3px solid var(--c-primary);
  border-radius: 0 var(--radius) var(--radius) 0;
  background: var(--c-primary-soft);
  color: var(--c-text-soft);
  font-style: normal;
}
.content blockquote::before {
  content: '\201C';
  position: absolute;
  left: 0.65rem; top: 0.05rem;
  font: 700 2.4rem/1 Georgia, 'Times New Roman', serif;
  color: var(--c-primary);
  opacity: 0.38;
}
.content blockquote p:first-child { margin-top: 0; }
.content blockquote p:last-child { margin-bottom: 0; }

/* Horizontal rule — gradient line with a centred diamond */
.content hr {
  position: relative;
  height: 1px;
  border: none;
  margin: 3rem 0;
  background: linear-gradient(90deg, transparent, var(--c-border-strong), transparent);
  overflow: visible;
}
.content hr::after {
  content: '';
  position: absolute;
  left: 50%; top: 50%;
  width: 7px; height: 7px;
  transform: translate(-50%, -50%) rotate(45deg);
  background: var(--grad);
  border-radius: 1.5px;
}

/* Plain prose bullet lists — gradient square markers */
.content article ul:not([class]) {
  list-style: none;
  padding-left: 1.5rem;
}
.content article ul:not([class]) > li {
  position: relative;
}
.content article ul:not([class]) > li::before {
  content: '';
  position: absolute;
  left: -1.15rem;
  top: 0.66em;
  width: 6px; height: 6px;
  border-radius: 2px;
  background: var(--grad);
}
/* Ordered lists — coloured, bolder numerals */
.content article ol:not([class]) > li::marker {
  color: var(--c-primary);
  font-weight: 700;
}

/* Footer — gradient accent line on top */
.footer { position: relative; }
.footer::before {
  content: '';
  position: absolute;
  top: 0; left: 0; right: 0;
  height: 2px;
  background: var(--grad);
  opacity: 0.65;
}
