/* Modern CSS Reset and Base Styles */
* {
    margin: 0;
    padding: 0;
    box-sizing: border-box;
}

/*
 * Latin Modern Roman webfonts are provided by
 * https://lalten.github.io/lmweb/style/latinmodern-roman.css linked in index.html.
 */

body {
    font-family: 'Latin Modern Roman', 'Computer Modern Serif', 'Computer Modern', 'Times New Roman', Times, serif;
    line-height: 1.6;
    color: #333;
    background: linear-gradient(135deg, #F4F3EE 0%, #E8E6DF 100%);
    min-height: 100vh;
}

.auth-view {
    min-height: 100vh;
    display: flex;
    align-items: center;
    justify-content: center;
    padding: 24px;
}

.auth-card {
    width: 100%;
    max-width: 420px;
    background: rgba(255, 255, 255, 0.95);
    backdrop-filter: blur(10px);
    border-radius: 16px;
    padding: 32px;
    box-shadow: 0 20px 45px rgba(0, 0, 0, 0.12);
    border: 1px solid rgba(255, 255, 255, 0.3);
}

.auth-card h1 {
    font-size: 1.875rem;
    color: #2d3748;
    margin-bottom: 8px;
}

.auth-subtitle {
    color: #4a5568;
    font-size: 0.95rem;
}

.auth-form {
    display: flex;
    flex-direction: column;
    gap: 12px;
    margin-top: 20px;
}

.otp-input-container {
    display: flex;
    flex-direction: column;
    gap: 8px;
    margin-top: 8px;
}

.auth-label {
    font-weight: 600;
    color: #4a5568;
}

.auth-input {
    padding: 12px 14px;
    border-radius: 10px;
    border: 1px solid rgba(148, 163, 184, 0.6);
    font-size: 1rem;
    transition: border-color 0.2s ease, box-shadow 0.2s ease;
}

.auth-input:focus {
    outline: none;
    border-color: #C15F3C;
    box-shadow: 0 0 0 3px rgba(193, 95, 60, 0.2);
}

.auth-submit {
    align-self: flex-start;
    padding: 10px 18px;
    background: #C15F3C !important;
    color: white !important;
    font-weight: 500;
    box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
}

.auth-submit:hover {
    background: #A34F30 !important;
    transform: translateY(-1px);
    box-shadow: 0 4px 8px rgba(0, 0, 0, 0.15);
}

.auth-submit:active {
    transform: translateY(0);
}

.auth-submit:disabled {
    opacity: 0.6;
    cursor: not-allowed;
    transform: none !important;
}

.status-message {
    margin-top: 18px;
    font-size: 0.95rem;
    border-radius: 10px;
    padding: 12px 14px;
}

.status-message.hidden {
    display: none;
}

.success {
    color: #8C3D24;
    background: #F4D7CC;
    border: 1px solid #C15F3C;
}

.container {
    max-width: 1000px;
    margin: 0 auto;
    padding: 20px;
    min-height: 100vh;
    display: flex;
    flex-direction: column;
    padding-top: 180px; /* Space for fixed header */
}

/* Header Styles - Google Docs style sticky toolbar */
.header {
    position: fixed;
    top: 0;
    left: 0;
    right: 0;
    background: #fff;
    box-shadow: 0 1px 3px rgba(0, 0, 0, 0.12), 0 1px 2px rgba(0, 0, 0, 0.08);
    padding: 12px 24px;
    z-index: 1000;
    border-bottom: 1px solid #B1ADA1;
}

.header h1 {
    color: #202124;
    margin-bottom: 8px;
    font-size: 1.25rem;
    font-weight: 500;
}

.header-top {
    display: flex;
    justify-content: space-between;
    align-items: center;
    gap: 16px;
    margin-bottom: 8px;
}

.auth-toolbar {
    display: flex;
    align-items: center;
    gap: 12px;
}

.user-badge {
    background: #F4F3EE;
    color: #3c4043;
    padding: 6px 14px;
    border-radius: 16px;
    font-size: 0.875rem;
    font-weight: 500;
    border: 1px solid #B1ADA1;
}

/* Toolbar Styles - Google Docs inspired */
.toolbar {
    display: flex;
    gap: 4px;
    flex-wrap: wrap;
    align-items: center;
}

.btn {
    background: transparent;
    color: #444;
    border: 1px solid transparent;
    padding: 6px 12px;
    border-radius: 4px;
    cursor: pointer;
    font-size: 14px;
    font-weight: 400;
    transition: all 0.15s ease;
    box-shadow: none;
}

.btn:hover {
    background: #F4F3EE;
    border-color: transparent;
}

.btn:active {
    background: #e8eaed;
}

.btn:disabled {
    opacity: 0.38;
    cursor: not-allowed;
    background: transparent;
}

.format-btn {
    background: transparent;
    font-weight: 600;
}

.format-btn:hover {
    background: #F4F3EE;
}

.toggle-btn {
    background: transparent;
    font-family: monospace;
    font-size: 14px;
    color: #444;
}

.toggle-btn:hover {
    background: #F4F3EE;
}

.toggle-btn.active {
    background: #F4D7CC;
    color: #C15F3C;
}

.btn-secondary {
    background: transparent;
    color: #5f6368;
    border: 1px solid #B1ADA1;
}

.btn-secondary:hover {
    background: #F4F3EE;
    border-color: #8C8980;
}

/* Auth toolbar sign-out button - more prominent */
.auth-toolbar .btn-secondary {
    background: #F4F3EE;
    color: #444;
    border: 1px solid #B1ADA1;
    padding: 6px 14px;
}

.auth-toolbar .btn-secondary:hover {
    background: #E8E6DF;
    border-color: #8C8980;
}

.separator {
    width: 1px;
    height: 20px;
    background: #B1ADA1;
    margin: 0 8px;
}

/* Editor Wrapper */
.editor-wrapper {
    flex: 1;
    display: flex;
    flex-direction: column;
    min-height: 0;
}

/* WYSIWYG Editor Styles */
.editor-container {
    flex: 1;
    background: #E8E6DF;
    border-radius: 15px;
    padding: 24px;
    box-shadow: 0 8px 32px rgba(0, 0, 0, 0.1);
    border: 1px solid #B1ADA1;
    outline: none;
    overflow-y: auto;
    font-size: 16px;
    line-height: 1.6;
    min-height: 500px;
    display: flex;
    justify-content: center;
}

.editor-container:focus {
    box-shadow: 0 8px 32px rgba(0, 0, 0, 0.15), 0 0 0 3px rgba(193, 95, 60, 0.1);
}

/* Page canvas (Google Docs/Word style) */
.page-canvas {
    width: 816px; /* 8.5in @ 96dpi */
    min-height: 1056px; /* ~11in @ 96dpi */
    background: white;
    box-shadow: 0 10px 30px rgba(0, 0, 0, 0.12);
    border: 1px solid #B1ADA1;
    border-radius: 8px;
    padding: 96px; /* 1 inch margins */
    margin: 24px auto;
    color: #111827;
    font-family: 'Latin Modern Roman', 'Computer Modern Serif', 'Computer Modern', 'Times New Roman', Times, serif;
}

.page-canvas:focus {
    outline: none;
}

/* Typography in Editor */
.page-canvas h1 {
    font-size: 2.5rem;
    margin: 2rem 0 1.5rem 0;
    color: #111827;
    font-weight: 700;
}

/* LaTeX-like article title block */
.article-title-block {
    text-align: center;
    margin-top: 0.5rem;
    margin-bottom: 2.5rem;
    padding: 1rem;
    border-radius: 8px;
    cursor: pointer;
    transition: all 0.2s ease;
    position: relative;
}

.article-title-block:hover {
    background: #FDF9F5;
    border: 1px solid #E8DED2;
}

.article-title-block .article-title {
    font-size: 2rem;
    font-weight: 700;
    margin: 0 0 0.5rem 0;
}

.article-title-block .article-authors {
    font-size: 1rem;
    font-style: italic;
    margin: 0.25rem 0;
}

.article-title-block .article-date {
    font-size: 0.95rem;
    color: #4b5563;
    margin-top: 0.25rem;
}

/* Edit indicator that appears on hover */
.article-title-block::after {
    content: '✎ Edit title';
    position: absolute;
    top: -8px;
    right: 8px;
    background: #C15F3C;
    color: white;
    padding: 4px 10px;
    border-radius: 4px;
    font-size: 0.75rem;
    font-weight: 600;
    opacity: 0;
    transition: opacity 0.2s ease;
    pointer-events: none;
}

.article-title-block:hover::after {
    opacity: 1;
}

.page-canvas h2 {
    font-size: 2rem;
    margin: 2rem 0 1rem 0;
    color: #111827;
    font-weight: 600;
}

.page-canvas h3 {
    font-size: 1.5rem;
    margin: 1.5rem 0 0.75rem 0;
    color: #111827;
    font-weight: 600;
}

/* LaTeX-style automatic section numbering for headings */
.page-canvas {
    counter-reset: section subsection subsubsection;
}

/* Section (h1) numbering, exclude title block h1 */
.page-canvas h1:not(.article-title) {
    counter-increment: section;
    counter-reset: subsection subsubsection;
}

.page-canvas h1:not(.article-title)::before {
    content: counter(section) ". ";
    color: #4b5563; /* subtle gray */
}

/* Subsection (h2) numbering */
.page-canvas h2 {
    counter-increment: subsection;
    counter-reset: subsubsection;
}

.page-canvas h2::before {
    content: counter(section) "." counter(subsection) " ";
    color: #4b5563;
}

/* Subsubsection (h3) numbering */
.page-canvas h3 {
    counter-increment: subsubsection;
}

.page-canvas h3::before {
    content: counter(section) "." counter(subsection) "." counter(subsubsection) " ";
    color: #4b5563;
}

.page-canvas p {
    margin: 1rem 0;
    color: #111827;
}

.page-canvas ul, .page-canvas ol {
    margin: 1rem 0;
    padding-left: 2rem;
}

.page-canvas li {
    margin: 0.5rem 0;
    color: #111827;
}

.page-canvas strong {
    font-weight: 700;
    color: #111827;
}

.page-canvas em {
    font-style: italic;
    color: #111827;
}

.page-canvas code {
    background: #f7fafc;
    border: 1px solid #e2e8f0;
    border-radius: 4px;
    padding: 2px 6px;
    font-family: 'Computer Modern Typewriter', 'Computer Modern Mono', 'Monaco', 'Menlo', 'Ubuntu Mono', monospace;
    font-size: 0.9em;
    color: #e53e3e;
}

/* Math Styles */
.math-inline {
    display: inline-block;
    background: #FDF9F5;
    border: 1px solid #E8DED2;
    border-radius: 6px;
    padding: 2px 6px;
    margin: 0 2px;
    font-family: 'KaTeX_Main', 'Computer Modern Serif', 'Computer Modern', 'Latin Modern Roman', 'Times New Roman', serif;
    color: #C15F3C;
    cursor: pointer;
    transition: all 0.2s ease;
}

.math-inline:hover {
    background: #F4F3EE;
    border-color: #C15F3C;
}

.math-display {
    display: block;
    background: #FDF9F5;
    border: 1px solid #E8DED2;
    border-radius: 8px;
    padding: 16px;
    margin: 16px 0;
    text-align: center;
    font-family: 'KaTeX_Main', 'Computer Modern Serif', 'Computer Modern', 'Latin Modern Roman', 'Times New Roman', serif;
    color: #C15F3C;
    cursor: pointer;
    transition: all 0.2s ease;
}

.math-display:hover {
    background: #F4F3EE;
    border-color: #C15F3C;
}

/* Equation Block with Label */
.equation-block {
    display: block;
    background: #FDF9F5;
    border: 2px solid #B1ADA1;
    border-radius: 10px;
    padding: 20px;
    margin: 20px 0;
    cursor: pointer;
    transition: all 0.2s ease;
    position: relative;
}

.equation-block:hover {
    background: #F4F3EE;
    border-color: #C15F3C;
    box-shadow: 0 4px 12px rgba(0, 0, 0, 0.08);
}

.equation-block .equation-content {
    text-align: center;
    font-size: 1.1em;
    color: #0f172a;
    padding: 12px 0;
    min-height: 40px;
}

.equation-block .equation-label {
    display: flex;
    align-items: center;
    gap: 8px;
    margin-top: 12px;
    padding-top: 12px;
    border-top: 1px solid #e2e8f0;
    font-size: 0.9em;
    color: #64748b;
}

.equation-block .equation-label-text {
    font-weight: 600;
    color: #475569;
}

.equation-block .equation-number {
    position: absolute;
    right: 20px;
    top: 50%;
    transform: translateY(-50%);
    font-weight: 600;
    color: #64748b;
    font-size: 1.1em;
}

/* Equation editing overlay */
.equation-editor-overlay {
    position: fixed;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    background: rgba(0, 0, 0, 0.5);
    display: flex;
    align-items: center;
    justify-content: center;
    z-index: 2000;
}

.equation-editor-dialog {
    background: white;
    border-radius: 12px;
    padding: 24px;
    width: 90%;
    max-width: 600px;
    box-shadow: 0 20px 50px rgba(0, 0, 0, 0.3);
}

.equation-editor-dialog h3 {
    margin: 0 0 20px 0;
    color: #1e293b;
    font-size: 1.4em;
}

.equation-editor-dialog label {
    display: block;
    margin-bottom: 8px;
    font-weight: 600;
    color: #475569;
    font-size: 0.95em;
}

.equation-editor-dialog input,
.equation-editor-dialog textarea {
    width: 100%;
    padding: 10px 12px;
    border: 2px solid #cbd5e0;
    border-radius: 6px;
    font-size: 1em;
    margin-bottom: 16px;
    font-family: 'Computer Modern Typewriter', 'Monaco', 'Menlo', monospace;
}

.equation-editor-dialog textarea {
    min-height: 100px;
    resize: vertical;
    font-size: 0.95em;
}

.equation-editor-dialog input:focus,
.equation-editor-dialog textarea:focus {
    outline: none;
    border-color: #C15F3C;
    box-shadow: 0 0 0 3px rgba(193, 95, 60, 0.1);
}

.equation-editor-preview {
    background: #f8fafc;
    border: 1px solid #e2e8f0;
    border-radius: 6px;
    padding: 16px;
    margin-bottom: 16px;
    text-align: center;
    min-height: 60px;
    display: flex;
    align-items: center;
    justify-content: center;
}

.equation-editor-preview-label {
    font-size: 0.85em;
    color: #64748b;
    margin-bottom: 4px;
    font-weight: 600;
}

.equation-editor-buttons {
    display: flex;
    gap: 10px;
    justify-content: flex-end;
}

.equation-editor-buttons button {
    padding: 10px 20px;
    border: none;
    border-radius: 6px;
    font-weight: 600;
    cursor: pointer;
    transition: all 0.2s ease;
}

.equation-editor-buttons .btn-primary {
    background: #C15F3C;
    color: white;
}

.equation-editor-buttons .btn-primary:hover {
    background: #A34F30;
}

.equation-editor-buttons .btn-cancel {
    background: #e2e8f0;
    color: #475569;
}

.equation-editor-buttons .btn-cancel:hover {
    background: #cbd5e0;
}

.equation-editor-buttons .btn-delete {
    background: #fee;
    color: #c53030;
    margin-right: auto;
}

.equation-editor-buttons .btn-delete:hover {
    background: #fcc;
}

/* Title editor dialog - reuses equation editor styles but with title-specific content */
.title-editor-overlay {
    position: fixed;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    background: rgba(0, 0, 0, 0.5);
    display: flex;
    align-items: center;
    justify-content: center;
    z-index: 2000;
}

.title-editor-dialog {
    background: white;
    border-radius: 12px;
    padding: 24px;
    width: 90%;
    max-width: 600px;
    box-shadow: 0 20px 50px rgba(0, 0, 0, 0.3);
}

.title-editor-dialog h3 {
    margin: 0 0 20px 0;
    color: #1e293b;
    font-size: 1.4em;
}

.title-editor-dialog label {
    display: block;
    margin-bottom: 8px;
    font-weight: 600;
    color: #475569;
    font-size: 0.95em;
}

.title-editor-dialog input {
    width: 100%;
    padding: 10px 12px;
    border: 2px solid #cbd5e0;
    border-radius: 6px;
    font-size: 1em;
    margin-bottom: 16px;
    font-family: 'Latin Modern Roman', 'Times New Roman', serif;
}

.title-editor-dialog input:focus {
    outline: none;
    border-color: #C15F3C;
    box-shadow: 0 0 0 3px rgba(193, 95, 60, 0.1);
}

.title-editor-buttons {
    display: flex;
    gap: 10px;
    justify-content: flex-end;
    margin-top: 20px;
}

.title-editor-buttons button {
    padding: 10px 20px;
    border: none;
    border-radius: 6px;
    font-weight: 600;
    cursor: pointer;
    transition: all 0.2s ease;
}

.title-editor-buttons .btn-primary {
    background: #C15F3C;
    color: white;
}

.title-editor-buttons .btn-primary:hover {
    background: #A34F30;
}

.title-editor-buttons .btn-cancel {
    background: #e2e8f0;
    color: #475569;
}

.title-editor-buttons .btn-cancel:hover {
    background: #cbd5e0;
}

/* Reference styles */
.equation-ref {
    display: inline-block;
    background: #F4D7CC;
    border: 1px solid #C15F3C;
    border-radius: 4px;
    padding: 2px 6px;
    margin: 0 2px;
    color: #8C3D24;
    cursor: pointer;
    font-weight: 600;
    transition: all 0.2s ease;
}

.equation-ref:hover {
    background: #EBC5B5;
    border-color: #A34F30;
}

.equation-btn {
    background: transparent !important;
    color: #444 !important;
}

.equation-btn:hover {
    background: #F4F3EE !important;
}

/* LaTeX Source Mode */
#latex-source {
    flex: 1;
    border: 2px solid #e2e8f0;
    border-radius: 15px;
    padding: 20px;
    font-family: 'Computer Modern Typewriter', 'Computer Modern Mono', 'Monaco', 'Menlo', 'Ubuntu Mono', monospace;
    font-size: 14px;
    line-height: 1.6;
    resize: none;
    outline: none;
    background: #1a202c;
    color: #e2e8f0;
    min-height: 500px;
}

#latex-source:focus {
    border-color: #C15F3C;
    box-shadow: 0 0 0 3px rgba(193, 95, 60, 0.1);
}

.hidden {
    display: none !important;
}

/* Status Bar */
.status-bar {
    background: rgba(255, 255, 255, 0.9);
    border-radius: 10px;
    padding: 10px 20px;
    margin-top: 15px;
    display: flex;
    justify-content: space-between;
    align-items: center;
    font-size: 14px;
    color: #4a5568;
    box-shadow: 0 4px 16px rgba(0, 0, 0, 0.1);
}

#mode-indicator {
    font-weight: 600;
    color: #2d3748;
}

/* Math Editing States */
.math-editing {
    background: #FDF9F5 !important;
    border-color: #C15F3C !important;
    box-shadow: 0 0 0 3px rgba(193, 95, 60, 0.2) !important;
}

/* Selection and Focus States */
.editor-container::selection,
.editor-container *::selection {
    background: rgba(193, 95, 60, 0.2);
}

/* Responsive Design */
@media (max-width: 768px) {
    .container {
        padding: 10px;
        padding-top: 160px; /* Adjust for fixed header on mobile */
    }
    
    .header {
        padding: 8px 12px;
    }
    
    .header h1 {
        font-size: 1.1rem;
    }
    
    .toolbar {
        gap: 2px;
    }
    
    .btn {
        padding: 5px 8px;
        font-size: 12px;
    }
    
    .separator {
        margin: 0 4px;
    }
    
    .editor-container {
        padding: 20px;
        font-size: 14px;
    }
    
    .editor-container h1 {
        font-size: 1.8rem;
    }
    
    .editor-container h2 {
        font-size: 1.4rem;
    }
}

/* Loading and Error States */
.error {
    color: #e53e3e;
    background: #fed7d7;
    border: 1px solid #feb2b2;
    border-radius: 8px;
    padding: 12px;
    margin: 10px 0;
}

.loading {
    color: #C15F3C;
    font-style: italic;
    text-align: center;
    padding: 20px;
}

/* Placeholder styles */
.page-canvas:empty::before {
    content: "Start typing your document here...";
    color: #a0aec0;
    font-style: italic;
}

/* PDF Loading Spinner */
.pdf-spinner {
    display: flex;
    align-items: center;
    gap: 8px;
    padding: 6px 12px;
    background: #F4F3EE;
    border-radius: 4px;
    border: 1px solid #B1ADA1;
    margin-left: 8px;
}

.pdf-spinner span {
    font-size: 13px;
    font-weight: 400;
    color: #5f6368;
}

.spinner {
    width: 16px;
    height: 16px;
    border: 2px solid #B1ADA1;
    border-top-color: #C15F3C;
    border-radius: 50%;
    animation: spin 0.8s linear infinite;
}

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

/* Documents panel styles */
.documents-panel {
    position: fixed;
    right: 0;
    top: 0;
    width: 350px;
    height: 100vh;
    background: white;
    box-shadow: -2px 0 10px rgba(0, 0, 0, 0.15);
    z-index: 1100; /* Above the fixed header */
    display: flex;
    flex-direction: column;
    transition: transform 0.3s ease;
}

.documents-panel.hidden {
    transform: translateX(100%);
}

.documents-header {
    padding: 20px;
    border-bottom: 1px solid #B1ADA1;
    display: flex;
    justify-content: space-between;
    align-items: center;
    background: #F4F3EE;
}

.documents-header h2 {
    margin: 0;
    font-size: 20px;
    font-weight: 600;
    color: #2d3748;
}

.close-btn {
    background: none;
    border: none;
    font-size: 24px;
    cursor: pointer;
    color: #718096;
    padding: 0;
    width: 30px;
    height: 30px;
    display: flex;
    align-items: center;
    justify-content: center;
    border-radius: 4px;
    transition: all 0.2s ease;
}

.close-btn:hover {
    background: #E8E6DF;
    color: #2d3748;
}

.documents-list {
    flex: 1;
    overflow-y: auto;
    padding: 10px;
}

.document-item {
    padding: 12px 15px;
    margin-bottom: 8px;
    background: #F4F3EE;
    border: 1px solid #B1ADA1;
    border-radius: 6px;
    cursor: pointer;
    transition: all 0.2s ease;
}

.document-item:hover {
    background: #E8E6DF;
    border-color: #8C8980;
    transform: translateY(-1px);
    box-shadow: 0 2px 4px rgba(0, 0, 0, 0.05);
}

.document-item.active {
    background: #F4D7CC;
    border-color: #C15F3C;
}

.document-title {
    font-weight: 600;
    color: #2d3748;
    margin-bottom: 4px;
    font-size: 14px;
}

.document-meta {
    font-size: 12px;
    color: #718096;
    display: flex;
    justify-content: space-between;
    align-items: center;
}

.document-delete {
    background: none;
    border: none;
    color: #e53e3e;
    cursor: pointer;
    padding: 4px 8px;
    border-radius: 4px;
    font-size: 12px;
    transition: all 0.2s ease;
}

.document-delete:hover {
    background: #fff5f5;
}

.loading-text {
    text-align: center;
    color: #718096;
    padding: 20px;
}

.no-documents {
    text-align: center;
    color: #718096;
    padding: 40px 20px;
}

.no-documents p {
    margin: 0 0 10px 0;
}

/* Print styles */
@media print {
    body, .container, .editor-wrapper {
        background: white !important;
    }
    .header, .status-bar { display: none !important; }
    .editor-container {
        box-shadow: none;
        border: none;
        padding: 0;
    }
    .page-canvas {
        width: auto;
        min-height: auto;
        margin: 0;
        padding: 1in;
        box-shadow: none;
        border: none;
    }
    .documents-panel {
        display: none !important;
    }
}
