:root {
    color-scheme: light;
    --bg:      #ffffff;
    --fg:      #1c1c1c;
    --panel:   #f6f6f4;
    --border:  #d8d8d2;
    --accent:  #1a6feb;
    --fg30: #1c1c1c; --fg31: #c0392b; --fg32: #2e7d32; --fg33: #8a6d00;
    --fg34: #1a6feb; --fg35: #8e24aa; --fg36: #00737d; --fg90: #6b7280;
    --mono:    ui-monospace, "SF Mono", "Cascadia Mono", Menlo, Consolas, monospace;
}

/* `darkmode-toggle.js` always stamps one of these before the first paint, so no media query is needed. */
html.dark-mode {
    color-scheme: dark;
    --bg:     #16181c;
    --fg:     #e6e6e6;
    --panel:  #1f2229;
    --border: #343841;
    --accent: #62a0ff;
    --fg30: #9aa0a6; --fg31: #ff7b6b; --fg32: #7ee787; --fg33: #e3b341;
    --fg34: #62a0ff; --fg35: #d2a8ff; --fg36: #56d4dd; --fg90: #8b949e;
}

#theme {
    display: flex;
    align-items: center;
    padding: .2rem;
    border-radius: 6px;
    cursor: pointer;
    user-select: none;
}

#theme:hover { background: rgba(127, 127, 127, .2); }
#theme svg { display: block; width: 20px; height: 20px; }

* { box-sizing: border-box; }

body {
    margin: 0;
    height: 100dvh;
    display: flex;
    flex-direction: column;
    background: var(--bg);
    color: var(--fg);
    font: 14px/1.5 system-ui, sans-serif;
}

header {
    display: flex;
    gap: .75rem;
    align-items: center;
    flex-wrap: wrap;
    padding: .5rem .75rem;
    border-bottom: 1px solid var(--border);
    background: var(--panel);
}

header h1 {
    margin: 0 .5rem 0 0;
    font-size: 1rem;
    font-weight: 600;
    letter-spacing: .02em;
}

header .spacer { flex: 1; }

button, select {
    font: inherit;
    color: inherit;
    background: var(--bg);
    border: 1px solid var(--border);
    border-radius: 6px;
    padding: .25rem .6rem;
}

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

#status { font-variant-numeric: tabular-nums; opacity: .7; }
#status.error { color: var(--fg31); opacity: 1; }

main {
    flex: 1;
    display: grid;
    grid-template-columns: minmax(0, 1fr) minmax(0, 1fr);
    min-height: 0;
}

@media (max-width: 800px) {
    main { grid-template-columns: minmax(0, 1fr); grid-template-rows: 1fr 1fr; }
}

#editor, #right {
    min-width: 0;
    min-height: 0;
    display: flex;
    flex-direction: column;
}

#editor { border-right: 1px solid var(--border); }

#editor .cm-editor { flex: 1; min-height: 0; }
#editor .cm-editor.cm-focused { outline: none; }

/* CodeMirror's base theme is light-only, so the page's palette has to override it. */
#editor .cm-gutters {
    background: var(--panel);
    color: var(--fg90);
    border-right: 1px solid var(--border);
}

#editor .cm-activeLineGutter { background: rgba(127, 127, 127, .18); color: var(--fg); }
#editor .cm-activeLine { background: rgba(127, 127, 127, .08); }
#editor .cm-cursor { border-left-color: var(--fg); }
#editor .cm-selectionBackground,
#editor .cm-content ::selection { background: rgba(127, 127, 127, .35); }

/* Mirrors `#tabs` so both columns start at the same height. */
#editor-bar {
    display: flex;
    justify-content: flex-end;
    padding: .4rem .5rem 0;
    border-bottom: 1px solid var(--border);
    background: var(--panel);
}

#editor-bar button {
    border: 0;
    border-radius: 0;
    border-bottom: 2px solid transparent;
    background: none;
    padding: .3rem .7rem;
    opacity: .7;
}

#editor-bar button:hover { opacity: 1; }

#editor-bar label {
    display: flex;
    gap: .3rem;
    align-items: center;
    margin-right: auto;
    padding: .3rem 0;
    opacity: .7;
    font-size: 12.5px;
}

#editor-bar label:hover { opacity: 1; }

#editor .cm-vim-panel {
    font-family: var(--mono);
    border-top: 1px solid var(--border);
    font-size: 12.5px;
    background: var(--panel);
    color: var(--fg);
}

#editor .cm-vim-panel input { color: var(--fg); }

#editor textarea {
    flex: 1;
    border: 0;
    resize: none;
    padding: .75rem;
    font-family: var(--mono);
    font-size: 13px;
    background: var(--bg);
    color: var(--fg);
}

#tabs {
    display: flex;
    gap: .25rem;
    padding: .4rem .5rem 0;
    border-bottom: 1px solid var(--border);
    background: var(--panel);
}

#tabs button {
    border: 0;
    border-bottom: 2px solid transparent;
    border-radius: 0;
    background: none;
    padding: .3rem .7rem;
    opacity: .7;
}

#tabs button[aria-selected="true"] {
    opacity: 1;
    border-bottom-color: var(--accent);
    font-weight: 600;
}

#panes { flex: 1; min-height: 0; position: relative; }

/* `#pane-graph`'s `display` outranks the UA's `[hidden]`, so hiding needs an id, too. */
#panes [hidden] { display: none; }

.pane {
    position: absolute;
    inset: 0;
    overflow: auto;
    padding: .75rem;
}

.pane pre {
    margin: 0;
    font-family: var(--mono);
    font-size: 12.5px;
    white-space: pre;
}

/* A pane with an options bar scrolls in its body, not as a whole. */
#pane-graph, #pane-mim { padding: 0; overflow: hidden; display: flex; flex-direction: column; }
#pane-mim pre { flex: 1; min-height: 0; overflow: auto; padding: .75rem; }

.opts {
    display: flex;
    flex-wrap: wrap;
    gap: .25rem .9rem;
    padding: .4rem .75rem;
    border-bottom: 1px solid var(--border);
    background: var(--panel);
    font-size: 12.5px;
}

.opts label { white-space: nowrap; }

#graph {
    flex: 1;
    min-height: 0;
    overflow: hidden;
    padding: .75rem;
    cursor: grab;
    outline: none;
    user-select: none;
    touch-action: none; /* otherwise a touch drag scrolls instead of panning */
}

#graph.grabbing { cursor: grabbing; }

#graph-nav {
    position: absolute;
    right: .75rem;
    bottom: .75rem;
    display: flex;
    gap: .25rem;
}

#graph-nav button {
    min-width: 1.9rem;
    padding: .15rem .4rem;
    line-height: 1.3;
    background: var(--panel);
    opacity: .7;
}

#graph-nav button:hover { opacity: 1; }
#graph-nav button svg { display: block; width: 14px; height: 14px; fill: currentColor; }

/* `graph.html` is the graph pane alone, so the body is what `#graph-nav` anchors to. */
body.popout { position: relative; }

/* The generated `width`/`height` are stripped, so the viewBox scales the graph to the pane. */
#graph svg { display: block; width: 100%; height: 100%; }

#graph svg :is(.node, .edge) { transition: opacity .1s ease; }
#graph svg.focus :is(.node, .edge) { opacity: .1; }
#graph svg.focus :is(.node, .edge).on { opacity: 1; }

/* A detached edge carries `stroke="none"`, and a presentation attribute loses to a rule - so lighting
   one up reveals exactly what `hidden edges` would otherwise need a recompile to show. */
#graph svg .edge.on path { stroke: var(--accent); stroke-width: 2; }
#graph svg .edge.on polygon { stroke: var(--accent); fill: var(--accent); }
#graph svg .node.hub :is(polygon, polyline) { stroke: var(--accent); stroke-width: 3; }
#pane-log pre { white-space: pre-wrap; }

/* mim's terminal colours, mapped onto the pane; see showLog. */
.fg30 { color: var(--fg30); } .fg31 { color: var(--fg31); } .fg32 { color: var(--fg32); }
.fg33 { color: var(--fg33); } .fg34 { color: var(--fg34); } .fg35 { color: var(--fg35); }
.fg36 { color: var(--fg36); } .fg90 { color: var(--fg90); }

/* The code panes' tokens, drawn from the same palette; see renderCode. */
.tok-comment { color: var(--fg90); }
.tok-string  { color: var(--fg32); }
.tok-number,
.tok-literal { color: var(--fg35); }
.tok-keyword { color: var(--fg34); }
.tok-type    { color: var(--fg36); }
.tok-special { color: var(--fg33); }
