/* ==========================================================================
   Wiz Chronicles — Ambience (task 3B.3): YouTube background video + music toggle
   Markup: includes/ambience.php · Behaviour: assets/js/ambience.js
   Direction: "Obsidian & Amethyst" (dev/luxury-directions.html#d2)

   Page knobs (set on :root or body of the page that enables $ambience):
     --ambience-offset     distance of the sound toggle from the bottom edge (default 16px).
                           Raise it on pages with a sticky action bar, e.g. 88px.
     --ambience-offset-x   distance from the right edge (default 16px)
     --ambience-overlay    readability overlay drawn above the video/poster
     --ambience-base       base colour under everything (default obsidian #08070C)
     --ambience-ui-z       stacking of the floating toggle (default: just above .action-bar)
     --ambience-ui-bg      surface of the toggle/volume pill (default: dark obsidian glass)
     --ambience-poster-dim extra darkening over the poster while no video plays (default .3)
     --ambience-ui-text/-soft/-muted/-accent/-ink  colours of the floating sound control
   Safe-area insets are always added on top of the offsets.
   ========================================================================== */

:root {
  --ambience-base: #08070C;
  /* floating sound control palette (always on dark glass, independent of page theme) */
  --ambience-ui-text: #F1ECF8;
  --ambience-ui-soft: #DCD3EC;
  --ambience-ui-muted: #B7AEC8;
  --ambience-ui-accent: #B49BFF;
  --ambience-ui-ink: #1D1430;
  --ambience-overlay:
    radial-gradient(40% 40% at 50% 18%, rgba(142, 108, 240, 0.30), transparent 70%),
    radial-gradient(45% 45% at 12% 80%, rgba(42, 26, 74, 0.85), transparent 70%),
    radial-gradient(40% 40% at 92% 60%, rgba(91, 54, 194, 0.28), transparent 70%),
    linear-gradient(180deg, rgba(8, 7, 12, 0.78), rgba(8, 7, 12, 0.88) 50%, var(--ambience-base));
}

/* The layer sits at z-index -1 in the root stacking context: it paints above the canvas
   background but below every in-flow block (.site-main, .topbar, footer). The canvas colour
   comes from <html>; <body> must stay transparent on ambience pages or it would cover the layer. */
html.has-ambience { background-color: var(--ambience-base); }
html.has-ambience body { background: transparent; }

/* ---------- 1. Background layer ---------- */
.ambience {
  position: fixed;
  inset: 0;
  z-index: var(--ambience-z, -1);
  overflow: hidden;
  pointer-events: none;
  background-color: var(--ambience-base);
  contain: strict;
}
.ambience__poster,
.ambience__overlay {
  position: absolute;
  inset: 0;
}
.ambience__poster {
  background-color: var(--ambience-base);
  background-image: var(--ambience-poster, none);
  background-position: center;
  background-size: cover;
  background-repeat: no-repeat;
  /* Poster-only treatment (mobile / reduced-motion / fallback / before the video fades in):
     soft blur + slight zoom so any text baked into a thumbnail is not legible and the blurred
     edges stay off-screen. The live video never gets this. */
  filter: blur(var(--ambience-poster-blur, 6px)) saturate(115%);
  transform: scale(1.1);
}
.ambience__overlay { background: var(--ambience-overlay); }
/* extra darkening over the poster only; fades away once the (sharper) video is live */
.ambience__overlay::after {
  content: "";
  position: absolute;
  inset: 0;
  background: rgba(8, 7, 12, var(--ambience-poster-dim, 0.3));
  transition: opacity 1.6s ease;
}
.ambience.is-live .ambience__overlay::after { opacity: 0; }

/* iframe frame: JS sizes it to "cover" the viewport (+ a small bleed that crops YouTube's
   own title bar / logo); the CSS max() values are the no-JS-yet starting point. */
.ambience__video {
  position: absolute;
  top: 50%;
  left: 50%;
  width: max(100vw, 177.78vh);
  height: max(100vh, 56.25vw);
  transform: translate(-50%, -50%);
  opacity: 0;
  visibility: hidden;
  transition: opacity 1.6s ease, visibility 0s linear 1.6s;
}
.ambience__video iframe {
  display: block;
  width: 100%;
  height: 100%;
  border: 0;
  pointer-events: none;
}
.ambience.is-live .ambience__video {
  opacity: 1;
  visibility: visible;
  transition: opacity 1.6s ease, visibility 0s;
}

/* Audio-only player (mobile / reduced-motion / Save-Data): invisible but still in the DOM.
   200x200 is deliberate: YouTube requires a >= 200px player viewport and, in testing, a 1x1
   player never left the UNSTARTED state. It sits under the whole page (layer z-index -1),
   fully transparent and click-through, so it is never seen or hit. */
.ambience__audio {
  position: absolute;
  left: 0;
  bottom: 0;
  width: 200px;
  height: 200px;
  overflow: hidden;
  opacity: 0;
  pointer-events: none;
}
.ambience__audio iframe { display: block; width: 200px; height: 200px; border: 0; pointer-events: none; }

/* ---------- 2. Floating sound toggle ---------- */
.ambience-ui {
  position: fixed;
  right: calc(var(--ambience-offset-x, 16px) + env(safe-area-inset-right, 0px));
  bottom: calc(var(--ambience-offset, 16px) + env(safe-area-inset-bottom, 0px));
  z-index: var(--ambience-ui-z, calc(var(--z-sticky, 100) + 1));
  display: flex;
  flex-direction: column;
  align-items: flex-end;
  gap: 10px;
  max-width: calc(100vw - 2 * var(--ambience-offset-x, 16px));
  font-family: var(--font-sans, "Noto Sans Thai", system-ui, sans-serif);
  color: var(--ambience-ui-text);
  pointer-events: none;              /* only the controls themselves catch clicks */
  transition: bottom 0.25s ease;
}
.ambience-ui > * { pointer-events: auto; }

.ambience-ui__bar {
  display: inline-flex;
  align-items: center;
  min-height: 44px;
  padding: 3px;
  border: 1px solid var(--hairline-strong, rgba(203, 179, 122, 0.45));
  border-radius: 999px;
  background: var(--ambience-ui-bg, var(--glass-bar, rgba(12, 10, 18, 0.96)));
  box-shadow: 0 10px 30px -14px rgba(0, 0, 0, 0.8), inset 0 1px 0 rgba(142, 108, 240, 0.18);
  transition: border-color 0.25s ease, background-color 0.25s ease;
}
/* Always a DARK obsidian glass, whatever is behind it (video, poster or the light pearl scroll):
   >= 0.84 obsidian keeps #F1ECF8 text above 10:1 even over pure white. */
@supports ((backdrop-filter: blur(1px)) or (-webkit-backdrop-filter: blur(1px))) {
  .ambience-ui__bar {
    background: var(--ambience-ui-bg, var(--glass-bar-blur, rgba(12, 10, 18, 0.84)));
    -webkit-backdrop-filter: var(--glass-blur, blur(16px) saturate(140%));
    backdrop-filter: var(--glass-blur, blur(16px) saturate(140%));
  }
}
.ambience-ui__bar:hover { border-color: rgba(203, 179, 122, 0.7); }
.ambience-ui[data-state="pending"] .ambience-ui__bar { border-color: rgba(203, 179, 122, 0.75); }
.ambience-ui[data-state="pending"] .ambience-eq { opacity: 0.55; }

.ambience-toggle,
.ambience-volbtn {
  -webkit-appearance: none;
  appearance: none;
  margin: 0;
  border: 0;
  background: transparent;
  color: inherit;
  font: inherit;
  cursor: pointer;
  -webkit-tap-highlight-color: transparent;
  touch-action: manipulation;
}
.ambience-toggle {
  display: inline-flex;
  align-items: center;
  gap: 10px;
  min-height: 38px;
  padding: 0 14px 0 3px;
  border-radius: 999px;
  font-size: 14px;
  font-weight: 400;
  line-height: 1;
  white-space: nowrap;
}
.ambience-toggle__icon {
  display: grid;
  place-items: center;
  flex: 0 0 auto;
  width: 34px;
  height: 34px;
  border-radius: 50%;
  background: linear-gradient(140deg, var(--ambience-ui-accent), var(--amethyst-deep, #5B36C2));
  color: var(--on-primary, #fff);
  box-shadow: inset 0 0 0 1px rgba(203, 179, 122, 0.55);
}
.ambience-ico,
.ambience-volbtn svg,
.ambience-ui__vol-icon {
  width: 18px;
  height: 18px;
  max-width: none;
  fill: none;
  stroke: currentColor;
  stroke-width: 2;
  stroke-linecap: round;
  stroke-linejoin: round;
}
.ambience-ico--on { display: none; }
.ambience-toggle[aria-pressed="true"] .ambience-ico--on { display: block; }
.ambience-toggle[aria-pressed="true"] .ambience-ico--off { display: none; }

.ambience-volbtn {
  display: grid;
  place-items: center;
  width: 38px;
  height: 38px;
  margin-left: 2px;
  border-radius: 50%;
  color: var(--ambience-ui-soft);
  position: relative;
}
.ambience-volbtn::before {           /* hairline divider between the two controls */
  content: "";
  position: absolute;
  left: -2px;
  top: 9px;
  bottom: 9px;
  width: 1px;
  background: rgba(255, 255, 255, 0.16);
}
/* 44px touch targets without growing the visual pill */
.ambience-toggle,
.ambience-volbtn { position: relative; }
.ambience-toggle::after,
.ambience-volbtn::after {
  content: "";
  position: absolute;
  inset: -3px;
}
/* Touch devices and small screens: the controls themselves become >= 44x44 (QA M2).
   The pill only grows upward from --ambience-offset, so it never reaches down over the
   apply page's sticky action bar; the dark obsidian glass and colours are unchanged. */
@media (pointer: coarse), (max-width: 47.99rem) {
  .ambience-ui__bar { padding: 2px; }
  .ambience-toggle {
    min-height: 44px;
    padding: 0 16px 0 3px;
  }
  .ambience-toggle__icon {
    width: 38px;
    height: 38px;
  }
  .ambience-volbtn {
    width: 44px;
    height: 44px;
  }
  .ambience-volbtn::before { top: 11px; bottom: 11px; }
  .ambience-ico,
  .ambience-volbtn svg { width: 20px; height: 20px; }
}
.ambience-toggle:hover,
.ambience-volbtn:hover,
.ambience-volbtn[aria-expanded="true"] { background: rgba(255, 255, 255, 0.08); }
.ambience-toggle:focus-visible,
.ambience-volbtn:focus-visible,
.ambience-ui__range:focus-visible {
  outline: 2px solid var(--ambience-ui-accent);
  outline-offset: 2px;
}

/* equalizer (d2) */
.ambience-eq {
  display: inline-flex;
  align-items: flex-end;
  gap: 2px;
  width: 0;
  height: 12px;
  overflow: hidden;
  transition: width 0.25s ease;
}
.ambience-eq i {
  display: block;
  flex: 0 0 auto;
  width: 2px;
  height: 4px;
  border-radius: 1px;
  background: currentColor;
}
.ambience-toggle[aria-pressed="true"] .ambience-eq { width: 12px; }
.ambience-ui.is-playing .ambience-eq i { animation: ambience-eq 1s ease-in-out infinite; }
.ambience-ui.is-playing .ambience-eq i:nth-child(2) { animation-delay: -0.35s; }
.ambience-ui.is-playing .ambience-eq i:nth-child(3) { animation-delay: -0.7s; }
@keyframes ambience-eq { 0%, 100% { height: 3px; } 50% { height: 12px; } }

/* volume popover */
.ambience-ui__vol {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 8px 14px;
  border: 1px solid var(--hairline-strong, rgba(203, 179, 122, 0.45));
  border-radius: 999px;
  background: var(--ambience-ui-bg, var(--glass-bar, rgba(12, 10, 18, 0.96)));   /* solid: sits right over the slider's track */
  box-shadow: 0 10px 30px -14px rgba(0, 0, 0, 0.8);
  color: var(--ambience-ui-soft);
}
.ambience-ui__vol[hidden] { display: none; }
.ambience-ui__range {
  width: min(160px, 42vw);
  height: 28px;
  margin: 0;
  accent-color: var(--amethyst, #8E6CF0);
  cursor: pointer;
  background: transparent;
}
.ambience-ui__vol-val {
  min-width: 2.2em;
  font-size: 13px;
  font-variant-numeric: tabular-nums;
  text-align: right;
  color: var(--ambience-ui-muted);
}

/* "music starts when you tap" hint bubble (pending state, once per session) */
.ambience-ui__hint {
  position: relative;
  margin-right: 8px;
  padding: 8px 12px;
  border-radius: 10px;
  background: var(--ambience-ui-text);
  color: var(--ambience-ui-ink);
  font-size: 13px;
  line-height: 1.3;
  white-space: nowrap;
  box-shadow: 0 8px 24px -10px rgba(142, 108, 240, 0.7);
  opacity: 0;
  visibility: hidden;
  transform: translateY(6px);
  transition: opacity 0.35s ease, transform 0.35s ease, visibility 0s linear 0.35s;
  pointer-events: none;
}
.ambience-ui__hint::after {
  content: "";
  position: absolute;
  right: 22px;
  bottom: -5px;
  width: 10px;
  height: 10px;
  background: inherit;
  transform: rotate(45deg);
}
.ambience-ui.has-hint .ambience-ui__hint {
  opacity: 1;
  visibility: visible;
  transform: none;
  transition: opacity 0.35s ease, transform 0.35s ease, visibility 0s;
}
/* hint and volume popover never stack on top of each other */
.ambience-ui:not(.has-hint) .ambience-ui__hint { position: absolute; bottom: 100%; right: 0; }

/* ---------- 3. Preferences ---------- */
@media (prefers-reduced-motion: reduce) {
  .ambience__video,
  .ambience-ui,
  .ambience-ui__hint,
  .ambience-eq { transition: none; }
  .ambience-ui.is-playing .ambience-eq i { animation: none; }
  .ambience-toggle[aria-pressed="true"] .ambience-eq i:nth-child(1) { height: 6px; }
  .ambience-toggle[aria-pressed="true"] .ambience-eq i:nth-child(2) { height: 11px; }
  .ambience-toggle[aria-pressed="true"] .ambience-eq i:nth-child(3) { height: 8px; }
}
.ambience-ui.is-static-motion .ambience-eq i { animation: none !important; }
.ambience-ui.is-static-motion .ambience-toggle[aria-pressed="true"] .ambience-eq i:nth-child(1) { height: 6px; }
.ambience-ui.is-static-motion .ambience-toggle[aria-pressed="true"] .ambience-eq i:nth-child(2) { height: 11px; }
.ambience-ui.is-static-motion .ambience-toggle[aria-pressed="true"] .ambience-eq i:nth-child(3) { height: 8px; }

@media (forced-colors: active) {
  .ambience-ui__bar,
  .ambience-ui__vol { border-color: CanvasText; }
  .ambience-toggle__icon { forced-color-adjust: none; }
}

@media print {
  .ambience,
  .ambience-ui { display: none !important; }
}
