/* readalong — the sentence highlight and its player.
 *
 * Every rule here is an accessibility decision, so each one says why.
 */

/* The player: a bar across the foot of the window.
 *
 * At the foot and not the top, and that is the one placement decision with
 * a reason rather than a taste behind it. Pages that carry a narration
 * usually also carry a sticky masthead, and a second sticky bar under the
 * first stacks two chrome strips over the text — a hundred-odd pixels of
 * reading gone, and no way to tell which bar owns which control. The foot
 * is also where a thumb already is on a phone, and where every music and
 * podcast player has put its controls for fifteen years.
 *
 * One behaviour, not two. A bar that is inline until you scroll and fixed
 * afterwards is two components to keep agreeing; this is one.
 *
 * Thin, because it is always there. The scrubber rides the bar's top edge
 * rather than taking a row: a progress line is a line, and a row of its own
 * would cost twenty pixels of every page for nothing.
 *
 * Colours come from the page where the page says what they are — see
 * `page_palette`. `--ra-accent` fills, `--ra-tint` washes, and both fall
 * back to a teal chosen for clearing 3:1 on a light page and a dark one.
 */

.ra-player {
  --ra-ink: var(--ra-accent, #0D9488);
  --ra-wash: var(--ra-tint, var(--ra-accent, #0D9488));
  --ra-face: color-mix(in srgb, var(--ra-wash) 22%, transparent);
  --ra-line: color-mix(in srgb, var(--ra-wash) 45%, transparent);
  /* The muted *text* stays neutral. Tinting type costs legibility for a
     tint nobody reads as colour at this size. */
  --ra-dim: color-mix(in srgb, currentColor 58%, transparent);
  --ra-progress: 0;
  /* The bar's own height, published so the page can keep its last
     paragraph out from under it. */
  --ra-bar: 3.5rem;

  display: block;
  margin: 1.5rem 0;
  padding: 0.875rem 1rem;
  border: 1px solid var(--ra-line);
  border-radius: 0.75rem;
  background: var(--ra-face);
}

.ra-title {
  margin: 0;
  font-size: 0.9375rem;
  font-weight: 600;
  line-height: 1.3;
}

.ra-player audio {
  width: 100%;
  margin-top: 0.625rem;
  /* Native controls as the floor. The designed bar is built on a
     `<button>` and an `<input type="range">` precisely so it is not a
     keyboard trap; this stays as what a reader gets when the module does
     not run at all. */
}

/* ── Enhanced: the bar ──────────────────────────────────────────────── */

.ra-player[data-ra-enhanced] {
  position: fixed;
  inset: auto 0 0 0;
  z-index: 2147483000;
  margin: 0;
  padding: 0;
  border: 0;
  border-top: 1px solid var(--ra-line);
  border-radius: 0;
  /* Opaque, not a wash: a translucent bar over scrolling text is text over
     text. The ground is the page's own paper when the build could read it,
     and `Canvas` otherwise — the browser's idea of a page background, which
     is white on any page that is dark through its own stylesheet without
     declaring `color-scheme`. */
  background: color-mix(in srgb, var(--ra-wash) 10%, var(--ra-paper, Canvas));
  backdrop-filter: saturate(1.4) blur(8px);
  box-shadow: 0 -1px 12px rgb(0 0 0 / 0.07);
}

/* The page keeps its last paragraph out from under the bar. Set here
   rather than left to the host: the component is what knows its height,
   and a reader who cannot reach the final sentence will not know why.
   
   Keyed off a class the module puts on <html>, not off `:has()`. The
   selector version reads better and silently does nothing on a browser
   without `:has()` — which would hide the end of the article on exactly
   the browsers least able to tell you why. The module is already running
   by the time the bar exists, so a class costs nothing. */
.ra-has-player body {
  padding-bottom: calc(var(--ra-bar, 3.5rem) + env(safe-area-inset-bottom, 0px));
}

.ra-player[data-ra-enhanced] .ra-title,
.ra-player[data-ra-enhanced] audio,
.ra-player[data-ra-enhanced] .ra-meta {
  display: none;
}

.ra-ui {
  display: block;
}

.ra-row {
  display: flex;
  align-items: center;
  gap: 0.75rem;
  max-width: 64rem;
  margin: 0 auto;
  padding: 0.5rem 1rem calc(0.5rem + env(safe-area-inset-bottom, 0px));
  min-width: 0;
}

/* ── The scrubber, riding the top edge ──────────────────────────────── */

.ra-scrub {
  -webkit-appearance: none;
  appearance: none;
  display: block;
  width: 100%;
  height: 14px;
  margin: -7px 0 -7px;
  background: transparent;
  cursor: pointer;
  position: relative;
  z-index: 1;
}

.ra-scrub::-webkit-slider-runnable-track {
  height: 3px;
  background:
    linear-gradient(var(--ra-ink), var(--ra-ink)) 0 /
      calc(var(--ra-progress) * 100%) 100% no-repeat,
    var(--ra-line);
}

.ra-scrub::-moz-range-track { height: 3px; background: var(--ra-line); }
.ra-scrub::-moz-range-progress { height: 3px; background: var(--ra-ink); }

/* The thumb is invisible until the scrubber is touched or focused: a dot
   parked on a hairline at the top of every page is a speck of dirt. It is
   still a full-size hit target, which is the part that matters. */
.ra-scrub::-webkit-slider-thumb {
  -webkit-appearance: none;
  appearance: none;
  width: 12px;
  height: 12px;
  margin-top: -4.5px;
  border: 0;
  border-radius: 50%;
  background: var(--ra-ink);
  opacity: 0;
  transition: opacity 120ms ease;
}

.ra-scrub::-moz-range-thumb {
  width: 12px; height: 12px; border: 0; border-radius: 50%;
  background: var(--ra-ink); opacity: 0;
}

.ra-player:hover .ra-scrub::-webkit-slider-thumb,
.ra-scrub:focus-visible::-webkit-slider-thumb { opacity: 1; }
.ra-player:hover .ra-scrub::-moz-range-thumb,
.ra-scrub:focus-visible::-moz-range-thumb { opacity: 1; }

/* ── The play button ────────────────────────────────────────────────── */
/*
 * A filled disc with the glyph cut out of it. The cut-out is the part that
 * matters: painting a pale triangle on a dark disc requires knowing what
 * pale *is*, and this component is injected into pages whose background it
 * has never seen. `Canvas` is the browser's idea of the page background and
 * not necessarily the page's; `white` is wrong half the time. Guess wrong
 * and the result is a solid disc with no symbol in it.
 *
 * So the disc is painted and the glyph is a hole. Whatever is behind shows
 * through, which is right on every page by construction.
 */
.ra-play {
  width: 2.25rem;
  height: 2.25rem;
  flex: none;
  padding: 0;
  border: 0;
  background: var(--ra-ink);
  cursor: pointer;
  -webkit-mask: var(--ra-glyph) center / contain no-repeat;
  mask: var(--ra-glyph) center / contain no-repeat;
  --ra-glyph: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24'%3E%3Cpath fill-rule='evenodd' d='M12 0A12 12 0 1 0 12 24 12 12 0 0 0 12 0ZM9.6 7.2 17 12 9.6 16.8Z'/%3E%3C/svg%3E");
}

.ra-play[data-ra-playing] {
  --ra-glyph: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24'%3E%3Cpath fill-rule='evenodd' d='M12 0A12 12 0 1 0 12 24 12 12 0 0 0 12 0ZM9 7.6h2.2v8.8H9ZM12.8 7.6H15v8.8h-2.2Z'/%3E%3C/svg%3E");
}

@supports not ((-webkit-mask: url("#x")) or (mask: url("#x"))) {
  .ra-play { border: 2px solid currentColor; border-radius: 50%; background: transparent; }
}

/* ── What the bar says ──────────────────────────────────────────────── */

.ra-said {
  display: flex;
  align-items: baseline;
  gap: 0.5rem;
  min-width: 0;
  flex: 1 1 auto;
}

.ra-said-title {
  font-size: 0.875rem;
  font-weight: 600;
  /* One line, truncated. A title that wraps makes the bar grow, and a bar
     that grows pushes the page it is supposed to sit quietly under. */
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  min-width: 0;
}

.ra-time {
  font-size: 0.75rem;
  font-variant-numeric: tabular-nums;
  color: var(--ra-dim);
  flex: none;
}

.ra-time-total::before { content: "/ "; }

.ra-tools {
  display: inline-flex;
  align-items: center;
  gap: 0.125rem;
  flex: none;
}

.ra-skip,
.ra-rate {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  min-width: 2.25rem;
  height: 2.25rem;
  padding: 0 0.375rem;
  border: 0;
  border-radius: 0.375rem;
  background: transparent;
  color: inherit;
  font: inherit;
  font-size: 0.75rem;
  font-variant-numeric: tabular-nums;
  cursor: pointer;
}

.ra-skip { position: relative; }

.ra-skip svg {
  width: 1.75rem;
  height: 1.75rem;
  /* Dimmer than the number it surrounds: the ring says "jump", the digits
     say how far, and only one of those is read. */
  color: var(--ra-dim);
}

/* The forward glyph is the back glyph mirrored. One path, two buttons --
   and the number inside is *not* mirrored, which is the whole reason this
   is a transform on the icon rather than on the button. */
.ra-skip-fwd svg { transform: scaleX(-1); }

/* The number sits in the ring's empty middle, slightly below centre
   because the ring's gap is at the top and the optical centre is not the
   geometric one. */
.ra-skip-n {
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -42%);
  font-size: 0.6875rem;
  font-weight: 700;
  line-height: 1;
  font-variant-numeric: tabular-nums;
  letter-spacing: -0.02em;
}

.ra-rate { font-weight: 600; font-size: 0.8125rem; color: var(--ra-dim); }

.ra-play:hover, .ra-skip:hover, .ra-rate:hover { opacity: 0.8; }

.ra-play:focus-visible,
.ra-skip:focus-visible,
.ra-rate:focus-visible,
.ra-scrub:focus-visible {
  outline: 2px solid var(--ra-ink);
  outline-offset: 2px;
}

.ra-meta {
  margin: 0.5rem 0 0;
  font-size: 0.8125rem;
  color: var(--ra-dim);
}

/* ── Narrow ─────────────────────────────────────────────────────────── */

@media (max-width: 34rem) {
  /* The title is the first thing to go: on a phone the bar's job is the
     controls, and the page itself says what is being read. */
  .ra-said-title { display: none; }
  .ra-row { gap: 0.5rem; padding-left: 0.625rem; padding-right: 0.625rem; }
  .ra-skip, .ra-rate { min-width: 2.5rem; }
}

@media (max-width: 22rem) {
  /* Below this the speed control is the one a thumb can live without. */
  .ra-rate { display: none; }
}

/* The highlight.
 *
 * A tint *and* a left border, never the tint alone: WCAG 1.4.1 forbids
 * colour as the only means of conveying information, and a reader with
 * low colour discrimination would otherwise see nothing move. */
.ra-s {
  /* The negative margin pulls back the border AND the padding: 3 + 3, not 3.
     With -3px the rule added three pixels of its own to the left of every
     sentence, which is a three-pixel indent at the start of each paragraph
     and a gap after every full stop that reads as a double space. Visible at
     a glance once rendered side by side against the same prose without the
     rule, and invisible in the source, where the markup has no whitespace
     between one </span> and the next <span>. */
  border-left: 3px solid transparent;
  margin-left: -6px;
  padding-left: 3px;
  /* Transitioning only `background-color` and not `border-color` would make
     the border snap while the tint fades. */
  transition: background-color 180ms ease, border-color 180ms ease;
}

/* The highlight takes the accent too. This is the one surface a reader
   looks at for an hour, and the grey version read as a selection left
   behind rather than as a place in a recording. The left border stays
   because WCAG 1.4.1 forbids colour as the only carrier of information —
   it is the part that survives a reader who cannot separate the tint from
   the page. */
.ra-s.is-reading {
  background: color-mix(
    in srgb, var(--ra-tint, var(--ra-accent, #0D9488)) 42%, transparent);
  border-left-color: var(--ra-accent, #0D9488);
}

/* Clickable to seek. `cursor: pointer` alone would be a lie to a keyboard
   user, so the span is made focusable in the module and gets a focus ring
   here -- the same one the host page uses, via `:focus-visible`. */
.ra-s[tabindex] {
  cursor: pointer;
}

.ra-s[tabindex]:focus-visible {
  outline: 2px solid currentColor;
  outline-offset: 2px;
}

@media (prefers-reduced-motion: reduce) {
  /* The highlight still appears -- it is information, not decoration. What
     goes is the fade, which is motion for its own sake. Auto-scroll is not
     suppressed: the module makes it instant instead, because a jump is not
     animation and removing the scroll would leave a reader hunting for the
     highlight they asked to follow. */
  .ra-s {
    transition: none;
  }

  .ra-play,
  .ra-skip,
  .ra-rate {
    transition: none;
  }
}

@media (forced-colors: active) {
  /* Windows High Contrast replaces `background` and `border-color` with its
     own, so the highlight disappeared exactly where it was needed most: this
     feature exists for readers with low vision, and some of them are in
     forced colours.

     `Mark` / `MarkText` are the system colours for highlighted text, which is
     precisely what this is -- not a selection (`Highlight`), not a link. They
     are set on the element rather than switched off with
     `forced-color-adjust: none`, so the reader's own palette still wins. */
  .ra-s.is-reading {
    background: Mark;
    color: MarkText;
    border-left-color: MarkText;
  }

  .ra-s[tabindex]:focus-visible {
    outline-color: Highlight;
  }

  /* The player in forced colours. `currentColor` collapses to the forced
     text colour, which would leave the filled play button and its glyph the
     same colour -- a solid disc with no symbol in it. The system button
     keywords are the pair that is guaranteed to contrast. */
  /* Forced colours drop background images, and the mask goes with them, so
     the disc would arrive as a plain square of forced colour. Draw the
     circle with a border instead: no symbol, but a control that still reads
     as one and keeps its accessible name. */
  .ra-play {
    background: ButtonFace;
    border: 2px solid ButtonText;
    border-radius: 50%;
  }

  .ra-player {
    border-color: CanvasText;
    background: Canvas;
  }

  .ra-scrub::-webkit-slider-runnable-track {
    background: CanvasText;
  }

  .ra-scrub::-webkit-slider-thumb,
  .ra-scrub::-moz-range-thumb {
    background: Highlight;
  }
}

@media (prefers-contrast: more) {
  .ra-s.is-reading {
    background: color-mix(
      in srgb, var(--ra-tint, var(--ra-accent, #0D9488)) 62%, transparent);
  }
}

/* A browser without `color-mix` gets a highlight that still reads, rather
   than a transparent background and an invisible cursor. */
@supports not (background: color-mix(in srgb, currentColor 12%, transparent)) {
  .ra-s.is-reading {
    background: rgba(13, 148, 136, 0.18);
  }

  .ra-player {
    background: rgba(13, 148, 136, 0.07);
    --ra-line: rgba(13, 148, 136, 0.28);
    --ra-dim: rgba(128, 128, 128, 1);
  }
}
