/* Page Wrapper - Inline document viewing (Viewer UI only) */

/* ========================================
   Page Wrapper & Container
   ======================================== */
.page-wrapper {
    position: fixed;
    top: 0;
    left: 280px; /* After left sidebar */
    right: 0;
    bottom: 0;
    z-index: 50;
    overflow-y: scroll;
    overflow-x: hidden;
    -webkit-overflow-scrolling: touch;
    overscroll-behavior: contain;
    scroll-behavior: smooth;
    display: none;
    background: transparent;
    scrollbar-width: none; /* Firefox */
    -ms-overflow-style: none; /* IE/Edge */
}

/* Hide scrollbar for Chrome, Safari, Opera */
.page-wrapper::-webkit-scrollbar {
    display: none;
}

/* Ensure page wrapper receives focus for keyboard scrolling */
.page-wrapper:focus {
    outline: none;
}

.page-wrapper.visible {
    display: block;
}

/* Adjust when right sidebar is open */
.page-wrapper.with-right-sidebar {
    right: 380px;  /* Chapter sidebar (340px) + margin (20px) + gap (20px) */
}

.page-wrapper > .page {
    position: relative;  /* For drag handle positioning on mobile */
    background: rgba(30, 30, 30, 0.85);
    backdrop-filter: blur(20px);
    -webkit-backdrop-filter: blur(20px);
    border: 1px solid rgba(255, 255, 255, 0.1);
    border-radius: 16px;
    box-shadow: 0 0 4px inset rgba(100, 100, 100, 0.2), 0 0 56px inset rgba(100, 100, 100, 0.1);
    transform: translateY(100%);
    opacity: 0;
    transition: transform 0.4s ease, opacity 0.3s ease;
    max-width: 800px;
    margin: 20px auto;  /* Top/bottom margins align with sidebar at scroll extremes */
    padding: 24px;
    counter-reset: section;
    min-height: calc(100vh - 40px);  /* Full height minus margins */
}

.page-wrapper.visible > .page {
    transform: translateY(0);
    opacity: 1;
    transition: transform 0.5s cubic-bezier(.17,0,.01,1.01), opacity 0.3s ease;
}

/* ========================================
   Page Header
   ======================================== */
.page .page-header {
    display: flex;
    flex-direction: row;
    align-items: center;
    gap: 16px;
    margin-bottom: 1.5rem;
    padding-bottom: 1rem;
    border-bottom: 1px solid rgba(255, 255, 255, 0.1);
}

.page .page-title {
    font-size: 1.8em;
    font-weight: bold;
    margin: 0;
    color: white;
}

.page .doc-type-tag {
    font-family: CMSerif;
    display: inline-block;
    font-size: 0.7rem;
    font-variant: small-caps;
    letter-spacing: 0.1em;
    color: rgba(160, 218, 0, 0.9);
    border-radius: 4px;
    padding: 4px 12px;
    white-space: nowrap;

    background: rgba(22, 131, 0, 0.337);
    border: none;
    border-top: 1px solid rgba(255, 255, 255, 0.15);
    box-shadow: 0 0 32px inset rgba(255, 255, 255, 0.05), 0 0 3px inset rgba(255, 255, 255, 0.1);
}

.page .doc-type-tag:empty {
    display: none;
}

.page .page-header-actions {
    margin-left: auto;
    display: flex;
    align-items: center;
    gap: 6px;
}

.page .theme-btn {
    width: 32px;
    height: 32px;
    display: flex;
    align-items: center;
    justify-content: center;
    background: transparent;
    border: none;
    border-radius: 6px;
    cursor: pointer;
    color: rgba(255, 255, 255, 0.4);
    transition: all 0.2s ease;
}

.page .theme-btn:hover {
    color: rgba(255, 255, 255, 0.8);
    background: rgba(255, 255, 255, 0.1);
}

.page .theme-btn.active {
    color: rgba(160, 218, 0, 0.9);
}

.page .theme-btn svg {
    width: 18px;
    height: 18px;
}

.page .edit-doc-btn {
    width: 32px;
    height: 32px;
    display: flex;
    align-items: center;
    justify-content: center;
    color: rgba(255, 255, 255, 0.4);
    text-decoration: none;
    border: none;
    background: transparent;
    border-radius: 6px;
    transition: all 0.2s ease;
}

.page .edit-doc-btn:hover {
    color: rgba(255, 255, 255, 0.8);
    background: rgba(255, 255, 255, 0.1);
}

.page .edit-doc-btn svg {
    width: 18px;
    height: 18px;
}

/* Back button - for returning from document on narrow screens */
.page .page-back-btn {
    display: none;
    align-items: center;
    justify-content: center;
    width: 44px;
    height: 44px;
    min-width: 44px;  /* iOS touch target minimum */
    min-height: 44px;
    background: none;
    border: none;
    color: rgba(255, 255, 255, 0.6);
    cursor: pointer;
    border-radius: 8px;
    transition: all 0.2s ease;
    flex-shrink: 0;
    touch-action: manipulation;  /* Prevent double-tap zoom delay */
    -webkit-tap-highlight-color: rgba(255, 255, 255, 0.1);
    position: relative;
    z-index: 10;
}

/* Ensure icon inside button is clickable */
.page .page-back-btn * {
    pointer-events: none;
}

.page .page-back-btn:hover {
    color: white;
    background: rgba(255, 255, 255, 0.1);
}

.page .page-back-btn svg {
    width: 24px;
    height: 24px;
}

/* Light mode back button */
.page-wrapper.theme-light .page-back-btn {
    color: rgba(0, 0, 0, 0.5);
}

.page-wrapper.theme-light .page-back-btn:hover {
    color: rgba(0, 0, 0, 0.8);
    background: rgba(0, 0, 0, 0.05);
}

/* ========================================
   Parser Output Container
   ======================================== */
.page .parser-output {
    line-height: 1.6;
    font-size: 16px;
}

.page .loading-text,
.page .error-text {
    text-align: center;
    padding: 2rem;
    color: rgba(255, 255, 255, 0.5);
}

.page .loading-text {
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 1rem;
}

.page .loading-spinner {
    width: 32px;
    height: 32px;
    border: 2px solid rgba(255, 255, 255, 0.1);
    border-top-color: rgba(160, 218, 0, 0.8);
    border-radius: 50%;
    animation: spin 0.8s linear infinite;
}

@keyframes spin {
    to { transform: rotate(360deg); }
}

.page .error-text {
    color: #ff6b6b;
    background: rgba(255, 100, 100, 0.1);
    border-radius: 8px;
}

/* ========================================
   Sketch Placeholders (page viewer specific)
   ======================================== */
.page .sketch-placeholder {
    position: relative;
    width: 100%;
    background: transparent;
    border-radius: 12px;
    margin: 1.5em 0;
}

.page .sketch-placeholder canvas {
    display: block;
    position: relative;
    z-index: 1;
    pointer-events: auto;
}

.page .sketch-placeholder .sketch-loading {
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
    color: rgba(255, 255, 255, 0.5);
    font-size: 14px;
    display: flex;
    align-items: center;
    gap: 8px;
}

.page .sketch-placeholder .sketch-loading::before {
    content: '';
    width: 16px;
    height: 16px;
    border: 2px solid rgba(255, 255, 255, 0.3);
    border-top-color: rgba(160, 218, 0, 0.8);
    border-radius: 50%;
    animation: sketch-spin 0.8s linear infinite;
}

@keyframes sketch-spin {
    to { transform: rotate(360deg); }
}

.page .sketch-placeholder.loaded .sketch-loading {
    display: none;
}

.page .sketch-placeholder.error {
    border: 2px dashed rgba(255, 100, 100, 0.5);
    display: flex;
    align-items: center;
    justify-content: center;
}

.page .sketch-error {
    color: #ff6b6b;
    font-size: 14px;
}

.sketch-pause-overlay {
    position: absolute;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    background: rgba(0, 0, 0, 0.2);
    display: flex;
    align-items: center;
    justify-content: center;
    cursor: pointer;
    z-index: 10;
}

.sketch-pause-overlay span {
    color: white;
    font-size: 14px;
    padding: 8px 16px;
    background: rgba(0, 0, 0, 0.6);
    border-radius: 4px;
}

.sketch-pause-overlay:hover span {
    background: rgba(0, 0, 0, 0.8);
}

/* ========================================
   Block Context Menu (glassmorphic style matching navbar)
   ======================================== */
.context-menu {
    position: fixed;
    z-index: 1000;
    display: none;
    min-width: 180px;
    font-family: var(--ui-font);
    background: linear-gradient(165deg, rgba(38, 38, 52, 0.9) 0%, rgba(28, 28, 40, 0.88) 100%);
    backdrop-filter: blur(24px) saturate(3);
    -webkit-backdrop-filter: blur(24px) saturate(3);
    border: 1px solid rgba(255, 255, 255, 0.12);
    border-top: 1px solid rgba(255, 255, 255, 0.18);
    border-radius: 12px;
    box-shadow:
        0 0 1px rgba(255, 255, 255, 0.1),
        0 8px 32px rgba(0, 0, 0, 0.3),
        inset 0 1px 0 rgba(255, 255, 255, 0.05);
    overflow: hidden;
}

.context-menu.visible {
    display: block;
    animation: contextMenuFadeIn 0.15s ease-out;
}

@keyframes contextMenuFadeIn {
    from {
        opacity: 0;
        transform: scale(0.95);
    }
    to {
        opacity: 1;
        transform: scale(1);
    }
}

.context-menu ul {
    list-style: none;
    margin: 0;
    padding: 6px 0;
}

.context-menu li {
    padding: 10px 16px;
    cursor: pointer;
    font-size: 14px;
    font-weight: 400;
    color: rgba(255, 255, 255, 0.7);
    display: flex;
    align-items: center;
    gap: 10px;
    transition: all 0.2s ease;
}

.context-menu li:hover {
    background: rgba(255, 255, 255, 0.05);
    color: white;
}

.context-menu .menu-icon {
    width: 16px;
    height: 16px;
    display: flex;
    align-items: center;
    justify-content: center;
    opacity: 0.7;
}

.context-menu .menu-icon svg {
    width: 16px;
    height: 16px;
    stroke: currentColor;
    fill: none;
    stroke-width: 2;
}

/* ========================================
   Block Toolbar (unified container for menu + star)
   ======================================== */
.page .parser-output .block[data-block-id] {
    position: relative;
}

/* Toolbar container - top right corner */
.page .parser-output .block-toolbar {
    position: absolute;
    top: 8px;
    right: 8px;
    display: flex;
    align-items: center;
    gap: 2px;
    z-index: 10;
}

/* Shared button styles */
.page .parser-output .block-toolbar button {
    width: 24px;
    height: 24px;
    padding: 0;
    background: transparent;
    border: none;
    border-radius: 4px;
    cursor: pointer;
    opacity: 0;
    transition: opacity 0.15s ease, background 0.15s ease;
    display: flex;
    align-items: center;
    justify-content: center;
}

/* Show buttons on block hover */
.page .parser-output .block[data-block-id]:hover .block-toolbar button {
    opacity: 0.4;
}

/* Button hover state */
.page .parser-output .block-toolbar button:hover {
    opacity: 1 !important;
    background: rgba(255, 255, 255, 0.1);
}

/* Menu button (•••) */
.page .parser-output .block-menu-btn {
    color: rgba(255, 255, 255, 0.7);
    font-size: 8px;
    font-weight: normal;
    letter-spacing: 1px;
}

/* Star button */
.page .parser-output .block-star svg {
    width: 16px;
    height: 16px;
    stroke: rgba(255, 255, 255, 0.7);
    stroke-width: 2;
    fill: none;
}

/* Always show star when favorited */
.page .parser-output .block-star.starred {
    opacity: 1 !important;
}

.page .parser-output .block-star.starred svg {
    fill: rgba(255, 200, 100, 0.9);
    stroke: rgba(255, 200, 100, 1);
}


/* ========================================
   Block Progress Button (Manual Mode)
   ======================================== */
.page .parser-output .block-progress-btn {
    position: absolute;
    top: 12px;
    right: 40px;
    width: 24px;
    height: 24px;
    padding: 0;
    background: transparent;
    border: none;
    border-radius: 4px;
    color: rgba(255, 255, 255, 0.25);
    cursor: pointer;
    opacity: 1;
    transition: opacity 0.2s ease, color 0.15s ease;
    z-index: 10;
    display: flex;
    align-items: center;
    justify-content: center;
}

.page .parser-output .block-progress-btn svg {
    width: 16px;
    height: 16px;
}

.page .parser-output .block-progress-btn:hover {
    color: rgba(255, 255, 255, 0.6);
}

.page .parser-output .block-progress-btn.read {
    color: rgba(160, 218, 0, 0.7);
}

.page .parser-output .block-progress-btn.read:hover {
    color: rgba(160, 218, 0, 1);
}

/* Block read state indicator - removed visual indicator, tracking only */

/* Light theme adjustments */
.page-wrapper.theme-light .block-progress-btn {
    color: rgba(0, 0, 0, 0.2);
}

.page-wrapper.theme-light .block-progress-btn:hover {
    color: rgba(0, 0, 0, 0.5);
}

.page-wrapper.theme-light .block-progress-btn.read {
    color: rgba(100, 140, 0, 0.7);
}


/* ========================================
   Responsive Breakpoints
   ======================================== */

/* Medium screens (900-1200px): Sidebar hides when doc open, chapter sidebar visible */
@media (max-width: 1200px) {
    .page-wrapper {
        left: 20px;  /* Gap from left edge when nav sidebar hidden */
        right: 20px; /* Default gap from right */
    }

    .page-wrapper > .page {
        margin: 20px 0;  /* Top/bottom margins - scrollable into the gap */
    }

    .page-wrapper.with-right-sidebar {
        right: 340px;  /* Chapter sidebar (300px) + margin (20px) + gap (20px) */
    }

    /* Show back button at this width too */
    .page .page-back-btn {
        display: flex;
    }
}

/* Small tablet (600-900px): Document overlays chapter sidebar */
@media (max-width: 900px) {
    .page-wrapper {
        left: 20px;
        right: 20px;
    }

    .page-wrapper.with-right-sidebar {
        right: 20px;  /* Right sidebar is behind page */
    }

    .page-wrapper > .page {
        max-width: none;
        margin: 20px 0;  /* Top/bottom margins - scrollable into the gap */
        min-height: calc(100vh - 40px);
    }

    /* Show back button */
    .page .page-back-btn {
        display: flex;
    }

    /* Adjust title size when back button is visible */
    .page .page-title {
        font-size: 1.4rem;
    }
}

@media (max-width: 768px) {
    .page-wrapper > .page {
        padding: 20px;
    }

    .page .page-title {
        font-size: 1.3rem;
    }

    .page .block {
        padding: 15px;
    }
}

/* Mobile (<600px): Full-screen iOS-style sheet */
@media (max-width: 600px) {
    .page-wrapper {
        left: 0;
        right: 0;
        top: 0;
        bottom: 0;
    }

    .page-wrapper.with-right-sidebar {
        right: 0;
    }

    .page-wrapper > .page {
        margin: 0;
        border-radius: 20px 20px 0 0;  /* iOS-style rounded top */
        margin-top: 20px;  /* Small gap at top for sheet appearance */
        min-height: calc(100% - 20px);
        padding-bottom: 100px;  /* Extra scroll room */
    }

    /* Drag handle indicator for swipe-to-dismiss */
    .page-wrapper > .page::before {
        content: '';
        position: absolute;
        top: 8px;
        left: 50%;
        transform: translateX(-50%);
        width: 36px;
        height: 5px;
        background: rgba(255, 255, 255, 0.3);
        border-radius: 3px;
        z-index: 10;
    }

    .page-wrapper.theme-light > .page::before {
        background: rgba(0, 0, 0, 0.2);
    }

    .page .page-header {
        padding: 24px 16px 12px 16px;  /* Extra top padding for drag handle */
        margin-bottom: 0;
        border-bottom: 1px solid rgba(255, 255, 255, 0.1);
    }

    .page .page-title {
        font-size: 1.1rem;
    }

    .page .doc-type-tag {
        display: none;  /* Hide tag on mobile to save space */
    }

    /* Reduce header actions on mobile */
    .page .page-header-actions {
        gap: 2px;
    }
}

/* ========================================
   Light Mode - Viewer UI Only
   ======================================== */
.page-wrapper.theme-light {
    background: transparent;
}

.page-wrapper.theme-light > .page {
    background: #ececec;
    border: 1px solid rgba(0, 0, 0, 0.06);
    box-shadow: none;
}

.page-wrapper.theme-light .page-header {
    border-bottom-color: rgba(0, 0, 0, 0.1);
}

.page-wrapper.theme-light .page-title {
    color: #111;
}

.page-wrapper.theme-light .theme-btn {
    color: rgba(0, 0, 0, 0.4);
}

.page-wrapper.theme-light .theme-btn:hover {
    color: rgba(0, 0, 0, 0.8);
    background: rgba(0, 0, 0, 0.05);
}

.page-wrapper.theme-light .theme-btn.active {
    color: rgb(120, 160, 0);
}

.page-wrapper.theme-light .edit-doc-btn {
    color: rgba(0, 0, 0, 0.4);
}

.page-wrapper.theme-light .edit-doc-btn:hover {
    color: rgba(0, 0, 0, 0.8);
    background: rgba(0, 0, 0, 0.05);
}

.page-wrapper.theme-light .loading-text,
.page-wrapper.theme-light .error-text {
    color: rgba(0, 0, 0, 0.5);
}

.page-wrapper.theme-light .block-menu-btn {
    color: rgba(0, 0, 0, 0.6);
}

.page-wrapper.theme-light .block-toolbar button:hover {
    background: rgba(0, 0, 0, 0.08);
}

.page-wrapper.theme-light .block-star svg {
    stroke: rgba(0, 0, 0, 0.6);
}

.page-wrapper.theme-light .block-star.starred svg {
    fill: rgba(230, 160, 30, 0.9);
    stroke: rgba(200, 140, 20, 1);
}

.page-wrapper.theme-light .sketch-placeholder .sketch-loading {
    color: rgba(0, 0, 0, 0.5);
}

.page-wrapper.theme-light .sketch-placeholder .sketch-loading::before {
    border-color: rgba(0, 0, 0, 0.2);
    border-top-color: rgb(120, 160, 0);
}
