* {
    box-sizing: border-box;
}

body, html {
    margin: 0;
    padding: 0;
    height: 100%;
    font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Helvetica, Arial, sans-serif;
    background-color: #f5f5f5;
    color: #333;
}

.app-container {
    display: flex;
    height: 100vh;
    overflow: hidden;
}

/* UI Elements - Hidden during print */
.editor-pane {
    width: 45%;
    min-width: 300px;
    display: flex;
    flex-direction: column;
    border-right: 1px solid #ccc;
    background-color: #fff;
}

.toolbar {
    padding: 10px;
    background: #fafafa;
    border-bottom: 1px solid #eee;
    display: flex;
    flex-direction: column;
    gap: 10px;
}

.toolbar-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
}

.branding {
    font-weight: 600;
    font-size: 14px;
}

.mobile-tabs {
    display: none;
    gap: 4px;
    background: #e5e5e5;
    padding: 2px;
    border-radius: 4px;
}

.tab-btn {
    padding: 4px 12px;
    border: none;
    background: transparent;
    font-size: 12px;
    font-weight: 500;
    cursor: pointer;
    border-radius: 3px;
    transition: background 0.15s ease;
}

.tab-btn.active {
    background: #fff;
    box-shadow: 0 1px 3px rgba(0,0,0,0.15);
}

.controls {
    display: flex;
    gap: 15px;
    align-items: center;
    font-size: 12px;
    flex-wrap: wrap;
}

.controls input[type="number"] {
    width: 50px;
    padding: 2px;
}

.controls select {
    padding: 2px 4px;
    font-size: 12px;
    border: 1px solid #ccc;
    border-radius: 3px;
    background: #fff;
}

.zoom-controls {
    display: flex;
    align-items: center;
    gap: 4px;
    background: #e8e8e8;
    padding: 2px 6px;
    border-radius: 4px;
}

.zoom-controls button {
    padding: 2px 6px;
    background: #fff;
    border: 1px solid #ccc;
    border-radius: 3px;
    cursor: pointer;
    font-size: 11px;
}

.zoom-controls button:hover {
    background: #f0f0f0;
}

#zoom-level-text {
    font-size: 11px;
    min-width: 32px;
    text-align: center;
    font-weight: 500;
}

button#print-btn {
    padding: 4px 10px;
    background: #000;
    color: #fff;
    border: none;
    border-radius: 4px;
    cursor: pointer;
    font-size: 12px;
}

button#print-btn:hover {
    background: #333;
}

#markdown-input {
    flex: 1;
    border: none;
    padding: 15px;
    resize: none;
    outline: none;
    font-family: 'Courier New', Courier, monospace;
    font-size: 14px;
    line-height: 1.5;
}

/* Preview Pane */
.preview-pane {
    flex: 1;
    overflow-y: auto;
    padding: 20px;
    background-color: #e0e0e0;
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 20px;
}

/* Measuring container (Hidden) */
.measuring-container {
    position: absolute;
    top: -9999px;
    left: -9999px;
    visibility: hidden;
}

/* Preview Container - Holds multi-sheet A4 pages */
#preview-container {
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 30px; /* Distinct vertical separation between consecutive A4 sheets */
}

/* Physical A4 Sheet Container (Screen Preview) */
.a4-sheet {
    width: 297mm;
    height: 210mm;
    background: white;
    box-shadow: 0 4px 10px rgba(0,0,0,0.1);
    display: flex;
    padding: 4mm;
    gap: 4mm;
    flex-shrink: 0;
    /* Scale down based on preview pane width */
    transform-origin: top center;
}

.a4-sheet.portrait {
    width: 210mm;
    height: 297mm;
}

/* Logical Portrait Page */
.logical-page {
    flex: 1;
    height: 100%;
    position: relative;
    border: none;
    overflow: hidden;
    padding: 3mm; /* extremely narrow inner padding */
}

/* Page content typography - Applied to both measuring and final render */
.page-content {
    font-size: var(--doc-font-size, 10px);
    line-height: 1.25;
}

.page-content p { margin: 2px 0; }
.page-content h1, .page-content h2, .page-content h3, .page-content h4 {
    margin: 4px 0 2px;
    line-height: 1.15;
    page-break-after: avoid;
    break-after: avoid;
}
.page-content h1 { font-size: 1.5em; }
.page-content h2 { font-size: 1.3em; }
.page-content h3 { font-size: 1.1em; }
.page-content ul, .page-content ol {
    margin: 2px 0;
    padding-left: 15px;
}
.page-content li {
    margin: 0;
    page-break-inside: avoid;
    break-inside: avoid;
}
.page-content code {
    background: #f0f0f0;
    padding: 0 2px;
    border-radius: 2px;
    font-family: monospace;
}
.page-content pre {
    background: #f9f9f9;
    padding: 4px;
    border: 1px solid #ddd;
    margin: 2px 0;
    white-space: pre-wrap;
    page-break-inside: avoid;
    break-inside: avoid;
}
.page-content blockquote {
    margin: 2px 0;
    padding-left: 8px;
    border-left: 3px solid #ccc;
    color: #555;
    page-break-inside: avoid;
    break-inside: avoid;
}
.page-content table {
    border-collapse: collapse;
    width: 100%;
    margin: 2px 0;
    page-break-inside: avoid;
    break-inside: avoid;
}

.page-content mark {
    background-color: #fff5b1;
    padding: 0 2px;
    border-radius: 2px;
}

.page-content sub, .page-content sup {
    font-size: 0.75em;
    line-height: 0;
    position: relative;
    vertical-align: baseline;
}
.page-content sup { top: -0.4em; }
.page-content sub { bottom: -0.2em; }

.page-content dl { margin: 2px 0; }
.page-content dt { font-weight: bold; margin-top: 2px; }
.page-content dd { margin-left: 12px; margin-bottom: 2px; }

.page-content details {
    border: 1px solid #ddd;
    padding: 4px;
    border-radius: 3px;
    margin: 2px 0;
}
.page-content summary {
    font-weight: bold;
    cursor: pointer;
}

.page-content input[type="checkbox"] {
    margin: 0 4px 0 0;
    vertical-align: middle;
}

/* GitHub Alerts / Callouts */
.markdown-alert {
    padding: 4px 8px;
    margin: 4px 0;
    border-left: 3px solid #0969da;
    background-color: #f6f8fa;
    border-radius: 0 3px 3px 0;
    page-break-inside: avoid;
    break-inside: avoid;
}
.markdown-alert-title {
    font-weight: bold;
    display: flex;
    align-items: center;
    gap: 4px;
    margin-bottom: 2px;
}
.markdown-alert-note { border-left-color: #0969da; background-color: #ddf4ff; }
.markdown-alert-note .markdown-alert-title { color: #0969da; }
.markdown-alert-tip { border-left-color: #1a7f37; background-color: #dafbe1; }
.markdown-alert-tip .markdown-alert-title { color: #1a7f37; }
.markdown-alert-important { border-left-color: #8250df; background-color: #f3e8ff; }
.markdown-alert-important .markdown-alert-title { color: #8250df; }
.markdown-alert-warning { border-left-color: #9a6700; background-color: #fff8c5; }
.markdown-alert-warning .markdown-alert-title { color: #9a6700; }
.markdown-alert-caution { border-left-color: #cf222e; background-color: #ffebe9; }
.markdown-alert-caution .markdown-alert-title { color: #cf222e; }

/* Footnotes */
.footnotes {
    margin-top: 10px;
    border-top: 1px solid #ddd;
    font-size: 0.85em;
    color: #555;
}

/* KaTeX Math & Mermaid */
.katex-display { margin: 4px 0 !important; }
.mermaid { text-align: center; margin: 4px 0; }
.page-content th, .page-content td {
    border: 1px solid #ccc;
    padding: 2px 4px;
    text-align: left;
}
.page-content img {
    max-width: 100%;
    height: auto;
    page-break-inside: avoid;
    break-inside: avoid;
}

/* Page Number */
.page-number {
    position: absolute;
    bottom: 1.5mm;
    left: 0;
    right: 0;
    text-align: center;
    font-size: 8px;
    color: #888;
}

/* Print Styles */
@media print {
    @page {
        size: A4 landscape;
        margin: 0;
    }

    html, body {
        margin: 0 !important;
        padding: 0 !important;
        background: #fff !important;
        width: 100% !important;
        height: auto !important;
        overflow: visible !important;
        -webkit-print-color-adjust: exact !important;
        print-color-adjust: exact !important;
    }

    .app-container {
        height: auto !important;
        overflow: visible !important;
        display: block !important;
        margin: 0 !important;
        padding: 0 !important;
        width: 100% !important;
    }

    .editor-pane {
        display: none !important;
    }

    .preview-pane {
        padding: 0 !important;
        margin: 0 !important;
        background: none !important;
        display: block !important;
        overflow: visible !important;
        width: 100% !important;
        height: auto !important;
    }

    #preview-container {
        zoom: 1 !important;
        transform: none !important;
        margin: 0 !important;
        padding: 0 !important;
        gap: 0 !important;
        width: 100% !important;
        height: auto !important;
        display: block !important;
    }

    .a4-sheet {
        width: 297mm !important;
        height: 210mm !important;
        box-shadow: none !important;
        margin: 0 !important;
        padding: 4mm !important;
        gap: 4mm !important;
        transform: none !important;
        page-break-after: always !important;
        break-after: page !important;
        page-break-inside: avoid !important;
        break-inside: avoid !important;
        box-sizing: border-box !important;
        display: flex !important;
    }

    .a4-sheet.portrait {
        width: 210mm !important;
        height: 297mm !important;
        margin: 0 !important;
    }

    .a4-sheet:last-child,
    .a4-sheet:last-of-type {
        page-break-after: auto !important;
        break-after: auto !important;
    }

    .logical-page {
        flex: 1 !important;
        height: 100% !important;
        position: relative !important;
        border: none !important;
        overflow: hidden !important;
        padding: 3mm !important;
        box-sizing: border-box !important;
    }

    .empty-page {
        display: block !important;
    }
}

/* Compact Mobile Screen Responsiveness (< 768px) */
@media screen and (max-width: 768px) {
    .mobile-tabs {
        display: flex;
    }
    
    .editor-pane {
        width: 100%;
        min-width: 100%;
        height: 100vh;
        border-right: none;
    }
    
    .preview-pane {
        width: 100%;
        height: 100vh;
        padding: 10px;
    }
    
    body.show-preview-mobile .editor-pane {
        display: none !important;
    }
    
    body:not(.show-preview-mobile) .preview-pane {
        display: none !important;
    }
    
    .controls {
        gap: 6px;
        font-size: 11px;
    }

    .controls input[type="number"] {
        width: 42px;
    }

    body.show-preview-mobile .exit-preview-mobile-btn {
        display: flex;
        align-items: center;
        gap: 6px;
    }
}

.exit-preview-mobile-btn {
    display: none;
    position: fixed;
    bottom: 20px;
    right: 20px;
    background: #000;
    color: #fff;
    border: none;
    padding: 10px 16px;
    border-radius: 20px;
    font-size: 13px;
    font-weight: 600;
    box-shadow: 0 4px 12px rgba(0,0,0,0.25);
    cursor: pointer;
    z-index: 1000;
}
