/**
 * MvMediaViewer V2 Styles
 *
 * A gesture-based media viewer with physics animation.
 * All classes are prefixed with 'mv-' to avoid conflicts.
 *
 * Required HTML structure:
 *   .mv-overlay
 *     .mv-top-bar
 *       .mv-top-left > .mv-btn-delete
 *       .mv-counter
 *       .mv-top-right > .mv-btn-close
 *     .mv-liked-by (hidden until populated)
 *     .mv-slider
 *       .mv-page (3 recycled pages, managed by JS)
 *     .mv-bottom-bar (optional, for video)
 *     .mv-action-bar
 *       .mv-action-left > .mv-author
 *       .mv-action-center > .mv-btn-like, .mv-btn-download
 *       .mv-action-right > .mv-megapixels
 */

/* =========================================================================
   MARK: Shared Utilities
   ========================================================================= */

.mv-glass {
    background: rgba(20, 20, 22, 0.60);
    backdrop-filter: saturate(180%) blur(20px);
    -webkit-backdrop-filter: saturate(180%) blur(20px);
    transform: translateZ(0);
}

.mv-pill {
    border-radius: 9999px;
}

/* =========================================================================
   MARK: Buttons
   ========================================================================= */

.mv-btn {
    width: 46px;
    height: 46px;
    background: transparent;
    border: none;
    color: #fff;
    font-size: 18px;
    cursor: pointer;
    padding: 0;
    display: flex;
    align-items: center;
    justify-content: center;
    flex-shrink: 0;
    border-radius: 50%;
    touch-action: manipulation;
    transition: background 0.15s, transform 0.15s;
    will-change: transform;
    transform: translateZ(0);
}

.mv-btn.mv-glass {
    background: rgba(20, 20, 22, 0.60);
}

/* =========================================================================
   MARK: Root Container
   ========================================================================= */

.mv-overlay {
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    height: var(--app-height, 100vh);
    z-index: 9999;
    background: #000;
    font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
    -webkit-touch-callout: none;
    -webkit-user-select: none;
    user-select: none;
    -webkit-tap-highlight-color: transparent;
    overflow: hidden;

    /* Open/close animation */
    opacity: 1;
    visibility: visible;
}

/* Hidden state (before open or after close) */
.mv-overlay.mv-hidden,
.mv-overlay.mv-hidden * {
    pointer-events: none !important;
}

.mv-overlay.mv-hidden {
    opacity: 0;
    visibility: hidden;
}

/* Closing animation - fade out everything */
.mv-overlay.mv-closing {
    opacity: 0;
    pointer-events: none;
}

/* =========================================================================
   MARK: Slider Container (V2 - Gesture Based)
   ========================================================================= */

.mv-slider {
    position: absolute;
    inset: 0;
    overflow: hidden;
    /* No scroll - all movement handled by JS transforms */
}

/* =========================================================================
   MARK: Pages (V2 - Recycled Page Pool)
   ========================================================================= */

.mv-page {
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    display: flex;
    align-items: center;
    justify-content: center;
    will-change: transform;
    /* Prevent text selection during drag */
    -webkit-user-select: none;
    user-select: none;
}

/* Content wrapper (images only — receives zoom transform, contains tile layer) */
.mv-content {
    position: relative;
    display: flex;
    align-items: center;
    justify-content: center;
    max-width: 100vw;
    max-height: 100vh;
    transform-origin: center center;
}

.mv-page img,
.mv-page video {
    max-width: 100vw;
    max-height: 100vh;
    object-fit: contain;
    -webkit-user-drag: none;
    user-drag: none;
    /* Prevent image dragging on desktop */
    pointer-events: none;
    display: block;
}

/* Allow video controls to receive pointer events */
.mv-page video {
    pointer-events: auto;
}

/* Tile canvas — sibling of .mv-content inside .mv-page.
   Viewport-sized canvas that draws tiles at computed screen positions.
   No CSS transform — redraws each frame from zoom state. */
.mv-tile-canvas {
    position: absolute;
    top: 0;
    left: 0;
    pointer-events: none;
    opacity: 0;
    transition: opacity 100ms ease-out;
    image-rendering: high-quality;
}

/* =========================================================================
   MARK: Page States
   ========================================================================= */

.mv-page-loading::after {
    content: '';
    position: absolute;
    top: 50%;
    left: 50%;
    width: 40px;
    height: 40px;
    margin: -20px 0 0 -20px;
    border: 3px solid rgba(255, 255, 255, 0.2);
    border-top-color: white;
    border-radius: 50%;
    animation: mv-spin 0.8s linear infinite;
}

.mv-page-error {
    background: #111;
}

.mv-page-error::after {
    content: '!';
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
    font-size: 48px;
    color: rgba(255, 255, 255, 0.3);
}

.mv-page-empty {
    background: #000;
}

/* Video buffering during playback - spinner overlay */
.mv-video-buffering::after {
    content: '';
    position: absolute;
    top: 50%;
    left: 50%;
    width: 40px;
    height: 40px;
    margin: -20px 0 0 -20px;
    border: 3px solid rgba(255, 255, 255, 0.2);
    border-top-color: white;
    border-radius: 50%;
    animation: mv-spin 0.8s linear infinite;
    z-index: 5;
    pointer-events: none;
}

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

/* =========================================================================
   MARK: Top Bar
   ========================================================================= */

.mv-top-bar {
    position: absolute;
    top: var(--sa-top);
    left: max(var(--sa-left), var(--sa-right), 16px);
    right: max(var(--sa-left), var(--sa-right), 16px);
    height: 52px;
    display: flex;
    align-items: center;
    justify-content: space-between;
    z-index: 10;
    pointer-events: none;
}

@media (hover: hover) and (pointer: fine) {
    .mv-top-bar {
        top: 16px;
    }
}

.mv-top-bar > * {
    pointer-events: auto;
}

.mv-top-left,
.mv-top-right {
    flex: 1;
    display: flex;
    align-items: center;
}

.mv-top-left {
    justify-content: flex-start;
}

.mv-top-right {
    justify-content: flex-end;
    gap: 8px;
}

/* =========================================================================
   MARK: Like Button
   ========================================================================= */

.mv-btn-like {
    color: rgba(255, 255, 255, 0.9);
    transition: background 0.15s, transform 0.15s, color 0.15s ease-out;
}

.mv-btn-like.mv-liked .icon {
    color: #ff3b5c;
}

/* =========================================================================
   MARK: Like Overlay
   ========================================================================= */

.mv-like-overlay {
    position: absolute;
    inset: 0;
    display: flex;
    align-items: center;
    justify-content: center;
    z-index: 20;
    pointer-events: none;
    opacity: 0;
    will-change: opacity, transform;
}

.mv-like-overlay-box {
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 10px;
    padding: 28px 36px;
    border-radius: 24px;
    background: rgba(20, 20, 22, 0.75);
    backdrop-filter: saturate(180%) blur(20px);
    -webkit-backdrop-filter: saturate(180%) blur(20px);
    transform: translateZ(0);
}

.mv-like-overlay-box svg {
    width: 48px;
    height: 48px;
    will-change: transform;
}

.mv-like-overlay-box span {
    font-size: 16px;
    font-weight: 600;
    color: white;
}

@keyframes mv-like-overlay-in {
    0%   { opacity: 0; transform: scale(0.9); }
    10%  { opacity: 1; transform: scale(1); }
    85%  { opacity: 1; transform: scale(1); }
    100% { opacity: 0; transform: scale(0.95); }
}

@keyframes mv-like-heart-beat {
    0%   { transform: scale(0); }
    50%  { transform: scale(1.2); }
    100% { transform: scale(1); }
}

.mv-like-overlay.mv-visible {
    animation: mv-like-overlay-in 2.5s ease-out forwards;
}

.mv-like-overlay.mv-visible svg {
    animation: mv-like-heart-beat 0.7s 0.1s cubic-bezier(0.34, 1.56, 0.64, 1) both;
}

/* =========================================================================
   MARK: Counter
   ========================================================================= */

.mv-counter {
    color: #fff;
    font-size: 15px;
    font-weight: 600;
    padding: 8px 18px;
}

/* =========================================================================
   MARK: Bottom Bar (Video Controls)
   ========================================================================= */

.mv-bottom-bar {
    position: absolute;
    left: max(var(--sa-left), var(--sa-right), 32px);
    right: max(var(--sa-left), var(--sa-right), 32px);
    bottom: calc(70px + max(16px, var(--sa-bottom)));
    margin: 0 auto;
    max-width: 800px;
    padding: 8px 18px 8px 10px;
    display: flex;
    align-items: center;
    gap: 12px;
    z-index: 10;
    border-radius: 100px;
    overflow: hidden;
    background: rgba(20, 20, 22, 0.6);
    backdrop-filter: saturate(180%) blur(20px);
    -webkit-backdrop-filter: saturate(180%) blur(20px);

    /* Animation: slide up from below + fade in */
    transform: translateY(20px);
    opacity: 0;
    visibility: hidden;
    transition:
        transform 0.15s cubic-bezier(0.4, 0, 0.2, 1),
        opacity 0.15s cubic-bezier(0.4, 0, 0.2, 1),
        visibility 0.15s;
}

.mv-bottom-bar.mv-visible {
    transform: translateY(0);
    opacity: 1;
    visibility: visible;
}

.mv-timeline {
    position: relative;
    flex: 1;
    height: 4px;
    background: rgba(255, 255, 255, 0.2);
    border-radius: 50px;
    cursor: pointer;
    /* Prevent text selection during drag */
    -webkit-user-select: none;
    user-select: none;
}

/* Expand touch hit area to 44px without changing visual appearance */
.mv-timeline::before {
    content: '';
    position: absolute;
    top: -20px;
    bottom: -20px;
    left: -8px;
    right: -8px;
}

.mv-timeline-buffer {
    position: absolute;
    top: 0;
    left: 0;
    height: 100%;
    width: 0;
    background: rgba(255, 255, 255, 0.3);
    border-radius: 50px;
    transition: width 0.2s ease-out;
}

.mv-timeline-progress {
    position: absolute;
    top: 0;
    left: 0;
    height: 100%;
    width: 0;
    background: #fff;
    border-radius: 50px;
    /* Smooth transition during playback, disabled during drag for responsiveness */
    transition: width 0.1s linear;
}

/* Disable transition during scrubbing for instant feedback */
.mv-timeline.mv-dragging .mv-timeline-progress {
    transition: none;
}

.mv-timeline-scrubber {
    position: absolute;
    left: 100%;
    top: 50%;
    transform: translate(-50%, -50%) scale(0);
    width: 12px;
    height: 12px;
    background: #fff;
    border-radius: 50%;
    box-shadow: 0 1px 4px rgba(0, 0, 0, 0.3);
    transition: transform 0.15s ease-out;
    pointer-events: none;
}

/* Desktop: show scrubber on hover */
@media (hover: hover) {
    .mv-timeline:hover .mv-timeline-scrubber {
        transform: translate(-50%, -50%) scale(1);
    }
}

/* Dragging state */
.mv-timeline.mv-dragging .mv-timeline-scrubber {
    transform: translate(-50%, -50%) scale(1.2);
}

/* Touch devices: larger scrubber for easier grabbing */
@media (hover: none) {
    .mv-timeline-scrubber {
        width: 20px;
        height: 20px;
    }

    /* Always show scrubber when controls visible */
    .mv-bottom-bar.mv-visible .mv-timeline-scrubber {
        transform: translate(-50%, -50%) scale(1);
    }

    /* Larger scale when dragging */
    .mv-timeline.mv-dragging .mv-timeline-scrubber {
        transform: translate(-50%, -50%) scale(1.15);
    }
}

.mv-time {
    color: #fff;
    font-size: 12px;
    opacity: 0.7;
    white-space: nowrap;
    flex-shrink: 0;
    padding-right: 4px;
}

/* =========================================================================
   MARK: Liked-By Pill
   ========================================================================= */

.mv-liked-by {
    position: absolute;
    top: calc(var(--sa-top) + 60px);
    left: 0;
    right: 0;
    width: fit-content;
    margin: 0 auto;
    display: flex;
    align-items: center;
    gap: 6px;
    padding: 8px 16px;
    font-size: 15px;
    z-index: 10;
    pointer-events: auto;

    /* Hidden by default until setLikedBy() called */
    opacity: 0;
    visibility: hidden;
    transition: opacity 0.15s ease-out, visibility 0.15s;
}

.mv-liked-by.mv-visible {
    opacity: 1;
    visibility: visible;
}

@media (hover: hover) and (pointer: fine) {
    .mv-liked-by {
        top: calc(16px + 60px);
    }
}

.mv-liked-avatars {
    display: flex;
    flex-shrink: 0;
    position: relative;
    margin-left: -4px;
}

.mv-liked-avatar {
    width: 22px;
    height: 22px;
    border-radius: 50%;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 10px;
    font-weight: 600;
    color: rgba(0, 0, 0, 0.7);
    margin-left: -6px;
    border: 2px solid rgba(20, 20, 22, 0.6);
}

.mv-liked-avatar:first-child {
    margin-left: 0;
}

.mv-liked-heart {
    position: absolute;
    bottom: -3px;
    right: -3px;
    width: 12px;
    height: 12px;
}

.mv-liked-text {
    color: #fff;
}

/* =========================================================================
   MARK: Action Bar
   ========================================================================= */

.mv-action-bar {
    position: absolute;
    bottom: max(16px, var(--sa-bottom));
    left: max(var(--sa-left), var(--sa-right), 40px);
    right: max(var(--sa-left), var(--sa-right), 40px);
    margin: 0 auto;
    max-width: 760px;
    display: flex;
    align-items: center;
    z-index: 10;
    pointer-events: none;

    /* Animation: slide up + fade in */
    transform: translateY(20px);
    opacity: 0;
    visibility: hidden;
    transition:
        transform 0.25s cubic-bezier(0.4, 0, 0.2, 1),
        opacity 0.25s cubic-bezier(0.4, 0, 0.2, 1),
        visibility 0.25s;
}

/* Show when overlay is visible (not hidden, not closing) */
.mv-overlay:not(.mv-hidden):not(.mv-closing) .mv-action-bar {
    transform: translateY(0);
    opacity: 1;
    visibility: visible;
}

.mv-action-bar > * {
    pointer-events: auto;
}

.mv-action-left,
.mv-action-right {
    flex: 1;
    display: flex;
    align-items: center;
}

.mv-action-left {
    justify-content: flex-start;
}

.mv-action-right {
    justify-content: flex-end;
}

.mv-action-center {
    display: flex;
    align-items: center;
    gap: 24px;
}

/* =========================================================================
   MARK: Author (inside action-left)
   ========================================================================= */

.mv-author {
    display: none;
    flex-direction: column;
    align-items: center;
    gap: 4px;
}

.mv-author.mv-visible {
    display: flex;
}

.mv-avatar {
    width: 36px;
    height: 36px;
    border-radius: 50%;
    background: rgba(255, 255, 255, 0.15);
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 14px;
    font-weight: 600;
    color: #fff;
}

.mv-author-name {
    font-size: 11px;
    color: #fff;
    opacity: 0.7;
}

/* =========================================================================
   MARK: Megapixels Badge (inside action-right)
   ========================================================================= */

.mv-megapixels {
    font-size: 12px;
    color: #fff;
    padding: 6px 14px;
    font-variant-numeric: tabular-nums;
    opacity: 0;
    visibility: hidden;
    transition: opacity 0.15s ease-out, visibility 0.15s;
}

.mv-megapixels.mv-visible {
    opacity: 0.7;
    visibility: visible;
}

/* =========================================================================
   MARK: Interactive States
   ========================================================================= */

/* Desktop hover effects */
@media (hover: hover) and (pointer: fine) {
    .mv-btn.mv-glass:hover {
        background: rgba(30, 30, 34, 0.9);
        transform: scale(1.05);
    }

    .mv-btn.mv-glass:active {
        transform: scale(0.98);
    }

    .mv-btn:not(.mv-glass):hover {
        background: rgba(255, 255, 255, 0.1);
    }
}

/* Touch device feedback */
@media (hover: none) and (pointer: coarse) {
    .mv-btn.mv-glass:active {
        background: rgba(40, 40, 44, 0.95);
        transform: scale(0.9);
        transition: transform 0.06s ease-out;
    }

    .mv-btn:not(.mv-glass):active {
        transform: scale(0.92);
        transition: transform 0.06s ease-out;
    }
}

/* Play button — subtle background inside the video bar */
.mv-btn-play {
    background: rgba(0, 0, 0, 0.2);
}

.mv-btn-play .icon {
    font-size: 16px;
}

/* =========================================================================
   MARK: Dismiss Animation States
   ========================================================================= */

/* Vertical dismiss animation */
.mv-overlay.mv-dismissing {
    transition: background-color 0.2s ease-out;
}

.mv-overlay.mv-dismissing .mv-page .mv-content,
.mv-overlay.mv-dismissing .mv-page video {
    transition: transform 0.2s ease-out;
}

/* Bounce back animation (when dismiss is cancelled) */
.mv-overlay.mv-bounce-back .mv-page .mv-content,
.mv-overlay.mv-bounce-back .mv-page video {
    transition: transform 0.3s cubic-bezier(0.4, 0, 0.2, 1);
}

/* Pinch-to-dismiss animation */
.mv-overlay.mv-pinch-dismiss .mv-page .mv-content,
.mv-overlay.mv-pinch-dismiss .mv-page video {
    transition: transform 0.2s ease-out, opacity 0.2s ease-out;
}

/* Ensure smooth background transitions during dismiss gestures */
.mv-overlay {
    transition: opacity 0.2s ease-out, visibility 0.2s, background-color 0s;
}

/* =========================================================================
   MARK: Chrome Auto-Hide Layer
   ========================================================================= */

/*
 * Layered visibility system:
 * - Chrome layer: controls overall UI visibility based on inactivity
 * - Video layer: controls video bar based on media type
 *
 * Final visibility = chrome visible AND layer-specific visible
 * This allows independent state management without coupling.
 */

/* Elements that fade out when chrome is hidden */
.mv-overlay .mv-counter,
.mv-overlay .mv-bottom-bar,
.mv-overlay .mv-action-bar,
.mv-overlay .mv-liked-by,
.mv-overlay .mv-btn-delete,
.mv-overlay .mv-btn-close {
    transition:
        transform 0.15s cubic-bezier(0.4, 0, 0.2, 1),
        opacity 0.15s cubic-bezier(0.4, 0, 0.2, 1),
        visibility 0.15s;
}

/* Chrome hidden state - fade out top bar elements */
.mv-overlay.mv-chrome-hidden .mv-counter,
.mv-overlay.mv-chrome-hidden .mv-btn-delete,
.mv-overlay.mv-chrome-hidden .mv-btn-close {
    opacity: 0;
    visibility: hidden;
}

/* Bottom bar: chrome-hidden overrides video-visible */
.mv-overlay.mv-chrome-hidden .mv-bottom-bar {
    opacity: 0 !important;
    visibility: hidden !important;
}

/* Action bar and liked-by: chrome-hidden overrides visible state */
.mv-overlay.mv-chrome-hidden .mv-action-bar {
    opacity: 0 !important;
    visibility: hidden !important;
}

.mv-overlay.mv-chrome-hidden .mv-liked-by {
    opacity: 0 !important;
    visibility: hidden !important;
}

/* =========================================================================
   MARK: Mobile Landscape
   No status bar in landscape — add a small top inset so UI doesn't
   sit flush against the screen edge. Extra 16px horizontal padding
   pushes elements away from the notch boundary.
   ========================================================================= */

@media (orientation: landscape) and (hover: none) and (pointer: coarse) {
    .mv-top-bar {
        top: calc(var(--sa-top) + 10px);
        left: calc(16px + max(var(--sa-left), var(--sa-right), 16px));
        right: calc(16px + max(var(--sa-left), var(--sa-right), 16px));
    }

    .mv-bottom-bar {
        left: calc(16px + max(var(--sa-left), var(--sa-right), 16px));
        right: calc(16px + max(var(--sa-left), var(--sa-right), 16px));
    }

    .mv-liked-by {
        top: calc(var(--sa-top) + 10px + 60px);
    }
}

/* =========================================================================
   MARK: Reduced Motion
   ========================================================================= */

@media (prefers-reduced-motion: reduce) {
    /* Disable all transitions and animations */
    .mv-overlay,
    .mv-overlay *,
    .mv-overlay *::before,
    .mv-overlay *::after {
        animation-duration: 0.01ms !important;
        animation-iteration-count: 1 !important;
        transition-duration: 0.01ms !important;
    }

    /* Remove the loading spinner animation */
    @keyframes mv-spin {
        to { transform: rotate(0deg); }
    }
}
