/* 深色模式全局底色修正 */
[data-bs-theme="dark"] body {
    background-color: var(--bg-body);
    color: var(--text-main);
}

[data-bs-theme="dark"] .card {
    background-color: var(--container-bg);
    border: 1px solid rgba(255, 255, 255, 0.1) !important;
    box-shadow: 0 4px 15px rgba(0, 0, 0, 0.5) !important;
}

/* 檔案選取框深色調整 */
[data-bs-theme="dark"] .file-input-label {
    background-color: #1a1a1a;
    border: 2px dashed #444;
}

[data-bs-theme="dark"] .file-input-label:hover {
    border-color: var(--bs-primary);
    background-color: #252525;
}

/* 分頁晶片 (Chips) 深色模式優化 */
[data-bs-theme="dark"] .page-chip {
    background-color: #2d2d2d;
    border: 1px solid #444;
    color: #bbb;
}

[data-bs-theme="dark"] .page-chip.active {
    background: linear-gradient(135deg, #6a11cb 0%, #2575fc 100%); /* 維持紫色調但稍微調亮 */
    color: white;
    border-color: transparent;
}

/* 旋轉標記在深色模式下的橘色對比 */
[data-bs-theme="dark"] .page-chip.rotated {
    background-color: #332211 !important;
    border: 2px solid #fd7e14 !important;
    color: #ff922b !important;
}

/* 預覽容器深色背景 (防止 PDF 邊緣有白邊) */
[data-bs-theme="dark"] .preview-container {
    background-color: #1a1a1a;
    border: 1px solid #333;
}

/* 設定區區塊背景 */
[data-bs-theme="dark"] .setting-box {
    background-color: #1a1a1a;
    border-color: #333;
}

/* 狀態訊息顏色調整 */
[data-bs-theme="dark"] .alert-info {
    background-color: #0c2a35;
    color: #84d8ff;
    border: none;
}

[data-bs-theme="dark"] .img-item {
    background: #1e1e1e;
    border: 1px solid #333;
}

[data-bs-theme="dark"] .page-tag {
    background: #4b39b5; /* 配合你的紫色調 */
    color: #fff;
}

/* 拖拽時的虛影樣式 */
.bg-primary-subtle {
    background-color: rgba(106, 17, 203, 0.2) !important;
    border: 2px dashed #6a11cb !important;
}

.preview-container {
    height: 320px;
    background-color: #f8f9fa;
    border-radius: 0.75rem;
    overflow: hidden;
    display: flex;
    align-items: center;
    justify-content: center;
    border: 1px dashed #dee2e6;
    position: relative;
}

.preview-frame {
    width: 100%;
    height: 100%;
    border: none;
}

.file-input-label {
    cursor: pointer;
    display: block;
    padding: 1.5rem;
    border: 2px dashed #dee2e6;
    border-radius: 1rem;
    background-color: #fbfbfb;
    text-align: center;
    transition: all 0.2s;
}

.file-input-label:hover {
    border-color: #667eea;
    background-color: #f0f2ff;
}

.preview-badge {
    position: absolute;
    top: 10px;
    right: 10px;
    z-index: 10;
}

.setting-box {
    background-color: #fcfcfc;
    border: 1px solid #efefef;
    border-radius: 1rem;
}

.btn-primary-gradient {
    background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
    border: none;
    color: white;
    font-weight: 600;
}

.page-selector-container {
    display: flex;
    flex-wrap: wrap;
    gap: 6px;
    padding: 8px;
    background: #f8f9fa;
    border-radius: 8px;
    min-height: 48px;
    max-height: 150px;
    overflow-y: auto;
}

.page-chip {
    width: 34px;
    height: 34px;
    background: #ffffff;
    border: 1px solid #dee2e6;
    color: #6c757d;
    border-radius: 6px;
    display: flex;
    align-items: center;
    justify-content: center;
    cursor: pointer;
    font-size: 0.8rem;
    font-weight: 600;
    transition: all 0.2s;
    user-select: none;
}

.page-chip:hover {
    border-color: #4b39b5;
    color: #4b39b5;
}

.page-chip.active {
    background: #4b39b5;
    border-color: #4b39b5;
    color: #ffffff;
    box-shadow: 0 2px 4px rgba(75, 57, 181, 0.2);
}

/* 基礎晶片樣式 */
.page-chip {
    position: relative; /* 必加：為了讓圖示定位 */
    transition: all 0.2s ease;
}

/* 讓標記旋轉的頁碼更有存在感 */
.page-chip.rotated {
    background-color: #fff3e6 !important;
    border: 2px solid #fd7e14 !important;
    color: #fd7e14 !important;
}

/* 加上旋轉小圖示 */
.page-chip.rotated::after {
    content: '\F548'; /* Bootstrap Icon bi-arrow-clockwise */
    font-family: bootstrap-icons;
    position: absolute;
    top: -8px;
    right: -8px;
    background: #fd7e14;
    color: white;
    font-size: 10px;
    width: 16px;
    height: 16px;
    border-radius: 50%;
    display: flex;
    align-items: center;
    justify-content: center;
    box-shadow: 0 2px 4px rgba(0,0,0,0.2);
}
/* 檔案載入區動態效果 */
.file-input-label {
    border: 2px dashed #e0e0e0;
    border-radius: 1.25rem;
    background: #fafafa;
    transition: all 0.3s cubic-bezier(0.4, 0, 0.2, 1);
}

.file-input-label:hover {
    border-color: #6a11cb;
    background: #f0f4ff;
    transform: scale(1.01);
}

.upload-icon-wrapper {
    transition: transform 0.3s ease;
}

.file-input-label:hover .upload-icon-wrapper {
    transform: translateY(-5px);
}

/* 頁面標籤強化 (實驗室風格) */
.page-tag {
    backdrop-filter: blur(4px);
    background: rgba(13, 110, 253, 0.85) !important;
    border-radius: 0 4px 4px 0 !important;
    left: 0 !important;
    top: 10px !important;
    box-shadow: 2px 2px 10px rgba(0,0,0,0.1);
}

/* 自定義滾動條 (讓介面更像 App) */
.custom-scrollbar::-webkit-scrollbar {
    width: 6px;
}
.custom-scrollbar::-webkit-scrollbar-track {
    background: transparent;
}
.custom-scrollbar::-webkit-scrollbar-thumb {
    background: #ccc;
    border-radius: 10px;
}
.custom-scrollbar::-webkit-scrollbar-thumb:hover {
    background: #aaa;
}

/* 縮圖項目美化 */
.img-item {
    border: 1px solid rgba(0,0,0,0.05) !important;
    transition: all 0.2s ease;
    cursor: grab;
}

.img-item:active {
    cursor: grabbing;
}

.img-item:hover {
    box-shadow: 0 10px 20px rgba(0,0,0,0.1) !important;
}

/* 深色模式修正 */
[data-bs-theme="dark"] .file-input-label {
    background: #111;
    border-color: #333;
}

[data-bs-theme="dark"] .reorder-footer {
    background: #1a1a1a !important;
}

.extra-small {
    font-size: 0.7rem;
}

/* 縮圖項目容器：縮小尺寸 */
.img-item {
    background: #fff;
    border: 1px solid rgba(0,0,0,0.05) !important;
    transition: all 0.3s ease;
    cursor: grab;
    width: 140px; /* 固定寬度，讓一排可以塞更多個 */
    height: 180px; /* 固定高度，避免長短不一 */
    margin: 5px;
    display: inline-block;
    border-radius: 8px;
}

/* 圖片容器：確保圖片在框內完美縮放 */
.img-wrapper {
    width: 100%;
    height: 100%;
    padding: 8px;
    display: flex;
    align-items: center;
    justify-content: center;
    background: #fdfdfd;
}

.img-item img {
    max-width: 100%;
    max-height: 100%;
    object-fit: contain; /* 保持比例 */
    box-shadow: 0 2px 5px rgba(0,0,0,0.08); /* 給 PDF 邊緣一點點陰影，像紙張 */
}

/* 懸浮遮罩：縮小圖示尺寸 */
.action-icon-btn, .drag-handle-icon {
    width: 30px !important;
    height: 30px !important;
    font-size: 14px;
    cursor: grab;
}

.drag-handle-icon i {
    cursor: grab;
}

/* 頁碼標籤：調小一點 */
.page-tag {
    font-size: 0.65rem !important;
    padding: 1px 6px !important;
}

[data-bs-theme="dark"] .img-wrapper {
    background: #252525;
}
.page-chip {
    width: 120px; /* 縮圖寬度 */
    min-height: 150px;
    border: 2px solid #ddd;
    border-radius: 8px;
    background: #fff;
    display: inline-flex;
    flex-direction: column;
    align-items: center;
    justify-content: space-between;
    padding: 5px;
    margin: 5px;
    cursor: pointer;
    transition: all 0.2s ease;
    position: relative;
    vertical-align: top;
}

/* 選取狀態 */
.page-chip.active { border-color: #4b39b5; box-shadow: 0 0 10px rgba(75, 57, 181, 0.3); }
.page-chip:not(.active) { opacity: 0.5; filter: grayscale(1); }

/* 縮圖容器 */
.thumb-container {
    width: 100%;
    height: 120px;
    display: flex;
    align-items: center;
    justify-content: center;
    overflow: hidden;
    pointer-events: none; /* 防止干擾點擊 */
}

.thumb-container canvas {
    max-width: 100%;
    max-height: 100%;
    transition: transform 0.3s cubic-bezier(0.4, 0, 0.2, 1);
}

/* 頁碼標籤 */
.page-label {
    font-size: 12px;
    font-weight: bold;
    color: #666;
    margin-top: 5px;
}

/* 旋轉角度控制 */
.page-chip[data-rotate="90"] canvas { transform: rotate(90deg); }
.page-chip[data-rotate="180"] canvas { transform: rotate(180deg); }
.page-chip[data-rotate="270"] canvas { transform: rotate(270deg); }

.img-item[data-rotate="90"] .img-wrapper img, 
.img-item[data-rotate="270"] .img-wrapper img {
    max-width: 150px; /* 這裡根據你的網格大小微調，防止橫過來時爆開 */
}
/* 修改 img-item 讓它有固定大小且垂直排列 */
.img-item {
    width: 160px;
    height: 220px;
    background: #fff;
    display: flex;
    flex-direction: column;
}

/* 圖片容器：核心在於 overflow: hidden 且居中 */
.img-wrapper {
    flex: 1;
    width: 100%;
    height: 100%;
    display: flex;
    align-items: center;
    justify-content: center;
    overflow: hidden;
    background: #f0f0f0;
    position: relative;
}

/* 圖片本身：核心在於 object-fit: contain */
.img-wrapper img {
    max-width: 100%;
    max-height: 100%;
    object-fit: contain; /* 確保不管怎麼轉，圖片都會縮在容器內 */
    transition: transform 0.3s ease;
    transform-origin: center center;
}

/* 當旋轉 90 或 270 度時，圖片的「寬」會變成「高」，需要限制它的寬度 */
.img-item[data-rotate="90"] img, 
.img-item[data-rotate="270"] img {
    /* 這裡的長寬比會反轉，我們強制它縮小以適應原本的寬度 */
    max-width: 220px; /* 原本的高度 */
    max-height: 160px; /* 原本的寬度 */
}