/* ============================================================
   Rex Flipbook Library — Bookshelf Styles
   ============================================================ */

.rex-fb-library{
    --rex-fb-lib-bg: #2d3748;
    --rex-fb-lib-wood-1: #8B5E3C;
    --rex-fb-lib-wood-2: #6B4423;
    --rex-fb-lib-wood-3: #5C3818;
    --rex-fb-lib-wood-shadow: #3D2410;
    --rex-fb-lib-accent: #00a9ce;
    --rex-fb-lib-text: #fff;
    --rex-fb-lib-text-dim: rgba(255,255,255,.7);
    --rex-fb-lib-cols: 6;

    position: relative;
    width: 100%;
    background: var(--rex-fb-lib-bg);
    border-radius: 12px;
    padding: 28px;
    color: var(--rex-fb-lib-text);
    font-family: -apple-system, BlinkMacSystemFont, "Inter", "Segoe UI", Roboto, sans-serif;
    box-sizing: border-box;
}

/* Toolbar */
.rex-fb-lib-toolbar{
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 16px;
    margin-bottom: 24px;
    flex-wrap: wrap;
}
.rex-fb-lib-search{
    display: flex;
    align-items: center;
    gap: 10px;
    background: rgba(255,255,255,.1);
    border: 1px solid rgba(255,255,255,.12);
    border-radius: 100px;
    padding: 8px 16px;
    flex: 1;
    max-width: 340px;
    color: var(--rex-fb-lib-text-dim);
    transition: border-color .15s, background .15s;
}
.rex-fb-lib-search:focus-within{
    border-color: var(--rex-fb-lib-accent);
    background: rgba(255,255,255,.14);
}
.rex-fb-lib-search input{
    flex: 1;
    background: none;
    border: none;
    color: var(--rex-fb-lib-text);
    font-size: 14px;
    outline: none;
    padding: 0;
}
.rex-fb-lib-search input::placeholder{ color: var(--rex-fb-lib-text-dim); }

.rex-fb-lib-sort{ display: flex; gap: 4px; }
.rex-fb-lib-sort-btn{
    background: rgba(255,255,255,.06);
    border: 1px solid rgba(255,255,255,.10);
    color: var(--rex-fb-lib-text-dim);
    padding: 8px 16px;
    border-radius: 100px;
    font-size: 13px;
    cursor: pointer;
    transition: all .15s;
}
.rex-fb-lib-sort-btn:hover{ background: rgba(255,255,255,.12); color: var(--rex-fb-lib-text); }
.rex-fb-lib-sort-btn.is-active{
    background: var(--rex-fb-lib-accent);
    border-color: var(--rex-fb-lib-accent);
    color: #fff;
    font-weight: 600;
}

/* ---------- Shelf ---------- */
.rex-fb-library-shelf .rex-fb-lib-shelf{
    background:
        linear-gradient(180deg, var(--rex-fb-lib-wood-1) 0%, var(--rex-fb-lib-wood-2) 100%);
    background-image:
        repeating-linear-gradient(
            90deg,
            rgba(0,0,0,.06) 0px,
            rgba(0,0,0,.06) 2px,
            transparent 2px,
            transparent 8px
        ),
        linear-gradient(180deg, var(--rex-fb-lib-wood-1) 0%, var(--rex-fb-lib-wood-2) 100%);
    padding: 20px 24px;
    border-radius: 10px;
    box-shadow:
        inset 0 2px 6px rgba(0,0,0,.25),
        0 12px 28px rgba(0,0,0,.4);
    border: 2px solid var(--rex-fb-lib-wood-shadow);
    position: relative;
    overflow: hidden;
}
.rex-fb-library-shelf .rex-fb-lib-shelf::before{
    content: '';
    position: absolute;
    inset: 0;
    background:
        radial-gradient(ellipse at 20% 50%, rgba(255,255,255,.05) 0%, transparent 50%),
        radial-gradient(ellipse at 80% 50%, rgba(0,0,0,.12) 0%, transparent 50%);
    pointer-events: none;
}

/* Grid layout */
.rex-fb-lib-shelf{
    display: grid;
    grid-template-columns: repeat(var(--rex-fb-lib-cols), 1fr);
    gap: 28px 18px;
    position: relative;
}
/* Wooden shelf separators (every Nth row) */
.rex-fb-library-shelf .rex-fb-lib-shelf::after{
    content: '';
    position: absolute;
    left: -24px; right: -24px;
    height: 10px;
    background: linear-gradient(180deg, var(--rex-fb-lib-wood-3) 0%, var(--rex-fb-lib-wood-shadow) 100%);
    box-shadow: 0 4px 8px rgba(0,0,0,.4);
    pointer-events: none;
}

/* Simple grid variant (no wood) */
.rex-fb-library-grid{ background: transparent; padding: 0; color: inherit; }
.rex-fb-library-grid .rex-fb-lib-toolbar{ color: inherit; }
.rex-fb-library-grid .rex-fb-lib-search,
.rex-fb-library-grid .rex-fb-lib-sort-btn{ background: rgba(0,0,0,.06); border-color: rgba(0,0,0,.10); color: inherit; }
.rex-fb-library-grid .rex-fb-lib-search input{ color: inherit; }
.rex-fb-library-grid .rex-fb-lib-shelf{ gap: 24px; }
.rex-fb-library-grid .rex-fb-lib-book-info{ color: inherit; }

/* ---------- Book Card (Realistic 3D) ---------- */
.rex-fb-lib-book{
    background: transparent;
    border: none;
    padding: 0 0 4px;
    cursor: pointer;
    display: flex;
    flex-direction: column;
    gap: 12px;
    color: inherit;
    font: inherit;
    text-align: left;
    perspective: 1500px;
    transition: transform .35s cubic-bezier(.2, .8, .2, 1);
}
.rex-fb-lib-book:focus-visible{ outline: 2px solid var(--rex-fb-lib-accent); outline-offset: 4px; border-radius: 4px; }
.rex-fb-lib-book:hover{ transform: translateY(-8px); }

/* 3D container */
.rex-fb-lib-book-3d{
    position: relative;
    display: block;
    aspect-ratio: 3 / 4;
    transform-style: preserve-3d;
    transform: rotateY(-22deg);
    transition: transform .45s cubic-bezier(.2, .8, .2, 1);
}
.rex-fb-lib-book:hover .rex-fb-lib-book-3d{
    transform: rotateY(-6deg);
}

/* Front cover (face of the book) */
.rex-fb-lib-book-cover{
    position: absolute;
    inset: 0;
    background: #fff;
    border-radius: 1px 4px 4px 1px;
    overflow: hidden;
    box-shadow:
        /* shelf cast shadow */
        0 14px 22px -10px rgba(0,0,0,.6),
        /* under-book shadow */
        0 4px 8px rgba(0,0,0,.35),
        /* darken on spine side */
        inset 5px 0 0 rgba(0,0,0,.25),
        inset 9px 0 8px rgba(0,0,0,.12);
    transform: translateZ(8px);
}
.rex-fb-lib-book-cover img{
    width: 100%;
    height: 100%;
    object-fit: cover;
    display: block;
    user-select: none;
    -webkit-user-drag: none;
}

/* Spine (left side of book, with depth) */
.rex-fb-lib-book-spine{
    position: absolute;
    left: 0; top: 0;
    width: 16px;
    height: 100%;
    background:
        linear-gradient(90deg,
            rgba(0,0,0,.5) 0%,
            rgba(0,0,0,.2) 40%,
            rgba(0,0,0,.35) 100%),
        linear-gradient(135deg, #2a1810 0%, #4a2818 100%);
    border-radius: 1px 0 0 1px;
    transform: rotateY(-90deg) translateZ(0);
    transform-origin: left center;
}

/* Pages (right side fore-edge with page stack texture) */
.rex-fb-lib-book-pages{
    position: absolute;
    right: 0; top: 1.5%;
    width: 16px;
    height: 97%;
    background:
        repeating-linear-gradient(0deg,
            #fffaf0 0px, #fffaf0 1px,
            #ded4ba 1px, #ded4ba 2px);
    transform: rotateY(90deg) translateZ(0);
    transform-origin: right center;
    box-shadow:
        inset 0 0 4px rgba(0,0,0,.25),
        inset 2px 0 2px rgba(0,0,0,.15);
}

/* Placeholder card (no cover image) */
.rex-fb-lib-book-placeholder{
    width: 100%;
    height: 100%;
    background: linear-gradient(135deg, #4a5568 0%, #2d3748 100%);
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    padding: 16px;
    text-align: center;
    color: #fff;
    gap: 10px;
}
.rex-fb-lib-book-placeholder-icon{ font-size: 32px; opacity: .55; }
.rex-fb-lib-book-placeholder-title{
    font-size: 12px;
    font-weight: 600;
    line-height: 1.3;
    opacity: .85;
    display: -webkit-box;
    -webkit-line-clamp: 4;
    -webkit-box-orient: vertical;
    overflow: hidden;
}

/* Book info under cover */
.rex-fb-lib-book-info{
    display: flex;
    flex-direction: column;
    gap: 2px;
    padding: 0 2px;
}
.rex-fb-lib-book-title{
    font-size: 13px;
    font-weight: 600;
    line-height: 1.3;
    color: var(--rex-fb-lib-text);
    display: -webkit-box;
    -webkit-line-clamp: 2;
    -webkit-box-orient: vertical;
    overflow: hidden;
}
.rex-fb-lib-book-date{
    font-size: 11px;
    color: var(--rex-fb-lib-text-dim);
}

/* Category chips */
.rex-fb-lib-cats{
    display: flex;
    flex-wrap: wrap;
    gap: 6px;
    margin-bottom: 18px;
}
.rex-fb-lib-cat{
    background: rgba(255,255,255,.07);
    border: 1px solid rgba(255,255,255,.10);
    color: var(--rex-fb-lib-text-dim);
    padding: 6px 14px;
    border-radius: 100px;
    font-size: 13px;
    cursor: pointer;
    transition: all .15s;
}
.rex-fb-lib-cat:hover{ background: rgba(255,255,255,.14); color: var(--rex-fb-lib-text); }
.rex-fb-lib-cat.is-active{
    background: var(--rex-fb-lib-accent);
    border-color: var(--rex-fb-lib-accent);
    color: #fff;
    font-weight: 600;
}
.rex-fb-library-grid .rex-fb-lib-cat{ background: rgba(0,0,0,.06); border-color: rgba(0,0,0,.10); color: inherit; }

/* Empty state */
.rex-fb-lib-empty{
    padding: 40px;
    text-align: center;
    color: var(--rex-fb-lib-text-dim);
    font-size: 14px;
}

/* ---------- Modal ---------- */
.rex-fb-lib-modal{
    position: fixed;
    inset: 0;
    z-index: 99999;
    display: none;
    align-items: center;
    justify-content: center;
}
.rex-fb-lib-modal.is-open{ display: flex; }

.rex-fb-lib-modal-backdrop{
    position: absolute;
    inset: 0;
    background: rgba(0,0,0,.85);
    backdrop-filter: blur(8px);
    -webkit-backdrop-filter: blur(8px);
    opacity: 0;
    animation: rex-fb-lib-fade-in .3s forwards;
}
.rex-fb-lib-modal-wrap{
    position: relative;
    width: calc(100vw - 40px);
    height: calc(100vh - 40px);
    max-width: 1400px;
    background: #0a0a0a;
    border-radius: 12px;
    overflow: hidden;
    box-shadow: 0 30px 80px rgba(0,0,0,.6);
    transform: scale(.85);
    opacity: 0;
    animation: rex-fb-lib-pop .35s cubic-bezier(.2, 1.1, .3, 1) forwards;
}
@keyframes rex-fb-lib-fade-in {
    to { opacity: 1; }
}
@keyframes rex-fb-lib-pop {
    to { transform: scale(1); opacity: 1; }
}

.rex-fb-lib-modal-close{
    position: absolute;
    top: 16px;
    right: 16px;
    z-index: 10;
    width: 52px; height: 52px;
    background: rgba(0,0,0,.65);
    border: 1px solid rgba(255,255,255,.22);
    border-radius: 50%;
    color: #fff;
    cursor: pointer;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    transition: background .15s, transform .15s, border-color .15s;
    backdrop-filter: blur(10px);
    -webkit-backdrop-filter: blur(10px);
    box-shadow: 0 4px 16px rgba(0,0,0,.4);
}
.rex-fb-lib-modal-close svg{ width: 26px; height: 26px; }
.rex-fb-lib-modal-close:hover{
    background: rgba(0,0,0,.85);
    border-color: rgba(255,255,255,.4);
    transform: scale(1.06);
}
.rex-fb-lib-modal-close:active{ transform: scale(.94); }

.rex-fb-lib-modal-body{
    width: 100%;
    height: 100%;
    overflow: hidden;
    display: flex;
}
.rex-fb-lib-modal-body > .rex-fb-wrap{ flex: 1; border-radius: 0; box-shadow: none; }

/* Reserve space on toolbar right so the modal close X doesn't overlap the
   flipbook toolbar icons (fullscreen, download, etc.) */
.rex-fb-lib-modal-body .rex-fb-toolbar{ padding-right: 80px; }
@media (max-width: 640px){
    .rex-fb-lib-modal-body .rex-fb-toolbar{ padding-right: 60px; }
    .rex-fb-lib-modal-close{ width: 42px; height: 42px; top: 10px; right: 10px; }
    .rex-fb-lib-modal-close svg{ width: 22px; height: 22px; }
}

/* Modal loader */
.rex-fb-lib-modal-loader{
    flex: 1;
    display: flex;
    align-items: center;
    justify-content: center;
    background: #0a0a0a;
}
.rex-fb-lib-modal-spinner{
    width: 44px; height: 44px;
    border: 3px solid rgba(255,255,255,.18);
    border-top-color: var(--rex-fb-lib-accent);
    border-radius: 50%;
    animation: rex-fb-spin .9s linear infinite;
}
.rex-fb-lib-modal-error{
    color: #f87171;
    padding: 40px;
    text-align: center;
    margin: auto;
}

/* ============================================================
   BOOKSHELF STYLE — pixel-perfect alignment over a real wooden
   bookshelf image. Each "section" matches one full bookshelf
   (24 slots: 6 cols × 4 rows for the bundled image). When
   books > slots, multiple sections stack vertically.

   Calibration vars (tune if you swap the bundled image):
     --rex-fb-bs-aspect: image width / height (CSS calc-friendly)
     --rex-fb-bs-pad-top    : % from image top to row 1 top
     --rex-fb-bs-pad-bottom : % from image bottom to last row bottom
     --rex-fb-bs-pad-left   : % from left edge to col 1
     --rex-fb-bs-pad-right  : % from right edge to last col
     --rex-fb-bs-cols / --rex-fb-bs-rows : grid inside the image
   ============================================================ */
.rex-fb-library-bookshelf{
    /* Calibrated via pixel-scanning of bookshelf.png (1672×941).
       Shelf-board TOP edges (where books physically rest) measured at:
         shelf 1 top: 38.58%   shelf 2 top: 51.65%
         shelf 3 top: 64.61%   plinth top:  75.03%
       Compartment 1 starts at ~29% (just below cornice dark band).
       Row 1 is shorter than other rows because cornice eats vertical space. */
    --rex-fb-bs-aspect: 1672 / 941;
    --rex-fb-bs-pad-top:    29%;     /* cornice + white margin */
    --rex-fb-bs-pad-bottom: 25%;     /* 100% - 75% (top of plinth) */
    --rex-fb-bs-pad-left:   6.5%;
    --rex-fb-bs-pad-right:  6.5%;
    --rex-fb-bs-cols: 6;
    --rex-fb-bs-rows: 4;
    /* Proportional row heights matching exact inter-shelf distances */
    --rex-fb-bs-row-template: 9.58fr 13.07fr 12.96fr 10.42fr;
    --rex-fb-bs-image: url('../img/bookshelf.png');
    --rex-fb-bs-section-gap: 18px;

    padding: 0;
    background: transparent;
    border-radius: 0;
    box-shadow: none;
    color: #fff;
    overflow: visible;
}
.rex-fb-library-bookshelf .rex-fb-lib-toolbar{
    margin: 0 0 14px;
    padding: 12px 16px;
    background: rgba(20,12,6,.78);
    border: 1px solid rgba(255,255,255,.06);
    border-radius: 10px;
    backdrop-filter: blur(8px);
    -webkit-backdrop-filter: blur(8px);
}
.rex-fb-library-bookshelf .rex-fb-lib-cats{
    margin: 0 0 14px;
    padding: 10px 14px;
    background: rgba(20,12,6,.62);
    border: 1px solid rgba(255,255,255,.05);
    border-radius: 10px;
}

/* The shelf container is just a vertical stack of sections */
.rex-fb-lib-shelf-bs{
    display: flex !important;
    flex-direction: column;
    gap: var(--rex-fb-bs-section-gap);
    background: transparent !important;
    padding: 0 !important;
    border: none !important;
    box-shadow: none !important;
}

/* Each section = one full bookshelf with the image as background */
.rex-fb-bs-section{
    position: relative;
    width: 100%;
    aspect-ratio: var(--rex-fb-bs-aspect);
    min-height: 320px; /* safety: ensure books are always readable even in narrow containers */
    background: var(--rex-fb-lib-bookshelf-bg, var(--rex-fb-bs-image)) center / 100% 100% no-repeat;
    filter: drop-shadow(0 16px 32px rgba(0,0,0,.4));
}

/* Escape Elementor / theme column constraints — use full viewport width */
.rex-fb-lib-fullwidth{
    width: 100vw !important;
    max-width: 100vw !important;
    margin-left:  calc(50% - 50vw) !important;
    margin-right: calc(50% - 50vw) !important;
    padding-left:  4vw;
    padding-right: 4vw;
    box-sizing: border-box;
}

/* Books grid — absolute positioning in % keeps it aligned with the image */
.rex-fb-bs-books{
    position: absolute;
    top:    var(--rex-fb-bs-pad-top);
    bottom: var(--rex-fb-bs-pad-bottom);
    left:   var(--rex-fb-bs-pad-left);
    right:  var(--rex-fb-bs-pad-right);
    display: grid;
    grid-template-columns: repeat(var(--rex-fb-bs-cols), 1fr);
    grid-template-rows:    var(--rex-fb-bs-row-template, repeat(var(--rex-fb-bs-rows), 1fr));
    align-items: end;
    justify-items: center;
    gap: 0;
}

/* Books — sit at the bottom of each cell (on the shelf) */
.rex-fb-library-bookshelf .rex-fb-lib-book{
    height: 92%;
    width: auto;
    max-width: 90%;
    padding: 0;
    margin-bottom: 0;
    perspective: 1400px;
    transition: transform .25s cubic-bezier(.2,.8,.2,1);
    position: relative;
    display: flex;
    align-items: flex-end;
    justify-content: center;
}
.rex-fb-library-bookshelf .rex-fb-lib-book:hover{ transform: translateY(-6px) scale(1.04); }

.rex-fb-library-bookshelf .rex-fb-lib-book-3d{
    aspect-ratio: 3 / 4;
    height: 100%;
    width: auto;
    transform: rotateY(-12deg);
    transition: transform .35s cubic-bezier(.2,.8,.2,1);
    filter: drop-shadow(0 5px 6px rgba(0,0,0,.5));
}
.rex-fb-library-bookshelf .rex-fb-lib-book:hover .rex-fb-lib-book-3d{
    transform: rotateY(-3deg);
}

/* No title/date strip below books (cleaner shelf look) */
.rex-fb-library-bookshelf .rex-fb-lib-book-info{ display: none; }

/* Tooltip with title on hover */
.rex-fb-library-bookshelf .rex-fb-lib-book::after{
    content: attr(data-title);
    position: absolute;
    left: 50%;
    bottom: calc(100% + 6px);
    transform: translateX(-50%) translateY(6px);
    background: rgba(15,10,5,.96);
    color: #fff;
    font-size: 12px;
    padding: 5px 10px;
    border-radius: 5px;
    white-space: nowrap;
    max-width: 220px;
    overflow: hidden;
    text-overflow: ellipsis;
    opacity: 0;
    pointer-events: none;
    transition: opacity .15s, transform .15s;
    box-shadow: 0 6px 18px rgba(0,0,0,.5);
    z-index: 5;
}
.rex-fb-library-bookshelf .rex-fb-lib-book:hover::after,
.rex-fb-library-bookshelf .rex-fb-lib-book:focus-visible::after{
    opacity: 1;
    transform: translateX(-50%) translateY(0);
}

/* Bookshelf responsive: image keeps its aspect, books scale with it.
   The cols count is set by PHP (shortcode `columns` attr), not media queries,
   because the bundled image has no visible vertical dividers — any cols count
   looks correct, and PHP-side chunking depends on this value. */

/* ---------- Mobile ---------- */
@media (max-width: 1024px){
    .rex-fb-lib-shelf{ --rex-fb-lib-cols: 4; }
}
@media (max-width: 768px){
    .rex-fb-library{ padding: 18px; }
    .rex-fb-lib-shelf{ --rex-fb-lib-cols: 3; gap: 20px 14px; }
    .rex-fb-library-shelf .rex-fb-lib-shelf{ padding: 16px 18px; }
    .rex-fb-lib-toolbar{ gap: 10px; }
    .rex-fb-lib-search{ max-width: none; }
    .rex-fb-lib-modal-wrap{ width: 100vw; height: 100vh; border-radius: 0; }
    .rex-fb-lib-modal-wrap{ max-width: none; }
}
@media (max-width: 480px){
    .rex-fb-lib-shelf{ --rex-fb-lib-cols: 2; }
    .rex-fb-lib-book-title{ font-size: 12px; }
}

/* Reduced motion */
@media (prefers-reduced-motion: reduce){
    .rex-fb-lib-book, .rex-fb-lib-book-cover, .rex-fb-lib-modal-wrap, .rex-fb-lib-modal-backdrop{ transition: none; animation: none; }
    .rex-fb-lib-modal-wrap{ transform: none; opacity: 1; }
    .rex-fb-lib-modal-backdrop{ opacity: 1; }
}
