/* Buddha PDF — front-end styles */

/* --- Design Tokens --- */
.bPdf {
    --accent: #cfa760;
    --accent-ink: #2b2418; /* text on a solid accent fill; PHP picks dark or white per accent */
    --ink: #2b2418;
    /* Surfaces, lines and scrollbars are neutral greys, never pale tints of
       the accent. The accent is only used at full strength. */
    --paper: #efefef;
    --line: #dadada;
    --hover: #f0f0f0;
    --muted: #8a8a8a;
    --scroll: #bdbdbd;
    --scroll-hover: #8f8f8f;

    --ease-expo: cubic-bezier(0.19, 1, 0.22, 1);
    /* Page turns use a balanced curve: --ease-expo finishes ~90% of the
       rotation in the first ~100ms, which makes the flip look like an
       instant swap. This one keeps the turn visible for its whole duration. */
    --ease-page: cubic-bezier(0.45, 0.05, 0.3, 1);
    --shadow-float: 0 20px 50px -10px rgba(43, 36, 24, 0.15);

    /* Single source of truth for animation timing (JS reads this) */
    --anim-duration: 0.7s;

    position: relative;
    width: 100%;
    height: 85vh;
    min-height: 500px;
    background: var(--paper);
    border-radius: 16px;
    overflow: hidden;
    font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif;
    color: var(--ink);
    display: flex;
    flex-direction: column;
    box-shadow: inset 0 0 0 1px rgba(0, 0, 0, 0.03);
    user-select: none;
}

/* Sizes include padding and border everywhere inside the viewer,
   whatever the theme sets. */
.bPdf *,
.bPdf *::before,
.bPdf *::after {
    box-sizing: border-box;
}

/* --- Floating Toolbar --- */
.bPdf-island {
    position: absolute;
    bottom: 30px;
    left: 10px;
    right: 10px;
    width: max-content;
    max-width: calc(100% - 20px);
    margin: 0 auto;
    z-index: 50;

    display: flex;
    align-items: center;
    gap: 6px;
    padding: 8px 10px;

    background: #fff;
    border: 1px solid var(--line);
    border-radius: 14px;
    box-shadow: 0 15px 35px -5px rgba(43, 36, 24, 0.1);
}

.bPdf:not(.is-sized) .bPdf-island {
    visibility: hidden;
}

.bPdf-group {
    display: flex;
    align-items: center;
    gap: 4px;
}

.bPdf-divider {
    flex: none;
    width: 1px;
    height: 18px;
    background: var(--line);
    margin: 0 6px;
}

/* Buttons */
.bPdf-btn {
    appearance: none;
    border: none;
    background: transparent;
    width: 38px;
    height: 38px;
    border-radius: 10px;
    color: var(--ink);
    cursor: pointer;
    flex: none;
    display: grid;
    place-items: center;
    /* Colors only: sizes must apply instantly so the toolbar fit check
       never measures a button mid-animation. */
    transition: background-color 0.2s ease, color 0.2s ease, box-shadow 0.2s ease;
    /* Touch: no double-tap zoom delay, no grey tap flash */
    touch-action: manipulation;
    -webkit-tap-highlight-color: transparent;
}

/* Hover effects only on devices that actually hover
   (prevents "sticky" highlighted buttons after a tap) */
@media (hover: hover) {
    .bPdf-btn:hover:not(:disabled) {
        background: var(--accent);
        color: var(--accent-ink);
        box-shadow: 0 4px 12px rgba(43, 36, 24, 0.18);
    }
}

.bPdf-btn:disabled {
    opacity: 0.3;
    cursor: not-allowed;
}

/* Focus ring for keyboard users only. Block themes outline every :focus,
   which rings the viewer and its buttons after a plain mouse click. */
.bPdf:focus,
.bPdf-btn:focus,
.bPdf-side-item:focus {
    outline: none;
}

.bPdf:focus-visible,
.bPdf-btn:focus-visible {
    outline: 2px solid var(--accent);
    outline-offset: 2px;
}

/* Pager */
.bPdf-pager {
    flex: none;
    font-family: 'Courier New', Courier, monospace;
    font-weight: 700;
    font-size: 14px;
    padding: 0 8px;
    display: flex;
    gap: 4px;
    color: var(--ink);
}

/* Fixed width (set from the page count) so the toolbar does not jump at 9 -> 10 */
.bPdf-pageVal {
    text-align: right;
}

.bPdf-sep {
    opacity: 0.4;
}

/* --- Page menu: 3-dot button + sidebar --- */
.bPdf-menuBtn {
    position: absolute;
    top: 12px;
    left: 12px;
    z-index: 60;
    background: #fff;
    border: 1px solid var(--line);
    box-shadow: 0 8px 20px -6px rgba(43, 36, 24, 0.12);
}

.bPdf-menuBtn:disabled {
    visibility: hidden;
}

.bPdf-side {
    position: absolute;
    top: 0;
    bottom: 0;
    left: 0;
    z-index: 70;
    width: 260px;
    max-width: 85%;
    display: flex;
    flex-direction: column;
    background: #fff;
    border-right: 1px solid var(--line);
    box-shadow: 12px 0 40px -12px rgba(43, 36, 24, 0.18);
    transform: translateX(calc(-100% - 48px));
    visibility: hidden;
    transition: transform 0.35s var(--ease-expo), visibility 0s linear 0.35s;
}

.bPdf.is-side-open .bPdf-side {
    transform: none;
    visibility: visible;
    transition: transform 0.35s var(--ease-expo);
}

.bPdf-side-head {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 8px;
    padding: 10px 10px 10px 18px;
    border-bottom: 1px solid var(--line);
}

.bPdf-side-heading {
    font-size: 12px;
    font-weight: 700;
    letter-spacing: 0.08em;
    text-transform: uppercase;
    color: var(--ink);
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}

.bPdf-side-list {
    position: relative; /* offsetTop of items is measured against the list */
    flex: 1;
    min-height: 0;
    overflow-y: auto;
    overscroll-behavior: contain;
    list-style: none;
    margin: 0;
    padding: 8px;
}

.bPdf-side-list > li {
    margin: 0;
    padding: 0;
}

.bPdf-side-list::-webkit-scrollbar {
    width: 6px;
}

.bPdf-side-list::-webkit-scrollbar-thumb {
    background: var(--scroll);
    border-radius: 4px;
}

.bPdf-side-list::-webkit-scrollbar-thumb:hover {
    background: var(--scroll-hover);
}

/* Full reset: themes often style every <button> */
.bPdf-side-item {
    appearance: none;
    display: flex;
    align-items: center;
    gap: 10px;
    width: 100%;
    min-height: 0;
    margin: 0;
    padding: 9px 10px;
    border: 0;
    border-radius: 10px;
    background: transparent;
    box-shadow: none;
    color: var(--ink);
    font: inherit;
    font-size: 14px;
    line-height: 1.3;
    letter-spacing: normal;
    text-align: left;
    text-transform: none;
    cursor: pointer;
    transition: background-color 0.15s ease, color 0.15s ease;
    touch-action: manipulation;
    -webkit-tap-highlight-color: transparent;
}

.bPdf-side-item:focus-visible {
    outline: 2px solid var(--accent);
    outline-offset: 2px;
}

.bPdf-side-label {
    display: flex;
    align-items: baseline;
    gap: 10px;
    min-width: 0;
}

.bPdf-side-num {
    flex: none;
    min-width: 2ch;
    font-family: 'Courier New', Courier, monospace;
    font-size: 12px;
    font-weight: 700;
    text-align: right;
    color: var(--muted);
}

.bPdf-side-title {
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}

/* Titles-only mode: solid hover, solid accent row for the current page */
@media (hover: hover) {
    .bPdf-side--list .bPdf-side-item:hover {
        background: var(--hover);
    }
}

.bPdf-side--list .bPdf-side-item[aria-current="page"] {
    background: var(--accent);
    color: var(--accent-ink);
    font-weight: 600;
}

.bPdf-side--list .bPdf-side-item[aria-current="page"] .bPdf-side-num {
    color: var(--accent-ink);
}

/* Thumbnail mode: page preview with the label underneath */
.bPdf-side--thumbs .bPdf-side-item {
    flex-direction: column;
    gap: 8px;
    padding: 12px 10px 10px;
}

.bPdf-side--thumbs .bPdf-side-label {
    justify-content: center;
    max-width: 100%;
    gap: 6px;
    padding: 2px 10px;
    border-radius: 999px;
}

.bPdf-side--thumbs .bPdf-side-num {
    min-width: 0;
}

.bPdf-side-thumb {
    display: block;
    width: 140px;
    max-width: 100%;
    height: auto;
    background: var(--hover); /* solid placeholder until the page is drawn */
    box-shadow: 0 4px 14px -4px rgba(43, 36, 24, 0.25);
    outline: 2px solid transparent;
    outline-offset: 3px;
    transition: outline-color 0.15s ease;
}

@media (hover: hover) {
    .bPdf-side--thumbs .bPdf-side-item:hover .bPdf-side-thumb {
        outline-color: var(--line);
    }
}

.bPdf-side--thumbs .bPdf-side-item[aria-current="page"] .bPdf-side-thumb {
    outline-color: var(--accent);
}

/* Current page label: solid accent pill */
.bPdf-side--thumbs .bPdf-side-item[aria-current="page"] .bPdf-side-label {
    background: var(--accent);
    color: var(--accent-ink);
    font-weight: 600;
}

.bPdf-side--thumbs .bPdf-side-item[aria-current="page"] .bPdf-side-num {
    color: var(--accent-ink);
}

/* --- Stage --- */
.bPdf-viewport {
    flex: 1;
    position: relative;
    overflow: auto;
    /* flex + margin:auto on the child centers the page when it fits, but
       degrades to normal top-aligned scrolling when zoomed content is
       bigger than the viewport. (grid + place-items:center makes the top
       overflow unreachable — it can't be scrolled to.) */
    display: flex;
    padding: 40px;
    perspective: 1400px;
    z-index: 1;
    touch-action: manipulation;
}

/* Scrollbar Customization */
.bPdf-viewport::-webkit-scrollbar {
    width: 6px;
    height: 6px;
}

.bPdf-viewport::-webkit-scrollbar-thumb {
    background: var(--scroll);
    border-radius: 4px;
}

.bPdf-viewport::-webkit-scrollbar-thumb:hover {
    background: var(--scroll-hover);
}

.bPdf-track {
    position: relative;
    margin: auto; /* safe centering — see .bPdf-viewport */
    transform-style: preserve-3d;
    transition: transform 0.2s ease-out;
    will-change: transform;
}

.bPdf-card {
    position: relative;
    background: #fff;
    box-shadow: var(--shadow-float);
    backface-visibility: hidden;
    transform-style: preserve-3d;
    outline: 1px solid rgba(0, 0, 0, 0.02);
}

.bPdf-layer-buffer {
    position: absolute;
    inset: 0;
    pointer-events: none;
    z-index: -1;
    opacity: 0;
    transform: rotateY(0);
}

.bPdf-canvas {
    display: block;
    width: 100%;
    height: auto;
}

/* Dynamic shadow overlay (both layers) — activated during page turns */
.bPdf-card::after {
    content: "";
    position: absolute;
    inset: 0;
    opacity: 0;
    pointer-events: none;
    mix-blend-mode: multiply;
}

/* --- Book Flip Animation ---
   "Next": the current page physically turns away around the spine (left
   edge) and reveals the new page waiting beneath it.
   "Prev": the exact mirror — the previous page turns back in over the
   current one. A gradient shade on the turning page and a cast shadow
   near the spine on the page beneath sell the depth. */

/* Next: current page (main) lifts and turns away — on top */
.anim-next .bPdf-layer-main {
    z-index: 10;
    transform-origin: left center;
    animation: bPdfPageOut var(--anim-duration) var(--ease-page) forwards;
}

/* Next: new page (buffer) revealed beneath, settling into place */
.anim-next .bPdf-layer-buffer {
    opacity: 1;
    z-index: 5;
    animation: bPdfReveal var(--anim-duration) var(--ease-page) forwards;
}

/* Prev: previous page (buffer) turns back in — on top */
.anim-prev .bPdf-layer-buffer {
    opacity: 1;
    z-index: 10;
    transform-origin: left center;
    animation: bPdfPageIn var(--anim-duration) var(--ease-page) forwards;
}

/* Prev: current page (main) recedes beneath */
.anim-prev .bPdf-layer-main {
    z-index: 5;
    animation: bPdfRecede var(--anim-duration) var(--ease-page) forwards;
}

/* Shade on the TURNING page: darkens toward the free (right) edge */
.anim-next .bPdf-layer-main::after,
.anim-prev .bPdf-layer-buffer::after {
    background: linear-gradient(to right, rgba(0, 0, 0, 0.03), rgba(0, 0, 0, 0.28));
    animation: bPdfShadeIn var(--anim-duration) var(--ease-page) forwards;
}

/* Cast shadow on the page BENEATH: strongest near the spine (left) */
.anim-next .bPdf-layer-buffer::after,
.anim-prev .bPdf-layer-main::after {
    background: linear-gradient(to right, rgba(0, 0, 0, 0.25), transparent 45%);
    opacity: 1;
    animation: bPdfShadeOut var(--anim-duration) var(--ease-page) forwards;
}

@keyframes bPdfPageOut {
    0% {
        transform: rotateY(0);
        filter: brightness(1);
        opacity: 1;
    }

    90% {
        opacity: 1;
    }

    100% {
        transform: rotateY(-88deg);
        filter: brightness(0.88);
        opacity: 0;
    }
}

@keyframes bPdfPageIn {
    0% {
        transform: rotateY(-88deg);
        filter: brightness(0.88);
        opacity: 0;
    }

    10% {
        opacity: 1;
    }

    100% {
        transform: rotateY(0);
        filter: brightness(1);
        opacity: 1;
    }
}

@keyframes bPdfReveal {
    from {
        transform: scale(0.985);
        filter: brightness(0.94);
    }

    to {
        transform: scale(1);
        filter: brightness(1);
    }
}

@keyframes bPdfRecede {
    from {
        transform: scale(1);
        filter: brightness(1);
    }

    to {
        transform: scale(0.985);
        filter: brightness(0.94);
    }
}

@keyframes bPdfShadeIn {
    from {
        opacity: 0;
    }

    to {
        opacity: 1;
    }
}

@keyframes bPdfShadeOut {
    from {
        opacity: 1;
    }

    to {
        opacity: 0;
    }
}

@keyframes bPdfFadeIn {
    from {
        opacity: 0;
    }

    to {
        opacity: 1;
    }
}

/* Accessibility: users who prefer reduced motion get a quick crossfade
   instead of the 3D flip. JS reads --anim-duration, so timing stays in sync. */
@media (prefers-reduced-motion: reduce) {
    .bPdf {
        /* !important: the settings page injects its own --anim-duration
           later in the cascade — accessibility must win over it */
        --anim-duration: 0.25s !important;
    }

    .anim-next .bPdf-layer-main,
    .anim-prev .bPdf-layer-main {
        animation: none;
        transform: none;
        filter: none;
    }

    .anim-next .bPdf-layer-buffer,
    .anim-prev .bPdf-layer-buffer {
        opacity: 1;
        z-index: 10;
        transform: none;
        animation: bPdfFadeIn var(--anim-duration) ease forwards;
    }

    .anim-next .bPdf-card::after,
    .anim-prev .bPdf-card::after {
        animation: none;
        opacity: 0;
    }

    .bPdf-side,
    .bPdf.is-side-open .bPdf-side {
        transition: none;
    }
}

/* --- Utility UI --- */
.bPdf-loader {
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
    width: 48px;
    height: 48px;
    color: var(--accent);
    opacity: 0;
    pointer-events: none;
    transition: opacity 0.2s;
    z-index: 100;
}

.bPdf-loader.active {
    opacity: 1;
}

.bPdf-loader svg {
    width: 100%;
    height: 100%;
}

.bPdf-loader circle {
    fill: none;
    stroke: currentColor;
    stroke-width: 2;
    stroke-dasharray: 60;
    stroke-dashoffset: 0;
    transform-origin: center;
    animation: bPdfSpin 1s linear infinite;
}

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

.bPdf-toast {
    position: absolute;
    top: 20px;
    left: 50%;
    transform: translateX(-50%) translateY(-20px);
    background: var(--ink);
    color: #fff;
    padding: 8px 16px;
    border-radius: 20px;
    font-size: 13px;
    font-weight: 500;
    opacity: 0;
    transition: all 0.3s var(--ease-expo);
    pointer-events: none;
    z-index: 101;
}

.bPdf-toast.show {
    opacity: 1;
    transform: translateX(-50%) translateY(0);
}

/* Fullscreen Overrides */
.bPdf:fullscreen {
    border-radius: 0;
    background: var(--paper);
}

.bPdf:fullscreen .bPdf-island {
    bottom: 40px;
}

/* --- Phones and narrow columns ---
   Driven by the viewer's own width (the script sets the classes), not the
   screen: the same phone shows a wide or a narrow viewer depending on the
   theme's margins and columns. */
.bPdf.is-compact {
    min-height: 420px;
    border-radius: 12px;
}

/* Reclaim space: 40px padding wastes a quarter of a phone's width */
.bPdf.is-compact .bPdf-viewport {
    padding: 16px 10px;
}

.bPdf.is-compact .bPdf-island {
    bottom: 12px;
    padding: 4px 6px;
    gap: 2px;
}

.bPdf.is-compact .bPdf-group {
    gap: 0;
}

.bPdf.is-compact .bPdf-divider {
    margin: 0 2px;
}

.bPdf.is-compact .bPdf-pager {
    font-size: 13px;
    padding: 0 6px;
}

/* 44px: comfortable touch targets */
.bPdf.is-compact .bPdf-btn {
    width: 44px;
    height: 44px;
}

.bPdf.is-compact .bPdf-side--list .bPdf-side-item {
    min-height: 44px;
}

/* Only when the full toolbar does not fit: drop the zoom buttons
   (pinch-zoom still works). */
.bPdf.is-narrow .bPdf-group--zoom,
.bPdf.is-narrow .bPdf-divider--zoom {
    display: none;
}

/* Still too narrow (e.g. folded-phone cover screens): the counter shows
   only the current page; the page list still has the full count. */
.bPdf.is-tiny .bPdf-sep,
.bPdf.is-tiny .bPdf-total {
    display: none;
}
