:root {
  --bg: #f3ede3;
  --bg-2: #e9e0d0;
  --surface: #fffdf8;
  --ink: #2b3639;
  --ink-soft: #6a7477;
  --line: rgba(43, 54, 57, 0.12);
  --accent: #e0703a;
  --accent-ink: #fff;
  --shadow: 0 18px 40px -18px rgba(60, 45, 20, 0.45);
  --radius: 14px;
  --font: "Sarabun", "Noto Sans Thai", "Leelawadee UI", "Thonburi", "Tahoma", system-ui, sans-serif;
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #1d2224;
    --bg-2: #161a1b;
    --surface: #262c2e;
    --ink: #eef0ee;
    --ink-soft: #a5adaf;
    --line: rgba(255, 255, 255, 0.1);
    --shadow: 0 18px 40px -18px rgba(0, 0, 0, 0.8);
  }
}
:root[data-theme="dark"] {
  --bg: #1d2224;
  --bg-2: #161a1b;
  --surface: #262c2e;
  --ink: #eef0ee;
  --ink-soft: #a5adaf;
  --line: rgba(255, 255, 255, 0.1);
  --shadow: 0 18px 40px -18px rgba(0, 0, 0, 0.8);
}

* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; }
body {
  font-family: var(--font);
  color: var(--ink);
  background: radial-gradient(ellipse at 50% 35%, var(--bg) 0%, var(--bg-2) 100%);
  background-color: var(--bg);
  -webkit-font-smoothing: antialiased;
}
[hidden] { display: none !important; }
svg { width: 22px; height: 22px; fill: none; stroke: currentColor; stroke-width: 2; stroke-linecap: round; stroke-linejoin: round; }

.btn {
  display: inline-flex; align-items: center; gap: 6px;
  min-width: 40px; height: 40px; padding: 0 10px;
  border: 0; border-radius: 10px;
  font: inherit; font-size: 15px; color: inherit; text-decoration: none;
  background: transparent; cursor: pointer;
  justify-content: center;
}
.btn-ghost:hover { background: var(--line); }
.btn:focus-visible, .nav:focus-visible, .thumbs-grid button:focus-visible, .shelf-book:focus-visible {
  outline: 3px solid var(--accent); outline-offset: 2px;
}
.btn:disabled { opacity: 0.35; cursor: default; background: transparent; }

/* ---------- Viewer ---------- */
.viewer {
  display: grid;
  grid-template-rows: auto 1fr auto;
  grid-template-columns: 1fr auto;
  height: 100vh; height: 100dvh;
  overflow: hidden;
}
.topbar, .toolbar { grid-column: 1; display: flex; align-items: center; gap: 4px; padding: 8px 12px; }
.topbar { display: grid; grid-template-columns: 1fr minmax(0, auto) 1fr; }
.topbar > :first-child { justify-self: start; }
.is-embed #backLink { visibility: hidden; }
.book-title { min-width: 0; margin: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }

.toolbar {
  justify-content: center;
  padding-bottom: max(10px, env(safe-area-inset-bottom));
}
.toolbar .divider { width: 1px; height: 22px; background: var(--line); margin: 0 6px; }
.page-info { min-width: 84px; text-align: center; font-variant-numeric: tabular-nums; font-size: 15px; color: var(--ink-soft); }

.stage {
  grid-column: 1; grid-row: 2;
  position: relative;
  min-height: 0; min-width: 0;
  padding: 8px 64px;
  display: grid; place-items: center;
}
.book {
  opacity: 0; transition: opacity .4s ease;
}
.is-ready .book { opacity: 1; }
.book .page { background: #fff; overflow: hidden; }
.book .page img { display: block; width: 100%; height: 100%; object-fit: cover; user-select: none; -webkit-user-drag: none; }
/* iOS: ปิดเมนูกดค้าง "บันทึกรูปภาพ" */
.book .page img, .zoom-inner img, .thumbs-grid img { -webkit-touch-callout: none; }
.stf__block { filter: drop-shadow(0 16px 24px rgba(40, 30, 10, 0.25)); }

.nav {
  position: absolute; top: 50%; transform: translateY(-50%);
  width: 48px; height: 48px; border-radius: 50%;
  display: grid; place-items: center;
  border: 0; cursor: pointer;
  color: var(--ink); background: var(--surface);
  box-shadow: var(--shadow);
  transition: transform .15s ease, opacity .2s;
  z-index: 5;
}
.nav:hover:not(:disabled) { transform: translateY(-50%) scale(1.08); }
.nav:disabled { opacity: 0; pointer-events: none; }
.nav-prev { left: 8px; }
.nav-next { right: 8px; }

.loader { position: absolute; inset: 0; display: grid; place-items: center; pointer-events: none; }
.loader span {
  width: 36px; height: 36px; border-radius: 50%;
  border: 3px solid var(--line); border-top-color: var(--accent);
  animation: spin .8s linear infinite;
}
.is-ready .loader { display: none; }
@keyframes spin { to { transform: rotate(360deg); } }

/* ภาพรวมทุกหน้า */
.thumbs {
  grid-column: 2; grid-row: 1 / 4;
  width: 260px; display: flex; flex-direction: column;
  background: var(--surface); border-left: 1px solid var(--line);
  min-height: 0;
}
.thumbs-head { display: flex; align-items: center; justify-content: space-between; padding: 10px 8px 10px 16px; border-bottom: 1px solid var(--line); }
.thumbs-grid {
  flex: 1; overflow-y: auto; padding: 12px;
  display: grid; grid-template-columns: 1fr 1fr; gap: 10px; align-content: start;
}
.thumbs-grid button {
  border: 0; padding: 0; background: none; cursor: pointer; color: var(--ink-soft);
  font: inherit; font-size: 13px; text-align: center;
}
.thumbs-grid img { display: block; width: 100%; aspect-ratio: var(--page-ratio, 1); object-fit: cover; border-radius: 6px; border: 2px solid transparent; background: var(--line); }
.thumbs-grid button:hover img { border-color: var(--line); }
.thumbs-grid .is-current img { border-color: var(--accent); }
.thumbs-grid .is-current { color: var(--accent); font-weight: 600; }

/* ขยายหน้า */
.zoom {
  position: fixed; inset: 0; z-index: 50;
  overflow: auto; background: rgba(20, 22, 23, 0.94);
  cursor: grab; overscroll-behavior: contain;
}
.zoom.is-dragging { cursor: grabbing; }
.zoom-inner { display: flex; width: max-content; margin: auto; padding: 24px; min-height: 100%; align-items: center; }
.zoom-inner img { display: block; width: min(1600px, 150vw); height: auto; user-select: none; }
.zoom-close {
  position: fixed; top: max(12px, env(safe-area-inset-top)); right: 12px; z-index: 2;
  background: rgba(255, 255, 255, 0.92); color: #222; border-radius: 50%; width: 44px; height: 44px;
}

.show-sm { display: none; }

@media (max-width: 720px) {
  .hide-sm, [data-wide] { display: none; }
  .show-sm { display: inline-flex; }
  .stage { padding: 4px 8px; }
  .topbar, .toolbar { padding-left: 6px; padding-right: 6px; gap: 2px; }
  .page-info { min-width: 64px; }
  .nav { display: none; }
  .thumbs {
    position: fixed; inset: auto 0 0 0; z-index: 40;
    width: auto; height: 60vh; border-left: 0; border-top: 1px solid var(--line);
    border-radius: 16px 16px 0 0; box-shadow: 0 -10px 30px rgba(0,0,0,.2);
  }
  .thumbs-grid { grid-template-columns: repeat(4, 1fr); }
}

/* มือถือแนวนอน / จอเตี้ย: ย่อแถบบน-ล่างให้หนังสือใหญ่ขึ้น */
@media (max-height: 520px) {
  .topbar { padding-top: max(2px, env(safe-area-inset-top)); padding-bottom: 0; }
  .toolbar { padding-top: 0; padding-bottom: max(2px, env(safe-area-inset-bottom)); }
  .btn { height: 36px; min-width: 36px; }
  .book-title { font-size: 15px; }
  .topbar-sponsor { height: 26px; padding: 3px 6px; }
  .stage { padding-top: 2px; padding-bottom: 2px; }
  .nav { width: 40px; height: 40px; }
  .thumbs { height: 85vh; }
}
@media (max-height: 520px) and (min-width: 721px) {
  .stage { padding-left: 56px; padding-right: 56px; }
}

/* ---------- โลโก้ผู้สนับสนุน (พื้นขาวเสมอ เพราะโลโก้มีตัวอักษรสีเข้ม) ---------- */
.site-footer { display: flex; justify-content: center; padding: 0 16px 40px; }
.site-footer img {
  display: block; width: min(100%, 300px); height: auto; box-sizing: content-box;
  padding: 14px 22px; border-radius: var(--radius); background: #fff; box-shadow: 0 0 0 1px var(--line);
}
.topbar-sponsor {
  justify-self: end; display: block; height: 30px; width: auto;
  padding: 4px 8px; border-radius: 8px; background: #fff;
}
@media (max-width: 700px) { .topbar-sponsor { display: none; } }

/* ---------- ชั้นหนังสือ ---------- */
.shelf-page { min-height: 100vh; }
.shelf-wrap { max-width: 1120px; margin: 0 auto; padding: 48px 16px 64px; }
.shelf-head { text-align: center; margin-bottom: 40px; }
.shelf-head h1 { margin: 0 0 8px; font-size: clamp(26px, 4vw, 38px); line-height: 1.3; }
.shelf-head p { margin: 0; color: var(--ink-soft); font-size: 17px; }
.install-hint {
  max-width: 560px; margin: 14px auto 0; padding: 0; list-style: none;
  color: var(--ink-soft); font-size: 14px; line-height: 1.6;
}
.install-hint li + li { margin-top: 4px; }
.install-hint b { color: var(--ink); font-weight: 600; }
.install-hint[hidden] { display: none; }
.shelf-head .offline-status {
  display: inline-block; margin-top: 14px; padding: 6px 14px; border-radius: 999px;
  font-size: 14px; background: var(--surface); box-shadow: 0 0 0 1px var(--line) inset;
}
.shelf-head .offline-status[hidden] { display: none; }
.shelf-head .offline-status.is-ready { color: #2e7d4f; }
@media (prefers-color-scheme: dark) { .shelf-head .offline-status.is-ready { color: #7fd39c; } }
.shelf-series + .shelf-series { margin-top: 64px; }
.series-title { margin: 0 0 4px; font-size: clamp(22px, 3vw, 28px); line-height: 1.3; }
.series-desc { margin: 0 0 24px; color: var(--ink-soft); font-size: 16px; line-height: 1.5; }
.plans-banner {
  display: flex; align-items: center; gap: 14px; margin-top: 32px; padding: 16px 20px;
  border-radius: var(--radius); background: var(--surface); color: inherit; text-decoration: none;
  box-shadow: 0 0 0 1px var(--line) inset; transition: box-shadow .15s, transform .15s;
}
.plans-banner:hover { box-shadow: var(--shadow), 0 0 0 2px var(--accent) inset; transform: translateY(-2px); }
.plans-banner:focus-visible { outline: 3px solid var(--accent); outline-offset: 2px; }
.plans-banner > svg:first-child { flex: none; width: 44px; height: 44px; padding: 10px; border-radius: 12px; background: var(--accent); color: #fff; }
.plans-banner > svg:last-child { flex: none; color: var(--accent); }
.plans-banner span { flex: 1; min-width: 0; }
.plans-banner b { display: block; font-size: 18px; line-height: 1.4; }
.plans-banner small { display: block; color: var(--ink-soft); font-size: 15px; line-height: 1.5; }
.shelf {
  list-style: none; margin: 0; padding: 0;
  display: grid; gap: 36px 28px;
  grid-template-columns: repeat(auto-fill, minmax(190px, 1fr));
}
.shelf-book { display: block; color: inherit; text-decoration: none; border-radius: var(--radius); }
.shelf-cover {
  position: relative; border-radius: 4px 10px 10px 4px; overflow: hidden;
  box-shadow: var(--shadow);
  transition: transform .25s ease, box-shadow .25s ease;
}
.shelf-cover::before {
  content: ""; position: absolute; inset: 0 auto 0 0; width: 10px;
  background: linear-gradient(90deg, rgba(0,0,0,.22), rgba(255,255,255,.18) 60%, transparent);
}
.shelf-cover img { display: block; width: 100%; aspect-ratio: 1; object-fit: cover; } /* สัดส่วนจริงกำหนดต่อเล่มใน index.html */
.shelf-book:hover .shelf-cover { transform: translateY(-6px) rotate(-1deg); }
.shelf-meta { padding: 14px 4px 0; }
.shelf-title { margin: 0; font-size: 18px; font-weight: 600; line-height: 1.35; }

/* ---------- ติดตั้งแอป (PWA) ---------- */
.install-btn {
  display: inline-flex; align-items: center; gap: 8px;
  margin-top: 18px; padding: 10px 18px 10px 14px; border: 0; border-radius: 999px;
  font: inherit; font-size: 15px; font-weight: 600; cursor: pointer;
  color: var(--accent-ink); background: var(--accent);
  box-shadow: 0 8px 20px -10px rgba(224, 112, 58, 0.9);
  transition: filter .15s, transform .15s;
}
.install-btn:hover { filter: brightness(1.07); }
.install-btn:active { transform: scale(.97); }
.install-btn:focus-visible { outline: 3px solid var(--accent); outline-offset: 3px; }

.pwa-help { position: fixed; inset: 0; z-index: 100; display: grid; place-items: center; padding: 16px; }
.pwa-help-backdrop { position: absolute; inset: 0; background: rgba(25, 22, 18, 0.55); }
.pwa-help-panel {
  position: relative; width: 100%; max-width: 380px; padding: 24px 22px 20px;
  background: var(--surface); color: var(--ink); border-radius: 18px; text-align: center;
  box-shadow: 0 30px 80px -20px rgba(0, 0, 0, 0.5);
}
.pwa-help-panel img { display: block; margin: 0 auto 8px; border-radius: 14px; }
.pwa-help-panel h2 { margin: 0 0 12px; font-size: 20px; }
.pwa-help-panel ol { margin: 0; padding-left: 22px; text-align: left; line-height: 1.6; font-size: 15px; }
.pwa-help-panel li + li { margin-top: 6px; }
.pwa-help-panel li svg { width: 20px; height: 20px; vertical-align: -4px; color: #0a84ff; }
.pwa-help-ok { width: 100%; height: 46px; margin-top: 18px; background: var(--accent); color: var(--accent-ink); font-weight: 600; }
@media (max-width: 560px) {
  .pwa-help { place-items: end stretch; padding: 0; }
  .pwa-help-panel { max-width: none; border-radius: 18px 18px 0 0; padding-bottom: max(20px, env(safe-area-inset-bottom)); }
}

@media (max-width: 560px) {
  .shelf-wrap { padding: 28px 16px 40px; }
  .shelf-head { margin-bottom: 24px; }
  .shelf-head p { font-size: 15px; }
  .shelf { grid-template-columns: 1fr 1fr; gap: 24px 16px; }
  .shelf-title { font-size: 16px; }
  .shelf-series + .shelf-series { margin-top: 44px; }
  .series-desc { font-size: 14px; margin-bottom: 18px; }
}
@media (hover: none) {
  .shelf-book:hover .shelf-cover { transform: none; }
  .shelf-book:active .shelf-cover { transform: scale(.97); }
}

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { animation-duration: 0.01ms !important; transition-duration: 0.01ms !important; }
}
