/* ============================================================
   VIRTUAL GLASSES TRY-ON - STYLES
   Mobile-first responsive CSS
   ============================================================ */

/* CSS Custom Properties */
:root {
    --color-primary: #4f46e5;
    --color-primary-hover: #4338ca;
    --color-secondary: #6b7280;
    --color-secondary-hover: #4b5563;
    --color-success: #10b981;
    --color-danger: #ef4444;
    --color-warning: #f59e0b;
    --color-bg: #111827;
    --color-surface: #1f2937;
    --color-surface-elevated: #374151;
    --color-text: #f9fafb;
    --color-text-muted: #9ca3af;
    --color-border: #374151;

    --header-height: 60px;
    --gallery-height: 22vh;
    --gallery-min-height: 140px;
    --gallery-max-height: 200px;
    --fit-drawer-height: 44px;
    --border-radius: 12px;
    --transition-fast: 150ms ease;
    --transition-normal: 250ms ease;

    /* Safe area insets for notched devices */
    --safe-area-top: env(safe-area-inset-top, 0px);
    --safe-area-bottom: env(safe-area-inset-bottom, 0px);
    --safe-area-left: env(safe-area-inset-left, 0px);
    --safe-area-right: env(safe-area-inset-right, 0px);
}

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

html, body {
    height: 100%;
    overflow: hidden;
    font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Oxygen, Ubuntu, sans-serif;
    font-size: 16px;
    line-height: 1.5;
    background-color: var(--color-bg);
    color: var(--color-text);
    -webkit-font-smoothing: antialiased;
    -moz-osx-font-smoothing: grayscale;
    touch-action: manipulation;
}

/* App Container */
#app {
    display: flex;
    flex-direction: column;
    height: 100%;
    height: 100dvh; /* Dynamic viewport height for mobile */
    padding-top: var(--safe-area-top);
    padding-bottom: var(--safe-area-bottom);
    padding-left: var(--safe-area-left);
    padding-right: var(--safe-area-right);
}

/* ============================================================
   HEADER
   ============================================================ */
.header {
    display: flex;
    flex-direction: column;
    gap: 8px;
    padding: 8px 12px;
    background: var(--color-surface);
    border-bottom: 1px solid var(--color-border);
    z-index: 100;
    min-height: var(--header-height);
}

.title {
    font-size: 1.125rem;
    font-weight: 600;
    text-align: center;
    white-space: nowrap;
}

.header-buttons {
    display: flex;
    gap: 8px;
    justify-content: center;
    flex-wrap: wrap;
}

/* Buttons */
.btn {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    gap: 6px;
    padding: 8px 14px;
    font-size: 0.875rem;
    font-weight: 500;
    border: none;
    border-radius: 8px;
    cursor: pointer;
    transition: all var(--transition-fast);
    touch-action: manipulation;
    user-select: none;
    -webkit-tap-highlight-color: transparent;
}

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

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

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

.btn-secondary {
    background: var(--color-surface-elevated);
    color: var(--color-text);
}

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

.btn-small {
    padding: 6px 12px;
    font-size: 0.75rem;
    background: var(--color-surface-elevated);
    color: var(--color-text);
}

.btn-icon {
    font-size: 1rem;
}

.upload-btn {
    cursor: pointer;
}

/* ============================================================
   CAMERA CONTAINER
   ============================================================ */
.camera-container {
    flex: 1;
    position: relative;
    display: flex;
    align-items: center;
    justify-content: center;
    background: #000;
    overflow: hidden;
    min-height: 0; /* Important for flex shrinking */
}

#videoElement {
    position: absolute;
    width: 1px;
    height: 1px;
    opacity: 0;
    pointer-events: none;
}

#outputCanvas {
    max-width: 100%;
    max-height: 100%;
    object-fit: contain;
}

/* Status Overlay */
.status-overlay {
    position: absolute;
    inset: 0;
    display: flex;
    align-items: center;
    justify-content: center;
    background: rgba(0, 0, 0, 0.85);
    z-index: 10;
    transition: opacity var(--transition-normal);
}

.status-overlay.hidden {
    opacity: 0;
    pointer-events: none;
}

.status-content {
    text-align: center;
    padding: 24px;
}

.status-icon {
    font-size: 3rem;
    margin-bottom: 16px;
    animation: pulse 2s ease-in-out infinite;
}

@keyframes pulse {
    0%, 100% { opacity: 1; transform: scale(1); }
    50% { opacity: 0.7; transform: scale(0.95); }
}

.status-text {
    font-size: 1.125rem;
    font-weight: 500;
    margin-bottom: 8px;
}

.status-subtext {
    font-size: 0.875rem;
    color: var(--color-text-muted);
    max-width: 280px;
}

/* No Face Hint */
.no-face-hint {
    position: absolute;
    top: 16px;
    left: 50%;
    transform: translateX(-50%);
    background: rgba(0, 0, 0, 0.7);
    color: var(--color-warning);
    padding: 8px 16px;
    border-radius: 20px;
    font-size: 0.875rem;
    z-index: 20;
    transition: opacity var(--transition-fast);
}

.no-face-hint.hidden {
    opacity: 0;
    pointer-events: none;
}

/* Flash Effect */
.flash-overlay {
    position: absolute;
    inset: 0;
    background: white;
    opacity: 0;
    pointer-events: none;
    z-index: 30;
    transition: opacity 100ms ease;
}

.flash-overlay.flash {
    opacity: 1;
}

/* ============================================================
   FIT ADJUSTMENT DRAWER
   ============================================================ */
.fit-drawer {
    background: var(--color-surface);
    border-top: 1px solid var(--color-border);
    z-index: 50;
}

.fit-drawer-toggle {
    width: 100%;
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 8px;
    padding: 12px;
    background: transparent;
    border: none;
    color: var(--color-text);
    font-size: 0.875rem;
    cursor: pointer;
    -webkit-tap-highlight-color: transparent;
}

.fit-arrow {
    transition: transform var(--transition-fast);
}

.fit-arrow.expanded {
    transform: rotate(180deg);
}

.fit-controls {
    padding: 0 16px 16px;
    display: flex;
    flex-direction: column;
    gap: 12px;
    max-height: 200px;
    overflow: hidden;
    transition: max-height var(--transition-normal), padding var(--transition-normal);
}

.fit-controls.hidden {
    max-height: 0;
    padding-top: 0;
    padding-bottom: 0;
}

.fit-control {
    display: grid;
    grid-template-columns: 70px 1fr 50px;
    align-items: center;
    gap: 12px;
}

.fit-control label {
    font-size: 0.75rem;
    color: var(--color-text-muted);
    text-transform: uppercase;
}

.fit-control input[type="range"] {
    width: 100%;
    height: 6px;
    -webkit-appearance: none;
    appearance: none;
    background: var(--color-surface-elevated);
    border-radius: 3px;
    cursor: pointer;
}

.fit-control input[type="range"]::-webkit-slider-thumb {
    -webkit-appearance: none;
    width: 18px;
    height: 18px;
    background: var(--color-primary);
    border-radius: 50%;
    cursor: pointer;
}

.fit-control input[type="range"]::-moz-range-thumb {
    width: 18px;
    height: 18px;
    background: var(--color-primary);
    border-radius: 50%;
    cursor: pointer;
    border: none;
}

.fit-control span {
    font-size: 0.75rem;
    color: var(--color-text-muted);
    text-align: right;
}

/* ============================================================
   SNAPSHOT BUTTON
   ============================================================ */
.btn-snapshot {
    position: absolute;
    right: 16px;
    bottom: calc(var(--gallery-height) + var(--fit-drawer-height) + 20px);
    width: 56px;
    height: 56px;
    border-radius: 50%;
    background: var(--color-primary);
    border: 3px solid white;
    box-shadow: 0 4px 12px rgba(0, 0, 0, 0.4);
    cursor: pointer;
    z-index: 60;
    transition: all var(--transition-fast);
    -webkit-tap-highlight-color: transparent;
}

.btn-snapshot:hover:not(:disabled) {
    transform: scale(1.05);
    background: var(--color-primary-hover);
}

.btn-snapshot:disabled {
    opacity: 0.5;
    cursor: not-allowed;
}

.btn-snapshot:active:not(:disabled) {
    transform: scale(0.95);
}

.snapshot-icon {
    font-size: 1.5rem;
}

/* ============================================================
   GALLERY PANE
   ============================================================ */
.gallery-pane {
    height: var(--gallery-height);
    min-height: var(--gallery-min-height);
    max-height: var(--gallery-max-height);
    background: var(--color-surface);
    border-top: 1px solid var(--color-border);
    display: flex;
    flex-direction: column;
    z-index: 40;
}

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

.gallery-title {
    font-size: 0.875rem;
    font-weight: 600;
}

.gallery-count {
    font-size: 0.75rem;
    color: var(--color-text-muted);
}

.gallery-container {
    flex: 1;
    overflow-x: auto;
    overflow-y: hidden;
    -webkit-overflow-scrolling: touch;
    scrollbar-width: thin;
    scrollbar-color: var(--color-surface-elevated) transparent;
}

.gallery-container::-webkit-scrollbar {
    height: 6px;
}

.gallery-container::-webkit-scrollbar-track {
    background: transparent;
}

.gallery-container::-webkit-scrollbar-thumb {
    background: var(--color-surface-elevated);
    border-radius: 3px;
}

.gallery-empty {
    height: 100%;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    text-align: center;
    padding: 16px;
    color: var(--color-text-muted);
}

.gallery-empty p:first-child {
    font-size: 0.875rem;
    margin-bottom: 4px;
}

.gallery-hint {
    font-size: 0.75rem;
    max-width: 250px;
}

.gallery-empty.hidden {
    display: none;
}

.gallery-items {
    display: flex;
    gap: 12px;
    padding: 12px 16px;
    height: 100%;
    align-items: center;
}

.gallery-item {
    position: relative;
    flex-shrink: 0;
    width: 80px;
    height: 60px;
    background: var(--color-surface-elevated);
    border-radius: 8px;
    border: 2px solid transparent;
    overflow: hidden;
    cursor: pointer;
    transition: all var(--transition-fast);
    -webkit-tap-highlight-color: transparent;
}

.gallery-item:hover {
    border-color: var(--color-text-muted);
}

.gallery-item.selected {
    border-color: var(--color-primary);
    box-shadow: 0 0 0 2px rgba(79, 70, 229, 0.3);
}

.gallery-item img {
    width: 100%;
    height: 100%;
    object-fit: contain;
    padding: 4px;
}

.gallery-item-delete {
    position: absolute;
    top: -6px;
    right: -6px;
    width: 20px;
    height: 20px;
    background: var(--color-danger);
    border: none;
    border-radius: 50%;
    color: white;
    font-size: 12px;
    line-height: 1;
    cursor: pointer;
    opacity: 0;
    transition: opacity var(--transition-fast);
    display: flex;
    align-items: center;
    justify-content: center;
}

.gallery-item:hover .gallery-item-delete {
    opacity: 1;
}

/* Always show delete button on touch devices */
@media (hover: none) {
    .gallery-item-delete {
        opacity: 1;
    }
}

/* ============================================================
   TOAST NOTIFICATIONS
   ============================================================ */
.toast-container {
    position: fixed;
    top: calc(var(--safe-area-top) + 70px);
    left: 50%;
    transform: translateX(-50%);
    z-index: 1000;
    display: flex;
    flex-direction: column;
    gap: 8px;
    pointer-events: none;
}

.toast {
    background: var(--color-surface-elevated);
    color: var(--color-text);
    padding: 12px 20px;
    border-radius: 8px;
    font-size: 0.875rem;
    box-shadow: 0 4px 12px rgba(0, 0, 0, 0.3);
    animation: toastIn 300ms ease;
    pointer-events: auto;
}

.toast.toast-error {
    background: var(--color-danger);
}

.toast.toast-success {
    background: var(--color-success);
}

.toast.toast-out {
    animation: toastOut 200ms ease forwards;
}

@keyframes toastIn {
    from {
        opacity: 0;
        transform: translateY(-10px);
    }
    to {
        opacity: 1;
        transform: translateY(0);
    }
}

@keyframes toastOut {
    from {
        opacity: 1;
        transform: translateY(0);
    }
    to {
        opacity: 0;
        transform: translateY(-10px);
    }
}

/* ============================================================
   LOADING SPINNER (for model loading)
   ============================================================ */
.spinner {
    display: inline-block;
    width: 24px;
    height: 24px;
    border: 3px solid var(--color-surface-elevated);
    border-top-color: var(--color-primary);
    border-radius: 50%;
    animation: spin 1s linear infinite;
}

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

/* ============================================================
   RESPONSIVE ADJUSTMENTS
   ============================================================ */

/* Tablet and larger */
@media (min-width: 768px) {
    .header {
        flex-direction: row;
        justify-content: space-between;
        align-items: center;
        padding: 12px 24px;
    }

    .title {
        font-size: 1.25rem;
    }

    .btn {
        padding: 10px 18px;
    }

    .gallery-item {
        width: 100px;
        height: 75px;
    }

    .btn-snapshot {
        width: 64px;
        height: 64px;
    }

    .fit-control {
        grid-template-columns: 80px 1fr 60px;
    }
}

/* Desktop */
@media (min-width: 1024px) {
    :root {
        --gallery-height: 180px;
        --gallery-max-height: 220px;
    }

    .gallery-item {
        width: 120px;
        height: 90px;
    }

    .gallery-items {
        gap: 16px;
        padding: 16px 24px;
    }
}

/* Landscape mode on mobile */
@media (max-height: 500px) and (orientation: landscape) {
    :root {
        --header-height: 50px;
        --gallery-height: 100px;
        --gallery-min-height: 80px;
    }

    .header {
        padding: 4px 12px;
    }

    .title {
        font-size: 1rem;
    }

    .btn {
        padding: 6px 10px;
        font-size: 0.75rem;
    }

    .gallery-item {
        width: 70px;
        height: 50px;
    }

    .btn-snapshot {
        width: 44px;
        height: 44px;
        bottom: calc(var(--gallery-height) + var(--fit-drawer-height) + 10px);
    }

    .snapshot-icon {
        font-size: 1.125rem;
    }
}

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

.visually-hidden {
    position: absolute;
    width: 1px;
    height: 1px;
    padding: 0;
    margin: -1px;
    overflow: hidden;
    clip: rect(0, 0, 0, 0);
    white-space: nowrap;
    border: 0;
}
