/* =========================================================================
   MX TICKER — thin live index tape pinned above the navbar.

   Self-contained: mx-ticker.js injects the markup and this file positions it,
   so adding the tape to a page is just the two <link>/<script> tags. Nothing
   in styles.css is edited.

   HOW THE OFFSET WORKS (the only fiddly part)
   `.nav` is `position: fixed; top: 0`, and every other rule in the codebase
   that needs "how far down does content start" reads `var(--nav-h)` — the
   mobile drawer, the cardnav panel, the hero padding, `.section`
   scroll-margin, heatmap, pages.css drawer, stock.css. Rather than patch all
   of those, we redefine `--nav-h` ON BODY to mean nav + tape, and give `.nav`
   its real height back explicitly. Every consumer then offsets correctly for
   free, and pages without the tape are untouched.

   `--mx-nav-real` is stamped on <body> by the JS from the computed `--nav-h`
   BEFORE injection, because the landing page's preview-overrides.css lowers
   it to 65px while the rest of the site uses 68px — reading it beats
   hardcoding either.
   ========================================================================= */

:root {
  --mx-tick-h: 30px;

  /* Brand navy, theme-aware — the same two values styles.css uses for
     `--purple` (#2708a0 light / #33058d dark, Pantone 2371-C).
     Deliberately NOT `var(--purple)`: pages.css line 54 remaps that token to
     #4969FF for `.mx-page` in dark mode, which would make the tape a different
     colour on the inner pages than on the landing page. Hardcoding the pair
     keeps one tape colour everywhere. */
  --mx-tick-bg:   #2708a0;
  --mx-tick-fg:   #ffffff;
  --mx-tick-dim:  rgba(255, 255, 255, 0.62);
  --mx-tick-line: rgba(255, 255, 255, 0.16);
  --mx-tick-up:   #5df2a5;
  --mx-tick-down: #ff9a94;
}
[data-theme="dark"] { --mx-tick-bg: #33058d; }

/* ⚠️ Text is WHITE in BOTH themes, not black-in-light. Black on this navy
   measures 1.63:1 (light) and 1.55:1 (dark) — WCAG AA needs 4.5:1, so it is
   genuinely unreadable, not merely low-contrast. White measures 12.9:1 / 13.6:1.
   To force black anyway, set --mx-tick-fg/#dim to #000 / rgba(0,0,0,.62). */

body:has(.mxtick) {
  /* now means "nav bottom edge", which is what every consumer actually wants */
  --nav-h: calc(var(--mx-nav-real, 68px) + var(--mx-tick-h));
}
body:has(.mxtick) .nav {
  top: var(--mx-tick-h);
  height: var(--mx-nav-real, 68px);
}

/* Anything that reads var(--nav-h) shifts for free via the rule above —
   heatmap's .hm-app, pages.css's drawer, stock.css's finnav, the scroll
   progress bar, the mobile drawer, the cardnav panel.

   These three HARDCODE their offset instead, so they have to be nudged by hand
   or content slides under the navbar. Kept as a short, explicit list rather
   than rewriting them to var(--nav-h), which would change those pages even
   when the tape is absent. */
body:has(.mxtick) main.mx-main { padding-top: calc(84px + var(--mx-tick-h)); }  /* pages.css:66  */
body:has(.mxtick) .mkd-rail    { top: calc(78px + var(--mx-tick-h)); }          /* markets-detail.css:28 */
body:has(.mxtick) .mkt-subnav  { top: calc(60px + var(--mx-tick-h)); }          /* markets.css:13 */

/* ------------------------------------------------------------- the tape -- */
.mxtick {
  position: fixed;
  inset: 0 0 auto 0;
  height: var(--mx-tick-h);
  z-index: 1001;                 /* above .nav (1000) — it sits on top of it */
  display: flex;
  align-items: center;
  overflow: hidden;
  background: var(--mx-tick-bg);
  border-bottom: 1px solid var(--mx-tick-line);
  font-family: var(--font-mono, ui-monospace, "JetBrains Mono", SFMono-Regular, Menlo, monospace);
  /* the tape is decoration around live numbers — never let it grab a tap that
     was meant for the navbar underneath */
  user-select: none;
}

/* Edge fades so items dissolve instead of being guillotined at the viewport
   edge. Pointer-events off or they'd eat clicks on the first/last item. */
.mxtick::before,
.mxtick::after {
  content: "";
  position: absolute; top: 0; bottom: 0;
  width: 46px; z-index: 2; pointer-events: none;
}
.mxtick::before { left: 0;  background: linear-gradient(90deg, var(--mx-tick-bg), transparent); }
.mxtick::after  { right: 0; background: linear-gradient(270deg, var(--mx-tick-bg), transparent); }

/* The track holds the items TWICE (JS duplicates them). Translating it by
   exactly -50% lands copy 2 where copy 1 started, so the loop is seamless.
   Percentage of the track's own width, so it stays correct at any item count.

   The roll is held back until `.is-rolling` is added, because the duration is
   derived from the MEASURED track width and the items are narrower while they
   still hold "—" placeholders. Animating before the real prices land locks in
   a duration for the wrong width and the tape runs ~2x too fast. */
.mxtick__track {
  display: flex;
  align-items: center;
  flex: none;
  white-space: nowrap;
  will-change: transform;
}
.mxtick__track.is-rolling {
  animation: mxtick-roll var(--mx-tick-dur, 60s) linear infinite;
}
@keyframes mxtick-roll {
  from { transform: translate3d(0, 0, 0); }
  to   { transform: translate3d(-50%, 0, 0); }
}

/* Reading a moving number is hard — hovering parks it. */
.mxtick:hover .mxtick__track,
.mxtick:focus-within .mxtick__track { animation-play-state: paused; }

/* ------------------------------------------------------------- an item -- */
.mxtick__i {
  display: inline-flex;
  align-items: baseline;
  gap: 6px;
  padding: 0 15px;
  font-size: 11.5px;
  line-height: 1;
  border-right: 1px solid var(--mx-tick-line);
}
.mxtick__n {                                   /* index name */
  font-family: Inter, system-ui, sans-serif;
  font-weight: 600;
  letter-spacing: .01em;
  color: var(--mx-tick-dim);
}
.mxtick__p {                                   /* last price */
  font-weight: 500;
  color: var(--mx-tick-fg);
  font-variant-numeric: tabular-nums;          /* stops width jitter on tick */
}
.mxtick__c {                                   /* change % */
  font-weight: 600;
  font-variant-numeric: tabular-nums;
  color: var(--mx-tick-dim);
}
/* Colour is NOT the only signal — every item carries an arrow glyph too, so
   this still reads for colour-blind users and in greyscale print. */
.mxtick__c.up   { color: var(--mx-tick-up); }
.mxtick__c.down { color: var(--mx-tick-down); }

/* A value that just changed flashes its background briefly, so you can see
   WHICH number moved on a tape that is already in motion. */
.mxtick__i.bump .mxtick__p { animation: mxtick-bump .9s ease-out; border-radius: 3px; }
@keyframes mxtick-bump {
  0%   { background: rgba(255,255,255,.22); box-shadow: 0 0 0 3px rgba(255,255,255,.22); }
  100% { background: transparent;        box-shadow: 0 0 0 3px transparent; }
}

/* skeleton shimmer for the first paint, before any quote has landed */
.mxtick__sk {
  display: inline-block; width: 92px; height: 8px; margin: 0 15px; border-radius: 4px;
  background: linear-gradient(90deg, var(--mx-tick-line) 0%,
              rgba(255,255,255,.42) 50%, var(--mx-tick-line) 100%);
  background-size: 220% 100%;
  animation: mxtick-sweep 1.3s ease-in-out infinite;
}
@keyframes mxtick-sweep { from { background-position: 130% 0; } to { background-position: -130% 0; } }

/* --------------------------------------------------------------- phones -- */
@media (max-width: 640px) {
  :root { --mx-tick-h: 26px; }
  .mxtick__i { font-size: 10.5px; padding: 0 11px; gap: 5px; }
  .mxtick::before, .mxtick::after { width: 26px; }
}

/* ------------------------------------------------------ reduced motion -- */
/* Auto-scrolling text is a genuine accessibility problem (WCAG 2.2.2: moving
   content must be pausable). When motion is not wanted the tape stops dead and
   becomes an ordinary horizontal scroller the reader drives themselves. */
@media (prefers-reduced-motion: reduce) {
  .mxtick { overflow-x: auto; scrollbar-width: none; }
  .mxtick::-webkit-scrollbar { display: none; }
  .mxtick__track { animation: none; }
  .mxtick__i.bump .mxtick__p { animation: none; }
  .mxtick__sk { animation: none; }
}
