:root {
    --page-width: 210mm;
    --page-height: 297mm;
    --border-color: #000000;
    --outer-border: 2px;
    --inner-border: 0.5px;
    --primary-color: #4CAF50;
    --secondary-color: #2196F3;
    --accent-color: #e74c3c;
    --bg-dark: #f0f2f5;
    --sidebar-width: 310px;
}

* {
    box-sizing: border-box;
}

body {
    margin: 0;
    padding: 0;
    background: var(--bg-dark);
    font-family: 'Pretendard', -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
}

/* 새로운 상단 헤더 */
.app-header {
    height: 60px;
    background: white;
    border-bottom: 1px solid #e0e0e0;
    display: flex;
    align-items: center;
    padding: 0 20px;
    justify-content: space-between;
    position: sticky;
    top: 0;
    z-index: 1000;
    box-shadow: 0 2px 4px rgba(0, 0, 0, 0.05);
}

.header-left {
    display: flex;
    align-items: center;
    gap: 15px;
}

.header-center {
    position: absolute;
    left: 50%;
    transform: translateX(-50%);
}

.header-right {
    display: flex;
    align-items: center;
    gap: 8px;
}

.logo {
    font-size: 1.2rem;
    font-weight: 800;
    color: #333;
    cursor: pointer;
    display: flex;
    align-items: center;
    gap: 5px;
}

.logo span {
    color: var(--primary-color);
}

.project-title-display {
    font-size: 1rem;
    font-weight: 600;
    color: #555;
    border-bottom: 1px dashed #ccc;
    padding: 2px 5px;
    cursor: pointer;
}

.btn-action {
    height: 38px;
    padding: 0 15px;
    border-radius: 6px;
    border: 1px solid #ddd;
    background: white;
    color: #333;
    font-size: 0.9rem;
    font-weight: 600;
    cursor: pointer;
    display: flex;
    align-items: center;
    gap: 6px;
    transition: all 0.2s;
}

.btn-action:hover {
    background: #f9f9f9;
    border-color: #ccc;
}

.btn-action-sm {
    flex: 0 0 auto;
    width: 28px;
    height: 28px;
    border-radius: 4px;
    border: 1px solid #ddd;
    background: white;
    color: #555;
    font-size: 1rem;
    font-weight: 700;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    padding: 0;
    transition: all 0.2s;
}

.btn-action-sm:hover {
    background: var(--primary-color);
    color: white;
    border-color: var(--primary-color);
}

.btn-action.primary {
    background: var(--primary-color);
    border-color: var(--primary-color);
    color: white;
}

.btn-action.primary:hover {
    background: #45a049;
}

.btn-action.blue {
    background: var(--secondary-color);
    border-color: var(--secondary-color);
    color: white;
}

.btn-action.red {
    background: var(--accent-color);
    border-color: var(--accent-color);
    color: white;
}

/* 레이아웃 구성 */
#photo-layout-app {
    display: flex;
    height: calc(100vh - 60px);
    overflow: hidden;
}

/* 사이드바 스타일 개선 */
#photo-sidebar {
    width: var(--sidebar-width);
    background: white;
    border-right: 1px solid #e0e0e0;
    color: #333;
    padding: 12px;
    overflow-y: auto;
    flex-shrink: 0;
    display: flex;
    flex-direction: column;
    gap: 12px;
}

.sidebar-section-title {
    font-size: 0.95rem;
    font-weight: 700;
    color: #1a1a1a;
    margin-bottom: 8px;
    display: flex;
    align-items: center;
    gap: 8px;
}

.sidebar-section-title i {
    font-size: 1.1rem;
    color: var(--primary-color);
}

.photo-control-group {
    margin-bottom: 0;
}

.photo-control-group label {
    display: block;
    font-size: 0.8rem;
    font-weight: 700;
    margin-bottom: 4px;
    color: #666;
    text-transform: uppercase;
}

.photo-control-group input,
.photo-control-group select {
    width: 100%;
    padding: 8px;
    border-radius: 6px;
    border: 1px solid #ddd;
    font-size: 0.9rem;
    background: #fafafa;
}

.photo-control-group input:focus {
    border-color: var(--primary-color);
    outline: none;
    background: white;
}

/* 세그먼트형 버튼 그룹 */
.btn-group {
    display: flex;
    background: #eee;
    padding: 3px;
    border-radius: 8px;
    width: 100%;
}

.btn-group button {
    flex: 1;
    padding: 6px;
    border: none;
    background: transparent;
    color: #666;
    border-radius: 6px;
    cursor: pointer;
    font-size: 0.85rem;
    font-weight: 600;
    transition: all 0.2s;
}

.btn-group button.active {
    background: white;
    color: #333;
    box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
}

/* 사진 리스트 영역 - 부모 스크롤 사용을 위해 flex만 유지 */
.sidebar-scroll-content {
    flex: 0 0 auto;
}

/* 사진 추가 영역 */
#photo-drop-zone {
    border: 2px dashed #ccc;
    padding: 18px 12px;
    text-align: center;
    cursor: pointer;
    background: #fafafa;
    border-radius: 12px;
    transition: all 0.3s;
    margin-bottom: 10px;
}

#photo-drop-zone:hover {
    border-color: var(--primary-color);
    background: #f0f7f0;
}

#photo-drop-zone p {
    margin: 0;
    font-weight: 700;
    color: #555;
    font-size: 1rem;
}

/* 미리보기 프레임 */
#photo-preview-frame {
    flex: 1;
    overflow: auto;
    padding: 50px;
    display: flex;
    flex-direction: column;
    align-items: center;
    background: #525659;
}

#photo-preview-container {
    display: flex;
    flex-direction: column;
    gap: 40px;
    align-items: center;
    padding-bottom: 50px;
}

.photo-page-wrapper {
    transform-origin: top center;
    transition: transform 0.2s;
    box-shadow: 0 10px 30px rgba(0, 0, 0, 0.5);
}

/* 페이지 스타일 */
.photo-pdf-page {
    width: var(--page-width);
    height: var(--page-height);
    background: white;
    display: flex;
    flex-direction: column;
    position: relative;
}

.photo-doc-header {
    text-align: center;
    border-bottom: 3px solid #000;
    margin-bottom: 10px;
    flex-shrink: 0;
    min-height: 60px;
    /* Allow height to grow */
    display: flex;
    flex-direction: column;
    /* Stack items vertically */
    align-items: center;
    justify-content: center;
    padding: 5px 0;
    /* Add some padding */
}

.photo-grid {
    display: grid;
    gap: 0;
    width: 100%;
    height: 100%;
    flex-grow: 1;
    min-height: 0;
    box-sizing: border-box;
    border: none;
    /* [수정] 2px 테두리가 그리드 외곽에서 잘리지 않도록 함 */
    padding: 0;
    background: transparent;
}

.photo-cell {
    /* [수정] JS에서 동적으로 border-width를 조절하므로 기본 설정만 유지 */
    display: flex;
    flex-direction: column;
    overflow: hidden;
    background: white;
    height: 100%;
    box-sizing: border-box;
    position: relative;
}

/* Ensure the first row and column also show borders if needed */


.photo-img-wrapper {
    flex: 1;
    display: flex;
    justify-content: center;
    align-items: center;
    background: #fff;
    overflow: hidden;
    position: relative;
}

.photo-img-wrapper:hover .photo-edit-controls {
    opacity: 1;
}

.photo-cell img {
    width: auto;
    height: auto;
    max-width: 100%;
    max-height: 100%;
    object-fit: contain;
    transition: transform 0.2s;
}

.photo-cell-empty {
    border: none !important;
    background: transparent !important;
    height: 100%;
}

/* 사진 위 플로팅 컨트롤 */
.photo-edit-controls {
    position: absolute;
    top: 10px;
    right: 10px;
    display: flex;
    gap: 5px;
    opacity: 1;
    /* 상시 노출로 변경하여 시인성 확보 */
    z-index: 10;
}

.btn-edit-tool {
    width: 32px;
    height: 32px;
    border-radius: 4px;
    border: none;
    background: rgba(0, 0, 0, 0.7);
    color: white;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 0.9rem;
}

.btn-edit-tool:hover {
    background: var(--primary-color);
}

.btn-edit-tool.yellow {
    background: #fbc02d;
    color: #333;
    font-weight: bold;
    border: 1px solid #f9a825;
}

.btn-edit-tool.yellow:hover {
    background: #f9a825;
}

.btn-edit-tool.blue {
    background: #2196F3;
    color: white;
    font-weight: bold;
    border: 1px solid #1976D2;
}

.btn-edit-tool.blue:hover {
    background: #1976D2;
}

.btn-edit-tool.black {
    background: #2d2d2d;
    color: white;
    border: 1px solid #000;
}

.btn-edit-tool.black:hover {
    background: #000;
}

.btn-edit-tool.remove:hover {
    background: var(--accent-color);
}

.photo-cell textarea,
.caption-text {
    width: 100%;
    height: 100%;
    border: none;
    resize: none;
    outline: none;
    background: transparent;
    font-family: inherit;
    overflow: hidden;
}

/* 수직 중앙 정렬을 위한 contenteditable 스타일 */
.caption-text {
    display: flex;
    align-items: center;
    justify-content: center;
    white-space: pre-wrap;
    /* 줄바꿈 허용 */
    word-break: break-all;
    line-height: 1.4;
}

.caption-text:empty::before {
    content: attr(data-placeholder);
    color: #aaa;
    pointer-events: none;
    display: block;
}

/* 고도화된 캡션 컨테이너 */
.caption-container {
    display: flex;
    flex-wrap: wrap;
    /* 2x2 격자 지원 */
    height: 80px;
    width: 100%;
    flex: none;
    /* [수정] 내부 구분선은 얇게 설정 */
    border-top: 1px solid #000;
    background: #eee;
    position: relative;
    overflow: hidden;
}

.caption-cell {
    box-sizing: border-box;
    background: white;
    display: flex;
    align-items: center;
    justify-content: center;
    flex-direction: column;
    position: relative;
    min-height: 40px;
    /* 2x2일 때 각 행의 최소 높이 */
}

/* 레이아웃 빌더 모달 */
.modal-overlay {
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background: rgba(0, 0, 0, 0.7);
    display: none;
    justify-content: center;
    align-items: center;
    z-index: 2000;
}

.builder-modal {
    background: white;
    padding: 20px;
    border-radius: 8px;
    width: 500px;
    max-width: 90%;
    box-shadow: 0 5px 30px rgba(0, 0, 0, 0.3);
}

.builder-section {
    margin-bottom: 20px;
    padding: 10px;
    border: 1px dashed #ccc;
    border-radius: 5px;
    min-height: 50px;
}

.source-tags {
    display: flex;
    gap: 8px;
    flex-wrap: wrap;
}

.tag-item {
    padding: 5px 12px;
    background: var(--primary-color);
    color: white;
    border-radius: 20px;
    cursor: move;
    font-size: 13px;
    user-select: none;
}

.target-layout {
    display: flex;
    background: #f0f0f0;
    border: 1px solid #333;
    height: 50px;
    align-items: stretch;
    position: relative;
}

.target-cell {
    background: white;
    border: 1px solid #ccc;
    display: flex;
    align-items: center;
    justify-content: center;
    position: relative;
    font-size: 12px;
    min-width: 40px;
}

.target-cell .remove-tag {
    position: absolute;
    top: -5px;
    right: -5px;
    background: red;
    color: white;
    border-radius: 50%;
    width: 15px;
    height: 15px;
    font-size: 10px;
    display: flex;
    align-items: center;
    justify-content: center;
    cursor: pointer;
}

.builder-resizer {
    width: 8px;
    /* 드래그 영역 확보 */
    background: transparent;
    cursor: col-resize;
    z-index: 110;
    position: absolute;
    transition: background 0.2s;
}

.builder-resizer:hover {
    background: rgba(33, 150, 243, 0.5);
    /* 드래그 시 피드백 */
}

.modal-footer {
    display: flex;
    justify-content: flex-end;
    gap: 10px;
}

/* 셀 설정 도구 (드롭다운) */
.cell-config-tool {
    position: absolute;
    top: 2px;
    right: 2px;
    z-index: 10;
    opacity: 0;
    transition: opacity 0.2s;
    display: flex;
    gap: 2px;
}

.caption-cell:hover .cell-config-tool {
    opacity: 1;
}

.cell-config-tool select {
    font-size: 10px;
    padding: 2px;
    border-radius: 3px;
    border: 1px solid #ccc;
    background: rgba(255, 255, 255, 0.9);
    cursor: pointer;
}

/* 리사이즈 핸들 */
.resizer-h,
.resizer-v {
    background: #666;
    z-index: 20;
    cursor: col-resize;
    transition: background 0.2s;
}

.resizer-h {
    width: 4px;
    border-left: 1px solid #fff;
    border-right: 1px solid #fff;
}

.resizer-v {
    height: 4px;
    cursor: row-resize;
    border-top: 1px solid #fff;
    border-bottom: 1px solid #fff;
}

.resizer-h:hover,
.resizer-v:hover {
    background: var(--primary-color);
}

/* 사진 관리 리스트 스타일 개선 */
#photo-image-list {
    list-style: none;
    padding: 0;
    margin-top: 5px;
    display: flex;
    flex-direction: column;
    gap: 4px;
}

#photo-image-list li {
    background: #fff;
    border: 1px solid #eee;
    padding: 6px 10px;
    border-radius: 8px;
    display: flex;
    align-items: center;
    gap: 8px;
    font-size: 0.85rem;
    color: #444;
    cursor: grab;
    box-shadow: 0 1px 3px rgba(0, 0, 0, 0.05);
}

#photo-image-list li:hover {
    border-color: #ddd;
    box-shadow: 0 2px 5px rgba(0, 0, 0, 0.1);
}

#photo-image-list img {
    width: 40px;
    height: 40px;
    border-radius: 4px;
    object-fit: cover;
    border: 1px solid #eee;
}

.list-remove-btn {
    margin-left: auto;
    background: none;
    border: none;
    color: #ccc;
    cursor: pointer;
    padding: 5px;
    font-size: 1.1rem;
}

.list-remove-btn:hover {
    color: var(--accent-color);
}

#photo-pdf-temp {
    position: fixed;
    left: -9999px;
    top: 0;
}

.sidebar-ad-wrap {
    color: #adb5bd;
    font-size: 0.75rem;
    text-align: center;
    font-weight: 600;
}

/* ============================================
   모바일 최적화 - 반응형 디자인
   ============================================ */

/* 태블릿 및 모바일 (768px 이하) */
@media (max-width: 768px) {

    /* 레이아웃을 세로 방향으로 변경 */
    #photo-layout-app {
        flex-direction: column;
        height: auto;
    }

    /* 사이드바를 상단에 배치 */
    #photo-sidebar {
        width: 100%;
        border-right: none;
        border-bottom: 2px solid #e0e0e0;
        max-height: 50vh;
        overflow-y: auto;
    }

    /* 미리보기를 하단에 배치 (스크롤 형태) */
    #photo-preview-frame {
        flex: 1;
        padding: 20px;
        min-height: 50vh;
    }

    /* 헤더 버튼 터치 최적화 */
    .btn-action {
        height: 44px;
        font-size: 0.85rem;
        padding: 0 12px;
        min-width: 44px;
    }

    /* 헤더 간격 조정 */
    .header-right {
        gap: 6px;
    }

    /* 로고 크기 조정 */
    .logo {
        font-size: 1.1rem;
    }

    /* 사이드바 섹션 간격 조정 */
    #photo-sidebar {
        gap: 10px;
        padding: 10px;
    }

    /* 버튼 그룹 터치 최적화 */
    .btn-group button {
        padding: 8px;
        font-size: 0.9rem;
        min-height: 40px;
    }

    /* 입력 필드 터치 최적화 */
    .photo-control-group input,
    .photo-control-group select {
        font-size: 16px;
        /* iOS 자동 줌 방지 */
        padding: 10px;
        min-height: 44px;
    }

    /* 작은 버튼도 터치 가능하게 */
    .btn-action-sm {
        width: 36px;
        height: 36px;
        font-size: 1.1rem;
    }

    /* 미리보기 컨테이너 간격 조정 */
    #photo-preview-container {
        gap: 30px;
        padding-bottom: 30px;
    }

    /* 모달 크기 조정 */
    .builder-modal {
        width: 95%;
        max-width: 95%;
    }
}

/* 모바일 (480px 이하) */
@media (max-width: 480px) {

    /* 헤더 재구성 */
    .app-header {
        flex-wrap: wrap;
        height: auto;
        padding: 8px;
        gap: 8px;
    }

    .header-left,
    .header-center {
        position: static;
        transform: none;
    }

    .header-center {
        order: -1;
        width: 100%;
        text-align: center;
        margin-bottom: 5px;
    }

    .header-right {
        width: 100%;
        justify-content: center;
        flex-wrap: wrap;
        gap: 5px;
    }

    /* 버튼 텍스트 축소 */
    .btn-action {
        font-size: 0.75rem;
        padding: 0 8px;
        height: 40px;
    }

    /* 언어 선택 버튼 */
    .btn-group {
        margin-right: 0 !important;
    }

    /* 로고 숨김 또는 축소 */
    .logo {
        font-size: 1rem;
    }

    .logo small {
        display: none;
    }

    /* 프로젝트 타이틀 크기 조정 */
    .project-title-display {
        font-size: 0.9rem;
    }

    /* 사이드바 너비 최대 활용 */
    #photo-sidebar {
        padding: 8px;
        gap: 8px;
    }

    /* 섹션 타이틀 크기 조정 */
    .sidebar-section-title {
        font-size: 0.85rem;
    }

    /* 그리드 입력 필드 간격 */
    .photo-control-group input[type="number"] {
        font-size: 16px;
    }

    /* 미리보기 패딩 최소화 */
    #photo-preview-frame {
        padding: 10px;
    }

    /* PDF 페이지 크기 조정 (화면에 맞게) */
    .photo-page-wrapper {
        transform: scale(0.9);
        transform-origin: top center;
    }

    /* 사진 드롭존 크기 조정 */
    #photo-drop-zone {
        padding: 12px 8px;
    }

    #photo-drop-zone p {
        font-size: 0.9rem;
    }

    /* 사진 리스트 아이템 크기 조정 */
    #photo-image-list li {
        padding: 5px 8px;
        font-size: 0.8rem;
    }

    #photo-image-list img {
        width: 35px;
        height: 35px;
    }

    /* 사진 편집 컨트롤 크기 조정 */
    .btn-edit-tool {
        width: 36px;
        height: 36px;
        font-size: 1rem;
    }

    /* 모달 버튼 크기 */
    .modal-footer .btn-action {
        font-size: 0.8rem;
        padding: 8px 10px;
        height: 42px;
    }
}

/* 초소형 모바일 (360px 이하) */
@media (max-width: 360px) {

    /* 헤더 패딩 최소화 */
    .app-header {
        padding: 5px;
    }

    /* 버튼 더 축소 */
    .btn-action {
        font-size: 0.7rem;
        padding: 0 6px;
    }

    /* 페이지 더 축소 */
    .photo-page-wrapper {
        transform: scale(0.75);
    }

    /* 사이드바 패딩 최소화 */
    #photo-sidebar {
        padding: 5px;
    }
}