/* === Сброс === */
*,
*::before,
*::after {
    box-sizing: border-box;
}

body {
    margin: 0;
    padding: 0;
    background: #1a1a1a;
    font-family: Arial, sans-serif;
    overflow: hidden;
    color: #fff;
}

/* Фон с логотипом для режимов с листалкой */
body.mode-single,
body.mode-double {
    background-color: #dce2e3;
    background-image: url("/images/reader-bg.jpg");
    background-size: cover;
    background-position: center;
}

/* ===========================
   ТУЛБАР
   =========================== */
#toolbar {
    position: fixed;
    top: 12px;
    left: 16px;
    right: 16px;
    height: 52px;
    background: rgba(36, 64, 17, 0.6);
    backdrop-filter: blur(24px) saturate(1.4);
    -webkit-backdrop-filter: blur(24px) saturate(1.4);
    border: 1px solid rgba(255, 255, 255, 0.08);
    border-radius: 14px;
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 0 10px;
    z-index: 100;
    box-shadow:
        0 8px 32px rgba(0, 0, 0, 0.55),
        0 2px 8px rgba(0, 0, 0, 0.3);
    gap: 8px;
}

#tb-left,
#tb-right {
    display: flex;
    align-items: center;
    gap: 2px;
    flex: 1;
}
#tb-right {
    justify-content: flex-end;
}
#tb-center {
    display: flex;
    align-items: center;
    gap: 6px;
    flex-shrink: 0;
}

/* Кнопка «На главную» */
#btn-close {
    display: flex;
    align-items: center;
    justify-content: center;
    width: 36px;
    height: 36px;
    color: rgba(255, 255, 255, 0.75);
    border-radius: 6px;
    text-decoration: none;
    transition: background 0.2s, color 0.2s;
}
#btn-close:hover {
    background: rgba(255, 255, 255, 0.15);
    color: #fff;
}
#btn-close svg {
    width: 20px;
    height: 20px;
}

/* Универсальная кнопка тулбара */
.tb-btn {
    display: flex;
    align-items: center;
    justify-content: center;
    width: 36px;
    height: 36px;
    background: transparent;
    border: none;
    border-radius: 6px;
    color: rgba(255, 255, 255, 0.55);
    cursor: pointer;
    transition: background 0.2s, color 0.2s;
    flex-shrink: 0;
}
.tb-btn:hover {
    background: rgba(255, 255, 255, 0.15);
    color: #fff;
}
.tb-btn.active {
    background: rgba(255, 255, 255, 0.22);
    color: #fff;
    box-shadow: inset 0 0 0 1px rgba(255, 255, 255, 0.25);
}
.tb-btn svg {
    width: 20px;
    height: 20px;
    pointer-events: none;
}
.tb-btn:disabled {
    opacity: 0.3;
    cursor: not-allowed;
}

/* Зум-контролы: скрыты пока zoom-active не установлен */
.zoom-ctrl {
    display: none;
    width: 30px;
}
body.zoom-active .zoom-ctrl {
    display: flex;
}
body.zoom-active #btn-zoom {
    background: rgba(255, 255, 255, 0.22);
    color: #fff;
    box-shadow: inset 0 0 0 1px rgba(255, 255, 255, 0.25);
}

/* Кнопки навигации */
#prev-page,
#next-page {
    display: flex;
    align-items: center;
    justify-content: center;
    width: 34px;
    height: 34px;
    background: rgba(255, 255, 255, 0.12);
    border: none;
    border-radius: 6px;
    color: #fff;
    cursor: pointer;
    transition: background 0.2s;
}
#prev-page:hover,
#next-page:hover {
    background: rgba(255, 255, 255, 0.25);
}
#prev-page:disabled,
#next-page:disabled {
    opacity: 0.3;
    cursor: not-allowed;
}
#prev-page svg,
#next-page svg {
    width: 16px;
    height: 16px;
}

/* Счётчик страниц */
#page-info {
    display: flex;
    align-items: center;
    gap: 5px;
    font-size: 0.875rem;
    color: rgba(255, 255, 255, 0.9);
    min-width: 90px;
    justify-content: center;
}
#page-input {
    width: 44px;
    text-align: center;
    background: rgba(255, 255, 255, 0.15);
    border: 1px solid rgba(255, 255, 255, 0.3);
    border-radius: 4px;
    color: #fff;
    font-size: 0.875rem;
    padding: 3px 4px;
    -moz-appearance: textfield;
}
#page-input::-webkit-inner-spin-button,
#page-input::-webkit-outer-spin-button {
    display: none;
}
#page-input:focus {
    outline: none;
    border-color: rgba(255, 255, 255, 0.65);
    background: rgba(255, 255, 255, 0.22);
}
#page-sep,
#page-count {
    color: rgba(255, 255, 255, 0.5);
}

/* Кнопки режима */
.mode-btn {
    display: flex;
    align-items: center;
    justify-content: center;
    width: 36px;
    height: 36px;
    background: transparent;
    border: none;
    border-radius: 6px;
    color: rgba(255, 255, 255, 0.55);
    cursor: pointer;
    transition: background 0.2s, color 0.2s;
}
.mode-btn:hover {
    background: rgba(255, 255, 255, 0.15);
    color: #fff;
}
.mode-btn.active {
    background: rgba(255, 255, 255, 0.22);
    color: #fff;
    box-shadow: inset 0 0 0 1px rgba(255, 255, 255, 0.25);
}
.mode-btn svg {
    width: 20px;
    height: 20px;
}

/* ===========================
   МЕНЮ КАЧЕСТВА
   =========================== */
#quality-wrap {
    position: relative;
}

#quality-menu {
    display: none;
    position: absolute;
    top: calc(100% + 8px);
    left: 0;
    background: rgba(30, 55, 15, 0.96);
    backdrop-filter: blur(20px);
    -webkit-backdrop-filter: blur(20px);
    border: 1px solid rgba(255, 255, 255, 0.12);
    border-radius: 10px;
    overflow: hidden;
    min-width: 220px;
    z-index: 200;
    box-shadow: 0 12px 40px rgba(0, 0, 0, 0.6);
}
#quality-menu.open {
    display: block;
}

.quality-option {
    display: flex;
    flex-direction: column;
    align-items: flex-start;
    width: 100%;
    padding: 10px 14px;
    background: transparent;
    border: none;
    color: #fff;
    cursor: pointer;
    text-align: left;
    transition: background 0.15s;
    gap: 2px;
}
.quality-option:not(:last-child) {
    border-bottom: 1px solid rgba(255, 255, 255, 0.07);
}
.quality-option:hover {
    background: rgba(255, 255, 255, 0.1);
}
.quality-option.active {
    background: rgba(255, 255, 255, 0.12);
}
.quality-option.active .quality-title::after {
    content: ' ✓';
    color: #7ec850;
}
.quality-title {
    font-size: 0.875rem;
    font-weight: 600;
    color: #fff;
}
.quality-option small {
    font-size: 0.6875rem;
    color: rgba(255, 255, 255, 0.5);
    line-height: 1.3;
}

/* ===========================
   ОБЛАСТЬ ПРОСМОТРА
   =========================== */
#viewer {
    position: fixed;
    top: 76px;
    left: 0;
    right: 0;
    bottom: 0;
    background: #1a1a1a;
}

/* Flipbook-режимы: прозрачный viewer, видно деревянный фон */
body.mode-single #viewer,
body.mode-double #viewer {
    background: transparent;
    display: flex;
    justify-content: center;
    align-items: center;
    overflow: hidden;
}

/* Контейнер StPageFlip */
#flip-container {
    transform-origin: center center;
    transition: transform 0.2s ease;
}

/* Слот страницы для StPageFlip (PDF и WebP) */
.pdf-page-slot,
.webp-page-slot {
    background: #ffffff;
    display: flex;
    align-items: center;
    justify-content: center;
    overflow: hidden;
    box-shadow: -4px 4px 20px rgba(0,0,0,0.5), 4px 4px 20px rgba(0,0,0,0.3);
}
/* PDF: canvas рендерится точно под размер слота, ограничиваем максимум */
.pdf-page-slot canvas {
    display: block;
    max-width: 100%;
    max-height: 100%;
    object-fit: contain;
}
/* WebP: img растягиваем на весь слот, object-fit центрирует изображение внутри */
.webp-page-slot img {
    display: block;
    width: 100%;
    height: 100%;
    object-fit: contain;
}


/* Canvas / img страницы */
.page-canvas {
    background: #fff;
    box-shadow: 0 4px 28px rgba(0, 0, 0, 0.65);
    display: block;
}

/* WebP в scroll/landscape */
body.mode-scroll img.page-canvas {
    max-width: calc(100vw - 40px);
}
body.mode-landscape img.page-canvas {
    max-height: calc(100vh - 116px);
}

/* Плейсхолдер до рендера */
.page-placeholder {
    background: #252525;
    display: flex;
    align-items: center;
    justify-content: center;
    color: rgba(255, 255, 255, 0.12);
    font-size: 0.8125rem;
    font-family: Arial, sans-serif;
}

/* --- Режим: горизонтальный скролл --- */
body.mode-landscape #viewer {
    top: 0;
    display: flex;
    flex-direction: row;
    align-items: center;
    overflow-x: auto;
    overflow-y: hidden;
    gap: 20px;
    padding: 80px 24px 20px;
    scroll-behavior: smooth;
    scrollbar-width: thin;
    scrollbar-color: #3a3a3a #1a1a1a;
}
body.mode-landscape #viewer::-webkit-scrollbar { height: 5px; }
body.mode-landscape #viewer::-webkit-scrollbar-track { background: #1a1a1a; }
body.mode-landscape #viewer::-webkit-scrollbar-thumb { background: #3a3a3a; border-radius: 3px; }

/* --- Режим: вертикальный скролл --- */
body.mode-scroll #viewer {
    top: 0;
    display: flex;
    flex-direction: column;
    align-items: center;
    overflow-y: auto;
    overflow-x: hidden;
    gap: 20px;
    padding: 80px 0 40px;
    scroll-behavior: smooth;
    scrollbar-width: thin;
    scrollbar-color: #3a3a3a #1a1a1a;
}
body.mode-scroll #viewer::-webkit-scrollbar { width: 5px; }
body.mode-scroll #viewer::-webkit-scrollbar-track { background: #1a1a1a; }
body.mode-scroll #viewer::-webkit-scrollbar-thumb { background: #3a3a3a; border-radius: 3px; }

/* Обёртка страницы (скролл-режимы) */
.page-wrap {
    flex-shrink: 0;
}

/* ===========================
   СЛОЙ ПЕРЕТАСКИВАНИЯ ПРИ ЗУМЕ
   =========================== */
#zoom-drag-layer {
    display: none;
    position: fixed;
    top: 76px;
    left: 0;
    right: 0;
    bottom: 0;
    cursor: grab;
    z-index: 50;
    user-select: none;
    -webkit-user-select: none;
}
#zoom-drag-layer.dragging {
    cursor: grabbing;
}

/* ===========================
   ГАЛЕРЕЯ СТРАНИЦ
   =========================== */
#gallery-overlay {
    position: fixed;
    inset: 0;
    background: rgba(10, 10, 10, 0.92);
    backdrop-filter: blur(8px);
    -webkit-backdrop-filter: blur(8px);
    z-index: 300;
    overflow-y: auto;
    padding: 60px 20px 30px;
    display: none;
    scrollbar-width: thin;
    scrollbar-color: #3a3a3a transparent;
}
#gallery-overlay.open {
    display: block;
}
#gallery-overlay::-webkit-scrollbar { width: 5px; }
#gallery-overlay::-webkit-scrollbar-thumb { background: #3a3a3a; border-radius: 3px; }

.gallery-close {
    position: fixed;
    top: 16px;
    right: 24px;
    width: 40px;
    height: 40px;
    background: rgba(255, 255, 255, 0.1);
    border: none;
    border-radius: 50%;
    color: #fff;
    font-size: 1.125rem;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    transition: background 0.2s;
    z-index: 301;
}
.gallery-close:hover {
    background: rgba(255, 255, 255, 0.22);
}

.gallery-grid {
    display: grid;
    grid-template-columns: repeat(auto-fill, minmax(120px, 1fr));
    gap: 12px;
    max-width: 1200px;
    margin: 0 auto;
}

.gallery-item {
    cursor: pointer;
    border-radius: 6px;
    overflow: hidden;
    border: 2px solid transparent;
    transition: border-color 0.2s, transform 0.15s;
    background: #252525;
}
.gallery-item:hover {
    border-color: rgba(255, 255, 255, 0.35);
    transform: scale(1.03);
}
.gallery-item.current {
    border-color: #4a8c26;
}

.gallery-thumb-wrap {
    width: 100%;
    aspect-ratio: 210 / 297;
    overflow: hidden;
    display: flex;
    align-items: center;
    justify-content: center;
    background: #1e1e1e;
}
.gallery-thumb {
    width: 100%;
    height: 100%;
    object-fit: cover;
    display: block;
}
canvas.gallery-thumb {
    width: 100% !important;
    height: 100% !important;
    object-fit: contain;
}

.gallery-num {
    display: block;
    text-align: center;
    font-size: 0.6875rem;
    color: rgba(255, 255, 255, 0.45);
    padding: 4px 0;
}

/* ===========================
   ТОСТ
   =========================== */
#toast {
    position: fixed;
    bottom: 24px;
    left: 50%;
    transform: translateX(-50%) translateY(16px);
    background: rgba(30, 55, 15, 0.95);
    border: 1px solid rgba(255, 255, 255, 0.12);
    backdrop-filter: blur(12px);
    color: #fff;
    font-size: 0.8125rem;
    padding: 10px 18px;
    border-radius: 8px;
    max-width: 360px;
    text-align: center;
    opacity: 0;
    transition: opacity 0.3s, transform 0.3s;
    pointer-events: none;
    z-index: 400;
    box-shadow: 0 6px 24px rgba(0, 0, 0, 0.5);
}
#toast.visible {
    opacity: 1;
    transform: translateX(-50%) translateY(0);
}

/* ===========================
   ДИАЛОГ ПРОДОЛЖЕНИЯ ЧТЕНИЯ
   =========================== */
#resume-dialog {
    display: none;
    position: fixed;
    inset: 0;
    z-index: 250;
    align-items: center;
    justify-content: center;
}
#resume-dialog.open {
    display: flex;
}
#resume-card {
    background: rgba(22, 40, 12, 0.97);
    border: 1px solid rgba(255, 255, 255, 0.12);
    border-radius: 16px;
    padding: 32px 28px 24px;
    max-width: 360px;
    width: calc(100% - 48px);
    box-shadow: 0 24px 64px rgba(0, 0, 0, 0.7);
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 16px;
    text-align: center;
}
#resume-title {
    font-size: 1.0625rem;
    font-weight: 600;
    color: #fff;
}
#resume-text {
    font-size: 0.875rem;
    color: rgba(255, 255, 255, 0.6);
    line-height: 1.5;
}
#resume-text strong {
    color: rgba(255, 255, 255, 0.9);
}
#resume-actions {
    display: flex;
    gap: 10px;
    width: 100%;
    margin-top: 4px;
}
#resume-continue {
    flex: 1;
    padding: 10px 16px;
    background: #2d5016;
    border: none;
    border-radius: 8px;
    color: #fff;
    font-size: 0.875rem;
    font-weight: 600;
    cursor: pointer;
    transition: background 0.2s;
}
#resume-continue:hover {
    background: #3d6b1f;
}
#resume-start {
    flex: 1;
    padding: 10px 16px;
    background: rgba(255, 255, 255, 0.08);
    border: 1px solid rgba(255, 255, 255, 0.12);
    border-radius: 8px;
    color: rgba(255, 255, 255, 0.75);
    font-size: 0.875rem;
    cursor: pointer;
    transition: background 0.2s;
}
#resume-start:hover {
    background: rgba(255, 255, 255, 0.15);
}

/* ===========================
   ОВЕРЛЕЙ ЗАГРУЗКИ
   =========================== */
#loading-overlay {
    position: fixed;
    inset: 0;
    background: rgba(26, 26, 26, 0.9);
    display: flex;
    align-items: center;
    justify-content: center;
    z-index: 200;
    backdrop-filter: blur(6px);
}
#loading-overlay.hidden {
    display: none;
}
#loading-box {
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 18px;
}
#loading-spinner {
    width: 42px;
    height: 42px;
    border: 3px solid rgba(255, 255, 255, 0.08);
    border-top-color: #4a8c26;
    border-radius: 50%;
    animation: spin 0.75s linear infinite;
}
@keyframes spin {
    to { transform: rotate(360deg); }
}
#loading-text {
    font-size: 0.9375rem;
    color: rgba(255, 255, 255, 0.6);
    letter-spacing: 0.02em;
}

/* Скрываем бейдж сессии — в читалке он перекрывает контент */
#session-badge {
    display: none !important;
}

/* ===========================
   МОБИЛЬНЫЙ АДАПТИВ
   =========================== */
@media (max-width: 600px) {
    /* Компактный тулбар */
    #toolbar {
        top: 8px;
        left: 8px;
        right: 8px;
        height: 46px;
        padding: 0 6px;
        gap: 4px;
    }
    #tb-left, #tb-right {
        gap: 1px;
    }
    .tb-btn,
    .mode-btn {
        width: 32px;
        height: 32px;
    }
    .tb-btn svg,
    .mode-btn svg {
        width: 18px;
        height: 18px;
    }
    #prev-page,
    #next-page {
        width: 30px;
        height: 30px;
    }
    #page-info {
        gap: 3px;
        font-size: 0.8125rem;
        min-width: 76px;
    }
    #page-input {
        width: 36px;
        font-size: 0.8125rem;
    }

    /* viewer начинается ниже компактного тулбара */
    #viewer {
        top: 62px;
    }
    #zoom-drag-layer {
        top: 62px;
    }
}
