/* The long-read "We crashed our event pipeline 3,827 times" (short code txm).
   Loaded only on that post, after studio.css and post.css. Everything is
   scoped to the article's own classes: .txm-hero (the diorama stage) and
   .txm-body (the four big numbers, sections 01 to 07, figures, cards), plus
   the chart tooltip .txm-tip. Amber is an event in flight, red a crash, teal
   the outbox path, magenta our own design and the numbers.

   Layering: generic prose (p, h2) is post.css's job. The fallbacks here sit
   at the lowest weight that still beats the studio reset, so post.css wins
   wherever it has a say (and its cursor effect can lift a paragraph). */

.txm, .txm-tip {
    --txm-ink-2: #3b3542;
    --txm-grid: #DCD8E3;
    --txm-panel: var(--porcelain);
    --txm-fault: #B91C1C;

    /* Chart series: ours is magenta, the rest keep the measured palette. */
    --txm-c1: #2a78d6;
    --txm-c2: #eb6834;
    --txm-c3: #1baf7a;
    --txm-c4: #eda100;
    --txm-c5: #6E4BE0;
    --txm-c6: color-mix(in srgb, var(--magenta) 45%, #fff);
    --txm-c7: var(--magenta);
    --txm-c8: #e34948;
    --txm-poll10: #184f95;
    --txm-poll1000: #86b6ef;

    /* The diorama: studio neutrals tinted violet. */
    --dio-ground: #E7E5EC;
    --dio-top: #FFFFFF;
    --dio-left: #DCD8E3;
    --dio-right: #C3BECE;
    --dio-line: #CBC6D4;
    --dio-event: var(--amber);
    --dio-event-2: #DC8526;
    --dio-event-3: #B96A16;
    --dio-outbox: var(--teal);
    --dio-fault: #B91C1C;
    --dio-ink: var(--ink);
    --dio-halo: var(--txm-panel);
}

/* Segmented controls: one pressed pill, like the studio's filter chips. */

.txm-seg {
    display: inline-flex;
    flex-wrap: wrap;
    gap: 2px;
    padding: 3px;
    border-radius: 999px;
    border: 1px solid var(--hairline);
    background: #fff;
}
.txm-seg button {
    min-height: 44px;
    padding: 0 15px;
    border: 0;
    border-radius: 999px;
    background: transparent;
    font: 500 14px/1.2 var(--font-body);
    color: var(--txm-ink-2);
    cursor: pointer;
    transition: background-color .25s var(--ease), color .25s var(--ease);
}
.txm-seg button:hover { background: rgba(23, 18, 28, .06); color: var(--ink); }
.txm-seg button[aria-pressed="true"] { background: var(--ink); color: #fff; }

/* Hero: the diorama stage alone. The four big numbers open the body, after
   the title, so the title is on the first screen. */

/* minmax(0, 1fr): nothing inside (the diorama) can widen the hero past a
   phone screen. */
.txm-hero { container: txm-hero / inline-size; display: grid; grid-template-columns: minmax(0, 1fr); }
.txm-stage {
    padding: 16px 18px 14px;
    border-radius: var(--radius-l);
    border: 1px solid var(--hairline);
    background: var(--txm-panel);
}
.txm-stage-top { display: flex; flex-wrap: wrap; align-items: center; justify-content: space-between; gap: 10px 18px; }
.txm-counters { display: flex; flex-wrap: wrap; gap: 4px 18px; font-size: 14px; color: var(--txm-ink-2); }
.txm-counters b {
    display: inline-block;
    min-width: 3ch;
    margin-left: 4px;
    font: 500 14px/1.4 var(--font-mono);
    font-variant-numeric: tabular-nums;
    color: var(--ink);
}
.txm-counters .is-bad b { color: var(--txm-fault); }
/* Without the script the switch and the counters would do nothing. */
.txm-stage:not(.is-live) :is(.txm-seg, .txm-counters) { visibility: hidden; }

/* The whole scene always fits the stage. On a phone it is drawn at about
   half size, so diorama.ts sets --dio-k on the svg and the labels and crash
   badges grow by it (txm-math.ts textScale). */
/* On a wide screen the scene is held to 46 percent of the window's height
   (660 by 396 is its viewBox), so the title of the post starts on the first
   screen; it stays centred in the stage. */
.txm-dio { --dio-h: 46vh; margin-top: 6px; }
.txm-dio svg { display: block; width: min(100%, var(--dio-h) * 660 / 396); max-width: none; height: auto; margin-inline: auto; }
/* The still poster stands in until diorama.ts has drawn the scene and marked
   the stage is-live, so a script that never runs or fails leaves a picture,
   not an empty box. The poster (1200 by 750) keeps to the same height. */
.txm-dio-poster { display: block; width: min(100%, var(--dio-h) * 1200 / 750); height: auto; margin-inline: auto; border-radius: 16px; }
.txm-stage.is-live .txm-dio-poster, .txm-stage:not(.is-live) .txm-dio svg { display: none; }
.txm .dio-label { font-family: var(--font-body); font-size: calc(12.5px * var(--dio-k, 1)); font-weight: 600; stroke-width: calc(4px * var(--dio-k, 1)); }
.txm .dio-crash { font-family: var(--font-mono); font-size: 11px; font-weight: 600; letter-spacing: -.02em; }
.txm-stage-caption { max-width: 72ch; margin-top: 6px; font-size: 14px; line-height: 1.5; color: var(--slate); text-wrap: pretty; }

/* The four big numbers: the first block of the body, in the wide band. */
.txm-stats { display: grid; grid-template-columns: repeat(4, minmax(0, 1fr)); gap: 12px; max-width: 1040px; margin: 0 auto clamp(48px, 7cqi, 72px); }
.txm-stat {
    display: grid;
    align-content: start;
    gap: 6px;
    padding: 14px 16px 16px;
    border-radius: 20px;
    border: 1px solid var(--hairline);
    background: #fff;
}
.txm-stat-n {
    font: 600 clamp(18px, 2.8cqi, 28px)/1.05 var(--font-display);
    letter-spacing: -.03em;
    font-variant-numeric: tabular-nums;
    white-space: nowrap;
    color: var(--magenta);
}
.txm-stat-l { font-size: 13.5px; line-height: 1.4; color: var(--slate); }

@container txm (max-width: 640px) {
    .txm-stats { grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 10px; }
}
@container txm-hero (max-width: 640px) {
    .txm-stage { padding: 12px 12px 10px; border-radius: 22px; }
    .txm-counters { gap: 2px 14px; font-size: 13px; }
    /* A wrapped pill turns into a lopsided blob: on a narrow stage the
       switch keeps one row across the full width, and a long label (the
       Russian "Транзакционный outbox") wraps inside its own button. */
    .txm-stage .txm-seg { display: flex; flex-wrap: nowrap; width: 100%; }
    .txm-stage .txm-seg button { flex: 1 1 auto; padding: 4px 10px; font-size: 13px; }
}

/* Body: a reading measure for the text, a wider band for figures and cards. */

/* The measure is about 68ch of the 18px body text, fixed in px: a ch would
   follow each element's own size and misalign the headings and kickers. */
.txm-body { --txm-measure: 700px; container: txm / inline-size; font-size: 18px; line-height: 1.7; color: var(--txm-ink-2); }
.txm-sec + .txm-sec { margin-top: clamp(56px, 8cqi, 88px); }
.txm-sec > :not(.txm-wide) { max-width: var(--txm-measure); margin-inline: auto; }
.txm-sec > .txm-wide { max-width: 1040px; margin-inline: auto; }
.txm-sec > * + * { margin-top: 18px; }
.txm-sec > .txm-wide, .txm-sec > .txm-wide + * { margin-top: 32px; }
.txm-sec > .txm-kicker + h2 { margin-top: 10px; }

:where(.txm-sec) p { text-wrap: pretty; }
:where(.txm-sec) h2 {
    font: 600 clamp(22px, 2.6vw, 30px)/1.12 var(--font-display);
    letter-spacing: -.03em;
    color: var(--ink);
    text-wrap: balance;
    scroll-margin-top: 24px;
}
:where(.txm-sec .post-illo) { width: min(100%, 480px); height: auto; margin: 0 auto 22px; }

.txm-kicker { color: var(--magenta); }
.txm-num {
    font: 600 .94em/1 var(--font-display);
    letter-spacing: -.02em;
    font-variant-numeric: tabular-nums;
    white-space: nowrap;
    color: var(--magenta);
}

.txm-sec .txm-callout {
    padding: 18px 22px;
    border-radius: 18px;
    border-left: 4px solid var(--amber);
    background: color-mix(in srgb, var(--amber) 11%, #fff);
    font-size: 1.04em;
    line-height: 1.6;
    color: var(--ink);
}

/* Figures */

.txm-fig {
    min-width: 0;
    padding: 20px 20px 16px;
    border-radius: 24px;
    border: 1px solid var(--hairline);
    background: var(--txm-panel);
}
.txm-fig + .txm-fig { margin-top: 16px; }
.txm-fig-head { display: flex; flex-wrap: wrap; align-items: center; justify-content: space-between; gap: 10px 16px; margin-bottom: 10px; }
.txm-sec .txm-fig-title { margin: 0; max-width: none; font: 600 16px/1.35 var(--font-body); color: var(--ink); }
.txm-legend { display: flex; flex-wrap: wrap; gap: 6px 18px; font-size: 13px; line-height: 1.4; color: var(--txm-ink-2); }
.txm-legend span { display: inline-flex; align-items: center; gap: 7px; }
.txm-sw { display: inline-block; flex: none; width: 12px; height: 12px; border-radius: 4px; background: var(--txm-grid); }
.txm-sw--c1 { background: var(--txm-c1); }
.txm-sw--c2 { background: var(--txm-c2); }
.txm-sw--c3 { background: var(--txm-c3); }
.txm-sw--c4 { background: var(--txm-c4); }
.txm-sw--c5 { background: var(--txm-c5); }
.txm-sw--c6 { background: var(--txm-c6); }
.txm-sw--c7 { background: var(--txm-c7); }
.txm-sw--c8 { background: var(--txm-c8); }

/* Below 560 px a chart is drawn anew for the width of its box, one unit per
   pixel, with its rows stacked (txm.ts), so its texts keep their size. Only
   a box under 280 px scrolls sideways: soft shadows at the edges then show
   that more of it lies beyond; the panel coloured covers scroll with the
   chart and hide each shadow at its end, so a chart that fits shows none.
   Each chart also carries its numbers as a table for screen readers. */
.txm-chart {
    margin-top: 8px;
    overflow-x: auto;
    overscroll-behavior-x: contain;
    scrollbar-width: thin;
    background:
        linear-gradient(to right, var(--txm-panel) 40%, transparent) left center / 28px 100% no-repeat local,
        linear-gradient(to left, var(--txm-panel) 40%, transparent) right center / 28px 100% no-repeat local,
        radial-gradient(farthest-side at 0 50%, rgba(23, 18, 28, .16), transparent) left center / 12px 100% no-repeat scroll,
        radial-gradient(farthest-side at 100% 50%, rgba(23, 18, 28, .16), transparent) right center / 12px 100% no-repeat scroll;
}
.txm-chart { position: relative; }
.txm-chart svg { display: block; width: 100%; max-width: none; height: auto; overflow: visible; }
.txm-chart text { font-family: var(--font-body); }
.txm-chart text.num { font-family: var(--font-mono); letter-spacing: -.02em; }
.txm-chart .hit { cursor: default; }
.txm-fig figcaption { margin-top: 10px; font-size: 13.5px; line-height: 1.5; color: var(--slate); text-wrap: pretty; }

/* Architecture cards */

.txm-arch { display: grid; grid-template-columns: repeat(4, minmax(0, 1fr)); gap: 12px; }
/* The name row takes the slack, so the numbers line up across the cards. */
.txm-card {
    display: grid;
    grid-template-rows: auto 1fr auto;
    gap: 8px;
    padding: 16px;
    border-radius: 22px;
    border: 1px solid var(--hairline);
    background: #fff;
    transition: transform .45s var(--ease), box-shadow .45s var(--ease);
}
.txm-card:hover { transform: translateY(-3px); box-shadow: 0 24px 50px -30px rgba(40, 10, 50, .35); }
.txm-card.is-best { border-color: var(--teal); box-shadow: inset 0 0 0 1px var(--teal); }
.txm-card.is-best:hover { box-shadow: inset 0 0 0 1px var(--teal), 0 24px 50px -30px rgba(0, 60, 80, .4); }
.txm-card-top { display: flex; align-items: center; justify-content: space-between; gap: 8px; min-height: 30px; }
.txm-card-k { font: 600 26px/1 var(--font-display); letter-spacing: -.03em; color: var(--ink); }
.txm-pill { padding: 6px 10px; border-radius: 999px; background: var(--teal); color: #fff; font: 500 10.5px/1 var(--font-mono); letter-spacing: .04em; white-space: nowrap; }
.txm-card-name { font-size: 14px; line-height: 1.4; color: var(--txm-ink-2); }
.txm-card-dl {
    display: grid;
    grid-template-columns: minmax(0, 1fr) auto;
    gap: 4px 10px;
    margin: 4px 0 0;
    padding-top: 10px;
    border-top: 1px solid var(--hairline);
    font-size: 13.5px;
    line-height: 1.45;
}
.txm-card-dl dt { color: var(--slate); }
.txm-card-dl dd { margin: 0; text-align: right; font: 500 12.5px/1.6 var(--font-mono); font-variant-numeric: tabular-nums; color: var(--ink); }
.txm-card-dl dd.is-bad { color: var(--txm-fault); }

/* The checklist */

.txm-sec .txm-checklist { display: grid; gap: 12px; margin-top: 22px; padding: 0; list-style: none; counter-reset: txm-step; }
.txm-sec .txm-checklist li {
    display: grid;
    grid-template-columns: 40px minmax(0, 1fr);
    gap: 0 14px;
    padding: 14px 18px 14px 14px;
    border-radius: 18px;
    border: 1px solid var(--hairline);
    background: var(--txm-panel);
    counter-increment: txm-step;
    line-height: 1.6;
}
.txm-sec .txm-checklist li::before {
    content: counter(txm-step, decimal-leading-zero);
    font: 600 17px/1.6 var(--font-display);
    letter-spacing: -.02em;
    color: var(--magenta);
}
.txm-checklist b { font-weight: 600; color: var(--ink); }

/* How we measured, and the way to ask for more */

.txm-method p:not(.txm-cta) { font-size: 16.5px; }
.txm-sec .txm-cta {
    display: flex;
    flex-wrap: wrap;
    align-items: center;
    justify-content: space-between;
    gap: 14px 20px;
    margin-top: 28px;
    padding: 20px 20px 20px 24px;
    border-radius: 24px;
    border: 1px solid color-mix(in srgb, var(--magenta) 18%, transparent);
    background: linear-gradient(135deg, color-mix(in srgb, var(--magenta) 9%, #fff), color-mix(in srgb, var(--teal) 7%, #fff));
    font: 600 17px/1.45 var(--font-body);
    color: var(--ink);
}
.txm-sec .txm-cta-link {
    display: inline-flex;
    align-items: center;
    min-height: 46px;
    padding: 0 22px;
    border-radius: 999px;
    background: var(--ink);
    color: #fff;
    font-weight: 600;
    text-decoration: none;
    transition: background-color .25s var(--ease), transform .3s var(--spring);
}
.txm-sec .txm-cta-link:hover { background: var(--magenta); transform: translateY(-1px); }

.txm-body > .post-glossary { max-width: var(--txm-measure); margin: clamp(56px, 8cqi, 88px) auto 0; }

@container txm (max-width: 760px) {
    .txm-arch { grid-template-columns: repeat(2, minmax(0, 1fr)); }
}
@container txm (max-width: 560px) {
    .txm-sec { font-size: 17px; }
    .txm-fig { padding: 14px 12px 12px; border-radius: 20px; }
    /* The figure switches too: one row, the buttons share it. */
    .txm-fig .txm-seg { display: flex; flex-wrap: nowrap; width: 100%; }
    .txm-fig .txm-seg button { flex: 1 1 auto; padding: 4px 10px; font-size: 13px; }
    .txm-card { padding: 14px; border-radius: 18px; }
    .txm-sec .txm-checklist li { grid-template-columns: 32px minmax(0, 1fr); gap: 0 10px; padding: 12px 14px 12px 12px; }
    .txm-sec .txm-cta { padding: 18px; }
}
@container txm (max-width: 380px) {
    .txm-arch { grid-template-columns: minmax(0, 1fr); }
}

/* The chart tooltip lives on the body, outside any transformed parent. */

.txm-tip {
    position: fixed;
    left: 0;
    top: 0;
    z-index: 60;
    max-width: 17rem;
    padding: 8px 11px;
    border-radius: 12px;
    background: var(--ink);
    color: #fff;
    font: 400 13px/1.45 var(--font-body);
    box-shadow: 0 14px 34px rgba(23, 18, 28, .24);
    pointer-events: none;
    opacity: 0;
    translate: 0 4px;
    transition: opacity .14s ease, translate .14s ease;
}
.txm-tip.is-on { opacity: 1; translate: 0 0; }
.txm-tip b { font: 500 12px var(--font-mono); letter-spacing: -.02em; }

@media (prefers-reduced-motion: reduce) {
    .txm-card:hover { transform: none; }
}
.motion-off .txm-card:hover { transform: none; }

/* On paper the switch and the live counters do nothing. */
@media print {
    .txm-seg, .txm-counters { display: none; }
}

@media (forced-colors: active) {
    .txm-seg button[aria-pressed="true"] { outline: 2px solid Highlight; }
    .txm-sw, .txm-chart svg, .txm-dio svg { forced-color-adjust: none; }
}
