/**
 * Document Scanner - Mobile-First Stylesheet
 * Responsive layout, touch-friendly handles, accessibility, dark mode.
 */

:root {
    --scanner-handle-size: 44px;
    --scanner-touch-min: 44px;
}

[data-theme="dark"] {
    --scanner-handle-size: 44px;
    --scanner-touch-min: 44px;
}

.scanner-page {
    background-color: var(--bg-primary);
    color: var(--text-primary);
    margin: 0;
    padding: env(safe-area-inset-top, 16px) env(safe-area-inset-right, 16px) env(safe-area-inset-bottom, 16px) env(safe-area-inset-left, 16px);
    font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
    min-height: 100vh;
    box-sizing: border-box;
}

.scanner-root {
    max-width: 1024px;
    margin: 0 auto;
    display: flex;
    flex-direction: column;
    gap: 1.25rem;
}

.scanner-header {
    display: flex;
    flex-direction: column;
    gap: 0.5rem;
    padding-bottom: 0.75rem;
    border-bottom: 1px solid var(--border-color);
}

.scanner-title {
    font-size: 1.5rem;
    font-weight: 700;
    color: var(--primary);
    margin: 0;
}

.scanner-storage-status {
    display: flex;
    align-items: center;
    gap: 0.5rem;
    font-size: 0.875rem;
    color: var(--text-muted);
}

.scanner-card {
    background: var(--bg-card);
    border: 1px solid var(--border-color);
    border-radius: var(--border-radius-sm);
    padding: 1.25rem;
}

.source-actions-grid {
    display: grid;
    grid-template-columns: 1fr;
    gap: 0.75rem;
}

@media (min-width: 640px) {
    .source-actions-grid {
        grid-template-columns: repeat(3, 1fr);
    }
}

.btn {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    gap: 0.5rem;
    min-height: var(--scanner-touch-min);
    min-width: var(--scanner-touch-min);
    padding: 0.625rem 1.25rem;
    font-size: 1rem;
    font-weight: 600;
    border-radius: var(--border-radius-sm);
    border: 1px solid transparent;
    cursor: pointer;
    text-decoration: none;
    transition: opacity 0.2s ease, background-color 0.2s ease;
    user-select: none;
    -webkit-user-select: none;
    touch-action: manipulation;
}

.btn:focus-visible {
    outline: 2px solid var(--primary);
    outline-offset: 2px;
}

.btn:disabled,
.btn[aria-disabled="true"] {
    opacity: 0.5;
    cursor: not-allowed;
    pointer-events: none;
}

.btn-primary {
    background-color: var(--primary);
    color: #ffffff !important;
    border: 1px solid rgba(255, 255, 255, 0.15);
    box-shadow: 0 4px 12px rgba(79, 70, 229, 0.25);
}

.btn-primary:hover:not(:disabled) {
    background-color: var(--primary-dark);
    color: #ffffff !important;
    box-shadow: 0 6px 16px rgba(79, 70, 229, 0.35);
}

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

.btn-secondary:hover:not(:disabled) {
    background-color: var(--bg-hover);
    border-color: var(--border-highlight);
}

.btn-outline {
    background-color: transparent;
    border-color: var(--border-color);
    color: var(--text-primary);
}

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

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

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

.btn-sm {
    min-height: 36px;
    padding: 0.375rem 0.75rem;
    font-size: 0.875rem;
}

.btn-icon-only {
    padding: 0.5rem;
    border-radius: 50%;
    min-width: var(--scanner-touch-min);
    min-height: var(--scanner-touch-min);
    background: var(--bg-card);
    border: 1px solid var(--border-color);
    color: var(--text-primary);
}

.scanner-dialog {
    border: 1px solid var(--border-color);
    border-radius: var(--border-radius);
    background: var(--bg-card);
    color: var(--text-primary);
    padding: 1.5rem;
    max-width: 90vw;
    width: 600px;
}

.scanner-dialog::backdrop {
    background: var(--bg-base);
    opacity: 0.8;
}

.crop-viewport {
    position: relative;
    width: 100%;
    max-height: 60vh;
    overflow: hidden;
    display: flex;
    align-items: center;
    justify-content: center;
    background: var(--bg-base);
    border-radius: var(--border-radius-sm);
    padding: 0.5rem;
    box-sizing: border-box;
}

.crop-canvas-container {
    position: relative;
    display: inline-block;
    width: fit-content;
    height: fit-content;
    touch-action: none;
    line-height: 0;
}

#cropCanvas {
    display: block;
    touch-action: none;
}

.crop-handle {
    position: absolute;
    width: var(--scanner-handle-size);
    height: var(--scanner-handle-size);
    margin-left: calc(var(--scanner-handle-size) / -2);
    margin-top: calc(var(--scanner-handle-size) / -2);
    border-radius: 50%;
    background: var(--primary);
    border: 2px solid var(--text-primary);
    cursor: grab;
    touch-action: none;
    z-index: 100 !important;
    pointer-events: auto !important;
}

.crop-handle[data-dragging="true"] {
    cursor: grabbing;
}

.crop-handle:focus-visible {
    outline: 3px solid var(--warning);
    outline-offset: 2px;
}

.filter-pills {
    display: flex;
    flex-wrap: wrap;
    gap: 0.5rem;
    margin: 1rem 0;
}

.btn-filter {
    background: var(--bg-base);
    border: 1px solid var(--border-color);
    color: var(--text-primary);
    border-radius: 9999px;
    padding: 0.375rem 1rem;
    font-size: 0.875rem;
    pointer-events: auto !important;
    z-index: 10 !important;
}

.btn-filter.active {
    background: var(--primary);
    color: var(--text-primary);
    border-color: var(--primary);
}

.thumbnail-virtual-grid {
    display: grid;
    grid-template-columns: repeat(auto-fill, minmax(140px, 1fr));
    gap: 1rem;
    max-height: 65vh;
    overflow-y: auto;
    padding: 0.75rem;
    border: 1px solid var(--border-color);
    border-radius: var(--border-radius-sm);
}

/* Scanner Thumbnail Card */
.scanner-thumb-card {
    position: relative;
    display: flex;
    flex-direction: column;
    background: var(--bg-card, #ffffff);
    border: 2px solid var(--border-color, #e2e8f0);
    border-radius: 8px;
    overflow: hidden;
    cursor: pointer;
    transition: transform 0.15s ease, border-color 0.2s ease, box-shadow 0.2s ease;
    user-select: none;
    -webkit-user-select: none;
}

.scanner-thumb-card:hover {
    border-color: var(--primary, #3b82f6);
    box-shadow: 0 4px 12px rgba(0, 0, 0, 0.08);
}

.scanner-thumb-card.selected {
    border-color: var(--primary, #3b82f6);
    box-shadow: 0 0 0 2px rgba(59, 130, 246, 0.35);
}

.scanner-thumb-card.is-processing {
    border-color: #38bdf8;
    box-shadow: 0 0 14px rgba(56, 189, 248, 0.4);
    animation: thumbPulseBorder 2s infinite ease-in-out;
}

@keyframes thumbPulseBorder {
    0%, 100% { border-color: #38bdf8; box-shadow: 0 0 8px rgba(56, 189, 248, 0.3); }
    50% { border-color: #0284c7; box-shadow: 0 0 16px rgba(2, 132, 199, 0.5); }
}

.thumb-header {
    padding: 4px 8px;
    font-size: 0.75rem;
    font-weight: 600;
    background: var(--bg-secondary, #f8fafc);
    color: var(--text-secondary, #475569);
    display: flex;
    justify-content: space-between;
    align-items: center;
    border-bottom: 1px solid var(--border-color, #e2e8f0);
}

.scanner-thumb-card.is-processing .thumb-header {
    background: #f0f9ff;
    color: #0369a1;
}

.thumb-image-wrapper {
    position: relative;
    width: 100%;
    aspect-ratio: 3 / 4;
    display: flex;
    align-items: center;
    justify-content: center;
    background: #0f172a;
    overflow: hidden;
}

.thumb-img {
    width: 100%;
    height: 100%;
    object-fit: contain;
    display: block;
    transition: opacity 0.25s ease;
}

/* Cyber Laser Scanner Overlay */
.thumb-processing-overlay {
    position: absolute;
    inset: 0;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: flex-end;
    padding-bottom: 8px;
    pointer-events: none;
    overflow: hidden;
    z-index: 10;
    background: linear-gradient(to bottom, rgba(56, 189, 248, 0.12), rgba(15, 23, 42, 0.45));
    transition: opacity 0.3s ease;
}

.laser-sweep-line {
    position: absolute;
    left: 0;
    right: 0;
    height: 3px;
    background: #38bdf8;
    box-shadow: 0 0 10px #38bdf8, 0 0 20px #0284c7;
    z-index: 12;
    animation: scanLaserMove 1.5s ease-in-out infinite alternate;
}

.laser-sweep-line::before {
    content: "";
    position: absolute;
    bottom: 0;
    left: 0;
    right: 0;
    height: 28px;
    background: linear-gradient(to top, rgba(56, 189, 248, 0.3), transparent);
    pointer-events: none;
}

@keyframes scanLaserMove {
    0% { top: 5%; }
    100% { top: 92%; }
}

.thumb-processing-badge {
    display: inline-flex;
    align-items: center;
    gap: 4px;
    padding: 3px 8px;
    border-radius: 9999px;
    background: rgba(15, 23, 42, 0.9);
    border: 1px solid rgba(56, 189, 248, 0.6);
    color: #ffffff;
    font-size: 0.68rem;
    font-weight: 600;
    white-space: nowrap;
    box-shadow: 0 2px 8px rgba(0, 0, 0, 0.4);
    z-index: 15;
}

.thumb-processing-badge .spin-icon {
    display: inline-block;
    animation: spinner-rotate 1s linear infinite;
    font-size: 0.72rem;
}


.ocr-text-editor {
    width: 100%;
    min-height: 200px;
    font-family: monospace;
    font-size: 0.95rem;
    padding: 0.75rem;
    border: 1px solid var(--border-color);
    border-radius: var(--border-radius-sm);
    background: var(--bg-primary);
    color: var(--text-primary);
    box-sizing: border-box;
}

/* Camera Viewfinder & Subtle Animations */
.camera-viewfinder-overlay {
    position: absolute;
    inset: 20px;
    pointer-events: none;
    display: flex;
    flex-direction: column;
    justify-content: space-between;
    box-sizing: border-box;
    z-index: 10;
}

.camera-viewfinder-bracket {
    position: absolute;
    width: 32px;
    height: 32px;
    border-color: rgba(59, 130, 246, 0.85);
    border-style: solid;
    animation: subtle-breath 3.5s ease-in-out infinite;
}

.camera-viewfinder-bracket.top-left {
    top: 0;
    left: 0;
    border-width: 3px 0 0 3px;
    border-top-left-radius: 8px;
}

.camera-viewfinder-bracket.top-right {
    top: 0;
    right: 0;
    border-width: 3px 3px 0 0;
    border-top-right-radius: 8px;
}

.camera-viewfinder-bracket.bottom-left {
    bottom: 0;
    left: 0;
    border-width: 0 0 3px 3px;
    border-bottom-left-radius: 8px;
}

.camera-viewfinder-bracket.bottom-right {
    bottom: 0;
    right: 0;
    border-width: 0 3px 3px 0;
    border-bottom-right-radius: 8px;
}

@keyframes subtle-breath {
    0%, 100% {
        opacity: 0.65;
        filter: drop-shadow(0 0 6px rgba(59, 130, 246, 0.3));
    }
    50% {
        opacity: 1;
        filter: drop-shadow(0 0 14px rgba(59, 130, 246, 0.7));
    }
}

/* Shutter Flash Animation */
.camera-shutter-flash {
    position: absolute;
    inset: 0;
    background: #ffffff;
    opacity: 0;
    pointer-events: none;
    z-index: 50;
    transition: opacity 0.15s ease-out;
}

.camera-shutter-flash.is-flashing {
    opacity: 0.85;
}

/* Subtle Floating Processing Pill */
.scanner-processing-overlay {
    position: absolute;
    inset: 0;
    background: rgba(15, 23, 42, 0.4);
    backdrop-filter: blur(3px);
    display: flex;
    align-items: center;
    justify-content: center;
    z-index: 80;
    animation: fade-in-overlay 0.2s ease-out;
}

@keyframes fade-in-overlay {
    from { opacity: 0; }
    to { opacity: 1; }
}

.scanner-processing-pill {
    display: inline-flex;
    align-items: center;
    gap: 0.65rem;
    padding: 0.6rem 1.25rem;
    background: rgba(15, 23, 42, 0.88);
    border: 1px solid rgba(59, 130, 246, 0.4);
    border-radius: 9999px;
    color: #f8fafc;
    font-size: 0.9rem;
    font-weight: 500;
    box-shadow: 0 8px 25px rgba(0, 0, 0, 0.4), 0 0 15px rgba(59, 130, 246, 0.25);
    animation: pill-appear 0.25s cubic-bezier(0.16, 1, 0.3, 1);
}

[data-theme="light"] .scanner-processing-pill {
    background: rgba(255, 255, 255, 0.95);
    border-color: rgba(59, 130, 246, 0.35);
    color: #1e293b;
    box-shadow: 0 8px 25px rgba(0, 0, 0, 0.12), 0 0 15px rgba(59, 130, 246, 0.15);
}

@keyframes pill-appear {
    from {
        opacity: 0;
        transform: scale(0.92) translateY(4px);
    }
    to {
        opacity: 1;
        transform: scale(1) translateY(0);
    }
}

.scanner-spinner-subtle {
    width: 16px;
    height: 16px;
    border: 2px solid rgba(59, 130, 246, 0.25);
    border-top-color: #3b82f6;
    border-radius: 50%;
    animation: spinner-rotate 0.8s linear infinite;
}

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

/* Crop handles entrance animation */
.crop-handle {
    animation: handle-pop 0.25s cubic-bezier(0.34, 1.56, 0.64, 1);
}

@keyframes handle-pop {
    from {
        transform: scale(0.65);
        opacity: 0;
    }
    to {
        transform: scale(1);
        opacity: 1;
    }
}

@media (prefers-reduced-motion: reduce) {
    *,
    *::before,
    *::after {
        animation-duration: 0.01ms !important;
        animation-iteration-count: 1 !important;
        transition-duration: 0.01ms !important;
        scroll-behavior: auto !important;
    }
}
