/* =====================================================================
 * Speech self-view body-language coach styles
 * ---------------------------------------------------------------------
 * Mirrors Shadow Interview's HUD treatment (see .kouli-live-hud /
 * .kouli-live-cue-* in shadow-interview.css):
 *   - small uppercase labels (`WPM`, `PAUSE`, `VOICE`, `FRAME`, `COACHING CUE`)
 *   - tabular numbers for the stat values
 *   - italic cue line for qualitative coaching
 *   - whisper-thin glass tint, soft text-shadow for legibility on any
 *     video background
 *
 * Layout safety:
 *   - Top-left STATS chip: clear of every persistent UI (small mode
 *     has SELF VIEW header at top-CENTRE; fullscreen has nothing at
 *     top-left).
 *   - Top-right COACHING CUE chip: pinned ~56px below the corner so
 *     it never collides with the "Full screen" toggle (small mode)
 *     or the "Exit" pill (fullscreen mode), both of which sit at
 *     top: 16px right: 16px.
 *   - Nothing renders bottom-CENTRE — fullscreen places End + Pause
 *     there. Bottom-right (timer in fullscreen) is also kept clear.
 * ===================================================================== */

/* Tile must host an absolutely-positioned overlay regardless of
 * layout mode (small thumbnail or fullscreen). */
.ted-recording-self-view {
  position: relative;
}

.speech-self-view-coach {
  position: absolute;
  inset: 0;
  pointer-events: none;
  z-index: 5;
  border-radius: inherit;
  overflow: hidden;
}

.speech-self-view-coach::after {
  content: '';
  position: absolute;
  inset: 0;
  z-index: 1;
  pointer-events: none;
  border-radius: inherit;
  opacity: 0;
  box-shadow: inset 0 0 0 3px rgba(255, 255, 255, 0);
  transition: opacity 420ms ease, box-shadow 420ms ease;
}

.speech-self-view-coach[data-rim-band='steady']::after {
  opacity: 1;
  box-shadow:
    inset 0 0 0 3px rgba(125, 211, 252, 0.58),
    inset 0 0 34px rgba(125, 211, 252, 0.16);
}

.speech-self-view-coach[data-rim-band='building']::after {
  opacity: 1;
  box-shadow:
    inset 0 0 0 3px rgba(251, 191, 36, 0.56),
    inset 0 0 38px rgba(251, 191, 36, 0.15);
  animation: speech-self-view-rim-breathe 2.8s ease-in-out infinite;
}

.speech-self-view-coach[data-rim-band='drifting']::after {
  opacity: 1;
  box-shadow:
    inset 0 0 0 3px rgba(167, 139, 250, 0.54),
    inset 0 0 40px rgba(167, 139, 250, 0.17);
  animation: speech-self-view-rim-breathe 2.4s ease-in-out infinite;
}

@keyframes speech-self-view-rim-breathe {
  0%, 100% {
    opacity: 0.72;
  }
  50% {
    opacity: 1;
  }
}

.speech-self-view-coach-guide {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  stroke: rgba(255, 255, 255, 0.12);
  stroke-width: 0.4;
  fill: none;
  mix-blend-mode: screen;
  pointer-events: none;
}

/* Shadow-style HUD chip — whisper-thin tint, soft blur, generous
 * text-shadow so it reads on any backdrop. Shared by both STATS and
 * COACHING CUE chips. */
.speech-self-view-coach-hud {
  position: absolute;
  z-index: 2;
  pointer-events: none;
  display: flex;
  flex-direction: column;
  gap: 3px;
  padding: 6px 10px;
  border: 1px solid rgba(255, 255, 255, 0.08);
  border-radius: 12px;
  background: rgba(255, 255, 255, 0.025);
  -webkit-backdrop-filter: blur(10px) saturate(125%);
  backdrop-filter: blur(10px) saturate(125%);
  box-shadow: 0 1px 8px rgba(0, 0, 0, 0.18);
  color: rgba(255, 255, 255, 0.92);
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
  text-shadow:
    0 1px 2px rgba(0, 0, 0, 0.55),
    0 0 6px rgba(0, 0, 0, 0.35);
  opacity: 0;
  transform: translateY(-4px);
  transition: opacity 320ms ease, transform 320ms ease, border-color 200ms ease, background 200ms ease;
}

.speech-self-view-coach-hud.is-visible {
  opacity: 1;
  transform: translateY(0);
}

/* Stat row — label on the left, value on the right. Identical visual
 * grammar as Shadow's .kouli-live-stat. */
.speech-self-view-coach-row {
  display: flex;
  align-items: baseline;
  justify-content: space-between;
  gap: 10px;
  font-size: 0.78rem;
  font-weight: 600;
  letter-spacing: 0.005em;
  color: rgba(255, 255, 255, 0.92);
}

/* The HTML `[hidden]` attribute defaults to `display: none` in user-agent
 * styles, but the rule above explicitly sets `display: flex`, which wins.
 * We re-establish hidden semantics here so the FRAME row truly disappears
 * on browsers without FaceDetector support. */
.speech-self-view-coach-row[hidden] {
  display: none;
}

.speech-self-view-coach-row.is-drift {
  /* Subtle warm tint on the FRAME row when face awareness reports
   * drift. Doesn't recolour the whole chip — just this row. */
  color: rgba(255, 235, 200, 0.96);
}

.speech-self-view-coach-label {
  font-size: 0.6rem;
  font-weight: 700;
  letter-spacing: 0.16em;
  text-transform: uppercase;
  color: rgba(255, 255, 255, 0.6);
  line-height: 1;
}

.speech-self-view-coach-value {
  font-size: 0.82rem;
  font-weight: 650;
  letter-spacing: -0.005em;
  color: rgba(255, 255, 255, 0.98);
  line-height: 1.05;
  font-variant-numeric: tabular-nums;
  font-feature-settings: 'tnum' 1, 'lnum' 1;
}

.speech-self-view-coach-row--voice {
  align-items: center;
}

.speech-self-view-coach-voice-bars {
  display: inline-flex;
  align-items: flex-end;
  gap: 3px;
  min-width: 34px;
  height: 12px;
}

.speech-self-view-coach-voice-bars span {
  width: 4px;
  height: 7px;
  border-radius: 999px;
  background: rgba(255, 255, 255, 0.24);
  box-shadow: 0 0 0 rgba(125, 211, 252, 0);
  transition: height 180ms ease, background 180ms ease, box-shadow 180ms ease, opacity 180ms ease;
}

.speech-self-view-coach-voice-bars span:nth-child(2) {
  height: 8px;
}

.speech-self-view-coach-voice-bars span:nth-child(3) {
  height: 10px;
}

.speech-self-view-coach-voice-bars span:nth-child(4) {
  height: 11px;
}

.speech-self-view-coach-voice-bars span:nth-child(5) {
  height: 12px;
}

.speech-self-view-coach-voice-bars span.is-active {
  background: rgba(125, 211, 252, 0.92);
  box-shadow: 0 0 8px rgba(125, 211, 252, 0.45);
}

.speech-self-view-coach-voice-bars.is-empty span {
  opacity: 0.58;
}

/* Italic, quietly breathing cue text — same language as Shadow's
 * .kouli-live-cue-text. */
.speech-self-view-coach-cue-text {
  font-size: 0.78rem;
  font-style: italic;
  line-height: 1.22;
  color: rgba(255, 255, 255, 0.94);
  max-width: 14ch;
  white-space: normal;
  word-break: break-word;
}

/* ── Stats chip (top-left) — always visible while the camera is on.
 *    Hosts WPM, PAUSE, VOICE, and (when face awareness is supported) a
 *    FRAME row underneath. */
.speech-self-view-coach-hud--stats {
  top: 12px;
  left: 12px;
  align-items: stretch;
  min-width: 88px;
}

/* ── Coaching cue chip (top-right) — offset down 56px so it clears
 *    the "Full screen" toggle (small mode) and the "Exit" pill
 *    (fullscreen). Right-aligned, narrower, italic body. */
.speech-self-view-coach-hud--cue {
  top: 56px;
  right: 12px;
  align-items: flex-end;
  text-align: right;
  max-width: 60%;
}

/* Engine acoustic tip variant — slightly cooler accent so users can
 * tell at a glance this isn't just a paced reminder. */
.speech-self-view-coach-hud--cue.is-engine-tip {
  border-color: rgba(167, 139, 250, 0.35);
  background: rgba(20, 16, 36, 0.55);
}

.speech-self-view-coach-hud--cue.is-engine-tip .speech-self-view-coach-label {
  color: rgba(199, 178, 255, 0.78);
}

/* Off-frame variant — sticky warm-amber when FaceDetector reports
 * the user has stepped out of frame. Highest priority. */
.speech-self-view-coach-hud--cue.is-off-frame {
  border-color: rgba(255, 200, 130, 0.42);
  background: rgba(38, 22, 6, 0.55);
}

.speech-self-view-coach-hud--cue.is-off-frame .speech-self-view-coach-cue-text {
  color: rgba(255, 235, 200, 0.96);
}

.speech-self-view-coach-hud--cue.is-off-frame .speech-self-view-coach-label {
  color: rgba(255, 220, 180, 0.7);
}

/* Fullscreen / stage mode — keep the same anchors but use safe-area
 * insets and bump everything up a touch in scale. */
.is-stage-fullscreen .speech-self-view-coach-guide {
  stroke: rgba(255, 255, 255, 0.10);
  stroke-width: 0.25;
}

/* Fullscreen — keep the stats chip top-left where the user expects
 * it (matching small-tile placement), but offset down ~56px so it
 * sits on the same horizontal line as the cue chip on the right.
 * Both chips end up below the top row reserved for the Exit pill,
 * out of the user's face but still in the corner where the eye
 * looks for live metrics.
 *
 * Sized noticeably bigger than the small-tile version: in fullscreen
 * the user is often standing further from the screen and the metrics
 * need to read at a glance, not be squinted at. */
.is-stage-fullscreen .speech-self-view-coach-hud--stats {
  /* Moved to the bottom-left so the teleprompter can own the top. */
  top: auto;
  bottom: calc(20px + env(safe-area-inset-bottom, 0px));
  left: calc(16px + env(safe-area-inset-left, 0px));
  padding: 12px 16px;
  gap: 6px;
  min-width: 124px;
  z-index: 11;
}

.is-stage-fullscreen .speech-self-view-coach-hud--cue {
  /* Moved to the bottom-right, sitting just above the timer chip, so
   * the teleprompter can own the top of the stage. */
  top: auto;
  bottom: calc(78px + env(safe-area-inset-bottom, 0px));
  right: calc(16px + env(safe-area-inset-right, 0px));
  padding: 12px 16px;
  max-width: 42%;
  z-index: 11;
}

/* Bigger labels + values in fullscreen so a standing speaker can
 * read the HUD across the room. Roughly 1.4x the small-tile sizes. */
.is-stage-fullscreen .speech-self-view-coach-label {
  font-size: 0.74rem;
  letter-spacing: 0.18em;
}

.is-stage-fullscreen .speech-self-view-coach-value {
  font-size: 1.2rem;
  line-height: 1.1;
}

.is-stage-fullscreen .speech-self-view-coach-row {
  font-size: 0.92rem;
  gap: 14px;
}

.is-stage-fullscreen .speech-self-view-coach-voice-bars {
  min-width: 48px;
  height: 16px;
  gap: 4px;
}

.is-stage-fullscreen .speech-self-view-coach-voice-bars span {
  width: 5px;
  height: 10px;
}

.is-stage-fullscreen .speech-self-view-coach-voice-bars span:nth-child(2) {
  height: 11px;
}

.is-stage-fullscreen .speech-self-view-coach-voice-bars span:nth-child(3) {
  height: 13px;
}

.is-stage-fullscreen .speech-self-view-coach-voice-bars span:nth-child(4) {
  height: 15px;
}

.is-stage-fullscreen .speech-self-view-coach-voice-bars span:nth-child(5) {
  height: 16px;
}

.is-stage-fullscreen .speech-self-view-coach-cue-text {
  font-size: 1rem;
  max-width: 22ch;
  line-height: 1.28;
}

/* Mobile (portrait phones, smaller tile) — keep chips tight and
 * within the surface. Cue chip stays clear of the Full screen
 * button at top-right. */
@media (max-width: 480px) {
  .speech-self-view-coach-hud {
    padding: 5px 8px;
  }
  .speech-self-view-coach-row {
    font-size: 0.7rem;
    gap: 8px;
  }
  .speech-self-view-coach-label {
    font-size: 0.56rem;
    letter-spacing: 0.14em;
  }
  .speech-self-view-coach-value {
    font-size: 0.74rem;
  }
  .speech-self-view-coach-cue-text {
    font-size: 0.72rem;
  }
  .speech-self-view-coach-hud--stats {
    min-width: 76px;
  }
  .speech-self-view-coach-hud--cue {
    top: 50px;
    max-width: 56%;
  }
}

/* Reduced motion — JS already shows a single static prompt. Disable
 * the transition so absolutely nothing animates. */
@media (prefers-reduced-motion: reduce) {
  .speech-self-view-coach-hud {
    transition: none;
  }
  .speech-self-view-coach::after,
  .speech-self-view-coach[data-rim-band='building']::after,
  .speech-self-view-coach[data-rim-band='drifting']::after {
    animation: none;
    transition: none;
  }
  .speech-self-view-coach-voice-bars span {
    transition: none;
  }
}

/* Backdrop-filter fallback — denser solid scrim so labels stay
 * legible on browsers without backdrop-filter (older Firefox, some
 * Android WebViews). */
@supports not ((backdrop-filter: blur(1px)) or (-webkit-backdrop-filter: blur(1px))) {
  .speech-self-view-coach-hud {
    background: rgba(15, 15, 25, 0.72);
  }
  .speech-self-view-coach-hud--cue.is-engine-tip {
    background: rgba(20, 16, 36, 0.82);
  }
  .speech-self-view-coach-hud--cue.is-off-frame {
    background: rgba(38, 22, 6, 0.82);
  }
}

/* =====================================================================
 * Speech teleprompter
 * ---------------------------------------------------------------------
 * Card mode mounts into #ted-recording-mist-stage (normal self-view or
 * camera off). HUD mode mounts into #ted-recording-self-view-tile when
 * the self-view is expanded. The overlay is text-only and touch-draggable:
 * drag down to rewind, drag up to move forward.
 * ===================================================================== */
.speech-teleprompter {
  position: absolute;
  z-index: 8;
  pointer-events: none;
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
  color: rgba(255, 255, 255, 0.94);
}

.speech-teleprompter-surface {
  pointer-events: auto;
  touch-action: none;
  -webkit-user-select: none;
  user-select: none;
  border: 1px solid rgba(255, 255, 255, 0.12);
  background:
    linear-gradient(180deg, rgba(8, 8, 18, 0.5), rgba(8, 8, 18, 0.28));
  -webkit-backdrop-filter: blur(16px) saturate(145%);
  backdrop-filter: blur(16px) saturate(145%);
  box-shadow:
    inset 0 1px 0 rgba(255, 255, 255, 0.06),
    0 16px 34px rgba(0, 0, 0, 0.28);
}

.speech-teleprompter-line {
  margin: 0;
  text-align: center;
  text-wrap: balance;
  text-shadow:
    0 1px 2px rgba(0, 0, 0, 0.65),
    0 0 8px rgba(0, 0, 0, 0.45);
}

.speech-teleprompter-line--prev,
.speech-teleprompter-line--next {
  color: rgba(255, 255, 255, 0.42);
}

.speech-teleprompter-line--beat {
  min-height: 1.35em;
  margin: 2px 0 4px;
  color: rgba(255, 255, 255, 0.42);
}

.speech-teleprompter-line--beat[hidden] {
  display: none !important;
}

.speech-teleprompter-line--current.has-pause-ahead {
  padding-bottom: 2px;
}

.speech-teleprompter-line--beat.is-pause-lookahead .speech-teleprompter-pause--lookahead {
  color: rgba(184, 205, 216, 0.72);
  font-size: 0.78rem;
  letter-spacing: 0.04em;
  text-transform: uppercase;
}

.speech-teleprompter-line--current {
  color: rgba(255, 255, 255, 0.96);
  font-weight: 720;
  letter-spacing: -0.015em;
}

.speech-teleprompter-word {
  color: rgba(255, 255, 255, 0.62);
  transition: color 140ms ease, text-shadow 140ms ease;
}

.speech-teleprompter-word.is-read {
  color: rgba(255, 255, 255, 1);
  text-shadow:
    0 0 12px rgba(122, 153, 171, 0.34),
    0 1px 2px rgba(0, 0, 0, 0.65);
}

.speech-teleprompter-word--emphasis-strong {
  font-weight: 760;
  color: rgba(184, 205, 216, 0.96);
}

.speech-teleprompter-word--emphasis-soft {
  text-decoration: underline;
  text-decoration-color: rgba(184, 205, 216, 0.55);
  text-underline-offset: 3px;
}

.speech-teleprompter-word.is-read.speech-teleprompter-word--emphasis-strong,
.speech-teleprompter-word.is-read.speech-teleprompter-word--emphasis-soft {
  color: rgba(255, 255, 255, 1);
}

.speech-teleprompter-pause {
  display: inline-block;
  margin-left: 0;
  padding: 0.12em 0.55em;
  border-radius: 4px;
  border: 1px solid rgba(255, 255, 255, 0.16);
  color: #7A99AB;
  font-size: 0.62rem;
  font-weight: 700;
  letter-spacing: 0.08em;
  text-transform: uppercase;
  vertical-align: middle;
  transition: opacity 160ms ease, border-color 160ms ease, color 160ms ease, box-shadow 160ms ease;
}

.speech-teleprompter-pause--upcoming {
  opacity: 0.42;
}

.speech-teleprompter-pause--approaching {
  opacity: 0.88;
  border-color: rgba(255, 255, 255, 0.28);
  color: #9BB4C4;
}

.speech-teleprompter-pause--dwell {
  opacity: 1;
  border-color: rgba(184, 205, 216, 0.42);
  color: #B8CDD8;
  box-shadow: 0 0 0 1px rgba(184, 205, 216, 0.12);
}

.speech-teleprompter-line--beat.is-pause-approaching .speech-teleprompter-pause {
  transform: translateY(-1px);
}

.speech-teleprompter-line--beat.is-pause-dwell .speech-teleprompter-pause {
  animation: speech-teleprompter-pause-hold 1.1s ease-in-out infinite;
}

@keyframes speech-teleprompter-pause-hold {
  0%, 100% { opacity: 1; }
  50% { opacity: 0.72; }
}

.speech-teleprompter-line--current.is-pause-dwell {
  opacity: 0.88;
}

.speech-teleprompter-hint {
  display: block;
  margin-top: 0;
  text-align: center;
  font-size: 0.64rem;
  font-weight: 700;
  letter-spacing: 0.16em;
  text-transform: uppercase;
  color: rgba(255, 255, 255, 0.34);
}

.speech-teleprompter-footer {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  margin-top: 8px;
}

.speech-teleprompter-footer .speech-teleprompter-hint {
  flex: 1 1 auto;
  text-align: left;
}

.speech-teleprompter-speed {
  display: inline-flex;
  align-items: center;
  gap: 4px;
  flex: 0 0 auto;
}

.speech-teleprompter-speed-btn {
  appearance: none;
  -webkit-appearance: none;
  width: 26px;
  height: 26px;
  padding: 0;
  border-radius: 8px;
  border: 1px solid rgba(255, 255, 255, 0.14);
  background: rgba(255, 255, 255, 0.06);
  color: rgba(255, 255, 255, 0.82);
  font-size: 1rem;
  font-weight: 650;
  line-height: 1;
  cursor: pointer;
  -webkit-tap-highlight-color: transparent;
  transition: background 140ms ease, border-color 140ms ease, opacity 140ms ease;
}

.speech-teleprompter-speed-btn:hover:not(:disabled),
.speech-teleprompter-speed-btn:focus-visible {
  background: rgba(255, 255, 255, 0.12);
  border-color: rgba(255, 255, 255, 0.22);
  outline: none;
}

.speech-teleprompter-speed-btn:active:not(:disabled) {
  transform: scale(0.94);
}

.speech-teleprompter-speed-btn:disabled {
  opacity: 0.32;
  cursor: default;
}

.speech-teleprompter.is-scrubbing .speech-teleprompter-surface {
  border-color: rgba(122, 153, 171, 0.34);
  background:
    linear-gradient(180deg, rgba(22, 32, 42, 0.62), rgba(8, 8, 18, 0.38));
}

/* Normal self-view / camera-off: semi-transparent panel inside the top
 * mist card. The self-view tile remains separate below. */
.speech-teleprompter--card {
  left: 50%;
  top: 50%;
  width: min(92%, 520px);
  transform: translate(-50%, -50%);
}

.speech-teleprompter--card .speech-teleprompter-surface {
  padding: clamp(14px, 3vw, 22px);
  border-radius: 22px;
}

.speech-teleprompter--card .speech-teleprompter-line--current {
  font-size: clamp(1rem, 3.4vw, 1.34rem);
  line-height: 1.24;
}

.speech-teleprompter--card .speech-teleprompter-line--prev,
.speech-teleprompter--card .speech-teleprompter-line--next {
  font-size: clamp(0.78rem, 2.4vw, 0.96rem);
  line-height: 1.22;
}

.speech-teleprompter--card .speech-teleprompter-line--prev {
  margin-bottom: 8px;
}

.speech-teleprompter--card .speech-teleprompter-line--next {
  margin-top: 8px;
}

/* Full-screen self-view: compact HUD band in the lower third, above End
 * and clear of the stats/cue/timer corners. */
.speech-teleprompter--hud {
  /* Top of the stage, just below the Exit pill row. The metrics/cue
   * chips move to the bottom corners so they don't clash with this. */
  left: 50%;
  right: auto;
  top: calc(54px + env(safe-area-inset-top, 0px));
  bottom: auto;
  width: min(82vw, 760px);
  transform: translateX(-50%);
  z-index: 12;
}

.speech-teleprompter--hud .speech-teleprompter-surface {
  padding: 13px 18px 10px;
  border-radius: 18px;
  background:
    linear-gradient(180deg, rgba(8, 8, 18, 0.48), rgba(8, 8, 18, 0.34));
}

.speech-teleprompter--hud .speech-teleprompter-line--prev {
  display: none;
}

.speech-teleprompter--hud .speech-teleprompter-line--current {
  font-size: clamp(1.05rem, 3.3vw, 1.85rem);
  line-height: 1.18;
}

.speech-teleprompter--hud .speech-teleprompter-line--next {
  margin-top: 5px;
  font-size: clamp(0.76rem, 2.2vw, 1.06rem);
  line-height: 1.16;
}

.speech-teleprompter--hud .speech-teleprompter-hint {
  margin-top: 6px;
  font-size: 0.58rem;
}

@media (max-width: 480px) {
  .speech-teleprompter--card {
    width: 94%;
  }
  .speech-teleprompter--card .speech-teleprompter-surface {
    padding: 13px 14px;
    border-radius: 18px;
  }
  .speech-teleprompter--hud {
    width: min(92vw, 460px);
    top: calc(50px + env(safe-area-inset-top, 0px));
    bottom: auto;
  }
  .speech-teleprompter--hud .speech-teleprompter-surface {
    padding: 11px 13px 9px;
  }
  .speech-teleprompter-hint {
    font-size: 0.54rem;
  }
}

@media (prefers-reduced-motion: reduce) {
  .speech-teleprompter-word {
    transition: none;
  }
}

@supports not ((backdrop-filter: blur(1px)) or (-webkit-backdrop-filter: blur(1px))) {
  .speech-teleprompter-surface {
    background: rgba(12, 14, 20, 0.82);
  }
}
