/* Базовый контейнер Mermaid */
.mermaid {
    text-align: center !important;
    margin: 1rem auto !important;
    padding: 1rem !important;
    overflow-x: auto !important;
    overflow-y: visible !important;
    -webkit-overflow-scrolling: touch !important;
    position: relative !important;
    background: var(--scheme-bg, #ffffff) !important;
    border-radius: var(--scheme-border-radius, 12px) !important;
    box-shadow: var(--scheme-shadow-md, 0 14px 40px rgba(15, 23, 42, 0.16)) !important;
    transition: box-shadow 0.3s ease, transform 0.2s ease !important;
}

.mermaid:hover {
    box-shadow: var(--scheme-shadow-lg, 0 18px 48px rgba(15, 23, 42, 0.22)) !important;
    transform: translateY(-2px) !important;
}

/* Кнопка полноэкранного режима (иконка в блоке схемы) */
.mermaid > .mermaid-fullscreen-btn {
    position: absolute !important;
    top: 10px !important;
    right: 10px !important;
    background: rgba(0, 0, 0, 0.6) !important;
    border: none !important;
    color: #ffffff !important;
    padding: 8px !important;
    border-radius: 6px !important;
    cursor: pointer !important;
    transition: opacity 0.2s ease, transform 0.2s ease, background-color 0.2s ease !important;
    font-size: 14px !important;
    width: 32px !important;
    height: 32px !important;
    display: flex !important;
    align-items: center !important;
    justify-content: center !important;
    backdrop-filter: blur(10px) !important;
    z-index: 10 !important;
    opacity: 0 !important;
}

.mermaid:hover > .mermaid-fullscreen-btn {
    opacity: 1 !important;
}

.mermaid > .mermaid-fullscreen-btn:hover {
    background: rgba(0, 0, 0, 0.8) !important;
    transform: scale(1.05) !important;
}

.mermaid > .mermaid-fullscreen-btn:active {
    transform: scale(0.95) !important;
}

.mermaid svg {
    display: block !important;
    margin: 0 auto !important;
    height: auto !important;
    overflow: visible !important;
    max-width: 100% !important;
}

.mermaid .node rect,
.mermaid .node polygon {
    fill: var(--scheme-node-fill, var(--scheme-bg, #ffffff)) !important;
    stroke: var(--scheme-branch-color, #4f46e5) !important;
    stroke-width: var(--scheme-arrow-width, 2px) !important;
    rx: var(--scheme-border-radius, 12px) !important;
    ry: var(--scheme-border-radius, 12px) !important;
    filter: drop-shadow(var(--scheme-shadow-md, 0 10px 25px rgba(15, 23, 42, 0.18))) !important;
    transition: fill var(--scheme-transition-duration, 0.2s) ease-in-out,
                stroke var(--scheme-transition-duration, 0.2s) ease-in-out,
                transform 0.2s ease-in-out !important;
}

.mermaid .node:hover rect,
.mermaid .node:hover polygon {
    fill: var(--scheme-node-hover-fill, var(--hover-bg, #eef2ff)) !important;
    stroke: var(--scheme-node-hover-border, var(--primary-color, #4338ca)) !important;
    transform: translateY(-2px) !important;
    filter: drop-shadow(var(--scheme-shadow-lg, 0 18px 40px rgba(15, 23, 42, 0.24))) !important;
}

/* Стили для текста в схемах */
.mermaid text {
    font-family: 'Inter', Arial, sans-serif !important;
    font-size: 14px !important;
    font-weight: 500 !important;
    fill: var(--scheme-text-color, #1f2937) !important;
    white-space: pre-line !important;
    transition: fill 0.3s ease !important;
}

.mermaid .node text {
    dominant-baseline: middle !important;
    text-anchor: middle !important;
}

.mermaid .node .label {
    font-size: 14px !important;
    text-align: center !important;
    padding: 0 !important;
    margin: 0 !important;
    white-space: pre-line !important;
    overflow: visible !important;
    word-break: break-word !important;
}

.mermaid .flowchart-link {
    stroke: var(--scheme-arrow-color, #6366f1) !important;
    stroke-width: var(--scheme-arrow-width, 2px) !important;
    transition: stroke var(--scheme-transition-duration, 0.2s) ease,
                stroke-width var(--scheme-transition-duration, 0.2s) ease !important;
    filter: drop-shadow(var(--scheme-shadow-sm, 0 4px 10px rgba(15, 23, 42, 0.18))) !important;
}

.mermaid .arrowheadPath {
    fill: var(--scheme-arrow-color, #6366f1) !important;
    stroke: var(--scheme-arrow-color, #6366f1) !important;
    filter: drop-shadow(var(--scheme-shadow-sm, 0 4px 10px rgba(15, 23, 42, 0.18))) !important;
}

.mermaid .edgeLabel rect {
    fill: var(--scheme-label-bg, rgba(255, 255, 255, 0.92)) !important;
    stroke: var(--scheme-label-border-color, rgba(99, 102, 241, 0.35)) !important;
    stroke-width: 1px !important;
    filter: drop-shadow(var(--scheme-shadow-sm, 0 4px 10px rgba(15, 23, 42, 0.12))) !important;
    transition: fill var(--scheme-transition-duration, 0.2s) ease,
                stroke var(--scheme-transition-duration, 0.2s) ease,
                filter var(--scheme-transition-duration, 0.2s) ease !important;
}

.mermaid .edgeLabel text {
    font-size: 18px !important;
    font-weight: 600 !important;
}

.mermaid .edgePath path,
.mermaid .edgePaths path,
.mermaid .edgePaths .edgePath path,
.mermaid .edgePaths .path,
.mermaid .edgePaths .line {
    stroke: var(--scheme-arrow-color, #6366f1) !important;
    stroke-width: var(--scheme-arrow-width, 2px) !important;
    fill: none !important;
}

.mermaid marker path,
.mermaid marker polygon {
    fill: var(--scheme-arrow-color, #6366f1) !important;
    stroke: var(--scheme-arrow-color, #6366f1) !important;
}

.mermaid .node {
    transform-origin: center !important;
    padding: 0 !important;
    margin: 0 !important;
}

/* Скрываем исходный код диаграммы */
.mermaid[data-processed="true"] pre,
.mermaid[data-processed="true"] code {
    display: none !important;
}

/* Полноэкранный режим для схем */
.mermaid-fullscreen-overlay {
    position: fixed !important;
    top: 0 !important;
    left: 0 !important;
    width: 100vw !important;
    height: 100vh !important;
    background: rgba(0, 0, 0, 0.9) !important;
    z-index: 10000 !important;
    display: none !important;
    justify-content: center !important;
    align-items: center !important;
    backdrop-filter: blur(5px) !important;
}

.mermaid-fullscreen-overlay.active {
    display: flex !important;
}

.mermaid-fullscreen-container {
    position: relative !important;
    width: 90vw !important;
    height: 90vh !important;
    background: var(--scheme-bg, #ffffff) !important;
    border-radius: var(--scheme-border-radius, 12px) !important;
    box-shadow: var(--scheme-shadow-lg, 0 18px 48px rgba(15, 23, 42, 0.22)) !important;
    overflow: hidden !important;
    display: flex !important;
    flex-direction: column !important;
    border: 1px solid var(--scheme-border, rgba(99, 102, 241, 0.25)) !important;
}

.mermaid-fullscreen-header {
    display: flex !important;
    justify-content: space-between !important;
    align-items: center !important;
    padding: 1rem 1.5rem !important;
    background: var(--primary-color, #4338ca) !important;
    color: white !important;
    border-bottom: 1px solid var(--border-color, rgba(99, 102, 241, 0.25)) !important;
}

.mermaid-fullscreen-title {
    font-size: 1.2rem !important;
    font-weight: 600 !important;
    margin: 0 !important;
}

.mermaid-fullscreen-controls {
    display: flex !important;
    gap: 0.5rem !important;
    align-items: center !important;
}

.mermaid-fullscreen-header .mermaid-fullscreen-btn,
.mermaid-fullscreen-controls .mermaid-fullscreen-btn {
    background: rgba(255, 255, 255, 0.18);
    border: none;
    color: #ffffff;
    padding: 0.5rem;
    border-radius: 6px;
    cursor: pointer;
    transition: background-color 0.2s ease, transform 0.2s ease;
    font-size: 1rem;
    display: flex;
    align-items: center;
    justify-content: center;
    min-width: 40px;
    height: 40px;
}

.mermaid-fullscreen-header .mermaid-fullscreen-btn:hover,
.mermaid-fullscreen-controls .mermaid-fullscreen-btn:hover {
    background: rgba(255, 255, 255, 0.28);
    transform: scale(1.05);
}

.mermaid-fullscreen-header .mermaid-fullscreen-btn:active,
.mermaid-fullscreen-controls .mermaid-fullscreen-btn:active {
    transform: scale(0.95);
}

.mermaid-fullscreen-close {
    background: rgba(220, 53, 69, 0.8) !important;
}

.mermaid-fullscreen-close:hover {
    background: rgba(220, 53, 69, 1) !important;
}

.mermaid-fullscreen-content {
    flex: 1 !important;
    overflow: hidden !important;
    position: relative !important;
    background: var(--scheme-bg, #ffffff) !important;
    display: flex !important;
    align-items: center !important;
    justify-content: center !important;
}

.mermaid-fullscreen-svg-container {
    width: 100% !important;
    height: 100% !important;
    overflow: auto !important;
    position: relative !important;
    cursor: grab !important;
    display: flex !important;
    align-items: center !important;
    justify-content: center !important;
}

.mermaid-fullscreen-svg-container:active {
    cursor: grabbing !important;
}

.mermaid-fullscreen-svg-container svg {
    display: block !important;
    margin: 0 auto !important;
    transform-origin: center !important;
    transition: transform 0.1s ease !important;
    max-width: 100% !important;
    max-height: 100% !important;
}

/* Панель управления масштабом */
.mermaid-zoom-controls {
    position: absolute !important;
    bottom: 20px !important;
    right: 20px !important;
    display: flex !important;
    flex-direction: column !important;
    gap: 0.5rem !important;
    z-index: 10001 !important;
}

.mermaid-zoom-btn {
    background: rgba(0, 0, 0, 0.7) !important;
    border: none !important;
    color: white !important;
    padding: 0.75rem !important;
    border-radius: 50% !important;
    cursor: pointer !important;
    transition: background-color 0.2s ease, transform 0.2s ease !important;
    font-size: 1.2rem !important;
    width: 50px !important;
    height: 50px !important;
    display: flex !important;
    align-items: center !important;
    justify-content: center !important;
    backdrop-filter: blur(10px) !important;
}

.mermaid-zoom-btn:hover {
    background: rgba(0, 0, 0, 0.8) !important;
    transform: scale(1.1) !important;
}

.mermaid-zoom-btn:active {
    transform: scale(0.95) !important;
}

.mermaid-zoom-info {
    background: rgba(0, 0, 0, 0.7) !important;
    color: white !important;
    padding: 0.5rem 1rem !important;
    border-radius: 20px !important;
    font-size: 0.9rem !important;
    text-align: center !important;
    backdrop-filter: blur(10px) !important;
    min-width: 80px !important;
}

/* Анимация появления */
@keyframes mermaid-fullscreen-fade-in {
    from {
        opacity: 0;
        transform: scale(0.9);
    }
    to {
        opacity: 1;
        transform: scale(1);
    }
}

.mermaid-fullscreen-overlay.active .mermaid-fullscreen-container {
    animation: mermaid-fullscreen-fade-in 0.3s ease-out;
}

@media (prefers-reduced-motion: reduce) {
    .mermaid,
    .mermaid > .mermaid-fullscreen-btn,
    .mermaid .node rect,
    .mermaid .node polygon,
    .mermaid text,
    .mermaid .flowchart-link,
    .mermaid .edgeLabel,
    .mermaid-fullscreen-header .mermaid-fullscreen-btn,
    .mermaid-fullscreen-controls .mermaid-fullscreen-btn,
    .mermaid-zoom-btn,
    .mermaid-fullscreen-svg-container svg {
        transition: none !important;
        animation: none !important;
    }

    .mermaid:hover,
    .mermaid .node:hover rect,
    .mermaid .node:hover polygon,
    .mermaid > .mermaid-fullscreen-btn:hover,
    .mermaid-fullscreen-header .mermaid-fullscreen-btn:hover,
    .mermaid-fullscreen-controls .mermaid-fullscreen-btn:hover,
    .mermaid-zoom-btn:hover,
    .mermaid-zoom-btn:active {
        transform: none !important;
    }
}

/* Адаптивность */
@media (max-width: 768px) {
    .mermaid-fullscreen-container {
        width: 95vw !important;
        height: 95vh !important;
    }
    
    .mermaid-fullscreen-header {
        padding: 0.75rem 1rem !important;
    }
    
    .mermaid-fullscreen-title {
        font-size: 1rem !important;
    }
    
    .mermaid-fullscreen-header .mermaid-fullscreen-btn,
    .mermaid-fullscreen-controls .mermaid-fullscreen-btn {
        padding: 0.4rem !important;
        min-width: 35px !important;
        height: 35px !important;
        font-size: 0.9rem !important;
    }
    
    .mermaid-zoom-controls {
        bottom: 15px !important;
        right: 15px !important;
    }
    
    .mermaid-zoom-btn {
        width: 45px !important;
        height: 45px !important;
        font-size: 1rem !important;
    }
    
    .mermaid > .mermaid-fullscreen-btn {
        opacity: 1 !important;
        width: 28px !important;
        height: 28px !important;
        font-size: 12px !important;
    }
}
