/*!
 * Andes PDF Flipbook — lightweight CSS page-flip PDF/image viewer
 * Clean-room, dependency-free (pdf.js loaded only on demand for PDF mode).
 * License: MIT (your project). Bundled pdf.js is Apache-2.0 (see vendor/pdfjs/LICENSE).
 */

.afb {
    /* Theming hooks — override per instance with inline style or a wrapper class. */
    --afb-bg: #515558;
    --afb-toolbar-bg: #2b2f33;
    --afb-toolbar-fg: #f3f4f6;
    --afb-accent: #3b82f6;
    --afb-btn-hover: rgba(255, 255, 255, 0.12);
    --afb-page-bg: #fff;
    --afb-shadow: 0 10px 30px rgba(0, 0, 0, 0.35);
    --afb-radius: 6px;
    --afb-panel-bg: #1f2326;
    --afb-panel-fg: #e5e7eb;

    position: relative;
    display: flex;
    flex-direction: column;
    width: 100%;
    height: 100%;
    min-height: 320px;
    background: var(--afb-bg);
    color: var(--afb-toolbar-fg);
    font-family: system-ui, -apple-system, Segoe UI, Roboto, Helvetica, Arial, sans-serif;
    overflow: hidden;
    box-sizing: border-box;
}
.afb *,
.afb *::before,
.afb *::after {
    box-sizing: border-box;
}
.afb:fullscreen,
.afb.afb--fs {
    width: 100vw;
    height: 100vh;
}

/* ---------- Stage ---------- */
.afb__stage {
    position: relative;
    flex: 1 1 auto;
    overflow: hidden;
    display: flex;
    align-items: center;
    justify-content: center;
    touch-action: none;
    user-select: none;
}
.afb__zoom {
    position: relative;
    transform-origin: center center;
    transition: transform 0.15s ease-out;
    will-change: transform;
}
.afb__zoom.afb--panning {
    transition: none;
}

/* ---------- Book ---------- */
.afb__book {
    position: relative;
    display: flex;
    perspective: 2200px;
    /* No shadow here: it would wrap the whole book box, including an empty slot
       beside a lone cover/last page or a blank leaf mid-flip. The shadow lives on
       each real leaf instead, so empty areas never cast one. */
}
/* Persistent centre crease for a two-page spread. It is fixed to the book's spine
   and sits above the turning page (z 31 > flip's 30), so the gutter stays constant
   throughout the flip instead of disappearing and snapping back with each page. */
.afb__book--spread::after {
    content: '';
    position: absolute;
    top: 0;
    bottom: 0;
    left: 50%;
    width: 44px;
    transform: translateX(-50%);
    pointer-events: none;
    z-index: 31;
    background: linear-gradient(
        to right,
        rgba(0, 0, 0, 0) 0%,
        rgba(0, 0, 0, 0.06) 38%,
        rgba(0, 0, 0, 0.14) 50%,
        rgba(0, 0, 0, 0.06) 62%,
        rgba(0, 0, 0, 0) 100%
    );
}
.afb__leaf {
    position: relative;
    background: var(--afb-page-bg);
    overflow: hidden;
    backface-visibility: hidden;
}
/* Shadow is driven by an explicit position class set in JS (--left/--right/--single),
   so it is identical for a still spread and for the leaves sitting under a flip. In a
   two-page spread each page's shadow is weighted outward, keeping a faint gutter
   instead of two overlapping shadows at the spine. */
.afb__leaf--single {
    box-shadow: var(--afb-shadow);
}
.afb__leaf--left {
    box-shadow: -5px 8px 24px rgba(0, 0, 0, 0.22);
}
.afb__leaf--right {
    box-shadow: 5px 8px 24px rgba(0, 0, 0, 0.22);
}
.afb__leaf canvas,
.afb__leaf img {
    display: block;
    width: 100%;
    height: 100%;
    object-fit: contain;
    pointer-events: none;
}
.afb__leaf--blank {
    background: transparent;
    box-shadow: none;
}

/* The animated flipping leaf overlays one half of the spread. */
.afb__flip {
    position: absolute;
    top: 0;
    transform-style: preserve-3d;
    transform-origin: left center;
    z-index: 30;
    transition: transform var(--afb-flip-ms, 600ms) ease-in-out;
    pointer-events: none;
}
.afb__flip--rtl {
    transform-origin: right center;
}
.afb__flip__face {
    position: absolute;
    inset: 0;
    background: var(--afb-page-bg);
    overflow: hidden;
    backface-visibility: hidden;
}
.afb__flip__face canvas,
.afb__flip__face img {
    display: block;
    width: 100%;
    height: 100%;
    object-fit: contain;
}
.afb__flip__face--back {
    transform: rotateY(180deg);
}

/* Page text layer (pdf mode, optional) — selectable + highlightable. */
.afb__textlayer {
    position: absolute;
    inset: 0;
    overflow: hidden;
    opacity: 0.01; /* invisible but selectable/searchable */
    line-height: 1;
    z-index: 5;
}
.afb__textlayer span {
    position: absolute;
    white-space: pre;
    transform-origin: 0 0;
    color: transparent;
}
.afb__textlayer mark {
    background: var(--afb-accent);
    color: transparent;
    opacity: 0.45;
    border-radius: 1px;
}

/* Annotation layer (pdf mode) — clickable Link annotations. The layer itself lets
   clicks through (so empty areas still turn the page); only the links capture them. */
.afb__annotationlayer {
    position: absolute;
    inset: 0;
    z-index: 7; /* above the text layer, below the flipping leaf */
    pointer-events: none;
}
.afb__annotationlayer section {
    position: absolute;
    pointer-events: auto;
}
.afb__annotationlayer a {
    position: absolute;
    inset: 0;
    cursor: pointer;
}
.afb__annotationlayer .linkAnnotation > a:hover {
    background: var(--afb-accent);
    opacity: 0.12;
}

/* ---------- Toolbar ---------- */
.afb__toolbar {
    flex: 0 0 auto;
    display: flex;
    align-items: center;
    gap: 2px;
    padding: 4px 8px;
    background: var(--afb-toolbar-bg);
    z-index: 40;
    flex-wrap: wrap;
}
.afb__toolbar--bottom {
    justify-content: center;
}
.afb__spacer {
    flex: 1 1 auto;
}
.afb__btn {
    appearance: none;
    border: 0;
    background: none;
    color: var(--afb-toolbar-fg);
    width: 38px;
    height: 38px;
    border-radius: var(--afb-radius);
    cursor: pointer;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    padding: 0;
    line-height: 0;
}
.afb__btn:hover {
    background: var(--afb-btn-hover);
}
.afb__btn:focus-visible {
    outline: 2px solid var(--afb-accent);
    outline-offset: -2px;
}
.afb__btn[disabled] {
    opacity: 0.35;
    cursor: default;
}
.afb__btn svg {
    width: 20px;
    height: 20px;
    fill: none;
    stroke: currentColor;
    stroke-width: 2;
    stroke-linecap: round;
    stroke-linejoin: round;
}
.afb__counter {
    display: inline-flex;
    align-items: center;
    gap: 4px;
    font-size: 13px;
    padding: 0 8px;
    white-space: nowrap;
}
.afb__counter input {
    width: 46px;
    text-align: center;
    background: rgba(0, 0, 0, 0.25);
    border: 1px solid rgba(255, 255, 255, 0.15);
    color: inherit;
    border-radius: 4px;
    padding: 3px 4px;
    font: inherit;
}

/* ---------- Arrows ---------- */
.afb__arrow {
    position: absolute;
    top: 50%;
    transform: translateY(-50%);
    width: 44px;
    height: 64px;
    border: 0;
    border-radius: var(--afb-radius);
    background: rgba(0, 0, 0, 0.25);
    color: #fff;
    cursor: pointer;
    z-index: 35;
    display: flex;
    align-items: center;
    justify-content: center;
}
.afb__arrow:hover {
    background: rgba(0, 0, 0, 0.45);
}
.afb__arrow[disabled] {
    opacity: 0;
    pointer-events: none;
}
.afb__arrow--prev {
    left: 8px;
}
.afb__arrow--next {
    right: 8px;
}
.afb__arrow svg {
    width: 22px;
    height: 22px;
    fill: none;
    stroke: currentColor;
    stroke-width: 2.5;
    stroke-linecap: round;
    stroke-linejoin: round;
}

/* ---------- Side panels (thumbnails / TOC / search) ---------- */
.afb__panel {
    position: absolute;
    top: 0;
    bottom: 0;
    left: 0;
    width: min(320px, 85%);
    background: var(--afb-panel-bg);
    color: var(--afb-panel-fg);
    transform: translateX(-100%);
    transition: transform 0.25s ease;
    z-index: 50;
    display: flex;
    flex-direction: column;
}
.afb__panel.afb--open {
    transform: translateX(0);
    /* Shadow only while open — otherwise the off-screen panel's blur bleeds
       across the viewer's left edge. */
    box-shadow: var(--afb-shadow);
}
.afb__panel__head {
    flex: 0 0 auto;
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 10px 12px;
    font-size: 14px;
    font-weight: 600;
    border-bottom: 1px solid rgba(255, 255, 255, 0.08);
}
.afb__panel__body {
    flex: 1 1 auto;
    overflow-y: auto;
    padding: 10px;
}
.afb__thumbs {
    display: grid;
    grid-template-columns: repeat(2, 1fr);
    gap: 10px;
}
.afb__thumb {
    background: #000;
    border: 2px solid transparent;
    border-radius: 4px;
    cursor: pointer;
    overflow: hidden;
    padding: 0;
}
.afb__thumb.afb--current {
    border-color: var(--afb-accent);
}
.afb__thumb img,
.afb__thumb canvas {
    display: block;
    width: 100%;
    height: auto;
}
.afb__thumb__no {
    display: block;
    text-align: center;
    font-size: 11px;
    padding: 2px;
    color: var(--afb-panel-fg);
}
.afb__toc-item {
    display: block;
    width: 100%;
    text-align: left;
    background: none;
    border: 0;
    color: inherit;
    padding: 7px 8px;
    border-radius: 4px;
    cursor: pointer;
    font-size: 13px;
}
.afb__toc-item:hover {
    background: rgba(255, 255, 255, 0.08);
}
.afb__search-box {
    display: flex;
    gap: 6px;
    margin-bottom: 10px;
}
.afb__search-box input {
    flex: 1;
    background: rgba(0, 0, 0, 0.3);
    border: 1px solid rgba(255, 255, 255, 0.15);
    color: inherit;
    border-radius: 4px;
    padding: 7px 9px;
    font: inherit;
}
.afb__result {
    display: block;
    width: 100%;
    text-align: left;
    background: none;
    border: 0;
    border-bottom: 1px solid rgba(255, 255, 255, 0.06);
    color: inherit;
    padding: 8px;
    cursor: pointer;
    font-size: 12.5px;
    line-height: 1.4;
}
.afb__result:hover {
    background: rgba(255, 255, 255, 0.06);
}
.afb__result b {
    color: var(--afb-accent);
}
.afb__result__no {
    display: block;
    font-size: 11px;
    opacity: 0.6;
    margin-bottom: 2px;
}
.afb__muted {
    opacity: 0.6;
    font-size: 12.5px;
    padding: 8px;
}

/* ---------- Loader / overlay ---------- */
.afb__loader {
    position: absolute;
    inset: 0;
    display: flex;
    align-items: center;
    justify-content: center;
    background: rgba(0, 0, 0, 0.25);
    z-index: 60;
}
.afb__spinner {
    width: 42px;
    height: 42px;
    border: 4px solid rgba(255, 255, 255, 0.25);
    border-top-color: var(--afb-accent);
    border-radius: 50%;
    animation: afb-spin 0.8s linear infinite;
}
@keyframes afb-spin {
    to {
        transform: rotate(360deg);
    }
}
.afb__error {
    position: absolute;
    inset: 0;
    display: flex;
    align-items: center;
    justify-content: center;
    text-align: center;
    padding: 24px;
    z-index: 61;
}
.afb__backdrop {
    position: absolute;
    inset: 0;
    background: rgba(0, 0, 0, 0.4);
    z-index: 45;
    opacity: 0;
    pointer-events: none;
    transition: opacity 0.2s ease;
}
.afb__backdrop.afb--open {
    opacity: 1;
    pointer-events: auto;
}

@media print {
    .afb {
        display: none !important;
    }
}
