/* ============================================
   A4 SIDLAYOUT (PRINT-READY) STYLES
   MINIMAL styles - only handles page layout, breaks, and numbers
   Content styling comes from main stylesheet (identical to Overview)
   ============================================ */

/* CSS Custom Properties for A4 Layout */
:root {
    --a4-page-width: 210mm;
    --a4-page-height: 297mm;
    --a4-page-padding: 20mm;
    --a4-page-content-padding: 5px;  /* Minimal padding for page content to maximize space */
    --a4-footer-height: 25mm;  /* Increased to provide more space for content above footer */
    --a4-footer-padding: 20mm; /* Reduced from 35mm for more content space */
    --a4-border-radius: 4px;
    --continuation-border-color: rgba(212, 175, 55, 0.5);
    --continuation-text-color: rgba(212, 175, 55, 0.7);
}

/* ============ SCREEN READER ONLY TEXT ============ */
/* Visually hidden text for screen readers */
.sr-only {
    position: absolute;
    width: 1px;
    height: 1px;
    padding: 0;
    margin: 0;
    overflow: hidden;
    clip-path: inset(50%);
    white-space: nowrap;
    border: 0;
}

.adventure-view .place-map img {
    max-width: 70%;
    border: 0px solid rgba(212, 175, 55, 0.2);
    border-radius: 4px;
    /* box-shadow: 0 4px 16px rgba(0, 0, 0, 0.2); */
    -webkit-mask-image: url(../images/mask.png);
    mask-image: url(../images/mask.png);
    -webkit-mask-size: 100% 100%;
    mask-size: 100% 100%;
    -webkit-mask-repeat: no-repeat;
    mask-repeat: no-repeat;
    -webkit-mask-mode: luminance;
    mask-mode: luminance;
}

/* ============ A4 CONTAINER ============ */
/* Root container inherits adventure-view styling */
.a4-layout.adventure-view.style-dragonbrew {
    background: var(--adventure-bg-primary, #1a1a1f);
    min-height: 100vh;
    padding: 2rem 1rem;
}

/* A4 page dimensions: 210mm x 297mm at 96 DPI */
/* Visual A4 page block with parchment appearance */
/* Note: min-height instead of fixed height allows content to expand naturally on screen */
/* Print styles enforce fixed height for proper pagination */
.a4-layout .a4-page {
    width: var(--a4-page-width);
    height: var(--a4-page-height);
    margin: 0 auto 2rem;
    padding: 0;
    /*background: var(--parchment-color, #f4e4bc);*/
    border: 1px solid rgba(181, 146, 24, 0.2);
    border-radius: var(--a4-border-radius);
    position: relative;
    overflow: visible; /* Allow content overflow to be visible on screen for debugging pagination */
    page-break-after: always;
    page-break-inside: avoid;
    break-after: page;
    box-sizing: border-box;
    box-shadow: 0 4px 20px rgba(0, 0, 0, 0.3);
}

/* Content wrapper inside A4 page - padding and spacing for content */
.a4-layout .a4-page-content {
    position: relative;
    height: calc(var(--a4-page-height) - var(--a4-footer-height));
    padding: var(--a4-page-content-padding);
    /* Bottom padding is intentionally larger to reserve space for the footer */
    padding-bottom: calc(var(--a4-page-padding) + var(--a4-footer-height));
    box-sizing: border-box;
    overflow: visible;
    border: none;
}

/* Last page shouldn't have margin bottom */
.a4-layout .a4-page:last-child {
    margin-bottom: 0;
}

/* ============ A4 HEADER/CONTROLS ============ */
.a4-layout .a4-controls {
    max-width: 210mm;
    margin: 0 auto 1.5rem;
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 1rem;
    background: rgba(0, 0, 0, 0.3);
    border-radius: 8px;
    box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
    color: #e0e0e0;
}

.btn-sm {
   padding: 0.5rem 1rem;
    background: #292929;
    font-size: 0.875rem;
    height: 35px;
}

.a4-layout .a4-controls .left {
    display: flex;
    gap: 1rem;
    align-items: center;
}

.a4-layout .a4-controls .right {
    display: flex;
    gap: 0.75rem;
    align-items: center;
}

/* ============ PAGE NUMBER FOOTER ============ */
.a4-layout .a4-page-number {
    position: absolute;
    bottom: 5mm; /* Closer to bottom edge */
    left: 0;
    right: 0;
    text-align: center;
    font-size: 0.9rem;
    color: var(--text-secondary, #888);
    padding-top: 0.5rem;
    z-index: 10; /* Ensure it stays on top */
}

/* Position page footer at bottom of A4 page */
.a4-layout .page-footer {
    position: absolute;
    bottom: 8mm;
    left: 0;
    right: 0;
    text-align: center;
    z-index: 100;
    pointer-events: none;
}

/* Footer banner - base styles */
.a4-layout .footer-banner {
    display: inline-block;
    background: linear-gradient(135deg, #d4af37 0%, #b8962e 100%);
    color: #1a1a1a;
    padding: 8px 20px;
    font-family: 'Cinzel', serif;
}

/* Large footer banner - chapter pages */
.a4-layout .footer-banner-large {
    background: linear-gradient(135deg, #d4af37 0%, #b8941f 50%, #d4af37 100%);
    padding: 6px 16px;
    clip-path: polygon(
        0% 30%, 5% 0%, 10% 30%, 15% 0%, 20% 30%, 25% 0%, 30% 30%, 35% 0%, 40% 30%, 45% 0%, 
        50% 30%, 55% 0%, 60% 30%, 65% 0%, 70% 30%, 75% 0%, 80% 30%, 85% 0%, 90% 30%, 95% 0%, 
        100% 30%, 100% 70%, 95% 100%, 90% 70%, 85% 100%, 80% 70%, 75% 100%, 70% 70%, 65% 100%, 
        60% 70%, 55% 100%, 50% 70%, 45% 100%, 40% 70%, 35% 100%, 30% 70%, 25% 100%, 20% 70%, 
        15% 100%, 10% 70%, 5% 100%, 0% 70%
    );
    box-shadow: 0 4px 8px rgba(0, 0, 0, 0.3);
    min-height: 40px;
}

.a4-layout .footer-banner-large .footer-chapter {
    font-size: 0.7rem;
    letter-spacing: 1px;
    font-weight: bold;
    color: #1a1a1f;
    text-transform: uppercase;
    margin-bottom: 4px;
}

.a4-layout .footer-banner-large .footer-page {
    font-size: 0.9rem;
    font-weight: bold;
    color: #1a1a1f;
}

/* Small footer banner - regular pages */
.a4-layout .footer-banner-small {
    background: linear-gradient(135deg, #d4af37 0%, #b8941f 50%, #d4af37 100%);
    padding: 8px 20px;
    border-radius: 4px;
    box-shadow: 0 2px 4px rgba(0, 0, 0, 0.2);
    min-height: 30px;
}

.a4-layout .footer-banner-small .footer-page {
    font-size: 1rem;
    font-weight: bold;
    color: #1a1a1f;
}

/* Remove any floating footer-chapter divs that appear in content */
.a4-layout .a4-page-content .footer-chapter,
.a4-layout .a4-page-content .footer-page,
.a4-layout .place-content .footer-chapter,
.a4-layout .place-content .footer-page {
    display: none !important; /* These should ONLY appear in .page-footer */
}

/* ============ PRINT STYLES ============ */
@media print {
    /* Hide ToC in print view */
    .a4-view-wrapper .sticky-toc {
        display: none !important;
    }
    
    /* Adjust wrapper for print */
    .a4-view-wrapper {
        display: block;
        padding: 0;
    }
    
    .a4-layout .a4-controls {
        display: none !important;
    }
    
    .a4-layout.adventure-view.style-dragonbrew {
        padding: 0;
        background: white;
    }
    
    .a4-layout .a4-page {
        width: var(--a4-page-width);
        height: var(--a4-page-height);
        margin: 0;
        padding: 0;
        box-shadow: none;
        page-break-after: always;
        page-break-inside: avoid;
        background: white;
        overflow: hidden; /* Hide overflow for proper print pagination */
    }
    
    .a4-layout .a4-page-content {
        padding: var(--a4-page-padding);
        padding-bottom: calc(var(--a4-page-padding) + var(--a4-footer-height));
        height: calc(var(--a4-page-height) - var(--a4-footer-height));
    }
    
    .a4-layout .a4-page:last-child {
        page-break-after: auto;
    }
    
    /* Keep headers with their content */
    .place-header,
    .place-section > h2,
    .adventure-content > h2,
    .adventure-content > h3 {
        page-break-after: avoid;
        break-after: avoid;
    }
    
    /* Don't break inside cards and boxes */
    .npc-card, .enemy-card, .item-card, .handout-card,
    .styled-box, .styled-image, .place-intro-group, .scene-section,
    .dialog-box, .random-event-table {
        page-break-inside: avoid;
        break-inside: avoid;
    }
    
    @page {
        size: A4;
        margin: 0;
    }
}

/* ============ RESPONSIVE ============ */
@media (max-width: 768px) {
    /* Hide ToC on mobile for better experience */
    .a4-view-wrapper .sticky-toc {
        display: none;
    }
    
    .a4-view-wrapper {
        padding: 1rem 0.5rem;
    }
    
    .a4-layout.adventure-view.style-dragonbrew {
        padding: 1rem 0.5rem;
    }
    
    .a4-layout .a4-page {
        width: var(--a4-page-width);
        height: var(--a4-page-height);
        margin-bottom: 1rem;
    }
    
    .a4-layout .a4-page-content {
        padding: 10mm;
    }
    
    .a4-layout .a4-controls {
        flex-direction: column;
        gap: 1rem;
        align-items: stretch;
    }
    .adventure-view .adventure-cover-img {
        box-shadow: 0px 0px 41px 8px rgb(0 0 0 / 0%);
    }
    
    .a4-layout .a4-controls .left,
    .a4-layout .a4-controls .right {
        flex-direction: column;
        width: 100%;
    }
}

/* ============ IMAGE MAX-HEIGHT ============ */
/* Prevent images from taking up entire pages */
.a4-layout .place-image {
    max-height: 550px;
    object-fit: contain;
    display: block;
    width: fit-content;
    margin: 0 auto;
    -webkit-mask-image: url(../images/mask.png);
    mask-image: url(../images/mask.png);
    -webkit-mask-size: 100% 100%;
    mask-size: 100% 100%;
    -webkit-mask-repeat: no-repeat;
    mask-repeat: no-repeat;
    -webkit-mask-mode: luminance;
    mask-mode: luminance;
}

/* Place images should stay with following text */
.a4-layout .place-section .place-image {
    break-after: avoid;
    page-break-after: avoid;
}

.a4-layout .place-section .place-image + p,
.a4-layout .place-section .place-image + .formatted-content,
.a4-layout .place-section .place-image + .drop-cap {
    break-before: avoid;
    page-break-before: avoid;
}

/* NEW: Group place introduction (header + image + first text) to prevent orphaned images */
.a4-layout .place-intro-group {
    page-break-inside: avoid;
    break-inside: avoid;
}

.a4-layout .adventure-cover-img {
    max-height: 600px;
    object-fit: contain;
    box-shadow: 0px 0px 41px 8px rgb(0 0 0 / 0%);
}

/* Fullpage hero sections should be constrained to fit within page */
/* 24mm accounts for page margins and spacing to prevent content overlap with footer */
.a4-layout .fullpage-hero-section {
    max-height: calc(297mm - var(--a4-footer-padding) - var(--a4-footer-height) - 24mm);
    overflow: hidden;
}

.a4-layout .fullpage-hero-section img,
.a4-layout .fullpage-hero-image img {
    max-height: 500px;  /* Limit image height */
    width: auto;
    object-fit: contain;
}

/* ============ SPLIT BOXES AT PAGE BREAKS ============ */
/* Visual indicator when a box continues from previous page */
.a4-layout .styled-box.continued-from-previous {
  border-top: 2px dashed var(--continuation-border-color);
  padding-top: 1rem;
}

.a4-layout .styled-box.continued-from-previous::before {
  content: "…continued";
  display: block;
  font-size: 0.75rem;
  color: var(--continuation-text-color);
  font-style: italic;
  margin-bottom: 0.5rem;
}

/* Visual indicator when a box continues on next page */
.a4-layout .styled-box.continues-on-next {
  border-bottom: 2px dashed var(--continuation-border-color);
  padding-bottom: 1rem;
}

.a4-layout .styled-box.continues-on-next::after {
  content: "continues…";
  display: block;
  font-size: 0.75rem;
  color: var(--continuation-text-color);
  font-style: italic;
  text-align: right;
  margin-top: 0.5rem;
}

/* Split tables */
.a4-layout table.continued-from-previous thead {
  border-top: 2px dashed var(--continuation-border-color);
}

/* Add "...fortsättning" marker for continued tables */
.a4-layout table.continued-from-previous::before {
  content: "...fortsättning";
  display: block;
  font-style: italic;
  color: #888;
  font-size: 0.85rem;
  margin-bottom: 0.5rem;
}

.a4-layout table.continues-on-next {
  border-bottom: 2px dashed var(--continuation-border-color);
}

/* Style for table continuation marker cells */
.a4-layout .table-continues-marker {
  border-top: 1px dashed rgba(212, 175, 55, 0.3);
  padding-top: 0.5rem;
}

/* ============ STICKY TOC NAVIGATION ============ */
/* Wrapper for ToC + Content layout */
.a4-view-wrapper {
    display: flex;
    gap: 2rem;
    max-width: 1400px;
    margin: 0 auto;
    padding: 2rem 1rem;
    background: var(--adventure-bg-primary, #1a1a1f);
    min-height: 100vh;
}

/* Sticky ToC sidebar */
.a4-view-wrapper .sticky-toc {
    position: sticky;
    top: 20px;
    width: 250px;
    height: fit-content;
    max-height: calc(100vh - 40px);
    background: rgba(0, 0, 0, 0.3);
    border: 1px solid rgba(212, 175, 55, 0.2);
    border-radius: 8px;
    padding: 1rem;
    overflow-y: auto;
    flex-shrink: 0;
}

.a4-view-wrapper .sticky-toc-progress {
    position: absolute;
    left: 0;
    top: 0;
    bottom: 0;
    width: 3px;
    background: rgba(212, 175, 55, 0.1);
    border-radius: 8px 0 0 8px;
}

.a4-view-wrapper .sticky-toc-progress-bar {
    width: 100%;
    height: 0%;
    background: #d4af37;
    transition: height 0.2s ease;
    border-radius: 8px 0 0 0;
}

.a4-view-wrapper .sticky-toc-title {
    font-family: 'Cinzel', serif;
    font-size: 0.875rem;
    color: #d4af37;
    text-transform: uppercase;
    letter-spacing: 0.05em;
    margin-bottom: 1rem;
    padding-bottom: 0.5rem;
    border-bottom: 1px solid rgba(212, 175, 55, 0.2);
}

.a4-view-wrapper .sticky-toc-items {
    display: flex;
    flex-direction: column;
    gap: 0.25rem;
}

.a4-view-wrapper .sticky-toc-item {
    display: flex;
    align-items: center;
    gap: 0.5rem;
    padding: 0.5rem;
    border-radius: 4px;
    color: #a0a0a0;
    text-decoration: none;
    font-size: 0.813rem;
    transition: all 0.2s;
    cursor: pointer;
}

.a4-view-wrapper .sticky-toc-item:hover {
    background: rgba(212, 175, 55, 0.1);
    color: #e0e0e0;
}

.a4-view-wrapper .sticky-toc-item.active {
    background: rgba(212, 175, 55, 0.15);
    color: #d4af37;
    font-weight: 600;
}

.a4-view-wrapper .sticky-toc-item.active .sticky-toc-indicator {
    color: #d4af37;
    transform: scale(1.2);
}

.a4-view-wrapper .sticky-toc-indicator {
    font-size: 0.5rem;
    color: #666;
    transition: all 0.2s;
    flex-shrink: 0;
}

.a4-view-wrapper .sticky-toc-text {
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

/* Scrollbar styling for ToC */
.a4-view-wrapper .sticky-toc::-webkit-scrollbar {
    width: 6px;
}

.a4-view-wrapper .sticky-toc::-webkit-scrollbar-track {
    background: transparent;
}

.a4-view-wrapper .sticky-toc::-webkit-scrollbar-thumb {
    background: rgba(212, 175, 55, 0.3);
    border-radius: 3px;
}

.a4-view-wrapper .sticky-toc::-webkit-scrollbar-thumb:hover {
    background: rgba(212, 175, 55, 0.5);
}

/* Adjust main A4 layout when inside wrapper */
.a4-view-wrapper .a4-layout {
    flex: 1;
    min-width: 0;
    padding: 0;
}

/* ============ FLOAT-BASED BOX PLACEMENT ============ */
/* Support data-placement attributes for right/left aligned boxes */
.a4-layout .styled-box.float-right,
.a4-layout .styled-box[data-placement="right"],
.a4-layout .styled-image.float-right,
.a4-layout .styled-image[data-placement="right"] {
    float: right;
    width: 280px;
    max-width: 40%;
    margin-left: 20px;
    margin-bottom: 16px;
    clear: right;
}

.a4-layout .styled-box.float-left,
.a4-layout .styled-box[data-placement="left"],
.a4-layout .styled-image.float-left,
.a4-layout .styled-image[data-placement="left"] {
    float: left;
    width: 280px;
    max-width: 40%;
    margin-right: 20px;
    margin-bottom: 16px;
    clear: left;
}

.a4-layout .styled-image[data-placement="full"],
.a4-layout .styled-image:not([data-placement]) {
    clear: both;
}

.formatted-content strong {
    color: #dbdbdb;
    font-size: 0.7rem;
    font-family: colus;
}

.adventure-view.style-dragonbrew p {
    margin-bottom: 1em;
    text-align: justify;
    font-family: CrimsonPro;
    font-size: 1rem;
}

.adventure-view .place-content table td {
    padding: 1rem 1.25rem;
    color: #d5d1ca;
    font-family: CrimsonPro;
    font-size: 0.8rem;
}

.adventure-view .adventure-content {
    border: 1px solid rgba(212, 175, 55, 0.15);
}

.adventure-cover-section {
    background: #14141700;
    border: 0px solid var(--fantasy-border);
    padding: 2rem;
    border-radius: 8px;
}

/* ============ "PÅ DENNA PLATS" TABLE STYLING ============ */
/* Ensure entity tables in A4 layout match the normal view styling */
.a4-layout .styled-entity-table {
    width: 100%;
    border-collapse: collapse;
    margin: 0;
}

.a4-layout .styled-entity-table thead {
    border-bottom: 2px solid rgba(212, 175, 55, 0.4);
}

.a4-layout .styled-entity-table th {
    text-align: left;
    color: #d4af37;
    font-size: 0.75rem;
    text-transform: uppercase;
    letter-spacing: 0.05em;
    padding: 0.75rem 1rem;
    border: none;
    background: transparent;
    font-family: 'Cinzel', serif;
    font-weight: 600;
}

.a4-layout .styled-entity-table td {
    padding: 1rem 1.25rem;
    color: #d5d1ca;
    font-family: 'Crimson Pro', Georgia, serif;
    font-size: 0.95rem;
    border-bottom: 1px solid rgba(212, 175, 55, 0.15);
    vertical-align: top;
}

.a4-layout .styled-entity-table td:first-child {
    width: 110px;
    font-size: 0.9rem;
}

/* Override first-row styling for table-body-standard class */
.a4-layout .table-body-standard tr:first-child td,
.a4-layout .styled-entity-table.table-body-standard tr:first-child td {
    color: #e0e0e0;
    font-family: "Crimson Pro", Georgia, serif;
    font-size: 0.95rem;
    font-weight: 400;
    text-transform: none;
    letter-spacing: normal;
    border-bottom: 1px solid rgba(212, 175, 55, 0.15);
}

/* ============ TABLE OF CONTENTS STYLING ============ */
.a4-layout .toc-section {
    margin: 2rem 0;
}

.a4-layout .toc-section h2 {
    text-align: center;
    font-size: 2rem;
    color: var(--gold-primary, #d4af37);
    margin-bottom: 2rem;
    font-family: 'Cinzel', serif;
    letter-spacing: 2px;
    text-transform: uppercase;
}

.a4-layout .toc-list {
    max-width: 600px;
    margin: 0 auto;
}

.a4-layout .toc-item {
    display: flex;
    align-items: baseline;
    margin-bottom: 1rem;
    font-family: "Crimson Pro", Georgia, serif;
    font-size: 1.1rem;
}

.a4-layout .toc-place-number {
    color: var(--gold-primary, #d4af37);
    font-weight: bold;
    margin-right: 0.5rem;
    min-width: 2.5rem;
    font-family: 'Cinzel', serif;
}

.a4-layout .toc-place-name {
    color: #e0e0e0;
    flex-shrink: 0;
    margin-right: 0.5rem;
}

.a4-layout .toc-dots {
    flex-grow: 1;
    border-bottom: 1px dotted rgba(212, 175, 55, 0.3);
    margin: 0 0.5rem 0.25rem 0.5rem;
    min-width: 2rem;
}

.a4-layout .toc-page {
    color: var(--gold-primary, #d4af37);
    font-weight: bold;
    margin-left: 0.5rem;
    min-width: 2rem;
    text-align: right;
}
.adventure-view .place-section {
    margin: 0.3rem 0;
}

/* ============ PDF UPLOADER STYLES ============ */
.pdf-uploader-container {
    margin-top: 0.5rem;
}

.pdf-current-file {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 1rem;
    background: var(--bg-secondary, rgba(0, 0, 0, 0.2));
    border: 1px solid var(--border, rgba(212, 175, 55, 0.2));
    border-radius: 8px;
}

.pdf-file-info {
    display: flex;
    align-items: center;
    gap: 0.75rem;
}

.pdf-icon {
    font-size: 1.5rem;
}

.pdf-file-name {
    color: var(--text-primary, #e0e0e0);
    text-decoration: none;
    font-weight: 500;
}

.pdf-file-name:hover {
    color: var(--accent-gold, #d4af37);
}

.upload-progress-bar {
    width: 100%;
    height: 8px;
    background: rgba(0, 0, 0, 0.3);
    border-radius: 4px;
    overflow: hidden;
}

.upload-progress-fill {
    height: 100%;
    background: linear-gradient(90deg, #d4af37 0%, #b8962e 100%);
    transition: width 0.3s ease;
    width: 0%;
}

.upload-progress-text {
    text-align: center;
    font-size: 0.9rem;
    color: var(--text-secondary, #a0a0a0);
    margin-top: 0.25rem;
}

/* ============ PDF VIEWER STYLES ============ */
.pdf-viewer-container {
    width: 100%;
    height: 100vh;
    background: var(--adventure-bg-primary, #1a1a1f);
    display: flex;
    flex-direction: column;
}

.pdf-viewer-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 1rem 2rem;
    background: rgba(0, 0, 0, 0.3);
    border-bottom: 1px solid rgba(212, 175, 55, 0.2);
}

.pdf-viewer-header h2 {
    margin: 0;
    color: var(--accent-gold, #d4af37);
    font-family: Colus;
}

.pdf-viewer-actions {
    display: flex;
    gap: 0.5rem;
}

.pdf-viewer-iframe {
    flex: 1;
    border: none;
    width: 100%;
    background: #525252;
}
