/* =========================================
   1. VARIABLES & THEME LOGIC
   ========================================= */
:root {
    /* Light Mode */
    --bg: #ffffff;
    --text: #1a1a1a;
    --accent: #4F46E5;
    --secondary-bg: #e5e7eb;
    --header-bg: #f5f5f5; 
    --footer-bg: #f5f5f5; 
}

/* Dark Mode Override (#202020) */
body.dark-theme {
    --bg: #202020;
    --text: #eeeeee;
    --accent: #818cf8;
    --secondary-bg: #333333;
    --header-bg: #2a2a2a; 
    --footer-bg: #2a2a2a; 
}

/* System Preference Sync */
@media (prefers-color-scheme: dark) {
    :root:not(.light-theme) {
        --bg: #202020;
        --text: #eeeeee;
        --accent: #818cf8;
        --secondary-bg: #333333;
        --header-bg: #2a2a2a;
        --footer-bg: #2a2a2a;
    }
}

/* =========================================
   2. GLOBAL RESET & BASE STYLES
   ========================================= */
* { 
    margin: 0; 
    padding: 0; 
    box-sizing: border-box; 
}

.hidden {
    display: none !important;
}

body {
    background-color: var(--bg);
    color: var(--text);
    font-family: 'Inter', -apple-system, BlinkMacSystemFont, sans-serif;
    transition: background 0.3s, color 0.3s;
    min-height: 100vh;
    display: flex;
    flex-direction: column;
    line-height: 1.6;
}

.container {
    max-width: 1200px;
    margin: 0 auto;
    padding: 0 40px;
    width: 100%;
}

/* =========================================
   3. HEADER & FOOTER
   ========================================= */

#main-header {
    min-height: 70px;
    display: block;
}

header {
    position: sticky;
    top: 0;
    z-index: 1000;
    background-color: var(--header-bg);
    padding: 1.2rem 0;
    border-bottom: 1px solid rgba(128, 128, 128, 0.1);
}

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

footer {
    position: sticky;
    bottom: 0;
    z-index: 1000;
    background-color: var(--footer-bg);
    padding: 2.5rem 0;
    text-align: center;
    border-top: 1px solid rgba(128, 128, 128, 0.1);
    margin-top: auto; /* Keeps footer at bottom of short pages */
}

/* Navigation & Logo */
.logo a {
    font-size: 1.6rem;
    font-weight: 800;
    color: var(--accent);
    text-decoration: none;
}

nav { 
    display: flex; 
    gap: 35px; 
    align-items: center; 
}

nav a { 
    text-decoration: none; 
    color: var(--text); 
    font-weight: 500; 
    opacity: 0.8; 
    transition: 0.2s; 
}

nav a:hover { 
    opacity: 1; 
    color: var(--accent); 
}

/* Theme Toggle Button */
.icon-btn {
    background: none;
    border: none;
    color: var(--text);
    font-size: 1.25rem;
    cursor: pointer;
    display: flex;
    padding: 5px;
}

/* =========================================
   4. LANDING PAGE (HERO SECTION)
   ========================================= */
main {
    flex: 1;
    display: flex;
    align-items: center;
    justify-content: center;
}

.hero {
    text-align: center;
    padding: 80px 0;
}

h1 { 
    font-size: clamp(3rem, 8vw, 4.5rem); 
    margin-bottom: 1rem; 
    letter-spacing: -2px; 
    line-height: 1.1;
}

.subtitle { 
    font-size: 1.2rem; 
    opacity: 0.7; 
    margin: 0 auto 2.5rem auto; 
    max-width: 600px; 
}

.button-group {
    display: flex;
    gap: 15px;
    justify-content: center;
    flex-wrap: wrap;
}

.btn {
    display: inline-block;
    padding: 0.9rem 2.2rem;
    border-radius: 50px;
    text-decoration: none;
    font-weight: 600;
    transition: all 0.2s ease;
}

.btn-primary { background: var(--accent); color: white; }
.btn-secondary { background: var(--secondary-bg); color: var(--text); }
.btn:hover { 
    transform: translateY(-3px); 
    box-shadow: 0 8px 15px rgba(0,0,0,0.1); 
}

/* =========================================
   5. ABOUT PAGE STYLES
   ========================================= */
.content-section { padding: 60px 0; text-align: center; }
.lead-text { font-size: 1.4rem; max-width: 800px; margin: 20px auto; opacity: 0.9; }

.about-grid {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(280px, 1fr));
    gap: 30px;
    margin-top: 50px;
}

.about-card {
    background: var(--header-bg);
    padding: 40px;
    border-radius: 20px;
    border: 1px solid rgba(128, 128, 128, 0.1);
    transition: transform 0.3s;
}

.about-card:hover { transform: translateY(-10px); }
.about-card i { font-size: 2.5rem; color: var(--accent); margin-bottom: 20px; }

/* =========================================
   6. WIKI PAGE STYLES
   ========================================= */

.wiki-content {
    display: block;
    text-align: left;
}

.wiki-textarea {
    width: 100%;
    height: calc(100vh - 550px);
    min-height: 300px;
    background: var(--header-bg);
    color: var(--text);
    border: 1px solid rgba(128, 128, 128, 0.1);
    border-radius: 15px;
    padding: 25px;
    margin-top: 15px;
    font-family: 'Inter', sans-serif;
    font-size: 1rem;
    line-height: 1.6;
    resize: none;
    outline: none;
    transition: border-color 0.2s, box-shadow 0.2s;
    overflow-y: auto;
}

.wiki-textarea:focus {
    border-color: var(--accent);
    box-shadow: 0 0 0 3px rgba(129, 140, 248, 0.1);
}

.editor-footer {
    width: 100%;
    margin-top: 25px;
    display: flex;
    justify-content: flex-start;
    align-items: center;
    gap: 20px;
    padding-bottom: 40px;
}

/* Container to keep tabs and editor locked together */
.editor-wrapper {
    width: 100%;
    display: flex;
    flex-direction: column;
    align-items: flex-start; /* Keeps tabs to the left */
}

.editor-toolbar {
    display: flex;
    gap: 5px;
    padding: 8px 15px;
    background: var(--header-bg);
    border-left: 1px solid rgba(128, 128, 128, 0.1);
    border-right: 1px solid rgba(128, 128, 128, 0.1);
    border-bottom: 1px solid rgba(128, 128, 128, 0.1);
    border-top-right-radius: 15px;
    width: 100%;
}

.editor-toolbar button {
    background: transparent;
    border: 1px solid transparent;
    color: var(--text);
    padding: 5px 10px;
    border-radius: 4px;
    cursor: pointer;
    opacity: 0.7;
    transition: 0.2s;
}

.editor-toolbar button:hover {
    background: var(--secondary-bg);
    opacity: 1;
}

.editor-tabs {
    display: flex;
    gap: 0; /* Remove gap between buttons for a "segmented" look */
    margin-bottom: -1px; /* Overlap the border of the textarea */
    z-index: 1; /* Ensure tabs sit on top of the border */
}

.tab-btn {
    background: var(--secondary-bg);
    color: var(--text);
    border: 1px solid rgba(128, 128, 128, 0.2);
    padding: 10px 25px;
    border-radius: 0; /* Square by default */
    cursor: pointer;
    font-weight: 600;
    font-size: 0.9rem;
    opacity: 0.5;
    transition: 0.2s;
}

.tab-btn:first-child {
    border-top-left-radius: 12px;
}

/* Maintain rounded corner when the Editor tab is hidden via script */
#edit-btn.hidden + #preview-btn {
    border-top-left-radius: 12px;
}

.tab-btn:last-child {
    border-top-right-radius: 12px;
}

.tab-btn.active {
    background: var(--header-bg); /* Match the editor background */
    opacity: 1;
    border-bottom: 1px solid var(--header-bg); /* Hide the bottom border when active */
    border-top: 2px solid var(--accent); /* Optional: add accent line on top */
}

.btn-danger {
    background: rgba(239, 68, 68, 0.1); /* Subtle red tint */
    color: #ef4444;
    border: 1px solid rgba(239, 68, 68, 0.2);
    padding: 0.9rem 1.8rem;
    border-radius: 50px;
    font-weight: 600;
    cursor: pointer;
    transition: all 0.2s;
}

.btn-danger:hover {
    background: #ef4444;
    color: white;
    transform: translateY(-2px);
    box-shadow: 0 5px 15px rgba(239, 68, 68, 0.3);
}

/* Adjust the Textarea and Preview to have no top margin */
.wiki-textarea, .wiki-preview, .wiki-history {
    margin-top: 0 !important; 
    border-radius: 0 15px 15px 15px; /* Unified rounded bottom corners */
    border-top: none;
    width: 100%;
}

.editor-toolbar { margin-top: 0 !important; border-top: none; width: 100%; }

/* =========================================
   10. LIVE PREVIEW (SPLIT VIEW) & SIDEBAR TOGGLE
   ========================================= */
.wiki-layout {
    display: grid !important; /* Use grid for layout */
    grid-template-columns: 260px 1fr; /* Sidebar width and content */
    gap: 50px;
    padding: 30px 100px;
    margin: 0 auto;
    max-width: initial;
    transition: grid-template-columns 0.3s ease-in-out, max-width 0.3s ease-in-out, transform 0.3s ease-in-out;
}

.wiki-layout.sidebar-collapsed {
    grid-template-columns: 1fr; /* Switch to single column */
    gap: 0;
    transform: none; /* Remove any horizontal shifts */
}

.wiki-layout.sidebar-collapsed .wiki-sidebar {
    display: none; /* Hide sidebar content */
}

/* =========================================
   10. LIVE PREVIEW (SPLIT VIEW)
   ========================================= */
.editor-wrapper.split-view {
    display: grid;
    grid-template-columns: 1fr 1fr;
    width: 100%;
    border-top-right-radius: 0px;
}

.split-view .editor-tabs, .split-view .editor-toolbar {
    grid-column: span 2;
}

.split-view #editor, .split-view #preview {
    display: block !important;
    height: calc(100vh - 550px); /* Dynamic height in split-view */
    min-height: 300px;
    border-top-right-radius: 0px;
}

.split-view #editor {
    border-right: none;
    border-bottom-right-radius: 0; /* Sharp corner for the middle seam */
}

.split-view #preview {
    border-bottom-left-radius: 0; /* Sharp corner for the middle seam */
    border-top: 0px;
}

#sidebar-toggle-btn {
    position: absolute; /* Position relative to wiki-content */
    top: 0;
    left: -70px; /* Position outside the wiki-content, next to sidebar */
    z-index: 100;
    background: var(--header-bg);
    border: 1px solid rgba(128, 128, 128, 0.1);
    border-radius: 50%;
    width: 40px;
    height: 40px;
    display: flex;
    align-items: center;
    justify-content: center;
    transition: left 0.3s ease;
}

/* Adjust position when sidebar is collapsed */
.wiki-layout.sidebar-collapsed #sidebar-toggle-btn {
    left: -50px; /* Overlap the edge slightly for a cleaner look */
}

/* TOC Styling */
#toc-list, #toc-list ul {
    list-style: none;
    padding: 0;
    margin: 0;
}

#toc-list ul {
    padding-left: 15px; /* Indent nested heading levels */
}

#toc-list .sidebar-page-item a {
    font-size: 0.9rem; /* Slightly smaller than main nav for hierarchy */
    padding: 6px 0;
}

.wiki-preview {
    width: 100%;
    height: calc(100vh - 550px);
    min-height: 300px;
    background: var(--header-bg); /* Match your editor theme */
    color: var(--text);
    padding: 35px;
    border: 1px solid rgba(128, 128, 128, 0.1);
    line-height: 1.7;
    overflow-y: auto;
    text-align: left;
    white-space: pre-wrap; /* Keeps your manual line breaks visible */
    word-wrap: break-word;
}

.wiki-history {
    width: 100%;
    height: calc(100vh - 550px);
    min-height: 300px;
    background: var(--header-bg);
    color: var(--text);
    padding: 20px;
    border: 1px solid rgba(128, 128, 128, 0.1);
    overflow-y: auto;
}

.diff-container {
    background: var(--bg);
    padding: 15px;
    border-radius: 8px;
    border: 1px solid rgba(128, 128, 128, 0.1);
    font-family: monospace;
    white-space: pre-wrap;
    font-size: 0.9rem;
    line-height: 1.5;
}

.diff-added {
    background-color: rgba(16, 185, 129, 0.2);
    color: #10b981;
}

.diff-removed {
    background-color: rgba(239, 68, 68, 0.2);
    color: #ef4444;
    text-decoration: line-through;
}

/* Markdown Element Styling */
.wiki-preview h1 { font-size: 2.2rem; color: var(--accent); margin-bottom: 1.5rem; border-bottom: 1px solid rgba(128, 128, 128, 0.2); padding-bottom: 10px; }
.wiki-preview h2 { font-size: 1.8rem; color: var(--accent); margin-top: 1.5rem; margin-bottom: 1rem; }
.wiki-preview h3 { font-size: 1.4rem; color: var(--text); margin-top: 1.2rem; }

.wiki-preview p { margin-bottom: 1.2rem; opacity: 0.9; }

.wiki-preview ul, .wiki-preview ol { 
    margin-left: 20px; 
    margin-bottom: 1.2rem; 
    color: var(--text);
}

.wiki-preview li { margin-bottom: 0.5rem; }

/* Content Hyperlinks Styling */
.wiki-preview a {
    color: var(--accent);
    text-decoration: none;
    font-weight: 600;
    transition: opacity 0.2s;
}

.wiki-preview a:hover {
    opacity: 0.8;
    text-decoration: underline;
}

/* Code blocks for your page */
.wiki-preview pre {
    background: #1a1a1a;
    padding: 15px;
    border-radius: 10px;
    overflow-x: auto;
    margin: 1.5rem 0;
    border: 1px solid rgba(255,255,255,0.1);
}

.wiki-preview code {
    font-family: 'Fira Code', monospace;
    font-size: 0.9rem;
    color: #e5e7eb;
}

.wiki-preview blockquote {
    border-left: 4px solid var(--accent);
    padding: 2px 20px;
    background: rgba(128, 128, 128, 0.05);
    font-style: italic;
    margin: 1rem 0;
    display: -webkit-inline-box;
}

.wiki-preview blockquote p {
    margin: 0 !important;
    padding: 0 !important;
}

/* Styling the Markdown content inside the preview */
.wiki-preview h1, .wiki-preview h2, .wiki-preview h3 { margin-top: 20px; margin-bottom: 10px; color: var(--accent); }
.wiki-preview ul, .wiki-preview ol { margin-left: 25px; margin-bottom: 15px; }
.wiki-preview code { background: rgba(128, 128, 128, 0.2); padding: 2px 5px; border-radius: 4px; font-family: monospace; }
.wiki-preview blockquote { border-left: 4px solid var(--accent); padding-left: 15px; font-style: italic; opacity: 0.8; }


.wiki-sidebar {
    position: sticky;
    top: 100px; /* Lowered to sit below the sticky header */
    width: 260px;
    flex-shrink: 0;
    max-height: calc(100vh - 350px);
    overflow-y: auto;
    padding-right: 20px;
    border-right: 1px solid rgba(128, 128, 128, 0.1);
    text-align: left;
}

.wiki-content {
    position: relative; /* For positioning the toggle button */
}


.sidebar-section { margin-bottom: 35px; }
.sidebar-section h4 {
    font-size: 0.7rem;
    text-transform: uppercase;
    letter-spacing: 1px;
    margin-bottom: 10px;
    padding-left: 12px;
    color: var(--accent);
    opacity: 0.8;
}

.sidebar-section a, .sidebar-section a:visited {
    text-decoration: none;
    color: var(--text);
    display: flex;
    align-items: center;
    gap: 12px;
    padding: 8px 12px;
    border-radius: 10px;
    opacity: 0.8;
    transition: all 0.2s ease;
}

.sidebar-section a:hover {
    opacity: 1;
    color: var(--accent);
    background: var(--secondary-bg);
}

.sidebar-page-item {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 5px;
    border-radius: 10px;
    padding: 0 12px;
    transition: all 0.2s ease;
    margin-bottom: 2px;
}

.sidebar-page-item a { 
    flex: 1;
    text-decoration: none; 
    color: var(--text); 
    padding: 8px 0; 
    font-size: 1rem;
    display: flex;
    align-items: center;
    gap: 12px;
}

.sidebar-page-item:hover { background: var(--secondary-bg); }
.sidebar-page-item:hover a { opacity: 1; color: var(--accent); }

.sidebar-page-item.active { 
    background: rgba(79, 70, 229, 0.1); /* Subtle blue tint for light mode */
}

body.dark-theme .sidebar-page-item.active {
    background: rgba(129, 140, 248, 0.15); /* Subtle indigo tint for dark mode */
}

.sidebar-page-item i {
    width: 20px;
    text-align: center;
    opacity: 0.9;
}

.sidebar-delete-btn {
    background: none;
    border: none;
    color: #ef4444;
    cursor: pointer;
    opacity: 0;
    transition: opacity 0.2s;
    padding: 5px;
}

.sidebar-page-item:hover .sidebar-delete-btn { opacity: 0.5; }
.sidebar-delete-btn:hover { opacity: 1 !important; }

.breadcrumb { font-size: 0.9rem; margin-bottom: 10px; opacity: 0.8; width: 100%; text-align: left; }
.breadcrumb a { 
    text-decoration: none; 
    color: var(--text); 
    transition: color 0.2s;
}
.breadcrumb a:hover { 
    color: var(--accent); 
}
.wiki-content h2 { font-size: 2rem; margin-bottom: 10px; width: 100%; text-align: left; }

/* =========================================
   8. MODAL STYLES
   ========================================= */
.modal-overlay {
    position: fixed;
    top: 0; left: 0;
    width: 100%; height: 100%;
    background: rgba(0, 0, 0, 0.7);
    backdrop-filter: blur(4px);
    display: flex;
    align-items: center;
    justify-content: center;
    z-index: 2000;
}

.modal-content {
    background: var(--header-bg);
    padding: 2.5rem;
    border-radius: 20px;
    width: 90%;
    max-width: 400px;
    position: relative;
    box-shadow: 0 20px 40px rgba(0,0,0,0.3);
    border: 1px solid rgba(128, 128, 128, 0.1);
}

.modal-close {
    position: absolute;
    top: 20px; right: 20px;
    background: none; border: none;
    color: var(--text); font-size: 1.5rem;
    cursor: pointer; opacity: 0.5;
}

.modal-close:hover { opacity: 1; }

.form-group { margin-bottom: 1.2rem; }
.form-group label { display: block; margin-bottom: 0.5rem; font-size: 0.9rem; font-weight: 600; }
.form-group input, .form-group select, .form-group textarea { 
    width: 100%; padding: 0.8rem; border-radius: 8px; 
    border: 1px solid rgba(128, 128, 128, 0.2); 
    background: var(--bg); color: var(--text); 
    font-family: inherit;
}

.form-group textarea {
    min-height: 80px;
    resize: vertical;
}

.error-msg { color: #ef4444; background: rgba(239, 68, 68, 0.1); padding: 10px; border-radius: 8px; margin-bottom: 1rem; font-size: 0.85rem; }
.success-msg { color: #10b981; background: rgba(16, 185, 129, 0.1); padding: 10px; border-radius: 8px; margin-bottom: 1rem; font-size: 0.85rem; }

/* =========================================
   9. USER MANAGEMENT TABLE
   ========================================= */
.user-table {
    width: 100%;
    border-collapse: collapse;
    margin-top: 30px;
    background: var(--header-bg);
    border-radius: 15px;
    overflow: hidden;
    border: 1px solid rgba(128, 128, 128, 0.1);
}

.user-table th, .user-table td {
    padding: 20px;
    text-align: left;
    border-bottom: 1px solid rgba(128, 128, 128, 0.1);
}

.user-table th {
    background: var(--secondary-bg);
    color: var(--accent);
    text-transform: uppercase;
    font-size: 0.75rem;
    letter-spacing: 1px;
}

.user-table select {
    background: var(--bg);
    color: var(--text);
    padding: 8px 12px;
    border-radius: 8px;
    border: 1px solid rgba(128, 128, 128, 0.2);
    cursor: pointer;
}

/* =========================================
   7. RESPONSIVE DESIGN
   ========================================= */
@media (max-width: 900px) {
    .wiki-layout { grid-template-columns: 1fr; padding: 40px 20px; }
    .wiki-sidebar {
        position: relative;
        top: 0;
        max-height: none;
        border-right: none;
        border-bottom: 1px solid rgba(128, 128, 128, 0.1);
        margin-bottom: 40px;
    }
    .header-flex { padding: 0 20px; }
    .container { padding: 0 20px; }
}