/* Penița Dreptului — Shared PDF Viewer styles
 * Used by /articles/<slug>/ and /books/<slug>/.
 * Replaces ~300 lines of duplicated inline <style> per template.
 */

/* ============================================================
 * Wrapper
 * ============================================================ */
.pdf-viewer-wrapper {
    /* Full-bleed: вырываем PDF viewer на полную ширину viewport,
     * игнорируя max-width родительского article-container/book-container.
     * Это даёт пользователю реально полную ширину для чтения. */
    width: 100vw;
    margin-left: calc(50% - 50vw);
    margin-right: calc(50% - 50vw);
    position: relative;
}

/* ============================================================
 * Reading progress (top of page)
 * ============================================================ */
.reading-progress {
    position: fixed;
    top: 0;
    left: 0;
    width: 0%;
    height: 4px;
    background: linear-gradient(90deg, #6B8E23, #2ecc71);
    box-shadow: 0 0 8px rgba(107, 142, 35, 0.5);
    z-index: 1000;
    transition: width 0.35s ease-out;
}

/* ============================================================
 * Dark mode (for PDF reader only — toggled via button)
 * ============================================================ */
.pdf-viewer-container.pdf-dark {
    background: #1a1a1a;
}
.pdf-viewer-container.pdf-dark .pdf-page {
    background: #1a1a1a;
}
/* Invert the canvas (PDF rendering) — keeps blacks/whites swapped, hue-rotate restores colors */
.pdf-viewer-container.pdf-dark canvas {
    filter: invert(0.92) hue-rotate(180deg);
}
/* Don't invert the text layer (it's transparent overlay for selection/search; inverting breaks selection visibility) */
.pdf-viewer-container.pdf-dark .textLayer {
    mix-blend-mode: normal;
    color: transparent;
}
/* Dark-mode button stays in same place but toggles icon (moon ↔ sun) via JS */

/* ============================================================
 * Bookmarks dropdown panel
 * ============================================================ */
.bookmarks-wrapper {
    position: relative;
}
.bookmarks-btn {
    position: relative;
}
.bookmark-count {
    position: absolute;
    top: -4px;
    right: -4px;
    background: #e74c3c;
    color: white;
    border-radius: 10px;
    font-size: 0.7rem;
    font-weight: 700;
    min-width: 18px;
    height: 18px;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    padding: 0 5px;
    line-height: 1;
}
.pdf-controls .bookmarks-panel {
    display: none;
    position: absolute;
    top: calc(100% + 8px);
    right: 0;
    background: #fff;
    border-radius: 8px;
    box-shadow: 0 8px 24px rgba(0, 0, 0, 0.25);
    min-width: 240px;
    max-width: 320px;
    max-height: 400px;
    overflow-y: auto;
    z-index: 200;
    color: #333;
}
.pdf-controls .bookmarks-panel.is-open { display: block; }
.pdf-controls .bookmarks-panel-header {
    padding: 10px 12px;
    border-bottom: 1px solid #e0e5d1;
}
/* Все кнопки внутри dropdown-панели — перебиваем глобальный
 * .pdf-controls button (зелёный градиент + white text + lift on hover).
 * :hover у глобального правила имеет специфичность 0,0,2,1 — поэтому ниже
 * нужны парные :hover-правила, иначе hover-state ломает читаемость. */
.pdf-controls .bookmarks-panel button,
.pdf-controls .bookmarks-panel button:hover,
.pdf-controls .bookmarks-panel button:focus,
.pdf-controls .bookmarks-panel button:focus-visible,
.pdf-controls .bookmarks-panel button:active {
    transform: none;
    box-shadow: none;
    text-shadow: none;
    outline: none;
}
.pdf-controls .bookmarks-panel button {
    background: transparent;
    color: #333;
    border: none;
    padding: 8px 10px;
    border-radius: 4px;
    cursor: pointer;
    font-size: 0.9rem;
    font-weight: 400;
    line-height: 1.4;
    transition: background 0.15s, color 0.15s;
}
.pdf-controls .bookmarks-panel button:hover { background: transparent; color: #333; }
.pdf-controls .bookmarks-panel .bookmark-toggle {
    width: 100%;
    background: #6B8E23;
    color: #fff;
    padding: 10px 14px;
    border-radius: 6px;
    font-size: 0.92rem;
    font-weight: 600;
    text-align: left;
    display: flex;
    align-items: center;
    gap: 8px;
}
.pdf-controls .bookmarks-panel .bookmark-toggle.is-active { background: #c0392b; }
.pdf-controls .bookmarks-panel .bookmark-toggle:hover { background: #556B2F; color: #fff; opacity: 1; }
.bookmarks-empty {
    padding: 18px 14px;
    color: #888;
    font-size: 0.85rem;
    text-align: center;
    font-style: italic;
    line-height: 1.5;
}
.bookmarks-list {
    list-style: none;
    margin: 0;
    padding: 6px 0;
}
.bookmarks-list li {
    display: flex;
    align-items: center;
    padding: 0 6px;
}
.pdf-controls .bookmarks-panel .bookmark-item {
    flex: 1;
    text-align: left;
    color: #2c3e50;
    font-weight: 500;
    display: flex;
    align-items: center;
    gap: 6px;
}
.pdf-controls .bookmarks-panel .bookmark-item:hover { background: #f0f5e0; color: #556B2F; }
.pdf-controls .bookmarks-panel .bookmark-item i { color: #6B8E23; flex-shrink: 0; }
.pdf-controls .bookmarks-panel .bookmark-remove {
    color: #999;
    padding: 6px 8px;
    flex-shrink: 0;
}
.pdf-controls .bookmarks-panel .bookmark-remove:hover { color: #c0392b; background: #fdecec; }

/* ============================================================
 * Quote button (floating, on text selection)
 * ============================================================ */
.pdf-quote-btn {
    position: fixed;            /* viewport-relative — работает в fullscreen */
    background: #2c3e50;
    color: white;
    border-radius: 6px;
    box-shadow: 0 4px 12px rgba(0, 0, 0, 0.3);
    z-index: 100001;            /* выше всего, в т.ч. red exit-btn (100000) */
    display: none;
    white-space: nowrap;
    overflow: hidden;
}
.pdf-quote-btn.is-visible { display: inline-flex; }
.pdf-quote-btn-action {
    background: transparent;
    color: white;
    border: none;
    padding: 8px 14px;
    font-size: 0.85rem;
    font-weight: 600;
    cursor: pointer;
    display: inline-flex;
    align-items: center;
    gap: 6px;
}
.pdf-quote-btn-action:hover { background: #34495e; }
.pdf-quote-btn-action + .pdf-quote-btn-action { border-left: 1px solid rgba(255, 255, 255, 0.2); }
.pdf-quote-btn-action i { font-size: 0.9rem; }

/* ============================================================
 * Annotations (highlights on textLayer spans + panel items)
 * ============================================================ */
.textLayer .annotation-highlight {
    background: rgba(255, 235, 59, 0.45);
    border-radius: 2px;
    cursor: pointer;
    transition: background 0.15s;
}
.textLayer .annotation-highlight.annotation-color-yellow { background: rgba(255, 235, 59, 0.45); }
.textLayer .annotation-highlight.annotation-color-green  { background: rgba(46, 204, 113, 0.45); }
.textLayer .annotation-highlight.annotation-color-blue   { background: rgba(52, 152, 219, 0.45); }
.textLayer .annotation-highlight.annotation-color-pink   { background: rgba(233, 30, 99, 0.45); }
.textLayer .annotation-highlight:hover { background: rgba(255, 193, 7, 0.65); }
/* In dark mode, pump the saturation so highlights remain visible after invert */
.pdf-viewer-container.pdf-dark .textLayer .annotation-highlight { mix-blend-mode: difference; }

.pdf-controls .bookmarks-panel .annotation-item {
    flex-direction: column;
    align-items: flex-start;
    gap: 4px;
    padding: 10px 12px;
}
.pdf-controls .bookmarks-panel .annotation-item .annotation-snippet {
    display: block;
    width: 100%;
    color: #6c7a89;
    font-size: 0.82rem;
    font-style: italic;
    margin-top: 4px;
    overflow: hidden;
    text-overflow: ellipsis;
    line-height: 1.4;
    font-weight: 400;
}
.pdf-controls .bookmarks-panel .annotation-item.annotation-color-yellow i { color: #f1c40f; }
.pdf-controls .bookmarks-panel .annotation-item.annotation-color-green  i { color: #2ecc71; }
.pdf-controls .bookmarks-panel .annotation-item.annotation-color-blue   i { color: #3498db; }
.pdf-controls .bookmarks-panel .annotation-item.annotation-color-pink   i { color: #e91e63; }
/* Page label inline with icon */
.pdf-controls .bookmarks-panel .annotation-item > i + * { display: inline; }

/* ============================================================
 * Controls bar
 * ============================================================ */
.pdf-controls {
    background: #2c3e50;
    padding: 10px 8px;
    display: flex;
    justify-content: center;
    align-items: center;
    gap: 10px;
    flex-wrap: wrap;
    position: sticky;
    top: 0;
    z-index: 100;
    box-shadow: 0 2px 10px rgba(0, 0, 0, 0.3);
}

.pdf-controls button {
    padding: 10px 20px;
    background: #6B8E23;
    color: white;
    border: none;
    border-radius: 5px;
    cursor: pointer;
    font-size: 14px;
    display: flex;
    align-items: center;
    gap: 5px;
    transition: background 0.3s ease, transform 0.2s ease, box-shadow 0.2s ease;
    font-weight: 500;
}
.pdf-controls button:hover {
    background: #556B2F;
    transform: translateY(-2px);
    box-shadow: 0 4px 8px rgba(0, 0, 0, 0.2);
}
.pdf-controls button:disabled {
    background: #7f8c8d;
    cursor: not-allowed;
    transform: none;
}
.pdf-controls button:focus-visible {
    outline: 2px solid #fff;
    outline-offset: 2px;
}

/* Page navigation group */
.page-navigation {
    display: flex;
    align-items: center;
    gap: 15px;
    background: rgba(255, 255, 255, 0.1);
    padding: 5px 15px;
    border-radius: 25px;
}
.page-info {
    font-weight: bold;
    color: white;
    font-size: 15px;
}

/* Zoom group */
.zoom-controls {
    display: flex;
    gap: 10px;
    align-items: center;
    background: rgba(255, 255, 255, 0.1);
    padding: 5px 10px;
    border-radius: 25px;
}
.zoom-controls select {
    padding: 8px;
    border: none;
    border-radius: 4px;
    background: white;
    color: #2c3e50;
    font-weight: 500;
}
.zoom-controls button {
    padding: 8px 12px;
    background: transparent;
    border: 1px solid white;
}
.zoom-controls button:hover {
    background: rgba(255, 255, 255, 0.2);
}

/* ============================================================
 * Search bar
 * ============================================================ */
.pdf-search {
    display: flex;
    align-items: center;
    gap: 6px;
    background: rgba(255, 255, 255, 0.1);
    padding: 5px 10px;
    border-radius: 25px;
    transition: background 0.2s ease;
}
.pdf-search.is-open {
    background: rgba(255, 255, 255, 0.2);
}

/* The toggle button is always visible; everything else hides until is-open */
.pdf-search [data-pdf-search-bar] {
    display: none;
    align-items: center;
    gap: 6px;
}
.pdf-search.is-open [data-pdf-search-bar] {
    display: flex;
}

.pdf-search [data-pdf-search-input] {
    padding: 8px 12px;
    border: none;
    border-radius: 4px;
    background: white;
    color: #2c3e50;
    font-size: 14px;
    font-family: inherit;
    width: 200px;
    min-width: 0;
}
.pdf-search [data-pdf-search-input]:focus {
    outline: 2px solid #6B8E23;
    outline-offset: 1px;
}

.pdf-search [data-pdf-match-counter] {
    color: white;
    font-size: 13px;
    font-weight: 500;
    min-width: 50px;
    text-align: center;
    white-space: nowrap;
}
.pdf-search [data-pdf-match-counter].is-empty {
    color: #f39c12;
    font-style: italic;
}

.pdf-search button {
    padding: 8px 12px;
    background: transparent;
    border: 1px solid white;
}
.pdf-search button:hover {
    background: rgba(255, 255, 255, 0.2);
    transform: none;
    box-shadow: none;
}
.pdf-search [data-action="close-search"] {
    font-size: 18px;
    line-height: 1;
    padding: 4px 10px;
}

/* ============================================================
 * PDF viewer container + page
 * ============================================================ */
.pdf-viewer-container {
    width: 100%;
    background: #34495e;
    position: relative;
    overflow: auto;
    height: calc(100vh - 150px);
}

[data-pdf-canvas-container] {
    display: flex;
    flex-direction: column;
    align-items: center;
    padding: 10px 0;
    gap: 15px;
    width: 100%;
    /* Slide animation transitions (prefers-reduced-motion respected below) */
    transition: transform 0.25s ease-out, opacity 0.25s ease-out;
    will-change: transform, opacity;
}

.pdf-page {
    background: white;
    box-shadow: 0 10px 30px rgba(0, 0, 0, 0.5);
    margin-bottom: 15px;
    border-radius: 4px;
    overflow: hidden;
    width: auto;
}

.pdf-page canvas {
    display: block;
    max-width: 100%;
    height: auto;
}

/* ============================================================
 * Slide animation between pages
 * ============================================================ */
.slide-leaving-left {
    transform: translateX(-30%);
    opacity: 0;
}
.slide-leaving-right {
    transform: translateX(30%);
    opacity: 0;
}
.slide-entering-left {
    animation: pdf-slide-from-right 0.25s ease-out;
}
.slide-entering-right {
    animation: pdf-slide-from-left 0.25s ease-out;
}

@keyframes pdf-slide-from-right {
    from { transform: translateX(30%); opacity: 0; }
    to   { transform: translateX(0);   opacity: 1; }
}
@keyframes pdf-slide-from-left {
    from { transform: translateX(-30%); opacity: 0; }
    to   { transform: translateX(0);    opacity: 1; }
}

@media (prefers-reduced-motion: reduce) {
    [data-pdf-canvas-container] {
        transition: none !important;
    }
    .slide-leaving-left,
    .slide-leaving-right,
    .slide-entering-left,
    .slide-entering-right {
        transform: none !important;
        opacity: 1 !important;
        animation: none !important;
    }
}

/* ============================================================
 * Text layer (enables search highlights AND text selection)
 * ============================================================ */
.textLayer {
    position: absolute;
    top: 0;
    left: 0;
    overflow: hidden;
    /* opacity: 1 — раньше 0.25 убивало видимость annotation-highlight'ов
     * (0.45 alpha × 0.25 opacity = ~11% — почти не видно). Spans остаются
     * полностью прозрачными через color: transparent ниже. */
    opacity: 1;
    line-height: 1.0;
    pointer-events: auto;
}
.textLayer > span {
    color: transparent;
    position: absolute;
    white-space: pre;
    cursor: text;
    transform-origin: 0% 0%;
}
.textLayer ::selection {
    background: rgba(0, 100, 0, 0.4);
}
.textLayer .highlight {
    background-color: rgba(255, 235, 59, 0.6);
    border-radius: 2px;
    padding: 1px 0;
}
.textLayer .highlight.selected {
    background-color: rgba(255, 152, 0, 0.85);
    box-shadow: 0 0 0 2px rgba(255, 152, 0, 0.85);
}
.textLayer .highlight.appended {
    position: initial;
}
.textLayer .highlight.begin { border-radius: 4px 0 0 4px; }
.textLayer .highlight.end   { border-radius: 0 4px 4px 0; }
.textLayer .highlight.middle { border-radius: 0; }

/* ============================================================
 * Loading & error states
 * ============================================================ */
.pdf-viewer-container .loading {
    text-align: center;
    padding: 50px;
    color: white;
    font-size: 1.2rem;
}
.loading-spinner {
    border: 3px solid rgba(255, 255, 255, 0.3);
    border-top: 3px solid white;
    border-radius: 50%;
    width: 50px;
    height: 50px;
    animation: pdf-spin 1s linear infinite;
    margin: 20px auto;
}
@keyframes pdf-spin {
    0%   { transform: rotate(0deg); }
    100% { transform: rotate(360deg); }
}
.pdf-error-message {
    background: #e74c3c;
    color: white;
    padding: 20px;
    margin: 20px;
    border-radius: 8px;
    text-align: center;
    font-size: 1.1rem;
}

/* ============================================================
 * Touch hint (mobile only)
 * ============================================================ */
.gesture-hint {
    display: none;
    background: #6B8E23;
    color: white;
    padding: 12px;
    text-align: center;
    font-size: 0.9rem;
}
@media (max-width: 768px) {
    .gesture-hint { display: block; }
}

/* ============================================================
 * Fullscreen button (always visible, native API + iOS pseudo)
 * ============================================================ */
.fullscreen-btn {
    position: fixed;
    bottom: 22px;
    right: 22px;
    width: 52px;
    height: 52px;
    padding: 0;
    background: #2c3e50;
    color: white;
    border: none;
    border-radius: 50%;
    cursor: pointer;
    box-shadow: 0 6px 18px rgba(0, 0, 0, 0.35);
    z-index: 10000;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 1.1rem;
    transition: background .2s ease, transform .2s ease;
}
.fullscreen-btn:hover { background: #34495e; transform: translateY(-2px); }
.fullscreen-btn:focus-visible { outline: 3px solid #6B8E23; outline-offset: 2px; }

/* Hide PDF fullscreen-btn (and sticky pdf-controls) while mobile nav menu
 * is open, otherwise they sit above the menu due to their high z-index. */
body.menu-open .fullscreen-btn,
body.menu-open .pdf-controls {
    display: none !important;
}

/* Fullscreen mode (native + iOS pseudo fallback).
 * Применяется к ВСЕМУ wrapper (controls + viewer + dropdown panels),
 * чтобы все фичи (bookmarks, annotations, dark mode, zoom, search) работали в fullscreen. */
.pdf-viewer-wrapper.is-pseudo-fullscreen {
    position: fixed !important;
    inset: 0 !important;
    width: 100vw !important;
    height: 100vh !important;
    height: 100dvh !important;
    margin: 0 !important;
    z-index: 9998;
    background: #34495e;
    display: flex;
    flex-direction: column;
}
.pdf-viewer-wrapper:fullscreen,
.pdf-viewer-wrapper:-webkit-full-screen {
    margin: 0;
    background: #34495e;
    display: flex;
    flex-direction: column;
    width: 100vw;
    height: 100vh;
}
/* В fullscreen контейнер с PDF растягивается на оставшееся место под controls */
.pdf-viewer-wrapper.is-pseudo-fullscreen .pdf-viewer-container,
.pdf-viewer-wrapper:fullscreen .pdf-viewer-container,
.pdf-viewer-wrapper:-webkit-full-screen .pdf-viewer-container {
    flex: 1 1 auto;
    height: auto;
    min-height: 0;
}
/* Dropdown-панели должны висеть выше viewer, но НИЖЕ красной exit-кнопки */
.pdf-viewer-wrapper.is-pseudo-fullscreen .pdf-controls .bookmarks-panel,
.pdf-viewer-wrapper:fullscreen .pdf-controls .bookmarks-panel,
.pdf-viewer-wrapper:-webkit-full-screen .pdf-controls .bookmarks-panel {
    z-index: 9999;
}
body.pdf-fullscreen-open {
    overflow: hidden;
}

/* === Exit button in pseudo-fullscreen mode (red, large, prominent) ===
 * On mobile (esp. iOS) the regular .fullscreen-btn was hard to find/tap
 * inside the immersive viewer. Make it impossible to miss. */
body.pdf-fullscreen-open .fullscreen-btn {
    position: fixed !important;
    /* Top-right corner, away from thumb-zone toolbars */
    top: max(18px, env(safe-area-inset-top, 18px)) !important;
    right: max(18px, env(safe-area-inset-right, 18px)) !important;
    bottom: auto !important;
    width: 56px !important;
    height: 56px !important;
    background: #c0392b !important;        /* Red — universally recognized as "close/exit" */
    color: #fff !important;
    z-index: 100000 !important;             /* Above viewer (9998) and any other UI */
    box-shadow: 0 8px 24px rgba(0, 0, 0, 0.55) !important;
    border: 2px solid rgba(255, 255, 255, 0.9) !important;
}
body.pdf-fullscreen-open .fullscreen-btn:hover {
    background: #a82817 !important;
    transform: scale(1.05) !important;
}
body.pdf-fullscreen-open .fullscreen-btn i {
    font-size: 1.3rem !important;
}

/* Same for native fullscreen on wrapper: red exit button should be on top */
.pdf-viewer-wrapper:fullscreen .fullscreen-btn,
.pdf-viewer-wrapper:-webkit-full-screen .fullscreen-btn {
    position: fixed !important;
    top: max(18px, env(safe-area-inset-top, 18px)) !important;
    right: max(18px, env(safe-area-inset-right, 18px)) !important;
    bottom: auto !important;
    width: 56px !important;
    height: 56px !important;
    background: #c0392b !important;
    color: #fff !important;
    z-index: 100000 !important;
    box-shadow: 0 8px 24px rgba(0, 0, 0, 0.55) !important;
    border: 2px solid rgba(255, 255, 255, 0.9) !important;
}

/* ============================================================
 * Mobile optimizations
 * ============================================================ */
@media (max-width: 768px) {
    .pdf-controls {
        padding: 10px 5px;
        gap: 8px;
    }
    .pdf-controls button {
        padding: 8px 12px;
        font-size: 12px;
    }
    .page-navigation { padding: 3px 10px; }
    .zoom-controls { width: 100%; justify-content: center; }
    .pdf-viewer-container { height: calc(100vh - 250px); }
    [data-pdf-canvas-container] { padding: 15px 10px; }
    .page-info { font-size: 13px; }
    .pdf-search [data-pdf-search-input] { width: 140px; }
}

@media (max-width: 480px) {
    .pdf-controls {
        flex-direction: column;
        position: relative;
        padding: 12px 10px;
        gap: 10px;
    }
    .page-navigation,
    .zoom-controls,
    .pdf-search {
        width: 100%;
        justify-content: center;
    }
    /* Search bar opens as a vertical stack on narrow screens so the
       close button (×) doesn't get cut off the right edge of viewport. */
    .pdf-search.is-open {
        flex-direction: column;
        align-items: stretch;
        gap: 8px;
        padding: 8px 10px;
    }
    .pdf-search.is-open [data-pdf-search-bar] {
        flex-wrap: wrap;
        justify-content: center;
        gap: 6px;
        width: 100%;
    }
    .pdf-search.is-open [data-pdf-search-input] {
        flex: 1 1 100%;            /* Input takes full width on its own row */
        width: 100%;
        min-width: 0;
        font-size: 16px;            /* Prevents iOS zoom-on-focus */
    }
    .pdf-search.is-open [data-pdf-match-counter] {
        flex: 1 1 auto;
        min-width: 70px;
        font-size: 12px;
    }
    .pdf-search.is-open [data-action="prev-match"],
    .pdf-search.is-open [data-action="next-match"],
    .pdf-search.is-open [data-action="close-search"] {
        flex: 0 0 auto;
        padding: 8px 14px;
        min-width: 44px;            /* WCAG min tap target size */
        min-height: 44px;
    }
    .pdf-search.is-open [data-action="close-search"] {
        background: rgba(192, 57, 43, 0.85);   /* Reddish — clearly "close" */
        border-color: rgba(255, 255, 255, 0.6);
    }
}
