:root {
  --purple: #6b2fa3; --purple-d: #47196f; --purple-l: #f4eefa;
  --ink: #2a2233; --muted: #7a6f86;
  --font: "Sarabun", "Noto Sans Thai", "Leelawadee UI", "Tahoma", system-ui, sans-serif;
}
* { box-sizing: border-box; }
html, body { margin: 0; }
body { font-family: var(--font); color: var(--ink); background: #faf8fc; font-size: 17px; }
.inner { max-width: 1180px; margin: 0 auto; padding: 0 1.2rem; }
html.viewer-lock, html.viewer-lock body { overflow: hidden; }

/* ---------- หัวเว็บ ---------- */
.site-head { background: linear-gradient(120deg, var(--purple-d), var(--purple) 60%, #8a4cc4); color: #fff; padding: 2.2rem 0 2.4rem; }
.site-head .kicker { font-size: 1.05rem; opacity: .85; letter-spacing: .02em; }
.site-head h1 { margin: .2rem 0 0; font-size: clamp(1.5rem, 3.6vw, 2.4rem); font-weight: 700; line-height: 1.25; }

/* ---------- รายการเล่ม ---------- */
main.inner { padding-top: 2rem; padding-bottom: 3rem; }
.grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(200px, 1fr)); gap: 2rem 1.6rem; }
.book-card { text-decoration: none; color: inherit; display: block; }
.book-card .cover { position: relative; border-radius: 4px 8px 8px 4px; overflow: hidden; background: #e9e3ef;
  box-shadow: 0 2px 4px rgba(40,10,70,.12), 0 10px 24px rgba(40,10,70,.14); transition: transform .25s, box-shadow .25s; }
.book-card .cover::before { content: ""; position: absolute; top: 0; bottom: 0; left: 0; width: 8px; z-index: 1;
  background: linear-gradient(90deg, rgba(0,0,0,.22), rgba(255,255,255,.15) 60%, transparent); }
.book-card .cover img { display: block; width: 100%; height: 100%; object-fit: cover; }
.book-card:hover .cover, .book-card:focus-visible .cover { transform: translateY(-6px); box-shadow: 0 4px 8px rgba(40,10,70,.15), 0 18px 34px rgba(40,10,70,.22); }
.book-card:focus-visible { outline: none; }
.latest { position: absolute; top: .6rem; right: .6rem; z-index: 2; background: #ffcf3f; color: #3b2600; font-weight: 700;
  font-size: .8rem; padding: .2rem .6rem; border-radius: 99px; box-shadow: 0 1px 3px rgba(0,0,0,.2); }
.book-card .meta { padding: .8rem .1rem 0; }
.book-card .title { font-weight: 700; font-size: 1.12rem; }
.book-card .sub { color: var(--muted); font-size: .95rem; margin-top: .1rem; }
.book-card .pages { color: var(--muted); font-size: .85rem; margin-top: .2rem; }
.empty { text-align: center; color: var(--muted); padding: 4rem 0; }
.site-foot { padding: 1.5rem 1.2rem 2.5rem; color: var(--muted); font-size: .9rem; border-top: 1px solid #ece6f2; }
.site-foot a { color: var(--purple); text-decoration: none; }
@media (max-width: 520px) {
  .grid { grid-template-columns: repeat(2, 1fr); gap: 1.4rem 1rem; }
  .book-card .title { font-size: 1rem; }
}

/* ---------- ตัวอ่าน ---------- */
.viewer { position: fixed; top: 0; right: 0; bottom: 0; left: 0; width: 100%; height: 100%; z-index: 1000; display: none; flex-direction: column;
  background: radial-gradient(ellipse at center, #4a3a5c 0%, #241b2e 75%); color: #fff; user-select: none; -webkit-user-select: none; }
.viewer.open { display: flex; }
.v-top { display: flex; align-items: center; gap: .5rem; padding: .4rem .5rem .4rem 1rem; padding-top: .4rem; padding-top: max(.4rem, env(safe-area-inset-top)); flex: none; }
.v-title { flex: 1; font-weight: 600; font-size: 1rem; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; opacity: .92; }

.v-stage { flex: 1 1 auto; min-height: 0; display: flex; overflow: hidden; position: relative; touch-action: none; -webkit-user-select: none; }
.viewer.zoomed .v-stage { overflow: auto; cursor: grab; touch-action: none; scrollbar-width: thin; }
.viewer.gesturing .v-book { transition: none !important; }
.v-stage.grabbing { cursor: grabbing !important; }
.v-book { margin: auto; position: relative; perspective: 2600px; transition: transform .65s ease, opacity .25s; flex: none; }
.v-book.fade { animation: vfade .35s ease; }
@keyframes vfade { from { opacity: .2; } to { opacity: 1; } }

.leaf, .face, .slide-ghost { position: absolute; top: 0; height: 100%; width: var(--pw); background: #fff; overflow: hidden; }
.leaf img, .face img, .slide-ghost img { display: block; width: 100%; height: 100%; object-fit: contain; -webkit-user-drag: none; pointer-events: none; }
.leaf.left { left: 0; }
.leaf.right { left: var(--pw); }
.leaf.blank { visibility: hidden; }
.leaf.loading { background: #fff linear-gradient(90deg, #f3f0f6 25%, #fff 50%, #f3f0f6 75%) 0 0 / 200% 100%; animation: shimmer 1.2s linear infinite; }
@keyframes shimmer { to { background-position: -200% 0; } }
.mode-double .leaf { box-shadow: 0 8px 30px rgba(0,0,0,.45); cursor: pointer; }
/* เงาสันหนังสือ */
.mode-double .leaf.left::after, .mode-double .leaf.right::after { content: ""; position: absolute; top: 0; bottom: 0; width: 7%; pointer-events: none; }
.mode-double .leaf.left::after { right: 0; background: linear-gradient(90deg, transparent, rgba(0,0,0,.16)); }
.mode-double .leaf.right::after { left: 0; background: linear-gradient(270deg, transparent, rgba(0,0,0,.2)); }

.mode-single .leaf.right { display: none; }
.mode-single .leaf.left { width: 100%; box-shadow: 0 8px 30px rgba(0,0,0,.45); }
.mode-single .v-book { overflow: hidden; }
.slide-ghost { left: 0; width: 100%; z-index: 2; }
.sliding { transition: transform .3s ease; }

/* แผ่นที่กำลังพลิก */
.flipper { position: absolute; top: 0; height: 100%; width: var(--pw); transform-style: preserve-3d; z-index: 5;
  transition: transform .65s cubic-bezier(.45,.05,.35,1); }
.flipper.to-left { left: var(--pw); transform-origin: left center; }
.flipper.to-left.go { transform: rotateY(-180deg); }
.flipper.to-right { left: 0; transform-origin: right center; }
.flipper.to-right.go { transform: rotateY(180deg); }
.face { left: 0; backface-visibility: hidden; -webkit-backface-visibility: hidden; box-shadow: 0 6px 24px rgba(0,0,0,.35); }
.face.back { transform: rotateY(180deg); }
.face.blank { background: transparent; box-shadow: none; }
.shade { position: absolute; top: 0; right: 0; bottom: 0; left: 0; pointer-events: none; opacity: 0; transition: opacity .65s;
  background: linear-gradient(90deg, rgba(0,0,0,.25), rgba(0,0,0,0) 40%); }
.flipper.go .shade { opacity: 1; }

/* ปุ่ม */
.v-btn { display: inline-flex; align-items: center; justify-content: center; width: 42px; height: 42px; border: 0; border-radius: 10px;
  background: transparent; color: #fff; cursor: pointer; text-decoration: none; flex: none; }
.v-btn:hover:not(:disabled), .v-btn.on { background: rgba(255,255,255,.14); }
.v-btn:disabled { opacity: .3; cursor: default; }
.v-btn svg, .v-side svg { width: 24px; height: 24px; fill: none; stroke: currentColor; stroke-width: 2; stroke-linecap: round; stroke-linejoin: round; }
.v-side { position: absolute; top: 50%; transform: translateY(-50%); width: 52px; height: 88px; border: 0; border-radius: 12px; z-index: 6;
  background: rgba(255,255,255,.08); color: #fff; cursor: pointer; }
.v-side:hover:not(:disabled) { background: rgba(255,255,255,.2); }
.v-side:disabled { opacity: 0; pointer-events: none; }
.v-side svg { width: 34px; height: 34px; }
.v-prev { left: 6px; } .v-next { right: 6px; }
@media (max-width: 699px) { .v-side { display: none; } }

.v-bar { display: flex; align-items: center; justify-content: center; gap: .15rem; flex-wrap: wrap;
  padding: .35rem .5rem; padding-bottom: .35rem; padding-bottom: max(.35rem, env(safe-area-inset-bottom)); background: rgba(0,0,0,.25); flex: none; }
.v-page { display: inline-flex; align-items: center; gap: .35rem; margin: 0 .2rem; font-size: .95rem; }
.v-page input { width: 4.6em; text-align: center; font: inherit; color: #fff; background: rgba(255,255,255,.12);
  border: 1px solid rgba(255,255,255,.25); border-radius: 8px; padding: .3rem .2rem; }
.v-page input:focus { outline: 2px solid #c89cf0; background: rgba(255,255,255,.2); }
.v-total { opacity: .8; white-space: nowrap; }
.v-sep { width: 1px; height: 26px; background: rgba(255,255,255,.2); margin: 0 .3rem; }

.v-thumbs { flex: none; max-height: 0; overflow: hidden; transition: max-height .25s ease; background: rgba(0,0,0,.3); }
.thumbs-open .v-thumbs { flex: none; max-height: 150px; }
.v-thumbs-inner { display: flex; gap: .5rem; overflow-x: auto; padding: .6rem; scrollbar-width: thin; }
.v-thumb { flex: none; border: 2px solid transparent; border-radius: 4px; padding: 0; background: #fff; cursor: pointer; position: relative; height: 112px; }
.v-thumb img { height: 100%; display: block; border-radius: 2px; }
.v-thumb span { position: absolute; bottom: 2px; right: 2px; background: rgba(0,0,0,.6); color: #fff; font-size: .7rem; padding: 0 .3rem; border-radius: 3px; }
.v-thumb.on { border-color: #ffcf3f; }

.v-toast { position: absolute; left: 50%; bottom: 80px; transform: translateX(-50%); background: rgba(0,0,0,.8); color: #fff;
  padding: .5rem 1rem; border-radius: 8px; opacity: 0; transition: opacity .2s; pointer-events: none; z-index: 10; }
.v-toast.show { opacity: 1; }

@media (prefers-reduced-motion: reduce) {
  .v-book, .flipper, .sliding { transition: none !important; }
}
@media (max-width: 480px) {
  .v-btn { width: 36px; height: 40px; }
  .v-sep { display: none; }
  .v-page input { width: 3.8em; }
  .v-bar { gap: 0; }
}

/* ===================== หน้าชั้นวางหนังสือ ===================== */
body.layout-shelf { background: #f3ece1 linear-gradient(#fbf6ee, #efe5d6 600px, #efe5d6); color: #3b2a1a; min-height: 100vh; }
.sh-inner { max-width: 1240px; margin: 0 auto; padding: 0 48px; }
.sh-top { background: linear-gradient(110deg, #47196f, #6b2fa3 60%, #8a4cc4); color: #fff; box-shadow: 0 10px 30px -12px rgba(60,20,100,.55); position: relative; z-index: 2; }
.sh-head { display: flex; align-items: center; gap: 16px; padding-top: 20px; padding-bottom: 8px; }
.sh-head .seal { width: 58px; height: 58px; border-radius: 50%; background: #fff; flex: none; display: flex; align-items: center; justify-content: center; box-shadow: 0 2px 8px rgba(0,0,0,.25); }
.sh-head .seal img { width: 74%; height: 74%; object-fit: contain; }
.sh-head h1 { margin: 0; font-size: 26px; line-height: 1.25; }
.sh-head p { margin: 0; font-size: 15px; opacity: .85; }
.sh-school { margin-left: auto; color: #fff; text-decoration: none; font-size: 14px; padding: 7px 14px; border-radius: 99px; background: rgba(255,255,255,.16); white-space: nowrap; }
.sh-school:hover { background: rgba(255,255,255,.28); }

.sh-hero { display: flex; align-items: flex-end; justify-content: center; gap: 80px; padding-top: 10px; padding-bottom: 34px; }
.sh-hero .txt { padding-bottom: 60px; max-width: 460px; }
.sh-hero .kick { color: #ffd76a; font-weight: 700; font-size: 15px; letter-spacing: .04em; line-height: 1.4; }
.sh-hero h2 { font-size: clamp(28px, 4vw, 42px); margin: .35em 0 .1em; line-height: 1.35; }
.sh-hero .sub { font-size: 18px; opacity: .9; }
.sh-hero .pages { font-size: 15px; opacity: .75; margin-top: .2em; }
.sh-btn { display: inline-block; margin-top: 20px; padding: 12px 26px; border-radius: 12px; background: #fff; color: #5a2590; font-weight: 700; font-size: 17px; text-decoration: none; box-shadow: 0 4px 14px rgba(40,10,70,.3); transition: transform .2s; }
.sh-btn:hover { transform: translateY(-2px); }
.stand { display: block; position: relative; padding: 0 30px; text-decoration: none; }
.standbase { display: block; height: 20px; border-radius: 4px; margin: -4px -20px 0; position: relative; z-index: 1;
  background: linear-gradient(#ecd0a4, #d9ad72 30%, #c99556 32%, #b77f42); box-shadow: inset 0 2px 0 rgba(255,255,255,.4), 0 14px 24px -10px rgba(0,0,0,.6); }

/* หนังสือ 3D */
.sbook { position: relative; display: block; width: var(--bw); aspect-ratio: var(--ar); flex: none; text-decoration: none; transform-origin: bottom center; transition: transform .3s ease; }
.sbook.big { --bw: 250px; }
.sb-body { position: absolute; top: 0; right: 0; bottom: 0; left: 0; display: block; transform: perspective(900px) rotateY(var(--ry, -10deg)); transform-origin: left center; transition: transform .3s ease; background: #ddd; border-radius: 2px 4px 4px 2px; }
.sb-body img { display: block; width: 100%; height: 100%; object-fit: cover; border-radius: 2px 4px 4px 2px; }
.sb-body::before { content: ""; position: absolute; top: 1.5%; bottom: 1.5%; right: -7px; width: 7px; background: repeating-linear-gradient(0deg, #fff 0 1px, #e8e2d8 1px 2px); transform: skewY(-40deg); transform-origin: left; border-radius: 0 2px 2px 0; }
.sb-body::after { content: ""; position: absolute; top: 0; right: 0; bottom: 0; left: 0; border-radius: 2px 4px 4px 2px; pointer-events: none;
  background: linear-gradient(90deg, rgba(0,0,0,.35) 0, rgba(255,255,255,.25) 2.5%, rgba(0,0,0,.06) 5%, transparent 9%, transparent 85%, rgba(255,255,255,.1)); }
.sb-shadow { position: absolute; left: 4%; right: -10%; bottom: -8px; height: 16px; background: radial-gradient(ellipse at center, rgba(0,0,0,.45), transparent 70%); filter: blur(3px); transition: .3s; }
.sb-tip { position: absolute; left: 0; right: 0; bottom: 0; z-index: 4; display: block; opacity: 0; pointer-events: none; transition: opacity .2s;
  padding: 2.2em .7em .7em; border-radius: 0 0 4px 2px; text-align: center; color: #fff; font-size: 14px; line-height: 1.35;
  background: linear-gradient(transparent, rgba(30,10,50,.88) 45%); }
.sb-tip b { display: block; font-size: 15px; }
.sb-tip span { display: block; font-size: 12.5px; opacity: .85; }
.sb-tip span::after { content: " · คลิกเพื่ออ่าน"; }
@media (hover: hover) {
  .sbook:hover, .sbook:focus-visible { transform: translateY(-22px) scale(1.06); }
  .sbook:hover .sb-body, .sbook:focus-visible .sb-body { --ry: -2deg; }
  .sbook:hover .sb-shadow, .sbook:focus-visible .sb-shadow { bottom: -30px; opacity: .5; transform: scale(.9); }
  .sbook:hover .sb-tip, .sbook:focus-visible .sb-tip { opacity: 1; }
  .sbook:hover, .sbook:focus-visible { z-index: 6; }
  .stand:hover .sbook { transform: translateY(-8px); }
}
.sbook:focus-visible { outline: none; }
.sbook:focus-visible .sb-body { outline: 3px solid #ffcf3f; outline-offset: 3px; }

/* ชั้นวาง */
.sh-wall { padding: 16px 0 40px; }
.shelf { margin-top: 36px; }
.shelf + .shelf { margin-top: 56px; }
.shelf-label { display: flex; align-items: center; gap: 12px; margin: 0 0 16px; font-size: 18px; font-weight: 700; color: #5a2590; }
.shelf-label::after { content: ""; flex: 1; height: 1px; background: currentColor; opacity: .25; }
.shelf-row, .shelf-tags { display: flex; justify-content: flex-start; gap: var(--gap); padding: 0 var(--pad); }
.shelf-row { align-items: flex-end; position: relative; z-index: 2; }
.plank { height: 26px; margin-top: -4px; position: relative; border-radius: 3px;
  background: linear-gradient(#ecd0a4, #d9ad72 30%, #c99556 32%, #b77f42 100%); box-shadow: inset 0 2px 0 rgba(255,255,255,.45); }
.plank::before { content: ""; position: absolute; top: 0; right: 0; bottom: 0; left: 0; border-radius: 3px; mix-blend-mode: multiply;
  background: repeating-linear-gradient(90deg, transparent 0 60px, rgba(120,70,20,.12) 60px 62px, transparent 62px 140px, rgba(120,70,20,.08) 140px 141px); }
.plank::after { content: ""; position: absolute; left: 1%; right: 1%; top: 100%; height: 26px; background: linear-gradient(rgba(0,0,0,.32), transparent); filter: blur(6px); }
.shelf-tags { margin-top: -20px; position: relative; z-index: 3; }
.shelf-tags .tag { width: var(--bw); flex: none; text-align: center; font-size: 12.5px; font-weight: 700; white-space: nowrap; }
.shelf-tags .tag span { display: inline-block; padding: 2px 9px; border-radius: 4px; background: linear-gradient(#f6e3b4, #d9b773); color: #5a3a12; box-shadow: 0 1px 2px rgba(0,0,0,.3); }
.sh-wall .empty { padding: 3rem 0; }
.sh-foot { text-align: center; padding: 10px 0 40px; font-size: 14px; }
.sh-foot a { color: #7a5a3a; text-decoration: none; }

@media (max-width: 760px) {
  .sh-inner { padding: 0 16px; }
  .sh-head { padding-top: 14px; gap: 12px; }
  .sh-head .seal { width: 48px; height: 48px; }
  .sh-head h1 { font-size: 20px; }
  .sh-head p { font-size: 13px; }
  .sh-school { display: none; }
  .sh-hero { flex-direction: column-reverse; align-items: center; gap: 14px; text-align: center; padding-bottom: 26px; }
  .sh-hero .txt { padding-bottom: 0; }
  .sh-hero .sub { font-size: 16px; }
  .sh-btn { margin-top: 12px; font-size: 16px; padding: 10px 22px; }
  .sbook.big { --bw: 190px; }
  .shelf { margin-top: 28px; }
  .shelf-label { font-size: 16px; }
  .shelf-tags .tag { font-size: 11.5px; }
}

/* เบราว์เซอร์เก่าที่ไม่รองรับ aspect-ratio */
@supports not (aspect-ratio: 1 / 1) {
  .sbook { height: calc(var(--bw) * 1.414); }
  .book-card .cover { height: 0; padding-bottom: 141.4%; }
}
