:root {
    color-scheme: light dark;
    --bg: #ffffff;
    --fg: #1f2328;
    --surface: #f6f8fa;
    --border: #d0d7de;
    --border-soft: #e6e9ed;
    --code-bg: #f6f8fa;
    --inline-code-bg: #eff1f3;
    --muted: #656d76;
    --accent: #3b5bdb;
    --accent-soft: rgba(59, 91, 219, 0.08);
    --btn-hover: rgba(0, 0, 0, 0.06);
    --btn-active: rgba(0, 0, 0, 0.1);
    --shadow: 0 1px 2px rgba(0, 0, 0, 0.05);
}

@media (prefers-color-scheme: dark) {
    :root {
        --bg: #1c1f24;
        --fg: #d6dbe1;
        --surface: #24282e;
        --border: #3a3f46;
        --border-soft: #2c3036;
        --code-bg: #22262c;
        --inline-code-bg: #2d323a;
        --muted: #8b949e;
        --accent: #7b96ff;
        --accent-soft: rgba(123, 150, 255, 0.12);
        --btn-hover: rgba(255, 255, 255, 0.07);
        --btn-active: rgba(255, 255, 255, 0.12);
        --shadow: 0 1px 2px rgba(0, 0, 0, 0.4);
    }
}

body {
    margin: 0;
    font-family: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
    background: var(--bg);
    color: var(--fg);
    display: flex;
    flex-direction: column;
    height: 100vh;
}

/* --- Toolbar --- */

#toolbar {
    padding: 6px 12px;
    background-color: var(--surface);
    border-bottom: 1px solid var(--border-soft);
    display: flex;
    flex-wrap: wrap;
    align-items: center;
    gap: 4px 0;
    box-shadow: var(--shadow);
    position: relative;
    z-index: 2;
}

#brand {
    font-size: 15px;
    font-weight: 300;
    letter-spacing: 0.02em;
    margin-right: 14px;
    color: var(--muted);
    user-select: none;
}

#brand b {
    font-weight: 700;
    color: var(--accent);
}

#toolbar .group {
    display: flex;
    align-items: center;
    padding: 0 8px;
    border-left: 1px solid var(--border-soft);
}

#toolbar .group:first-of-type {
    border-left: none;
    padding-left: 0;
}

#toolbar button {
    appearance: none;
    border: none;
    background: transparent;
    color: var(--fg);
    font: 500 13px/1 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
    padding: 7px 10px;
    margin: 0 1px;
    border-radius: 6px;
    cursor: pointer;
    white-space: nowrap;
    transition: background-color 0.12s;
}

#toolbar button:hover {
    background-color: var(--btn-hover);
}

#toolbar button:active {
    background-color: var(--btn-active);
}

#toolbar button:focus-visible {
    outline: 2px solid var(--accent);
    outline-offset: -2px;
}

/* --- Panes --- */

.container {
    display: flex;
    flex: 1;
    min-height: 0; /* allow children to scroll instead of overflowing */
}

.CodeMirror {
    box-sizing: border-box;
    width: 50%;
    height: 100%;
    border-right: 1px solid var(--border-soft);
    font-family: ui-monospace, "Cascadia Code", Consolas, Menlo, monospace;
    font-size: 14px;
    line-height: 1.55;
}

/* pad inside CM's scroll area, not the outer box (breaks CM's size math) */
.CodeMirror-lines {
    padding: 12px 4px;
}

.CodeMirror-gutters {
    border-right: none;
    background: transparent;
}

.CodeMirror-linenumber {
    color: var(--muted);
    opacity: 0.55;
}

#output {
    box-sizing: border-box;
    padding: 24px 32px 48px;
    width: 50%;
    height: 100%;
    overflow-y: auto;
    line-height: 1.6;
    font-size: 15px;
}

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

#output h1, #output h2, #output h3, #output h4 {
    margin: 1.4em 0 0.5em;
    line-height: 1.25;
    font-weight: 650;
}

#output h1:first-child, #output h2:first-child, #output h3:first-child {
    margin-top: 0;
}

#output h1 {
    font-size: 1.9em;
    padding-bottom: 0.25em;
    border-bottom: 1px solid var(--border-soft);
}

#output h2 {
    font-size: 1.45em;
    padding-bottom: 0.25em;
    border-bottom: 1px solid var(--border-soft);
}

#output h3 { font-size: 1.15em; }

#output a {
    color: var(--accent);
    text-decoration: none;
}

#output a:hover {
    text-decoration: underline;
}

#output hr {
    border: none;
    border-top: 2px solid var(--border-soft);
    margin: 2em 0;
}

#output ul, #output ol {
    padding-left: 1.6em;
}

#output li + li {
    margin-top: 0.2em;
}

#output li > input[type="checkbox"] {
    margin-right: 0.4em;
    accent-color: var(--accent);
}

/* --- Code --- */

pre {
    background-color: var(--code-bg);
    border: 1px solid var(--border-soft);
    padding: 12px 14px;
    border-radius: 8px;
    overflow-x: auto;
    font-size: 13px;
    line-height: 1.5;
}

pre code.hljs {
    padding: 0;
    background: transparent;
}

code {
    font-family: ui-monospace, "Cascadia Code", Consolas, Menlo, monospace;
}

:not(pre) > code {
    background-color: var(--inline-code-bg);
    padding: 0.15em 0.4em;
    border-radius: 4px;
    font-size: 88%;
}

.mermaid-error {
    color: #d33;
}

.mermaid {
    text-align: center;
}

/* --- Media, tables, quotes --- */

img {
    max-width: 100%;
    height: auto;
    border-radius: 6px;
}

table {
    border-collapse: collapse;
    margin: 1em 0;
    display: block;
    max-width: 100%;
    overflow-x: auto;
}

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

th {
    background-color: var(--surface);
    font-weight: 600;
}

tr:nth-child(2n) td {
    background-color: var(--surface);
}

blockquote {
    border-left: 4px solid var(--accent);
    background: var(--accent-soft);
    color: var(--muted);
    padding: 0.6em 14px;
    margin: 1em 0;
    border-radius: 0 6px 6px 0;
}

blockquote p {
    margin: 0;
}

/* --- Status bar --- */

#status-bar {
    padding: 5px 14px;
    font-size: 12px;
    letter-spacing: 0.01em;
    color: var(--muted);
    background-color: var(--surface);
    border-top: 1px solid var(--border-soft);
    user-select: none;
}

/* --- Scrollbars (WebKit) --- */

::-webkit-scrollbar {
    width: 10px;
    height: 10px;
}

::-webkit-scrollbar-thumb {
    background: var(--border);
    border-radius: 5px;
    border: 2px solid transparent;
    background-clip: content-box;
}

::-webkit-scrollbar-thumb:hover {
    background: var(--muted);
    background-clip: content-box;
}

::-webkit-scrollbar-track {
    background: transparent;
}

/* --- Print: only the rendered preview (Print button = PDF export) --- */

@media print {
    #toolbar, .CodeMirror, #status-bar {
        display: none;
    }
    .container {
        display: block;
        height: auto;
    }
    #output {
        width: 100%;
        height: auto;
        overflow: visible;
        padding: 0;
    }
}
