/**
 * Live Stream PiP
 * ===============
 * Floating "you are in a live stream" indicator. It defaults bottom-right
 * across the app, but live-room pages pin it bottom-left so it does not cover
 * the room chat.
 * Lives outside <main> so it persists across SPA navigation.
 *
 * Layout (bottom up):
 *   - card        — title, timer, controls (always visible)
 *   - overlay     — viewers + raised hands chips (above card, shown on hover)
 *   - chat stack  — last 3-4 chat messages with opacity fade (above overlay)
 */

#liveStreamPip {
  position: fixed;
  right: var(--spacing-md, 16px);
  bottom: var(--spacing-md, 16px);
  z-index: 9000;
  display: flex;
  flex-direction: column;
  align-items: stretch;
  gap: 6px;
  pointer-events: none;
  max-width: min(380px, calc(100vw - 32px));
}

main.stream-layout:has(.stream-room-page) ~ #liveStreamPip {
  right: auto;
  left: var(--spacing-md, 16px);
}

#liveStreamPip[hidden] {
  display: none !important;
}

/* ===========
   Chat stack
   =========== */

.live-pip-chat {
  pointer-events: auto;
  display: flex;
  flex-direction: column;
  gap: 3px;
  padding: 0 4px;
  transition: opacity 0.18s ease;
}

.live-pip-chat-msg {
  display: flex;
  align-items: center;
  gap: 8px;
  background: var(--color-bg-primary, #fff);
  border: 1px solid var(--color-border, #E5E5E5);
  border-radius: var(--radius-md, 6px);
  padding: 5px 10px 5px 6px;
  font-size: 12px;
  line-height: 1.35;
  color: var(--color-text-primary);
  box-shadow: 0 1px 2px rgba(0, 0, 0, 0.04);
  transition: opacity 0.2s ease, transform 0.2s ease;
  min-width: 0;
}

.live-pip-chat-avatar {
  width: 20px;
  height: 20px;
  border-radius: var(--radius-sm, 4px);
  object-fit: cover;
  flex-shrink: 0;
}

.live-pip-chat-avatar-fallback {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  background: var(--color-bg-tertiary, #EBEBEB);
  color: var(--color-text-secondary);
  font-size: 10px;
  font-weight: var(--font-weight-semibold, 600);
}

.live-pip-chat-body {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  min-width: 0;
  flex: 1 1 auto;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.live-pip-chat-meta {
  display: inline-flex;
  align-items: center;
  gap: 4px;
  margin-left: auto;
  padding-left: 4px;
  flex-shrink: 0;
}

.live-pip-chat-reaction {
  display: inline-flex;
  align-items: center;
  gap: 2px;
  padding: 1px 5px 1px 4px;
  background: var(--color-bg-secondary, #F5F5F5);
  border: 1px solid var(--color-border, #E5E5E5);
  border-radius: var(--radius-full, 9999px);
  font-size: 10px;
  line-height: 1;
  color: var(--color-text-secondary);
}

.live-pip-chat-reaction[data-active="true"] {
  background: color-mix(in srgb, var(--color-accent, #5C6F5B) 12%, #fff);
  border-color: color-mix(in srgb, var(--color-accent, #5C6F5B) 30%, transparent);
  color: var(--color-accent, #5C6F5B);
}

.live-pip-chat-reaction-emoji {
  font-size: 11px;
}

.live-pip-chat-reaction-count {
  font-variant-numeric: tabular-nums;
  font-weight: var(--font-weight-semibold, 600);
}

.live-pip-chat-reaction-more {
  padding: 1px 5px;
  font-weight: var(--font-weight-semibold, 600);
}

.live-pip-chat-replies {
  display: inline-flex;
  align-items: center;
  gap: 3px;
  padding: 2px 6px 2px 5px;
  background: transparent;
  border: 1px solid var(--color-border, #E5E5E5);
  border-radius: var(--radius-full, 9999px);
  color: var(--color-text-secondary);
  font-size: 10px;
  font-variant-numeric: tabular-nums;
  font-weight: var(--font-weight-medium, 500);
  cursor: pointer;
  font-family: inherit;
  line-height: 1;
}

.live-pip-chat-replies:hover {
  background: color-mix(in srgb, var(--color-accent, #5C6F5B) 10%, #fff);
  border-color: color-mix(in srgb, var(--color-accent, #5C6F5B) 30%, transparent);
  color: var(--color-accent, #5C6F5B);
}

.live-pip-chat-replies svg {
  flex-shrink: 0;
}

.live-pip-chat-msg[data-depth="0"] { opacity: 0.92; }
.live-pip-chat-msg[data-depth="1"] { opacity: 0.55; transform: scale(0.985); }
.live-pip-chat-msg[data-depth="2"] { opacity: 0.32; transform: scale(0.97); }
.live-pip-chat-msg[data-depth="3"] { opacity: 0.18; transform: scale(0.955); }

.live-pip-chat:hover .live-pip-chat-msg {
  opacity: 1;
  transform: none;
}

.live-pip-chat-author {
  font-weight: var(--font-weight-semibold, 600);
  color: var(--color-text-secondary);
  margin-right: 6px;
}

.live-pip-chat-text {
  color: var(--color-text-primary);
}

/* ===========
   Overlay (viewers / raised hands)
   =========== */

.live-pip-overlay {
  pointer-events: auto;
  display: flex;
  gap: 4px;
  justify-content: flex-end;
  padding: 0 4px;
  opacity: 0.7;
  transition: opacity 0.18s ease;
}

#liveStreamPip:hover .live-pip-overlay,
.live-pip-overlay:hover {
  opacity: 1;
}

.live-pip-chip {
  display: inline-flex;
  align-items: center;
  gap: 4px;
  padding: 3px 8px;
  background: var(--color-bg-primary, #fff);
  border: 1px solid var(--color-border, #E5E5E5);
  border-radius: var(--radius-full, 9999px);
  font-size: 11px;
  font-weight: var(--font-weight-semibold, 600);
  color: var(--color-text-secondary);
  font-variant-numeric: tabular-nums;
  box-shadow: 0 1px 2px rgba(0, 0, 0, 0.04);
}

.live-pip-chip svg {
  flex-shrink: 0;
}

.live-pip-chip[data-active="true"] {
  background: color-mix(in srgb, var(--color-accent, #5C6F5B) 12%, #fff);
  color: var(--color-accent, #5C6F5B);
  border-color: color-mix(in srgb, var(--color-accent, #5C6F5B) 30%, transparent);
}

.live-pip-chip-button {
  cursor: pointer;
  font: inherit;
}

.live-pip-chip-button:hover {
  background: var(--color-bg-hover, #EBEBEB);
}

.live-pip-chip-button[data-active="true"]:hover {
  background: color-mix(in srgb, var(--color-accent, #5C6F5B) 18%, #fff);
}

/* ===========
   Raised-hands popover (host only)
   =========== */

.live-pip-hands-popover {
  pointer-events: auto;
  background: var(--color-bg-primary, #fff);
  border: 1px solid var(--color-border, #E5E5E5);
  border-radius: var(--radius-lg, 8px);
  box-shadow: 0 12px 32px rgba(0, 0, 0, 0.18), 0 2px 6px rgba(0, 0, 0, 0.08);
  padding: 6px;
  font-size: 12px;
  max-width: 100%;
}

.live-pip-hands-popover-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  padding: 4px 6px 6px;
  color: var(--color-text-secondary);
  font-size: 11px;
  font-weight: var(--font-weight-semibold);
  letter-spacing: 0.04em;
  text-transform: uppercase;
}

.live-pip-hands-close {
  background: transparent;
  border: 0;
  color: var(--color-text-tertiary);
  cursor: pointer;
  font-size: 16px;
  line-height: 1;
  padding: 0 4px;
}

.live-pip-hands-close:hover {
  color: var(--color-text-primary);
}

.live-pip-hands-empty {
  padding: 8px;
  color: var(--color-text-tertiary);
  text-align: center;
}

.live-pip-hands-list {
  list-style: none;
  padding: 0;
  margin: 0;
  max-height: 220px;
  overflow-y: auto;
}

.live-pip-hand-row {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 6px;
  border-radius: var(--radius-md, 6px);
}

.live-pip-hand-row + .live-pip-hand-row {
  margin-top: 2px;
}

.live-pip-hand-row:hover {
  background: var(--color-bg-secondary, #F5F5F5);
}

.live-pip-hand-avatar {
  width: 22px;
  height: 22px;
  border-radius: var(--radius-sm, 4px);
  object-fit: cover;
  flex-shrink: 0;
}

.live-pip-hand-avatar-fallback {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  background: var(--color-bg-tertiary, #EBEBEB);
  color: var(--color-text-secondary);
  font-size: 11px;
  font-weight: var(--font-weight-semibold);
}

.live-pip-hand-name {
  flex: 1;
  min-width: 0;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  color: var(--color-text-primary);
}

.live-pip-hand-lower {
  background: transparent;
  border: 1px solid var(--color-border, #E5E5E5);
  border-radius: var(--radius-md, 6px);
  padding: 2px 8px;
  font-size: 11px;
  font-weight: var(--font-weight-medium, 500);
  color: var(--color-text-secondary);
  cursor: pointer;
}

.live-pip-hand-lower:hover {
  background: var(--color-bg-hover, #EBEBEB);
  color: var(--color-text-primary);
}

/* ===========
   Main card
   =========== */

.live-pip-card {
  pointer-events: auto;
  display: inline-flex;
  align-items: stretch;
  gap: 0;
  max-width: 100%;
  background: var(--color-bg-primary, #fff);
  border: 1px solid var(--color-border, #E5E5E5);
  border-radius: var(--radius-lg, 8px);
  box-shadow: 0 12px 32px rgba(0, 0, 0, 0.18), 0 2px 6px rgba(0, 0, 0, 0.08);
  font-family: inherit;
  font-size: var(--font-size-sm, 13px);
  color: var(--color-text-primary);
  overflow: hidden;
}

.live-pip-main {
  display: inline-flex;
  align-items: center;
  gap: var(--spacing-sm, 8px);
  padding: 10px 12px 10px 10px;
  background: transparent;
  border: 0;
  cursor: pointer;
  color: inherit;
  font: inherit;
  text-align: left;
  min-width: 0;
  flex: 1 1 auto;
  text-decoration: none;
}

.live-pip-main:hover {
  background: var(--color-bg-hover, #EBEBEB);
}

.live-pip-main:hover .live-pip-return-arrow {
  transform: translateX(-2px);
  opacity: 1;
}

.live-pip-return-arrow {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  color: var(--color-text-tertiary, #9a9a9a);
  opacity: 0.65;
  transition: transform 0.16s ease, opacity 0.16s ease;
  flex-shrink: 0;
}

.live-pip-dot {
  display: inline-block;
  width: 8px;
  height: 8px;
  border-radius: 50%;
  background: var(--color-danger, #8A6060);
  flex-shrink: 0;
  animation: live-pip-pulse 1.8s ease-in-out infinite;
}

.live-pip-card[data-mode="classroom"] .live-pip-dot {
  background: var(--color-accent, #5C6F5B);
  animation-name: live-pip-classroom-pulse;
}

@keyframes live-pip-pulse {
  0%, 100% { box-shadow: 0 0 0 0 color-mix(in srgb, var(--color-danger, #8A6060) 40%, transparent); }
  50%      { box-shadow: 0 0 0 6px color-mix(in srgb, var(--color-danger, #8A6060) 0%, transparent); }
}

@keyframes live-pip-classroom-pulse {
  0%, 100% { box-shadow: 0 0 0 0 color-mix(in srgb, var(--color-accent, #5C6F5B) 34%, transparent); }
  50%      { box-shadow: 0 0 0 6px color-mix(in srgb, var(--color-accent, #5C6F5B) 0%, transparent); }
}

.live-pip-text {
  display: flex;
  flex-direction: column;
  min-width: 0;
  gap: 1px;
}

.live-pip-eyebrow {
  font-size: 10px;
  font-weight: var(--font-weight-semibold, 600);
  letter-spacing: 0.08em;
  text-transform: uppercase;
  color: var(--color-danger, #8A6060);
  line-height: 1;
}

.live-pip-card[data-mode="classroom"] .live-pip-eyebrow {
  color: var(--color-accent, #5C6F5B);
}

.live-pip-title {
  font-size: var(--font-size-sm, 13px);
  font-weight: var(--font-weight-semibold, 600);
  line-height: var(--line-height-snug, 1.3);
  color: var(--color-text-primary);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  max-width: 200px;
}

.live-pip-timer {
  margin-left: auto;
  padding-left: var(--spacing-sm, 8px);
  font-variant-numeric: tabular-nums;
  font-size: 12px;
  color: var(--color-text-secondary);
  font-weight: var(--font-weight-medium, 500);
  flex-shrink: 0;
}

/* ===========
   Action buttons
   =========== */

.live-pip-actions {
  display: inline-flex;
  align-items: stretch;
  border-left: 1px solid var(--color-border, #E5E5E5);
}

.live-pip-icon-btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 36px;
  background: transparent;
  border: 0;
  color: var(--color-text-secondary);
  cursor: pointer;
  padding: 0;
}

.live-pip-icon-btn + .live-pip-icon-btn {
  border-left: 1px solid var(--color-border, #E5E5E5);
}

.live-pip-icon-btn:hover {
  background: var(--color-bg-hover, #EBEBEB);
  color: var(--color-text-primary);
}

.live-pip-icon-btn[data-active="false"] {
  color: var(--color-danger, #8A6060);
}

.live-pip-icon-btn[data-pip-hand][data-active="false"] {
  color: var(--color-text-secondary);
}

.live-pip-icon-btn[data-pip-hand][data-active="true"] {
  color: var(--color-accent, #5C6F5B);
  background: color-mix(in srgb, var(--color-accent, #5C6F5B) 10%, transparent);
}

.live-pip-icon-btn[data-pip-hand][data-active="true"]:hover {
  color: var(--color-accent, #5C6F5B);
  background: color-mix(in srgb, var(--color-accent, #5C6F5B) 16%, transparent);
}

.live-pip-icon-btn.live-pip-leave {
  color: var(--color-danger, #8A6060);
}

.live-pip-icon-btn.live-pip-leave:hover {
  background: color-mix(in srgb, var(--color-danger, #8A6060) 12%, transparent);
  color: var(--color-danger, #8A6060);
}

/* Two-click confirm armed: button turns solid red so the user knows the next
   click ends the broadcast. Pulses subtly to draw attention without being
   obnoxious. Disarms automatically after a few seconds. */
.live-pip-icon-btn.live-pip-leave[data-confirm-pending="true"] {
  background: var(--color-danger, #8A6060);
  color: #fff;
  animation: live-pip-leave-pulse 0.9s ease-in-out infinite;
}

.live-pip-icon-btn.live-pip-leave[data-confirm-pending="true"]:hover {
  background: var(--color-danger, #8A6060);
  color: #fff;
}

@keyframes live-pip-leave-pulse {
  0%, 100% { box-shadow: 0 0 0 0 color-mix(in srgb, var(--color-danger, #8A6060) 45%, transparent); }
  50% { box-shadow: 0 0 0 4px color-mix(in srgb, var(--color-danger, #8A6060) 0%, transparent); }
}

/* Same arming style on the page's "End event" header button. */
[data-end-event][data-confirm-pending="true"] {
  background: var(--color-danger, #8A6060) !important;
  color: #fff !important;
  border-color: var(--color-danger, #8A6060) !important;
  animation: live-pip-leave-pulse 0.9s ease-in-out infinite;
}

/* Pip stays visible on the stream-room and hosting pages too — it provides
   quick controls (mic, cam, screen, leave) without scrolling. */

@media (max-width: 540px) {
  #liveStreamPip {
    max-width: calc(100vw - 32px);
  }
  .live-pip-title {
    max-width: 130px;
  }
  .live-pip-timer {
    display: none;
  }
  .live-pip-overlay,
  .live-pip-chat {
    display: none; /* keep mobile compact — only show core card */
  }
}
