/**
 * iBestContentBuilder Plugin Styles
 * 整合 TinyMCE 上架神器版型插件的所有樣式
 * 
 * @version 1.0.0
 * @author iBest Tech Team
 */

/* ========================================
 * 側邊欄主容器
 * ======================================== */
.ibest-contentbuilder-sidebar {
    display: flex;
    flex-direction: column;
    height: 100%;
    background: #ffffff;
    font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Oxygen, Ubuntu, Cantarell, sans-serif;
}

/* ========================================
 * 標題區域
 * ======================================== */
.ibest-contentbuilder-sidebar .sidebar-header {
    padding: 16px;
    border-bottom: 1px solid #e0e0e0;
    background: #f5f5f5;
    display: flex;
    align-items: center;
    justify-content: space-between;
}

.ibest-contentbuilder-sidebar .sidebar-header h3 {
    margin: 0;
    font-size: 16px;
    font-weight: 600;
    color: #333333;
    flex: 1;
}

/* 關閉按鈕 */
.ibest-contentbuilder-sidebar .sidebar-close-btn {
    display: flex;
    align-items: center;
    justify-content: center;
    width: 32px;
    height: 32px;
    padding: 0;
    margin-left: 12px;
    border: 1px solid #d0d0d0;
    border-radius: 4px;
    background: #ffffff;
    color: #666666;
    cursor: pointer;
    transition: all 0.2s;
    flex-shrink: 0;
}

.ibest-contentbuilder-sidebar .sidebar-close-btn:hover {
    background: #f0f0f0;
    border-color: #999999;
    color: #333333;
}

.ibest-contentbuilder-sidebar .sidebar-close-btn:active {
    background: #e0e0e0;
    transform: scale(0.95);
}

.ibest-contentbuilder-sidebar .sidebar-close-btn svg {
    display: block;
    pointer-events: none;
}

/* ========================================
 * 篩選區域
 * ======================================== */
.ibest-contentbuilder-sidebar .sidebar-filters {
    padding: 12px 16px;
    border-bottom: 1px solid #e0e0e0;
    display: flex;
    align-items: center;
    gap: 12px;
    background: #fafafa;
}

.ibest-contentbuilder-sidebar .category-select {
    flex: 1;
    padding: 8px 12px;
    border: 1px solid #d0d0d0;
    border-radius: 4px;
    background: #ffffff;
    font-size: 14px;
    color: #333333;
    cursor: pointer;
    transition: border-color 0.2s;
}

.ibest-contentbuilder-sidebar .category-select:hover {
    border-color: #999999;
}

.ibest-contentbuilder-sidebar .category-select:focus {
    outline: none;
    border-color: #0066cc;
    box-shadow: 0 0 0 2px rgba(0, 102, 204, 0.1);
}

.ibest-contentbuilder-sidebar .category-select:disabled {
    background: #f0f0f0;
    cursor: not-allowed;
    opacity: 0.6;
}

.ibest-contentbuilder-sidebar .template-count {
    font-size: 12px;
    color: #666666;
    white-space: nowrap;
}

/* ========================================
 * 內容區域
 * ======================================== */
.ibest-contentbuilder-sidebar .sidebar-content {
    flex: 1;
    overflow-y: auto;
    padding: 16px;
    contain: layout style paint;
    /* 效能優化 */
    transform: translateZ(0);
    /* 創建合成層 */
    will-change: scroll-position;
}

/* 自訂滾動條 */
.ibest-contentbuilder-sidebar .sidebar-content::-webkit-scrollbar {
    width: 8px;
}

.ibest-contentbuilder-sidebar .sidebar-content::-webkit-scrollbar-track {
    background: #f0f0f0;
}

.ibest-contentbuilder-sidebar .sidebar-content::-webkit-scrollbar-thumb {
    background: #c0c0c0;
    border-radius: 4px;
}

.ibest-contentbuilder-sidebar .sidebar-content::-webkit-scrollbar-thumb:hover {
    background: #a0a0a0;
}

/* Safari 14 以下降級樣式 */
@supports not (contain: layout) {
    .ibest-contentbuilder-sidebar .sidebar-content {
        overflow: hidden;
    }
}

/* ========================================
 * 版型網格
 * ======================================== */
.ibest-contentbuilder-sidebar .templates-grid {
    display: grid;
    grid-template-columns: 1fr;
    gap: 16px;
}

/* ========================================
 * 版型項目
 * ======================================== */
.ibest-contentbuilder-sidebar .template-item {
    position: relative; /* 為浮動按鈕提供定位基準 */
    border: 2px solid #e0e0e0;
    border-radius: 8px;
    overflow: hidden;
    cursor: pointer;
    transition: all 0.2s;
    background: #ffffff;
    contain: layout style;
    /* 效能優化 */
    transform: translateZ(0);
    /* 創建合成層 */
    will-change: transform;
}

.ibest-contentbuilder-sidebar .template-item:hover {
    border-color: #0066cc;
    box-shadow: 0 4px 12px rgba(0, 0, 0, 0.1);
    transform: translateY(-2px);
}

.ibest-contentbuilder-sidebar .template-item:active {
    transform: translateY(0);
}

/* 圖片容器 */
.ibest-contentbuilder-sidebar .template-item-image {
    position: relative;
    width: 100%;
    height: 120px;
    overflow: hidden;
}

.ibest-contentbuilder-sidebar .template-item img {
    width: 100%;
    height: 100%;
    object-fit: cover;
    display: block;
    background: #f5f5f5;
}

.ibest-contentbuilder-sidebar .template-name {
    padding: 8px;
    font-size: 13px;
    color: #333333;
    text-align: center;
    background: #fafafa;
    border-top: 1px solid #e0e0e0;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}

/* 插入按鈕 - 漂浮在圖片上方 */
.ibest-contentbuilder-sidebar .template-insert-btn {
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 6px;
    padding: 10px 20px;
    border: none;
    border-radius: 6px;
    background: rgba(0, 102, 204, 0.95);
    color: #ffffff;
    font-size: 13px;
    font-weight: 500;
    cursor: pointer;
    opacity: 0;
    pointer-events: none;
    transition: all 0.3s ease;
    box-shadow: 0 4px 12px rgba(0, 0, 0, 0.3);
    z-index: 10;
    white-space: nowrap;
}

/* Hover 時顯示按鈕 */
.ibest-contentbuilder-sidebar .template-item:hover .template-insert-btn {
    opacity: 1;
    pointer-events: auto;
}

.ibest-contentbuilder-sidebar .template-insert-btn:hover {
    background: rgba(0, 82, 163, 0.95);
    transform: translate(-50%, -50%) scale(1.05);
    box-shadow: 0 6px 16px rgba(0, 0, 0, 0.4);
}

.ibest-contentbuilder-sidebar .template-insert-btn:active {
    background: rgba(0, 61, 122, 0.95);
    transform: translate(-50%, -50%) scale(0.98);
}

.ibest-contentbuilder-sidebar .template-insert-btn svg {
    display: block;
    flex-shrink: 0;
}

/* Hover 時圖片變暗效果 */
.ibest-contentbuilder-sidebar .template-item:hover .template-item-image::after {
    content: '';
    position: absolute;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    background: rgba(0, 0, 0, 0.4);
    transition: background 0.3s ease;
    z-index: 5;
}

/* Safari 14 以下降級樣式 */
@supports not (contain: layout) {
    .ibest-contentbuilder-sidebar .template-item {
        isolation: isolate;
    }
}

/* ========================================
 * 載入中狀態
 * ======================================== */
.ibest-contentbuilder-sidebar .loading-state {
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    padding: 40px 20px;
    color: #666666;
}

.ibest-contentbuilder-sidebar .loading-state .spinner {
    width: 40px;
    height: 40px;
    border: 4px solid #f0f0f0;
    border-top-color: #0066cc;
    border-radius: 50%;
    animation: spin 1s linear infinite;
    margin-bottom: 16px;
}

@keyframes spin {
    to {
        transform: rotate(360deg);
    }
}

.ibest-contentbuilder-sidebar .loading-state p {
    margin: 0;
    font-size: 14px;
}

/* ========================================
 * 空狀態
 * ======================================== */
.ibest-contentbuilder-sidebar .empty-state {
    text-align: center;
    padding: 40px 20px;
    color: #999999;
}

.ibest-contentbuilder-sidebar .empty-state p {
    margin: 0;
    font-size: 14px;
}

/* ========================================
 * 錯誤狀態
 * ======================================== */
.ibest-contentbuilder-sidebar .error-state {
    text-align: center;
    padding: 40px 20px;
    color: #cc0000;
}

.ibest-contentbuilder-sidebar .error-state .error-icon {
    font-size: 48px;
    margin-bottom: 16px;
}

.ibest-contentbuilder-sidebar .error-state p {
    margin: 0 0 16px 0;
    font-size: 14px;
}

.ibest-contentbuilder-sidebar .error-state .retry-button {
    padding: 8px 16px;
    border: 1px solid #cc0000;
    border-radius: 4px;
    background: #ffffff;
    color: #cc0000;
    font-size: 14px;
    cursor: pointer;
    transition: all 0.2s;
}

.ibest-contentbuilder-sidebar .error-state .retry-button:hover {
    background: #cc0000;
    color: #ffffff;
}

/* ========================================
 * 響應式調整
 * ======================================== */
@media (max-width: 480px) {
    .ibest-contentbuilder-sidebar .templates-grid {
        gap: 12px;
    }

    .ibest-contentbuilder-sidebar .template-item img {
        height: 100px;
    }
}

/* ========================================
 * 插入位置標示線
 * ======================================== */
.insert-indicator {
    position: absolute;
    left: 20px;
    right: 20px;
    height: 3px;
    background: linear-gradient(90deg, #0066cc, #0080ff, #0066cc);
    pointer-events: none;
    z-index: 1000;
    transition: top 0.1s ease-out;
    border-radius: 2px;
    box-shadow: 0 2px 8px rgba(0, 102, 204, 0.4);
    will-change: top;
    /* 效能優化 */
}

.insert-indicator::before,
.insert-indicator::after {
    content: '';
    position: absolute;
    top: -6px;
    width: 12px;
    height: 12px;
    background: #0066cc;
    border-radius: 50%;
    border: 2px solid #ffffff;
    box-shadow: 0 2px 4px rgba(0, 0, 0, 0.2);
}

.insert-indicator::before {
    left: -6px;
}

.insert-indicator::after {
    right: -6px;
}

/* 根據插入位置的不同顏色標示 */
.insert-indicator[data-position*="開頭"] {
    background: linear-gradient(90deg, #28a745, #20c997, #28a745);
}

.insert-indicator[data-position*="開頭"]::before,
.insert-indicator[data-position*="開頭"]::after {
    background: #28a745;
}

/* 高對比模式支援 */
@media (prefers-contrast: high) {
    .insert-indicator {
        height: 3px;
        background: #000000;
    }
}

/* ========================================
 * 插入位置提示標籤
 * ======================================== */
.insert-position-tooltip {
    position: absolute;
    padding: 6px 12px;
    background: rgba(0, 102, 204, 0.95);
    color: #ffffff;
    font-size: 12px;
    font-weight: 500;
    border-radius: 4px;
    pointer-events: none;
    z-index: 1001;
    white-space: nowrap;
    box-shadow: 0 2px 8px rgba(0, 0, 0, 0.2);
}

/* ========================================
 * 浮動排序按鈕
 * ======================================== */
.contentbuilder-floating-buttons {
    position: absolute;
    right: -50px;
    top: 50%;
    transform: translateY(-50%);
    display: flex;
    flex-direction: column;
    gap: 8px;
    opacity: 0;
    pointer-events: none;
    transition: opacity 0.2s ease-in-out;
    z-index: 1000;
}

.contentbuilder-floating-buttons.show {
    opacity: 1;
    pointer-events: auto;
}

.contentbuilder-floating-buttons button {
    width: 36px;
    height: 36px;
    border: none;
    border-radius: 6px;
    background: #0066cc;
    color: white;
    cursor: pointer;
    transition: all 0.2s ease;
    display: flex;
    align-items: center;
    justify-content: center;
    box-shadow: 0 2px 8px rgba(0, 102, 204, 0.3);
}

.contentbuilder-floating-buttons button:hover:not(.disabled) {
    background: #0052a3;
    transform: scale(1.1);
    box-shadow: 0 4px 12px rgba(0, 102, 204, 0.4);
}

.contentbuilder-floating-buttons button:active:not(.disabled) {
    transform: scale(0.95);
}

.contentbuilder-floating-buttons button.disabled {
    background: #cccccc;
    cursor: not-allowed;
    opacity: 0.5;
    box-shadow: none;
}

/* ========================================
 * 區塊移動動畫
 * ======================================== */
.contentBuilder.block-moving,
.contentbuilder-block.block-moving {
    transition: transform 0.3s ease-out;
}

.contentBuilder.has-floating-buttons,
.contentbuilder-block.has-floating-buttons {
    outline: 2px solid rgba(0, 102, 204, 0.3);
    outline-offset: 2px;
}

.contentBuilder.block-will-move,
.contentbuilder-block.block-will-move {
    animation: blockPulse 0.3s ease-in-out;
}

.contentBuilder.block-moved-highlight,
.contentbuilder-block.block-moved-highlight {
    animation: blockHighlight 1s ease-in-out;
}

/* 脈衝動畫 - 準備移動時 */
@keyframes blockPulse {

    0%,
    100% {
        transform: scale(1);
        box-shadow: 0 0 0 0 rgba(0, 102, 204, 0);
    }

    50% {
        transform: scale(1.02);
        box-shadow: 0 0 0 8px rgba(0, 102, 204, 0.3);
    }
}

/* 高亮動畫 - 移動完成後 */
@keyframes blockHighlight {
    0% {
        background-color: rgba(0, 102, 204, 0.15);
        box-shadow: 0 0 0 0 rgba(0, 102, 204, 0.5);
    }

    25% {
        background-color: rgba(0, 102, 204, 0.25);
        box-shadow: 0 0 0 10px rgba(0, 102, 204, 0.3);
    }

    50% {
        background-color: rgba(0, 102, 204, 0.15);
        box-shadow: 0 0 0 15px rgba(0, 102, 204, 0.1);
    }

    100% {
        background-color: transparent;
        box-shadow: 0 0 0 20px rgba(0, 102, 204, 0);
    }
}

/* ========================================
 * 插入預覽區塊樣式
 * ======================================== */

/* 預覽區塊淡入動畫 */
@keyframes fadeInPreview {
    from {
        opacity: 0;
        transform: translateY(-10px) scale(0.95);
    }

    to {
        opacity: 1;
        transform: translateY(0) scale(1);
    }
}

/* 預覽內容區域 */
.contentbuilder-insert-preview .preview-content {
    padding: 16px;
}

/* 預覽標題區域 */
.contentbuilder-insert-preview .preview-header {
    display: flex;
    align-items: center;
    margin-bottom: 12px;
}

/* 預覽圖示 */
.contentbuilder-insert-preview .preview-icon {
    font-size: 20px;
    margin-right: 12px;
    opacity: 0.8;
}

/* 預覽資訊 */
.contentbuilder-insert-preview .preview-info {
    flex: 1;
}

/* 預覽標題 */
.contentbuilder-insert-preview .preview-title {
    font-weight: bold;
    color: #0066cc;
    font-size: 14px;
    margin-bottom: 2px;
}

/* 預覽副標題 */
.contentbuilder-insert-preview .preview-subtitle {
    color: #666;
    font-size: 12px;
}

/* 預覽徽章 */
.contentbuilder-insert-preview .preview-badge {
    background: #0066cc;
    color: white;
    padding: 4px 8px;
    border-radius: 12px;
    font-size: 10px;
    font-weight: bold;
    text-transform: uppercase;
}

/* 預覽主體 */
.contentbuilder-insert-preview .preview-body {
    background: rgba(255, 255, 255, 0.7);
    border-radius: 8px;
    padding: 12px;
    border: 1px solid rgba(0, 102, 204, 0.2);
}

/* 預覽佔位元素容器 */
.contentbuilder-insert-preview .preview-placeholder {
    padding: 8px 0;
}

/* 佔位線條 */
.contentbuilder-insert-preview .placeholder-line {
    height: 8px;
    background: linear-gradient(90deg, #e0e0e0, #f0f0f0, #e0e0e0);
    border-radius: 4px;
    margin-bottom: 6px;
    animation: shimmer 1.5s infinite;
}

/* 佔位線條長度變化 */
.contentbuilder-insert-preview .placeholder-line.long {
    width: 85%;
}

.contentbuilder-insert-preview .placeholder-line.medium {
    width: 65%;
}

.contentbuilder-insert-preview .placeholder-line.short {
    width: 45%;
}

/* 佔位圖片 */
.contentbuilder-insert-preview .placeholder-image {
    width: 60px;
    height: 40px;
    background: linear-gradient(135deg, #e0e0e0, #f0f0f0);
    border-radius: 6px;
    margin-top: 8px;
    position: relative;
}

.contentbuilder-insert-preview .placeholder-image::before {
    content: '🖼️';
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
    font-size: 16px;
    opacity: 0.6;
}

/* 閃爍動畫 */
@keyframes shimmer {
    0% {
        transform: translateX(-100%);
    }

    100% {
        transform: translateX(100%);
    }
}

/* ========================================
 * 平滑滾動支援
 * ======================================== */
html {
    scroll-behavior: smooth;
}