/* Enhanced Mermaid Diagram Styling with MkDocs Material Integration */

/* Fix for newer Mermaid versions that wrap text in <p> or <div> tags inside foreignObject */
.mermaid svg .nodeLabel p,
.mermaid svg .nodeLabel div,
.mermaid svg .edgeLabel p,
.mermaid svg .edgeLabel div {
    margin: 0 !important;
    padding: 0 !important;
    line-height: 1.3 !important;
}

/* Font size for node labels (HTML elements inside foreignObject) */
.mermaid svg .nodeLabel,
.mermaid svg .nodeLabel p,
.mermaid svg .nodeLabel div,
.mermaid svg .nodeLabel span {
    font-family: 'SF Pro Display', -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif !important;
    font-size: 14px !important;
    font-weight: 500 !important;
    text-rendering: optimizeLegibility !important;
    -webkit-font-smoothing: antialiased !important;
    -moz-osx-font-smoothing: grayscale !important;
}

/* Bold the first line (top text) in node labels */
.mermaid svg .nodeLabel div:first-child {
    font-weight: 600 !important;
}

/* Edge labels styling */
.mermaid svg .edgeLabel,
.mermaid svg .edgeLabel p,
.mermaid svg .edgeLabel span {
    font-family: 'SF Pro Display', -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif !important;
    font-size: 14px !important;
    font-weight: 500 !important;
}

/* Base font styling for SVG text elements (fallback for older Mermaid) */
.mermaid svg text,
.mermaid svg tspan {
    font-family: 'SF Pro Display', -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif !important;
    font-size: 14px !important;
    font-weight: 500 !important;
    text-rendering: optimizeLegibility !important;
    -webkit-font-smoothing: antialiased !important;
    -moz-osx-font-smoothing: grayscale !important;
}

/* Cluster/subgraph labels */
.mermaid svg .cluster-label text,
.mermaid svg .cluster-label .nodeLabel {
    font-size: 15px !important;
    font-weight: 600 !important;
}

/* Auto-detect and style Mermaid diagrams followed by diagram controls */
.mermaid + .diagram-controls {
    margin-top: -0.5rem; /* Reduce gap between diagram and controls */
}

/* Style the mermaid diagram container when followed by controls */
.mermaid:has(+ .diagram-controls),
.mermaid-container .mermaid {
    position: relative;
    background: var(--md-code-bg-color);
    border-radius: 0.2rem;
    padding: 1rem;
    margin: 1rem 0;
    box-shadow: 0 0.1rem 0.5rem rgba(0,0,0,0.1);
}

/* Legacy support for existing mermaid-container class */
.mermaid-container {
    position: relative;
    background: var(--md-code-bg-color);
    border-radius: 0.2rem;
    padding: 1rem;
    margin: 1rem 0;
    box-shadow: 0 0.1rem 0.5rem rgba(0,0,0,0.1);
}

.diagram-controls {
    display: flex;
    justify-content: center;
    margin-top: 0.5rem;
}

.enlarge-diagram {
    font-size: 0.75rem;
    padding: 0.4rem 0.8rem;
    border-radius: 0.2rem;
    border: none;
    background: var(--md-primary-fg-color);
    color: var(--md-primary-bg-color);
    cursor: pointer;
    transition: all 0.2s;
    display: flex;
    align-items: center;
    gap: 0.3rem;
}

.enlarge-diagram:hover {
    transform: translateY(-1px);
    box-shadow: 0 0.2rem 0.5rem rgba(0,0,0,0.2);
}

.diagram-modal {
    display: none;
    position: fixed;
    z-index: 9999;
    left: 0;
    top: 0;
    width: 100%;
    height: 100%;
    background-color: rgba(0,0,0,0.8);
    backdrop-filter: blur(4px);
}

.diagram-modal-content {
    background-color: var(--md-default-bg-color);
    margin: 1% auto;
    padding: 0.5rem;
    border-radius: 0.5rem;
    width: calc(100vw - 40px);
    height: calc(100vh - 40px);
    max-width: calc(100vw - 40px);
    max-height: calc(100vh - 40px);
    position: relative;
    overflow: hidden;
    box-shadow: 0 1rem 3rem rgba(0,0,0,0.3);
    display: flex;
    flex-direction: column;
}

.diagram-modal-close {
    color: var(--md-default-fg-color--light);
    font-size: 1.5rem;
    font-weight: bold;
    cursor: pointer;
    position: absolute;
    top: 0.5rem;
    right: 0.5rem;
    z-index: 10000;
    background: rgba(0,0,0,0.7);
    border-radius: 50%;
    width: 2rem;
    height: 2rem;
    display: flex;
    align-items: center;
    justify-content: center;
    backdrop-filter: blur(4px);
}

.diagram-modal-close:hover {
    color: var(--md-default-fg-color);
    background: rgba(0,0,0,0.9);
}

#enlarged-diagram {
    flex: 1;
    display: flex;
    align-items: center;
    justify-content: center;
    overflow: hidden;
    padding: 0.25rem;
    min-height: 0; /* Important for flex shrinking */
    width: 100%;
    height: 100%;
}

#enlarged-diagram .mermaid,
#enlarged-diagram .mermaid-enlarged {
    background: var(--md-code-bg-color);
    border-radius: 0.5rem;
    padding: 0.5rem;
    width: 100%;
    height: 100%;
    overflow: hidden;
    display: flex;
    align-items: center;
    justify-content: center;
    position: relative;
}

#enlarged-diagram svg {
    max-width: none !important;
    max-height: none !important;
    width: auto !important;
    height: auto !important;
    flex-shrink: 0;
}

/* Enhanced styling for enlarged diagrams */
#enlarged-diagram .mermaid-enlarged svg text,
#enlarged-diagram .mermaid-enlarged svg tspan {
    font-family: 'SF Pro Display', -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif !important;
    font-weight: 500 !important;
    text-rendering: optimizeLegibility !important;
    -webkit-font-smoothing: antialiased !important;
    -moz-osx-font-smoothing: grayscale !important;
}

/* Ensure nodes and edges maintain styling in enlarged view */
#enlarged-diagram .mermaid-enlarged svg .node rect,
#enlarged-diagram .mermaid-enlarged svg .node circle,
#enlarged-diagram .mermaid-enlarged svg .node polygon {
    stroke-width: 2px !important;
}

/* Enhanced edge styling for enlarged view */
#enlarged-diagram .mermaid-enlarged svg .edgePath path {
    stroke-width: 2px !important;
}

/* Better contrast for decision nodes in enlarged view */
#enlarged-diagram .mermaid-enlarged svg .node.decision polygon {
    stroke-width: 3px !important;
}

/* Mobile responsive styles */
@media (max-width: 768px) {
    .diagram-modal-content {
        margin: 5% auto;
        padding: 1rem;
        max-width: 98vw;
        max-height: 85vh;
    }
    
    .enlarge-diagram {
        font-size: 0.7rem;
        padding: 0.3rem 0.6rem;
    }
}

/* Dark theme adjustments */
[data-md-color-scheme="slate"] .mermaid-container {
    box-shadow: 0 0.1rem 0.5rem rgba(255,255,255,0.1);
}

[data-md-color-scheme="slate"] .diagram-modal {
    background-color: rgba(0,0,0,0.9);
}
