/* Keep each lifeline at the centre of its actor's half of the diagram. */
.kem-sequence {
    --lane: color-mix(in srgb, #8c6ac7 75%, var(--fg));
    --actor-fill: color-mix(in srgb, var(--bg) 87%, #9274d0);
    --note-fill: color-mix(in srgb, var(--bg) 81%, #edc151);
    --note-edge: color-mix(in srgb, var(--bg) 35%, #ac8525);
    max-width: 68rem;
    margin: 2rem auto;
    color: var(--fg);
    line-height: 1.5;
    overflow-wrap: anywhere;
    break-inside: avoid;
}

.kem-actors {
    display: grid;
    grid-template-columns: repeat(2, minmax(0, 1fr));
    justify-items: center;
}

.kem-actors > span {
    box-sizing: border-box;
    width: min(92%, 19rem);
    padding: 0.6em;
    border: 1px solid var(--lane);
    border-radius: 0.4rem;
    background: var(--actor-fill);
    text-align: center;
}

.kem-actors small { display: block; }

.kem-events {
    position: relative;
    isolation: isolate;
    display: grid;
    gap: 1.6rem;
    margin: 0;
    padding: 1.6rem 0;
    list-style: none;
}

.kem-events::before, .kem-events::after {
    content: "";
    position: absolute;
    z-index: -1;
    inset-block: 0;
    border-left: 2px solid var(--lane);
}

.kem-events::before { left: 25%; }
.kem-events::after { left: 75%; }

.kem-note, .kem-outcome {
    box-sizing: border-box;
    padding: 0.65em 1em;
    border: 1px solid var(--note-edge);
    border-radius: 0.4rem;
    background: var(--note-fill);
}

.kem-note { width: fit-content; max-width: 68%; }
.kem-bob { justify-self: end; margin-right: 2%; }
.kem-alice { justify-self: start; margin-left: 2%; }
.kem-outcome { justify-self: center; max-width: 90%; text-align: center; font-weight: 600; }

.kem-message {
    justify-self: center;
    width: 50%;
    text-align: center;
    font-weight: 600;
}

.kem-message svg { display: block; margin-top: 0.25em; fill: currentColor; }
.kem-message line { stroke: currentColor; stroke-width: 2; }
.kem-left > svg { transform: scaleX(-1); }

.kem-reader {
    position: absolute;
    width: 1px;
    height: 1px;
    overflow: hidden;
    clip-path: inset(50%);
    white-space: nowrap;
}

/* A vertical reading order stays the same when parallel branches wrap. */
.crypto-flow {
    max-width: 68rem;
    margin: 2rem auto;
    color: var(--fg);
    line-height: 1.55;
    overflow-wrap: anywhere;
    container-type: inline-size;
}

.crypto-flow figcaption {
    font-size: 1.05em;
    font-weight: 650;
    margin-bottom: 1em;
}

.flow-steps {
    display: grid;
    gap: 2rem;
    margin: 0;
    padding: 0;
    list-style: none;
}

.flow-steps > li {
    position: relative;
    min-width: 0;
    margin: 0;
}

/* Connect stages, leaving their labels in normal document flow. */
.flow-steps > li + li::before {
    content: "";
    position: absolute;
    top: -1.75rem;
    height: 1.35rem;
    width: 0.85rem;
    left: calc(50% - 0.425rem);
    background: currentColor;
    clip-path: polygon(42% 0, 58% 0, 58% 60%, 100% 60%, 50% 100%, 0 60%, 42% 60%);
}

.flow-stage, .flow-pair > div {
    box-sizing: border-box;
    border: 1px solid color-mix(in srgb, var(--fg) 42%, var(--bg));
    border-radius: 0.45rem;
    padding: 0.7em 1em;
    background: color-mix(in srgb, var(--bg) 88%, #af9638);
}

.flow-stage > strong, .flow-pair > div > strong { display: block; }
.byte-map {
    display: block;
    width: 100%;
    height: 1.2rem;
    margin-top: 0.5em;
    border: 1px solid currentColor;
    box-sizing: border-box;
}
.byte-map rect { fill: var(--fg); fill-opacity: 0.65; }
.byte-map rect:nth-child(even) { fill-opacity: 0.18; }
.flow-stage.flow-result { background: color-mix(in srgb, var(--bg) 85%, #9380cb); }

.flow-pair {
    display: grid;
    grid-template-columns: repeat(2, minmax(0, 1fr));
    gap: 1rem;
}

.flow-pair > div { background: color-mix(in srgb, var(--bg) 90%, #7997ba); }

@container (max-width: 34rem) {
    .flow-pair { grid-template-columns: minmax(0, 1fr); }
}

@media print {
    .crypto-flow { --bg: white; --fg: #171717; }
    .flow-steps > li, .kem-events > li { break-inside: avoid; }
    .crypto-flow figcaption, .kem-actors:first-child { break-after: avoid; }
    #x-wing-diagram { break-inside: auto; }
    .kem-sequence {
        --bg: white;
        --fg: #171717;
        --lane: #555;
        --note-edge: #777;
        background: white;
    }
}
