@font-face { font-family: "IBM Plex Sans Thai"; src: url("fonts/IBMPlexSansThai-Regular.ttf") format("truetype"); font-weight: 400; font-style: normal; font-display: swap; }
@font-face { font-family: "IBM Plex Sans Thai"; src: url("fonts/IBMPlexSansThai-Medium.ttf") format("truetype"); font-weight: 500; font-style: normal; font-display: swap; }
@font-face { font-family: "IBM Plex Sans Thai"; src: url("fonts/IBMPlexSansThai-SemiBold.ttf") format("truetype"); font-weight: 600; font-style: normal; font-display: swap; }
@font-face { font-family: "IBM Plex Sans Thai"; src: url("fonts/IBMPlexSansThai-Bold.ttf") format("truetype"); font-weight: 700; font-style: normal; font-display: swap; }
:root {
  color-scheme: dark;
  font-family: "IBM Plex Sans Thai", "Noto Sans Thai", "Leelawadee UI", Tahoma, sans-serif;
  background: #071118;
  color: #f5fbff;
}

* { box-sizing: border-box; }
html, body { width: 100%; min-height: 100%; height: 100%; margin: 0; overflow: hidden; }
button, input { font: inherit; }

body {
  background:
    radial-gradient(circle at 16% 8%, rgba(13, 151, 168, .2), transparent 32rem),
    linear-gradient(150deg, #0a1d28 0%, #071118 58%, #03090d 100%);
}

.viewer-shell {
  display: grid;
  grid-template-rows: 70px minmax(0, 1fr) 64px 28px;
  width: 100%;
  height: 100vh;
  height: 100dvh;
  padding: env(safe-area-inset-top) env(safe-area-inset-right) env(safe-area-inset-bottom) env(safe-area-inset-left);
}

body.pseudo-fullscreen { overflow: hidden; }
body.pseudo-fullscreen .viewer-shell { position: fixed; inset: 0; z-index: 9999; }

.topbar {
  z-index: 20;
  display: flex;
  align-items: center;
  gap: 14px;
  padding: 11px clamp(14px, 3vw, 38px);
  border-bottom: 1px solid #dce7eb;
  background: rgba(255, 255, 255, .97);
  box-shadow: 0 8px 30px rgba(2,22,31,.14);
  backdrop-filter: blur(16px);
}

.brand-logo {
  display: block;
  flex: 0 0 auto;
  width: clamp(156px, 18vw, 230px);
  height: auto;
  max-height: 42px;
  padding: 5px 8px;
  border-radius: 8px;
}

.title-wrap { min-width: 0; }
.eyebrow { margin: 0 0 2px; color: #087f99; font-size: 10px; font-weight: 800; letter-spacing: .18em; }
h1 { overflow: hidden; margin: 0; color: #172d36; font-size: clamp(15px, 2vw, 21px); font-weight: 650; line-height: 1.25; text-overflow: ellipsis; white-space: nowrap; }

.header-button {
  display: inline-flex;
  align-items: center;
  gap: 8px;
  margin-left: auto;
  min-height: 44px;
  padding: 9px 13px;
  border: 1px solid #c9d9de;
  border-radius: 10px;
  color: #15566a;
  background: #f3f8fa;
  font-size: 13px;
  text-decoration: none;
  transition: border-color 150ms ease, background 150ms ease, transform 150ms ease;
}

.header-button:hover { border-color: #6fc3d2; background: #e9f6f8; transform: translateY(-1px); }

.viewer {
  position: relative;
  display: grid;
  min-height: 0;
  grid-template-columns: minmax(52px, 1fr) minmax(240px, 1100px) minmax(52px, 1fr);
  align-items: center;
  overflow: hidden;
  perspective: 2200px;
}

.book {
  position: relative;
  grid-column: 2;
  display: flex;
  align-items: center;
  justify-content: center;
  width: 100%;
  height: calc(100% - 34px);
  max-height: 760px;
  transform: translate(var(--pan-x, 0), var(--pan-y, 0)) scale(var(--zoom, 1));
  transform-origin: center;
  transition: transform 180ms ease;
  user-select: none;
  -webkit-user-select: none;
  touch-action: none;
}

/* Panning follows the pointer, so the easing has to be out of the way. */
.book.panning { transition: none; }

.paper {
  position: relative;
  flex: 0 0 auto;
  overflow: hidden;
  background: white;
  cursor: grab;
  /* box-shadow composites on the GPU; the drop-shadow filter this replaces
     forced a full repaint of the book on every pan, zoom and page turn. */
  box-shadow: 0 22px 30px rgba(0,0,0,.38);
}

.paper canvas { display: block; }

.paper:active { cursor: grabbing; }
.book.drag-forward .right-paper { transform: rotateY(var(--drag-angle, -8deg)); box-shadow: -16px 0 28px rgba(0,0,0,.25); }
.book.drag-back .left-paper { transform: rotateY(var(--drag-angle, 8deg)); box-shadow: 16px 0 28px rgba(0,0,0,.25); }
.book.drag-forward .right-paper, .book.drag-back .left-paper { transition: none; will-change: transform; }
.book.snap-back .right-paper, .book.snap-back .left-paper { transform: rotateY(0deg); transition: transform 220ms cubic-bezier(.22,.8,.3,1), box-shadow 220ms ease; }
.book.pan-mode .paper { cursor: grab; }
.book.panning .paper { cursor: grabbing; }

.paper[hidden] { display: none; }
.paper canvas { display: block; width: auto; height: auto; background: white; }
.left-paper { border-radius: 7px 0 0 7px; transform-origin: right center; }
.right-paper { border-radius: 0 7px 7px 0; transform-origin: left center; }
.book.cover-mode .right-paper { border-radius: 7px; }

.page-number {
  position: absolute;
  right: 10px;
  bottom: 7px;
  display: none;
  padding: 2px 7px;
  border-radius: 10px;
  background: rgba(2,13,18,.62);
  color: white;
  font-size: 11px;
}

.left-paper .page-number { right: auto; left: 10px; }

.book-spine {
  position: absolute;
  z-index: 5;
  top: 50%;
  bottom: auto;
  left: 50%;
  width: 22px;
  pointer-events: none;
  transform: translate(-50%, -50%);
  background: linear-gradient(90deg, transparent, rgba(0,0,0,.22), rgba(255,255,255,.2), rgba(0,0,0,.18), transparent);
  opacity: .72;
}

.cover-mode .book-spine { display: none; }

.edge-hint {
  position: absolute;
  z-index: 8;
  top: 50%;
  display: flex;
  align-items: center;
  gap: 7px;
  padding: 9px 12px;
  border: 1px solid rgba(105,217,229,.32);
  border-radius: 12px;
  color: #e8fcff;
  background: rgba(3,24,31,.86);
  box-shadow: 0 8px 24px rgba(0,0,0,.28);
  font-size: 12px;
  opacity: 0;
  pointer-events: none;
  transform: translateY(-50%) scale(.94);
  transition: opacity 140ms ease, transform 140ms ease;
}

.edge-hint i { color: #69d9e5; font-size: 18px; }
.edge-hint-back { left: 4px; }
.edge-hint-forward { right: 4px; }
.book.hover-back .edge-hint-back, .book.hover-forward .edge-hint-forward { opacity: 1; transform: translateY(-50%) scale(1); }
.book.hover-back .left-paper { box-shadow: inset 18px 0 24px rgba(18,154,170,.1); }
.book.hover-forward .right-paper { box-shadow: inset -18px 0 24px rgba(18,154,170,.1); }

.mode-tip {
  position: absolute;
  z-index: 20;
  left: 50%;
  bottom: 12px;
  padding: 7px 11px;
  border: 1px solid rgba(255,255,255,.16);
  border-radius: 10px;
  color: #dff8fb;
  background: rgba(3,18,25,.84);
  box-shadow: 0 6px 20px rgba(0,0,0,.25);
  font-size: 11px;
  pointer-events: none;
  transform: translateX(-50%);
  white-space: nowrap;
}

.mode-tip[hidden] { display: none; }

.flip-sheet {
  position: absolute;
  z-index: 10;
  top: 0;
  left: 0;
  background: #fff;
  transform-origin: left center;
  backface-visibility: hidden;
  box-shadow: -8px 0 22px rgba(0,0,0,.22);
}

.flip-sheet canvas { width: auto; height: auto; display: block; }

/* Promote the turning sheet to its own compositor layer and keep it there for
   the whole animation — this is what removes the hitch on the first frame. */
.flip-sheet.turn-forward,
.flip-sheet.turn-back {
  display: block;
  will-change: transform;
  backface-visibility: hidden;
  -webkit-backface-visibility: hidden;
  contain: paint;
}

.flip-sheet.turn-forward { animation: turnForward 520ms cubic-bezier(.32,.06,.22,1) both; }
.flip-sheet.turn-back {
  transform-origin: right center;
  animation: turnBack 520ms cubic-bezier(.32,.06,.22,1) both;
}

@keyframes turnForward {
  from { transform: rotateY(0) translateZ(0); }
  to   { transform: rotateY(-180deg) translateZ(0); }
}
@keyframes turnBack {
  from { transform: rotateY(0) translateZ(0); }
  to   { transform: rotateY(180deg) translateZ(0); }
}

@media (prefers-reduced-motion: reduce) {
  .flip-sheet.turn-forward,
  .flip-sheet.turn-back { animation-duration: 1ms; }
}

.page-arrow {
  z-index: 12;
  display: grid;
  width: 52px;
  height: 86px;
  place-items: center;
  border: 1px solid transparent;
  border-radius: 16px;
  color: rgba(255,255,255,.8);
  background: transparent;
  font-size: 38px;
  cursor: pointer;
  transition: color 150ms ease, background 150ms ease, transform 150ms ease;
}

.page-arrow:hover:not(:disabled) { color: white; background: rgba(105,217,229,.1); transform: scale(1.05); }
.page-arrow:disabled { cursor: default; opacity: .18; }
.previous { grid-column: 1; justify-self: center; }
.next { grid-column: 3; justify-self: center; }

.controls {
  z-index: 20;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 7px;
  padding: 8px 12px;
  border-top: 1px solid rgba(255,255,255,.1);
  background: rgba(4,14,20,.92);
}

.controls button {
  display: grid;
  width: 44px;
  height: 44px;
  place-items: center;
  border: 1px solid transparent;
  border-radius: 8px;
  color: #dff8fb;
  background: transparent;
  cursor: pointer;
}

.controls button:hover:not(:disabled) { border-color: rgba(105,217,229,.25); background: rgba(105,217,229,.12); }
.controls button:disabled { cursor: default; opacity: .3; }
.page-field, .page-total, .zoom-label { color: #bcd2d8; font-size: 13px; }
.page-field input { width: 58px; margin-left: 5px; padding: 6px; border: 1px solid rgba(255,255,255,.16); border-radius: 7px; outline: none; color: white; background: rgba(255,255,255,.07); text-align: center; }
.page-field input:focus { border-color: #69d9e5; }
.divider { width: 1px; height: 24px; margin: 0 3px; background: rgba(255,255,255,.14); }
.zoom-label { width: 44px; text-align: center; }

.site-footer {
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 4px 12px;
  border-top: 1px solid rgba(255,255,255,.08);
  color: #8da8ae;
  background: rgba(4,14,20,.96);
  font-size: 11px;
  line-height: 1.2;
  text-align: center;
}
.touch-tip {
  position: absolute;
  z-index: 35;
  right: max(12px, env(safe-area-inset-right));
  bottom: 14px;
  display: flex;
  align-items: center;
  gap: 9px;
  max-width: min(420px, calc(100% - 24px));
  padding: 10px 12px;
  border: 1px solid rgba(105,217,229,.4);
  border-radius: 12px;
  color: #e8fcff;
  background: rgba(3,24,31,.94);
  box-shadow: 0 8px 24px rgba(0,0,0,.3);
  font-size: 12px;
}
.touch-tip[hidden] { display: none; }
.touch-tip i { color: #69d9e5; font-size: 18px; }
.touch-tip button { min-height: 44px; border: 1px solid rgba(105,217,229,.5); border-radius: 8px; color: #e8fcff; background: transparent; font: inherit; font-weight: 700; white-space: nowrap; }

.loading-card, .error-message {
  position: absolute;
  z-index: 30;
  top: 50%;
  left: 50%;
  display: grid;
  min-width: 240px;
  gap: 8px;
  justify-items: center;
  padding: 24px 28px;
  border: 1px solid rgba(255,255,255,.12);
  border-radius: 16px;
  background: rgba(8,24,33,.94);
  box-shadow: 0 16px 50px rgba(0,0,0,.4);
  transform: translate(-50%, -50%);
  text-align: center;
}

.loading-card[hidden], .error-message[hidden] { display: none; }
.loading-card span { color: #a9c4ca; font-size: 13px; }
.spinner { width: 28px; height: 28px; border: 3px solid rgba(105,217,229,.2); border-top-color: #69d9e5; border-radius: 50%; animation: spin .8s linear infinite; }
@keyframes spin { to { transform: rotate(360deg); } }
.error-message { color: #ffd7d7; border-color: rgba(255,102,102,.35); }

@media (max-width: 720px) {
  .viewer-shell { grid-template-rows: 62px minmax(0,1fr) 64px 28px; }
  .topbar { gap: 9px; padding: 9px 11px; }
  .brand-logo { width: 140px; max-height: 36px; }
  .title-wrap { display: none; }
  .header-button span, .divider { display: none; }
  .viewer { grid-template-columns: 46px minmax(180px,1fr) 46px; }
  .book { height: calc(100% - 24px); }
  .book:not(.cover-mode) .paper { max-width: 50%; }
  .page-arrow { width: 44px; height: 70px; font-size: 28px; }
  .controls { justify-content: flex-start; gap: 4px; overflow-x: auto; -webkit-overflow-scrolling: touch; padding-inline: 8px; }
  .controls button { width: 44px; height: 44px; flex: 0 0 auto; }
  .zoom-label { width: 40px; font-size: 12px; }
  .edge-hint span { display: none; }
  .edge-hint { padding: 8px 10px; }
  .mode-tip { max-width: 82vw; overflow: hidden; text-overflow: ellipsis; }
  .touch-tip { right: 12px; bottom: 10px; max-width: calc(100% - 24px); }
  .touch-tip span { line-height: 1.35; }
}
