/*
 * Persistent audio player — visual layer (python-podcast staging proof).
 *
 * Loaded ONLY when PYTHON_PODCAST_PERSISTENT_AUDIO_PLAYER is on (the base
 * templates gate the <link> behind the same flag as the dock region + manager),
 * so production — which never enables the flag — is completely unaffected. This
 * file is presentation only: it adds zero behaviour and touches no django-cast
 * internals. It restyles the light-DOM the django-cast custom player renders
 * (`.cast-player__*`) from the outside and styles the python-podcast-owned dock
 * chrome + play card.
 *
 * Colours come from the same `--cast-player-*` tokens the player already uses
 * (declared per-theme in site-overrides.css), so the dock matches the player and
 * adapts to light/dark automatically. A few dock-only knobs are added under the
 * `--cast-dock-*` namespace.
 */

:root {
  --cast-dock-radius: 1.1rem;
  --cast-dock-gap: 0.9rem;
  --cast-dock-max: 46rem; /* centred floating width on wide viewports */
}

/* Derived colour tokens are declared on the subtree roots (NOT :root) so the
   var(--cast-player-*) they reference resolve against the inherited per-theme
   token values at this element. Declaring them on :root would substitute the
   light-theme values once at :root and inherit those everywhere — breaking dark
   mode (light text on a light dock). */
.cast-persistent,
.cast-play-card {
  --cast-dock-surface: var(--cast-player-bg, Canvas);
  --cast-dock-line: color-mix(in srgb, var(--cast-player-fg, CanvasText) 12%, transparent);
  --cast-dock-shadow: 0 -1px 0 color-mix(in srgb, var(--cast-player-fg, #111) 8%, transparent),
    0 12px 40px -12px color-mix(in srgb, #000 55%, transparent),
    0 4px 14px -8px color-mix(in srgb, #000 40%, transparent);
}

/* Reserve real space so the fixed dock never hides content. --cast-dock-height
   tracks the dock's actual rendered height (the manager keeps it current with a
   ResizeObserver), so even an expanded transcript/chapters sheet (~42vh) leaves
   the last paragraphs, footer, and pagination reachable below the content. */
body.cast-dock-open {
  padding-bottom: calc(var(--cast-dock-height, 8.5rem) + 1.5rem);
  scroll-padding-bottom: calc(var(--cast-dock-height, 8.5rem) + 1.5rem);
}

/* ==========================================================================
   Episode play card — the in-article affordance that starts an episode and
   mirrors the dock's playback state for its own episode. One cohesive surface
   (no pill-in-a-box): poster, circular play control, label + duration.
   data-cast-state (set by the manager) flips it into the now-playing proxy:
   pause glyph, live elapsed/total readout, equalizer badge on the poster,
   accent border. The button's hit area stretches over the whole card.
   ========================================================================== */

.cast-play-card {
  position: relative; /* anchor for the stretched button hit area */
  display: flex;
  align-items: center;
  gap: 0.9rem;
  margin: 1.25rem 0;
  padding: 0.7rem 0.95rem;
  border: 1px solid var(--cast-dock-line);
  border-radius: 1rem;
  background: color-mix(in srgb, var(--cast-player-accent, #2d8260) 4%, var(--cast-dock-surface));
  max-width: 30rem;
  transition: border-color 0.16s ease, background-color 0.16s ease, box-shadow 0.18s ease;
}

.cast-play-card:hover {
  border-color: color-mix(in srgb, var(--cast-player-accent, #2d8260) 45%, transparent);
  box-shadow: 0 4px 18px -8px color-mix(in srgb, var(--cast-player-accent, #2d8260) 45%, transparent);
}

.cast-play-card[data-cast-state] {
  border-color: color-mix(in srgb, var(--cast-player-accent, #2d8260) 55%, transparent);
  background: color-mix(in srgb, var(--cast-player-accent, #2d8260) 9%, var(--cast-dock-surface));
}

/* On overview/list cards the affordance is more compact. */
.cast-play-card--compact {
  margin: 0.5rem 0 0;
  padding: 0.5rem 0.7rem;
  gap: 0.7rem;
}

.cast-play-card__posterwrap {
  position: relative;
  flex: none;
  display: block;
}

.cast-play-card__poster {
  display: block;
  width: 3.4rem;
  height: 3.4rem;
  border-radius: 0.7rem;
  object-fit: cover;
  background: var(--cast-player-progress-track, #e5e7eb);
}

.cast-play-card--compact .cast-play-card__poster {
  width: 2.9rem;
  height: 2.9rem;
}

/* Equalizer badge: pulses over the poster while this episode is playing. */
.cast-play-card__eq {
  position: absolute;
  inset: 0;
  display: none;
  align-items: flex-end;
  justify-content: center;
  gap: 3px;
  padding-bottom: 0.55rem;
  border-radius: 0.7rem;
  background: color-mix(in srgb, #000 45%, transparent);
}

.cast-play-card[data-cast-state="playing"] .cast-play-card__eq {
  display: flex;
}

.cast-play-card__eq > i {
  width: 3px;
  height: 30%;
  border-radius: 2px;
  background: #fff;
}

@media (prefers-reduced-motion: no-preference) {
  /* Per-bar durations + delays keep the pulse organic instead of metronomic. */
  .cast-play-card[data-cast-state="playing"] .cast-play-card__eq > i {
    animation: cast-eq 0.9s ease-in-out infinite;
  }

  .cast-play-card[data-cast-state="playing"] .cast-play-card__eq > i:nth-child(2) {
    animation-duration: 1.15s;
    animation-delay: 0.2s;
  }

  .cast-play-card[data-cast-state="playing"] .cast-play-card__eq > i:nth-child(3) {
    animation-duration: 0.75s;
    animation-delay: 0.35s;
  }
}

@keyframes cast-eq {
  0%,
  100% {
    height: 25%;
  }

  50% {
    height: 65%;
  }
}

/* The button IS the card's single action (the manager wires [data-cast-play]).
   Visually it is the circular glyph + text; its hit area covers the card. */
.cast-play-card__btn {
  display: flex;
  align-items: center;
  gap: 0.8rem;
  min-width: 0;
  padding: 0;
  border: 0;
  background: none;
  color: inherit;
  font: inherit;
  text-align: left;
  cursor: pointer;
}

.cast-play-card__btn::after {
  content: "";
  position: absolute;
  inset: 0;
  border-radius: 1rem;
}

.cast-play-card__btn:focus-visible {
  outline: none;
}

.cast-play-card:has(.cast-play-card__btn:focus-visible) {
  outline: 3px solid var(--cast-player-focus, var(--cast-player-accent, #2d8260));
  outline-offset: 2px;
}

/* The circular accent play/pause control. */
.cast-play-card__glyph {
  flex: none;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 2.75rem;
  height: 2.75rem;
  border-radius: 50%;
  background: var(--cast-player-accent, #2d8260);
  color: var(--cast-player-on-accent, #fff);
  box-shadow: 0 2px 10px color-mix(in srgb, var(--cast-player-accent, #2d8260) 45%, transparent);
  transition: transform 0.14s cubic-bezier(0.34, 1.56, 0.64, 1), box-shadow 0.18s ease;
}

.cast-play-card--compact .cast-play-card__glyph {
  width: 2.4rem;
  height: 2.4rem;
}

.cast-play-card:hover .cast-play-card__glyph {
  transform: scale(1.06);
  box-shadow: 0 4px 16px color-mix(in srgb, var(--cast-player-accent, #2d8260) 55%, transparent);
}

.cast-play-card__btn:active .cast-play-card__glyph {
  transform: scale(0.94);
}

.cast-play-card__icon--play {
  transform: translateX(1px); /* optically centre the play triangle */
}

.cast-play-card__icon--pause {
  display: none;
}

.cast-play-card[data-cast-state="playing"] .cast-play-card__icon--play {
  display: none;
}

.cast-play-card[data-cast-state="playing"] .cast-play-card__icon--pause {
  display: block;
}

.cast-play-card__text {
  display: flex;
  flex-direction: column;
  gap: 0.15rem;
  min-width: 0;
}

.cast-play-card__label {
  font-weight: 650;
  line-height: 1.2;
}

.cast-play-card__meta {
  display: flex;
  align-items: baseline;
  gap: 0.4rem;
  font-size: 0.85em;
  font-weight: 500;
  color: var(--cast-player-muted, #6b7280);
  font-variant-numeric: tabular-nums;
  white-space: nowrap;
}

/* While the card mirrors the dock, the static duration yields to the live
   elapsed/total readout the manager writes into __time. */
.cast-play-card[data-cast-state] .cast-play-card__dur {
  display: none;
}

/* ==========================================================================
   The dock — single live player, lifted OUT of document flow and fixed to the
   bottom edge (this is the fix for "the player renders below the pagination":
   the region used to sit in normal flow at the very end of the page).
   ========================================================================== */

.cast-persistent {
  position: fixed;
  left: 0;
  right: 0;
  bottom: 0;
  z-index: 1030; /* above Bootstrap sticky navbar (1020), below modals */
  /* Neutralise Bootstrap's `.container` (max-width + auto margins) on the bs5
     region: the fixed positioner spans full width; the inner card is centred. */
  max-width: none;
  margin: 0;
  padding: 0;
  pointer-events: none; /* only the inner card is interactive */
}

.cast-persistent[hidden] {
  display: none;
}

/* The centred floating card. */
.cast-dock__inner {
  pointer-events: auto;
  box-sizing: border-box;
  width: 100%;
  max-width: var(--cast-dock-max);
  margin: 0 auto;
  padding: 0.7rem clamp(0.8rem, 3vw, 1.1rem) calc(0.7rem + env(safe-area-inset-bottom, 0px));
  background: color-mix(in srgb, var(--cast-dock-surface) 92%, transparent);
  -webkit-backdrop-filter: saturate(140%) blur(14px);
  backdrop-filter: saturate(140%) blur(14px);
  border: 1px solid var(--cast-dock-line);
  border-bottom: 0;
  border-radius: var(--cast-dock-radius) var(--cast-dock-radius) 0 0;
  box-shadow: var(--cast-dock-shadow);
  color: var(--cast-player-fg, CanvasText);
}

@media (min-width: 48rem) {
  /* Float it as a rounded pill clear of the bottom edge on larger screens. */
  .cast-dock__inner {
    margin-bottom: 0.9rem;
    border-bottom: 1px solid var(--cast-dock-line);
    border-radius: var(--cast-dock-radius);
  }
}

/* Header row: poster + title/subtitle + close. */
.cast-dock__header {
  display: flex;
  align-items: center;
  gap: var(--cast-dock-gap);
  margin-bottom: 0.4rem;
}

.cast-dock__poster {
  flex: none;
  width: 2.9rem;
  height: 2.9rem;
  border-radius: 0.55rem;
  object-fit: cover;
  background: var(--cast-player-progress-track, #e5e7eb);
  box-shadow: 0 1px 5px color-mix(in srgb, #000 22%, transparent);
}

.cast-dock__meta {
  flex: 1 1 auto;
  min-width: 0;
  display: flex;
  flex-direction: column;
  gap: 0.1rem;
}

.cast-dock__title {
  font-weight: 650;
  font-size: 0.95rem;
  line-height: 1.25;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.cast-dock__subtitle {
  font-size: 0.8rem;
  color: var(--cast-player-muted, #6b7280);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.cast-dock__close,
.cast-dock__minify {
  flex: none;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 2rem;
  height: 2rem;
  border: 0;
  border-radius: 50%;
  background: transparent;
  color: var(--cast-player-muted, #6b7280);
  cursor: pointer;
  transition: background-color 0.15s ease, color 0.15s ease;
}

.cast-dock__close:hover,
.cast-dock__minify:hover {
  background: color-mix(in srgb, var(--cast-player-fg, CanvasText) 10%, transparent);
  color: var(--cast-player-fg, CanvasText);
}

.cast-dock__close:focus-visible,
.cast-dock__minify:focus-visible {
  outline: 2px solid var(--cast-player-focus, var(--cast-player-accent, #2d8260));
  outline-offset: 2px;
}

.cast-dock__minify > svg {
  transition: transform 0.2s ease;
}

.cast-persistent[data-cast-min] .cast-dock__minify > svg {
  transform: rotate(180deg);
}

/* ==========================================================================
   Minimized dock — a one-row strip: poster · title · play · elapsed · ⌃ · ✕.
   `display: contents` folds the header into the inner's flex row so the
   transport can sit between the title and the window buttons; everything not
   needed for "keep listening" (subtitle, seek, share, shortcuts, panels) is
   hidden. Panel open/closed state is preserved — expanding shows it again.
   ========================================================================== */

.cast-persistent[data-cast-min] .cast-dock__inner {
  display: flex;
  flex-direction: row;
  align-items: center;
  gap: 0.65rem;
  max-width: 30rem;
  padding-top: 0.45rem;
  padding-bottom: calc(0.45rem + env(safe-area-inset-bottom, 0px));
}

.cast-persistent[data-cast-min] .cast-dock__header {
  display: contents;
}

.cast-persistent[data-cast-min] .cast-dock__poster {
  order: 1;
  width: 2.3rem;
  height: 2.3rem;
}

.cast-persistent[data-cast-min] .cast-dock__meta {
  order: 2;
  flex: 1 1 auto;
  min-width: 0;
}

.cast-persistent[data-cast-min] .cast-dock__subtitle {
  display: none;
}

.cast-persistent[data-cast-min] cast-audio-player {
  order: 3;
  flex: none;
}

.cast-persistent[data-cast-min] .cast-dock__minify {
  order: 4;
}

.cast-persistent[data-cast-min] .cast-dock__close {
  order: 5;
}

.cast-persistent[data-cast-min] .cast-player__seek,
.cast-persistent[data-cast-min] .cast-player__remaining,
.cast-persistent[data-cast-min] .cast-player__share,
.cast-persistent[data-cast-min] .cast-player__shortcuts-btn,
.cast-persistent[data-cast-min] .cast-player__panels {
  display: none;
}

.cast-persistent[data-cast-min] .cast-player__transport {
  gap: 0.5rem;
}

@media (prefers-reduced-motion: no-preference) {
  .cast-dock__inner {
    transition: max-width 0.25s ease, padding 0.25s ease;
  }
}

/* Blend the player's own pill transport into the dock surface so it does not
   read as a box-in-a-box. The accent play button stays as-is. */
.cast-persistent .cast-player__transport {
  background: transparent;
  border: 0;
  padding: 0.15rem 0;
  min-height: 0;
}

/* Keep the transcript/chapters panels usable but scrollable inside the dock. */
.cast-persistent .cast-player__panels {
  margin-top: 0.35rem;
}

.cast-persistent .cast-panel__body {
  max-height: min(42vh, 22rem);
  overflow-y: auto;
}

/* ==========================================================================
   Motion — dock entrance + the View-Transitions poster morph.
   ========================================================================== */

@media (prefers-reduced-motion: no-preference) {
  /* Non-VT browsers get a gentle rise. Triggered by a one-shot class the manager
     adds ONLY on the non-View-Transition first open — never tied to
     `data-cast-active`, so removing `.cast-vt-active` after a View Transition can
     not re-fire it, and an in-place switch does not re-rise. The sink rule is
     listed after it so on the close tie (equal specificity) the sink wins. */
  .cast-persistent .cast-dock__inner.cast-rise {
    animation: cast-dock-rise 0.32s cubic-bezier(0.22, 0.61, 0.36, 1) both;
  }

  .cast-persistent[data-cast-closing] .cast-dock__inner {
    animation: cast-dock-sink 0.22s ease-in both;
  }
}

@keyframes cast-dock-rise {
  from {
    opacity: 0;
    transform: translateY(28px);
  }
  to {
    opacity: 1;
    transform: translateY(0);
  }
}

@keyframes cast-dock-sink {
  from {
    opacity: 1;
    transform: translateY(0);
  }
  to {
    opacity: 0;
    transform: translateY(28px);
  }
}

/* View Transitions: the pressed play card morphs into the dock (the group
   interpolates position + size, so the card visibly *becomes* the player) and
   the poster glides as its own nested pair. cast-vt-dock stays as the entrance
   for the no-card fallback. Names are assigned/cleared by the manager only for
   the duration of a transition, so they never collide between snapshots. */
::view-transition-group(cast-vt-card) {
  animation-duration: 0.45s;
  animation-timing-function: cubic-bezier(0.22, 0.61, 0.36, 1);
}

/* Scale both snapshots to the morphing group box so the card/dock crossfade
   tracks the size interpolation instead of overflowing it. */
::view-transition-old(cast-vt-card),
::view-transition-new(cast-vt-card) {
  height: 100%;
  width: 100%;
}

::view-transition-group(cast-vt-poster) {
  animation-duration: 0.4s;
  animation-timing-function: cubic-bezier(0.22, 0.61, 0.36, 1);
}

::view-transition-new(cast-vt-dock) {
  animation: cast-dock-rise 0.36s cubic-bezier(0.22, 0.61, 0.36, 1) both;
}

@media (prefers-reduced-motion: reduce) {
  /* Belt-and-braces: the manager already skips startViewTransition under reduced
     motion, but pin durations to ~0 if a transition is triggered another way. */
  ::view-transition-group(*),
  ::view-transition-new(cast-vt-dock) {
    animation-duration: 0.01ms !important;
  }
}
