:root {
  --bg: #0f1115;
  --fg: #e7ebf3;
  --muted: #9aa3b2;
  --panel: #161a22;
  --border: rgba(255,255,255,.08);
  --accent: #8ab4ff;
}
* { box-sizing: border-box; }
html, body { height:100%; margin:0; background: var(--bg); color: var(--fg); font-family: system-ui, -apple-system, Segoe UI, Roboto, "Apple SD Gothic Neo", "Malgun Gothic", sans-serif; }
.layout { display:grid; grid-template-columns: 170px 1fr; grid-template-rows: 56px 1fr; height:100%; }
header { grid-column: 1 / 3; display:flex; gap:8px; align-items:center; padding:8px 12px; background: var(--panel); border-bottom:1px solid var(--border); position:sticky; top:0; z-index:10; }
header .title { font-weight:700; margin-right:auto; }
header .btn, header input, header select {
  height:32px; padding:0 8px; border:1px solid var(--border); background:#12161d; color:var(--fg); border-radius:8px; line-height: 1; box-sizing: border-box;
}
header .btn { cursor:pointer; }
header .btn:disabled { opacity:.4; cursor:not-allowed; }
header .sep { opacity:.4; padding:0 4px; }

aside { background:#0c0f14; border-right:1px solid var(--border); overflow:auto; }
.panel { padding:10px 6px 10px 10px; display:flex; flex-direction:column; gap:10px; }
.thumbs { display:grid; grid-template-columns: 1fr; gap:8px; padding: 6px 0; }
.thumb { 
  border:1px solid var(--border); 
  background:#0b0e13; 
  border-radius:10px; 
  padding:8px; 
  cursor:pointer; 
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  min-height: 80px;
}
.thumb canvas { 
  width:100%; 
  max-width: 100%;
  display:block; 
  aspect-ratio: .7; 
  background:#0a0d11; 
  border-radius:6px;
  image-rendering: auto;
  image-rendering: -webkit-optimize-contrast;
}
.thumb .pg { 
  font-size:10px; 
  color:var(--muted); 
  margin-top:6px; 
  text-align:center; 
  width: 100%;
  line-height: 1;
}

main { overflow:auto; position:relative; }
#viewerWrap { display:flex; justify-content:center; padding:24px; perspective: 1000px; position:relative; }
#pageLayer { position:relative; transform-style: preserve-3d; }

/* PDF 로딩 화면 */
.loading {
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
  height: 100%;
  min-height: 600px;
  width: 400px;
  color: var(--muted);
  gap: 20px;
  background: var(--bg);
  border-radius: 12px;
  border: 1px solid var(--border);
}

.spinner {
  width: 50px;
  height: 50px;
  border: 4px solid var(--border);
  border-top: 4px solid var(--accent);
  border-radius: 50%;
  animation: spin 1s linear infinite;
}

.bar {
  height: 8px;
  width: 80%;
  max-width: 400px;
  border-radius: 8px;
  background: rgba(255,255,255,.08);
  overflow: hidden;
}

.bar > i {
  display: block;
  height: 100%;
  width: 0%;
  background: var(--accent);
  transition: width .15s;
}

@keyframes spin {
  0% { transform: rotate(0deg); }
  100% { transform: rotate(360deg); }
}

/* 좌우 가장자리 클릭 영역 */
.click-area {
  position: absolute;
  top: 0;
  height: 100%;
  width: 80px;
  z-index: 5;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  opacity: 0;
  transition: opacity 0.2s ease;
  -webkit-tap-highlight-color: transparent;
  -webkit-touch-callout: none;
  -webkit-user-select: none;
  -moz-user-select: none;
  -ms-user-select: none;
  user-select: none;
}
.click-area:hover {
  opacity: 0;
  background: var(--accent);
}
.click-area:active {
  opacity: 0;
  background: var(--accent);
}
.click-area.left {
  left: 0;
  border-radius: 12px 0 0 12px;
}
.click-area.right {
  right: 0;
  border-radius: 0 12px 12px 0;
}
.click-area::before {
  content: '';
  width: 20px;
  height: 20px;
  border: 2px solid var(--accent);
  border-radius: 50%;
  opacity: 0;
  transition: opacity 0.2s ease;
}
.click-area:hover::before {
  opacity: 0;
}
.click-area.left::before {
  border-right: none;
  border-bottom: none;
  transform: rotate(-45deg);
}
.click-area.right::before {
  border-left: none;
  border-top: none;
  transform: rotate(-45deg);
}
#pdfCanvas { 
  background:#11151c; 
  border-radius:12px; 
  box-shadow: 
    0 10px 30px rgba(0,0,0,.35),
    0 0 0 1px rgba(255,255,255,0.05);
  transition: all 0.3s ease-in-out; 
  cursor: grab; 
  position: relative;
  image-rendering: auto;
  image-rendering: -webkit-optimize-contrast;
}

/* 1장 보기에서도 책 느낌 */
#pdfCanvas::after {
  content: '';
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  border-radius: 12px;
  background: linear-gradient(135deg, 
    rgba(255,255,255,0.1) 0%, 
    transparent 50%, 
    rgba(0,0,0,0.1) 100%);
  pointer-events: none;
  z-index: 1;
}

/* 2장 보기 스타일 - 책 느낌 */
.dual-page-view { 
  display: flex; 
  gap: 0; 
  justify-content: center; 
  align-items: flex-start; 
  perspective: 1000px;
  transform-style: preserve-3d;
}
.dual-page-view .click-area {
  width: 120px; /* 2장 보기에서는 더 넓게 */
}
.dual-page-view .page-container { 
  position: relative; 
  transform-style: preserve-3d;
}
.dual-page-view #pdfCanvas { 
  margin: 0; 
  border-radius: 12px 0 0 12px;
  border-left: none;
  box-shadow: 
    -5px 0 15px rgba(0,0,0,0.3),
    inset -1px 0 0 rgba(255,255,255,0.1);
}
.dual-page-view #pdfCanvas2 { 
  background:#11151c; 
  border-radius: 0 12px 12px 0;
  border-right: none;
  box-shadow: 
    5px 0 15px rgba(0,0,0,0.3),
    inset 1px 0 0 rgba(255,255,255,0.1);
  transition: all 0.3s ease-in-out; 
  cursor: grab; 
  position: relative;
  image-rendering: auto;
  image-rendering: -webkit-optimize-contrast;
}
.dual-page-view .text-layer { position:absolute; left:0; top:0; right:0; bottom:0; pointer-events:none; }

/* 책 중앙 접힘 효과 */
.dual-page-view::before {
  content: '';
  position: absolute;
  top: 0;
  left: 50%;
  width: 40px;
  height: 100%;
  background: linear-gradient(to right, 
    rgba(0,0,0,0.0) 0%, 
    rgba(0,0,0,0.15) 30%, 
    rgba(0,0,0,0.3) 50%, 
    rgba(0,0,0,0.15) 70%, 
    rgba(0,0,0,0.0) 100%);
  transform: translateX(-50%);
  z-index: 10;
}

/* 책 그림자 효과 */
.dual-page-view::after {
  content: '';
  position: absolute;
  top: 0;
  left: 50%;
  width: 100%;
  height: 100%;
  background: transparent;
  transform: translateX(-50%);
  z-index: 1;
  pointer-events: none;
}

/* 마지막 홀수 페이지 - 왼쪽에 표시하고 우측은 빈 페이지 */
.dual-page-view #pdfCanvas2:empty,
.dual-page-view #pdfCanvas2[style*="display: none"] {
  border-radius: 0 12px 12px 0;
  border-right: none;
  box-shadow: 
    5px 0 15px rgba(0,0,0,0.3),
    inset 1px 0 0 rgba(255,255,255,0.1);
  display: block !important;
}
#pdfCanvas:active { cursor: grabbing; }

/* 기본 curl 효과 (항상 보임) */
#pdfCanvas::before {
  content: '';
  position: absolute;
  top: 0;
  right: 0;
  width: 60px;
  height: 100%;
  background: linear-gradient(90deg, transparent 0%, rgba(0,0,0,0.1) 30%, rgba(0,0,0,0.2) 70%, rgba(0,0,0,0.3) 100%);
  transform-origin: right center;
  transform: rotateY(var(--curl-angle, -15deg));
  z-index: 5;
  pointer-events: none;
  transition: all 0.2s ease-out;
}

/* 드래그 중일 때 기본 curl 효과 강화 */
#pdfCanvas.dragging::before {
  transform: rotateY(var(--curl-angle, -45deg)) !important;
  background: linear-gradient(90deg, transparent 0%, rgba(0,0,0,0.2) 20%, rgba(0,0,0,0.4) 50%, rgba(0,0,0,0.6) 80%, rgba(0,0,0,0.8) 100%) !important;
  width: 80px !important;
  box-shadow: -5px 0 15px rgba(0,0,0,0.4) !important;
}
#textLayer { position:absolute; left:0; top:0; right:0; bottom:0; pointer-events:none; }

/* 실제 HTML 요소로 curl 효과 */
#curlEffect {
  position: absolute;
  top: 0;
  right: 0;
  width: 80px;
  height: 100%;
  background: linear-gradient(90deg, transparent 0%, rgba(0,0,0,0.3) 20%, rgba(0,0,0,0.5) 50%, rgba(0,0,0,0.7) 80%, rgba(0,0,0,0.9) 100%);
  transform-origin: right center;
  transform: rotateY(-20deg);
  z-index: 10;
  pointer-events: none;
  transition: all 0.1s ease-out;
  opacity: 0.5; /* 테스트용으로 항상 보이게 */
  box-shadow: -5px 0 15px rgba(0,0,0,0.3);
}

#curlEffect.dragging {
  opacity: 1 !important;
  transform: rotateY(-60deg) !important;
  background: linear-gradient(90deg, transparent 0%, rgba(0,0,0,0.4) 20%, rgba(0,0,0,0.6) 50%, rgba(0,0,0,0.8) 80%, rgba(0,0,0,1) 100%) !important;
  width: 100px !important;
  box-shadow: -10px 0 25px rgba(0,0,0,0.6) !important;
}

/* 페이지 말림(curl) 효과 */
.page-curl {
  position: relative;
  overflow: visible;
}

.page-curl::before {
  content: '';
  position: absolute;
  top: 0;
  right: 0;
  width: 100px;
  height: 100%;
  background: linear-gradient(90deg, transparent 0%, rgba(0,0,0,0.3) 20%, rgba(0,0,0,0.5) 50%, rgba(0,0,0,0.7) 80%, rgba(0,0,0,0.9) 100%);
  transform-origin: right center;
  transform: rotateY(var(--curl-angle, -25deg));
  z-index: 10;
  pointer-events: none;
  transition: transform 0.1s ease-out;
  box-shadow: -10px 0 20px rgba(0,0,0,0.5);
}

.page-curl::after {
  content: '';
  position: absolute;
  top: 0;
  right: 0;
  width: 50px;
  height: 100%;
  background: linear-gradient(90deg, transparent 0%, rgba(255,255,255,0.2) 30%, rgba(255,255,255,0.4) 60%, rgba(255,255,255,0.6) 100%);
  transform-origin: right center;
  transform: rotateY(calc(var(--curl-angle, -25deg) * 1.8));
  z-index: 11;
  pointer-events: none;
  transition: transform 0.1s ease-out;
}

/* 드래그 중일 때 페이지 말림 효과 */
.dragging .page-curl::before {
  transform: rotateY(-70deg) !important;
  transition: transform 0.1s ease-out;
}

.dragging .page-curl::after {
  transform: rotateY(-90deg) !important;
  transition: transform 0.1s ease-out;
}

/* 책장 넘기기 효과 */
#pdfCanvas.page-flip {
  animation: pageFlip 0.8s ease-in-out;
}

@keyframes pageFlip {
  0% { transform: rotateY(0deg); }
  50% { transform: rotateY(-90deg) scale(0.95); }
  100% { transform: rotateY(0deg); }
}

/* 페이지 전환 효과 */
#pdfCanvas.page-transition {
  animation: pageTransition 0.6s ease-in-out;
}

@keyframes pageTransition {
  0% { opacity: 1; transform: scale(1); }
  50% { opacity: 0.3; transform: scale(0.98); }
  100% { opacity: 1; transform: scale(1); }
}

/* 슬라이드 효과 */
#pdfCanvas.page-slide-left {
  animation: slideLeft 0.5s ease-in-out;
}

#pdfCanvas.page-slide-right {
  animation: slideRight 0.5s ease-in-out;
}

@keyframes slideLeft {
  0% { transform: translateX(0); }
  50% { transform: translateX(-50%); }
  100% { transform: translateX(0); }
}

@keyframes slideRight {
  0% { transform: translateX(0); }
  50% { transform: translateX(50%); }
  100% { transform: translateX(0); }
}

/* 페이드 효과 */
#pdfCanvas.page-fade {
  animation: fadeInOut 0.4s ease-in-out;
}

@keyframes fadeInOut {
  0% { opacity: 1; }
  50% { opacity: 0; }
  100% { opacity: 1; }
}

.info { color:var(--muted); font-size:12px; }
.kbd { border:1px solid var(--border); padding:0 6px; border-radius:6px; font-size:12px; background:#11151c; color:var(--muted); }

/* 다크/라이트 토글용 */
.light { --bg:#f5f6fb; --fg:#0c1220; --muted:#556077; --panel:#ffffff; --border:rgba(0,0,0,.08); --accent:#2559e4; }
.light #pdfCanvas { 
  background:#fff; 
  box-shadow: 
    0 10px 30px rgba(0,0,0,.15),
    0 0 0 1px rgba(0,0,0,0.05);
}
.light #pdfCanvas::after {
  background: linear-gradient(135deg, 
    rgba(255,255,255,0.8) 0%, 
    transparent 50%, 
    rgba(0,0,0,0.05) 100%);
}
.light aside { background:#f1f3f9; }
.light header .btn, .light header input, .light header select { background:#fff; }

/* 라이트 테마에서 클릭 영역 */
.light .click-area:hover {
  background: var(--accent);
  opacity: 0.1;
}
.light .click-area:active {
  background: var(--accent);
  opacity: 0.05;
}

/* 라이트 테마에서 2장 보기 책 느낌 */
.light .dual-page-view #pdfCanvas { 
  box-shadow: 
    -5px 0 15px rgba(0,0,0,0.2),
    inset -1px 0 0 rgba(0,0,0,0.1);
}
.light .dual-page-view #pdfCanvas2 { 
  box-shadow: 
    5px 0 15px rgba(0,0,0,0.2),
    inset 1px 0 0 rgba(0,0,0,0.1);
}
.light .dual-page-view::before {
  background: linear-gradient(to bottom, 
    rgba(0,0,0,0.6) 0%, 
    rgba(0,0,0,0.3) 20%, 
    rgba(0,0,0,0.1) 50%, 
    rgba(0,0,0,0.3) 80%, 
    rgba(0,0,0,0.6) 100%);
}
.light .dual-page-view::after {
  background: radial-gradient(ellipse at center, 
    transparent 0%, 
    transparent 40%, 
    rgba(0,0,0,0.05) 70%, 
    rgba(0,0,0,0.1) 100%);
}

/* 라이트 테마 마지막 홀수 페이지 빈 페이지 */
.light .dual-page-view #pdfCanvas2:empty,
.light .dual-page-view #pdfCanvas2[style*="display: none"] {
  background: #ffffff;
  border: 1px solid var(--border);
  box-shadow: 
    5px 0 15px rgba(0,0,0,0.15),
    inset 1px 0 0 rgba(255,255,255,0.1);
}

/* 모바일 반응형 디자인 */
@media (max-width: 768px) {
  .layout { grid-template-columns: 1fr; grid-template-rows: auto auto 1fr; }
  header { grid-column: 1; flex-wrap: wrap; gap: 4px; padding: 8px; }
  header .title { font-size: 14px; }
  header .btn, header input, header select { 
    height: 32px; 
    padding: 0 6px; 
    font-size: 11px; 
    min-width: auto;
    line-height: 1;
    box-sizing: border-box;
  }
  header .info { font-size: 11px; }
  header .kbd { font-size: 10px; padding: 0 4px; }
  
  aside { 
    grid-row: 2; 
    height: 80px; 
    max-height: 80px; 
    min-height: 80px;
    border-right: none; 
    border-bottom: 1px solid var(--border);
    overflow-x: auto;
    overflow-y: hidden;
    background: var(--panel);
  }
  .panel { 
    padding: 8px 4px 8px 8px; 
    height: 100%;
    display: flex;
    flex-direction: column;
  }
  .thumbs { 
    display: flex;
    flex-direction: row;
    gap: 4px;
    overflow-x: auto;
    overflow-y: hidden;
    padding: 4px 2px;
    scrollbar-width: thin;
    scrollbar-color: var(--muted) transparent;
  }
  
  .thumbs::-webkit-scrollbar {
    height: 4px;
  }
  
  .thumbs::-webkit-scrollbar-track {
    background: transparent;
  }
  
  .thumbs::-webkit-scrollbar-thumb {
    background: var(--muted);
    border-radius: 2px;
  }
  .thumb { 
    padding: 2px; 
    flex-shrink: 0;
    width: 45px;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    margin: 0 1px;
    border: 1px solid var(--border);
    background: #0b0e13;
    border-radius: 6px;
  }
  .thumb canvas { 
    aspect-ratio: 0.7; 
    width: 100%;
    max-width: 35px;
    height: auto;
    display: block;
    border-radius: 3px;
  }
  .thumb .pg { 
    font-size: 8px; 
    margin-top: 2px; 
    text-align: center;
    color: var(--muted);
    width: 100%;
    line-height: 1;
  }
  
  main { grid-row: 3; }
  #viewerWrap { padding: 12px; }
  #pdfCanvas { border-radius: 8px; }
  
  /* 2장 보기 모바일 최적화 */
  .dual-page-view { 
    flex-direction: column; 
    gap: 10px; 
    align-items: center;
  }
  .dual-page-view .page-container { width: 100%; }
  .dual-page-view #pdfCanvas { 
    border-radius: 12px; 
    border: 1px solid var(--border);
    border-left: 1px solid var(--border);
  }
  .dual-page-view #pdfCanvas2 { 
    border-radius: 12px; 
    border: 1px solid var(--border);
    border-right: 1px solid var(--border);
  }
  .dual-page-view::before,
  .dual-page-view::after { display: none; }
  
  /* 모바일에서 일부 버튼 숨김 */
  .mobile-hidden { display: none; }
  
  /* 모바일 클릭 영역 */
  .click-area { width: 60px; }
  
  /* 모바일 로딩 화면 */
  .loading {
    min-height: 300px !important;
    gap: 15px !important;
  }
  .spinner {
    width: 40px !important;
    height: 40px !important;
    border: 3px solid var(--border) !important;
    border-top: 3px solid var(--accent) !important;
  }
  .bar {
    height: 6px !important;
    width: 90% !important;
    max-width: 300px !important;
  }
}

@media (max-width: 480px) {
  header { padding: 10px; }
  header .btn, header input, header select { 
    height: 32px; padding: 0 6px; font-size: 11px;
    min-height: 32px;
    min-width: 32px;
  }
  header .sep { display: none; }
  header .info { display: none; }
  
  aside { max-height: 80px; }
  .panel { padding:6px 3px 6px 6px; }
  .thumbs { grid-template-columns: repeat(auto-fill, minmax(50px, 1fr)); }
  
  #viewerWrap { padding: 8px; }
  
  /* 매우 작은 화면에서는 추가 버튼 숨김 */
  #rotateBtn, #fitWidthBtn, #fitHeightBtn, #dualPageBtn, #themeBtn { 
    display: none; 
  }
  #animationSelect { display: none; }
}

/* 터치 디바이스 최적화 */
@media (hover: none) and (pointer: coarse) {
  .btn { min-height: 44px; } /* 터치 타겟 최소 크기 */
  .thumb { min-height: 44px; }
  
  /* 터치 시 호버 효과 제거 */
  .btn:hover, .thumb:hover { 
    background: inherit; 
    transform: none; 
  }
  
  /* 터치 피드백 */
  .btn:active, .thumb:active { 
    background: var(--accent); 
    opacity: 0.8; 
  }
}
