/**
 * Flipbook Viewer Styles
 * Matches diagram-viewer-modal sizing and patterns exactly
 * 
 * @version 4.4.0
 * @updated January 11, 2026
 */

/* ================================
   MODAL - Matches diagram-viewer-modal exactly
   ================================ */
.flipbook-viewer-modal {
    z-index: 100003; /* Above other modals */
}

.flipbook-viewer-modal .flipbook-modal-content {
    /* Desktop: Match diagram-viewer-modal sizing (75vw/75vh) */
    width: 75vw;
    height: 75vh;
    max-width: 1400px;  /* Match diagram viewer */
    max-height: 90vh;   /* Match diagram viewer */
    display: flex;
    flex-direction: column;
    border-radius: 12px;
    overflow: hidden;
    border: 2px solid var(--border-light);
    box-shadow: 0 4px 20px rgba(0, 0, 0, 0.12), 0 2px 8px rgba(0, 0, 0, 0.08);
}

/* Header - Solid theme primary color */
.flipbook-viewer-modal .modal-header {
    flex-shrink: 0;
    background: var(--theme-primary);
    color: var(--theme-action-text);
    padding: 16px 24px;
    border-radius: 12px 12px 0 0;
    display: flex;
    justify-content: space-between;
    align-items: center;
}

.flipbook-viewer-modal .modal-header h3 {
    color: var(--theme-action-text) !important;
    margin: 0;
    font-size: 18px;
    font-weight: 600;
    display: flex;
    align-items: center;
    gap: 10px;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

.flipbook-viewer-modal .modal-header h3 i {
    font-size: 18px;
}

.flipbook-viewer-modal .modal-close {
    color: var(--theme-action-text);
    opacity: 0.9;
    background: transparent;
    border: none;
    font-size: 28px;
    cursor: pointer;
    padding: 0 8px;
    line-height: 1;
    transition: opacity 0.2s, background 0.2s;
    border-radius: 4px;
}

.flipbook-viewer-modal .modal-close:hover {
    opacity: 1;
    background: rgba(255, 255, 255, 0.2);
}

/* Body - nav buttons positioned here (not in scroll area) */
.flipbook-viewer-body {
    flex: 1;
    display: flex;
    flex-direction: column;
    background: var(--bg-primary);
    overflow: hidden;
    min-height: 0; /* CRITICAL: Allow flex child to shrink below content size */
    position: relative; /* Nav buttons positioned relative to this */
}

/* Embed container */
.flipbook-embed-container {
    flex: 1;
    width: 100%;
    height: 100%;
}

/* Slide area - contains image with proper sizing for both orientations */
.flipbook-slide-area {
    flex: 1;
    display: flex;
    align-items: center; /* Center vertically */
    justify-content: center;
    padding: 24px 70px; /* Top/bottom padding for landscape slides, sides for nav buttons */
    background: var(--bg-primary);
    min-height: 0; /* CRITICAL: Allow flex child to shrink */
    overflow: auto; /* Scroll if needed (portrait docs) */
    scroll-behavior: smooth;
    box-sizing: border-box;
}

/* Scroll indicator shadow at bottom when content is scrollable */
.flipbook-scroll-indicator {
    position: absolute;
    bottom: 70px; /* Above thumbnails */
    left: 70px;
    right: 70px;
    height: 30px;
    background: linear-gradient(to bottom, transparent, rgba(0,0,0,0.1));
    pointer-events: none;
    opacity: 0;
    transition: opacity 0.3s;
}

.flipbook-scroll-indicator.visible {
    opacity: 1;
}

/* Navigation buttons - FIXED to body (don't scroll with content) */
.flipbook-nav-btn {
    position: absolute;
    top: 50%;
    transform: translateY(-50%);
    width: 48px;
    height: 48px;
    border-radius: 50%;
    background: var(--bg-secondary);
    border: 1px solid var(--border-light);
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    z-index: 20; /* Above scroll area */
    transition: all 0.2s;
    color: var(--text-primary);
    box-shadow: 0 2px 8px rgba(0,0,0,0.15);
}

.flipbook-nav-btn:hover {
    background: var(--bg-tertiary);
    transform: translateY(-50%) scale(1.05);
}

.flipbook-nav-prev {
    left: 12px;
}

.flipbook-nav-next {
    right: 12px;
}

.flipbook-nav-btn i {
    font-size: 18px;
}

/* Slide container - centers content with padding buffer */
.flipbook-slide-container {
    display: flex;
    align-items: center;
    justify-content: center;
    width: 100%;
    height: 100%;
    box-sizing: border-box;
}

.flipbook-slide {
    display: flex;
    align-items: center;
    justify-content: center;
    max-width: 100%;
    max-height: 100%;
}

/* Image: fits within container, maintaining aspect ratio
   - Landscape: fits height with top/bottom padding buffer
   - Portrait: fits width, may scroll vertically */
.flipbook-slide img {
    max-width: 100%;
    max-height: calc(100% - 8px); /* Small buffer to prevent touching edges */
    width: auto;
    height: auto;
    object-fit: contain;
    border-radius: 4px;
    box-shadow: 0 4px 20px rgba(0, 0, 0, 0.3);
}

/* Thumbnails strip - compact to give more space to slide */
.flipbook-thumbnails-strip {
    flex-shrink: 0;
    max-height: 70px;
    position: relative;
    z-index: 15;
}

/* Text slide */
.flipbook-text-slide {
    padding: 40px;
    background: var(--bg-secondary);
    border-radius: 8px;
    max-width: 800px;
    text-align: center;
}

.flipbook-text-slide p {
    margin: 0;
    font-size: 18px;
    line-height: 1.6;
    color: var(--text-primary);
}

/* Empty slide */
.flipbook-empty-slide {
    padding: 60px;
    background: var(--bg-secondary);
    border-radius: 8px;
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 12px;
    color: var(--text-muted);
}

.flipbook-empty-slide i {
    font-size: 48px;
}

/* Thumbnails strip */
.flipbook-thumbnails-strip {
    display: flex;
    gap: 8px;
    padding: 12px 16px;
    background: var(--bg-secondary);
    border-top: 1px solid var(--border-light);
    overflow-x: auto;
    scrollbar-width: thin;
}

.flipbook-thumb {
    flex-shrink: 0;
    width: 60px;
    height: 40px;
    border-radius: 4px;
    overflow: hidden;
    cursor: pointer;
    border: 2px solid transparent;
    transition: border-color 0.2s, transform 0.2s;
    background: var(--bg-tertiary);
}

.flipbook-thumb:hover {
    transform: scale(1.05);
}

.flipbook-thumb.active {
    border-color: var(--theme-primary);
}

.flipbook-thumb img {
    width: 100%;
    height: 100%;
    object-fit: cover;
}

.flipbook-thumb-placeholder {
    width: 100%;
    height: 100%;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 12px;
    color: var(--text-muted);
}

/* Controls - Matches video/diagram viewer */
.flipbook-viewer-controls {
    flex-shrink: 0;
    display: flex;
    align-items: center;
    gap: 12px;
    padding: 14px 20px;
    background: var(--bg-secondary);
    border-top: 1px solid var(--border-light);
}

.flipbook-viewer-controls .modal-btn {
    padding: 10px 14px;
    font-size: 13px;
    border-radius: 6px;
    cursor: pointer;
    display: flex;
    align-items: center;
    gap: 6px;
    transition: all 0.2s;
}

.flipbook-viewer-controls .modal-btn.secondary {
    background: var(--bg-tertiary);
    border: 1px solid var(--border-light);
    color: var(--text-primary);
}

.flipbook-viewer-controls .modal-btn.secondary:hover {
    background: var(--bg-primary);
}

.flipbook-viewer-controls .modal-btn:not(.secondary) {
    background: var(--theme-primary);
    border: 1px solid var(--theme-primary);
    color: var(--theme-action-text);
}

.flipbook-slide-counter {
    font-size: 14px;
    color: var(--text-secondary);
    min-width: 60px;
    text-align: center;
}

/* ================================
   FLIP STYLE ANIMATIONS
   ================================ */

/* Card flip style */
.flipbook-style-card .flipbook-slide {
    animation: flipbook-card-in 0.4s ease-out;
}

@keyframes flipbook-card-in {
    from {
        transform: rotateY(90deg);
        opacity: 0;
    }
    to {
        transform: rotateY(0);
        opacity: 1;
    }
}

/* Slide style (carousel) */
.flipbook-style-slide .flipbook-slide {
    animation: flipbook-slide-in 0.3s ease-out;
}

@keyframes flipbook-slide-in {
    from {
        transform: translateX(100%);
        opacity: 0;
    }
    to {
        transform: translateX(0);
        opacity: 1;
    }
}

/* Book page turn style */
.flipbook-style-book .flipbook-slide {
    animation: flipbook-book-in 0.5s ease-out;
    transform-origin: left center;
}

@keyframes flipbook-book-in {
    from {
        transform: perspective(1500px) rotateY(-90deg);
        opacity: 0;
    }
    to {
        transform: perspective(1500px) rotateY(0);
        opacity: 1;
    }
}

/* Fade style */
.flipbook-style-fade .flipbook-slide {
    animation: flipbook-fade-in 0.4s ease-out;
}

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

/* None style - no animation (instant change) */
.flipbook-style-none .flipbook-slide {
    animation: none;
}

/* Respect user's prefers-reduced-motion preference */
@media (prefers-reduced-motion: reduce) {
    .flipbook-style-card .flipbook-slide,
    .flipbook-style-slide .flipbook-slide,
    .flipbook-style-book .flipbook-slide,
    .flipbook-style-fade .flipbook-slide {
        animation: none;
    }
}

/* ================================
   IN-CHAT FLIPBOOK DISPLAY
   ================================ */

/* Chat flipbook container - content-sized, buttons relative to content */
.json-flipbook-section {
    display: inline-block; /* Size to content */
    max-width: 100%;
    margin: 12px 0;
}

/* Chat slideshow container - PDF/Image flipbooks
   Container sizes to content (no black bars)
   Buttons positioned relative to actual content, not full-width container */
.chat-flipbook-slideshow {
    position: relative;
    border-radius: 8px;
    overflow: visible; /* Allow buttons to be visible */
    margin-bottom: 12px;
    display: inline-block; /* Size to content, not full width */
    max-width: 100%; /* Don't exceed parent width */
}

.chat-flipbook-slide {
    display: block;
    line-height: 0; /* Remove extra space below image */
}

/* In-chat flip style animations (same keyframes as modal) */
.chat-flipbook-slideshow.flipbook-style-card .chat-flipbook-slide img {
    animation: flipbook-card-in 0.4s ease-out;
}

.chat-flipbook-slideshow.flipbook-style-slide .chat-flipbook-slide img {
    animation: flipbook-slide-in 0.3s ease-out;
}

.chat-flipbook-slideshow.flipbook-style-book .chat-flipbook-slide img {
    animation: flipbook-book-in 0.5s ease-out;
    transform-origin: left center;
}

.chat-flipbook-slideshow.flipbook-style-fade .chat-flipbook-slide img {
    animation: flipbook-fade-in 0.4s ease-out;
}

.chat-flipbook-slideshow.flipbook-style-none .chat-flipbook-slide img {
    animation: none;
}

/* Respect prefers-reduced-motion for in-chat flipbooks */
@media (prefers-reduced-motion: reduce) {
    .chat-flipbook-slideshow .chat-flipbook-slide img {
        animation: none !important;
    }
}

/* Image fills container width, height is natural (full page visible) */
.chat-flipbook-slide img {
    width: 100%;
    height: auto;
    border-radius: 8px;
    display: block;
}

/* Chat slideshow controls overlay - always in top-right of content */
.chat-flipbook-controls {
    position: absolute;
    top: 8px;
    right: 8px;
    z-index: 100;
    display: flex;
    gap: 6px;
}

/* Flipbook expand button - matches diagram/image/video/chart expand buttons */
.chat-flipbook-btn {
    background: var(--theme-primary, #11a3f0);
    color: var(--theme-action-text);
    border: none;
    border-radius: 6px;
    padding: 8px 12px;
    cursor: pointer;
    font-size: 14px;
    display: flex;
    align-items: center;
    gap: 6px;
    box-shadow: 0 2px 8px rgba(0, 0, 0, 0.15);
    transition: all 0.2s ease;
    /* CRITICAL: Mobile touch fixes - prevents double-tap requirement */
    touch-action: manipulation;
    -webkit-tap-highlight-color: transparent;
    user-select: none;
}

/* Hover effects only on devices with hover capability (not touch) */
@media (hover: hover) and (pointer: fine) {
    .chat-flipbook-btn:hover {
        background: var(--theme-primary-dark, #0d8fd8);
        box-shadow: 0 4px 12px rgba(0, 0, 0, 0.25);
        transform: translateY(-1px);
    }
}

.chat-flipbook-btn i {
    font-size: 13px;
}

[data-theme="dark"] .chat-flipbook-btn {
    background: var(--theme-primary-light, #4DD0E1);
    color: var(--bg-primary, #1e1e1e);
}

/* Dark theme hover - only on devices with hover capability */
@media (hover: hover) and (pointer: fine) {
    [data-theme="dark"] .chat-flipbook-btn:hover {
        background: var(--theme-primary-dark, #00ACC1);
    }
}

/* Mobile: Keep buttons inside container (overflow: hidden clips them otherwise) */
@media (max-width: 768px) {
    .chat-flipbook-controls {
        top: 8px;
        right: 8px;
    }
    
    .chat-flipbook-btn {
        padding: 6px 10px;
        font-size: 12px;
        border-radius: 5px;
        box-shadow: 0 2px 6px rgba(0, 0, 0, 0.2);
    }
}

/* Hide in print */
@media print {
    .chat-flipbook-btn,
    .chat-flipbook-controls,
    .chat-flipbook-nav {
        display: none !important;
    }
}

/* Chat slideshow navigation */
.chat-flipbook-nav {
    position: absolute;
    top: 50%;
    transform: translateY(-50%);
    width: 40px;
    height: 40px;
    border-radius: 50%;
    background: rgba(255, 255, 255, 0.9);
    border: none;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    opacity: 0;
    transition: opacity 0.2s;
    z-index: 5;
    /* Touch fixes */
    touch-action: manipulation;
    -webkit-tap-highlight-color: transparent;
}

/* Desktop: show nav on hover */
@media (hover: hover) and (pointer: fine) {
    .chat-flipbook-slideshow:hover .chat-flipbook-nav {
        opacity: 1;
    }
    
    .chat-flipbook-nav:hover {
        background: #fff;
        transform: translateY(-50%) scale(1.1);
    }
}

/* Mobile/Touch: always show nav buttons */
@media (hover: none), (pointer: coarse) {
    .chat-flipbook-nav {
        opacity: 0.8;
    }
}

.chat-flipbook-nav.prev {
    left: 12px;
}

.chat-flipbook-nav.next {
    right: 12px;
}

/* Slide counter badge */
.chat-flipbook-counter {
    position: absolute;
    bottom: 8px;
    left: 8px;
    background: rgba(0, 0, 0, 0.8);
    color: white;
    padding: 4px 10px;
    border-radius: 4px;
    font-size: 12px;
    font-weight: 500;
}

/* Chat flipbook caption */
.chat-flipbook-caption {
    margin: 8px 0 0 0;
    font-size: 13px;
    color: var(--text-secondary);
}

/* ================================
   MOBILE RESPONSIVE - Matches diagram-viewer-modal exactly
   ================================ */
@media (max-width: 1024px), (hover: none) and (pointer: coarse) {
    .flipbook-viewer-modal .flipbook-modal-content {
        width: 95%;
        max-width: none;
        margin: 10px;
        max-height: 80vh;
        border-radius: 12px;
        border: 1px solid var(--border-light);
        box-shadow: 0 8px 32px rgba(0, 0, 0, 0.15), 
                    0 4px 16px rgba(0, 0, 0, 0.1),
                    0 2px 8px rgba(0, 0, 0, 0.08);
    }
    
    .flipbook-viewer-modal .modal-header {
        border-radius: 12px 12px 0 0;
        padding: 14px 18px;
    }
    
    .flipbook-viewer-modal .modal-header h3 {
        font-size: 16px;
    }
    
    .flipbook-viewer-body {
        flex: 1;
        overflow: hidden;
    }
    
    .flipbook-slide-area {
        padding: 10px 40px;
    }
    
    .flipbook-nav-btn {
        width: 36px;
        height: 36px;
    }
    
    .flipbook-nav-prev {
        left: 5px;
    }
    
    .flipbook-nav-next {
        right: 5px;
    }
    
    .flipbook-viewer-controls {
        padding: 12px 14px;
        gap: 8px;
        flex-wrap: wrap;
        /* Touch-friendly controls */
        touch-action: manipulation;
    }
    
    .flipbook-viewer-controls .modal-btn {
        padding: 10px 12px;
        font-size: 12px;
        touch-action: manipulation;
        -webkit-tap-highlight-color: transparent;
    }
    
    /* Keep thumbnails visible on tablets if enabled in config */
    .flipbook-thumbnails-strip {
        max-height: 60px;
    }
    
    .flipbook-thumbnail {
        width: 50px;
        height: 35px;
    }
}

/* Small phones - match diagram-viewer-modal */
@media (max-width: 480px) {
    .flipbook-viewer-modal .flipbook-modal-content {
        max-height: 75vh;
        margin: 8px;
    }
    
    .flipbook-viewer-modal .modal-header {
        padding: 14px 16px;
    }
    
    .flipbook-viewer-controls {
        padding: 10px 12px;
    }
    
    /* Thumbnails can still show if enabled - just smaller */
    .flipbook-thumbnails-strip {
        max-height: 50px;
    }
    
    .flipbook-thumbnail {
        width: 40px;
        height: 28px;
    }
}

/* ================================
   DARK THEME
   ================================ */
[data-theme="dark"] .flipbook-viewer-modal .modal-header {
    background: var(--theme-primary);
}

[data-theme="dark"] .flipbook-viewer-body {
    background: var(--bg-primary);
}

[data-theme="dark"] .flipbook-viewer-controls {
    background: var(--bg-tertiary);
    border-color: var(--border-medium);
}
