/* ── 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 {
    color: var(--text-muted);
    font-size: 14px;
    transition: transform 0.15s;
}

.tree-folder[open] > summary .folder-icon {
    transform: rotate(90deg);
}

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

.tree-file {
    display: block;
    padding: 5px 16px 5px 28px;
    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);
}

/* ── 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;
}

/* ── Print styles ── */
@media print {
    .sidebar,
    .content-header,
    .theme-toggle {
        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;
    }
}
