/* uPDF Viewer — front-end styles */

/* --- Design Tokens --- */
.uPdf {
    --accent: #cfa760;
    --accent-dim: #e6d3a3;
    --ink: #2b2418;
    --paper: #f9f7f1;
    --glass: rgba(255, 255, 255, 0.9);
    /* Derived from --accent so the settings page color applies everywhere */
    --border: color-mix(in srgb, var(--accent) 30%, transparent);
    --glow: color-mix(in srgb, var(--accent) 40%, transparent);

    --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;
}

/* Background Texture */
.uPdf::before {
    content: "";
    position: absolute;
    inset: 0;
    background-image: radial-gradient(var(--accent-dim) 1px, transparent 0);
    background-size: 32px 32px;
    opacity: 0.3;
    pointer-events: none;
}

/* --- Floating Toolbar --- */
.uPdf-island {
    position: absolute;
    bottom: 30px;
    left: 50%;
    transform: translateX(-50%) translateY(0);
    z-index: 50;

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

    background: var(--glass);
    backdrop-filter: blur(16px);
    -webkit-backdrop-filter: blur(16px);
    border: 1px solid var(--border);
    border-radius: 14px;
    box-shadow: 0 15px 35px -5px rgba(43, 36, 24, 0.1);
    transition: transform 0.4s var(--ease-expo);
}

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

.uPdf-divider {
    width: 1px;
    height: 18px;
    background: var(--border);
    margin: 0 6px;
}

/* Buttons */
.uPdf-btn {
    appearance: none;
    border: none;
    background: transparent;
    width: 38px;
    height: 38px;
    border-radius: 10px;
    color: var(--ink);
    cursor: pointer;
    display: grid;
    place-items: center;
    transition: all 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) {
    .uPdf-btn:hover:not(:disabled) {
        background: var(--accent);
        color: #fff;
        box-shadow: 0 4px 12px var(--glow);
    }
}

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

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

.uPdf-sep {
    opacity: 0.4;
}

/* --- Stage --- */
.uPdf-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;
}

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

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

.uPdf-viewport::-webkit-scrollbar-thumb:hover {
    background: var(--accent);
}

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

.uPdf-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);
}

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

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

/* Dynamic shadow overlay (both layers) — activated during page turns */
.uPdf-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 .uPdf-layer-main {
    z-index: 10;
    transform-origin: left center;
    animation: uPdfPageOut var(--anim-duration) var(--ease-page) forwards;
}

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

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

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

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

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

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

    90% {
        opacity: 1;
    }

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

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

    10% {
        opacity: 1;
    }

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

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

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

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

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

@keyframes uPdfShadeIn {
    from {
        opacity: 0;
    }

    to {
        opacity: 1;
    }
}

@keyframes uPdfShadeOut {
    from {
        opacity: 1;
    }

    to {
        opacity: 0;
    }
}

@keyframes uPdfFadeIn {
    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) {
    .uPdf {
        /* !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 .uPdf-layer-main,
    .anim-prev .uPdf-layer-main {
        animation: none;
        transform: none;
        filter: none;
    }

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

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

/* --- Utility UI --- */
.uPdf-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;
}

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

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

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

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

.uPdf-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;
}

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

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

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

/* --- Mobile --- */
@media (max-width: 600px) {
    .uPdf {
        min-height: 420px;
        border-radius: 12px;
    }

    /* Reclaim screen space: 40px padding wastes ~25% of a phone's width */
    .uPdf-viewport {
        padding: 16px 10px;
    }

    .uPdf-island {
        bottom: 12px;
        padding: 6px 8px;
        gap: 4px;
        max-width: calc(100% - 20px);
    }

    .uPdf-divider {
        margin: 0 2px;
    }

    /* Slightly larger touch targets */
    .uPdf-btn {
        width: 42px;
        height: 42px;
    }

    /* Fullscreen doesn't exist on iPhones — hiding it there is handled
       by JS; on small Android screens keep it but tighten spacing */
}

/* Very small screens: drop the zoom group to keep the island on one line */
@media (max-width: 340px) {
    .uPdf-island {
        gap: 2px;
        padding: 5px 6px;
    }

    .uPdf-btn {
        width: 38px;
        height: 38px;
    }
}
