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

:root {
    --sidebar-width: 280px;
    --toc-width: 240px;
    --header-height: 50px;
    --bg-primary: #1a1a2e;
    --bg-secondary: #16213e;
    --bg-content: #f8f9fa;
    --bg-card: #ffffff;
    --text-primary: #2d3748;
    --text-secondary: #718096;
    --text-sidebar: #e2e8f0;
    --accent: #4f46e5;
    --accent-light: #818cf8;
    --border: #e2e8f0;
    --fold-bg: #f1f5f9;
    --fold-hover: #e8edf3;
    --shadow: 0 1px 3px rgba(0,0,0,0.1);
}

html, body {
    height: 100%;
    font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, 'Helvetica Neue', Arial, sans-serif;
    font-size: 15px;
    line-height: 1.6;
    color: var(--text-primary);
    background: var(--bg-content);
}

/* Layout - auth is enforced server-side; login.html and app.html are separate pages */
#app {
    display: flex;
    height: 100vh;
    overflow: hidden;
}

/* Sidebar */
#sidebar {
    width: var(--sidebar-width);
    min-width: var(--sidebar-width);
    background: var(--bg-primary);
    color: var(--text-sidebar);
    display: flex;
    flex-direction: column;
    overflow: hidden;
    transition: margin-left 0.3s ease;
    z-index: 10;
}

#sidebar.hidden {
    margin-left: calc(var(--sidebar-width) * -1);
}

#sidebar-header {
    padding: 20px 16px 12px;
    border-bottom: 1px solid rgba(255,255,255,0.1);
}

#sidebar-header h1 {
    font-size: 18px;
    font-weight: 700;
    color: #fff;
}

#sidebar-header .subtitle {
    font-size: 12px;
    color: var(--accent-light);
    margin-top: 2px;
}

#doc-search {
    padding: 12px 16px;
}

#search-input {
    width: 100%;
    padding: 8px 12px;
    border: 1px solid rgba(255,255,255,0.15);
    border-radius: 6px;
    background: rgba(255,255,255,0.08);
    color: #fff;
    font-size: 13px;
    outline: none;
    transition: border-color 0.2s;
}

#search-input:focus {
    border-color: var(--accent-light);
}

#search-input::placeholder {
    color: rgba(255,255,255,0.4);
}

#doc-list {
    flex: 1;
    overflow-y: auto;
    padding: 4px 0;
}

/* Document list categories */
.doc-category {
    padding: 8px 16px 4px;
    font-size: 11px;
    font-weight: 600;
    text-transform: uppercase;
    letter-spacing: 0.5px;
    color: var(--accent-light);
    cursor: pointer;
    user-select: none;
}

.doc-category:hover {
    color: #fff;
}

.doc-category-items {
    overflow: hidden;
    transition: max-height 0.3s ease;
}

.doc-item {
    padding: 8px 16px 8px 24px;
    font-size: 13px;
    cursor: pointer;
    transition: background 0.15s;
    border-left: 3px solid transparent;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

.doc-item:hover {
    background: rgba(255,255,255,0.06);
}

.doc-item.active {
    background: rgba(79, 70, 229, 0.2);
    border-left-color: var(--accent-light);
    color: #fff;
    font-weight: 500;
}

/* Main Content */
#content {
    flex: 1;
    display: flex;
    flex-direction: column;
    min-width: 0;
    overflow: hidden;
}

#content-header {
    display: flex;
    align-items: center;
    gap: 12px;
    padding: 8px 20px;
    height: var(--header-height);
    background: var(--bg-card);
    border-bottom: 1px solid var(--border);
    box-shadow: var(--shadow);
    z-index: 5;
}

#sidebar-toggle {
    background: none;
    border: 1px solid var(--border);
    border-radius: 6px;
    padding: 4px 8px;
    font-size: 18px;
    cursor: pointer;
    color: var(--text-secondary);
    transition: color 0.2s;
}

#sidebar-toggle:hover { color: var(--accent); }

#doc-title {
    flex: 1;
    font-size: 16px;
    font-weight: 600;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

#content-actions {
    display: flex;
    gap: 8px;
}

#content-actions button {
    padding: 4px 12px;
    font-size: 12px;
    border: 1px solid var(--border);
    border-radius: 6px;
    background: var(--bg-card);
    color: var(--text-secondary);
    cursor: pointer;
    transition: all 0.2s;
}

#content-actions button:hover {
    color: var(--accent);
    border-color: var(--accent);
}

#content-body {
    flex: 1;
    overflow-y: auto;
    padding: 30px 40px;
    scroll-behavior: smooth;
}

/* Welcome screen */
#welcome-screen {
    text-align: center;
    padding: 60px 20px;
    color: var(--text-secondary);
}

#welcome-screen h2 {
    font-size: 24px;
    color: var(--text-primary);
    margin-bottom: 12px;
}

#stats {
    margin-top: 30px;
    display: flex;
    gap: 20px;
    justify-content: center;
    flex-wrap: wrap;
}

.stat-card {
    background: var(--bg-card);
    border: 1px solid var(--border);
    border-radius: 10px;
    padding: 16px 24px;
    min-width: 140px;
    box-shadow: var(--shadow);
}

.stat-card .number {
    font-size: 28px;
    font-weight: 700;
    color: var(--accent);
}

.stat-card .label {
    font-size: 12px;
    color: var(--text-secondary);
    margin-top: 4px;
}

/* Markdown rendered content */
#md-content {
    display: none;
    max-width: 900px;
    margin: 0 auto;
}

#md-content.visible {
    display: block;
}

/* Foldable sections */
.md-section {
    margin-bottom: 4px;
}

.md-section-header {
    display: flex;
    align-items: center;
    gap: 8px;
    padding: 10px 14px;
    background: var(--fold-bg);
    border: 1px solid var(--border);
    border-radius: 8px;
    cursor: pointer;
    user-select: none;
    transition: background 0.15s;
}

.md-section-header:hover {
    background: var(--fold-hover);
}

.md-section-header .fold-icon {
    font-size: 10px;
    color: var(--text-secondary);
    transition: transform 0.2s;
    width: 14px;
    text-align: center;
}

.md-section.collapsed .fold-icon {
    transform: rotate(-90deg);
}

.md-section-header h1,
.md-section-header h2,
.md-section-header h3,
.md-section-header h4,
.md-section-header h5,
.md-section-header h6 {
    margin: 0;
    font-size: inherit;
}

.md-section-header[data-level="1"] { font-size: 22px; font-weight: 700; background: #e8e4f8; border-color: #c4b5fd; }
.md-section-header[data-level="2"] { font-size: 18px; font-weight: 600; background: #eef2ff; border-color: #c7d2fe; margin-left: 0; }
.md-section-header[data-level="3"] { font-size: 15px; font-weight: 600; margin-left: 12px; }
.md-section-header[data-level="4"] { font-size: 14px; font-weight: 500; margin-left: 24px; }
.md-section-header[data-level="5"],
.md-section-header[data-level="6"] { font-size: 13px; font-weight: 500; margin-left: 36px; }

.md-section-body {
    padding: 12px 16px;
    overflow: hidden;
    transition: max-height 0.3s ease;
}

.md-section.collapsed .md-section-body {
    max-height: 0 !important;
    padding: 0 16px;
}

/* Typography inside markdown */
#md-content h1 { font-size: 22px; margin: 0; }
#md-content h2 { font-size: 18px; margin: 0; }
#md-content h3 { font-size: 15px; margin: 0; }
#md-content h4 { font-size: 14px; margin: 0; }

#md-content p { margin: 8px 0; }
#md-content ul, #md-content ol { margin: 8px 0; padding-left: 24px; }
#md-content li { margin: 4px 0; }
#md-content li > ul, #md-content li > ol { margin: 2px 0; }

#md-content a {
    color: var(--accent);
    text-decoration: none;
}
#md-content a:hover { text-decoration: underline; }

#md-content code {
    background: #f1f5f9;
    padding: 2px 6px;
    border-radius: 4px;
    font-size: 13px;
    font-family: 'Fira Code', 'Consolas', monospace;
}

#md-content pre {
    background: #1e293b;
    color: #e2e8f0;
    padding: 16px;
    border-radius: 8px;
    overflow-x: auto;
    margin: 12px 0;
}

#md-content pre code {
    background: none;
    padding: 0;
    color: inherit;
}

#md-content blockquote {
    border-left: 4px solid var(--accent);
    margin: 12px 0;
    padding: 8px 16px;
    background: #f8fafc;
    color: var(--text-secondary);
    border-radius: 0 6px 6px 0;
}

#md-content table {
    width: 100%;
    border-collapse: collapse;
    margin: 12px 0;
    font-size: 14px;
}

#md-content th, #md-content td {
    padding: 8px 12px;
    border: 1px solid var(--border);
    text-align: left;
}

#md-content th {
    background: var(--fold-bg);
    font-weight: 600;
}

#md-content tr:nth-child(even) { background: #fafbfc; }

#md-content hr {
    border: none;
    border-top: 2px solid var(--border);
    margin: 20px 0;
}

#md-content img {
    max-width: 100%;
    border-radius: 8px;
}

#md-content strong { font-weight: 600; }

/* TOC Panel */
#toc-panel {
    width: var(--toc-width);
    min-width: var(--toc-width);
    background: var(--bg-card);
    border-left: 1px solid var(--border);
    display: flex;
    flex-direction: column;
    overflow: hidden;
    transition: margin-right 0.3s ease;
}

#toc-panel.hidden {
    margin-right: calc(var(--toc-width) * -1);
}

#toc-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 12px 16px;
    border-bottom: 1px solid var(--border);
}

#toc-header h3 {
    font-size: 13px;
    font-weight: 600;
    text-transform: uppercase;
    letter-spacing: 0.5px;
    color: var(--text-secondary);
}

#toc-toggle {
    background: none;
    border: none;
    cursor: pointer;
    font-size: 14px;
    color: var(--text-secondary);
    padding: 2px 6px;
    border-radius: 4px;
}

#toc-toggle:hover { background: var(--fold-bg); }

#toc-nav {
    flex: 1;
    overflow-y: auto;
    padding: 8px 0;
}

.toc-item {
    display: block;
    padding: 5px 16px;
    font-size: 13px;
    color: var(--text-secondary);
    text-decoration: none;
    cursor: pointer;
    border-left: 2px solid transparent;
    transition: all 0.15s;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

.toc-item:hover {
    color: var(--accent);
    background: #f8fafc;
}

.toc-item.active {
    color: var(--accent);
    border-left-color: var(--accent);
    font-weight: 500;
}

.toc-item[data-level="1"] { padding-left: 16px; font-weight: 600; }
.toc-item[data-level="2"] { padding-left: 24px; }
.toc-item[data-level="3"] { padding-left: 36px; font-size: 12px; }
.toc-item[data-level="4"] { padding-left: 48px; font-size: 12px; }

/* Scrollbar styling */
::-webkit-scrollbar { width: 6px; }
::-webkit-scrollbar-track { background: transparent; }
::-webkit-scrollbar-thumb { background: #cbd5e1; border-radius: 3px; }
::-webkit-scrollbar-thumb:hover { background: #94a3b8; }

/* Responsive */
@media (max-width: 1024px) {
    #toc-panel { display: none; }
    #content-body { padding: 20px; }
}

@media (max-width: 768px) {
    #sidebar {
        position: fixed;
        left: 0;
        top: 0;
        height: 100vh;
        z-index: 100;
        box-shadow: 4px 0 20px rgba(0,0,0,0.3);
    }
    #sidebar.hidden { margin-left: calc(var(--sidebar-width) * -1); }
    #content-body { padding: 16px; }
    #content-actions { display: none; }
}

/* ===== Login Screen (login.html - separate page, no protected content) ===== */
#login-screen {
    position: fixed;
    inset: 0;
    display: flex;
    align-items: center;
    justify-content: center;
    background: linear-gradient(135deg, var(--bg-primary) 0%, #0f172a 50%, #1e1b4b 100%);
    z-index: 1000;
}

#login-card {
    width: 100%;
    max-width: 400px;
    background: var(--bg-card);
    border-radius: 16px;
    box-shadow: 0 25px 60px rgba(0,0,0,0.3);
    overflow: hidden;
    animation: loginSlideUp 0.4s ease-out;
}

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

#login-header {
    text-align: center;
    padding: 36px 32px 24px;
    background: linear-gradient(135deg, var(--bg-primary), var(--bg-secondary));
    color: #fff;
}

#login-icon {
    margin-bottom: 16px;
    color: var(--accent-light);
}

#login-header h1 {
    font-size: 22px;
    font-weight: 700;
    margin-bottom: 4px;
}

#login-header p {
    font-size: 13px;
    color: var(--accent-light);
}

#login-form {
    padding: 28px 32px 20px;
}

.login-field {
    margin-bottom: 18px;
}

.login-field label {
    display: block;
    font-size: 13px;
    font-weight: 600;
    color: var(--text-primary);
    margin-bottom: 6px;
}

.login-field input {
    width: 100%;
    padding: 10px 14px;
    border: 1.5px solid var(--border);
    border-radius: 8px;
    font-size: 14px;
    color: var(--text-primary);
    background: #fff;
    outline: none;
    transition: border-color 0.2s, box-shadow 0.2s;
}

.login-field input:focus {
    border-color: var(--accent);
    box-shadow: 0 0 0 3px rgba(79, 70, 229, 0.1);
}

.login-field input::placeholder {
    color: #a0aec0;
}

.login-options {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-bottom: 20px;
}

.remember-me {
    display: flex;
    align-items: center;
    gap: 6px;
    font-size: 13px;
    color: var(--text-secondary);
    cursor: pointer;
}

.remember-me input[type="checkbox"] {
    width: 16px;
    height: 16px;
    accent-color: var(--accent);
    cursor: pointer;
}

#login-submit {
    width: 100%;
    padding: 12px;
    background: var(--accent);
    color: #fff;
    border: none;
    border-radius: 8px;
    font-size: 15px;
    font-weight: 600;
    cursor: pointer;
    transition: background 0.2s, transform 0.1s;
}

#login-submit:hover {
    background: #4338ca;
}

#login-submit:active {
    transform: scale(0.98);
}

#login-submit:disabled {
    background: #94a3b8;
    cursor: not-allowed;
    transform: none;
}

#login-error {
    margin-top: 14px;
    text-align: center;
    font-size: 13px;
    color: #dc2626;
    min-height: 20px;
}

#login-error.shake {
    animation: loginShake 0.4s ease;
}

@keyframes loginShake {
    0%, 100% { transform: translateX(0); }
    20%, 60% { transform: translateX(-8px); }
    40%, 80% { transform: translateX(8px); }
}

#login-footer {
    padding: 16px 32px;
    text-align: center;
    border-top: 1px solid var(--border);
    background: #fafbfc;
}

#login-footer p {
    font-size: 12px;
    color: var(--text-secondary);
}

/* ===== Sidebar Footer (User Info / Logout) ===== */
#sidebar-footer {
    padding: 12px 16px;
    border-top: 1px solid rgba(255,255,255,0.1);
    background: rgba(0,0,0,0.15);
}

#user-info {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 8px;
}

#user-email {
    font-size: 12px;
    color: rgba(255,255,255,0.6);
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}

#logout-btn {
    background: none;
    border: 1px solid rgba(255,255,255,0.2);
    border-radius: 4px;
    color: rgba(255,255,255,0.6);
    font-size: 11px;
    padding: 3px 10px;
    cursor: pointer;
    white-space: nowrap;
    transition: all 0.2s;
}

#logout-btn:hover {
    color: #fff;
    border-color: rgba(255,255,255,0.5);
    background: rgba(255,255,255,0.1);
}

/* Login responsive */
@media (max-width: 480px) {
    #login-card { margin: 16px; border-radius: 12px; }
    #login-form { padding: 20px 24px 16px; }
    #login-header { padding: 28px 24px 20px; }
}
