/* ─── Reset ──────────────────────────────────────────────── */
*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }

/* ─── Theme Variables ─────────────────────────────────────── */
:root {
  --header-h: 48px;
  --bar-h:    54px;
  --radius:   8px;
  --accent:   #4f8ef7;
  --anim-dur: 0.35s;
  --side-btn: 52px;    /* 좌우 chevron 버튼 크기 */
}

html[data-theme="dark"] {
  --bg:           #111112;
  --bar-bg:       rgba(6, 6, 8, 0.52);
  --border:       rgba(255,255,255,0.09);
  --text:         #e2e2e2;
  --text-dim:     #666;
  --page-shadow:  0 6px 40px rgba(0,0,0,0.80);
  --side-btn-bg:  rgba(0,0,0,0.30);
  --side-btn-ho:  rgba(0,0,0,0.55);
  --spine-shadow: inset -4px 0 12px rgba(0,0,0,0.40), inset 4px 0 12px rgba(0,0,0,0.40);
}

html[data-theme="light"] {
  --bg:           #d0d0d0;
  --bar-bg:       rgba(215, 215, 218, 0.58);
  --border:       rgba(0,0,0,0.10);
  --text:         #1a1a1a;
  --text-dim:     #888;
  --page-shadow:  0 6px 32px rgba(0,0,0,0.22);
  --side-btn-bg:  rgba(255,255,255,0.30);
  --side-btn-ho:  rgba(255,255,255,0.60);
  --spine-shadow: inset -4px 0 12px rgba(0,0,0,0.12), inset 4px 0 12px rgba(0,0,0,0.12);
}

/* ─── Base ───────────────────────────────────────────────── */
html, body {
  height: 100%;
  background: var(--bg);
  color: var(--text);
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', sans-serif;
  font-size: 14px;
  overscroll-behavior: none;
  -webkit-tap-highlight-color: transparent;
  user-select: none;
  transition: background 0.3s;
}

/* ─── Loading ────────────────────────────────────────────── */
#loading-overlay {
  position: fixed; inset: 0;
  background: var(--bg);
  display: flex; flex-direction: column;
  align-items: center; justify-content: center;
  gap: 16px; z-index: 999;
  transition: opacity 0.5s;
}
#loading-overlay.hidden { opacity: 0; pointer-events: none; }
.loading-spinner {
  width: 36px; height: 36px;
  border: 3px solid var(--border);
  border-top-color: var(--accent);
  border-radius: 50%;
  animation: spin 0.85s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }
.loading-text { color: var(--text-dim); font-size: 13px; }

/* ─── Top Bar ────────────────────────────────────────────── */
#top-bar {
  position: fixed; top: 0; left: 0; right: 0;
  height: var(--header-h);
  background: var(--bar-bg);
  backdrop-filter: blur(20px) saturate(1.5);
  -webkit-backdrop-filter: blur(20px) saturate(1.5);
  border-bottom: 1px solid var(--border);
  display: flex; align-items: center; justify-content: center;
  padding: 0 56px; /* 양쪽 테마 버튼 공간 */
  z-index: 200;
  transition: background 0.3s;
}

/* 타이틀 이미지 */
.title-img {
  max-height: calc(var(--header-h) - 14px);
  max-width: calc(100vw - 120px);
  object-fit: contain;
  display: none;
}
html[data-theme="dark"]  #title-dark  { display: block; }
html[data-theme="dark"]  #title-light { display: none;  }
html[data-theme="light"] #title-dark  { display: none;  }
html[data-theme="light"] #title-light { display: block; }

/* 테마 토글 버튼 */
#theme-toggle {
  position: absolute; right: 12px;
  display: flex; align-items: center; justify-content: center;
  width: 34px; height: 34px;
  border: none; background: transparent;
  color: var(--text-dim);
  border-radius: var(--radius); cursor: pointer;
  transition: background 0.15s, color 0.15s;
}
#theme-toggle:hover { background: var(--border); color: var(--text); }
#theme-toggle svg   { width: 18px; height: 18px; }
html[data-theme="dark"]  #icon-moon { display: block; }
html[data-theme="dark"]  #icon-sun  { display: none;  }
html[data-theme="light"] #icon-moon { display: none;  }
html[data-theme="light"] #icon-sun  { display: block; }

/* ─── Side Chevron Navigation Buttons ───────────────────── */
.side-nav-btn {
  position: fixed;
  top: calc(var(--header-h) + (100vh - var(--header-h) - var(--bar-h)) / 2);
  transform: translateY(-50%);
  width:  var(--side-btn);
  height: var(--side-btn);
  border-radius: 50%;
  border: 1px solid var(--border);
  background: var(--side-btn-bg);
  backdrop-filter: blur(12px);
  -webkit-backdrop-filter: blur(12px);
  color: var(--text);
  display: flex; align-items: center; justify-content: center;
  cursor: pointer; z-index: 150;
  transition: background 0.2s, opacity 0.2s, transform 0.15s;
  opacity: 0.70;
}
.side-nav-btn:hover  { background: var(--side-btn-ho); opacity: 1; }
.side-nav-btn:active { transform: translateY(-50%) scale(0.93); }
.side-nav-btn:disabled { opacity: 0.18; cursor: default; pointer-events: none; }

.side-nav-btn svg {
  width: 28px; height: 28px;
  stroke: currentColor; stroke-width: 2.2; fill: none;
  stroke-linecap: round; stroke-linejoin: round;
}

#side-prev { left: 12px; }
#side-next { right: 12px; }

/* ─── Viewer Wrap ────────────────────────────────────────── */
#viewer-wrap {
  position: fixed; inset: 0;
  padding-top: var(--header-h);
  padding-bottom: var(--bar-h);
  overflow: hidden;       /* 슬라이드 중 넘치는 부분 숨김 */
  display: flex; align-items: center; justify-content: center;
  background: var(--bg);
  -webkit-overflow-scrolling: touch;
  transition: background 0.3s;
}

/* ─── Zoom Layer ─────────────────────────────────────────── */
#zoom-layer {
  display: flex; align-items: center; justify-content: center;
  transform-origin: center center;
  transition: transform 0.25s ease;
  will-change: transform;
}

/* ─── Book Clip (슬라이드 클리핑) ─────────────────────────── */
#book-clip {
  position: relative;
  overflow: hidden;   /* 슬라이드 아웃 중 넘치는 슬롯 클리핑 */
  border-radius: 10px;
}

/* ─── Book ───────────────────────────────────────────────── */
#book {
  position: relative;
}

/* ─── Page Slots ─────────────────────────────────────────── */
.page-slot {
  position: absolute; inset: 0;
  display: flex; align-items: stretch; justify-content: center;
  opacity: 0; pointer-events: none;
  will-change: transform, opacity;
}
.page-slot.active {
  position: relative;
  opacity: 1; pointer-events: auto;
}

/* ══════════════════════════════════════════════════════════
   SLIDE ANIMATION  –  오른쪽→왼쪽 (책장 밀기)
   Next: exit slides LEFT / enter slides from RIGHT
   Prev: exit slides RIGHT / enter slides from LEFT
══════════════════════════════════════════════════════════ */

/* ── 앞으로 ──────────────────────────────────────────────── */
.page-slot.exit-forward {
  animation: slideOutLeft var(--anim-dur) cubic-bezier(0.4,0,0.6,1) forwards;
}
.page-slot.enter-forward {
  animation: slideInRight var(--anim-dur) cubic-bezier(0.4,0,0.2,1) forwards;
  position: absolute !important; inset: 0;
  opacity: 1; pointer-events: none;
}

@keyframes slideOutLeft {
  from { transform: translateX(0);     opacity: 1; }
  to   { transform: translateX(-100%); opacity: 0.6; }
}
@keyframes slideInRight {
  from { transform: translateX(100%);  opacity: 0.6; }
  to   { transform: translateX(0);     opacity: 1; }
}

/* ── 뒤로 ───────────────────────────────────────────────── */
.page-slot.exit-backward {
  animation: slideOutRight var(--anim-dur) cubic-bezier(0.4,0,0.6,1) forwards;
}
.page-slot.enter-backward {
  animation: slideInLeft var(--anim-dur) cubic-bezier(0.4,0,0.2,1) forwards;
  position: absolute !important; inset: 0;
  opacity: 1; pointer-events: none;
}

@keyframes slideOutRight {
  from { transform: translateX(0);    opacity: 1; }
  to   { transform: translateX(100%); opacity: 0.6; }
}
@keyframes slideInLeft {
  from { transform: translateX(-100%); opacity: 0.6; }
  to   { transform: translateX(0);     opacity: 1; }
}

/* ─── Page Inner ─────────────────────────────────────────── */
.page-inner {
  display: flex;
  gap: 0;
  align-items: stretch;
}

/* ─── Page Images ────────────────────────────────────────── */
.page-img {
  display: block;
  height: auto;
  box-shadow: var(--page-shadow);
  image-rendering: auto;
  object-fit: contain;
  background: #fff;
}
.page-img.hidden { display: none; }

/* 두 페이지 모드 — 안쪽은 직각, 바깥 모서리만 라운드 */
.page-inner:not(.two-page) .page-img {
  border-radius: 10px;
}
.page-inner.two-page .page-img:first-child {
  border-radius: 10px 0 0 10px;
  box-shadow: var(--page-shadow);
}
.page-inner.two-page .page-img:last-child {
  border-radius: 0 10px 10px 0;
  box-shadow: var(--page-shadow);
}
/* 책 중앙 척추(spine) 그림자 */
.page-inner.two-page::after {
  content: '';
  position: absolute;
  top: 0; bottom: 0;
  left: 50%;
  width: 20px;
  transform: translateX(-50%);
  background: var(--spine-shadow);
  pointer-events: none;
  z-index: 2;
  border-radius: 0;
}
.page-inner.two-page {
  position: relative; /* for ::after */
}

/* ─── Control Bar ────────────────────────────────────────── */
#control-bar {
  position: fixed; bottom: 0; left: 0; right: 0;
  height: var(--bar-h);
  background: var(--bar-bg);
  backdrop-filter: blur(20px) saturate(1.5);
  -webkit-backdrop-filter: blur(20px) saturate(1.5);
  border-top: 1px solid var(--border);
  display: flex; align-items: center; justify-content: center;
  gap: 20px; z-index: 200; padding: 0 16px;
  transition: background 0.3s;
}

.ctrl-group { display: flex; align-items: center; gap: 4px; }
.zoom-group { border-left: 1px solid var(--border); padding-left: 20px; }

.ctrl-btn {
  display: flex; align-items: center; justify-content: center;
  width: 34px; height: 34px;
  border: none; background: transparent;
  color: var(--text-dim); border-radius: var(--radius);
  cursor: pointer; transition: background 0.15s, color 0.15s;
}
.ctrl-btn:hover  { background: var(--border); color: var(--text); }
.ctrl-btn:active { opacity: 0.7; }
.ctrl-btn:disabled { opacity: 0.22; cursor: default; }
.ctrl-btn svg {
  width: 18px; height: 18px;
  stroke: currentColor; stroke-width: 2; fill: none;
  stroke-linecap: round; stroke-linejoin: round;
}

.fit-btn { width: auto; padding: 0 12px; font-size: 12px; font-weight: 600; letter-spacing: 0.04em; }
.fit-btn.active { background: rgba(79,142,247,0.18); color: var(--accent); }

.page-info { display: flex; align-items: center; gap: 6px; padding: 0 6px; }
#page-input {
  width: 44px; background: var(--border);
  border: 1px solid var(--border); border-radius: 5px;
  color: var(--text); font-size: 13px;
  text-align: center; padding: 4px; outline: none;
  appearance: textfield; -moz-appearance: textfield;
}
#page-input::-webkit-outer-spin-button,
#page-input::-webkit-inner-spin-button { -webkit-appearance: none; margin: 0; }
#page-input:focus { border-color: var(--accent); }
#page-total { color: var(--text-dim); font-size: 13px; min-width: 44px; }
#zoom-level {
  font-size: 12px; color: var(--text-dim);
  min-width: 44px; text-align: center; font-variant-numeric: tabular-nums;
}

.download-group { border-left: 1px solid var(--border); padding-left: 16px; }

.download-btn {
  color: var(--text-dim);
  text-decoration: none;
}
.download-btn:hover { color: var(--accent) !important; background: rgba(79,142,247,0.10) !important; }

/* ─── Mobile ─────────────────────────────────────────────── */
@media (max-width: 600px) {
  :root { --header-h: 40px; --bar-h: 52px; --side-btn: 40px; }
  #side-prev { left: 6px; }
  #side-next { right: 6px; }
  .side-nav-btn svg { width: 22px; height: 22px; }
  .ctrl-group.zoom-group { padding-left: 12px; gap: 2px; }
  .fit-btn { display: none; }
  #zoom-level { min-width: 36px; }
}
