/* Kaimei AI — scrollytelling option: Neuron by neuron (css/scrolly/trace.css)
   js/scrolly/trace.js adds .tr-has-fig (it drew the diagram) and .is-live (the section pins and plays
   as you scroll). Without JS the steps are a plain list; under reduced motion or on short screens the
   finished diagram sits above that list. */

.tr {
  --tr-pos: #4f6ff0;
  --tr-neg: #dc3f96;
  --tr-edge: rgba(11, 12, 16, 0.2);
  --tr-math: 14px;
}
html[data-theme="dark"] .tr {
  --tr-pos: #82a0ff;
  --tr-neg: #ff7cbd;
  --tr-edge: rgba(255, 255, 255, 0.17);
}
.tr .section-head { margin-bottom: clamp(32px, 4vw, 48px); }
.tr i, .tr em { font-style: normal; }
.tr-pos { color: var(--tr-pos); }
.tr-neg { color: var(--tr-neg); }

/* ---------- Copy ---------- */
.tr-k {
  display: inline-flex; align-items: center; gap: 10px;
  font-family: var(--font-mono); font-size: 12px; font-weight: 500; letter-spacing: 0.12em; text-transform: uppercase; color: var(--ink-3);
}
.tr-k::before { content: ""; width: 18px; height: 2px; border-radius: 2px; background: var(--grad); }
.tr-k span { color: var(--ink-4); letter-spacing: 0.04em; }
.tr-h { margin-top: 10px; font-size: clamp(1.3rem, 1.9vw, 1.6rem); font-weight: 600; letter-spacing: -0.02em; line-height: 1.2; text-wrap: balance; }
.tr-p { margin-top: 10px; font-size: 15.5px; line-height: 1.6; color: var(--ink-2); text-wrap: pretty; max-width: 34em; }
.tr-p code, .tr-note code { font-size: 0.88em; padding: 1px 6px; border-radius: 6px; background: var(--bg-sunken); border: 1px solid var(--line); color: var(--ink); }
.tr-sw { font-weight: 600; }
.tr-sw-pos { color: var(--tr-pos); }
.tr-sw-neg { color: var(--tr-neg); }
.tr-links { display: flex; flex-wrap: wrap; gap: 8px 22px; margin-top: 16px; font-size: 15px; }

/* ---------- Maths ---------- */
.tr-math { font-family: var(--font-mono); font-size: var(--tr-math); line-height: 1.6; color: var(--ink-2); min-width: 0; font-variant-numeric: tabular-nums; }
.tr-math b { font-weight: 500; color: var(--ink); }
.tr-cap { font-size: 11.5px; letter-spacing: 0.1em; text-transform: uppercase; color: var(--ink-3); margin-bottom: 8px; }
.tr-quote { display: flex; flex-wrap: wrap; align-items: baseline; gap: 4px 10px; margin-top: 16px; font-size: 12.5px; color: var(--ink-3); }
.tr-quote > span { font-family: var(--font-sans); font-size: 13px; }
.tr-quote code { padding: 2px 8px; border-radius: 6px; background: var(--bg-sunken); border: 1px solid var(--line); color: var(--ink-2); white-space: nowrap; }

/* step 1: the request */
.tr-req { margin: 0; padding: 14px 16px; border-radius: 12px; background: var(--bg-sunken); border: 1px solid var(--line); font-size: 13px; line-height: 1.65; color: var(--ink-3); overflow-x: auto; }
.tr-req .tr-key { color: var(--ink-2); }
.tr-req b { color: var(--accent); font-weight: 500; }
.tr-stats { display: flex; flex-wrap: wrap; gap: 8px; margin-top: 12px; }
.tr-stats li { padding: 3px 10px; border-radius: 999px; border: 1px solid var(--line-strong); font-size: 12px; color: var(--ink-3); }
.tr-stats b { color: var(--ink); }

/* step 2: standardise */
.tr-std { display: grid; grid-template-columns: auto repeat(9, auto); justify-content: start; align-items: baseline; column-gap: 0.55ch; row-gap: 8px; }
.tr-std > :nth-child(10n+1) { color: var(--ink-3); padding-right: 1.4ch; }
.tr-std > :nth-child(10n+3), .tr-std > :nth-child(10n+5), .tr-std > :nth-child(10n+8), .tr-std > :nth-child(10n+10) { justify-self: end; }
.tr-std .tr-in { color: var(--accent); }
.tr-std .tr-eqs { padding: 0 0.5ch; color: var(--ink-3); }
.tr-std .tr-res { color: var(--ink); font-weight: 600; }
.tr-short { display: none; }

/* step 3: W x + b = h */
.tr-eq { display: flex; align-items: center; gap: 1.1ch; }
.tr-mxw { display: flex; flex-direction: column; align-items: center; gap: 4px; }
.tr-ml { font-size: 11px; letter-spacing: 0.06em; color: var(--ink-3); }
.tr-mx { position: relative; display: grid; grid-template-columns: auto; column-gap: 0.4ch; row-gap: 1px; padding: 3px 7px; text-align: right; }
.tr-mx.tr-c3 { grid-template-columns: repeat(3, auto); }
.tr-mx::before, .tr-mx::after { content: ""; position: absolute; top: 0; bottom: 0; width: 5px; border: 1.5px solid var(--ink-4); }
.tr-mx::before { left: 0; border-right: 0; border-radius: 3px 0 0 3px; }
.tr-mx::after { right: 0; border-left: 0; border-radius: 0 3px 3px 0; }
.tr-mx > span { padding: 0 0.45ch; border-radius: 5px; transition: background-color 0.25s, color 0.25s; }
.tr-op { color: var(--ink-3); }
.tr-mh[data-hl="0"] [data-r="0"], .tr-mh[data-hl="1"] [data-r="1"], .tr-mh[data-hl="2"] [data-r="2"], .tr-mh[data-hl="3"] [data-r="3"],
.tr-mr[data-hl="0"] [data-r="0"], .tr-mr[data-hl="1"] [data-r="1"], .tr-mr[data-hl="2"] [data-r="2"], .tr-mr[data-hl="3"] [data-r="3"] { background: var(--accent-soft); color: var(--ink); }
.tr-exps { display: grid; gap: 6px; margin-top: 14px; }
.tr-exp { font-size: 13.5px; line-height: 1.7; }
.tr-exp > span { display: block; }
.tr-exp i, .tr-olines i { color: var(--ink); font-weight: 500; }
.tr-exp b { font-weight: 600; }
.tr-pad { display: inline-block; width: 2ch; }
.tr-narrow { display: none; }

/* step 4: relu */
.tr-relu { display: grid; grid-template-columns: auto auto auto auto 1fr; justify-content: start; align-items: center; row-gap: 3px; }
.tr-relu > * { padding: 1px 0.45ch; transition: background-color 0.25s, color 0.25s; }
.tr-relu > :nth-child(5n+1) { border-radius: 5px 0 0 5px; color: var(--ink-3); }
.tr-relu > :nth-child(5n+2) { text-align: right; }
.tr-relu > :nth-child(5n+3) { color: var(--ink-3); }
.tr-relu > :nth-child(5n+4) { text-align: right; min-width: 7ch; border-radius: 0 5px 5px 0; font-weight: 600; }
.tr-relu > :nth-child(5n) { background: none !important; padding-left: 12px; }
.tr-relu .tr-off { color: var(--ink-4); }
.tr-tag-off { justify-self: start; font-size: 11px; letter-spacing: 0.08em; text-transform: uppercase; color: var(--tr-neg); }

/* step 5: output */
.tr-mo { display: grid; grid-template-columns: auto minmax(0, 176px); justify-content: start; align-items: center; gap: 0 24px; }
.tr-olines { min-width: 0; }
.tr-olines p { white-space: nowrap; }
.tr-olines s { text-decoration-color: var(--ink-4); color: var(--ink-4); }
.tr-big { font-size: 1.15em; font-weight: 600 !important; }
.tr-verdict { display: flex; align-items: center; gap: 12px; margin-top: 6px; color: var(--ink-3); }
.tr-churn {
  display: inline-block; padding: 1px 12px; border-radius: 999px; color: var(--ink); font-weight: 500;
  border: 1.5px solid transparent; background: linear-gradient(var(--bg), var(--bg)) padding-box, var(--grad) border-box;
}
.tr-sig { width: 100%; height: auto; overflow: visible; }
.tr-sig-ax { stroke: var(--line-strong); stroke-width: 1; }
.tr-sig-th { stroke: var(--ink-4); stroke-width: 1; stroke-dasharray: 3 3; }
.tr-sig-c { fill: none; stroke: var(--ink-2); stroke-width: 1.75; stroke-linejoin: round; }
.tr-sig-g { stroke: var(--ink-4); stroke-width: 1; stroke-dasharray: 2 3; }
.tr-sig-d { fill: var(--accent); stroke: var(--bg); stroke-width: 2; }
.tr-sig text { font-family: var(--font-mono); font-size: 11px; fill: var(--ink-3); }

/* step 6: drivers */
.tr-drv { display: grid; grid-template-columns: auto minmax(80px, 260px) auto auto; align-items: center; column-gap: 16px; row-gap: 10px; }
.tr-dh { font-size: 11px; letter-spacing: 0.1em; text-transform: uppercase; color: var(--ink-3); }
.tr-drv code { color: var(--ink); }
.tr-drv > :nth-child(4n+3), .tr-drv > :nth-child(4n) { text-align: right; }
.tr-dbar { display: block; height: 10px; border-radius: 99px; background: var(--bg-sunken); border: 1px solid var(--line); overflow: hidden; }
.tr-dbar i { display: block; height: 100%; border-radius: 99px; transform-origin: 0 50%; transform: scaleX(var(--s)); }
.tr-pos-fill { background: var(--toward); }
.tr-neg-fill { background: var(--away); }

/* step 7: code */
.tr-code { margin: 0; padding: 16px 18px; border-radius: 14px; background: var(--bg-code); color: #d7dbe8; font-size: 13px; line-height: 1.65; overflow-x: auto; border: 1px solid rgba(255, 255, 255, 0.08); }
.tr-cm { color: #7a8299; }

.tr-note { margin-top: clamp(28px, 4vw, 44px); max-width: 880px; font-size: 12.5px; line-height: 1.6; color: var(--ink-3); }

/* ---------- Static list (no JS, reduced motion, short screens) ---------- */
.tr-bar { display: none; }
.tr-fig { display: none; margin: 0; }
.tr-steps { display: grid; border-top: 1px solid var(--line); }
.tr-step { display: grid; grid-template-columns: minmax(0, 5fr) minmax(0, 7fr); gap: 20px clamp(32px, 5vw, 72px); align-items: start; align-content: start; padding: 30px 0; border-bottom: 1px solid var(--line); }
.tr.tr-has-fig .tr-fig { display: block; }
.tr.tr-has-fig:not(.is-live) .tr-fig { height: clamp(380px, 44vw, 520px); margin-bottom: 36px; }
.tr.tr-has-fig:not(.is-live) .tr-svg { width: 100%; height: 100%; }

/* ---------- Diagram (built by js/scrolly/trace.js) ---------- */
.tr-svg { display: block; overflow: visible; font-family: var(--font-mono); }
.tr-svg .tr-e0 { fill: none; stroke: var(--tr-edge); }
.tr-svg .tr-e0.tr-dead { stroke-dasharray: 3 4; }
.tr-svg .tr-e1 { fill: none; stroke-linecap: round; }
.tr-svg .tr-e1.tr-up { stroke: var(--tr-pos); }
.tr-svg .tr-e1.tr-down { stroke: var(--tr-neg); }
.tr-svg .tr-e2, .tr-svg .tr-e3, .tr-svg .tr-e4 { fill: none; stroke-linecap: round; }
.tr-svg .tr-e2 { stroke-opacity: 0.55; }
.tr-svg .tr-pill { fill: var(--bg-elev); stroke: var(--line-strong); stroke-width: 1; }
.tr-svg .tr-pill.tr-wait { stroke: var(--ink-4); stroke-dasharray: 3 3; }
.tr-svg .tr-pill.tr-dead { fill: var(--bg-sunken); stroke: var(--ink-4); stroke-dasharray: 3 3; }
.tr-svg .tr-tint.tr-up { fill: var(--tr-pos); }
.tr-svg .tr-tint.tr-down { fill: var(--tr-neg); }
.tr-svg .tr-ring { fill: none; stroke-width: 1.6; }
.tr-svg .tr-halo { fill: none; stroke-width: 6; }
.tr-svg .tr-val { fill: var(--ink); font-weight: 500; }
.tr-svg .tr-val.tr-dead { fill: var(--ink-4); }
.tr-svg .tr-lab { fill: var(--ink-2); }
.tr-svg .tr-tag { fill: var(--ink-3); }
.tr-svg .tr-tag.tr-hot { fill: var(--tr-neg); font-weight: 500; }
.tr-svg .tr-idx { fill: var(--ink-3); }
.tr-svg .tr-offl { fill: var(--tr-neg); letter-spacing: 0.08em; text-transform: uppercase; }
.tr-svg .tr-chip { fill: var(--bg-elev); stroke-width: 1.5; }
.tr-svg .tr-chip-t { fill: var(--ink); font-weight: 500; }
.tr-svg .tr-hn { font-family: var(--font-sans); font-weight: 600; fill: var(--ink-3); transition: fill 0.3s; }
.tr-svg .tr-hf { fill: var(--ink-4); transition: fill 0.3s; }
.tr-svg .tr-hn.tr-on { fill: var(--ink); }
.tr-svg .tr-hf.tr-on { fill: var(--ink-2); }
.tr-svg .tr-hu { stroke-width: 2; stroke-linecap: round; opacity: 0; transition: opacity 0.3s; }
.tr-svg .tr-hu.tr-on { opacity: 1; }
.tr-svg .tr-lgt { fill: var(--ink-3); }
.tr-svg .tr-lgs { stroke-width: 2.5; stroke-linecap: round; }
.tr-svg .tr-lgs.tr-up { stroke: var(--tr-pos); }
.tr-svg .tr-lgs.tr-down { stroke: var(--tr-neg); }
.tr-svg .tr-lgs.tr-offs { stroke: var(--ink-4); stroke-width: 1.5; stroke-dasharray: 3 3; stroke-linecap: butt; }

/* ---------- Live: pinned, one step at a time ---------- */
.tr.is-live .tr-track { height: calc(100vh + 7.1 * 68vh); }
.tr.is-live .tr-pin { position: sticky; top: 0; height: 100vh; height: 100svh; padding: 78px 0 clamp(22px, 7vh, 64px); }
.tr.is-live .tr-frame { display: flex; flex-direction: column; height: 100%; }
.tr.is-live .tr-bar { display: flex; align-items: center; justify-content: space-between; gap: 16px; flex: none; min-width: 0; }
.tr-endpoint { min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; font-family: var(--font-mono); font-size: 12px; color: var(--ink-3); }
.tr-endpoint b { margin-right: 6px; font-weight: 500; color: var(--accent); }
.tr-rail { display: flex; flex: none; gap: 2px; }
.tr-rail button { padding: 10px 2px; border-radius: 4px; }
.tr-rail i {
  display: block; width: 24px; height: 3px; border-radius: 3px;
  background: linear-gradient(90deg, var(--blue), var(--violet)) 0 0 / calc(var(--t, 0) * 100%) 100% no-repeat, var(--line-strong);
}
.tr-rail button:hover i { background-color: var(--ink-4); }
.tr.is-live .tr-fig { position: relative; flex: 1 1 auto; min-height: 0; margin: 6px 0 0; }
.tr.is-live .tr-svg { position: absolute; inset: 0; width: 100%; height: 100%; }
.tr.is-live .tr-steps { flex: none; border-top: 1px solid var(--line); padding-top: 20px; margin-top: 14px; }
.tr.is-live .tr-step {
  grid-area: 1 / 1; padding: 0; border: 0; opacity: 0; visibility: hidden; transform: translateY(8px);
  transition: opacity 0.35s var(--ease), transform 0.5s var(--ease), visibility 0.35s;
}
.tr.is-live .tr-step.is-active { opacity: 1; visibility: visible; transform: none; }
.tr.is-live .tr-exps { grid-template-columns: minmax(0, 1fr); }
.tr.is-live .tr-exp { grid-area: 1 / 1; opacity: 0; transition: opacity 0.3s var(--ease); }
.tr.is-live .tr-exp.tr-on { opacity: 1; }
.tr.is-live .tr-n { display: inline-block; }
.tr.is-live .tr-dbar i { transform: scaleX(0); }

/* ---------- Narrower screens ---------- */
@media (max-width: 980px) {
  .tr { --tr-math: 13px; }
  .tr-step { grid-template-columns: minmax(0, 1fr); }
  .tr.is-live .tr-step { gap: 14px; }
  .tr-p { max-width: none; }
}
/* landscape tablets and small laptops: keep copy and maths side by side so the diagram keeps its height */
@media (max-width: 980px) and (min-width: 860px) {
  .tr.is-live .tr-step { grid-template-columns: minmax(0, 5fr) minmax(0, 7fr); gap: 0 28px; }
  .tr-p { font-size: 14.5px; }
}
@media (max-width: 640px) {
  .tr { --tr-math: 13px; }
  .tr-wide { display: none !important; }
  .tr-narrow { display: inline; }
  .tr-narrow.tr-pad { display: inline-block; }
  .tr-resl { display: block; }
  .tr-h { font-size: 1.2rem; margin-top: 6px; }
  .tr-p { font-size: 14px; line-height: 1.55; margin-top: 6px; }
  .tr-k { font-size: 11.5px; }
  .tr-eq { gap: 0.6ch; font-size: 12px; }
  .tr-mx { padding: 2px 5px; }
  .tr-mx > span { padding: 0 0.3ch; }
  .tr-exp { font-size: 13px; }
  .tr-req { font-size: 12.5px; padding: 12px 14px; }
  .tr-code { font-size: 12px; padding: 12px 14px; }
  .tr-mo { grid-template-columns: minmax(0, 1fr); }
  .tr-sig { display: none; }
  .tr-drv { column-gap: 10px; grid-template-columns: auto minmax(40px, 1fr) auto auto; }
  .tr-quote code { white-space: normal; }
  .tr-cap { display: none; }
  .tr-stats { gap: 4px 12px; margin-top: 10px; }
  .tr-stats li { padding: 0; border: 0; font-size: 11.5px; }
  .tr-links { margin-top: 10px; }
  .tr.is-live .tr-pin { padding: 70px 0 58px; }
  .tr.is-live .tr-steps { padding-top: 14px; margin-top: 8px; }
  .tr.is-live .tr-step { gap: 10px; }
  .tr-endpoint { display: none; }
  .tr.is-live .tr-bar { justify-content: center; }
  .tr-rail i { width: 30px; }
  /* the pinned phone layout keeps only the call itself; the static list shows its output too */
  .tr.is-live .tr-code-out { display: none; }
}
@media (max-width: 640px) and (max-height: 799px) {
  .tr.is-live .tr-quote { display: none; }
}
@media (max-width: 560px) {
  .tr-long { display: none; }
  .tr-short { display: inline; }
}
