/* Scan Tool - Single-Screen Workspace Layout */
:root {
    --scan-bg: #f8fafc;
    --scan-surface: #ffffff;
    --scan-primary: #2563eb;
    --scan-primary-hover: #1d4ed8;
    --scan-text: #1e293b;
    --scan-text-secondary: #64748b;
    --scan-border: #e2e8f0;
    --scan-shadow-sm: 0 1px 2px rgba(0, 0, 0, 0.05);
    --scan-shadow-md: 0 4px 6px -1px rgba(0, 0, 0, 0.1);
    --scan-shadow-lg: 0 10px 15px -3px rgba(0, 0, 0, 0.1);
}

/* Drag-and-drop reorder styles */
.thumbnail-item {
    cursor: grab;
    transition: transform 0.2s ease, box-shadow 0.2s ease;
}

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

.sortable-ghost {
    opacity: 0.4;
    background: var(--scan-primary);
    border-radius: 8px;
}

.sortable-drag {
    background: var(--scan-surface);
    box-shadow: 0 8px 25px rgba(0, 0, 0, 0.25);
    transform: scale(1.05);
}

/* Workspace Container - Grid Layout */
.workspace-container {
    display: grid;
    grid-template-columns: 180px 1fr 280px;
    height: calc(100vh - var(--nav-height));
    background: var(--scan-bg);
    gap: 0;
    overflow: hidden;
}

/* Left: Thumbnail Sidebar */
.thumbnail-sidebar {
    background: white;
    border-right: 1px solid var(--scan-border);
    display: flex;
    flex-direction: column;
    overflow: hidden;
}

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

.sidebar-header h3 {
    font-size: 1.1rem;
    font-weight: 700;
    color: var(--scan-text);
    margin: 0;
}

.thumbnail-list {
    flex: 1;
    overflow-y: auto;
    padding: 10px;
    display: flex;
    flex-direction: column;
    gap: 8px;
}

.thumbnail-item {
    position: relative;
    aspect-ratio: 0.7;
    border: 2px solid var(--scan-border);
    border-radius: 8px;
    overflow: hidden;
    cursor: pointer;
    transition: all 0.2s;
    background: #f8fafc;
}

.thumbnail-item img {
    width: 100%;
    height: 100%;
    object-fit: contain;
}

.thumbnail-item:hover {
    border-color: #cbd5e1;
    transform: translateX(4px);
}

.thumbnail-item.active {
    border-color: var(--scan-primary);
    box-shadow: 0 0 0 3px rgba(37, 99, 235, 0.2);
}

.thumbnail-badge {
    position: absolute;
    bottom: 8px;
    left: 8px;
    background: rgba(37, 99, 235, 0.9);
    color: white;
    padding: 4px 8px;
    border-radius: 4px;
    font-size: 0.75rem;
    font-weight: 700;
}

/* Center: Canvas Area */
.canvas-area {
    display: flex;
    flex-direction: column;
    background: #f1f5f9;
    overflow: hidden;
    min-height: 0;
}

.canvas-toolbar {
    height: 48px;
    background: white;
    border-bottom: 1px solid var(--scan-border);
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 0 16px;
    gap: 12px;
    flex-shrink: 0;
}

.canvas-toolbar h2 {
    flex: 1;
    text-align: center;
    font-size: 1rem;
    font-weight: 700;
    color: var(--scan-text);
    margin: 0;
}

.canvas-container {
    flex: 1;
    display: flex;
    align-items: center;
    justify-content: center;
    padding: 8px;
    position: relative;
    overflow: hidden;
    min-height: 0;
    height: calc(100vh - var(--nav-height) - 48px);
}

#main-canvas,
#crop-overlay-canvas {
    max-width: calc(100% - 16px);
    max-height: calc(100% - 16px);
    width: auto;
    height: auto;
    object-fit: contain;
    box-shadow: 0 8px 30px rgba(0, 0, 0, 0.12);
    border-radius: 6px;
    background: white;
}

#crop-overlay-canvas {
    position: absolute;
    cursor: crosshair;
    touch-action: none;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
}

#crop-overlay-canvas.grabbing {
    cursor: grabbing;
}

.canvas-loading {
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
    background: rgba(255, 255, 255, 0.95);
    padding: 32px;
    border-radius: 16px;
    display: flex;
    flex-direction: column;
    align-items: center;
    box-shadow: 0 10px 25px rgba(0, 0, 0, 0.15);
    border: 1px solid var(--scan-border);
}

.canvas-loading span {
    color: var(--scan-text);
    font-weight: 600;
    margin-top: 12px;
}

/* Right: Control Panel */
.control-panel {
    background: white;
    border-left: 1px solid var(--scan-border);
    overflow-y: auto;
    padding: 16px;
    display: flex;
    flex-direction: column;
    gap: 16px;
}

.control-section {
    display: flex;
    flex-direction: column;
    gap: 10px;
}

.section-title {
    font-size: 0.85rem;
    text-transform: uppercase;
    letter-spacing: 0.5px;
    color: var(--scan-text-secondary);
    font-weight: 700;
    margin: 0;
}

.help-text {
    font-size: 0.85rem;
    color: var(--scan-text-secondary);
    line-height: 1.5;
    margin: 0;
}

/* Filter Grid */
.filter-grid {
    display: grid;
    grid-template-columns: repeat(2, 1fr);
    gap: 10px;
}

.filter-option {
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 4px;
    padding: 10px 6px;
    background-color: #f8fafc;
    border: 2px solid var(--scan-border);
    border-radius: 10px;
    cursor: pointer;
    transition: all 0.2s;
}

.filter-option.active {
    background-color: #eff6ff;
    border-color: var(--scan-primary);
    color: var(--scan-primary);
    box-shadow: 0 4px 6px -1px rgba(37, 99, 235, 0.2);
}

.filter-option:hover {
    background-color: #f1f5f9;
    border-color: #cbd5e1;
    transform: translateY(-2px);
}

.filter-icon {
    font-size: 1.2rem;
    margin-bottom: 2px;
}

.filter-name {
    font-size: 0.8rem;
    font-weight: 600;
}

/* Sliders */
.range-slider-container {
    display: flex;
    flex-direction: column;
    gap: 8px;
}

.range-header {
    display: flex;
    justify-content: space-between;
    font-size: 0.9rem;
    color: var(--scan-text);
    font-weight: 600;
}

input[type=range] {
    -webkit-appearance: none;
    -moz-appearance: none;
    appearance: none;
    width: 100%;
    height: 6px;
    background: #e2e8f0;
    border-radius: 3px;
    outline: none;
}

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

input[type=range]::-moz-range-thumb {
    width: 20px;
    height: 20px;
    background: var(--scan-primary);
    border-radius: 50%;
    cursor: pointer;
    border: 3px solid white;
    box-shadow: 0 2px 4px rgba(0, 0, 0, 0.2);
}

/* Crop Buttons */
.crop-buttons {
    display: flex;
    flex-direction: column;
    gap: 8px;
}

.btn-control {
    width: 100%;
    padding: 12px 16px;
    background: #f8fafc;
    border: 1px solid var(--scan-border);
    border-radius: 8px;
    font-weight: 600;
    font-size: 0.9rem;
    color: var(--scan-text);
    cursor: pointer;
    transition: all 0.2s;
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 8px;
}

.btn-control:hover {
    background: #eff6ff;
    border-color: var(--scan-primary);
    color: var(--scan-primary);
}

.btn-control.btn-danger {
    background: #fef2f2;
    border-color: #fecaca;
    color: #dc2626;
}

.btn-control.btn-danger:hover {
    background: #fee2e2;
    border-color: #ef4444;
}

/* Rotation Controls */
.rotation-controls {
    display: flex;
    justify-content: center;
    gap: 12px;
    padding: 8px;
    background-color: #f8fafc;
    border-radius: 12px;
    border: 1px solid var(--scan-border);
}

.btn-rotate {
    background: white;
    border: 1px solid var(--scan-border);
    color: var(--scan-text);
    padding: 10px 14px;
    cursor: pointer;
    font-size: 1.1rem;
    border-radius: 8px;
    transition: all 0.2s;
}

.btn-rotate:hover {
    background-color: #eff6ff;
    color: var(--scan-primary);
    border-color: var(--scan-primary);
    transform: translateY(-1px);
}

/* Buttons */
.btn-icon {
    background: none;
    border: none;
    color: var(--scan-text-secondary);
    font-size: 1.2rem;
    padding: 8px;
    cursor: pointer;
    border-radius: 8px;
    transition: all 0.2s;
}

.btn-icon:hover {
    background-color: #eff6ff;
    color: var(--scan-primary);
}

.btn-secondary {
    background: white;
    color: var(--scan-text-secondary);
    border: 1px solid var(--scan-border);
    padding: 10px 20px;
    border-radius: 8px;
    font-weight: 600;
    cursor: pointer;
    transition: all 0.2s;
    display: flex;
    align-items: center;
    gap: 8px;
}

.btn-secondary:hover {
    background: #f1f5f9;
    color: var(--scan-text);
    border-color: #cbd5e1;
}

.btn-primary {
    background: linear-gradient(135deg, var(--scan-primary) 0%, #1d4ed8 100%);
    color: white;
    border: none;
    padding: 10px 20px;
    border-radius: 8px;
    font-weight: 600;
    cursor: pointer;
    transition: all 0.2s;
    display: flex;
    align-items: center;
    gap: 8px;
    box-shadow: 0 4px 6px -1px rgba(37, 99, 235, 0.3);
}

.btn-primary:hover {
    transform: translateY(-1px);
    box-shadow: 0 6px 10px -1px rgba(37, 99, 235, 0.4);
}

.btn-primary-large {
    background: linear-gradient(135deg, var(--scan-primary) 0%, #1d4ed8 100%);
    color: white;
    border: none;
    padding: 16px;
    border-radius: 12px;
    font-weight: 700;
    font-size: 1rem;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 8px;
    transition: all 0.2s;
    box-shadow: 0 4px 6px -1px rgba(37, 99, 235, 0.3);
    width: 100%;
}

.btn-primary-large:hover {
    transform: translateY(-2px);
    box-shadow: 0 10px 15px -3px rgba(37, 99, 235, 0.4);
}

.btn-text {
    background: none;
    border: none;
    color: var(--scan-text-secondary);
    padding: 8px;
    cursor: pointer;
    text-decoration: underline;
    font-weight: 600;
    width: 100%;
    text-align: center;
}

.btn-text:hover {
    color: var(--scan-text);
}

/* Camera Modal */
.camera-modal {
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background: rgba(0, 0, 0, 0.85);
    display: flex;
    align-items: center;
    justify-content: center;
    z-index: 3500;
    backdrop-filter: blur(5px);
}

.camera-modal-content {
    width: 90%;
    max-width: 800px;
    max-height: 90vh;
    background: white;
    border-radius: 16px;
    overflow: hidden;
    display: flex;
    flex-direction: column;
}

.camera-modal-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 20px 24px;
    border-bottom: 1px solid var(--scan-border);
}

.camera-modal-header h3 {
    margin: 0;
    font-size: 1.25rem;
    color: var(--scan-text);
    font-weight: 700;
}

.camera-view {
    position: relative;
    flex: 1;
    background: #1e293b;
    display: flex;
    align-items: center;
    justify-content: center;
}

#camera-video {
    width: 100%;
    height: 100%;
    object-fit: contain;
}

.camera-overlay {
    position: absolute;
    bottom: 0;
    left: 0;
    width: 100%;
    padding: 24px;
    display: flex;
    justify-content: center;
    align-items: center;
    background: linear-gradient(to top, rgba(0, 0, 0, 0.6), transparent);
}

.btn-capture {
    width: 72px;
    height: 72px;
    border-radius: 50%;
    background-color: white;
    border: 4px solid rgba(255, 255, 255, 0.3);
    cursor: pointer;
    transition: transform 0.2s;
}

.btn-capture:hover {
    transform: scale(1.05);
}

.btn-gallery {
    position: absolute;
    left: 32px;
    color: white;
    font-size: 24px;
    background: rgba(0, 0, 0, 0.3);
    border: none;
    cursor: pointer;
    padding: 12px;
    border-radius: 50%;
    backdrop-filter: blur(10px);
}

/* Method Modal (Camera/Upload Choice) */
.method-modal-content {
    background: white;
    padding: 2.5rem;
    border-radius: 16px;
    text-align: center;
    max-width: 90%;
    width: 420px;
    box-shadow: 0 20px 50px rgba(0, 0, 0, 0.3);
}

.method-modal-content h3 {
    margin: 0 0 2rem 0;
    font-size: 1.5rem;
    color: var(--scan-text);
    font-weight: 700;
}

.method-buttons {
    display: flex;
    flex-direction: column;
    gap: 12px;
}

.btn-secondary-large {
    background: white;
    color: var(--scan-text);
    border: 2px solid var(--scan-border);
    padding: 16px;
    border-radius: 12px;
    font-weight: 700;
    font-size: 1rem;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 8px;
    transition: all 0.2s;
    width: 100%;
}

.btn-secondary-large:hover {
    background: #f8fafc;
    border-color: var(--scan-primary);
    color: var(--scan-primary);
}

/* Loading Spinner */
.spinner-light {
    width: 40px;
    height: 40px;
    border: 4px solid #e2e8f0;
    border-radius: 50%;
    border-top-color: var(--scan-primary);
    animation: spin 1s ease-in-out infinite;
}

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

/* Utility */
.hidden {
    display: none !important;
}

/* Responsive */
@media (max-width: 1024px) {
    .workspace-container {
        grid-template-columns: 200px 1fr 300px;
    }
}

/* Mobile-First Design - App-like Interface */
@media (max-width: 768px) {

    /* Hide header when in workspace for full-screen app feel */
    .workspace-container~nav,
    .workspace-container~header {
        display: none;
    }

    .workspace-container {
        grid-template-columns: 1fr;
        grid-template-rows: 60px 1fr auto;
        height: 100vh;
        position: fixed;
        top: 0;
        left: 0;
        right: 0;
        bottom: 0;
    }

    /* Canvas Area - Full Screen */
    .canvas-area {
        grid-row: 2;
        background: #000;
        min-height: 0;
    }

    .canvas-toolbar {
        height: 60px;
        padding: 0 12px;
        background: rgba(255, 255, 255, 0.98);
        backdrop-filter: blur(10px);
    }

    .canvas-toolbar h2 {
        font-size: 1.1rem;
    }

    .canvas-container {
        padding: 0;
        background: #000;
        flex: 1;
        min-height: 0;
    }

    #main-canvas,
    #crop-overlay-canvas {
        max-width: 100%;
        max-height: 100%;
        border-radius: 0;
    }

    /* Thumbnail Sidebar - Horizontal Strip */
    .thumbnail-sidebar {
        grid-row: 3;
        border-right: none;
        border-top: 1px solid var(--scan-border);
        height: auto;
        min-height: 100px;
        max-height: 100px;
        background: rgba(255, 255, 255, 0.98);
        backdrop-filter: blur(10px);
        z-index: 100;
        position: relative;
        display: flex;
        flex-direction: column;
    }

    .sidebar-header {
        padding: 6px 12px;
        min-height: 32px;
        flex-shrink: 0;
    }

    .sidebar-header h3 {
        font-size: 0.85rem;
    }

    .sidebar-header .btn-icon {
        font-size: 1.2rem;
        padding: 8px;
    }

    .thumbnail-list {
        flex-direction: row;
        overflow-x: auto;
        overflow-y: hidden;
        padding: 4px 12px 8px 12px;
        gap: 8px;
        flex: 1;
        min-height: 0;
    }

    .thumbnail-item {
        min-width: 45px;
        max-width: 45px;
        height: 55px;
        aspect-ratio: auto;
        flex-shrink: 0;
    }

    .thumbnail-item:hover {
        transform: none;
    }

    .thumbnail-badge {
        font-size: 0.65rem;
        padding: 1px 4px;
        bottom: 2px;
        left: 2px;
    }

    /* Control Panel - Fixed Above Thumbnails */
    .control-panel {
        position: fixed;
        bottom: 100px;
        left: 0;
        right: 0;
        height: auto;
        max-height: calc(100vh - 160px);
        border-left: none;
        border-top: 1px solid var(--scan-border);
        border-radius: 16px 16px 0 0;
        padding: 0;
        transform: translateY(calc(100% - 56px));
        transition: transform 0.3s cubic-bezier(0.4, 0.0, 0.2, 1);
        z-index: 200;
        overflow-y: auto;
        overflow-x: hidden;
        box-shadow: 0 -4px 20px rgba(0, 0, 0, 0.15);
        background: white;
    }

    .control-panel.expanded {
        transform: translateY(0);
    }

    /* Draggable Handle */
    .control-panel::before {
        content: '';
        position: absolute;
        top: 8px;
        left: 50%;
        transform: translateX(-50%);
        width: 36px;
        height: 4px;
        background: #cbd5e1;
        border-radius: 2px;
        z-index: 10;
    }

    /* Bottom Sheet Header - Tappable Handle Area */
    .control-panel>*:first-child {
        padding-top: 20px;
    }

    .control-section {
        padding: 0 16px;
        gap: 12px;
    }

    .control-section:last-child {
        padding-bottom: 16px;
    }

    .section-title {
        font-size: 0.75rem;
        margin-top: 6px;
    }

    /* Filter options in a horizontal scroll for mobile */
    .filter-options {
        display: flex;
        flex-wrap: nowrap;
        overflow-x: auto;
        gap: 8px;
        padding-bottom: 8px;
    }

    .filter-option {
        padding: 10px 6px;
        min-height: 52px;
        min-width: 70px;
        flex-shrink: 0;
    }

    .filter-icon {
        font-size: 1.3rem;
    }

    .filter-name {
        font-size: 0.75rem;
    }

    /* Enhanced Sliders for Touch */
    input[type=range] {
        height: 6px;
    }

    input[type=range]::-webkit-slider-thumb {
        width: 24px;
        height: 24px;
        border-width: 3px;
    }

    input[type=range]::-moz-range-thumb {
        width: 24px;
        height: 24px;
        border-width: 3px;
    }

    .range-slider-container {
        gap: 8px;
        margin-bottom: 6px;
    }

    .range-header {
        font-size: 0.85rem;
    }

    /* Touch-Optimized Buttons */
    .btn-control {
        padding: 14px;
        font-size: 0.95rem;
        min-height: 48px;
    }

    .btn-rotate {
        padding: 12px 16px;
        font-size: 1.2rem;
        min-width: 48px;
        min-height: 48px;
    }

    .crop-buttons {
        gap: 8px;
    }

    /* Toolbar Buttons - Larger */
    .btn-secondary {
        padding: 10px 14px;
        font-size: 0.9rem;
    }

    .btn-primary {
        padding: 10px 18px;
        font-size: 0.9rem;
    }

    /* Camera Modal - Full Screen on Mobile */
    .camera-modal-content {
        width: 100%;
        max-width: 100%;
        height: 100%;
        max-height: 100%;
        border-radius: 0;
    }

    .camera-view {
        flex: 1;
    }

    .btn-capture {
        width: 80px;
        height: 80px;
    }

    .btn-gallery {
        font-size: 28px;
        padding: 16px;
    }

    /* Method Modal - Optimized for Mobile */
    .method-modal-content {
        width: 90%;
        max-width: 340px;
        padding: 2rem 1.5rem;
    }

    .method-modal-content h3 {
        font-size: 1.3rem;
        margin-bottom: 1.5rem;
    }

    .btn-primary-large,
    .btn-secondary-large {
        padding: 18px;
        font-size: 1.05rem;
        min-height: 56px;
    }

    /* Crop Mode on Mobile */
    #crop-mode-controls {
        position: fixed;
        bottom: 0;
        left: 0;
        right: 0;
        background: white;
        padding: 20px;
        border-radius: 20px 20px 0 0;
        box-shadow: 0 -10px 40px rgba(0, 0, 0, 0.2);
        z-index: 2500;
    }

    #crop-mode-controls .btn-primary-large {
        margin-bottom: 8px;
    }

    /* Hide checkbox on mobile, make it a switch style */
    .checkbox-control {
        background: #f8fafc;
        padding: 14px;
        border-radius: 10px;
        margin-top: 8px !important;
    }

    .checkbox-control input[type="checkbox"] {
        width: 48px !important;
        height: 24px !important;
        position: relative;
        appearance: none;
        -webkit-appearance: none;
        background: #cbd5e1;
        border-radius: 12px;
        transition: all 0.3s;
        cursor: pointer;
    }

    .checkbox-control input[type="checkbox"]:checked {
        background: var(--scan-primary);
    }

    .checkbox-control input[type="checkbox"]::before {
        content: '';
        position: absolute;
        width: 20px;
        height: 20px;
        border-radius: 50%;
        background: white;
        top: 2px;
        left: 2px;
        transition: all 0.3s;
        box-shadow: 0 2px 4px rgba(0, 0, 0, 0.2);
    }

    .checkbox-control input[type="checkbox"]:checked::before {
        left: 26px;
    }

    .checkbox-control label {
        font-size: 0.95rem !important;
    }

    /* Loading Overlay - Full Screen */
    .canvas-loading {
        width: 100%;
        height: 100%;
        border-radius: 0;
        padding: 40px 20px;
    }
}

/* Extra Small Devices */
@media (max-width: 480px) {
    .filter-grid {
        gap: 8px;
    }

    .filter-option {
        padding: 12px 6px;
        min-height: 60px;
    }

    .control-section {
        padding: 0 16px;
    }

    .method-modal-content {
        padding: 1.5rem 1.25rem;
    }
}

/* iOS Safe Area Support */
@supports (padding-bottom: env(safe-area-inset-bottom)) {
    @media (max-width: 768px) {
        .control-panel {
            padding-bottom: calc(env(safe-area-inset-bottom) + 20px);
        }

        .thumbnail-sidebar {
            padding-bottom: env(safe-area-inset-bottom);
        }

        #crop-mode-controls {
            padding-bottom: calc(env(safe-area-inset-bottom) + 20px);
        }
    }
}