/* ============================================================
   文档扫描应用 - 样式表
   移动优先设计，暗色主题支持
   ============================================================ */

:root {
    --primary: #2563eb;
    --primary-dark: #1d4ed8;
    --primary-light: #3b82f6;
    --bg: #f1f5f9;
    --bg-card: #ffffff;
    --bg-dark: #0f172a;
    --bg-card-dark: #1e293b;
    --text: #1e293b;
    --text-secondary: #64748b;
    --text-dark: #f1f5f9;
    --border: #e2e8f0;
    --border-dark: #334155;
    --shadow: 0 1px 3px rgba(0,0,0,0.1);
    --shadow-lg: 0 10px 25px rgba(0,0,0,0.15);
    --radius: 12px;
    --radius-sm: 8px;
    --safe-bottom: env(safe-area-inset-bottom, 0px);
}

* {
    margin: 0;
    padding: 0;
    box-sizing: border-box;
    -webkit-tap-highlight-color: transparent;
}

html, body {
    height: 100%;
    overflow: hidden;
    font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto,
                 "Helvetica Neue", Arial, "Noto Sans SC", sans-serif;
    background: var(--bg);
    color: var(--text);
    user-select: none;
    -webkit-user-select: none;
}

/* ============================================================
   布局
   ============================================================ */

body {
    display: flex;
    flex-direction: column;
    position: relative;
}

.toolbar {
    display: flex;
    align-items: center;
    padding: 8px 16px;
    background: var(--bg-card);
    z-index: 10;
    flex-shrink: 0;
}

.top-toolbar {
    justify-content: space-between;
    padding-top: calc(8px + env(safe-area-inset-top, 0px));
    box-shadow: 0 1px 3px rgba(0,0,0,0.08);
}

.bottom-toolbar {
    justify-content: space-around;
    padding-bottom: calc(8px + var(--safe-bottom));
    box-shadow: 0 -1px 3px rgba(0,0,0,0.08);
}

.toolbar-left {
    display: flex;
    align-items: center;
    gap: 12px;
}

.toolbar-right {
    display: flex;
    gap: 8px;
}

.app-title {
    font-size: 20px;
    font-weight: 700;
    letter-spacing: -0.5px;
}

.page-indicator {
    font-size: 13px;
    color: var(--text-secondary);
    background: var(--bg);
    padding: 2px 10px;
    border-radius: 20px;
}

/* ============================================================
   预览区
   ============================================================ */

.preview-area {
    flex: 1;
    min-height: 0;     /* 关键: 让 flex-basis:0 能正确计算高度 */
    display: flex;
    align-items: center;
    justify-content: center;
    overflow: hidden;
    position: relative;
    background: #e2e8f0;
}

/* 满屏预览 — 移动端 */
@media (max-width: 768px) {
    body {
        position: fixed;
        width: 100%;
        height: 100%;
    }
    .top-toolbar {
        padding: 4px 12px;
        padding-top: calc(4px + env(safe-area-inset-top, 0px));
        min-height: 44px;
    }
    .bottom-toolbar {
        padding: 4px 8px;
        padding-bottom: calc(4px + var(--safe-bottom));
        min-height: 52px;
    }
    .app-title { font-size: 17px; }
    .tool-btn span { display: none; }
    .tool-btn { padding: 4px 8px; gap: 0; }

    .preview-area {
        flex: 1 1 0;
        min-height: 0;
    }

    /* 移动端满屏 */
    .image-container { width: 100%; height: 100%; }
    .result-preview { width: 100%; height: 100%; }
    .result-preview img { width: 100%; height: 100%; object-fit: contain; }
    #mainCanvas { border-radius: 0; box-shadow: none; }
}

/* 空状态 */
.empty-state {
    text-align: center;
    padding: 40px 20px;
}

.empty-icon {
    margin-bottom: 16px;
    opacity: 0.6;
}

.empty-text {
    font-size: 16px;
    color: var(--text-secondary);
    margin-bottom: 4px;
}

.empty-hint {
    font-size: 13px;
    color: #94a3b8;
}

/* 图片容器 */
.image-container {
    position: relative;
    width: 100%;
    height: 100%;
    min-height: 0;    /* 百分比高度链传递 */
}

.canvas-wrapper {
    position: relative;
    width: 100%;
    height: 100%;
    min-height: 0;    /* 百分比高度链传递 */
    display: flex;
    align-items: center;
    justify-content: center;
}

#mainCanvas {
    max-width: 100%;
    max-height: 100%;
    object-fit: contain;
    border-radius: 4px;
    box-shadow: 0 2px 8px rgba(0,0,0,0.15);
}

.result-preview {
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    display: flex;
    align-items: center;
    justify-content: center;
    background: #e2e8f0;
}

.result-preview img {
    max-width: 100%;
    max-height: 100%;
    object-fit: contain;
}

/* 处理中的加载遮罩 */
.loading-overlay {
    position: absolute;
    inset: 0;
    background: rgba(0,0,0,0.5);
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    z-index: 20;
    color: white;
    gap: 12px;
}

.spinner {
    width: 40px;
    height: 40px;
    border: 4px solid rgba(255,255,255,0.3);
    border-top-color: white;
    border-radius: 50%;
    animation: spin 0.8s linear infinite;
}

@keyframes spin {
    to { transform: rotate(360deg); }
}

/* ============================================================
   底部工具栏按钮
   ============================================================ */

.tool-btn {
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 2px;
    background: none;
    border: none;
    color: var(--text-secondary);
    font-size: 11px;
    padding: 4px 12px;
    cursor: pointer;
    transition: color 0.2s;
    border-radius: 8px;
}

.tool-btn:active {
    background: rgba(0,0,0,0.05);
}

.tool-btn:disabled {
    opacity: 0.35;
    cursor: not-allowed;
}

.tool-btn:disabled:active {
    background: none;
}

.tool-btn:hover:not(:disabled) {
    color: var(--primary);
}

.icon-btn {
    background: none;
    border: none;
    color: var(--text-secondary);
    padding: 6px;
    cursor: pointer;
    border-radius: 8px;
    transition: background 0.2s;
}

.icon-btn:hover {
    background: rgba(0,0,0,0.05);
}

.icon-btn:disabled {
    opacity: 0.35;
    cursor: not-allowed;
}

.text-btn {
    background: none;
    border: none;
    color: var(--primary);
    padding: 4px 12px;
    cursor: pointer;
    border-radius: 6px;
    font-size: 14px;
    font-weight: 600;
    transition: background 0.2s;
}

.text-btn:hover {
    background: rgba(37,99,235,0.08);
}

.text-btn:disabled {
    opacity: 0.35;
    cursor: not-allowed;
    color: var(--text-secondary);
}

/* 顶部纯文字按钮 */
.top-text-btn {
    background: none;
    border: none;
    color: var(--text-secondary);
    padding: 4px 8px;
    cursor: pointer;
    font-size: 14px;
    border-radius: 6px;
    transition: background 0.2s;
}
.top-text-btn:hover {
    background: rgba(0,0,0,0.05);
}
.top-text-btn:disabled {
    opacity: 0.35;
    cursor: not-allowed;
}

/* ============================================================
   底部弹出面板 (Bottom Sheet)
   ============================================================ */

.bottom-sheet-overlay {
    position: fixed;
    inset: 0;
    background: rgba(0,0,0,0.4);
    z-index: 100;
    display: flex;
    align-items: flex-end;
    animation: fadeIn 0.2s ease;
}

.bottom-sheet {
    width: 100%;
    max-height: 80vh;
    background: var(--bg-card);
    border-radius: 16px 16px 0 0;
    display: flex;
    flex-direction: column;
    animation: slideUp 0.3s ease;
    padding-bottom: var(--safe-bottom);
}

.bottom-sheet-lg {
    max-height: 85vh;
}

.sheet-handle {
    width: 36px;
    height: 4px;
    background: #d1d5db;
    border-radius: 2px;
    margin: 8px auto;
    flex-shrink: 0;
}

.sheet-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 8px 20px 12px;
    border-bottom: 1px solid var(--border);
}

.sheet-header h3 {
    font-size: 18px;
    font-weight: 600;
}

.sheet-close {
    background: none;
    border: none;
    font-size: 20px;
    color: var(--text-secondary);
    cursor: pointer;
    padding: 4px 8px;
    border-radius: 6px;
}

.sheet-close:hover {
    background: rgba(0,0,0,0.05);
}

.sheet-body {
    padding: 16px 20px;
    overflow-y: auto;
    flex: 1;
}

@keyframes fadeIn {
    from { opacity: 0; }
    to { opacity: 1; }
}

@keyframes slideUp {
    from { transform: translateY(100%); }
    to { transform: translateY(0); }
}

/* ============================================================
   滤镜面板
   ============================================================ */

.filter-grid {
    display: grid;
    grid-template-columns: repeat(3, 1fr);
    gap: 12px;
    margin-bottom: 20px;
}

.filter-option {
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 6px;
    padding: 12px 8px;
    background: var(--bg);
    border: 2px solid transparent;
    border-radius: var(--radius-sm);
    cursor: pointer;
    transition: all 0.2s;
}

.filter-option.active {
    border-color: var(--primary);
    background: #eff6ff;
}

.filter-option:active {
    transform: scale(0.96);
}

.filter-preview {
    width: 100%;
    height: 56px;
    border-radius: 4px;
    background: linear-gradient(135deg, #e2e8f0, #94a3b8);
    display: block;
    overflow: hidden;
}

/* 简单的预览指示器 */
.preview-original { background: linear-gradient(135deg, #f8fafc, #cbd5e1); }
.preview-grayscale { background: linear-gradient(135deg, #e2e8f0, #64748b); }
.preview-bw { background: linear-gradient(135deg, #fff, #000); }
.preview-adaptive { background: linear-gradient(135deg, #f1f5f9, #1e293b); }
.preview-enhance { background: linear-gradient(135deg, #dbeafe, #1e40af); }
.preview-magazine { background: linear-gradient(135deg, #fef3c7, #92400e); }

.filter-label {
    font-size: 12px;
    font-weight: 500;
    color: var(--text);
}

/* 滑块 */
.filter-sliders {
    margin-bottom: 16px;
}

.slider-group {
    margin-bottom: 12px;
}

.slider-group label {
    display: flex;
    justify-content: space-between;
    font-size: 13px;
    color: var(--text-secondary);
    margin-bottom: 4px;
}

.slider-group input[type="range"] {
    width: 100%;
    height: 6px;
    -webkit-appearance: none;
    appearance: none;
    background: var(--border);
    border-radius: 3px;
    outline: none;
}

.slider-group input[type="range"]::-webkit-slider-thumb {
    -webkit-appearance: none;
    appearance: none;
    width: 20px;
    height: 20px;
    border-radius: 50%;
    background: var(--primary);
    cursor: pointer;
    border: 2px solid white;
    box-shadow: 0 1px 3px rgba(0,0,0,0.2);
}

/* ============================================================
   格式面板
   ============================================================ */

.format-list {
    display: flex;
    flex-direction: column;
    gap: 4px;
    margin-bottom: 16px;
}

.format-option {
    padding: 14px 16px;
    background: var(--bg);
    border: 2px solid transparent;
    border-radius: var(--radius-sm);
    font-size: 15px;
    text-align: left;
    cursor: pointer;
    transition: all 0.2s;
}

.format-option.active {
    border-color: var(--primary);
    background: #eff6ff;
    color: var(--primary);
    font-weight: 600;
}

/* ============================================================
   页面管理
   ============================================================ */

.pages-grid {
    display: grid;
    grid-template-columns: repeat(auto-fill, minmax(120px, 1fr));
    gap: 12px;
    margin-bottom: 16px;
    max-height: 50vh;
    overflow-y: auto;
}

.page-thumb {
    position: relative;
    aspect-ratio: 210 / 297;
    background: var(--bg);
    border: 2px solid var(--border);
    border-radius: var(--radius-sm);
    overflow: hidden;
    cursor: pointer;
    transition: border-color 0.2s;
}

.page-thumb.active {
    border-color: var(--primary);
}

.page-thumb img {
    width: 100%;
    height: 100%;
    object-fit: cover;
}

.page-thumb .page-num {
    position: absolute;
    top: 4px;
    left: 4px;
    background: rgba(0,0,0,0.6);
    color: white;
    font-size: 11px;
    padding: 1px 6px;
    border-radius: 4px;
}

.page-thumb .page-delete {
    position: absolute;
    top: 4px;
    right: 4px;
    background: rgba(239,68,68,0.85);
    color: white;
    border: none;
    width: 22px;
    height: 22px;
    border-radius: 50%;
    font-size: 12px;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
}

.pages-actions {
    display: flex;
    gap: 8px;
}

/* ============================================================
   导出面板
   ============================================================ */

.export-info {
    text-align: center;
    font-size: 15px;
    color: var(--text-secondary);
    margin-bottom: 16px;
}

.export-options {
    display: flex;
    flex-direction: column;
    gap: 10px;
}

.export-options .btn {
    width: 100%;
    text-align: center;
}

/* 排版方式切换 */
.merge-toggle {
    display: flex;
    align-items: center;
    gap: 12px;
    margin-bottom: 16px;
    padding: 12px 0;
    border-top: 1px solid var(--border);
    border-bottom: 1px solid var(--border);
}

.merge-label {
    font-size: 14px;
    color: var(--text-secondary);
    flex-shrink: 0;
}

.toggle-group {
    display: flex;
    background: var(--bg);
    border-radius: 8px;
    overflow: hidden;
    flex: 1;
}

.toggle-btn {
    flex: 1;
    padding: 8px 12px;
    border: none;
    background: transparent;
    font-size: 14px;
    color: var(--text-secondary);
    cursor: pointer;
    transition: all 0.2s;
    border-radius: 6px;
}

.toggle-btn.active {
    background: var(--primary);
    color: white;
    font-weight: 600;
}

/* 页面管理工具栏 */
.pages-toolbar {
    display: flex;
    align-items: center;
    gap: 12px;
    margin-bottom: 12px;
    padding-bottom: 8px;
    border-bottom: 1px solid var(--border);
}

.pages-toolbar .checkbox-all {
    display: flex;
    align-items: center;
    gap: 6px;
    font-size: 13px;
    color: var(--text-secondary);
    cursor: pointer;
}

.pages-toolbar .checkbox-all input[type="checkbox"] {
    width: 16px;
    height: 16px;
    cursor: pointer;
}

.pages-toolbar #pagesCountInfo {
    font-size: 13px;
    color: var(--text-secondary);
    margin-left: auto;
}

.btn-sm {
    padding: 6px 12px;
    font-size: 13px;
}

/* 页面缩略图上的勾选框 */
.page-thumb {
    position: relative;
}

.page-thumb .page-checkbox {
    position: absolute;
    top: 4px;
    left: 4px;
    z-index: 2;
    width: 20px;
    height: 20px;
    cursor: pointer;
    accent-color: var(--primary);
    opacity: 0.9;
}

.page-thumb.selected {
    border-color: var(--primary);
    box-shadow: 0 0 0 2px var(--primary);
}

/* ============================================================
   通用按钮
   ============================================================ */

.btn {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    gap: 6px;
    padding: 10px 20px;
    border-radius: var(--radius-sm);
    font-size: 15px;
    font-weight: 600;
    border: none;
    cursor: pointer;
    transition: all 0.2s;
}

.btn:active {
    transform: scale(0.97);
}

.btn-primary {
    background: var(--primary);
    color: white;
}

.btn-primary:hover {
    background: var(--primary-dark);
}

.btn-outline {
    background: var(--bg);
    color: var(--text);
    border: 1px solid var(--border);
}

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

.btn-lg {
    padding: 14px 20px;
    font-size: 16px;
}

/* ============================================================
   工具类
   ============================================================ */

.hidden {
    display: none !important;
}

/* ============================================================
   响应式 - 横屏 / 平板
   ============================================================ */

@media (min-width: 768px) {
    .filter-grid {
        grid-template-columns: repeat(6, 1fr);
    }

    .bottom-sheet {
        max-width: 480px;
        margin: 0 auto;
        border-radius: 16px;
        margin-bottom: 20px;
    }

    .bottom-sheet-overlay {
        justify-content: center;
    }

    #mainCanvas {
        max-height: calc(100vh - 120px);
    }
}

@media (orientation: landscape) and (max-height: 500px) {
    .top-toolbar {
        padding: 4px 16px;
    }

    .app-title {
        font-size: 16px;
    }

    .tool-btn span {
        display: none;
    }

    #mainCanvas {
        max-height: calc(100vh - 90px);
    }
}
