/* EPUB Reader Styles */

.epub-reader-container,
.epub-protected-container {
    border: 1px solid #ddd;
    border-radius: 8px;
    overflow: visible;
    background: #fff;
    box-shadow: 0 2px 10px rgba(0, 0, 0, 0.1);
    margin: 20px 0;
    min-height: 500px;
    display: flex;
    flex-direction: column;
    height: auto;
}

.epub-title {
    margin: 0;
    padding: 15px 20px;
    background: #f8f9fa;
    border-bottom: 1px solid #ddd;
    font-size: 18px;
    font-weight: 600;
    color: #333;
    flex-shrink: 0;
}

.epub-viewer {
    width: 100%;
    height: auto;
    min-height: 500px;
    position: relative;
    background: #fafafa;
    flex: 1;
    overflow: visible;
}

.epub-viewer iframe {
    width: 100%;
    height: 100%;
    border: none;
    overflow: visible;
}

.epub-viewer .epub-view {
    height: auto !important;
    min-height: 500px;
    overflow: visible !important;
}

.epub-viewer .epub-view iframe {
    height: auto !important;
    min-height: 500px;
    overflow: visible !important;
}


/* Additional styles for better content flow */

.epub-viewer .epub-view .epub-view {
    height: auto !important;
    min-height: 500px;
    overflow: visible !important;
}

.epub-viewer .epub-view .epub-view iframe {
    height: auto !important;
    min-height: 500px;
    overflow: visible !important;
}


/* Handle EPUB.js pagination better */

.epub-viewer .epub-view {
    page-break-inside: avoid;
    break-inside: avoid;
}

.epub-viewer .epub-view .epub-view {
    page-break-inside: avoid;
    break-inside: avoid;
}


/* Ensure proper spacing and layout */

.epub-viewer .epub-view {
    margin: 0;
    padding: 0;
    box-sizing: border-box;
}

.epub-viewer .epub-view .epub-view {
    margin: 0;
    padding: 0;
    box-sizing: border-box;
}


/* Controls */

.epub-controls {
    display: flex;
    align-items: center;
    justify-content: center;
    padding: 10px 20px;
    background: #f8f9fa;
    border-bottom: 1px solid #ddd;
    gap: 10px;
    flex-shrink: 0;
}

.epub-controls button {
    background: #afb211;
    color: white;
    border: none;
    padding: 8px 12px;
    border-radius: 4px;
    cursor: pointer;
    font-size: 14px;
    transition: background-color 0.2s;
}

.epub-controls button:hover {
    background: #afb211;
}

.epub-controls button:active {
    transform: translateY(1px);
}

.epub-controls button.navigating {
    opacity: 0.6;
    cursor: not-allowed;
    pointer-events: none;
}

.epub-controls button.navigating:hover {
    background: #262b30;
}

.epub-location {
    font-size: 14px;
    color: #666;
    margin: 0 15px;
    min-width: 80px;
    text-align: center;
}


/* Password Protection */

.epub-protected {
    position: relative;
    height: 100%;
    min-height: 400px;
}

.epub-password-form {
    padding: 40px 20px;
    text-align: center;
    background: #f8f9fa;
    height: 100%;
    display: flex;
    flex-direction: column;
    justify-content: center;
    align-items: center;
}

.epub-message {
    margin-bottom: 20px;
    color: #333;
    font-size: 16px;
    line-height: 1.5;
}

.epub-password-input {
    width: 100%;
    max-width: 300px;
    padding: 12px 15px;
    border: 2px solid #ddd;
    border-radius: 6px;
    font-size: 16px;
    margin-bottom: 15px;
    transition: border-color 0.2s;
}

.epub-password-input:focus {
    outline: none;
    box-shadow: 0 0 0 3px rgba(0, 124, 186, 0.1);
}

.epub-password-submit {
    background: #afb211;
    color: white;
    border: none;
    padding: 12px 24px;
    border-radius: 6px;
    font-size: 16px;
    cursor: pointer;
    transition: background-color 0.2s;
}

.epub-password-submit:hover {
    background: #afb211;
}

.epub-password-submit:active {
    transform: translateY(1px);
}

.epub-password-error {
    color: #dc3545;
    font-size: 14px;
    margin-top: 10px;
    padding: 8px 12px;
    background: #f8d7da;
    border: 1px solid #f5c6cb;
    border-radius: 4px;
}

.epub-viewer-container {
    width: 100%;
    height: 100%;
    min-height: 400px;
}


/* Table of Contents Modal */

.epub-toc-modal {
    position: fixed !important;
    top: 0 !important;
    left: 0 !important;
    width: 100vw !important;
    height: 100vh !important;
    z-index: 2147483647 !important;
    /* Maximum z-index to ensure visibility */
    display: none;
    /* Hidden by default, shown with JavaScript */
    align-items: center !important;
    justify-content: center !important;
    padding: 20px !important;
    box-sizing: border-box !important;
    overflow: hidden !important;
    /* Force visibility when shown */
    visibility: visible !important;
    opacity: 1 !important;
}


/* When TOC modal is shown, ensure it's visible */

.epub-toc-modal:not([style*="display: none"]) {
    display: flex !important;
}

.epub-toc-overlay {
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background: rgba(0, 0, 0, 0.6);
    backdrop-filter: blur(2px);
    -webkit-backdrop-filter: blur(2px);
    /* Ensure overlay is clickable */
    cursor: pointer;
    z-index: 2147483646;
}

.epub-toc-content {
    position: relative !important;
    z-index: 2147483647 !important;
    /* Ensure content is always on top */
    background: white;
    border-radius: 12px;
    padding: 0;
    max-width: 600px;
    max-height: 80vh;
    width: 90%;
    min-width: 300px;
    box-shadow: 0 8px 32px rgba(0, 0, 0, 0.3);
    overflow: hidden;
    margin: auto;
    /* Force hardware acceleration */
    transform: translateZ(0);
    /* Force centering even if parent flexbox fails */
    top: auto;
    left: auto;
    right: auto;
    bottom: auto;
}

.epub-toc-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 20px 30px;
    border-bottom: 1px solid #eee;
    background: #f8f9fa;
}

.epub-toc-header h3 {
    margin: 0;
    color: #333;
    font-size: 20px;
    font-weight: 600;
}

.epub-toc-close {
    background: none;
    border: none;
    font-size: 24px;
    color: #666;
    cursor: pointer;
    padding: 5px;
    border-radius: 4px;
    transition: all 0.2s;
    width: 32px;
    height: 32px;
    display: flex;
    align-items: center;
    justify-content: center;
    /* Ensure close button is easily clickable */
    z-index: 2147483647;
    position: relative;
}

.epub-toc-close:hover {
    background: #e9ecef;
    color: #333;
}

.epub-toc-list {
    max-height: calc(80vh - 80px);
    overflow-y: auto;
    padding: 20px 30px;
}

.epub-toc-list ul {
    list-style: none;
    padding: 0;
    margin: 0;
}

.epub-toc-list li {
    margin: 0;
    border-bottom: 1px solid #f0f0f0;
}

.epub-toc-list li:last-child {
    border-bottom: none;
}

.epub-toc-list a {
    color: #333;
    text-decoration: none;
    padding: 12px 0;
    display: block;
    border-radius: 4px;
    transition: all 0.2s;
    font-size: 16px;
    line-height: 1.4;
}

.epub-toc-list a:hover {
    background: #f8f9fa;
    color: #007cba;
    padding-left: 8px;
}

.epub-toc-list a:focus {
    outline: 2px solid #007cba;
    outline-offset: 2px;
}

.epub-toc-error {
    padding: 40px 30px;
    text-align: center;
    color: #666;
}

.epub-toc-error p {
    margin: 10px 0;
    font-size: 16px;
}


/* Fullscreen TOC Modal */

.epub-reader-container.fullscreen .epub-toc-modal,
.epub-protected-container.fullscreen .epub-toc-modal {
    z-index: 2147483647;
    /* Maximum z-index to appear above fullscreen container */
    position: fixed !important;
    top: 0 !important;
    left: 0 !important;
    width: 100vw !important;
    height: 100vh !important;
    /* Ensure modal covers entire viewport even when appended to fullscreen container */
}

.epub-reader-container.fullscreen .epub-toc-content,
.epub-protected-container.fullscreen .epub-toc-content {
    max-width: 700px;
    max-height: 85vh;
    z-index: 2147483647;
    /* Ensure content also has maximum z-index */
}


/* Error States */

.epub-error {
    display: flex;
    align-items: center;
    justify-content: center;
    height: 100%;
    color: #dc3545;
    font-size: 16px;
    text-align: center;
    padding: 20px;
}


/* Fullscreen Mode */

.epub-reader-container.fullscreen,
.epub-protected-container.fullscreen {
    position: fixed;
    top: 0;
    left: 0;
    width: 100vw;
    height: 100vh;
    z-index: 9999;
    border: none;
    border-radius: 0;
}

.epub-reader-container.fullscreen .epub-viewer,
.epub-protected-container.fullscreen .epub-viewer {
    height: calc(100vh - 120px);
}


/* iOS Safari specific fullscreen styles */

@supports (-webkit-touch-callout: none) {
    .epub-reader-container.fullscreen,
    .epub-protected-container.fullscreen {
        /* iOS Safari fullscreen adjustments */
        position: fixed !important;
        top: 0 !important;
        left: 0 !important;
        width: 100vw !important;
        height: 100vh !important;
        z-index: 2147483647 !important;
        /* Maximum z-index for iOS */
        -webkit-transform: translateZ(0);
        /* Force hardware acceleration */
        transform: translateZ(0);
    }
    .epub-reader-container.fullscreen .epub-viewer,
    .epub-protected-container.fullscreen .epub-viewer {
        height: calc(100vh - 120px) !important;
        -webkit-overflow-scrolling: touch;
        /* Smooth scrolling on iOS */
    }
    /* Ensure controls are visible and properly positioned on iOS */
    .epub-reader-container.fullscreen .epub-controls,
    .epub-protected-container.fullscreen .epub-controls {
        position: relative;
        z-index: 2147483646;
        background: rgba(248, 249, 250, 0.95);
        backdrop-filter: blur(10px);
        -webkit-backdrop-filter: blur(10px);
    }
}


/* Show table of contents button in fullscreen mode */

.epub-reader-container.fullscreen .epub-toc,
.epub-protected-container.fullscreen .epub-toc {
    display: inline-block !important;
    /* Ensure TOC button is visible in fullscreen */
    position: relative;
    z-index: 2147483646;
}


/* Adjust controls spacing in fullscreen mode */

.epub-reader-container.fullscreen .epub-controls,
.epub-protected-container.fullscreen .epub-controls {
    justify-content: center;
    gap: 15px;
}


/* Ensure proper spacing between remaining buttons in fullscreen */

.epub-reader-container.fullscreen .epub-controls button:not(.epub-toc),
.epub-protected-container.fullscreen .epub-controls button:not(.epub-toc) {
    /* margin: 0 5px; */
}


/* Responsive Design */

@media (max-width: 768px) {
    .epub-controls {
        flex-wrap: wrap;
        gap: 5px;
    }
    .epub-controls button {
        padding: 6px 10px;
        font-size: 12px;
        /* Ensure buttons are touch-friendly on mobile */
        min-height: 44px;
        min-width: 44px;
        touch-action: manipulation;
    }
    .epub-location {
        font-size: 12px;
        margin: 0 10px;
    }
    .epub-password-form {
        padding: 20px 15px;
        min-height: 300px;
    }
    .epub-message {
        font-size: 14px;
    }
    .epub-password-input {
        font-size: 14px;
        padding: 10px 12px;
    }
    .epub-password-submit {
        font-size: 14px;
        padding: 10px 20px;
    }
    .epub-toc-content {
        margin: 20px;
        padding: 0;
        max-width: calc(100vw - 40px);
        max-height: calc(100vh - 40px);
        width: calc(100vw - 40px);
        min-width: 280px;
        /* Ensure proper centering on mobile */
        margin: 10px auto;
        transform: translateZ(0);
    }
    .epub-toc-header {
        padding: 15px 20px;
    }
    .epub-toc-header h3 {
        font-size: 18px;
    }
    .epub-toc-list {
        padding: 15px 20px;
        max-height: calc(100vh - 120px);
    }
    .epub-toc-list a {
        font-size: 14px;
        padding: 10px 0;
    }
    .epub-protected-container,
    .epub-reader-container {
        min-height: 400px;
    }
    .epub-viewer {
        min-height: 350px;
    }
    /* Fullscreen button specific mobile styles */
    .epub-controls .epub-fullscreen {
        /* Ensure fullscreen button is easily tappable */
        min-width: 50px;
        min-height: 44px;
        font-size: 16px;
        font-weight: bold;
        background: #afb211;
        color: white;
        border: 2px solid #007cba;
        border-radius: 6px;
        transition: all 0.2s ease;
    }
    .epub-controls .epub-fullscreen:active {
        background: #afb211;
        border-color: #afb211;
        transform: scale(0.95);
    }
}

@media (max-width: 480px) {
    .epub-controls {
        padding: 8px 15px;
    }
    .epub-controls button {
        padding: 5px 8px;
        font-size: 11px;
        /* iPhone-specific touch improvements */
        min-height: 44px;
        min-width: 44px;
        touch-action: manipulation;
        -webkit-tap-highlight-color: transparent;
    }
    .epub-title {
        font-size: 16px;
        padding: 12px 15px;
    }
    /* Mobile TOC Modal */
    .epub-toc-content {
        margin: 10px;
        max-width: calc(100vw - 20px);
        max-height: calc(100vh - 20px);
        width: calc(100vw - 20px);
        min-width: 260px;
        /* Ensure perfect centering on iPhone */
        margin: 5px auto;
        transform: translateZ(0);
        border-radius: 8px;
    }
    .epub-toc-header {
        padding: 12px 15px;
    }
    .epub-toc-header h3 {
        font-size: 16px;
    }
    .epub-toc-close {
        font-size: 20px;
        width: 28px;
        height: 28px;
    }
    .epub-toc-list {
        padding: 12px 15px;
        max-height: calc(100vh - 100px);
    }
    .epub-toc-list a {
        font-size: 13px;
        padding: 8px 0;
    }
    /* iPhone fullscreen button improvements */
    .epub-controls .epub-fullscreen {
        min-width: 52px;
        min-height: 48px;
        font-size: 18px;
        font-weight: bold;
        background: #afb211;
        color: white;
        border: 2px solid #afb211;
        border-radius: 8px;
        box-shadow: 0 2px 4px rgba(0, 0, 0, 0.2);
        /* Ensure button is always visible */
        display: inline-block !important;
        visibility: visible !important;
        opacity: 1 !important;
        /* Prevent any hiding */
        position: relative !important;
        z-index: 10 !important;
    }
    .epub-controls .epub-fullscreen:active {
        background: #afb211;
        border-color: #afb211;
        transform: scale(0.92);
        box-shadow: 0 1px 2px rgba(0, 0, 0, 0.3);
    }
    /* Ensure fullscreen button is never hidden on mobile */
    .epub-controls .epub-fullscreen {
        display: inline-block !important;
        visibility: visible !important;
        opacity: 1 !important;
    }
    /* iPhone fullscreen container improvements */
    .epub-reader-container.fullscreen,
    .epub-protected-container.fullscreen {
        /* Ensure proper iPhone fullscreen behavior */
        position: fixed !important;
        top: 0 !important;
        left: 0 !important;
        width: 100vw !important;
        height: 100vh !important;
        z-index: 2147483647 !important;
        -webkit-transform: translateZ(0);
        transform: translateZ(0);
        /* Prevent iPhone Safari UI interference */
        -webkit-user-select: none;
        user-select: none;
        -webkit-touch-callout: none;
    }
}


/* Loading States */

.epub-loading {
    display: flex;
    align-items: center;
    justify-content: center;
    height: 100%;
    color: #666;
    font-size: 16px;
}

.epub-loading::before {
    content: '';
    width: 20px;
    height: 20px;
    border: 2px solid #ddd;
    border-top: 2px solid #007cba;
    border-radius: 50%;
    animation: spin 1s linear infinite;
    margin-right: 10px;
}

@keyframes spin {
    0% {
        transform: rotate(0deg);
    }
    100% {
        transform: rotate(360deg);
    }
}


/* Accessibility */

.epub-controls button:focus,
.epub-password-input:focus,
.epub-password-submit:focus {
    outline: 2px solid #afb211;
    outline-offset: 2px;
}


/* High contrast mode support */

@media (prefers-contrast: high) {
    .epub-reader-container,
    .epub-protected-container {
        border: 2px solid #000;
    }
    .epub-controls button {
        border: 1px solid #000;
    }
}


/* iOS Fullscreen Notification */

.epub-ios-notification {
    position: fixed;
    top: 20px;
    left: 50%;
    transform: translateX(-50%);
    background: #afb211;
    color: white;
    padding: 15px 20px;
    border-radius: 8px;
    box-shadow: 0 4px 12px rgba(0, 0, 0, 0.3);
    z-index: 2147483647;
    max-width: 90%;
    text-align: center;
    font-size: 14px;
    line-height: 1.4;
    animation: slideDown 0.3s ease-out;
}

@keyframes slideDown {
    from {
        transform: translateX(-50%) translateY(-100%);
        opacity: 0;
    }
    to {
        transform: translateX(-50%) translateY(0);
        opacity: 1;
    }
}


/* iPhone-specific notification adjustments */

@media (max-width: 480px) {
    .epub-ios-notification {
        top: 10px;
        left: 10px;
        right: 10px;
        transform: none;
        max-width: none;
        font-size: 13px;
        padding: 12px 15px;
    }
    @keyframes slideDown {
        from {
            transform: translateY(-100%);
            opacity: 0;
        }
        to {
            transform: translateY(0);
            opacity: 1;
        }
    }
}


/* Global fullscreen button visibility - ensure it's always shown */

.epub-controls .epub-fullscreen {
    display: inline-block !important;
    visibility: visible !important;
    opacity: 1 !important;
    position: relative !important;
    z-index: 10 !important;
}


/* Additional iPhone-specific fullscreen button styles */

@media (max-width: 480px) {
    .epub-controls .epub-fullscreen {
        display: inline-block !important;
        visibility: visible !important;
        opacity: 1 !important;
        min-width: 44px !important;
        min-height: 44px !important;
        font-size: 18px !important;
        background: #afb211 !important;
        color: white !important;
        border: 2px solid #afb211 !important;
        border-radius: 8px !important;
        box-shadow: 0 2px 4px rgba(0, 0, 0, 0.2) !important;
    }
}


/* iPhone 390px width specific control button layout */

@media (max-width: 390px) {
    .epub-controls {
        display: flex !important;
        flex-direction: row !important;
        flex-wrap: nowrap !important;
        align-items: center !important;
        justify-content: space-between !important;
        padding: 8px 12px !important;
        gap: 6px !important;
        width: 100% !important;
        box-sizing: border-box !important;
    }
    .epub-controls button {
        display: inline-block !important;
        flex: 0 0 auto !important;
        min-width: 44px !important;
        min-height: 44px !important;
        padding: 6px 8px !important;
        font-size: 14px !important;
        margin: 0 !important;
        white-space: nowrap !important;
        overflow: hidden !important;
        text-overflow: ellipsis !important;
        touch-action: manipulation !important;
        -webkit-tap-highlight-color: transparent !important;
    }
    .epub-controls .epub-fullscreen {
        display: inline-block !important;
        visibility: visible !important;
        opacity: 1 !important;
        min-width: 48px !important;
        min-height: 44px !important;
        font-size: 16px !important;
        padding: 6px 8px !important;
        margin: 0 !important;
        flex: 0 0 auto !important;
    }
    .epub-location {
        display: inline-block !important;
        font-size: 12px !important;
        margin: 0 8px !important;
        min-width: 60px !important;
        flex: 0 0 auto !important;
        text-align: center !important;
    }
    /* Ensure all buttons are visible and properly spaced */
    .epub-controls button:not(.epub-fullscreen) {
        display: inline-block !important;
        visibility: visible !important;
        opacity: 1 !important;
        min-width: 44px !important;
        min-height: 44px !important;
        font-size: 14px !important;
        padding: 6px 8px !important;
        margin: 0 !important;
        flex: 0 0 auto !important;
    }
    /* Prevent button wrapping */
    .epub-controls {
        overflow: hidden !important;
        white-space: nowrap !important;
    }
    /* Ensure proper touch targets */
    .epub-controls button,
    .epub-controls .epub-fullscreen {
        touch-action: manipulation !important;
        -webkit-tap-highlight-color: transparent !important;
        user-select: none !important;
        -webkit-user-select: none !important;
    }
}