@import url('https://fonts.googleapis.com/css2?family=Inter:wght@300;400;500;600;700&family=Outfit:wght@400;500;600;700&display=swap');

:root {
    --primary: #6366f1;
    --primary-hover: #4f46e5;
    --bg-main: #f8fafc;
    --bg-sidebar: #ffffff;
    --bg-card: #ffffff;
    --text-main: #1e293b;
    --text-muted: #64748b;
    --border-color: #e2e8f0;
    --shadow-sm: 0 1px 2px 0 rgb(0 0 0 / 0.05);
    --shadow-md: 0 4px 6px -1px rgb(0 0 0 / 0.1);
    --glass-bg: rgba(255, 255, 255, 0.7);
    --glass-border: rgba(255, 255, 255, 0.3);
}

[data-theme="dark"] {
    --bg-main: #0f172a;
    --bg-sidebar: #1e293b;
    --bg-card: #1e293b;
    --text-main: #f1f5f9;
    --text-muted: #94a3b8;
    --border-color: #334155;
    --glass-bg: rgba(15, 23, 42, 0.7);
    --glass-border: rgba(255, 255, 255, 0.1);
}

* {
    margin: 0;
    padding: 0;
    box-sizing: border-box;
}

body {
    font-family: 'Inter', sans-serif;
    background-color: var(--bg-main);
    color: var(--text-main);
    line-height: 1.5;
    overflow: hidden;
    transition: background-color 0.3s ease, color 0.3s ease;
}

/* Sidebar Components */
.sidebar-section-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    margin-bottom: 1rem;
}

.section-title {
    font-size: 0.875rem;
    text-transform: uppercase;
    letter-spacing: 0.05em;
    color: var(--text-muted);
}

.add-btn {
    background: none;
    border: none;
    color: var(--primary);
    cursor: pointer;
    font-size: 1.25rem;
    transition: transform 0.2s;
}

.add-btn:hover {
    transform: scale(1.2);
}

.space-item-header {
    display: flex;
    align-items: center;
}

.edit-space-btn {
    font-size: 0.75rem;
    opacity: 0.6;
    cursor: pointer;
    color: var(--text-muted);
    transition: all 0.2s;
}

.edit-space-btn:hover {
    opacity: 1;
    color: var(--primary);
}

/* Dark Mode CKEditor Overrides */
[data-theme="dark"] .ck-editor__editable {
    background-color: #1e293b !important;
    color: #f1f5f9 !important;
    border-color: #334155 !important;
}

[data-theme="dark"] .ck.ck-toolbar {
    background-color: #1e293b !important;
    border-color: #334155 !important;
}

[data-theme="dark"] .ck.ck-button {
    color: #f1f5f9 !important;
}

[data-theme="dark"] .ck.ck-button:hover {
    background-color: #334155 !important;
}

[data-theme="dark"] .ck.ck-dropdown__panel {
    background-color: #1e293b !important;
}

/* Top Bar Refinement */
.top-bar {
    height: 64px;
    background: var(--bg-sidebar);
    border-bottom: 1px solid var(--border-color);
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 0 2rem;
    gap: 1rem;
    transition: background-color 0.3s ease, border-color 0.3s ease;
}

[data-theme="dark"] .top-bar {
    background: #1e293b;
}

#breadcrumb {
    color: var(--text-muted);
}

h1, h2, h3, h4 {
    font-family: 'Outfit', sans-serif;
}

/* Auth Layout */
.auth-container {
    height: 100vh;
    display: flex;
    align-items: center;
    justify-content: center;
    background: linear-gradient(135deg, #6366f1 0%, #a855f7 100%);
}

.auth-card {
    background: var(--bg-sidebar);
    padding: 2.5rem;
    border-radius: 1.5rem;
    width: 100%;
    max-width: 450px;
    box-shadow: var(--shadow-md);
    animation: slideUp 0.5s ease-out;
}

.auth-card h1 {
    margin-bottom: 0.5rem;
    font-size: 1.875rem;
    text-align: center;
}

.auth-card p {
    color: var(--text-muted);
    text-align: center;
    margin-bottom: 2rem;
}

/* Forms */
.form-group {
    margin-bottom: 1.25rem;
}

.form-group label {
    display: block;
    font-size: 0.875rem;
    font-weight: 500;
    margin-bottom: 0.5rem;
}

.form-input {
    width: 100%;
    padding: 0.75rem 1rem;
    border: 1px solid var(--border-color);
    border-radius: 0.75rem;
    background: var(--bg-main);
    color: var(--text-main);
    transition: all 0.2s;
}

.form-input:focus {
    outline: none;
    border-color: var(--primary);
    box-shadow: 0 0 0 3px rgba(99, 102, 241, 0.2);
}

.btn {
    width: 100%;
    padding: 0.75rem 1.5rem;
    border-radius: 0.75rem;
    border: none;
    font-weight: 600;
    cursor: pointer;
    transition: all 0.2s;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    gap: 0.5rem;
}

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

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

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

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

.btn-icon {
    width: 40px;
    height: 40px;
    padding: 0;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    border: 1px solid var(--border-color);
    background: var(--bg-main);
    color: var(--text-main);
}

.btn-icon:hover {
    background: var(--border-color);
}

.btn-sm {
    padding: 0.4rem 1rem;
    font-size: 0.8125rem;
}

.btn-dashed {
    background: var(--bg-main);
    border: 1px dashed var(--border-color);
    color: var(--text-muted);
    font-size: 0.75rem;
    padding: 0.4rem 1rem;
    margin-top: 0.75rem;
    width: 100%;
}

.btn-dashed:hover {
    border-color: var(--primary);
    color: var(--primary);
    background: rgba(99, 102, 241, 0.05);
}

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

/* App Layout */
.app-container {
    display: grid;
    grid-template-columns: 280px 1fr;
    height: 100vh;
}

.sidebar {
    background: var(--bg-sidebar);
    border-right: 1px solid var(--border-color);
    display: flex;
    flex-direction: column;
    overflow-y: auto;
    z-index: 1001;
    transition: transform 0.3s ease;
    position: relative;
}

.resizer {
    position: absolute;
    right: 0;
    top: 0;
    bottom: 0;
    width: 6px;
    cursor: ew-resize;
    z-index: 1002;
    transition: background 0.2s;
}

.resizer:hover, .resizer:active {
    background: var(--primary);
    opacity: 0.5;
}

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

.logo {
    font-size: 1.1rem;
    font-weight: 700;
    color: var(--primary);
    text-decoration: none;
}

.main-content {
    display: flex;
    flex-direction: column;
    height: 100vh;
    overflow: hidden;
}

.top-bar {
    height: 64px;
    background: var(--bg-sidebar);
    border-bottom: 1px solid var(--border-color);
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 0 2rem;
    gap: 1rem;
}

.editor-container {
    flex: 1;
    overflow-y: auto;
    padding: 2rem 3rem;
    width: 100%;
}

.menu-toggle {
    display: none;
    background: none;
    border: none;
    font-size: 1.5rem;
    cursor: pointer;
    color: var(--text-main);
    padding: 0.5rem;
    margin-left: -0.5rem;
}

/* Tree View */
.tree-item {
    margin: 0.25rem 0;
}

.tree-link {
    display: block;
    padding: 0.35rem 0.75rem;
    text-decoration: none;
    color: var(--text-main);
    border-radius: 0.4rem;
    font-size: 0.875rem;
    transition: all 0.2s;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
    max-width: 100%;
}

.tree-link:hover {
    background: var(--bg-main);
}

.tree-link.active {
    background: rgba(99, 102, 241, 0.1);
    color: var(--primary);
    font-weight: 500;
}

.nested-tree {
    margin-left: 0.85rem;
    border-left: 1px solid var(--border-color);
    min-height: 5px; 
    transition: all 0.3s ease;
}

.nested-tree.collapsed {
    display: none;
}

.toggle-icon {
    cursor: pointer;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    width: 18px;
    height: 18px;
    font-size: 0.75rem;
    color: var(--text-muted);
    transition: transform 0.2s;
    user-select: none;
}

.toggle-icon.expanded {
    transform: rotate(90deg);
}

.drag-handle {
    cursor: grab;
    margin-right: 4px;
    color: var(--text-muted);
    opacity: 0.3;
    flex-shrink: 0;
    transition: opacity 0.2s;
}

.tree-item:hover .drag-handle {
    opacity: 0.8;
}

.back-to-top {
    position: fixed;
    bottom: 30px;
    right: 30px;
    width: 45px;
    height: 45px;
    background: var(--primary);
    color: white;
    border-radius: 50%;
    display: flex;
    align-items: center;
    justify-content: center;
    cursor: pointer;
    box-shadow: var(--shadow-md);
    transition: all 0.3s;
    opacity: 0;
    visibility: hidden;
    z-index: 1000;
    border: none;
    font-size: 1.25rem;
}

.back-to-top.show {
    opacity: 1;
    visibility: visible;
    transform: translateY(0);
}

.back-to-top:hover {
    background: var(--primary-hover);
    transform: translateY(-5px);
    box-shadow: 0 10px 15px -3px rgba(99, 102, 241, 0.4);
}

/* Diff Styles */
.diff-added {
    background-color: #dcfce7;
    color: #166534;
    text-decoration: none;
}

.diff-removed {
    background-color: #fee2e2;
    color: #991b1b;
    text-decoration: line-through;
}

.history-item {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 0.75rem;
    border-bottom: 1px solid var(--border-color);
    cursor: pointer;
    transition: background 0.2s;
}

.history-item:hover {
    background: var(--bg-main);
}

.history-item.selected {
    background: rgba(99, 102, 241, 0.1);
    border-left: 3px solid var(--primary);
}

.icon-label {
    cursor: pointer;
    text-align: center;
    padding: 0.5rem;
    border: 1px solid var(--border-color);
    border-radius: 0.5rem;
    transition: all 0.2s;
}

.icon-label:hover {
    background: var(--bg-main);
}

.icon-label input:checked + span {
    filter: drop-shadow(0 0 5px var(--primary));
}

.icon-label:has(input:checked) {
    border-color: var(--primary);
    background: rgba(99, 102, 241, 0.05);
}

/* Sortable Styles */
.sortable-ghost {
    opacity: 0.4;
    background: var(--primary) !important;
}

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

@media print {
    .sidebar, .top-bar, .btn, #save-status, .ck-toolbar {
        display: none !important;
    }
    .app-container {
        display: block;
    }
    .main-content {
        padding: 0;
        height: auto;
        overflow: visible;
    }
    .editor-container {
        padding: 0;
        max-width: 100%;
    }
    .ck.ck-editor__editable_inline {
        border: none !important;
        box-shadow: none !important;
    }
}

/* Rendered Content Typography */
.content-render {
    font-size: 1.125rem;
    color: var(--text-main);
    width: 100%;
}

.content-render h1.page-title { 
    margin-bottom: 0.5rem; 
    font-size: 2.5rem; 
    border-bottom: none; 
}

.page-meta-row {
    display: flex; 
    gap: 1.5rem; 
    align-items: center; 
    margin-bottom: 2rem; 
    padding-bottom: 1rem; 
    border-bottom: 1px solid var(--border-color); 
    font-size: 0.8125rem; 
    color: var(--text-muted);
    flex-wrap: wrap;
}

.content-render h1 { font-size: 2.5rem; margin-bottom: 1.5rem; font-weight: 700; border-bottom: 2px solid var(--border-color); padding-bottom: 0.5rem; }
.content-render h2 { font-size: 1.875rem; margin-top: 2.5rem; margin-bottom: 1.25rem; font-weight: 600; }
.content-render h3 { font-size: 1.5rem; margin-top: 2rem; margin-bottom: 1rem; font-weight: 600; }
.content-render p { margin-bottom: 1.25rem; line-height: 1.8; color: var(--text-main); opacity: 0.9; }

.content-render ul, .content-render ol { margin-bottom: 1.25rem; padding-left: 1.5rem; }
.content-render li { margin-bottom: 0.5rem; }

.content-render blockquote {
    border-left: 4px solid var(--primary);
    padding: 1rem 1.5rem;
    background: var(--bg-main);
    border-radius: 0.5rem;
    margin: 1.5rem 0;
    font-style: italic;
    color: var(--text-muted);
}

.content-render table {
    width: 100%;
    border-collapse: collapse;
    margin: 2rem 0;
    border-radius: 0.5rem;
    overflow: hidden;
    border: 1px solid var(--border-color);
}

.content-render th, .content-render td {
    padding: 0.75rem 1rem;
    text-align: left;
    border-bottom: 1px solid var(--border-color);
}

.content-render th {
    background: var(--bg-main);
    font-weight: 600;
}

.content-render pre, .content-render code {
    font-family: 'Fira Code', 'Courier New', monospace;
    background: var(--bg-main);
    padding: 0.2rem 0.4rem;
    border-radius: 0.25rem;
    font-size: 0.9em;
}

.content-render pre {
    padding: 1.5rem;
    overflow-x: auto;
    margin: 1.5rem 0;
}

.content-render img {
    max-width: 100%;
    height: auto;
    border-radius: 0.75rem;
    margin: 2rem 0;
    box-shadow: var(--shadow-md);
}

/* Responsive Media Queries */
@media (max-width: 1024px) {
    .editor-container {
        padding: 2rem;
    }
}

@media (max-width: 768px) {
    .app-container {
        display: block;
    }

    .sidebar {
        position: fixed;
        left: 0;
        top: 0;
        bottom: 0;
        width: 280px;
        transform: translateX(-100%);
    }

    .sidebar.active {
        transform: translateX(0);
        box-shadow: 10px 0 30px rgba(0,0,0,0.1);
    }

    .top-bar {
        padding: 0 1rem;
    }

    .menu-toggle {
        display: block;
    }

    .editor-container {
        padding: 1.5rem 1rem;
    }

    .content-render h1 {
        font-size: 1.875rem;
    }

    .content-render {
        font-size: 1rem;
    }
    
    #breadcrumb {
        display: none;
    }
    
    .sidebar-overlay {
        position: fixed;
        top: 0;
        left: 0;
        right: 0;
        bottom: 0;
        background: rgba(0,0,0,0.3);
        backdrop-filter: blur(2px);
        z-index: 1000;
        display: none;
    }
    
    .sidebar-overlay.active {
        display: block;
    }

    #history-modal .auth-card {
        height: 100vh;
        margin-top: 0;
        width: 100%;
        border-radius: 0;
    }

    #history-modal .auth-card > div {
        grid-template-columns: 1fr !important;
    }

    #history-list {
        height: 40vh;
    }

    #diff-viewer {
        height: 60vh;
    }
}

.save-status {
    display: flex;
    align-items: center;
    gap: 0.5rem;
    font-size: 0.75rem;
    font-weight: 500;
    transition: all 0.3s ease;
    white-space: nowrap;
    flex-shrink: 0;
}

.status-dot {
    width: 8px;
    height: 8px;
    border-radius: 50%;
    background: #10b981; /* Green */
    display: inline-block;
    transition: all 0.3s ease;
}

.status-dot.unsaved {
    background: #f59e0b; /* Amber */
    box-shadow: 0 0 10px rgba(245, 158, 11, 0.4);
    animation: pulse-amber 2s infinite;
}

@keyframes pulse-amber {
    0% { transform: scale(1); opacity: 1; }
    50% { transform: scale(1.3); opacity: 0.6; }
    100% { transform: scale(1); opacity: 1; }
}

/* Sidebar Search */
.search-container {
    position: relative;
    display: flex;
    align-items: center;
}

.search-icon {
    position: absolute;
    left: 10px;
    font-size: 0.875rem;
    color: var(--text-muted);
}

.search-input {
    width: 100%;
    padding: 0.5rem 0.75rem 0.5rem 2.25rem;
    font-size: 0.8125rem;
    border: 1px solid var(--border-color);
    border-radius: 0.5rem;
    background: var(--bg-main);
    color: var(--text-main);
    transition: all 0.2s;
}

.search-input:focus {
    outline: none;
    border-color: var(--primary);
    box-shadow: 0 0 0 3px rgba(99, 102, 241, 0.1);
}

/* Sidebar Footer & User Profile */
.sidebar-footer {
    margin-top: auto;
    padding: 1rem 1.25rem;
    border-top: 1px solid var(--border-color);
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 0.5rem;
}

.user-avatar {
    width: 32px;
    height: 32px;
    background: var(--primary);
    color: white;
    border-radius: 50%;
    display: flex;
    align-items: center;
    justify-content: center;
    font-weight: 700;
    font-size: 0.875rem;
    flex-shrink: 0;
}

.logout-link {
    color: var(--text-muted);
    text-decoration: none;
    padding: 0.4rem;
    border-radius: 0.4rem;
    transition: all 0.2s;
}

.logout-link:hover {
    background: #fee2e2;
    color: #ef4444;
}

.theme-toggle {
    cursor: pointer;
    font-size: 1.1rem;
    padding: 0.4rem;
    border-radius: 0.5rem;
    transition: background 0.2s;
    user-select: none;
}

.theme-toggle:hover {
    background: var(--bg-main);
}

/* Premium Content Area */
.editor-container {
    width: 100%;
    margin: 0;
    padding: 3rem 4rem;
    transition: opacity 0.4s ease;
}

@media (max-width: 1024px) {
    .editor-container {
        padding: 2rem 3rem;
    }
}

.drag-handle {
    padding: 0 0.5rem;
    color: var(--text-muted);
    cursor: grab;
    user-select: none;
    opacity: 0.3;
    transition: opacity 0.2s;
    font-size: 1.1rem;
}

.tree-item:hover .drag-handle {
    opacity: 0.8;
}

.drag-handle:active {
    cursor: grabbing;
}

.hidden { display: none !important; }

@media (max-width: 480px) {
    .btn-sm {
        padding: 0.3rem 0.6rem;
        font-size: 0.75rem;
    }
    
    .auth-card {
        padding: 1.5rem;
        border-radius: 1rem;
    }
}
.modal {
    display: none;
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    height: 100vh;
    background: rgba(0,0,0,0.5);
    backdrop-filter: blur(4px);
    z-index: 2000;
    align-items: center;
    justify-content: center;
}

.modal-content {
    background: var(--bg-sidebar);
    border-radius: 1.5rem;
    box-shadow: var(--shadow-md);
    width: 95%;
    max-width: 500px;
    animation: slideUp 0.3s ease-out;
    position: relative;
    padding: 2.5rem;
}

.modal-content.large {
    width: 100vw;
    height: 100vh;
    max-width: none;
    max-height: none;
    border-radius: 0;
    margin: 0;
    padding: 0;
    display: flex;
    flex-direction: column;
    overflow: hidden;
    animation: fadeIn 0.2s ease-out;
}

@keyframes fadeIn {
    from { opacity: 0; }
    to { opacity: 1; }
}

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

.modal-body {
    flex: 1;
    overflow: hidden;
    display: flex;
}

.modal-footer {
    padding: 1rem 1.5rem;
    border-top: 1px solid var(--border-color);
    display: flex;
    justify-content: flex-end;
    gap: 1rem;
}

@media (max-width: 768px) {
    .modal-content.large {
        height: 100vh;
        width: 100%;
        max-width: none;
        border-radius: 0;
    }

    .modal-body {
        flex-direction: column;
    }

    #history-list {
        height: 40%;
        border-right: none !important;
        border-bottom: 1px solid var(--border-color);
    }

    #diff-viewer {
        height: 60%;
    }
}

/* History Modal Specifics */
.history-item {
    padding: 0.75rem 1rem;
    border-bottom: 1px solid var(--border-color);
    cursor: pointer;
    transition: all 0.2s;
    display: flex;
    align-items: center;
    gap: 0.75rem;
    position: relative;
    font-size: 0.8125rem;
}

.history-item:hover {
    background: var(--bg-main);
}

.history-item.selected {
    background: rgba(99, 102, 241, 0.08);
    border-left: 3px solid var(--primary);
}

.history-item input[type="checkbox"] {
    width: 18px;
    height: 18px;
    cursor: pointer;
    accent-color: var(--primary);
}

.loading-spinner {
    width: 30px;
    height: 30px;
    border: 3px solid rgba(99, 102, 241, 0.1);
    border-top: 3px solid var(--primary);
    border-radius: 50%;
    animation: spin 1s linear infinite;
    margin: 0 auto 1rem;
}

@keyframes spin {
    0% { transform: rotate(0deg); }
    100% { transform: rotate(360deg); }
}

.diff-content span {
    padding: 2px 0;
}

/* Global Search Results */
.search-result-item {
    display: block;
    padding: 1rem;
    border-radius: 0.75rem;
    text-decoration: none;
    transition: all 0.2s;
    border: 1px solid transparent;
}

.search-result-item:hover {
    background: var(--bg-main);
    border-color: var(--border-color);
    transform: translateX(5px);
}

/* Auto-TOC Sidebar */
.auto-toc {
    position: fixed;
    top: 120px;
    right: 40px;
    width: 280px;
    max-height: calc(100vh - 200px);
    overflow-y: auto;
    padding: 1.5rem;
    background-color: var(--bg-card) !important;
    border: 1px solid var(--border-color);
    border-radius: 1.25rem;
    box-shadow: 0 10px 25px -5px rgba(0, 0, 0, 0.1), 0 8px 10px -6px rgba(0, 0, 0, 0.1);
    font-size: 0.875rem;
    opacity: 1 !important;
    z-index: 1000;
}

.toc-link {
    display: block;
    padding: 0.45rem 0;
    color: var(--text-main);
    opacity: 0.7;
    text-decoration: none;
    transition: all 0.2s;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
    border-left: 2px solid transparent;
}

.toc-link:hover {
    color: var(--primary);
    opacity: 1;
    padding-left: 0.25rem;
}

.toc-link.h1 { font-weight: 600; color: var(--text-main); }
.toc-link.h2 { padding-left: 0.75rem; }
.toc-link.h3 { padding-left: 1.5rem; font-size: 0.75rem; }

@media (max-width: 1400px) {
    .auto-toc {
        display: none !important;
    }
}
