html,
body {
    margin: 0;
    width: 100%;
    height: 100%;
}

body {
    background: #333;
    font-family: Arial, sans-serif;
    color: white;
    overflow: hidden;
}

#flipbook {
    box-sizing: border-box;
    width: 100%;
    height: 100%;
    text-align: center;
    padding: 15px;
}

#title {
    height: 28px;
    line-height: 28px;
    font-size: 20px;
    margin-bottom: 10px;
}

#reader {
    position: relative;
    height: calc(100vh - 137px);
    display: flex;
    justify-content: center;
    align-items: center;
    overflow: hidden;
    touch-action: none;
}

#reader.spread {
    gap: 0;
}

.bookPage {
    display: block;
    max-height: 100%;
    object-fit: contain;
    background: white;
    box-shadow: 0 4px 18px rgba(0, 0, 0, 0.55);
    user-select: none;
    -webkit-user-select: none;
    -webkit-user-drag: none;
}

.spread .bookPage {
    max-width: 48%;
}

.singlePage .bookPage {
    max-width: 90%;
}

#controls {
    height: 45px;
    display: flex;
    justify-content: center;
    align-items: center;
    margin-top: 10px;
}

button {
    padding: 8px 16px;
    margin: 0 10px;
    cursor: pointer;
}

button:disabled {
    opacity: 0.4;
    cursor: default;
}

#pageInfo {
    display: inline-block;
    min-width: 165px;
    white-space: nowrap;
}

#pageInput {
    width: 48px;
    padding: 4px;
    box-sizing: border-box;
    text-align: center;
    font-size: 14px;
}

#tools {
    display: flex;
    margin-left: 5px;
}

.toolButton {
    min-width: 42px;
    padding: 7px 10px;
    margin: 0 3px;
}

#magnifierButton.active {
    background: #ddd;
}

/* Desktop magnifier */
#magnifier {
    position: fixed;
    display: none;
    width: 720px;
    height: 720px;
    border: 3px solid white;
    border-radius: 50%;
    box-sizing: border-box;
    box-shadow: 0 4px 20px rgba(0, 0, 0, 0.65);
    background-repeat: no-repeat;
    pointer-events: none;
    z-index: 9999;
}

/* Mobile zoom */
.mobilePage .bookPage {
    transform-origin: center center;
}

/* Fullscreen */
#flipbook:fullscreen {
    background: #333;
}

#flipbook:-webkit-full-screen {
    background: #333;
}

/* Mobile */
@media (max-width: 700px) {
    #flipbook {
        padding: 6px;
    }

    #title {
        height: 24px;
        line-height: 24px;
        font-size: 16px;
        margin-bottom: 4px;
    }

    #reader {
        height: calc(100vh - 100px);
    }

    .bookPage {
        max-width: 100% !important;
    }

    #controls {
        height: 55px;
        margin-top: 3px;
    }

    button {
        padding: 7px 8px;
        margin: 0 3px;
        font-size: 12px;
    }

    #pageInfo {
        min-width: 118px;
        font-size: 12px;
    }

    #pageInput {
        width: 40px;
        font-size: 12px;
    }

    #tools {
        margin-left: 2px;
    }

    .toolButton {
        min-width: 34px;
        padding: 7px 6px;
    }

    #magnifierButton {
        display: none;
    }

    #magnifier {
        display: none !important;
    }
}

#readerHints {
    height: 20px;
    line-height: 20px;
    margin-top: 2px;
    font-size: 12px;
    opacity: 0.75;
    white-space: nowrap;
}

#readerHints kbd {
    display: inline-block;
    min-width: 18px;
    padding: 1px 5px;
    margin: 0 2px;
    border: 1px solid rgba(255, 255, 255, 0.45);
    border-radius: 4px;
    box-sizing: border-box;
    font-family: Arial, sans-serif;
    font-size: 11px;
    line-height: 16px;
    text-align: center;
    background: rgba(255, 255, 255, 0.10);
    color: white;
}

@media (max-width: 700px) {
    #readerHints {
        display: none;
    }
}

#touchHints {
    display: none;
}

@media (pointer: coarse) {
    #mouseHints {
        display: none;
    }

    #touchHints {
        display: inline;
    }
}

/*
 * v0.7.3 touch/tablet control improvements
 */

@media (pointer: coarse) {
    #controls {
        display: flex;
        flex-wrap: wrap;
        justify-content: center;
        align-items: center;
        height: auto;
        min-height: 45px;
        gap: 4px;
    }

    #pageInfo {
        display: inline-flex;
        align-items: center;
        justify-content: center;
        min-width: 150px;
    }

    #pageInput {
        display: inline-block;
        width: 54px;
        min-height: 34px;
        margin: 0 5px;
        font-size: 16px;
    }
}

@media (max-width: 700px) {
    #controls {
        display: flex;
        flex-wrap: wrap;
        height: auto;
        min-height: 52px;
        gap: 2px;
    }

    #pageInfo {
        display: inline-flex;
        min-width: 128px;
        align-items: center;
        justify-content: center;
    }

    #pageInput {
        display: inline-block;
        width: 48px;
        min-height: 32px;
        margin: 0 3px;
        font-size: 14px;
    }
}

/*
 * v0.7.4 - Touch UI
 * Keep navigation visible on phones/iPads and remove desktop-only UI.
 */

@media (pointer: coarse) {
    #reader {
        /* Reserve space for the fixed touch navigation bar. */
        height: calc(100dvh - 105px - env(safe-area-inset-bottom));
        padding-bottom: 0;
    }

    #controls {
        position: fixed;
        left: 0;
        right: 0;
        bottom: 0;
        z-index: 10000;

        box-sizing: border-box;
        width: 100%;
        min-height: 58px;
        padding:
            7px
            6px
            calc(7px + env(safe-area-inset-bottom))
            6px;

        margin: 0;
        display: flex;
        flex-wrap: nowrap;
        justify-content: center;
        align-items: center;
        gap: 3px;

        background: rgba(35, 35, 35, 0.96);
    }

    #controls button {
        margin: 0 3px;
    }

    #pageInfo {
        display: inline-flex;
        align-items: center;
        justify-content: center;
        min-width: 138px;
        white-space: nowrap;
    }

    #pageInput {
        display: inline-block;
        width: 52px;
        min-height: 34px;
        margin: 0 4px;
        font-size: 16px;
    }

    /* Desktop-only functions are unnecessary on touch devices. */
    #magnifierButton,
    #readerHints {
        display: none !important;
    }

    #tools {
        margin-left: 2px;
    }
}

/* Phones need slightly more compact navigation. */
@media (max-width: 700px) and (pointer: coarse) {
    #reader {
        height: calc(100dvh - 94px - env(safe-area-inset-bottom));
    }

    #controls {
        min-height: 54px;
        padding:
            6px
            4px
            calc(6px + env(safe-area-inset-bottom))
            4px;
    }

    #controls button {
        padding: 7px 6px;
        margin: 0 2px;
        font-size: 12px;
    }

    #pageInfo {
        min-width: 120px;
        font-size: 12px;
    }

    #pageInput {
        width: 45px;
        min-height: 32px;
        margin: 0 2px;
        font-size: 14px;
    }
}

/* ==========================================================
   v0.9.0 - mesh-style curved page prototype
   Canvas overlay only exists during the page turn.
   The destination page is rendered normally underneath it.
   ========================================================== */

.meshTurnCanvas {
    position: fixed;
    z-index: 20000;
    pointer-events: none;
    display: block;
    transform: translateZ(0);
    will-change: contents;
}

@media (prefers-reduced-motion: reduce) {
    .meshTurnCanvas {
        display: none;
    }
}


/* v0.10.7: invisible half-page used only to keep the fixed spread geometry
   for closed front/back states. */
.bookPage.blankPage {
    visibility: hidden;
    pointer-events: none;
    box-shadow: none;
    background: transparent;
}


/* v0.10.11 ---------------------------------------------------------------
   Lock the book spine to the exact centre of #reader.
   Previously the two IMG widths were flexed as a centered pair. Tiny width /
   aspect-ratio differences between scans changed the pair's total width and
   caused the whole opened book to shift after a turn.
   ---------------------------------------------------------------------- */
#reader.spread {
    position: relative;
    display: block;
}

#reader.spread .bookPage {
    position: absolute;
    top: 50%;
    transform: translateY(-50%);
    max-width: 48%;
    max-height: 100%;
}

#reader.spread .bookPage.leftPage {
    right: 50%;
    left: auto;
}

#reader.spread .bookPage.rightPage {
    left: 50%;
    right: auto;
}


/* v0.10.16 desktop Single / Double page control */
#viewModeButton.active {
    background: #ddd;
    color: #222;
}

@media (pointer: coarse) {
    #viewModeButton {
        display: none !important;
    }
}

/* v0.11.0 - per-book Table of Contents panel */
#tocButton {
    display: none;
}

#tocButton.active {
    background: #ddd;
    color: #222;
}

#tocPanel {
    position: fixed;
    top: 54px;
    right: 12px;
    width: 320px;
    max-width: calc(100vw - 24px);
    max-height: calc(100vh - 145px);
    box-sizing: border-box;
    display: none;
    flex-direction: column;
    text-align: left;
    background: rgba(28, 28, 28, 0.97);
    border: 1px solid rgba(255, 255, 255, 0.25);
    border-radius: 7px;
    box-shadow: 0 8px 28px rgba(0, 0, 0, 0.55);
    z-index: 15000;
    overflow: hidden;
}

#tocPanel.open {
    display: flex;
}

#tocHeader {
    display: flex;
    align-items: center;
    justify-content: space-between;
    flex: 0 0 auto;
    padding: 10px 10px 8px 14px;
    border-bottom: 1px solid rgba(255, 255, 255, 0.18);
    font-size: 18px;
    font-weight: bold;
}

#tocClose {
    min-width: 32px;
    margin: 0;
    padding: 2px 8px;
    border: 0;
    background: transparent;
    color: white;
    font-size: 24px;
    line-height: 28px;
}

#tocBody {
    overflow-y: auto;
    padding: 6px 8px 12px;
}

.tocSectionTitle {
    margin: 9px 6px 4px;
    padding-bottom: 4px;
    border-bottom: 1px solid rgba(255, 255, 255, 0.25);
    font-size: 15px;
    font-weight: bold;
}

.tocItem {
    width: 100%;
    display: flex;
    align-items: flex-start;
    justify-content: space-between;
    gap: 12px;
    margin: 0;
    padding: 7px 7px;
    border: 0;
    border-radius: 4px;
    background: transparent;
    color: white;
    text-align: left;
    font-size: 13px;
    line-height: 1.3;
}

.tocItem:hover {
    background: rgba(255, 255, 255, 0.12);
}

.tocArticle {
    flex: 1 1 auto;
}

.tocPage {
    flex: 0 0 auto;
    min-width: 24px;
    text-align: right;
    font-weight: bold;
    opacity: 0.9;
}

@media (max-width: 900px), (pointer: coarse) {
    #tocPanel {
        top: 45px;
        right: 8px !important;
        left: auto !important;
        width: min(340px, calc(100vw - 16px));
        max-height: calc(100dvh - 115px - env(safe-area-inset-bottom));
    }
}


/* v0.11.4 - responsive TOC overlay for phone / touch portrait */
@media (max-width: 700px) {
    #tocPanel {
        top: 10px;
        right: 8px !important;
        left: 8px !important;
        width: auto;
        max-width: none;
        max-height: calc(100dvh - 78px - env(safe-area-inset-bottom));
        z-index: 25000;
    }
}

@media (pointer: coarse) and (orientation: portrait) and (min-width: 701px) {
    #tocPanel {
        top: 24px;
        right: 12px !important;
        left: auto !important;
        width: min(420px, calc(100vw - 24px));
        max-height: calc(100dvh - 100px - env(safe-area-inset-bottom));
        z-index: 25000;
    }
}


/* v0.11.10 - brand styling + ChatGPT credit beside toolbar */
#title {
    font-weight: normal;
}

#title .brandTitle {
    font-weight: bold;
    color: #4da3ff;
}

#poweredBy {
    display: inline-block;
    margin-left: 10px;
    font-size: 11px;
    line-height: 1.2;
    font-style: italic;
    font-weight: normal;
    opacity: 0.85;
    white-space: nowrap;
    vertical-align: middle;
}

@media (max-width: 700px) {
    #poweredBy {
        margin-left: 4px;
        font-size: 8px;
    }
}

@media (pointer: coarse) {
    #poweredBy {
        margin-left: 5px;
        font-size: 9px;
    }
}

/* v0.11.15 - phone toolbar fit fix
   Keep the complete navigation row inside narrow mobile viewports.
   Desktop and iPad/tablet rules above remain unchanged. */
@media (max-width: 700px) and (pointer: coarse) {
    #controls {
        justify-content: center;
        gap: 1px;
        padding-left: 2px;
        padding-right: 2px;
    }

    #controls > button,
    #controls #tools > button {
        flex: 0 0 auto;
        min-width: 28px;
        padding-left: 4px;
        padding-right: 4px;
        margin-left: 1px;
        margin-right: 1px;
    }

    #pageInfo {
        flex: 0 0 auto;
        min-width: 0;
        font-size: 11px;
    }

    #pageInput {
        width: 40px;
        min-height: 30px;
        margin-left: 1px;
        margin-right: 1px;
        font-size: 13px;
    }

    #tools {
        flex: 0 0 auto;
        margin-left: 0;
        gap: 0;
    }

    #tools .toolButton {
        min-width: 28px;
    }

    #poweredBy {
        display: none;
    }
}

