/* ==========================================================================
   Printables browse pages (books and activity pages)
   Shared by users/books/books.html and the pages list, category, tag and
   search templates. Tile markup and class names are driven by
   users/js/books/new_books.js and pages/js/pages.js — keep them stable.
   ========================================================================== */

:root {
    --browse-radius-sm: 6px;
    --browse-sheet: #ffffff; /* Printed paper is white in both themes. */
}

/* The dashboard shell resets .content padding on small screens, which drops
   the fixed header's offset. Restore it for these pages. */
@media (max-width: 768px) {
    .content {
        padding-top: calc(var(--header-height, 64px) + 1rem);
    }
}

/* --------------------------------------------------------------------------
   Page hero: full-bleed graph paper, heading left, context right
   -------------------------------------------------------------------------- */
.page-hero {
    display: grid;
    grid-template-columns: minmax(0, 1.35fr) minmax(0, 1fr);
    column-gap: clamp(2rem, 5vw, 5rem);
    row-gap: 1.25rem;
    align-items: end;
    margin: -1.5rem -1.5rem clamp(2rem, 4vw, 3.5rem);
    padding: clamp(2.5rem, 6vw, 5.5rem) clamp(1.5rem, 4vw, 3.5rem) clamp(2rem, 4vw, 3.5rem);
    background-color: var(--bg-primary);
    background-image:
        linear-gradient(var(--jg-rule) 1px, transparent 1px),
        linear-gradient(90deg, var(--jg-rule) 1px, transparent 1px);
    background-size: 24px 24px;
    background-position: -1px -1px;
    border-bottom: 1px solid var(--border-medium);
}

.page-hero > .breadcrumb-nav {
    grid-column: 1 / -1;
    align-self: start;
    margin: 0 0 clamp(0.5rem, 2vw, 1.5rem);
}

.page-hero__title {
    margin: 0;
    font-family: var(--font-display);
    font-size: clamp(2.5rem, 6vw, 4.75rem);
    font-weight: 700;
    line-height: 0.98;
    letter-spacing: -0.035em;
    color: var(--text-primary);
    overflow-wrap: anywhere;
}

.page-hero__body {
    max-width: 34rem;
    padding-bottom: 0.35rem;
}

.page-hero__subtitle {
    margin: 0;
    font-size: 1.125rem;
    line-height: 1.55;
    color: var(--text-secondary);
}

.page-hero__meta {
    margin: 1rem 0 0;
    font-size: 0.875rem;
    color: var(--text-muted);
    font-variant-numeric: tabular-nums;
}

@media (max-width: 1100px) {
    .page-hero {
        grid-template-columns: 1fr;
        align-items: start;
    }
}

@media (max-width: 768px) {
    .page-hero {
        margin: -1rem -1rem 2rem;
        padding: 2.25rem 1rem 1.75rem;
    }

    .page-hero__subtitle {
        font-size: 1rem;
    }
}

/* base.html pages (.browse): the hero is a full-width .jg-paper band and the grid
   layout lives on .page-hero__inner inside the container. */
.browse .page-hero {
    display: block;
    margin: 0 0 clamp(2rem, 4vw, 3.5rem);
    padding: clamp(2.5rem, 6vw, 5.5rem) 0 clamp(2rem, 4vw, 3.5rem);
}

.browse .page-hero__inner {
    display: grid;
    grid-template-columns: minmax(0, 1.35fr) minmax(0, 1fr);
    column-gap: clamp(2rem, 5vw, 5rem);
    row-gap: 1.25rem;
    align-items: end;
}

.browse .page-hero__inner > .breadcrumb-nav {
    grid-column: 1 / -1;
    align-self: start;
    margin: 0 0 clamp(0.5rem, 2vw, 1.5rem);
}

.browse .page-hero__title {
    font-weight: 800;
}

@media (max-width: 1100px) {
    .browse .page-hero__inner {
        grid-template-columns: 1fr;
        align-items: start;
    }
}

@media (max-width: 768px) {
    .browse .page-hero {
        margin: 0 0 2rem;
        padding: 2.25rem 0 1.75rem;
    }
}

/* --------------------------------------------------------------------------
   Search form
   -------------------------------------------------------------------------- */
.page-search-form {
    display: flex;
    gap: 0.5rem;
    max-width: 40rem;
    margin-top: 1.25rem;
}

.page-search-form input[type="search"] {
    flex: 1;
    min-width: 0;
    min-height: 48px;
    padding: 0.625rem 0.875rem;
    font: inherit;
    color: var(--text-primary);
    background: var(--input-bg);
    border: 1px solid var(--border-strong);
    border-radius: 6px;
}

.page-search-form input[type="search"]::placeholder {
    color: var(--text-muted);
}

.page-search-form input[type="search"]:focus-visible {
    outline: 2px solid var(--brand-primary);
    outline-offset: 1px;
}

.page-search-form .btn-browse {
    min-height: 48px;
}

.page-search-suggestions {
    display: flex;
    flex-wrap: wrap;
    align-items: center;
    gap: 0.5rem 1rem;
    margin: 0.875rem 0 0;
    font-size: 0.875rem;
    color: var(--text-muted);
}

.page-search-suggestions a {
    color: var(--brand-primary);
    text-decoration: underline;
    text-underline-offset: 3px;
}

@media (max-width: 480px) {
    .page-search-form {
        flex-direction: column;
    }

    .page-search-form .btn-browse {
        width: 100%;
    }
}

/* --------------------------------------------------------------------------
   Grid: open image-and-caption items, no card boxes
   -------------------------------------------------------------------------- */
.books-grid {
    display: grid;
    grid-template-columns: repeat(auto-fill, minmax(210px, 1fr));
    gap: clamp(2rem, 3vw, 2.75rem) clamp(1.25rem, 2vw, 2rem);
    margin: 0 0 3rem;
}

.book-tile {
    position: relative;
    display: flex;
    flex-direction: column;
    min-width: 0;
}

.book-cover {
    position: relative;
    aspect-ratio: 3 / 4;
    overflow: hidden;
    background: var(--bg-secondary);
    border: 1px solid var(--border-medium);
    border-radius: 4px;
    transition: border-color 0.15s ease;
}

.book-tile:hover .book-cover,
.book-tile:focus-within .book-cover {
    border-color: var(--brand-primary);
}

.book-cover picture {
    display: contents;
}

.book-cover img {
    display: block;
    width: 100%;
    height: 100%;
    object-fit: cover;
    object-position: top;
}

.no-cover {
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    gap: 0.5rem;
    width: 100%;
    height: 100%;
    font-size: 0.875rem;
    color: var(--text-muted);
}

.no-cover i {
    font-size: 1.5rem;
    opacity: 0.6;
}

.book-tile.preview-active .book-cover > img,
.book-tile.preview-active .book-cover > picture,
.book-tile.preview-active .no-cover {
    visibility: hidden;
}

.book-info {
    display: flex;
    flex: 1;
    flex-direction: column;
    gap: 0.25rem;
    padding: 0.875rem 0 0;
}

.book-title {
    display: -webkit-box;
    margin: 0;
    overflow: hidden;
    font-size: 1.0625rem;
    font-weight: 600;
    line-height: 1.3;
    letter-spacing: -0.01em;
    color: var(--text-primary);
    -webkit-line-clamp: 2;
    -webkit-box-orient: vertical;
}

.book-title__link {
    color: inherit;
    text-decoration: none;
}

.book-title__link:hover,
.book-tile:hover .book-title__link {
    color: inherit;
    text-decoration: underline;
    text-underline-offset: 3px;
}

.book-title__link:focus-visible {
    outline: 2px solid var(--brand-primary);
    outline-offset: 2px;
    border-radius: 2px;
}

.book-meta {
    display: flex;
    flex-wrap: wrap;
    gap: 0.25rem 0.875rem;
    font-size: 0.8125rem;
    color: var(--text-muted);
    font-variant-numeric: tabular-nums;
}

.book-rating,
.book-downloads {
    display: inline-flex;
    align-items: center;
    gap: 0.375rem;
}

.book-rating i,
.book-downloads i {
    font-size: 0.75rem;
}

.book-rating i {
    color: var(--accent-yellow);
}

/* Actions: always visible. Download is the primary action. */
.book-actions {
    display: flex;
    align-items: center;
    gap: 0.125rem;
    margin-top: auto;
    padding-top: 0.625rem;
}

.book-actions button,
.book-actions .btn-amazon {
    position: relative;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    width: 36px;
    height: 36px;
    padding: 0;
    font-size: 0.875rem;
    color: var(--text-secondary);
    text-decoration: none;
    cursor: pointer;
    background: transparent;
    border: 1px solid transparent;
    border-radius: 6px;
    transition: color 0.15s ease, border-color 0.15s ease, background-color 0.15s ease;
}

.book-actions button:hover,
.book-actions .btn-amazon:hover {
    color: var(--text-primary);
    border-color: var(--border-strong);
}

.book-actions .btn-tile-download {
    margin-right: auto;
    color: #ffffff;
    background: var(--brand-primary-strong);
}

.book-actions .btn-tile-download:hover {
    color: #ffffff;
    border-color: transparent;
    background: var(--accent-primary-hover);
}

.book-actions .btn-tile-download.already-downloaded {
    background: var(--brand-secondary-strong);
}

.book-actions button:disabled {
    cursor: progress;
    opacity: 0.7;
}

.book-tile .btn-tile-favorite.favorited {
    color: var(--brand-highlight-strong);
}

.book-actions button:focus-visible,
.book-actions .btn-amazon:focus-visible {
    outline: 2px solid var(--brand-primary);
    outline-offset: 2px;
}

/* Tooltip from the aria-label */
.book-actions button::after,
.book-actions .btn-amazon::after {
    content: attr(aria-label);
    position: absolute;
    bottom: calc(100% + 6px);
    left: 50%;
    z-index: 20;
    padding: 0.25rem 0.5rem;
    font-size: 0.75rem;
    font-weight: 500;
    line-height: 1.3;
    white-space: nowrap;
    color: var(--bg-primary);
    background: var(--text-primary);
    border-radius: 4px;
    transform: translateX(-50%);
    opacity: 0;
    visibility: hidden;
    pointer-events: none;
    transition: opacity 0.15s ease;
}

.book-actions .btn-tile-download::after {
    left: 0;
    transform: none;
}

.book-actions > :last-child::after {
    left: auto;
    right: 0;
    transform: none;
}

.book-actions button:hover::after,
.book-actions .btn-amazon:hover::after,
.book-actions button:focus-visible::after,
.book-actions .btn-amazon:focus-visible::after {
    opacity: 1;
    visibility: visible;
}

/* Phones: a ruled list, cover beside the details. */
@media (max-width: 480px) {
    .books-grid {
        grid-template-columns: 1fr;
        gap: 0;
        border-top: 1px solid var(--border-medium);
    }

    .book-tile {
        flex-direction: row;
        gap: 1rem;
        padding: 1rem 0;
        border-bottom: 1px solid var(--border-light);
    }

    .book-cover {
        flex: 0 0 96px;
        align-self: flex-start;
    }

    .book-info {
        padding: 0;
    }

    .book-actions button,
    .book-actions .btn-amazon {
        width: 40px;
        height: 40px;
    }

    .book-actions button::after,
    .book-actions .btn-amazon::after {
        display: none;
    }
}

/* --------------------------------------------------------------------------
   Empty state: open layout, heading left, next steps right
   -------------------------------------------------------------------------- */
.browse-empty {
    grid-column: 1 / -1;
    display: grid;
    grid-template-columns: minmax(0, 1fr) minmax(0, 1.6fr);
    gap: 1.5rem clamp(2rem, 5vw, 5rem);
    align-items: start;
}

.browse-empty__title {
    margin: 0;
    font-size: clamp(1.6rem, 3vw, 2.25rem);
    font-weight: 600;
    line-height: 1.15;
    letter-spacing: -0.02em;
    color: var(--text-primary);
}

.browse-empty__text {
    max-width: 60ch;
    margin: 0;
    font-size: 1.0625rem;
    line-height: 1.6;
    color: var(--text-secondary);
}

.browse-empty__actions {
    display: flex;
    flex-wrap: wrap;
    gap: 0.75rem;
    margin-top: 1.25rem;
}

.btn-browse {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    min-height: 44px;
    padding: 0.625rem 1.125rem;
    font: inherit;
    font-size: 0.9375rem;
    font-weight: 600;
    line-height: 1.2;
    color: var(--brand-primary);
    text-decoration: none;
    cursor: pointer;
    background: transparent;
    border: 1px solid var(--border-strong);
    border-radius: 6px;
    transition: border-color 0.15s ease, background-color 0.15s ease, color 0.15s ease;
}

.btn-browse:hover {
    color: var(--brand-primary);
    border-color: var(--brand-primary);
}

.btn-browse--primary,
.btn-browse--primary:hover {
    color: #ffffff;
    background: var(--brand-primary-strong);
    border-color: var(--brand-primary-strong);
}

.btn-browse--primary:hover {
    background: var(--accent-primary-hover);
}

.btn-browse:focus-visible {
    outline: 2px solid var(--brand-primary);
    outline-offset: 2px;
}

/* Real tools you can use instead, as a ruled list */
.browse-tools {
    margin-top: 2.5rem;
}

.browse-tools__title {
    margin: 0 0 0.5rem;
    font-size: 0.9375rem;
    font-weight: 600;
    color: var(--text-muted);
}

.browse-tools__list {
    margin: 0;
    padding: 0;
    list-style: none;
    border-top: 1px solid var(--border-medium);
}

.browse-tools__list li {
    border-bottom: 1px solid var(--border-light);
}

.browse-tools__link {
    display: grid;
    grid-template-columns: minmax(10rem, 1fr) minmax(0, 2fr);
    gap: 0.25rem 1.5rem;
    align-items: baseline;
    padding: 0.875rem 0;
    color: inherit;
    text-decoration: none;
}

.browse-tools__name {
    font-size: 1.0625rem;
    font-weight: 600;
    color: var(--text-primary);
}

.browse-tools__desc {
    font-size: 0.9375rem;
    color: var(--text-secondary);
}

.browse-tools__link:hover .browse-tools__name {
    color: var(--brand-primary);
    text-decoration: underline;
    text-underline-offset: 3px;
}

.browse-tools__link:focus-visible {
    outline: 2px solid var(--brand-primary);
    outline-offset: 2px;
}

.browse-tools__all {
    display: inline-block;
    margin-top: 0.875rem;
    font-size: 0.9375rem;
    color: var(--brand-primary);
    text-decoration: underline;
    text-underline-offset: 3px;
}

@media (max-width: 900px) {
    .browse-empty {
        grid-template-columns: 1fr;
    }
}

@media (max-width: 480px) {
    .btn-browse {
        width: 100%;
    }

    .browse-tools__link {
        grid-template-columns: 1fr;
    }
}

/* --------------------------------------------------------------------------
   Pagination
   -------------------------------------------------------------------------- */
.pagination {
    display: flex;
    flex-wrap: wrap;
    align-items: center;
    justify-content: center;
    gap: 0.375rem;
    margin: 2rem 0;
}

.pagination a,
.pagination span {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    min-width: 40px;
    height: 40px;
    padding: 0 0.75rem;
    font-size: 0.875rem;
    font-weight: 500;
    color: var(--text-primary);
    text-decoration: none;
    background: var(--bg-card);
    border: 1px solid var(--border-medium);
    border-radius: var(--browse-radius-sm);
    transition: border-color 0.15s ease;
}

.pagination a:hover {
    border-color: var(--brand-primary);
}

.pagination a:focus-visible {
    outline: 2px solid var(--brand-primary);
    outline-offset: 2px;
}

.pagination .current {
    font-weight: 600;
    color: #ffffff;
    background: var(--brand-primary-strong);
    border-color: var(--brand-primary-strong);
}

/* --------------------------------------------------------------------------
   Preview (created by new_books.js when the eye button is pressed)
   -------------------------------------------------------------------------- */
.flipbook-container {
    position: absolute;
    inset: 0;
    z-index: 5;
    background: var(--bg-secondary);
    perspective: 1500px;
}

.flipbook-container.effect-coloring {
    perspective: none;
}

.coloring-page {
    position: absolute;
    inset: 6px;
    background: var(--browse-sheet);
    border: 1px solid var(--border-light);
    border-radius: 3px;
    transform-origin: 50% 100%;
    animation: coloringPageCycle 10s infinite ease-in-out;
}

.coloring-page img {
    display: block;
    width: 100%;
    height: 100%;
    padding: 8px;
    object-fit: contain;
}

.coloring-page:nth-child(1) { z-index: 5; animation-delay: 0s; }
.coloring-page:nth-child(2) { z-index: 4; animation-delay: 2s; }
.coloring-page:nth-child(3) { z-index: 3; animation-delay: 4s; }
.coloring-page:nth-child(4) { z-index: 2; animation-delay: 6s; }
.coloring-page:nth-child(5) { z-index: 1; animation-delay: 8s; }

@keyframes coloringPageCycle {
    0%, 16% { opacity: 1; transform: translateY(0); }
    26%, 86% { opacity: 0; transform: translateY(-16px); }
    100% { opacity: 1; transform: translateY(0); }
}

.flip-page {
    position: absolute;
    inset: 0;
    transform-origin: left center;
    transform-style: preserve-3d;
    animation: pageFlip 6s infinite ease-in-out;
}

.flip-page img {
    position: absolute;
    inset: 0;
    width: 100%;
    height: 100%;
    object-fit: cover;
    background: var(--browse-sheet);
    backface-visibility: hidden;
}

.flip-page::before {
    content: '';
    position: absolute;
    inset: 0;
    background: var(--browse-sheet);
    transform: rotateY(180deg);
    backface-visibility: hidden;
}

@keyframes pageFlip {
    0% { transform: rotateY(0deg); }
    35%, 80% { transform: rotateY(-180deg); opacity: 1; }
    100% { transform: rotateY(-180deg); opacity: 0; }
}

.effect-puzzle .flip-page,
.effect-sudoku .flip-page,
.effect-word-search .flip-page {
    transform-origin: center bottom;
    animation-name: puzzleStack;
}

@keyframes puzzleStack {
    0%, 15% { transform: translateY(0); opacity: 1; z-index: 10; }
    25% { transform: translateY(-16px); opacity: 0; z-index: 10; }
    26%, 100% { transform: translateY(0); opacity: 0; z-index: 0; }
}

/* --------------------------------------------------------------------------
   Inline rating (created by new_books.js / pages.js on the rate button)
   -------------------------------------------------------------------------- */
.inline-rating-widget {
    position: absolute;
    right: 0;
    bottom: 0;
    left: 0;
    z-index: 30;
    padding: 1rem;
    background: var(--bg-card);
    border-top: 1px solid var(--border-strong);
    transform: translateY(100%);
    opacity: 0;
    pointer-events: none;
    transition: transform 0.2s ease, opacity 0.2s ease;
}

.inline-rating-widget.active {
    transform: translateY(0);
    opacity: 1;
    pointer-events: auto;
}

.inline-rating-widget .rating-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    margin-bottom: 0.75rem;
}

.inline-rating-widget .rating-title {
    font-size: 0.9375rem;
    font-weight: 600;
    color: var(--text-primary);
}

.inline-rating-widget .rating-close {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    width: 32px;
    height: 32px;
    color: var(--text-secondary);
    cursor: pointer;
    background: transparent;
    border: 1px solid var(--border-medium);
    border-radius: 50%;
}

.inline-rating-widget .rating-close:hover {
    color: var(--text-primary);
    border-color: var(--border-strong);
}

.inline-rating-widget .star-row {
    display: flex;
    justify-content: center;
    gap: 0.5rem;
    margin-bottom: 0.25rem;
}

.inline-rating-widget .inline-star {
    font-size: 1.75rem;
    color: var(--text-muted);
    cursor: pointer;
    transition: color 0.1s ease;
}

.inline-rating-widget .inline-star.hovered,
.inline-rating-widget .inline-star.selected {
    color: var(--accent-yellow);
}

.inline-rating-widget .rating-success {
    display: none;
    flex-direction: column;
    align-items: center;
    gap: 0.5rem;
    padding: 0.5rem 0;
}

.inline-rating-widget.success .star-row,
.inline-rating-widget.success .rating-header,
.inline-rating-widget.success .review-input-section {
    display: none;
}

.inline-rating-widget.success .rating-success {
    display: flex;
}

.inline-rating-widget .success-icon {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    width: 40px;
    height: 40px;
    font-size: 1.125rem;
    color: #ffffff;
    background: var(--brand-secondary-strong);
    border-radius: 50%;
}

.inline-rating-widget .success-text {
    font-size: 0.9375rem;
    font-weight: 600;
    color: var(--text-primary);
}

.inline-rating-widget.loading .star-row,
.inline-rating-widget.loading .review-input-section {
    opacity: 0.5;
    pointer-events: none;
}

.inline-rating-widget .rating-error {
    display: none;
    margin-top: 0.5rem;
    padding: 0.5rem;
    font-size: 0.8125rem;
    text-align: center;
    color: var(--error-text);
    background: var(--error-bg);
    border: 1px solid var(--error-border);
    border-radius: var(--browse-radius-sm);
}

.inline-rating-widget.error .rating-error {
    display: block;
}

.inline-rating-widget .review-input-section {
    display: flex;
    flex-wrap: wrap;
    align-items: flex-end;
    gap: 0.5rem;
    max-height: 0;
    margin-top: 0;
    overflow: hidden;
    opacity: 0;
    pointer-events: none;
    transition: max-height 0.2s ease, opacity 0.2s ease, margin 0.2s ease;
}

.inline-rating-widget.expanded .review-input-section {
    max-height: 180px;
    margin-top: 0.75rem;
    opacity: 1;
    pointer-events: auto;
}

.inline-rating-widget .review-hint {
    order: 1;
    width: 100%;
    font-size: 0.75rem;
    color: var(--text-muted);
}

.inline-rating-widget .review-text {
    flex: 1;
    min-width: 0;
    padding: 0.625rem 0.75rem;
    font: inherit;
    font-size: 0.875rem;
    line-height: 1.4;
    color: var(--text-primary);
    resize: none;
    background: var(--input-bg);
    border: 1px solid var(--border-strong);
    border-radius: var(--browse-radius-sm);
}

.inline-rating-widget .review-text::placeholder {
    color: var(--text-muted);
}

.inline-rating-widget .review-text:focus {
    outline: 2px solid var(--brand-primary);
    outline-offset: 1px;
}

.inline-rating-widget .submit-review-btn {
    display: inline-flex;
    flex-shrink: 0;
    align-items: center;
    justify-content: center;
    width: 44px;
    height: 44px;
    color: #ffffff;
    cursor: pointer;
    background: var(--brand-primary-strong);
    border: none;
    border-radius: var(--browse-radius-sm);
}

.inline-rating-widget .submit-review-btn:disabled {
    color: var(--text-muted);
    cursor: not-allowed;
    background: var(--bg-secondary);
}

.inline-rating-widget .submit-review-btn:focus-visible,
.inline-rating-widget .rating-close:focus-visible {
    outline: 2px solid var(--brand-primary);
    outline-offset: 2px;
}

@media (prefers-reduced-motion: reduce) {
    .coloring-page,
    .flip-page {
        animation: none;
    }

    .coloring-page:not(:first-child),
    .flip-page:not(:first-child) {
        display: none;
    }

    .book-tile,
    .inline-rating-widget,
    .inline-rating-widget .review-input-section {
        transition: none;
    }
}
