/* General Body and Layout Styles */
body {
    font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol";
    line-height: 1.6;
    color: #24292e;
    background-color: #f6f8fa;
    margin: 0;
    padding: 20px;
    display: flex;
    flex-direction: column;
    align-items: center;
}

header {
    width: 100%;
    max-width: 1400px;
    margin-bottom: 20px;
    display: flex;
    justify-content: space-between;
    align-items: center;
}

h1.title {
    font-size: 24px;
    color: #24292e;
}

.container {
    display: flex;
    width: 100%;
    max-width: 1400px;
    height: calc(100vh - 100px); /* Adjusted for header */
}

#editor-container, #preview-container {
    flex: 1;
    display: flex;
    flex-direction: column;
    height: 100%;
    border: 1px solid #d1d5da;
    border-radius: 6px;
    background-color: #fff;
    box-shadow: 0 1px 3px rgba(0,0,0,0.1);
    min-width: 0; /* Allow panels to shrink beyond their content's intrinsic size */
}

/* Resizer Handle */
#resizer {
    flex: 0 0 10px;
    cursor: col-resize;
    user-select: none;
    background: #f6f8fa; /* Match body background */
    position: relative;
}

#resizer:hover::before {
    background-color: #0366d6;
}

#resizer::before {
    content: '';
    position: absolute;
    top: 0;
    bottom: 0;
    left: 4px;
    width: 2px;
    background-color: #d1d5da;
    transition: background-color 0.2s ease;
}

/* Viewer Mode for full-screen preview */
body.viewer-mode #editor-container,
body.viewer-mode #resizer {
    display: none;
}

body.viewer-mode #preview-container {
    flex-basis: 100%;
    max-width: 100%;
}

.panel-header {
    padding: 8px 12px;
    background-color: #f6f8fa;
    border-bottom: 1px solid #d1d5da;
    font-weight: 600;
    border-top-left-radius: 6px;
    border-top-right-radius: 6px;
}

#editor {
    flex: 1;
    font-family: "SFMono-Regular", Consolas, "Liberation Mono", Menlo, Courier, monospace;
    font-size: 14px;
    line-height: 1.5;
    padding: 15px;
    border: none;
    resize: none;
    outline: none;
    border-bottom-left-radius: 6px;
    border-bottom-right-radius: 6px;
}

#preview {
    flex: 1;
    padding: 20px;
    overflow-y: auto;
}

/* Markdown Content Styling */
#preview h1, #preview h2, #preview h3, #preview h4, #preview h5, #preview h6 {
    margin-top: 24px;
    margin-bottom: 16px;
    font-weight: 600;
    line-height: 1.25;
}

#preview h1 {
    padding-bottom: 0.3em;
    font-size: 2em;
    border-bottom: 1px solid #eaecef;
}

#preview h2 {
    padding-bottom: 0.3em;
    font-size: 1.5em;
    border-bottom: 1px solid #eaecef;
}

#preview h3 {
    font-size: 1.25em;
}

#preview h4 {
    font-size: 1em;
}

#preview h5 {
    font-size: 0.875em;
}

#preview h6 {
    font-size: 0.85em;
    color: #6a737d;
}

#preview p {
    margin-top: 0;
    margin-bottom: 16px;
}

#preview a {
    color: #0366d6;
    text-decoration: none;
}

#preview a:hover {
    text-decoration: underline;
}

#preview ul, #preview ol {
    padding-left: 2em;
    margin-top: 0;
    margin-bottom: 16px;
}

#preview li {
    margin-bottom: 0.25em;
}

#preview blockquote {
    margin: 0 0 16px 0;
    padding: 0 1em;
    color: #6a737d;
    border-left: 0.25em solid #dfe2e5;
}

#preview code {
    font-family: "SFMono-Regular", Consolas, "Liberation Mono", Menlo, Courier, monospace;
    font-size: 85%;
    padding: 0.2em 0.4em;
    margin: 0;
    background-color: rgba(27,31,35,0.05);
    border-radius: 3px;
}

#preview pre {
    padding: 16px;
    overflow: auto;
    font-size: 85%;
    line-height: 1.45;
    background-color: #f6f8fa;
    border-radius: 3px;
    margin-bottom: 16px;
    word-wrap: normal;
}

#preview pre code {
    display: inline;
    padding: 0;
    margin: 0;
    overflow: visible;
    line-height: inherit;
    word-wrap: normal;
    background-color: transparent;
    border: 0;
}

#preview img {
    max-width: 100%;
    height: auto;
    box-sizing: border-box;
    background-color: #fff;
}

#preview table {
    width: 100%;
    border-collapse: collapse;
    margin-top: 0;
    margin-bottom: 16px;
    display: block;
    overflow: auto;
}

#preview th, #preview td {
    padding: 6px 13px;
    border: 1px solid #dfe2e5;
}

#preview th {
    font-weight: 600;
}

#preview tr {
    background-color: #fff;
    border-top: 1px solid #c6cbd1;
}

#preview tr:nth-child(2n) {
    background-color: #f6f8fa;
}

#preview hr {
    height: 0.25em;
    padding: 0;
    margin: 24px 0;
    background-color: #e1e4e8;
    border: 0;
}

/* Mermaid diagram support */
.mermaid {
    text-align: center;
    margin-bottom: 16px;
    background-color: #fff; /* Ensure diagrams have a white background if needed */
}

/* Controls */
.controls {
    display: flex;
    gap: 10px;
}

.controls button {
    padding: 8px 16px;
    font-size: 14px;
    font-weight: 500;
    color: #24292e;
    background-color: #fafbfc;
    border: 1px solid rgba(27,31,35,0.15);
    border-radius: 6px;
    cursor: pointer;
    transition: background-color 0.2s;
}

.controls button:hover {
    background-color: #f3f4f6;
}

.controls button.primary {
    color: #fff;
    background-color: #2ea44f;
    border-color: rgba(27,31,35,0.15);
}

.controls button.primary:hover {
    background-color: #2c974b;
}

/* Print-specific styles for PDF generation */
@media print {
    body {
        padding: 0;
        background-color: #fff;
    }
    header, .container, .controls {
        display: none;
    }
    #preview-container {
        border: none;
        box-shadow: none;
    }
    #preview {
        display: block;
        padding: 0;
        overflow: visible;
    }
    /* Avoid page breaks inside code blocks and blockquotes */
    pre, blockquote {
        page-break-inside: avoid;
    }
    /* Ensure headings don't get separated from the following paragraph */
    h1, h2, h3, h4, h5, h6 {
        page-break-after: avoid;
    }
}

/* Toolbar */
.toolbar {
    display: flex;
    flex-wrap: wrap;
    gap: 5px;
    padding: 5px;
    background-color: #f6f8fa;
    border-bottom: 1px solid #d1d5da;
}

.toolbar button {
    background-color: transparent;
    border: 1px solid transparent;
    border-radius: 4px;
    padding: 5px;
    cursor: pointer;
    font-size: 16px;
    line-height: 1;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    width: 30px;
    height: 30px;
}

.toolbar button:hover {
    background-color: #e1e4e8;
    border-color: #d1d5da;
}

.toolbar select {
    padding: 4px;
    border-radius: 4px;
    border: 1px solid #d1d5da;
    background-color: #fff;
}

/* Modal Styles */
.modal {
    display: none; /* Hidden by default */
    position: fixed; /* Stay in place */
    z-index: 1000; /* Sit on top */
    left: 0;
    top: 0;
    width: 100%; /* Full width */
    height: 100%; /* Full height */
    overflow: auto; /* Enable scroll if needed */
    background-color: rgba(0,0,0,0.4); /* Black w/ opacity */
}

.modal-content {
    background-color: #fefefe;
    margin: 15% auto; /* 15% from the top and centered */
    padding: 20px;
    border: 1px solid #888;
    width: 80%; /* Could be more or less, depending on screen size */
    max-width: 600px;
    border-radius: 8px;
    position: relative;
    box-shadow: 0 4px 8px 0 rgba(0,0,0,0.2),0 6px 20px 0 rgba(0,0,0,0.19);
}

.close-button {
    color: #aaa;
    float: right;
    font-size: 28px;
    font-weight: bold;
    position: absolute;
    top: 10px;
    right: 20px;
}

.close-button:hover,
.close-button:focus {
    color: black;
    text-decoration: none;
    cursor: pointer;
}

.modal-header {
    padding-bottom: 10px;
    border-bottom: 1px solid #eee;
    margin-bottom: 20px;
}

.modal-header h2 {
    margin: 0;
    font-size: 1.5em;
    border-bottom: none;
    padding-bottom: 0;
}

.modal-body {
    padding: 2px 16px;
}

.modal-footer {
    padding-top: 10px;
    border-top: 1px solid #eee;
    margin-top: 20px;
    text-align: right;
}

/* Toast Notification */
#toast {
    visibility: hidden;
    min-width: 250px;
    margin-left: -125px;
    background-color: #333;
    color: #fff;
    text-align: center;
    border-radius: 2px;
    padding: 16px;
    position: fixed;
    z-index: 1001; /* Higher than modal */
    left: 50%;
    bottom: 30px;
    font-size: 17px;
    opacity: 0;
    transition: visibility 0s 0.5s, opacity 0.5s linear;
}

#toast.show {
    visibility: visible;
    opacity: 1;
    transition: opacity 0.5s linear;
}

/* Dark Theme */
body.dark-mode {
    background-color: #0d1117;
    color: #c9d1d9;
}

body.dark-mode header,
body.dark-mode .panel-header,
body.dark-mode #resizer {
    background-color: #161b22;
}

body.dark-mode h1.title {
    color: #c9d1d9;
}

body.dark-mode #editor-container,
body.dark-mode #preview-container {
    background-color: #0d1117;
    border-color: #30363d;
}

body.dark-mode #editor {
    background-color: #0d1117;
    color: #c9d1d9;
}

body.dark-mode #resizer::before {
    background-color: #30363d;
}

body.dark-mode #resizer:hover::before {
    background-color: #58a6ff;
}

body.dark-mode .panel-header {
    border-bottom-color: #30363d;
}

/* Dark mode markdown styles */
body.dark-mode #preview h1,
body.dark-mode #preview h2 {
    border-bottom-color: #30363d;
}

body.dark-mode #preview h6 {
    color: #8b949e;
}

body.dark-mode #preview a {
    color: #58a6ff;
}

body.dark-mode #preview blockquote {
    color: #8b949e;
    border-left-color: #30363d;
}

body.dark-mode #preview code {
    background-color: rgba(110,118,129,0.4);
}

body.dark-mode #preview pre {
    background-color: #161b22;
}

body.dark-mode #preview pre code {
    background-color: transparent;
}

body.dark-mode #preview img {
    background-color: #0d1117;
}

body.dark-mode #preview table,
body.dark-mode #preview th,
body.dark-mode #preview td {
    border-color: #30363d;
}

body.dark-mode #preview tr {
    background-color: #0d1117;
    border-top-color: #21262d;
}

body.dark-mode #preview tr:nth-child(2n) {
    background-color: #161b22;
}

body.dark-mode #preview hr {
    background-color: #30363d;
}

/* Dark mode controls */
body.dark-mode .controls button {
    color: #c9d1d9;
    background-color: #21262d;
    border-color: #30363d;
}

body.dark-mode .controls button:hover {
    background-color: #30363d;
    border-color: #8b949e;
}

body.dark-mode .controls button.primary {
    color: #fff;
    background-color: #238636;
    border-color: rgba(240,246,252,0.1);
}

body.dark-mode .controls button.primary:hover {
    background-color: #2ea043;
}

/* Dark mode modal */
body.dark-mode .modal-content {
    background-color: #161b22;
    border-color: #30363d;
}

body.dark-mode .modal-header {
    border-bottom-color: #30363d;
}

body.dark-mode .modal-footer {
    border-top-color: #30363d;
}

body.dark-mode .close-button {
    color: #8b949e;
}

body.dark-mode .close-button:hover,
body.dark-mode .close-button:focus {
    color: #c9d1d9;
}

/* Dark mode toolbar */
body.dark-mode .toolbar {
    background-color: #161b22;
    border-bottom-color: #30363d;
}

body.dark-mode .toolbar button {
    color: #c9d1d9;
}

body.dark-mode .toolbar button:hover {
    background-color: #30363d;
    border-color: #8b949e;
}

body.dark-mode .toolbar select {
    background-color: #0d1117;
    color: #c9d1d9;
    border-color: #30363d;
}