:root {
    --bg: #f3f3f3;
    --fg: #000000;
    --acc: #8f8f8f;
}

* {
    box-sizing: border-box;
}

body {
    margin: 0;
    padding: 0;
    width: 100vw;
    height: 100vh;
    background-color: var(--bg);
    color: var(--fg);
    font-family: 'Times New Roman';
    font-size: 1em;
}

main {
    width: 100%;
    height: 100%;
    display: flex;
    flex-direction: row;
}

canvas {
    filter: grayscale(100%);
}

button {
    cursor: pointer;
}

#controls-window {
    margin: 1rem;
    width: 300px;
    height: fit-content;
    flex-shrink: 0;
}

#controls-window .window-body {
    display: flex;
    flex-direction: column;
    gap: 0.75rem;
}

#info-panel {
    padding: 0.5rem;
}

#preview {
    flex: 2;
    background-color: var(--bg);
    overflow-x: hidden; /* Change from auto to hidden */
    overflow-y: auto;   /* Change to auto for vertical scrolling */
    display: flex;
    justify-content: center; 
    align-items: flex-start; /* Align to the top */
    #book-container {
        background: var(--acc);
        margin: 20px;
        padding: 20px;
        display: flex;
        flex-direction: column;
        gap: 20px;
        width: 90%;
        max-width: 500px; /* Adjust max-width for a single column view */
        .page-container {
            display: flex;
            flex-direction: column;
            align-items: center;
            gap: 10px;
            height: auto; 
            aspect-ratio: 148 / 210; /* A-series paper ratio (A5, A6, etc.) */
        }
        #og-page, .rmx-page, #first-page, #last-page {
            font-family: 'Times New Roman';
            font-size: 1em;
            background: var(--bg);
            width: 100%;
            height: 100%;
            box-shadow: 0 4px 8px rgba(0,0,0,0.3);
            position: relative; /* Crucial for positioning layout blocks */
            overflow: hidden;
            #pdf-canvas, #fc-canvas, #bc-canvas {
                width: 100%;
                height: 100%;
                object-fit: contain;
            }
            .layout-block {
                position: absolute;
                background-color: none;
                box-sizing: border-box;
                color: var(--fg);
                overflow: hidden;
                padding: 2px;
                white-space: pre-wrap; /* Respects whitespace and wraps text */
                word-break: break-all;
            }
            .small-sentence-block,
            .big-sentence-block {
                font-size: 0.5em;
            }
            .multi-term-block {
                font-size: 0.7em;
            }
            .basic-block {
                font-size: 0.9em;
            }
        }
    }
}

.generated-page-image {
    width: 100%;
    height: 100%;
    object-fit: contain;
}

#loading-overlay {
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background-color: rgba(255, 255, 255, 0.8);
    z-index: 10000;
    display: flex;
    justify-content: center;
    align-items: center;
    font-size: 2em;
    font-family: 'Times New Roman', serif;
    color: var(--fg);
}

/* --- PRINT STYLES (REVISED) --- */
@media print {
    @page {
        size: A6 portrait;
        margin: 0mm;
    }

    /* Hide everything by default */
    body * {
        visibility: hidden;
    }

    /* Make the book container and its children visible */
    #book-container, #book-container * {
        visibility: visible;
    }

    /* Position the book container to fill the printed page */
    #book-container {
        position: absolute;
        left: 0;
        top: 0;
        width: 100% !important;
        margin: 0;
        padding: 0;
        gap: 0;
    }

    /* Ensure each page container takes up the full page */
    #book-container > div {
        width: 100% !important;
        height: 100% !important;
        page-break-after: always; /* Each page gets its own sheet */
    }

    /* Prevent an extra blank page at the end */
    #book-container > div:last-child {
        page-break-after: auto;
    }

    #og-page, .rmx-page, #first-page, #last-page {
        box-shadow: none !important;
        display: flex !important;
        justify-content: center !important; /* Center horizontally */
        align-items: center !important;     /* Center vertically */
        width: 100% !important;
        height: 100% !important;
    }

    .generated-page-image {
        height: 100% !important;  /* Touch the top and bottom edges */
        width: auto !important;   /* Let the width adjust automatically */
        object-fit: contain !important; /* Still useful as a fallback */
    }
}