/**
 * ✨ RB Editor - 자동 목차 & 드래그 앤 드롭 스타일
 * 워드프레스 스타일 블록 에디터 디자인
 * 반응형 지원: 모바일, 태블릿, 데스크톱
 */

/* ========================================
   자동 목차 (Table of Contents) 스타일
======================================== */

#auto-toc-wrap {
    position: fixed;
    right: 20px;
    top: 120px;
    width: 280px;
    max-height: calc(100vh - 200px);
    background: #ffffff;
    border: 1px solid rgba(0, 0, 0, 0.1);
    border-radius: 12px;
    box-shadow: 0 4px 16px rgba(0, 0, 0, 0.12);
    z-index: 9998;
    overflow: hidden;
    transition: all 0.3s ease;
}

#auto-toc-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 16px 20px;
    background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
    color: #fff;
    border-bottom: 1px solid rgba(255, 255, 255, 0.1);
}

#auto-toc-header h4 {
    margin: 0;
    font-size: 16px;
    font-weight: 700;
    letter-spacing: -0.3px;
}

#auto-toc-close-btn {
    background: rgba(255, 255, 255, 0.2);
    border: none;
    color: #fff;
    font-size: 20px;
    width: 28px;
    height: 28px;
    border-radius: 50%;
    cursor: pointer;
    transition: all 0.2s;
    display: flex;
    align-items: center;
    justify-content: center;
    padding: 0;
}

#auto-toc-close-btn:hover {
    background: rgba(255, 255, 255, 0.3);
    transform: scale(1.1);
}

#auto-toc-content {
    padding: 12px 0;
    max-height: calc(100vh - 280px);
    overflow-y: auto;
    overflow-x: hidden;
}

#auto-toc-content::-webkit-scrollbar {
    width: 6px;
}

#auto-toc-content::-webkit-scrollbar-track {
    background: #f5f5f5;
}

#auto-toc-content::-webkit-scrollbar-thumb {
    background: #667eea;
    border-radius: 3px;
}

.toc-list {
    list-style: none;
    margin: 0;
    padding: 0;
}

.toc-item {
    margin: 0;
    border-left: 3px solid transparent;
    transition: all 0.2s;
}

.toc-item:hover {
    background: rgba(102, 126, 234, 0.05);
    border-left-color: #667eea;
}

.toc-link {
    display: block;
    padding: 10px 20px;
    color: #374d6e;
    text-decoration: none;
    font-size: 14px;
    line-height: 1.5;
    transition: all 0.2s;
    word-break: break-word;
}

.toc-link:hover {
    color: #667eea;
    font-weight: 600;
}

/* 헤딩 레벨별 스타일 */
.toc-level-1 .toc-link {
    font-weight: 700;
    font-size: 15px;
    color: #09244B;
}

.toc-level-2 .toc-link {
    font-weight: 600;
    font-size: 14px;
}

.toc-level-3 .toc-link,
.toc-level-4 .toc-link,
.toc-level-5 .toc-link {
    font-weight: 400;
    font-size: 13px;
    color: #666;
}

/* 목차 비어있을 때 */
.toc-empty {
    padding: 20px;
    text-align: center;
    color: #999;
    font-size: 13px;
    line-height: 1.6;
}

/* 스크롤 이동 시 하이라이트 효과 */
.toc-highlight {
    animation: tocHighlight 2s ease;
}

@keyframes tocHighlight {
    0%, 100% {
        background: transparent;
    }
    50% {
        background: rgba(102, 126, 234, 0.2);
    }
}

/* ========================================
   드래그 앤 드롭 블록 에디터 스타일
======================================== */

/* 드래그 핸들 기본 스타일 */
.drag-handle {
    position: absolute;
    left: -30px;
    top: 50%;
    transform: translateY(-50%);
    width: 24px;
    height: 24px;
    background: rgba(102, 126, 234, 0.1);
    border: 1px solid rgba(102, 126, 234, 0.3);
    border-radius: 4px;
    cursor: grab !important;
    pointer-events: auto !important; /* ✅ 강제로 마우스 이벤트 활성화 */
    display: none; /* 기본적으로 숨김, 호버시 표시 */
    align-items: center;
    justify-content: center;
    font-size: 12px;
    color: #667eea;
    user-select: none;
    z-index: 10;
    transition: all 0.2s ease;
}

.drag-handle:hover {
    background: rgba(102, 126, 234, 0.2);
    border-color: #667eea;
    transform: translateY(-50%) scale(1.1);
}

.drag-handle:active {
    cursor: grabbing;
    background: rgba(102, 126, 234, 0.3);
}

/* 드래그 가능한 요소 스타일 */
.sortable-item {
    position: relative;
    padding-left: 8px;
    transition: all 0.2s ease;
    margin: 8px 0;
    border-left: 3px solid transparent;
}

.sortable-item:hover {
    border-left-color: rgba(102, 126, 234, 0.3);
}

.sortable-item:hover .drag-handle {
    display: flex; /* 호버 시 핸들 표시 */
}

/* 드래그 중 스타일 */
.sortable-ghost {
    opacity: 0.4;
    background: rgba(102, 126, 234, 0.1);
    border: 2px dashed #667eea !important;
    border-radius: 8px;
}

.sortable-chosen {
    background: rgba(102, 126, 234, 0.05);
    box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
}

.sortable-drag {
    opacity: 0.8;
    box-shadow: 0 4px 16px rgba(0, 0, 0, 0.2);
    cursor: grabbing;
}

/* ✅ 이미지 컨테이너 드래그 핸들 위치 조정 */
.image-container.sortable-item {
    position: relative;
}

.image-container.sortable-item > .drag-handle {
    left: -30px;
    top: 10px !important;
    transform: none;
}

/* 이미지 블록 특수 스타일 */
.sortable-item img {
    display: block;
    max-width: 100%;
    height: auto;
    border-radius: 8px;
    margin: 8px 0;
}

/* 테이블 블록 특수 스타일 */
.sortable-item table {
    border-collapse: collapse;
    width: 100%;
    margin: 8px 0;
}

/* 코드 블록 특수 스타일 */
.sortable-item pre {
    background: #f5f7fa;
    padding: 16px;
    border-radius: 8px;
    overflow-x: auto;
    margin: 8px 0;
}

/* 영상 블록 특수 스타일 */
.sortable-item iframe {
    max-width: 100%;
    border-radius: 8px;
    margin: 8px 0;
}

/* ========================================
   반응형 디자인 (모바일 우선)
======================================== */

/* 모바일 (768px 이하) */
@media screen and (max-width: 768px) {
    /* 목차를 화면 하단에 고정 */
    #auto-toc-wrap {
        position: fixed;
        right: 0;
        left: 0;
        top: auto;
        bottom: 0;
        width: 100%;
        max-height: 50vh;
        border-radius: 20px 20px 0 0;
        box-shadow: 0 -4px 16px rgba(0, 0, 0, 0.2);
    }

    #auto-toc-content {
        max-height: calc(50vh - 80px);
    }

    /* 드래그 핸들 항상 표시 (모바일에서는 터치 편의성) */
    .drag-handle {
        display: flex;
        left: 5px;
        background: rgba(102, 126, 234, 0.2);
    }

    .sortable-item {
        padding-left: 35px; /* 핸들 공간 확보 */
    }

    /* 터치 디바이스용 핸들 크기 증가 */
    .drag-handle {
        width: 28px;
        height: 28px;
        font-size: 14px;
    }
}

/* 태블릿 (769px ~ 1024px) */
@media screen and (min-width: 769px) and (max-width: 1024px) {
    #auto-toc-wrap {
        width: 240px;
        right: 15px;
        top: 100px;
    }

    .drag-handle {
        left: -28px;
    }
}

/* 데스크톱 (1025px 이상) */
@media screen and (min-width: 1025px) {
    #auto-toc-wrap {
        width: 300px;
        right: 30px;
        top: 120px;
    }

    .drag-handle {
        left: -32px;
    }
}

/* 와이드 스크린 (1440px 이상) */
@media screen and (min-width: 1440px) {
    #auto-toc-wrap {
        width: 320px;
        right: 40px;
    }
}

/* ========================================
   접근성 개선
======================================== */

/* 키보드 포커스 스타일 */
.toc-link:focus,
.drag-handle:focus {
    outline: 2px solid #667eea;
    outline-offset: 2px;
}

/* 다크 모드 지원 (선택사항) */
@media (prefers-color-scheme: dark) {
    #auto-toc-wrap {
        background: #1e293b;
        border-color: rgba(255, 255, 255, 0.1);
    }

    .toc-link {
        color: #e2e8f0;
    }

    .toc-level-1 .toc-link {
        color: #f1f5f9;
    }

    .toc-item:hover {
        background: rgba(102, 126, 234, 0.1);
    }

    .sortable-item {
        border-left-color: rgba(102, 126, 234, 0.2);
    }

    .drag-handle {
        background: rgba(102, 126, 234, 0.2);
        color: #93c5fd;
    }
}

/* 인쇄 시 숨김 */
@media print {
    #auto-toc-wrap,
    .drag-handle,
    .image-quick-actions {
        display: none !important;
    }
}

/* ========================================
   이미지 퀵 액션 버튼 스타일
======================================== */

.image-container {
    position: relative;
    display: block;
    margin: 16px 0;
    transition: all 0.2s ease;
}

.image-container.selected {
    outline: 2px solid #667eea;
    outline-offset: 4px;
    border-radius: 8px;
}

.image-container img {
    display: block;
    max-width: 100%;
    height: auto;
    border-radius: 8px;
    transition: all 0.2s ease;
}

.image-quick-actions {
    position: absolute;
    top: -40px;
    left: 50%;
    transform: translateX(-50%);
    display: none; /* 기본 숨김 */
    align-items: center;
    gap: 4px;
    padding: 6px;
    background: rgba(9, 36, 75, 0.95);
    border-radius: 8px;
    box-shadow: 0 4px 12px rgba(0, 0, 0, 0.2);
    z-index: 100;
    backdrop-filter: blur(10px);
    animation: fadeInDown 0.2s ease;
}

@keyframes fadeInDown {
    from {
        opacity: 0;
        transform: translateX(-50%) translateY(-10px);
    }
    to {
        opacity: 1;
        transform: translateX(-50%) translateY(0);
    }
}

.img-quick-btn {
    width: 32px;
    height: 32px;
    padding: 0;
    border: none;
    background: rgba(255, 255, 255, 0.1);
    color: #fff;
    border-radius: 6px;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    transition: all 0.2s ease;
    outline: none;
}

.img-quick-btn:hover {
    background: rgba(255, 255, 255, 0.2);
    transform: scale(1.1);
}

.img-quick-btn:active {
    background: rgba(255, 255, 255, 0.3);
    transform: scale(0.95);
}

.img-quick-btn svg {
    width: 16px;
    height: 16px;
    pointer-events: none;
}

/* 이미지 캡션 스타일 */
.image-caption {
    display: block;
    text-align: center;
    font-size: 13px;
    color: #666;
    margin-top: 8px;
    padding: 6px 12px;
    font-style: italic;
    line-height: 1.5;
    border-radius: 4px;
    transition: all 0.2s ease;
}

.image-caption:empty::before {
    content: attr(data-placeholder);
    color: #999;
    font-style: italic;
}

.image-caption:focus {
    background: rgba(102, 126, 234, 0.05);
    outline: none;
}

/* 모바일에서 퀵 액션 위치 조정 */
@media screen and (max-width: 768px) {
    .image-quick-actions {
        top: auto;
        bottom: -45px;
        padding: 8px;
    }

    .img-quick-btn {
        width: 36px;
        height: 36px;
    }

    .img-quick-btn svg {
        width: 18px;
        height: 18px;
    }

    .image-container {
        margin: 24px 0;
    }
}
