content: sub init()
      m.loginScreen = m.top.findNode("loginScreen")
      m.homeScreen = m.top.findNode("homeScreen")
      m.videoPlayer = m.top.findNode("videoPlayer")
      m.putioAPI = m.top.findNode("putioAPI")
      
      ' Set up observers
      m.loginScreen.observeField("authComplete", "onAuthComplete")
      m.homeScreen.observeField("playVideo", "onPlayVideo")
      m.homeScreen.observeField("logout", "onLogout")
      m.videoPlayer.observeField("finished", "onVideoFinished")
      
      ' Check for existing auth token
      checkExistingAuth()
      
      ' Handle deep linking
      m.top.observeField("contentId", "onContentId")
  end sub
  
  sub checkExistingAuth()
      ' Check registry for stored access token
      registry = CreateObject("roRegistry")
      section = CreateObject("roRegistrySection", "putio_auth")
      accessToken = section.read("access_token")
      
      if accessToken <> invalid and accessToken <> ""
          m.top.accessToken = accessToken
          m.putioAPI.accessToken = accessToken
          m.putioAPI.validateToken = true
      end if
  end sub
  
  sub onAuthComplete(event as Object)
      authData = event.getData()
      if authData.success
          m.top.accessToken = authData.accessToken
          m.top.currentUser = authData.user
          
          ' Store token in registry
          registry = CreateObject("roRegistry")
          section = CreateObject("roRegistrySection", "putio_auth")
          section.write("access_token", authData.accessToken)
          section.flush()
          
          ' Switch to home screen
          showHomeScreen()
      else
          print "Authentication failed: "; authData.error
      end if
  end sub
  
  sub onPlayVideo(event as Object)
      videoData = event.getData()
      m.videoPlayer.videoContent = videoData
      m.videoPlayer.visible = true
      m.homeScreen.visible = false
      m.videoPlayer.setFocus(true)
  end sub
  
  sub onVideoFinished(event as Object)
      m.videoPlayer.visible = false
      m.homeScreen.visible = true
      m.homeScreen.setFocus(true)
  end sub
  
  sub onLogout(event as Object)
      ' Clear stored auth
      registry = CreateObject("roRegistry")
      section = CreateObject("roRegistrySection", "putio_auth")
      section.delete("access_token")
      section.flush()
      
      ' Reset state
      m.top.accessToken = ""
      m.top.currentUser = invalid
      
      ' Show login screen
      showLoginScreen()
  end sub
  
  sub onContentId(event as Object)
      contentId = event.getData()
      if contentId <> invalid and m.top.accessToken <> ""
          ' Handle deep link to specific content
          m.homeScreen.playContentId = contentId
      end if
  end sub
  
  sub showLoginScreen()
      m.loginScreen.visible = true
      m.homeScreen.visible = false
      m.videoPlayer.visible = false
      m.loginScreen.setFocus(true)
  end sub
  
  sub showHomeScreen()
      m.loginScreen.visible = false
      m.homeScreen.visible = true
      m.videoPlayer.visible = false
      m.homeScreen.accessToken = m.top.accessToken
      m.homeScreen.setFocus(true)
  end sub
  
  function onKeyEvent(key as String, press as Boolean) as Boolean
      handled = false
      
      if press
          if key = "back"
              if m.videoPlayer.visible
                  onVideoFinished(invalid)
                  handled = true
              else if m.homeScreen.visible
                  ' Show exit dialog
                  handled = true
              end if
          end if
      end if
      
      return handled
  end function
  