:root {
  --bg: #17171a;
  --bg-bar: #101013;
  --fg: #e9e9ec;
  --fg-dim: #9a9aa4;
  --line: #2c2c33;
  --accent: #e8477f;
  --bar-h: 52px;
}

* { box-sizing: border-box; }

html, body {
  height: 100%;
  margin: 0;
  overscroll-behavior: none;
  /* 넘김 애니메이션 중 StPageFlip 의 그림자(.stf__outerShadow 등)가 뷰포트 밖까지
     뻗어 문서 스크롤 영역을 늘린다. 그대로 두면 스크롤바가 생기고 → 뷰포트가 줄고
     → 다시 크기를 맞추면서 화면이 튄다. 전체화면 고정 레이아웃이라 스크롤은 불필요. */
  overflow: hidden;
}

body {
  background: var(--bg);
  color: var(--fg);
  font-family: "Pretendard", -apple-system, BlinkMacSystemFont, "Segoe UI",
    "Malgun Gothic", "Apple SD Gothic Neo", sans-serif;
  -webkit-text-size-adjust: 100%;
}

/* ---------- 상·하단 바 ---------- */

.bar {
  display: flex;
  align-items: center;
  gap: 8px;
  min-height: var(--bar-h);
  padding: 0 12px;
  background: var(--bg-bar);
  border-color: var(--line);
  border-style: solid;
  border-width: 0;
}

.bar--top {
  position: fixed;
  z-index: 40;
  top: 0;
  left: 0;
  right: 0;
  padding-top: env(safe-area-inset-top);
  border-bottom-width: 1px;
  transition: opacity .18s ease, transform .22s ease;
}

/* 기본 화면에서는 책이 상단 바보다 앞 레이어에 놓인다. */
body:not(.is-fullscreen) .bar--top { z-index: 1; }
body:not(.is-fullscreen) #stage { z-index: 2; }

.bar--bottom {
  position: fixed;
  z-index: 30;
  left: 0;
  right: 0;
  bottom: 0;
  border-top-width: 1px;
  justify-content: center;
  padding-bottom: env(safe-area-inset-bottom);
  opacity: 0;
  pointer-events: none;
  transform: translateY(calc(100% + 8px));
  transition: opacity .18s ease, transform .22s ease;
}

body.footer-visible .bar--bottom {
  opacity: 1;
  pointer-events: auto;
  transform: translateY(0);
}

/* PC 첫 진입에서는 스크립트가 안내 시간을 끝낼 때까지 하단 조작부를 보여 준다. */
body.footer-intro .bar--bottom {
  opacity: 1;
  pointer-events: auto;
  transform: translateY(0);
}

#footer-hotspot { display: none; }

.titles {
  display: flex;
  flex-direction: column;
  min-width: 0;
  line-height: 1.3;
}

.titles strong {
  font-size: 14px;
  font-weight: 600;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.titles span {
  font-size: 11px;
  color: var(--fg-dim);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.actions {
  margin-left: auto;
  display: flex;
  gap: 6px;
  flex: 0 0 auto;
}

button,
.actions a {
  appearance: none;
  border: 1px solid var(--line);
  background: transparent;
  color: var(--fg);
  font: inherit;
  font-size: 13px;
  line-height: 1;
  padding: 8px 12px;
  border-radius: 7px;
  cursor: pointer;
  text-decoration: none;
  white-space: nowrap;
  transition: background-color .15s, border-color .15s;
}

button:hover,
.actions a:hover { background: #24242b; }

button:active { background: #2e2e37; }

button:focus-visible,
.actions a:focus-visible,
#scrubber:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 2px;
}

button[disabled] { opacity: .35; cursor: default; }

#btn-prev, #btn-next,
#zoom-prev, #zoom-next {
  font-size: 20px;
  padding: 4px 14px;
}

#counter, #zoom-counter {
  font-size: 13px;
  color: var(--fg-dim);
  font-variant-numeric: tabular-nums;
  min-width: 62px;
  text-align: center;
}

#scrubber {
  width: min(320px, 38vw);
  accent-color: var(--accent);
}

/* ---------- 플립북 ---------- */

#stage {
  position: fixed;
  inset: 0;
  width: 100%;
  height: 100dvh;
  display: grid;
  place-items: center;
  padding: 0;
}

body:not(.is-fullscreen) #stage {
  top: calc(var(--bar-h) + env(safe-area-inset-top));
  height: calc(100dvh - var(--bar-h) - env(safe-area-inset-top));
}

body.is-fullscreen .bar--top {
  opacity: 0;
  pointer-events: none;
  transform: translateY(calc(-100% - 8px));
}

html:fullscreen,
body:fullscreen {
  width: 100%;
  height: 100%;
}

body.is-mobile-fullscreen #stage {
  width: 100vw;
  height: 100dvh;
}

#book-wrap {
  /* 실제 크기는 app.js 가 뷰포트에 맞춰 계산해 넣는다 */
  max-width: 100%;
  max-height: 100%;
}

#flipbook { margin: 0 auto; }

#flipbook .page {
  background: #fff;
  overflow: hidden;
}

#flipbook .page img {
  display: block;
  width: 100%;
  height: 100%;
  object-fit: contain;
  pointer-events: none;
  user-select: none;
}

#loading {
  position: absolute;
  color: var(--fg-dim);
  font-size: 14px;
}

/* ---------- 목차 (썸네일) ---------- */

#thumbs[hidden] { display: none; }

#thumbs {
  position: fixed;
  z-index: 29;
  left: 0;
  right: 0;
  bottom: calc(var(--bar-h) + env(safe-area-inset-bottom));
  display: flex;
  gap: 8px;
  overflow-x: auto;
  padding: 10px 12px calc(10px + env(safe-area-inset-bottom));
  background: transparent;
  border-top: 0;
  scrollbar-width: thin;
}

#thumbs button {
  flex: 0 0 auto;
  padding: 0;
  border-radius: 3px;
  line-height: 0;
  background: #fff;
}

#thumbs img {
  display: block;
  width: 58px;
  height: auto;
  border-radius: 2px;
}

#thumbs button[aria-current="true"] {
  border-color: var(--accent);
  box-shadow: 0 0 0 1px var(--accent);
}

/* ---------- 확대 보기 ---------- */

#zoom {
  position: fixed;
  inset: 0;
  z-index: 50;
  display: flex;
  flex-direction: column;
  background: #0c0c0e;
}

#zoom[hidden] { display: none; }

.zoom-bar {
  flex: 0 0 auto;
  display: flex;
  align-items: center;
  gap: 6px;
  padding: 8px 12px;
  padding-top: calc(8px + env(safe-area-inset-top));
  background: var(--bg-bar);
  border-bottom: 1px solid var(--line);
}

.zoom-bar .spacer { flex: 1 1 auto; }

.zoom-scroll {
  flex: 1 1 auto;
  min-height: 0; /* 이게 없으면 컨테이너 높이가 확정되지 않아 아래 max-height 가 먹지 않는다 */
  overflow: auto;
  display: flex;
  /* safe: 확대해서 넘칠 때 왼쪽·위가 잘려 스크롤로 못 가는 현상을 막는다 */
  align-items: safe center;
  justify-content: safe center;
  padding: 12px;
  -webkit-overflow-scrolling: touch;
}

.zoom-scroll.can-pan { cursor: grab; }
.zoom-scroll.is-dragging {
  cursor: grabbing;
  user-select: none;
}

.zoom-pages {
  display: flex;
  align-items: flex-start;
  justify-content: center;
  flex: 0 0 auto;
  line-height: 0;
}

.zoom-pages img {
  display: block;
  background: #fff;
  flex: 0 0 auto;
  /* 기본은 화면에 꽉 맞추고, 확대 단계부터는 스크롤로 본다 */
  width: auto;
  height: auto;
  max-width: none;
  max-height: none;
  user-select: none;
  -webkit-user-drag: none;
  will-change: width;
  transition: width .32s cubic-bezier(.22, .8, .24, 1);
}

.zoom-pages img[hidden] { display: none; }

/* ---------- 기타 ---------- */

.fallback {
  padding: 24px;
  text-align: center;
  color: var(--fg-dim);
}

.fallback a { color: var(--accent); }

.sr-only {
  position: absolute;
  width: 1px;
  height: 1px;
  padding: 0;
  margin: -1px;
  overflow: hidden;
  clip: rect(0 0 0 0);
  white-space: nowrap;
  border: 0;
}

@media (max-width: 620px) {
  .titles span { display: none; }
  .titles strong { font-size: 13px; }
  button, .actions a { padding: 8px 10px; font-size: 12px; }
  #scrubber { width: 30vw; }
  #stage { padding: 4px; }
}

@media (hover: none), (pointer: coarse) {
  #stage,
  #zoom { touch-action: none; }

  /* 모바일 일반 화면은 상단·하단 조작 바 사이만 책 영역으로 쓴다. */
  body:not(.is-fullscreen) #stage {
    top: calc(var(--bar-h) + env(safe-area-inset-top));
    right: 0;
    bottom: calc(var(--bar-h) + env(safe-area-inset-bottom));
    left: 0;
    width: auto;
    height: auto;
    padding: 0;
  }

  /* 모바일 일반 화면에서는 페이지·목차 조작부를 항상 남긴다. */
  body:not(.is-fullscreen) .bar--bottom {
    opacity: 1;
    pointer-events: auto;
    transform: translateY(0);
  }

  /* 모바일 전체화면은 책에만 집중하도록 하단 조작부와 목차를 숨긴다. */
  body.is-fullscreen .bar--bottom {
    opacity: 0;
    pointer-events: none;
    transform: translateY(calc(100% + 8px));
  }

  body.is-mobile-fullscreen #thumbs,
  body.is-mobile-fullscreen #footer-hotspot { display: none; }

  #footer-hotspot {
    position: fixed;
    z-index: 28;
    left: 0;
    right: 0;
    bottom: 0;
    display: block;
    width: 100%;
    height: 28px;
    padding: 0;
    border: 0;
    border-radius: 0;
    background: transparent;
  }

  #footer-hotspot::after {
    content: "";
    position: absolute;
    left: 50%;
    bottom: 7px;
    width: 42px;
    height: 3px;
    border-radius: 999px;
    background: rgba(255, 255, 255, .3);
    transform: translateX(-50%);
  }

  body.footer-visible #footer-hotspot { pointer-events: none; }
  body:not(.is-fullscreen) #footer-hotspot { display: none; }
}

@media (prefers-reduced-motion: reduce) {
  * { transition: none !important; }
}
