:root {
    --bg: #ffffff;
    --bg-soft: #f6f7f9;
    --border: #e2e5ea;
    --text: #1f2328;
    --muted: #6b7280;
    --accent: #6d4aff;
    --accent-text: #ffffff;
    --danger: #d1242f;
    --ok: #1a7f37;
    --code-bg: #f0f1f4;
    color-scheme: light dark;
}

@media (prefers-color-scheme: dark) {
    :root {
        --bg: #16181d;
        --bg-soft: #1d2026;
        --border: #2d313a;
        --text: #e6e8eb;
        --muted: #9aa1ac;
        --accent: #8b6dff;
        --danger: #ff6b6b;
        --ok: #4ac26b;
        --code-bg: #242830;
    }
}

* { box-sizing: border-box; }

[hidden] { display: none !important; }

html, body {
    height: 100%;
    margin: 0;
    background: var(--bg);
    color: var(--text);
    font: 15px/1.5 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
}

button {
    font: inherit;
    padding: 0.35rem 0.8rem;
    border: 1px solid var(--border);
    border-radius: 6px;
    background: var(--bg);
    color: var(--text);
    cursor: pointer;
}
button:hover:not(:disabled) { border-color: var(--accent); }
button:disabled { opacity: 0.45; cursor: default; }
button.primary { background: var(--accent); border-color: var(--accent); color: var(--accent-text); }
button.danger { color: var(--danger); }
button.link { border: none; background: none; color: var(--muted); padding: 0.25rem 0; text-align: left; }
button.link:hover { color: var(--text); }

input {
    font: inherit;
    width: 100%;
    padding: 0.45rem 0.6rem;
    border: 1px solid var(--border);
    border-radius: 6px;
    background: var(--bg);
    color: var(--text);
}
input:focus { outline: 2px solid var(--accent); outline-offset: -1px; }

.muted { color: var(--muted); }
.error { color: var(--danger); min-height: 1.5em; margin: 0; }

/* ---------------------------------------------------------------- Login */

.login {
    min-height: 100%;
    display: grid;
    place-items: center;
    background: var(--bg-soft);
}
.login-card {
    width: min(340px, 90vw);
    display: grid;
    gap: 0.9rem;
    padding: 2rem;
    background: var(--bg);
    border: 1px solid var(--border);
    border-radius: 12px;
}
.login-card h1 { margin: 0 0 0.5rem; font-size: 1.5rem; text-align: center; }
.login-card label { display: grid; gap: 0.3rem; font-size: 0.9rem; color: var(--muted); }

/* ---------------------------------------------------------------- Layout */

.app {
    height: 100%;
    display: grid;
    grid-template-columns: 280px 1fr;
}

.sidebar {
    display: flex;
    flex-direction: column;
    gap: 0.75rem;
    padding: 0.9rem;
    background: var(--bg-soft);
    border-right: 1px solid var(--border);
    min-height: 0;
}
.sidebar-head { display: flex; align-items: center; justify-content: space-between; }
.brand { font-weight: 600; font-size: 1.1rem; }

.tree { flex: 1; overflow-y: auto; font-size: 0.93rem; }
.tree ul { list-style: none; margin: 0; padding-left: 0.9rem; }
.tree > ul { padding-left: 0; }
.tree summary { cursor: pointer; padding: 0.15rem 0; color: var(--muted); user-select: none; }
.tree a {
    display: block;
    padding: 0.15rem 0.45rem;
    border-radius: 5px;
    color: var(--text);
    text-decoration: none;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}
.tree a:hover { background: var(--border); }
.tree a.active { background: var(--accent); color: var(--accent-text); }

.results { list-style: none; margin: 0; padding: 0; display: grid; gap: 0.2rem; }
.results a { white-space: normal; padding: 0.4rem 0.5rem; }
.results strong { display: block; font-weight: 600; }
.results small { display: block; color: var(--muted); font-size: 0.78rem; }
.results .snippet { display: block; font-size: 0.83rem; color: var(--muted); margin-top: 0.15rem; }
.results mark { background: #ffe58f; color: #1f2328; border-radius: 2px; padding: 0 1px; }
.results a.active small, .results a.active .snippet { color: inherit; opacity: 0.85; }
.results-heading { margin: 0.9rem 0.5rem 0.3rem; font-size: 0.75rem; font-weight: 600; text-transform: uppercase; letter-spacing: 0.04em; color: var(--muted); }

.main { display: flex; flex-direction: column; min-width: 0; min-height: 0; }

.toolbar {
    display: flex;
    align-items: center;
    gap: 0.5rem;
    padding: 0.6rem 1rem;
    border-bottom: 1px solid var(--border);
}
.note-path { font-weight: 600; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.status { flex: 1; font-size: 0.85rem; color: var(--muted); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.status.ok { color: var(--ok); }
.status.error { color: var(--danger); }

.empty-state { margin: 4rem auto; color: var(--muted); text-align: center; padding: 0 1rem; }

/* ---------------------------------------------------------------- Format bar */

.format-bar {
    display: flex;
    flex-wrap: wrap;
    align-items: center;
    gap: 0.2rem;
    padding: 0.35rem 1rem;
    border-bottom: 1px solid var(--border);
}
.format-bar button {
    min-width: 2rem;
    padding: 0.2rem 0.45rem;
    border-color: transparent;
    background: none;
    line-height: 1.3;
}
.format-bar button:hover { background: var(--bg-soft); border-color: var(--border); }
.format-bar code { font-size: 0.8rem; }
.format-bar .sep { width: 1px; height: 1.2rem; background: var(--border); margin: 0 0.3rem; }

/* ---------------------------------------------------------------- Editor (CodeMirror, live preview) */

.editor { flex: 1; min-height: 0; overflow: hidden; }
.editor .cm-editor { height: 100%; background: var(--bg); color: var(--text); }
.editor .cm-editor.cm-focused { outline: none; }
.editor .cm-scroller { font: 16px/1.7 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif; }
.editor .cm-content {
    max-width: 820px;
    margin: 0 auto;
    padding: 1.5rem 2rem 40vh;
    caret-color: var(--accent);
}
.editor .cm-line { padding: 0; }
.editor .cm-cursor { border-left: 2px solid var(--accent); }
.editor .cm-selectionBackground,
.editor .cm-focused .cm-selectionBackground { background: color-mix(in srgb, var(--accent) 25%, transparent) !important; }
.editor .cm-placeholder { color: var(--muted); }
.editor .cm-selectionMatch { background: color-mix(in srgb, #ffe58f 45%, transparent); }
.editor .cm-panels { background: var(--bg-soft); color: var(--text); border-color: var(--border); }
.editor .cm-panels input, .editor .cm-panels button { width: auto; font-size: 0.85rem; padding: 0.15rem 0.4rem; }
.editor .cm-searchMatch { background: color-mix(in srgb, #ffe58f 60%, transparent); }

/* Headings: the whole line gets the size, so it also applies while the "#" is visible. */
.editor .md-h { font-weight: 700; line-height: 1.3; }
.editor .md-h1 { font-size: 1.9em; padding-top: 0.6em !important; }
.editor .md-h2 { font-size: 1.5em; padding-top: 0.5em !important; }
.editor .md-h3 { font-size: 1.25em; padding-top: 0.4em !important; }
.editor .md-h4, .editor .md-h5, .editor .md-h6 { font-size: 1.05em; }

.editor .md-strong { font-weight: 700; }
.editor .md-em { font-style: italic; }
.editor .md-strike { text-decoration: line-through; color: var(--muted); }
.editor .md-link { color: var(--accent); text-decoration: underline; text-underline-offset: 2px; }
.editor .md-url { color: var(--muted); }
.editor .md-mark { color: var(--muted); }
.editor .md-code {
    font-family: ui-monospace, "JetBrains Mono", "Cascadia Code", Menlo, Consolas, monospace;
    font-size: 0.9em;
    background: var(--code-bg);
    border-radius: 3px;
}

.editor .md-quote { border-left: 3px solid var(--accent); padding-left: 1rem !important; color: var(--muted); }
.editor .md-quote-text { font-style: italic; }

.editor .md-bullet { color: var(--accent); font-weight: 700; padding-right: 0.15em; }
.editor .md-task { width: auto; margin: 0 0.35em 0 0; vertical-align: middle; cursor: pointer; accent-color: var(--accent); }
.editor .md-task-done { color: var(--muted); text-decoration: line-through; }

.editor .md-hr { display: inline-block; width: 100%; height: 0; border-top: 1px solid var(--border); vertical-align: middle; }

.editor .md-codeblock, .editor .md-table, .editor .md-frontmatter {
    font-family: ui-monospace, "JetBrains Mono", "Cascadia Code", Menlo, Consolas, monospace;
    font-size: 0.88em;
}
.editor .md-codeblock { background: var(--code-bg); padding: 0 0.8rem !important; }
.editor .md-codeblock-first { border-top-left-radius: 6px; border-top-right-radius: 6px; }
.editor .md-codeblock-last { border-bottom-left-radius: 6px; border-bottom-right-radius: 6px; }
.editor .md-codeblock .md-code { background: none; }
.editor .md-frontmatter { color: var(--muted); background: var(--bg-soft); padding: 0 0.8rem !important; }
/* Frontmatter is YAML, not Markdown: undo the Markdown highlighting inside it. */
.editor .md-frontmatter span { color: inherit; text-decoration: none; font-weight: inherit; font-style: inherit; background: none; }

/* ---------------------------------------------------------------- Preview */

.preview {
    flex: 1;
    overflow-y: auto;
    width: 100%;
    max-width: 900px;
    margin: 0 auto;
    padding: 1.5rem 2rem 4rem;
    line-height: 1.7;
}
.preview h1, .preview h2, .preview h3 { line-height: 1.3; margin: 1.6em 0 0.6em; }
.preview h1 { font-size: 1.9rem; }
.preview h2 { font-size: 1.45rem; border-bottom: 1px solid var(--border); padding-bottom: 0.3rem; }
.preview > div > :first-child { margin-top: 0; }
.preview a { color: var(--accent); }
.preview code { background: var(--code-bg); padding: 0.1em 0.35em; border-radius: 4px; font-size: 0.9em; }
.preview pre { background: var(--code-bg); padding: 0.9rem 1rem; border-radius: 8px; overflow-x: auto; }
.preview pre code { background: none; padding: 0; }
.preview blockquote { margin: 1em 0; padding: 0.2em 1em; border-left: 4px solid var(--accent); color: var(--muted); }
.preview table { border-collapse: collapse; margin: 1em 0; }
.preview th, .preview td { border: 1px solid var(--border); padding: 0.4rem 0.7rem; }
.preview img { max-width: 100%; }
.preview hr { border: none; border-top: 1px solid var(--border); margin: 2em 0; }
.preview pre.frontmatter { font-size: 0.85rem; color: var(--muted); margin: 0 0 1.5rem; }

/* ---------------------------------------------------------------- Mobile */

@media (max-width: 760px) {
    .app { grid-template-columns: 1fr; grid-template-rows: auto 1fr; }
    .sidebar { max-height: 40vh; border-right: none; border-bottom: 1px solid var(--border); }
    .toolbar { flex-wrap: wrap; }
    .preview { padding: 1rem; }
    .editor .cm-content { padding: 1rem 1rem 40vh; }
    .format-bar { padding: 0.3rem 0.5rem; flex-wrap: nowrap; overflow-x: auto; }
    .format-bar button { flex: none; }
}
