:root {
    --bg-body: #f4f6f8;
    --bg-card: #ffffff;
    --text-main: #1a1a1a;
    --text-sub: #666666;
    --primary: #2563eb;
    --primary-hover: #1d4ed8;
    --border: #e2e8f0;
    --input-bg: #f8fafc;
    --accent: #FF3B30;
    --nav-height: 60px;
}

[data-theme="dark"] {
    --bg-body: #111111;
    --bg-card: #1e1e1e;
    --text-main: #ffffff;
    --text-sub: #a0a0a0;
    --primary: #3b82f6;
    --primary-hover: #60a5fa;
    --border: #333333;
    --input-bg: #2d2d2d;
}

* { margin: 0; padding: 0; box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
body { font-family: 'Pretendard', -apple-system, BlinkMacSystemFont, Roboto, sans-serif; background-color: var(--bg-body); color: var(--text-main); transition: background 0.3s, color 0.3s; padding-bottom: env(safe-area-inset-bottom); }

.app-container { max-width: 1400px; margin: 0 auto; min-height: 100vh; display: flex; flex-direction: column; }

/* Header */
.app-header { display: flex; justify-content: space-between; align-items: center; padding: 1rem 1.5rem; background: var(--bg-body); position: sticky; top: 0; z-index: 100; }
.logo-area h1 { font-size: 1.4rem; font-weight: 800; background: linear-gradient(135deg, var(--text-main), var(--text-sub)); -webkit-background-clip: text; -webkit-text-fill-color: transparent; }
.header-controls { display: flex; align-items: center; gap: 12px; }
.icon-btn { background: none; border: none; color: var(--text-main); font-size: 1.4rem; cursor: pointer; padding: 8px; border-radius: 50%; }

/* Language Selector */
.lang-select {
    background: var(--input-bg);
    border: 1px solid var(--border);
    border-radius: 8px;
    color: var(--text-main);
    font-size: 0.8rem;
    padding: 6px 12px;
    cursor: pointer;
    outline: none;
    transition: all 0.2s ease;
    font-weight: 600;
    letter-spacing: 0.5px;
    text-align: center;
    min-width: 50px;
    /* 기본 화살표 제거 */
    -webkit-appearance: none;
    -moz-appearance: none;
    appearance: none;
    /* 커스텀 화살표 아이콘 제거 (배경 없음) */
    background-image: none;
    padding-right: 12px;
}

.lang-select:hover {
    border-color: var(--primary);
    background: rgba(37, 99, 235, 0.05);
}

.lang-select:focus {
    border-color: var(--primary);
    box-shadow: 0 0 0 3px rgba(37, 99, 235, 0.1);
}

.lang-select option {
    background: var(--bg-card);
    color: var(--text-main);
    padding: 8px;
    font-weight: 600;
}

/* Layout */
.desktop-split { display: grid; grid-template-columns: 420px 1fr 420px; gap: 20px; padding: 0 1.5rem 2rem; align-items: start; }
.card { background: var(--bg-card); border-radius: 12px; padding: 16px; box-shadow: 0 2px 12px rgba(0,0,0,0.05); margin-bottom: 16px; border: 1px solid var(--border); }
.card-header { display: flex; justify-content: space-between; align-items: center; margin-bottom: 12px; }
.header-actions { display: flex; justify-content: space-between; align-items: center; gap: 10px; flex: 1; margin-left: 12px; }
.card h3 { font-size: 0.95rem; font-weight: 700; color: var(--text-main); }
.mt-2 { margin-top: 0.5rem; }

/* Upload Area */
.upload-area.small-mode { height: 60px; border: 2px dashed var(--border); border-radius: 10px; display: flex; justify-content: center; align-items: center; cursor: pointer; background: var(--input-bg); transition: 0.3s; width: 100%; }
.upload-area:hover, .upload-area.drag-over { border-color: var(--primary); background: rgba(37,99,235,0.05); }
.upload-placeholder { display: flex; align-items: center; gap: 10px; color: var(--text-sub); }
.upload-icon { font-size: 1.4rem; color: var(--primary); }
.upload-placeholder p { font-size: 0.9rem; font-weight: 500; }

.file-info-area { margin-top: 10px; display: none; /* 파일명 표시 영역 숨김 */ }
.mini-preview-wrapper { display: flex; justify-content: space-between; align-items: center; background: var(--input-bg); padding: 8px 12px; border-radius: 8px; border: 1px solid var(--border); }
#fileName { font-size: 0.85rem; color: var(--text-main); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; max-width: 70%; }
.text-btn-danger { color: #ef4444; font-size: 0.8rem; background: none; border: none; cursor: pointer; font-weight: 600; padding: 4px; }

/* Inputs */
.input-wrap { display: flex; align-items: center; background: var(--input-bg); border-radius: 8px; padding: 8px 12px; border: 1px solid var(--border); }
.input-wrap i { color: var(--text-sub); margin-right: 10px; }
.input-wrap input { border: none; background: transparent; width: 100%; color: var(--text-main); outline: none; font-size: 0.95rem; }

/* List Item Style Fix (버튼 깨짐 해결) */
.metadata-list { display: flex; flex-direction: column; gap: 8px; max-height: 500px; overflow-y: auto; }
/* 표시항목 카드 사이즈 */
.list-card-expanded .metadata-list { max-height: 760px; min-height: 760px; height: 760px; }
.metadata-item {
    display: flex;
    align-items: center;
    background: var(--input-bg);
    padding: 8px 10px;
    border-radius: 8px;
    gap: 8px;
    border: 1px solid transparent;
    transition: 0.2s;
}
.metadata-item:hover { border-color: var(--primary); }
.metadata-item.disabled { opacity: 0.5; }
.drag-handle { color: var(--text-sub); cursor: grab; font-size: 1.1rem; flex-shrink: 0; }
.metadata-checkbox { flex-shrink: 0; width: 18px; height: 18px; }

/* 모바일 재정렬 버튼 */
.mobile-reorder-btns {
    display: none;
    flex-direction: column;
    gap: 4px;
    flex-shrink: 0;
}

.mobile-arrow-btn {
    background: var(--bg-card);
    border: 1px solid var(--border);
    border-radius: 4px;
    color: var(--text-main);
    cursor: pointer;
    padding: 2px;
    font-size: 1rem;
    display: flex;
    align-items: center;
    justify-content: center;
    width: 28px;
    height: 24px;
    transition: 0.2s;
}

.mobile-arrow-btn:hover:not(:disabled) {
    border-color: var(--primary);
    color: var(--primary);
    background: rgba(37, 99, 235, 0.1);
}

.mobile-arrow-btn:disabled {
    opacity: 0.3;
    cursor: not-allowed;
}

.mobile-arrow-btn i {
    pointer-events: none;
}

.meta-content { 
    flex: 1; 
    overflow: hidden; 
    display: flex;
    flex-direction: column;
    justify-content: center;
}
.meta-key { display: block; font-size: 0.7rem; color: var(--text-sub); font-weight: 600; text-transform: uppercase; margin-bottom: 2px; }
.meta-val { display: block; font-size: 0.85rem; color: var(--text-main); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }

/* 버튼 크기 고정 및 스타일 */
.label-toggle-btn { 
    background: var(--bg-card); 
    border: 1px solid var(--border); 
    padding: 6px 10px; 
    border-radius: 6px; 
    font-size: 0.75rem; 
    cursor: pointer; 
    color: var(--text-main); 
    min-width: 70px; /* 고정 너비 */
    text-align: center;
    flex-shrink: 0; /* 줄어들지 않게 */
}
.label-toggle-btn:hover { border-color: var(--primary); color: var(--primary); }

/* 전체선택 버튼 스타일 */
.active-text-btn { color: var(--primary); font-weight: 700; cursor: pointer; background: rgba(37,99,235,0.1); border: 1px solid var(--primary); font-size: 0.8rem; padding: 6px 12px; border-radius: 20px; transition: 0.2s; white-space: nowrap; }
.active-text-btn:hover { background: var(--primary); color: white; }

/* Result Options */
.format-options { display: flex; align-items: center; gap: 10px; }
.sep-input { width: 70px; padding: 6px 10px; border-radius: 6px; border: 1px solid var(--border); background: var(--input-bg); color: var(--text-main); font-size: 0.8rem; }
.sep-input:disabled { opacity: 0.4; cursor: not-allowed; }

.radio-group { display: flex; gap: 6px; }
.radio-chip input { display: none; }
.radio-chip span { display: inline-block; padding: 5px 10px; border-radius: 15px; border: 1px solid var(--border); font-size: 0.8rem; cursor: pointer; color: var(--text-sub); transition: 0.2s; }
.radio-chip input:checked + span { background: var(--primary); color: white; border-color: var(--primary); }

/* Editor 텍스트 편집 */
#textEditor { width: 100%; height: 650px; background: var(--input-bg); border: 1px solid var(--border); border-radius: 10px; padding: 12px; color: var(--text-main); font-size: 0.95rem; resize: none; outline: none; margin-bottom: 12px; line-height: 1.5; white-space: pre-wrap; font-family: inherit; }
.action-row { display: flex; justify-content: space-between; align-items: center; }
.tag-btn { background: transparent; border: 1px solid var(--primary); color: var(--primary); padding: 8px 14px; border-radius: 20px; font-size: 0.85rem; cursor: pointer; transition: 0.2s; display: flex; align-items: center; gap: 4px; }
.btn-primary { background: var(--primary); color: white; border: none; padding: 10px 20px; border-radius: 8px; font-size: 0.95rem; font-weight: 600; cursor: pointer; display: flex; align-items: center; gap: 6px; }
.btn-secondary-sm { background: var(--input-bg); border: 1px solid var(--border); color: var(--text-main); padding: 6px 12px; border-radius: 6px; font-size: 0.8rem; cursor: pointer; display: flex; align-items: center; gap: 4px; }

/* Preset */
.preset-group {
    display: flex;
    align-items: center;
    gap: 4px;
}

.preset-select {
    padding: 6px 10px;
    border-radius: 6px;
    border: 1px solid var(--border);
    background: var(--input-bg);
    color: var(--text-main);
    font-size: 0.8rem;
    min-width: 100px;
    cursor: pointer;
    outline: none;
}

.icon-btn-sm {
    background: var(--input-bg);
    border: 1px solid var(--border);
    border-radius: 6px;
    color: var(--text-main);
    padding: 6px 8px;
    cursor: pointer;
    font-size: 1rem;
    display: flex;
    align-items: center;
    transition: 0.2s;
}

.icon-btn-sm:hover {
    border-color: var(--primary);
    color: var(--primary);
}

.icon-btn-sm.delete-btn {
    border-color: #ef4444;
    color: #ef4444;
}

.icon-btn-sm.delete-btn:hover {
    background: #ef4444;
    color: white;
}

/* Instagram Preview (Fixed Layout) */
.insta-mockup { background: var(--bg-card); border: 1px solid var(--border); border-radius: 3px; max-width: 100%; margin: 0 auto; width: 380px; }
.insta-header { padding: 10px 12px; display: flex; align-items: center; gap: 10px; width: 100%; height: 56px; }
.insta-profile-pic { width: 32px; height: 32px; border-radius: 50%; background: linear-gradient(45deg, #f09433, #bc1888); flex-shrink: 0; }
/* 중요: 이름 뜸 현상 수정 */
.insta-info { display: flex; flex-direction: column; justify-content: center; flex: 1; min-width: 0; }
.insta-username { font-size: 0.9rem; font-weight: 600; line-height: 1.2; color: var(--text-main); }
.insta-loc { font-size: 0.75rem; color: var(--text-sub); line-height: 1.2; }
.icon-more { font-size: 1.2rem; color: var(--text-main); }
.ml-auto { margin-left: auto; }

/* Photo Container - White Padding Logic for 4:5 Ratio */
.insta-photo-container {
    width: 100%;
    background-color: #ffffff; /* 흰색 배경 (여백) */
    display: flex;
    justify-content: center;
    align-items: center;
    overflow: hidden;
    position: relative;
    border-top: 1px solid var(--border);
    border-bottom: 1px solid var(--border);
}

.insta-photo-container img {
    display: block;
    max-width: 100%;
    /* 최대 높이를 너비의 125% (4:5 비율)로 제한 */
    max-height: 125vw;
    object-fit: contain; /* 비율 유지하며 컨테이너 안에 맞춤 (여백 발생) */
}

@media (min-width: 769px) {
    /* 데스크탑 프리뷰 너비 380px 기준 */
    .insta-photo-container img {
        max-height: 475px; /* 380px * 1.25 */
    }
}

@media (min-width: 450px) {
    /* 태블릿 프리뷰 */
    .insta-photo-container img {
        max-height: 475px;
    }
}

@media (min-width: 300px) {
    /* 모바일 프리뷰 */
    .insta-photo-container img {
        max-height: 400px;
    }
}

.insta-actions { padding: 8px 12px; display: flex; justify-content: space-between; font-size: 1.5rem; color: var(--text-main); }
.left-icons { display: flex; gap: 16px; }

/* Caption Area Fix */
.insta-caption-area { padding: 0 12px 12px; font-size: 0.9rem; color: var(--text-main); }
.caption-wrapper {
    display: block; /* 블록 요소로 변경하여 내부 요소들이 흐르게 함 */
}
.insta-username-bold { 
    font-weight: 600; 
    display: inline; /* 인라인 유지 */
}
.caption-text {
    white-space: pre-wrap; /* 줄바꿈 적용 */
    word-break: break-word; 
    line-height: 1.4;
    display: inline; /* 아이디 옆에 바로 붙도록 */
}

/* Mobile Nav */
.mobile-nav { display: none; position: fixed; bottom: 0; left: 0; width: 100%; background: var(--bg-card); border-top: 1px solid var(--border); height: var(--nav-height); justify-content: space-around; align-items: center; z-index: 1000; padding-bottom: env(safe-area-inset-bottom); }
.nav-item { background: none; border: none; display: flex; flex-direction: column; align-items: center; color: var(--text-sub); font-size: 0.7rem; gap: 4px; cursor: pointer; width: 100%; height: 100%; justify-content: center; }
.nav-item i { font-size: 1.4rem; }
.nav-item.active { color: var(--primary); }

/* Toast */
.toast { position: fixed; bottom: 80px; left: 50%; transform: translateX(-50%); background: rgba(0,0,0,0.8); color: white; padding: 10px 20px; border-radius: 20px; font-size: 0.85rem; opacity: 0; pointer-events: none; transition: 0.3s; z-index: 2000; }
.toast.show { opacity: 1; }

/* 모든 세로 모드 (Portrait) - 모바일 레이아웃 적용 */
@media (orientation: portrait) and (max-width: 1024px) {
    .desktop-split { display: block; padding: 0 12rem; }
    .mobile-nav { display: flex; }

    .upload-card, .setting-card, .list-card, .result-card, .preview-card { display: none; margin-bottom: 16px; }

    /* 탭별 표시 로직 */
    /* 1. 미리보기 탭: 업로드 + 인스타그램 미리보기 */
    body[data-view="preview"] .upload-card,
    body[data-view="preview"] .preview-card { display: block; }

    /* 2. 설정 탭: 리스트만 */
    body[data-view="settings"] .list-card { display: block; }

    /* 3. 편집 탭: 텍스트 에디터 (넓게) */
    body[data-view="edit"] .result-card { display: block; }

    .mobile-spacer { height: 70px; }
    .insta-mockup { width: 100%; }

    /* 미리보기 이미지 높이 증가 */
    .insta-photo-container img {
        max-height: 600px !important;
    }

    /* 표시 항목 리스트 높이 줄임 */
    .list-card-expanded .metadata-list {
        max-height: calc(100vh - 350px);
        min-height: calc(100vh - 350px);
        height: calc(100vh - 350px);
    }

    /* 텍스트 에디터 높이 줄임 */
    #textEditor {
        height: calc(100vh - 460px);
    }

    /* 화살표 버튼 표시, 드래그 핸들 숨김 */
    .mobile-reorder-btns {
        display: flex;
    }

    .drag-handle {
        display: none;
    }
}

/* 태블릿 가로 모드 (아이패드 Landscape) - 데스크탑과 동일 */
@media (min-width: 1024px) and (orientation: landscape) {
    .mobile-nav { display: none; }
    .mobile-spacer { display: none; }
    .upload-card, .setting-card, .list-card, .result-card, .preview-card { display: block !important; }
}

/* 모바일 레이아웃 */
@media (max-width: 768px) {
    .desktop-split { display: block; padding: 0 1rem; }
    .mobile-nav { display: flex; }

    /* 모바일에서 언어 선택기 작게 */
    .lang-select {
        font-size: 0.75rem;
        padding: 5px 10px;
        min-width: 45px;
        border-radius: 6px;
    }

    .header-controls {
        gap: 8px;
    }

    .app-header {
        padding: 0.75rem 1rem;
    }

    .upload-card, .setting-card, .list-card, .result-card, .preview-card { display: none; margin-bottom: 20px; }

    /* 모바일 탭별 표시 로직 (사용자 요청 반영) */
    /* 1. 미리보기 탭: 업로드 + 인스타그램 미리보기 */
    body[data-view="preview"] .upload-card,
    body[data-view="preview"] .preview-card { display: block; }

    /* 2. 설정 탭: 리스트만 */
    body[data-view="settings"] .list-card { display: block; }

    /* 3. 편집 탭: 텍스트 에디터 (넓게) */
    body[data-view="edit"] .result-card { display: block; }

    .mobile-spacer { height: 70px; }
    .insta-mockup { width: 100%; }

    /* 모바일에서 표시 항목 리스트 높이 조정 */
    .list-card-expanded .metadata-list {
        max-height: calc(100vh - 280px);
        min-height: calc(100vh - 280px);
        height: calc(100vh - 280px);
    }

    /* 모바일에서 텍스트 에디터 높이 조정 */
    #textEditor {
        height: calc(100vh - 390px);
    }

    /* 모바일에서 화살표 버튼 표시, 드래그 핸들 숨김 */
    .mobile-reorder-btns {
        display: flex;
    }

    .drag-handle {
        display: none;
    }
}

/* 데스크탑 레이아웃 */
@media (min-width: 1025px) {
    .mobile-nav { display: none; }
    .mobile-spacer { display: none; }
    .upload-card, .setting-card, .list-card, .result-card, .preview-card { display: block !important; }
}