/*
 * lutul - Minimal Film Simulation
 * WCAG 2.1 AA Compliant
 */

/* Reset & Base */
* {
    margin: 0;
    padding: 0;
    box-sizing: border-box;
}

:root {
    /* Palette */
    --bg-primary: #ffffff;
    --bg-secondary: #f5f5f5;
    --text-primary: #1a1a1a;
    --text-secondary: #666666;
    --border: #d0d0d0;
    --accent: #0066cc;
    --accent-hover: #0052a3;

    /* Spacing */
    --space-xs: 0.5rem;
    --space-sm: 1rem;
    --space-md: 1.5rem;
    --space-lg: 2rem;

    /* Typography */
    --font-base:
        -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
}

@media (prefers-color-scheme: dark) {
    :root {
        --bg-primary: #1a1a1a;
        --bg-secondary: #2a2a2a;
        --text-primary: #f5f5f5;
        --text-secondary: #a0a0a0;
        --border: #404040;
        --accent: #4d9fff;
        --accent-hover: #66b3ff;
    }
}

body {
    font-family: var(--font-base);
    background: var(--bg-primary);
    color: var(--text-primary);
    -webkit-font-smoothing: antialiased;
    overflow: hidden;
    height: 100vh;
    width: 100vw;
    line-height: 1.5;
    position: fixed;
    top: 0;
    left: 0;
}

/* Screen-reader only content */
.sr-only {
    position: absolute;
    width: 1px;
    height: 1px;
    padding: 0;
    margin: -1px;
    overflow: hidden;
    clip: rect(0, 0, 0, 0);
    white-space: nowrap;
    border: 0;
}

/* App Container */
#app {
    height: 100vh;
    display: flex;
    flex-direction: column;
}

.screen {
    display: none;
    flex-direction: column;
    height: 100vh;
}

.screen.active {
    display: flex;
}

/* Upload Screen */
#upload-screen {
    justify-content: center;
    align-items: center;
    padding: var(--space-lg);
}

#upload-screen header {
    text-align: center;
    margin-bottom: var(--space-lg);
}

#upload-screen h1 {
    font-size: 2rem;
    font-weight: 400;
    letter-spacing: 0.1em;
    color: var(--text-primary);
}

.upload-options {
    display: flex;
    flex-direction: column;
    gap: var(--space-sm);
    width: 100%;
    max-width: 320px;
}

.option-btn {
    text-align: center;
    display: flex;
    align-items: center;
    justify-content: center;
    gap: var(--space-sm);
    padding: var(--space-md);
    background: var(--bg-secondary);
    border: 1px solid var(--border);
    border-radius: 4px;
    font-size: 1rem;
    color: var(--text-primary);
    cursor: pointer;
    transition:
        background 0.2s,
        border-color 0.2s;
    position: relative;
}

.option-btn:hover,
.option-btn:focus-visible {
    background: var(--bg-primary);
    border-color: var(--accent);
    outline: none;
}

.option-btn input[type="file"] {
    position: absolute;
    opacity: 0;
    width: 0;
    height: 0;
}

.option-btn .icon {
    font-size: 1.5rem;
}

.hint {
    margin-top: var(--space-md);
    font-size: 0.875rem;
    color: var(--text-secondary);
    text-align: center;
}

/* Camera Screen */
#camera-screen {
    background: #000;
    position: relative;
    overflow: hidden;
    /* Ensure camera screen fills viewport even with Safari UI */
    min-height: 100vh;
    min-height: 100dvh;
}

.camera-container {
    position: relative;
    height: 100vh; /* Fallback for older browsers */
    height: 100dvh; /* Dynamic viewport height (Safari 15.4+, iOS 15.4+) */
    height: calc(var(--viewport-height, 1vh) * 100); /* JS fallback for maximum compatibility */
    width: 100vw;
}

#camera-preview {
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    object-fit: cover;
    background: #000;
    display: block;
}

.camera-controls {
    position: absolute;
    bottom: 0;
    left: 0;
    right: 0;
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: var(--space-lg);
    /* Extra padding for Safari bottom toolbar (defensive) */
    padding-bottom: calc(var(--space-lg) + env(safe-area-inset-bottom, 20px));
    background: linear-gradient(to top, rgba(0, 0, 0, 0.8), transparent);
    z-index: 10;
    min-height: 100px;
}

/* Landscape mode: controls stay on the side */
@media (orientation: landscape) {
    .camera-controls {
        position: absolute;
        right: 0;
        left: auto;
        bottom: 0;
        top: 0;
        flex-direction: column;
        justify-content: center;
        width: auto;
        min-width: 120px;
        padding: var(--space-lg);
        padding-right: calc(var(--space-lg) + env(safe-area-inset-right, 20px));
        background: linear-gradient(to left, rgba(0, 0, 0, 0.8), transparent);
        gap: var(--space-lg);
    }

    .spacer {
        display: none; /* Not needed in landscape */
    }
}

.btn-text {
    background: transparent;
    border: none;
    color: #fff;
    font-size: 1rem;
    padding: var(--space-sm);
    cursor: pointer;
    min-width: 80px;
}

.btn-text:hover,
.btn-text:focus-visible {
    text-decoration: underline;
    outline: 2px solid #fff;
    outline-offset: 2px;
}

.btn-capture {
    width: 64px;
    height: 64px;
    background: #fff;
    border: 4px solid rgba(255, 255, 255, 0.5);
    border-radius: 50%;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    transition: transform 0.1s;
}

.btn-capture:active {
    transform: scale(0.95);
}

.btn-capture:focus-visible {
    outline: 2px solid #4d9fff;
    outline-offset: 4px;
}

.capture-ring {
    width: 48px;
    height: 48px;
    background: #fff;
    border-radius: 50%;
}

.spacer {
    min-width: 80px;
}

/* Editor Screen */
#editor-screen {
    position: relative;
    overflow-y: auto;
    overflow-x: hidden;
    -webkit-overflow-scrolling: touch;
    scroll-behavior: smooth;
}

#canvas {
    width: 100%;
    max-width: 100vw;
    height: auto;
    object-fit: contain;
    background: var(--bg-secondary);
    display: block;
    /* Add padding at bottom for fixed controls */
    padding-bottom: 250px;
}

.controls {
    position: fixed;
    bottom: 0;
    left: 0;
    right: 0;
    background: var(--bg-primary);
    border-top: 1px solid var(--border);
    padding: var(--space-md);
    padding-bottom: calc(var(--space-md) + env(safe-area-inset-bottom, 0px));
    width: 100%;
    max-width: 100vw;
    box-sizing: border-box;
    z-index: 10;
}

/* Filter Selection */
.filter-group {
    margin-bottom: var(--space-md);
}

.filter-group label {
    display: block;
    margin-bottom: var(--space-xs);
    font-size: 0.875rem;
    color: var(--text-secondary);
    font-weight: 500;
}

#filter-select {
    width: 100%;
    padding: var(--space-sm) var(--space-md);
    background: var(--bg-secondary);
    border: 1px solid var(--border);
    border-radius: 4px;
    font-size: 1rem;
    color: var(--text-primary);
    font-family: var(--font-base);
    cursor: pointer;
    min-height: 44px; /* WCAG 2.1 touch target */
    appearance: none;
    -webkit-appearance: none;
    -moz-appearance: none;
    background-image: url("data:image/svg+xml;charset=UTF-8,%3csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24' fill='none' stroke='currentColor' stroke-width='2' stroke-linecap='round' stroke-linejoin='round'%3e%3cpolyline points='6 9 12 15 18 9'%3e%3c/polyline%3e%3c/svg%3e");
    background-repeat: no-repeat;
    background-position: right var(--space-sm) center;
    background-size: 1.5rem;
    padding-right: calc(var(--space-md) + 2rem);
}

#filter-select:hover,
#filter-select:focus {
    border-color: var(--accent);
    outline: none;
}

#filter-select:focus-visible {
    outline: 2px solid var(--accent);
    outline-offset: 2px;
}

/* Dark mode arrow color */
@media (prefers-color-scheme: dark) {
    #filter-select {
        background-image: url("data:image/svg+xml;charset=UTF-8,%3csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24' fill='none' stroke='%23f5f5f5' stroke-width='2' stroke-linecap='round' stroke-linejoin='round'%3e%3cpolyline points='6 9 12 15 18 9'%3e%3c/polyline%3e%3c/svg%3e");
    }
}

.filter-pills {
    display: flex;
    gap: var(--space-xs);
    overflow-x: auto;
    overflow-y: hidden;
    padding-bottom: var(--space-xs);
    scrollbar-width: none;
    -webkit-overflow-scrolling: touch;
    /* Prevent overflow */
    max-width: 100%;
    width: 100%;
}

.filter-pills button {
    flex-shrink: 0;
    padding: 0.5rem 1rem;
    background: var(--bg-secondary);
    border: 1px solid var(--border);
    color: var(--text-primary);
    border-radius: 4px;
    font-size: 0.875rem;
    cursor: pointer;
    transition: all 0.2s;
    white-space: nowrap;
    min-height: 44px; /* WCAG 2.1 touch target */
}

.filter-pills button:hover,
.filter-pills button:focus-visible {
    border-color: var(--accent);
    outline: none;
}

.filter-pills button.active {
    background: var(--accent);
    color: #fff;
    border-color: var(--accent);
}

.filter-pills button:focus-visible {
    outline: 2px solid var(--accent);
    outline-offset: 2px;
}

/* Slider */
.slider-group {
    margin-bottom: var(--space-md);
}

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

#grain-value {
    color: var(--text-primary);
    font-weight: 500;
    min-width: 3ch;
    text-align: right;
}

input[type="range"] {
    width: 100%;
    height: 44px; /* WCAG 2.1 touch target */
    background: transparent;
    outline: none;
    -webkit-appearance: none;
    cursor: pointer;
}

input[type="range"]::-webkit-slider-track {
    width: 100%;
    height: 4px;
    background: var(--border);
    border-radius: 2px;
}

input[type="range"]::-webkit-slider-thumb {
    -webkit-appearance: none;
    width: 20px;
    height: 20px;
    background: var(--accent);
    border-radius: 50%;
    cursor: pointer;
    margin-top: -8px;
}

input[type="range"]:focus-visible::-webkit-slider-thumb {
    outline: 2px solid var(--accent);
    outline-offset: 2px;
}

input[type="range"]::-moz-range-track {
    width: 100%;
    height: 4px;
    background: var(--border);
    border-radius: 2px;
}

input[type="range"]::-moz-range-thumb {
    width: 20px;
    height: 20px;
    background: var(--accent);
    border-radius: 50%;
    cursor: pointer;
    border: none;
}

input[type="range"]:focus-visible::-moz-range-thumb {
    outline: 2px solid var(--accent);
    outline-offset: 2px;
}

/* Actions */
.actions {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: var(--space-sm);
}

button {
    padding: var(--space-md);
    border: 1px solid var(--border);
    border-radius: 4px;
    font-size: 1rem;
    font-weight: 500;
    cursor: pointer;
    transition: all 0.2s;
    min-height: 44px; /* WCAG 2.1 touch target */
}

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

.btn-secondary:hover,
.btn-secondary:focus-visible {
    background: var(--bg-primary);
    border-color: var(--accent);
    outline: none;
}

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

.btn-primary {
    background: var(--accent);
    color: #fff;
    border-color: var(--accent);
}

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

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

/* Processing Screen */
#processing-screen {
    justify-content: center;
    align-items: center;
    background: rgba(0, 0, 0, 0.5);
}

.processing-modal {
    text-align: center;
    background: var(--bg-primary);
    padding: var(--space-lg);
    border-radius: 8px;
    min-width: 200px;
}

.spinner {
    width: 48px;
    height: 48px;
    border: 3px solid var(--border);
    border-top-color: var(--accent);
    border-radius: 50%;
    animation: spin 1s linear infinite;
    margin: 0 auto var(--space-md);
}

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

.progress-text {
    font-size: 1.5rem;
    font-weight: 500;
    color: var(--text-primary);
    margin-top: var(--space-xs);
}

/* Export Size Modal */
.modal {
    display: none;
    position: fixed;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    z-index: 1000;
    align-items: center;
    justify-content: center;
}

.modal.active {
    display: flex;
}

.modal-overlay {
    position: absolute;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    background: rgba(0, 0, 0, 0.5);
}

.modal-content {
    position: relative;
    background: var(--bg-primary);
    border-radius: 8px;
    padding: var(--space-lg);
    max-width: 400px;
    width: 90%;
    box-shadow: 0 10px 40px rgba(0, 0, 0, 0.3);
    z-index: 1;
}

.modal-content h2 {
    margin: 0 0 var(--space-md);
    font-size: 1.25rem;
    font-weight: 500;
    color: var(--text-primary);
}

.modal-content fieldset {
    border: none;
    padding: 0;
    margin: 0 0 var(--space-lg);
}

.size-option {
    display: flex;
    align-items: center;
    padding: var(--space-sm);
    border: 1px solid var(--border);
    border-radius: 4px;
    margin-bottom: var(--space-xs);
    cursor: pointer;
    transition: all 0.2s;
}

.size-option:hover {
    border-color: var(--accent);
    background: var(--bg-secondary);
}

.size-option input[type="radio"] {
    margin: 0 var(--space-sm) 0 0;
    cursor: pointer;
}

.size-label {
    display: flex;
    flex-direction: column;
    gap: 0.25rem;
}

.size-label strong {
    font-size: 1rem;
    color: var(--text-primary);
}

.size-label small {
    font-size: 0.875rem;
    color: var(--text-secondary);
}

.modal-actions {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: var(--space-sm);
}

/* Focus Visible for Keyboard Navigation */
*:focus-visible {
    outline: 2px solid var(--accent);
    outline-offset: 2px;
}

/* Mobile-specific fixes */
@media (max-width: 767px) {
    /* Ensure no horizontal scroll on mobile */
    html,
    body {
        overflow-x: hidden;
        max-width: 100vw;
    }

    /* Tighter controls padding on mobile */
    .controls {
        padding: var(--space-sm);
        padding-bottom: calc(var(--space-sm) + env(safe-area-inset-bottom, 0px));
    }

    /* Canvas padding for mobile */
    #canvas {
        padding-bottom: 200px;
    }
}

/* Responsive */
@media (min-width: 768px) {
    .controls {
        max-width: 600px;
        left: 50%;
        transform: translateX(-50%);
        right: auto;
    }

    #upload-screen h1 {
        font-size: 3rem;
    }
}

/* High Contrast Mode Support */
@media (prefers-contrast: high) {
    :root {
        --border: #000;
        --accent: #0000ff;
    }

    button,
    .option-btn {
        border-width: 2px;
    }
}

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