/* ── CSS Variables ── */
:root {
    --bg-primary: #fafafa;
    --bg-secondary: #fff;
    --bg-tertiary: #f4f4f5;
    --bg-hover: #f4f4f5;
    --bg-active: #eff6ff;
    --border: #e4e4e7;
    --border-light: #f4f4f5;
    --text-primary: #18181b;
    --text-secondary: #52525b;
    --text-tertiary: #71717a;
    --text-muted: #a1a1aa;
    --accent: #2563eb;
    --accent-hover: #1d4ed8;
    --danger: #ef4444;
    --scrollbar: #d4d4d8;
    --code-bg: #f4f4f5;
    --shadow: rgba(0,0,0,0.06);
}

[data-theme="dark"] {
    --bg-primary: #09090b;
    --bg-secondary: #18181b;
    --bg-tertiary: #27272a;
    --bg-hover: #27272a;
    --bg-active: #1e293b;
    --border: #27272a;
    --border-light: #27272a;
    --text-primary: #fafafa;
    --text-secondary: #a1a1aa;
    --text-tertiary: #71717a;
    --text-muted: #52525b;
    --accent: #3b82f6;
    --accent-hover: #2563eb;
    --danger: #f87171;
    --scrollbar: #3f3f46;
    --code-bg: #27272a;
    --shadow: rgba(0,0,0,0.3);
}

/* ── Base ── */
* { box-sizing: border-box; }

body {
    font-family: 'Inter', system-ui, -apple-system, sans-serif;
    margin: 0;
    background: var(--bg-primary);
    color: var(--text-primary);
    transition: background 0.2s, color 0.2s;
}

/* ── Theme toggle ── */
.theme-toggle {
    background: none;
    border: 1px solid var(--border);
    border-radius: 6px;
    padding: 6px;
    cursor: pointer;
    color: var(--text-muted);
    display: flex;
    align-items: center;
    justify-content: center;
    transition: all 0.15s;
}

.theme-toggle:hover {
    color: var(--text-primary);
    border-color: var(--text-muted);
}

/* Light: show moon, hide sun */
.icon-sun { display: none; }
.icon-moon { display: block; }

/* Dark: show sun, hide moon */
[data-theme="dark"] .icon-sun { display: block; }
[data-theme="dark"] .icon-moon { display: none; }

.theme-toggle--login {
    position: absolute;
    top: 16px;
    right: 16px;
}

/* ── Login page ── */
.login-container {
    min-height: 100vh;
    display: flex;
    align-items: center;
    justify-content: center;
    background: var(--bg-tertiary);
}

.login-card {
    background: var(--bg-secondary);
    border: 1px solid var(--border);
    border-radius: 12px;
    padding: 48px;
    text-align: center;
    max-width: 400px;
    width: 100%;
    box-shadow: 0 1px 3px var(--shadow);
    position: relative;
}

.login-card h1 {
    font-size: 28px;
    font-weight: 700;
    margin: 0 0 8px;
    letter-spacing: -0.5px;
    color: var(--text-primary);
}

.login-card p {
    color: var(--text-tertiary);
    margin: 0 0 32px;
    font-size: 14px;
}

.btn-microsoft {
    display: inline-flex;
    align-items: center;
    gap: 10px;
    background: var(--accent);
    color: #fff;
    border: none;
    padding: 12px 28px;
    border-radius: 8px;
    font-size: 14px;
    font-weight: 500;
    cursor: pointer;
    text-decoration: none;
    transition: background 0.15s;
}

.btn-microsoft:hover {
    background: var(--accent-hover);
}

.btn-microsoft svg {
    width: 20px;
    height: 20px;
}

/* ── Viewer layout ── */
.viewer {
    display: flex;
    height: 100vh;
}

/* ── Sidebar ── */
.sidebar {
    width: 280px;
    min-width: 280px;
    background: var(--bg-secondary);
    border-right: 1px solid var(--border);
    display: flex;
    flex-direction: column;
    height: 100vh;
    overflow: hidden;
    transition: background 0.2s, border-color 0.2s;
}

.sidebar-header {
    padding: 20px 16px 12px;
    border-bottom: 1px solid var(--border-light);
}

.sidebar-header-row {
    display: flex;
    align-items: center;
    justify-content: space-between;
    margin-bottom: 12px;
}

.sidebar-header-actions {
    display: flex;
    align-items: center;
    gap: 4px;
}

.btn-icon {
    background: none;
    border: 1px solid var(--border);
    border-radius: 6px;
    padding: 6px;
    cursor: pointer;
    color: var(--text-muted);
    display: flex;
    align-items: center;
    justify-content: center;
    transition: all 0.15s;
}

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

.btn-icon.htmx-request {
    animation: spin 0.8s linear infinite;
}

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

.sidebar-header h1 {
    font-size: 18px;
    font-weight: 700;
    margin: 0;
    letter-spacing: -0.3px;
}

.sidebar-search {
    width: 100%;
    padding: 8px 12px;
    border: 1px solid var(--border);
    border-radius: 6px;
    font-size: 13px;
    outline: none;
    transition: border-color 0.15s, background 0.2s, color 0.2s;
    font-family: inherit;
    background: var(--bg-tertiary);
    color: var(--text-primary);
}

.sidebar-search::placeholder {
    color: var(--text-muted);
}

.sidebar-search:focus {
    border-color: var(--text-muted);
}

.sidebar-tree {
    flex: 1;
    overflow-y: auto;
    padding: 8px 0;
}

.sidebar-tree::-webkit-scrollbar {
    width: 4px;
}

.sidebar-tree::-webkit-scrollbar-thumb {
    background: var(--scrollbar);
    border-radius: 2px;
}

.sidebar-footer {
    padding: 12px 16px;
    border-top: 1px solid var(--border-light);
    display: flex;
    align-items: center;
    justify-content: space-between;
}

.sidebar-footer .user-name {
    font-size: 13px;
    color: var(--text-secondary);
    font-weight: 500;
}

.btn-logout {
    font-size: 13px;
    color: var(--text-muted);
    text-decoration: none;
    transition: color 0.15s;
}

.btn-logout:hover {
    color: var(--danger);
}

/* ── Tree items ── */
.tree-folder > summary {
    display: flex;
    align-items: center;
    gap: 6px;
    padding: 5px 16px;
    font-size: 13px;
    color: var(--text-secondary);
    cursor: pointer;
    user-select: none;
    list-style: none;
    transition: background 0.1s;
}

.tree-folder > summary::-webkit-details-marker {
    display: none;
}

.tree-folder > summary:hover {
    background: var(--bg-hover);
}

.tree-folder > summary .folder-icon {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    width: 14px;
    height: 14px;
    border: 1px solid var(--text-muted);
    border-radius: 2px;
    font-size: 12px;
    line-height: 1;
    color: var(--text-muted);
    flex-shrink: 0;
}

.tree-folder > summary .folder-icon::before {
    content: "+";
}

.tree-folder[open] > summary .folder-icon::before {
    content: "\2212";
}

.tree-folder .tree-children {
    padding-left: 12px;
}

.tree-file {
    display: block;
    padding: 5px 16px 5px 34px;
    font-size: 13px;
    color: var(--text-tertiary);
    text-decoration: none;
    cursor: pointer;
    transition: all 0.1s;
    border-left: 2px solid transparent;
}

.tree-file:hover {
    background: var(--bg-hover);
    color: var(--text-primary);
}

.tree-file.active {
    background: var(--bg-active);
    color: var(--accent);
    border-left-color: var(--accent);
}

.tree-file--empty {
    color: var(--text-muted);
}

.tree-file-badge {
    display: inline-block;
    margin-left: 6px;
    font-size: 11px;
    color: var(--text-muted);
    opacity: 0.7;
}

/* ── Content area ── */
.content-area {
    flex: 1;
    overflow-y: auto;
    padding: 40px 56px;
    background: var(--bg-primary);
    transition: background 0.2s;
}

.content-welcome {
    display: flex;
    align-items: center;
    justify-content: center;
    height: 100%;
    color: var(--text-muted);
    font-size: 14px;
}

/* ── Markdown rendered content ── */
.markdown-body {
    max-width: 800px;
    line-height: 1.7;
    color: var(--text-primary);
}

.markdown-body h1 {
    font-size: 24px;
    font-weight: 700;
    margin: 0 0 8px;
    padding-bottom: 8px;
    border-bottom: 1px solid var(--border);
    letter-spacing: -0.3px;
}

.markdown-body h2 {
    font-size: 20px;
    font-weight: 600;
    margin: 24px 0 8px;
}

.markdown-body h3 {
    font-size: 16px;
    font-weight: 600;
    margin: 20px 0 6px;
    color: var(--text-secondary);
}

.markdown-body p {
    margin: 0 0 12px;
    font-size: 14px;
}

.markdown-body ul, .markdown-body ol {
    margin: 0 0 12px;
    padding-left: 24px;
}

.markdown-body li {
    font-size: 14px;
    margin-bottom: 4px;
}

.markdown-body table {
    width: 100%;
    border-collapse: collapse;
    margin: 12px 0;
    font-size: 14px;
}

.markdown-body th, .markdown-body td {
    border: 1px solid var(--border);
    padding: 8px 12px;
    text-align: left;
}

.markdown-body th {
    background: var(--bg-tertiary);
    font-weight: 600;
}

.markdown-body code {
    background: var(--code-bg);
    padding: 2px 6px;
    border-radius: 4px;
    font-size: 13px;
}

.markdown-body pre {
    background: var(--code-bg);
    padding: 16px;
    border-radius: 8px;
    overflow-x: auto;
    margin: 12px 0;
}

.markdown-body pre code {
    background: none;
    padding: 0;
}

.markdown-body .mermaid {
    margin: 16px 0;
    width: calc(100vw - 280px - 112px);
    max-width: calc(100vw - 280px - 112px);
    overflow-x: auto;
}

.markdown-body .mermaid svg {
    height: auto !important;
    min-height: 400px;
}

.markdown-body blockquote {
    border-left: 3px solid var(--scrollbar);
    margin: 12px 0;
    padding: 4px 16px;
    color: var(--text-tertiary);
}

/* ── Content header with print button ── */
.content-header {
    display: flex;
    justify-content: flex-end;
    gap: 8px;
    margin-bottom: 16px;
    max-width: 800px;
}

.btn-share,
.btn-print {
    display: inline-flex;
    align-items: center;
    gap: 6px;
    background: var(--bg-tertiary);
    border: 1px solid var(--border);
    border-radius: 6px;
    padding: 6px 14px;
    font-size: 13px;
    font-family: inherit;
    color: var(--text-secondary);
    cursor: pointer;
    transition: all 0.15s;
}

.btn-share:hover,
.btn-print:hover {
    background: var(--bg-hover);
    color: var(--text-primary);
    border-color: var(--text-muted);
}

.btn-share.copied {
    color: var(--accent);
    border-color: var(--accent);
}

/* ── HTMX loading indicator ── */
.htmx-indicator {
    display: none;
}
.htmx-request .htmx-indicator {
    display: inline-block;
}

/* ── Mobile top bar & sidebar ── */
.mobile-topbar {
    display: none;
}

.sidebar-overlay {
    display: none;
    position: fixed;
    inset: 0;
    background: rgba(0, 0, 0, 0.4);
    z-index: 999;
}

@media (max-width: 768px) {
    .mobile-topbar {
        display: flex;
        align-items: center;
        position: fixed;
        top: 0;
        left: 0;
        right: 0;
        height: 48px;
        z-index: 1001;
        background: var(--bg-secondary);
        border-bottom: 1px solid var(--border);
        padding: 0 12px;
        transition: background 0.2s, border-color 0.2s;
    }

    .mobile-topbar-btn {
        background: none;
        border: none;
        padding: 6px;
        cursor: pointer;
        color: var(--text-secondary);
        display: flex;
        align-items: center;
        justify-content: center;
    }

    .mobile-topbar-btn .icon-close {
        display: none;
    }

    .mobile-topbar-btn.active .icon-menu {
        display: none;
    }

    .mobile-topbar-btn.active .icon-close {
        display: block;
    }

    .mobile-topbar-title {
        flex: 1;
        text-align: center;
        font-size: 16px;
        font-weight: 700;
        letter-spacing: -0.3px;
        color: var(--text-primary);
    }

    .mobile-topbar-spacer {
        width: 32px;
    }

    .sidebar {
        position: fixed;
        top: 48px;
        left: 0;
        z-index: 1000;
        height: calc(100vh - 48px);
        transform: translateX(-100%);
        transition: transform 0.25s ease, background 0.2s, border-color 0.2s;
        box-shadow: 4px 0 16px var(--shadow);
    }

    .sidebar.open {
        transform: translateX(0);
    }

    .sidebar-header h1 {
        display: none;
    }

    .sidebar-overlay.open {
        display: block;
        top: 48px;
    }

    .content-area {
        padding: 64px 20px 20px;
    }

    .markdown-body .mermaid {
        width: calc(100vw - 40px);
        max-width: calc(100vw - 40px);
    }

    .btn-share .btn-share-text,
    .btn-print .btn-print-text {
        display: none;
    }
}

/* ── Print styles ── */
@media print {
    .sidebar,
    .content-header,
    .theme-toggle,
    .mobile-topbar {
        display: none !important;
    }

    .viewer {
        display: block;
    }

    .content-area {
        padding: 0;
        overflow: visible;
        background: #fff;
    }

    .markdown-body {
        max-width: 100%;
        color: #000;
    }

    .markdown-body h1 {
        border-bottom-color: #ccc;
    }

    .markdown-body th {
        background: #eee;
    }

    .markdown-body th, .markdown-body td {
        border-color: #ccc;
    }

    .markdown-body code {
        background: #eee;
    }

    .markdown-body pre {
        background: #eee;
    }

    .markdown-body blockquote {
        border-left-color: #ccc;
        color: #555;
    }
}
