* { box-sizing: border-box; }

html, body {
  margin: 0;
  padding: 0;
  width: 100%;
  height: 100%;
  overflow: hidden;
  font-family: -apple-system, "Malgun Gothic", "Apple SD Gothic Neo", sans-serif;
}

body {
  color: #eee;
  background:
    repeating-linear-gradient(45deg, rgba(255,255,255,0.5) 0px, rgba(255,255,255,0.5) 1px, transparent 1px, transparent 5px),
    repeating-linear-gradient(-45deg, rgba(0,0,0,0.03) 0px, rgba(0,0,0,0.03) 1px, transparent 1px, transparent 5px),
    radial-gradient(ellipse at 50% 30%, #e6e5e1 0%, #cfcecb 60%, #b7b6b1 100%);
}

#stage {
  position: absolute;
  top: 0; left: 0; right: 0; bottom: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  overflow: auto;
}

#book {
  position: relative;
  transform-origin: center center;
  transition: transform 0.2s ease;
  flex-shrink: 0;
  box-shadow:
    0 2px 6px rgba(0,0,0,0.25),
    0 30px 46px -12px rgba(0,0,0,0.55);
}

/* 책 아래 "선반" 그림자 */
#stage::after {
  content: "";
  position: absolute;
  left: 50%;
  bottom: 14%;
  width: min(70vw, 760px);
  height: 34px;
  background: radial-gradient(ellipse, rgba(20,20,20,0.45) 0%, rgba(20,20,20,0) 72%);
  transform: translateX(-50%);
  pointer-events: none;
  z-index: 0;
}
#book { z-index: 1; }
.navArrow { z-index: 4; }

.navArrow {
  position: absolute;
  top: 50%;
  transform: translateY(-50%);
  width: 40px;
  height: 40px;
  border: none;
  background: none;
  color: rgba(40,40,40,0.55);
  cursor: pointer;
  filter: drop-shadow(0 1px 2px rgba(255,255,255,0.6));
}
.navArrow svg { width: 100%; height: 100%; }
.navArrow:hover { color: rgba(20,20,20,0.85); }
.navArrowLeft { left: 10px; }
.navArrowRight { right: 10px; }

/* 확대 상태에서 드래그로 화면 이동(패닝)하기 위한 투명 레이어.
   확대 전(100%)에는 꺼둬서 책장 넘기기 드래그와 겹치지 않게 한다. */
#dragLayer {
  position: fixed;
  inset: 0;
  z-index: 3;
  cursor: grab;
  display: none;
}
body.zoomed #dragLayer { display: block; }
#dragLayer.dragging { cursor: grabbing; }

/* ---- 하단 떠있는 툴바 ---- */
#bottomBar {
  position: fixed;
  left: 50%;
  bottom: calc(16px + env(safe-area-inset-bottom, 0px));
  transform: translateX(-50%);
  display: flex;
  align-items: center;
  gap: 2px;
  padding: 6px;
  background: rgba(24,24,24,0.78);
  backdrop-filter: blur(6px);
  border-radius: 999px;
  box-shadow: 0 8px 24px rgba(0,0,0,0.35);
  z-index: 20;
}

.toolIcon {
  width: 38px;
  height: 38px;
  border: none;
  background: none;
  color: #eee;
  border-radius: 50%;
  cursor: pointer;
  padding: 9px;
  display: flex;
  align-items: center;
  justify-content: center;
}
.toolIcon svg { width: 100%; height: 100%; }
.toolIcon:hover { background: rgba(255,255,255,0.15); }
.toolIcon:disabled { opacity: 0.35; cursor: default; }
.toolIcon:disabled:hover { background: none; }

.toolIcon.active { color: #ff9500; }
.toolIcon.active:hover { background: rgba(255,149,0,0.15); }

/* ---- 더보기 팝업 (북마크/소리/자동넘김/전체화면) ---- */
#moreMenu {
  position: absolute;
  bottom: calc(100% + 10px);
  right: 6px;
  display: none;
  flex-direction: column;
  gap: 2px;
  padding: 6px;
  background: rgba(24,24,24,0.9);
  backdrop-filter: blur(6px);
  border-radius: 20px;
  box-shadow: 0 8px 24px rgba(0,0,0,0.35);
}
body.moreOpen #moreMenu { display: flex; }

.toolDivider {
  width: 1px;
  height: 20px;
  background: rgba(255,255,255,0.2);
  margin: 0 4px;
}

.pill {
  position: fixed;
  left: 50%;
  bottom: calc(66px + env(safe-area-inset-bottom, 0px));
  transform: translateX(-50%);
  background: rgba(24,24,24,0.7);
  color: #ddd;
  font-size: 12px;
  padding: 4px 14px;
  border-radius: 999px;
  z-index: 20;
  pointer-events: none;
}

.iconBtn {
  background: none;
  border: none;
  color: #eee;
  font-size: 16px;
  padding: 8px 12px;
  cursor: pointer;
  border-radius: 6px;
}
.iconBtn:hover { background: rgba(255,255,255,0.1); }

/* ---- 공용 패널(목차/썸네일) ---- */
.scrim {
  position: fixed;
  inset: 0;
  background: rgba(0,0,0,0.5);
  z-index: 25;
  display: none;
}
body.tocOpen #tocOverlay,
body.thumbOpen #thumbOverlay { display: block; }

#tocPanel {
  position: fixed;
  top: 0;
  left: -320px;
  width: 320px;
  height: 100%;
  background: #1c1c1c;
  z-index: 30;
  display: flex;
  flex-direction: column;
  transition: left 0.25s ease;
}
body.tocOpen #tocPanel { left: 0; }

/* 썸네일은 사이드바가 아니라 화면 중앙에 뜨는 모달 그리드 */
#thumbPanel {
  position: fixed;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%) scale(0.96);
  width: min(92vw, 820px);
  height: min(86vh, 760px);
  background: #1c1c1c;
  border-radius: 14px;
  z-index: 30;
  display: flex;
  flex-direction: column;
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.2s ease, transform 0.2s ease;
  overflow: hidden;
}
body.thumbOpen #thumbPanel {
  opacity: 1;
  pointer-events: auto;
  transform: translate(-50%, -50%) scale(1);
}

#tocPanelHeader,
#thumbPanelHeader {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 14px 16px;
  font-size: 16px;
  font-weight: 600;
  color: #fff;
  border-bottom: 1px solid #333;
  flex-shrink: 0;
}

#tocList {
  list-style: none;
  margin: 0;
  padding: 8px 0;
  overflow-y: auto;
}

#tocList li.tocCategory { padding: 0; }

.tocCategoryRow {
  padding: 12px 18px;
  font-size: 14px;
  font-weight: 700;
  cursor: pointer;
  border-left: 3px solid transparent;
  color: #fff;
}

.tocChildren {
  list-style: none;
  margin: 0;
  padding: 0 0 6px 0;
}

.tocChildren li {
  padding: 7px 18px 7px 34px;
  font-size: 13px;
  cursor: pointer;
  border-left: 3px solid transparent;
  color: #bbb;
}

.tocCategoryRow:hover,
.tocChildren li:hover { background: rgba(255,255,255,0.06); }

.tocCategoryRow.active,
.tocChildren li.active {
  border-left-color: #ff9500;
  background: rgba(255,149,0,0.12);
  color: #fff;
}

/* ---- 썸네일 그리드 ---- */
#thumbGrid {
  flex: 1;
  min-height: 0;
  width: 100%;
  overflow-y: auto;
  padding: 16px;
  display: grid;
  /* 행 높이를 aspect-ratio 기반 auto 계산에 맡기면 일부 브라우저에서
     행이 겹쳐버려서, 칼럼 폭과 행 높이를 둘 다 고정값으로 명시 */
  grid-template-columns: repeat(auto-fill, 112px);
  grid-auto-rows: 158px;
  justify-content: center;
  gap: 12px;
  align-content: start;
}
.thumbItem {
  position: relative;
  aspect-ratio: 906 / 1280;
  cursor: pointer;
  border-radius: 6px;
  overflow: hidden;
  border: 2px solid transparent;
  background: #000;
}
.thumbItem:hover { border-color: rgba(255,255,255,0.35); }
.thumbItem.active { border-color: #ff9500; }
.thumbItem.bookmarked::after {
  content: "\2605";
  position: absolute;
  top: 2px;
  right: 4px;
  font-size: 13px;
  color: #ff9500;
  text-shadow: 0 0 3px rgba(0,0,0,0.9);
}
.thumbItem img {
  display: block;
  width: 100%;
  height: 100%;
  object-fit: cover;
}
.thumbItem .thumbNum {
  position: absolute;
  left: 0;
  right: 0;
  bottom: 0;
  text-align: center;
  font-size: 11px;
  color: #eee;
  background: rgba(0,0,0,0.55);
  padding: 3px 0;
}

@media (max-width: 768px) {
  #tocPanel { width: 80%; left: -100%; }
  #thumbPanel { width: 94vw; height: 80vh; }
  .navArrow { width: 32px; height: 32px; }
  #thumbGrid { grid-template-columns: repeat(auto-fill, 84px); }
}
