* {
  box-sizing: border-box;
}

html, body {
  height: 100%;
  width: 100vw;
  margin: 0;
  padding: 0;
  background: #f0f4fa;
  overflow: hidden;
  font-family: 'Segoe UI', 'Apple SD Gothic Neo', sans-serif;
  user-select: none;
}

#imageContainer {
  position: fixed;
  top: 0;
  left: 0;
  width: 100vw;
  height: 100vh;
  overflow: hidden;
  touch-action: none;
  -webkit-overflow-scrolling: touch;
  background: #e8eff8;
  cursor: grab;
}

#imageContainer:active {
  cursor: grabbing;
}

#originalImage {
  position: absolute;
  transform-origin: 0 0;
  z-index: 1;
  touch-action: none;
  transition: none !important;
  image-rendering: -webkit-optimize-contrast;
  image-rendering: crisp-edges;
  -webkit-transform: translateZ(0);
  transform: translateZ(0);
  will-change: transform;
  pointer-events: none;
}

/* 로딩 인디케이터 */
.loading-indicator {
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  z-index: 100;
  background: rgba(255, 255, 255, 0.9);
  padding: 16px 24px;
  border-radius: 12px;
  font-weight: 500;
  color: #374151;
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.15);
  display: none;
  align-items: center;
  justify-content: center;
}

.loading-indicator::after {
  content: '';
  margin-left: 12px;
  width: 16px;
  height: 16px;
  border: 2px solid #3b82f6;
  border-top: 2px solid transparent;
  border-radius: 50%;
  animation: spin 1s linear infinite;
}

@keyframes spin {
  to { transform: rotate(360deg); }
}

/* 영역 선택 레이어 */
#areasLayer {
  position: absolute;
  top: 0;
  left: 0;
  width: 100vw;
  height: 100vh;
  z-index: 10;
  pointer-events: auto;
  touch-action: manipulation;
}

/* 영역 박스 - 기본 */
.area-rect {
    position: absolute;
    cursor: pointer;
    transform-origin: top left;
    border-radius: 3px;
    transition: border-color 0.2s;
    will-change: transform;
    pointer-events: all;
    box-sizing: content-box;

    /* 터치 반응성 향상 */
    touch-action: manipulation;
    -webkit-tap-highlight-color: rgba(52, 149, 235, 0.3);
}

/* 모드 1: 항상 표시 */
.area-visible {
    border: 2px solid #3495ebdd;
    background: rgba(80,180,255,0.15);
    box-shadow: 0 0 6px #3a8ace44;
}

.area-visible:hover,
.area-visible:focus {
    border-color: #f9a521;
    background: rgba(245, 190, 70, 0.2);
    box-shadow: 0 0 15px rgba(234, 187, 0, 0.5);
}

/* 모드 2: 완전 숨김 (클릭만 가능) */
.area-hidden {
    border: none;
    background: transparent;
    box-shadow: none;
}

.area-hidden:hover,
.area-hidden:focus {
    border: none;
    background: transparent;
    box-shadow: none;
    transform: none;
}

/* 모드 3: hover 시에만 어둡게 표시 */
.area-hover-only {
    border: none;
    background: transparent;
    box-shadow: none;
    transition: all 0.1s ease;
}

.area-hover-only:hover,
.area-hover-only:focus {
    background: rgba(0, 0, 0, 0.1);
    box-shadow: inset 0 0 20px rgba(0, 0, 0, 0.2);
    backdrop-filter: blur(2px);
}

/* 터치 디바이스에서 hover-only 모드 활성화 스타일 */
.area-hover-only:active {
    background: rgba(0, 0, 0, 0.6);
    box-shadow: inset 0 0 20px rgba(0, 0, 0, 0.3);
    backdrop-filter: blur(2px);
}

/* 모바일에서 터치 영역 강화 */
@media (max-width: 768px) {
    .area-rect {
        -webkit-transform: translateZ(0);
        transform: translateZ(0);
    }
    .area-visible {
        border-width: 3px;
    }
    .welcome-modal {
        align-items: stretch;
    }
    .welcome-messages {
        display: flex !important;
        flex-direction: column;
        gap: 20px;
        max-width: 100%;
        width: 100%;
        padding: 0 16px;
        box-sizing: border-box;
    }
    .welcome-message {
        width: 100%;
        max-width: none;
        margin: 0;
    }
}

/* 미니맵 */
#minimap.minimap {
  width: 120px;
  height: 80px;
  background: rgba(248, 251, 254, 0.95);
  border: 2px solid #afcafd;
  border-radius: 10px;
  box-shadow: 0 4px 12px rgba(73, 108, 206, 0.2);
  position: fixed;
  right: 20px;
  bottom: 80px;
  z-index: 12;
  overflow: hidden;
  backdrop-filter: blur(5px);
}

#minimap img {
  width: 100%;
  height: 100%;
  object-fit: contain;
  filter: grayscale(0.1) brightness(1.1);
}

#minimap > div {
  width: 100%;
  height: 100%;
  position: relative;
}

#minimap .viewport-rect {
  border: 2px solid #2aafff;
  position: absolute;
  background: rgba(255, 255, 255, 0.2);
  z-index: 2;
  pointer-events: none;
}

/* 확대/축소 컨트롤 */
.zoom-controls {
  position: fixed;
  right: 20px;
  top: 80px;
  z-index: 15;
  display: flex;
  flex-direction: column;
  gap: 8px;
  background: rgba(255, 255, 255, 0.95);
  border-radius: 12px;
  box-shadow: 0 4px 16px rgba(133, 179, 235, 0.2);
  padding: 8px;
  backdrop-filter: blur(10px);
}

.zoom-controls button {
  background: linear-gradient(135deg, #3897f0, #2574c4);
  color: #fff;
  border: none;
  border-radius: 8px;
  padding: 8px 12px;
  font-size: 16px;
  font-weight: 600;
  cursor: pointer;
  transition: all 0.2s ease;
  box-shadow: 0 2px 8px rgba(37, 116, 196, 0.3);
  min-width: 40px;
  height: 40px;
}

.zoom-controls button:hover {
  background: linear-gradient(135deg, #2574c4, #1a5c9e);
  transform: translateY(-1px);
  box-shadow: 0 4px 12px rgba(37, 116, 196, 0.4);
}

.zoom-controls button:active {
  transform: translateY(0);
}

/* 갤러리 네비게이션 */
.gallery-nav {
  position: fixed;
  left: 50%;
  top: 20px;
  z-index: 21;
  transform: translateX(-50%);
  background: rgba(234, 242, 252, 0.95);
  border-radius: 12px;
  box-shadow: 0 4px 16px rgba(21, 64, 128, 0.15);
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 8px 16px;
  backdrop-filter: blur(10px);
}

.gallery-nav button {
  background: linear-gradient(135deg, #2987e1, #1a5c9e);
  color: #fff;
  border: none;
  border-radius: 8px;
  font-size: 18px;
  width: 36px;
  height: 36px;
  cursor: pointer;
  font-weight: bold;
  transition: all 0.2s ease;
  display: flex;
  align-items: center;
  justify-content: center;
}

.gallery-nav button:hover:not(:disabled) {
  background: linear-gradient(135deg, #1a5c9e, #144a7c);
  transform: scale(1.05);
}

.gallery-nav button:disabled {
  background: #a8c8e5;
  cursor: not-allowed;
  opacity: 0.6;
}

#galleryTitle {
  font-weight: 600;
  color: #184e84;
  letter-spacing: 0.02em;
  font-size: 16px;
  margin: 0 8px;
  white-space: nowrap;
}

/* 모달 */
.modal {
  position: fixed;
  z-index: 9999;
  left: 0;
  top: 0;
  right: 0;
  bottom: 0;
  background: rgba(0, 0, 0, 0.8);
  display: flex;
  align-items: center;
  justify-content: center;
  backdrop-filter: blur(5px);
}

.modal-content {
  background: #fff;
  padding: 24px;
  border-radius: 16px;
  min-width: 320px;
  max-width: 90vw;
  max-height: 90vh;
  box-shadow: 0 12px 40px rgba(34, 64, 102, 0.3);
  position: relative;
  text-align: center;
  overflow-y: auto;
}

.modal-content img {
  max-width: 100%;
  max-height: 60vh;
  margin-bottom: 16px;
  border-radius: 12px;
  box-shadow: 0 4px 16px rgba(34, 71, 125, 0.2);
  background: #f0f4fa;
  transition: opacity 0.3s ease;
}

.modal-loading {
  display: none;
  color: #64748b;
  font-size: 14px;
  margin: 16px 0;
  font-style: italic;
}

.close {
  position: absolute;
  right: 16px;
  top: 16px;
  font-size: 28px;
  color: #7683b6;
  cursor: pointer;
  font-weight: bold;
  transition: color 0.2s ease;
}

.close:hover {
  color: #2987e1;
}

.modal-nav {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 16px;
  font-size: 16px;
  margin-bottom: 16px;
}

.modal-nav button {
  background: linear-gradient(135deg, #0693ff, #0569cc);
  color: #fff;
  border: none;
  border-radius: 8px;
  padding: 8px 16px;
  font-size: 14px;
  transition: all 0.2s ease;
  cursor: pointer;
  font-weight: 500;
}

.modal-nav button:disabled {
  background: #98c4e1;
  cursor: not-allowed;
  opacity: 0.6;
}

.modal-nav button:hover:enabled {
  background: linear-gradient(135deg, #0569cc, #044a9e);
  transform: translateY(-1px);
}

.modal-message {
  margin-bottom: 8px;
  font-weight: 500;
  color: #324174;
  line-height: 1.4;
  font-size: 16px;
}

/* 환영 모달 */
 .welcome-modal {
   position: fixed;
   top: 0;
   left: 0;
   right: 0;
   bottom: 0;
   z-index: 20000;
   background: rgba(65, 80, 125, 0.8);
   display: flex;
   flex-direction: column;
   align-items: center;
   justify-content: flex-start;
   animation: fadeIn 0.4s ease;
   backdrop-filter: blur(3px);
   overflow-y: auto;
   -webkit-overflow-scrolling: touch;
   overscroll-behavior-y: contain;
   padding-top: calc(24px + env(safe-area-inset-top));
   padding-bottom: calc(24px + env(safe-area-inset-bottom));
 }

@keyframes fadeIn {
  from { opacity: 0; transform: scale(0.9); }
  to { opacity: 1; transform: scale(1); }
}

.welcome-messages {
  display: grid;
  grid-template-columns: repeat(2, minmax(280px, 1fr));
  gap: 20px;
  justify-items: center;
  align-items: start;
  max-width: min(960px, 90vw);
}

.welcome-message {
  background: #fff;
  padding: 24px 20px;
  border-radius: 16px;
  min-width: 280px;
  max-width: 350px;
  box-shadow: 0 8px 32px rgba(20, 132, 252, 0.2);
  text-align: left;
}

.welcome-message h2 {
  font-size: 18px;
  color: #266baf;
  margin-top: 0;
  margin-bottom: 12px;
}

.welcome-message p {
  font-size: 14px;
  margin: 8px 0;
  line-height: 1.5;
  color: #444;
}

.welcome-message ul {
  margin: 8px 0;
  padding-left: 20px;
}

.welcome-message li {
  font-size: 14px;
  margin: 4px 0;
  color: #555;
}

.welcome-btns {
  margin: 24px auto 0 auto;
  display: flex;
  gap: 16px;
  justify-content: center;
}

.welcome-btns button {
  font-size: 14px;
  padding: 10px 20px;
  border-radius: 8px;
  border: none;
  background: linear-gradient(135deg, #347adb, #2563a8);
  color: #fff;
  font-weight: 500;
  box-shadow: 0 4px 12px rgba(42, 71, 119, 0.3);
  cursor: pointer;
  transition: all 0.2s ease;
}

.welcome-btns button:hover {
  background: linear-gradient(135deg, #2563a8, #1a4c86);
  transform: translateY(-1px);
}

/* 모바일 최적화 */
@media (max-width: 768px) {
  #minimap {
      width: 80px;
      height: 50px;
      right: 15px;
      bottom: 60px;
  }
  
  .zoom-controls {
      right: 15px;
      top: 60px;
      padding: 6px;
      gap: 6px;
  }
  
  .zoom-controls button {
      min-width: 36px;
      height: 36px;
      font-size: 14px;
  }
  
  .gallery-nav {
      top: 15px;
      font-size: 14px;
      padding: 6px 12px;
  }
  
  .gallery-nav button {
      width: 32px;
      height: 32px;
      font-size: 16px;
  }
  
  #galleryTitle {
      font-size: 14px;
  }
  
  .modal-content {
      margin: 20px;
      padding: 20px;
      max-width: calc(100vw - 40px);
  }
  
  .welcome-messages {
      flex-direction: column;
      align-items: center;
      gap: 16px;
  }
  
  .welcome-message {
      min-width: 260px;
      max-width: calc(100vw - 40px);
      margin: 0 20px;
  }
}

/* 성능 최적화 */
.area-rect {
  contain: layout style paint;
}

#originalImage {
  contain: layout;
}

/* 스크롤바 숨기기 */
body::-webkit-scrollbar {
  width: 0;
  height: 0;
}
