/*
    Diagrams : figures built from boxes and arrows, used everywhere on the site so they all look alike
    - Docs : <figure class="dd"> (ex. FrameworkDocs/Diagrams)
    - Product pages : <figure class="dd compact"> in a Section's image column (ex. MTConnectNet/AgentDiagram), a smaller size of the same parts
    Layouts respond to the diagram's own width (container queries), so rows stack vertically whenever the diagram is narrow
*/
.dd {
    margin: 8px 0 28px;
    padding: 20px;
    border: 1px solid var(--border);
    border-radius: var(--radiusLarge);
    background: var(--surfaceMuted);
    font-size: 14px;
    line-height: 20px;
    text-align: left;
}

    .dd code {
        padding: 2px 6px;
        border-radius: var(--radiusSmall);
        background: #eef2f6;
        font-family: Consolas, "Courier New", monospace;
        font-size: 14px;
        color: #1b4b7e;
    }

    /* Long names and URLs (ex. Client.Services.Tables.ReadMultiple<Part>(...)) break instead of overflowing */
    .dd code,
    .dd-box b {
        overflow-wrap: anywhere;
    }

    .dd figcaption {
        margin-top: 14px;
        font-size: 13px;
        line-height: 18px;
        color: var(--textSubtle);
        text-align: center;
    }

/* Box : a step or part of the system */
.dd-box {
    padding: 10px 14px;
    border: 1px solid var(--border);
    border-radius: var(--radius);
    background: var(--surface);
    color: var(--textColor);
}

    .dd-box b {
        display: block;
        color: var(--textStrong);
        font-weight: 600;
    }

    .dd-box .icon {
        margin-right: 6px;
        color: var(--accent);
    }

    .dd-box small {
        display: block;
        margin-top: 2px;
        font-size: 13px;
        line-height: 18px;
        color: var(--textMuted);
    }

        /* A name in the description (ex. a menu path like Config > Variables) stays inline */
        .dd-box small b {
            display: inline;
            font-weight: 500;
        }

    .dd-box.accent {
        border-color: var(--accent);
        background: var(--accentSoft);
    }

    .dd-box.ok {
        border-color: #9bd8b0;
        background: #eefaf2;
    }

    .dd-box.stop {
        border-color: #f0b4b4;
        background: #fdf0f0;
    }

    /* Warn : allowed, but needs attention (ex. Audit mode, a retired version) */
    .dd-box.warn {
        border-color: #e8c27a;
        background: #fdf7ea;
    }

    .dd-box.dashed {
        border-style: dashed;
        background: transparent;
    }

/* Group : a labelled region, ex. the Instance */
.dd-group {
    padding: 16px;
    border: 2px solid var(--accent);
    border-radius: var(--radiusLarge);
    background: var(--surface);
}

.dd-group-label {
    display: flex;
    align-items: center;
    gap: 8px;
    margin-bottom: 12px;
    font-size: 13px;
    font-weight: 600;
    text-transform: uppercase;
    letter-spacing: 0.04em;
    color: var(--accentDark);
}

/* Down Arrow : a connector between stacked boxes, with an optional label */
.dd-down {
    position: relative;
    box-sizing: border-box;
    min-height: 30px;
    margin-left: 28px;
    padding: 7px 0 9px 14px;
    border-left: 2px solid var(--borderStrong);
}

    .dd-down::after {
        content: "";
        position: absolute;
        bottom: -1px;
        left: -6px;
        border-top: 7px solid var(--borderStrong);
        border-left: 5px solid transparent;
        border-right: 5px solid transparent;
    }

    /* The label sits in the flow beside the line, so it wraps on narrow screens */
    .dd-down span {
        display: block;
        font-size: 12px;
        line-height: 16px;
        color: var(--textSubtle);
    }

    .dd-down.ok {
        border-left-color: var(--success);
    }

        .dd-down.ok::after {
            border-top-color: var(--success);
        }

        .dd-down.ok span {
            color: #157a3a;
        }

/* Step : a box with a note beside it (ex. what happens when the step refuses the Request) */
.dd-step {
    display: grid;
    grid-template-columns: minmax(0, 1fr) minmax(0, 0.8fr);
    gap: 12px;
    align-items: center;
}

.dd-note {
    padding: 6px 10px;
    border-left: 3px solid var(--borderStrong);
    font-size: 13px;
    line-height: 18px;
    color: var(--textMuted);
}

    .dd-note.stop {
        border-left-color: var(--danger);
    }

    .dd-note.ok {
        border-left-color: var(--success);
    }

    .dd-note code {
        font-size: 12px;
    }

/* Row : boxes side by side with arrows between them */
.dd-row {
    display: flex;
    align-items: stretch;
    gap: 0;
}

    .dd-row > .dd-box,
    .dd-row > .dd-stack,
    .dd-row > .dd-group {
        flex: 1 1 0;
        min-width: 0;
    }

    /* A stack in a row sits in the middle of the row's height instead of stretching its boxes */
    .dd-row > .dd-stack {
        align-content: center;
    }

    /* A box shorter than the columns beside it sits in the middle instead of stretching (ex. one box beside a stack of three) */
    .dd-row > .middle {
        align-self: center;
    }

.dd-right {
    position: relative;
    box-sizing: border-box;
    flex: 0 0 auto;
    min-width: var(--ddArrowWidth, 44px);
    align-self: center;
    height: 2px;
    margin: 0 4px;
    background: var(--borderStrong);
}

    .dd-right::after {
        content: "";
        position: absolute;
        right: -1px;
        top: -5px;
        border-left: 7px solid var(--borderStrong);
        border-top: 6px solid transparent;
        border-bottom: 6px solid transparent;
    }

    /* Both : a request and its response, or a two-way connection */
    .dd-right.both::before {
        content: "";
        position: absolute;
        left: -1px;
        top: -5px;
        border-right: 7px solid var(--borderStrong);
        border-top: 6px solid transparent;
        border-bottom: 6px solid transparent;
    }

    /* The label is in the flow for its width only, so the arrow is as wide as its label and the label never covers the boxes beside it */
    .dd-right span {
        position: relative;
        top: -20px;
        display: block;
        height: 0;
        padding: 0 8px;
        font-size: 11px;
        line-height: 14px;
        white-space: nowrap;
        text-align: center;
        color: var(--textSubtle);
    }

/* Grid : columns of boxes (ex. one Package and its Configurations) */
.dd-columns {
    display: grid;
    grid-template-columns: minmax(0, 0.9fr) 44px minmax(0, 1.4fr);
    align-items: center;
}

.dd-stack {
    display: grid;
    gap: 10px;
}

.dd-chips {
    display: flex;
    flex-wrap: wrap;
    gap: 6px;
    margin-top: 8px;
}

.dd-chip {
    padding: 2px 8px;
    border: 1px solid var(--border);
    border-radius: var(--radiusPill);
    background: var(--surfaceMuted);
    font-family: Consolas, "Courier New", monospace;
    font-size: 12px;
    line-height: 18px;
    color: var(--textMuted);
}

    .dd-chip.accent {
        border-color: var(--accent);
        background: var(--accentSoft);
        color: var(--accentDark);
    }

/* Grid : a map of boxes in 2 or 3 columns (ex. what's inside an Instance) */
.dd-grid {
    display: grid;
    grid-template-columns: repeat(2, minmax(0, 1fr));
    gap: 10px;
}

    .dd-grid.three {
        grid-template-columns: repeat(3, minmax(0, 1fr));
    }

    .dd-grid .wide {
        grid-column: 1 / -1;
    }

/* Label : a small heading inside a diagram */
.dd-label {
    margin: 4px 0 8px;
    font-size: 12px;
    font-weight: 600;
    text-transform: uppercase;
    letter-spacing: 0.04em;
    color: var(--textSubtle);
}

/* Sequence : numbered messages between the parts of a system, in order (ex. an Agent module and the machine it reads) */
.dd-seq {
    display: grid;
    gap: 6px;
    margin: 0;
    padding: 0;
    list-style: none;
    counter-reset: dd-seq;
}

    .dd-seq > li {
        display: grid;
        grid-template-columns: 24px minmax(0, 0.9fr) minmax(0, 1.4fr);
        gap: 12px;
        align-items: center;
        padding: 8px 12px;
        border: 1px solid var(--border);
        border-radius: var(--radius);
        background: var(--surface);
        counter-increment: dd-seq;
    }

        .dd-seq > li::before {
            content: counter(dd-seq);
            display: flex;
            align-items: center;
            justify-content: center;
            width: 24px;
            height: 24px;
            border-radius: 50%;
            background: var(--accentSoft);
            font-size: 12px;
            font-weight: 600;
            color: var(--accentDark);
        }

        /* A reply (ex. a response to a request) */
        .dd-seq > li.reply {
            border-style: dashed;
            background: transparent;
        }

        .dd-seq > li.ok {
            border-color: #9bd8b0;
        }

        .dd-seq > li.stop {
            border-color: #f0b4b4;
        }

    /* From and to : "Module → Agent" */
    .dd-seq-route {
        display: flex;
        flex-wrap: wrap;
        align-items: center;
        gap: 4px 6px;
        font-size: 13px;
        font-weight: 600;
        color: var(--textStrong);
    }

        .dd-seq-route .fa-arrow-right {
            font-size: 11px;
            color: var(--textFaint);
        }

    .dd-seq-text {
        font-size: 13px;
        line-height: 18px;
        color: var(--textMuted);
    }

    /* A label between messages (ex. "Every 500 ms" for a loop) */
    .dd-seq > .dd-seq-label {
        display: block;
        padding: 6px 0 0 36px;
        border: none;
        background: none;
        font-size: 12px;
        font-weight: 600;
        text-transform: uppercase;
        letter-spacing: 0.04em;
        color: var(--textSubtle);
        counter-increment: none;
    }

        .dd-seq > .dd-seq-label::before {
            content: none;
        }


/* Narrow Diagrams : rows stack and their side arrows point down. Rows of 5 boxes (.long) stack sooner, and rows of 3 (.short) later */
.dd {
    container-type: inline-size;
}

@container (max-width: 920px) {

    .dd-row.long {
        flex-direction: column;
    }

        .dd-row.long > .dd-right {
            flex: 0 0 26px;
            width: 2px;
            min-width: 0;
            height: 26px;
            margin: 2px 0 2px 28px;
            align-self: flex-start;
        }

            .dd-row.long > .dd-right::after,
            .dd-row.long > .dd-right.both::before {
                right: auto;
                left: -5px;
                border-left: 6px solid transparent;
                border-right: 6px solid transparent;
            }

            .dd-row.long > .dd-right::after {
                top: auto;
                bottom: -1px;
                border-top: 7px solid var(--borderStrong);
                border-bottom: 0;
            }

            .dd-row.long > .dd-right.both::before {
                top: -1px;
                border-top: 0;
                border-bottom: 7px solid var(--borderStrong);
            }

            .dd-row.long > .dd-right span {
                position: absolute;
                top: 50%;
                left: 14px;
                height: auto;
                padding: 0;
                text-align: left;
                transform: translateY(-50%);
            }
}

@container (max-width: 640px) {

    .dd-grid.three {
        grid-template-columns: minmax(0, 1fr);
    }

    .dd-row:not(.short),
    .dd-columns {
        display: flex;
        flex-direction: column;
        align-items: stretch;
    }

        .dd-row:not(.short) > .middle {
            align-self: stretch;
        }

        .dd-row:not(.short) > .dd-right,
        .dd-columns > .dd-right {
            flex: 0 0 26px;
            width: 2px;
            min-width: 0;
            height: 26px;
            margin: 2px 0 2px 28px;
            align-self: flex-start;
        }

            .dd-row:not(.short) > .dd-right::after,
            .dd-row:not(.short) > .dd-right.both::before,
            .dd-columns > .dd-right::after,
            .dd-columns > .dd-right.both::before {
                right: auto;
                left: -5px;
                border-left: 6px solid transparent;
                border-right: 6px solid transparent;
            }

            .dd-row:not(.short) > .dd-right::after,
            .dd-columns > .dd-right::after {
                top: auto;
                bottom: -1px;
                border-top: 7px solid var(--borderStrong);
                border-bottom: 0;
            }

            .dd-row:not(.short) > .dd-right.both::before,
            .dd-columns > .dd-right.both::before {
                top: -1px;
                border-top: 0;
                border-bottom: 7px solid var(--borderStrong);
            }

            .dd-row:not(.short) > .dd-right span,
            .dd-columns > .dd-right span {
                position: absolute;
                top: 50%;
                left: 14px;
                height: auto;
                padding: 0;
                text-align: left;
                transform: translateY(-50%);
            }
}

@container (max-width: 520px) {

    .dd-row.short {
        flex-direction: column;
    }

        .dd-row.short > .middle {
            align-self: stretch;
        }

        .dd-row.short > .dd-right {
            flex: 0 0 26px;
            width: 2px;
            min-width: 0;
            height: 26px;
            margin: 2px 0 2px 28px;
            align-self: flex-start;
        }

            .dd-row.short > .dd-right::after,
            .dd-row.short > .dd-right.both::before {
                right: auto;
                left: -5px;
                border-left: 6px solid transparent;
                border-right: 6px solid transparent;
            }

            .dd-row.short > .dd-right::after {
                top: auto;
                bottom: -1px;
                border-top: 7px solid var(--borderStrong);
                border-bottom: 0;
            }

            .dd-row.short > .dd-right.both::before {
                top: -1px;
                border-top: 0;
                border-bottom: 7px solid var(--borderStrong);
            }

            .dd-row.short > .dd-right span {
                position: absolute;
                top: 50%;
                left: 14px;
                height: auto;
                padding: 0;
                text-align: left;
                transform: translateY(-50%);
            }
}

@container (max-width: 460px) {

    .dd-seq > li {
        grid-template-columns: 24px minmax(0, 1fr);
    }

        .dd-seq > li .dd-seq-text {
            grid-column: 2;
        }

    .dd-grid {
        grid-template-columns: minmax(0, 1fr);
    }

    .dd-step {
        grid-template-columns: minmax(0, 1fr);
        gap: 6px;
    }
}

@media only screen and (max-width: 700px) {

    .dd {
        padding: 14px;
    }
}


/* Compact : the same parts, smaller, for a Section's image column on a product page */
.dd.compact {
    --ddArrowWidth: 28px;
    padding: 16px;
    font-size: 13px;
    line-height: 18px;
}

    .dd.compact code {
        padding: 1px 5px;
        font-size: 12px;
    }

    .dd.compact .dd-box {
        padding: 8px 10px;
    }

        .dd.compact .dd-box small {
            margin-top: 1px;
            font-size: 12px;
            line-height: 16px;
        }

    .dd.compact .dd-group {
        padding: 12px;
    }

    .dd.compact .dd-group-label {
        margin-bottom: 8px;
        font-size: 12px;
    }

    .dd.compact .dd-stack,
    .dd.compact .dd-grid {
        gap: 6px;
    }

    .dd.compact .dd-label {
        margin: 0 0 2px;
        font-size: 11px;
        line-height: 16px;
    }

    .dd.compact .dd-chips {
        gap: 4px;
        margin-top: 6px;
    }

    .dd.compact .dd-chip {
        padding: 1px 7px;
        font-size: 11px;
    }

    .dd.compact .dd-down {
        min-height: 24px;
        padding: 4px 0 6px 12px;
    }

    .dd.compact figcaption {
        margin-top: 12px;
        font-size: 12px;
        line-height: 16px;
    }

    /* Compact boxes are small enough to keep 3 columns until the diagram is phone width */
    .dd.compact .dd-grid.three {
        grid-template-columns: repeat(3, minmax(0, 1fr));
    }

@container (max-width: 460px) {

    .dd.compact .dd-grid.three {
        grid-template-columns: minmax(0, 1fr);
    }
}

/* In a product page's Section, a diagram fills the image column up to the width of the Section's images */
.section-image .dd {
    box-sizing: border-box;
    width: 600px; /* An explicit width : the image column sizes to its content, and a container query box has no content width */
    max-width: 100%;
    margin: 0 auto;
    box-shadow: 0 4px 14px rgba(0, 0, 0, 0.05);
}
