:root {
    --mf-viewer-bg: #1a1a1a;
    --mf-toolbar-bg: #252525;
    --mf-toolbar-txt: #ffffff;
    --mf-btn-primary-bg: #007bff;
    --mf-btn-primary-txt: #ffffff;
    --mf-btn-primary-hover: #0056b3;
    --mf-btn-secondary-bg: #444444;
    --mf-btn-secondary-txt: #ffffff;
    --mf-btn-secondary-hover: #555555;
}

/* ========================================================================== */
/* SECTION 1: CANVAS CORE WRAPPERS                                            */
/* ========================================================================== */
#max-flipbook-app-wrapper {
    background: transparent;
    color: #ffffff;
    font-family: Arial, sans-serif;
}

#mf-view-viewer {
    display: none; 
    flex-direction: column;
    background: #222222 !important; /* Pure solid opaque mask to block under-page leakage */
    position: fixed !important;
    top: 0 !important;
    left: 0 !important;
    right: 0 !important;
    bottom: 0 !important;
    width: 100vw !important;
    height: 100vh !important;
    height: 100dvh !important; /* Uses dynamic mobile viewport to prevent browser menu bar push down */
    max-width: 100vw !important;
    max-height: 100vh !important;
    max-height: 100dvh !important;
    margin: 0 !important;
    padding: 0 !important;
    z-index: 9999999 !important;
    overflow: hidden;
}

/* ========================================================================== */
/* SECTION 2: TOP TOOLBAR & BUTTONS                                           */
/* ========================================================================== */
#toolbar {
    display: flex;
    align-items: center;
    gap: 10px;
    padding: 6px 10px; 
    min-height: 44px; 
    height: auto; 
    background: #222;
    color: white;
    box-sizing: border-box;
    flex-wrap: wrap; 
    position: relative; 
    flex-shrink: 0 !important;
}

#mf-back-to-shelf,
#toolbar button {
    flex: 0 0 auto;
    height: 30px; 
    display: inline-flex;
    align-items: center;
    justify-content: center;
    padding: 0 10px !important;
    font-size: 13px;
    font-weight: bold;
    background: #ffffff !important;
    color: #000000 !important;
    border: 1px solid #ffffff !important;
    border-radius: 2px;
    text-decoration: none;
    cursor: pointer;
    box-sizing: border-box;
}

#mf-back-to-shelf:hover,
#toolbar button:hover {
    background: #e5e5e5 !important;
    border-color: #e5e5e5 !important;
}

#searchInput {
    width: 200px; 
    height: 30px; 
    padding: 0 10px;
    font-size: 14px;
    box-sizing: border-box;
    border: 1px solid #ccc;
    border-radius: 2px;
}

/* FIXED: Dropped absolute tracking. The title now flows inline naturally */
.toolbar-center {
    display: inline-flex;
    align-items: center;
    pointer-events: auto;
}

.toolbar-right-spacer {
    margin-left: auto;
}

/* MATCHED STYLING: Scaled to match the adjacent buttons perfectly */
#dynamicTitle {
    font-size: 13px;            /* Same size as buttons */
    font-family: Arial, sans-serif; /* Same font family */
    font-weight: bold;          /* Kept bold */
    text-decoration: underline; /* Kept underlined */
    color: #ffffff;           
    white-space: nowrap;
    letter-spacing: normal;
    pointer-events: auto;     
}

/* ========================================================================== */
/* SECTION 3: BOTTOM TOOLBAR & PAGINATION                                     */
/* ========================================================================== */
#bottomToolbar {
    height: auto;
    min-height: 65px;
    background: #222;
    color: white;
    display: flex;
    flex-direction: column; 
    align-items: center;
    justify-content: center;
    padding: 4px 6px !important;
    box-sizing: border-box;
    flex-shrink: 0 !important;
}

.pagination-row {
    display: flex;
    flex-direction: row;
    align-items: center;
    justify-content: center;
    gap: 6px;
    width: 100%;
}

#bottomToolbar button {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    padding: 0 12px;
    height: 28px;
    cursor: pointer;
    background: #ffffff !important;
    color: #000000 !important;
    border: 1px solid #ffffff !important;
    border-radius: 2px;
    font-weight: bold;
    font-size: 14px;
}

#bottomToolbar button:hover {
    background: #e5e5e5 !important;
    border-color: #e5e5e5 !important;
}

#pageInput {
    width: 50px;
    height: 28px;
    padding: 0 5px;
    text-align: center;
    box-sizing: border-box;
    border: 1px solid #ccc;
    border-radius: 2px;
}

#pageLabel {
    font-size: 14px;
    color: #fff;
    line-height: 28px;
}

#toolbarCopyright {
    font-size: 11px;
    color: #aaa;              
    letter-spacing: 0.5px;
    text-align: center;
    width: 100%;
    display: block;
    margin-top: 6px; 
    pointer-events: none;
}

/* ========================================================================== */
/* SECTION 4: FLIPBOOK STRUCTURAL GRAPHICS WRAPPERS                           */
/* ========================================================================== */
#mainLayout {
    display: flex;
    flex: 1;
    min-height: 0; 
    box-sizing: border-box;
    overflow: hidden;
    width: 100%;
}

#bookContainer {
    background: #222;
    flex: 1;
    display: flex;
    justify-content: center;
    align-items: center;
    overflow: hidden;
    padding: 20px; 
    box-sizing: border-box;
    position: relative;
    width: 100%;
}

#flipbook {
    width: 100%;
    height: 100%;
    max-width: 1200px;
    position: relative;
    margin: 0 auto;
}

.page {
    background: #fff;
    overflow: hidden;
    box-shadow: 0 0 10px rgba(0,0,0,0.4);
}

.page img { 
    width: 100%; 
    height: 100%; 
    display: block; 
    object-fit: fill; 
}

.ocrLayer { 
    position: absolute; 
    top: 0; 
    left: 0; 
    width: 100%; 
    height: 100%; 
    pointer-events: none; 
    z-index: 5; 
}

.ocrRect { 
    position: absolute !important; 
    box-sizing: border-box; 
    border: 1px solid transparent; 
    background: transparent; 
    color: transparent; 
    user-select: text; 
    pointer-events: auto; 
}

.ocrRect:hover { background: rgba(0, 123, 255, 0.08); }
.ocrRect.search-match { background: rgba(255, 255, 0, 0.4) !important; border: 1px solid #e6b800 !important; }

/* ========================================================================== */
/* SECTION 5: MODAL & SIDEBAR PANELS                                          */
/* ========================================================================== */
#sidebar { width: 320px; background: #fff; border-right: 1px solid #ccc; display: flex; flex-direction: column; overflow: hidden; transition: width 0.2s ease, opacity 0.2s ease; }
#sidebar.closed { width: 0; opacity: 0; }
#results { flex: 1; overflow-y: auto; background: #fff; color: #333; }
.resultItem { padding: 12px; border-bottom: 1px solid #ddd; cursor: pointer; color: #333; }
.resultItem:hover { background: #f5f5f5; }
.resultItem strong { display: block; margin-bottom: 5px; color: #003366; }

#ocrModal { position: absolute; top: 0; left: 0; right: 0; bottom: 0; background: rgba(0, 0, 0, 0.45); pointer-events: none; z-index: 9999; }
#ocrModal.hidden { display: none; }
#ocrModalContent { pointer-events: auto; position: absolute; background: #1e1e1e; box-sizing: border-box; display: flex; flex-direction: column; box-shadow: 0 8px 24px rgba(0,0,0,0.6); }
#modalToolbar { background: #2a2a2a; color: white; height: 44px; display: flex; align-items: center; padding: 0 12px; gap: 8px; box-sizing: border-box; border-bottom: 1px solid #3a3a3a; }
#closeOcrModal { margin-left: auto; background: #c62828 !important; border: 1px solid #b71c1c !important; color: white !important; font-weight: bold; }
#modalBookSpread { flex: 1; display: flex; width: 100%; overflow: hidden; background: #222; gap: 4px; }
.modalPagePanel { flex: 1; background: white; display: flex; flex-direction: column; overflow: hidden; }
.modalPanelHeader { background: #f5f5f5; color: #555; font-size: 11px; text-transform: uppercase; font-weight: bold; letter-spacing: 1px; padding: 6px 16px; border-bottom: 1px solid #e0e0e0; }
.modalTextBody { flex: 1; padding: 24px; overflow-y: auto; font-size: 20px; line-height: 1.7; color: #222; white-space: pre-wrap; text-align: justify; }
.stf__parent, .stf__wrapper { margin: 0 auto !important; }

/* ========================================================================== */
/* SECTION 6: ATOM CONNECTION                                                 */
/* ========================================================================== */
.atom-status {
    font-size: 11px;
    padding: 6px 12px;
    border-radius: 12px;
    font-weight: bold;
    text-transform: uppercase;
    letter-spacing: 0.5px;
    margin-right: 15px;
    display: inline-flex;
    align-items: center;
    transition: all 0.3s ease;
}
.status-pending { background: #444; color: #fff; border: 1px solid #555; }
.status-connected { background: #1b5e20; color: #a5d6a7; border: 1px solid #4caf50; }
.status-failed { background: #b71c1c; color: #ffcdd2; border: 1px solid #f44336; }

.page canvas {
    width: 100% !important;
    height: 100% !important;
    object-fit: contain !important;
    display: block !important;
    user-select: none;
}

/* ========================================================================== */
/* SECTION 7: DYNAMIC RESPONSIVE REPAIR OVERRIDES                             */
/* ========================================================================== */

/* Clean Default View: Completely hides scrollbars at normal 100% zoom */
#bookContainer {
    overflow: hidden !important;
    -webkit-overflow-scrolling: touch !important; /* Enables smooth inertial momentum scrolling on iOS devices */
}

/* Zoomed-In View: Dynamically reveals scrollbars only when magnification is active */
#mf-view-viewer.is-zoomed #bookContainer {
    overflow: auto !important;
}

/* Chunky High-Visibility Touchscreen Scrollbars (Easily grabbable with fingers) */
#bookContainer::-webkit-scrollbar {
    width: 26px !important; /* Thick vertical scrollbar for touch dragging */
    height: 26px !important; /* Thick horizontal scrollbar for touch dragging */
}
#bookContainer::-webkit-scrollbar-track {
    background: #111111 !important;
}
#bookContainer::-webkit-scrollbar-thumb {
    background: #555555 !important;
    border: 5px solid #111111 !important; /* Internal track boundary padding padding */
    border-radius: 14px !important;
}
#bookContainer::-webkit-scrollbar-thumb:hover,
#bookContainer::-webkit-scrollbar-thumb:active {
    background: #007bff !important; /* Highlights blue when actively grabbed or touched */
}

/* Override touch behaviors when zoomed in to prioritize browser panning over library drag kills */
#mf-view-viewer.is-zoomed #bookContainer,
#mf-view-viewer.is-zoomed #flipbook,
#mf-view-viewer.is-zoomed .page {
    touch-action: pan-x pan-y !important; /* Restores native finger-swipe scrolling across zoomed spaces */
    -webkit-user-drag: none;
}

/* Tablet & Mobile Layout Adaptations (768px down) */
@media screen and (max-width: 768px) {
    #modalBookSpread {
        flex-direction: column !important;
    }

    .modalPagePanel.hidden-page {
        display: none !important;
    }

    .modalPagePanel {
        width: 100% !important;
        flex: 1 1 auto !important;
    }

    .modalPanelHeader {
        display: none !important;
    }

    .modalTextBody {
        font-size: 16px !important;
        line-height: 1.6 !important;
        padding: 16px !important;
    }

    #modalToolbar {
        height: auto !important;
        min-height: 44px;
        flex-wrap: wrap;
        padding: 6px 10px !important;
        gap: 6px !important;
        justify-content: flex-start;
    }

    #modalToolbar button {
        padding: 0 6px !important;
        font-size: 11px !important;
        height: 26px !important;
    }

    #modalPageLabel { font-size: 12px !important; }
    #modalZoomLabel { font-size: 12px !important; min-width: 35px !important; }

    #closeOcrModal {
        margin-left: auto !important;
        background: #c62828 !important;
        color: #ffffff !important;
    }

    #modalFirstPage, 
    #modalLastPage {
        display: none !important;
    }

    #modalZoomOut {
        margin-left: 10px !important;
    }

    #bottomToolbar {
        height: auto !important;
        min-height: 52px;
        padding: 6px !important;
    }

    .pagination-row {
        display: flex !important;
        flex-wrap: wrap !important;
        gap: 6px !important;
        justify-content: center !important;
        width: 100%;
    }

    #bottomToolbar button {
        height: 30px !important;
        padding: 0 8px !important;
        font-size: 12px !important;
        display: inline-flex !important;
        align-items: center;
        justify-content: center;
    }

    #pageInput {
        width: 40px !important;
        height: 30px !important;
        font-size: 12px !important;
    }

    #pageLabel { font-size: 12px !important; }
    #firstPage, #lastPage { display: none !important; }
    #bottomZoomSlider { width: 75px !important; }

    .bottom-zoom-container {
        display: inline-flex !important;
        align-items: center;
        gap: 4px !important;
        padding: 0 2px !important;
    }

    #bottomZoomLabel { font-size: 12px !important; min-width: 35px !important; }
    #fullscreenToggleBtn { font-size: 12px !important; height: 30px !important; padding: 0 8px !important; font-weight: bold !important; display: inline-flex !important; }
    #toolbarCopyright { display: none !important; }
    
    /* Reposition mobile fullscreen floating interface row controls cleanly along the bottom */
    #mf-view-viewer:fullscreen #fsExitBtn { top: 12px !important; right: 12px !important; }
    #mf-view-viewer:fullscreen .fs-page-btn { top: auto !important; bottom: 12px !important; transform: none !important; width: 40px !important; height: 40px !important; font-size: 14px !important; }
    #mf-view-viewer:fullscreen #fsPrevPageBtn { left: 12px !important; }
    #mf-view-viewer:fullscreen #fsNextPageBtn { right: 12px !important; left: auto !important; }
    #mf-view-viewer:fullscreen #fsVerticalZoomContainer { top: auto !important; bottom: 12px !important; left: 50% !important; transform: translateX(-50%) !important; flex-direction: row !important; padding: 0 10px !important; height: 40px !important; gap: 6px !important; border-radius: 20px !important; width: auto !important; }
    #mf-view-viewer:fullscreen #fsZoomSlider { writing-mode: horizontal-tb !important; direction: ltr !important; width: 65px !important; height: 18px !important; }
    #mf-view-viewer:fullscreen #fsVerticalZoomLabel { width: auto !important; min-width: 30px; text-align: right; font-size: 10px !important; }
}

/* Micro-Display Framework Optimization (For extreme mobile screen scaling down to 250px) */
@media screen and (max-width: 420px) {
    /* Enforce total background isolation block even if theme containers warp beneath the viewport */
    body, html {
        background-color: #222222 !important;
    }

    #toolbar {
        padding: 4px 6px !important;
        gap: 4px !important;
    }
    
    #toolbar button, #mf-back-to-shelf {
        font-size: 10px !important;
        padding: 0 4px !important;
        height: 24px !important;
    }
    
    #searchInput {
        width: 70px !important; /* Scaled down tightly to prevent form fields from causing horizontal page blowouts */
        height: 24px !important;
        font-size: 11px !important;
        padding: 0 4px !important;
    }
    
    #dynamicTitle {
        font-size: 10px !important;
        max-width: 70px;
        overflow: hidden;
        text-overflow: ellipsis;
    }
    
    #atomConnectionStatus {
        display: none !important; /* Removes connection text block to preserve narrow horizontal header space */
    }

    #bottomToolbar {
        min-height: 44px !important;
        padding: 4px !important;
    }

    .pagination-row {
        gap: 4px 3px !important;
    }

    #bottomToolbar button {
        height: 24px !important;
        padding: 0 4px !important;
        font-size: 10px !important;
    }

    #pageInput {
        width: 30px !important;
        height: 24px !important;
        font-size: 10px !important;
    }

    #pageLabel {
        font-size: 10px !important;
    }

    #bottomZoomSlider {
        width: 40px !important; /* Ultra-compressed slider bar track for mini mobile phone frames */
    }

    .bottom-zoom-container {
        gap: 2px !important;
        padding: 0 !important;
    }

    #bottomZoomLabel {
        font-size: 10px !important;
        min-width: 25px !important;
    }

    #fullscreenToggleBtn {
        height: 24px !important;
        font-size: 10px !important;
        padding: 0 4px !important;
    }
}

/* ========================================================================== */
/* SECTION 8: FLOATING FULLSCREEN OVERRIDES & CONTROLS                        */
/* ========================================================================== */
#fsExitBtn {
    display: none !important; 
    position: fixed;
    top: 20px;
    right: 20px;
    z-index: 9999999;
    background: #ffffff !important;
    color: #000000 !important;
    border: 1px solid #ffffff !important;
    border-radius: 2px;
    width: 35px;
    height: 35px;
    font-size: 20px;
    font-weight: bold;
    cursor: pointer;
    box-shadow: 0 4px 12px rgba(0,0,0,0.5);
    transition: background 0.2s;
}
#fsExitBtn:hover { background: #e5e5e5 !important; }

.fs-page-btn {
    display: none !important; 
    position: fixed;
    top: 50%;
    transform: translateY(-50%);
    z-index: 9999999;
    background: #ffffff !important;
    color: #000000 !important;
    border: 1px solid #ffffff !important;
    border-radius: 2px;
    width: 40px;
    height: 40px;
    font-size: 14px;
    font-weight: bold;
    cursor: pointer;
    box-shadow: 0 4px 12px rgba(0,0,0,0.5);
    transition: background 0.2s;
}
.fs-page-btn:hover { background: #e5e5e5 !important; }
#fsPrevPageBtn { left: 25px; }
#fsNextPageBtn { right: 100px; } 

#fsVerticalZoomContainer {
    display: none !important; 
    position: fixed;
    right: 25px;
    top: 50%;
    transform: translateY(-50%);
    z-index: 9999999;
    background: rgba(30, 30, 30, 0.85);
    padding: 20px 10px;
    border-radius: 25px;
    border: 1px solid #444;
    flex-direction: column;
    align-items: center;
    gap: 12px;
    box-shadow: 0 4px 15px rgba(0,0,0,0.6);
}

#fsVerticalZoomContainer span {
    font-size: 18px;
    font-weight: bold;
    color: #fff;
    cursor: pointer;
}

#fsZoomSlider {
    writing-mode: vertical-lr; 
    direction: rtl;            
    width: 20px;
    height: 150px;
    cursor: pointer;
    accent-color: #007bff;
}

#fsVerticalZoomLabel {
    font-size: 11px;
    color: #ccc;
    font-weight: bold;
    width: 35px;
    text-align: center;
}

#mf-view-viewer:fullscreen #toolbar,
#mf-view-viewer:fullscreen #bottomToolbar,
#mf-view-viewer:fullscreen #sidebar {
    display: none !important;
}

#mf-view-viewer:fullscreen #mainLayout {
    height: 100vh !important;
    width: 100vw !important;
}

/* Fix native background leakage and transformation errors across mobile screen layers */
#mf-view-viewer:fullscreen,
#mf-view-viewer::backdrop,
#mf-view-viewer:-webkit-full-screen,
#mf-view-viewer:-moz-full-screen,
#mf-view-viewer:-ms-fullscreen {
    background: #222222 !important;
    width: 100vw !important;
    height: 100vh !important;
}

#mf-view-viewer:fullscreen #fsExitBtn,
#mf-view-viewer:fullscreen .fs-page-btn,
#mf-view-viewer:fullscreen #fsVerticalZoomContainer {
    display: inline-flex !important; 
    align-items: center;
    justify-content: center;
}

/* ========================================================================== */
/* SECTION 9: BOTTOM TOOLBAR HORIZONTAL ZOOM BAR STYLE                       */
/* ========================================================================== */
.bottom-zoom-container {
    display: inline-flex;
    align-items: center;
    gap: 8px;
    padding: 0 10px;
    user-select: none;
}

.bottom-zoom-container .zoom-icon-btn {
    font-size: 16px;
    font-weight: bold;
    color: #ffffff;
    cursor: pointer;
    padding: 0 4px;
    transition: color 0.15s ease;
}

.bottom-zoom-container .zoom-icon-btn:hover {
    color: #007bff;
}

#bottomZoomSlider {
    width: 110px;
    height: 5px;
    cursor: pointer;
    accent-color: #007bff;
    vertical-align: middle;
}

#bottomZoomLabel {
    font-size: 13px;
    font-weight: bold;
    color: #ffffff;
    min-width: 42px;
    text-align: right;
    display: inline-block;
    font-family: Arial, sans-serif;
}

/* ========================================================================== */
/* SECTION 10: TEXT TRANSCRIPT MODAL OVERRIDES & ISOLATION                   */
/* ========================================================================== */

/* Grey out and freeze interactions for the background bottom navigation */
.disabled-toolbar {
    opacity: 0.25 !important;
    pointer-events: none !important;
    cursor: not-allowed !important;
}

/* Formally block book floating zoom/navigation triggers when viewing text transcripts */
#mf-view-viewer.text-mode-active #fsVerticalZoomContainer,
#mf-view-viewer.text-mode-active #fsExitBtn,
#mf-view-viewer.text-mode-active .fs-page-btn {
    display: none !important;
}

/* Force transcript wrapper box to occupy full viewport scale limits when fullscreened */
#ocrModal:fullscreen #ocrModalContent {
    left: 0 !important;
    top: 0 !important;
    width: 100vw !important;
    height: 100vh !important;
}

/* Style consistency adjustments for inner modal menu buttons */
#modalToolbar button {
    background: #ffffff !important;
    color: #000000 !important;
    border: 1px solid #ffffff !important;
    font-weight: bold;
    height: 28px;
    padding: 0 10px;
    cursor: pointer;
    border-radius: 2px;
}
#modalToolbar button:hover {
    background: #e5e5e5 !important;
}