/* 기본 스타일 - 전체 화면 및 어두운 무채색 */
html, body {
    margin: 0;
    padding: 0;
    width: 100%;
    height: 100%;
    overflow: hidden;
    font-family: Arial, sans-serif;
    background-color: #1a1a1a;
    color: #e0e0e0;
}

.viewer-container {
    width: 100%;
    height: 100%;
    background-color: #1a1a1a; 
    display: flex;
    flex-direction: column;
}

/* 컨트롤 영역 (헤더) - 가로 스크롤 활성화 */
.controls {
    display: flex;
    flex-wrap: nowrap; /* 줄바꿈 방지 */
    gap: 20px;
    align-items: center;
    padding: 8px 16px;
    background-color: #2a2a2a;
    border-bottom: 1px solid #444;
    flex-shrink: 0;
    font-size: 14px;
    z-index: 10;
    overflow-x: auto; /* 가로 스크롤 활성화 */
    overflow-y: hidden;
    -webkit-overflow-scrolling: touch; /* 모바일 스크롤 부드럽게 */
}

/* 스크롤바 숨기기 */
.controls::-webkit-scrollbar {
    display: none; /* Chrome, Safari, Opera */
}
.controls {
    -ms-overflow-style: none;  /* IE, Edge */
    scrollbar-width: none;  /* Firefox */
}

/* --- 뷰 모드 아이콘 버튼 스타일 시작 --- */

/* 그룹 컨테이너 */
.view-mode-group,
.zoom-mode-group { 
    display: flex;
    gap: 8px;
    background: transparent;
    padding: 0;
    align-items: center;
    flex-shrink: 0; /* 축소 방지 */
}

/* 읽기 방향 & 클릭 방향 & 줌 모드 그룹 - 구분선 및 간격 처리 */
.reading-direction-group,
.click-direction-group,
.zoom-mode-group { 
    display: flex; 
    gap: 8px;
    align-items: center;
    padding-left: 20px;
    margin-left: 0;
    border-left: 1px solid #555; /* 구분선 */
    flex-shrink: 0; /* 축소 방지 */
}

/* 기본 숨김 (JS로 제어) */
.reading-direction-group,
.zoom-mode-group { 
    display: none; 
}


/* 개별 아이템 래퍼 */
.view-mode-item {
    position: relative;
    display: flex;
    align-items: center;
}

/* 실제 라디오 버튼 숨김 */
.view-mode-item input[type="radio"] {
    display: none;
}

/* 라디오 버튼 라벨 (아이콘 버튼 역할) */
.view-mode-item label {
    display: flex;
    justify-content: center;
    align-items: center;
    width: 40px;
    height: 36px;
    background-color: #333;
    border: 1px solid #444;
    border-radius: 4px;
    cursor: pointer;
    transition: all 0.2s ease;
    padding: 4px;
    box-sizing: border-box;
    margin: 0; 
}

/* SVG 아이콘 기본 스타일 */
.view-mode-item svg.icon,
.control-btn svg.icon {
    width: 100%;
    height: 100%;
    color: #a0a0a0; /* 기본 회색 */
    pointer-events: none;
}

/* 마우스 오버 효과 */
.view-mode-item label:hover {
    background-color: #444;
    border-color: #555;
}
.view-mode-item label:hover svg.icon {
    color: #e0e0e0;
}

/* 선택된(Checked) 상태 효과 */
.view-mode-item input[type="radio"]:checked + label {
    background-color: #555;
    border-color: #777;
    box-shadow: 0 0 4px rgba(0,0,0,0.3);
}

.view-mode-item input[type="radio"]:checked + label svg.icon {
    color: #fff; /* 흰색으로 강조 */
    filter: drop-shadow(0 0 2px rgba(255,255,255,0.3));
}

/* --- 뷰 모드 아이콘 버튼 스타일 끝 --- */

/* 페이지네이션 그룹 */
.pagination-group {
    display: flex;
    align-items: center;
    gap: 10px;
    flex-shrink: 0; /* 축소 방지 */
}

/* 기타 체크박스 라벨 공통 */
.controls > label {
    cursor: pointer;
    display: flex;
    align-items: center;
    gap: 5px;
    margin: 0;
}

select {
    padding: 5px 10px;
    background-color: #3a3a3a;
    color: #f0f0f0;
    border: 1px solid #555;
    border-radius: 4px;
    outline: none;
    white-space: nowrap; /* 텍스트 줄바꿈 방지 */
}

/* 유틸리티 버튼 그룹 (도움말 + 전체화면) */
.utility-button-group {
    display: flex;
    gap: 8px;
    align-items: center;
    padding-left: 20px;
    margin-left: auto; /* 우측 정렬 */
    border-left: 1px solid #555; /* 구분선 */
    flex-shrink: 0; /* 축소 방지 */
}

/* 컨트롤 버튼 (도움말, 전체화면) 공통 스타일 */
.control-btn {
    display: flex;
    justify-content: center;
    align-items: center;
    width: 40px; 
    height: 36px; 
    padding: 6px; 
    box-sizing: border-box;
    background-color: #333;
    color: #a0a0a0;
    border: 1px solid #444;
    border-radius: 4px;
    cursor: pointer;
    transition: all 0.2s ease;
}

.control-btn:hover {
    background-color: #444;
    border-color: #555;
}

.control-btn:hover svg.icon {
    color: #e0e0e0;
}

/* 전체 화면 활성화 상태일 때 (선택사항) */
.fullscreen-button.active {
    background-color: #555;
    border-color: #777;
    color: #fff;
}
.fullscreen-button.active svg.icon {
    color: #fff;
}

/* 이미지 뷰어 영역 */
.image-viewer {
    position: relative;
    width: 100%;
    flex-grow: 1;
    background-color: #000;
    overflow: hidden; 
    user-select: none; 
    -webkit-overflow-scrolling: touch;
}

/* 세로 스크롤 모드에서는 터치 액션 허용 */
.image-viewer.mode-vertical {
    touch-action: pan-y;
    overflow-y: auto;
}

/* 전체 화면 모드일 때 헤더 숨기기 */
:fullscreen .controls { display: none; }
:-webkit-full-screen .controls { display: none; }
:-moz-full-screen .controls { display: none; }
:-ms-fullscreen .controls { display: none; }

/* 이미지 컨테이너 공통 */
.image-container {
    width: 100%;
    height: 100%;
    display: flex;
    justify-content: center;
    align-items: center;
    transform: translateX(0); 
}

/* --- [수정됨] 너비 맞춤(Zoom-Fit-Width) 시 상단 잘림 해결 --- */
/* justify-content는 좌우 정렬, align-items는 상하 정렬(flex-direction: row 기준) */
.image-container.zoom-fit-width {
    align-items: flex-start; /* 중요: 이미지가 커져도 위쪽부터 시작하게 함 (center면 위가 잘림) */
    height: auto;            /* 컨테이너 높이를 내용물에 맞게 확장 */
    min-height: 100%;        /* 최소 높이는 화면 전체 */
    overflow-y: visible;     /* 내부 스크롤 허용 */
}

.image-container img {
    max-width: 100%;
    max-height: 100%;
    object-fit: contain; /* 기본: 높이 맞춤 (fit-height) */
    display: block;
    pointer-events: none; 
}

/* 너비 맞춤 모드 (fit-width) */
.image-container.zoom-fit-width img {
    max-width: 100%;
    max-height: none; /* 높이 제한 해제 */
    width: 100%;      /* 너비를 뷰어 너비에 맞춤 */
    height: auto;     /* 높이는 비율에 맞게 조정 */
    object-fit: fill; /* contain 대신 fill을 사용하여 width: 100%를 보장 */
}

/* --- 모드별 스타일 --- */

/* 1. 한 장 보기 (Single) */
.single-view {
    flex-direction: row;
    position: relative;
    width: 100%;
    height: 100%;
}

.single-view img {
    position: absolute; 
    left: 0;
    width: 100%;
    height: 100%;
}

/* 1.1. 한 장 보기 + 너비 맞춤 */
.single-view.zoom-fit-width img {
    /* 너비 맞춤에서는 스크롤이 필요하므로 static으로 변경 */
    height: auto;
    position: static; /* absolute 해제하여 flow 따르게 함 */
    max-height: none;
    width: 100%;
    object-fit: fill;
}
/* 너비 맞춤 시 한 장 보기 컨테이너 */
.single-view.zoom-fit-width {
    display: flex;
    flex-direction: column; /* 세로 스크롤을 위해 column으로 변경 */
    justify-content: flex-start; /* 위쪽 정렬 */
}



/* 2. 두 장 보기 (Double) */
.double-view {
    flex-direction: row;
    gap: 0;
    justify-content: center; 
}

.double-view img {
    width: 50%;
    height: 100%;
    object-fit: contain;
}

/* 2.1. 두 장 보기 + 너비 맞춤 */
.double-view.zoom-fit-width img {
    height: auto;
    max-height: none;
    width: 50%;
    object-fit: fill;
}

/* 너비 맞춤 시 두 장 보기 컨테이너 */
.double-view.zoom-fit-width {
    align-items: flex-start; /* 상단 정렬 필수 */
}

/* 기본값: 왼쪽 정렬, 오른쪽 정렬 (LTR 기준) */
.double-view img:first-child {
    object-position: right center;
}

.double-view img:nth-child(2) {
    object-position: left center;
}

/* 두 장 보기에서 너비 맞춤일 때 정렬 위치 조정 */
.double-view.zoom-fit-width img:first-child,
.double-view.zoom-fit-width img:nth-child(2) {
    object-position: top center; /* 상단 기준 정렬 */
}


.double-view.single-item img {
    width: 50%;
    margin-right: 50%; 
}

/* 3. 세로 스크롤 (Vertical) */
.vertical-view {
    flex-direction: column;
    justify-content: flex-start;
    align-items: center; /* 이미지를 중앙 정렬 */
    height: auto;
    min-height: 100%;
    padding-bottom: 50px; /* 맨 아래 여백 추가 */
}

.vertical-view img {
    /* 모바일 기본값: 가로 폭에 맞춤 (기존 유지) */
    width: 100%;
    max-width: 100%;
    height: auto; 
    margin-bottom: 0;
    display: block;
    object-fit: fill; /* vertical 모드는 너비 맞춤이 기본이므로 fill 사용 */
}

/* 3.1. 세로 스크롤 + 높이 맞춤 (특이 케이스 - Height Fit) */
.vertical-view.zoom-fit-height img {
    width: auto;
    max-width: 100%;
    height: 100vh; /* 뷰어 높이 대신 뷰포트 높이에 맞춤 */
    max-height: 100vh;
    object-fit: contain; 
    margin: 0 auto;
}
.image-viewer.mode-vertical .vertical-view.zoom-fit-height {
    overflow-y: auto;
}


/* --- PC 및 태블릿 최적화 --- */
@media (min-width: 768px) {
    .vertical-view img {
        width: auto;         
        max-width: 100%;     
        max-height: none;    
        margin: 0 auto;      
        height: auto;        
    }

    /* PC에서 세로 모드 + 높이 맞춤 */
    .vertical-view.zoom-fit-height img {
        height: 100vh;
        max-height: 100vh;
        width: auto;
        max-width: 100%;
        object-fit: contain;
    }
}

/* 클릭 영역 */
.click-area {
    position: absolute;
    top: 0;
    width: 50%; 
    height: 100%;
    cursor: pointer;
    z-index: 5;
}

.image-viewer.mode-vertical .click-area {
    display: none;
}

/* 너비 맞춤 시 클릭 이동 비활성화를 위한 CSS 처리 */
.image-viewer.zoom-fit-width .click-area {
    display: none; 
}


.left-area { left: 0; }
.right-area { right: 0; }

/* --- 도움말 오버레이 스타일 --- */
.help-overlay {
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background-color: rgba(0, 0, 0, 0.85); /* 배경 어둡게 */
    display: none; /* 기본 숨김 */
    justify-content: center;
    align-items: center;
    z-index: 9999;
}

.help-overlay.active {
    display: flex;
}

.help-content {
    background-color: #2a2a2a;
    border: 1px solid #444;
    border-radius: 8px;
    width: 90%;
    max-width: 650px; /* 너비 약간 증가 */
    max-height: 90%;
    overflow-y: hidden;
    box-shadow: 0 4px 12px rgba(0, 0, 0, 0.5);
    color: #e0e0e0;
    display: flex;
    flex-direction: column;
}

.help-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 15px 20px;
    border-bottom: 1px solid #444;
    background-color: #333;
    flex-shrink: 0;
    top: 0;
    z-index: 10;
}

.help-header h2 {
    margin: 0;
    font-size: 18px;
    font-weight: 600;
}

.close-btn {
    background: none;
    border: none;
    color: #a0a0a0;
    cursor: pointer;
    width: 32px;
    height: 32px;
    padding: 4px;
}
.close-btn:hover {
    color: #fff;
    background-color: #444;
    border-radius: 4px;
}

.help-body {
    padding: 20px;
    overflow-y: auto;
    flex-grow: 1;
}

.help-section {
    margin-bottom: 24px;
}

.help-section h3 {
    margin-top: 0;
    margin-bottom: 12px;
    font-size: 16px;
    color: #fff;
    border-bottom: 1px solid #444;
    padding-bottom: 8px;
}

/* 도움말 테이블 (아이콘 + 설명) */
.help-table {
    width: 100%;
    border-collapse: collapse;
}

.help-table td {
    padding: 10px 8px;
    border-bottom: 1px solid #3a3a3a;
    font-size: 14px;
    vertical-align: middle;
}

.help-table tr:last-child td {
    border-bottom: none;
}

/* 아이콘 셀 */
.help-table .icon-cell {
    width: 50px; /* 아이콘 영역 너비 고정 */
    text-align: center;
}

.help-icon-wrapper {
    width: 36px;
    height: 32px;
    background-color: #333;
    border: 1px solid #444;
    border-radius: 4px;
    padding: 4px;
    box-sizing: border-box;
    display: inline-flex;
    justify-content: center;
    align-items: center;
}

.help-icon-wrapper svg.icon {
    width: 100%;
    height: 100%;
    color: #ccc;
}

/* 단축키 테이블 */
.shortcut-table {
    width: 100%;
    border-collapse: collapse;
}

.shortcut-table td {
    padding: 8px;
    border-bottom: 1px solid #3a3a3a;
    font-size: 14px;
}

.shortcut-table tr:last-child td {
    border-bottom: none;
}

.shortcut-table .key {
    font-weight: bold;
    color: #4db8ff; /* 키 포인트 컬러 */
    white-space: nowrap;
    width: 80px;
}

.shortcut-table .desc {
    color: #ccc;
}

/* 모바일 최적화 */
@media (max-width: 768px) {
    .controls {
        font-size: 12px;
        padding: 4px 8px;
        gap: 10px;
    }
    
    .view-mode-group, 
    .reading-direction-group,
    .click-direction-group,
    .zoom-mode-group,
    .utility-button-group {
        gap: 4px;
    }

    .reading-direction-group,
    .click-direction-group,
    .zoom-mode-group,
    .utility-button-group {
        padding-left: 10px; /* 모바일에서 간격 축소 */
    }

    .view-mode-item label,
    .control-btn { /* 버튼 크기 조정 */
        width: 36px;
        height: 32px;
        padding: 3px;
    }
    
    select {
        padding: 4px 8px;
        font-size: 12px;
    }
    
    /* 도움말 창 크기 조정 */
    .help-content {
        width: 95%;
        max-height: 85%;
    }
    
    .help-table td, 
    .shortcut-table td {
        font-size: 13px;
        padding: 8px 4px;
    }
}