/* ===== CSS Variables ===== */
:root {
    --primary-color: #4f46e5;
    --primary-hover: #4338ca;
    --primary-light: #eef2ff;
    --secondary-color: #64748b;
    --secondary-hover: #475569;
    --background-color: #f8fafc;
    --surface-color: #ffffff;
    --border-color: #e2e8f0;
    --text-primary: #1e293b;
    --text-secondary: #64748b;
    --text-muted: #94a3b8;
    --success-color: #10b981;
    --danger-color: #ef4444;
    --shadow-sm: 0 1px 2px 0 rgb(0 0 0 / 0.05);
    --shadow: 0 1px 3px 0 rgb(0 0 0 / 0.1), 0 1px 2px -1px rgb(0 0 0 / 0.1);
    --shadow-md: 0 4px 6px -1px rgb(0 0 0 / 0.1), 0 2px 4px -2px rgb(0 0 0 / 0.1);
    --shadow-lg: 0 10px 15px -3px rgb(0 0 0 / 0.1), 0 4px 6px -4px rgb(0 0 0 / 0.1);
    --radius-sm: 0.375rem;
    --radius: 0.5rem;
    --radius-md: 0.625rem;
    --radius-lg: 0.75rem;
    --radius-xl: 1rem;
    --transition: all 0.2s cubic-bezier(0.4, 0, 0.2, 1);
}

/* ===== Reset & Base ===== */
*, *::before, *::after {
    box-sizing: border-box;
    margin: 0;
    padding: 0;
}

html {
    font-size: 16px;
    -webkit-font-smoothing: antialiased;
    -moz-osx-font-smoothing: grayscale;
}

body {
    font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif, "Apple Color Emoji", "Segoe UI Emoji";
    background-color: var(--background-color);
    color: var(--text-primary);
    line-height: 1.6;
    min-height: 100vh;
}

/* ===== App Container ===== */
.app-container {
    display: flex;
    flex-direction: column;
    min-height: 100vh;
}

/* ===== Header ===== */
.app-header {
    background: linear-gradient(135deg, var(--primary-color), #6366f1);
    color: white;
    padding: 1rem 1.5rem;
    box-shadow: var(--shadow-md);
    position: relative;
    z-index: 10;
}

.header-content {
    max-width: 1600px;
    margin: 0 auto;
    display: flex;
    justify-content: space-between;
    align-items: center;
    flex-wrap: wrap;
    gap: 1rem;
}

.logo {
    display: flex;
    align-items: center;
    gap: 0.75rem;
}

.logo-icon {
    width: 28px;
    height: 28px;
    color: white;
}

.logo h1 {
    font-size: 1.5rem;
    font-weight: 700;
    letter-spacing: -0.025em;
}

.tagline {
    font-size: 0.875rem;
    opacity: 0.9;
    font-weight: 500;
}

/* ===== Toolbar ===== */
.toolbar {
    background: var(--surface-color);
    border-bottom: 1px solid var(--border-color);
    padding: 0.75rem 1.5rem;
    display: flex;
    justify-content: space-between;
    align-items: center;
    flex-wrap: wrap;
    gap: 1rem;
    position: sticky;
    top: 0;
    z-index: 5;
}

.toolbar-left,
.toolbar-right {
    display: flex;
    align-items: center;
    gap: 0.75rem;
}

/* ===== Buttons ===== */
.btn {
    display: inline-flex;
    align-items: center;
    gap: 0.5rem;
    padding: 0.625rem 1rem;
    border: none;
    border-radius: var(--radius);
    font-size: 0.875rem;
    font-weight: 500;
    cursor: pointer;
    transition: var(--transition);
    white-space: nowrap;
}

.btn-icon {
    width: 16px;
    height: 16px;
}

.btn-primary {
    background: var(--primary-color);
    color: white;
    box-shadow: var(--shadow-sm);
}

.btn-primary:hover {
    background: var(--primary-hover);
    transform: translateY(-1px);
    box-shadow: var(--shadow-md);
}

.btn-primary:active {
    transform: translateY(0);
}

.btn-secondary {
    background: var(--surface-color);
    color: var(--text-secondary);
    border: 1px solid var(--border-color);
}

.btn-secondary:hover {
    background: var(--background-color);
    color: var(--text-primary);
    border-color: var(--text-muted);
}

.toolbar-info {
    display: flex;
    gap: 1rem;
    font-size: 0.875rem;
    color: var(--text-secondary);
    font-weight: 500;
}

/* ===== Main Content ===== */
.main-content {
    flex: 1;
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 1rem;
    padding: 1rem;
    max-width: 1600px;
    margin: 0 auto;
    width: 100%;
}

@media (max-width: 768px) {
    .main-content {
        grid-template-columns: 1fr;
    }
}

/* ===== Sections ===== */
.editor-section,
.preview-section {
    display: flex;
    flex-direction: column;
    background: var(--surface-color);
    border-radius: var(--radius-lg);
    border: 1px solid var(--border-color);
    box-shadow: var(--shadow);
    overflow: hidden;
}

.section-header {
    display: flex;
    align-items: center;
    gap: 0.5rem;
    padding: 0.75rem 1rem;
    background: var(--primary-light);
    border-bottom: 1px solid var(--border-color);
}

.section-icon {
    width: 18px;
    height: 18px;
    color: var(--primary-color);
}

.section-header h2 {
    font-size: 0.875rem;
    font-weight: 600;
    color: var(--primary-color);
    flex: 1;
}

.section-badges {
    display: flex;
    gap: 0.375rem;
}

.badge {
    display: inline-flex;
    align-items: center;
    padding: 0.25rem 0.5rem;
    background: rgba(79, 70, 229, 0.1);
    color: var(--primary-color);
    font-size: 0.625rem;
    font-weight: 600;
    text-transform: uppercase;
    letter-spacing: 0.05em;
    border-radius: var(--radius-sm);
}

.badge-preview {
    background: rgba(16, 185, 129, 0.1);
    color: var(--success-color);
}

/* ===== Editor ===== */
.editor-container {
    flex: 1;
    display: flex;
    flex-direction: column;
    min-height: 500px;
}

.markdown-editor {
    flex: 1;
    width: 100%;
    padding: 1rem;
    border: none;
    background: var(--surface-color);
    font-family: "SF Mono", "Monaco", "Inconsolata", "Roboto Mono", Consolas, "Courier New", monospace;
    font-size: 0.875rem;
    line-height: 1.7;
    color: var(--text-primary);
    resize: none;
    outline: none;
    tab-size: 4;
}

.markdown-editor::placeholder {
    color: var(--text-muted);
}

/* ===== Preview ===== */
.preview-container {
    flex: 1;
    padding: 1rem;
    background: #fafafa;
    overflow: auto;
    min-height: 500px;
}

.pdf-preview {
    background: white;
    min-height: 100%;
    padding: 2rem;
    border-radius: var(--radius);
    box-shadow: var(--shadow-sm);
}

/* ===== Markdown Body Styles ===== */
.markdown-body {
    color: #24292e;
    font-size: 14px;
    line-height: 1.6;
    word-wrap: break-word;
}

.markdown-body h1,
.markdown-body h2,
.markdown-body h3,
.markdown-body h4,
.markdown-body h5,
.markdown-body h6 {
    margin-top: 24px;
    margin-bottom: 16px;
    font-weight: 600;
    line-height: 1.25;
}

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

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

.markdown-body h3 { font-size: 1.25em; }
.markdown-body h4 { font-size: 1em; }
.markdown-body h5 { font-size: 0.875em; }
.markdown-body h6 { font-size: 0.85em; color: #6a737d; }

.markdown-body p { margin-bottom: 16px; }

.markdown-body a {
    color: #0366d6;
    text-decoration: none;
}

.markdown-body a:hover { text-decoration: underline; }

.markdown-body ul,
.markdown-body ol {
    padding-left: 2em;
    margin-bottom: 16px;
}

.markdown-body ul ul,
.markdown-body ul ol,
.markdown-body ol ol,
.markdown-body ol ul {
    margin-top: 0;
    margin-bottom: 0;
}

.markdown-body li { margin-bottom: 0.25em; }

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

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

.markdown-body pre {
    padding: 16px;
    overflow: auto;
    font-size: 85%;
    line-height: 1.45;
    background-color: #f6f8fa;
    border-radius: 6px;
    margin-bottom: 16px;
}

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

.markdown-body table {
    display: block;
    width: 100%;
    overflow: auto;
    border-spacing: 0;
    border-collapse: collapse;
    margin-bottom: 16px;
}

.markdown-body table th,
.markdown-body table td {
    padding: 6px 13px;
    border: 1px solid #dfe2e5;
}

.markdown-body table tr {
    background-color: #fff;
    border-top: 1px solid #c6cbd1;
}

.markdown-body table tr:nth-child(2n) { background-color: #f6f8fa; }

.markdown-body table th {
    font-weight: 600;
    background-color: #f6f8fa;
}

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

.markdown-body img {
    max-width: 100%;
    box-sizing: content-box;
}

/* ===== Footer ===== */
.app-footer {
    background: var(--surface-color);
    border-top: 1px solid var(--border-color);
    padding: 1rem 1.5rem;
    margin-top: auto;
}

.footer-content {
    max-width: 1600px;
    margin: 0 auto;
    display: flex;
    justify-content: space-between;
    align-items: center;
    flex-wrap: wrap;
    gap: 1rem;
    font-size: 0.875rem;
}

.tech-stack {
    display: flex;
    align-items: center;
    gap: 0.5rem;
    color: var(--text-secondary);
}

.tech-item {
    padding: 0.25rem 0.5rem;
    background: var(--background-color);
    border-radius: var(--radius-sm);
    font-weight: 500;
}

.footer-info {
    color: var(--text-muted);
}

/* ===== Modal ===== */
.modal {
    display: none;
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background: rgba(0, 0, 0, 0.5);
    z-index: 100;
    justify-content: center;
    align-items: center;
    padding: 1rem;
}

.modal.active {
    display: flex;
}

.modal-content {
    background: var(--surface-color);
    border-radius: var(--radius-lg);
    max-width: 600px;
    width: 100%;
    max-height: 90vh;
    overflow: auto;
    box-shadow: var(--shadow-lg);
    animation: modalSlideIn 0.3s ease-out;
}

@keyframes modalSlideIn {
    from {
        opacity: 0;
        transform: translateY(-20px);
    }
    to {
        opacity: 1;
        transform: translateY(0);
    }
}

.modal-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 1rem 1.5rem;
    border-bottom: 1px solid var(--border-color);
}

.modal-header h3 {
    display: flex;
    align-items: center;
    gap: 0.5rem;
    font-size: 1.125rem;
    font-weight: 600;
}

.modal-icon {
    width: 20px;
    height: 20px;
    color: var(--primary-color);
}

.modal-close {
    background: none;
    border: none;
    font-size: 1.5rem;
    color: var(--text-muted);
    cursor: pointer;
    padding: 0.25rem;
    line-height: 1;
    transition: var(--transition);
}

.modal-close:hover {
    color: var(--text-primary);
}

.modal-body {
    padding: 1.5rem;
}

.help-section {
    margin-bottom: 1.5rem;
}

.help-section:last-child {
    margin-bottom: 0;
}

.help-section h4 {
    font-size: 0.875rem;
    font-weight: 600;
    color: var(--text-primary);
    margin-bottom: 0.75rem;
    padding-bottom: 0.5rem;
    border-bottom: 1px solid var(--border-color);
}

.help-section ul {
    list-style: none;
    padding-left: 0;
}

.help-section li {
    padding: 0.375rem 0;
    color: var(--text-secondary);
    font-size: 0.875rem;
}

.help-section code {
    background: var(--primary-light);
    color: var(--primary-color);
    padding: 0.125rem 0.375rem;
    border-radius: var(--radius-sm);
    font-family: monospace;
    font-size: 0.8em;
}

/* ===== Scrollbar ===== */
::-webkit-scrollbar {
    width: 8px;
    height: 8px;
}

::-webkit-scrollbar-track {
    background: transparent;
}

::-webkit-scrollbar-thumb {
    background: var(--border-color);
    border-radius: 4px;
}

::-webkit-scrollbar-thumb:hover {
    background: var(--text-muted);
}

/* ===== Print Styles (for PDF generation) ===== */
@media print {
    .app-header,
    .toolbar,
    .app-footer,
    .modal {
        display: none !important;
    }
    
    .main-content {
        display: block;
        padding: 0;
    }
    
    .editor-section {
        display: none;
    }
    
    .preview-section {
        border: none;
        box-shadow: none;
    }
    
    .preview-container {
        background: white;
        padding: 0;
    }
    
    .pdf-preview {
        box-shadow: none;
    }
    
    /* Page break and content protection styles */
    * {
        page-break-inside: avoid;
        page-break-after: avoid;
        page-break-before: avoid;
    }
    
    /* Allow page breaks after certain elements */
    p,
    ul,
    ol,
    blockquote,
    table,
    pre {
        page-break-inside: auto;
    }
    
    /* Add page spacing */
    body {
        margin: 0;
        padding: 0;
    }
    
    .pdf-preview {
        padding: 20mm;
        margin: 0;
    }
    
    /* Page break before h1 */
    .markdown-body h1 {
        page-break-before: always;
        margin-top: 0;
    }
    
    /* PDF page break marker */
    .markdown-body .pdf-page-break,
    .pdf-page-break {
        page-break-before: always !important;
        page-break-inside: avoid !important;
        height: 0;
        overflow: hidden;
        margin: 0 0 10mm 0;
        padding: 0;
        clear: both;
        border: none !important;
        position: static !important;
        line-height: 0;
    }
    
    /* Hide PDF page break marker text in print */
    .markdown-body .pdf-page-break::before,
    .pdf-page-break::before {
        display: none !important;
        content: none !important;
    }
    
    /* Ensure page break marker is not affected by parent elements */
    .markdown-body *:has(> .pdf-page-break) {
        page-break-inside: avoid !important;
        page-break-after: avoid !important;
    }
    
    /* Ensure first page has proper margin */
    .markdown-body :first-child {
        page-break-before: avoid;
        margin-top: 10mm !important;
    }
    
    /* Add proper margins to headings */
    .markdown-body h1,
    .markdown-body h2,
    .markdown-body h3,
    .markdown-body h4,
    .markdown-body h5,
    .markdown-body h6 {
        margin-top: 16px !important;
        margin-bottom: 12px !important;
    }
    
    /* Allow list items to break across pages but prevent line cutting */
    .markdown-body ul,
    .markdown-body ol {
        page-break-inside: auto !important;
    }
    
    .markdown-body li {
        page-break-inside: avoid !important;
        page-break-after: auto !important;
    }
    
    /* Allow paragraphs and blockquotes to break across pages */
    .markdown-body p,
    .markdown-body blockquote {
        page-break-inside: auto !important;
        break-inside: auto !important;
    }
    
    /* Prevent headings from breaking */
    .markdown-body h1,
    .markdown-body h2,
    .markdown-body h3,
    .markdown-body h4,
    .markdown-body h5,
    .markdown-body h6 {
        page-break-inside: avoid !important;
        break-inside: avoid !important;
    }
    
    /* Allow code blocks to break if needed */
    .markdown-body pre,
    .markdown-body code {
        page-break-inside: auto !important;
        break-inside: auto !important;
    }
    
    /* Allow table cells to break but not cut lines */
    .markdown-body table {
        page-break-inside: auto !important;
        break-inside: auto !important;
    }
    
    .markdown-body table td,
    .markdown-body table th {
        page-break-inside: avoid !important;
        break-inside: avoid !important;
    }
    
    /* General rule: allow most elements to break */
    .markdown-body * {
        page-break-inside: auto !important;
        break-inside: auto !important;
    }
    
    /* Exceptions - prevent these specific elements from breaking */
    .markdown-body h1,
    .markdown-body h2,
    .markdown-body h3,
    .markdown-body h4,
    .markdown-body h5,
    .markdown-body h6,
    .markdown-body li,
    .markdown-body img,
    .markdown-body table td,
    .markdown-body table th {
        page-break-inside: avoid !important;
        break-inside: avoid !important;
    }
    
    /* Allow list containers to break */
    .markdown-body ul,
    .markdown-body ol,
    .markdown-body dl {
        page-break-inside: auto !important;
        break-inside: auto !important;
    }
    
    /* Add spacing between pages */
    @page {
        margin: 10mm;
        size: A4 portrait;
    }
    
    /* Add proper padding for PDF preview */
    .pdf-preview {
        padding: 10mm;
        margin: 0;
    }
}

/* ===== Loading State ===== */
.btn:disabled {
    opacity: 0.6;
    cursor: not-allowed;
    transform: none !important;
}

.btn.loading {
    position: relative;
    color: transparent;
}

.btn.loading::after {
    content: "";
    position: absolute;
    width: 16px;
    height: 16px;
    top: 50%;
    left: 50%;
    margin-left: -8px;
    margin-top: -8px;
    border: 2px solid #ffffff;
    border-radius: 50%;
    border-top-color: transparent;
    animation: spinner 0.8s linear infinite;
}

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

/* ===== Responsive ===== */
@media (max-width: 768px) {
    .header-content {
        flex-direction: column;
        text-align: center;
    }
    
    .toolbar {
        flex-direction: column;
        align-items: stretch;
    }
    
    .toolbar-left,
    .toolbar-right {
        justify-content: center;
        flex-wrap: wrap;
    }
    
    .main-content {
        grid-template-columns: 1fr;
        padding: 0.5rem;
    }
    
    .preview-container {
        padding: 0.5rem;
    }
    
    .pdf-preview {
        padding: 1rem;
    }
}

/* ===== PDF Page Break Marker (for preview) ===== */
.pdf-page-break {
    border-top: 1px dashed #e2e8f0;
    margin: 10px 0;
    position: relative;
}

.pdf-page-break::before {
    content: 'PDF Page Break';
    position: absolute;
    top: -10px;
    left: 50%;
    transform: translateX(-50%);
    background: var(--background-color);
    padding: 0 10px;
    font-size: 12px;
    color: var(--text-muted);
    font-weight: 500;
}

/* ===== Focus States ===== */
.btn:focus,
.markdown-editor:focus {
    outline: 2px solid var(--primary-color);
    outline-offset: 2px;
}

.markdown-editor:focus {
    outline: none;
    box-shadow: inset 0 0 0 2px var(--primary-light);
}

/* ===== Selection ===== */
::selection {
    background: var(--primary-light);
    color: var(--primary-color);
}
