:root {
  color-scheme: dark;
  --bg-pitch: #000000;
  --surface-1: #09090b;
  --surface-2: #121216;
  --border-subtle: #222228;
  --border-strong: #34343e;
  --text-primary: #f5f5f7;
  --text-secondary: #9898a6;
  --accent-voice: #00ff66;
  --accent-danger: #ff2a4b;
  --accent-live: #00d4ff;
  --font-sans: -apple-system, BlinkMacSystemFont, 'SF Pro Display', 'Inter', system-ui, sans-serif;
}

*,
*::before,
*::after {
  box-sizing: border-box;
  margin: 0;
  padding: 0;
}

html,
body {
  width: 100%;
  height: 100%;
  background-color: var(--bg-pitch);
  color: var(--text-primary);
  font-family: var(--font-sans);
  overflow: hidden;
  user-select: none;
  -webkit-font-smoothing: antialiased;
}

[hidden] {
  display: none !important;
}

.app-shell {
  display: flex;
  flex-direction: column;
  width: 100%;
  height: 100dvh;
  background: #000000;
  position: relative;
}

/* --- Top Status Pill --- */
.top-bar {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  justify-content: center;
  gap: 8px;
  padding: max(14px, env(safe-area-inset-top)) 16px 8px;
  z-index: 20;
  flex-shrink: 0;
}

.status-pill {
  display: inline-flex;
  align-items: center;
  gap: 8px;
  padding: 6px 14px;
  background: var(--surface-1);
  border: 1px solid var(--border-subtle);
  border-radius: 999px;
  font-size: 12px;
  font-weight: 600;
  letter-spacing: 0.05em;
  color: var(--text-secondary);
}

.conn-dot {
  width: 8px;
  height: 8px;
  border-radius: 50%;
  background: #f59e0b;
}

.conn-dot[data-status="online"] {
  background: var(--accent-voice);
  box-shadow: 0 0 10px var(--accent-voice);
}

.conn-dot[data-status="reconnecting"] {
  background: var(--accent-danger);
}

.divider {
  color: #3a3a44;
}

.peer-counter {
  display: inline-flex;
  align-items: center;
  gap: 5px;
  color: var(--text-primary);
}

.wake-lock-badge {
  display: inline-flex;
  align-items: center;
  gap: 4px;
  color: #656572;
  font-size: 10px;
  font-weight: 800;
  letter-spacing: 0.06em;
  cursor: pointer;
  transition: color 0.15s ease;
}

.wake-lock-badge[data-active="true"] {
  color: var(--accent-voice);
}

.version-badge {
  color: #00d4ff;
  font-size: 11px;
  font-weight: 800;
}

.unlock-banner {
  display: inline-flex;
  align-items: center;
  gap: 7px;
  padding: 6px 14px;
  background: #14291b;
  color: var(--accent-voice);
  border: 1px solid var(--accent-voice);
  border-radius: 999px;
  font-size: 12px;
  font-weight: 600;
  cursor: pointer;
}

/* --- Main Stage --- */
.main-stage {
  flex: 1;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  padding: 12px 20px;
  gap: 18px;
  min-height: 0;
  overflow: hidden;
}

/* --- Avatar Stage (Centered South Park Avatars) --- */
.avatar-stage {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  justify-content: center;
  gap: 26px;
  max-width: 1120px;
  width: 100%;
  margin: auto;
}

.avatar-card {
  --voice-level: 0;
  --avatar-accent: #00ff66;
  position: relative;
  width: clamp(148px, 22vw, 210px);
  aspect-ratio: 1 / 1;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: transform 0.14s ease;
}

.avatar-card.is-self {
  cursor: pointer;
}

.avatar-card.is-self:hover .avatar-ring {
  border-color: #3f3f4a;
}

.avatar-ring {
  position: relative;
  width: 100%;
  height: 100%;
  border-radius: 50%;
  background: radial-gradient(circle at 50% 42%, #131318 0%, #050507 78%, #000000 100%);
  border: 2.5px solid #1e1e24;
  display: flex;
  align-items: center;
  justify-content: center;
  overflow: visible;
  transition: border-color 0.08s ease, box-shadow 0.08s ease, transform 0.08s ease;
}

.avatar-figure {
  width: 88%;
  height: 88%;
  border-radius: 50%;
  overflow: hidden;
  display: flex;
  align-items: flex-end;
  justify-content: center;
}

.sp-svg {
  width: 100%;
  height: 100%;
  display: block;
  overflow: visible;
}

/* Speaking illumination & pulse */
.avatar-card.is-speaking .avatar-ring {
  border-color: var(--accent-voice);
  box-shadow:
    0 0 calc(16px + var(--voice-level) * 34px) color-mix(in oklab, var(--accent-voice) 75%, transparent),
    0 0 calc(6px + var(--voice-level) * 18px) var(--avatar-accent),
    inset 0 0 22px color-mix(in oklab, var(--accent-voice) 25%, transparent);
  transform: scale(calc(1 + var(--voice-level) * 0.055));
}

.avatar-card.is-muted .avatar-figure {
  opacity: 0.62;
  filter: saturate(0.65);
}

.avatar-card.is-muted .avatar-ring {
  border-color: #2b1217;
}

.avatar-badges {
  position: absolute;
  bottom: -6px;
  left: 50%;
  transform: translateX(-50%);
  display: flex;
  align-items: center;
  gap: 5px;
  z-index: 4;
}

.badge {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  padding: 2px 8px;
  border-radius: 999px;
  font-size: 10px;
  font-weight: 800;
  letter-spacing: 0.06em;
  border: 1px solid #26262e;
  background: #0c0c0f;
  color: var(--text-secondary);
}

.badge-self {
  color: var(--text-primary);
  border-color: #3a3a46;
}

.badge-muted {
  background: #28090e;
  color: var(--accent-danger);
  border-color: var(--accent-danger);
  padding: 3px 6px;
}

.badge-live {
  background: #052630;
  color: var(--accent-live);
  border-color: var(--accent-live);
}

/* --- Screen Share Stage & 3 Viewer Sizes --- */
.screen-stage {
  display: flex;
  flex-direction: column;
  width: 100%;
  max-width: 1180px;
  flex: 1;
  min-height: 0;
  background: #050507;
  border: 1px solid var(--border-subtle);
  border-radius: 16px;
  overflow: hidden;
  transition: max-width 0.2s ease, max-height 0.2s ease;
}

.app-shell[data-viewer-size="compact"] .screen-stage {
  max-width: 680px;
  max-height: 46vh;
}

.app-shell[data-viewer-size="cinema"] .screen-stage {
  max-width: 1120px;
  max-height: 64vh;
}

.app-shell[data-viewer-size="full"] .screen-stage {
  max-width: 98vw;
  max-height: 76vh;
}

/* Dock avatars cleanly underneath when screen share is active */
.app-shell.has-screen-share .avatar-stage {
  flex-shrink: 0;
  margin: 0;
  gap: 14px;
}

.app-shell.has-screen-share .avatar-card {
  width: clamp(78px, 10vw, 108px);
}

.screen-toolbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 8px 14px;
  background: #0a0a0d;
  border-bottom: 1px solid #18181f;
  flex-shrink: 0;
}

.screen-meta {
  display: flex;
  align-items: center;
  gap: 8px;
}

.live-dot {
  width: 8px;
  height: 8px;
  border-radius: 50%;
  background: var(--accent-danger);
  box-shadow: 0 0 8px var(--accent-danger);
}

.screen-badge {
  font-size: 11px;
  font-weight: 700;
  padding: 3px 8px;
  border-radius: 6px;
  background: #14141a;
  color: var(--text-primary);
  border: 1px solid #262630;
}

.screen-badge-muted {
  color: var(--accent-voice);
  border-color: #193826;
  background: #09170f;
}

.screen-size-controls {
  display: flex;
  align-items: center;
  gap: 4px;
}

.size-btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  min-width: 28px;
  height: 26px;
  padding: 0 7px;
  border-radius: 6px;
  border: 1px solid var(--border-subtle);
  background: #111116;
  color: var(--text-secondary);
  font-size: 11px;
  font-weight: 700;
  cursor: pointer;
}

.size-btn:hover {
  color: var(--text-primary);
  border-color: var(--border-strong);
}

.size-btn.is-active {
  background: var(--text-primary);
  color: #000000;
  border-color: var(--text-primary);
}

.video-frame {
  flex: 1;
  min-height: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  background: #000000;
}

#screenVideo {
  width: 100%;
  height: 100%;
  object-fit: contain;
  background: #000000;
}

/* --- Bottom Control Dock --- */
.control-dock-wrap {
  display: flex;
  justify-content: center;
  padding: 8px 16px max(18px, env(safe-area-inset-bottom));
  z-index: 20;
  flex-shrink: 0;
}

.control-dock {
  display: flex;
   flex-wrap: wrap;
  align-items: center;
  justify-content: center;
  gap: 8px;
  padding: 8px;
  background: var(--surface-1);
  border: 1px solid var(--border-subtle);
  border-radius: 20px;
}

.dock-btn {
  display: inline-flex;
  align-items: center;
  gap: 8px;
  height: 44px;
  padding: 0 15px;
  border-radius: 13px;
  border: 1px solid var(--border-subtle);
  background: var(--surface-2);
  color: var(--text-primary);
  font-size: 13px;
  font-weight: 600;
  cursor: pointer;
  transition: background-color 0.14s ease, border-color 0.14s ease, transform 0.1s ease;
}

.dock-btn:hover {
  background: #1a1a20;
  border-color: var(--border-strong);
}

.dock-btn:active {
  transform: scale(0.97);
}

/* Microphone states */
.dock-btn-mic .icon-mic-off {
  display: none;
}

.dock-btn-mic.is-muted {
  background: #24090e;
  border-color: var(--accent-danger);
  color: var(--accent-danger);
}

.dock-btn-mic.is-muted .icon-mic-on {
  display: none;
}

.dock-btn-mic.is-muted .icon-mic-off {
  display: inline-flex;
}

/* Screen Share + 3 Presets Group */
.share-control-group {
  display: inline-flex;
  align-items: center;
  background: #0d0d11;
  border: 1px solid var(--border-subtle);
  border-radius: 14px;
  padding: 3px;
  gap: 4px;
}

.preset-selector {
  display: inline-flex;
  align-items: center;
  gap: 2px;
  padding: 0 4px;
}

.preset-btn {
  height: 34px;
  padding: 0 10px;
  border-radius: 9px;
  border: 1px solid transparent;
  background: transparent;
  color: var(--text-secondary);
  font-size: 12px;
  font-weight: 700;
  cursor: pointer;
  transition: background-color 0.12s ease, color 0.12s ease;
}

.preset-btn:hover {
  color: var(--text-primary);
}

.preset-btn.is-active {
  background: #202028;
  color: var(--accent-voice);
  border-color: #2f3b33;
}

.fps-tag {
  font-size: 10px;
  font-weight: 800;
  color: #6c6c7a;
  padding: 0 6px 0 3px;
  letter-spacing: 0.03em;
}

.dock-btn-share {
  height: 36px;
  border-radius: 10px;
}

.dock-btn-share.is-sharing {
  background: #062814;
  border-color: var(--accent-voice);
  color: var(--accent-voice);
}

/* PWA Shortcut Button */
.dock-btn-pwa.is-ready {
  border-color: var(--accent-voice);
  color: var(--accent-voice);
}

/* --- PWA Modal Dialog (Modern Web Guidance Top-Layer Animations) --- */
dialog.pwa-dialog {
  margin: auto;
  width: min(92vw, 440px);
  background: #0b0b0e;
  color: var(--text-primary);
  border: 1px solid var(--border-strong);
  border-radius: 18px;
  padding: 0;
  opacity: 0;
  transform: scale(0.92);
  transition-property: opacity, transform, display, overlay;
  transition-duration: 0.24s;
  transition-timing-function: ease-out;
  transition-behavior: allow-discrete;
}

dialog.pwa-dialog::backdrop {
  background-color: rgb(0 0 0 / 0);
  backdrop-filter: blur(4px);
  transition-property: background-color, display, overlay;
  transition-duration: 0.24s;
  transition-timing-function: ease-out;
  transition-behavior: allow-discrete;
}

:is(dialog.pwa-dialog[open]):where(:not([data-closing])) {
  opacity: 1;
  transform: scale(1);

  @starting-style {
    opacity: 0;
    transform: scale(0.92);
  }

  &::backdrop {
    background-color: rgb(0 0 0 / 0.76);

    @starting-style {
      background-color: rgb(0 0 0 / 0);
    }
  }
}

.pwa-dialog-inner {
  padding: 20px;
}

.pwa-dialog-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 14px;
}

.pwa-dialog-header h2 {
  font-size: 16px;
  font-weight: 700;
}

.dialog-close-btn {
  width: 30px;
  height: 30px;
  border-radius: 8px;
  border: 1px solid var(--border-subtle);
  background: var(--surface-2);
  color: var(--text-secondary);
  cursor: pointer;
}

.pwa-dialog-body {
  display: flex;
  flex-direction: column;
  gap: 12px;
  font-size: 13px;
  line-height: 1.45;
  color: var(--text-secondary);
}

.pwa-step {
  padding: 10px 12px;
  border-radius: 10px;
  background: #121217;
  border: 1px solid #1e1e26;
}

.pwa-step strong {
  display: block;
  color: var(--text-primary);
  margin-bottom: 3px;
}

/* Accessibility: Reduced motion & Forced colors */
@media (prefers-reduced-motion: reduce) {
  dialog.pwa-dialog {
    transform: none;
    transition-duration: 0.08s;
  }

  @starting-style {
    dialog.pwa-dialog[open] {
      transform: none;
    }
  }

  .avatar-card,
  .avatar-ring {
    transition: none;
  }
}

@media (forced-colors: active) {
  .avatar-card.is-speaking .avatar-ring {
    outline: 3px solid Highlight;
  }

  .dock-btn,
  .preset-btn.is-active,
  .size-btn.is-active {
    border: 1px solid ButtonText;
  }
}

/* Mobile adjustments */
@media (max-width: 640px) {
  .avatar-stage {
    gap: 16px;
  }

  .avatar-card {
    width: clamp(120px, 38vw, 156px);
  }

  .dock-label {
    font-size: 12px;
  }

  .dock-btn {
    height: 40px;
    padding: 0 11px;
  }
}
