:root {
    --bg: #eef4f7;
    --text: #171c40;
    --muted: #526071;
    --note: #68758a;
    --panel-bg: #ffffff;
    --panel-border: #e0e8f1;
    --panel-shadow: rgba(32, 45, 72, 0.035);
    --header-border: #eaf0f6;
    --btn-bg: #ffffff;
    --btn-border: #d5e1ef;
    --btn-text: #171c40;
    --field-bg: #ffffff;
    --field-text: #172033;
    --console-bg: #0f172a;
    --console-text: #e2e8f0;
    --error-bg: #fff1f1;
    --error-border: #f0b9b9;
    --error-text: #a40000;
    --accent: #2563eb;

    /* Deterministic per-variable-name palette (see .var-color-N below). Each node id is
       hashed to one of these 16 slots so the same id always gets the same color, shared
       between the "Show variable names" preview badge and the source highlight overlay.
       Built from 8 hues spaced 45deg apart (double the gap of a naive 16-way hue split)
       so any two different hue groups are always clearly distinct; slots 0-7 are a vivid,
       highly saturated variant of each hue and slots 8-15 are a muted/desaturated variant
       of the same 8 hues, so even same-hue neighbors read as visibly different colors. */
    --var-color-0: hsl(0, 80%, 38%);
    --var-color-1: hsl(45, 80%, 38%);
    --var-color-2: hsl(90, 80%, 32%);
    --var-color-3: hsl(135, 80%, 32%);
    --var-color-4: hsl(180, 80%, 32%);
    --var-color-5: hsl(225, 80%, 45%);
    --var-color-6: hsl(270, 80%, 45%);
    --var-color-7: hsl(315, 80%, 42%);
    --var-color-8: hsl(0, 40%, 48%);
    --var-color-9: hsl(45, 45%, 40%);
    --var-color-10: hsl(90, 40%, 38%);
    --var-color-11: hsl(135, 40%, 40%);
    --var-color-12: hsl(180, 40%, 38%);
    --var-color-13: hsl(225, 45%, 55%);
    --var-color-14: hsl(270, 45%, 55%);
    --var-color-15: hsl(315, 40%, 50%);
}

/* Scope the base typography/colors to the editor wrapper instead of :root so the
   consuming app's global styles are not affected. */
.mermaid-editor {
    font-family: Inter, Arial, sans-serif;
    color: var(--text);
    background: var(--bg);
}

:root[data-theme="dark"] {
    --bg: #0b1120;
    --text: #e2e8f0;
    --muted: #94a3b8;
    --note: #8595ad;
    --panel-bg: #131c2e;
    --panel-border: #263247;
    --panel-shadow: rgba(0, 0, 0, 0.45);
    --header-border: #263247;
    --btn-bg: #1e293b;
    --btn-border: #334155;
    --btn-text: #e2e8f0;
    --field-bg: #0f1729;
    --field-text: #e2e8f0;
    --console-bg: #05080f;
    --console-text: #cbd5e1;
    --error-bg: #3b1212;
    --error-border: #7f1d1d;
    --error-text: #fca5a5;

    /* Same 8-hue vivid/muted scheme, lightened for contrast against dark panels. */
    --var-color-0: hsl(0, 85%, 68%);
    --var-color-1: hsl(45, 85%, 62%);
    --var-color-2: hsl(90, 75%, 62%);
    --var-color-3: hsl(135, 70%, 62%);
    --var-color-4: hsl(180, 70%, 60%);
    --var-color-5: hsl(225, 85%, 75%);
    --var-color-6: hsl(270, 80%, 78%);
    --var-color-7: hsl(315, 80%, 72%);
    --var-color-8: hsl(0, 45%, 72%);
    --var-color-9: hsl(45, 45%, 68%);
    --var-color-10: hsl(90, 40%, 65%);
    --var-color-11: hsl(135, 40%, 65%);
    --var-color-12: hsl(180, 40%, 65%);
    --var-color-13: hsl(225, 50%, 80%);
    --var-color-14: hsl(270, 45%, 82%);
    --var-color-15: hsl(315, 45%, 78%);
}

/* ----- Data Flow Diagram mode -----
   Self-contained teal/cyan theme with both a light and a dark variant. The
   variables are declared on the .page.mode-dfd element itself so they override
   the inherited palette for this subtree, while still responding to the
   light/dark toggle (data-theme on :root). Default below is the LIGHT variant. */
.page.mode-dfd {
    --bg: #e6f6f3;
    --text: #0b3a36;
    --muted: #3f726b;
    --note: #4f827b;
    --panel-bg: #f3fbfa;
    --panel-border: #b3ddd6;
    --panel-shadow: rgba(13, 148, 136, 0.15);
    --header-border: #b3ddd6;
    --btn-bg: #d6f0eb;
    --btn-border: #8fccc2;
    --btn-text: #0b3a36;
    --field-bg: #ffffff;
    --field-text: #0b3a36;
    --console-bg: #e1f5f1;
    --console-text: #0b3a36;
    --error-bg: #fde8e8;
    --error-border: #f5b5b5;
    --error-text: #b91c1c;
    --accent: #0d9488;

    background: var(--bg);
    color: var(--text);
}

/* DARK variant of the Data Flow Diagram theme. */
:root[data-theme="dark"] .page.mode-dfd {
    --bg: #07171c;
    --text: #d7f3ee;
    --muted: #8fb3ad;
    --note: #7fa39d;
    --panel-bg: #0f242b;
    --panel-border: #1d4a52;
    --panel-shadow: rgba(0, 0, 0, 0.5);
    --header-border: #1d4a52;
    --btn-bg: #123038;
    --btn-border: #1f5560;
    --btn-text: #d7f3ee;
    --field-bg: #0b1f25;
    --field-text: #d7f3ee;
    --console-bg: #05161a;
    --console-text: #b8e6df;
    --error-bg: #3b1212;
    --error-border: #7f1d1d;
    --error-text: #fca5a5;
    --accent: #14b8a6;
}

.mode-select {
    gap: 6px;
}

.mode-select select {
    border: 1px solid var(--btn-border);
    background: var(--btn-bg);
    color: var(--btn-text);
    border-radius: 4px;
    padding: 6px 8px;
    min-width: 0;
    width: auto;
    max-width: 100%;
    font-weight: 600;
    cursor: pointer;
}

.mode-badge {
    display: inline-block;
    margin-left: 10px;
    padding: 2px 10px;
    font-size: 13px;
    font-weight: 700;
    color: #ffffff;
    background: var(--accent);
    border-radius: 999px;
    vertical-align: middle;
}

.dfd-bar {
    display: flex;
    flex-wrap: wrap;
    align-items: center;
    gap: 8px;
    margin: 0;
    padding: 8px 10px;
    background: var(--panel-bg);
    border-bottom: 1px solid var(--header-border);
    flex-shrink: 0;
}

.dfd-bar .dfd-title {
    font-weight: 700;
    margin-right: 4px;
    color: var(--accent);
}

.dfd-bar button {
    padding: 6px 10px;
    border-radius: 4px;
}

.dfd-bar .dfd-settings-button {
    margin-left: auto;
}

.dfd-sep {
    width: 1px;
    align-self: stretch;
    background: var(--panel-border);
    margin: 0 4px;
}

.dfd-legend {
    display: flex;
    flex-wrap: wrap;
    gap: 14px;
    width: 100%;
    font-size: 12px;
    color: var(--muted);
    align-items: center;
}

.dfd-legend code {
    background: var(--field-bg);
    color: var(--field-text);
    padding: 2px 6px;
    border-radius: 6px;
    border: 1px solid var(--panel-border);
}

.dfd-legend .swatch {
    display: inline-block;
    width: 12px;
    height: 12px;
    margin-right: 6px;
    vertical-align: middle;
    border: 1px solid var(--accent);
}

.dfd-legend .swatch.entity {
    border-radius: 2px;
}

.dfd-legend .swatch.process {
    border-radius: 2px;
    box-shadow: inset 3px 0 0 var(--accent), inset -3px 0 0 var(--accent);
}

.dfd-legend .swatch.store {
    border-radius: 50% / 30%;
}

.dfd-legend .swatch.flow {
    border: 0;
    height: 0;
    border-top: 2px solid var(--accent);
    width: 16px;
}

.dfd-legend-panel {
    padding: 10px;
    background: var(--panel-bg);
    border-bottom: 1px solid var(--header-border);
    flex-shrink: 0;
}

.dfd-dialog {
    width: min(720px, calc(100vw - 32px));
    max-width: none;
    max-height: calc(100dvh - 48px);
    margin: auto;
    padding: 20px;
    box-sizing: border-box;
    overflow-y: auto;
    border: 1px solid var(--panel-border);
    border-radius: 8px;
    background: var(--panel-bg);
    color: var(--text);
}

.dfd-dialog::backdrop {
    background: rgba(10, 20, 40, 0.5);
}

.dfd-dialog-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 12px;
    margin-bottom: 16px;
}

.dfd-dialog h2 {
    margin: 0;
    font-size: 18px;
}

.dfd-dialog .dfd-legend {
    margin: 16px 0;
}

.dfd-dialog .dfd-legend-panel {
    padding: 16px 0 0;
    border-top: 1px solid var(--header-border);
    border-bottom: 0;
}

.dfd-dialog .small-note {
    white-space: normal;
}

.dfd-legend-head {
    display: flex;
    flex-wrap: wrap;
    align-items: center;
    gap: 10px;
    margin-bottom: 10px;
}

.dfd-legend-head .dfd-title {
    color: var(--accent);
}

.dfd-legend-actions {
    display: inline-flex;
    flex-wrap: wrap;
    gap: 8px;
    margin-left: auto;
}

.dfd-legend-actions button {
    padding: 6px 10px;
    border-radius: 4px;
}

.mermaid-editor .dfd-legend-text {
    width: 100%;
    min-height: 96px;
    box-sizing: border-box;
    padding: 10px 12px;
    border: 1px solid var(--panel-border);
    border-radius: 10px;
    resize: vertical;
    font-family: Consolas, Monaco, monospace;
    font-size: 13px;
    line-height: 1.5;
    background: var(--field-bg);
    color: var(--field-text);
    outline: none;
}

.mermaid-editor.page {
    min-height: 100vh;
    padding: 8px;
    box-sizing: border-box;
}

.header {
    display: flex;
    justify-content: space-between;
    gap: 12px;
    align-items: center;
    margin-bottom: 8px;
    min-height: 30px;
}

.header-brand {
    display: flex;
    align-items: center;
    gap: 4px;
    flex-shrink: 0;
}

.header-logo {
    width: 20px;
    height: 20px;
    object-fit: contain;
    /* white PNG → #14B8A6 (teal accent from social preview) */
    filter: brightness(0) invert(70%) sepia(49%) saturate(441%) hue-rotate(124deg) brightness(82%) contrast(92%);
}

.header h1 {
    margin: 0;
    font-size: 20px;
    font-weight: 600;
    color: #00ada6;
}

.brand-wordmark {
    display: flex;
    align-items: center;
    gap: 14px;
}

.brand-divider {
    height: 20px;
    width: 1px;
    background: var(--btn-border);
}

.brand-product {
    font-size: 13px;
    color: var(--note);
}

.mermaid-editor button.brand-product {
    border: 0;
    background: transparent;
    padding: 0;
    font-weight: 400;
}

.mermaid-editor button.brand-product:hover {
    text-decoration: underline;
    text-underline-offset: 3px;
}

.console-context-tip {
    flex: 1 1 0;
    min-width: 0;
    color: var(--muted);
    font-size: 12px;
    font-weight: 400;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}

.console-context-tip:not(:empty)::before {
    content: "-- ";
}

.toolbar {
    display: flex;
    gap: 8px;
    align-items: center;
    flex-wrap: wrap;
}

:where(.mermaid-editor) button, :where(.mermaid-editor) .toggle {
    border: 1px solid var(--btn-border);
    background: var(--btn-bg);
    color: var(--btn-text);
    border-radius: 4px;
    padding: 6px 10px;
    font-size: 12px;
    font-weight: 600;
    cursor: pointer;
}

.mermaid-editor button:focus-visible,
.mermaid-editor summary:focus-visible,
.mermaid-editor select:focus-visible,
.mermaid-editor input[type="checkbox"]:focus-visible {
    outline: 2px solid var(--accent);
    outline-offset: 2px;
}

.toolbar > button,
.panel-title {
    display: inline-flex;
    align-items: center;
    gap: 8px;
    white-space: nowrap;
}

.toolbar .share-button {
    background: #00a5a8;
    border-color: #00b8bb;
    color: #ffffff;
    padding: 8px 14px;
    box-shadow: inset 0 0 0 1px #ffffff1a;
    min-width: 160px;
    justify-content: center;
}

.source-copy-button {
    min-width: 104px;
}

.mermaid-editor button.copy-success {
    color: #ffffff;
    background: #087f70;
    border-color: #087f70;
}

.mermaid-editor button.copy-error {
    color: var(--error-text);
    background: var(--error-bg);
    border-color: var(--error-border);
}

.editor-notice {
    position: fixed;
    top: 52px;
    right: 12px;
    z-index: 20;
    max-width: min(420px, calc(100vw - 48px));
    padding: 12px;
    border: 1px solid var(--error-border);
    border-radius: 6px;
    background: var(--error-bg);
    color: var(--error-text);
}

.toolbar .mode-select {
    border: 0;
    border-left: 1px solid var(--btn-border);
    border-radius: 0;
    background: transparent;
    padding: 0 0 0 14px;
    margin-left: 6px;
}

.toolbar .theme-button {
    margin-left: 6px;
    background: transparent;
}

.ui-icon {
    width: 16px;
    height: 16px;
    flex-shrink: 0;
    fill: none;
    stroke: currentColor;
    stroke-width: 1.7;
    stroke-linecap: round;
    stroke-linejoin: round;
}

/* Icon-only buttons (e.g. undo/redo in the toolbar, fullscreen in the preview bar)
   center their SVG and size consistently with neighbouring text buttons. */
.mermaid-editor .icon-btn {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    line-height: 0;
}

.mermaid-editor .icon-btn svg {
    display: block;
}

/* In the header toolbar, icon buttons stretch to match the height of the
   neighbouring text buttons so the row stays visually even. */
.mermaid-editor .toolbar .icon-btn {
    align-self: stretch;
}

.mermaid-editor .icon-btn:disabled {
    opacity: 0.45;
    cursor: default;
}

.toggle {
    display: inline-flex;
    align-items: center;
    gap: 8px;
    margin: 0;
}

.grid {
    display: grid;
    grid-template-columns: var(--source-width, 42%) 14px minmax(0, 1fr);
    grid-template-rows: 100%;
    gap: 0;
    align-items: stretch;
}

/* Draggable divider between the source and preview panels. Dragging it adjusts
   --source-width on .grid, growing one panel as the other shrinks. */
.grid-resizer {
    align-self: stretch;
    width: 14px;
    cursor: col-resize;
    position: relative;
    background: linear-gradient(var(--btn-border), var(--btn-border)) center / 1px 100% no-repeat;
    opacity: 0.8;
    transition: opacity 0.15s ease, background 0.15s ease;
    touch-action: none;
}

.grid-resizer::after {
    content: "";
    position: absolute;
    top: 45%;
    left: 4px;
    width: 6px;
    height: 18px;
    background: radial-gradient(circle, #698bb6 1px, transparent 1.5px) 0 0 / 3px 5px;
}

.grid-resizer:hover {
    opacity: 1;
    background: var(--accent);
}

.grid-resizer.dragging {
    opacity: 1;
    background: var(--accent);
}

body.grid-resizing {
    cursor: col-resize;
    user-select: none;
}

.panel {
    background: var(--panel-bg);
    border: 1px solid var(--panel-border);
    border-radius: 8px;
    overflow: hidden;
    box-shadow: 0 2px 12px var(--panel-shadow);
    display: flex;
    flex-direction: column;
    min-width: 0;
}

.console-panel {
    margin-top: 8px;
}

.panel-header {
    padding: 6px 10px;
    border-bottom: 1px solid var(--header-border);
    font-weight: 700;
    display: flex;
    flex-wrap: wrap;
    gap: 14px;
    flex-shrink: 0;
    min-width: 0;
    align-items: center;
    font-size: 14px;
    min-height: 38px;
    box-sizing: border-box;
}

.preview-actions {
    display: inline-flex;
    align-items: center;
    gap: 8px;
    margin-left: auto;
    min-width: 0;
    overflow: hidden;
}

.panel-header .preview-actions {
    flex-wrap: wrap;
    overflow: visible;
}

.preview-actions button,
.preview-actions .toggle {
    height: 28px;
    box-sizing: border-box;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    gap: 6px;
    padding: 0 10px;
    border-radius: 4px;
    font-size: 12px;
    font-weight: 600;
    line-height: 1;
    white-space: nowrap;
}

.preview-actions .icon-btn {
    padding: 0 8px;
    line-height: 0;
}

.scroll-lock-btn.active {
    background: var(--accent);
    border-color: var(--accent);
    color: #ffffff;
}

.display-options {
    position: relative;
    font-size: 12px;
    font-weight: 600;
}

.display-options summary {
    display: flex;
    align-items: center;
    gap: 8px;
    cursor: pointer;
    list-style: none;
    padding: 6px 10px;
    border: 1px solid var(--btn-border);
    border-radius: 4px;
    background: var(--btn-bg);
    white-space: nowrap;
}

.display-options summary::-webkit-details-marker {
    display: none;
}

.display-options .chevron {
    width: 12px;
    height: 12px;
}

.display-options[open] .chevron {
    transform: rotate(180deg);
}

.display-options-menu {
    position: absolute;
    top: calc(100% + 2px);
    left: 0;
    width: 214px;
    padding: 6px;
    box-sizing: border-box;
    border: 1px solid var(--panel-border);
    border-radius: 6px;
    background: var(--panel-bg);
    box-shadow: 0 6px 16px rgba(32, 45, 72, 0.15);
    z-index: 10;
}

.display-options-menu button,
.display-option {
    display: flex;
    align-items: center;
    gap: 12px;
    width: 100%;
    min-height: 32px;
    padding: 7px 8px;
    box-sizing: border-box;
    border: 0;
    background: transparent;
    color: var(--text);
    font-size: 12px;
    font-weight: 600;
    text-align: left;
    cursor: pointer;
}

.display-options-menu button:hover,
.display-option:hover {
    background: var(--bg);
}

.display-option input {
    width: 14px;
    height: 14px;
    margin: 0;
    accent-color: var(--accent);
}

#previewPanel {
    position: relative;
}

#previewPanel > .panel-header {
    position: relative;
    z-index: 6;
}

/* Keep the original palette in its interaction layer so visibility and painting
   continue to use the same handlers; the footer actions sit alongside it. */
.preview-footer {
    position: absolute;
    bottom: 0;
    left: 0;
    right: 0;
    height: 40px;
    border-top: 1px solid var(--header-border);
    pointer-events: none;
    background: var(--panel-bg);
    display: flex;
    align-items: center;
    padding: 0 8px 0 14px;
}

.preview-footer .zoom-actions {
    position: relative;
    z-index: 5;
    pointer-events: auto;
    gap: 8px;
    overflow: visible;
}

#previewPanel.preview-controls-hidden:not(:fullscreen) .preview-footer {
    border-top: 0;
    background: transparent;
}

#previewPanel.preview-controls-hidden:not(:fullscreen) .reset-zoom-button {
    display: none;
}

.zoom-actions button {
    min-width: 28px;
}

.zoom-actions button:nth-child(3) {
    margin: 0 4px;
}

.color-caption {
    position: relative;
    z-index: 5;
    pointer-events: auto;
    font-size: 12px;
    font-weight: 600;
}

#previewPanel.preview-controls-hidden .color-caption {
    display: none;
}

:where(.mermaid-editor) textarea {
    width: 100%;
    border: 0;
    padding: 16px;
    box-sizing: border-box;
    resize: vertical;
    font-family: Consolas, Monaco, monospace;
    font-size: 14px;
    line-height: 1.45;
    outline: none;
    background: var(--field-bg);
    color: var(--field-text);
}

.mermaid-editor .console-output {
    min-height: 180px;
    max-height: 300px;
    border-top: 1px solid var(--header-border);
    background: var(--console-bg);
    color: var(--console-text);
    display: none;
}

.console-panel.expanded .console-output {
    display: block;
}

.console-panel.has-errors {
    border-color: var(--error-border);
}

.console-panel.has-errors .console-bar,
.mermaid-editor .console-panel.has-errors .console-output {
    background: var(--error-bg);
    color: var(--error-text);
}

/* Collapsed by default: the console is just a bar (title + copy/clear buttons). The
   chevron button rotates to show open/closed state; expanding never resizes the
   source/preview grid above it (see updateViewportLayout in mermaidEditor.js) -- it
   just reveals the log below, growing the page past the viewport so only then do you
   need to scroll. */
.panel-header.console-bar {
    align-items: center;
    flex-wrap: nowrap;
}

.console-bar > button,
.console-bar > span:not(.console-context-tip),
.console-bar > .preview-actions {
    flex-shrink: 0;
}

.console-bar .console-toggle {
    border: 0;
    padding: 0;
    width: 18px;
    height: 24px;
    background: transparent;
}

.console-toggle svg {
    transition: transform 0.15s ease;
    transform: rotate(-90deg);
}

.console-panel.expanded .console-toggle svg {
    transform: rotate(0);
}

.preview-wrap {
    flex: 1 1 auto;
    min-height: 0;
    padding: 18px;
    overflow: hidden;
    position: relative;
    cursor: grab;
    touch-action: none;
    user-select: none;
}

.preview-wrap.dragging {
    cursor: grabbing;
}

/* ----- Fullscreen preview panel ----- */
#previewPanel:fullscreen {
    display: flex;
    flex-direction: column;
    width: 100vw;
    height: 100vh;
    background: var(--bg);
}

#previewPanel:fullscreen .preview-wrap {
    flex: 1 1 auto;
    min-height: 0;
}

#previewPanel:fullscreen > .panel-header,
#previewPanel:fullscreen > .dfd-bar,
#previewPanel:fullscreen #interactionLayer,
#previewPanel:fullscreen #errorBox,
#previewPanel:fullscreen .color-caption {
    display: none;
}

#previewPanel:fullscreen {
    border: 0;
    border-radius: 0;
}

#previewPanel:fullscreen .preview-footer {
    position: static;
    flex-shrink: 0;
}

#diagramPreview svg {
    display: block;
}

/* Mermaid appends an empty, padded tooltip to body after the editor. */
.mermaidTooltip {
    top: 0;
    left: 0;
}

.mermaidTooltip:empty {
    display: none;
}

#diagramPreview {
    position: relative;
    min-height: 100%;
}

/* "Show variable names" id prefix (e.g. "A:") injected into node labels. The color
   itself comes from the matching .var-color-N class (see the palette below); this
   just gives every one of them the same bold, clearly-monospace treatment. */
#diagramPreview code {
    font-family: "Cascadia Code", "Courier New", Consolas, monospace;
    font-weight: 700;
}

/* Per-variable-name color classes. Each node id is hashed (see varColorClass() in
   mermaidEditor.js) to one of these 16 slots, applied both to the id badge in the
   preview and to matching id tokens in the source highlight overlay below. */
.var-color-0, .var-color-1, .var-color-2, .var-color-3,
.var-color-4, .var-color-5, .var-color-6, .var-color-7,
.var-color-8, .var-color-9, .var-color-10, .var-color-11,
.var-color-12, .var-color-13, .var-color-14, .var-color-15 {
    font-weight: 700;
    border-radius: 3px;
}

.var-color-0 { color: var(--var-color-0); }
.var-color-1 { color: var(--var-color-1); }
.var-color-2 { color: var(--var-color-2); }
.var-color-3 { color: var(--var-color-3); }
.var-color-4 { color: var(--var-color-4); }
.var-color-5 { color: var(--var-color-5); }
.var-color-6 { color: var(--var-color-6); }
.var-color-7 { color: var(--var-color-7); }
.var-color-8 { color: var(--var-color-8); }
.var-color-9 { color: var(--var-color-9); }
.var-color-10 { color: var(--var-color-10); }
.var-color-11 { color: var(--var-color-11); }
.var-color-12 { color: var(--var-color-12); }
.var-color-13 { color: var(--var-color-13); }
.var-color-14 { color: var(--var-color-14); }
.var-color-15 { color: var(--var-color-15); }

/* ----- Mermaid source syntax highlight overlay -----
   #mermaidSourceInput's own text is made transparent so this <pre> (an exact visual
   twin, same font/padding/line-height, absolutely positioned underneath) shows through
   with colored spans over each known node id. Kept in sync on every keystroke/scroll. */
.source-wrap {
    position: relative;
    flex: 1 1 auto;
    min-height: 0;
    background: var(--field-bg);
    --gutter-width: 44px;
}

/* Highlights the source line matching whatever's hovered in the preview (node, edge,
   subgraph). Covers the text/textarea region only (the gutter's own number is
   highlighted separately via .gutter-line.active, since the gutter has an opaque
   background that would otherwise hide this bar). */
#mermaidSourceLineHighlight {
    position: absolute;
    left: var(--gutter-width);
    right: 0;
    background: rgba(255, 196, 0, 0.22);
    pointer-events: none;
    z-index: 0;
    display: none;
}

#mermaidSourceGutter {
    position: absolute;
    inset: 0;
    right: auto;
    width: var(--gutter-width);
    margin: 0;
    padding: 16px 8px 16px 0;
    box-sizing: border-box;
    font-family: Consolas, Monaco, monospace;
    font-size: 14px;
    line-height: 1.45;
    text-align: right;
    color: var(--text-muted, #888);
    background: var(--panel-bg);
    border-right: 1px solid var(--panel-border);
    overflow: hidden;
    pointer-events: none;
    user-select: none;
    white-space: pre;
    z-index: 1;
}

.gutter-line.active {
    color: var(--field-text);
    font-weight: 700;
    background: rgba(255, 196, 0, 0.35);
}

.source-wrap #mermaidSourceHighlight,
.source-wrap #mermaidSourceInput {
    position: absolute;
    top: 0;
    right: 0;
    bottom: 0;
    left: var(--gutter-width);
    margin: 0;
    width: auto;
    height: 100%;
    padding: 16px;
    box-sizing: border-box;
    border: 0;
    font-family: Consolas, Monaco, monospace;
    font-size: 14px;
    line-height: 1.45;
    /* Wrapping is disabled (rather than pre-wrap) so each source line stays exactly one
       visual line -- required for the line-number gutter to stay aligned with the text. */
    white-space: pre;
    overflow: auto;
}

#mermaidSourceHighlight {
    color: var(--field-text);
    background: transparent;
    pointer-events: none;
    user-select: none;
    z-index: 2;
}

#mermaidSourceInput {
    z-index: 3;
    background: transparent;
    color: transparent;
    caret-color: var(--field-text);
    resize: none;
}

/* Nodexr-style interaction layer */
#interactionLayer {
    position: absolute;
    inset: 0;
    pointer-events: none;
    z-index: 4;
}

#interactionLayer.controls-hidden {
    display: none;
}

/* ----- Paint bucket color bar (bottom of preview) ----- */
#colorBar {
    position: absolute;
    left: 128px;
    right: 200px;
    bottom: 10px;
    height: 20px;
    display: flex;
    align-items: stretch;
    pointer-events: auto;
    z-index: 5;
    border: 1px solid var(--panel-border);
    border-radius: 3px;
    background: var(--panel-bg);
    box-shadow: none;
}

#colorBar.colors-hidden,
.unsupported-diagram #colorBar {
    display: none;
}

.color-swatch {
    flex: 1 1 0;
    min-width: 0;
    height: 100%;
    padding: 0;
    margin: 0;
    border: none;
    border-right: 1px solid rgba(0, 0, 0, 0.12);
    border-radius: 0;
    cursor: pointer;
}

.color-swatch:last-child {
    border-right: none;
}

.color-swatch:hover {
    outline: 2px solid #2563eb;
    outline-offset: -2px;
    z-index: 1;
}

/* While a color is armed for painting its swatch shows a black outline. */
.color-swatch.selected {
    outline: 3px solid #111827;
    outline-offset: -3px;
    z-index: 2;
}

/* Once the color is deselected it keeps a white outline so the bar still shows the
   most recently used color while making clear nothing is currently armed. A thin
   inner dark ring keeps the white outline visible on light swatch hues. */
.color-swatch.last-selected {
    outline: 3px solid #ffffff;
    outline-offset: -3px;
    box-shadow: inset 0 0 0 1px rgba(0, 0, 0, 0.4);
    z-index: 3;
}

#interactionLayer.painting .node-overlay {
    cursor: crosshair;
}

/* While a paint color is armed, the delete/label buttons must not intercept clicks meant
   for the arrows/nodes underneath, so the paint bucket can reach the edge hit paths. */
#interactionLayer.painting .node-delete,
#interactionLayer.painting .edge-delete,
#interactionLayer.painting .edge-label-btn,
#interactionLayer.painting .subgraph-delete {
    pointer-events: none;
}

#noodleSvg {
    position: absolute;
    inset: 0;
    width: 100%;
    height: 100%;
    overflow: visible;
    pointer-events: none;
    display: none;
}

#tempNoodle {
    fill: none;
    stroke: #2563eb;
    stroke-width: 2.5;
    stroke-dasharray: 6 4;
}

.node-overlay {
    position: absolute;
    box-sizing: border-box;
    border-radius: 6px;
    pointer-events: auto;
    cursor: crosshair;
}

.node-overlay:hover,
.cluster-overlay.is-hovered {
    outline: 2px solid #2563eb;
    outline-offset: 2px;
    background: rgba(37, 99, 235, 0.06);
}

.cluster-overlay {
    pointer-events: none;
}

.node-overlay.connect-target {
    outline: 2px solid #16a34a;
    outline-offset: 2px;
    background: rgba(22, 163, 74, 0.12);
}

.node-overlay.subgraph-selected {
    outline: 2px solid #7c3aed;
    outline-offset: 2px;
    background: rgba(124, 58, 237, 0.15);
}

#interactionLayer.subgraph-selecting .node-overlay {
    cursor: pointer;
}

/* While selecting nodes for a subgraph, delete/edge controls must not intercept clicks. */
#interactionLayer.subgraph-selecting .node-delete,
#interactionLayer.subgraph-selecting .edge-delete,
#interactionLayer.subgraph-selecting .edge-label-btn,
#interactionLayer.subgraph-selecting .subgraph-delete {
    pointer-events: none;
}

/* Small "x" badge pinned to a subgraph's top-right corner to remove it (ungrouping its
   nodes rather than deleting them). Faint until hovering the preview, like edge-delete. */
.subgraph-delete {
    position: absolute;
    transform: translate(-50%, -50%);
    width: 20px;
    height: 20px;
    padding: 0;
    box-sizing: border-box;
    line-height: 1;
    font-family: Arial, Helvetica, sans-serif;
    font-size: 13px;
    text-align: center;
    display: flex;
    align-items: center;
    justify-content: center;
    border-radius: 999px;
    border: 1px solid #b91c1c;
    background: #fff1f2;
    color: #991b1b;
    pointer-events: auto;
    opacity: 0.25;
    transition: opacity 0.15s ease;
    z-index: 5;
}

.preview-wrap:hover .subgraph-delete {
    opacity: 0.85;
}

.subgraph-delete:hover {
    opacity: 1;
    transform: translate(-50%, -50%) scale(1.15);
}

.subgraph-select-bar {
    position: absolute;
    top: 8px;
    left: 50%;
    transform: translateX(-50%);
    z-index: 6;
    display: flex;
    align-items: center;
    gap: 10px;
    background: var(--panel-bg);
    border: 1px solid var(--panel-border);
    border-radius: 999px;
    padding: 6px 14px;
    box-shadow: 0 6px 18px var(--panel-shadow);
    pointer-events: auto;
}

.subgraph-select-info {
    font-size: 13px;
    color: var(--text);
    white-space: nowrap;
}

.subgraph-select-create,
.subgraph-select-cancel {
    border-radius: 999px;
    padding: 5px 12px;
    font-size: 13px;
    font-weight: 600;
    cursor: pointer;
}

.subgraph-select-create {
    border: 1px solid var(--accent);
    background: var(--accent);
    color: #ffffff;
}

.subgraph-select-create:disabled {
    opacity: 0.5;
    cursor: not-allowed;
}

.subgraph-select-cancel {
    border: 1px solid var(--btn-border);
    background: var(--btn-bg);
    color: var(--btn-text);
}

.mermaid-editor .node-delete {
    position: absolute;
    top: -10px;
    right: -10px;
    width: 20px;
    height: 20px;
    padding: 0;
    box-sizing: border-box;
    line-height: 1;
    font-family: Arial, Helvetica, sans-serif;
    font-size: 13px;
    text-align: center;
    align-items: center;
    justify-content: center;
    border-radius: 999px;
    border: 1px solid #b91c1c;
    background: #fff1f2;
    color: #991b1b;
    display: none;
    pointer-events: auto;
}

.mermaid-editor .node-overlay:hover .node-delete {
    display: flex;
}

.mermaid-editor .edge-delete {
    position: absolute;
    transform: translate(-50%, -50%);
    width: 20px;
    height: 20px;
    padding: 0;
    box-sizing: border-box;
    line-height: 1;
    font-family: Arial, Helvetica, sans-serif;
    font-size: 13px;
    text-align: center;
    display: flex;
    align-items: center;
    justify-content: center;
    border-radius: 999px;
    border: 1px solid #b91c1c;
    background: #fff1f2;
    color: #991b1b;
    pointer-events: auto;
    opacity: 0.25;
    transition: opacity 0.15s ease;
}

.preview-wrap:hover .edge-delete {
    opacity: 0.85;
}

.edge-delete:hover {
    opacity: 1;
    transform: translate(-50%, -50%) scale(1.15);
}

/* Pulse the arrow whose delete button is being hovered, so it's obvious which
   connection is about to be removed. */
@keyframes edgeDeletePulse {
    0%, 100% { stroke: #ef4444; stroke-width: 4px; }
    50% { stroke: #b91c1c; stroke-width: 6px; }
}

#diagramPreview g.edgePaths > path.edge-highlight {
    stroke: #ef4444 !important;
    animation: edgeDeletePulse 0.7s ease-in-out infinite;
}

.node-label-editor {
    position: absolute;
    z-index: 6;
    min-width: 120px;
    border: 2px solid #2563eb;
    border-radius: 8px;
    padding: 6px 8px;
    font-size: 14px;
    box-shadow: 0 6px 18px rgba(37, 99, 235, 0.25);
    pointer-events: auto;
}

/* Edge label pill: sits just above the edge's delete button at the arrow's midpoint.
   Shows the current label text, or a faint "+" when the edge has none yet. Click to
   open an inline editor; clearing the text removes the label. */
.mermaid-editor .edge-label-btn {
    position: absolute;
    transform: translate(-50%, calc(-100% - 18px));
    max-width: 160px;
    padding: 2px 8px;
    box-sizing: border-box;
    line-height: 1.3;
    font-family: Arial, Helvetica, sans-serif;
    font-size: 12px;
    font-weight: 600;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
    display: flex;
    align-items: center;
    justify-content: center;
    border-radius: 999px;
    border: 1px solid var(--btn-border);
    background: var(--btn-bg);
    color: var(--btn-text);
    pointer-events: auto;
    opacity: 0.3;
    transition: opacity 0.15s ease, background 0.15s ease;
}

.mermaid-editor .edge-label-btn.empty {
    font-weight: 700;
    color: var(--accent);
}

.preview-wrap:hover .edge-label-btn {
    opacity: 0.9;
}

.mermaid-editor .edge-label-btn:hover {
    opacity: 1;
    border-color: var(--accent);
}

.edge-label-editor {
    position: absolute;
    z-index: 6;
    transform: translate(-50%, -50%);
    min-width: 120px;
    border: 2px solid #2563eb;
    border-radius: 999px;
    padding: 6px 10px;
    font-size: 13px;
    text-align: center;
    box-shadow: 0 6px 18px rgba(37, 99, 235, 0.25);
    pointer-events: auto;
}

/* ----- Create-connected-node modal (drag into empty space) ----- */
.node-modal-backdrop {
    position: fixed;
    inset: 0;
    z-index: 1000;
    display: flex;
    align-items: center;
    justify-content: center;
    background: rgba(0, 0, 0, 0.45);
}

.node-modal {
    width: min(960px, calc(100vw - 32px));
    box-sizing: border-box;
    max-height: 86vh;
    overflow-y: auto;
    background: var(--panel-bg);
    color: var(--text);
    border: 1px solid var(--panel-border);
    border-radius: 12px;
    box-shadow: 0 18px 50px rgba(0, 0, 0, 0.35);
    padding: 18px;
}

.node-modal-title {
    font-weight: 700;
    font-size: 16px;
    margin-bottom: 12px;
}

.node-modal-types {
    display: grid;
    grid-template-columns: repeat(auto-fill, minmax(110px, 1fr));
    gap: 8px;
    margin-bottom: 14px;
    max-height: 48vh;
    overflow-y: auto;
}

.dfd-dialog.diagram-settings {
    width: min(1200px, calc(100vw - 32px));
}

.diagram-settings-layout {
    display: grid;
    grid-template-columns: minmax(0, 1fr) minmax(0, 1fr);
    gap: 24px;
}

.diagram-settings-selections,
.diagram-settings-preview {
    min-width: 0;
    max-height: calc(100dvh - 170px);
    overflow-y: auto;
    padding: 2px 8px;
}

.diagram-settings-preview {
    border-left: 1px solid var(--panel-border);
    padding-left: 20px;
}

.diagram-settings-preview h3 {
    margin-top: 0;
    font-size: 14px;
}

.diagram-settings-preview p,
.settings-preview-label {
    font-size: 12px;
    color: var(--muted);
    line-height: 1.5;
}

.settings-preview-image {
    min-height: 260px;
    height: min(42vh, 440px);
    display: flex;
    align-items: center;
    justify-content: center;
    overflow: auto;
    border: 1px solid var(--panel-border);
    border-radius: 8px;
    background: var(--panel-bg);
    padding: 12px;
    box-sizing: border-box;
}

.settings-preview-image svg {
    width: 100%;
    height: 100%;
}

.settings-choice-group {
    border: 0;
    padding: 0;
    margin: 0 0 16px;
    min-width: 0;
}

.settings-choice-group legend {
    font-size: 12px;
    font-weight: 600;
    margin-bottom: 8px;
}

.settings-choices {
    display: flex;
    flex-wrap: wrap;
    gap: 6px;
}

.settings-choices.scrollable {
    max-height: 170px;
    overflow-y: auto;
    padding: 2px;
}

.mermaid-editor .settings-choices button.active {
    border-color: var(--accent);
    background: var(--accent);
    color: #fff;
}

.diagram-settings .settings-grid {
    margin-bottom: 16px;
}

@media (max-width: 700px) {
    .diagram-settings-layout {
        grid-template-columns: minmax(0, 1fr);
    }
    .diagram-settings-selections,
    .diagram-settings-preview {
        max-height: none;
        overflow-y: visible;
    }
    .diagram-settings-preview {
        border-left: 0;
        border-top: 1px solid var(--panel-border);
        padding: 16px 0 0;
    }
}

.settings-grid {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(min(200px, 100%), 1fr));
    gap: 12px 20px;
}

.settings-grid label {
    display: flex;
    flex-direction: column;
    gap: 6px;
    min-width: 0;
    font-size: 12px;
}

.settings-grid input,
.settings-grid select {
    min-width: 0;
    width: 100%;
    box-sizing: border-box;
    border: 1px solid var(--panel-border);
    border-radius: 4px;
    background: var(--panel-bg);
    color: var(--text);
    padding: 8px;
    font: inherit;
}

.options-docs {
    display: flex;
    flex-wrap: wrap;
    gap: 8px 20px;
    margin: 12px 0;
}

.node-modal fieldset label {
    display: flex;
    flex-direction: column;
    gap: 6px;
    margin: 12px 0;
}

.options-docs a {
    color: var(--accent);
    font-size: 12px;
}

.settings-error {
    color: var(--error-text);
}

.node-modal fieldset[hidden] {
    display: none;
}

.node-type-btn {
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    gap: 6px;
    border: 1px solid var(--btn-border);
    background: var(--btn-bg);
    color: var(--btn-text);
    border-radius: 8px;
    padding: 8px 6px;
    font-size: 12px;
    font-weight: 600;
    cursor: pointer;
}

.node-type-btn .node-type-label {
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
    max-width: 100%;
}

.node-type-btn.selected {
    border-color: var(--accent);
    background: var(--accent);
    color: #ffffff;
}

.node-shape-preview {
    display: flex;
    align-items: center;
    justify-content: center;
    width: 80px;
    height: 48px;
    flex-shrink: 0;
    font-size: 11px;
}

.node-shape-thumbnail {
    width: 80px;
    height: 48px;
    object-fit: contain;
}

.node-thumbnail-staging {
    position: fixed;
    left: -100000px;
    top: 0;
    visibility: hidden;
    pointer-events: none;
}

.node-preview-status {
    color: var(--note);
    font-size: 12px;
}

.node-preview-status:empty {
    display: none;
}

/* Shape preview icons shown on each type button, so the option's outline is obvious
   before creating. Built with plain CSS (border/border-radius/clip-path) rather than a
   rendered Mermaid node, so they appear instantly with no async cost. */
.shape-preview {
    display: block;
    width: 40px;
    height: 24px;
    box-sizing: border-box;
    border: 2px solid currentColor;
    background: transparent;
    flex-shrink: 0;
}

.shape-preview.shape-rect {
    border-radius: 2px;
}

.shape-preview.shape-round {
    border-radius: 8px;
}

.shape-preview.shape-stadium {
    border-radius: 999px;
}

.shape-preview.shape-subroutine {
    border-radius: 2px;
    box-shadow: inset 3px 0 0 currentColor, inset -3px 0 0 currentColor;
}

.shape-preview.shape-cylinder {
    border-radius: 50% 50% 4px 4px / 35% 35% 8px 8px;
}

.shape-preview.shape-circle {
    width: 26px;
    border-radius: 50%;
}

.shape-preview.shape-doublecircle {
    width: 26px;
    border-radius: 50%;
    outline: 2px solid currentColor;
    outline-offset: 3px;
}

.shape-preview.shape-diamond {
    border: none;
    width: 32px;
    height: 32px;
    background: currentColor;
    clip-path: polygon(50% 0, 100% 50%, 50% 100%, 0 50%);
}

.shape-preview.shape-hexagon {
    border: none;
    background: currentColor;
    clip-path: polygon(22% 0%, 78% 0%, 100% 50%, 78% 100%, 22% 100%, 0% 50%);
}

.shape-preview.shape-parallelogram {
    border: none;
    background: currentColor;
    clip-path: polygon(22% 0%, 100% 0%, 78% 100%, 0% 100%);
}

.shape-preview.shape-trapezoid {
    border: none;
    background: currentColor;
    clip-path: polygon(28% 0%, 72% 0%, 100% 100%, 0% 100%);
}

.shape-preview.shape-flag {
    border: none;
    background: currentColor;
    clip-path: polygon(0 0, 80% 0, 100% 50%, 80% 100%, 0 100%);
}

.node-type-btn .shape-preview {
    color: var(--btn-text);
}

.node-type-btn.selected .shape-preview {
    color: #ffffff;
}

.node-modal-name {
    width: 100%;
    box-sizing: border-box;
    border: 1px solid var(--panel-border);
    background: var(--field-bg);
    color: var(--field-text);
    border-radius: 8px;
    padding: 8px 10px;
    font-size: 14px;
    margin-bottom: 16px;
}

.node-modal-actions {
    display: flex;
    justify-content: flex-end;
    gap: 8px;
}

.node-modal-cancel,
.node-modal-create {
    border-radius: 8px;
    padding: 7px 14px;
    font-size: 14px;
    font-weight: 600;
    cursor: pointer;
}

.node-modal-cancel {
    border: 1px solid var(--btn-border);
    background: var(--btn-bg);
    color: var(--btn-text);
}

.node-modal-create {
    border: 1px solid var(--accent);
    background: var(--accent);
    color: #ffffff;
}

#errorBox {
    color: var(--error-text);
    background: var(--error-bg);
    border: 1px solid var(--error-border);
    border-radius: 10px;
    padding: 10px;
    margin-bottom: 12px;
    white-space: pre-wrap;
    display: none;
}

.small-note {
    color: var(--note);
    font-size: 13px;
    min-width: 0;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}

/* Below this width, treat the layout as mobile: stack the Mermaid source above the
   preview (instead of squeezing both into side-by-side columns) and let the page
   scroll down through source -> preview -> console, rather than forcing everything
   to fit one screen (see updateViewportLayout's mobile bail-out in mermaidEditor.js,
   which skips setting a fixed viewport-fill height on .grid at this width). */
@media (max-width: 900px) {
    /* The top app header (logo/title + mode select + undo/redo) wraps onto its own
       rows instead of forcing the page wider than the viewport. */
    .header {
        flex-wrap: wrap;
    }
    .header-brand,
    .toolbar {
        min-width: 0;
        flex: 1 1 100%;
    }

    .grid {
        grid-template-columns: 1fr;
        grid-template-rows: none;
    }
    .grid-resizer { display: none; }
    .grid .panel { min-height: 70vh; }
    #previewPanel:not(:fullscreen) .preview-wrap { min-height: 45vh; }
}

@media (max-width: 600px) {
    #previewPanel:not(:fullscreen) .preview-footer {
        height: 72px;
        box-sizing: border-box;
        align-items: flex-end;
        padding-bottom: 6px;
    }

    #previewPanel:not(:fullscreen) .color-caption {
        position: absolute;
        top: 6px;
        left: 8px;
    }

    #previewPanel:not(:fullscreen) #colorBar {
        bottom: 46px;
        left: 128px;
        right: 12px;
    }

    .header,
    .toolbar {
        gap: 8px;
    }

    .toolbar .mode-select {
        border-left: 0;
        margin-left: 0;
        padding-left: 0;
    }

    .mode-select select {
        min-width: 0;
        max-width: 100%;
    }

    .brand-wordmark {
        flex-wrap: wrap;
        gap: 10px;
    }

    .panel-header {
        flex-wrap: wrap;
        gap: 6px 10px;
        padding: 6px 8px;
    }

    .panel-header .preview-actions {
        overflow: visible;
        gap: 6px;
    }

    .display-options-menu {
        top: calc(100% + 1px);
        left: auto;
        right: 8px;
        width: 200px;
    }

    .display-options {
        position: static;
    }

    .mode-badge {
        margin-left: 0;
    }
}
